update node

This commit is contained in:
olevole
2021-10-17 01:15:13 +03:00
parent 35782af048
commit a268022aca
27 changed files with 795 additions and 1428 deletions

374
node/node_modules/ws/lib/websocket.js generated vendored
View File

@@ -11,14 +11,20 @@ const url = require('url');
const PerMessageDeflate = require('./permessage-deflate');
const EventTarget = require('./event-target');
const extension = require('./extension');
const constants = require('./constants');
const Receiver = require('./receiver');
const Sender = require('./sender');
const {
BINARY_TYPES,
EMPTY_BUFFER,
GUID,
kStatusCode,
kWebSocket,
NOOP
} = require('./constants');
const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
const kWebSocket = constants.kWebSocket;
const protocolVersions = [8, 13];
const closeTimeout = 30 * 1000; // Allow 30 seconds to terminate the connection cleanly.
const closeTimeout = 30 * 1000;
/**
* Class representing a WebSocket.
@@ -33,25 +39,27 @@ class WebSocket extends EventEmitter {
* @param {(String|String[])} protocols The subprotocols
* @param {Object} options Connection options
*/
constructor (address, protocols, options) {
constructor(address, protocols, options) {
super();
this.readyState = WebSocket.CONNECTING;
this.protocol = '';
this._binaryType = constants.BINARY_TYPES[0];
this._binaryType = BINARY_TYPES[0];
this._closeFrameReceived = false;
this._closeFrameSent = false;
this._closeMessage = '';
this._closeTimer = null;
this._closeCode = 1006;
this._extensions = {};
this._isServer = true;
this._receiver = null;
this._sender = null;
this._socket = null;
if (address !== null) {
this._isServer = false;
this._redirects = 0;
if (Array.isArray(protocols)) {
protocols = protocols.join(', ');
} else if (typeof protocols === 'object' && protocols !== null) {
@@ -59,27 +67,38 @@ class WebSocket extends EventEmitter {
protocols = undefined;
}
initAsClient.call(this, address, protocols, options);
initAsClient(this, address, protocols, options);
} else {
this._isServer = true;
}
}
get CONNECTING () { return WebSocket.CONNECTING; }
get CLOSING () { return WebSocket.CLOSING; }
get CLOSED () { return WebSocket.CLOSED; }
get OPEN () { return WebSocket.OPEN; }
get CONNECTING() {
return WebSocket.CONNECTING;
}
get CLOSING() {
return WebSocket.CLOSING;
}
get CLOSED() {
return WebSocket.CLOSED;
}
get OPEN() {
return WebSocket.OPEN;
}
/**
* This deviates from the WHATWG interface since ws doesn't support the required
* default "blob" type (instead we define a custom "nodebuffer" type).
* This deviates from the WHATWG interface since ws doesn't support the
* required default "blob" type (instead we define a custom "nodebuffer"
* type).
*
* @type {String}
*/
get binaryType () {
get binaryType() {
return this._binaryType;
}
set binaryType (type) {
if (constants.BINARY_TYPES.indexOf(type) < 0) return;
set binaryType(type) {
if (!BINARY_TYPES.includes(type)) return;
this._binaryType = type;
@@ -92,7 +111,7 @@ class WebSocket extends EventEmitter {
/**
* @type {Number}
*/
get bufferedAmount () {
get bufferedAmount() {
if (!this._socket) return 0;
//
@@ -104,7 +123,7 @@ class WebSocket extends EventEmitter {
/**
* @type {String}
*/
get extensions () {
get extensions() {
return Object.keys(this._extensions).join();
}
@@ -116,7 +135,7 @@ class WebSocket extends EventEmitter {
* @param {Number} maxPayload The maximum allowed message size
* @private
*/
setSocket (socket, head, maxPayload) {
setSocket(socket, head, maxPayload) {
const receiver = new Receiver(
this._binaryType,
this._extensions,
@@ -156,7 +175,7 @@ class WebSocket extends EventEmitter {
*
* @private
*/
emitClose () {
emitClose() {
this.readyState = WebSocket.CLOSED;
if (!this._socket) {
@@ -191,7 +210,7 @@ class WebSocket extends EventEmitter {
* @param {String} data A string explaining why the connection is closing
* @public
*/
close (code, data) {
close(code, data) {
if (this.readyState === WebSocket.CLOSED) return;
if (this.readyState === WebSocket.CONNECTING) {
const msg = 'WebSocket was closed before the connection was established';
@@ -212,20 +231,16 @@ class WebSocket extends EventEmitter {
if (err) return;
this._closeFrameSent = true;
if (this._socket.writable) {
if (this._closeFrameReceived) this._socket.end();
//
// Ensure that the connection is closed even if the closing handshake
// fails.
//
this._closeTimer = setTimeout(
this._socket.destroy.bind(this._socket),
closeTimeout
);
}
if (this._closeFrameReceived) this._socket.end();
});
//
// Specify a timeout for the closing handshake to complete.
//
this._closeTimer = setTimeout(
this._socket.destroy.bind(this._socket),
closeTimeout
);
}
/**
@@ -236,7 +251,7 @@ class WebSocket extends EventEmitter {
* @param {Function} cb Callback which is executed when the ping is sent
* @public
*/
ping (data, mask, cb) {
ping(data, mask, cb) {
if (typeof data === 'function') {
cb = data;
data = mask = undefined;
@@ -257,7 +272,7 @@ class WebSocket extends EventEmitter {
if (typeof data === 'number') data = data.toString();
if (mask === undefined) mask = !this._isServer;
this._sender.ping(data || constants.EMPTY_BUFFER, mask, cb);
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
}
/**
@@ -268,7 +283,7 @@ class WebSocket extends EventEmitter {
* @param {Function} cb Callback which is executed when the pong is sent
* @public
*/
pong (data, mask, cb) {
pong(data, mask, cb) {
if (typeof data === 'function') {
cb = data;
data = mask = undefined;
@@ -289,7 +304,7 @@ class WebSocket extends EventEmitter {
if (typeof data === 'number') data = data.toString();
if (mask === undefined) mask = !this._isServer;
this._sender.pong(data || constants.EMPTY_BUFFER, mask, cb);
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
}
/**
@@ -304,7 +319,7 @@ class WebSocket extends EventEmitter {
* @param {Function} cb Callback which is executed when data is written out
* @public
*/
send (data, options, cb) {
send(data, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
@@ -322,18 +337,21 @@ class WebSocket extends EventEmitter {
if (typeof data === 'number') data = data.toString();
const opts = Object.assign({
binary: typeof data !== 'string',
mask: !this._isServer,
compress: true,
fin: true
}, options);
const opts = Object.assign(
{
binary: typeof data !== 'string',
mask: !this._isServer,
compress: true,
fin: true
},
options
);
if (!this._extensions[PerMessageDeflate.extensionName]) {
opts.compress = false;
}
this._sender.send(data || constants.EMPTY_BUFFER, opts, cb);
this._sender.send(data || EMPTY_BUFFER, opts, cb);
}
/**
@@ -341,7 +359,7 @@ class WebSocket extends EventEmitter {
*
* @public
*/
terminate () {
terminate() {
if (this.readyState === WebSocket.CLOSED) return;
if (this.readyState === WebSocket.CONNECTING) {
const msg = 'WebSocket was closed before the connection was established';
@@ -356,7 +374,7 @@ class WebSocket extends EventEmitter {
}
readyStates.forEach((readyState, i) => {
WebSocket[readyStates[i]] = i;
WebSocket[readyState] = i;
});
//
@@ -371,11 +389,13 @@ readyStates.forEach((readyState, i) => {
* @return {(Function|undefined)} The event listener or `undefined`
* @public
*/
get () {
get() {
const listeners = this.listeners(method);
for (var i = 0; i < listeners.length; i++) {
if (listeners[i]._listener) return listeners[i]._listener;
}
return undefined;
},
/**
* Add a listener for the event.
@@ -383,7 +403,7 @@ readyStates.forEach((readyState, i) => {
* @param {Function} listener The listener to add
* @public
*/
set (listener) {
set(listener) {
const listeners = this.listeners(method);
for (var i = 0; i < listeners.length; i++) {
//
@@ -404,154 +424,200 @@ 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} protocols The subprotocols
* @param {Object} options Connection options
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable permessage-deflate
* @param {Number} options.handshakeTimeout Timeout in milliseconds for the handshake request
* @param {Number} options.protocolVersion Value of the `Sec-WebSocket-Version` header
* @param {String} options.origin Value of the `Origin` or `Sec-WebSocket-Origin` header
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable
* permessage-deflate
* @param {Number} options.handshakeTimeout Timeout in milliseconds for the
* handshake request
* @param {Number} options.protocolVersion Value of the `Sec-WebSocket-Version`
* header
* @param {String} options.origin Value of the `Origin` or
* `Sec-WebSocket-Origin` header
* @param {Number} options.maxPayload The maximum allowed message size
* @param {Boolean} options.followRedirects Whether or not to follow redirects
* @param {Number} options.maxRedirects The maximum number of redirects allowed
* @private
*/
function initAsClient (address, protocols, options) {
options = Object.assign({
protocolVersion: protocolVersions[1],
perMessageDeflate: true,
maxPayload: 100 * 1024 * 1024
}, options, {
createConnection: undefined,
socketPath: undefined,
hostname: undefined,
protocol: undefined,
timeout: undefined,
method: undefined,
auth: undefined,
host: undefined,
path: undefined,
port: undefined
});
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
}
);
if (protocolVersions.indexOf(options.protocolVersion) === -1) {
if (!protocolVersions.includes(opts.protocolVersion)) {
throw new RangeError(
`Unsupported protocol version: ${options.protocolVersion} ` +
`Unsupported protocol version: ${opts.protocolVersion} ` +
`(supported versions: ${protocolVersions.join(', ')})`
);
}
this._isServer = false;
var parsedUrl;
if (typeof address === 'object' && address.href !== undefined) {
parsedUrl = address;
this.url = address.href;
websocket.url = address.href;
} else {
parsedUrl = url.parse(address);
this.url = address;
//
// The WHATWG URL constructor is not available on Node.js < 6.13.0
//
parsedUrl = url.URL ? new url.URL(address) : url.parse(address);
websocket.url = address;
}
const isUnixSocket = parsedUrl.protocol === 'ws+unix:';
if (!parsedUrl.host && (!isUnixSocket || !parsedUrl.pathname)) {
throw new Error(`Invalid URL: ${this.url}`);
throw new Error(`Invalid URL: ${websocket.url}`);
}
const isSecure = parsedUrl.protocol === 'wss:' || parsedUrl.protocol === 'https:';
const isSecure =
parsedUrl.protocol === 'wss:' || parsedUrl.protocol === 'https:';
const defaultPort = isSecure ? 443 : 80;
const key = crypto.randomBytes(16).toString('base64');
const httpObj = isSecure ? https : http;
const get = isSecure ? https.get : http.get;
const path = parsedUrl.search
? `${parsedUrl.pathname || '/'}${parsedUrl.search}`
: parsedUrl.pathname || '/';
var perMessageDeflate;
options.createConnection = isSecure ? tlsConnect : netConnect;
options.port = parsedUrl.port || (isSecure ? 443 : 80);
options.host = parsedUrl.hostname.startsWith('[')
opts.createConnection = isSecure ? tlsConnect : netConnect;
opts.defaultPort = opts.defaultPort || defaultPort;
opts.port = parsedUrl.port || defaultPort;
opts.host = parsedUrl.hostname.startsWith('[')
? parsedUrl.hostname.slice(1, -1)
: parsedUrl.hostname;
options.headers = Object.assign({
'Sec-WebSocket-Version': options.protocolVersion,
'Sec-WebSocket-Key': key,
'Connection': 'Upgrade',
'Upgrade': 'websocket'
}, options.headers);
options.path = path;
opts.headers = Object.assign(
{
'Sec-WebSocket-Version': opts.protocolVersion,
'Sec-WebSocket-Key': key,
Connection: 'Upgrade',
Upgrade: 'websocket'
},
opts.headers
);
opts.path = path;
opts.timeout = opts.handshakeTimeout;
if (options.perMessageDeflate) {
if (opts.perMessageDeflate) {
perMessageDeflate = new PerMessageDeflate(
options.perMessageDeflate !== true ? options.perMessageDeflate : {},
opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
false,
options.maxPayload
opts.maxPayload
);
options.headers['Sec-WebSocket-Extensions'] = extension.format({
opts.headers['Sec-WebSocket-Extensions'] = extension.format({
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
});
}
if (protocols) {
options.headers['Sec-WebSocket-Protocol'] = protocols;
opts.headers['Sec-WebSocket-Protocol'] = protocols;
}
if (options.origin) {
if (options.protocolVersion < 13) {
options.headers['Sec-WebSocket-Origin'] = options.origin;
if (opts.origin) {
if (opts.protocolVersion < 13) {
opts.headers['Sec-WebSocket-Origin'] = opts.origin;
} else {
options.headers.Origin = options.origin;
opts.headers.Origin = opts.origin;
}
}
if (parsedUrl.auth) {
options.auth = parsedUrl.auth;
opts.auth = parsedUrl.auth;
} else if (parsedUrl.username || parsedUrl.password) {
options.auth = `${parsedUrl.username}:${parsedUrl.password}`;
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
}
if (isUnixSocket) {
const parts = path.split(':');
options.socketPath = parts[0];
options.path = parts[1];
opts.socketPath = parts[0];
opts.path = parts[1];
}
var req = this._req = httpObj.get(options);
var req = (websocket._req = get(opts));
if (options.handshakeTimeout) {
req.setTimeout(
options.handshakeTimeout,
() => abortHandshake(this, req, 'Opening handshake has timed out')
);
if (opts.timeout) {
req.on('timeout', () => {
abortHandshake(websocket, req, 'Opening handshake has timed out');
});
}
req.on('error', (err) => {
if (this._req.aborted) return;
if (websocket._req.aborted) return;
req = this._req = null;
this.readyState = WebSocket.CLOSING;
this.emit('error', err);
this.emitClose();
req = websocket._req = null;
websocket.readyState = WebSocket.CLOSING;
websocket.emit('error', err);
websocket.emitClose();
});
req.on('response', (res) => {
if (this.emit('unexpected-response', req, res)) return;
const location = res.headers.location;
const statusCode = res.statusCode;
abortHandshake(this, req, `Unexpected server response: ${res.statusCode}`);
if (
location &&
opts.followRedirects &&
statusCode >= 300 &&
statusCode < 400
) {
if (++websocket._redirects > opts.maxRedirects) {
abortHandshake(websocket, req, 'Maximum redirects exceeded');
return;
}
req.abort();
const addr = url.URL
? new url.URL(location, address)
: url.resolve(address, location);
initAsClient(websocket, addr, protocols, options);
} else if (!websocket.emit('unexpected-response', req, res)) {
abortHandshake(
websocket,
req,
`Unexpected server response: ${res.statusCode}`
);
}
});
req.on('upgrade', (res, socket, head) => {
this.emit('upgrade', res);
websocket.emit('upgrade', res);
//
// The user may have closed the connection from a listener of the `upgrade`
// event.
//
if (this.readyState !== WebSocket.CONNECTING) return;
if (websocket.readyState !== WebSocket.CONNECTING) return;
req = this._req = null;
req = websocket._req = null;
const digest = crypto.createHash('sha1')
.update(key + constants.GUID, 'binary')
const digest = crypto
.createHash('sha1')
.update(key + GUID)
.digest('base64');
if (res.headers['sec-websocket-accept'] !== digest) {
abortHandshake(this, socket, 'Invalid Sec-WebSocket-Accept header');
abortHandshake(websocket, socket, 'Invalid Sec-WebSocket-Accept header');
return;
}
@@ -563,16 +629,16 @@ function initAsClient (address, protocols, options) {
protError = 'Server sent a subprotocol but none was requested';
} else if (protocols && !serverProt) {
protError = 'Server sent no subprotocol';
} else if (serverProt && protList.indexOf(serverProt) === -1) {
} else if (serverProt && !protList.includes(serverProt)) {
protError = 'Server sent an invalid subprotocol';
}
if (protError) {
abortHandshake(this, socket, protError);
abortHandshake(websocket, socket, protError);
return;
}
if (serverProt) this.protocol = serverProt;
if (serverProt) websocket.protocol = serverProt;
if (perMessageDeflate) {
try {
@@ -581,18 +647,22 @@ function initAsClient (address, protocols, options) {
);
if (extensions[PerMessageDeflate.extensionName]) {
perMessageDeflate.accept(
extensions[PerMessageDeflate.extensionName]
);
this._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
websocket._extensions[
PerMessageDeflate.extensionName
] = perMessageDeflate;
}
} catch (err) {
abortHandshake(this, socket, 'Invalid Sec-WebSocket-Extensions header');
abortHandshake(
websocket,
socket,
'Invalid Sec-WebSocket-Extensions header'
);
return;
}
}
this.setSocket(socket, head, options.maxPayload);
websocket.setSocket(socket, head, opts.maxPayload);
});
}
@@ -603,7 +673,7 @@ function initAsClient (address, protocols, options) {
* @return {net.Socket} The newly created socket used to start the connection
* @private
*/
function netConnect (options) {
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
@@ -621,7 +691,7 @@ function netConnect (options) {
* @return {tls.TLSSocket} The newly created socket used to start the connection
* @private
*/
function tlsConnect (options) {
function tlsConnect(options) {
options.path = undefined;
options.servername = options.servername || options.host;
return tls.connect(options);
@@ -636,7 +706,7 @@ function tlsConnect (options) {
* @param {String} message The error message
* @private
*/
function abortHandshake (websocket, stream, message) {
function abortHandshake(websocket, stream, message) {
websocket.readyState = WebSocket.CLOSING;
const err = new Error(message);
@@ -660,7 +730,7 @@ function abortHandshake (websocket, stream, message) {
* @param {String} reason The reason for closing
* @private
*/
function receiverOnConclude (code, reason) {
function receiverOnConclude(code, reason) {
const websocket = this[kWebSocket];
websocket._socket.removeListener('data', socketOnData);
@@ -679,7 +749,7 @@ function receiverOnConclude (code, reason) {
*
* @private
*/
function receiverOnDrain () {
function receiverOnDrain() {
this[kWebSocket]._socket.resume();
}
@@ -689,13 +759,13 @@ function receiverOnDrain () {
* @param {(RangeError|Error)} err The emitted error
* @private
*/
function receiverOnError (err) {
function receiverOnError(err) {
const websocket = this[kWebSocket];
websocket._socket.removeListener('data', socketOnData);
websocket.readyState = WebSocket.CLOSING;
websocket._closeCode = err[constants.kStatusCode];
websocket._closeCode = err[kStatusCode];
websocket.emit('error', err);
websocket._socket.destroy();
}
@@ -705,7 +775,7 @@ function receiverOnError (err) {
*
* @private
*/
function receiverOnFinish () {
function receiverOnFinish() {
this[kWebSocket].emitClose();
}
@@ -715,7 +785,7 @@ function receiverOnFinish () {
* @param {(String|Buffer|ArrayBuffer|Buffer[])} data The message
* @private
*/
function receiverOnMessage (data) {
function receiverOnMessage(data) {
this[kWebSocket].emit('message', data);
}
@@ -725,10 +795,10 @@ function receiverOnMessage (data) {
* @param {Buffer} data The data included in the ping frame
* @private
*/
function receiverOnPing (data) {
function receiverOnPing(data) {
const websocket = this[kWebSocket];
websocket.pong(data, !websocket._isServer, constants.NOOP);
websocket.pong(data, !websocket._isServer, NOOP);
websocket.emit('ping', data);
}
@@ -738,7 +808,7 @@ function receiverOnPing (data) {
* @param {Buffer} data The data included in the pong frame
* @private
*/
function receiverOnPong (data) {
function receiverOnPong(data) {
this[kWebSocket].emit('pong', data);
}
@@ -747,7 +817,7 @@ function receiverOnPong (data) {
*
* @private
*/
function socketOnClose () {
function socketOnClose() {
const websocket = this[kWebSocket];
this.removeListener('close', socketOnClose);
@@ -790,7 +860,7 @@ function socketOnClose () {
* @param {Buffer} chunk A chunk of data
* @private
*/
function socketOnData (chunk) {
function socketOnData(chunk) {
if (!this[kWebSocket]._receiver.write(chunk)) {
this.pause();
}
@@ -801,7 +871,7 @@ function socketOnData (chunk) {
*
* @private
*/
function socketOnEnd () {
function socketOnEnd() {
const websocket = this[kWebSocket];
websocket.readyState = WebSocket.CLOSING;
@@ -814,14 +884,12 @@ function socketOnEnd () {
*
* @private
*/
function socketOnError () {
function socketOnError() {
const websocket = this[kWebSocket];
this.removeListener('error', socketOnError);
this.on('error', constants.NOOP);
this.on('error', NOOP);
if (websocket) {
websocket.readyState = WebSocket.CLOSING;
this.destroy();
}
websocket.readyState = WebSocket.CLOSING;
this.destroy();
}