update node modules
This commit is contained in:
252
node_modules/ws/lib/websocket.js
generated
vendored
252
node_modules/ws/lib/websocket.js
generated
vendored
@@ -1,16 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const EventEmitter = require('events');
|
||||
const crypto = require('crypto');
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
const net = require('net');
|
||||
const tls = require('tls');
|
||||
const url = require('url');
|
||||
const { randomBytes, createHash } = require('crypto');
|
||||
const { URL } = require('url');
|
||||
|
||||
const PerMessageDeflate = require('./permessage-deflate');
|
||||
const EventTarget = require('./event-target');
|
||||
const extension = require('./extension');
|
||||
const Receiver = require('./receiver');
|
||||
const Sender = require('./sender');
|
||||
const {
|
||||
@@ -21,6 +19,9 @@ const {
|
||||
kWebSocket,
|
||||
NOOP
|
||||
} = require('./constants');
|
||||
const { addEventListener, removeEventListener } = require('./event-target');
|
||||
const { format, parse } = require('./extension');
|
||||
const { toBuffer } = require('./buffer-util');
|
||||
|
||||
const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
|
||||
const protocolVersions = [8, 13];
|
||||
@@ -35,7 +36,7 @@ class WebSocket extends EventEmitter {
|
||||
/**
|
||||
* Create a new `WebSocket`.
|
||||
*
|
||||
* @param {(String|url.Url|url.URL)} address The URL to which to connect
|
||||
* @param {(String|url.URL)} address The URL to which to connect
|
||||
* @param {(String|String[])} protocols The subprotocols
|
||||
* @param {Object} options Connection options
|
||||
*/
|
||||
@@ -57,6 +58,7 @@ class WebSocket extends EventEmitter {
|
||||
this._socket = null;
|
||||
|
||||
if (address !== null) {
|
||||
this._bufferedAmount = 0;
|
||||
this._isServer = false;
|
||||
this._redirects = 0;
|
||||
|
||||
@@ -112,12 +114,9 @@ class WebSocket extends EventEmitter {
|
||||
* @type {Number}
|
||||
*/
|
||||
get bufferedAmount() {
|
||||
if (!this._socket) return 0;
|
||||
if (!this._socket) return this._bufferedAmount;
|
||||
|
||||
//
|
||||
// `socket.bufferSize` is `undefined` if the socket is closed.
|
||||
//
|
||||
return (this._socket.bufferSize || 0) + this._sender._bufferedBytes;
|
||||
return this._socket._writableState.length + this._sender._bufferedBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,6 +138,7 @@ class WebSocket extends EventEmitter {
|
||||
const receiver = new Receiver(
|
||||
this._binaryType,
|
||||
this._extensions,
|
||||
this._isServer,
|
||||
maxPayload
|
||||
);
|
||||
|
||||
@@ -176,9 +176,8 @@ class WebSocket extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
emitClose() {
|
||||
this.readyState = WebSocket.CLOSED;
|
||||
|
||||
if (!this._socket) {
|
||||
this.readyState = WebSocket.CLOSED;
|
||||
this.emit('close', this._closeCode, this._closeMessage);
|
||||
return;
|
||||
}
|
||||
@@ -188,6 +187,7 @@ class WebSocket extends EventEmitter {
|
||||
}
|
||||
|
||||
this._receiver.removeAllListeners();
|
||||
this.readyState = WebSocket.CLOSED;
|
||||
this.emit('close', this._closeCode, this._closeMessage);
|
||||
}
|
||||
|
||||
@@ -252,6 +252,10 @@ class WebSocket extends EventEmitter {
|
||||
* @public
|
||||
*/
|
||||
ping(data, mask, cb) {
|
||||
if (this.readyState === WebSocket.CONNECTING) {
|
||||
throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
|
||||
}
|
||||
|
||||
if (typeof data === 'function') {
|
||||
cb = data;
|
||||
data = mask = undefined;
|
||||
@@ -260,17 +264,13 @@ class WebSocket extends EventEmitter {
|
||||
mask = undefined;
|
||||
}
|
||||
|
||||
if (this.readyState !== WebSocket.OPEN) {
|
||||
const err = new Error(
|
||||
`WebSocket is not open: readyState ${this.readyState} ` +
|
||||
`(${readyStates[this.readyState]})`
|
||||
);
|
||||
if (typeof data === 'number') data = data.toString();
|
||||
|
||||
if (cb) return cb(err);
|
||||
throw err;
|
||||
if (this.readyState !== WebSocket.OPEN) {
|
||||
sendAfterClose(this, data, cb);
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof data === 'number') data = data.toString();
|
||||
if (mask === undefined) mask = !this._isServer;
|
||||
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
||||
}
|
||||
@@ -284,6 +284,10 @@ class WebSocket extends EventEmitter {
|
||||
* @public
|
||||
*/
|
||||
pong(data, mask, cb) {
|
||||
if (this.readyState === WebSocket.CONNECTING) {
|
||||
throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
|
||||
}
|
||||
|
||||
if (typeof data === 'function') {
|
||||
cb = data;
|
||||
data = mask = undefined;
|
||||
@@ -292,17 +296,13 @@ class WebSocket extends EventEmitter {
|
||||
mask = undefined;
|
||||
}
|
||||
|
||||
if (this.readyState !== WebSocket.OPEN) {
|
||||
const err = new Error(
|
||||
`WebSocket is not open: readyState ${this.readyState} ` +
|
||||
`(${readyStates[this.readyState]})`
|
||||
);
|
||||
if (typeof data === 'number') data = data.toString();
|
||||
|
||||
if (cb) return cb(err);
|
||||
throw err;
|
||||
if (this.readyState !== WebSocket.OPEN) {
|
||||
sendAfterClose(this, data, cb);
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof data === 'number') data = data.toString();
|
||||
if (mask === undefined) mask = !this._isServer;
|
||||
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
||||
}
|
||||
@@ -312,7 +312,8 @@ class WebSocket extends EventEmitter {
|
||||
*
|
||||
* @param {*} data The message to send
|
||||
* @param {Object} options Options object
|
||||
* @param {Boolean} options.compress Specifies whether or not to compress `data`
|
||||
* @param {Boolean} options.compress Specifies whether or not to compress
|
||||
* `data`
|
||||
* @param {Boolean} options.binary Specifies whether `data` is binary or text
|
||||
* @param {Boolean} options.fin Specifies whether the fragment is the last one
|
||||
* @param {Boolean} options.mask Specifies whether or not to mask `data`
|
||||
@@ -320,32 +321,29 @@ class WebSocket extends EventEmitter {
|
||||
* @public
|
||||
*/
|
||||
send(data, options, cb) {
|
||||
if (this.readyState === WebSocket.CONNECTING) {
|
||||
throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
|
||||
}
|
||||
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
if (this.readyState !== WebSocket.OPEN) {
|
||||
const err = new Error(
|
||||
`WebSocket is not open: readyState ${this.readyState} ` +
|
||||
`(${readyStates[this.readyState]})`
|
||||
);
|
||||
|
||||
if (cb) return cb(err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (typeof data === 'number') data = data.toString();
|
||||
|
||||
const opts = Object.assign(
|
||||
{
|
||||
binary: typeof data !== 'string',
|
||||
mask: !this._isServer,
|
||||
compress: true,
|
||||
fin: true
|
||||
},
|
||||
options
|
||||
);
|
||||
if (this.readyState !== WebSocket.OPEN) {
|
||||
sendAfterClose(this, data, cb);
|
||||
return;
|
||||
}
|
||||
|
||||
const opts = {
|
||||
binary: typeof data !== 'string',
|
||||
mask: !this._isServer,
|
||||
compress: true,
|
||||
fin: true,
|
||||
...options
|
||||
};
|
||||
|
||||
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
||||
opts.compress = false;
|
||||
@@ -391,7 +389,7 @@ readyStates.forEach((readyState, i) => {
|
||||
*/
|
||||
get() {
|
||||
const listeners = this.listeners(method);
|
||||
for (var i = 0; i < listeners.length; i++) {
|
||||
for (let i = 0; i < listeners.length; i++) {
|
||||
if (listeners[i]._listener) return listeners[i]._listener;
|
||||
}
|
||||
|
||||
@@ -405,7 +403,7 @@ readyStates.forEach((readyState, i) => {
|
||||
*/
|
||||
set(listener) {
|
||||
const listeners = this.listeners(method);
|
||||
for (var i = 0; i < listeners.length; i++) {
|
||||
for (let i = 0; i < listeners.length; i++) {
|
||||
//
|
||||
// Remove only the listeners added via `addEventListener`.
|
||||
//
|
||||
@@ -416,8 +414,8 @@ readyStates.forEach((readyState, i) => {
|
||||
});
|
||||
});
|
||||
|
||||
WebSocket.prototype.addEventListener = EventTarget.addEventListener;
|
||||
WebSocket.prototype.removeEventListener = EventTarget.removeEventListener;
|
||||
WebSocket.prototype.addEventListener = addEventListener;
|
||||
WebSocket.prototype.removeEventListener = removeEventListener;
|
||||
|
||||
module.exports = WebSocket;
|
||||
|
||||
@@ -425,7 +423,7 @@ module.exports = WebSocket;
|
||||
* Initialize a WebSocket client.
|
||||
*
|
||||
* @param {WebSocket} websocket The client to initialize
|
||||
* @param {(String|url.Url|url.URL)} address The URL to which to connect
|
||||
* @param {(String|url.URL)} address The URL to which to connect
|
||||
* @param {String} protocols The subprotocols
|
||||
* @param {Object} options Connection options
|
||||
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable
|
||||
@@ -442,28 +440,23 @@ module.exports = WebSocket;
|
||||
* @private
|
||||
*/
|
||||
function initAsClient(websocket, address, protocols, options) {
|
||||
const opts = Object.assign(
|
||||
{
|
||||
protocolVersion: protocolVersions[1],
|
||||
maxPayload: 100 * 1024 * 1024,
|
||||
perMessageDeflate: true,
|
||||
followRedirects: false,
|
||||
maxRedirects: 10
|
||||
},
|
||||
options,
|
||||
{
|
||||
createConnection: undefined,
|
||||
socketPath: undefined,
|
||||
hostname: undefined,
|
||||
protocol: undefined,
|
||||
timeout: undefined,
|
||||
method: undefined,
|
||||
auth: undefined,
|
||||
host: undefined,
|
||||
path: undefined,
|
||||
port: undefined
|
||||
}
|
||||
);
|
||||
const opts = {
|
||||
protocolVersion: protocolVersions[1],
|
||||
maxPayload: 100 * 1024 * 1024,
|
||||
perMessageDeflate: true,
|
||||
followRedirects: false,
|
||||
maxRedirects: 10,
|
||||
...options,
|
||||
createConnection: undefined,
|
||||
socketPath: undefined,
|
||||
hostname: undefined,
|
||||
protocol: undefined,
|
||||
timeout: undefined,
|
||||
method: undefined,
|
||||
host: undefined,
|
||||
path: undefined,
|
||||
port: undefined
|
||||
};
|
||||
|
||||
if (!protocolVersions.includes(opts.protocolVersion)) {
|
||||
throw new RangeError(
|
||||
@@ -472,16 +465,13 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
);
|
||||
}
|
||||
|
||||
var parsedUrl;
|
||||
let parsedUrl;
|
||||
|
||||
if (typeof address === 'object' && address.href !== undefined) {
|
||||
if (address instanceof URL) {
|
||||
parsedUrl = address;
|
||||
websocket.url = address.href;
|
||||
} else {
|
||||
//
|
||||
// The WHATWG URL constructor is not available on Node.js < 6.13.0
|
||||
//
|
||||
parsedUrl = url.URL ? new url.URL(address) : url.parse(address);
|
||||
parsedUrl = new URL(address);
|
||||
websocket.url = address;
|
||||
}
|
||||
|
||||
@@ -494,12 +484,9 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
const isSecure =
|
||||
parsedUrl.protocol === 'wss:' || parsedUrl.protocol === 'https:';
|
||||
const defaultPort = isSecure ? 443 : 80;
|
||||
const key = crypto.randomBytes(16).toString('base64');
|
||||
const key = randomBytes(16).toString('base64');
|
||||
const get = isSecure ? https.get : http.get;
|
||||
const path = parsedUrl.search
|
||||
? `${parsedUrl.pathname || '/'}${parsedUrl.search}`
|
||||
: parsedUrl.pathname || '/';
|
||||
var perMessageDeflate;
|
||||
let perMessageDeflate;
|
||||
|
||||
opts.createConnection = isSecure ? tlsConnect : netConnect;
|
||||
opts.defaultPort = opts.defaultPort || defaultPort;
|
||||
@@ -507,16 +494,14 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
opts.host = parsedUrl.hostname.startsWith('[')
|
||||
? parsedUrl.hostname.slice(1, -1)
|
||||
: parsedUrl.hostname;
|
||||
opts.headers = Object.assign(
|
||||
{
|
||||
'Sec-WebSocket-Version': opts.protocolVersion,
|
||||
'Sec-WebSocket-Key': key,
|
||||
Connection: 'Upgrade',
|
||||
Upgrade: 'websocket'
|
||||
},
|
||||
opts.headers
|
||||
);
|
||||
opts.path = path;
|
||||
opts.headers = {
|
||||
'Sec-WebSocket-Version': opts.protocolVersion,
|
||||
'Sec-WebSocket-Key': key,
|
||||
Connection: 'Upgrade',
|
||||
Upgrade: 'websocket',
|
||||
...opts.headers
|
||||
};
|
||||
opts.path = parsedUrl.pathname + parsedUrl.search;
|
||||
opts.timeout = opts.handshakeTimeout;
|
||||
|
||||
if (opts.perMessageDeflate) {
|
||||
@@ -525,7 +510,7 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
false,
|
||||
opts.maxPayload
|
||||
);
|
||||
opts.headers['Sec-WebSocket-Extensions'] = extension.format({
|
||||
opts.headers['Sec-WebSocket-Extensions'] = format({
|
||||
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
||||
});
|
||||
}
|
||||
@@ -539,20 +524,18 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
opts.headers.Origin = opts.origin;
|
||||
}
|
||||
}
|
||||
if (parsedUrl.auth) {
|
||||
opts.auth = parsedUrl.auth;
|
||||
} else if (parsedUrl.username || parsedUrl.password) {
|
||||
if (parsedUrl.username || parsedUrl.password) {
|
||||
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
||||
}
|
||||
|
||||
if (isUnixSocket) {
|
||||
const parts = path.split(':');
|
||||
const parts = opts.path.split(':');
|
||||
|
||||
opts.socketPath = parts[0];
|
||||
opts.path = parts[1];
|
||||
}
|
||||
|
||||
var req = (websocket._req = get(opts));
|
||||
let req = (websocket._req = get(opts));
|
||||
|
||||
if (opts.timeout) {
|
||||
req.on('timeout', () => {
|
||||
@@ -586,9 +569,7 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
|
||||
req.abort();
|
||||
|
||||
const addr = url.URL
|
||||
? new url.URL(location, address)
|
||||
: url.resolve(address, location);
|
||||
const addr = new URL(location, address);
|
||||
|
||||
initAsClient(websocket, addr, protocols, options);
|
||||
} else if (!websocket.emit('unexpected-response', req, res)) {
|
||||
@@ -611,8 +592,7 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
|
||||
req = websocket._req = null;
|
||||
|
||||
const digest = crypto
|
||||
.createHash('sha1')
|
||||
const digest = createHash('sha1')
|
||||
.update(key + GUID)
|
||||
.digest('base64');
|
||||
|
||||
@@ -623,7 +603,7 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
|
||||
const serverProt = res.headers['sec-websocket-protocol'];
|
||||
const protList = (protocols || '').split(/, */);
|
||||
var protError;
|
||||
let protError;
|
||||
|
||||
if (!protocols && serverProt) {
|
||||
protError = 'Server sent a subprotocol but none was requested';
|
||||
@@ -642,9 +622,7 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
|
||||
if (perMessageDeflate) {
|
||||
try {
|
||||
const extensions = extension.parse(
|
||||
res.headers['sec-websocket-extensions']
|
||||
);
|
||||
const extensions = parse(res.headers['sec-websocket-extensions']);
|
||||
|
||||
if (extensions[PerMessageDeflate.extensionName]) {
|
||||
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
||||
@@ -674,13 +652,7 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
* @private
|
||||
*/
|
||||
function netConnect(options) {
|
||||
//
|
||||
// Override `options.path` only if `options` is a copy of the original options
|
||||
// object. This is always true on Node.js >= 8 but not on Node.js 6 where
|
||||
// `options.socketPath` might be `undefined` even if the `socketPath` option
|
||||
// was originally set.
|
||||
//
|
||||
if (options.protocolVersion) options.path = options.socketPath;
|
||||
options.path = options.socketPath;
|
||||
return net.connect(options);
|
||||
}
|
||||
|
||||
@@ -693,7 +665,11 @@ function netConnect(options) {
|
||||
*/
|
||||
function tlsConnect(options) {
|
||||
options.path = undefined;
|
||||
options.servername = options.servername || options.host;
|
||||
|
||||
if (!options.servername && options.servername !== '') {
|
||||
options.servername = options.host;
|
||||
}
|
||||
|
||||
return tls.connect(options);
|
||||
}
|
||||
|
||||
@@ -723,6 +699,38 @@ function abortHandshake(websocket, stream, message) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle cases where the `ping()`, `pong()`, or `send()` methods are called
|
||||
* when the `readyState` attribute is `CLOSING` or `CLOSED`.
|
||||
*
|
||||
* @param {WebSocket} websocket The WebSocket instance
|
||||
* @param {*} data The data to send
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
function sendAfterClose(websocket, data, cb) {
|
||||
if (data) {
|
||||
const length = toBuffer(data).length;
|
||||
|
||||
//
|
||||
// The `_bufferedAmount` property is used only when the peer is a client and
|
||||
// the opening handshake fails. Under these circumstances, in fact, the
|
||||
// `setSocket()` method is not called, so the `_socket` and `_sender`
|
||||
// properties are set to `null`.
|
||||
//
|
||||
if (websocket._socket) websocket._sender._bufferedBytes += length;
|
||||
else websocket._bufferedAmount += length;
|
||||
}
|
||||
|
||||
if (cb) {
|
||||
const err = new Error(
|
||||
`WebSocket is not open: readyState ${websocket.readyState} ` +
|
||||
`(${readyStates[websocket.readyState]})`
|
||||
);
|
||||
cb(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The listener of the `Receiver` `'conclude'` event.
|
||||
*
|
||||
@@ -890,6 +898,8 @@ function socketOnError() {
|
||||
this.removeListener('error', socketOnError);
|
||||
this.on('error', NOOP);
|
||||
|
||||
websocket.readyState = WebSocket.CLOSING;
|
||||
this.destroy();
|
||||
if (websocket) {
|
||||
websocket.readyState = WebSocket.CLOSING;
|
||||
this.destroy();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user