claude-threads 1.4.7 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/bin/claude-threads-daemon +25 -1
- package/dist/index.js +19182 -19106
- package/dist/mcp/permission-server.js +2864 -4
- package/package.json +3 -1
|
@@ -15267,6 +15267,2857 @@ var require_yauzl = __commonJS((exports) => {
|
|
|
15267
15267
|
}
|
|
15268
15268
|
});
|
|
15269
15269
|
|
|
15270
|
+
// node_modules/ws/lib/constants.js
|
|
15271
|
+
var require_constants2 = __commonJS((exports, module) => {
|
|
15272
|
+
var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
15273
|
+
var hasBlob = typeof Blob !== "undefined";
|
|
15274
|
+
if (hasBlob)
|
|
15275
|
+
BINARY_TYPES.push("blob");
|
|
15276
|
+
module.exports = {
|
|
15277
|
+
BINARY_TYPES,
|
|
15278
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
15279
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
15280
|
+
hasBlob,
|
|
15281
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
15282
|
+
kListener: Symbol("kListener"),
|
|
15283
|
+
kStatusCode: Symbol("status-code"),
|
|
15284
|
+
kWebSocket: Symbol("websocket"),
|
|
15285
|
+
NOOP: () => {}
|
|
15286
|
+
};
|
|
15287
|
+
});
|
|
15288
|
+
|
|
15289
|
+
// node_modules/ws/lib/buffer-util.js
|
|
15290
|
+
var require_buffer_util = __commonJS((exports, module) => {
|
|
15291
|
+
var { EMPTY_BUFFER } = require_constants2();
|
|
15292
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
15293
|
+
function concat(list, totalLength) {
|
|
15294
|
+
if (list.length === 0)
|
|
15295
|
+
return EMPTY_BUFFER;
|
|
15296
|
+
if (list.length === 1)
|
|
15297
|
+
return list[0];
|
|
15298
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
15299
|
+
let offset = 0;
|
|
15300
|
+
for (let i = 0;i < list.length; i++) {
|
|
15301
|
+
const buf = list[i];
|
|
15302
|
+
target.set(buf, offset);
|
|
15303
|
+
offset += buf.length;
|
|
15304
|
+
}
|
|
15305
|
+
if (offset < totalLength) {
|
|
15306
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
15307
|
+
}
|
|
15308
|
+
return target;
|
|
15309
|
+
}
|
|
15310
|
+
function _mask(source, mask, output, offset, length) {
|
|
15311
|
+
for (let i = 0;i < length; i++) {
|
|
15312
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
15313
|
+
}
|
|
15314
|
+
}
|
|
15315
|
+
function _unmask(buffer, mask) {
|
|
15316
|
+
for (let i = 0;i < buffer.length; i++) {
|
|
15317
|
+
buffer[i] ^= mask[i & 3];
|
|
15318
|
+
}
|
|
15319
|
+
}
|
|
15320
|
+
function toArrayBuffer(buf) {
|
|
15321
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
15322
|
+
return buf.buffer;
|
|
15323
|
+
}
|
|
15324
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
15325
|
+
}
|
|
15326
|
+
function toBuffer(data) {
|
|
15327
|
+
toBuffer.readOnly = true;
|
|
15328
|
+
if (Buffer.isBuffer(data))
|
|
15329
|
+
return data;
|
|
15330
|
+
let buf;
|
|
15331
|
+
if (data instanceof ArrayBuffer) {
|
|
15332
|
+
buf = new FastBuffer(data);
|
|
15333
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
15334
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
15335
|
+
} else {
|
|
15336
|
+
buf = Buffer.from(data);
|
|
15337
|
+
toBuffer.readOnly = false;
|
|
15338
|
+
}
|
|
15339
|
+
return buf;
|
|
15340
|
+
}
|
|
15341
|
+
module.exports = {
|
|
15342
|
+
concat,
|
|
15343
|
+
mask: _mask,
|
|
15344
|
+
toArrayBuffer,
|
|
15345
|
+
toBuffer,
|
|
15346
|
+
unmask: _unmask
|
|
15347
|
+
};
|
|
15348
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
15349
|
+
try {
|
|
15350
|
+
const bufferUtil = (()=>{throw new Error("Cannot require module "+"bufferutil");})();
|
|
15351
|
+
module.exports.mask = function(source, mask, output, offset, length) {
|
|
15352
|
+
if (length < 48)
|
|
15353
|
+
_mask(source, mask, output, offset, length);
|
|
15354
|
+
else
|
|
15355
|
+
bufferUtil.mask(source, mask, output, offset, length);
|
|
15356
|
+
};
|
|
15357
|
+
module.exports.unmask = function(buffer, mask) {
|
|
15358
|
+
if (buffer.length < 32)
|
|
15359
|
+
_unmask(buffer, mask);
|
|
15360
|
+
else
|
|
15361
|
+
bufferUtil.unmask(buffer, mask);
|
|
15362
|
+
};
|
|
15363
|
+
} catch (e) {}
|
|
15364
|
+
}
|
|
15365
|
+
});
|
|
15366
|
+
|
|
15367
|
+
// node_modules/ws/lib/limiter.js
|
|
15368
|
+
var require_limiter = __commonJS((exports, module) => {
|
|
15369
|
+
var kDone = Symbol("kDone");
|
|
15370
|
+
var kRun = Symbol("kRun");
|
|
15371
|
+
|
|
15372
|
+
class Limiter {
|
|
15373
|
+
constructor(concurrency) {
|
|
15374
|
+
this[kDone] = () => {
|
|
15375
|
+
this.pending--;
|
|
15376
|
+
this[kRun]();
|
|
15377
|
+
};
|
|
15378
|
+
this.concurrency = concurrency || Infinity;
|
|
15379
|
+
this.jobs = [];
|
|
15380
|
+
this.pending = 0;
|
|
15381
|
+
}
|
|
15382
|
+
add(job) {
|
|
15383
|
+
this.jobs.push(job);
|
|
15384
|
+
this[kRun]();
|
|
15385
|
+
}
|
|
15386
|
+
[kRun]() {
|
|
15387
|
+
if (this.pending === this.concurrency)
|
|
15388
|
+
return;
|
|
15389
|
+
if (this.jobs.length) {
|
|
15390
|
+
const job = this.jobs.shift();
|
|
15391
|
+
this.pending++;
|
|
15392
|
+
job(this[kDone]);
|
|
15393
|
+
}
|
|
15394
|
+
}
|
|
15395
|
+
}
|
|
15396
|
+
module.exports = Limiter;
|
|
15397
|
+
});
|
|
15398
|
+
|
|
15399
|
+
// node_modules/ws/lib/permessage-deflate.js
|
|
15400
|
+
var require_permessage_deflate = __commonJS((exports, module) => {
|
|
15401
|
+
var zlib = __require("zlib");
|
|
15402
|
+
var bufferUtil = require_buffer_util();
|
|
15403
|
+
var Limiter = require_limiter();
|
|
15404
|
+
var { kStatusCode } = require_constants2();
|
|
15405
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
15406
|
+
var TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
15407
|
+
var kPerMessageDeflate = Symbol("permessage-deflate");
|
|
15408
|
+
var kTotalLength = Symbol("total-length");
|
|
15409
|
+
var kCallback = Symbol("callback");
|
|
15410
|
+
var kBuffers = Symbol("buffers");
|
|
15411
|
+
var kError = Symbol("error");
|
|
15412
|
+
var zlibLimiter;
|
|
15413
|
+
|
|
15414
|
+
class PerMessageDeflate {
|
|
15415
|
+
constructor(options2, isServer, maxPayload) {
|
|
15416
|
+
this._maxPayload = maxPayload | 0;
|
|
15417
|
+
this._options = options2 || {};
|
|
15418
|
+
this._threshold = this._options.threshold !== undefined ? this._options.threshold : 1024;
|
|
15419
|
+
this._isServer = !!isServer;
|
|
15420
|
+
this._deflate = null;
|
|
15421
|
+
this._inflate = null;
|
|
15422
|
+
this.params = null;
|
|
15423
|
+
if (!zlibLimiter) {
|
|
15424
|
+
const concurrency = this._options.concurrencyLimit !== undefined ? this._options.concurrencyLimit : 10;
|
|
15425
|
+
zlibLimiter = new Limiter(concurrency);
|
|
15426
|
+
}
|
|
15427
|
+
}
|
|
15428
|
+
static get extensionName() {
|
|
15429
|
+
return "permessage-deflate";
|
|
15430
|
+
}
|
|
15431
|
+
offer() {
|
|
15432
|
+
const params = {};
|
|
15433
|
+
if (this._options.serverNoContextTakeover) {
|
|
15434
|
+
params.server_no_context_takeover = true;
|
|
15435
|
+
}
|
|
15436
|
+
if (this._options.clientNoContextTakeover) {
|
|
15437
|
+
params.client_no_context_takeover = true;
|
|
15438
|
+
}
|
|
15439
|
+
if (this._options.serverMaxWindowBits) {
|
|
15440
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
15441
|
+
}
|
|
15442
|
+
if (this._options.clientMaxWindowBits) {
|
|
15443
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
15444
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
15445
|
+
params.client_max_window_bits = true;
|
|
15446
|
+
}
|
|
15447
|
+
return params;
|
|
15448
|
+
}
|
|
15449
|
+
accept(configurations) {
|
|
15450
|
+
configurations = this.normalizeParams(configurations);
|
|
15451
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
15452
|
+
return this.params;
|
|
15453
|
+
}
|
|
15454
|
+
cleanup() {
|
|
15455
|
+
if (this._inflate) {
|
|
15456
|
+
this._inflate.close();
|
|
15457
|
+
this._inflate = null;
|
|
15458
|
+
}
|
|
15459
|
+
if (this._deflate) {
|
|
15460
|
+
const callback = this._deflate[kCallback];
|
|
15461
|
+
this._deflate.close();
|
|
15462
|
+
this._deflate = null;
|
|
15463
|
+
if (callback) {
|
|
15464
|
+
callback(new Error("The deflate stream was closed while data was being processed"));
|
|
15465
|
+
}
|
|
15466
|
+
}
|
|
15467
|
+
}
|
|
15468
|
+
acceptAsServer(offers) {
|
|
15469
|
+
const opts = this._options;
|
|
15470
|
+
const accepted = offers.find((params) => {
|
|
15471
|
+
if (opts.serverNoContextTakeover === false && params.server_no_context_takeover || params.server_max_window_bits && (opts.serverMaxWindowBits === false || typeof opts.serverMaxWindowBits === "number" && opts.serverMaxWindowBits > params.server_max_window_bits) || typeof opts.clientMaxWindowBits === "number" && !params.client_max_window_bits) {
|
|
15472
|
+
return false;
|
|
15473
|
+
}
|
|
15474
|
+
return true;
|
|
15475
|
+
});
|
|
15476
|
+
if (!accepted) {
|
|
15477
|
+
throw new Error("None of the extension offers can be accepted");
|
|
15478
|
+
}
|
|
15479
|
+
if (opts.serverNoContextTakeover) {
|
|
15480
|
+
accepted.server_no_context_takeover = true;
|
|
15481
|
+
}
|
|
15482
|
+
if (opts.clientNoContextTakeover) {
|
|
15483
|
+
accepted.client_no_context_takeover = true;
|
|
15484
|
+
}
|
|
15485
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
15486
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
15487
|
+
}
|
|
15488
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
15489
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
15490
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
15491
|
+
delete accepted.client_max_window_bits;
|
|
15492
|
+
}
|
|
15493
|
+
return accepted;
|
|
15494
|
+
}
|
|
15495
|
+
acceptAsClient(response) {
|
|
15496
|
+
const params = response[0];
|
|
15497
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
15498
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
15499
|
+
}
|
|
15500
|
+
if (!params.client_max_window_bits) {
|
|
15501
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
15502
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
15503
|
+
}
|
|
15504
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
15505
|
+
throw new Error('Unexpected or invalid parameter "client_max_window_bits"');
|
|
15506
|
+
}
|
|
15507
|
+
return params;
|
|
15508
|
+
}
|
|
15509
|
+
normalizeParams(configurations) {
|
|
15510
|
+
configurations.forEach((params) => {
|
|
15511
|
+
Object.keys(params).forEach((key) => {
|
|
15512
|
+
let value = params[key];
|
|
15513
|
+
if (value.length > 1) {
|
|
15514
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
15515
|
+
}
|
|
15516
|
+
value = value[0];
|
|
15517
|
+
if (key === "client_max_window_bits") {
|
|
15518
|
+
if (value !== true) {
|
|
15519
|
+
const num = +value;
|
|
15520
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
15521
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
15522
|
+
}
|
|
15523
|
+
value = num;
|
|
15524
|
+
} else if (!this._isServer) {
|
|
15525
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
15526
|
+
}
|
|
15527
|
+
} else if (key === "server_max_window_bits") {
|
|
15528
|
+
const num = +value;
|
|
15529
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
15530
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
15531
|
+
}
|
|
15532
|
+
value = num;
|
|
15533
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
15534
|
+
if (value !== true) {
|
|
15535
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
15536
|
+
}
|
|
15537
|
+
} else {
|
|
15538
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
15539
|
+
}
|
|
15540
|
+
params[key] = value;
|
|
15541
|
+
});
|
|
15542
|
+
});
|
|
15543
|
+
return configurations;
|
|
15544
|
+
}
|
|
15545
|
+
decompress(data, fin, callback) {
|
|
15546
|
+
zlibLimiter.add((done) => {
|
|
15547
|
+
this._decompress(data, fin, (err, result) => {
|
|
15548
|
+
done();
|
|
15549
|
+
callback(err, result);
|
|
15550
|
+
});
|
|
15551
|
+
});
|
|
15552
|
+
}
|
|
15553
|
+
compress(data, fin, callback) {
|
|
15554
|
+
zlibLimiter.add((done) => {
|
|
15555
|
+
this._compress(data, fin, (err, result) => {
|
|
15556
|
+
done();
|
|
15557
|
+
callback(err, result);
|
|
15558
|
+
});
|
|
15559
|
+
});
|
|
15560
|
+
}
|
|
15561
|
+
_decompress(data, fin, callback) {
|
|
15562
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
15563
|
+
if (!this._inflate) {
|
|
15564
|
+
const key = `${endpoint}_max_window_bits`;
|
|
15565
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
15566
|
+
this._inflate = zlib.createInflateRaw({
|
|
15567
|
+
...this._options.zlibInflateOptions,
|
|
15568
|
+
windowBits
|
|
15569
|
+
});
|
|
15570
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
15571
|
+
this._inflate[kTotalLength] = 0;
|
|
15572
|
+
this._inflate[kBuffers] = [];
|
|
15573
|
+
this._inflate.on("error", inflateOnError);
|
|
15574
|
+
this._inflate.on("data", inflateOnData);
|
|
15575
|
+
}
|
|
15576
|
+
this._inflate[kCallback] = callback;
|
|
15577
|
+
this._inflate.write(data);
|
|
15578
|
+
if (fin)
|
|
15579
|
+
this._inflate.write(TRAILER);
|
|
15580
|
+
this._inflate.flush(() => {
|
|
15581
|
+
const err = this._inflate[kError];
|
|
15582
|
+
if (err) {
|
|
15583
|
+
this._inflate.close();
|
|
15584
|
+
this._inflate = null;
|
|
15585
|
+
callback(err);
|
|
15586
|
+
return;
|
|
15587
|
+
}
|
|
15588
|
+
const data2 = bufferUtil.concat(this._inflate[kBuffers], this._inflate[kTotalLength]);
|
|
15589
|
+
if (this._inflate._readableState.endEmitted) {
|
|
15590
|
+
this._inflate.close();
|
|
15591
|
+
this._inflate = null;
|
|
15592
|
+
} else {
|
|
15593
|
+
this._inflate[kTotalLength] = 0;
|
|
15594
|
+
this._inflate[kBuffers] = [];
|
|
15595
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
15596
|
+
this._inflate.reset();
|
|
15597
|
+
}
|
|
15598
|
+
}
|
|
15599
|
+
callback(null, data2);
|
|
15600
|
+
});
|
|
15601
|
+
}
|
|
15602
|
+
_compress(data, fin, callback) {
|
|
15603
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
15604
|
+
if (!this._deflate) {
|
|
15605
|
+
const key = `${endpoint}_max_window_bits`;
|
|
15606
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
15607
|
+
this._deflate = zlib.createDeflateRaw({
|
|
15608
|
+
...this._options.zlibDeflateOptions,
|
|
15609
|
+
windowBits
|
|
15610
|
+
});
|
|
15611
|
+
this._deflate[kTotalLength] = 0;
|
|
15612
|
+
this._deflate[kBuffers] = [];
|
|
15613
|
+
this._deflate.on("data", deflateOnData);
|
|
15614
|
+
}
|
|
15615
|
+
this._deflate[kCallback] = callback;
|
|
15616
|
+
this._deflate.write(data);
|
|
15617
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
15618
|
+
if (!this._deflate) {
|
|
15619
|
+
return;
|
|
15620
|
+
}
|
|
15621
|
+
let data2 = bufferUtil.concat(this._deflate[kBuffers], this._deflate[kTotalLength]);
|
|
15622
|
+
if (fin) {
|
|
15623
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
15624
|
+
}
|
|
15625
|
+
this._deflate[kCallback] = null;
|
|
15626
|
+
this._deflate[kTotalLength] = 0;
|
|
15627
|
+
this._deflate[kBuffers] = [];
|
|
15628
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
15629
|
+
this._deflate.reset();
|
|
15630
|
+
}
|
|
15631
|
+
callback(null, data2);
|
|
15632
|
+
});
|
|
15633
|
+
}
|
|
15634
|
+
}
|
|
15635
|
+
module.exports = PerMessageDeflate;
|
|
15636
|
+
function deflateOnData(chunk) {
|
|
15637
|
+
this[kBuffers].push(chunk);
|
|
15638
|
+
this[kTotalLength] += chunk.length;
|
|
15639
|
+
}
|
|
15640
|
+
function inflateOnData(chunk) {
|
|
15641
|
+
this[kTotalLength] += chunk.length;
|
|
15642
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
15643
|
+
this[kBuffers].push(chunk);
|
|
15644
|
+
return;
|
|
15645
|
+
}
|
|
15646
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
15647
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
15648
|
+
this[kError][kStatusCode] = 1009;
|
|
15649
|
+
this.removeListener("data", inflateOnData);
|
|
15650
|
+
this.reset();
|
|
15651
|
+
}
|
|
15652
|
+
function inflateOnError(err) {
|
|
15653
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
15654
|
+
if (this[kError]) {
|
|
15655
|
+
this[kCallback](this[kError]);
|
|
15656
|
+
return;
|
|
15657
|
+
}
|
|
15658
|
+
err[kStatusCode] = 1007;
|
|
15659
|
+
this[kCallback](err);
|
|
15660
|
+
}
|
|
15661
|
+
});
|
|
15662
|
+
|
|
15663
|
+
// node_modules/ws/lib/validation.js
|
|
15664
|
+
var require_validation3 = __commonJS((exports, module) => {
|
|
15665
|
+
var { isUtf8 } = __require("buffer");
|
|
15666
|
+
var { hasBlob } = require_constants2();
|
|
15667
|
+
var tokenChars = [
|
|
15668
|
+
0,
|
|
15669
|
+
0,
|
|
15670
|
+
0,
|
|
15671
|
+
0,
|
|
15672
|
+
0,
|
|
15673
|
+
0,
|
|
15674
|
+
0,
|
|
15675
|
+
0,
|
|
15676
|
+
0,
|
|
15677
|
+
0,
|
|
15678
|
+
0,
|
|
15679
|
+
0,
|
|
15680
|
+
0,
|
|
15681
|
+
0,
|
|
15682
|
+
0,
|
|
15683
|
+
0,
|
|
15684
|
+
0,
|
|
15685
|
+
0,
|
|
15686
|
+
0,
|
|
15687
|
+
0,
|
|
15688
|
+
0,
|
|
15689
|
+
0,
|
|
15690
|
+
0,
|
|
15691
|
+
0,
|
|
15692
|
+
0,
|
|
15693
|
+
0,
|
|
15694
|
+
0,
|
|
15695
|
+
0,
|
|
15696
|
+
0,
|
|
15697
|
+
0,
|
|
15698
|
+
0,
|
|
15699
|
+
0,
|
|
15700
|
+
0,
|
|
15701
|
+
1,
|
|
15702
|
+
0,
|
|
15703
|
+
1,
|
|
15704
|
+
1,
|
|
15705
|
+
1,
|
|
15706
|
+
1,
|
|
15707
|
+
1,
|
|
15708
|
+
0,
|
|
15709
|
+
0,
|
|
15710
|
+
1,
|
|
15711
|
+
1,
|
|
15712
|
+
0,
|
|
15713
|
+
1,
|
|
15714
|
+
1,
|
|
15715
|
+
0,
|
|
15716
|
+
1,
|
|
15717
|
+
1,
|
|
15718
|
+
1,
|
|
15719
|
+
1,
|
|
15720
|
+
1,
|
|
15721
|
+
1,
|
|
15722
|
+
1,
|
|
15723
|
+
1,
|
|
15724
|
+
1,
|
|
15725
|
+
1,
|
|
15726
|
+
0,
|
|
15727
|
+
0,
|
|
15728
|
+
0,
|
|
15729
|
+
0,
|
|
15730
|
+
0,
|
|
15731
|
+
0,
|
|
15732
|
+
0,
|
|
15733
|
+
1,
|
|
15734
|
+
1,
|
|
15735
|
+
1,
|
|
15736
|
+
1,
|
|
15737
|
+
1,
|
|
15738
|
+
1,
|
|
15739
|
+
1,
|
|
15740
|
+
1,
|
|
15741
|
+
1,
|
|
15742
|
+
1,
|
|
15743
|
+
1,
|
|
15744
|
+
1,
|
|
15745
|
+
1,
|
|
15746
|
+
1,
|
|
15747
|
+
1,
|
|
15748
|
+
1,
|
|
15749
|
+
1,
|
|
15750
|
+
1,
|
|
15751
|
+
1,
|
|
15752
|
+
1,
|
|
15753
|
+
1,
|
|
15754
|
+
1,
|
|
15755
|
+
1,
|
|
15756
|
+
1,
|
|
15757
|
+
1,
|
|
15758
|
+
1,
|
|
15759
|
+
0,
|
|
15760
|
+
0,
|
|
15761
|
+
0,
|
|
15762
|
+
1,
|
|
15763
|
+
1,
|
|
15764
|
+
1,
|
|
15765
|
+
1,
|
|
15766
|
+
1,
|
|
15767
|
+
1,
|
|
15768
|
+
1,
|
|
15769
|
+
1,
|
|
15770
|
+
1,
|
|
15771
|
+
1,
|
|
15772
|
+
1,
|
|
15773
|
+
1,
|
|
15774
|
+
1,
|
|
15775
|
+
1,
|
|
15776
|
+
1,
|
|
15777
|
+
1,
|
|
15778
|
+
1,
|
|
15779
|
+
1,
|
|
15780
|
+
1,
|
|
15781
|
+
1,
|
|
15782
|
+
1,
|
|
15783
|
+
1,
|
|
15784
|
+
1,
|
|
15785
|
+
1,
|
|
15786
|
+
1,
|
|
15787
|
+
1,
|
|
15788
|
+
1,
|
|
15789
|
+
1,
|
|
15790
|
+
1,
|
|
15791
|
+
0,
|
|
15792
|
+
1,
|
|
15793
|
+
0,
|
|
15794
|
+
1,
|
|
15795
|
+
0
|
|
15796
|
+
];
|
|
15797
|
+
function isValidStatusCode(code) {
|
|
15798
|
+
return code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3000 && code <= 4999;
|
|
15799
|
+
}
|
|
15800
|
+
function _isValidUTF8(buf) {
|
|
15801
|
+
const len = buf.length;
|
|
15802
|
+
let i = 0;
|
|
15803
|
+
while (i < len) {
|
|
15804
|
+
if ((buf[i] & 128) === 0) {
|
|
15805
|
+
i++;
|
|
15806
|
+
} else if ((buf[i] & 224) === 192) {
|
|
15807
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) {
|
|
15808
|
+
return false;
|
|
15809
|
+
}
|
|
15810
|
+
i += 2;
|
|
15811
|
+
} else if ((buf[i] & 240) === 224) {
|
|
15812
|
+
if (i + 2 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || buf[i] === 224 && (buf[i + 1] & 224) === 128 || buf[i] === 237 && (buf[i + 1] & 224) === 160) {
|
|
15813
|
+
return false;
|
|
15814
|
+
}
|
|
15815
|
+
i += 3;
|
|
15816
|
+
} else if ((buf[i] & 248) === 240) {
|
|
15817
|
+
if (i + 3 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || (buf[i + 3] & 192) !== 128 || buf[i] === 240 && (buf[i + 1] & 240) === 128 || buf[i] === 244 && buf[i + 1] > 143 || buf[i] > 244) {
|
|
15818
|
+
return false;
|
|
15819
|
+
}
|
|
15820
|
+
i += 4;
|
|
15821
|
+
} else {
|
|
15822
|
+
return false;
|
|
15823
|
+
}
|
|
15824
|
+
}
|
|
15825
|
+
return true;
|
|
15826
|
+
}
|
|
15827
|
+
function isBlob(value) {
|
|
15828
|
+
return hasBlob && typeof value === "object" && typeof value.arrayBuffer === "function" && typeof value.type === "string" && typeof value.stream === "function" && (value[Symbol.toStringTag] === "Blob" || value[Symbol.toStringTag] === "File");
|
|
15829
|
+
}
|
|
15830
|
+
module.exports = {
|
|
15831
|
+
isBlob,
|
|
15832
|
+
isValidStatusCode,
|
|
15833
|
+
isValidUTF8: _isValidUTF8,
|
|
15834
|
+
tokenChars
|
|
15835
|
+
};
|
|
15836
|
+
if (isUtf8) {
|
|
15837
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
15838
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
15839
|
+
};
|
|
15840
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
15841
|
+
try {
|
|
15842
|
+
const isValidUTF8 = (()=>{throw new Error("Cannot require module "+"utf-8-validate");})();
|
|
15843
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
15844
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
15845
|
+
};
|
|
15846
|
+
} catch (e) {}
|
|
15847
|
+
}
|
|
15848
|
+
});
|
|
15849
|
+
|
|
15850
|
+
// node_modules/ws/lib/receiver.js
|
|
15851
|
+
var require_receiver = __commonJS((exports, module) => {
|
|
15852
|
+
var { Writable: Writable2 } = __require("stream");
|
|
15853
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
15854
|
+
var {
|
|
15855
|
+
BINARY_TYPES,
|
|
15856
|
+
EMPTY_BUFFER,
|
|
15857
|
+
kStatusCode,
|
|
15858
|
+
kWebSocket
|
|
15859
|
+
} = require_constants2();
|
|
15860
|
+
var { concat, toArrayBuffer, unmask } = require_buffer_util();
|
|
15861
|
+
var { isValidStatusCode, isValidUTF8 } = require_validation3();
|
|
15862
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
15863
|
+
var GET_INFO = 0;
|
|
15864
|
+
var GET_PAYLOAD_LENGTH_16 = 1;
|
|
15865
|
+
var GET_PAYLOAD_LENGTH_64 = 2;
|
|
15866
|
+
var GET_MASK = 3;
|
|
15867
|
+
var GET_DATA = 4;
|
|
15868
|
+
var INFLATING = 5;
|
|
15869
|
+
var DEFER_EVENT = 6;
|
|
15870
|
+
|
|
15871
|
+
class Receiver extends Writable2 {
|
|
15872
|
+
constructor(options2 = {}) {
|
|
15873
|
+
super();
|
|
15874
|
+
this._allowSynchronousEvents = options2.allowSynchronousEvents !== undefined ? options2.allowSynchronousEvents : true;
|
|
15875
|
+
this._binaryType = options2.binaryType || BINARY_TYPES[0];
|
|
15876
|
+
this._extensions = options2.extensions || {};
|
|
15877
|
+
this._isServer = !!options2.isServer;
|
|
15878
|
+
this._maxPayload = options2.maxPayload | 0;
|
|
15879
|
+
this._skipUTF8Validation = !!options2.skipUTF8Validation;
|
|
15880
|
+
this[kWebSocket] = undefined;
|
|
15881
|
+
this._bufferedBytes = 0;
|
|
15882
|
+
this._buffers = [];
|
|
15883
|
+
this._compressed = false;
|
|
15884
|
+
this._payloadLength = 0;
|
|
15885
|
+
this._mask = undefined;
|
|
15886
|
+
this._fragmented = 0;
|
|
15887
|
+
this._masked = false;
|
|
15888
|
+
this._fin = false;
|
|
15889
|
+
this._opcode = 0;
|
|
15890
|
+
this._totalPayloadLength = 0;
|
|
15891
|
+
this._messageLength = 0;
|
|
15892
|
+
this._fragments = [];
|
|
15893
|
+
this._errored = false;
|
|
15894
|
+
this._loop = false;
|
|
15895
|
+
this._state = GET_INFO;
|
|
15896
|
+
}
|
|
15897
|
+
_write(chunk, encoding, cb) {
|
|
15898
|
+
if (this._opcode === 8 && this._state == GET_INFO)
|
|
15899
|
+
return cb();
|
|
15900
|
+
this._bufferedBytes += chunk.length;
|
|
15901
|
+
this._buffers.push(chunk);
|
|
15902
|
+
this.startLoop(cb);
|
|
15903
|
+
}
|
|
15904
|
+
consume(n) {
|
|
15905
|
+
this._bufferedBytes -= n;
|
|
15906
|
+
if (n === this._buffers[0].length)
|
|
15907
|
+
return this._buffers.shift();
|
|
15908
|
+
if (n < this._buffers[0].length) {
|
|
15909
|
+
const buf = this._buffers[0];
|
|
15910
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
15911
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
15912
|
+
}
|
|
15913
|
+
const dst = Buffer.allocUnsafe(n);
|
|
15914
|
+
do {
|
|
15915
|
+
const buf = this._buffers[0];
|
|
15916
|
+
const offset = dst.length - n;
|
|
15917
|
+
if (n >= buf.length) {
|
|
15918
|
+
dst.set(this._buffers.shift(), offset);
|
|
15919
|
+
} else {
|
|
15920
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
15921
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
15922
|
+
}
|
|
15923
|
+
n -= buf.length;
|
|
15924
|
+
} while (n > 0);
|
|
15925
|
+
return dst;
|
|
15926
|
+
}
|
|
15927
|
+
startLoop(cb) {
|
|
15928
|
+
this._loop = true;
|
|
15929
|
+
do {
|
|
15930
|
+
switch (this._state) {
|
|
15931
|
+
case GET_INFO:
|
|
15932
|
+
this.getInfo(cb);
|
|
15933
|
+
break;
|
|
15934
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
15935
|
+
this.getPayloadLength16(cb);
|
|
15936
|
+
break;
|
|
15937
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
15938
|
+
this.getPayloadLength64(cb);
|
|
15939
|
+
break;
|
|
15940
|
+
case GET_MASK:
|
|
15941
|
+
this.getMask();
|
|
15942
|
+
break;
|
|
15943
|
+
case GET_DATA:
|
|
15944
|
+
this.getData(cb);
|
|
15945
|
+
break;
|
|
15946
|
+
case INFLATING:
|
|
15947
|
+
case DEFER_EVENT:
|
|
15948
|
+
this._loop = false;
|
|
15949
|
+
return;
|
|
15950
|
+
}
|
|
15951
|
+
} while (this._loop);
|
|
15952
|
+
if (!this._errored)
|
|
15953
|
+
cb();
|
|
15954
|
+
}
|
|
15955
|
+
getInfo(cb) {
|
|
15956
|
+
if (this._bufferedBytes < 2) {
|
|
15957
|
+
this._loop = false;
|
|
15958
|
+
return;
|
|
15959
|
+
}
|
|
15960
|
+
const buf = this.consume(2);
|
|
15961
|
+
if ((buf[0] & 48) !== 0) {
|
|
15962
|
+
const error49 = this.createError(RangeError, "RSV2 and RSV3 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_2_3");
|
|
15963
|
+
cb(error49);
|
|
15964
|
+
return;
|
|
15965
|
+
}
|
|
15966
|
+
const compressed = (buf[0] & 64) === 64;
|
|
15967
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
15968
|
+
const error49 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
15969
|
+
cb(error49);
|
|
15970
|
+
return;
|
|
15971
|
+
}
|
|
15972
|
+
this._fin = (buf[0] & 128) === 128;
|
|
15973
|
+
this._opcode = buf[0] & 15;
|
|
15974
|
+
this._payloadLength = buf[1] & 127;
|
|
15975
|
+
if (this._opcode === 0) {
|
|
15976
|
+
if (compressed) {
|
|
15977
|
+
const error49 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
15978
|
+
cb(error49);
|
|
15979
|
+
return;
|
|
15980
|
+
}
|
|
15981
|
+
if (!this._fragmented) {
|
|
15982
|
+
const error49 = this.createError(RangeError, "invalid opcode 0", true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
15983
|
+
cb(error49);
|
|
15984
|
+
return;
|
|
15985
|
+
}
|
|
15986
|
+
this._opcode = this._fragmented;
|
|
15987
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
15988
|
+
if (this._fragmented) {
|
|
15989
|
+
const error49 = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
15990
|
+
cb(error49);
|
|
15991
|
+
return;
|
|
15992
|
+
}
|
|
15993
|
+
this._compressed = compressed;
|
|
15994
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
15995
|
+
if (!this._fin) {
|
|
15996
|
+
const error49 = this.createError(RangeError, "FIN must be set", true, 1002, "WS_ERR_EXPECTED_FIN");
|
|
15997
|
+
cb(error49);
|
|
15998
|
+
return;
|
|
15999
|
+
}
|
|
16000
|
+
if (compressed) {
|
|
16001
|
+
const error49 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
16002
|
+
cb(error49);
|
|
16003
|
+
return;
|
|
16004
|
+
}
|
|
16005
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
16006
|
+
const error49 = this.createError(RangeError, `invalid payload length ${this._payloadLength}`, true, 1002, "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");
|
|
16007
|
+
cb(error49);
|
|
16008
|
+
return;
|
|
16009
|
+
}
|
|
16010
|
+
} else {
|
|
16011
|
+
const error49 = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
16012
|
+
cb(error49);
|
|
16013
|
+
return;
|
|
16014
|
+
}
|
|
16015
|
+
if (!this._fin && !this._fragmented)
|
|
16016
|
+
this._fragmented = this._opcode;
|
|
16017
|
+
this._masked = (buf[1] & 128) === 128;
|
|
16018
|
+
if (this._isServer) {
|
|
16019
|
+
if (!this._masked) {
|
|
16020
|
+
const error49 = this.createError(RangeError, "MASK must be set", true, 1002, "WS_ERR_EXPECTED_MASK");
|
|
16021
|
+
cb(error49);
|
|
16022
|
+
return;
|
|
16023
|
+
}
|
|
16024
|
+
} else if (this._masked) {
|
|
16025
|
+
const error49 = this.createError(RangeError, "MASK must be clear", true, 1002, "WS_ERR_UNEXPECTED_MASK");
|
|
16026
|
+
cb(error49);
|
|
16027
|
+
return;
|
|
16028
|
+
}
|
|
16029
|
+
if (this._payloadLength === 126)
|
|
16030
|
+
this._state = GET_PAYLOAD_LENGTH_16;
|
|
16031
|
+
else if (this._payloadLength === 127)
|
|
16032
|
+
this._state = GET_PAYLOAD_LENGTH_64;
|
|
16033
|
+
else
|
|
16034
|
+
this.haveLength(cb);
|
|
16035
|
+
}
|
|
16036
|
+
getPayloadLength16(cb) {
|
|
16037
|
+
if (this._bufferedBytes < 2) {
|
|
16038
|
+
this._loop = false;
|
|
16039
|
+
return;
|
|
16040
|
+
}
|
|
16041
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
16042
|
+
this.haveLength(cb);
|
|
16043
|
+
}
|
|
16044
|
+
getPayloadLength64(cb) {
|
|
16045
|
+
if (this._bufferedBytes < 8) {
|
|
16046
|
+
this._loop = false;
|
|
16047
|
+
return;
|
|
16048
|
+
}
|
|
16049
|
+
const buf = this.consume(8);
|
|
16050
|
+
const num = buf.readUInt32BE(0);
|
|
16051
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
16052
|
+
const error49 = this.createError(RangeError, "Unsupported WebSocket frame: payload length > 2^53 - 1", false, 1009, "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");
|
|
16053
|
+
cb(error49);
|
|
16054
|
+
return;
|
|
16055
|
+
}
|
|
16056
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
16057
|
+
this.haveLength(cb);
|
|
16058
|
+
}
|
|
16059
|
+
haveLength(cb) {
|
|
16060
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
16061
|
+
this._totalPayloadLength += this._payloadLength;
|
|
16062
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
16063
|
+
const error49 = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
16064
|
+
cb(error49);
|
|
16065
|
+
return;
|
|
16066
|
+
}
|
|
16067
|
+
}
|
|
16068
|
+
if (this._masked)
|
|
16069
|
+
this._state = GET_MASK;
|
|
16070
|
+
else
|
|
16071
|
+
this._state = GET_DATA;
|
|
16072
|
+
}
|
|
16073
|
+
getMask() {
|
|
16074
|
+
if (this._bufferedBytes < 4) {
|
|
16075
|
+
this._loop = false;
|
|
16076
|
+
return;
|
|
16077
|
+
}
|
|
16078
|
+
this._mask = this.consume(4);
|
|
16079
|
+
this._state = GET_DATA;
|
|
16080
|
+
}
|
|
16081
|
+
getData(cb) {
|
|
16082
|
+
let data = EMPTY_BUFFER;
|
|
16083
|
+
if (this._payloadLength) {
|
|
16084
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
16085
|
+
this._loop = false;
|
|
16086
|
+
return;
|
|
16087
|
+
}
|
|
16088
|
+
data = this.consume(this._payloadLength);
|
|
16089
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
16090
|
+
unmask(data, this._mask);
|
|
16091
|
+
}
|
|
16092
|
+
}
|
|
16093
|
+
if (this._opcode > 7) {
|
|
16094
|
+
this.controlMessage(data, cb);
|
|
16095
|
+
return;
|
|
16096
|
+
}
|
|
16097
|
+
if (this._compressed) {
|
|
16098
|
+
this._state = INFLATING;
|
|
16099
|
+
this.decompress(data, cb);
|
|
16100
|
+
return;
|
|
16101
|
+
}
|
|
16102
|
+
if (data.length) {
|
|
16103
|
+
this._messageLength = this._totalPayloadLength;
|
|
16104
|
+
this._fragments.push(data);
|
|
16105
|
+
}
|
|
16106
|
+
this.dataMessage(cb);
|
|
16107
|
+
}
|
|
16108
|
+
decompress(data, cb) {
|
|
16109
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
16110
|
+
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
|
16111
|
+
if (err)
|
|
16112
|
+
return cb(err);
|
|
16113
|
+
if (buf.length) {
|
|
16114
|
+
this._messageLength += buf.length;
|
|
16115
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
16116
|
+
const error49 = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
16117
|
+
cb(error49);
|
|
16118
|
+
return;
|
|
16119
|
+
}
|
|
16120
|
+
this._fragments.push(buf);
|
|
16121
|
+
}
|
|
16122
|
+
this.dataMessage(cb);
|
|
16123
|
+
if (this._state === GET_INFO)
|
|
16124
|
+
this.startLoop(cb);
|
|
16125
|
+
});
|
|
16126
|
+
}
|
|
16127
|
+
dataMessage(cb) {
|
|
16128
|
+
if (!this._fin) {
|
|
16129
|
+
this._state = GET_INFO;
|
|
16130
|
+
return;
|
|
16131
|
+
}
|
|
16132
|
+
const messageLength = this._messageLength;
|
|
16133
|
+
const fragments = this._fragments;
|
|
16134
|
+
this._totalPayloadLength = 0;
|
|
16135
|
+
this._messageLength = 0;
|
|
16136
|
+
this._fragmented = 0;
|
|
16137
|
+
this._fragments = [];
|
|
16138
|
+
if (this._opcode === 2) {
|
|
16139
|
+
let data;
|
|
16140
|
+
if (this._binaryType === "nodebuffer") {
|
|
16141
|
+
data = concat(fragments, messageLength);
|
|
16142
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
16143
|
+
data = toArrayBuffer(concat(fragments, messageLength));
|
|
16144
|
+
} else if (this._binaryType === "blob") {
|
|
16145
|
+
data = new Blob(fragments);
|
|
16146
|
+
} else {
|
|
16147
|
+
data = fragments;
|
|
16148
|
+
}
|
|
16149
|
+
if (this._allowSynchronousEvents) {
|
|
16150
|
+
this.emit("message", data, true);
|
|
16151
|
+
this._state = GET_INFO;
|
|
16152
|
+
} else {
|
|
16153
|
+
this._state = DEFER_EVENT;
|
|
16154
|
+
setImmediate(() => {
|
|
16155
|
+
this.emit("message", data, true);
|
|
16156
|
+
this._state = GET_INFO;
|
|
16157
|
+
this.startLoop(cb);
|
|
16158
|
+
});
|
|
16159
|
+
}
|
|
16160
|
+
} else {
|
|
16161
|
+
const buf = concat(fragments, messageLength);
|
|
16162
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
16163
|
+
const error49 = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
16164
|
+
cb(error49);
|
|
16165
|
+
return;
|
|
16166
|
+
}
|
|
16167
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
16168
|
+
this.emit("message", buf, false);
|
|
16169
|
+
this._state = GET_INFO;
|
|
16170
|
+
} else {
|
|
16171
|
+
this._state = DEFER_EVENT;
|
|
16172
|
+
setImmediate(() => {
|
|
16173
|
+
this.emit("message", buf, false);
|
|
16174
|
+
this._state = GET_INFO;
|
|
16175
|
+
this.startLoop(cb);
|
|
16176
|
+
});
|
|
16177
|
+
}
|
|
16178
|
+
}
|
|
16179
|
+
}
|
|
16180
|
+
controlMessage(data, cb) {
|
|
16181
|
+
if (this._opcode === 8) {
|
|
16182
|
+
if (data.length === 0) {
|
|
16183
|
+
this._loop = false;
|
|
16184
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
16185
|
+
this.end();
|
|
16186
|
+
} else {
|
|
16187
|
+
const code = data.readUInt16BE(0);
|
|
16188
|
+
if (!isValidStatusCode(code)) {
|
|
16189
|
+
const error49 = this.createError(RangeError, `invalid status code ${code}`, true, 1002, "WS_ERR_INVALID_CLOSE_CODE");
|
|
16190
|
+
cb(error49);
|
|
16191
|
+
return;
|
|
16192
|
+
}
|
|
16193
|
+
const buf = new FastBuffer(data.buffer, data.byteOffset + 2, data.length - 2);
|
|
16194
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
16195
|
+
const error49 = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
16196
|
+
cb(error49);
|
|
16197
|
+
return;
|
|
16198
|
+
}
|
|
16199
|
+
this._loop = false;
|
|
16200
|
+
this.emit("conclude", code, buf);
|
|
16201
|
+
this.end();
|
|
16202
|
+
}
|
|
16203
|
+
this._state = GET_INFO;
|
|
16204
|
+
return;
|
|
16205
|
+
}
|
|
16206
|
+
if (this._allowSynchronousEvents) {
|
|
16207
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
16208
|
+
this._state = GET_INFO;
|
|
16209
|
+
} else {
|
|
16210
|
+
this._state = DEFER_EVENT;
|
|
16211
|
+
setImmediate(() => {
|
|
16212
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
16213
|
+
this._state = GET_INFO;
|
|
16214
|
+
this.startLoop(cb);
|
|
16215
|
+
});
|
|
16216
|
+
}
|
|
16217
|
+
}
|
|
16218
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
16219
|
+
this._loop = false;
|
|
16220
|
+
this._errored = true;
|
|
16221
|
+
const err = new ErrorCtor(prefix ? `Invalid WebSocket frame: ${message}` : message);
|
|
16222
|
+
Error.captureStackTrace(err, this.createError);
|
|
16223
|
+
err.code = errorCode;
|
|
16224
|
+
err[kStatusCode] = statusCode;
|
|
16225
|
+
return err;
|
|
16226
|
+
}
|
|
16227
|
+
}
|
|
16228
|
+
module.exports = Receiver;
|
|
16229
|
+
});
|
|
16230
|
+
|
|
16231
|
+
// node_modules/ws/lib/sender.js
|
|
16232
|
+
var require_sender = __commonJS((exports, module) => {
|
|
16233
|
+
var { Duplex } = __require("stream");
|
|
16234
|
+
var { randomFillSync } = __require("crypto");
|
|
16235
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
16236
|
+
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants2();
|
|
16237
|
+
var { isBlob, isValidStatusCode } = require_validation3();
|
|
16238
|
+
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
16239
|
+
var kByteLength = Symbol("kByteLength");
|
|
16240
|
+
var maskBuffer = Buffer.alloc(4);
|
|
16241
|
+
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
16242
|
+
var randomPool;
|
|
16243
|
+
var randomPoolPointer = RANDOM_POOL_SIZE;
|
|
16244
|
+
var DEFAULT = 0;
|
|
16245
|
+
var DEFLATING = 1;
|
|
16246
|
+
var GET_BLOB_DATA = 2;
|
|
16247
|
+
|
|
16248
|
+
class Sender {
|
|
16249
|
+
constructor(socket, extensions, generateMask) {
|
|
16250
|
+
this._extensions = extensions || {};
|
|
16251
|
+
if (generateMask) {
|
|
16252
|
+
this._generateMask = generateMask;
|
|
16253
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
16254
|
+
}
|
|
16255
|
+
this._socket = socket;
|
|
16256
|
+
this._firstFragment = true;
|
|
16257
|
+
this._compress = false;
|
|
16258
|
+
this._bufferedBytes = 0;
|
|
16259
|
+
this._queue = [];
|
|
16260
|
+
this._state = DEFAULT;
|
|
16261
|
+
this.onerror = NOOP;
|
|
16262
|
+
this[kWebSocket] = undefined;
|
|
16263
|
+
}
|
|
16264
|
+
static frame(data, options2) {
|
|
16265
|
+
let mask;
|
|
16266
|
+
let merge3 = false;
|
|
16267
|
+
let offset = 2;
|
|
16268
|
+
let skipMasking = false;
|
|
16269
|
+
if (options2.mask) {
|
|
16270
|
+
mask = options2.maskBuffer || maskBuffer;
|
|
16271
|
+
if (options2.generateMask) {
|
|
16272
|
+
options2.generateMask(mask);
|
|
16273
|
+
} else {
|
|
16274
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
16275
|
+
if (randomPool === undefined) {
|
|
16276
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
16277
|
+
}
|
|
16278
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
16279
|
+
randomPoolPointer = 0;
|
|
16280
|
+
}
|
|
16281
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
16282
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
16283
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
16284
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
16285
|
+
}
|
|
16286
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
16287
|
+
offset = 6;
|
|
16288
|
+
}
|
|
16289
|
+
let dataLength;
|
|
16290
|
+
if (typeof data === "string") {
|
|
16291
|
+
if ((!options2.mask || skipMasking) && options2[kByteLength] !== undefined) {
|
|
16292
|
+
dataLength = options2[kByteLength];
|
|
16293
|
+
} else {
|
|
16294
|
+
data = Buffer.from(data);
|
|
16295
|
+
dataLength = data.length;
|
|
16296
|
+
}
|
|
16297
|
+
} else {
|
|
16298
|
+
dataLength = data.length;
|
|
16299
|
+
merge3 = options2.mask && options2.readOnly && !skipMasking;
|
|
16300
|
+
}
|
|
16301
|
+
let payloadLength = dataLength;
|
|
16302
|
+
if (dataLength >= 65536) {
|
|
16303
|
+
offset += 8;
|
|
16304
|
+
payloadLength = 127;
|
|
16305
|
+
} else if (dataLength > 125) {
|
|
16306
|
+
offset += 2;
|
|
16307
|
+
payloadLength = 126;
|
|
16308
|
+
}
|
|
16309
|
+
const target = Buffer.allocUnsafe(merge3 ? dataLength + offset : offset);
|
|
16310
|
+
target[0] = options2.fin ? options2.opcode | 128 : options2.opcode;
|
|
16311
|
+
if (options2.rsv1)
|
|
16312
|
+
target[0] |= 64;
|
|
16313
|
+
target[1] = payloadLength;
|
|
16314
|
+
if (payloadLength === 126) {
|
|
16315
|
+
target.writeUInt16BE(dataLength, 2);
|
|
16316
|
+
} else if (payloadLength === 127) {
|
|
16317
|
+
target[2] = target[3] = 0;
|
|
16318
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
16319
|
+
}
|
|
16320
|
+
if (!options2.mask)
|
|
16321
|
+
return [target, data];
|
|
16322
|
+
target[1] |= 128;
|
|
16323
|
+
target[offset - 4] = mask[0];
|
|
16324
|
+
target[offset - 3] = mask[1];
|
|
16325
|
+
target[offset - 2] = mask[2];
|
|
16326
|
+
target[offset - 1] = mask[3];
|
|
16327
|
+
if (skipMasking)
|
|
16328
|
+
return [target, data];
|
|
16329
|
+
if (merge3) {
|
|
16330
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
16331
|
+
return [target];
|
|
16332
|
+
}
|
|
16333
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
16334
|
+
return [target, data];
|
|
16335
|
+
}
|
|
16336
|
+
close(code, data, mask, cb) {
|
|
16337
|
+
let buf;
|
|
16338
|
+
if (code === undefined) {
|
|
16339
|
+
buf = EMPTY_BUFFER;
|
|
16340
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
16341
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
16342
|
+
} else if (data === undefined || !data.length) {
|
|
16343
|
+
buf = Buffer.allocUnsafe(2);
|
|
16344
|
+
buf.writeUInt16BE(code, 0);
|
|
16345
|
+
} else {
|
|
16346
|
+
const length = Buffer.byteLength(data);
|
|
16347
|
+
if (length > 123) {
|
|
16348
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
16349
|
+
}
|
|
16350
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
16351
|
+
buf.writeUInt16BE(code, 0);
|
|
16352
|
+
if (typeof data === "string") {
|
|
16353
|
+
buf.write(data, 2);
|
|
16354
|
+
} else {
|
|
16355
|
+
buf.set(data, 2);
|
|
16356
|
+
}
|
|
16357
|
+
}
|
|
16358
|
+
const options2 = {
|
|
16359
|
+
[kByteLength]: buf.length,
|
|
16360
|
+
fin: true,
|
|
16361
|
+
generateMask: this._generateMask,
|
|
16362
|
+
mask,
|
|
16363
|
+
maskBuffer: this._maskBuffer,
|
|
16364
|
+
opcode: 8,
|
|
16365
|
+
readOnly: false,
|
|
16366
|
+
rsv1: false
|
|
16367
|
+
};
|
|
16368
|
+
if (this._state !== DEFAULT) {
|
|
16369
|
+
this.enqueue([this.dispatch, buf, false, options2, cb]);
|
|
16370
|
+
} else {
|
|
16371
|
+
this.sendFrame(Sender.frame(buf, options2), cb);
|
|
16372
|
+
}
|
|
16373
|
+
}
|
|
16374
|
+
ping(data, mask, cb) {
|
|
16375
|
+
let byteLength;
|
|
16376
|
+
let readOnly;
|
|
16377
|
+
if (typeof data === "string") {
|
|
16378
|
+
byteLength = Buffer.byteLength(data);
|
|
16379
|
+
readOnly = false;
|
|
16380
|
+
} else if (isBlob(data)) {
|
|
16381
|
+
byteLength = data.size;
|
|
16382
|
+
readOnly = false;
|
|
16383
|
+
} else {
|
|
16384
|
+
data = toBuffer(data);
|
|
16385
|
+
byteLength = data.length;
|
|
16386
|
+
readOnly = toBuffer.readOnly;
|
|
16387
|
+
}
|
|
16388
|
+
if (byteLength > 125) {
|
|
16389
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
16390
|
+
}
|
|
16391
|
+
const options2 = {
|
|
16392
|
+
[kByteLength]: byteLength,
|
|
16393
|
+
fin: true,
|
|
16394
|
+
generateMask: this._generateMask,
|
|
16395
|
+
mask,
|
|
16396
|
+
maskBuffer: this._maskBuffer,
|
|
16397
|
+
opcode: 9,
|
|
16398
|
+
readOnly,
|
|
16399
|
+
rsv1: false
|
|
16400
|
+
};
|
|
16401
|
+
if (isBlob(data)) {
|
|
16402
|
+
if (this._state !== DEFAULT) {
|
|
16403
|
+
this.enqueue([this.getBlobData, data, false, options2, cb]);
|
|
16404
|
+
} else {
|
|
16405
|
+
this.getBlobData(data, false, options2, cb);
|
|
16406
|
+
}
|
|
16407
|
+
} else if (this._state !== DEFAULT) {
|
|
16408
|
+
this.enqueue([this.dispatch, data, false, options2, cb]);
|
|
16409
|
+
} else {
|
|
16410
|
+
this.sendFrame(Sender.frame(data, options2), cb);
|
|
16411
|
+
}
|
|
16412
|
+
}
|
|
16413
|
+
pong(data, mask, cb) {
|
|
16414
|
+
let byteLength;
|
|
16415
|
+
let readOnly;
|
|
16416
|
+
if (typeof data === "string") {
|
|
16417
|
+
byteLength = Buffer.byteLength(data);
|
|
16418
|
+
readOnly = false;
|
|
16419
|
+
} else if (isBlob(data)) {
|
|
16420
|
+
byteLength = data.size;
|
|
16421
|
+
readOnly = false;
|
|
16422
|
+
} else {
|
|
16423
|
+
data = toBuffer(data);
|
|
16424
|
+
byteLength = data.length;
|
|
16425
|
+
readOnly = toBuffer.readOnly;
|
|
16426
|
+
}
|
|
16427
|
+
if (byteLength > 125) {
|
|
16428
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
16429
|
+
}
|
|
16430
|
+
const options2 = {
|
|
16431
|
+
[kByteLength]: byteLength,
|
|
16432
|
+
fin: true,
|
|
16433
|
+
generateMask: this._generateMask,
|
|
16434
|
+
mask,
|
|
16435
|
+
maskBuffer: this._maskBuffer,
|
|
16436
|
+
opcode: 10,
|
|
16437
|
+
readOnly,
|
|
16438
|
+
rsv1: false
|
|
16439
|
+
};
|
|
16440
|
+
if (isBlob(data)) {
|
|
16441
|
+
if (this._state !== DEFAULT) {
|
|
16442
|
+
this.enqueue([this.getBlobData, data, false, options2, cb]);
|
|
16443
|
+
} else {
|
|
16444
|
+
this.getBlobData(data, false, options2, cb);
|
|
16445
|
+
}
|
|
16446
|
+
} else if (this._state !== DEFAULT) {
|
|
16447
|
+
this.enqueue([this.dispatch, data, false, options2, cb]);
|
|
16448
|
+
} else {
|
|
16449
|
+
this.sendFrame(Sender.frame(data, options2), cb);
|
|
16450
|
+
}
|
|
16451
|
+
}
|
|
16452
|
+
send(data, options2, cb) {
|
|
16453
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
16454
|
+
let opcode = options2.binary ? 2 : 1;
|
|
16455
|
+
let rsv1 = options2.compress;
|
|
16456
|
+
let byteLength;
|
|
16457
|
+
let readOnly;
|
|
16458
|
+
if (typeof data === "string") {
|
|
16459
|
+
byteLength = Buffer.byteLength(data);
|
|
16460
|
+
readOnly = false;
|
|
16461
|
+
} else if (isBlob(data)) {
|
|
16462
|
+
byteLength = data.size;
|
|
16463
|
+
readOnly = false;
|
|
16464
|
+
} else {
|
|
16465
|
+
data = toBuffer(data);
|
|
16466
|
+
byteLength = data.length;
|
|
16467
|
+
readOnly = toBuffer.readOnly;
|
|
16468
|
+
}
|
|
16469
|
+
if (this._firstFragment) {
|
|
16470
|
+
this._firstFragment = false;
|
|
16471
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
16472
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
16473
|
+
}
|
|
16474
|
+
this._compress = rsv1;
|
|
16475
|
+
} else {
|
|
16476
|
+
rsv1 = false;
|
|
16477
|
+
opcode = 0;
|
|
16478
|
+
}
|
|
16479
|
+
if (options2.fin)
|
|
16480
|
+
this._firstFragment = true;
|
|
16481
|
+
const opts = {
|
|
16482
|
+
[kByteLength]: byteLength,
|
|
16483
|
+
fin: options2.fin,
|
|
16484
|
+
generateMask: this._generateMask,
|
|
16485
|
+
mask: options2.mask,
|
|
16486
|
+
maskBuffer: this._maskBuffer,
|
|
16487
|
+
opcode,
|
|
16488
|
+
readOnly,
|
|
16489
|
+
rsv1
|
|
16490
|
+
};
|
|
16491
|
+
if (isBlob(data)) {
|
|
16492
|
+
if (this._state !== DEFAULT) {
|
|
16493
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
16494
|
+
} else {
|
|
16495
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
16496
|
+
}
|
|
16497
|
+
} else if (this._state !== DEFAULT) {
|
|
16498
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
16499
|
+
} else {
|
|
16500
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
16501
|
+
}
|
|
16502
|
+
}
|
|
16503
|
+
getBlobData(blob, compress, options2, cb) {
|
|
16504
|
+
this._bufferedBytes += options2[kByteLength];
|
|
16505
|
+
this._state = GET_BLOB_DATA;
|
|
16506
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
16507
|
+
if (this._socket.destroyed) {
|
|
16508
|
+
const err = new Error("The socket was closed while the blob was being read");
|
|
16509
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
16510
|
+
return;
|
|
16511
|
+
}
|
|
16512
|
+
this._bufferedBytes -= options2[kByteLength];
|
|
16513
|
+
const data = toBuffer(arrayBuffer);
|
|
16514
|
+
if (!compress) {
|
|
16515
|
+
this._state = DEFAULT;
|
|
16516
|
+
this.sendFrame(Sender.frame(data, options2), cb);
|
|
16517
|
+
this.dequeue();
|
|
16518
|
+
} else {
|
|
16519
|
+
this.dispatch(data, compress, options2, cb);
|
|
16520
|
+
}
|
|
16521
|
+
}).catch((err) => {
|
|
16522
|
+
process.nextTick(onError, this, err, cb);
|
|
16523
|
+
});
|
|
16524
|
+
}
|
|
16525
|
+
dispatch(data, compress, options2, cb) {
|
|
16526
|
+
if (!compress) {
|
|
16527
|
+
this.sendFrame(Sender.frame(data, options2), cb);
|
|
16528
|
+
return;
|
|
16529
|
+
}
|
|
16530
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
16531
|
+
this._bufferedBytes += options2[kByteLength];
|
|
16532
|
+
this._state = DEFLATING;
|
|
16533
|
+
perMessageDeflate.compress(data, options2.fin, (_, buf) => {
|
|
16534
|
+
if (this._socket.destroyed) {
|
|
16535
|
+
const err = new Error("The socket was closed while data was being compressed");
|
|
16536
|
+
callCallbacks(this, err, cb);
|
|
16537
|
+
return;
|
|
16538
|
+
}
|
|
16539
|
+
this._bufferedBytes -= options2[kByteLength];
|
|
16540
|
+
this._state = DEFAULT;
|
|
16541
|
+
options2.readOnly = false;
|
|
16542
|
+
this.sendFrame(Sender.frame(buf, options2), cb);
|
|
16543
|
+
this.dequeue();
|
|
16544
|
+
});
|
|
16545
|
+
}
|
|
16546
|
+
dequeue() {
|
|
16547
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
16548
|
+
const params = this._queue.shift();
|
|
16549
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
16550
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
16551
|
+
}
|
|
16552
|
+
}
|
|
16553
|
+
enqueue(params) {
|
|
16554
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
16555
|
+
this._queue.push(params);
|
|
16556
|
+
}
|
|
16557
|
+
sendFrame(list, cb) {
|
|
16558
|
+
if (list.length === 2) {
|
|
16559
|
+
this._socket.cork();
|
|
16560
|
+
this._socket.write(list[0]);
|
|
16561
|
+
this._socket.write(list[1], cb);
|
|
16562
|
+
this._socket.uncork();
|
|
16563
|
+
} else {
|
|
16564
|
+
this._socket.write(list[0], cb);
|
|
16565
|
+
}
|
|
16566
|
+
}
|
|
16567
|
+
}
|
|
16568
|
+
module.exports = Sender;
|
|
16569
|
+
function callCallbacks(sender, err, cb) {
|
|
16570
|
+
if (typeof cb === "function")
|
|
16571
|
+
cb(err);
|
|
16572
|
+
for (let i = 0;i < sender._queue.length; i++) {
|
|
16573
|
+
const params = sender._queue[i];
|
|
16574
|
+
const callback = params[params.length - 1];
|
|
16575
|
+
if (typeof callback === "function")
|
|
16576
|
+
callback(err);
|
|
16577
|
+
}
|
|
16578
|
+
}
|
|
16579
|
+
function onError(sender, err, cb) {
|
|
16580
|
+
callCallbacks(sender, err, cb);
|
|
16581
|
+
sender.onerror(err);
|
|
16582
|
+
}
|
|
16583
|
+
});
|
|
16584
|
+
|
|
16585
|
+
// node_modules/ws/lib/event-target.js
|
|
16586
|
+
var require_event_target = __commonJS((exports, module) => {
|
|
16587
|
+
var { kForOnEventAttribute, kListener } = require_constants2();
|
|
16588
|
+
var kCode = Symbol("kCode");
|
|
16589
|
+
var kData = Symbol("kData");
|
|
16590
|
+
var kError = Symbol("kError");
|
|
16591
|
+
var kMessage = Symbol("kMessage");
|
|
16592
|
+
var kReason = Symbol("kReason");
|
|
16593
|
+
var kTarget = Symbol("kTarget");
|
|
16594
|
+
var kType = Symbol("kType");
|
|
16595
|
+
var kWasClean = Symbol("kWasClean");
|
|
16596
|
+
|
|
16597
|
+
class Event {
|
|
16598
|
+
constructor(type) {
|
|
16599
|
+
this[kTarget] = null;
|
|
16600
|
+
this[kType] = type;
|
|
16601
|
+
}
|
|
16602
|
+
get target() {
|
|
16603
|
+
return this[kTarget];
|
|
16604
|
+
}
|
|
16605
|
+
get type() {
|
|
16606
|
+
return this[kType];
|
|
16607
|
+
}
|
|
16608
|
+
}
|
|
16609
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
16610
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
16611
|
+
|
|
16612
|
+
class CloseEvent extends Event {
|
|
16613
|
+
constructor(type, options2 = {}) {
|
|
16614
|
+
super(type);
|
|
16615
|
+
this[kCode] = options2.code === undefined ? 0 : options2.code;
|
|
16616
|
+
this[kReason] = options2.reason === undefined ? "" : options2.reason;
|
|
16617
|
+
this[kWasClean] = options2.wasClean === undefined ? false : options2.wasClean;
|
|
16618
|
+
}
|
|
16619
|
+
get code() {
|
|
16620
|
+
return this[kCode];
|
|
16621
|
+
}
|
|
16622
|
+
get reason() {
|
|
16623
|
+
return this[kReason];
|
|
16624
|
+
}
|
|
16625
|
+
get wasClean() {
|
|
16626
|
+
return this[kWasClean];
|
|
16627
|
+
}
|
|
16628
|
+
}
|
|
16629
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
16630
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
16631
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
16632
|
+
|
|
16633
|
+
class ErrorEvent extends Event {
|
|
16634
|
+
constructor(type, options2 = {}) {
|
|
16635
|
+
super(type);
|
|
16636
|
+
this[kError] = options2.error === undefined ? null : options2.error;
|
|
16637
|
+
this[kMessage] = options2.message === undefined ? "" : options2.message;
|
|
16638
|
+
}
|
|
16639
|
+
get error() {
|
|
16640
|
+
return this[kError];
|
|
16641
|
+
}
|
|
16642
|
+
get message() {
|
|
16643
|
+
return this[kMessage];
|
|
16644
|
+
}
|
|
16645
|
+
}
|
|
16646
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
16647
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
16648
|
+
|
|
16649
|
+
class MessageEvent extends Event {
|
|
16650
|
+
constructor(type, options2 = {}) {
|
|
16651
|
+
super(type);
|
|
16652
|
+
this[kData] = options2.data === undefined ? null : options2.data;
|
|
16653
|
+
}
|
|
16654
|
+
get data() {
|
|
16655
|
+
return this[kData];
|
|
16656
|
+
}
|
|
16657
|
+
}
|
|
16658
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
16659
|
+
var EventTarget = {
|
|
16660
|
+
addEventListener(type, handler2, options2 = {}) {
|
|
16661
|
+
for (const listener of this.listeners(type)) {
|
|
16662
|
+
if (!options2[kForOnEventAttribute] && listener[kListener] === handler2 && !listener[kForOnEventAttribute]) {
|
|
16663
|
+
return;
|
|
16664
|
+
}
|
|
16665
|
+
}
|
|
16666
|
+
let wrapper;
|
|
16667
|
+
if (type === "message") {
|
|
16668
|
+
wrapper = function onMessage(data, isBinary) {
|
|
16669
|
+
const event = new MessageEvent("message", {
|
|
16670
|
+
data: isBinary ? data : data.toString()
|
|
16671
|
+
});
|
|
16672
|
+
event[kTarget] = this;
|
|
16673
|
+
callListener(handler2, this, event);
|
|
16674
|
+
};
|
|
16675
|
+
} else if (type === "close") {
|
|
16676
|
+
wrapper = function onClose(code, message) {
|
|
16677
|
+
const event = new CloseEvent("close", {
|
|
16678
|
+
code,
|
|
16679
|
+
reason: message.toString(),
|
|
16680
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
16681
|
+
});
|
|
16682
|
+
event[kTarget] = this;
|
|
16683
|
+
callListener(handler2, this, event);
|
|
16684
|
+
};
|
|
16685
|
+
} else if (type === "error") {
|
|
16686
|
+
wrapper = function onError(error49) {
|
|
16687
|
+
const event = new ErrorEvent("error", {
|
|
16688
|
+
error: error49,
|
|
16689
|
+
message: error49.message
|
|
16690
|
+
});
|
|
16691
|
+
event[kTarget] = this;
|
|
16692
|
+
callListener(handler2, this, event);
|
|
16693
|
+
};
|
|
16694
|
+
} else if (type === "open") {
|
|
16695
|
+
wrapper = function onOpen() {
|
|
16696
|
+
const event = new Event("open");
|
|
16697
|
+
event[kTarget] = this;
|
|
16698
|
+
callListener(handler2, this, event);
|
|
16699
|
+
};
|
|
16700
|
+
} else {
|
|
16701
|
+
return;
|
|
16702
|
+
}
|
|
16703
|
+
wrapper[kForOnEventAttribute] = !!options2[kForOnEventAttribute];
|
|
16704
|
+
wrapper[kListener] = handler2;
|
|
16705
|
+
if (options2.once) {
|
|
16706
|
+
this.once(type, wrapper);
|
|
16707
|
+
} else {
|
|
16708
|
+
this.on(type, wrapper);
|
|
16709
|
+
}
|
|
16710
|
+
},
|
|
16711
|
+
removeEventListener(type, handler2) {
|
|
16712
|
+
for (const listener of this.listeners(type)) {
|
|
16713
|
+
if (listener[kListener] === handler2 && !listener[kForOnEventAttribute]) {
|
|
16714
|
+
this.removeListener(type, listener);
|
|
16715
|
+
break;
|
|
16716
|
+
}
|
|
16717
|
+
}
|
|
16718
|
+
}
|
|
16719
|
+
};
|
|
16720
|
+
module.exports = {
|
|
16721
|
+
CloseEvent,
|
|
16722
|
+
ErrorEvent,
|
|
16723
|
+
Event,
|
|
16724
|
+
EventTarget,
|
|
16725
|
+
MessageEvent
|
|
16726
|
+
};
|
|
16727
|
+
function callListener(listener, thisArg, event) {
|
|
16728
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
16729
|
+
listener.handleEvent.call(listener, event);
|
|
16730
|
+
} else {
|
|
16731
|
+
listener.call(thisArg, event);
|
|
16732
|
+
}
|
|
16733
|
+
}
|
|
16734
|
+
});
|
|
16735
|
+
|
|
16736
|
+
// node_modules/ws/lib/extension.js
|
|
16737
|
+
var require_extension = __commonJS((exports, module) => {
|
|
16738
|
+
var { tokenChars } = require_validation3();
|
|
16739
|
+
function push(dest, name, elem) {
|
|
16740
|
+
if (dest[name] === undefined)
|
|
16741
|
+
dest[name] = [elem];
|
|
16742
|
+
else
|
|
16743
|
+
dest[name].push(elem);
|
|
16744
|
+
}
|
|
16745
|
+
function parse10(header) {
|
|
16746
|
+
const offers = Object.create(null);
|
|
16747
|
+
let params = Object.create(null);
|
|
16748
|
+
let mustUnescape = false;
|
|
16749
|
+
let isEscaping = false;
|
|
16750
|
+
let inQuotes = false;
|
|
16751
|
+
let extensionName;
|
|
16752
|
+
let paramName;
|
|
16753
|
+
let start = -1;
|
|
16754
|
+
let code = -1;
|
|
16755
|
+
let end = -1;
|
|
16756
|
+
let i = 0;
|
|
16757
|
+
for (;i < header.length; i++) {
|
|
16758
|
+
code = header.charCodeAt(i);
|
|
16759
|
+
if (extensionName === undefined) {
|
|
16760
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
16761
|
+
if (start === -1)
|
|
16762
|
+
start = i;
|
|
16763
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
16764
|
+
if (end === -1 && start !== -1)
|
|
16765
|
+
end = i;
|
|
16766
|
+
} else if (code === 59 || code === 44) {
|
|
16767
|
+
if (start === -1) {
|
|
16768
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
16769
|
+
}
|
|
16770
|
+
if (end === -1)
|
|
16771
|
+
end = i;
|
|
16772
|
+
const name = header.slice(start, end);
|
|
16773
|
+
if (code === 44) {
|
|
16774
|
+
push(offers, name, params);
|
|
16775
|
+
params = Object.create(null);
|
|
16776
|
+
} else {
|
|
16777
|
+
extensionName = name;
|
|
16778
|
+
}
|
|
16779
|
+
start = end = -1;
|
|
16780
|
+
} else {
|
|
16781
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
16782
|
+
}
|
|
16783
|
+
} else if (paramName === undefined) {
|
|
16784
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
16785
|
+
if (start === -1)
|
|
16786
|
+
start = i;
|
|
16787
|
+
} else if (code === 32 || code === 9) {
|
|
16788
|
+
if (end === -1 && start !== -1)
|
|
16789
|
+
end = i;
|
|
16790
|
+
} else if (code === 59 || code === 44) {
|
|
16791
|
+
if (start === -1) {
|
|
16792
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
16793
|
+
}
|
|
16794
|
+
if (end === -1)
|
|
16795
|
+
end = i;
|
|
16796
|
+
push(params, header.slice(start, end), true);
|
|
16797
|
+
if (code === 44) {
|
|
16798
|
+
push(offers, extensionName, params);
|
|
16799
|
+
params = Object.create(null);
|
|
16800
|
+
extensionName = undefined;
|
|
16801
|
+
}
|
|
16802
|
+
start = end = -1;
|
|
16803
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
16804
|
+
paramName = header.slice(start, i);
|
|
16805
|
+
start = end = -1;
|
|
16806
|
+
} else {
|
|
16807
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
16808
|
+
}
|
|
16809
|
+
} else {
|
|
16810
|
+
if (isEscaping) {
|
|
16811
|
+
if (tokenChars[code] !== 1) {
|
|
16812
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
16813
|
+
}
|
|
16814
|
+
if (start === -1)
|
|
16815
|
+
start = i;
|
|
16816
|
+
else if (!mustUnescape)
|
|
16817
|
+
mustUnescape = true;
|
|
16818
|
+
isEscaping = false;
|
|
16819
|
+
} else if (inQuotes) {
|
|
16820
|
+
if (tokenChars[code] === 1) {
|
|
16821
|
+
if (start === -1)
|
|
16822
|
+
start = i;
|
|
16823
|
+
} else if (code === 34 && start !== -1) {
|
|
16824
|
+
inQuotes = false;
|
|
16825
|
+
end = i;
|
|
16826
|
+
} else if (code === 92) {
|
|
16827
|
+
isEscaping = true;
|
|
16828
|
+
} else {
|
|
16829
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
16830
|
+
}
|
|
16831
|
+
} else if (code === 34 && header.charCodeAt(i - 1) === 61) {
|
|
16832
|
+
inQuotes = true;
|
|
16833
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
16834
|
+
if (start === -1)
|
|
16835
|
+
start = i;
|
|
16836
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
16837
|
+
if (end === -1)
|
|
16838
|
+
end = i;
|
|
16839
|
+
} else if (code === 59 || code === 44) {
|
|
16840
|
+
if (start === -1) {
|
|
16841
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
16842
|
+
}
|
|
16843
|
+
if (end === -1)
|
|
16844
|
+
end = i;
|
|
16845
|
+
let value = header.slice(start, end);
|
|
16846
|
+
if (mustUnescape) {
|
|
16847
|
+
value = value.replace(/\\/g, "");
|
|
16848
|
+
mustUnescape = false;
|
|
16849
|
+
}
|
|
16850
|
+
push(params, paramName, value);
|
|
16851
|
+
if (code === 44) {
|
|
16852
|
+
push(offers, extensionName, params);
|
|
16853
|
+
params = Object.create(null);
|
|
16854
|
+
extensionName = undefined;
|
|
16855
|
+
}
|
|
16856
|
+
paramName = undefined;
|
|
16857
|
+
start = end = -1;
|
|
16858
|
+
} else {
|
|
16859
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
16860
|
+
}
|
|
16861
|
+
}
|
|
16862
|
+
}
|
|
16863
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) {
|
|
16864
|
+
throw new SyntaxError("Unexpected end of input");
|
|
16865
|
+
}
|
|
16866
|
+
if (end === -1)
|
|
16867
|
+
end = i;
|
|
16868
|
+
const token = header.slice(start, end);
|
|
16869
|
+
if (extensionName === undefined) {
|
|
16870
|
+
push(offers, token, params);
|
|
16871
|
+
} else {
|
|
16872
|
+
if (paramName === undefined) {
|
|
16873
|
+
push(params, token, true);
|
|
16874
|
+
} else if (mustUnescape) {
|
|
16875
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
16876
|
+
} else {
|
|
16877
|
+
push(params, paramName, token);
|
|
16878
|
+
}
|
|
16879
|
+
push(offers, extensionName, params);
|
|
16880
|
+
}
|
|
16881
|
+
return offers;
|
|
16882
|
+
}
|
|
16883
|
+
function format(extensions) {
|
|
16884
|
+
return Object.keys(extensions).map((extension) => {
|
|
16885
|
+
let configurations = extensions[extension];
|
|
16886
|
+
if (!Array.isArray(configurations))
|
|
16887
|
+
configurations = [configurations];
|
|
16888
|
+
return configurations.map((params) => {
|
|
16889
|
+
return [extension].concat(Object.keys(params).map((k) => {
|
|
16890
|
+
let values = params[k];
|
|
16891
|
+
if (!Array.isArray(values))
|
|
16892
|
+
values = [values];
|
|
16893
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
16894
|
+
})).join("; ");
|
|
16895
|
+
}).join(", ");
|
|
16896
|
+
}).join(", ");
|
|
16897
|
+
}
|
|
16898
|
+
module.exports = { format, parse: parse10 };
|
|
16899
|
+
});
|
|
16900
|
+
|
|
16901
|
+
// node_modules/ws/lib/websocket.js
|
|
16902
|
+
var require_websocket = __commonJS((exports, module) => {
|
|
16903
|
+
var EventEmitter3 = __require("events");
|
|
16904
|
+
var https = __require("https");
|
|
16905
|
+
var http = __require("http");
|
|
16906
|
+
var net = __require("net");
|
|
16907
|
+
var tls = __require("tls");
|
|
16908
|
+
var { randomBytes, createHash } = __require("crypto");
|
|
16909
|
+
var { Duplex, Readable: Readable2 } = __require("stream");
|
|
16910
|
+
var { URL: URL2 } = __require("url");
|
|
16911
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
16912
|
+
var Receiver = require_receiver();
|
|
16913
|
+
var Sender = require_sender();
|
|
16914
|
+
var { isBlob } = require_validation3();
|
|
16915
|
+
var {
|
|
16916
|
+
BINARY_TYPES,
|
|
16917
|
+
EMPTY_BUFFER,
|
|
16918
|
+
GUID,
|
|
16919
|
+
kForOnEventAttribute,
|
|
16920
|
+
kListener,
|
|
16921
|
+
kStatusCode,
|
|
16922
|
+
kWebSocket,
|
|
16923
|
+
NOOP
|
|
16924
|
+
} = require_constants2();
|
|
16925
|
+
var {
|
|
16926
|
+
EventTarget: { addEventListener, removeEventListener }
|
|
16927
|
+
} = require_event_target();
|
|
16928
|
+
var { format, parse: parse10 } = require_extension();
|
|
16929
|
+
var { toBuffer } = require_buffer_util();
|
|
16930
|
+
var closeTimeout = 30 * 1000;
|
|
16931
|
+
var kAborted = Symbol("kAborted");
|
|
16932
|
+
var protocolVersions = [8, 13];
|
|
16933
|
+
var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
16934
|
+
var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
16935
|
+
|
|
16936
|
+
class WebSocket extends EventEmitter3 {
|
|
16937
|
+
constructor(address, protocols, options2) {
|
|
16938
|
+
super();
|
|
16939
|
+
this._binaryType = BINARY_TYPES[0];
|
|
16940
|
+
this._closeCode = 1006;
|
|
16941
|
+
this._closeFrameReceived = false;
|
|
16942
|
+
this._closeFrameSent = false;
|
|
16943
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
16944
|
+
this._closeTimer = null;
|
|
16945
|
+
this._errorEmitted = false;
|
|
16946
|
+
this._extensions = {};
|
|
16947
|
+
this._paused = false;
|
|
16948
|
+
this._protocol = "";
|
|
16949
|
+
this._readyState = WebSocket.CONNECTING;
|
|
16950
|
+
this._receiver = null;
|
|
16951
|
+
this._sender = null;
|
|
16952
|
+
this._socket = null;
|
|
16953
|
+
if (address !== null) {
|
|
16954
|
+
this._bufferedAmount = 0;
|
|
16955
|
+
this._isServer = false;
|
|
16956
|
+
this._redirects = 0;
|
|
16957
|
+
if (protocols === undefined) {
|
|
16958
|
+
protocols = [];
|
|
16959
|
+
} else if (!Array.isArray(protocols)) {
|
|
16960
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
16961
|
+
options2 = protocols;
|
|
16962
|
+
protocols = [];
|
|
16963
|
+
} else {
|
|
16964
|
+
protocols = [protocols];
|
|
16965
|
+
}
|
|
16966
|
+
}
|
|
16967
|
+
initAsClient(this, address, protocols, options2);
|
|
16968
|
+
} else {
|
|
16969
|
+
this._autoPong = options2.autoPong;
|
|
16970
|
+
this._isServer = true;
|
|
16971
|
+
}
|
|
16972
|
+
}
|
|
16973
|
+
get binaryType() {
|
|
16974
|
+
return this._binaryType;
|
|
16975
|
+
}
|
|
16976
|
+
set binaryType(type) {
|
|
16977
|
+
if (!BINARY_TYPES.includes(type))
|
|
16978
|
+
return;
|
|
16979
|
+
this._binaryType = type;
|
|
16980
|
+
if (this._receiver)
|
|
16981
|
+
this._receiver._binaryType = type;
|
|
16982
|
+
}
|
|
16983
|
+
get bufferedAmount() {
|
|
16984
|
+
if (!this._socket)
|
|
16985
|
+
return this._bufferedAmount;
|
|
16986
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
16987
|
+
}
|
|
16988
|
+
get extensions() {
|
|
16989
|
+
return Object.keys(this._extensions).join();
|
|
16990
|
+
}
|
|
16991
|
+
get isPaused() {
|
|
16992
|
+
return this._paused;
|
|
16993
|
+
}
|
|
16994
|
+
get onclose() {
|
|
16995
|
+
return null;
|
|
16996
|
+
}
|
|
16997
|
+
get onerror() {
|
|
16998
|
+
return null;
|
|
16999
|
+
}
|
|
17000
|
+
get onopen() {
|
|
17001
|
+
return null;
|
|
17002
|
+
}
|
|
17003
|
+
get onmessage() {
|
|
17004
|
+
return null;
|
|
17005
|
+
}
|
|
17006
|
+
get protocol() {
|
|
17007
|
+
return this._protocol;
|
|
17008
|
+
}
|
|
17009
|
+
get readyState() {
|
|
17010
|
+
return this._readyState;
|
|
17011
|
+
}
|
|
17012
|
+
get url() {
|
|
17013
|
+
return this._url;
|
|
17014
|
+
}
|
|
17015
|
+
setSocket(socket, head, options2) {
|
|
17016
|
+
const receiver = new Receiver({
|
|
17017
|
+
allowSynchronousEvents: options2.allowSynchronousEvents,
|
|
17018
|
+
binaryType: this.binaryType,
|
|
17019
|
+
extensions: this._extensions,
|
|
17020
|
+
isServer: this._isServer,
|
|
17021
|
+
maxPayload: options2.maxPayload,
|
|
17022
|
+
skipUTF8Validation: options2.skipUTF8Validation
|
|
17023
|
+
});
|
|
17024
|
+
const sender = new Sender(socket, this._extensions, options2.generateMask);
|
|
17025
|
+
this._receiver = receiver;
|
|
17026
|
+
this._sender = sender;
|
|
17027
|
+
this._socket = socket;
|
|
17028
|
+
receiver[kWebSocket] = this;
|
|
17029
|
+
sender[kWebSocket] = this;
|
|
17030
|
+
socket[kWebSocket] = this;
|
|
17031
|
+
receiver.on("conclude", receiverOnConclude);
|
|
17032
|
+
receiver.on("drain", receiverOnDrain);
|
|
17033
|
+
receiver.on("error", receiverOnError);
|
|
17034
|
+
receiver.on("message", receiverOnMessage);
|
|
17035
|
+
receiver.on("ping", receiverOnPing);
|
|
17036
|
+
receiver.on("pong", receiverOnPong);
|
|
17037
|
+
sender.onerror = senderOnError;
|
|
17038
|
+
if (socket.setTimeout)
|
|
17039
|
+
socket.setTimeout(0);
|
|
17040
|
+
if (socket.setNoDelay)
|
|
17041
|
+
socket.setNoDelay();
|
|
17042
|
+
if (head.length > 0)
|
|
17043
|
+
socket.unshift(head);
|
|
17044
|
+
socket.on("close", socketOnClose);
|
|
17045
|
+
socket.on("data", socketOnData);
|
|
17046
|
+
socket.on("end", socketOnEnd);
|
|
17047
|
+
socket.on("error", socketOnError);
|
|
17048
|
+
this._readyState = WebSocket.OPEN;
|
|
17049
|
+
this.emit("open");
|
|
17050
|
+
}
|
|
17051
|
+
emitClose() {
|
|
17052
|
+
if (!this._socket) {
|
|
17053
|
+
this._readyState = WebSocket.CLOSED;
|
|
17054
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
17055
|
+
return;
|
|
17056
|
+
}
|
|
17057
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
17058
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
17059
|
+
}
|
|
17060
|
+
this._receiver.removeAllListeners();
|
|
17061
|
+
this._readyState = WebSocket.CLOSED;
|
|
17062
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
17063
|
+
}
|
|
17064
|
+
close(code, data) {
|
|
17065
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
17066
|
+
return;
|
|
17067
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
17068
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
17069
|
+
abortHandshake(this, this._req, msg);
|
|
17070
|
+
return;
|
|
17071
|
+
}
|
|
17072
|
+
if (this.readyState === WebSocket.CLOSING) {
|
|
17073
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
17074
|
+
this._socket.end();
|
|
17075
|
+
}
|
|
17076
|
+
return;
|
|
17077
|
+
}
|
|
17078
|
+
this._readyState = WebSocket.CLOSING;
|
|
17079
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
17080
|
+
if (err)
|
|
17081
|
+
return;
|
|
17082
|
+
this._closeFrameSent = true;
|
|
17083
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
17084
|
+
this._socket.end();
|
|
17085
|
+
}
|
|
17086
|
+
});
|
|
17087
|
+
setCloseTimer(this);
|
|
17088
|
+
}
|
|
17089
|
+
pause() {
|
|
17090
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
17091
|
+
return;
|
|
17092
|
+
}
|
|
17093
|
+
this._paused = true;
|
|
17094
|
+
this._socket.pause();
|
|
17095
|
+
}
|
|
17096
|
+
ping(data, mask, cb) {
|
|
17097
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
17098
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
17099
|
+
}
|
|
17100
|
+
if (typeof data === "function") {
|
|
17101
|
+
cb = data;
|
|
17102
|
+
data = mask = undefined;
|
|
17103
|
+
} else if (typeof mask === "function") {
|
|
17104
|
+
cb = mask;
|
|
17105
|
+
mask = undefined;
|
|
17106
|
+
}
|
|
17107
|
+
if (typeof data === "number")
|
|
17108
|
+
data = data.toString();
|
|
17109
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
17110
|
+
sendAfterClose(this, data, cb);
|
|
17111
|
+
return;
|
|
17112
|
+
}
|
|
17113
|
+
if (mask === undefined)
|
|
17114
|
+
mask = !this._isServer;
|
|
17115
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
17116
|
+
}
|
|
17117
|
+
pong(data, mask, cb) {
|
|
17118
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
17119
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
17120
|
+
}
|
|
17121
|
+
if (typeof data === "function") {
|
|
17122
|
+
cb = data;
|
|
17123
|
+
data = mask = undefined;
|
|
17124
|
+
} else if (typeof mask === "function") {
|
|
17125
|
+
cb = mask;
|
|
17126
|
+
mask = undefined;
|
|
17127
|
+
}
|
|
17128
|
+
if (typeof data === "number")
|
|
17129
|
+
data = data.toString();
|
|
17130
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
17131
|
+
sendAfterClose(this, data, cb);
|
|
17132
|
+
return;
|
|
17133
|
+
}
|
|
17134
|
+
if (mask === undefined)
|
|
17135
|
+
mask = !this._isServer;
|
|
17136
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
17137
|
+
}
|
|
17138
|
+
resume() {
|
|
17139
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
17140
|
+
return;
|
|
17141
|
+
}
|
|
17142
|
+
this._paused = false;
|
|
17143
|
+
if (!this._receiver._writableState.needDrain)
|
|
17144
|
+
this._socket.resume();
|
|
17145
|
+
}
|
|
17146
|
+
send(data, options2, cb) {
|
|
17147
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
17148
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
17149
|
+
}
|
|
17150
|
+
if (typeof options2 === "function") {
|
|
17151
|
+
cb = options2;
|
|
17152
|
+
options2 = {};
|
|
17153
|
+
}
|
|
17154
|
+
if (typeof data === "number")
|
|
17155
|
+
data = data.toString();
|
|
17156
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
17157
|
+
sendAfterClose(this, data, cb);
|
|
17158
|
+
return;
|
|
17159
|
+
}
|
|
17160
|
+
const opts = {
|
|
17161
|
+
binary: typeof data !== "string",
|
|
17162
|
+
mask: !this._isServer,
|
|
17163
|
+
compress: true,
|
|
17164
|
+
fin: true,
|
|
17165
|
+
...options2
|
|
17166
|
+
};
|
|
17167
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
17168
|
+
opts.compress = false;
|
|
17169
|
+
}
|
|
17170
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
17171
|
+
}
|
|
17172
|
+
terminate() {
|
|
17173
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
17174
|
+
return;
|
|
17175
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
17176
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
17177
|
+
abortHandshake(this, this._req, msg);
|
|
17178
|
+
return;
|
|
17179
|
+
}
|
|
17180
|
+
if (this._socket) {
|
|
17181
|
+
this._readyState = WebSocket.CLOSING;
|
|
17182
|
+
this._socket.destroy();
|
|
17183
|
+
}
|
|
17184
|
+
}
|
|
17185
|
+
}
|
|
17186
|
+
Object.defineProperty(WebSocket, "CONNECTING", {
|
|
17187
|
+
enumerable: true,
|
|
17188
|
+
value: readyStates.indexOf("CONNECTING")
|
|
17189
|
+
});
|
|
17190
|
+
Object.defineProperty(WebSocket.prototype, "CONNECTING", {
|
|
17191
|
+
enumerable: true,
|
|
17192
|
+
value: readyStates.indexOf("CONNECTING")
|
|
17193
|
+
});
|
|
17194
|
+
Object.defineProperty(WebSocket, "OPEN", {
|
|
17195
|
+
enumerable: true,
|
|
17196
|
+
value: readyStates.indexOf("OPEN")
|
|
17197
|
+
});
|
|
17198
|
+
Object.defineProperty(WebSocket.prototype, "OPEN", {
|
|
17199
|
+
enumerable: true,
|
|
17200
|
+
value: readyStates.indexOf("OPEN")
|
|
17201
|
+
});
|
|
17202
|
+
Object.defineProperty(WebSocket, "CLOSING", {
|
|
17203
|
+
enumerable: true,
|
|
17204
|
+
value: readyStates.indexOf("CLOSING")
|
|
17205
|
+
});
|
|
17206
|
+
Object.defineProperty(WebSocket.prototype, "CLOSING", {
|
|
17207
|
+
enumerable: true,
|
|
17208
|
+
value: readyStates.indexOf("CLOSING")
|
|
17209
|
+
});
|
|
17210
|
+
Object.defineProperty(WebSocket, "CLOSED", {
|
|
17211
|
+
enumerable: true,
|
|
17212
|
+
value: readyStates.indexOf("CLOSED")
|
|
17213
|
+
});
|
|
17214
|
+
Object.defineProperty(WebSocket.prototype, "CLOSED", {
|
|
17215
|
+
enumerable: true,
|
|
17216
|
+
value: readyStates.indexOf("CLOSED")
|
|
17217
|
+
});
|
|
17218
|
+
[
|
|
17219
|
+
"binaryType",
|
|
17220
|
+
"bufferedAmount",
|
|
17221
|
+
"extensions",
|
|
17222
|
+
"isPaused",
|
|
17223
|
+
"protocol",
|
|
17224
|
+
"readyState",
|
|
17225
|
+
"url"
|
|
17226
|
+
].forEach((property) => {
|
|
17227
|
+
Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
|
|
17228
|
+
});
|
|
17229
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
17230
|
+
Object.defineProperty(WebSocket.prototype, `on${method}`, {
|
|
17231
|
+
enumerable: true,
|
|
17232
|
+
get() {
|
|
17233
|
+
for (const listener of this.listeners(method)) {
|
|
17234
|
+
if (listener[kForOnEventAttribute])
|
|
17235
|
+
return listener[kListener];
|
|
17236
|
+
}
|
|
17237
|
+
return null;
|
|
17238
|
+
},
|
|
17239
|
+
set(handler2) {
|
|
17240
|
+
for (const listener of this.listeners(method)) {
|
|
17241
|
+
if (listener[kForOnEventAttribute]) {
|
|
17242
|
+
this.removeListener(method, listener);
|
|
17243
|
+
break;
|
|
17244
|
+
}
|
|
17245
|
+
}
|
|
17246
|
+
if (typeof handler2 !== "function")
|
|
17247
|
+
return;
|
|
17248
|
+
this.addEventListener(method, handler2, {
|
|
17249
|
+
[kForOnEventAttribute]: true
|
|
17250
|
+
});
|
|
17251
|
+
}
|
|
17252
|
+
});
|
|
17253
|
+
});
|
|
17254
|
+
WebSocket.prototype.addEventListener = addEventListener;
|
|
17255
|
+
WebSocket.prototype.removeEventListener = removeEventListener;
|
|
17256
|
+
module.exports = WebSocket;
|
|
17257
|
+
function initAsClient(websocket, address, protocols, options2) {
|
|
17258
|
+
const opts = {
|
|
17259
|
+
allowSynchronousEvents: true,
|
|
17260
|
+
autoPong: true,
|
|
17261
|
+
protocolVersion: protocolVersions[1],
|
|
17262
|
+
maxPayload: 100 * 1024 * 1024,
|
|
17263
|
+
skipUTF8Validation: false,
|
|
17264
|
+
perMessageDeflate: true,
|
|
17265
|
+
followRedirects: false,
|
|
17266
|
+
maxRedirects: 10,
|
|
17267
|
+
...options2,
|
|
17268
|
+
socketPath: undefined,
|
|
17269
|
+
hostname: undefined,
|
|
17270
|
+
protocol: undefined,
|
|
17271
|
+
timeout: undefined,
|
|
17272
|
+
method: "GET",
|
|
17273
|
+
host: undefined,
|
|
17274
|
+
path: undefined,
|
|
17275
|
+
port: undefined
|
|
17276
|
+
};
|
|
17277
|
+
websocket._autoPong = opts.autoPong;
|
|
17278
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
17279
|
+
throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} ` + `(supported versions: ${protocolVersions.join(", ")})`);
|
|
17280
|
+
}
|
|
17281
|
+
let parsedUrl;
|
|
17282
|
+
if (address instanceof URL2) {
|
|
17283
|
+
parsedUrl = address;
|
|
17284
|
+
} else {
|
|
17285
|
+
try {
|
|
17286
|
+
parsedUrl = new URL2(address);
|
|
17287
|
+
} catch (e) {
|
|
17288
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
17289
|
+
}
|
|
17290
|
+
}
|
|
17291
|
+
if (parsedUrl.protocol === "http:") {
|
|
17292
|
+
parsedUrl.protocol = "ws:";
|
|
17293
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
17294
|
+
parsedUrl.protocol = "wss:";
|
|
17295
|
+
}
|
|
17296
|
+
websocket._url = parsedUrl.href;
|
|
17297
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
17298
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
17299
|
+
let invalidUrlMessage;
|
|
17300
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
17301
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", ` + '"http:", "https:", or "ws+unix:"';
|
|
17302
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
17303
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
17304
|
+
} else if (parsedUrl.hash) {
|
|
17305
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
17306
|
+
}
|
|
17307
|
+
if (invalidUrlMessage) {
|
|
17308
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
17309
|
+
if (websocket._redirects === 0) {
|
|
17310
|
+
throw err;
|
|
17311
|
+
} else {
|
|
17312
|
+
emitErrorAndClose(websocket, err);
|
|
17313
|
+
return;
|
|
17314
|
+
}
|
|
17315
|
+
}
|
|
17316
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
17317
|
+
const key = randomBytes(16).toString("base64");
|
|
17318
|
+
const request = isSecure ? https.request : http.request;
|
|
17319
|
+
const protocolSet = new Set;
|
|
17320
|
+
let perMessageDeflate;
|
|
17321
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
17322
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
17323
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
17324
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
17325
|
+
opts.headers = {
|
|
17326
|
+
...opts.headers,
|
|
17327
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
17328
|
+
"Sec-WebSocket-Key": key,
|
|
17329
|
+
Connection: "Upgrade",
|
|
17330
|
+
Upgrade: "websocket"
|
|
17331
|
+
};
|
|
17332
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
17333
|
+
opts.timeout = opts.handshakeTimeout;
|
|
17334
|
+
if (opts.perMessageDeflate) {
|
|
17335
|
+
perMessageDeflate = new PerMessageDeflate(opts.perMessageDeflate !== true ? opts.perMessageDeflate : {}, false, opts.maxPayload);
|
|
17336
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({
|
|
17337
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
17338
|
+
});
|
|
17339
|
+
}
|
|
17340
|
+
if (protocols.length) {
|
|
17341
|
+
for (const protocol of protocols) {
|
|
17342
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
17343
|
+
throw new SyntaxError("An invalid or duplicated subprotocol was specified");
|
|
17344
|
+
}
|
|
17345
|
+
protocolSet.add(protocol);
|
|
17346
|
+
}
|
|
17347
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
17348
|
+
}
|
|
17349
|
+
if (opts.origin) {
|
|
17350
|
+
if (opts.protocolVersion < 13) {
|
|
17351
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
17352
|
+
} else {
|
|
17353
|
+
opts.headers.Origin = opts.origin;
|
|
17354
|
+
}
|
|
17355
|
+
}
|
|
17356
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
17357
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
17358
|
+
}
|
|
17359
|
+
if (isIpcUrl) {
|
|
17360
|
+
const parts = opts.path.split(":");
|
|
17361
|
+
opts.socketPath = parts[0];
|
|
17362
|
+
opts.path = parts[1];
|
|
17363
|
+
}
|
|
17364
|
+
let req;
|
|
17365
|
+
if (opts.followRedirects) {
|
|
17366
|
+
if (websocket._redirects === 0) {
|
|
17367
|
+
websocket._originalIpc = isIpcUrl;
|
|
17368
|
+
websocket._originalSecure = isSecure;
|
|
17369
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
17370
|
+
const headers = options2 && options2.headers;
|
|
17371
|
+
options2 = { ...options2, headers: {} };
|
|
17372
|
+
if (headers) {
|
|
17373
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
17374
|
+
options2.headers[key2.toLowerCase()] = value;
|
|
17375
|
+
}
|
|
17376
|
+
}
|
|
17377
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
17378
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
17379
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
17380
|
+
delete opts.headers.authorization;
|
|
17381
|
+
delete opts.headers.cookie;
|
|
17382
|
+
if (!isSameHost)
|
|
17383
|
+
delete opts.headers.host;
|
|
17384
|
+
opts.auth = undefined;
|
|
17385
|
+
}
|
|
17386
|
+
}
|
|
17387
|
+
if (opts.auth && !options2.headers.authorization) {
|
|
17388
|
+
options2.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
17389
|
+
}
|
|
17390
|
+
req = websocket._req = request(opts);
|
|
17391
|
+
if (websocket._redirects) {
|
|
17392
|
+
websocket.emit("redirect", websocket.url, req);
|
|
17393
|
+
}
|
|
17394
|
+
} else {
|
|
17395
|
+
req = websocket._req = request(opts);
|
|
17396
|
+
}
|
|
17397
|
+
if (opts.timeout) {
|
|
17398
|
+
req.on("timeout", () => {
|
|
17399
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
17400
|
+
});
|
|
17401
|
+
}
|
|
17402
|
+
req.on("error", (err) => {
|
|
17403
|
+
if (req === null || req[kAborted])
|
|
17404
|
+
return;
|
|
17405
|
+
req = websocket._req = null;
|
|
17406
|
+
emitErrorAndClose(websocket, err);
|
|
17407
|
+
});
|
|
17408
|
+
req.on("response", (res) => {
|
|
17409
|
+
const location = res.headers.location;
|
|
17410
|
+
const statusCode = res.statusCode;
|
|
17411
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
17412
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
17413
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
17414
|
+
return;
|
|
17415
|
+
}
|
|
17416
|
+
req.abort();
|
|
17417
|
+
let addr;
|
|
17418
|
+
try {
|
|
17419
|
+
addr = new URL2(location, address);
|
|
17420
|
+
} catch (e) {
|
|
17421
|
+
const err = new SyntaxError(`Invalid URL: ${location}`);
|
|
17422
|
+
emitErrorAndClose(websocket, err);
|
|
17423
|
+
return;
|
|
17424
|
+
}
|
|
17425
|
+
initAsClient(websocket, addr, protocols, options2);
|
|
17426
|
+
} else if (!websocket.emit("unexpected-response", req, res)) {
|
|
17427
|
+
abortHandshake(websocket, req, `Unexpected server response: ${res.statusCode}`);
|
|
17428
|
+
}
|
|
17429
|
+
});
|
|
17430
|
+
req.on("upgrade", (res, socket, head) => {
|
|
17431
|
+
websocket.emit("upgrade", res);
|
|
17432
|
+
if (websocket.readyState !== WebSocket.CONNECTING)
|
|
17433
|
+
return;
|
|
17434
|
+
req = websocket._req = null;
|
|
17435
|
+
const upgrade = res.headers.upgrade;
|
|
17436
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
17437
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
17438
|
+
return;
|
|
17439
|
+
}
|
|
17440
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
17441
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
17442
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
17443
|
+
return;
|
|
17444
|
+
}
|
|
17445
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
17446
|
+
let protError;
|
|
17447
|
+
if (serverProt !== undefined) {
|
|
17448
|
+
if (!protocolSet.size) {
|
|
17449
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
17450
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
17451
|
+
protError = "Server sent an invalid subprotocol";
|
|
17452
|
+
}
|
|
17453
|
+
} else if (protocolSet.size) {
|
|
17454
|
+
protError = "Server sent no subprotocol";
|
|
17455
|
+
}
|
|
17456
|
+
if (protError) {
|
|
17457
|
+
abortHandshake(websocket, socket, protError);
|
|
17458
|
+
return;
|
|
17459
|
+
}
|
|
17460
|
+
if (serverProt)
|
|
17461
|
+
websocket._protocol = serverProt;
|
|
17462
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
17463
|
+
if (secWebSocketExtensions !== undefined) {
|
|
17464
|
+
if (!perMessageDeflate) {
|
|
17465
|
+
const message = "Server sent a Sec-WebSocket-Extensions header but no extension " + "was requested";
|
|
17466
|
+
abortHandshake(websocket, socket, message);
|
|
17467
|
+
return;
|
|
17468
|
+
}
|
|
17469
|
+
let extensions;
|
|
17470
|
+
try {
|
|
17471
|
+
extensions = parse10(secWebSocketExtensions);
|
|
17472
|
+
} catch (err) {
|
|
17473
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
17474
|
+
abortHandshake(websocket, socket, message);
|
|
17475
|
+
return;
|
|
17476
|
+
}
|
|
17477
|
+
const extensionNames = Object.keys(extensions);
|
|
17478
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
17479
|
+
const message = "Server indicated an extension that was not requested";
|
|
17480
|
+
abortHandshake(websocket, socket, message);
|
|
17481
|
+
return;
|
|
17482
|
+
}
|
|
17483
|
+
try {
|
|
17484
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
17485
|
+
} catch (err) {
|
|
17486
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
17487
|
+
abortHandshake(websocket, socket, message);
|
|
17488
|
+
return;
|
|
17489
|
+
}
|
|
17490
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
17491
|
+
}
|
|
17492
|
+
websocket.setSocket(socket, head, {
|
|
17493
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
17494
|
+
generateMask: opts.generateMask,
|
|
17495
|
+
maxPayload: opts.maxPayload,
|
|
17496
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
17497
|
+
});
|
|
17498
|
+
});
|
|
17499
|
+
if (opts.finishRequest) {
|
|
17500
|
+
opts.finishRequest(req, websocket);
|
|
17501
|
+
} else {
|
|
17502
|
+
req.end();
|
|
17503
|
+
}
|
|
17504
|
+
}
|
|
17505
|
+
function emitErrorAndClose(websocket, err) {
|
|
17506
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
17507
|
+
websocket._errorEmitted = true;
|
|
17508
|
+
websocket.emit("error", err);
|
|
17509
|
+
websocket.emitClose();
|
|
17510
|
+
}
|
|
17511
|
+
function netConnect(options2) {
|
|
17512
|
+
options2.path = options2.socketPath;
|
|
17513
|
+
return net.connect(options2);
|
|
17514
|
+
}
|
|
17515
|
+
function tlsConnect(options2) {
|
|
17516
|
+
options2.path = undefined;
|
|
17517
|
+
if (!options2.servername && options2.servername !== "") {
|
|
17518
|
+
options2.servername = net.isIP(options2.host) ? "" : options2.host;
|
|
17519
|
+
}
|
|
17520
|
+
return tls.connect(options2);
|
|
17521
|
+
}
|
|
17522
|
+
function abortHandshake(websocket, stream, message) {
|
|
17523
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
17524
|
+
const err = new Error(message);
|
|
17525
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
17526
|
+
if (stream.setHeader) {
|
|
17527
|
+
stream[kAborted] = true;
|
|
17528
|
+
stream.abort();
|
|
17529
|
+
if (stream.socket && !stream.socket.destroyed) {
|
|
17530
|
+
stream.socket.destroy();
|
|
17531
|
+
}
|
|
17532
|
+
process.nextTick(emitErrorAndClose, websocket, err);
|
|
17533
|
+
} else {
|
|
17534
|
+
stream.destroy(err);
|
|
17535
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
17536
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
17537
|
+
}
|
|
17538
|
+
}
|
|
17539
|
+
function sendAfterClose(websocket, data, cb) {
|
|
17540
|
+
if (data) {
|
|
17541
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
17542
|
+
if (websocket._socket)
|
|
17543
|
+
websocket._sender._bufferedBytes += length;
|
|
17544
|
+
else
|
|
17545
|
+
websocket._bufferedAmount += length;
|
|
17546
|
+
}
|
|
17547
|
+
if (cb) {
|
|
17548
|
+
const err = new Error(`WebSocket is not open: readyState ${websocket.readyState} ` + `(${readyStates[websocket.readyState]})`);
|
|
17549
|
+
process.nextTick(cb, err);
|
|
17550
|
+
}
|
|
17551
|
+
}
|
|
17552
|
+
function receiverOnConclude(code, reason) {
|
|
17553
|
+
const websocket = this[kWebSocket];
|
|
17554
|
+
websocket._closeFrameReceived = true;
|
|
17555
|
+
websocket._closeMessage = reason;
|
|
17556
|
+
websocket._closeCode = code;
|
|
17557
|
+
if (websocket._socket[kWebSocket] === undefined)
|
|
17558
|
+
return;
|
|
17559
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
17560
|
+
process.nextTick(resume, websocket._socket);
|
|
17561
|
+
if (code === 1005)
|
|
17562
|
+
websocket.close();
|
|
17563
|
+
else
|
|
17564
|
+
websocket.close(code, reason);
|
|
17565
|
+
}
|
|
17566
|
+
function receiverOnDrain() {
|
|
17567
|
+
const websocket = this[kWebSocket];
|
|
17568
|
+
if (!websocket.isPaused)
|
|
17569
|
+
websocket._socket.resume();
|
|
17570
|
+
}
|
|
17571
|
+
function receiverOnError(err) {
|
|
17572
|
+
const websocket = this[kWebSocket];
|
|
17573
|
+
if (websocket._socket[kWebSocket] !== undefined) {
|
|
17574
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
17575
|
+
process.nextTick(resume, websocket._socket);
|
|
17576
|
+
websocket.close(err[kStatusCode]);
|
|
17577
|
+
}
|
|
17578
|
+
if (!websocket._errorEmitted) {
|
|
17579
|
+
websocket._errorEmitted = true;
|
|
17580
|
+
websocket.emit("error", err);
|
|
17581
|
+
}
|
|
17582
|
+
}
|
|
17583
|
+
function receiverOnFinish() {
|
|
17584
|
+
this[kWebSocket].emitClose();
|
|
17585
|
+
}
|
|
17586
|
+
function receiverOnMessage(data, isBinary) {
|
|
17587
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
17588
|
+
}
|
|
17589
|
+
function receiverOnPing(data) {
|
|
17590
|
+
const websocket = this[kWebSocket];
|
|
17591
|
+
if (websocket._autoPong)
|
|
17592
|
+
websocket.pong(data, !this._isServer, NOOP);
|
|
17593
|
+
websocket.emit("ping", data);
|
|
17594
|
+
}
|
|
17595
|
+
function receiverOnPong(data) {
|
|
17596
|
+
this[kWebSocket].emit("pong", data);
|
|
17597
|
+
}
|
|
17598
|
+
function resume(stream) {
|
|
17599
|
+
stream.resume();
|
|
17600
|
+
}
|
|
17601
|
+
function senderOnError(err) {
|
|
17602
|
+
const websocket = this[kWebSocket];
|
|
17603
|
+
if (websocket.readyState === WebSocket.CLOSED)
|
|
17604
|
+
return;
|
|
17605
|
+
if (websocket.readyState === WebSocket.OPEN) {
|
|
17606
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
17607
|
+
setCloseTimer(websocket);
|
|
17608
|
+
}
|
|
17609
|
+
this._socket.end();
|
|
17610
|
+
if (!websocket._errorEmitted) {
|
|
17611
|
+
websocket._errorEmitted = true;
|
|
17612
|
+
websocket.emit("error", err);
|
|
17613
|
+
}
|
|
17614
|
+
}
|
|
17615
|
+
function setCloseTimer(websocket) {
|
|
17616
|
+
websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), closeTimeout);
|
|
17617
|
+
}
|
|
17618
|
+
function socketOnClose() {
|
|
17619
|
+
const websocket = this[kWebSocket];
|
|
17620
|
+
this.removeListener("close", socketOnClose);
|
|
17621
|
+
this.removeListener("data", socketOnData);
|
|
17622
|
+
this.removeListener("end", socketOnEnd);
|
|
17623
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
17624
|
+
let chunk;
|
|
17625
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && (chunk = websocket._socket.read()) !== null) {
|
|
17626
|
+
websocket._receiver.write(chunk);
|
|
17627
|
+
}
|
|
17628
|
+
websocket._receiver.end();
|
|
17629
|
+
this[kWebSocket] = undefined;
|
|
17630
|
+
clearTimeout(websocket._closeTimer);
|
|
17631
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
|
|
17632
|
+
websocket.emitClose();
|
|
17633
|
+
} else {
|
|
17634
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
17635
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
17636
|
+
}
|
|
17637
|
+
}
|
|
17638
|
+
function socketOnData(chunk) {
|
|
17639
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
17640
|
+
this.pause();
|
|
17641
|
+
}
|
|
17642
|
+
}
|
|
17643
|
+
function socketOnEnd() {
|
|
17644
|
+
const websocket = this[kWebSocket];
|
|
17645
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
17646
|
+
websocket._receiver.end();
|
|
17647
|
+
this.end();
|
|
17648
|
+
}
|
|
17649
|
+
function socketOnError() {
|
|
17650
|
+
const websocket = this[kWebSocket];
|
|
17651
|
+
this.removeListener("error", socketOnError);
|
|
17652
|
+
this.on("error", NOOP);
|
|
17653
|
+
if (websocket) {
|
|
17654
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
17655
|
+
this.destroy();
|
|
17656
|
+
}
|
|
17657
|
+
}
|
|
17658
|
+
});
|
|
17659
|
+
|
|
17660
|
+
// node_modules/ws/lib/stream.js
|
|
17661
|
+
var require_stream = __commonJS((exports, module) => {
|
|
17662
|
+
var WebSocket = require_websocket();
|
|
17663
|
+
var { Duplex } = __require("stream");
|
|
17664
|
+
function emitClose(stream) {
|
|
17665
|
+
stream.emit("close");
|
|
17666
|
+
}
|
|
17667
|
+
function duplexOnEnd() {
|
|
17668
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
17669
|
+
this.destroy();
|
|
17670
|
+
}
|
|
17671
|
+
}
|
|
17672
|
+
function duplexOnError(err) {
|
|
17673
|
+
this.removeListener("error", duplexOnError);
|
|
17674
|
+
this.destroy();
|
|
17675
|
+
if (this.listenerCount("error") === 0) {
|
|
17676
|
+
this.emit("error", err);
|
|
17677
|
+
}
|
|
17678
|
+
}
|
|
17679
|
+
function createWebSocketStream(ws, options2) {
|
|
17680
|
+
let terminateOnDestroy = true;
|
|
17681
|
+
const duplex = new Duplex({
|
|
17682
|
+
...options2,
|
|
17683
|
+
autoDestroy: false,
|
|
17684
|
+
emitClose: false,
|
|
17685
|
+
objectMode: false,
|
|
17686
|
+
writableObjectMode: false
|
|
17687
|
+
});
|
|
17688
|
+
ws.on("message", function message(msg, isBinary) {
|
|
17689
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
17690
|
+
if (!duplex.push(data))
|
|
17691
|
+
ws.pause();
|
|
17692
|
+
});
|
|
17693
|
+
ws.once("error", function error49(err) {
|
|
17694
|
+
if (duplex.destroyed)
|
|
17695
|
+
return;
|
|
17696
|
+
terminateOnDestroy = false;
|
|
17697
|
+
duplex.destroy(err);
|
|
17698
|
+
});
|
|
17699
|
+
ws.once("close", function close() {
|
|
17700
|
+
if (duplex.destroyed)
|
|
17701
|
+
return;
|
|
17702
|
+
duplex.push(null);
|
|
17703
|
+
});
|
|
17704
|
+
duplex._destroy = function(err, callback) {
|
|
17705
|
+
if (ws.readyState === ws.CLOSED) {
|
|
17706
|
+
callback(err);
|
|
17707
|
+
process.nextTick(emitClose, duplex);
|
|
17708
|
+
return;
|
|
17709
|
+
}
|
|
17710
|
+
let called = false;
|
|
17711
|
+
ws.once("error", function error49(err2) {
|
|
17712
|
+
called = true;
|
|
17713
|
+
callback(err2);
|
|
17714
|
+
});
|
|
17715
|
+
ws.once("close", function close() {
|
|
17716
|
+
if (!called)
|
|
17717
|
+
callback(err);
|
|
17718
|
+
process.nextTick(emitClose, duplex);
|
|
17719
|
+
});
|
|
17720
|
+
if (terminateOnDestroy)
|
|
17721
|
+
ws.terminate();
|
|
17722
|
+
};
|
|
17723
|
+
duplex._final = function(callback) {
|
|
17724
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
17725
|
+
ws.once("open", function open() {
|
|
17726
|
+
duplex._final(callback);
|
|
17727
|
+
});
|
|
17728
|
+
return;
|
|
17729
|
+
}
|
|
17730
|
+
if (ws._socket === null)
|
|
17731
|
+
return;
|
|
17732
|
+
if (ws._socket._writableState.finished) {
|
|
17733
|
+
callback();
|
|
17734
|
+
if (duplex._readableState.endEmitted)
|
|
17735
|
+
duplex.destroy();
|
|
17736
|
+
} else {
|
|
17737
|
+
ws._socket.once("finish", function finish() {
|
|
17738
|
+
callback();
|
|
17739
|
+
});
|
|
17740
|
+
ws.close();
|
|
17741
|
+
}
|
|
17742
|
+
};
|
|
17743
|
+
duplex._read = function() {
|
|
17744
|
+
if (ws.isPaused)
|
|
17745
|
+
ws.resume();
|
|
17746
|
+
};
|
|
17747
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
17748
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
17749
|
+
ws.once("open", function open() {
|
|
17750
|
+
duplex._write(chunk, encoding, callback);
|
|
17751
|
+
});
|
|
17752
|
+
return;
|
|
17753
|
+
}
|
|
17754
|
+
ws.send(chunk, callback);
|
|
17755
|
+
};
|
|
17756
|
+
duplex.on("end", duplexOnEnd);
|
|
17757
|
+
duplex.on("error", duplexOnError);
|
|
17758
|
+
return duplex;
|
|
17759
|
+
}
|
|
17760
|
+
module.exports = createWebSocketStream;
|
|
17761
|
+
});
|
|
17762
|
+
|
|
17763
|
+
// node_modules/ws/lib/subprotocol.js
|
|
17764
|
+
var require_subprotocol = __commonJS((exports, module) => {
|
|
17765
|
+
var { tokenChars } = require_validation3();
|
|
17766
|
+
function parse10(header) {
|
|
17767
|
+
const protocols = new Set;
|
|
17768
|
+
let start = -1;
|
|
17769
|
+
let end = -1;
|
|
17770
|
+
let i = 0;
|
|
17771
|
+
for (i;i < header.length; i++) {
|
|
17772
|
+
const code = header.charCodeAt(i);
|
|
17773
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
17774
|
+
if (start === -1)
|
|
17775
|
+
start = i;
|
|
17776
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
17777
|
+
if (end === -1 && start !== -1)
|
|
17778
|
+
end = i;
|
|
17779
|
+
} else if (code === 44) {
|
|
17780
|
+
if (start === -1) {
|
|
17781
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
17782
|
+
}
|
|
17783
|
+
if (end === -1)
|
|
17784
|
+
end = i;
|
|
17785
|
+
const protocol2 = header.slice(start, end);
|
|
17786
|
+
if (protocols.has(protocol2)) {
|
|
17787
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
17788
|
+
}
|
|
17789
|
+
protocols.add(protocol2);
|
|
17790
|
+
start = end = -1;
|
|
17791
|
+
} else {
|
|
17792
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
17793
|
+
}
|
|
17794
|
+
}
|
|
17795
|
+
if (start === -1 || end !== -1) {
|
|
17796
|
+
throw new SyntaxError("Unexpected end of input");
|
|
17797
|
+
}
|
|
17798
|
+
const protocol = header.slice(start, i);
|
|
17799
|
+
if (protocols.has(protocol)) {
|
|
17800
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
17801
|
+
}
|
|
17802
|
+
protocols.add(protocol);
|
|
17803
|
+
return protocols;
|
|
17804
|
+
}
|
|
17805
|
+
module.exports = { parse: parse10 };
|
|
17806
|
+
});
|
|
17807
|
+
|
|
17808
|
+
// node_modules/ws/lib/websocket-server.js
|
|
17809
|
+
var require_websocket_server = __commonJS((exports, module) => {
|
|
17810
|
+
var EventEmitter3 = __require("events");
|
|
17811
|
+
var http = __require("http");
|
|
17812
|
+
var { Duplex } = __require("stream");
|
|
17813
|
+
var { createHash } = __require("crypto");
|
|
17814
|
+
var extension = require_extension();
|
|
17815
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
17816
|
+
var subprotocol = require_subprotocol();
|
|
17817
|
+
var WebSocket = require_websocket();
|
|
17818
|
+
var { GUID, kWebSocket } = require_constants2();
|
|
17819
|
+
var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
17820
|
+
var RUNNING = 0;
|
|
17821
|
+
var CLOSING = 1;
|
|
17822
|
+
var CLOSED = 2;
|
|
17823
|
+
|
|
17824
|
+
class WebSocketServer extends EventEmitter3 {
|
|
17825
|
+
constructor(options2, callback) {
|
|
17826
|
+
super();
|
|
17827
|
+
options2 = {
|
|
17828
|
+
allowSynchronousEvents: true,
|
|
17829
|
+
autoPong: true,
|
|
17830
|
+
maxPayload: 100 * 1024 * 1024,
|
|
17831
|
+
skipUTF8Validation: false,
|
|
17832
|
+
perMessageDeflate: false,
|
|
17833
|
+
handleProtocols: null,
|
|
17834
|
+
clientTracking: true,
|
|
17835
|
+
verifyClient: null,
|
|
17836
|
+
noServer: false,
|
|
17837
|
+
backlog: null,
|
|
17838
|
+
server: null,
|
|
17839
|
+
host: null,
|
|
17840
|
+
path: null,
|
|
17841
|
+
port: null,
|
|
17842
|
+
WebSocket,
|
|
17843
|
+
...options2
|
|
17844
|
+
};
|
|
17845
|
+
if (options2.port == null && !options2.server && !options2.noServer || options2.port != null && (options2.server || options2.noServer) || options2.server && options2.noServer) {
|
|
17846
|
+
throw new TypeError('One and only one of the "port", "server", or "noServer" options ' + "must be specified");
|
|
17847
|
+
}
|
|
17848
|
+
if (options2.port != null) {
|
|
17849
|
+
this._server = http.createServer((req, res) => {
|
|
17850
|
+
const body = http.STATUS_CODES[426];
|
|
17851
|
+
res.writeHead(426, {
|
|
17852
|
+
"Content-Length": body.length,
|
|
17853
|
+
"Content-Type": "text/plain"
|
|
17854
|
+
});
|
|
17855
|
+
res.end(body);
|
|
17856
|
+
});
|
|
17857
|
+
this._server.listen(options2.port, options2.host, options2.backlog, callback);
|
|
17858
|
+
} else if (options2.server) {
|
|
17859
|
+
this._server = options2.server;
|
|
17860
|
+
}
|
|
17861
|
+
if (this._server) {
|
|
17862
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
17863
|
+
this._removeListeners = addListeners(this._server, {
|
|
17864
|
+
listening: this.emit.bind(this, "listening"),
|
|
17865
|
+
error: this.emit.bind(this, "error"),
|
|
17866
|
+
upgrade: (req, socket, head) => {
|
|
17867
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
17868
|
+
}
|
|
17869
|
+
});
|
|
17870
|
+
}
|
|
17871
|
+
if (options2.perMessageDeflate === true)
|
|
17872
|
+
options2.perMessageDeflate = {};
|
|
17873
|
+
if (options2.clientTracking) {
|
|
17874
|
+
this.clients = new Set;
|
|
17875
|
+
this._shouldEmitClose = false;
|
|
17876
|
+
}
|
|
17877
|
+
this.options = options2;
|
|
17878
|
+
this._state = RUNNING;
|
|
17879
|
+
}
|
|
17880
|
+
address() {
|
|
17881
|
+
if (this.options.noServer) {
|
|
17882
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
17883
|
+
}
|
|
17884
|
+
if (!this._server)
|
|
17885
|
+
return null;
|
|
17886
|
+
return this._server.address();
|
|
17887
|
+
}
|
|
17888
|
+
close(cb) {
|
|
17889
|
+
if (this._state === CLOSED) {
|
|
17890
|
+
if (cb) {
|
|
17891
|
+
this.once("close", () => {
|
|
17892
|
+
cb(new Error("The server is not running"));
|
|
17893
|
+
});
|
|
17894
|
+
}
|
|
17895
|
+
process.nextTick(emitClose, this);
|
|
17896
|
+
return;
|
|
17897
|
+
}
|
|
17898
|
+
if (cb)
|
|
17899
|
+
this.once("close", cb);
|
|
17900
|
+
if (this._state === CLOSING)
|
|
17901
|
+
return;
|
|
17902
|
+
this._state = CLOSING;
|
|
17903
|
+
if (this.options.noServer || this.options.server) {
|
|
17904
|
+
if (this._server) {
|
|
17905
|
+
this._removeListeners();
|
|
17906
|
+
this._removeListeners = this._server = null;
|
|
17907
|
+
}
|
|
17908
|
+
if (this.clients) {
|
|
17909
|
+
if (!this.clients.size) {
|
|
17910
|
+
process.nextTick(emitClose, this);
|
|
17911
|
+
} else {
|
|
17912
|
+
this._shouldEmitClose = true;
|
|
17913
|
+
}
|
|
17914
|
+
} else {
|
|
17915
|
+
process.nextTick(emitClose, this);
|
|
17916
|
+
}
|
|
17917
|
+
} else {
|
|
17918
|
+
const server = this._server;
|
|
17919
|
+
this._removeListeners();
|
|
17920
|
+
this._removeListeners = this._server = null;
|
|
17921
|
+
server.close(() => {
|
|
17922
|
+
emitClose(this);
|
|
17923
|
+
});
|
|
17924
|
+
}
|
|
17925
|
+
}
|
|
17926
|
+
shouldHandle(req) {
|
|
17927
|
+
if (this.options.path) {
|
|
17928
|
+
const index = req.url.indexOf("?");
|
|
17929
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
|
17930
|
+
if (pathname !== this.options.path)
|
|
17931
|
+
return false;
|
|
17932
|
+
}
|
|
17933
|
+
return true;
|
|
17934
|
+
}
|
|
17935
|
+
handleUpgrade(req, socket, head, cb) {
|
|
17936
|
+
socket.on("error", socketOnError);
|
|
17937
|
+
const key = req.headers["sec-websocket-key"];
|
|
17938
|
+
const upgrade = req.headers.upgrade;
|
|
17939
|
+
const version3 = +req.headers["sec-websocket-version"];
|
|
17940
|
+
if (req.method !== "GET") {
|
|
17941
|
+
const message = "Invalid HTTP method";
|
|
17942
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
|
|
17943
|
+
return;
|
|
17944
|
+
}
|
|
17945
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
17946
|
+
const message = "Invalid Upgrade header";
|
|
17947
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
17948
|
+
return;
|
|
17949
|
+
}
|
|
17950
|
+
if (key === undefined || !keyRegex.test(key)) {
|
|
17951
|
+
const message = "Missing or invalid Sec-WebSocket-Key header";
|
|
17952
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
17953
|
+
return;
|
|
17954
|
+
}
|
|
17955
|
+
if (version3 !== 13 && version3 !== 8) {
|
|
17956
|
+
const message = "Missing or invalid Sec-WebSocket-Version header";
|
|
17957
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
|
|
17958
|
+
"Sec-WebSocket-Version": "13, 8"
|
|
17959
|
+
});
|
|
17960
|
+
return;
|
|
17961
|
+
}
|
|
17962
|
+
if (!this.shouldHandle(req)) {
|
|
17963
|
+
abortHandshake(socket, 400);
|
|
17964
|
+
return;
|
|
17965
|
+
}
|
|
17966
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
17967
|
+
let protocols = new Set;
|
|
17968
|
+
if (secWebSocketProtocol !== undefined) {
|
|
17969
|
+
try {
|
|
17970
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
17971
|
+
} catch (err) {
|
|
17972
|
+
const message = "Invalid Sec-WebSocket-Protocol header";
|
|
17973
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
17974
|
+
return;
|
|
17975
|
+
}
|
|
17976
|
+
}
|
|
17977
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
17978
|
+
const extensions = {};
|
|
17979
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== undefined) {
|
|
17980
|
+
const perMessageDeflate = new PerMessageDeflate(this.options.perMessageDeflate, true, this.options.maxPayload);
|
|
17981
|
+
try {
|
|
17982
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
17983
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
17984
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
17985
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
17986
|
+
}
|
|
17987
|
+
} catch (err) {
|
|
17988
|
+
const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
17989
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
17990
|
+
return;
|
|
17991
|
+
}
|
|
17992
|
+
}
|
|
17993
|
+
if (this.options.verifyClient) {
|
|
17994
|
+
const info = {
|
|
17995
|
+
origin: req.headers[`${version3 === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
17996
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
17997
|
+
req
|
|
17998
|
+
};
|
|
17999
|
+
if (this.options.verifyClient.length === 2) {
|
|
18000
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
18001
|
+
if (!verified) {
|
|
18002
|
+
return abortHandshake(socket, code || 401, message, headers);
|
|
18003
|
+
}
|
|
18004
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
18005
|
+
});
|
|
18006
|
+
return;
|
|
18007
|
+
}
|
|
18008
|
+
if (!this.options.verifyClient(info))
|
|
18009
|
+
return abortHandshake(socket, 401);
|
|
18010
|
+
}
|
|
18011
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
18012
|
+
}
|
|
18013
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
18014
|
+
if (!socket.readable || !socket.writable)
|
|
18015
|
+
return socket.destroy();
|
|
18016
|
+
if (socket[kWebSocket]) {
|
|
18017
|
+
throw new Error("server.handleUpgrade() was called more than once with the same " + "socket, possibly due to a misconfiguration");
|
|
18018
|
+
}
|
|
18019
|
+
if (this._state > RUNNING)
|
|
18020
|
+
return abortHandshake(socket, 503);
|
|
18021
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
18022
|
+
const headers = [
|
|
18023
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
18024
|
+
"Upgrade: websocket",
|
|
18025
|
+
"Connection: Upgrade",
|
|
18026
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
18027
|
+
];
|
|
18028
|
+
const ws = new this.options.WebSocket(null, undefined, this.options);
|
|
18029
|
+
if (protocols.size) {
|
|
18030
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
18031
|
+
if (protocol) {
|
|
18032
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
18033
|
+
ws._protocol = protocol;
|
|
18034
|
+
}
|
|
18035
|
+
}
|
|
18036
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
18037
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
18038
|
+
const value = extension.format({
|
|
18039
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
18040
|
+
});
|
|
18041
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
18042
|
+
ws._extensions = extensions;
|
|
18043
|
+
}
|
|
18044
|
+
this.emit("headers", headers, req);
|
|
18045
|
+
socket.write(headers.concat(`\r
|
|
18046
|
+
`).join(`\r
|
|
18047
|
+
`));
|
|
18048
|
+
socket.removeListener("error", socketOnError);
|
|
18049
|
+
ws.setSocket(socket, head, {
|
|
18050
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
18051
|
+
maxPayload: this.options.maxPayload,
|
|
18052
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
18053
|
+
});
|
|
18054
|
+
if (this.clients) {
|
|
18055
|
+
this.clients.add(ws);
|
|
18056
|
+
ws.on("close", () => {
|
|
18057
|
+
this.clients.delete(ws);
|
|
18058
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
18059
|
+
process.nextTick(emitClose, this);
|
|
18060
|
+
}
|
|
18061
|
+
});
|
|
18062
|
+
}
|
|
18063
|
+
cb(ws, req);
|
|
18064
|
+
}
|
|
18065
|
+
}
|
|
18066
|
+
module.exports = WebSocketServer;
|
|
18067
|
+
function addListeners(server, map3) {
|
|
18068
|
+
for (const event of Object.keys(map3))
|
|
18069
|
+
server.on(event, map3[event]);
|
|
18070
|
+
return function removeListeners() {
|
|
18071
|
+
for (const event of Object.keys(map3)) {
|
|
18072
|
+
server.removeListener(event, map3[event]);
|
|
18073
|
+
}
|
|
18074
|
+
};
|
|
18075
|
+
}
|
|
18076
|
+
function emitClose(server) {
|
|
18077
|
+
server._state = CLOSED;
|
|
18078
|
+
server.emit("close");
|
|
18079
|
+
}
|
|
18080
|
+
function socketOnError() {
|
|
18081
|
+
this.destroy();
|
|
18082
|
+
}
|
|
18083
|
+
function abortHandshake(socket, code, message, headers) {
|
|
18084
|
+
message = message || http.STATUS_CODES[code];
|
|
18085
|
+
headers = {
|
|
18086
|
+
Connection: "close",
|
|
18087
|
+
"Content-Type": "text/html",
|
|
18088
|
+
"Content-Length": Buffer.byteLength(message),
|
|
18089
|
+
...headers
|
|
18090
|
+
};
|
|
18091
|
+
socket.once("finish", socket.destroy);
|
|
18092
|
+
socket.end(`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
18093
|
+
` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join(`\r
|
|
18094
|
+
`) + `\r
|
|
18095
|
+
\r
|
|
18096
|
+
` + message);
|
|
18097
|
+
}
|
|
18098
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
18099
|
+
if (server.listenerCount("wsClientError")) {
|
|
18100
|
+
const err = new Error(message);
|
|
18101
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
18102
|
+
server.emit("wsClientError", err, socket, req);
|
|
18103
|
+
} else {
|
|
18104
|
+
abortHandshake(socket, code, message, headers);
|
|
18105
|
+
}
|
|
18106
|
+
}
|
|
18107
|
+
});
|
|
18108
|
+
|
|
18109
|
+
// node_modules/ws/index.js
|
|
18110
|
+
var require_ws = __commonJS((exports, module) => {
|
|
18111
|
+
var WebSocket = require_websocket();
|
|
18112
|
+
WebSocket.createWebSocketStream = require_stream();
|
|
18113
|
+
WebSocket.Server = require_websocket_server();
|
|
18114
|
+
WebSocket.Receiver = require_receiver();
|
|
18115
|
+
WebSocket.Sender = require_sender();
|
|
18116
|
+
WebSocket.WebSocket = WebSocket;
|
|
18117
|
+
WebSocket.WebSocketServer = WebSocket.Server;
|
|
18118
|
+
module.exports = WebSocket;
|
|
18119
|
+
});
|
|
18120
|
+
|
|
15270
18121
|
// node_modules/@modelcontextprotocol/sdk/node_modules/zod/v3/helpers/util.js
|
|
15271
18122
|
var util;
|
|
15272
18123
|
(function(util2) {
|
|
@@ -52205,6 +55056,15 @@ var sessionLog6 = createSessionLog(log19);
|
|
|
52205
55056
|
// src/operations/monitor/handler.ts
|
|
52206
55057
|
var log20 = createLogger("monitor");
|
|
52207
55058
|
var DEFAULT_INTERVAL_MS = 60 * 1000;
|
|
55059
|
+
// src/utils/websocket.ts
|
|
55060
|
+
var WS;
|
|
55061
|
+
if (typeof globalThis.WebSocket !== "undefined") {
|
|
55062
|
+
WS = globalThis.WebSocket;
|
|
55063
|
+
} else {
|
|
55064
|
+
const wsModule = require_ws();
|
|
55065
|
+
WS = wsModule.default ?? wsModule;
|
|
55066
|
+
}
|
|
55067
|
+
|
|
52208
55068
|
// src/platform/mattermost/formatter.ts
|
|
52209
55069
|
class MattermostFormatter {
|
|
52210
55070
|
formatBold(text) {
|
|
@@ -52401,10 +55261,10 @@ class MattermostPermissionApi {
|
|
|
52401
55261
|
return new Promise((resolve4) => {
|
|
52402
55262
|
const wsUrl = this.config.url.replace(/^http/, "ws") + "/api/v4/websocket";
|
|
52403
55263
|
mcpLogger.debug(`Connecting to WebSocket: ${wsUrl}`);
|
|
52404
|
-
const ws = new
|
|
55264
|
+
const ws = new WS(wsUrl);
|
|
52405
55265
|
let resolved = false;
|
|
52406
55266
|
const cleanup = () => {
|
|
52407
|
-
if (ws.readyState ===
|
|
55267
|
+
if (ws.readyState === WS.OPEN) {
|
|
52408
55268
|
ws.close();
|
|
52409
55269
|
}
|
|
52410
55270
|
};
|
|
@@ -52648,7 +55508,7 @@ class SlackPermissionApi {
|
|
|
52648
55508
|
let resolved = false;
|
|
52649
55509
|
let ws = null;
|
|
52650
55510
|
const cleanup = () => {
|
|
52651
|
-
if (ws && ws.readyState ===
|
|
55511
|
+
if (ws && ws.readyState === WS.OPEN) {
|
|
52652
55512
|
ws.close();
|
|
52653
55513
|
}
|
|
52654
55514
|
};
|
|
@@ -52664,7 +55524,7 @@ class SlackPermissionApi {
|
|
|
52664
55524
|
if (resolved)
|
|
52665
55525
|
return;
|
|
52666
55526
|
mcpLogger.debug(`Connecting to Socket Mode: ${wsUrl.substring(0, 50)}...`);
|
|
52667
|
-
ws = new
|
|
55527
|
+
ws = new WS(wsUrl);
|
|
52668
55528
|
ws.onopen = () => {
|
|
52669
55529
|
mcpLogger.debug("Socket Mode WebSocket connected");
|
|
52670
55530
|
};
|