mirror of
https://github.com/S2-/minifyfromhtml.git
synced 2025-08-03 04:10:04 +02:00
update node modules
This commit is contained in:
BIN
node_modules/ws/lib/.DS_Store
generated
vendored
BIN
node_modules/ws/lib/.DS_Store
generated
vendored
Binary file not shown.
153
node_modules/ws/lib/buffer-util.js
generated
vendored
153
node_modules/ws/lib/buffer-util.js
generated
vendored
@@ -1,8 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const safeBuffer = require('safe-buffer');
|
||||
|
||||
const Buffer = safeBuffer.Buffer;
|
||||
const { EMPTY_BUFFER } = require('./constants');
|
||||
|
||||
/**
|
||||
* Merges an array of buffers into a new buffer.
|
||||
@@ -12,7 +10,10 @@ const Buffer = safeBuffer.Buffer;
|
||||
* @return {Buffer} The resulting buffer
|
||||
* @public
|
||||
*/
|
||||
const concat = (list, totalLength) => {
|
||||
function concat(list, totalLength) {
|
||||
if (list.length === 0) return EMPTY_BUFFER;
|
||||
if (list.length === 1) return list[0];
|
||||
|
||||
const target = Buffer.allocUnsafe(totalLength);
|
||||
var offset = 0;
|
||||
|
||||
@@ -23,43 +24,121 @@ const concat = (list, totalLength) => {
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Masks a buffer using the given mask.
|
||||
*
|
||||
* @param {Buffer} source The buffer to mask
|
||||
* @param {Buffer} mask The mask to use
|
||||
* @param {Buffer} output The buffer where to store the result
|
||||
* @param {Number} offset The offset at which to start writing
|
||||
* @param {Number} length The number of bytes to mask.
|
||||
* @public
|
||||
*/
|
||||
function _mask(source, mask, output, offset, length) {
|
||||
for (var i = 0; i < length; i++) {
|
||||
output[offset + i] = source[i] ^ mask[i & 3];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmasks a buffer using the given mask.
|
||||
*
|
||||
* @param {Buffer} buffer The buffer to unmask
|
||||
* @param {Buffer} mask The mask to use
|
||||
* @public
|
||||
*/
|
||||
function _unmask(buffer, mask) {
|
||||
// Required until https://github.com/nodejs/node/issues/9006 is resolved.
|
||||
const length = buffer.length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
buffer[i] ^= mask[i & 3];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a buffer to an `ArrayBuffer`.
|
||||
*
|
||||
* @param {Buffer} buf The buffer to convert
|
||||
* @return {ArrayBuffer} Converted buffer
|
||||
* @public
|
||||
*/
|
||||
function toArrayBuffer(buf) {
|
||||
if (buf.byteLength === buf.buffer.byteLength) {
|
||||
return buf.buffer;
|
||||
}
|
||||
|
||||
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts `data` to a `Buffer`.
|
||||
*
|
||||
* @param {*} data The data to convert
|
||||
* @return {Buffer} The buffer
|
||||
* @throws {TypeError}
|
||||
* @public
|
||||
*/
|
||||
function toBuffer(data) {
|
||||
toBuffer.readOnly = true;
|
||||
|
||||
if (Buffer.isBuffer(data)) return data;
|
||||
|
||||
var buf;
|
||||
|
||||
if (data instanceof ArrayBuffer) {
|
||||
buf = Buffer.from(data);
|
||||
} else if (ArrayBuffer.isView(data)) {
|
||||
buf = viewToBuffer(data);
|
||||
} else {
|
||||
buf = Buffer.from(data);
|
||||
toBuffer.readOnly = false;
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an `ArrayBuffer` view into a buffer.
|
||||
*
|
||||
* @param {(DataView|TypedArray)} view The view to convert
|
||||
* @return {Buffer} Converted view
|
||||
* @private
|
||||
*/
|
||||
function viewToBuffer(view) {
|
||||
const buf = Buffer.from(view.buffer);
|
||||
|
||||
if (view.byteLength !== view.buffer.byteLength) {
|
||||
return buf.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
try {
|
||||
const bufferUtil = require('bufferutil');
|
||||
const bu = bufferUtil.BufferUtil || bufferUtil;
|
||||
|
||||
module.exports = Object.assign({ concat }, bufferUtil.BufferUtil || bufferUtil);
|
||||
module.exports = {
|
||||
concat,
|
||||
mask(source, mask, output, offset, length) {
|
||||
if (length < 48) _mask(source, mask, output, offset, length);
|
||||
else bu.mask(source, mask, output, offset, length);
|
||||
},
|
||||
toArrayBuffer,
|
||||
toBuffer,
|
||||
unmask(buffer, mask) {
|
||||
if (buffer.length < 32) _unmask(buffer, mask);
|
||||
else bu.unmask(buffer, mask);
|
||||
}
|
||||
};
|
||||
} catch (e) /* istanbul ignore next */ {
|
||||
/**
|
||||
* Masks a buffer using the given mask.
|
||||
*
|
||||
* @param {Buffer} source The buffer to mask
|
||||
* @param {Buffer} mask The mask to use
|
||||
* @param {Buffer} output The buffer where to store the result
|
||||
* @param {Number} offset The offset at which to start writing
|
||||
* @param {Number} length The number of bytes to mask.
|
||||
* @public
|
||||
*/
|
||||
const mask = (source, mask, output, offset, length) => {
|
||||
for (var i = 0; i < length; i++) {
|
||||
output[offset + i] = source[i] ^ mask[i & 3];
|
||||
}
|
||||
module.exports = {
|
||||
concat,
|
||||
mask: _mask,
|
||||
toArrayBuffer,
|
||||
toBuffer,
|
||||
unmask: _unmask
|
||||
};
|
||||
|
||||
/**
|
||||
* Unmasks a buffer using the given mask.
|
||||
*
|
||||
* @param {Buffer} buffer The buffer to unmask
|
||||
* @param {Buffer} mask The mask to use
|
||||
* @public
|
||||
*/
|
||||
const unmask = (buffer, mask) => {
|
||||
// Required until https://github.com/nodejs/node/issues/9006 is resolved.
|
||||
const length = buffer.length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
buffer[i] ^= mask[i & 3];
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { concat, mask, unmask };
|
||||
}
|
||||
|
16
node_modules/ws/lib/constants.js
generated
vendored
16
node_modules/ws/lib/constants.js
generated
vendored
@@ -1,10 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const safeBuffer = require('safe-buffer');
|
||||
|
||||
const Buffer = safeBuffer.Buffer;
|
||||
|
||||
exports.BINARY_TYPES = ['nodebuffer', 'arraybuffer', 'fragments'];
|
||||
exports.GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
||||
exports.EMPTY_BUFFER = Buffer.alloc(0);
|
||||
exports.NOOP = () => {};
|
||||
module.exports = {
|
||||
BINARY_TYPES: ['nodebuffer', 'arraybuffer', 'fragments'],
|
||||
GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
|
||||
kStatusCode: Symbol('status-code'),
|
||||
kWebSocket: Symbol('websocket'),
|
||||
EMPTY_BUFFER: Buffer.alloc(0),
|
||||
NOOP: () => {}
|
||||
};
|
||||
|
22
node_modules/ws/lib/event-target.js
generated
vendored
22
node_modules/ws/lib/event-target.js
generated
vendored
@@ -12,7 +12,7 @@ class Event {
|
||||
* @param {String} type The name of the event
|
||||
* @param {Object} target A reference to the target to which the event was dispatched
|
||||
*/
|
||||
constructor (type, target) {
|
||||
constructor(type, target) {
|
||||
this.target = target;
|
||||
this.type = type;
|
||||
}
|
||||
@@ -31,7 +31,7 @@ class MessageEvent extends Event {
|
||||
* @param {(String|Buffer|ArrayBuffer|Buffer[])} data The received data
|
||||
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
||||
*/
|
||||
constructor (data, target) {
|
||||
constructor(data, target) {
|
||||
super('message', target);
|
||||
|
||||
this.data = data;
|
||||
@@ -52,7 +52,7 @@ class CloseEvent extends Event {
|
||||
* @param {String} reason A human-readable string explaining why the connection is closing
|
||||
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
||||
*/
|
||||
constructor (code, reason, target) {
|
||||
constructor(code, reason, target) {
|
||||
super('close', target);
|
||||
|
||||
this.wasClean = target._closeFrameReceived && target._closeFrameSent;
|
||||
@@ -73,7 +73,7 @@ class OpenEvent extends Event {
|
||||
*
|
||||
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
||||
*/
|
||||
constructor (target) {
|
||||
constructor(target) {
|
||||
super('open', target);
|
||||
}
|
||||
}
|
||||
@@ -91,7 +91,7 @@ class ErrorEvent extends Event {
|
||||
* @param {Object} error The error that generated this event
|
||||
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
||||
*/
|
||||
constructor (error, target) {
|
||||
constructor(error, target) {
|
||||
super('error', target);
|
||||
|
||||
this.message = error.message;
|
||||
@@ -113,22 +113,22 @@ const EventTarget = {
|
||||
* @param {Function} listener The listener to add
|
||||
* @public
|
||||
*/
|
||||
addEventListener (method, listener) {
|
||||
addEventListener(method, listener) {
|
||||
if (typeof listener !== 'function') return;
|
||||
|
||||
function onMessage (data) {
|
||||
function onMessage(data) {
|
||||
listener.call(this, new MessageEvent(data, this));
|
||||
}
|
||||
|
||||
function onClose (code, message) {
|
||||
function onClose(code, message) {
|
||||
listener.call(this, new CloseEvent(code, message, this));
|
||||
}
|
||||
|
||||
function onError (error) {
|
||||
function onError(error) {
|
||||
listener.call(this, new ErrorEvent(error, this));
|
||||
}
|
||||
|
||||
function onOpen () {
|
||||
function onOpen() {
|
||||
listener.call(this, new OpenEvent(this));
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ const EventTarget = {
|
||||
* @param {Function} listener The listener to remove
|
||||
* @public
|
||||
*/
|
||||
removeEventListener (method, listener) {
|
||||
removeEventListener(method, listener) {
|
||||
const listeners = this.listeners(method);
|
||||
|
||||
for (var i = 0; i < listeners.length; i++) {
|
||||
|
49
node_modules/ws/lib/extension.js
generated
vendored
49
node_modules/ws/lib/extension.js
generated
vendored
@@ -11,6 +11,7 @@
|
||||
// tokenChars[34] === 0 // '"'
|
||||
// ...
|
||||
//
|
||||
// prettier-ignore
|
||||
const tokenChars = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
|
||||
@@ -32,7 +33,7 @@ const tokenChars = [
|
||||
* parameter value
|
||||
* @private
|
||||
*/
|
||||
function push (dest, name, elem) {
|
||||
function push(dest, name, elem) {
|
||||
if (Object.prototype.hasOwnProperty.call(dest, name)) dest[name].push(elem);
|
||||
else dest[name] = [elem];
|
||||
}
|
||||
@@ -44,7 +45,7 @@ function push (dest, name, elem) {
|
||||
* @return {Object} The parsed object
|
||||
* @public
|
||||
*/
|
||||
function parse (header) {
|
||||
function parse(header) {
|
||||
const offers = {};
|
||||
|
||||
if (header === undefined || header === '') return offers;
|
||||
@@ -64,9 +65,9 @@ function parse (header) {
|
||||
if (extensionName === undefined) {
|
||||
if (end === -1 && tokenChars[code] === 1) {
|
||||
if (start === -1) start = i;
|
||||
} else if (code === 0x20/* ' ' */|| code === 0x09/* '\t' */) {
|
||||
} else if (code === 0x20 /* ' ' */ || code === 0x09 /* '\t' */) {
|
||||
if (end === -1 && start !== -1) end = i;
|
||||
} else if (code === 0x3b/* ';' */ || code === 0x2c/* ',' */) {
|
||||
} else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
|
||||
if (start === -1) {
|
||||
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||
}
|
||||
@@ -103,7 +104,7 @@ function parse (header) {
|
||||
}
|
||||
|
||||
start = end = -1;
|
||||
} else if (code === 0x3d/* '=' */&& start !== -1 && end === -1) {
|
||||
} else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
|
||||
paramName = header.slice(start, i);
|
||||
start = end = -1;
|
||||
} else {
|
||||
@@ -125,10 +126,10 @@ function parse (header) {
|
||||
} else if (inQuotes) {
|
||||
if (tokenChars[code] === 1) {
|
||||
if (start === -1) start = i;
|
||||
} else if (code === 0x22/* '"' */ && start !== -1) {
|
||||
} else if (code === 0x22 /* '"' */ && start !== -1) {
|
||||
inQuotes = false;
|
||||
end = i;
|
||||
} else if (code === 0x5c/* '\' */) {
|
||||
} else if (code === 0x5c /* '\' */) {
|
||||
isEscaping = true;
|
||||
} else {
|
||||
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||
@@ -194,18 +195,28 @@ function parse (header) {
|
||||
* @return {String} A string representing the given object
|
||||
* @public
|
||||
*/
|
||||
function format (extensions) {
|
||||
return Object.keys(extensions).map((extension) => {
|
||||
var configurations = extensions[extension];
|
||||
if (!Array.isArray(configurations)) configurations = [configurations];
|
||||
return configurations.map((params) => {
|
||||
return [extension].concat(Object.keys(params).map((k) => {
|
||||
var values = params[k];
|
||||
if (!Array.isArray(values)) values = [values];
|
||||
return values.map((v) => v === true ? k : `${k}=${v}`).join('; ');
|
||||
})).join('; ');
|
||||
}).join(', ');
|
||||
}).join(', ');
|
||||
function format(extensions) {
|
||||
return Object.keys(extensions)
|
||||
.map((extension) => {
|
||||
var configurations = extensions[extension];
|
||||
if (!Array.isArray(configurations)) configurations = [configurations];
|
||||
return configurations
|
||||
.map((params) => {
|
||||
return [extension]
|
||||
.concat(
|
||||
Object.keys(params).map((k) => {
|
||||
var values = params[k];
|
||||
if (!Array.isArray(values)) values = [values];
|
||||
return values
|
||||
.map((v) => (v === true ? k : `${k}=${v}`))
|
||||
.join('; ');
|
||||
})
|
||||
)
|
||||
.join('; ');
|
||||
})
|
||||
.join(', ');
|
||||
})
|
||||
.join(', ');
|
||||
}
|
||||
|
||||
module.exports = { format, parse };
|
||||
|
141
node_modules/ws/lib/permessage-deflate.js
generated
vendored
141
node_modules/ws/lib/permessage-deflate.js
generated
vendored
@@ -1,23 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const safeBuffer = require('safe-buffer');
|
||||
const Limiter = require('async-limiter');
|
||||
const zlib = require('zlib');
|
||||
|
||||
const bufferUtil = require('./buffer-util');
|
||||
|
||||
const Buffer = safeBuffer.Buffer;
|
||||
const { kStatusCode, NOOP } = require('./constants');
|
||||
|
||||
const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
|
||||
const EMPTY_BLOCK = Buffer.from([0x00]);
|
||||
|
||||
const kWriteInProgress = Symbol('write-in-progress');
|
||||
const kPendingClose = Symbol('pending-close');
|
||||
const kPerMessageDeflate = Symbol('permessage-deflate');
|
||||
const kTotalLength = Symbol('total-length');
|
||||
const kCallback = Symbol('callback');
|
||||
const kBuffers = Symbol('buffers');
|
||||
const kError = Symbol('error');
|
||||
const kOwner = Symbol('owner');
|
||||
|
||||
//
|
||||
// We limit zlib concurrency, which prevents severe memory fragmentation
|
||||
@@ -54,12 +50,11 @@ class PerMessageDeflate {
|
||||
* mode
|
||||
* @param {Number} maxPayload The maximum allowed message length
|
||||
*/
|
||||
constructor (options, isServer, maxPayload) {
|
||||
constructor(options, isServer, maxPayload) {
|
||||
this._maxPayload = maxPayload | 0;
|
||||
this._options = options || {};
|
||||
this._threshold = this._options.threshold !== undefined
|
||||
? this._options.threshold
|
||||
: 1024;
|
||||
this._threshold =
|
||||
this._options.threshold !== undefined ? this._options.threshold : 1024;
|
||||
this._isServer = !!isServer;
|
||||
this._deflate = null;
|
||||
this._inflate = null;
|
||||
@@ -67,9 +62,10 @@ class PerMessageDeflate {
|
||||
this.params = null;
|
||||
|
||||
if (!zlibLimiter) {
|
||||
const concurrency = this._options.concurrencyLimit !== undefined
|
||||
? this._options.concurrencyLimit
|
||||
: 10;
|
||||
const concurrency =
|
||||
this._options.concurrencyLimit !== undefined
|
||||
? this._options.concurrencyLimit
|
||||
: 10;
|
||||
zlibLimiter = new Limiter({ concurrency });
|
||||
}
|
||||
}
|
||||
@@ -77,7 +73,7 @@ class PerMessageDeflate {
|
||||
/**
|
||||
* @type {String}
|
||||
*/
|
||||
static get extensionName () {
|
||||
static get extensionName() {
|
||||
return 'permessage-deflate';
|
||||
}
|
||||
|
||||
@@ -87,7 +83,7 @@ class PerMessageDeflate {
|
||||
* @return {Object} Extension parameters
|
||||
* @public
|
||||
*/
|
||||
offer () {
|
||||
offer() {
|
||||
const params = {};
|
||||
|
||||
if (this._options.serverNoContextTakeover) {
|
||||
@@ -115,7 +111,7 @@ class PerMessageDeflate {
|
||||
* @return {Object} Accepted configuration
|
||||
* @public
|
||||
*/
|
||||
accept (configurations) {
|
||||
accept(configurations) {
|
||||
configurations = this.normalizeParams(configurations);
|
||||
|
||||
this.params = this._isServer
|
||||
@@ -130,22 +126,15 @@ class PerMessageDeflate {
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
cleanup () {
|
||||
cleanup() {
|
||||
if (this._inflate) {
|
||||
if (this._inflate[kWriteInProgress]) {
|
||||
this._inflate[kPendingClose] = true;
|
||||
} else {
|
||||
this._inflate.close();
|
||||
this._inflate = null;
|
||||
}
|
||||
this._inflate.close();
|
||||
this._inflate = null;
|
||||
}
|
||||
|
||||
if (this._deflate) {
|
||||
if (this._deflate[kWriteInProgress]) {
|
||||
this._deflate[kPendingClose] = true;
|
||||
} else {
|
||||
this._deflate.close();
|
||||
this._deflate = null;
|
||||
}
|
||||
this._deflate.close();
|
||||
this._deflate = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +145,7 @@ class PerMessageDeflate {
|
||||
* @return {Object} Accepted configuration
|
||||
* @private
|
||||
*/
|
||||
acceptAsServer (offers) {
|
||||
acceptAsServer(offers) {
|
||||
const opts = this._options;
|
||||
const accepted = offers.find((params) => {
|
||||
if (
|
||||
@@ -207,7 +196,7 @@ class PerMessageDeflate {
|
||||
* @return {Object} Accepted configuration
|
||||
* @private
|
||||
*/
|
||||
acceptAsClient (response) {
|
||||
acceptAsClient(response) {
|
||||
const params = response[0];
|
||||
|
||||
if (
|
||||
@@ -241,7 +230,7 @@ class PerMessageDeflate {
|
||||
* @return {Array} The offers/response with normalized parameters
|
||||
* @private
|
||||
*/
|
||||
normalizeParams (configurations) {
|
||||
normalizeParams(configurations) {
|
||||
configurations.forEach((params) => {
|
||||
Object.keys(params).forEach((key) => {
|
||||
var value = params[key];
|
||||
@@ -302,7 +291,7 @@ class PerMessageDeflate {
|
||||
* @param {Function} callback Callback
|
||||
* @public
|
||||
*/
|
||||
decompress (data, fin, callback) {
|
||||
decompress(data, fin, callback) {
|
||||
zlibLimiter.push((done) => {
|
||||
this._decompress(data, fin, (err, result) => {
|
||||
done();
|
||||
@@ -319,7 +308,7 @@ class PerMessageDeflate {
|
||||
* @param {Function} callback Callback
|
||||
* @public
|
||||
*/
|
||||
compress (data, fin, callback) {
|
||||
compress(data, fin, callback) {
|
||||
zlibLimiter.push((done) => {
|
||||
this._compress(data, fin, (err, result) => {
|
||||
done();
|
||||
@@ -336,31 +325,27 @@ class PerMessageDeflate {
|
||||
* @param {Function} callback Callback
|
||||
* @private
|
||||
*/
|
||||
_decompress (data, fin, callback) {
|
||||
_decompress(data, fin, callback) {
|
||||
const endpoint = this._isServer ? 'client' : 'server';
|
||||
|
||||
if (!this._inflate) {
|
||||
const key = `${endpoint}_max_window_bits`;
|
||||
const windowBits = typeof this.params[key] !== 'number'
|
||||
? zlib.Z_DEFAULT_WINDOWBITS
|
||||
: this.params[key];
|
||||
const windowBits =
|
||||
typeof this.params[key] !== 'number'
|
||||
? zlib.Z_DEFAULT_WINDOWBITS
|
||||
: this.params[key];
|
||||
|
||||
this._inflate = zlib.createInflateRaw(
|
||||
Object.assign(
|
||||
{},
|
||||
this._options.zlibInflateOptions,
|
||||
{ windowBits }
|
||||
)
|
||||
Object.assign({}, this._options.zlibInflateOptions, { windowBits })
|
||||
);
|
||||
this._inflate[kPerMessageDeflate] = this;
|
||||
this._inflate[kTotalLength] = 0;
|
||||
this._inflate[kBuffers] = [];
|
||||
this._inflate[kOwner] = this;
|
||||
this._inflate.on('error', inflateOnError);
|
||||
this._inflate.on('data', inflateOnData);
|
||||
}
|
||||
|
||||
this._inflate[kCallback] = callback;
|
||||
this._inflate[kWriteInProgress] = true;
|
||||
|
||||
this._inflate.write(data);
|
||||
if (fin) this._inflate.write(TRAILER);
|
||||
@@ -380,14 +365,10 @@ class PerMessageDeflate {
|
||||
this._inflate[kTotalLength]
|
||||
);
|
||||
|
||||
if (
|
||||
(fin && this.params[`${endpoint}_no_context_takeover`]) ||
|
||||
this._inflate[kPendingClose]
|
||||
) {
|
||||
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
||||
this._inflate.close();
|
||||
this._inflate = null;
|
||||
} else {
|
||||
this._inflate[kWriteInProgress] = false;
|
||||
this._inflate[kTotalLength] = 0;
|
||||
this._inflate[kBuffers] = [];
|
||||
}
|
||||
@@ -404,7 +385,7 @@ class PerMessageDeflate {
|
||||
* @param {Function} callback Callback
|
||||
* @private
|
||||
*/
|
||||
_compress (data, fin, callback) {
|
||||
_compress(data, fin, callback) {
|
||||
if (!data || data.length === 0) {
|
||||
process.nextTick(callback, null, EMPTY_BLOCK);
|
||||
return;
|
||||
@@ -414,37 +395,40 @@ class PerMessageDeflate {
|
||||
|
||||
if (!this._deflate) {
|
||||
const key = `${endpoint}_max_window_bits`;
|
||||
const windowBits = typeof this.params[key] !== 'number'
|
||||
? zlib.Z_DEFAULT_WINDOWBITS
|
||||
: this.params[key];
|
||||
const windowBits =
|
||||
typeof this.params[key] !== 'number'
|
||||
? zlib.Z_DEFAULT_WINDOWBITS
|
||||
: this.params[key];
|
||||
|
||||
this._deflate = zlib.createDeflateRaw(
|
||||
Object.assign(
|
||||
// TODO deprecate memLevel/level and recommend zlibDeflateOptions instead
|
||||
{
|
||||
memLevel: this._options.memLevel,
|
||||
level: this._options.level
|
||||
},
|
||||
this._options.zlibDeflateOptions,
|
||||
{ windowBits }
|
||||
)
|
||||
Object.assign({}, this._options.zlibDeflateOptions, { windowBits })
|
||||
);
|
||||
|
||||
this._deflate[kTotalLength] = 0;
|
||||
this._deflate[kBuffers] = [];
|
||||
|
||||
//
|
||||
// `zlib.DeflateRaw` emits an `'error'` event only when an attempt to use
|
||||
// it is made after it has already been closed. This cannot happen here,
|
||||
// so we only add a listener for the `'data'` event.
|
||||
// An `'error'` event is emitted, only on Node.js < 10.0.0, if the
|
||||
// `zlib.DeflateRaw` instance is closed while data is being processed.
|
||||
// This can happen if `PerMessageDeflate#cleanup()` is called at the wrong
|
||||
// time due to an abnormal WebSocket closure.
|
||||
//
|
||||
this._deflate.on('error', NOOP);
|
||||
this._deflate.on('data', deflateOnData);
|
||||
}
|
||||
|
||||
this._deflate[kWriteInProgress] = true;
|
||||
|
||||
this._deflate.write(data);
|
||||
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
||||
if (!this._deflate) {
|
||||
//
|
||||
// This `if` statement is only needed for Node.js < 10.0.0 because as of
|
||||
// commit https://github.com/nodejs/node/commit/5e3f5164, the flush
|
||||
// callback is no longer called if the deflate stream is closed while
|
||||
// data is being processed.
|
||||
//
|
||||
return;
|
||||
}
|
||||
|
||||
var data = bufferUtil.concat(
|
||||
this._deflate[kBuffers],
|
||||
this._deflate[kTotalLength]
|
||||
@@ -452,14 +436,10 @@ class PerMessageDeflate {
|
||||
|
||||
if (fin) data = data.slice(0, data.length - 4);
|
||||
|
||||
if (
|
||||
(fin && this.params[`${endpoint}_no_context_takeover`]) ||
|
||||
this._deflate[kPendingClose]
|
||||
) {
|
||||
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
||||
this._deflate.close();
|
||||
this._deflate = null;
|
||||
} else {
|
||||
this._deflate[kWriteInProgress] = false;
|
||||
this._deflate[kTotalLength] = 0;
|
||||
this._deflate[kBuffers] = [];
|
||||
}
|
||||
@@ -477,7 +457,7 @@ module.exports = PerMessageDeflate;
|
||||
* @param {Buffer} chunk A chunk of data
|
||||
* @private
|
||||
*/
|
||||
function deflateOnData (chunk) {
|
||||
function deflateOnData(chunk) {
|
||||
this[kBuffers].push(chunk);
|
||||
this[kTotalLength] += chunk.length;
|
||||
}
|
||||
@@ -488,19 +468,19 @@ function deflateOnData (chunk) {
|
||||
* @param {Buffer} chunk A chunk of data
|
||||
* @private
|
||||
*/
|
||||
function inflateOnData (chunk) {
|
||||
function inflateOnData(chunk) {
|
||||
this[kTotalLength] += chunk.length;
|
||||
|
||||
if (
|
||||
this[kOwner]._maxPayload < 1 ||
|
||||
this[kTotalLength] <= this[kOwner]._maxPayload
|
||||
this[kPerMessageDeflate]._maxPayload < 1 ||
|
||||
this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
|
||||
) {
|
||||
this[kBuffers].push(chunk);
|
||||
return;
|
||||
}
|
||||
|
||||
this[kError] = new RangeError('Max payload size exceeded');
|
||||
this[kError].closeCode = 1009;
|
||||
this[kError][kStatusCode] = 1009;
|
||||
this.removeListener('data', inflateOnData);
|
||||
this.reset();
|
||||
}
|
||||
@@ -511,11 +491,12 @@ function inflateOnData (chunk) {
|
||||
* @param {Error} err The emitted error
|
||||
* @private
|
||||
*/
|
||||
function inflateOnError (err) {
|
||||
function inflateOnError(err) {
|
||||
//
|
||||
// There is no need to call `Zlib#close()` as the handle is automatically
|
||||
// closed when an error is emitted.
|
||||
//
|
||||
this[kOwner]._inflate = null;
|
||||
this[kPerMessageDeflate]._inflate = null;
|
||||
err[kStatusCode] = 1007;
|
||||
this[kCallback](err);
|
||||
}
|
||||
|
509
node_modules/ws/lib/receiver.js
generated
vendored
509
node_modules/ws/lib/receiver.js
generated
vendored
@@ -1,13 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const safeBuffer = require('safe-buffer');
|
||||
const { Writable } = require('stream');
|
||||
|
||||
const PerMessageDeflate = require('./permessage-deflate');
|
||||
const bufferUtil = require('./buffer-util');
|
||||
const validation = require('./validation');
|
||||
const constants = require('./constants');
|
||||
|
||||
const Buffer = safeBuffer.Buffer;
|
||||
const {
|
||||
BINARY_TYPES,
|
||||
EMPTY_BUFFER,
|
||||
kStatusCode,
|
||||
kWebSocket
|
||||
} = require('./constants');
|
||||
const { concat, toArrayBuffer, unmask } = require('./buffer-util');
|
||||
const { isValidStatusCode, isValidUTF8 } = require('./validation');
|
||||
|
||||
const GET_INFO = 0;
|
||||
const GET_PAYLOAD_LENGTH_16 = 1;
|
||||
@@ -18,17 +21,22 @@ const INFLATING = 5;
|
||||
|
||||
/**
|
||||
* HyBi Receiver implementation.
|
||||
*
|
||||
* @extends stream.Writable
|
||||
*/
|
||||
class Receiver {
|
||||
class Receiver extends Writable {
|
||||
/**
|
||||
* Creates a Receiver instance.
|
||||
*
|
||||
* @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
|
||||
* @param {String} binaryType The type for binary data
|
||||
*/
|
||||
constructor (extensions, maxPayload, binaryType) {
|
||||
this._binaryType = binaryType || constants.BINARY_TYPES[0];
|
||||
constructor(binaryType, extensions, maxPayload) {
|
||||
super();
|
||||
|
||||
this._binaryType = binaryType || BINARY_TYPES[0];
|
||||
this[kWebSocket] = undefined;
|
||||
this._extensions = extensions || {};
|
||||
this._maxPayload = maxPayload | 0;
|
||||
|
||||
@@ -37,46 +45,43 @@ class Receiver {
|
||||
|
||||
this._compressed = false;
|
||||
this._payloadLength = 0;
|
||||
this._mask = undefined;
|
||||
this._fragmented = 0;
|
||||
this._masked = false;
|
||||
this._fin = false;
|
||||
this._mask = null;
|
||||
this._opcode = 0;
|
||||
|
||||
this._totalPayloadLength = 0;
|
||||
this._messageLength = 0;
|
||||
this._fragments = [];
|
||||
|
||||
this._cleanupCallback = null;
|
||||
this._isCleaningUp = false;
|
||||
this._hadError = false;
|
||||
this._loop = false;
|
||||
|
||||
this.add = this.add.bind(this);
|
||||
this.onmessage = null;
|
||||
this.onclose = null;
|
||||
this.onerror = null;
|
||||
this.onping = null;
|
||||
this.onpong = null;
|
||||
|
||||
this._state = GET_INFO;
|
||||
this._loop = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consumes `n` bytes from the buffered data, calls `cleanup` if necessary.
|
||||
* Implements `Writable.prototype._write()`.
|
||||
*
|
||||
* @param {Buffer} chunk The chunk of data to write
|
||||
* @param {String} encoding The character encoding of `chunk`
|
||||
* @param {Function} cb Callback
|
||||
*/
|
||||
_write(chunk, encoding, cb) {
|
||||
if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
|
||||
|
||||
this._bufferedBytes += chunk.length;
|
||||
this._buffers.push(chunk);
|
||||
this.startLoop(cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Consumes `n` bytes from the buffered data.
|
||||
*
|
||||
* @param {Number} n The number of bytes to consume
|
||||
* @return {(Buffer|null)} The consumed bytes or `null` if `n` bytes are not
|
||||
* available
|
||||
* @return {Buffer} The consumed bytes
|
||||
* @private
|
||||
*/
|
||||
consume (n) {
|
||||
if (this._bufferedBytes < n) {
|
||||
this._loop = false;
|
||||
if (this._isCleaningUp) this.cleanup(this._cleanupCallback);
|
||||
return null;
|
||||
}
|
||||
|
||||
consume(n) {
|
||||
this._bufferedBytes -= n;
|
||||
|
||||
if (n === this._buffers[0].length) return this._buffers.shift();
|
||||
@@ -105,74 +110,67 @@ class Receiver {
|
||||
return dst;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new data to the parser.
|
||||
*
|
||||
* @param {Buffer} chunk A chunk of data
|
||||
* @public
|
||||
*/
|
||||
add (chunk) {
|
||||
this._bufferedBytes += chunk.length;
|
||||
this._buffers.push(chunk);
|
||||
this.startLoop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the parsing loop.
|
||||
*
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
startLoop () {
|
||||
startLoop(cb) {
|
||||
var err;
|
||||
this._loop = true;
|
||||
|
||||
do {
|
||||
switch (this._state) {
|
||||
case GET_INFO:
|
||||
this.getInfo();
|
||||
err = this.getInfo();
|
||||
break;
|
||||
case GET_PAYLOAD_LENGTH_16:
|
||||
this.getPayloadLength16();
|
||||
err = this.getPayloadLength16();
|
||||
break;
|
||||
case GET_PAYLOAD_LENGTH_64:
|
||||
this.getPayloadLength64();
|
||||
err = this.getPayloadLength64();
|
||||
break;
|
||||
case GET_MASK:
|
||||
this.getMask();
|
||||
break;
|
||||
case GET_DATA:
|
||||
this.getData();
|
||||
err = this.getData(cb);
|
||||
break;
|
||||
default: // `INFLATING`
|
||||
default:
|
||||
// `INFLATING`
|
||||
this._loop = false;
|
||||
return;
|
||||
}
|
||||
} while (this._loop);
|
||||
|
||||
cb(err);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the first two bytes of a frame.
|
||||
*
|
||||
* @return {(RangeError|undefined)} A possible error
|
||||
* @private
|
||||
*/
|
||||
getInfo () {
|
||||
getInfo() {
|
||||
if (this._bufferedBytes < 2) {
|
||||
this._loop = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const buf = this.consume(2);
|
||||
if (buf === null) return;
|
||||
|
||||
if ((buf[0] & 0x30) !== 0x00) {
|
||||
this.error(
|
||||
new RangeError('Invalid WebSocket frame: RSV2 and RSV3 must be clear'),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
this._loop = false;
|
||||
return error(RangeError, 'RSV2 and RSV3 must be clear', true, 1002);
|
||||
}
|
||||
|
||||
const compressed = (buf[0] & 0x40) === 0x40;
|
||||
|
||||
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
||||
this.error(
|
||||
new RangeError('Invalid WebSocket frame: RSV1 must be clear'),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
this._loop = false;
|
||||
return error(RangeError, 'RSV1 must be clear', true, 1002);
|
||||
}
|
||||
|
||||
this._fin = (buf[0] & 0x80) === 0x80;
|
||||
@@ -181,130 +179,118 @@ class Receiver {
|
||||
|
||||
if (this._opcode === 0x00) {
|
||||
if (compressed) {
|
||||
this.error(
|
||||
new RangeError('Invalid WebSocket frame: RSV1 must be clear'),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
this._loop = false;
|
||||
return error(RangeError, 'RSV1 must be clear', true, 1002);
|
||||
}
|
||||
|
||||
if (!this._fragmented) {
|
||||
this.error(
|
||||
new RangeError('Invalid WebSocket frame: invalid opcode 0'),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
} else {
|
||||
this._opcode = this._fragmented;
|
||||
this._loop = false;
|
||||
return error(RangeError, 'invalid opcode 0', true, 1002);
|
||||
}
|
||||
|
||||
this._opcode = this._fragmented;
|
||||
} else if (this._opcode === 0x01 || this._opcode === 0x02) {
|
||||
if (this._fragmented) {
|
||||
this.error(
|
||||
new RangeError(
|
||||
`Invalid WebSocket frame: invalid opcode ${this._opcode}`
|
||||
),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
this._loop = false;
|
||||
return error(RangeError, `invalid opcode ${this._opcode}`, true, 1002);
|
||||
}
|
||||
|
||||
this._compressed = compressed;
|
||||
} else if (this._opcode > 0x07 && this._opcode < 0x0b) {
|
||||
if (!this._fin) {
|
||||
this.error(
|
||||
new RangeError('Invalid WebSocket frame: FIN must be set'),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
this._loop = false;
|
||||
return error(RangeError, 'FIN must be set', true, 1002);
|
||||
}
|
||||
|
||||
if (compressed) {
|
||||
this.error(
|
||||
new RangeError('Invalid WebSocket frame: RSV1 must be clear'),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
this._loop = false;
|
||||
return error(RangeError, 'RSV1 must be clear', true, 1002);
|
||||
}
|
||||
|
||||
if (this._payloadLength > 0x7d) {
|
||||
this.error(
|
||||
new RangeError(
|
||||
`Invalid WebSocket frame: invalid payload length ` +
|
||||
`${this._payloadLength}`
|
||||
),
|
||||
this._loop = false;
|
||||
return error(
|
||||
RangeError,
|
||||
`invalid payload length ${this._payloadLength}`,
|
||||
true,
|
||||
1002
|
||||
);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
this.error(
|
||||
new RangeError(
|
||||
`Invalid WebSocket frame: invalid opcode ${this._opcode}`
|
||||
),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
this._loop = false;
|
||||
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._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
|
||||
else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
|
||||
else this.haveLength();
|
||||
else return this.haveLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets extended payload length (7+16).
|
||||
*
|
||||
* @return {(RangeError|undefined)} A possible error
|
||||
* @private
|
||||
*/
|
||||
getPayloadLength16 () {
|
||||
const buf = this.consume(2);
|
||||
if (buf === null) return;
|
||||
getPayloadLength16() {
|
||||
if (this._bufferedBytes < 2) {
|
||||
this._loop = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this._payloadLength = buf.readUInt16BE(0, true);
|
||||
this.haveLength();
|
||||
this._payloadLength = this.consume(2).readUInt16BE(0);
|
||||
return this.haveLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets extended payload length (7+64).
|
||||
*
|
||||
* @return {(RangeError|undefined)} A possible error
|
||||
* @private
|
||||
*/
|
||||
getPayloadLength64 () {
|
||||
const buf = this.consume(8);
|
||||
if (buf === null) return;
|
||||
getPayloadLength64() {
|
||||
if (this._bufferedBytes < 8) {
|
||||
this._loop = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const num = buf.readUInt32BE(0, true);
|
||||
const buf = this.consume(8);
|
||||
const num = buf.readUInt32BE(0);
|
||||
|
||||
//
|
||||
// The maximum safe integer in JavaScript is 2^53 - 1. An error is returned
|
||||
// if payload length is greater than this number.
|
||||
//
|
||||
if (num > Math.pow(2, 53 - 32) - 1) {
|
||||
this.error(
|
||||
new RangeError(
|
||||
'Unsupported WebSocket frame: payload length > 2^53 - 1'
|
||||
),
|
||||
this._loop = false;
|
||||
return error(
|
||||
RangeError,
|
||||
'Unsupported WebSocket frame: payload length > 2^53 - 1',
|
||||
false,
|
||||
1009
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4, true);
|
||||
this.haveLength();
|
||||
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
||||
return this.haveLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Payload length has been read.
|
||||
*
|
||||
* @return {(RangeError|undefined)} A possible error
|
||||
* @private
|
||||
*/
|
||||
haveLength () {
|
||||
if (this._opcode < 0x08 && this.maxPayloadExceeded(this._payloadLength)) {
|
||||
return;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (this._masked) this._state = GET_MASK;
|
||||
@@ -316,64 +302,94 @@ class Receiver {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
getMask () {
|
||||
this._mask = this.consume(4);
|
||||
if (this._mask === null) return;
|
||||
getMask() {
|
||||
if (this._bufferedBytes < 4) {
|
||||
this._loop = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this._mask = this.consume(4);
|
||||
this._state = GET_DATA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads data bytes.
|
||||
*
|
||||
* @param {Function} cb Callback
|
||||
* @return {(Error|RangeError|undefined)} A possible error
|
||||
* @private
|
||||
*/
|
||||
getData () {
|
||||
var data = constants.EMPTY_BUFFER;
|
||||
getData(cb) {
|
||||
var data = EMPTY_BUFFER;
|
||||
|
||||
if (this._payloadLength) {
|
||||
if (this._bufferedBytes < this._payloadLength) {
|
||||
this._loop = false;
|
||||
return;
|
||||
}
|
||||
|
||||
data = this.consume(this._payloadLength);
|
||||
if (data === null) return;
|
||||
|
||||
if (this._masked) bufferUtil.unmask(data, this._mask);
|
||||
if (this._masked) unmask(data, this._mask);
|
||||
}
|
||||
|
||||
if (this._opcode > 0x07) {
|
||||
this.controlMessage(data);
|
||||
} else if (this._compressed) {
|
||||
if (this._opcode > 0x07) return this.controlMessage(data);
|
||||
|
||||
if (this._compressed) {
|
||||
this._state = INFLATING;
|
||||
this.decompress(data);
|
||||
} else if (this.pushFragment(data)) {
|
||||
this.dataMessage();
|
||||
this.decompress(data, cb);
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.length) {
|
||||
//
|
||||
// This message is not compressed so its lenght is the sum of the payload
|
||||
// length of all fragments.
|
||||
//
|
||||
this._messageLength = this._totalPayloadLength;
|
||||
this._fragments.push(data);
|
||||
}
|
||||
|
||||
return this.dataMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompresses data.
|
||||
*
|
||||
* @param {Buffer} data Compressed data
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
decompress (data) {
|
||||
decompress(data, cb) {
|
||||
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
||||
|
||||
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
||||
if (err) {
|
||||
this.error(err, err.closeCode === 1009 ? 1009 : 1007);
|
||||
return;
|
||||
if (err) return cb(err);
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
this._fragments.push(buf);
|
||||
}
|
||||
|
||||
if (this.pushFragment(buf)) this.dataMessage();
|
||||
this.startLoop();
|
||||
const er = this.dataMessage();
|
||||
if (er) return cb(er);
|
||||
|
||||
this.startLoop(cb);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a data message.
|
||||
*
|
||||
* @return {(Error|undefined)} A possible error
|
||||
* @private
|
||||
*/
|
||||
dataMessage () {
|
||||
dataMessage() {
|
||||
if (this._fin) {
|
||||
const messageLength = this._messageLength;
|
||||
const fragments = this._fragments;
|
||||
@@ -387,26 +403,23 @@ class Receiver {
|
||||
var data;
|
||||
|
||||
if (this._binaryType === 'nodebuffer') {
|
||||
data = toBuffer(fragments, messageLength);
|
||||
data = concat(fragments, messageLength);
|
||||
} else if (this._binaryType === 'arraybuffer') {
|
||||
data = toArrayBuffer(toBuffer(fragments, messageLength));
|
||||
data = toArrayBuffer(concat(fragments, messageLength));
|
||||
} else {
|
||||
data = fragments;
|
||||
}
|
||||
|
||||
this.onmessage(data);
|
||||
this.emit('message', data);
|
||||
} else {
|
||||
const buf = toBuffer(fragments, messageLength);
|
||||
const buf = concat(fragments, messageLength);
|
||||
|
||||
if (!validation.isValidUTF8(buf)) {
|
||||
this.error(
|
||||
new Error('Invalid WebSocket frame: invalid UTF-8 sequence'),
|
||||
1007
|
||||
);
|
||||
return;
|
||||
if (!isValidUTF8(buf)) {
|
||||
this._loop = false;
|
||||
return error(Error, 'invalid UTF-8 sequence', true, 1007);
|
||||
}
|
||||
|
||||
this.onmessage(buf.toString());
|
||||
this.emit('message', buf.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -417,173 +430,63 @@ class Receiver {
|
||||
* Handles a control message.
|
||||
*
|
||||
* @param {Buffer} data Data to handle
|
||||
* @return {(Error|RangeError|undefined)} A possible error
|
||||
* @private
|
||||
*/
|
||||
controlMessage (data) {
|
||||
controlMessage(data) {
|
||||
if (this._opcode === 0x08) {
|
||||
if (data.length === 0) {
|
||||
this._loop = false;
|
||||
this.onclose(1005, '');
|
||||
this.cleanup(this._cleanupCallback);
|
||||
} else if (data.length === 1) {
|
||||
this.error(
|
||||
new RangeError('Invalid WebSocket frame: invalid payload length 1'),
|
||||
1002
|
||||
);
|
||||
} else {
|
||||
const code = data.readUInt16BE(0, true);
|
||||
this._loop = false;
|
||||
|
||||
if (!validation.isValidStatusCode(code)) {
|
||||
this.error(
|
||||
new RangeError(
|
||||
`Invalid WebSocket frame: invalid status code ${code}`
|
||||
),
|
||||
1002
|
||||
);
|
||||
return;
|
||||
if (data.length === 0) {
|
||||
this.emit('conclude', 1005, '');
|
||||
this.end();
|
||||
} else if (data.length === 1) {
|
||||
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);
|
||||
}
|
||||
|
||||
const buf = data.slice(2);
|
||||
|
||||
if (!validation.isValidUTF8(buf)) {
|
||||
this.error(
|
||||
new Error('Invalid WebSocket frame: invalid UTF-8 sequence'),
|
||||
1007
|
||||
);
|
||||
return;
|
||||
if (!isValidUTF8(buf)) {
|
||||
return error(Error, 'invalid UTF-8 sequence', true, 1007);
|
||||
}
|
||||
|
||||
this._loop = false;
|
||||
this.onclose(code, buf.toString());
|
||||
this.cleanup(this._cleanupCallback);
|
||||
this.emit('conclude', code, buf.toString());
|
||||
this.end();
|
||||
}
|
||||
|
||||
return;
|
||||
} else if (this._opcode === 0x09) {
|
||||
this.emit('ping', data);
|
||||
} else {
|
||||
this.emit('pong', data);
|
||||
}
|
||||
|
||||
if (this._opcode === 0x09) this.onping(data);
|
||||
else this.onpong(data);
|
||||
|
||||
this._state = GET_INFO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an error.
|
||||
*
|
||||
* @param {Error} err The error
|
||||
* @param {Number} code Close code
|
||||
* @private
|
||||
*/
|
||||
error (err, code) {
|
||||
this._hadError = true;
|
||||
this._loop = false;
|
||||
this.onerror(err, code);
|
||||
this.cleanup(this._cleanupCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks payload size, disconnects socket when it exceeds `maxPayload`.
|
||||
*
|
||||
* @param {Number} length Payload length
|
||||
* @private
|
||||
*/
|
||||
maxPayloadExceeded (length) {
|
||||
if (length === 0 || this._maxPayload < 1) return false;
|
||||
|
||||
const fullLength = this._totalPayloadLength + length;
|
||||
|
||||
if (fullLength <= this._maxPayload) {
|
||||
this._totalPayloadLength = fullLength;
|
||||
return false;
|
||||
}
|
||||
|
||||
this.error(new RangeError('Max payload size exceeded'), 1009);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a fragment in the fragments array after checking that the sum of
|
||||
* fragment lengths does not exceed `maxPayload`.
|
||||
*
|
||||
* @param {Buffer} fragment The fragment to add
|
||||
* @return {Boolean} `true` if `maxPayload` is not exceeded, else `false`
|
||||
* @private
|
||||
*/
|
||||
pushFragment (fragment) {
|
||||
if (fragment.length === 0) return true;
|
||||
|
||||
const totalLength = this._messageLength + fragment.length;
|
||||
|
||||
if (this._maxPayload < 1 || totalLength <= this._maxPayload) {
|
||||
this._messageLength = totalLength;
|
||||
this._fragments.push(fragment);
|
||||
return true;
|
||||
}
|
||||
|
||||
this.error(new RangeError('Max payload size exceeded'), 1009);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases resources used by the receiver.
|
||||
*
|
||||
* @param {Function} cb Callback
|
||||
* @public
|
||||
*/
|
||||
cleanup (cb) {
|
||||
if (this._extensions === null) {
|
||||
if (cb) cb();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this._hadError && (this._loop || this._state === INFLATING)) {
|
||||
this._cleanupCallback = cb;
|
||||
this._isCleaningUp = true;
|
||||
return;
|
||||
}
|
||||
|
||||
this._extensions = null;
|
||||
this._fragments = null;
|
||||
this._buffers = null;
|
||||
this._mask = null;
|
||||
|
||||
this._cleanupCallback = null;
|
||||
this.onmessage = null;
|
||||
this.onclose = null;
|
||||
this.onerror = null;
|
||||
this.onping = null;
|
||||
this.onpong = null;
|
||||
|
||||
if (cb) cb();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Receiver;
|
||||
|
||||
/**
|
||||
* Makes a buffer from a list of fragments.
|
||||
* Builds an error object.
|
||||
*
|
||||
* @param {Buffer[]} fragments The list of fragments composing the message
|
||||
* @param {Number} messageLength The length of the message
|
||||
* @return {Buffer}
|
||||
* @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
|
||||
* @return {(Error|RangeError)} The error
|
||||
* @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;
|
||||
}
|
||||
function error(ErrorCtor, message, prefix, statusCode) {
|
||||
const err = new ErrorCtor(
|
||||
prefix ? `Invalid WebSocket frame: ${message}` : message
|
||||
);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
Error.captureStackTrace(err, error);
|
||||
err[kStatusCode] = statusCode;
|
||||
return err;
|
||||
}
|
||||
|
220
node_modules/ws/lib/sender.js
generated
vendored
220
node_modules/ws/lib/sender.js
generated
vendored
@@ -1,14 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const safeBuffer = require('safe-buffer');
|
||||
const crypto = require('crypto');
|
||||
const { randomBytes } = require('crypto');
|
||||
|
||||
const PerMessageDeflate = require('./permessage-deflate');
|
||||
const bufferUtil = require('./buffer-util');
|
||||
const validation = require('./validation');
|
||||
const constants = require('./constants');
|
||||
|
||||
const Buffer = safeBuffer.Buffer;
|
||||
const { EMPTY_BUFFER } = require('./constants');
|
||||
const { isValidStatusCode } = require('./validation');
|
||||
const { mask: applyMask, toBuffer } = require('./buffer-util');
|
||||
|
||||
/**
|
||||
* HyBi Sender implementation.
|
||||
@@ -20,7 +17,7 @@ class Sender {
|
||||
* @param {net.Socket} socket The connection socket
|
||||
* @param {Object} extensions An object containing the negotiated extensions
|
||||
*/
|
||||
constructor (socket, extensions) {
|
||||
constructor(socket, extensions) {
|
||||
this._extensions = extensions || {};
|
||||
this._socket = socket;
|
||||
|
||||
@@ -45,8 +42,8 @@ class Sender {
|
||||
* @return {Buffer[]} The framed data as a list of `Buffer` instances
|
||||
* @public
|
||||
*/
|
||||
static frame (data, options) {
|
||||
const merge = data.length < 1024 || (options.mask && options.readOnly);
|
||||
static frame(data, options) {
|
||||
const merge = options.mask && options.readOnly;
|
||||
var offset = options.mask ? 6 : 2;
|
||||
var payloadLength = data.length;
|
||||
|
||||
@@ -63,37 +60,31 @@ class Sender {
|
||||
target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
|
||||
if (options.rsv1) target[0] |= 0x40;
|
||||
|
||||
target[1] = payloadLength;
|
||||
|
||||
if (payloadLength === 126) {
|
||||
target.writeUInt16BE(data.length, 2, true);
|
||||
target.writeUInt16BE(data.length, 2);
|
||||
} else if (payloadLength === 127) {
|
||||
target.writeUInt32BE(0, 2, true);
|
||||
target.writeUInt32BE(data.length, 6, true);
|
||||
target.writeUInt32BE(0, 2);
|
||||
target.writeUInt32BE(data.length, 6);
|
||||
}
|
||||
|
||||
if (!options.mask) {
|
||||
target[1] = payloadLength;
|
||||
if (merge) {
|
||||
data.copy(target, offset);
|
||||
return [target];
|
||||
}
|
||||
if (!options.mask) return [target, data];
|
||||
|
||||
return [target, data];
|
||||
}
|
||||
const mask = randomBytes(4);
|
||||
|
||||
const mask = crypto.randomBytes(4);
|
||||
|
||||
target[1] = payloadLength | 0x80;
|
||||
target[1] |= 0x80;
|
||||
target[offset - 4] = mask[0];
|
||||
target[offset - 3] = mask[1];
|
||||
target[offset - 2] = mask[2];
|
||||
target[offset - 1] = mask[3];
|
||||
|
||||
if (merge) {
|
||||
bufferUtil.mask(data, mask, target, offset, data.length);
|
||||
applyMask(data, mask, target, offset, data.length);
|
||||
return [target];
|
||||
}
|
||||
|
||||
bufferUtil.mask(data, mask, data, 0, data.length);
|
||||
applyMask(data, mask, data, 0, data.length);
|
||||
return [target, data];
|
||||
}
|
||||
|
||||
@@ -106,19 +97,19 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @public
|
||||
*/
|
||||
close (code, data, mask, cb) {
|
||||
close(code, data, mask, cb) {
|
||||
var buf;
|
||||
|
||||
if (code === undefined) {
|
||||
buf = constants.EMPTY_BUFFER;
|
||||
} else if (typeof code !== 'number' || !validation.isValidStatusCode(code)) {
|
||||
buf = EMPTY_BUFFER;
|
||||
} else if (typeof code !== 'number' || !isValidStatusCode(code)) {
|
||||
throw new TypeError('First argument must be a valid error code number');
|
||||
} else if (data === undefined || data === '') {
|
||||
buf = Buffer.allocUnsafe(2);
|
||||
buf.writeUInt16BE(code, 0, true);
|
||||
buf.writeUInt16BE(code, 0);
|
||||
} else {
|
||||
buf = Buffer.allocUnsafe(2 + Buffer.byteLength(data));
|
||||
buf.writeUInt16BE(code, 0, true);
|
||||
buf.writeUInt16BE(code, 0);
|
||||
buf.write(data, 2);
|
||||
}
|
||||
|
||||
@@ -137,14 +128,17 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
doClose (data, mask, cb) {
|
||||
this.sendFrame(Sender.frame(data, {
|
||||
fin: true,
|
||||
rsv1: false,
|
||||
opcode: 0x08,
|
||||
mask,
|
||||
readOnly: false
|
||||
}), cb);
|
||||
doClose(data, mask, cb) {
|
||||
this.sendFrame(
|
||||
Sender.frame(data, {
|
||||
fin: true,
|
||||
rsv1: false,
|
||||
opcode: 0x08,
|
||||
mask,
|
||||
readOnly: false
|
||||
}),
|
||||
cb
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,24 +149,13 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @public
|
||||
*/
|
||||
ping (data, mask, cb) {
|
||||
var readOnly = true;
|
||||
|
||||
if (!Buffer.isBuffer(data)) {
|
||||
if (data instanceof ArrayBuffer) {
|
||||
data = Buffer.from(data);
|
||||
} else if (ArrayBuffer.isView(data)) {
|
||||
data = viewToBuffer(data);
|
||||
} else {
|
||||
data = Buffer.from(data);
|
||||
readOnly = false;
|
||||
}
|
||||
}
|
||||
ping(data, mask, cb) {
|
||||
const buf = toBuffer(data);
|
||||
|
||||
if (this._deflating) {
|
||||
this.enqueue([this.doPing, data, mask, readOnly, cb]);
|
||||
this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
|
||||
} else {
|
||||
this.doPing(data, mask, readOnly, cb);
|
||||
this.doPing(buf, mask, toBuffer.readOnly, cb);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,14 +168,17 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
doPing (data, mask, readOnly, cb) {
|
||||
this.sendFrame(Sender.frame(data, {
|
||||
fin: true,
|
||||
rsv1: false,
|
||||
opcode: 0x09,
|
||||
mask,
|
||||
readOnly
|
||||
}), cb);
|
||||
doPing(data, mask, readOnly, cb) {
|
||||
this.sendFrame(
|
||||
Sender.frame(data, {
|
||||
fin: true,
|
||||
rsv1: false,
|
||||
opcode: 0x09,
|
||||
mask,
|
||||
readOnly
|
||||
}),
|
||||
cb
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,24 +189,13 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @public
|
||||
*/
|
||||
pong (data, mask, cb) {
|
||||
var readOnly = true;
|
||||
|
||||
if (!Buffer.isBuffer(data)) {
|
||||
if (data instanceof ArrayBuffer) {
|
||||
data = Buffer.from(data);
|
||||
} else if (ArrayBuffer.isView(data)) {
|
||||
data = viewToBuffer(data);
|
||||
} else {
|
||||
data = Buffer.from(data);
|
||||
readOnly = false;
|
||||
}
|
||||
}
|
||||
pong(data, mask, cb) {
|
||||
const buf = toBuffer(data);
|
||||
|
||||
if (this._deflating) {
|
||||
this.enqueue([this.doPong, data, mask, readOnly, cb]);
|
||||
this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
|
||||
} else {
|
||||
this.doPong(data, mask, readOnly, cb);
|
||||
this.doPong(buf, mask, toBuffer.readOnly, cb);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,14 +208,17 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
doPong (data, mask, readOnly, cb) {
|
||||
this.sendFrame(Sender.frame(data, {
|
||||
fin: true,
|
||||
rsv1: false,
|
||||
opcode: 0x0a,
|
||||
mask,
|
||||
readOnly
|
||||
}), cb);
|
||||
doPong(data, mask, readOnly, cb) {
|
||||
this.sendFrame(
|
||||
Sender.frame(data, {
|
||||
fin: true,
|
||||
rsv1: false,
|
||||
opcode: 0x0a,
|
||||
mask,
|
||||
readOnly
|
||||
}),
|
||||
cb
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,28 +233,16 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @public
|
||||
*/
|
||||
send (data, options, cb) {
|
||||
send(data, options, cb) {
|
||||
const buf = toBuffer(data);
|
||||
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
||||
var opcode = options.binary ? 2 : 1;
|
||||
var rsv1 = options.compress;
|
||||
var readOnly = true;
|
||||
|
||||
if (!Buffer.isBuffer(data)) {
|
||||
if (data instanceof ArrayBuffer) {
|
||||
data = Buffer.from(data);
|
||||
} else if (ArrayBuffer.isView(data)) {
|
||||
data = viewToBuffer(data);
|
||||
} else {
|
||||
data = Buffer.from(data);
|
||||
readOnly = false;
|
||||
}
|
||||
}
|
||||
|
||||
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
||||
|
||||
if (this._firstFragment) {
|
||||
this._firstFragment = false;
|
||||
if (rsv1 && perMessageDeflate) {
|
||||
rsv1 = data.length >= perMessageDeflate._threshold;
|
||||
rsv1 = buf.length >= perMessageDeflate._threshold;
|
||||
}
|
||||
this._compress = rsv1;
|
||||
} else {
|
||||
@@ -292,22 +258,25 @@ class Sender {
|
||||
rsv1,
|
||||
opcode,
|
||||
mask: options.mask,
|
||||
readOnly
|
||||
readOnly: toBuffer.readOnly
|
||||
};
|
||||
|
||||
if (this._deflating) {
|
||||
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
||||
this.enqueue([this.dispatch, buf, this._compress, opts, cb]);
|
||||
} else {
|
||||
this.dispatch(data, this._compress, opts, cb);
|
||||
this.dispatch(buf, this._compress, opts, cb);
|
||||
}
|
||||
} else {
|
||||
this.sendFrame(Sender.frame(data, {
|
||||
fin: options.fin,
|
||||
rsv1: false,
|
||||
opcode,
|
||||
mask: options.mask,
|
||||
readOnly
|
||||
}), cb);
|
||||
this.sendFrame(
|
||||
Sender.frame(buf, {
|
||||
fin: options.fin,
|
||||
rsv1: false,
|
||||
opcode,
|
||||
mask: options.mask,
|
||||
readOnly: toBuffer.readOnly
|
||||
}),
|
||||
cb
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,7 +294,7 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
dispatch (data, compress, options, cb) {
|
||||
dispatch(data, compress, options, cb) {
|
||||
if (!compress) {
|
||||
this.sendFrame(Sender.frame(data, options), cb);
|
||||
return;
|
||||
@@ -335,9 +304,9 @@ class Sender {
|
||||
|
||||
this._deflating = true;
|
||||
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
||||
this._deflating = false;
|
||||
options.readOnly = false;
|
||||
this.sendFrame(Sender.frame(buf, options), cb);
|
||||
this._deflating = false;
|
||||
this.dequeue();
|
||||
});
|
||||
}
|
||||
@@ -347,7 +316,7 @@ class Sender {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
dequeue () {
|
||||
dequeue() {
|
||||
while (!this._deflating && this._queue.length) {
|
||||
const params = this._queue.shift();
|
||||
|
||||
@@ -362,7 +331,7 @@ class Sender {
|
||||
* @param {Array} params Send operation parameters.
|
||||
* @private
|
||||
*/
|
||||
enqueue (params) {
|
||||
enqueue(params) {
|
||||
this._bufferedBytes += params[1].length;
|
||||
this._queue.push(params);
|
||||
}
|
||||
@@ -374,10 +343,12 @@ class Sender {
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
sendFrame (list, cb) {
|
||||
sendFrame(list, cb) {
|
||||
if (list.length === 2) {
|
||||
this._socket.cork();
|
||||
this._socket.write(list[0]);
|
||||
this._socket.write(list[1], cb);
|
||||
this._socket.uncork();
|
||||
} else {
|
||||
this._socket.write(list[0], cb);
|
||||
}
|
||||
@@ -385,20 +356,3 @@ class Sender {
|
||||
}
|
||||
|
||||
module.exports = Sender;
|
||||
|
||||
/**
|
||||
* Converts an `ArrayBuffer` view into a buffer.
|
||||
*
|
||||
* @param {(DataView|TypedArray)} view The view to convert
|
||||
* @return {Buffer} Converted view
|
||||
* @private
|
||||
*/
|
||||
function viewToBuffer (view) {
|
||||
const buf = Buffer.from(view.buffer);
|
||||
|
||||
if (view.byteLength !== view.buffer.byteLength) {
|
||||
return buf.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
7
node_modules/ws/lib/validation.js
generated
vendored
7
node_modules/ws/lib/validation.js
generated
vendored
@@ -3,9 +3,10 @@
|
||||
try {
|
||||
const isValidUTF8 = require('utf-8-validate');
|
||||
|
||||
exports.isValidUTF8 = typeof isValidUTF8 === 'object'
|
||||
? isValidUTF8.Validation.isValidUTF8 // utf-8-validate@<3.0.0
|
||||
: isValidUTF8;
|
||||
exports.isValidUTF8 =
|
||||
typeof isValidUTF8 === 'object'
|
||||
? isValidUTF8.Validation.isValidUTF8 // utf-8-validate@<3.0.0
|
||||
: isValidUTF8;
|
||||
} catch (e) /* istanbul ignore next */ {
|
||||
exports.isValidUTF8 = () => true;
|
||||
}
|
||||
|
198
node_modules/ws/lib/websocket-server.js
generated
vendored
198
node_modules/ws/lib/websocket-server.js
generated
vendored
@@ -1,17 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const safeBuffer = require('safe-buffer');
|
||||
const EventEmitter = require('events');
|
||||
const crypto = require('crypto');
|
||||
const http = require('http');
|
||||
const url = require('url');
|
||||
|
||||
const PerMessageDeflate = require('./permessage-deflate');
|
||||
const extension = require('./extension');
|
||||
const constants = require('./constants');
|
||||
const WebSocket = require('./websocket');
|
||||
const { GUID } = require('./constants');
|
||||
|
||||
const Buffer = safeBuffer.Buffer;
|
||||
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
||||
|
||||
/**
|
||||
* Class representing a WebSocket server.
|
||||
@@ -23,34 +21,41 @@ class WebSocketServer extends EventEmitter {
|
||||
* Create a `WebSocketServer` instance.
|
||||
*
|
||||
* @param {Object} options Configuration options
|
||||
* @param {Number} options.backlog The maximum length of the queue of pending
|
||||
* connections
|
||||
* @param {Boolean} options.clientTracking Specifies whether or not to track
|
||||
* clients
|
||||
* @param {Function} options.handleProtocols An hook to handle protocols
|
||||
* @param {String} options.host The hostname where to bind the server
|
||||
* @param {Number} options.maxPayload The maximum allowed message size
|
||||
* @param {Boolean} options.noServer Enable no server mode
|
||||
* @param {String} options.path Accept only connections matching this path
|
||||
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable
|
||||
* permessage-deflate
|
||||
* @param {Number} options.port The port where to bind the server
|
||||
* @param {http.Server} options.server A pre-created HTTP/S server to use
|
||||
* @param {Function} options.verifyClient An hook to reject connections
|
||||
* @param {Function} options.handleProtocols An hook to handle protocols
|
||||
* @param {String} options.path Accept only connections matching this path
|
||||
* @param {Boolean} options.noServer Enable no server mode
|
||||
* @param {Boolean} options.clientTracking Specifies whether or not to track clients
|
||||
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable permessage-deflate
|
||||
* @param {Number} options.maxPayload The maximum allowed message size
|
||||
* @param {Function} callback A listener for the `listening` event
|
||||
*/
|
||||
constructor (options, callback) {
|
||||
constructor(options, callback) {
|
||||
super();
|
||||
|
||||
options = Object.assign({
|
||||
maxPayload: 100 * 1024 * 1024,
|
||||
perMessageDeflate: false,
|
||||
handleProtocols: null,
|
||||
clientTracking: true,
|
||||
verifyClient: null,
|
||||
noServer: false,
|
||||
backlog: null, // use default (511 as implemented in net.js)
|
||||
server: null,
|
||||
host: null,
|
||||
path: null,
|
||||
port: null
|
||||
}, options);
|
||||
options = Object.assign(
|
||||
{
|
||||
maxPayload: 100 * 1024 * 1024,
|
||||
perMessageDeflate: false,
|
||||
handleProtocols: null,
|
||||
clientTracking: true,
|
||||
verifyClient: null,
|
||||
noServer: false,
|
||||
backlog: null, // use default (511 as implemented in net.js)
|
||||
server: null,
|
||||
host: null,
|
||||
path: null,
|
||||
port: null
|
||||
},
|
||||
options
|
||||
);
|
||||
|
||||
if (options.port == null && !options.server && !options.noServer) {
|
||||
throw new TypeError(
|
||||
@@ -68,7 +73,12 @@ class WebSocketServer extends EventEmitter {
|
||||
});
|
||||
res.end(body);
|
||||
});
|
||||
this._server.listen(options.port, options.host, options.backlog, callback);
|
||||
this._server.listen(
|
||||
options.port,
|
||||
options.host,
|
||||
options.backlog,
|
||||
callback
|
||||
);
|
||||
} else if (options.server) {
|
||||
this._server = options.server;
|
||||
}
|
||||
@@ -99,7 +109,7 @@ class WebSocketServer extends EventEmitter {
|
||||
* @return {(Object|String|null)} The address of the server
|
||||
* @public
|
||||
*/
|
||||
address () {
|
||||
address() {
|
||||
if (this.options.noServer) {
|
||||
throw new Error('The server is operating in "noServer" mode');
|
||||
}
|
||||
@@ -114,7 +124,9 @@ class WebSocketServer extends EventEmitter {
|
||||
* @param {Function} cb Callback
|
||||
* @public
|
||||
*/
|
||||
close (cb) {
|
||||
close(cb) {
|
||||
if (cb) this.once('close', cb);
|
||||
|
||||
//
|
||||
// Terminate all associated clients.
|
||||
//
|
||||
@@ -131,10 +143,13 @@ class WebSocketServer extends EventEmitter {
|
||||
//
|
||||
// Close the http server if it was internally created.
|
||||
//
|
||||
if (this.options.port != null) return server.close(cb);
|
||||
if (this.options.port != null) {
|
||||
server.close(() => this.emit('close'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (cb) cb();
|
||||
process.nextTick(emitClose, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,9 +159,12 @@ class WebSocketServer extends EventEmitter {
|
||||
* @return {Boolean} `true` if the request is valid, else `false`
|
||||
* @public
|
||||
*/
|
||||
shouldHandle (req) {
|
||||
if (this.options.path && url.parse(req.url).pathname !== this.options.path) {
|
||||
return false;
|
||||
shouldHandle(req) {
|
||||
if (this.options.path) {
|
||||
const index = req.url.indexOf('?');
|
||||
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
||||
|
||||
if (pathname !== this.options.path) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -161,18 +179,25 @@ class WebSocketServer extends EventEmitter {
|
||||
* @param {Function} cb Callback
|
||||
* @public
|
||||
*/
|
||||
handleUpgrade (req, socket, head, cb) {
|
||||
handleUpgrade(req, socket, head, cb) {
|
||||
socket.on('error', socketOnError);
|
||||
|
||||
const key =
|
||||
req.headers['sec-websocket-key'] !== undefined
|
||||
? req.headers['sec-websocket-key'].trim()
|
||||
: false;
|
||||
const version = +req.headers['sec-websocket-version'];
|
||||
const extensions = {};
|
||||
|
||||
if (
|
||||
req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket' ||
|
||||
!req.headers['sec-websocket-key'] || (version !== 8 && version !== 13) ||
|
||||
req.method !== 'GET' ||
|
||||
req.headers.upgrade.toLowerCase() !== 'websocket' ||
|
||||
!key ||
|
||||
!keyRegex.test(key) ||
|
||||
(version !== 8 && version !== 13) ||
|
||||
!this.shouldHandle(req)
|
||||
) {
|
||||
return abortConnection(socket, 400);
|
||||
return abortHandshake(socket, 400);
|
||||
}
|
||||
|
||||
if (this.options.perMessageDeflate) {
|
||||
@@ -183,60 +208,49 @@ class WebSocketServer extends EventEmitter {
|
||||
);
|
||||
|
||||
try {
|
||||
const offers = extension.parse(
|
||||
req.headers['sec-websocket-extensions']
|
||||
);
|
||||
const offers = extension.parse(req.headers['sec-websocket-extensions']);
|
||||
|
||||
if (offers[PerMessageDeflate.extensionName]) {
|
||||
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
||||
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
||||
}
|
||||
} catch (err) {
|
||||
return abortConnection(socket, 400);
|
||||
return abortHandshake(socket, 400);
|
||||
}
|
||||
}
|
||||
|
||||
var protocol = (req.headers['sec-websocket-protocol'] || '').split(/, */);
|
||||
|
||||
//
|
||||
// Optionally call external protocol selection handler.
|
||||
//
|
||||
if (this.options.handleProtocols) {
|
||||
protocol = this.options.handleProtocols(protocol, req);
|
||||
if (protocol === false) return abortConnection(socket, 401);
|
||||
} else {
|
||||
protocol = protocol[0];
|
||||
}
|
||||
|
||||
//
|
||||
// Optionally call external client verification handler.
|
||||
//
|
||||
if (this.options.verifyClient) {
|
||||
const info = {
|
||||
origin: req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
|
||||
origin:
|
||||
req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
|
||||
secure: !!(req.connection.authorized || req.connection.encrypted),
|
||||
req
|
||||
};
|
||||
|
||||
if (this.options.verifyClient.length === 2) {
|
||||
this.options.verifyClient(info, (verified, code, message) => {
|
||||
if (!verified) return abortConnection(socket, code || 401, message);
|
||||
this.options.verifyClient(info, (verified, code, message, headers) => {
|
||||
if (!verified) {
|
||||
return abortHandshake(socket, code || 401, message, headers);
|
||||
}
|
||||
|
||||
this.completeUpgrade(protocol, extensions, req, socket, head, cb);
|
||||
this.completeUpgrade(key, extensions, req, socket, head, cb);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.options.verifyClient(info)) return abortConnection(socket, 401);
|
||||
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
||||
}
|
||||
|
||||
this.completeUpgrade(protocol, extensions, req, socket, head, cb);
|
||||
this.completeUpgrade(key, extensions, req, socket, head, cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrade the connection to WebSocket.
|
||||
*
|
||||
* @param {String} protocol The chosen subprotocol
|
||||
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
||||
* @param {Object} extensions The accepted extensions
|
||||
* @param {http.IncomingMessage} req The request object
|
||||
* @param {net.Socket} socket The network socket between the server and client
|
||||
@@ -244,29 +258,45 @@ class WebSocketServer extends EventEmitter {
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
completeUpgrade (protocol, extensions, req, socket, head, cb) {
|
||||
completeUpgrade(key, extensions, req, socket, head, cb) {
|
||||
//
|
||||
// Destroy the socket if the client has already sent a FIN packet.
|
||||
//
|
||||
if (!socket.readable || !socket.writable) return socket.destroy();
|
||||
|
||||
const key = crypto.createHash('sha1')
|
||||
.update(req.headers['sec-websocket-key'] + constants.GUID, 'binary')
|
||||
const digest = crypto
|
||||
.createHash('sha1')
|
||||
.update(key + GUID)
|
||||
.digest('base64');
|
||||
|
||||
const headers = [
|
||||
'HTTP/1.1 101 Switching Protocols',
|
||||
'Upgrade: websocket',
|
||||
'Connection: Upgrade',
|
||||
`Sec-WebSocket-Accept: ${key}`
|
||||
`Sec-WebSocket-Accept: ${digest}`
|
||||
];
|
||||
|
||||
const ws = new WebSocket(null);
|
||||
var protocol = req.headers['sec-websocket-protocol'];
|
||||
|
||||
if (protocol) {
|
||||
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
||||
ws.protocol = protocol;
|
||||
protocol = protocol.trim().split(/ *, */);
|
||||
|
||||
//
|
||||
// Optionally call external protocol selection handler.
|
||||
//
|
||||
if (this.options.handleProtocols) {
|
||||
protocol = this.options.handleProtocols(protocol, req);
|
||||
} else {
|
||||
protocol = protocol[0];
|
||||
}
|
||||
|
||||
if (protocol) {
|
||||
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
||||
ws.protocol = protocol;
|
||||
}
|
||||
}
|
||||
|
||||
if (extensions[PerMessageDeflate.extensionName]) {
|
||||
const params = extensions[PerMessageDeflate.extensionName].params;
|
||||
const value = extension.format({
|
||||
@@ -306,22 +336,32 @@ module.exports = WebSocketServer;
|
||||
* @return {Function} A function that will remove the added listeners when called
|
||||
* @private
|
||||
*/
|
||||
function addListeners (server, map) {
|
||||
function addListeners(server, map) {
|
||||
for (const event of Object.keys(map)) server.on(event, map[event]);
|
||||
|
||||
return function removeListeners () {
|
||||
return function removeListeners() {
|
||||
for (const event of Object.keys(map)) {
|
||||
server.removeListener(event, map[event]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit a `'close'` event on an `EventEmitter`.
|
||||
*
|
||||
* @param {EventEmitter} server The event emitter
|
||||
* @private
|
||||
*/
|
||||
function emitClose(server) {
|
||||
server.emit('close');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle premature socket errors.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
function socketOnError () {
|
||||
function socketOnError() {
|
||||
this.destroy();
|
||||
}
|
||||
|
||||
@@ -331,18 +371,28 @@ function socketOnError () {
|
||||
* @param {net.Socket} socket The socket of the upgrade request
|
||||
* @param {Number} code The HTTP response status code
|
||||
* @param {String} [message] The HTTP response body
|
||||
* @param {Object} [headers] Additional HTTP response headers
|
||||
* @private
|
||||
*/
|
||||
function abortConnection (socket, code, message) {
|
||||
function abortHandshake(socket, code, message, headers) {
|
||||
if (socket.writable) {
|
||||
message = message || http.STATUS_CODES[code];
|
||||
headers = Object.assign(
|
||||
{
|
||||
Connection: 'close',
|
||||
'Content-type': 'text/html',
|
||||
'Content-Length': Buffer.byteLength(message)
|
||||
},
|
||||
headers
|
||||
);
|
||||
|
||||
socket.write(
|
||||
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
|
||||
'Connection: close\r\n' +
|
||||
'Content-type: text/html\r\n' +
|
||||
`Content-Length: ${Buffer.byteLength(message)}\r\n` +
|
||||
'\r\n' +
|
||||
message
|
||||
Object.keys(headers)
|
||||
.map((h) => `${h}: ${headers[h]}`)
|
||||
.join('\r\n') +
|
||||
'\r\n\r\n' +
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
|
840
node_modules/ws/lib/websocket.js
generated
vendored
840
node_modules/ws/lib/websocket.js
generated
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user