Update stream.js
This commit is contained in:
parent
1a4c21eacf
commit
8b5e1b644a
1 changed files with 21 additions and 5 deletions
26
node_modules/ws/lib/stream.js
generated
vendored
26
node_modules/ws/lib/stream.js
generated
vendored
|
@ -5,7 +5,7 @@ const { Duplex } = require('stream');
|
||||||
/**
|
/**
|
||||||
* Emits the `'close'` event on a stream.
|
* Emits the `'close'` event on a stream.
|
||||||
*
|
*
|
||||||
* @param {stream.Duplex} The stream.
|
* @param {Duplex} stream The stream.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function emitClose(stream) {
|
function emitClose(stream) {
|
||||||
|
@ -26,6 +26,7 @@ function duplexOnEnd() {
|
||||||
/**
|
/**
|
||||||
* The listener of the `'error'` event.
|
* The listener of the `'error'` event.
|
||||||
*
|
*
|
||||||
|
* @param {Error} err The error
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function duplexOnError(err) {
|
function duplexOnError(err) {
|
||||||
|
@ -41,12 +42,13 @@ function duplexOnError(err) {
|
||||||
* Wraps a `WebSocket` in a duplex stream.
|
* Wraps a `WebSocket` in a duplex stream.
|
||||||
*
|
*
|
||||||
* @param {WebSocket} ws The `WebSocket` to wrap
|
* @param {WebSocket} ws The `WebSocket` to wrap
|
||||||
* @param {Object} options The options for the `Duplex` constructor
|
* @param {Object} [options] The options for the `Duplex` constructor
|
||||||
* @return {stream.Duplex} The duplex stream
|
* @return {Duplex} The duplex stream
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
function createWebSocketStream(ws, options) {
|
function createWebSocketStream(ws, options) {
|
||||||
let resumeOnReceiverDrain = true;
|
let resumeOnReceiverDrain = true;
|
||||||
|
let terminateOnDestroy = true;
|
||||||
|
|
||||||
function receiverOnDrain() {
|
function receiverOnDrain() {
|
||||||
if (resumeOnReceiverDrain) ws._socket.resume();
|
if (resumeOnReceiverDrain) ws._socket.resume();
|
||||||
|
@ -80,6 +82,16 @@ function createWebSocketStream(ws, options) {
|
||||||
ws.once('error', function error(err) {
|
ws.once('error', function error(err) {
|
||||||
if (duplex.destroyed) return;
|
if (duplex.destroyed) return;
|
||||||
|
|
||||||
|
// Prevent `ws.terminate()` from being called by `duplex._destroy()`.
|
||||||
|
//
|
||||||
|
// - If the `'error'` event is emitted before the `'open'` event, then
|
||||||
|
// `ws.terminate()` is a noop as no socket is assigned.
|
||||||
|
// - Otherwise, the error is re-emitted by the listener of the `'error'`
|
||||||
|
// event of the `Receiver` object. The listener already closes the
|
||||||
|
// connection by calling `ws.close()`. This allows a close frame to be
|
||||||
|
// sent to the other peer. If `ws.terminate()` is called right after this,
|
||||||
|
// then the close frame might not be sent.
|
||||||
|
terminateOnDestroy = false;
|
||||||
duplex.destroy(err);
|
duplex.destroy(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -107,7 +119,8 @@ function createWebSocketStream(ws, options) {
|
||||||
if (!called) callback(err);
|
if (!called) callback(err);
|
||||||
process.nextTick(emitClose, duplex);
|
process.nextTick(emitClose, duplex);
|
||||||
});
|
});
|
||||||
ws.terminate();
|
|
||||||
|
if (terminateOnDestroy) ws.terminate();
|
||||||
};
|
};
|
||||||
|
|
||||||
duplex._final = function (callback) {
|
duplex._final = function (callback) {
|
||||||
|
@ -139,7 +152,10 @@ function createWebSocketStream(ws, options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
duplex._read = function () {
|
duplex._read = function () {
|
||||||
if (ws.readyState === ws.OPEN && !resumeOnReceiverDrain) {
|
if (
|
||||||
|
(ws.readyState === ws.OPEN || ws.readyState === ws.CLOSING) &&
|
||||||
|
!resumeOnReceiverDrain
|
||||||
|
) {
|
||||||
resumeOnReceiverDrain = true;
|
resumeOnReceiverDrain = true;
|
||||||
if (!ws._receiver._writableState.needDrain) ws._socket.resume();
|
if (!ws._receiver._writableState.needDrain) ws._socket.resume();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue