@playcademy/sandbox 0.1.0-beta.14 → 0.1.0-beta.16
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/dist/cli.js +2940 -4
- package/dist/lib/realtime.d.ts +2 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +2940 -4
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -49389,6 +49389,2843 @@ var init_dist2 = __esm(() => {
|
|
|
49389
49389
|
}
|
|
49390
49390
|
});
|
|
49391
49391
|
|
|
49392
|
+
// ../../node_modules/ws/lib/constants.js
|
|
49393
|
+
var require_constants = __commonJS((exports, module2) => {
|
|
49394
|
+
var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
49395
|
+
var hasBlob = typeof Blob !== "undefined";
|
|
49396
|
+
if (hasBlob)
|
|
49397
|
+
BINARY_TYPES.push("blob");
|
|
49398
|
+
module2.exports = {
|
|
49399
|
+
BINARY_TYPES,
|
|
49400
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
49401
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
49402
|
+
hasBlob,
|
|
49403
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
49404
|
+
kListener: Symbol("kListener"),
|
|
49405
|
+
kStatusCode: Symbol("status-code"),
|
|
49406
|
+
kWebSocket: Symbol("websocket"),
|
|
49407
|
+
NOOP: () => {}
|
|
49408
|
+
};
|
|
49409
|
+
});
|
|
49410
|
+
|
|
49411
|
+
// ../../node_modules/ws/lib/buffer-util.js
|
|
49412
|
+
var require_buffer_util = __commonJS((exports, module2) => {
|
|
49413
|
+
var { EMPTY_BUFFER } = require_constants();
|
|
49414
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
49415
|
+
function concat2(list, totalLength) {
|
|
49416
|
+
if (list.length === 0)
|
|
49417
|
+
return EMPTY_BUFFER;
|
|
49418
|
+
if (list.length === 1)
|
|
49419
|
+
return list[0];
|
|
49420
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
49421
|
+
let offset = 0;
|
|
49422
|
+
for (let i3 = 0;i3 < list.length; i3++) {
|
|
49423
|
+
const buf = list[i3];
|
|
49424
|
+
target.set(buf, offset);
|
|
49425
|
+
offset += buf.length;
|
|
49426
|
+
}
|
|
49427
|
+
if (offset < totalLength) {
|
|
49428
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
49429
|
+
}
|
|
49430
|
+
return target;
|
|
49431
|
+
}
|
|
49432
|
+
function _mask(source, mask, output, offset, length) {
|
|
49433
|
+
for (let i3 = 0;i3 < length; i3++) {
|
|
49434
|
+
output[offset + i3] = source[i3] ^ mask[i3 & 3];
|
|
49435
|
+
}
|
|
49436
|
+
}
|
|
49437
|
+
function _unmask(buffer, mask) {
|
|
49438
|
+
for (let i3 = 0;i3 < buffer.length; i3++) {
|
|
49439
|
+
buffer[i3] ^= mask[i3 & 3];
|
|
49440
|
+
}
|
|
49441
|
+
}
|
|
49442
|
+
function toArrayBuffer(buf) {
|
|
49443
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
49444
|
+
return buf.buffer;
|
|
49445
|
+
}
|
|
49446
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
49447
|
+
}
|
|
49448
|
+
function toBuffer(data) {
|
|
49449
|
+
toBuffer.readOnly = true;
|
|
49450
|
+
if (Buffer.isBuffer(data))
|
|
49451
|
+
return data;
|
|
49452
|
+
let buf;
|
|
49453
|
+
if (data instanceof ArrayBuffer) {
|
|
49454
|
+
buf = new FastBuffer(data);
|
|
49455
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
49456
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
49457
|
+
} else {
|
|
49458
|
+
buf = Buffer.from(data);
|
|
49459
|
+
toBuffer.readOnly = false;
|
|
49460
|
+
}
|
|
49461
|
+
return buf;
|
|
49462
|
+
}
|
|
49463
|
+
module2.exports = {
|
|
49464
|
+
concat: concat2,
|
|
49465
|
+
mask: _mask,
|
|
49466
|
+
toArrayBuffer,
|
|
49467
|
+
toBuffer,
|
|
49468
|
+
unmask: _unmask
|
|
49469
|
+
};
|
|
49470
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
49471
|
+
try {
|
|
49472
|
+
const bufferUtil = (()=>{throw new Error("Cannot require module "+"bufferutil");})();
|
|
49473
|
+
module2.exports.mask = function(source, mask, output, offset, length) {
|
|
49474
|
+
if (length < 48)
|
|
49475
|
+
_mask(source, mask, output, offset, length);
|
|
49476
|
+
else
|
|
49477
|
+
bufferUtil.mask(source, mask, output, offset, length);
|
|
49478
|
+
};
|
|
49479
|
+
module2.exports.unmask = function(buffer, mask) {
|
|
49480
|
+
if (buffer.length < 32)
|
|
49481
|
+
_unmask(buffer, mask);
|
|
49482
|
+
else
|
|
49483
|
+
bufferUtil.unmask(buffer, mask);
|
|
49484
|
+
};
|
|
49485
|
+
} catch (e) {}
|
|
49486
|
+
}
|
|
49487
|
+
});
|
|
49488
|
+
|
|
49489
|
+
// ../../node_modules/ws/lib/limiter.js
|
|
49490
|
+
var require_limiter = __commonJS((exports, module2) => {
|
|
49491
|
+
var kDone = Symbol("kDone");
|
|
49492
|
+
var kRun = Symbol("kRun");
|
|
49493
|
+
|
|
49494
|
+
class Limiter {
|
|
49495
|
+
constructor(concurrency) {
|
|
49496
|
+
this[kDone] = () => {
|
|
49497
|
+
this.pending--;
|
|
49498
|
+
this[kRun]();
|
|
49499
|
+
};
|
|
49500
|
+
this.concurrency = concurrency || Infinity;
|
|
49501
|
+
this.jobs = [];
|
|
49502
|
+
this.pending = 0;
|
|
49503
|
+
}
|
|
49504
|
+
add(job) {
|
|
49505
|
+
this.jobs.push(job);
|
|
49506
|
+
this[kRun]();
|
|
49507
|
+
}
|
|
49508
|
+
[kRun]() {
|
|
49509
|
+
if (this.pending === this.concurrency)
|
|
49510
|
+
return;
|
|
49511
|
+
if (this.jobs.length) {
|
|
49512
|
+
const job = this.jobs.shift();
|
|
49513
|
+
this.pending++;
|
|
49514
|
+
job(this[kDone]);
|
|
49515
|
+
}
|
|
49516
|
+
}
|
|
49517
|
+
}
|
|
49518
|
+
module2.exports = Limiter;
|
|
49519
|
+
});
|
|
49520
|
+
|
|
49521
|
+
// ../../node_modules/ws/lib/permessage-deflate.js
|
|
49522
|
+
var require_permessage_deflate = __commonJS((exports, module2) => {
|
|
49523
|
+
var zlib = __require("zlib");
|
|
49524
|
+
var bufferUtil = require_buffer_util();
|
|
49525
|
+
var Limiter = require_limiter();
|
|
49526
|
+
var { kStatusCode } = require_constants();
|
|
49527
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
49528
|
+
var TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
49529
|
+
var kPerMessageDeflate = Symbol("permessage-deflate");
|
|
49530
|
+
var kTotalLength = Symbol("total-length");
|
|
49531
|
+
var kCallback = Symbol("callback");
|
|
49532
|
+
var kBuffers = Symbol("buffers");
|
|
49533
|
+
var kError = Symbol("error");
|
|
49534
|
+
var zlibLimiter;
|
|
49535
|
+
|
|
49536
|
+
class PerMessageDeflate {
|
|
49537
|
+
constructor(options, isServer, maxPayload) {
|
|
49538
|
+
this._maxPayload = maxPayload | 0;
|
|
49539
|
+
this._options = options || {};
|
|
49540
|
+
this._threshold = this._options.threshold !== undefined ? this._options.threshold : 1024;
|
|
49541
|
+
this._isServer = !!isServer;
|
|
49542
|
+
this._deflate = null;
|
|
49543
|
+
this._inflate = null;
|
|
49544
|
+
this.params = null;
|
|
49545
|
+
if (!zlibLimiter) {
|
|
49546
|
+
const concurrency = this._options.concurrencyLimit !== undefined ? this._options.concurrencyLimit : 10;
|
|
49547
|
+
zlibLimiter = new Limiter(concurrency);
|
|
49548
|
+
}
|
|
49549
|
+
}
|
|
49550
|
+
static get extensionName() {
|
|
49551
|
+
return "permessage-deflate";
|
|
49552
|
+
}
|
|
49553
|
+
offer() {
|
|
49554
|
+
const params = {};
|
|
49555
|
+
if (this._options.serverNoContextTakeover) {
|
|
49556
|
+
params.server_no_context_takeover = true;
|
|
49557
|
+
}
|
|
49558
|
+
if (this._options.clientNoContextTakeover) {
|
|
49559
|
+
params.client_no_context_takeover = true;
|
|
49560
|
+
}
|
|
49561
|
+
if (this._options.serverMaxWindowBits) {
|
|
49562
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
49563
|
+
}
|
|
49564
|
+
if (this._options.clientMaxWindowBits) {
|
|
49565
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
49566
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
49567
|
+
params.client_max_window_bits = true;
|
|
49568
|
+
}
|
|
49569
|
+
return params;
|
|
49570
|
+
}
|
|
49571
|
+
accept(configurations) {
|
|
49572
|
+
configurations = this.normalizeParams(configurations);
|
|
49573
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
49574
|
+
return this.params;
|
|
49575
|
+
}
|
|
49576
|
+
cleanup() {
|
|
49577
|
+
if (this._inflate) {
|
|
49578
|
+
this._inflate.close();
|
|
49579
|
+
this._inflate = null;
|
|
49580
|
+
}
|
|
49581
|
+
if (this._deflate) {
|
|
49582
|
+
const callback = this._deflate[kCallback];
|
|
49583
|
+
this._deflate.close();
|
|
49584
|
+
this._deflate = null;
|
|
49585
|
+
if (callback) {
|
|
49586
|
+
callback(new Error("The deflate stream was closed while data was being processed"));
|
|
49587
|
+
}
|
|
49588
|
+
}
|
|
49589
|
+
}
|
|
49590
|
+
acceptAsServer(offers) {
|
|
49591
|
+
const opts = this._options;
|
|
49592
|
+
const accepted = offers.find((params) => {
|
|
49593
|
+
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) {
|
|
49594
|
+
return false;
|
|
49595
|
+
}
|
|
49596
|
+
return true;
|
|
49597
|
+
});
|
|
49598
|
+
if (!accepted) {
|
|
49599
|
+
throw new Error("None of the extension offers can be accepted");
|
|
49600
|
+
}
|
|
49601
|
+
if (opts.serverNoContextTakeover) {
|
|
49602
|
+
accepted.server_no_context_takeover = true;
|
|
49603
|
+
}
|
|
49604
|
+
if (opts.clientNoContextTakeover) {
|
|
49605
|
+
accepted.client_no_context_takeover = true;
|
|
49606
|
+
}
|
|
49607
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
49608
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
49609
|
+
}
|
|
49610
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
49611
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
49612
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
49613
|
+
delete accepted.client_max_window_bits;
|
|
49614
|
+
}
|
|
49615
|
+
return accepted;
|
|
49616
|
+
}
|
|
49617
|
+
acceptAsClient(response) {
|
|
49618
|
+
const params = response[0];
|
|
49619
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
49620
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
49621
|
+
}
|
|
49622
|
+
if (!params.client_max_window_bits) {
|
|
49623
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
49624
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
49625
|
+
}
|
|
49626
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
49627
|
+
throw new Error('Unexpected or invalid parameter "client_max_window_bits"');
|
|
49628
|
+
}
|
|
49629
|
+
return params;
|
|
49630
|
+
}
|
|
49631
|
+
normalizeParams(configurations) {
|
|
49632
|
+
configurations.forEach((params) => {
|
|
49633
|
+
Object.keys(params).forEach((key) => {
|
|
49634
|
+
let value = params[key];
|
|
49635
|
+
if (value.length > 1) {
|
|
49636
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
49637
|
+
}
|
|
49638
|
+
value = value[0];
|
|
49639
|
+
if (key === "client_max_window_bits") {
|
|
49640
|
+
if (value !== true) {
|
|
49641
|
+
const num = +value;
|
|
49642
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
49643
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
49644
|
+
}
|
|
49645
|
+
value = num;
|
|
49646
|
+
} else if (!this._isServer) {
|
|
49647
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
49648
|
+
}
|
|
49649
|
+
} else if (key === "server_max_window_bits") {
|
|
49650
|
+
const num = +value;
|
|
49651
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
49652
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
49653
|
+
}
|
|
49654
|
+
value = num;
|
|
49655
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
49656
|
+
if (value !== true) {
|
|
49657
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
49658
|
+
}
|
|
49659
|
+
} else {
|
|
49660
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
49661
|
+
}
|
|
49662
|
+
params[key] = value;
|
|
49663
|
+
});
|
|
49664
|
+
});
|
|
49665
|
+
return configurations;
|
|
49666
|
+
}
|
|
49667
|
+
decompress(data, fin, callback) {
|
|
49668
|
+
zlibLimiter.add((done) => {
|
|
49669
|
+
this._decompress(data, fin, (err2, result) => {
|
|
49670
|
+
done();
|
|
49671
|
+
callback(err2, result);
|
|
49672
|
+
});
|
|
49673
|
+
});
|
|
49674
|
+
}
|
|
49675
|
+
compress(data, fin, callback) {
|
|
49676
|
+
zlibLimiter.add((done) => {
|
|
49677
|
+
this._compress(data, fin, (err2, result) => {
|
|
49678
|
+
done();
|
|
49679
|
+
callback(err2, result);
|
|
49680
|
+
});
|
|
49681
|
+
});
|
|
49682
|
+
}
|
|
49683
|
+
_decompress(data, fin, callback) {
|
|
49684
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
49685
|
+
if (!this._inflate) {
|
|
49686
|
+
const key = `${endpoint}_max_window_bits`;
|
|
49687
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
49688
|
+
this._inflate = zlib.createInflateRaw({
|
|
49689
|
+
...this._options.zlibInflateOptions,
|
|
49690
|
+
windowBits
|
|
49691
|
+
});
|
|
49692
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
49693
|
+
this._inflate[kTotalLength] = 0;
|
|
49694
|
+
this._inflate[kBuffers] = [];
|
|
49695
|
+
this._inflate.on("error", inflateOnError);
|
|
49696
|
+
this._inflate.on("data", inflateOnData);
|
|
49697
|
+
}
|
|
49698
|
+
this._inflate[kCallback] = callback;
|
|
49699
|
+
this._inflate.write(data);
|
|
49700
|
+
if (fin)
|
|
49701
|
+
this._inflate.write(TRAILER);
|
|
49702
|
+
this._inflate.flush(() => {
|
|
49703
|
+
const err2 = this._inflate[kError];
|
|
49704
|
+
if (err2) {
|
|
49705
|
+
this._inflate.close();
|
|
49706
|
+
this._inflate = null;
|
|
49707
|
+
callback(err2);
|
|
49708
|
+
return;
|
|
49709
|
+
}
|
|
49710
|
+
const data2 = bufferUtil.concat(this._inflate[kBuffers], this._inflate[kTotalLength]);
|
|
49711
|
+
if (this._inflate._readableState.endEmitted) {
|
|
49712
|
+
this._inflate.close();
|
|
49713
|
+
this._inflate = null;
|
|
49714
|
+
} else {
|
|
49715
|
+
this._inflate[kTotalLength] = 0;
|
|
49716
|
+
this._inflate[kBuffers] = [];
|
|
49717
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
49718
|
+
this._inflate.reset();
|
|
49719
|
+
}
|
|
49720
|
+
}
|
|
49721
|
+
callback(null, data2);
|
|
49722
|
+
});
|
|
49723
|
+
}
|
|
49724
|
+
_compress(data, fin, callback) {
|
|
49725
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
49726
|
+
if (!this._deflate) {
|
|
49727
|
+
const key = `${endpoint}_max_window_bits`;
|
|
49728
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
49729
|
+
this._deflate = zlib.createDeflateRaw({
|
|
49730
|
+
...this._options.zlibDeflateOptions,
|
|
49731
|
+
windowBits
|
|
49732
|
+
});
|
|
49733
|
+
this._deflate[kTotalLength] = 0;
|
|
49734
|
+
this._deflate[kBuffers] = [];
|
|
49735
|
+
this._deflate.on("data", deflateOnData);
|
|
49736
|
+
}
|
|
49737
|
+
this._deflate[kCallback] = callback;
|
|
49738
|
+
this._deflate.write(data);
|
|
49739
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
49740
|
+
if (!this._deflate) {
|
|
49741
|
+
return;
|
|
49742
|
+
}
|
|
49743
|
+
let data2 = bufferUtil.concat(this._deflate[kBuffers], this._deflate[kTotalLength]);
|
|
49744
|
+
if (fin) {
|
|
49745
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
49746
|
+
}
|
|
49747
|
+
this._deflate[kCallback] = null;
|
|
49748
|
+
this._deflate[kTotalLength] = 0;
|
|
49749
|
+
this._deflate[kBuffers] = [];
|
|
49750
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
49751
|
+
this._deflate.reset();
|
|
49752
|
+
}
|
|
49753
|
+
callback(null, data2);
|
|
49754
|
+
});
|
|
49755
|
+
}
|
|
49756
|
+
}
|
|
49757
|
+
module2.exports = PerMessageDeflate;
|
|
49758
|
+
function deflateOnData(chunk) {
|
|
49759
|
+
this[kBuffers].push(chunk);
|
|
49760
|
+
this[kTotalLength] += chunk.length;
|
|
49761
|
+
}
|
|
49762
|
+
function inflateOnData(chunk) {
|
|
49763
|
+
this[kTotalLength] += chunk.length;
|
|
49764
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
49765
|
+
this[kBuffers].push(chunk);
|
|
49766
|
+
return;
|
|
49767
|
+
}
|
|
49768
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
49769
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
49770
|
+
this[kError][kStatusCode] = 1009;
|
|
49771
|
+
this.removeListener("data", inflateOnData);
|
|
49772
|
+
this.reset();
|
|
49773
|
+
}
|
|
49774
|
+
function inflateOnError(err2) {
|
|
49775
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
49776
|
+
if (this[kError]) {
|
|
49777
|
+
this[kCallback](this[kError]);
|
|
49778
|
+
return;
|
|
49779
|
+
}
|
|
49780
|
+
err2[kStatusCode] = 1007;
|
|
49781
|
+
this[kCallback](err2);
|
|
49782
|
+
}
|
|
49783
|
+
});
|
|
49784
|
+
|
|
49785
|
+
// ../../node_modules/ws/lib/validation.js
|
|
49786
|
+
var require_validation = __commonJS((exports, module2) => {
|
|
49787
|
+
var { isUtf8 } = __require("buffer");
|
|
49788
|
+
var { hasBlob } = require_constants();
|
|
49789
|
+
var tokenChars = [
|
|
49790
|
+
0,
|
|
49791
|
+
0,
|
|
49792
|
+
0,
|
|
49793
|
+
0,
|
|
49794
|
+
0,
|
|
49795
|
+
0,
|
|
49796
|
+
0,
|
|
49797
|
+
0,
|
|
49798
|
+
0,
|
|
49799
|
+
0,
|
|
49800
|
+
0,
|
|
49801
|
+
0,
|
|
49802
|
+
0,
|
|
49803
|
+
0,
|
|
49804
|
+
0,
|
|
49805
|
+
0,
|
|
49806
|
+
0,
|
|
49807
|
+
0,
|
|
49808
|
+
0,
|
|
49809
|
+
0,
|
|
49810
|
+
0,
|
|
49811
|
+
0,
|
|
49812
|
+
0,
|
|
49813
|
+
0,
|
|
49814
|
+
0,
|
|
49815
|
+
0,
|
|
49816
|
+
0,
|
|
49817
|
+
0,
|
|
49818
|
+
0,
|
|
49819
|
+
0,
|
|
49820
|
+
0,
|
|
49821
|
+
0,
|
|
49822
|
+
0,
|
|
49823
|
+
1,
|
|
49824
|
+
0,
|
|
49825
|
+
1,
|
|
49826
|
+
1,
|
|
49827
|
+
1,
|
|
49828
|
+
1,
|
|
49829
|
+
1,
|
|
49830
|
+
0,
|
|
49831
|
+
0,
|
|
49832
|
+
1,
|
|
49833
|
+
1,
|
|
49834
|
+
0,
|
|
49835
|
+
1,
|
|
49836
|
+
1,
|
|
49837
|
+
0,
|
|
49838
|
+
1,
|
|
49839
|
+
1,
|
|
49840
|
+
1,
|
|
49841
|
+
1,
|
|
49842
|
+
1,
|
|
49843
|
+
1,
|
|
49844
|
+
1,
|
|
49845
|
+
1,
|
|
49846
|
+
1,
|
|
49847
|
+
1,
|
|
49848
|
+
0,
|
|
49849
|
+
0,
|
|
49850
|
+
0,
|
|
49851
|
+
0,
|
|
49852
|
+
0,
|
|
49853
|
+
0,
|
|
49854
|
+
0,
|
|
49855
|
+
1,
|
|
49856
|
+
1,
|
|
49857
|
+
1,
|
|
49858
|
+
1,
|
|
49859
|
+
1,
|
|
49860
|
+
1,
|
|
49861
|
+
1,
|
|
49862
|
+
1,
|
|
49863
|
+
1,
|
|
49864
|
+
1,
|
|
49865
|
+
1,
|
|
49866
|
+
1,
|
|
49867
|
+
1,
|
|
49868
|
+
1,
|
|
49869
|
+
1,
|
|
49870
|
+
1,
|
|
49871
|
+
1,
|
|
49872
|
+
1,
|
|
49873
|
+
1,
|
|
49874
|
+
1,
|
|
49875
|
+
1,
|
|
49876
|
+
1,
|
|
49877
|
+
1,
|
|
49878
|
+
1,
|
|
49879
|
+
1,
|
|
49880
|
+
1,
|
|
49881
|
+
0,
|
|
49882
|
+
0,
|
|
49883
|
+
0,
|
|
49884
|
+
1,
|
|
49885
|
+
1,
|
|
49886
|
+
1,
|
|
49887
|
+
1,
|
|
49888
|
+
1,
|
|
49889
|
+
1,
|
|
49890
|
+
1,
|
|
49891
|
+
1,
|
|
49892
|
+
1,
|
|
49893
|
+
1,
|
|
49894
|
+
1,
|
|
49895
|
+
1,
|
|
49896
|
+
1,
|
|
49897
|
+
1,
|
|
49898
|
+
1,
|
|
49899
|
+
1,
|
|
49900
|
+
1,
|
|
49901
|
+
1,
|
|
49902
|
+
1,
|
|
49903
|
+
1,
|
|
49904
|
+
1,
|
|
49905
|
+
1,
|
|
49906
|
+
1,
|
|
49907
|
+
1,
|
|
49908
|
+
1,
|
|
49909
|
+
1,
|
|
49910
|
+
1,
|
|
49911
|
+
1,
|
|
49912
|
+
1,
|
|
49913
|
+
0,
|
|
49914
|
+
1,
|
|
49915
|
+
0,
|
|
49916
|
+
1,
|
|
49917
|
+
0
|
|
49918
|
+
];
|
|
49919
|
+
function isValidStatusCode(code) {
|
|
49920
|
+
return code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3000 && code <= 4999;
|
|
49921
|
+
}
|
|
49922
|
+
function _isValidUTF8(buf) {
|
|
49923
|
+
const len = buf.length;
|
|
49924
|
+
let i3 = 0;
|
|
49925
|
+
while (i3 < len) {
|
|
49926
|
+
if ((buf[i3] & 128) === 0) {
|
|
49927
|
+
i3++;
|
|
49928
|
+
} else if ((buf[i3] & 224) === 192) {
|
|
49929
|
+
if (i3 + 1 === len || (buf[i3 + 1] & 192) !== 128 || (buf[i3] & 254) === 192) {
|
|
49930
|
+
return false;
|
|
49931
|
+
}
|
|
49932
|
+
i3 += 2;
|
|
49933
|
+
} else if ((buf[i3] & 240) === 224) {
|
|
49934
|
+
if (i3 + 2 >= len || (buf[i3 + 1] & 192) !== 128 || (buf[i3 + 2] & 192) !== 128 || buf[i3] === 224 && (buf[i3 + 1] & 224) === 128 || buf[i3] === 237 && (buf[i3 + 1] & 224) === 160) {
|
|
49935
|
+
return false;
|
|
49936
|
+
}
|
|
49937
|
+
i3 += 3;
|
|
49938
|
+
} else if ((buf[i3] & 248) === 240) {
|
|
49939
|
+
if (i3 + 3 >= len || (buf[i3 + 1] & 192) !== 128 || (buf[i3 + 2] & 192) !== 128 || (buf[i3 + 3] & 192) !== 128 || buf[i3] === 240 && (buf[i3 + 1] & 240) === 128 || buf[i3] === 244 && buf[i3 + 1] > 143 || buf[i3] > 244) {
|
|
49940
|
+
return false;
|
|
49941
|
+
}
|
|
49942
|
+
i3 += 4;
|
|
49943
|
+
} else {
|
|
49944
|
+
return false;
|
|
49945
|
+
}
|
|
49946
|
+
}
|
|
49947
|
+
return true;
|
|
49948
|
+
}
|
|
49949
|
+
function isBlob(value) {
|
|
49950
|
+
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");
|
|
49951
|
+
}
|
|
49952
|
+
module2.exports = {
|
|
49953
|
+
isBlob,
|
|
49954
|
+
isValidStatusCode,
|
|
49955
|
+
isValidUTF8: _isValidUTF8,
|
|
49956
|
+
tokenChars
|
|
49957
|
+
};
|
|
49958
|
+
if (isUtf8) {
|
|
49959
|
+
module2.exports.isValidUTF8 = function(buf) {
|
|
49960
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
49961
|
+
};
|
|
49962
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
49963
|
+
try {
|
|
49964
|
+
const isValidUTF8 = (()=>{throw new Error("Cannot require module "+"utf-8-validate");})();
|
|
49965
|
+
module2.exports.isValidUTF8 = function(buf) {
|
|
49966
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
49967
|
+
};
|
|
49968
|
+
} catch (e) {}
|
|
49969
|
+
}
|
|
49970
|
+
});
|
|
49971
|
+
|
|
49972
|
+
// ../../node_modules/ws/lib/receiver.js
|
|
49973
|
+
var require_receiver = __commonJS((exports, module2) => {
|
|
49974
|
+
var { Writable } = __require("stream");
|
|
49975
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
49976
|
+
var {
|
|
49977
|
+
BINARY_TYPES,
|
|
49978
|
+
EMPTY_BUFFER,
|
|
49979
|
+
kStatusCode,
|
|
49980
|
+
kWebSocket
|
|
49981
|
+
} = require_constants();
|
|
49982
|
+
var { concat: concat2, toArrayBuffer, unmask } = require_buffer_util();
|
|
49983
|
+
var { isValidStatusCode, isValidUTF8 } = require_validation();
|
|
49984
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
49985
|
+
var GET_INFO = 0;
|
|
49986
|
+
var GET_PAYLOAD_LENGTH_16 = 1;
|
|
49987
|
+
var GET_PAYLOAD_LENGTH_64 = 2;
|
|
49988
|
+
var GET_MASK = 3;
|
|
49989
|
+
var GET_DATA = 4;
|
|
49990
|
+
var INFLATING = 5;
|
|
49991
|
+
var DEFER_EVENT = 6;
|
|
49992
|
+
|
|
49993
|
+
class Receiver extends Writable {
|
|
49994
|
+
constructor(options = {}) {
|
|
49995
|
+
super();
|
|
49996
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== undefined ? options.allowSynchronousEvents : true;
|
|
49997
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
49998
|
+
this._extensions = options.extensions || {};
|
|
49999
|
+
this._isServer = !!options.isServer;
|
|
50000
|
+
this._maxPayload = options.maxPayload | 0;
|
|
50001
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
50002
|
+
this[kWebSocket] = undefined;
|
|
50003
|
+
this._bufferedBytes = 0;
|
|
50004
|
+
this._buffers = [];
|
|
50005
|
+
this._compressed = false;
|
|
50006
|
+
this._payloadLength = 0;
|
|
50007
|
+
this._mask = undefined;
|
|
50008
|
+
this._fragmented = 0;
|
|
50009
|
+
this._masked = false;
|
|
50010
|
+
this._fin = false;
|
|
50011
|
+
this._opcode = 0;
|
|
50012
|
+
this._totalPayloadLength = 0;
|
|
50013
|
+
this._messageLength = 0;
|
|
50014
|
+
this._fragments = [];
|
|
50015
|
+
this._errored = false;
|
|
50016
|
+
this._loop = false;
|
|
50017
|
+
this._state = GET_INFO;
|
|
50018
|
+
}
|
|
50019
|
+
_write(chunk, encoding, cb) {
|
|
50020
|
+
if (this._opcode === 8 && this._state == GET_INFO)
|
|
50021
|
+
return cb();
|
|
50022
|
+
this._bufferedBytes += chunk.length;
|
|
50023
|
+
this._buffers.push(chunk);
|
|
50024
|
+
this.startLoop(cb);
|
|
50025
|
+
}
|
|
50026
|
+
consume(n3) {
|
|
50027
|
+
this._bufferedBytes -= n3;
|
|
50028
|
+
if (n3 === this._buffers[0].length)
|
|
50029
|
+
return this._buffers.shift();
|
|
50030
|
+
if (n3 < this._buffers[0].length) {
|
|
50031
|
+
const buf = this._buffers[0];
|
|
50032
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n3, buf.length - n3);
|
|
50033
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n3);
|
|
50034
|
+
}
|
|
50035
|
+
const dst = Buffer.allocUnsafe(n3);
|
|
50036
|
+
do {
|
|
50037
|
+
const buf = this._buffers[0];
|
|
50038
|
+
const offset = dst.length - n3;
|
|
50039
|
+
if (n3 >= buf.length) {
|
|
50040
|
+
dst.set(this._buffers.shift(), offset);
|
|
50041
|
+
} else {
|
|
50042
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n3), offset);
|
|
50043
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n3, buf.length - n3);
|
|
50044
|
+
}
|
|
50045
|
+
n3 -= buf.length;
|
|
50046
|
+
} while (n3 > 0);
|
|
50047
|
+
return dst;
|
|
50048
|
+
}
|
|
50049
|
+
startLoop(cb) {
|
|
50050
|
+
this._loop = true;
|
|
50051
|
+
do {
|
|
50052
|
+
switch (this._state) {
|
|
50053
|
+
case GET_INFO:
|
|
50054
|
+
this.getInfo(cb);
|
|
50055
|
+
break;
|
|
50056
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
50057
|
+
this.getPayloadLength16(cb);
|
|
50058
|
+
break;
|
|
50059
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
50060
|
+
this.getPayloadLength64(cb);
|
|
50061
|
+
break;
|
|
50062
|
+
case GET_MASK:
|
|
50063
|
+
this.getMask();
|
|
50064
|
+
break;
|
|
50065
|
+
case GET_DATA:
|
|
50066
|
+
this.getData(cb);
|
|
50067
|
+
break;
|
|
50068
|
+
case INFLATING:
|
|
50069
|
+
case DEFER_EVENT:
|
|
50070
|
+
this._loop = false;
|
|
50071
|
+
return;
|
|
50072
|
+
}
|
|
50073
|
+
} while (this._loop);
|
|
50074
|
+
if (!this._errored)
|
|
50075
|
+
cb();
|
|
50076
|
+
}
|
|
50077
|
+
getInfo(cb) {
|
|
50078
|
+
if (this._bufferedBytes < 2) {
|
|
50079
|
+
this._loop = false;
|
|
50080
|
+
return;
|
|
50081
|
+
}
|
|
50082
|
+
const buf = this.consume(2);
|
|
50083
|
+
if ((buf[0] & 48) !== 0) {
|
|
50084
|
+
const error2 = this.createError(RangeError, "RSV2 and RSV3 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_2_3");
|
|
50085
|
+
cb(error2);
|
|
50086
|
+
return;
|
|
50087
|
+
}
|
|
50088
|
+
const compressed = (buf[0] & 64) === 64;
|
|
50089
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
50090
|
+
const error2 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
50091
|
+
cb(error2);
|
|
50092
|
+
return;
|
|
50093
|
+
}
|
|
50094
|
+
this._fin = (buf[0] & 128) === 128;
|
|
50095
|
+
this._opcode = buf[0] & 15;
|
|
50096
|
+
this._payloadLength = buf[1] & 127;
|
|
50097
|
+
if (this._opcode === 0) {
|
|
50098
|
+
if (compressed) {
|
|
50099
|
+
const error2 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
50100
|
+
cb(error2);
|
|
50101
|
+
return;
|
|
50102
|
+
}
|
|
50103
|
+
if (!this._fragmented) {
|
|
50104
|
+
const error2 = this.createError(RangeError, "invalid opcode 0", true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
50105
|
+
cb(error2);
|
|
50106
|
+
return;
|
|
50107
|
+
}
|
|
50108
|
+
this._opcode = this._fragmented;
|
|
50109
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
50110
|
+
if (this._fragmented) {
|
|
50111
|
+
const error2 = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
50112
|
+
cb(error2);
|
|
50113
|
+
return;
|
|
50114
|
+
}
|
|
50115
|
+
this._compressed = compressed;
|
|
50116
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
50117
|
+
if (!this._fin) {
|
|
50118
|
+
const error2 = this.createError(RangeError, "FIN must be set", true, 1002, "WS_ERR_EXPECTED_FIN");
|
|
50119
|
+
cb(error2);
|
|
50120
|
+
return;
|
|
50121
|
+
}
|
|
50122
|
+
if (compressed) {
|
|
50123
|
+
const error2 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
50124
|
+
cb(error2);
|
|
50125
|
+
return;
|
|
50126
|
+
}
|
|
50127
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
50128
|
+
const error2 = this.createError(RangeError, `invalid payload length ${this._payloadLength}`, true, 1002, "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");
|
|
50129
|
+
cb(error2);
|
|
50130
|
+
return;
|
|
50131
|
+
}
|
|
50132
|
+
} else {
|
|
50133
|
+
const error2 = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
50134
|
+
cb(error2);
|
|
50135
|
+
return;
|
|
50136
|
+
}
|
|
50137
|
+
if (!this._fin && !this._fragmented)
|
|
50138
|
+
this._fragmented = this._opcode;
|
|
50139
|
+
this._masked = (buf[1] & 128) === 128;
|
|
50140
|
+
if (this._isServer) {
|
|
50141
|
+
if (!this._masked) {
|
|
50142
|
+
const error2 = this.createError(RangeError, "MASK must be set", true, 1002, "WS_ERR_EXPECTED_MASK");
|
|
50143
|
+
cb(error2);
|
|
50144
|
+
return;
|
|
50145
|
+
}
|
|
50146
|
+
} else if (this._masked) {
|
|
50147
|
+
const error2 = this.createError(RangeError, "MASK must be clear", true, 1002, "WS_ERR_UNEXPECTED_MASK");
|
|
50148
|
+
cb(error2);
|
|
50149
|
+
return;
|
|
50150
|
+
}
|
|
50151
|
+
if (this._payloadLength === 126)
|
|
50152
|
+
this._state = GET_PAYLOAD_LENGTH_16;
|
|
50153
|
+
else if (this._payloadLength === 127)
|
|
50154
|
+
this._state = GET_PAYLOAD_LENGTH_64;
|
|
50155
|
+
else
|
|
50156
|
+
this.haveLength(cb);
|
|
50157
|
+
}
|
|
50158
|
+
getPayloadLength16(cb) {
|
|
50159
|
+
if (this._bufferedBytes < 2) {
|
|
50160
|
+
this._loop = false;
|
|
50161
|
+
return;
|
|
50162
|
+
}
|
|
50163
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
50164
|
+
this.haveLength(cb);
|
|
50165
|
+
}
|
|
50166
|
+
getPayloadLength64(cb) {
|
|
50167
|
+
if (this._bufferedBytes < 8) {
|
|
50168
|
+
this._loop = false;
|
|
50169
|
+
return;
|
|
50170
|
+
}
|
|
50171
|
+
const buf = this.consume(8);
|
|
50172
|
+
const num = buf.readUInt32BE(0);
|
|
50173
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
50174
|
+
const error2 = this.createError(RangeError, "Unsupported WebSocket frame: payload length > 2^53 - 1", false, 1009, "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");
|
|
50175
|
+
cb(error2);
|
|
50176
|
+
return;
|
|
50177
|
+
}
|
|
50178
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
50179
|
+
this.haveLength(cb);
|
|
50180
|
+
}
|
|
50181
|
+
haveLength(cb) {
|
|
50182
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
50183
|
+
this._totalPayloadLength += this._payloadLength;
|
|
50184
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
50185
|
+
const error2 = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
50186
|
+
cb(error2);
|
|
50187
|
+
return;
|
|
50188
|
+
}
|
|
50189
|
+
}
|
|
50190
|
+
if (this._masked)
|
|
50191
|
+
this._state = GET_MASK;
|
|
50192
|
+
else
|
|
50193
|
+
this._state = GET_DATA;
|
|
50194
|
+
}
|
|
50195
|
+
getMask() {
|
|
50196
|
+
if (this._bufferedBytes < 4) {
|
|
50197
|
+
this._loop = false;
|
|
50198
|
+
return;
|
|
50199
|
+
}
|
|
50200
|
+
this._mask = this.consume(4);
|
|
50201
|
+
this._state = GET_DATA;
|
|
50202
|
+
}
|
|
50203
|
+
getData(cb) {
|
|
50204
|
+
let data = EMPTY_BUFFER;
|
|
50205
|
+
if (this._payloadLength) {
|
|
50206
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
50207
|
+
this._loop = false;
|
|
50208
|
+
return;
|
|
50209
|
+
}
|
|
50210
|
+
data = this.consume(this._payloadLength);
|
|
50211
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
50212
|
+
unmask(data, this._mask);
|
|
50213
|
+
}
|
|
50214
|
+
}
|
|
50215
|
+
if (this._opcode > 7) {
|
|
50216
|
+
this.controlMessage(data, cb);
|
|
50217
|
+
return;
|
|
50218
|
+
}
|
|
50219
|
+
if (this._compressed) {
|
|
50220
|
+
this._state = INFLATING;
|
|
50221
|
+
this.decompress(data, cb);
|
|
50222
|
+
return;
|
|
50223
|
+
}
|
|
50224
|
+
if (data.length) {
|
|
50225
|
+
this._messageLength = this._totalPayloadLength;
|
|
50226
|
+
this._fragments.push(data);
|
|
50227
|
+
}
|
|
50228
|
+
this.dataMessage(cb);
|
|
50229
|
+
}
|
|
50230
|
+
decompress(data, cb) {
|
|
50231
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
50232
|
+
perMessageDeflate.decompress(data, this._fin, (err2, buf) => {
|
|
50233
|
+
if (err2)
|
|
50234
|
+
return cb(err2);
|
|
50235
|
+
if (buf.length) {
|
|
50236
|
+
this._messageLength += buf.length;
|
|
50237
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
50238
|
+
const error2 = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
50239
|
+
cb(error2);
|
|
50240
|
+
return;
|
|
50241
|
+
}
|
|
50242
|
+
this._fragments.push(buf);
|
|
50243
|
+
}
|
|
50244
|
+
this.dataMessage(cb);
|
|
50245
|
+
if (this._state === GET_INFO)
|
|
50246
|
+
this.startLoop(cb);
|
|
50247
|
+
});
|
|
50248
|
+
}
|
|
50249
|
+
dataMessage(cb) {
|
|
50250
|
+
if (!this._fin) {
|
|
50251
|
+
this._state = GET_INFO;
|
|
50252
|
+
return;
|
|
50253
|
+
}
|
|
50254
|
+
const messageLength = this._messageLength;
|
|
50255
|
+
const fragments = this._fragments;
|
|
50256
|
+
this._totalPayloadLength = 0;
|
|
50257
|
+
this._messageLength = 0;
|
|
50258
|
+
this._fragmented = 0;
|
|
50259
|
+
this._fragments = [];
|
|
50260
|
+
if (this._opcode === 2) {
|
|
50261
|
+
let data;
|
|
50262
|
+
if (this._binaryType === "nodebuffer") {
|
|
50263
|
+
data = concat2(fragments, messageLength);
|
|
50264
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
50265
|
+
data = toArrayBuffer(concat2(fragments, messageLength));
|
|
50266
|
+
} else if (this._binaryType === "blob") {
|
|
50267
|
+
data = new Blob(fragments);
|
|
50268
|
+
} else {
|
|
50269
|
+
data = fragments;
|
|
50270
|
+
}
|
|
50271
|
+
if (this._allowSynchronousEvents) {
|
|
50272
|
+
this.emit("message", data, true);
|
|
50273
|
+
this._state = GET_INFO;
|
|
50274
|
+
} else {
|
|
50275
|
+
this._state = DEFER_EVENT;
|
|
50276
|
+
setImmediate(() => {
|
|
50277
|
+
this.emit("message", data, true);
|
|
50278
|
+
this._state = GET_INFO;
|
|
50279
|
+
this.startLoop(cb);
|
|
50280
|
+
});
|
|
50281
|
+
}
|
|
50282
|
+
} else {
|
|
50283
|
+
const buf = concat2(fragments, messageLength);
|
|
50284
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
50285
|
+
const error2 = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
50286
|
+
cb(error2);
|
|
50287
|
+
return;
|
|
50288
|
+
}
|
|
50289
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
50290
|
+
this.emit("message", buf, false);
|
|
50291
|
+
this._state = GET_INFO;
|
|
50292
|
+
} else {
|
|
50293
|
+
this._state = DEFER_EVENT;
|
|
50294
|
+
setImmediate(() => {
|
|
50295
|
+
this.emit("message", buf, false);
|
|
50296
|
+
this._state = GET_INFO;
|
|
50297
|
+
this.startLoop(cb);
|
|
50298
|
+
});
|
|
50299
|
+
}
|
|
50300
|
+
}
|
|
50301
|
+
}
|
|
50302
|
+
controlMessage(data, cb) {
|
|
50303
|
+
if (this._opcode === 8) {
|
|
50304
|
+
if (data.length === 0) {
|
|
50305
|
+
this._loop = false;
|
|
50306
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
50307
|
+
this.end();
|
|
50308
|
+
} else {
|
|
50309
|
+
const code = data.readUInt16BE(0);
|
|
50310
|
+
if (!isValidStatusCode(code)) {
|
|
50311
|
+
const error2 = this.createError(RangeError, `invalid status code ${code}`, true, 1002, "WS_ERR_INVALID_CLOSE_CODE");
|
|
50312
|
+
cb(error2);
|
|
50313
|
+
return;
|
|
50314
|
+
}
|
|
50315
|
+
const buf = new FastBuffer(data.buffer, data.byteOffset + 2, data.length - 2);
|
|
50316
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
50317
|
+
const error2 = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
50318
|
+
cb(error2);
|
|
50319
|
+
return;
|
|
50320
|
+
}
|
|
50321
|
+
this._loop = false;
|
|
50322
|
+
this.emit("conclude", code, buf);
|
|
50323
|
+
this.end();
|
|
50324
|
+
}
|
|
50325
|
+
this._state = GET_INFO;
|
|
50326
|
+
return;
|
|
50327
|
+
}
|
|
50328
|
+
if (this._allowSynchronousEvents) {
|
|
50329
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
50330
|
+
this._state = GET_INFO;
|
|
50331
|
+
} else {
|
|
50332
|
+
this._state = DEFER_EVENT;
|
|
50333
|
+
setImmediate(() => {
|
|
50334
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
50335
|
+
this._state = GET_INFO;
|
|
50336
|
+
this.startLoop(cb);
|
|
50337
|
+
});
|
|
50338
|
+
}
|
|
50339
|
+
}
|
|
50340
|
+
createError(ErrorCtor, message3, prefix2, statusCode, errorCode) {
|
|
50341
|
+
this._loop = false;
|
|
50342
|
+
this._errored = true;
|
|
50343
|
+
const err2 = new ErrorCtor(prefix2 ? `Invalid WebSocket frame: ${message3}` : message3);
|
|
50344
|
+
Error.captureStackTrace(err2, this.createError);
|
|
50345
|
+
err2.code = errorCode;
|
|
50346
|
+
err2[kStatusCode] = statusCode;
|
|
50347
|
+
return err2;
|
|
50348
|
+
}
|
|
50349
|
+
}
|
|
50350
|
+
module2.exports = Receiver;
|
|
50351
|
+
});
|
|
50352
|
+
|
|
50353
|
+
// ../../node_modules/ws/lib/sender.js
|
|
50354
|
+
var require_sender = __commonJS((exports, module2) => {
|
|
50355
|
+
var { Duplex } = __require("stream");
|
|
50356
|
+
var { randomFillSync } = __require("crypto");
|
|
50357
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
50358
|
+
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
|
|
50359
|
+
var { isBlob, isValidStatusCode } = require_validation();
|
|
50360
|
+
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
50361
|
+
var kByteLength = Symbol("kByteLength");
|
|
50362
|
+
var maskBuffer = Buffer.alloc(4);
|
|
50363
|
+
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
50364
|
+
var randomPool;
|
|
50365
|
+
var randomPoolPointer = RANDOM_POOL_SIZE;
|
|
50366
|
+
var DEFAULT = 0;
|
|
50367
|
+
var DEFLATING = 1;
|
|
50368
|
+
var GET_BLOB_DATA = 2;
|
|
50369
|
+
|
|
50370
|
+
class Sender {
|
|
50371
|
+
constructor(socket, extensions, generateMask) {
|
|
50372
|
+
this._extensions = extensions || {};
|
|
50373
|
+
if (generateMask) {
|
|
50374
|
+
this._generateMask = generateMask;
|
|
50375
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
50376
|
+
}
|
|
50377
|
+
this._socket = socket;
|
|
50378
|
+
this._firstFragment = true;
|
|
50379
|
+
this._compress = false;
|
|
50380
|
+
this._bufferedBytes = 0;
|
|
50381
|
+
this._queue = [];
|
|
50382
|
+
this._state = DEFAULT;
|
|
50383
|
+
this.onerror = NOOP;
|
|
50384
|
+
this[kWebSocket] = undefined;
|
|
50385
|
+
}
|
|
50386
|
+
static frame(data, options) {
|
|
50387
|
+
let mask;
|
|
50388
|
+
let merge = false;
|
|
50389
|
+
let offset = 2;
|
|
50390
|
+
let skipMasking = false;
|
|
50391
|
+
if (options.mask) {
|
|
50392
|
+
mask = options.maskBuffer || maskBuffer;
|
|
50393
|
+
if (options.generateMask) {
|
|
50394
|
+
options.generateMask(mask);
|
|
50395
|
+
} else {
|
|
50396
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
50397
|
+
if (randomPool === undefined) {
|
|
50398
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
50399
|
+
}
|
|
50400
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
50401
|
+
randomPoolPointer = 0;
|
|
50402
|
+
}
|
|
50403
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
50404
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
50405
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
50406
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
50407
|
+
}
|
|
50408
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
50409
|
+
offset = 6;
|
|
50410
|
+
}
|
|
50411
|
+
let dataLength;
|
|
50412
|
+
if (typeof data === "string") {
|
|
50413
|
+
if ((!options.mask || skipMasking) && options[kByteLength] !== undefined) {
|
|
50414
|
+
dataLength = options[kByteLength];
|
|
50415
|
+
} else {
|
|
50416
|
+
data = Buffer.from(data);
|
|
50417
|
+
dataLength = data.length;
|
|
50418
|
+
}
|
|
50419
|
+
} else {
|
|
50420
|
+
dataLength = data.length;
|
|
50421
|
+
merge = options.mask && options.readOnly && !skipMasking;
|
|
50422
|
+
}
|
|
50423
|
+
let payloadLength = dataLength;
|
|
50424
|
+
if (dataLength >= 65536) {
|
|
50425
|
+
offset += 8;
|
|
50426
|
+
payloadLength = 127;
|
|
50427
|
+
} else if (dataLength > 125) {
|
|
50428
|
+
offset += 2;
|
|
50429
|
+
payloadLength = 126;
|
|
50430
|
+
}
|
|
50431
|
+
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
|
50432
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
50433
|
+
if (options.rsv1)
|
|
50434
|
+
target[0] |= 64;
|
|
50435
|
+
target[1] = payloadLength;
|
|
50436
|
+
if (payloadLength === 126) {
|
|
50437
|
+
target.writeUInt16BE(dataLength, 2);
|
|
50438
|
+
} else if (payloadLength === 127) {
|
|
50439
|
+
target[2] = target[3] = 0;
|
|
50440
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
50441
|
+
}
|
|
50442
|
+
if (!options.mask)
|
|
50443
|
+
return [target, data];
|
|
50444
|
+
target[1] |= 128;
|
|
50445
|
+
target[offset - 4] = mask[0];
|
|
50446
|
+
target[offset - 3] = mask[1];
|
|
50447
|
+
target[offset - 2] = mask[2];
|
|
50448
|
+
target[offset - 1] = mask[3];
|
|
50449
|
+
if (skipMasking)
|
|
50450
|
+
return [target, data];
|
|
50451
|
+
if (merge) {
|
|
50452
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
50453
|
+
return [target];
|
|
50454
|
+
}
|
|
50455
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
50456
|
+
return [target, data];
|
|
50457
|
+
}
|
|
50458
|
+
close(code, data, mask, cb) {
|
|
50459
|
+
let buf;
|
|
50460
|
+
if (code === undefined) {
|
|
50461
|
+
buf = EMPTY_BUFFER;
|
|
50462
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
50463
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
50464
|
+
} else if (data === undefined || !data.length) {
|
|
50465
|
+
buf = Buffer.allocUnsafe(2);
|
|
50466
|
+
buf.writeUInt16BE(code, 0);
|
|
50467
|
+
} else {
|
|
50468
|
+
const length = Buffer.byteLength(data);
|
|
50469
|
+
if (length > 123) {
|
|
50470
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
50471
|
+
}
|
|
50472
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
50473
|
+
buf.writeUInt16BE(code, 0);
|
|
50474
|
+
if (typeof data === "string") {
|
|
50475
|
+
buf.write(data, 2);
|
|
50476
|
+
} else {
|
|
50477
|
+
buf.set(data, 2);
|
|
50478
|
+
}
|
|
50479
|
+
}
|
|
50480
|
+
const options = {
|
|
50481
|
+
[kByteLength]: buf.length,
|
|
50482
|
+
fin: true,
|
|
50483
|
+
generateMask: this._generateMask,
|
|
50484
|
+
mask,
|
|
50485
|
+
maskBuffer: this._maskBuffer,
|
|
50486
|
+
opcode: 8,
|
|
50487
|
+
readOnly: false,
|
|
50488
|
+
rsv1: false
|
|
50489
|
+
};
|
|
50490
|
+
if (this._state !== DEFAULT) {
|
|
50491
|
+
this.enqueue([this.dispatch, buf, false, options, cb]);
|
|
50492
|
+
} else {
|
|
50493
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
50494
|
+
}
|
|
50495
|
+
}
|
|
50496
|
+
ping(data, mask, cb) {
|
|
50497
|
+
let byteLength;
|
|
50498
|
+
let readOnly;
|
|
50499
|
+
if (typeof data === "string") {
|
|
50500
|
+
byteLength = Buffer.byteLength(data);
|
|
50501
|
+
readOnly = false;
|
|
50502
|
+
} else if (isBlob(data)) {
|
|
50503
|
+
byteLength = data.size;
|
|
50504
|
+
readOnly = false;
|
|
50505
|
+
} else {
|
|
50506
|
+
data = toBuffer(data);
|
|
50507
|
+
byteLength = data.length;
|
|
50508
|
+
readOnly = toBuffer.readOnly;
|
|
50509
|
+
}
|
|
50510
|
+
if (byteLength > 125) {
|
|
50511
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
50512
|
+
}
|
|
50513
|
+
const options = {
|
|
50514
|
+
[kByteLength]: byteLength,
|
|
50515
|
+
fin: true,
|
|
50516
|
+
generateMask: this._generateMask,
|
|
50517
|
+
mask,
|
|
50518
|
+
maskBuffer: this._maskBuffer,
|
|
50519
|
+
opcode: 9,
|
|
50520
|
+
readOnly,
|
|
50521
|
+
rsv1: false
|
|
50522
|
+
};
|
|
50523
|
+
if (isBlob(data)) {
|
|
50524
|
+
if (this._state !== DEFAULT) {
|
|
50525
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
50526
|
+
} else {
|
|
50527
|
+
this.getBlobData(data, false, options, cb);
|
|
50528
|
+
}
|
|
50529
|
+
} else if (this._state !== DEFAULT) {
|
|
50530
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
50531
|
+
} else {
|
|
50532
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
50533
|
+
}
|
|
50534
|
+
}
|
|
50535
|
+
pong(data, mask, cb) {
|
|
50536
|
+
let byteLength;
|
|
50537
|
+
let readOnly;
|
|
50538
|
+
if (typeof data === "string") {
|
|
50539
|
+
byteLength = Buffer.byteLength(data);
|
|
50540
|
+
readOnly = false;
|
|
50541
|
+
} else if (isBlob(data)) {
|
|
50542
|
+
byteLength = data.size;
|
|
50543
|
+
readOnly = false;
|
|
50544
|
+
} else {
|
|
50545
|
+
data = toBuffer(data);
|
|
50546
|
+
byteLength = data.length;
|
|
50547
|
+
readOnly = toBuffer.readOnly;
|
|
50548
|
+
}
|
|
50549
|
+
if (byteLength > 125) {
|
|
50550
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
50551
|
+
}
|
|
50552
|
+
const options = {
|
|
50553
|
+
[kByteLength]: byteLength,
|
|
50554
|
+
fin: true,
|
|
50555
|
+
generateMask: this._generateMask,
|
|
50556
|
+
mask,
|
|
50557
|
+
maskBuffer: this._maskBuffer,
|
|
50558
|
+
opcode: 10,
|
|
50559
|
+
readOnly,
|
|
50560
|
+
rsv1: false
|
|
50561
|
+
};
|
|
50562
|
+
if (isBlob(data)) {
|
|
50563
|
+
if (this._state !== DEFAULT) {
|
|
50564
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
50565
|
+
} else {
|
|
50566
|
+
this.getBlobData(data, false, options, cb);
|
|
50567
|
+
}
|
|
50568
|
+
} else if (this._state !== DEFAULT) {
|
|
50569
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
50570
|
+
} else {
|
|
50571
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
50572
|
+
}
|
|
50573
|
+
}
|
|
50574
|
+
send(data, options, cb) {
|
|
50575
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
50576
|
+
let opcode = options.binary ? 2 : 1;
|
|
50577
|
+
let rsv1 = options.compress;
|
|
50578
|
+
let byteLength;
|
|
50579
|
+
let readOnly;
|
|
50580
|
+
if (typeof data === "string") {
|
|
50581
|
+
byteLength = Buffer.byteLength(data);
|
|
50582
|
+
readOnly = false;
|
|
50583
|
+
} else if (isBlob(data)) {
|
|
50584
|
+
byteLength = data.size;
|
|
50585
|
+
readOnly = false;
|
|
50586
|
+
} else {
|
|
50587
|
+
data = toBuffer(data);
|
|
50588
|
+
byteLength = data.length;
|
|
50589
|
+
readOnly = toBuffer.readOnly;
|
|
50590
|
+
}
|
|
50591
|
+
if (this._firstFragment) {
|
|
50592
|
+
this._firstFragment = false;
|
|
50593
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
50594
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
50595
|
+
}
|
|
50596
|
+
this._compress = rsv1;
|
|
50597
|
+
} else {
|
|
50598
|
+
rsv1 = false;
|
|
50599
|
+
opcode = 0;
|
|
50600
|
+
}
|
|
50601
|
+
if (options.fin)
|
|
50602
|
+
this._firstFragment = true;
|
|
50603
|
+
const opts = {
|
|
50604
|
+
[kByteLength]: byteLength,
|
|
50605
|
+
fin: options.fin,
|
|
50606
|
+
generateMask: this._generateMask,
|
|
50607
|
+
mask: options.mask,
|
|
50608
|
+
maskBuffer: this._maskBuffer,
|
|
50609
|
+
opcode,
|
|
50610
|
+
readOnly,
|
|
50611
|
+
rsv1
|
|
50612
|
+
};
|
|
50613
|
+
if (isBlob(data)) {
|
|
50614
|
+
if (this._state !== DEFAULT) {
|
|
50615
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
50616
|
+
} else {
|
|
50617
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
50618
|
+
}
|
|
50619
|
+
} else if (this._state !== DEFAULT) {
|
|
50620
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
50621
|
+
} else {
|
|
50622
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
50623
|
+
}
|
|
50624
|
+
}
|
|
50625
|
+
getBlobData(blob2, compress, options, cb) {
|
|
50626
|
+
this._bufferedBytes += options[kByteLength];
|
|
50627
|
+
this._state = GET_BLOB_DATA;
|
|
50628
|
+
blob2.arrayBuffer().then((arrayBuffer) => {
|
|
50629
|
+
if (this._socket.destroyed) {
|
|
50630
|
+
const err2 = new Error("The socket was closed while the blob was being read");
|
|
50631
|
+
process.nextTick(callCallbacks, this, err2, cb);
|
|
50632
|
+
return;
|
|
50633
|
+
}
|
|
50634
|
+
this._bufferedBytes -= options[kByteLength];
|
|
50635
|
+
const data = toBuffer(arrayBuffer);
|
|
50636
|
+
if (!compress) {
|
|
50637
|
+
this._state = DEFAULT;
|
|
50638
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
50639
|
+
this.dequeue();
|
|
50640
|
+
} else {
|
|
50641
|
+
this.dispatch(data, compress, options, cb);
|
|
50642
|
+
}
|
|
50643
|
+
}).catch((err2) => {
|
|
50644
|
+
process.nextTick(onError, this, err2, cb);
|
|
50645
|
+
});
|
|
50646
|
+
}
|
|
50647
|
+
dispatch(data, compress, options, cb) {
|
|
50648
|
+
if (!compress) {
|
|
50649
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
50650
|
+
return;
|
|
50651
|
+
}
|
|
50652
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
50653
|
+
this._bufferedBytes += options[kByteLength];
|
|
50654
|
+
this._state = DEFLATING;
|
|
50655
|
+
perMessageDeflate.compress(data, options.fin, (_4, buf) => {
|
|
50656
|
+
if (this._socket.destroyed) {
|
|
50657
|
+
const err2 = new Error("The socket was closed while data was being compressed");
|
|
50658
|
+
callCallbacks(this, err2, cb);
|
|
50659
|
+
return;
|
|
50660
|
+
}
|
|
50661
|
+
this._bufferedBytes -= options[kByteLength];
|
|
50662
|
+
this._state = DEFAULT;
|
|
50663
|
+
options.readOnly = false;
|
|
50664
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
50665
|
+
this.dequeue();
|
|
50666
|
+
});
|
|
50667
|
+
}
|
|
50668
|
+
dequeue() {
|
|
50669
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
50670
|
+
const params = this._queue.shift();
|
|
50671
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
50672
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
50673
|
+
}
|
|
50674
|
+
}
|
|
50675
|
+
enqueue(params) {
|
|
50676
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
50677
|
+
this._queue.push(params);
|
|
50678
|
+
}
|
|
50679
|
+
sendFrame(list, cb) {
|
|
50680
|
+
if (list.length === 2) {
|
|
50681
|
+
this._socket.cork();
|
|
50682
|
+
this._socket.write(list[0]);
|
|
50683
|
+
this._socket.write(list[1], cb);
|
|
50684
|
+
this._socket.uncork();
|
|
50685
|
+
} else {
|
|
50686
|
+
this._socket.write(list[0], cb);
|
|
50687
|
+
}
|
|
50688
|
+
}
|
|
50689
|
+
}
|
|
50690
|
+
module2.exports = Sender;
|
|
50691
|
+
function callCallbacks(sender, err2, cb) {
|
|
50692
|
+
if (typeof cb === "function")
|
|
50693
|
+
cb(err2);
|
|
50694
|
+
for (let i3 = 0;i3 < sender._queue.length; i3++) {
|
|
50695
|
+
const params = sender._queue[i3];
|
|
50696
|
+
const callback = params[params.length - 1];
|
|
50697
|
+
if (typeof callback === "function")
|
|
50698
|
+
callback(err2);
|
|
50699
|
+
}
|
|
50700
|
+
}
|
|
50701
|
+
function onError(sender, err2, cb) {
|
|
50702
|
+
callCallbacks(sender, err2, cb);
|
|
50703
|
+
sender.onerror(err2);
|
|
50704
|
+
}
|
|
50705
|
+
});
|
|
50706
|
+
|
|
50707
|
+
// ../../node_modules/ws/lib/event-target.js
|
|
50708
|
+
var require_event_target = __commonJS((exports, module2) => {
|
|
50709
|
+
var { kForOnEventAttribute, kListener } = require_constants();
|
|
50710
|
+
var kCode = Symbol("kCode");
|
|
50711
|
+
var kData = Symbol("kData");
|
|
50712
|
+
var kError = Symbol("kError");
|
|
50713
|
+
var kMessage = Symbol("kMessage");
|
|
50714
|
+
var kReason = Symbol("kReason");
|
|
50715
|
+
var kTarget = Symbol("kTarget");
|
|
50716
|
+
var kType = Symbol("kType");
|
|
50717
|
+
var kWasClean = Symbol("kWasClean");
|
|
50718
|
+
|
|
50719
|
+
class Event {
|
|
50720
|
+
constructor(type) {
|
|
50721
|
+
this[kTarget] = null;
|
|
50722
|
+
this[kType] = type;
|
|
50723
|
+
}
|
|
50724
|
+
get target() {
|
|
50725
|
+
return this[kTarget];
|
|
50726
|
+
}
|
|
50727
|
+
get type() {
|
|
50728
|
+
return this[kType];
|
|
50729
|
+
}
|
|
50730
|
+
}
|
|
50731
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
50732
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
50733
|
+
|
|
50734
|
+
class CloseEvent extends Event {
|
|
50735
|
+
constructor(type, options = {}) {
|
|
50736
|
+
super(type);
|
|
50737
|
+
this[kCode] = options.code === undefined ? 0 : options.code;
|
|
50738
|
+
this[kReason] = options.reason === undefined ? "" : options.reason;
|
|
50739
|
+
this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
|
|
50740
|
+
}
|
|
50741
|
+
get code() {
|
|
50742
|
+
return this[kCode];
|
|
50743
|
+
}
|
|
50744
|
+
get reason() {
|
|
50745
|
+
return this[kReason];
|
|
50746
|
+
}
|
|
50747
|
+
get wasClean() {
|
|
50748
|
+
return this[kWasClean];
|
|
50749
|
+
}
|
|
50750
|
+
}
|
|
50751
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
50752
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
50753
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
50754
|
+
|
|
50755
|
+
class ErrorEvent extends Event {
|
|
50756
|
+
constructor(type, options = {}) {
|
|
50757
|
+
super(type);
|
|
50758
|
+
this[kError] = options.error === undefined ? null : options.error;
|
|
50759
|
+
this[kMessage] = options.message === undefined ? "" : options.message;
|
|
50760
|
+
}
|
|
50761
|
+
get error() {
|
|
50762
|
+
return this[kError];
|
|
50763
|
+
}
|
|
50764
|
+
get message() {
|
|
50765
|
+
return this[kMessage];
|
|
50766
|
+
}
|
|
50767
|
+
}
|
|
50768
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
50769
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
50770
|
+
|
|
50771
|
+
class MessageEvent extends Event {
|
|
50772
|
+
constructor(type, options = {}) {
|
|
50773
|
+
super(type);
|
|
50774
|
+
this[kData] = options.data === undefined ? null : options.data;
|
|
50775
|
+
}
|
|
50776
|
+
get data() {
|
|
50777
|
+
return this[kData];
|
|
50778
|
+
}
|
|
50779
|
+
}
|
|
50780
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
50781
|
+
var EventTarget = {
|
|
50782
|
+
addEventListener(type, handler, options = {}) {
|
|
50783
|
+
for (const listener of this.listeners(type)) {
|
|
50784
|
+
if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
50785
|
+
return;
|
|
50786
|
+
}
|
|
50787
|
+
}
|
|
50788
|
+
let wrapper;
|
|
50789
|
+
if (type === "message") {
|
|
50790
|
+
wrapper = function onMessage(data, isBinary) {
|
|
50791
|
+
const event = new MessageEvent("message", {
|
|
50792
|
+
data: isBinary ? data : data.toString()
|
|
50793
|
+
});
|
|
50794
|
+
event[kTarget] = this;
|
|
50795
|
+
callListener(handler, this, event);
|
|
50796
|
+
};
|
|
50797
|
+
} else if (type === "close") {
|
|
50798
|
+
wrapper = function onClose(code, message3) {
|
|
50799
|
+
const event = new CloseEvent("close", {
|
|
50800
|
+
code,
|
|
50801
|
+
reason: message3.toString(),
|
|
50802
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
50803
|
+
});
|
|
50804
|
+
event[kTarget] = this;
|
|
50805
|
+
callListener(handler, this, event);
|
|
50806
|
+
};
|
|
50807
|
+
} else if (type === "error") {
|
|
50808
|
+
wrapper = function onError(error2) {
|
|
50809
|
+
const event = new ErrorEvent("error", {
|
|
50810
|
+
error: error2,
|
|
50811
|
+
message: error2.message
|
|
50812
|
+
});
|
|
50813
|
+
event[kTarget] = this;
|
|
50814
|
+
callListener(handler, this, event);
|
|
50815
|
+
};
|
|
50816
|
+
} else if (type === "open") {
|
|
50817
|
+
wrapper = function onOpen() {
|
|
50818
|
+
const event = new Event("open");
|
|
50819
|
+
event[kTarget] = this;
|
|
50820
|
+
callListener(handler, this, event);
|
|
50821
|
+
};
|
|
50822
|
+
} else {
|
|
50823
|
+
return;
|
|
50824
|
+
}
|
|
50825
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
50826
|
+
wrapper[kListener] = handler;
|
|
50827
|
+
if (options.once) {
|
|
50828
|
+
this.once(type, wrapper);
|
|
50829
|
+
} else {
|
|
50830
|
+
this.on(type, wrapper);
|
|
50831
|
+
}
|
|
50832
|
+
},
|
|
50833
|
+
removeEventListener(type, handler) {
|
|
50834
|
+
for (const listener of this.listeners(type)) {
|
|
50835
|
+
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
50836
|
+
this.removeListener(type, listener);
|
|
50837
|
+
break;
|
|
50838
|
+
}
|
|
50839
|
+
}
|
|
50840
|
+
}
|
|
50841
|
+
};
|
|
50842
|
+
module2.exports = {
|
|
50843
|
+
CloseEvent,
|
|
50844
|
+
ErrorEvent,
|
|
50845
|
+
Event,
|
|
50846
|
+
EventTarget,
|
|
50847
|
+
MessageEvent
|
|
50848
|
+
};
|
|
50849
|
+
function callListener(listener, thisArg, event) {
|
|
50850
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
50851
|
+
listener.handleEvent.call(listener, event);
|
|
50852
|
+
} else {
|
|
50853
|
+
listener.call(thisArg, event);
|
|
50854
|
+
}
|
|
50855
|
+
}
|
|
50856
|
+
});
|
|
50857
|
+
|
|
50858
|
+
// ../../node_modules/ws/lib/extension.js
|
|
50859
|
+
var require_extension = __commonJS((exports, module2) => {
|
|
50860
|
+
var { tokenChars } = require_validation();
|
|
50861
|
+
function push(dest, name3, elem) {
|
|
50862
|
+
if (dest[name3] === undefined)
|
|
50863
|
+
dest[name3] = [elem];
|
|
50864
|
+
else
|
|
50865
|
+
dest[name3].push(elem);
|
|
50866
|
+
}
|
|
50867
|
+
function parse2(header) {
|
|
50868
|
+
const offers = Object.create(null);
|
|
50869
|
+
let params = Object.create(null);
|
|
50870
|
+
let mustUnescape = false;
|
|
50871
|
+
let isEscaping = false;
|
|
50872
|
+
let inQuotes = false;
|
|
50873
|
+
let extensionName;
|
|
50874
|
+
let paramName;
|
|
50875
|
+
let start2 = -1;
|
|
50876
|
+
let code = -1;
|
|
50877
|
+
let end = -1;
|
|
50878
|
+
let i3 = 0;
|
|
50879
|
+
for (;i3 < header.length; i3++) {
|
|
50880
|
+
code = header.charCodeAt(i3);
|
|
50881
|
+
if (extensionName === undefined) {
|
|
50882
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
50883
|
+
if (start2 === -1)
|
|
50884
|
+
start2 = i3;
|
|
50885
|
+
} else if (i3 !== 0 && (code === 32 || code === 9)) {
|
|
50886
|
+
if (end === -1 && start2 !== -1)
|
|
50887
|
+
end = i3;
|
|
50888
|
+
} else if (code === 59 || code === 44) {
|
|
50889
|
+
if (start2 === -1) {
|
|
50890
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
50891
|
+
}
|
|
50892
|
+
if (end === -1)
|
|
50893
|
+
end = i3;
|
|
50894
|
+
const name3 = header.slice(start2, end);
|
|
50895
|
+
if (code === 44) {
|
|
50896
|
+
push(offers, name3, params);
|
|
50897
|
+
params = Object.create(null);
|
|
50898
|
+
} else {
|
|
50899
|
+
extensionName = name3;
|
|
50900
|
+
}
|
|
50901
|
+
start2 = end = -1;
|
|
50902
|
+
} else {
|
|
50903
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
50904
|
+
}
|
|
50905
|
+
} else if (paramName === undefined) {
|
|
50906
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
50907
|
+
if (start2 === -1)
|
|
50908
|
+
start2 = i3;
|
|
50909
|
+
} else if (code === 32 || code === 9) {
|
|
50910
|
+
if (end === -1 && start2 !== -1)
|
|
50911
|
+
end = i3;
|
|
50912
|
+
} else if (code === 59 || code === 44) {
|
|
50913
|
+
if (start2 === -1) {
|
|
50914
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
50915
|
+
}
|
|
50916
|
+
if (end === -1)
|
|
50917
|
+
end = i3;
|
|
50918
|
+
push(params, header.slice(start2, end), true);
|
|
50919
|
+
if (code === 44) {
|
|
50920
|
+
push(offers, extensionName, params);
|
|
50921
|
+
params = Object.create(null);
|
|
50922
|
+
extensionName = undefined;
|
|
50923
|
+
}
|
|
50924
|
+
start2 = end = -1;
|
|
50925
|
+
} else if (code === 61 && start2 !== -1 && end === -1) {
|
|
50926
|
+
paramName = header.slice(start2, i3);
|
|
50927
|
+
start2 = end = -1;
|
|
50928
|
+
} else {
|
|
50929
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
50930
|
+
}
|
|
50931
|
+
} else {
|
|
50932
|
+
if (isEscaping) {
|
|
50933
|
+
if (tokenChars[code] !== 1) {
|
|
50934
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
50935
|
+
}
|
|
50936
|
+
if (start2 === -1)
|
|
50937
|
+
start2 = i3;
|
|
50938
|
+
else if (!mustUnescape)
|
|
50939
|
+
mustUnescape = true;
|
|
50940
|
+
isEscaping = false;
|
|
50941
|
+
} else if (inQuotes) {
|
|
50942
|
+
if (tokenChars[code] === 1) {
|
|
50943
|
+
if (start2 === -1)
|
|
50944
|
+
start2 = i3;
|
|
50945
|
+
} else if (code === 34 && start2 !== -1) {
|
|
50946
|
+
inQuotes = false;
|
|
50947
|
+
end = i3;
|
|
50948
|
+
} else if (code === 92) {
|
|
50949
|
+
isEscaping = true;
|
|
50950
|
+
} else {
|
|
50951
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
50952
|
+
}
|
|
50953
|
+
} else if (code === 34 && header.charCodeAt(i3 - 1) === 61) {
|
|
50954
|
+
inQuotes = true;
|
|
50955
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
50956
|
+
if (start2 === -1)
|
|
50957
|
+
start2 = i3;
|
|
50958
|
+
} else if (start2 !== -1 && (code === 32 || code === 9)) {
|
|
50959
|
+
if (end === -1)
|
|
50960
|
+
end = i3;
|
|
50961
|
+
} else if (code === 59 || code === 44) {
|
|
50962
|
+
if (start2 === -1) {
|
|
50963
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
50964
|
+
}
|
|
50965
|
+
if (end === -1)
|
|
50966
|
+
end = i3;
|
|
50967
|
+
let value = header.slice(start2, end);
|
|
50968
|
+
if (mustUnescape) {
|
|
50969
|
+
value = value.replace(/\\/g, "");
|
|
50970
|
+
mustUnescape = false;
|
|
50971
|
+
}
|
|
50972
|
+
push(params, paramName, value);
|
|
50973
|
+
if (code === 44) {
|
|
50974
|
+
push(offers, extensionName, params);
|
|
50975
|
+
params = Object.create(null);
|
|
50976
|
+
extensionName = undefined;
|
|
50977
|
+
}
|
|
50978
|
+
paramName = undefined;
|
|
50979
|
+
start2 = end = -1;
|
|
50980
|
+
} else {
|
|
50981
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
50982
|
+
}
|
|
50983
|
+
}
|
|
50984
|
+
}
|
|
50985
|
+
if (start2 === -1 || inQuotes || code === 32 || code === 9) {
|
|
50986
|
+
throw new SyntaxError("Unexpected end of input");
|
|
50987
|
+
}
|
|
50988
|
+
if (end === -1)
|
|
50989
|
+
end = i3;
|
|
50990
|
+
const token = header.slice(start2, end);
|
|
50991
|
+
if (extensionName === undefined) {
|
|
50992
|
+
push(offers, token, params);
|
|
50993
|
+
} else {
|
|
50994
|
+
if (paramName === undefined) {
|
|
50995
|
+
push(params, token, true);
|
|
50996
|
+
} else if (mustUnescape) {
|
|
50997
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
50998
|
+
} else {
|
|
50999
|
+
push(params, paramName, token);
|
|
51000
|
+
}
|
|
51001
|
+
push(offers, extensionName, params);
|
|
51002
|
+
}
|
|
51003
|
+
return offers;
|
|
51004
|
+
}
|
|
51005
|
+
function format2(extensions) {
|
|
51006
|
+
return Object.keys(extensions).map((extension) => {
|
|
51007
|
+
let configurations = extensions[extension];
|
|
51008
|
+
if (!Array.isArray(configurations))
|
|
51009
|
+
configurations = [configurations];
|
|
51010
|
+
return configurations.map((params) => {
|
|
51011
|
+
return [extension].concat(Object.keys(params).map((k3) => {
|
|
51012
|
+
let values = params[k3];
|
|
51013
|
+
if (!Array.isArray(values))
|
|
51014
|
+
values = [values];
|
|
51015
|
+
return values.map((v3) => v3 === true ? k3 : `${k3}=${v3}`).join("; ");
|
|
51016
|
+
})).join("; ");
|
|
51017
|
+
}).join(", ");
|
|
51018
|
+
}).join(", ");
|
|
51019
|
+
}
|
|
51020
|
+
module2.exports = { format: format2, parse: parse2 };
|
|
51021
|
+
});
|
|
51022
|
+
|
|
51023
|
+
// ../../node_modules/ws/lib/websocket.js
|
|
51024
|
+
var require_websocket = __commonJS((exports, module2) => {
|
|
51025
|
+
var EventEmitter = __require("events");
|
|
51026
|
+
var https = __require("https");
|
|
51027
|
+
var http = __require("http");
|
|
51028
|
+
var net = __require("net");
|
|
51029
|
+
var tls = __require("tls");
|
|
51030
|
+
var { randomBytes, createHash } = __require("crypto");
|
|
51031
|
+
var { Duplex, Readable: Readable2 } = __require("stream");
|
|
51032
|
+
var { URL: URL2 } = __require("url");
|
|
51033
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
51034
|
+
var Receiver = require_receiver();
|
|
51035
|
+
var Sender = require_sender();
|
|
51036
|
+
var { isBlob } = require_validation();
|
|
51037
|
+
var {
|
|
51038
|
+
BINARY_TYPES,
|
|
51039
|
+
EMPTY_BUFFER,
|
|
51040
|
+
GUID,
|
|
51041
|
+
kForOnEventAttribute,
|
|
51042
|
+
kListener,
|
|
51043
|
+
kStatusCode,
|
|
51044
|
+
kWebSocket,
|
|
51045
|
+
NOOP
|
|
51046
|
+
} = require_constants();
|
|
51047
|
+
var {
|
|
51048
|
+
EventTarget: { addEventListener: addEventListener2, removeEventListener }
|
|
51049
|
+
} = require_event_target();
|
|
51050
|
+
var { format: format2, parse: parse2 } = require_extension();
|
|
51051
|
+
var { toBuffer } = require_buffer_util();
|
|
51052
|
+
var closeTimeout = 30 * 1000;
|
|
51053
|
+
var kAborted = Symbol("kAborted");
|
|
51054
|
+
var protocolVersions = [8, 13];
|
|
51055
|
+
var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
51056
|
+
var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
51057
|
+
|
|
51058
|
+
class WebSocket2 extends EventEmitter {
|
|
51059
|
+
constructor(address, protocols, options) {
|
|
51060
|
+
super();
|
|
51061
|
+
this._binaryType = BINARY_TYPES[0];
|
|
51062
|
+
this._closeCode = 1006;
|
|
51063
|
+
this._closeFrameReceived = false;
|
|
51064
|
+
this._closeFrameSent = false;
|
|
51065
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
51066
|
+
this._closeTimer = null;
|
|
51067
|
+
this._errorEmitted = false;
|
|
51068
|
+
this._extensions = {};
|
|
51069
|
+
this._paused = false;
|
|
51070
|
+
this._protocol = "";
|
|
51071
|
+
this._readyState = WebSocket2.CONNECTING;
|
|
51072
|
+
this._receiver = null;
|
|
51073
|
+
this._sender = null;
|
|
51074
|
+
this._socket = null;
|
|
51075
|
+
if (address !== null) {
|
|
51076
|
+
this._bufferedAmount = 0;
|
|
51077
|
+
this._isServer = false;
|
|
51078
|
+
this._redirects = 0;
|
|
51079
|
+
if (protocols === undefined) {
|
|
51080
|
+
protocols = [];
|
|
51081
|
+
} else if (!Array.isArray(protocols)) {
|
|
51082
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
51083
|
+
options = protocols;
|
|
51084
|
+
protocols = [];
|
|
51085
|
+
} else {
|
|
51086
|
+
protocols = [protocols];
|
|
51087
|
+
}
|
|
51088
|
+
}
|
|
51089
|
+
initAsClient(this, address, protocols, options);
|
|
51090
|
+
} else {
|
|
51091
|
+
this._autoPong = options.autoPong;
|
|
51092
|
+
this._isServer = true;
|
|
51093
|
+
}
|
|
51094
|
+
}
|
|
51095
|
+
get binaryType() {
|
|
51096
|
+
return this._binaryType;
|
|
51097
|
+
}
|
|
51098
|
+
set binaryType(type) {
|
|
51099
|
+
if (!BINARY_TYPES.includes(type))
|
|
51100
|
+
return;
|
|
51101
|
+
this._binaryType = type;
|
|
51102
|
+
if (this._receiver)
|
|
51103
|
+
this._receiver._binaryType = type;
|
|
51104
|
+
}
|
|
51105
|
+
get bufferedAmount() {
|
|
51106
|
+
if (!this._socket)
|
|
51107
|
+
return this._bufferedAmount;
|
|
51108
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
51109
|
+
}
|
|
51110
|
+
get extensions() {
|
|
51111
|
+
return Object.keys(this._extensions).join();
|
|
51112
|
+
}
|
|
51113
|
+
get isPaused() {
|
|
51114
|
+
return this._paused;
|
|
51115
|
+
}
|
|
51116
|
+
get onclose() {
|
|
51117
|
+
return null;
|
|
51118
|
+
}
|
|
51119
|
+
get onerror() {
|
|
51120
|
+
return null;
|
|
51121
|
+
}
|
|
51122
|
+
get onopen() {
|
|
51123
|
+
return null;
|
|
51124
|
+
}
|
|
51125
|
+
get onmessage() {
|
|
51126
|
+
return null;
|
|
51127
|
+
}
|
|
51128
|
+
get protocol() {
|
|
51129
|
+
return this._protocol;
|
|
51130
|
+
}
|
|
51131
|
+
get readyState() {
|
|
51132
|
+
return this._readyState;
|
|
51133
|
+
}
|
|
51134
|
+
get url() {
|
|
51135
|
+
return this._url;
|
|
51136
|
+
}
|
|
51137
|
+
setSocket(socket, head, options) {
|
|
51138
|
+
const receiver = new Receiver({
|
|
51139
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
51140
|
+
binaryType: this.binaryType,
|
|
51141
|
+
extensions: this._extensions,
|
|
51142
|
+
isServer: this._isServer,
|
|
51143
|
+
maxPayload: options.maxPayload,
|
|
51144
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
51145
|
+
});
|
|
51146
|
+
const sender = new Sender(socket, this._extensions, options.generateMask);
|
|
51147
|
+
this._receiver = receiver;
|
|
51148
|
+
this._sender = sender;
|
|
51149
|
+
this._socket = socket;
|
|
51150
|
+
receiver[kWebSocket] = this;
|
|
51151
|
+
sender[kWebSocket] = this;
|
|
51152
|
+
socket[kWebSocket] = this;
|
|
51153
|
+
receiver.on("conclude", receiverOnConclude);
|
|
51154
|
+
receiver.on("drain", receiverOnDrain);
|
|
51155
|
+
receiver.on("error", receiverOnError);
|
|
51156
|
+
receiver.on("message", receiverOnMessage);
|
|
51157
|
+
receiver.on("ping", receiverOnPing);
|
|
51158
|
+
receiver.on("pong", receiverOnPong);
|
|
51159
|
+
sender.onerror = senderOnError;
|
|
51160
|
+
if (socket.setTimeout)
|
|
51161
|
+
socket.setTimeout(0);
|
|
51162
|
+
if (socket.setNoDelay)
|
|
51163
|
+
socket.setNoDelay();
|
|
51164
|
+
if (head.length > 0)
|
|
51165
|
+
socket.unshift(head);
|
|
51166
|
+
socket.on("close", socketOnClose);
|
|
51167
|
+
socket.on("data", socketOnData);
|
|
51168
|
+
socket.on("end", socketOnEnd);
|
|
51169
|
+
socket.on("error", socketOnError);
|
|
51170
|
+
this._readyState = WebSocket2.OPEN;
|
|
51171
|
+
this.emit("open");
|
|
51172
|
+
}
|
|
51173
|
+
emitClose() {
|
|
51174
|
+
if (!this._socket) {
|
|
51175
|
+
this._readyState = WebSocket2.CLOSED;
|
|
51176
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
51177
|
+
return;
|
|
51178
|
+
}
|
|
51179
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
51180
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
51181
|
+
}
|
|
51182
|
+
this._receiver.removeAllListeners();
|
|
51183
|
+
this._readyState = WebSocket2.CLOSED;
|
|
51184
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
51185
|
+
}
|
|
51186
|
+
close(code, data) {
|
|
51187
|
+
if (this.readyState === WebSocket2.CLOSED)
|
|
51188
|
+
return;
|
|
51189
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
51190
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
51191
|
+
abortHandshake(this, this._req, msg);
|
|
51192
|
+
return;
|
|
51193
|
+
}
|
|
51194
|
+
if (this.readyState === WebSocket2.CLOSING) {
|
|
51195
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
51196
|
+
this._socket.end();
|
|
51197
|
+
}
|
|
51198
|
+
return;
|
|
51199
|
+
}
|
|
51200
|
+
this._readyState = WebSocket2.CLOSING;
|
|
51201
|
+
this._sender.close(code, data, !this._isServer, (err2) => {
|
|
51202
|
+
if (err2)
|
|
51203
|
+
return;
|
|
51204
|
+
this._closeFrameSent = true;
|
|
51205
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
51206
|
+
this._socket.end();
|
|
51207
|
+
}
|
|
51208
|
+
});
|
|
51209
|
+
setCloseTimer(this);
|
|
51210
|
+
}
|
|
51211
|
+
pause() {
|
|
51212
|
+
if (this.readyState === WebSocket2.CONNECTING || this.readyState === WebSocket2.CLOSED) {
|
|
51213
|
+
return;
|
|
51214
|
+
}
|
|
51215
|
+
this._paused = true;
|
|
51216
|
+
this._socket.pause();
|
|
51217
|
+
}
|
|
51218
|
+
ping(data, mask, cb) {
|
|
51219
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
51220
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
51221
|
+
}
|
|
51222
|
+
if (typeof data === "function") {
|
|
51223
|
+
cb = data;
|
|
51224
|
+
data = mask = undefined;
|
|
51225
|
+
} else if (typeof mask === "function") {
|
|
51226
|
+
cb = mask;
|
|
51227
|
+
mask = undefined;
|
|
51228
|
+
}
|
|
51229
|
+
if (typeof data === "number")
|
|
51230
|
+
data = data.toString();
|
|
51231
|
+
if (this.readyState !== WebSocket2.OPEN) {
|
|
51232
|
+
sendAfterClose(this, data, cb);
|
|
51233
|
+
return;
|
|
51234
|
+
}
|
|
51235
|
+
if (mask === undefined)
|
|
51236
|
+
mask = !this._isServer;
|
|
51237
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
51238
|
+
}
|
|
51239
|
+
pong(data, mask, cb) {
|
|
51240
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
51241
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
51242
|
+
}
|
|
51243
|
+
if (typeof data === "function") {
|
|
51244
|
+
cb = data;
|
|
51245
|
+
data = mask = undefined;
|
|
51246
|
+
} else if (typeof mask === "function") {
|
|
51247
|
+
cb = mask;
|
|
51248
|
+
mask = undefined;
|
|
51249
|
+
}
|
|
51250
|
+
if (typeof data === "number")
|
|
51251
|
+
data = data.toString();
|
|
51252
|
+
if (this.readyState !== WebSocket2.OPEN) {
|
|
51253
|
+
sendAfterClose(this, data, cb);
|
|
51254
|
+
return;
|
|
51255
|
+
}
|
|
51256
|
+
if (mask === undefined)
|
|
51257
|
+
mask = !this._isServer;
|
|
51258
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
51259
|
+
}
|
|
51260
|
+
resume() {
|
|
51261
|
+
if (this.readyState === WebSocket2.CONNECTING || this.readyState === WebSocket2.CLOSED) {
|
|
51262
|
+
return;
|
|
51263
|
+
}
|
|
51264
|
+
this._paused = false;
|
|
51265
|
+
if (!this._receiver._writableState.needDrain)
|
|
51266
|
+
this._socket.resume();
|
|
51267
|
+
}
|
|
51268
|
+
send(data, options, cb) {
|
|
51269
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
51270
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
51271
|
+
}
|
|
51272
|
+
if (typeof options === "function") {
|
|
51273
|
+
cb = options;
|
|
51274
|
+
options = {};
|
|
51275
|
+
}
|
|
51276
|
+
if (typeof data === "number")
|
|
51277
|
+
data = data.toString();
|
|
51278
|
+
if (this.readyState !== WebSocket2.OPEN) {
|
|
51279
|
+
sendAfterClose(this, data, cb);
|
|
51280
|
+
return;
|
|
51281
|
+
}
|
|
51282
|
+
const opts = {
|
|
51283
|
+
binary: typeof data !== "string",
|
|
51284
|
+
mask: !this._isServer,
|
|
51285
|
+
compress: true,
|
|
51286
|
+
fin: true,
|
|
51287
|
+
...options
|
|
51288
|
+
};
|
|
51289
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
51290
|
+
opts.compress = false;
|
|
51291
|
+
}
|
|
51292
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
51293
|
+
}
|
|
51294
|
+
terminate() {
|
|
51295
|
+
if (this.readyState === WebSocket2.CLOSED)
|
|
51296
|
+
return;
|
|
51297
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
51298
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
51299
|
+
abortHandshake(this, this._req, msg);
|
|
51300
|
+
return;
|
|
51301
|
+
}
|
|
51302
|
+
if (this._socket) {
|
|
51303
|
+
this._readyState = WebSocket2.CLOSING;
|
|
51304
|
+
this._socket.destroy();
|
|
51305
|
+
}
|
|
51306
|
+
}
|
|
51307
|
+
}
|
|
51308
|
+
Object.defineProperty(WebSocket2, "CONNECTING", {
|
|
51309
|
+
enumerable: true,
|
|
51310
|
+
value: readyStates.indexOf("CONNECTING")
|
|
51311
|
+
});
|
|
51312
|
+
Object.defineProperty(WebSocket2.prototype, "CONNECTING", {
|
|
51313
|
+
enumerable: true,
|
|
51314
|
+
value: readyStates.indexOf("CONNECTING")
|
|
51315
|
+
});
|
|
51316
|
+
Object.defineProperty(WebSocket2, "OPEN", {
|
|
51317
|
+
enumerable: true,
|
|
51318
|
+
value: readyStates.indexOf("OPEN")
|
|
51319
|
+
});
|
|
51320
|
+
Object.defineProperty(WebSocket2.prototype, "OPEN", {
|
|
51321
|
+
enumerable: true,
|
|
51322
|
+
value: readyStates.indexOf("OPEN")
|
|
51323
|
+
});
|
|
51324
|
+
Object.defineProperty(WebSocket2, "CLOSING", {
|
|
51325
|
+
enumerable: true,
|
|
51326
|
+
value: readyStates.indexOf("CLOSING")
|
|
51327
|
+
});
|
|
51328
|
+
Object.defineProperty(WebSocket2.prototype, "CLOSING", {
|
|
51329
|
+
enumerable: true,
|
|
51330
|
+
value: readyStates.indexOf("CLOSING")
|
|
51331
|
+
});
|
|
51332
|
+
Object.defineProperty(WebSocket2, "CLOSED", {
|
|
51333
|
+
enumerable: true,
|
|
51334
|
+
value: readyStates.indexOf("CLOSED")
|
|
51335
|
+
});
|
|
51336
|
+
Object.defineProperty(WebSocket2.prototype, "CLOSED", {
|
|
51337
|
+
enumerable: true,
|
|
51338
|
+
value: readyStates.indexOf("CLOSED")
|
|
51339
|
+
});
|
|
51340
|
+
[
|
|
51341
|
+
"binaryType",
|
|
51342
|
+
"bufferedAmount",
|
|
51343
|
+
"extensions",
|
|
51344
|
+
"isPaused",
|
|
51345
|
+
"protocol",
|
|
51346
|
+
"readyState",
|
|
51347
|
+
"url"
|
|
51348
|
+
].forEach((property) => {
|
|
51349
|
+
Object.defineProperty(WebSocket2.prototype, property, { enumerable: true });
|
|
51350
|
+
});
|
|
51351
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
51352
|
+
Object.defineProperty(WebSocket2.prototype, `on${method}`, {
|
|
51353
|
+
enumerable: true,
|
|
51354
|
+
get() {
|
|
51355
|
+
for (const listener of this.listeners(method)) {
|
|
51356
|
+
if (listener[kForOnEventAttribute])
|
|
51357
|
+
return listener[kListener];
|
|
51358
|
+
}
|
|
51359
|
+
return null;
|
|
51360
|
+
},
|
|
51361
|
+
set(handler) {
|
|
51362
|
+
for (const listener of this.listeners(method)) {
|
|
51363
|
+
if (listener[kForOnEventAttribute]) {
|
|
51364
|
+
this.removeListener(method, listener);
|
|
51365
|
+
break;
|
|
51366
|
+
}
|
|
51367
|
+
}
|
|
51368
|
+
if (typeof handler !== "function")
|
|
51369
|
+
return;
|
|
51370
|
+
this.addEventListener(method, handler, {
|
|
51371
|
+
[kForOnEventAttribute]: true
|
|
51372
|
+
});
|
|
51373
|
+
}
|
|
51374
|
+
});
|
|
51375
|
+
});
|
|
51376
|
+
WebSocket2.prototype.addEventListener = addEventListener2;
|
|
51377
|
+
WebSocket2.prototype.removeEventListener = removeEventListener;
|
|
51378
|
+
module2.exports = WebSocket2;
|
|
51379
|
+
function initAsClient(websocket, address, protocols, options) {
|
|
51380
|
+
const opts = {
|
|
51381
|
+
allowSynchronousEvents: true,
|
|
51382
|
+
autoPong: true,
|
|
51383
|
+
protocolVersion: protocolVersions[1],
|
|
51384
|
+
maxPayload: 100 * 1024 * 1024,
|
|
51385
|
+
skipUTF8Validation: false,
|
|
51386
|
+
perMessageDeflate: true,
|
|
51387
|
+
followRedirects: false,
|
|
51388
|
+
maxRedirects: 10,
|
|
51389
|
+
...options,
|
|
51390
|
+
socketPath: undefined,
|
|
51391
|
+
hostname: undefined,
|
|
51392
|
+
protocol: undefined,
|
|
51393
|
+
timeout: undefined,
|
|
51394
|
+
method: "GET",
|
|
51395
|
+
host: undefined,
|
|
51396
|
+
path: undefined,
|
|
51397
|
+
port: undefined
|
|
51398
|
+
};
|
|
51399
|
+
websocket._autoPong = opts.autoPong;
|
|
51400
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
51401
|
+
throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} ` + `(supported versions: ${protocolVersions.join(", ")})`);
|
|
51402
|
+
}
|
|
51403
|
+
let parsedUrl;
|
|
51404
|
+
if (address instanceof URL2) {
|
|
51405
|
+
parsedUrl = address;
|
|
51406
|
+
} else {
|
|
51407
|
+
try {
|
|
51408
|
+
parsedUrl = new URL2(address);
|
|
51409
|
+
} catch (e) {
|
|
51410
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
51411
|
+
}
|
|
51412
|
+
}
|
|
51413
|
+
if (parsedUrl.protocol === "http:") {
|
|
51414
|
+
parsedUrl.protocol = "ws:";
|
|
51415
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
51416
|
+
parsedUrl.protocol = "wss:";
|
|
51417
|
+
}
|
|
51418
|
+
websocket._url = parsedUrl.href;
|
|
51419
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
51420
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
51421
|
+
let invalidUrlMessage;
|
|
51422
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
51423
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", ` + '"http:", "https:", or "ws+unix:"';
|
|
51424
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
51425
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
51426
|
+
} else if (parsedUrl.hash) {
|
|
51427
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
51428
|
+
}
|
|
51429
|
+
if (invalidUrlMessage) {
|
|
51430
|
+
const err2 = new SyntaxError(invalidUrlMessage);
|
|
51431
|
+
if (websocket._redirects === 0) {
|
|
51432
|
+
throw err2;
|
|
51433
|
+
} else {
|
|
51434
|
+
emitErrorAndClose(websocket, err2);
|
|
51435
|
+
return;
|
|
51436
|
+
}
|
|
51437
|
+
}
|
|
51438
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
51439
|
+
const key = randomBytes(16).toString("base64");
|
|
51440
|
+
const request = isSecure ? https.request : http.request;
|
|
51441
|
+
const protocolSet = new Set;
|
|
51442
|
+
let perMessageDeflate;
|
|
51443
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
51444
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
51445
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
51446
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
51447
|
+
opts.headers = {
|
|
51448
|
+
...opts.headers,
|
|
51449
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
51450
|
+
"Sec-WebSocket-Key": key,
|
|
51451
|
+
Connection: "Upgrade",
|
|
51452
|
+
Upgrade: "websocket"
|
|
51453
|
+
};
|
|
51454
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
51455
|
+
opts.timeout = opts.handshakeTimeout;
|
|
51456
|
+
if (opts.perMessageDeflate) {
|
|
51457
|
+
perMessageDeflate = new PerMessageDeflate(opts.perMessageDeflate !== true ? opts.perMessageDeflate : {}, false, opts.maxPayload);
|
|
51458
|
+
opts.headers["Sec-WebSocket-Extensions"] = format2({
|
|
51459
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
51460
|
+
});
|
|
51461
|
+
}
|
|
51462
|
+
if (protocols.length) {
|
|
51463
|
+
for (const protocol of protocols) {
|
|
51464
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
51465
|
+
throw new SyntaxError("An invalid or duplicated subprotocol was specified");
|
|
51466
|
+
}
|
|
51467
|
+
protocolSet.add(protocol);
|
|
51468
|
+
}
|
|
51469
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
51470
|
+
}
|
|
51471
|
+
if (opts.origin) {
|
|
51472
|
+
if (opts.protocolVersion < 13) {
|
|
51473
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
51474
|
+
} else {
|
|
51475
|
+
opts.headers.Origin = opts.origin;
|
|
51476
|
+
}
|
|
51477
|
+
}
|
|
51478
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
51479
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
51480
|
+
}
|
|
51481
|
+
if (isIpcUrl) {
|
|
51482
|
+
const parts2 = opts.path.split(":");
|
|
51483
|
+
opts.socketPath = parts2[0];
|
|
51484
|
+
opts.path = parts2[1];
|
|
51485
|
+
}
|
|
51486
|
+
let req;
|
|
51487
|
+
if (opts.followRedirects) {
|
|
51488
|
+
if (websocket._redirects === 0) {
|
|
51489
|
+
websocket._originalIpc = isIpcUrl;
|
|
51490
|
+
websocket._originalSecure = isSecure;
|
|
51491
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
51492
|
+
const headers = options && options.headers;
|
|
51493
|
+
options = { ...options, headers: {} };
|
|
51494
|
+
if (headers) {
|
|
51495
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
51496
|
+
options.headers[key2.toLowerCase()] = value;
|
|
51497
|
+
}
|
|
51498
|
+
}
|
|
51499
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
51500
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
51501
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
51502
|
+
delete opts.headers.authorization;
|
|
51503
|
+
delete opts.headers.cookie;
|
|
51504
|
+
if (!isSameHost)
|
|
51505
|
+
delete opts.headers.host;
|
|
51506
|
+
opts.auth = undefined;
|
|
51507
|
+
}
|
|
51508
|
+
}
|
|
51509
|
+
if (opts.auth && !options.headers.authorization) {
|
|
51510
|
+
options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
51511
|
+
}
|
|
51512
|
+
req = websocket._req = request(opts);
|
|
51513
|
+
if (websocket._redirects) {
|
|
51514
|
+
websocket.emit("redirect", websocket.url, req);
|
|
51515
|
+
}
|
|
51516
|
+
} else {
|
|
51517
|
+
req = websocket._req = request(opts);
|
|
51518
|
+
}
|
|
51519
|
+
if (opts.timeout) {
|
|
51520
|
+
req.on("timeout", () => {
|
|
51521
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
51522
|
+
});
|
|
51523
|
+
}
|
|
51524
|
+
req.on("error", (err2) => {
|
|
51525
|
+
if (req === null || req[kAborted])
|
|
51526
|
+
return;
|
|
51527
|
+
req = websocket._req = null;
|
|
51528
|
+
emitErrorAndClose(websocket, err2);
|
|
51529
|
+
});
|
|
51530
|
+
req.on("response", (res) => {
|
|
51531
|
+
const location2 = res.headers.location;
|
|
51532
|
+
const statusCode = res.statusCode;
|
|
51533
|
+
if (location2 && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
51534
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
51535
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
51536
|
+
return;
|
|
51537
|
+
}
|
|
51538
|
+
req.abort();
|
|
51539
|
+
let addr2;
|
|
51540
|
+
try {
|
|
51541
|
+
addr2 = new URL2(location2, address);
|
|
51542
|
+
} catch (e) {
|
|
51543
|
+
const err2 = new SyntaxError(`Invalid URL: ${location2}`);
|
|
51544
|
+
emitErrorAndClose(websocket, err2);
|
|
51545
|
+
return;
|
|
51546
|
+
}
|
|
51547
|
+
initAsClient(websocket, addr2, protocols, options);
|
|
51548
|
+
} else if (!websocket.emit("unexpected-response", req, res)) {
|
|
51549
|
+
abortHandshake(websocket, req, `Unexpected server response: ${res.statusCode}`);
|
|
51550
|
+
}
|
|
51551
|
+
});
|
|
51552
|
+
req.on("upgrade", (res, socket, head) => {
|
|
51553
|
+
websocket.emit("upgrade", res);
|
|
51554
|
+
if (websocket.readyState !== WebSocket2.CONNECTING)
|
|
51555
|
+
return;
|
|
51556
|
+
req = websocket._req = null;
|
|
51557
|
+
const upgrade = res.headers.upgrade;
|
|
51558
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
51559
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
51560
|
+
return;
|
|
51561
|
+
}
|
|
51562
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
51563
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
51564
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
51565
|
+
return;
|
|
51566
|
+
}
|
|
51567
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
51568
|
+
let protError;
|
|
51569
|
+
if (serverProt !== undefined) {
|
|
51570
|
+
if (!protocolSet.size) {
|
|
51571
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
51572
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
51573
|
+
protError = "Server sent an invalid subprotocol";
|
|
51574
|
+
}
|
|
51575
|
+
} else if (protocolSet.size) {
|
|
51576
|
+
protError = "Server sent no subprotocol";
|
|
51577
|
+
}
|
|
51578
|
+
if (protError) {
|
|
51579
|
+
abortHandshake(websocket, socket, protError);
|
|
51580
|
+
return;
|
|
51581
|
+
}
|
|
51582
|
+
if (serverProt)
|
|
51583
|
+
websocket._protocol = serverProt;
|
|
51584
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
51585
|
+
if (secWebSocketExtensions !== undefined) {
|
|
51586
|
+
if (!perMessageDeflate) {
|
|
51587
|
+
const message3 = "Server sent a Sec-WebSocket-Extensions header but no extension " + "was requested";
|
|
51588
|
+
abortHandshake(websocket, socket, message3);
|
|
51589
|
+
return;
|
|
51590
|
+
}
|
|
51591
|
+
let extensions;
|
|
51592
|
+
try {
|
|
51593
|
+
extensions = parse2(secWebSocketExtensions);
|
|
51594
|
+
} catch (err2) {
|
|
51595
|
+
const message3 = "Invalid Sec-WebSocket-Extensions header";
|
|
51596
|
+
abortHandshake(websocket, socket, message3);
|
|
51597
|
+
return;
|
|
51598
|
+
}
|
|
51599
|
+
const extensionNames = Object.keys(extensions);
|
|
51600
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
51601
|
+
const message3 = "Server indicated an extension that was not requested";
|
|
51602
|
+
abortHandshake(websocket, socket, message3);
|
|
51603
|
+
return;
|
|
51604
|
+
}
|
|
51605
|
+
try {
|
|
51606
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
51607
|
+
} catch (err2) {
|
|
51608
|
+
const message3 = "Invalid Sec-WebSocket-Extensions header";
|
|
51609
|
+
abortHandshake(websocket, socket, message3);
|
|
51610
|
+
return;
|
|
51611
|
+
}
|
|
51612
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
51613
|
+
}
|
|
51614
|
+
websocket.setSocket(socket, head, {
|
|
51615
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
51616
|
+
generateMask: opts.generateMask,
|
|
51617
|
+
maxPayload: opts.maxPayload,
|
|
51618
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
51619
|
+
});
|
|
51620
|
+
});
|
|
51621
|
+
if (opts.finishRequest) {
|
|
51622
|
+
opts.finishRequest(req, websocket);
|
|
51623
|
+
} else {
|
|
51624
|
+
req.end();
|
|
51625
|
+
}
|
|
51626
|
+
}
|
|
51627
|
+
function emitErrorAndClose(websocket, err2) {
|
|
51628
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
51629
|
+
websocket._errorEmitted = true;
|
|
51630
|
+
websocket.emit("error", err2);
|
|
51631
|
+
websocket.emitClose();
|
|
51632
|
+
}
|
|
51633
|
+
function netConnect(options) {
|
|
51634
|
+
options.path = options.socketPath;
|
|
51635
|
+
return net.connect(options);
|
|
51636
|
+
}
|
|
51637
|
+
function tlsConnect(options) {
|
|
51638
|
+
options.path = undefined;
|
|
51639
|
+
if (!options.servername && options.servername !== "") {
|
|
51640
|
+
options.servername = net.isIP(options.host) ? "" : options.host;
|
|
51641
|
+
}
|
|
51642
|
+
return tls.connect(options);
|
|
51643
|
+
}
|
|
51644
|
+
function abortHandshake(websocket, stream, message3) {
|
|
51645
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
51646
|
+
const err2 = new Error(message3);
|
|
51647
|
+
Error.captureStackTrace(err2, abortHandshake);
|
|
51648
|
+
if (stream.setHeader) {
|
|
51649
|
+
stream[kAborted] = true;
|
|
51650
|
+
stream.abort();
|
|
51651
|
+
if (stream.socket && !stream.socket.destroyed) {
|
|
51652
|
+
stream.socket.destroy();
|
|
51653
|
+
}
|
|
51654
|
+
process.nextTick(emitErrorAndClose, websocket, err2);
|
|
51655
|
+
} else {
|
|
51656
|
+
stream.destroy(err2);
|
|
51657
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
51658
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
51659
|
+
}
|
|
51660
|
+
}
|
|
51661
|
+
function sendAfterClose(websocket, data, cb) {
|
|
51662
|
+
if (data) {
|
|
51663
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
51664
|
+
if (websocket._socket)
|
|
51665
|
+
websocket._sender._bufferedBytes += length;
|
|
51666
|
+
else
|
|
51667
|
+
websocket._bufferedAmount += length;
|
|
51668
|
+
}
|
|
51669
|
+
if (cb) {
|
|
51670
|
+
const err2 = new Error(`WebSocket is not open: readyState ${websocket.readyState} ` + `(${readyStates[websocket.readyState]})`);
|
|
51671
|
+
process.nextTick(cb, err2);
|
|
51672
|
+
}
|
|
51673
|
+
}
|
|
51674
|
+
function receiverOnConclude(code, reason) {
|
|
51675
|
+
const websocket = this[kWebSocket];
|
|
51676
|
+
websocket._closeFrameReceived = true;
|
|
51677
|
+
websocket._closeMessage = reason;
|
|
51678
|
+
websocket._closeCode = code;
|
|
51679
|
+
if (websocket._socket[kWebSocket] === undefined)
|
|
51680
|
+
return;
|
|
51681
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
51682
|
+
process.nextTick(resume, websocket._socket);
|
|
51683
|
+
if (code === 1005)
|
|
51684
|
+
websocket.close();
|
|
51685
|
+
else
|
|
51686
|
+
websocket.close(code, reason);
|
|
51687
|
+
}
|
|
51688
|
+
function receiverOnDrain() {
|
|
51689
|
+
const websocket = this[kWebSocket];
|
|
51690
|
+
if (!websocket.isPaused)
|
|
51691
|
+
websocket._socket.resume();
|
|
51692
|
+
}
|
|
51693
|
+
function receiverOnError(err2) {
|
|
51694
|
+
const websocket = this[kWebSocket];
|
|
51695
|
+
if (websocket._socket[kWebSocket] !== undefined) {
|
|
51696
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
51697
|
+
process.nextTick(resume, websocket._socket);
|
|
51698
|
+
websocket.close(err2[kStatusCode]);
|
|
51699
|
+
}
|
|
51700
|
+
if (!websocket._errorEmitted) {
|
|
51701
|
+
websocket._errorEmitted = true;
|
|
51702
|
+
websocket.emit("error", err2);
|
|
51703
|
+
}
|
|
51704
|
+
}
|
|
51705
|
+
function receiverOnFinish() {
|
|
51706
|
+
this[kWebSocket].emitClose();
|
|
51707
|
+
}
|
|
51708
|
+
function receiverOnMessage(data, isBinary) {
|
|
51709
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
51710
|
+
}
|
|
51711
|
+
function receiverOnPing(data) {
|
|
51712
|
+
const websocket = this[kWebSocket];
|
|
51713
|
+
if (websocket._autoPong)
|
|
51714
|
+
websocket.pong(data, !this._isServer, NOOP);
|
|
51715
|
+
websocket.emit("ping", data);
|
|
51716
|
+
}
|
|
51717
|
+
function receiverOnPong(data) {
|
|
51718
|
+
this[kWebSocket].emit("pong", data);
|
|
51719
|
+
}
|
|
51720
|
+
function resume(stream) {
|
|
51721
|
+
stream.resume();
|
|
51722
|
+
}
|
|
51723
|
+
function senderOnError(err2) {
|
|
51724
|
+
const websocket = this[kWebSocket];
|
|
51725
|
+
if (websocket.readyState === WebSocket2.CLOSED)
|
|
51726
|
+
return;
|
|
51727
|
+
if (websocket.readyState === WebSocket2.OPEN) {
|
|
51728
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
51729
|
+
setCloseTimer(websocket);
|
|
51730
|
+
}
|
|
51731
|
+
this._socket.end();
|
|
51732
|
+
if (!websocket._errorEmitted) {
|
|
51733
|
+
websocket._errorEmitted = true;
|
|
51734
|
+
websocket.emit("error", err2);
|
|
51735
|
+
}
|
|
51736
|
+
}
|
|
51737
|
+
function setCloseTimer(websocket) {
|
|
51738
|
+
websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), closeTimeout);
|
|
51739
|
+
}
|
|
51740
|
+
function socketOnClose() {
|
|
51741
|
+
const websocket = this[kWebSocket];
|
|
51742
|
+
this.removeListener("close", socketOnClose);
|
|
51743
|
+
this.removeListener("data", socketOnData);
|
|
51744
|
+
this.removeListener("end", socketOnEnd);
|
|
51745
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
51746
|
+
let chunk;
|
|
51747
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && (chunk = websocket._socket.read()) !== null) {
|
|
51748
|
+
websocket._receiver.write(chunk);
|
|
51749
|
+
}
|
|
51750
|
+
websocket._receiver.end();
|
|
51751
|
+
this[kWebSocket] = undefined;
|
|
51752
|
+
clearTimeout(websocket._closeTimer);
|
|
51753
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
|
|
51754
|
+
websocket.emitClose();
|
|
51755
|
+
} else {
|
|
51756
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
51757
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
51758
|
+
}
|
|
51759
|
+
}
|
|
51760
|
+
function socketOnData(chunk) {
|
|
51761
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
51762
|
+
this.pause();
|
|
51763
|
+
}
|
|
51764
|
+
}
|
|
51765
|
+
function socketOnEnd() {
|
|
51766
|
+
const websocket = this[kWebSocket];
|
|
51767
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
51768
|
+
websocket._receiver.end();
|
|
51769
|
+
this.end();
|
|
51770
|
+
}
|
|
51771
|
+
function socketOnError() {
|
|
51772
|
+
const websocket = this[kWebSocket];
|
|
51773
|
+
this.removeListener("error", socketOnError);
|
|
51774
|
+
this.on("error", NOOP);
|
|
51775
|
+
if (websocket) {
|
|
51776
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
51777
|
+
this.destroy();
|
|
51778
|
+
}
|
|
51779
|
+
}
|
|
51780
|
+
});
|
|
51781
|
+
|
|
51782
|
+
// ../../node_modules/ws/lib/stream.js
|
|
51783
|
+
var require_stream = __commonJS((exports, module2) => {
|
|
51784
|
+
var WebSocket2 = require_websocket();
|
|
51785
|
+
var { Duplex } = __require("stream");
|
|
51786
|
+
function emitClose(stream) {
|
|
51787
|
+
stream.emit("close");
|
|
51788
|
+
}
|
|
51789
|
+
function duplexOnEnd() {
|
|
51790
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
51791
|
+
this.destroy();
|
|
51792
|
+
}
|
|
51793
|
+
}
|
|
51794
|
+
function duplexOnError(err2) {
|
|
51795
|
+
this.removeListener("error", duplexOnError);
|
|
51796
|
+
this.destroy();
|
|
51797
|
+
if (this.listenerCount("error") === 0) {
|
|
51798
|
+
this.emit("error", err2);
|
|
51799
|
+
}
|
|
51800
|
+
}
|
|
51801
|
+
function createWebSocketStream(ws, options) {
|
|
51802
|
+
let terminateOnDestroy = true;
|
|
51803
|
+
const duplex = new Duplex({
|
|
51804
|
+
...options,
|
|
51805
|
+
autoDestroy: false,
|
|
51806
|
+
emitClose: false,
|
|
51807
|
+
objectMode: false,
|
|
51808
|
+
writableObjectMode: false
|
|
51809
|
+
});
|
|
51810
|
+
ws.on("message", function message(msg, isBinary) {
|
|
51811
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
51812
|
+
if (!duplex.push(data))
|
|
51813
|
+
ws.pause();
|
|
51814
|
+
});
|
|
51815
|
+
ws.once("error", function error(err2) {
|
|
51816
|
+
if (duplex.destroyed)
|
|
51817
|
+
return;
|
|
51818
|
+
terminateOnDestroy = false;
|
|
51819
|
+
duplex.destroy(err2);
|
|
51820
|
+
});
|
|
51821
|
+
ws.once("close", function close() {
|
|
51822
|
+
if (duplex.destroyed)
|
|
51823
|
+
return;
|
|
51824
|
+
duplex.push(null);
|
|
51825
|
+
});
|
|
51826
|
+
duplex._destroy = function(err2, callback) {
|
|
51827
|
+
if (ws.readyState === ws.CLOSED) {
|
|
51828
|
+
callback(err2);
|
|
51829
|
+
process.nextTick(emitClose, duplex);
|
|
51830
|
+
return;
|
|
51831
|
+
}
|
|
51832
|
+
let called = false;
|
|
51833
|
+
ws.once("error", function error(err3) {
|
|
51834
|
+
called = true;
|
|
51835
|
+
callback(err3);
|
|
51836
|
+
});
|
|
51837
|
+
ws.once("close", function close() {
|
|
51838
|
+
if (!called)
|
|
51839
|
+
callback(err2);
|
|
51840
|
+
process.nextTick(emitClose, duplex);
|
|
51841
|
+
});
|
|
51842
|
+
if (terminateOnDestroy)
|
|
51843
|
+
ws.terminate();
|
|
51844
|
+
};
|
|
51845
|
+
duplex._final = function(callback) {
|
|
51846
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
51847
|
+
ws.once("open", function open() {
|
|
51848
|
+
duplex._final(callback);
|
|
51849
|
+
});
|
|
51850
|
+
return;
|
|
51851
|
+
}
|
|
51852
|
+
if (ws._socket === null)
|
|
51853
|
+
return;
|
|
51854
|
+
if (ws._socket._writableState.finished) {
|
|
51855
|
+
callback();
|
|
51856
|
+
if (duplex._readableState.endEmitted)
|
|
51857
|
+
duplex.destroy();
|
|
51858
|
+
} else {
|
|
51859
|
+
ws._socket.once("finish", function finish() {
|
|
51860
|
+
callback();
|
|
51861
|
+
});
|
|
51862
|
+
ws.close();
|
|
51863
|
+
}
|
|
51864
|
+
};
|
|
51865
|
+
duplex._read = function() {
|
|
51866
|
+
if (ws.isPaused)
|
|
51867
|
+
ws.resume();
|
|
51868
|
+
};
|
|
51869
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
51870
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
51871
|
+
ws.once("open", function open() {
|
|
51872
|
+
duplex._write(chunk, encoding, callback);
|
|
51873
|
+
});
|
|
51874
|
+
return;
|
|
51875
|
+
}
|
|
51876
|
+
ws.send(chunk, callback);
|
|
51877
|
+
};
|
|
51878
|
+
duplex.on("end", duplexOnEnd);
|
|
51879
|
+
duplex.on("error", duplexOnError);
|
|
51880
|
+
return duplex;
|
|
51881
|
+
}
|
|
51882
|
+
module2.exports = createWebSocketStream;
|
|
51883
|
+
});
|
|
51884
|
+
|
|
51885
|
+
// ../../node_modules/ws/lib/subprotocol.js
|
|
51886
|
+
var require_subprotocol = __commonJS((exports, module2) => {
|
|
51887
|
+
var { tokenChars } = require_validation();
|
|
51888
|
+
function parse2(header) {
|
|
51889
|
+
const protocols = new Set;
|
|
51890
|
+
let start2 = -1;
|
|
51891
|
+
let end = -1;
|
|
51892
|
+
let i3 = 0;
|
|
51893
|
+
for (i3;i3 < header.length; i3++) {
|
|
51894
|
+
const code = header.charCodeAt(i3);
|
|
51895
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
51896
|
+
if (start2 === -1)
|
|
51897
|
+
start2 = i3;
|
|
51898
|
+
} else if (i3 !== 0 && (code === 32 || code === 9)) {
|
|
51899
|
+
if (end === -1 && start2 !== -1)
|
|
51900
|
+
end = i3;
|
|
51901
|
+
} else if (code === 44) {
|
|
51902
|
+
if (start2 === -1) {
|
|
51903
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
51904
|
+
}
|
|
51905
|
+
if (end === -1)
|
|
51906
|
+
end = i3;
|
|
51907
|
+
const protocol2 = header.slice(start2, end);
|
|
51908
|
+
if (protocols.has(protocol2)) {
|
|
51909
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
51910
|
+
}
|
|
51911
|
+
protocols.add(protocol2);
|
|
51912
|
+
start2 = end = -1;
|
|
51913
|
+
} else {
|
|
51914
|
+
throw new SyntaxError(`Unexpected character at index ${i3}`);
|
|
51915
|
+
}
|
|
51916
|
+
}
|
|
51917
|
+
if (start2 === -1 || end !== -1) {
|
|
51918
|
+
throw new SyntaxError("Unexpected end of input");
|
|
51919
|
+
}
|
|
51920
|
+
const protocol = header.slice(start2, i3);
|
|
51921
|
+
if (protocols.has(protocol)) {
|
|
51922
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
51923
|
+
}
|
|
51924
|
+
protocols.add(protocol);
|
|
51925
|
+
return protocols;
|
|
51926
|
+
}
|
|
51927
|
+
module2.exports = { parse: parse2 };
|
|
51928
|
+
});
|
|
51929
|
+
|
|
51930
|
+
// ../../node_modules/ws/lib/websocket-server.js
|
|
51931
|
+
var require_websocket_server = __commonJS((exports, module2) => {
|
|
51932
|
+
var EventEmitter = __require("events");
|
|
51933
|
+
var http = __require("http");
|
|
51934
|
+
var { Duplex } = __require("stream");
|
|
51935
|
+
var { createHash } = __require("crypto");
|
|
51936
|
+
var extension = require_extension();
|
|
51937
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
51938
|
+
var subprotocol = require_subprotocol();
|
|
51939
|
+
var WebSocket2 = require_websocket();
|
|
51940
|
+
var { GUID, kWebSocket } = require_constants();
|
|
51941
|
+
var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
51942
|
+
var RUNNING = 0;
|
|
51943
|
+
var CLOSING = 1;
|
|
51944
|
+
var CLOSED = 2;
|
|
51945
|
+
|
|
51946
|
+
class WebSocketServer extends EventEmitter {
|
|
51947
|
+
constructor(options, callback) {
|
|
51948
|
+
super();
|
|
51949
|
+
options = {
|
|
51950
|
+
allowSynchronousEvents: true,
|
|
51951
|
+
autoPong: true,
|
|
51952
|
+
maxPayload: 100 * 1024 * 1024,
|
|
51953
|
+
skipUTF8Validation: false,
|
|
51954
|
+
perMessageDeflate: false,
|
|
51955
|
+
handleProtocols: null,
|
|
51956
|
+
clientTracking: true,
|
|
51957
|
+
verifyClient: null,
|
|
51958
|
+
noServer: false,
|
|
51959
|
+
backlog: null,
|
|
51960
|
+
server: null,
|
|
51961
|
+
host: null,
|
|
51962
|
+
path: null,
|
|
51963
|
+
port: null,
|
|
51964
|
+
WebSocket: WebSocket2,
|
|
51965
|
+
...options
|
|
51966
|
+
};
|
|
51967
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
|
|
51968
|
+
throw new TypeError('One and only one of the "port", "server", or "noServer" options ' + "must be specified");
|
|
51969
|
+
}
|
|
51970
|
+
if (options.port != null) {
|
|
51971
|
+
this._server = http.createServer((req, res) => {
|
|
51972
|
+
const body2 = http.STATUS_CODES[426];
|
|
51973
|
+
res.writeHead(426, {
|
|
51974
|
+
"Content-Length": body2.length,
|
|
51975
|
+
"Content-Type": "text/plain"
|
|
51976
|
+
});
|
|
51977
|
+
res.end(body2);
|
|
51978
|
+
});
|
|
51979
|
+
this._server.listen(options.port, options.host, options.backlog, callback);
|
|
51980
|
+
} else if (options.server) {
|
|
51981
|
+
this._server = options.server;
|
|
51982
|
+
}
|
|
51983
|
+
if (this._server) {
|
|
51984
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
51985
|
+
this._removeListeners = addListeners(this._server, {
|
|
51986
|
+
listening: this.emit.bind(this, "listening"),
|
|
51987
|
+
error: this.emit.bind(this, "error"),
|
|
51988
|
+
upgrade: (req, socket, head) => {
|
|
51989
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
51990
|
+
}
|
|
51991
|
+
});
|
|
51992
|
+
}
|
|
51993
|
+
if (options.perMessageDeflate === true)
|
|
51994
|
+
options.perMessageDeflate = {};
|
|
51995
|
+
if (options.clientTracking) {
|
|
51996
|
+
this.clients = new Set;
|
|
51997
|
+
this._shouldEmitClose = false;
|
|
51998
|
+
}
|
|
51999
|
+
this.options = options;
|
|
52000
|
+
this._state = RUNNING;
|
|
52001
|
+
}
|
|
52002
|
+
address() {
|
|
52003
|
+
if (this.options.noServer) {
|
|
52004
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
52005
|
+
}
|
|
52006
|
+
if (!this._server)
|
|
52007
|
+
return null;
|
|
52008
|
+
return this._server.address();
|
|
52009
|
+
}
|
|
52010
|
+
close(cb) {
|
|
52011
|
+
if (this._state === CLOSED) {
|
|
52012
|
+
if (cb) {
|
|
52013
|
+
this.once("close", () => {
|
|
52014
|
+
cb(new Error("The server is not running"));
|
|
52015
|
+
});
|
|
52016
|
+
}
|
|
52017
|
+
process.nextTick(emitClose, this);
|
|
52018
|
+
return;
|
|
52019
|
+
}
|
|
52020
|
+
if (cb)
|
|
52021
|
+
this.once("close", cb);
|
|
52022
|
+
if (this._state === CLOSING)
|
|
52023
|
+
return;
|
|
52024
|
+
this._state = CLOSING;
|
|
52025
|
+
if (this.options.noServer || this.options.server) {
|
|
52026
|
+
if (this._server) {
|
|
52027
|
+
this._removeListeners();
|
|
52028
|
+
this._removeListeners = this._server = null;
|
|
52029
|
+
}
|
|
52030
|
+
if (this.clients) {
|
|
52031
|
+
if (!this.clients.size) {
|
|
52032
|
+
process.nextTick(emitClose, this);
|
|
52033
|
+
} else {
|
|
52034
|
+
this._shouldEmitClose = true;
|
|
52035
|
+
}
|
|
52036
|
+
} else {
|
|
52037
|
+
process.nextTick(emitClose, this);
|
|
52038
|
+
}
|
|
52039
|
+
} else {
|
|
52040
|
+
const server2 = this._server;
|
|
52041
|
+
this._removeListeners();
|
|
52042
|
+
this._removeListeners = this._server = null;
|
|
52043
|
+
server2.close(() => {
|
|
52044
|
+
emitClose(this);
|
|
52045
|
+
});
|
|
52046
|
+
}
|
|
52047
|
+
}
|
|
52048
|
+
shouldHandle(req) {
|
|
52049
|
+
if (this.options.path) {
|
|
52050
|
+
const index6 = req.url.indexOf("?");
|
|
52051
|
+
const pathname = index6 !== -1 ? req.url.slice(0, index6) : req.url;
|
|
52052
|
+
if (pathname !== this.options.path)
|
|
52053
|
+
return false;
|
|
52054
|
+
}
|
|
52055
|
+
return true;
|
|
52056
|
+
}
|
|
52057
|
+
handleUpgrade(req, socket, head, cb) {
|
|
52058
|
+
socket.on("error", socketOnError);
|
|
52059
|
+
const key = req.headers["sec-websocket-key"];
|
|
52060
|
+
const upgrade = req.headers.upgrade;
|
|
52061
|
+
const version3 = +req.headers["sec-websocket-version"];
|
|
52062
|
+
if (req.method !== "GET") {
|
|
52063
|
+
const message3 = "Invalid HTTP method";
|
|
52064
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message3);
|
|
52065
|
+
return;
|
|
52066
|
+
}
|
|
52067
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
52068
|
+
const message3 = "Invalid Upgrade header";
|
|
52069
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message3);
|
|
52070
|
+
return;
|
|
52071
|
+
}
|
|
52072
|
+
if (key === undefined || !keyRegex.test(key)) {
|
|
52073
|
+
const message3 = "Missing or invalid Sec-WebSocket-Key header";
|
|
52074
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message3);
|
|
52075
|
+
return;
|
|
52076
|
+
}
|
|
52077
|
+
if (version3 !== 8 && version3 !== 13) {
|
|
52078
|
+
const message3 = "Missing or invalid Sec-WebSocket-Version header";
|
|
52079
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message3);
|
|
52080
|
+
return;
|
|
52081
|
+
}
|
|
52082
|
+
if (!this.shouldHandle(req)) {
|
|
52083
|
+
abortHandshake(socket, 400);
|
|
52084
|
+
return;
|
|
52085
|
+
}
|
|
52086
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
52087
|
+
let protocols = new Set;
|
|
52088
|
+
if (secWebSocketProtocol !== undefined) {
|
|
52089
|
+
try {
|
|
52090
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
52091
|
+
} catch (err2) {
|
|
52092
|
+
const message3 = "Invalid Sec-WebSocket-Protocol header";
|
|
52093
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message3);
|
|
52094
|
+
return;
|
|
52095
|
+
}
|
|
52096
|
+
}
|
|
52097
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
52098
|
+
const extensions = {};
|
|
52099
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== undefined) {
|
|
52100
|
+
const perMessageDeflate = new PerMessageDeflate(this.options.perMessageDeflate, true, this.options.maxPayload);
|
|
52101
|
+
try {
|
|
52102
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
52103
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
52104
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
52105
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
52106
|
+
}
|
|
52107
|
+
} catch (err2) {
|
|
52108
|
+
const message3 = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
52109
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message3);
|
|
52110
|
+
return;
|
|
52111
|
+
}
|
|
52112
|
+
}
|
|
52113
|
+
if (this.options.verifyClient) {
|
|
52114
|
+
const info2 = {
|
|
52115
|
+
origin: req.headers[`${version3 === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
52116
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
52117
|
+
req
|
|
52118
|
+
};
|
|
52119
|
+
if (this.options.verifyClient.length === 2) {
|
|
52120
|
+
this.options.verifyClient(info2, (verified, code, message3, headers) => {
|
|
52121
|
+
if (!verified) {
|
|
52122
|
+
return abortHandshake(socket, code || 401, message3, headers);
|
|
52123
|
+
}
|
|
52124
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
52125
|
+
});
|
|
52126
|
+
return;
|
|
52127
|
+
}
|
|
52128
|
+
if (!this.options.verifyClient(info2))
|
|
52129
|
+
return abortHandshake(socket, 401);
|
|
52130
|
+
}
|
|
52131
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
52132
|
+
}
|
|
52133
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
52134
|
+
if (!socket.readable || !socket.writable)
|
|
52135
|
+
return socket.destroy();
|
|
52136
|
+
if (socket[kWebSocket]) {
|
|
52137
|
+
throw new Error("server.handleUpgrade() was called more than once with the same " + "socket, possibly due to a misconfiguration");
|
|
52138
|
+
}
|
|
52139
|
+
if (this._state > RUNNING)
|
|
52140
|
+
return abortHandshake(socket, 503);
|
|
52141
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
52142
|
+
const headers = [
|
|
52143
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
52144
|
+
"Upgrade: websocket",
|
|
52145
|
+
"Connection: Upgrade",
|
|
52146
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
52147
|
+
];
|
|
52148
|
+
const ws = new this.options.WebSocket(null, undefined, this.options);
|
|
52149
|
+
if (protocols.size) {
|
|
52150
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
52151
|
+
if (protocol) {
|
|
52152
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
52153
|
+
ws._protocol = protocol;
|
|
52154
|
+
}
|
|
52155
|
+
}
|
|
52156
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
52157
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
52158
|
+
const value = extension.format({
|
|
52159
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
52160
|
+
});
|
|
52161
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
52162
|
+
ws._extensions = extensions;
|
|
52163
|
+
}
|
|
52164
|
+
this.emit("headers", headers, req);
|
|
52165
|
+
socket.write(headers.concat(`\r
|
|
52166
|
+
`).join(`\r
|
|
52167
|
+
`));
|
|
52168
|
+
socket.removeListener("error", socketOnError);
|
|
52169
|
+
ws.setSocket(socket, head, {
|
|
52170
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
52171
|
+
maxPayload: this.options.maxPayload,
|
|
52172
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
52173
|
+
});
|
|
52174
|
+
if (this.clients) {
|
|
52175
|
+
this.clients.add(ws);
|
|
52176
|
+
ws.on("close", () => {
|
|
52177
|
+
this.clients.delete(ws);
|
|
52178
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
52179
|
+
process.nextTick(emitClose, this);
|
|
52180
|
+
}
|
|
52181
|
+
});
|
|
52182
|
+
}
|
|
52183
|
+
cb(ws, req);
|
|
52184
|
+
}
|
|
52185
|
+
}
|
|
52186
|
+
module2.exports = WebSocketServer;
|
|
52187
|
+
function addListeners(server2, map) {
|
|
52188
|
+
for (const event of Object.keys(map))
|
|
52189
|
+
server2.on(event, map[event]);
|
|
52190
|
+
return function removeListeners() {
|
|
52191
|
+
for (const event of Object.keys(map)) {
|
|
52192
|
+
server2.removeListener(event, map[event]);
|
|
52193
|
+
}
|
|
52194
|
+
};
|
|
52195
|
+
}
|
|
52196
|
+
function emitClose(server2) {
|
|
52197
|
+
server2._state = CLOSED;
|
|
52198
|
+
server2.emit("close");
|
|
52199
|
+
}
|
|
52200
|
+
function socketOnError() {
|
|
52201
|
+
this.destroy();
|
|
52202
|
+
}
|
|
52203
|
+
function abortHandshake(socket, code, message3, headers) {
|
|
52204
|
+
message3 = message3 || http.STATUS_CODES[code];
|
|
52205
|
+
headers = {
|
|
52206
|
+
Connection: "close",
|
|
52207
|
+
"Content-Type": "text/html",
|
|
52208
|
+
"Content-Length": Buffer.byteLength(message3),
|
|
52209
|
+
...headers
|
|
52210
|
+
};
|
|
52211
|
+
socket.once("finish", socket.destroy);
|
|
52212
|
+
socket.end(`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
52213
|
+
` + Object.keys(headers).map((h3) => `${h3}: ${headers[h3]}`).join(`\r
|
|
52214
|
+
`) + `\r
|
|
52215
|
+
\r
|
|
52216
|
+
` + message3);
|
|
52217
|
+
}
|
|
52218
|
+
function abortHandshakeOrEmitwsClientError(server2, req, socket, code, message3) {
|
|
52219
|
+
if (server2.listenerCount("wsClientError")) {
|
|
52220
|
+
const err2 = new Error(message3);
|
|
52221
|
+
Error.captureStackTrace(err2, abortHandshakeOrEmitwsClientError);
|
|
52222
|
+
server2.emit("wsClientError", err2, socket, req);
|
|
52223
|
+
} else {
|
|
52224
|
+
abortHandshake(socket, code, message3);
|
|
52225
|
+
}
|
|
52226
|
+
}
|
|
52227
|
+
});
|
|
52228
|
+
|
|
49392
52229
|
// ../../node_modules/commander/lib/error.js
|
|
49393
52230
|
var require_error = __commonJS((exports) => {
|
|
49394
52231
|
class CommanderError extends Error {
|
|
@@ -53377,7 +56214,7 @@ var logger = (fn = console.log) => {
|
|
|
53377
56214
|
// package.json
|
|
53378
56215
|
var package_default = {
|
|
53379
56216
|
name: "@playcademy/sandbox",
|
|
53380
|
-
version: "0.1.0-beta.
|
|
56217
|
+
version: "0.1.0-beta.15",
|
|
53381
56218
|
description: "Local development server for Playcademy game development",
|
|
53382
56219
|
type: "module",
|
|
53383
56220
|
exports: {
|
|
@@ -65072,7 +67909,8 @@ function setupAuth() {
|
|
|
65072
67909
|
}
|
|
65073
67910
|
// ../logger/src/index.ts
|
|
65074
67911
|
var isBrowser = () => {
|
|
65075
|
-
|
|
67912
|
+
const g5 = globalThis;
|
|
67913
|
+
return typeof g5.window !== "undefined" && typeof g5.document !== "undefined";
|
|
65076
67914
|
};
|
|
65077
67915
|
var colors = {
|
|
65078
67916
|
reset: "\x1B[0m",
|
|
@@ -66817,14 +69655,112 @@ async function createRealtimeServer(options = {}) {
|
|
|
66817
69655
|
}
|
|
66818
69656
|
return server;
|
|
66819
69657
|
}
|
|
69658
|
+
// ../realtime/src/server/sandbox.ts
|
|
69659
|
+
import http from "node:http";
|
|
69660
|
+
|
|
69661
|
+
// ../../node_modules/ws/wrapper.mjs
|
|
69662
|
+
var import_stream2 = __toESM(require_stream(), 1);
|
|
69663
|
+
var import_receiver = __toESM(require_receiver(), 1);
|
|
69664
|
+
var import_sender = __toESM(require_sender(), 1);
|
|
69665
|
+
var import_websocket = __toESM(require_websocket(), 1);
|
|
69666
|
+
var import_websocket_server = __toESM(require_websocket_server(), 1);
|
|
69667
|
+
|
|
69668
|
+
// ../realtime/src/server/sandbox.ts
|
|
69669
|
+
async function createSandboxRealtimeServer(options = {}) {
|
|
69670
|
+
const config2 = await loadConfig(options);
|
|
69671
|
+
const httpServer = http.createServer((req, res) => {
|
|
69672
|
+
if (req.url?.endsWith(config2.healthCheckPath)) {
|
|
69673
|
+
const body2 = JSON.stringify({
|
|
69674
|
+
status: "OK",
|
|
69675
|
+
connections: getActiveConnections(),
|
|
69676
|
+
timestamp: new Date().toISOString()
|
|
69677
|
+
});
|
|
69678
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
69679
|
+
res.end(body2);
|
|
69680
|
+
return;
|
|
69681
|
+
}
|
|
69682
|
+
res.writeHead(200);
|
|
69683
|
+
res.end("Playcademy realtime relay (sandbox)");
|
|
69684
|
+
});
|
|
69685
|
+
const wss = new import_websocket_server.default({ noServer: true });
|
|
69686
|
+
wss.on("connection", (ws) => {
|
|
69687
|
+
addConnection();
|
|
69688
|
+
ws.on("close", () => {
|
|
69689
|
+
removeConnection();
|
|
69690
|
+
});
|
|
69691
|
+
ws.on("message", (data) => {
|
|
69692
|
+
wss.clients.forEach((client2) => {
|
|
69693
|
+
if (client2.readyState === import_websocket.default.OPEN) {
|
|
69694
|
+
client2.send(data);
|
|
69695
|
+
}
|
|
69696
|
+
});
|
|
69697
|
+
});
|
|
69698
|
+
});
|
|
69699
|
+
httpServer.on("upgrade", async (request, socket, head) => {
|
|
69700
|
+
const fullUrl = `http://${request.headers.host}${request.url}`;
|
|
69701
|
+
const token = extractTokenFromUrl(fullUrl);
|
|
69702
|
+
if (!token) {
|
|
69703
|
+
socket.write(`HTTP/1.1 401 Unauthorized\r
|
|
69704
|
+
\r
|
|
69705
|
+
`);
|
|
69706
|
+
socket.destroy();
|
|
69707
|
+
return;
|
|
69708
|
+
}
|
|
69709
|
+
const authResult = await verifyToken(token, config2.betterAuthSecret);
|
|
69710
|
+
if (!authResult.success) {
|
|
69711
|
+
socket.write(`HTTP/1.1 401 Unauthorized\r
|
|
69712
|
+
\r
|
|
69713
|
+
`);
|
|
69714
|
+
socket.destroy();
|
|
69715
|
+
return;
|
|
69716
|
+
}
|
|
69717
|
+
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
69718
|
+
const data = {
|
|
69719
|
+
userId: authResult.userId,
|
|
69720
|
+
gameId: authResult.gameId,
|
|
69721
|
+
joinedChannels: new Set,
|
|
69722
|
+
requestedChannel: null,
|
|
69723
|
+
isAuthenticated: true
|
|
69724
|
+
};
|
|
69725
|
+
ws.data = data;
|
|
69726
|
+
wss.emit("connection", ws, request);
|
|
69727
|
+
});
|
|
69728
|
+
});
|
|
69729
|
+
await new Promise((resolve2) => {
|
|
69730
|
+
httpServer.listen(config2.port, () => resolve2());
|
|
69731
|
+
});
|
|
69732
|
+
if (!options.quiet) {
|
|
69733
|
+
log2.info("Sandbox realtime server started", {
|
|
69734
|
+
port: config2.port,
|
|
69735
|
+
healthCheck: `http://localhost:${config2.port}${config2.healthCheckPath}`
|
|
69736
|
+
});
|
|
69737
|
+
}
|
|
69738
|
+
return {
|
|
69739
|
+
port: config2.port,
|
|
69740
|
+
stop() {
|
|
69741
|
+
wss.clients.forEach((client2) => client2.close());
|
|
69742
|
+
wss.close();
|
|
69743
|
+
httpServer.close();
|
|
69744
|
+
}
|
|
69745
|
+
};
|
|
69746
|
+
}
|
|
69747
|
+
|
|
66820
69748
|
// src/lib/realtime.ts
|
|
66821
69749
|
async function startRealtimeServer(realtimeOptions, betterAuthSecret) {
|
|
66822
69750
|
if (!realtimeOptions.enabled) {
|
|
66823
69751
|
return null;
|
|
66824
69752
|
}
|
|
66825
69753
|
if (typeof Bun === "undefined") {
|
|
66826
|
-
|
|
66827
|
-
|
|
69754
|
+
try {
|
|
69755
|
+
return await createSandboxRealtimeServer({
|
|
69756
|
+
port: realtimeOptions.port,
|
|
69757
|
+
betterAuthSecret,
|
|
69758
|
+
quiet: true
|
|
69759
|
+
});
|
|
69760
|
+
} catch (error2) {
|
|
69761
|
+
console.error("Failed to start realtime server:", error2);
|
|
69762
|
+
return null;
|
|
69763
|
+
}
|
|
66828
69764
|
}
|
|
66829
69765
|
try {
|
|
66830
69766
|
return await createRealtimeServer({
|