Dependency update

This commit is contained in:
2025-04-08 22:04:41 +02:00
committed by Philipp Horstenkamp
parent 78549e06b4
commit f7b40cb8dd
492 changed files with 7483 additions and 47496 deletions

274
node_modules/ws/lib/receiver.js generated vendored
View File

@@ -1,16 +1,11 @@
'use strict';
const { Writable } = require('stream');
const stream = require('stream');
const PerMessageDeflate = require('./permessage-deflate');
const {
BINARY_TYPES,
EMPTY_BUFFER,
kStatusCode,
kWebSocket
} = require('./constants');
const { concat, toArrayBuffer, unmask } = require('./buffer-util');
const { isValidStatusCode, isValidUTF8 } = require('./validation');
const bufferUtil = require('./buffer-util');
const validation = require('./validation');
const constants = require('./constants');
const GET_INFO = 0;
const GET_PAYLOAD_LENGTH_16 = 1;
@@ -22,25 +17,22 @@ const INFLATING = 5;
/**
* HyBi Receiver implementation.
*
* @extends Writable
* @extends stream.Writable
*/
class Receiver extends Writable {
class Receiver extends stream.Writable {
/**
* Creates a Receiver instance.
*
* @param {String} [binaryType=nodebuffer] The type for binary data
* @param {Object} [extensions] An object containing the negotiated extensions
* @param {Boolean} [isServer=false] Specifies whether to operate in client or
* server mode
* @param {Number} [maxPayload=0] The maximum allowed message length
* @param {String} binaryType The type for binary data
* @param {Object} extensions An object containing the negotiated extensions
* @param {Number} maxPayload The maximum allowed message length
*/
constructor(binaryType, extensions, isServer, maxPayload) {
constructor (binaryType, extensions, maxPayload) {
super();
this._binaryType = binaryType || BINARY_TYPES[0];
this[kWebSocket] = undefined;
this._binaryType = binaryType || constants.BINARY_TYPES[0];
this[constants.kWebSocket] = undefined;
this._extensions = extensions || {};
this._isServer = !!isServer;
this._maxPayload = maxPayload | 0;
this._bufferedBytes = 0;
@@ -68,10 +60,9 @@ class Receiver extends Writable {
* @param {Buffer} chunk The chunk of data to write
* @param {String} encoding The character encoding of `chunk`
* @param {Function} cb Callback
* @private
*/
_write(chunk, encoding, cb) {
if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
_write (chunk, encoding, cb) {
if (this._opcode === 0x08) return cb();
this._bufferedBytes += chunk.length;
this._buffers.push(chunk);
@@ -85,7 +76,7 @@ class Receiver extends Writable {
* @return {Buffer} The consumed bytes
* @private
*/
consume(n) {
consume (n) {
this._bufferedBytes -= n;
if (n === this._buffers[0].length) return this._buffers.shift();
@@ -100,12 +91,11 @@ class Receiver extends Writable {
do {
const buf = this._buffers[0];
const offset = dst.length - n;
if (n >= buf.length) {
dst.set(this._buffers.shift(), offset);
this._buffers.shift().copy(dst, dst.length - n);
} else {
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
buf.copy(dst, dst.length - n, 0, n);
this._buffers[0] = buf.slice(n);
}
@@ -121,8 +111,8 @@ class Receiver extends Writable {
* @param {Function} cb Callback
* @private
*/
startLoop(cb) {
let err;
startLoop (cb) {
var err;
this._loop = true;
do {
@@ -142,8 +132,7 @@ class Receiver extends Writable {
case GET_DATA:
err = this.getData(cb);
break;
default:
// `INFLATING`
default: // `INFLATING`
this._loop = false;
return;
}
@@ -158,7 +147,7 @@ class Receiver extends Writable {
* @return {(RangeError|undefined)} A possible error
* @private
*/
getInfo() {
getInfo () {
if (this._bufferedBytes < 2) {
this._loop = false;
return;
@@ -168,26 +157,14 @@ class Receiver extends Writable {
if ((buf[0] & 0x30) !== 0x00) {
this._loop = false;
return error(
RangeError,
'RSV2 and RSV3 must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_RSV_2_3'
);
return error(RangeError, 'RSV2 and RSV3 must be clear', true, 1002);
}
const compressed = (buf[0] & 0x40) === 0x40;
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
this._loop = false;
return error(
RangeError,
'RSV1 must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_RSV_1'
);
return error(RangeError, 'RSV1 must be clear', true, 1002);
}
this._fin = (buf[0] & 0x80) === 0x80;
@@ -197,61 +174,31 @@ class Receiver extends Writable {
if (this._opcode === 0x00) {
if (compressed) {
this._loop = false;
return error(
RangeError,
'RSV1 must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_RSV_1'
);
return error(RangeError, 'RSV1 must be clear', true, 1002);
}
if (!this._fragmented) {
this._loop = false;
return error(
RangeError,
'invalid opcode 0',
true,
1002,
'WS_ERR_INVALID_OPCODE'
);
return error(RangeError, 'invalid opcode 0', true, 1002);
}
this._opcode = this._fragmented;
} else if (this._opcode === 0x01 || this._opcode === 0x02) {
if (this._fragmented) {
this._loop = false;
return error(
RangeError,
`invalid opcode ${this._opcode}`,
true,
1002,
'WS_ERR_INVALID_OPCODE'
);
return error(RangeError, `invalid opcode ${this._opcode}`, true, 1002);
}
this._compressed = compressed;
} else if (this._opcode > 0x07 && this._opcode < 0x0b) {
if (!this._fin) {
this._loop = false;
return error(
RangeError,
'FIN must be set',
true,
1002,
'WS_ERR_EXPECTED_FIN'
);
return error(RangeError, 'FIN must be set', true, 1002);
}
if (compressed) {
this._loop = false;
return error(
RangeError,
'RSV1 must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_RSV_1'
);
return error(RangeError, 'RSV1 must be clear', true, 1002);
}
if (this._payloadLength > 0x7d) {
@@ -260,46 +207,17 @@ class Receiver extends Writable {
RangeError,
`invalid payload length ${this._payloadLength}`,
true,
1002,
'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
1002
);
}
} else {
this._loop = false;
return error(
RangeError,
`invalid opcode ${this._opcode}`,
true,
1002,
'WS_ERR_INVALID_OPCODE'
);
return error(RangeError, `invalid opcode ${this._opcode}`, true, 1002);
}
if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
this._masked = (buf[1] & 0x80) === 0x80;
if (this._isServer) {
if (!this._masked) {
this._loop = false;
return error(
RangeError,
'MASK must be set',
true,
1002,
'WS_ERR_EXPECTED_MASK'
);
}
} else if (this._masked) {
this._loop = false;
return error(
RangeError,
'MASK must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_MASK'
);
}
if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
else return this.haveLength();
@@ -311,7 +229,7 @@ class Receiver extends Writable {
* @return {(RangeError|undefined)} A possible error
* @private
*/
getPayloadLength16() {
getPayloadLength16 () {
if (this._bufferedBytes < 2) {
this._loop = false;
return;
@@ -327,7 +245,7 @@ class Receiver extends Writable {
* @return {(RangeError|undefined)} A possible error
* @private
*/
getPayloadLength64() {
getPayloadLength64 () {
if (this._bufferedBytes < 8) {
this._loop = false;
return;
@@ -346,8 +264,7 @@ class Receiver extends Writable {
RangeError,
'Unsupported WebSocket frame: payload length > 2^53 - 1',
false,
1009,
'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH'
1009
);
}
@@ -361,18 +278,12 @@ class Receiver extends Writable {
* @return {(RangeError|undefined)} A possible error
* @private
*/
haveLength() {
haveLength () {
if (this._payloadLength && this._opcode < 0x08) {
this._totalPayloadLength += this._payloadLength;
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
this._loop = false;
return error(
RangeError,
'Max payload size exceeded',
false,
1009,
'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
);
return error(RangeError, 'Max payload size exceeded', false, 1009);
}
}
@@ -385,7 +296,7 @@ class Receiver extends Writable {
*
* @private
*/
getMask() {
getMask () {
if (this._bufferedBytes < 4) {
this._loop = false;
return;
@@ -402,8 +313,8 @@ class Receiver extends Writable {
* @return {(Error|RangeError|undefined)} A possible error
* @private
*/
getData(cb) {
let data = EMPTY_BUFFER;
getData (cb) {
var data = constants.EMPTY_BUFFER;
if (this._payloadLength) {
if (this._bufferedBytes < this._payloadLength) {
@@ -412,7 +323,7 @@ class Receiver extends Writable {
}
data = this.consume(this._payloadLength);
if (this._masked) unmask(data, this._mask);
if (this._masked) bufferUtil.unmask(data, this._mask);
}
if (this._opcode > 0x07) return this.controlMessage(data);
@@ -442,7 +353,7 @@ class Receiver extends Writable {
* @param {Function} cb Callback
* @private
*/
decompress(data, cb) {
decompress (data, cb) {
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
@@ -451,15 +362,7 @@ class Receiver extends Writable {
if (buf.length) {
this._messageLength += buf.length;
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
return cb(
error(
RangeError,
'Max payload size exceeded',
false,
1009,
'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
)
);
return cb(error(RangeError, 'Max payload size exceeded', false, 1009));
}
this._fragments.push(buf);
@@ -478,7 +381,7 @@ class Receiver extends Writable {
* @return {(Error|undefined)} A possible error
* @private
*/
dataMessage() {
dataMessage () {
if (this._fin) {
const messageLength = this._messageLength;
const fragments = this._fragments;
@@ -489,29 +392,23 @@ class Receiver extends Writable {
this._fragments = [];
if (this._opcode === 2) {
let data;
var data;
if (this._binaryType === 'nodebuffer') {
data = concat(fragments, messageLength);
data = toBuffer(fragments, messageLength);
} else if (this._binaryType === 'arraybuffer') {
data = toArrayBuffer(concat(fragments, messageLength));
data = toArrayBuffer(toBuffer(fragments, messageLength));
} else {
data = fragments;
}
this.emit('message', data);
} else {
const buf = concat(fragments, messageLength);
const buf = toBuffer(fragments, messageLength);
if (!isValidUTF8(buf)) {
if (!validation.isValidUTF8(buf)) {
this._loop = false;
return error(
Error,
'invalid UTF-8 sequence',
true,
1007,
'WS_ERR_INVALID_UTF8'
);
return error(Error, 'invalid UTF-8 sequence', true, 1007);
}
this.emit('message', buf.toString());
@@ -528,7 +425,7 @@ class Receiver extends Writable {
* @return {(Error|RangeError|undefined)} A possible error
* @private
*/
controlMessage(data) {
controlMessage (data) {
if (this._opcode === 0x08) {
this._loop = false;
@@ -536,47 +433,30 @@ class Receiver extends Writable {
this.emit('conclude', 1005, '');
this.end();
} else if (data.length === 1) {
return error(
RangeError,
'invalid payload length 1',
true,
1002,
'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
);
return error(RangeError, 'invalid payload length 1', true, 1002);
} else {
const code = data.readUInt16BE(0);
if (!isValidStatusCode(code)) {
return error(
RangeError,
`invalid status code ${code}`,
true,
1002,
'WS_ERR_INVALID_CLOSE_CODE'
);
if (!validation.isValidStatusCode(code)) {
return error(RangeError, `invalid status code ${code}`, true, 1002);
}
const buf = data.slice(2);
if (!isValidUTF8(buf)) {
return error(
Error,
'invalid UTF-8 sequence',
true,
1007,
'WS_ERR_INVALID_UTF8'
);
if (!validation.isValidUTF8(buf)) {
return error(Error, 'invalid UTF-8 sequence', true, 1007);
}
this.emit('conclude', code, buf.toString());
this.end();
}
} else if (this._opcode === 0x09) {
this.emit('ping', data);
} else {
this.emit('pong', data);
return;
}
if (this._opcode === 0x09) this.emit('ping', data);
else this.emit('pong', data);
this._state = GET_INFO;
}
}
@@ -586,22 +466,48 @@ module.exports = Receiver;
/**
* Builds an error object.
*
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
* @param {(Error|RangeError)} ErrorCtor The error constructor
* @param {String} message The error message
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
* `message`
* @param {Number} statusCode The status code
* @param {String} errorCode The exposed error code
* @return {(Error|RangeError)} The error
* @private
*/
function error(ErrorCtor, message, prefix, statusCode, errorCode) {
function error (ErrorCtor, message, prefix, statusCode) {
const err = new ErrorCtor(
prefix ? `Invalid WebSocket frame: ${message}` : message
);
Error.captureStackTrace(err, error);
err.code = errorCode;
err[kStatusCode] = statusCode;
err[constants.kStatusCode] = statusCode;
return err;
}
/**
* Makes a buffer from a list of fragments.
*
* @param {Buffer[]} fragments The list of fragments composing the message
* @param {Number} messageLength The length of the message
* @return {Buffer}
* @private
*/
function toBuffer (fragments, messageLength) {
if (fragments.length === 1) return fragments[0];
if (fragments.length > 1) return bufferUtil.concat(fragments, messageLength);
return constants.EMPTY_BUFFER;
}
/**
* Converts a buffer to an `ArrayBuffer`.
*
* @param {Buffer} The buffer to convert
* @return {ArrayBuffer} Converted buffer
*/
function toArrayBuffer (buf) {
if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
return buf.buffer;
}
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
}