@phenx-inc/ctlsurf 0.1.2 → 0.1.3
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/out/headless/index.mjs +4 -2
- package/out/headless/index.mjs.map +2 -2
- package/out/main/index.js +3678 -4
- package/out/renderer/assets/{cssMode-DL0XItGB.js → cssMode-CY6x0qXW.js} +3 -3
- package/out/renderer/assets/{freemarker2-CrOEuDcF.js → freemarker2-BXSW9BAX.js} +1 -1
- package/out/renderer/assets/{handlebars-D4QYaBof.js → handlebars-BYUZ1IOs.js} +1 -1
- package/out/renderer/assets/{html-B2Dqk2ai.js → html-DPocQM4t.js} +1 -1
- package/out/renderer/assets/{htmlMode-CdZ0Prhd.js → htmlMode-CsPinKYA.js} +3 -3
- package/out/renderer/assets/{index-pZmE1QXB.js → index-Bml7oDn9.js} +84 -36
- package/out/renderer/assets/{index-CJ6RsQWP.css → index-DK9wLFFm.css} +146 -0
- package/out/renderer/assets/{javascript-CK8zNQXj.js → javascript-_HVGB-lj.js} +2 -2
- package/out/renderer/assets/{jsonMode-Cewaellc.js → jsonMode-JbrRQBOU.js} +3 -3
- package/out/renderer/assets/{liquid-Bd3GPNs2.js → liquid-B7izKdqo.js} +1 -1
- package/out/renderer/assets/{lspLanguageFeatures-DSDH7BnA.js → lspLanguageFeatures-DzxH499X.js} +1 -1
- package/out/renderer/assets/{mdx-CCPVCrXC.js → mdx-CmvUeYLw.js} +1 -1
- package/out/renderer/assets/{python-34jOtlcC.js → python-DJqYTFoi.js} +1 -1
- package/out/renderer/assets/{razor-DXRw694z.js → razor-CGEA5nUK.js} +1 -1
- package/out/renderer/assets/{tsMode-CmND5_wB.js → tsMode-CN0FOHMy.js} +1 -1
- package/out/renderer/assets/{typescript-BNNI0Euv.js → typescript-CIn-DSfY.js} +1 -1
- package/out/renderer/assets/{xml-CgdndrNB.js → xml-C5t3U2jS.js} +1 -1
- package/out/renderer/assets/{yaml-DNWPIf1s.js → yaml-n-Jb6xf1.js} +1 -1
- package/out/renderer/index.html +2 -2
- package/package.json +6 -4
- package/src/main/workerWs.ts +6 -2
- package/src/renderer/App.tsx +38 -12
- package/src/renderer/components/AgentPicker.tsx +49 -0
- package/src/renderer/styles.css +146 -0
package/out/main/index.js
CHANGED
|
@@ -4,7 +4,16 @@ const path = require("path");
|
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const os = require("os");
|
|
6
6
|
const module$1 = require("module");
|
|
7
|
-
const
|
|
7
|
+
const require$$1$1 = require("crypto");
|
|
8
|
+
const require$$0$3 = require("events");
|
|
9
|
+
const require$$1$2 = require("https");
|
|
10
|
+
const require$$2$1 = require("http");
|
|
11
|
+
const require$$3 = require("net");
|
|
12
|
+
const require$$4 = require("tls");
|
|
13
|
+
const require$$0$2 = require("stream");
|
|
14
|
+
const require$$7 = require("url");
|
|
15
|
+
const require$$0 = require("zlib");
|
|
16
|
+
const require$$0$1 = require("buffer");
|
|
8
17
|
function getShellCommand() {
|
|
9
18
|
if (process.platform === "win32") return "powershell.exe";
|
|
10
19
|
return process.env.SHELL || "/bin/zsh";
|
|
@@ -323,6 +332,3671 @@ class ConversationBridge {
|
|
|
323
332
|
function stripAnsi(str) {
|
|
324
333
|
return str.replace(/\x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]/g, "").replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, "").replace(/\x1b[^[\]](.|$)/g, "").replace(/\x1b/g, "").replace(/\r/g, "").replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, "");
|
|
325
334
|
}
|
|
335
|
+
function getDefaultExportFromCjs(x) {
|
|
336
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
|
|
337
|
+
}
|
|
338
|
+
function getAugmentedNamespace(n) {
|
|
339
|
+
if (Object.prototype.hasOwnProperty.call(n, "__esModule")) return n;
|
|
340
|
+
var f = n.default;
|
|
341
|
+
if (typeof f == "function") {
|
|
342
|
+
var a = function a2() {
|
|
343
|
+
if (this instanceof a2) {
|
|
344
|
+
return Reflect.construct(f, arguments, this.constructor);
|
|
345
|
+
}
|
|
346
|
+
return f.apply(this, arguments);
|
|
347
|
+
};
|
|
348
|
+
a.prototype = f.prototype;
|
|
349
|
+
} else a = {};
|
|
350
|
+
Object.defineProperty(a, "__esModule", { value: true });
|
|
351
|
+
Object.keys(n).forEach(function(k) {
|
|
352
|
+
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
353
|
+
Object.defineProperty(a, k, d.get ? d : {
|
|
354
|
+
enumerable: true,
|
|
355
|
+
get: function() {
|
|
356
|
+
return n[k];
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
return a;
|
|
361
|
+
}
|
|
362
|
+
var bufferUtil = { exports: {} };
|
|
363
|
+
var constants;
|
|
364
|
+
var hasRequiredConstants;
|
|
365
|
+
function requireConstants() {
|
|
366
|
+
if (hasRequiredConstants) return constants;
|
|
367
|
+
hasRequiredConstants = 1;
|
|
368
|
+
const BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
369
|
+
const hasBlob = typeof Blob !== "undefined";
|
|
370
|
+
if (hasBlob) BINARY_TYPES.push("blob");
|
|
371
|
+
constants = {
|
|
372
|
+
BINARY_TYPES,
|
|
373
|
+
CLOSE_TIMEOUT: 3e4,
|
|
374
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
375
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
376
|
+
hasBlob,
|
|
377
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
378
|
+
kListener: Symbol("kListener"),
|
|
379
|
+
kStatusCode: Symbol("status-code"),
|
|
380
|
+
kWebSocket: Symbol("websocket"),
|
|
381
|
+
NOOP: () => {
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
return constants;
|
|
385
|
+
}
|
|
386
|
+
const __viteOptionalPeerDep_bufferutil_ws = {};
|
|
387
|
+
const __viteOptionalPeerDep_bufferutil_ws$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
388
|
+
__proto__: null,
|
|
389
|
+
default: __viteOptionalPeerDep_bufferutil_ws
|
|
390
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
391
|
+
const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteOptionalPeerDep_bufferutil_ws$1);
|
|
392
|
+
var hasRequiredBufferUtil;
|
|
393
|
+
function requireBufferUtil() {
|
|
394
|
+
if (hasRequiredBufferUtil) return bufferUtil.exports;
|
|
395
|
+
hasRequiredBufferUtil = 1;
|
|
396
|
+
const { EMPTY_BUFFER } = requireConstants();
|
|
397
|
+
const FastBuffer = Buffer[Symbol.species];
|
|
398
|
+
function concat(list, totalLength) {
|
|
399
|
+
if (list.length === 0) return EMPTY_BUFFER;
|
|
400
|
+
if (list.length === 1) return list[0];
|
|
401
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
402
|
+
let offset = 0;
|
|
403
|
+
for (let i = 0; i < list.length; i++) {
|
|
404
|
+
const buf = list[i];
|
|
405
|
+
target.set(buf, offset);
|
|
406
|
+
offset += buf.length;
|
|
407
|
+
}
|
|
408
|
+
if (offset < totalLength) {
|
|
409
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
410
|
+
}
|
|
411
|
+
return target;
|
|
412
|
+
}
|
|
413
|
+
function _mask(source, mask, output, offset, length) {
|
|
414
|
+
for (let i = 0; i < length; i++) {
|
|
415
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function _unmask(buffer, mask) {
|
|
419
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
420
|
+
buffer[i] ^= mask[i & 3];
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function toArrayBuffer(buf) {
|
|
424
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
425
|
+
return buf.buffer;
|
|
426
|
+
}
|
|
427
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
428
|
+
}
|
|
429
|
+
function toBuffer(data) {
|
|
430
|
+
toBuffer.readOnly = true;
|
|
431
|
+
if (Buffer.isBuffer(data)) return data;
|
|
432
|
+
let buf;
|
|
433
|
+
if (data instanceof ArrayBuffer) {
|
|
434
|
+
buf = new FastBuffer(data);
|
|
435
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
436
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
437
|
+
} else {
|
|
438
|
+
buf = Buffer.from(data);
|
|
439
|
+
toBuffer.readOnly = false;
|
|
440
|
+
}
|
|
441
|
+
return buf;
|
|
442
|
+
}
|
|
443
|
+
bufferUtil.exports = {
|
|
444
|
+
concat,
|
|
445
|
+
mask: _mask,
|
|
446
|
+
toArrayBuffer,
|
|
447
|
+
toBuffer,
|
|
448
|
+
unmask: _unmask
|
|
449
|
+
};
|
|
450
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
451
|
+
try {
|
|
452
|
+
const bufferUtil$1 = require$$1;
|
|
453
|
+
bufferUtil.exports.mask = function(source, mask, output, offset, length) {
|
|
454
|
+
if (length < 48) _mask(source, mask, output, offset, length);
|
|
455
|
+
else bufferUtil$1.mask(source, mask, output, offset, length);
|
|
456
|
+
};
|
|
457
|
+
bufferUtil.exports.unmask = function(buffer, mask) {
|
|
458
|
+
if (buffer.length < 32) _unmask(buffer, mask);
|
|
459
|
+
else bufferUtil$1.unmask(buffer, mask);
|
|
460
|
+
};
|
|
461
|
+
} catch (e) {
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return bufferUtil.exports;
|
|
465
|
+
}
|
|
466
|
+
var limiter;
|
|
467
|
+
var hasRequiredLimiter;
|
|
468
|
+
function requireLimiter() {
|
|
469
|
+
if (hasRequiredLimiter) return limiter;
|
|
470
|
+
hasRequiredLimiter = 1;
|
|
471
|
+
const kDone = Symbol("kDone");
|
|
472
|
+
const kRun = Symbol("kRun");
|
|
473
|
+
class Limiter {
|
|
474
|
+
/**
|
|
475
|
+
* Creates a new `Limiter`.
|
|
476
|
+
*
|
|
477
|
+
* @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
|
|
478
|
+
* to run concurrently
|
|
479
|
+
*/
|
|
480
|
+
constructor(concurrency) {
|
|
481
|
+
this[kDone] = () => {
|
|
482
|
+
this.pending--;
|
|
483
|
+
this[kRun]();
|
|
484
|
+
};
|
|
485
|
+
this.concurrency = concurrency || Infinity;
|
|
486
|
+
this.jobs = [];
|
|
487
|
+
this.pending = 0;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Adds a job to the queue.
|
|
491
|
+
*
|
|
492
|
+
* @param {Function} job The job to run
|
|
493
|
+
* @public
|
|
494
|
+
*/
|
|
495
|
+
add(job) {
|
|
496
|
+
this.jobs.push(job);
|
|
497
|
+
this[kRun]();
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Removes a job from the queue and runs it if possible.
|
|
501
|
+
*
|
|
502
|
+
* @private
|
|
503
|
+
*/
|
|
504
|
+
[kRun]() {
|
|
505
|
+
if (this.pending === this.concurrency) return;
|
|
506
|
+
if (this.jobs.length) {
|
|
507
|
+
const job = this.jobs.shift();
|
|
508
|
+
this.pending++;
|
|
509
|
+
job(this[kDone]);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
limiter = Limiter;
|
|
514
|
+
return limiter;
|
|
515
|
+
}
|
|
516
|
+
var permessageDeflate;
|
|
517
|
+
var hasRequiredPermessageDeflate;
|
|
518
|
+
function requirePermessageDeflate() {
|
|
519
|
+
if (hasRequiredPermessageDeflate) return permessageDeflate;
|
|
520
|
+
hasRequiredPermessageDeflate = 1;
|
|
521
|
+
const zlib = require$$0;
|
|
522
|
+
const bufferUtil2 = requireBufferUtil();
|
|
523
|
+
const Limiter = requireLimiter();
|
|
524
|
+
const { kStatusCode } = requireConstants();
|
|
525
|
+
const FastBuffer = Buffer[Symbol.species];
|
|
526
|
+
const TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
527
|
+
const kPerMessageDeflate = Symbol("permessage-deflate");
|
|
528
|
+
const kTotalLength = Symbol("total-length");
|
|
529
|
+
const kCallback = Symbol("callback");
|
|
530
|
+
const kBuffers = Symbol("buffers");
|
|
531
|
+
const kError = Symbol("error");
|
|
532
|
+
let zlibLimiter;
|
|
533
|
+
class PerMessageDeflate {
|
|
534
|
+
/**
|
|
535
|
+
* Creates a PerMessageDeflate instance.
|
|
536
|
+
*
|
|
537
|
+
* @param {Object} [options] Configuration options
|
|
538
|
+
* @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
|
|
539
|
+
* for, or request, a custom client window size
|
|
540
|
+
* @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
|
|
541
|
+
* acknowledge disabling of client context takeover
|
|
542
|
+
* @param {Number} [options.concurrencyLimit=10] The number of concurrent
|
|
543
|
+
* calls to zlib
|
|
544
|
+
* @param {Boolean} [options.isServer=false] Create the instance in either
|
|
545
|
+
* server or client mode
|
|
546
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
|
547
|
+
* @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
|
|
548
|
+
* use of a custom server window size
|
|
549
|
+
* @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
|
|
550
|
+
* disabling of server context takeover
|
|
551
|
+
* @param {Number} [options.threshold=1024] Size (in bytes) below which
|
|
552
|
+
* messages should not be compressed if context takeover is disabled
|
|
553
|
+
* @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
|
|
554
|
+
* deflate
|
|
555
|
+
* @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
|
|
556
|
+
* inflate
|
|
557
|
+
*/
|
|
558
|
+
constructor(options) {
|
|
559
|
+
this._options = options || {};
|
|
560
|
+
this._threshold = this._options.threshold !== void 0 ? this._options.threshold : 1024;
|
|
561
|
+
this._maxPayload = this._options.maxPayload | 0;
|
|
562
|
+
this._isServer = !!this._options.isServer;
|
|
563
|
+
this._deflate = null;
|
|
564
|
+
this._inflate = null;
|
|
565
|
+
this.params = null;
|
|
566
|
+
if (!zlibLimiter) {
|
|
567
|
+
const concurrency = this._options.concurrencyLimit !== void 0 ? this._options.concurrencyLimit : 10;
|
|
568
|
+
zlibLimiter = new Limiter(concurrency);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* @type {String}
|
|
573
|
+
*/
|
|
574
|
+
static get extensionName() {
|
|
575
|
+
return "permessage-deflate";
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Create an extension negotiation offer.
|
|
579
|
+
*
|
|
580
|
+
* @return {Object} Extension parameters
|
|
581
|
+
* @public
|
|
582
|
+
*/
|
|
583
|
+
offer() {
|
|
584
|
+
const params = {};
|
|
585
|
+
if (this._options.serverNoContextTakeover) {
|
|
586
|
+
params.server_no_context_takeover = true;
|
|
587
|
+
}
|
|
588
|
+
if (this._options.clientNoContextTakeover) {
|
|
589
|
+
params.client_no_context_takeover = true;
|
|
590
|
+
}
|
|
591
|
+
if (this._options.serverMaxWindowBits) {
|
|
592
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
593
|
+
}
|
|
594
|
+
if (this._options.clientMaxWindowBits) {
|
|
595
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
596
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
597
|
+
params.client_max_window_bits = true;
|
|
598
|
+
}
|
|
599
|
+
return params;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Accept an extension negotiation offer/response.
|
|
603
|
+
*
|
|
604
|
+
* @param {Array} configurations The extension negotiation offers/reponse
|
|
605
|
+
* @return {Object} Accepted configuration
|
|
606
|
+
* @public
|
|
607
|
+
*/
|
|
608
|
+
accept(configurations) {
|
|
609
|
+
configurations = this.normalizeParams(configurations);
|
|
610
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
611
|
+
return this.params;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Releases all resources used by the extension.
|
|
615
|
+
*
|
|
616
|
+
* @public
|
|
617
|
+
*/
|
|
618
|
+
cleanup() {
|
|
619
|
+
if (this._inflate) {
|
|
620
|
+
this._inflate.close();
|
|
621
|
+
this._inflate = null;
|
|
622
|
+
}
|
|
623
|
+
if (this._deflate) {
|
|
624
|
+
const callback = this._deflate[kCallback];
|
|
625
|
+
this._deflate.close();
|
|
626
|
+
this._deflate = null;
|
|
627
|
+
if (callback) {
|
|
628
|
+
callback(
|
|
629
|
+
new Error(
|
|
630
|
+
"The deflate stream was closed while data was being processed"
|
|
631
|
+
)
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Accept an extension negotiation offer.
|
|
638
|
+
*
|
|
639
|
+
* @param {Array} offers The extension negotiation offers
|
|
640
|
+
* @return {Object} Accepted configuration
|
|
641
|
+
* @private
|
|
642
|
+
*/
|
|
643
|
+
acceptAsServer(offers) {
|
|
644
|
+
const opts = this._options;
|
|
645
|
+
const accepted = offers.find((params) => {
|
|
646
|
+
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) {
|
|
647
|
+
return false;
|
|
648
|
+
}
|
|
649
|
+
return true;
|
|
650
|
+
});
|
|
651
|
+
if (!accepted) {
|
|
652
|
+
throw new Error("None of the extension offers can be accepted");
|
|
653
|
+
}
|
|
654
|
+
if (opts.serverNoContextTakeover) {
|
|
655
|
+
accepted.server_no_context_takeover = true;
|
|
656
|
+
}
|
|
657
|
+
if (opts.clientNoContextTakeover) {
|
|
658
|
+
accepted.client_no_context_takeover = true;
|
|
659
|
+
}
|
|
660
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
661
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
662
|
+
}
|
|
663
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
664
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
665
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
666
|
+
delete accepted.client_max_window_bits;
|
|
667
|
+
}
|
|
668
|
+
return accepted;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Accept the extension negotiation response.
|
|
672
|
+
*
|
|
673
|
+
* @param {Array} response The extension negotiation response
|
|
674
|
+
* @return {Object} Accepted configuration
|
|
675
|
+
* @private
|
|
676
|
+
*/
|
|
677
|
+
acceptAsClient(response) {
|
|
678
|
+
const params = response[0];
|
|
679
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
680
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
681
|
+
}
|
|
682
|
+
if (!params.client_max_window_bits) {
|
|
683
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
684
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
685
|
+
}
|
|
686
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
687
|
+
throw new Error(
|
|
688
|
+
'Unexpected or invalid parameter "client_max_window_bits"'
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
return params;
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Normalize parameters.
|
|
695
|
+
*
|
|
696
|
+
* @param {Array} configurations The extension negotiation offers/reponse
|
|
697
|
+
* @return {Array} The offers/response with normalized parameters
|
|
698
|
+
* @private
|
|
699
|
+
*/
|
|
700
|
+
normalizeParams(configurations) {
|
|
701
|
+
configurations.forEach((params) => {
|
|
702
|
+
Object.keys(params).forEach((key) => {
|
|
703
|
+
let value = params[key];
|
|
704
|
+
if (value.length > 1) {
|
|
705
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
706
|
+
}
|
|
707
|
+
value = value[0];
|
|
708
|
+
if (key === "client_max_window_bits") {
|
|
709
|
+
if (value !== true) {
|
|
710
|
+
const num = +value;
|
|
711
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
712
|
+
throw new TypeError(
|
|
713
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
value = num;
|
|
717
|
+
} else if (!this._isServer) {
|
|
718
|
+
throw new TypeError(
|
|
719
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
} else if (key === "server_max_window_bits") {
|
|
723
|
+
const num = +value;
|
|
724
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
725
|
+
throw new TypeError(
|
|
726
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
value = num;
|
|
730
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
731
|
+
if (value !== true) {
|
|
732
|
+
throw new TypeError(
|
|
733
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
} else {
|
|
737
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
738
|
+
}
|
|
739
|
+
params[key] = value;
|
|
740
|
+
});
|
|
741
|
+
});
|
|
742
|
+
return configurations;
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Decompress data. Concurrency limited.
|
|
746
|
+
*
|
|
747
|
+
* @param {Buffer} data Compressed data
|
|
748
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
749
|
+
* @param {Function} callback Callback
|
|
750
|
+
* @public
|
|
751
|
+
*/
|
|
752
|
+
decompress(data, fin, callback) {
|
|
753
|
+
zlibLimiter.add((done) => {
|
|
754
|
+
this._decompress(data, fin, (err, result) => {
|
|
755
|
+
done();
|
|
756
|
+
callback(err, result);
|
|
757
|
+
});
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Compress data. Concurrency limited.
|
|
762
|
+
*
|
|
763
|
+
* @param {(Buffer|String)} data Data to compress
|
|
764
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
765
|
+
* @param {Function} callback Callback
|
|
766
|
+
* @public
|
|
767
|
+
*/
|
|
768
|
+
compress(data, fin, callback) {
|
|
769
|
+
zlibLimiter.add((done) => {
|
|
770
|
+
this._compress(data, fin, (err, result) => {
|
|
771
|
+
done();
|
|
772
|
+
callback(err, result);
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Decompress data.
|
|
778
|
+
*
|
|
779
|
+
* @param {Buffer} data Compressed data
|
|
780
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
781
|
+
* @param {Function} callback Callback
|
|
782
|
+
* @private
|
|
783
|
+
*/
|
|
784
|
+
_decompress(data, fin, callback) {
|
|
785
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
786
|
+
if (!this._inflate) {
|
|
787
|
+
const key = `${endpoint}_max_window_bits`;
|
|
788
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
789
|
+
this._inflate = zlib.createInflateRaw({
|
|
790
|
+
...this._options.zlibInflateOptions,
|
|
791
|
+
windowBits
|
|
792
|
+
});
|
|
793
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
794
|
+
this._inflate[kTotalLength] = 0;
|
|
795
|
+
this._inflate[kBuffers] = [];
|
|
796
|
+
this._inflate.on("error", inflateOnError);
|
|
797
|
+
this._inflate.on("data", inflateOnData);
|
|
798
|
+
}
|
|
799
|
+
this._inflate[kCallback] = callback;
|
|
800
|
+
this._inflate.write(data);
|
|
801
|
+
if (fin) this._inflate.write(TRAILER);
|
|
802
|
+
this._inflate.flush(() => {
|
|
803
|
+
const err = this._inflate[kError];
|
|
804
|
+
if (err) {
|
|
805
|
+
this._inflate.close();
|
|
806
|
+
this._inflate = null;
|
|
807
|
+
callback(err);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
const data2 = bufferUtil2.concat(
|
|
811
|
+
this._inflate[kBuffers],
|
|
812
|
+
this._inflate[kTotalLength]
|
|
813
|
+
);
|
|
814
|
+
if (this._inflate._readableState.endEmitted) {
|
|
815
|
+
this._inflate.close();
|
|
816
|
+
this._inflate = null;
|
|
817
|
+
} else {
|
|
818
|
+
this._inflate[kTotalLength] = 0;
|
|
819
|
+
this._inflate[kBuffers] = [];
|
|
820
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
821
|
+
this._inflate.reset();
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
callback(null, data2);
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Compress data.
|
|
829
|
+
*
|
|
830
|
+
* @param {(Buffer|String)} data Data to compress
|
|
831
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
832
|
+
* @param {Function} callback Callback
|
|
833
|
+
* @private
|
|
834
|
+
*/
|
|
835
|
+
_compress(data, fin, callback) {
|
|
836
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
837
|
+
if (!this._deflate) {
|
|
838
|
+
const key = `${endpoint}_max_window_bits`;
|
|
839
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
840
|
+
this._deflate = zlib.createDeflateRaw({
|
|
841
|
+
...this._options.zlibDeflateOptions,
|
|
842
|
+
windowBits
|
|
843
|
+
});
|
|
844
|
+
this._deflate[kTotalLength] = 0;
|
|
845
|
+
this._deflate[kBuffers] = [];
|
|
846
|
+
this._deflate.on("data", deflateOnData);
|
|
847
|
+
}
|
|
848
|
+
this._deflate[kCallback] = callback;
|
|
849
|
+
this._deflate.write(data);
|
|
850
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
851
|
+
if (!this._deflate) {
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
let data2 = bufferUtil2.concat(
|
|
855
|
+
this._deflate[kBuffers],
|
|
856
|
+
this._deflate[kTotalLength]
|
|
857
|
+
);
|
|
858
|
+
if (fin) {
|
|
859
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
860
|
+
}
|
|
861
|
+
this._deflate[kCallback] = null;
|
|
862
|
+
this._deflate[kTotalLength] = 0;
|
|
863
|
+
this._deflate[kBuffers] = [];
|
|
864
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
865
|
+
this._deflate.reset();
|
|
866
|
+
}
|
|
867
|
+
callback(null, data2);
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
permessageDeflate = PerMessageDeflate;
|
|
872
|
+
function deflateOnData(chunk) {
|
|
873
|
+
this[kBuffers].push(chunk);
|
|
874
|
+
this[kTotalLength] += chunk.length;
|
|
875
|
+
}
|
|
876
|
+
function inflateOnData(chunk) {
|
|
877
|
+
this[kTotalLength] += chunk.length;
|
|
878
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
879
|
+
this[kBuffers].push(chunk);
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
883
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
884
|
+
this[kError][kStatusCode] = 1009;
|
|
885
|
+
this.removeListener("data", inflateOnData);
|
|
886
|
+
this.reset();
|
|
887
|
+
}
|
|
888
|
+
function inflateOnError(err) {
|
|
889
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
890
|
+
if (this[kError]) {
|
|
891
|
+
this[kCallback](this[kError]);
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
err[kStatusCode] = 1007;
|
|
895
|
+
this[kCallback](err);
|
|
896
|
+
}
|
|
897
|
+
return permessageDeflate;
|
|
898
|
+
}
|
|
899
|
+
var validation = { exports: {} };
|
|
900
|
+
const __viteOptionalPeerDep_utf8Validate_ws = {};
|
|
901
|
+
const __viteOptionalPeerDep_utf8Validate_ws$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
902
|
+
__proto__: null,
|
|
903
|
+
default: __viteOptionalPeerDep_utf8Validate_ws
|
|
904
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
905
|
+
const require$$2 = /* @__PURE__ */ getAugmentedNamespace(__viteOptionalPeerDep_utf8Validate_ws$1);
|
|
906
|
+
var hasRequiredValidation;
|
|
907
|
+
function requireValidation() {
|
|
908
|
+
if (hasRequiredValidation) return validation.exports;
|
|
909
|
+
hasRequiredValidation = 1;
|
|
910
|
+
const { isUtf8 } = require$$0$1;
|
|
911
|
+
const { hasBlob } = requireConstants();
|
|
912
|
+
const tokenChars = [
|
|
913
|
+
0,
|
|
914
|
+
0,
|
|
915
|
+
0,
|
|
916
|
+
0,
|
|
917
|
+
0,
|
|
918
|
+
0,
|
|
919
|
+
0,
|
|
920
|
+
0,
|
|
921
|
+
0,
|
|
922
|
+
0,
|
|
923
|
+
0,
|
|
924
|
+
0,
|
|
925
|
+
0,
|
|
926
|
+
0,
|
|
927
|
+
0,
|
|
928
|
+
0,
|
|
929
|
+
// 0 - 15
|
|
930
|
+
0,
|
|
931
|
+
0,
|
|
932
|
+
0,
|
|
933
|
+
0,
|
|
934
|
+
0,
|
|
935
|
+
0,
|
|
936
|
+
0,
|
|
937
|
+
0,
|
|
938
|
+
0,
|
|
939
|
+
0,
|
|
940
|
+
0,
|
|
941
|
+
0,
|
|
942
|
+
0,
|
|
943
|
+
0,
|
|
944
|
+
0,
|
|
945
|
+
0,
|
|
946
|
+
// 16 - 31
|
|
947
|
+
0,
|
|
948
|
+
1,
|
|
949
|
+
0,
|
|
950
|
+
1,
|
|
951
|
+
1,
|
|
952
|
+
1,
|
|
953
|
+
1,
|
|
954
|
+
1,
|
|
955
|
+
0,
|
|
956
|
+
0,
|
|
957
|
+
1,
|
|
958
|
+
1,
|
|
959
|
+
0,
|
|
960
|
+
1,
|
|
961
|
+
1,
|
|
962
|
+
0,
|
|
963
|
+
// 32 - 47
|
|
964
|
+
1,
|
|
965
|
+
1,
|
|
966
|
+
1,
|
|
967
|
+
1,
|
|
968
|
+
1,
|
|
969
|
+
1,
|
|
970
|
+
1,
|
|
971
|
+
1,
|
|
972
|
+
1,
|
|
973
|
+
1,
|
|
974
|
+
0,
|
|
975
|
+
0,
|
|
976
|
+
0,
|
|
977
|
+
0,
|
|
978
|
+
0,
|
|
979
|
+
0,
|
|
980
|
+
// 48 - 63
|
|
981
|
+
0,
|
|
982
|
+
1,
|
|
983
|
+
1,
|
|
984
|
+
1,
|
|
985
|
+
1,
|
|
986
|
+
1,
|
|
987
|
+
1,
|
|
988
|
+
1,
|
|
989
|
+
1,
|
|
990
|
+
1,
|
|
991
|
+
1,
|
|
992
|
+
1,
|
|
993
|
+
1,
|
|
994
|
+
1,
|
|
995
|
+
1,
|
|
996
|
+
1,
|
|
997
|
+
// 64 - 79
|
|
998
|
+
1,
|
|
999
|
+
1,
|
|
1000
|
+
1,
|
|
1001
|
+
1,
|
|
1002
|
+
1,
|
|
1003
|
+
1,
|
|
1004
|
+
1,
|
|
1005
|
+
1,
|
|
1006
|
+
1,
|
|
1007
|
+
1,
|
|
1008
|
+
1,
|
|
1009
|
+
0,
|
|
1010
|
+
0,
|
|
1011
|
+
0,
|
|
1012
|
+
1,
|
|
1013
|
+
1,
|
|
1014
|
+
// 80 - 95
|
|
1015
|
+
1,
|
|
1016
|
+
1,
|
|
1017
|
+
1,
|
|
1018
|
+
1,
|
|
1019
|
+
1,
|
|
1020
|
+
1,
|
|
1021
|
+
1,
|
|
1022
|
+
1,
|
|
1023
|
+
1,
|
|
1024
|
+
1,
|
|
1025
|
+
1,
|
|
1026
|
+
1,
|
|
1027
|
+
1,
|
|
1028
|
+
1,
|
|
1029
|
+
1,
|
|
1030
|
+
1,
|
|
1031
|
+
// 96 - 111
|
|
1032
|
+
1,
|
|
1033
|
+
1,
|
|
1034
|
+
1,
|
|
1035
|
+
1,
|
|
1036
|
+
1,
|
|
1037
|
+
1,
|
|
1038
|
+
1,
|
|
1039
|
+
1,
|
|
1040
|
+
1,
|
|
1041
|
+
1,
|
|
1042
|
+
1,
|
|
1043
|
+
0,
|
|
1044
|
+
1,
|
|
1045
|
+
0,
|
|
1046
|
+
1,
|
|
1047
|
+
0
|
|
1048
|
+
// 112 - 127
|
|
1049
|
+
];
|
|
1050
|
+
function isValidStatusCode(code) {
|
|
1051
|
+
return code >= 1e3 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3e3 && code <= 4999;
|
|
1052
|
+
}
|
|
1053
|
+
function _isValidUTF8(buf) {
|
|
1054
|
+
const len = buf.length;
|
|
1055
|
+
let i = 0;
|
|
1056
|
+
while (i < len) {
|
|
1057
|
+
if ((buf[i] & 128) === 0) {
|
|
1058
|
+
i++;
|
|
1059
|
+
} else if ((buf[i] & 224) === 192) {
|
|
1060
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) {
|
|
1061
|
+
return false;
|
|
1062
|
+
}
|
|
1063
|
+
i += 2;
|
|
1064
|
+
} else if ((buf[i] & 240) === 224) {
|
|
1065
|
+
if (i + 2 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || buf[i] === 224 && (buf[i + 1] & 224) === 128 || // Overlong
|
|
1066
|
+
buf[i] === 237 && (buf[i + 1] & 224) === 160) {
|
|
1067
|
+
return false;
|
|
1068
|
+
}
|
|
1069
|
+
i += 3;
|
|
1070
|
+
} else if ((buf[i] & 248) === 240) {
|
|
1071
|
+
if (i + 3 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || (buf[i + 3] & 192) !== 128 || buf[i] === 240 && (buf[i + 1] & 240) === 128 || // Overlong
|
|
1072
|
+
buf[i] === 244 && buf[i + 1] > 143 || buf[i] > 244) {
|
|
1073
|
+
return false;
|
|
1074
|
+
}
|
|
1075
|
+
i += 4;
|
|
1076
|
+
} else {
|
|
1077
|
+
return false;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
return true;
|
|
1081
|
+
}
|
|
1082
|
+
function isBlob(value) {
|
|
1083
|
+
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");
|
|
1084
|
+
}
|
|
1085
|
+
validation.exports = {
|
|
1086
|
+
isBlob,
|
|
1087
|
+
isValidStatusCode,
|
|
1088
|
+
isValidUTF8: _isValidUTF8,
|
|
1089
|
+
tokenChars
|
|
1090
|
+
};
|
|
1091
|
+
if (isUtf8) {
|
|
1092
|
+
validation.exports.isValidUTF8 = function(buf) {
|
|
1093
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
1094
|
+
};
|
|
1095
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
1096
|
+
try {
|
|
1097
|
+
const isValidUTF8 = require$$2;
|
|
1098
|
+
validation.exports.isValidUTF8 = function(buf) {
|
|
1099
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
1100
|
+
};
|
|
1101
|
+
} catch (e) {
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
return validation.exports;
|
|
1105
|
+
}
|
|
1106
|
+
var receiver;
|
|
1107
|
+
var hasRequiredReceiver;
|
|
1108
|
+
function requireReceiver() {
|
|
1109
|
+
if (hasRequiredReceiver) return receiver;
|
|
1110
|
+
hasRequiredReceiver = 1;
|
|
1111
|
+
const { Writable } = require$$0$2;
|
|
1112
|
+
const PerMessageDeflate = requirePermessageDeflate();
|
|
1113
|
+
const {
|
|
1114
|
+
BINARY_TYPES,
|
|
1115
|
+
EMPTY_BUFFER,
|
|
1116
|
+
kStatusCode,
|
|
1117
|
+
kWebSocket
|
|
1118
|
+
} = requireConstants();
|
|
1119
|
+
const { concat, toArrayBuffer, unmask } = requireBufferUtil();
|
|
1120
|
+
const { isValidStatusCode, isValidUTF8 } = requireValidation();
|
|
1121
|
+
const FastBuffer = Buffer[Symbol.species];
|
|
1122
|
+
const GET_INFO = 0;
|
|
1123
|
+
const GET_PAYLOAD_LENGTH_16 = 1;
|
|
1124
|
+
const GET_PAYLOAD_LENGTH_64 = 2;
|
|
1125
|
+
const GET_MASK = 3;
|
|
1126
|
+
const GET_DATA = 4;
|
|
1127
|
+
const INFLATING = 5;
|
|
1128
|
+
const DEFER_EVENT = 6;
|
|
1129
|
+
class Receiver extends Writable {
|
|
1130
|
+
/**
|
|
1131
|
+
* Creates a Receiver instance.
|
|
1132
|
+
*
|
|
1133
|
+
* @param {Object} [options] Options object
|
|
1134
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
|
|
1135
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
1136
|
+
* multiple times in the same tick
|
|
1137
|
+
* @param {String} [options.binaryType=nodebuffer] The type for binary data
|
|
1138
|
+
* @param {Object} [options.extensions] An object containing the negotiated
|
|
1139
|
+
* extensions
|
|
1140
|
+
* @param {Boolean} [options.isServer=false] Specifies whether to operate in
|
|
1141
|
+
* client or server mode
|
|
1142
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
|
1143
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
1144
|
+
* not to skip UTF-8 validation for text and close messages
|
|
1145
|
+
*/
|
|
1146
|
+
constructor(options = {}) {
|
|
1147
|
+
super();
|
|
1148
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== void 0 ? options.allowSynchronousEvents : true;
|
|
1149
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
1150
|
+
this._extensions = options.extensions || {};
|
|
1151
|
+
this._isServer = !!options.isServer;
|
|
1152
|
+
this._maxPayload = options.maxPayload | 0;
|
|
1153
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
1154
|
+
this[kWebSocket] = void 0;
|
|
1155
|
+
this._bufferedBytes = 0;
|
|
1156
|
+
this._buffers = [];
|
|
1157
|
+
this._compressed = false;
|
|
1158
|
+
this._payloadLength = 0;
|
|
1159
|
+
this._mask = void 0;
|
|
1160
|
+
this._fragmented = 0;
|
|
1161
|
+
this._masked = false;
|
|
1162
|
+
this._fin = false;
|
|
1163
|
+
this._opcode = 0;
|
|
1164
|
+
this._totalPayloadLength = 0;
|
|
1165
|
+
this._messageLength = 0;
|
|
1166
|
+
this._fragments = [];
|
|
1167
|
+
this._errored = false;
|
|
1168
|
+
this._loop = false;
|
|
1169
|
+
this._state = GET_INFO;
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Implements `Writable.prototype._write()`.
|
|
1173
|
+
*
|
|
1174
|
+
* @param {Buffer} chunk The chunk of data to write
|
|
1175
|
+
* @param {String} encoding The character encoding of `chunk`
|
|
1176
|
+
* @param {Function} cb Callback
|
|
1177
|
+
* @private
|
|
1178
|
+
*/
|
|
1179
|
+
_write(chunk, encoding, cb) {
|
|
1180
|
+
if (this._opcode === 8 && this._state == GET_INFO) return cb();
|
|
1181
|
+
this._bufferedBytes += chunk.length;
|
|
1182
|
+
this._buffers.push(chunk);
|
|
1183
|
+
this.startLoop(cb);
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* Consumes `n` bytes from the buffered data.
|
|
1187
|
+
*
|
|
1188
|
+
* @param {Number} n The number of bytes to consume
|
|
1189
|
+
* @return {Buffer} The consumed bytes
|
|
1190
|
+
* @private
|
|
1191
|
+
*/
|
|
1192
|
+
consume(n) {
|
|
1193
|
+
this._bufferedBytes -= n;
|
|
1194
|
+
if (n === this._buffers[0].length) return this._buffers.shift();
|
|
1195
|
+
if (n < this._buffers[0].length) {
|
|
1196
|
+
const buf = this._buffers[0];
|
|
1197
|
+
this._buffers[0] = new FastBuffer(
|
|
1198
|
+
buf.buffer,
|
|
1199
|
+
buf.byteOffset + n,
|
|
1200
|
+
buf.length - n
|
|
1201
|
+
);
|
|
1202
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
1203
|
+
}
|
|
1204
|
+
const dst = Buffer.allocUnsafe(n);
|
|
1205
|
+
do {
|
|
1206
|
+
const buf = this._buffers[0];
|
|
1207
|
+
const offset = dst.length - n;
|
|
1208
|
+
if (n >= buf.length) {
|
|
1209
|
+
dst.set(this._buffers.shift(), offset);
|
|
1210
|
+
} else {
|
|
1211
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
1212
|
+
this._buffers[0] = new FastBuffer(
|
|
1213
|
+
buf.buffer,
|
|
1214
|
+
buf.byteOffset + n,
|
|
1215
|
+
buf.length - n
|
|
1216
|
+
);
|
|
1217
|
+
}
|
|
1218
|
+
n -= buf.length;
|
|
1219
|
+
} while (n > 0);
|
|
1220
|
+
return dst;
|
|
1221
|
+
}
|
|
1222
|
+
/**
|
|
1223
|
+
* Starts the parsing loop.
|
|
1224
|
+
*
|
|
1225
|
+
* @param {Function} cb Callback
|
|
1226
|
+
* @private
|
|
1227
|
+
*/
|
|
1228
|
+
startLoop(cb) {
|
|
1229
|
+
this._loop = true;
|
|
1230
|
+
do {
|
|
1231
|
+
switch (this._state) {
|
|
1232
|
+
case GET_INFO:
|
|
1233
|
+
this.getInfo(cb);
|
|
1234
|
+
break;
|
|
1235
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
1236
|
+
this.getPayloadLength16(cb);
|
|
1237
|
+
break;
|
|
1238
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
1239
|
+
this.getPayloadLength64(cb);
|
|
1240
|
+
break;
|
|
1241
|
+
case GET_MASK:
|
|
1242
|
+
this.getMask();
|
|
1243
|
+
break;
|
|
1244
|
+
case GET_DATA:
|
|
1245
|
+
this.getData(cb);
|
|
1246
|
+
break;
|
|
1247
|
+
case INFLATING:
|
|
1248
|
+
case DEFER_EVENT:
|
|
1249
|
+
this._loop = false;
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
} while (this._loop);
|
|
1253
|
+
if (!this._errored) cb();
|
|
1254
|
+
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Reads the first two bytes of a frame.
|
|
1257
|
+
*
|
|
1258
|
+
* @param {Function} cb Callback
|
|
1259
|
+
* @private
|
|
1260
|
+
*/
|
|
1261
|
+
getInfo(cb) {
|
|
1262
|
+
if (this._bufferedBytes < 2) {
|
|
1263
|
+
this._loop = false;
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
1266
|
+
const buf = this.consume(2);
|
|
1267
|
+
if ((buf[0] & 48) !== 0) {
|
|
1268
|
+
const error = this.createError(
|
|
1269
|
+
RangeError,
|
|
1270
|
+
"RSV2 and RSV3 must be clear",
|
|
1271
|
+
true,
|
|
1272
|
+
1002,
|
|
1273
|
+
"WS_ERR_UNEXPECTED_RSV_2_3"
|
|
1274
|
+
);
|
|
1275
|
+
cb(error);
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
const compressed = (buf[0] & 64) === 64;
|
|
1279
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
1280
|
+
const error = this.createError(
|
|
1281
|
+
RangeError,
|
|
1282
|
+
"RSV1 must be clear",
|
|
1283
|
+
true,
|
|
1284
|
+
1002,
|
|
1285
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
1286
|
+
);
|
|
1287
|
+
cb(error);
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
this._fin = (buf[0] & 128) === 128;
|
|
1291
|
+
this._opcode = buf[0] & 15;
|
|
1292
|
+
this._payloadLength = buf[1] & 127;
|
|
1293
|
+
if (this._opcode === 0) {
|
|
1294
|
+
if (compressed) {
|
|
1295
|
+
const error = this.createError(
|
|
1296
|
+
RangeError,
|
|
1297
|
+
"RSV1 must be clear",
|
|
1298
|
+
true,
|
|
1299
|
+
1002,
|
|
1300
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
1301
|
+
);
|
|
1302
|
+
cb(error);
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
if (!this._fragmented) {
|
|
1306
|
+
const error = this.createError(
|
|
1307
|
+
RangeError,
|
|
1308
|
+
"invalid opcode 0",
|
|
1309
|
+
true,
|
|
1310
|
+
1002,
|
|
1311
|
+
"WS_ERR_INVALID_OPCODE"
|
|
1312
|
+
);
|
|
1313
|
+
cb(error);
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
this._opcode = this._fragmented;
|
|
1317
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
1318
|
+
if (this._fragmented) {
|
|
1319
|
+
const error = this.createError(
|
|
1320
|
+
RangeError,
|
|
1321
|
+
`invalid opcode ${this._opcode}`,
|
|
1322
|
+
true,
|
|
1323
|
+
1002,
|
|
1324
|
+
"WS_ERR_INVALID_OPCODE"
|
|
1325
|
+
);
|
|
1326
|
+
cb(error);
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
this._compressed = compressed;
|
|
1330
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
1331
|
+
if (!this._fin) {
|
|
1332
|
+
const error = this.createError(
|
|
1333
|
+
RangeError,
|
|
1334
|
+
"FIN must be set",
|
|
1335
|
+
true,
|
|
1336
|
+
1002,
|
|
1337
|
+
"WS_ERR_EXPECTED_FIN"
|
|
1338
|
+
);
|
|
1339
|
+
cb(error);
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
if (compressed) {
|
|
1343
|
+
const error = this.createError(
|
|
1344
|
+
RangeError,
|
|
1345
|
+
"RSV1 must be clear",
|
|
1346
|
+
true,
|
|
1347
|
+
1002,
|
|
1348
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
1349
|
+
);
|
|
1350
|
+
cb(error);
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
1354
|
+
const error = this.createError(
|
|
1355
|
+
RangeError,
|
|
1356
|
+
`invalid payload length ${this._payloadLength}`,
|
|
1357
|
+
true,
|
|
1358
|
+
1002,
|
|
1359
|
+
"WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH"
|
|
1360
|
+
);
|
|
1361
|
+
cb(error);
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
} else {
|
|
1365
|
+
const error = this.createError(
|
|
1366
|
+
RangeError,
|
|
1367
|
+
`invalid opcode ${this._opcode}`,
|
|
1368
|
+
true,
|
|
1369
|
+
1002,
|
|
1370
|
+
"WS_ERR_INVALID_OPCODE"
|
|
1371
|
+
);
|
|
1372
|
+
cb(error);
|
|
1373
|
+
return;
|
|
1374
|
+
}
|
|
1375
|
+
if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
|
|
1376
|
+
this._masked = (buf[1] & 128) === 128;
|
|
1377
|
+
if (this._isServer) {
|
|
1378
|
+
if (!this._masked) {
|
|
1379
|
+
const error = this.createError(
|
|
1380
|
+
RangeError,
|
|
1381
|
+
"MASK must be set",
|
|
1382
|
+
true,
|
|
1383
|
+
1002,
|
|
1384
|
+
"WS_ERR_EXPECTED_MASK"
|
|
1385
|
+
);
|
|
1386
|
+
cb(error);
|
|
1387
|
+
return;
|
|
1388
|
+
}
|
|
1389
|
+
} else if (this._masked) {
|
|
1390
|
+
const error = this.createError(
|
|
1391
|
+
RangeError,
|
|
1392
|
+
"MASK must be clear",
|
|
1393
|
+
true,
|
|
1394
|
+
1002,
|
|
1395
|
+
"WS_ERR_UNEXPECTED_MASK"
|
|
1396
|
+
);
|
|
1397
|
+
cb(error);
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
|
|
1401
|
+
else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
|
|
1402
|
+
else this.haveLength(cb);
|
|
1403
|
+
}
|
|
1404
|
+
/**
|
|
1405
|
+
* Gets extended payload length (7+16).
|
|
1406
|
+
*
|
|
1407
|
+
* @param {Function} cb Callback
|
|
1408
|
+
* @private
|
|
1409
|
+
*/
|
|
1410
|
+
getPayloadLength16(cb) {
|
|
1411
|
+
if (this._bufferedBytes < 2) {
|
|
1412
|
+
this._loop = false;
|
|
1413
|
+
return;
|
|
1414
|
+
}
|
|
1415
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
1416
|
+
this.haveLength(cb);
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Gets extended payload length (7+64).
|
|
1420
|
+
*
|
|
1421
|
+
* @param {Function} cb Callback
|
|
1422
|
+
* @private
|
|
1423
|
+
*/
|
|
1424
|
+
getPayloadLength64(cb) {
|
|
1425
|
+
if (this._bufferedBytes < 8) {
|
|
1426
|
+
this._loop = false;
|
|
1427
|
+
return;
|
|
1428
|
+
}
|
|
1429
|
+
const buf = this.consume(8);
|
|
1430
|
+
const num = buf.readUInt32BE(0);
|
|
1431
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
1432
|
+
const error = this.createError(
|
|
1433
|
+
RangeError,
|
|
1434
|
+
"Unsupported WebSocket frame: payload length > 2^53 - 1",
|
|
1435
|
+
false,
|
|
1436
|
+
1009,
|
|
1437
|
+
"WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH"
|
|
1438
|
+
);
|
|
1439
|
+
cb(error);
|
|
1440
|
+
return;
|
|
1441
|
+
}
|
|
1442
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
1443
|
+
this.haveLength(cb);
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Payload length has been read.
|
|
1447
|
+
*
|
|
1448
|
+
* @param {Function} cb Callback
|
|
1449
|
+
* @private
|
|
1450
|
+
*/
|
|
1451
|
+
haveLength(cb) {
|
|
1452
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
1453
|
+
this._totalPayloadLength += this._payloadLength;
|
|
1454
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
1455
|
+
const error = this.createError(
|
|
1456
|
+
RangeError,
|
|
1457
|
+
"Max payload size exceeded",
|
|
1458
|
+
false,
|
|
1459
|
+
1009,
|
|
1460
|
+
"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
|
|
1461
|
+
);
|
|
1462
|
+
cb(error);
|
|
1463
|
+
return;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
if (this._masked) this._state = GET_MASK;
|
|
1467
|
+
else this._state = GET_DATA;
|
|
1468
|
+
}
|
|
1469
|
+
/**
|
|
1470
|
+
* Reads mask bytes.
|
|
1471
|
+
*
|
|
1472
|
+
* @private
|
|
1473
|
+
*/
|
|
1474
|
+
getMask() {
|
|
1475
|
+
if (this._bufferedBytes < 4) {
|
|
1476
|
+
this._loop = false;
|
|
1477
|
+
return;
|
|
1478
|
+
}
|
|
1479
|
+
this._mask = this.consume(4);
|
|
1480
|
+
this._state = GET_DATA;
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* Reads data bytes.
|
|
1484
|
+
*
|
|
1485
|
+
* @param {Function} cb Callback
|
|
1486
|
+
* @private
|
|
1487
|
+
*/
|
|
1488
|
+
getData(cb) {
|
|
1489
|
+
let data = EMPTY_BUFFER;
|
|
1490
|
+
if (this._payloadLength) {
|
|
1491
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
1492
|
+
this._loop = false;
|
|
1493
|
+
return;
|
|
1494
|
+
}
|
|
1495
|
+
data = this.consume(this._payloadLength);
|
|
1496
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
1497
|
+
unmask(data, this._mask);
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
if (this._opcode > 7) {
|
|
1501
|
+
this.controlMessage(data, cb);
|
|
1502
|
+
return;
|
|
1503
|
+
}
|
|
1504
|
+
if (this._compressed) {
|
|
1505
|
+
this._state = INFLATING;
|
|
1506
|
+
this.decompress(data, cb);
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
if (data.length) {
|
|
1510
|
+
this._messageLength = this._totalPayloadLength;
|
|
1511
|
+
this._fragments.push(data);
|
|
1512
|
+
}
|
|
1513
|
+
this.dataMessage(cb);
|
|
1514
|
+
}
|
|
1515
|
+
/**
|
|
1516
|
+
* Decompresses data.
|
|
1517
|
+
*
|
|
1518
|
+
* @param {Buffer} data Compressed data
|
|
1519
|
+
* @param {Function} cb Callback
|
|
1520
|
+
* @private
|
|
1521
|
+
*/
|
|
1522
|
+
decompress(data, cb) {
|
|
1523
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1524
|
+
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
|
1525
|
+
if (err) return cb(err);
|
|
1526
|
+
if (buf.length) {
|
|
1527
|
+
this._messageLength += buf.length;
|
|
1528
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
1529
|
+
const error = this.createError(
|
|
1530
|
+
RangeError,
|
|
1531
|
+
"Max payload size exceeded",
|
|
1532
|
+
false,
|
|
1533
|
+
1009,
|
|
1534
|
+
"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
|
|
1535
|
+
);
|
|
1536
|
+
cb(error);
|
|
1537
|
+
return;
|
|
1538
|
+
}
|
|
1539
|
+
this._fragments.push(buf);
|
|
1540
|
+
}
|
|
1541
|
+
this.dataMessage(cb);
|
|
1542
|
+
if (this._state === GET_INFO) this.startLoop(cb);
|
|
1543
|
+
});
|
|
1544
|
+
}
|
|
1545
|
+
/**
|
|
1546
|
+
* Handles a data message.
|
|
1547
|
+
*
|
|
1548
|
+
* @param {Function} cb Callback
|
|
1549
|
+
* @private
|
|
1550
|
+
*/
|
|
1551
|
+
dataMessage(cb) {
|
|
1552
|
+
if (!this._fin) {
|
|
1553
|
+
this._state = GET_INFO;
|
|
1554
|
+
return;
|
|
1555
|
+
}
|
|
1556
|
+
const messageLength = this._messageLength;
|
|
1557
|
+
const fragments = this._fragments;
|
|
1558
|
+
this._totalPayloadLength = 0;
|
|
1559
|
+
this._messageLength = 0;
|
|
1560
|
+
this._fragmented = 0;
|
|
1561
|
+
this._fragments = [];
|
|
1562
|
+
if (this._opcode === 2) {
|
|
1563
|
+
let data;
|
|
1564
|
+
if (this._binaryType === "nodebuffer") {
|
|
1565
|
+
data = concat(fragments, messageLength);
|
|
1566
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
1567
|
+
data = toArrayBuffer(concat(fragments, messageLength));
|
|
1568
|
+
} else if (this._binaryType === "blob") {
|
|
1569
|
+
data = new Blob(fragments);
|
|
1570
|
+
} else {
|
|
1571
|
+
data = fragments;
|
|
1572
|
+
}
|
|
1573
|
+
if (this._allowSynchronousEvents) {
|
|
1574
|
+
this.emit("message", data, true);
|
|
1575
|
+
this._state = GET_INFO;
|
|
1576
|
+
} else {
|
|
1577
|
+
this._state = DEFER_EVENT;
|
|
1578
|
+
setImmediate(() => {
|
|
1579
|
+
this.emit("message", data, true);
|
|
1580
|
+
this._state = GET_INFO;
|
|
1581
|
+
this.startLoop(cb);
|
|
1582
|
+
});
|
|
1583
|
+
}
|
|
1584
|
+
} else {
|
|
1585
|
+
const buf = concat(fragments, messageLength);
|
|
1586
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
1587
|
+
const error = this.createError(
|
|
1588
|
+
Error,
|
|
1589
|
+
"invalid UTF-8 sequence",
|
|
1590
|
+
true,
|
|
1591
|
+
1007,
|
|
1592
|
+
"WS_ERR_INVALID_UTF8"
|
|
1593
|
+
);
|
|
1594
|
+
cb(error);
|
|
1595
|
+
return;
|
|
1596
|
+
}
|
|
1597
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
1598
|
+
this.emit("message", buf, false);
|
|
1599
|
+
this._state = GET_INFO;
|
|
1600
|
+
} else {
|
|
1601
|
+
this._state = DEFER_EVENT;
|
|
1602
|
+
setImmediate(() => {
|
|
1603
|
+
this.emit("message", buf, false);
|
|
1604
|
+
this._state = GET_INFO;
|
|
1605
|
+
this.startLoop(cb);
|
|
1606
|
+
});
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
/**
|
|
1611
|
+
* Handles a control message.
|
|
1612
|
+
*
|
|
1613
|
+
* @param {Buffer} data Data to handle
|
|
1614
|
+
* @return {(Error|RangeError|undefined)} A possible error
|
|
1615
|
+
* @private
|
|
1616
|
+
*/
|
|
1617
|
+
controlMessage(data, cb) {
|
|
1618
|
+
if (this._opcode === 8) {
|
|
1619
|
+
if (data.length === 0) {
|
|
1620
|
+
this._loop = false;
|
|
1621
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
1622
|
+
this.end();
|
|
1623
|
+
} else {
|
|
1624
|
+
const code = data.readUInt16BE(0);
|
|
1625
|
+
if (!isValidStatusCode(code)) {
|
|
1626
|
+
const error = this.createError(
|
|
1627
|
+
RangeError,
|
|
1628
|
+
`invalid status code ${code}`,
|
|
1629
|
+
true,
|
|
1630
|
+
1002,
|
|
1631
|
+
"WS_ERR_INVALID_CLOSE_CODE"
|
|
1632
|
+
);
|
|
1633
|
+
cb(error);
|
|
1634
|
+
return;
|
|
1635
|
+
}
|
|
1636
|
+
const buf = new FastBuffer(
|
|
1637
|
+
data.buffer,
|
|
1638
|
+
data.byteOffset + 2,
|
|
1639
|
+
data.length - 2
|
|
1640
|
+
);
|
|
1641
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
1642
|
+
const error = this.createError(
|
|
1643
|
+
Error,
|
|
1644
|
+
"invalid UTF-8 sequence",
|
|
1645
|
+
true,
|
|
1646
|
+
1007,
|
|
1647
|
+
"WS_ERR_INVALID_UTF8"
|
|
1648
|
+
);
|
|
1649
|
+
cb(error);
|
|
1650
|
+
return;
|
|
1651
|
+
}
|
|
1652
|
+
this._loop = false;
|
|
1653
|
+
this.emit("conclude", code, buf);
|
|
1654
|
+
this.end();
|
|
1655
|
+
}
|
|
1656
|
+
this._state = GET_INFO;
|
|
1657
|
+
return;
|
|
1658
|
+
}
|
|
1659
|
+
if (this._allowSynchronousEvents) {
|
|
1660
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
1661
|
+
this._state = GET_INFO;
|
|
1662
|
+
} else {
|
|
1663
|
+
this._state = DEFER_EVENT;
|
|
1664
|
+
setImmediate(() => {
|
|
1665
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
1666
|
+
this._state = GET_INFO;
|
|
1667
|
+
this.startLoop(cb);
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1672
|
+
* Builds an error object.
|
|
1673
|
+
*
|
|
1674
|
+
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
|
|
1675
|
+
* @param {String} message The error message
|
|
1676
|
+
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
|
|
1677
|
+
* `message`
|
|
1678
|
+
* @param {Number} statusCode The status code
|
|
1679
|
+
* @param {String} errorCode The exposed error code
|
|
1680
|
+
* @return {(Error|RangeError)} The error
|
|
1681
|
+
* @private
|
|
1682
|
+
*/
|
|
1683
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
1684
|
+
this._loop = false;
|
|
1685
|
+
this._errored = true;
|
|
1686
|
+
const err = new ErrorCtor(
|
|
1687
|
+
prefix ? `Invalid WebSocket frame: ${message}` : message
|
|
1688
|
+
);
|
|
1689
|
+
Error.captureStackTrace(err, this.createError);
|
|
1690
|
+
err.code = errorCode;
|
|
1691
|
+
err[kStatusCode] = statusCode;
|
|
1692
|
+
return err;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
receiver = Receiver;
|
|
1696
|
+
return receiver;
|
|
1697
|
+
}
|
|
1698
|
+
var sender;
|
|
1699
|
+
var hasRequiredSender;
|
|
1700
|
+
function requireSender() {
|
|
1701
|
+
if (hasRequiredSender) return sender;
|
|
1702
|
+
hasRequiredSender = 1;
|
|
1703
|
+
const { Duplex } = require$$0$2;
|
|
1704
|
+
const { randomFillSync } = require$$1$1;
|
|
1705
|
+
const PerMessageDeflate = requirePermessageDeflate();
|
|
1706
|
+
const { EMPTY_BUFFER, kWebSocket, NOOP } = requireConstants();
|
|
1707
|
+
const { isBlob, isValidStatusCode } = requireValidation();
|
|
1708
|
+
const { mask: applyMask, toBuffer } = requireBufferUtil();
|
|
1709
|
+
const kByteLength = Symbol("kByteLength");
|
|
1710
|
+
const maskBuffer = Buffer.alloc(4);
|
|
1711
|
+
const RANDOM_POOL_SIZE = 8 * 1024;
|
|
1712
|
+
let randomPool;
|
|
1713
|
+
let randomPoolPointer = RANDOM_POOL_SIZE;
|
|
1714
|
+
const DEFAULT = 0;
|
|
1715
|
+
const DEFLATING = 1;
|
|
1716
|
+
const GET_BLOB_DATA = 2;
|
|
1717
|
+
class Sender {
|
|
1718
|
+
/**
|
|
1719
|
+
* Creates a Sender instance.
|
|
1720
|
+
*
|
|
1721
|
+
* @param {Duplex} socket The connection socket
|
|
1722
|
+
* @param {Object} [extensions] An object containing the negotiated extensions
|
|
1723
|
+
* @param {Function} [generateMask] The function used to generate the masking
|
|
1724
|
+
* key
|
|
1725
|
+
*/
|
|
1726
|
+
constructor(socket, extensions, generateMask) {
|
|
1727
|
+
this._extensions = extensions || {};
|
|
1728
|
+
if (generateMask) {
|
|
1729
|
+
this._generateMask = generateMask;
|
|
1730
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
1731
|
+
}
|
|
1732
|
+
this._socket = socket;
|
|
1733
|
+
this._firstFragment = true;
|
|
1734
|
+
this._compress = false;
|
|
1735
|
+
this._bufferedBytes = 0;
|
|
1736
|
+
this._queue = [];
|
|
1737
|
+
this._state = DEFAULT;
|
|
1738
|
+
this.onerror = NOOP;
|
|
1739
|
+
this[kWebSocket] = void 0;
|
|
1740
|
+
}
|
|
1741
|
+
/**
|
|
1742
|
+
* Frames a piece of data according to the HyBi WebSocket protocol.
|
|
1743
|
+
*
|
|
1744
|
+
* @param {(Buffer|String)} data The data to frame
|
|
1745
|
+
* @param {Object} options Options object
|
|
1746
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
1747
|
+
* FIN bit
|
|
1748
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
1749
|
+
* masking key
|
|
1750
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1751
|
+
* `data`
|
|
1752
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
1753
|
+
* key
|
|
1754
|
+
* @param {Number} options.opcode The opcode
|
|
1755
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
1756
|
+
* modified
|
|
1757
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
1758
|
+
* RSV1 bit
|
|
1759
|
+
* @return {(Buffer|String)[]} The framed data
|
|
1760
|
+
* @public
|
|
1761
|
+
*/
|
|
1762
|
+
static frame(data, options) {
|
|
1763
|
+
let mask;
|
|
1764
|
+
let merge = false;
|
|
1765
|
+
let offset = 2;
|
|
1766
|
+
let skipMasking = false;
|
|
1767
|
+
if (options.mask) {
|
|
1768
|
+
mask = options.maskBuffer || maskBuffer;
|
|
1769
|
+
if (options.generateMask) {
|
|
1770
|
+
options.generateMask(mask);
|
|
1771
|
+
} else {
|
|
1772
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
1773
|
+
if (randomPool === void 0) {
|
|
1774
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
1775
|
+
}
|
|
1776
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
1777
|
+
randomPoolPointer = 0;
|
|
1778
|
+
}
|
|
1779
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
1780
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
1781
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
1782
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
1783
|
+
}
|
|
1784
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
1785
|
+
offset = 6;
|
|
1786
|
+
}
|
|
1787
|
+
let dataLength;
|
|
1788
|
+
if (typeof data === "string") {
|
|
1789
|
+
if ((!options.mask || skipMasking) && options[kByteLength] !== void 0) {
|
|
1790
|
+
dataLength = options[kByteLength];
|
|
1791
|
+
} else {
|
|
1792
|
+
data = Buffer.from(data);
|
|
1793
|
+
dataLength = data.length;
|
|
1794
|
+
}
|
|
1795
|
+
} else {
|
|
1796
|
+
dataLength = data.length;
|
|
1797
|
+
merge = options.mask && options.readOnly && !skipMasking;
|
|
1798
|
+
}
|
|
1799
|
+
let payloadLength = dataLength;
|
|
1800
|
+
if (dataLength >= 65536) {
|
|
1801
|
+
offset += 8;
|
|
1802
|
+
payloadLength = 127;
|
|
1803
|
+
} else if (dataLength > 125) {
|
|
1804
|
+
offset += 2;
|
|
1805
|
+
payloadLength = 126;
|
|
1806
|
+
}
|
|
1807
|
+
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
|
1808
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
1809
|
+
if (options.rsv1) target[0] |= 64;
|
|
1810
|
+
target[1] = payloadLength;
|
|
1811
|
+
if (payloadLength === 126) {
|
|
1812
|
+
target.writeUInt16BE(dataLength, 2);
|
|
1813
|
+
} else if (payloadLength === 127) {
|
|
1814
|
+
target[2] = target[3] = 0;
|
|
1815
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
1816
|
+
}
|
|
1817
|
+
if (!options.mask) return [target, data];
|
|
1818
|
+
target[1] |= 128;
|
|
1819
|
+
target[offset - 4] = mask[0];
|
|
1820
|
+
target[offset - 3] = mask[1];
|
|
1821
|
+
target[offset - 2] = mask[2];
|
|
1822
|
+
target[offset - 1] = mask[3];
|
|
1823
|
+
if (skipMasking) return [target, data];
|
|
1824
|
+
if (merge) {
|
|
1825
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
1826
|
+
return [target];
|
|
1827
|
+
}
|
|
1828
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
1829
|
+
return [target, data];
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* Sends a close message to the other peer.
|
|
1833
|
+
*
|
|
1834
|
+
* @param {Number} [code] The status code component of the body
|
|
1835
|
+
* @param {(String|Buffer)} [data] The message component of the body
|
|
1836
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
|
|
1837
|
+
* @param {Function} [cb] Callback
|
|
1838
|
+
* @public
|
|
1839
|
+
*/
|
|
1840
|
+
close(code, data, mask, cb) {
|
|
1841
|
+
let buf;
|
|
1842
|
+
if (code === void 0) {
|
|
1843
|
+
buf = EMPTY_BUFFER;
|
|
1844
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
1845
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
1846
|
+
} else if (data === void 0 || !data.length) {
|
|
1847
|
+
buf = Buffer.allocUnsafe(2);
|
|
1848
|
+
buf.writeUInt16BE(code, 0);
|
|
1849
|
+
} else {
|
|
1850
|
+
const length = Buffer.byteLength(data);
|
|
1851
|
+
if (length > 123) {
|
|
1852
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
1853
|
+
}
|
|
1854
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
1855
|
+
buf.writeUInt16BE(code, 0);
|
|
1856
|
+
if (typeof data === "string") {
|
|
1857
|
+
buf.write(data, 2);
|
|
1858
|
+
} else {
|
|
1859
|
+
buf.set(data, 2);
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
const options = {
|
|
1863
|
+
[kByteLength]: buf.length,
|
|
1864
|
+
fin: true,
|
|
1865
|
+
generateMask: this._generateMask,
|
|
1866
|
+
mask,
|
|
1867
|
+
maskBuffer: this._maskBuffer,
|
|
1868
|
+
opcode: 8,
|
|
1869
|
+
readOnly: false,
|
|
1870
|
+
rsv1: false
|
|
1871
|
+
};
|
|
1872
|
+
if (this._state !== DEFAULT) {
|
|
1873
|
+
this.enqueue([this.dispatch, buf, false, options, cb]);
|
|
1874
|
+
} else {
|
|
1875
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
/**
|
|
1879
|
+
* Sends a ping message to the other peer.
|
|
1880
|
+
*
|
|
1881
|
+
* @param {*} data The message to send
|
|
1882
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
|
1883
|
+
* @param {Function} [cb] Callback
|
|
1884
|
+
* @public
|
|
1885
|
+
*/
|
|
1886
|
+
ping(data, mask, cb) {
|
|
1887
|
+
let byteLength;
|
|
1888
|
+
let readOnly;
|
|
1889
|
+
if (typeof data === "string") {
|
|
1890
|
+
byteLength = Buffer.byteLength(data);
|
|
1891
|
+
readOnly = false;
|
|
1892
|
+
} else if (isBlob(data)) {
|
|
1893
|
+
byteLength = data.size;
|
|
1894
|
+
readOnly = false;
|
|
1895
|
+
} else {
|
|
1896
|
+
data = toBuffer(data);
|
|
1897
|
+
byteLength = data.length;
|
|
1898
|
+
readOnly = toBuffer.readOnly;
|
|
1899
|
+
}
|
|
1900
|
+
if (byteLength > 125) {
|
|
1901
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
1902
|
+
}
|
|
1903
|
+
const options = {
|
|
1904
|
+
[kByteLength]: byteLength,
|
|
1905
|
+
fin: true,
|
|
1906
|
+
generateMask: this._generateMask,
|
|
1907
|
+
mask,
|
|
1908
|
+
maskBuffer: this._maskBuffer,
|
|
1909
|
+
opcode: 9,
|
|
1910
|
+
readOnly,
|
|
1911
|
+
rsv1: false
|
|
1912
|
+
};
|
|
1913
|
+
if (isBlob(data)) {
|
|
1914
|
+
if (this._state !== DEFAULT) {
|
|
1915
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
1916
|
+
} else {
|
|
1917
|
+
this.getBlobData(data, false, options, cb);
|
|
1918
|
+
}
|
|
1919
|
+
} else if (this._state !== DEFAULT) {
|
|
1920
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
1921
|
+
} else {
|
|
1922
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
/**
|
|
1926
|
+
* Sends a pong message to the other peer.
|
|
1927
|
+
*
|
|
1928
|
+
* @param {*} data The message to send
|
|
1929
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
|
1930
|
+
* @param {Function} [cb] Callback
|
|
1931
|
+
* @public
|
|
1932
|
+
*/
|
|
1933
|
+
pong(data, mask, cb) {
|
|
1934
|
+
let byteLength;
|
|
1935
|
+
let readOnly;
|
|
1936
|
+
if (typeof data === "string") {
|
|
1937
|
+
byteLength = Buffer.byteLength(data);
|
|
1938
|
+
readOnly = false;
|
|
1939
|
+
} else if (isBlob(data)) {
|
|
1940
|
+
byteLength = data.size;
|
|
1941
|
+
readOnly = false;
|
|
1942
|
+
} else {
|
|
1943
|
+
data = toBuffer(data);
|
|
1944
|
+
byteLength = data.length;
|
|
1945
|
+
readOnly = toBuffer.readOnly;
|
|
1946
|
+
}
|
|
1947
|
+
if (byteLength > 125) {
|
|
1948
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
1949
|
+
}
|
|
1950
|
+
const options = {
|
|
1951
|
+
[kByteLength]: byteLength,
|
|
1952
|
+
fin: true,
|
|
1953
|
+
generateMask: this._generateMask,
|
|
1954
|
+
mask,
|
|
1955
|
+
maskBuffer: this._maskBuffer,
|
|
1956
|
+
opcode: 10,
|
|
1957
|
+
readOnly,
|
|
1958
|
+
rsv1: false
|
|
1959
|
+
};
|
|
1960
|
+
if (isBlob(data)) {
|
|
1961
|
+
if (this._state !== DEFAULT) {
|
|
1962
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
1963
|
+
} else {
|
|
1964
|
+
this.getBlobData(data, false, options, cb);
|
|
1965
|
+
}
|
|
1966
|
+
} else if (this._state !== DEFAULT) {
|
|
1967
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
1968
|
+
} else {
|
|
1969
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
/**
|
|
1973
|
+
* Sends a data message to the other peer.
|
|
1974
|
+
*
|
|
1975
|
+
* @param {*} data The message to send
|
|
1976
|
+
* @param {Object} options Options object
|
|
1977
|
+
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
|
|
1978
|
+
* or text
|
|
1979
|
+
* @param {Boolean} [options.compress=false] Specifies whether or not to
|
|
1980
|
+
* compress `data`
|
|
1981
|
+
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
|
|
1982
|
+
* last one
|
|
1983
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1984
|
+
* `data`
|
|
1985
|
+
* @param {Function} [cb] Callback
|
|
1986
|
+
* @public
|
|
1987
|
+
*/
|
|
1988
|
+
send(data, options, cb) {
|
|
1989
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1990
|
+
let opcode = options.binary ? 2 : 1;
|
|
1991
|
+
let rsv1 = options.compress;
|
|
1992
|
+
let byteLength;
|
|
1993
|
+
let readOnly;
|
|
1994
|
+
if (typeof data === "string") {
|
|
1995
|
+
byteLength = Buffer.byteLength(data);
|
|
1996
|
+
readOnly = false;
|
|
1997
|
+
} else if (isBlob(data)) {
|
|
1998
|
+
byteLength = data.size;
|
|
1999
|
+
readOnly = false;
|
|
2000
|
+
} else {
|
|
2001
|
+
data = toBuffer(data);
|
|
2002
|
+
byteLength = data.length;
|
|
2003
|
+
readOnly = toBuffer.readOnly;
|
|
2004
|
+
}
|
|
2005
|
+
if (this._firstFragment) {
|
|
2006
|
+
this._firstFragment = false;
|
|
2007
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
2008
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
2009
|
+
}
|
|
2010
|
+
this._compress = rsv1;
|
|
2011
|
+
} else {
|
|
2012
|
+
rsv1 = false;
|
|
2013
|
+
opcode = 0;
|
|
2014
|
+
}
|
|
2015
|
+
if (options.fin) this._firstFragment = true;
|
|
2016
|
+
const opts = {
|
|
2017
|
+
[kByteLength]: byteLength,
|
|
2018
|
+
fin: options.fin,
|
|
2019
|
+
generateMask: this._generateMask,
|
|
2020
|
+
mask: options.mask,
|
|
2021
|
+
maskBuffer: this._maskBuffer,
|
|
2022
|
+
opcode,
|
|
2023
|
+
readOnly,
|
|
2024
|
+
rsv1
|
|
2025
|
+
};
|
|
2026
|
+
if (isBlob(data)) {
|
|
2027
|
+
if (this._state !== DEFAULT) {
|
|
2028
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
2029
|
+
} else {
|
|
2030
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
2031
|
+
}
|
|
2032
|
+
} else if (this._state !== DEFAULT) {
|
|
2033
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
2034
|
+
} else {
|
|
2035
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
/**
|
|
2039
|
+
* Gets the contents of a blob as binary data.
|
|
2040
|
+
*
|
|
2041
|
+
* @param {Blob} blob The blob
|
|
2042
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
|
2043
|
+
* the data
|
|
2044
|
+
* @param {Object} options Options object
|
|
2045
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
2046
|
+
* FIN bit
|
|
2047
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
2048
|
+
* masking key
|
|
2049
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
2050
|
+
* `data`
|
|
2051
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
2052
|
+
* key
|
|
2053
|
+
* @param {Number} options.opcode The opcode
|
|
2054
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
2055
|
+
* modified
|
|
2056
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
2057
|
+
* RSV1 bit
|
|
2058
|
+
* @param {Function} [cb] Callback
|
|
2059
|
+
* @private
|
|
2060
|
+
*/
|
|
2061
|
+
getBlobData(blob, compress, options, cb) {
|
|
2062
|
+
this._bufferedBytes += options[kByteLength];
|
|
2063
|
+
this._state = GET_BLOB_DATA;
|
|
2064
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
2065
|
+
if (this._socket.destroyed) {
|
|
2066
|
+
const err = new Error(
|
|
2067
|
+
"The socket was closed while the blob was being read"
|
|
2068
|
+
);
|
|
2069
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
2070
|
+
return;
|
|
2071
|
+
}
|
|
2072
|
+
this._bufferedBytes -= options[kByteLength];
|
|
2073
|
+
const data = toBuffer(arrayBuffer);
|
|
2074
|
+
if (!compress) {
|
|
2075
|
+
this._state = DEFAULT;
|
|
2076
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
2077
|
+
this.dequeue();
|
|
2078
|
+
} else {
|
|
2079
|
+
this.dispatch(data, compress, options, cb);
|
|
2080
|
+
}
|
|
2081
|
+
}).catch((err) => {
|
|
2082
|
+
process.nextTick(onError, this, err, cb);
|
|
2083
|
+
});
|
|
2084
|
+
}
|
|
2085
|
+
/**
|
|
2086
|
+
* Dispatches a message.
|
|
2087
|
+
*
|
|
2088
|
+
* @param {(Buffer|String)} data The message to send
|
|
2089
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
|
2090
|
+
* `data`
|
|
2091
|
+
* @param {Object} options Options object
|
|
2092
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
2093
|
+
* FIN bit
|
|
2094
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
2095
|
+
* masking key
|
|
2096
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
2097
|
+
* `data`
|
|
2098
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
2099
|
+
* key
|
|
2100
|
+
* @param {Number} options.opcode The opcode
|
|
2101
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
2102
|
+
* modified
|
|
2103
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
2104
|
+
* RSV1 bit
|
|
2105
|
+
* @param {Function} [cb] Callback
|
|
2106
|
+
* @private
|
|
2107
|
+
*/
|
|
2108
|
+
dispatch(data, compress, options, cb) {
|
|
2109
|
+
if (!compress) {
|
|
2110
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
2111
|
+
return;
|
|
2112
|
+
}
|
|
2113
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
2114
|
+
this._bufferedBytes += options[kByteLength];
|
|
2115
|
+
this._state = DEFLATING;
|
|
2116
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
|
2117
|
+
if (this._socket.destroyed) {
|
|
2118
|
+
const err = new Error(
|
|
2119
|
+
"The socket was closed while data was being compressed"
|
|
2120
|
+
);
|
|
2121
|
+
callCallbacks(this, err, cb);
|
|
2122
|
+
return;
|
|
2123
|
+
}
|
|
2124
|
+
this._bufferedBytes -= options[kByteLength];
|
|
2125
|
+
this._state = DEFAULT;
|
|
2126
|
+
options.readOnly = false;
|
|
2127
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
2128
|
+
this.dequeue();
|
|
2129
|
+
});
|
|
2130
|
+
}
|
|
2131
|
+
/**
|
|
2132
|
+
* Executes queued send operations.
|
|
2133
|
+
*
|
|
2134
|
+
* @private
|
|
2135
|
+
*/
|
|
2136
|
+
dequeue() {
|
|
2137
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
2138
|
+
const params = this._queue.shift();
|
|
2139
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
2140
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
/**
|
|
2144
|
+
* Enqueues a send operation.
|
|
2145
|
+
*
|
|
2146
|
+
* @param {Array} params Send operation parameters.
|
|
2147
|
+
* @private
|
|
2148
|
+
*/
|
|
2149
|
+
enqueue(params) {
|
|
2150
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
2151
|
+
this._queue.push(params);
|
|
2152
|
+
}
|
|
2153
|
+
/**
|
|
2154
|
+
* Sends a frame.
|
|
2155
|
+
*
|
|
2156
|
+
* @param {(Buffer | String)[]} list The frame to send
|
|
2157
|
+
* @param {Function} [cb] Callback
|
|
2158
|
+
* @private
|
|
2159
|
+
*/
|
|
2160
|
+
sendFrame(list, cb) {
|
|
2161
|
+
if (list.length === 2) {
|
|
2162
|
+
this._socket.cork();
|
|
2163
|
+
this._socket.write(list[0]);
|
|
2164
|
+
this._socket.write(list[1], cb);
|
|
2165
|
+
this._socket.uncork();
|
|
2166
|
+
} else {
|
|
2167
|
+
this._socket.write(list[0], cb);
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
sender = Sender;
|
|
2172
|
+
function callCallbacks(sender2, err, cb) {
|
|
2173
|
+
if (typeof cb === "function") cb(err);
|
|
2174
|
+
for (let i = 0; i < sender2._queue.length; i++) {
|
|
2175
|
+
const params = sender2._queue[i];
|
|
2176
|
+
const callback = params[params.length - 1];
|
|
2177
|
+
if (typeof callback === "function") callback(err);
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
function onError(sender2, err, cb) {
|
|
2181
|
+
callCallbacks(sender2, err, cb);
|
|
2182
|
+
sender2.onerror(err);
|
|
2183
|
+
}
|
|
2184
|
+
return sender;
|
|
2185
|
+
}
|
|
2186
|
+
var eventTarget;
|
|
2187
|
+
var hasRequiredEventTarget;
|
|
2188
|
+
function requireEventTarget() {
|
|
2189
|
+
if (hasRequiredEventTarget) return eventTarget;
|
|
2190
|
+
hasRequiredEventTarget = 1;
|
|
2191
|
+
const { kForOnEventAttribute, kListener } = requireConstants();
|
|
2192
|
+
const kCode = Symbol("kCode");
|
|
2193
|
+
const kData = Symbol("kData");
|
|
2194
|
+
const kError = Symbol("kError");
|
|
2195
|
+
const kMessage = Symbol("kMessage");
|
|
2196
|
+
const kReason = Symbol("kReason");
|
|
2197
|
+
const kTarget = Symbol("kTarget");
|
|
2198
|
+
const kType = Symbol("kType");
|
|
2199
|
+
const kWasClean = Symbol("kWasClean");
|
|
2200
|
+
class Event {
|
|
2201
|
+
/**
|
|
2202
|
+
* Create a new `Event`.
|
|
2203
|
+
*
|
|
2204
|
+
* @param {String} type The name of the event
|
|
2205
|
+
* @throws {TypeError} If the `type` argument is not specified
|
|
2206
|
+
*/
|
|
2207
|
+
constructor(type) {
|
|
2208
|
+
this[kTarget] = null;
|
|
2209
|
+
this[kType] = type;
|
|
2210
|
+
}
|
|
2211
|
+
/**
|
|
2212
|
+
* @type {*}
|
|
2213
|
+
*/
|
|
2214
|
+
get target() {
|
|
2215
|
+
return this[kTarget];
|
|
2216
|
+
}
|
|
2217
|
+
/**
|
|
2218
|
+
* @type {String}
|
|
2219
|
+
*/
|
|
2220
|
+
get type() {
|
|
2221
|
+
return this[kType];
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
2225
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
2226
|
+
class CloseEvent extends Event {
|
|
2227
|
+
/**
|
|
2228
|
+
* Create a new `CloseEvent`.
|
|
2229
|
+
*
|
|
2230
|
+
* @param {String} type The name of the event
|
|
2231
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
2232
|
+
* attributes via object members of the same name
|
|
2233
|
+
* @param {Number} [options.code=0] The status code explaining why the
|
|
2234
|
+
* connection was closed
|
|
2235
|
+
* @param {String} [options.reason=''] A human-readable string explaining why
|
|
2236
|
+
* the connection was closed
|
|
2237
|
+
* @param {Boolean} [options.wasClean=false] Indicates whether or not the
|
|
2238
|
+
* connection was cleanly closed
|
|
2239
|
+
*/
|
|
2240
|
+
constructor(type, options = {}) {
|
|
2241
|
+
super(type);
|
|
2242
|
+
this[kCode] = options.code === void 0 ? 0 : options.code;
|
|
2243
|
+
this[kReason] = options.reason === void 0 ? "" : options.reason;
|
|
2244
|
+
this[kWasClean] = options.wasClean === void 0 ? false : options.wasClean;
|
|
2245
|
+
}
|
|
2246
|
+
/**
|
|
2247
|
+
* @type {Number}
|
|
2248
|
+
*/
|
|
2249
|
+
get code() {
|
|
2250
|
+
return this[kCode];
|
|
2251
|
+
}
|
|
2252
|
+
/**
|
|
2253
|
+
* @type {String}
|
|
2254
|
+
*/
|
|
2255
|
+
get reason() {
|
|
2256
|
+
return this[kReason];
|
|
2257
|
+
}
|
|
2258
|
+
/**
|
|
2259
|
+
* @type {Boolean}
|
|
2260
|
+
*/
|
|
2261
|
+
get wasClean() {
|
|
2262
|
+
return this[kWasClean];
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
2266
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
2267
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
2268
|
+
class ErrorEvent extends Event {
|
|
2269
|
+
/**
|
|
2270
|
+
* Create a new `ErrorEvent`.
|
|
2271
|
+
*
|
|
2272
|
+
* @param {String} type The name of the event
|
|
2273
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
2274
|
+
* attributes via object members of the same name
|
|
2275
|
+
* @param {*} [options.error=null] The error that generated this event
|
|
2276
|
+
* @param {String} [options.message=''] The error message
|
|
2277
|
+
*/
|
|
2278
|
+
constructor(type, options = {}) {
|
|
2279
|
+
super(type);
|
|
2280
|
+
this[kError] = options.error === void 0 ? null : options.error;
|
|
2281
|
+
this[kMessage] = options.message === void 0 ? "" : options.message;
|
|
2282
|
+
}
|
|
2283
|
+
/**
|
|
2284
|
+
* @type {*}
|
|
2285
|
+
*/
|
|
2286
|
+
get error() {
|
|
2287
|
+
return this[kError];
|
|
2288
|
+
}
|
|
2289
|
+
/**
|
|
2290
|
+
* @type {String}
|
|
2291
|
+
*/
|
|
2292
|
+
get message() {
|
|
2293
|
+
return this[kMessage];
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
2297
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
2298
|
+
class MessageEvent extends Event {
|
|
2299
|
+
/**
|
|
2300
|
+
* Create a new `MessageEvent`.
|
|
2301
|
+
*
|
|
2302
|
+
* @param {String} type The name of the event
|
|
2303
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
2304
|
+
* attributes via object members of the same name
|
|
2305
|
+
* @param {*} [options.data=null] The message content
|
|
2306
|
+
*/
|
|
2307
|
+
constructor(type, options = {}) {
|
|
2308
|
+
super(type);
|
|
2309
|
+
this[kData] = options.data === void 0 ? null : options.data;
|
|
2310
|
+
}
|
|
2311
|
+
/**
|
|
2312
|
+
* @type {*}
|
|
2313
|
+
*/
|
|
2314
|
+
get data() {
|
|
2315
|
+
return this[kData];
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
2319
|
+
const EventTarget = {
|
|
2320
|
+
/**
|
|
2321
|
+
* Register an event listener.
|
|
2322
|
+
*
|
|
2323
|
+
* @param {String} type A string representing the event type to listen for
|
|
2324
|
+
* @param {(Function|Object)} handler The listener to add
|
|
2325
|
+
* @param {Object} [options] An options object specifies characteristics about
|
|
2326
|
+
* the event listener
|
|
2327
|
+
* @param {Boolean} [options.once=false] A `Boolean` indicating that the
|
|
2328
|
+
* listener should be invoked at most once after being added. If `true`,
|
|
2329
|
+
* the listener would be automatically removed when invoked.
|
|
2330
|
+
* @public
|
|
2331
|
+
*/
|
|
2332
|
+
addEventListener(type, handler, options = {}) {
|
|
2333
|
+
for (const listener of this.listeners(type)) {
|
|
2334
|
+
if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
2335
|
+
return;
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
let wrapper;
|
|
2339
|
+
if (type === "message") {
|
|
2340
|
+
wrapper = function onMessage(data, isBinary) {
|
|
2341
|
+
const event = new MessageEvent("message", {
|
|
2342
|
+
data: isBinary ? data : data.toString()
|
|
2343
|
+
});
|
|
2344
|
+
event[kTarget] = this;
|
|
2345
|
+
callListener(handler, this, event);
|
|
2346
|
+
};
|
|
2347
|
+
} else if (type === "close") {
|
|
2348
|
+
wrapper = function onClose(code, message) {
|
|
2349
|
+
const event = new CloseEvent("close", {
|
|
2350
|
+
code,
|
|
2351
|
+
reason: message.toString(),
|
|
2352
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
2353
|
+
});
|
|
2354
|
+
event[kTarget] = this;
|
|
2355
|
+
callListener(handler, this, event);
|
|
2356
|
+
};
|
|
2357
|
+
} else if (type === "error") {
|
|
2358
|
+
wrapper = function onError(error) {
|
|
2359
|
+
const event = new ErrorEvent("error", {
|
|
2360
|
+
error,
|
|
2361
|
+
message: error.message
|
|
2362
|
+
});
|
|
2363
|
+
event[kTarget] = this;
|
|
2364
|
+
callListener(handler, this, event);
|
|
2365
|
+
};
|
|
2366
|
+
} else if (type === "open") {
|
|
2367
|
+
wrapper = function onOpen() {
|
|
2368
|
+
const event = new Event("open");
|
|
2369
|
+
event[kTarget] = this;
|
|
2370
|
+
callListener(handler, this, event);
|
|
2371
|
+
};
|
|
2372
|
+
} else {
|
|
2373
|
+
return;
|
|
2374
|
+
}
|
|
2375
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
2376
|
+
wrapper[kListener] = handler;
|
|
2377
|
+
if (options.once) {
|
|
2378
|
+
this.once(type, wrapper);
|
|
2379
|
+
} else {
|
|
2380
|
+
this.on(type, wrapper);
|
|
2381
|
+
}
|
|
2382
|
+
},
|
|
2383
|
+
/**
|
|
2384
|
+
* Remove an event listener.
|
|
2385
|
+
*
|
|
2386
|
+
* @param {String} type A string representing the event type to remove
|
|
2387
|
+
* @param {(Function|Object)} handler The listener to remove
|
|
2388
|
+
* @public
|
|
2389
|
+
*/
|
|
2390
|
+
removeEventListener(type, handler) {
|
|
2391
|
+
for (const listener of this.listeners(type)) {
|
|
2392
|
+
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
2393
|
+
this.removeListener(type, listener);
|
|
2394
|
+
break;
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
}
|
|
2398
|
+
};
|
|
2399
|
+
eventTarget = {
|
|
2400
|
+
CloseEvent,
|
|
2401
|
+
ErrorEvent,
|
|
2402
|
+
Event,
|
|
2403
|
+
EventTarget,
|
|
2404
|
+
MessageEvent
|
|
2405
|
+
};
|
|
2406
|
+
function callListener(listener, thisArg, event) {
|
|
2407
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
2408
|
+
listener.handleEvent.call(listener, event);
|
|
2409
|
+
} else {
|
|
2410
|
+
listener.call(thisArg, event);
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
return eventTarget;
|
|
2414
|
+
}
|
|
2415
|
+
var extension;
|
|
2416
|
+
var hasRequiredExtension;
|
|
2417
|
+
function requireExtension() {
|
|
2418
|
+
if (hasRequiredExtension) return extension;
|
|
2419
|
+
hasRequiredExtension = 1;
|
|
2420
|
+
const { tokenChars } = requireValidation();
|
|
2421
|
+
function push(dest, name, elem) {
|
|
2422
|
+
if (dest[name] === void 0) dest[name] = [elem];
|
|
2423
|
+
else dest[name].push(elem);
|
|
2424
|
+
}
|
|
2425
|
+
function parse(header) {
|
|
2426
|
+
const offers = /* @__PURE__ */ Object.create(null);
|
|
2427
|
+
let params = /* @__PURE__ */ Object.create(null);
|
|
2428
|
+
let mustUnescape = false;
|
|
2429
|
+
let isEscaping = false;
|
|
2430
|
+
let inQuotes = false;
|
|
2431
|
+
let extensionName;
|
|
2432
|
+
let paramName;
|
|
2433
|
+
let start = -1;
|
|
2434
|
+
let code = -1;
|
|
2435
|
+
let end = -1;
|
|
2436
|
+
let i = 0;
|
|
2437
|
+
for (; i < header.length; i++) {
|
|
2438
|
+
code = header.charCodeAt(i);
|
|
2439
|
+
if (extensionName === void 0) {
|
|
2440
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
2441
|
+
if (start === -1) start = i;
|
|
2442
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
2443
|
+
if (end === -1 && start !== -1) end = i;
|
|
2444
|
+
} else if (code === 59 || code === 44) {
|
|
2445
|
+
if (start === -1) {
|
|
2446
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2447
|
+
}
|
|
2448
|
+
if (end === -1) end = i;
|
|
2449
|
+
const name = header.slice(start, end);
|
|
2450
|
+
if (code === 44) {
|
|
2451
|
+
push(offers, name, params);
|
|
2452
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2453
|
+
} else {
|
|
2454
|
+
extensionName = name;
|
|
2455
|
+
}
|
|
2456
|
+
start = end = -1;
|
|
2457
|
+
} else {
|
|
2458
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2459
|
+
}
|
|
2460
|
+
} else if (paramName === void 0) {
|
|
2461
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
2462
|
+
if (start === -1) start = i;
|
|
2463
|
+
} else if (code === 32 || code === 9) {
|
|
2464
|
+
if (end === -1 && start !== -1) end = i;
|
|
2465
|
+
} else if (code === 59 || code === 44) {
|
|
2466
|
+
if (start === -1) {
|
|
2467
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2468
|
+
}
|
|
2469
|
+
if (end === -1) end = i;
|
|
2470
|
+
push(params, header.slice(start, end), true);
|
|
2471
|
+
if (code === 44) {
|
|
2472
|
+
push(offers, extensionName, params);
|
|
2473
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2474
|
+
extensionName = void 0;
|
|
2475
|
+
}
|
|
2476
|
+
start = end = -1;
|
|
2477
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
2478
|
+
paramName = header.slice(start, i);
|
|
2479
|
+
start = end = -1;
|
|
2480
|
+
} else {
|
|
2481
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2482
|
+
}
|
|
2483
|
+
} else {
|
|
2484
|
+
if (isEscaping) {
|
|
2485
|
+
if (tokenChars[code] !== 1) {
|
|
2486
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2487
|
+
}
|
|
2488
|
+
if (start === -1) start = i;
|
|
2489
|
+
else if (!mustUnescape) mustUnescape = true;
|
|
2490
|
+
isEscaping = false;
|
|
2491
|
+
} else if (inQuotes) {
|
|
2492
|
+
if (tokenChars[code] === 1) {
|
|
2493
|
+
if (start === -1) start = i;
|
|
2494
|
+
} else if (code === 34 && start !== -1) {
|
|
2495
|
+
inQuotes = false;
|
|
2496
|
+
end = i;
|
|
2497
|
+
} else if (code === 92) {
|
|
2498
|
+
isEscaping = true;
|
|
2499
|
+
} else {
|
|
2500
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2501
|
+
}
|
|
2502
|
+
} else if (code === 34 && header.charCodeAt(i - 1) === 61) {
|
|
2503
|
+
inQuotes = true;
|
|
2504
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
2505
|
+
if (start === -1) start = i;
|
|
2506
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
2507
|
+
if (end === -1) end = i;
|
|
2508
|
+
} else if (code === 59 || code === 44) {
|
|
2509
|
+
if (start === -1) {
|
|
2510
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2511
|
+
}
|
|
2512
|
+
if (end === -1) end = i;
|
|
2513
|
+
let value = header.slice(start, end);
|
|
2514
|
+
if (mustUnescape) {
|
|
2515
|
+
value = value.replace(/\\/g, "");
|
|
2516
|
+
mustUnescape = false;
|
|
2517
|
+
}
|
|
2518
|
+
push(params, paramName, value);
|
|
2519
|
+
if (code === 44) {
|
|
2520
|
+
push(offers, extensionName, params);
|
|
2521
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2522
|
+
extensionName = void 0;
|
|
2523
|
+
}
|
|
2524
|
+
paramName = void 0;
|
|
2525
|
+
start = end = -1;
|
|
2526
|
+
} else {
|
|
2527
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) {
|
|
2532
|
+
throw new SyntaxError("Unexpected end of input");
|
|
2533
|
+
}
|
|
2534
|
+
if (end === -1) end = i;
|
|
2535
|
+
const token = header.slice(start, end);
|
|
2536
|
+
if (extensionName === void 0) {
|
|
2537
|
+
push(offers, token, params);
|
|
2538
|
+
} else {
|
|
2539
|
+
if (paramName === void 0) {
|
|
2540
|
+
push(params, token, true);
|
|
2541
|
+
} else if (mustUnescape) {
|
|
2542
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
2543
|
+
} else {
|
|
2544
|
+
push(params, paramName, token);
|
|
2545
|
+
}
|
|
2546
|
+
push(offers, extensionName, params);
|
|
2547
|
+
}
|
|
2548
|
+
return offers;
|
|
2549
|
+
}
|
|
2550
|
+
function format(extensions) {
|
|
2551
|
+
return Object.keys(extensions).map((extension2) => {
|
|
2552
|
+
let configurations = extensions[extension2];
|
|
2553
|
+
if (!Array.isArray(configurations)) configurations = [configurations];
|
|
2554
|
+
return configurations.map((params) => {
|
|
2555
|
+
return [extension2].concat(
|
|
2556
|
+
Object.keys(params).map((k) => {
|
|
2557
|
+
let values = params[k];
|
|
2558
|
+
if (!Array.isArray(values)) values = [values];
|
|
2559
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
2560
|
+
})
|
|
2561
|
+
).join("; ");
|
|
2562
|
+
}).join(", ");
|
|
2563
|
+
}).join(", ");
|
|
2564
|
+
}
|
|
2565
|
+
extension = { format, parse };
|
|
2566
|
+
return extension;
|
|
2567
|
+
}
|
|
2568
|
+
var websocket;
|
|
2569
|
+
var hasRequiredWebsocket;
|
|
2570
|
+
function requireWebsocket() {
|
|
2571
|
+
if (hasRequiredWebsocket) return websocket;
|
|
2572
|
+
hasRequiredWebsocket = 1;
|
|
2573
|
+
const EventEmitter = require$$0$3;
|
|
2574
|
+
const https = require$$1$2;
|
|
2575
|
+
const http = require$$2$1;
|
|
2576
|
+
const net = require$$3;
|
|
2577
|
+
const tls = require$$4;
|
|
2578
|
+
const { randomBytes, createHash } = require$$1$1;
|
|
2579
|
+
const { Duplex, Readable } = require$$0$2;
|
|
2580
|
+
const { URL } = require$$7;
|
|
2581
|
+
const PerMessageDeflate = requirePermessageDeflate();
|
|
2582
|
+
const Receiver = requireReceiver();
|
|
2583
|
+
const Sender = requireSender();
|
|
2584
|
+
const { isBlob } = requireValidation();
|
|
2585
|
+
const {
|
|
2586
|
+
BINARY_TYPES,
|
|
2587
|
+
CLOSE_TIMEOUT,
|
|
2588
|
+
EMPTY_BUFFER,
|
|
2589
|
+
GUID,
|
|
2590
|
+
kForOnEventAttribute,
|
|
2591
|
+
kListener,
|
|
2592
|
+
kStatusCode,
|
|
2593
|
+
kWebSocket,
|
|
2594
|
+
NOOP
|
|
2595
|
+
} = requireConstants();
|
|
2596
|
+
const {
|
|
2597
|
+
EventTarget: { addEventListener, removeEventListener }
|
|
2598
|
+
} = requireEventTarget();
|
|
2599
|
+
const { format, parse } = requireExtension();
|
|
2600
|
+
const { toBuffer } = requireBufferUtil();
|
|
2601
|
+
const kAborted = Symbol("kAborted");
|
|
2602
|
+
const protocolVersions = [8, 13];
|
|
2603
|
+
const readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
2604
|
+
const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
2605
|
+
class WebSocket2 extends EventEmitter {
|
|
2606
|
+
/**
|
|
2607
|
+
* Create a new `WebSocket`.
|
|
2608
|
+
*
|
|
2609
|
+
* @param {(String|URL)} address The URL to which to connect
|
|
2610
|
+
* @param {(String|String[])} [protocols] The subprotocols
|
|
2611
|
+
* @param {Object} [options] Connection options
|
|
2612
|
+
*/
|
|
2613
|
+
constructor(address, protocols, options) {
|
|
2614
|
+
super();
|
|
2615
|
+
this._binaryType = BINARY_TYPES[0];
|
|
2616
|
+
this._closeCode = 1006;
|
|
2617
|
+
this._closeFrameReceived = false;
|
|
2618
|
+
this._closeFrameSent = false;
|
|
2619
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
2620
|
+
this._closeTimer = null;
|
|
2621
|
+
this._errorEmitted = false;
|
|
2622
|
+
this._extensions = {};
|
|
2623
|
+
this._paused = false;
|
|
2624
|
+
this._protocol = "";
|
|
2625
|
+
this._readyState = WebSocket2.CONNECTING;
|
|
2626
|
+
this._receiver = null;
|
|
2627
|
+
this._sender = null;
|
|
2628
|
+
this._socket = null;
|
|
2629
|
+
if (address !== null) {
|
|
2630
|
+
this._bufferedAmount = 0;
|
|
2631
|
+
this._isServer = false;
|
|
2632
|
+
this._redirects = 0;
|
|
2633
|
+
if (protocols === void 0) {
|
|
2634
|
+
protocols = [];
|
|
2635
|
+
} else if (!Array.isArray(protocols)) {
|
|
2636
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
2637
|
+
options = protocols;
|
|
2638
|
+
protocols = [];
|
|
2639
|
+
} else {
|
|
2640
|
+
protocols = [protocols];
|
|
2641
|
+
}
|
|
2642
|
+
}
|
|
2643
|
+
initAsClient(this, address, protocols, options);
|
|
2644
|
+
} else {
|
|
2645
|
+
this._autoPong = options.autoPong;
|
|
2646
|
+
this._closeTimeout = options.closeTimeout;
|
|
2647
|
+
this._isServer = true;
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
/**
|
|
2651
|
+
* For historical reasons, the custom "nodebuffer" type is used by the default
|
|
2652
|
+
* instead of "blob".
|
|
2653
|
+
*
|
|
2654
|
+
* @type {String}
|
|
2655
|
+
*/
|
|
2656
|
+
get binaryType() {
|
|
2657
|
+
return this._binaryType;
|
|
2658
|
+
}
|
|
2659
|
+
set binaryType(type) {
|
|
2660
|
+
if (!BINARY_TYPES.includes(type)) return;
|
|
2661
|
+
this._binaryType = type;
|
|
2662
|
+
if (this._receiver) this._receiver._binaryType = type;
|
|
2663
|
+
}
|
|
2664
|
+
/**
|
|
2665
|
+
* @type {Number}
|
|
2666
|
+
*/
|
|
2667
|
+
get bufferedAmount() {
|
|
2668
|
+
if (!this._socket) return this._bufferedAmount;
|
|
2669
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
2670
|
+
}
|
|
2671
|
+
/**
|
|
2672
|
+
* @type {String}
|
|
2673
|
+
*/
|
|
2674
|
+
get extensions() {
|
|
2675
|
+
return Object.keys(this._extensions).join();
|
|
2676
|
+
}
|
|
2677
|
+
/**
|
|
2678
|
+
* @type {Boolean}
|
|
2679
|
+
*/
|
|
2680
|
+
get isPaused() {
|
|
2681
|
+
return this._paused;
|
|
2682
|
+
}
|
|
2683
|
+
/**
|
|
2684
|
+
* @type {Function}
|
|
2685
|
+
*/
|
|
2686
|
+
/* istanbul ignore next */
|
|
2687
|
+
get onclose() {
|
|
2688
|
+
return null;
|
|
2689
|
+
}
|
|
2690
|
+
/**
|
|
2691
|
+
* @type {Function}
|
|
2692
|
+
*/
|
|
2693
|
+
/* istanbul ignore next */
|
|
2694
|
+
get onerror() {
|
|
2695
|
+
return null;
|
|
2696
|
+
}
|
|
2697
|
+
/**
|
|
2698
|
+
* @type {Function}
|
|
2699
|
+
*/
|
|
2700
|
+
/* istanbul ignore next */
|
|
2701
|
+
get onopen() {
|
|
2702
|
+
return null;
|
|
2703
|
+
}
|
|
2704
|
+
/**
|
|
2705
|
+
* @type {Function}
|
|
2706
|
+
*/
|
|
2707
|
+
/* istanbul ignore next */
|
|
2708
|
+
get onmessage() {
|
|
2709
|
+
return null;
|
|
2710
|
+
}
|
|
2711
|
+
/**
|
|
2712
|
+
* @type {String}
|
|
2713
|
+
*/
|
|
2714
|
+
get protocol() {
|
|
2715
|
+
return this._protocol;
|
|
2716
|
+
}
|
|
2717
|
+
/**
|
|
2718
|
+
* @type {Number}
|
|
2719
|
+
*/
|
|
2720
|
+
get readyState() {
|
|
2721
|
+
return this._readyState;
|
|
2722
|
+
}
|
|
2723
|
+
/**
|
|
2724
|
+
* @type {String}
|
|
2725
|
+
*/
|
|
2726
|
+
get url() {
|
|
2727
|
+
return this._url;
|
|
2728
|
+
}
|
|
2729
|
+
/**
|
|
2730
|
+
* Set up the socket and the internal resources.
|
|
2731
|
+
*
|
|
2732
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
2733
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
2734
|
+
* @param {Object} options Options object
|
|
2735
|
+
* @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
|
|
2736
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
2737
|
+
* multiple times in the same tick
|
|
2738
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
2739
|
+
* masking key
|
|
2740
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message size
|
|
2741
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
2742
|
+
* not to skip UTF-8 validation for text and close messages
|
|
2743
|
+
* @private
|
|
2744
|
+
*/
|
|
2745
|
+
setSocket(socket, head, options) {
|
|
2746
|
+
const receiver2 = new Receiver({
|
|
2747
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
2748
|
+
binaryType: this.binaryType,
|
|
2749
|
+
extensions: this._extensions,
|
|
2750
|
+
isServer: this._isServer,
|
|
2751
|
+
maxPayload: options.maxPayload,
|
|
2752
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
2753
|
+
});
|
|
2754
|
+
const sender2 = new Sender(socket, this._extensions, options.generateMask);
|
|
2755
|
+
this._receiver = receiver2;
|
|
2756
|
+
this._sender = sender2;
|
|
2757
|
+
this._socket = socket;
|
|
2758
|
+
receiver2[kWebSocket] = this;
|
|
2759
|
+
sender2[kWebSocket] = this;
|
|
2760
|
+
socket[kWebSocket] = this;
|
|
2761
|
+
receiver2.on("conclude", receiverOnConclude);
|
|
2762
|
+
receiver2.on("drain", receiverOnDrain);
|
|
2763
|
+
receiver2.on("error", receiverOnError);
|
|
2764
|
+
receiver2.on("message", receiverOnMessage);
|
|
2765
|
+
receiver2.on("ping", receiverOnPing);
|
|
2766
|
+
receiver2.on("pong", receiverOnPong);
|
|
2767
|
+
sender2.onerror = senderOnError;
|
|
2768
|
+
if (socket.setTimeout) socket.setTimeout(0);
|
|
2769
|
+
if (socket.setNoDelay) socket.setNoDelay();
|
|
2770
|
+
if (head.length > 0) socket.unshift(head);
|
|
2771
|
+
socket.on("close", socketOnClose);
|
|
2772
|
+
socket.on("data", socketOnData);
|
|
2773
|
+
socket.on("end", socketOnEnd);
|
|
2774
|
+
socket.on("error", socketOnError);
|
|
2775
|
+
this._readyState = WebSocket2.OPEN;
|
|
2776
|
+
this.emit("open");
|
|
2777
|
+
}
|
|
2778
|
+
/**
|
|
2779
|
+
* Emit the `'close'` event.
|
|
2780
|
+
*
|
|
2781
|
+
* @private
|
|
2782
|
+
*/
|
|
2783
|
+
emitClose() {
|
|
2784
|
+
if (!this._socket) {
|
|
2785
|
+
this._readyState = WebSocket2.CLOSED;
|
|
2786
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
2787
|
+
return;
|
|
2788
|
+
}
|
|
2789
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
2790
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
2791
|
+
}
|
|
2792
|
+
this._receiver.removeAllListeners();
|
|
2793
|
+
this._readyState = WebSocket2.CLOSED;
|
|
2794
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
2795
|
+
}
|
|
2796
|
+
/**
|
|
2797
|
+
* Start a closing handshake.
|
|
2798
|
+
*
|
|
2799
|
+
* +----------+ +-----------+ +----------+
|
|
2800
|
+
* - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
|
|
2801
|
+
* | +----------+ +-----------+ +----------+ |
|
|
2802
|
+
* +----------+ +-----------+ |
|
|
2803
|
+
* CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
|
|
2804
|
+
* +----------+ +-----------+ |
|
|
2805
|
+
* | | | +---+ |
|
|
2806
|
+
* +------------------------+-->|fin| - - - -
|
|
2807
|
+
* | +---+ | +---+
|
|
2808
|
+
* - - - - -|fin|<---------------------+
|
|
2809
|
+
* +---+
|
|
2810
|
+
*
|
|
2811
|
+
* @param {Number} [code] Status code explaining why the connection is closing
|
|
2812
|
+
* @param {(String|Buffer)} [data] The reason why the connection is
|
|
2813
|
+
* closing
|
|
2814
|
+
* @public
|
|
2815
|
+
*/
|
|
2816
|
+
close(code, data) {
|
|
2817
|
+
if (this.readyState === WebSocket2.CLOSED) return;
|
|
2818
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
2819
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
2820
|
+
abortHandshake(this, this._req, msg);
|
|
2821
|
+
return;
|
|
2822
|
+
}
|
|
2823
|
+
if (this.readyState === WebSocket2.CLOSING) {
|
|
2824
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
2825
|
+
this._socket.end();
|
|
2826
|
+
}
|
|
2827
|
+
return;
|
|
2828
|
+
}
|
|
2829
|
+
this._readyState = WebSocket2.CLOSING;
|
|
2830
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
2831
|
+
if (err) return;
|
|
2832
|
+
this._closeFrameSent = true;
|
|
2833
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
2834
|
+
this._socket.end();
|
|
2835
|
+
}
|
|
2836
|
+
});
|
|
2837
|
+
setCloseTimer(this);
|
|
2838
|
+
}
|
|
2839
|
+
/**
|
|
2840
|
+
* Pause the socket.
|
|
2841
|
+
*
|
|
2842
|
+
* @public
|
|
2843
|
+
*/
|
|
2844
|
+
pause() {
|
|
2845
|
+
if (this.readyState === WebSocket2.CONNECTING || this.readyState === WebSocket2.CLOSED) {
|
|
2846
|
+
return;
|
|
2847
|
+
}
|
|
2848
|
+
this._paused = true;
|
|
2849
|
+
this._socket.pause();
|
|
2850
|
+
}
|
|
2851
|
+
/**
|
|
2852
|
+
* Send a ping.
|
|
2853
|
+
*
|
|
2854
|
+
* @param {*} [data] The data to send
|
|
2855
|
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
|
2856
|
+
* @param {Function} [cb] Callback which is executed when the ping is sent
|
|
2857
|
+
* @public
|
|
2858
|
+
*/
|
|
2859
|
+
ping(data, mask, cb) {
|
|
2860
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
2861
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2862
|
+
}
|
|
2863
|
+
if (typeof data === "function") {
|
|
2864
|
+
cb = data;
|
|
2865
|
+
data = mask = void 0;
|
|
2866
|
+
} else if (typeof mask === "function") {
|
|
2867
|
+
cb = mask;
|
|
2868
|
+
mask = void 0;
|
|
2869
|
+
}
|
|
2870
|
+
if (typeof data === "number") data = data.toString();
|
|
2871
|
+
if (this.readyState !== WebSocket2.OPEN) {
|
|
2872
|
+
sendAfterClose(this, data, cb);
|
|
2873
|
+
return;
|
|
2874
|
+
}
|
|
2875
|
+
if (mask === void 0) mask = !this._isServer;
|
|
2876
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
2877
|
+
}
|
|
2878
|
+
/**
|
|
2879
|
+
* Send a pong.
|
|
2880
|
+
*
|
|
2881
|
+
* @param {*} [data] The data to send
|
|
2882
|
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
|
2883
|
+
* @param {Function} [cb] Callback which is executed when the pong is sent
|
|
2884
|
+
* @public
|
|
2885
|
+
*/
|
|
2886
|
+
pong(data, mask, cb) {
|
|
2887
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
2888
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2889
|
+
}
|
|
2890
|
+
if (typeof data === "function") {
|
|
2891
|
+
cb = data;
|
|
2892
|
+
data = mask = void 0;
|
|
2893
|
+
} else if (typeof mask === "function") {
|
|
2894
|
+
cb = mask;
|
|
2895
|
+
mask = void 0;
|
|
2896
|
+
}
|
|
2897
|
+
if (typeof data === "number") data = data.toString();
|
|
2898
|
+
if (this.readyState !== WebSocket2.OPEN) {
|
|
2899
|
+
sendAfterClose(this, data, cb);
|
|
2900
|
+
return;
|
|
2901
|
+
}
|
|
2902
|
+
if (mask === void 0) mask = !this._isServer;
|
|
2903
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
2904
|
+
}
|
|
2905
|
+
/**
|
|
2906
|
+
* Resume the socket.
|
|
2907
|
+
*
|
|
2908
|
+
* @public
|
|
2909
|
+
*/
|
|
2910
|
+
resume() {
|
|
2911
|
+
if (this.readyState === WebSocket2.CONNECTING || this.readyState === WebSocket2.CLOSED) {
|
|
2912
|
+
return;
|
|
2913
|
+
}
|
|
2914
|
+
this._paused = false;
|
|
2915
|
+
if (!this._receiver._writableState.needDrain) this._socket.resume();
|
|
2916
|
+
}
|
|
2917
|
+
/**
|
|
2918
|
+
* Send a data message.
|
|
2919
|
+
*
|
|
2920
|
+
* @param {*} data The message to send
|
|
2921
|
+
* @param {Object} [options] Options object
|
|
2922
|
+
* @param {Boolean} [options.binary] Specifies whether `data` is binary or
|
|
2923
|
+
* text
|
|
2924
|
+
* @param {Boolean} [options.compress] Specifies whether or not to compress
|
|
2925
|
+
* `data`
|
|
2926
|
+
* @param {Boolean} [options.fin=true] Specifies whether the fragment is the
|
|
2927
|
+
* last one
|
|
2928
|
+
* @param {Boolean} [options.mask] Specifies whether or not to mask `data`
|
|
2929
|
+
* @param {Function} [cb] Callback which is executed when data is written out
|
|
2930
|
+
* @public
|
|
2931
|
+
*/
|
|
2932
|
+
send(data, options, cb) {
|
|
2933
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
2934
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2935
|
+
}
|
|
2936
|
+
if (typeof options === "function") {
|
|
2937
|
+
cb = options;
|
|
2938
|
+
options = {};
|
|
2939
|
+
}
|
|
2940
|
+
if (typeof data === "number") data = data.toString();
|
|
2941
|
+
if (this.readyState !== WebSocket2.OPEN) {
|
|
2942
|
+
sendAfterClose(this, data, cb);
|
|
2943
|
+
return;
|
|
2944
|
+
}
|
|
2945
|
+
const opts = {
|
|
2946
|
+
binary: typeof data !== "string",
|
|
2947
|
+
mask: !this._isServer,
|
|
2948
|
+
compress: true,
|
|
2949
|
+
fin: true,
|
|
2950
|
+
...options
|
|
2951
|
+
};
|
|
2952
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
2953
|
+
opts.compress = false;
|
|
2954
|
+
}
|
|
2955
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
2956
|
+
}
|
|
2957
|
+
/**
|
|
2958
|
+
* Forcibly close the connection.
|
|
2959
|
+
*
|
|
2960
|
+
* @public
|
|
2961
|
+
*/
|
|
2962
|
+
terminate() {
|
|
2963
|
+
if (this.readyState === WebSocket2.CLOSED) return;
|
|
2964
|
+
if (this.readyState === WebSocket2.CONNECTING) {
|
|
2965
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
2966
|
+
abortHandshake(this, this._req, msg);
|
|
2967
|
+
return;
|
|
2968
|
+
}
|
|
2969
|
+
if (this._socket) {
|
|
2970
|
+
this._readyState = WebSocket2.CLOSING;
|
|
2971
|
+
this._socket.destroy();
|
|
2972
|
+
}
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
Object.defineProperty(WebSocket2, "CONNECTING", {
|
|
2976
|
+
enumerable: true,
|
|
2977
|
+
value: readyStates.indexOf("CONNECTING")
|
|
2978
|
+
});
|
|
2979
|
+
Object.defineProperty(WebSocket2.prototype, "CONNECTING", {
|
|
2980
|
+
enumerable: true,
|
|
2981
|
+
value: readyStates.indexOf("CONNECTING")
|
|
2982
|
+
});
|
|
2983
|
+
Object.defineProperty(WebSocket2, "OPEN", {
|
|
2984
|
+
enumerable: true,
|
|
2985
|
+
value: readyStates.indexOf("OPEN")
|
|
2986
|
+
});
|
|
2987
|
+
Object.defineProperty(WebSocket2.prototype, "OPEN", {
|
|
2988
|
+
enumerable: true,
|
|
2989
|
+
value: readyStates.indexOf("OPEN")
|
|
2990
|
+
});
|
|
2991
|
+
Object.defineProperty(WebSocket2, "CLOSING", {
|
|
2992
|
+
enumerable: true,
|
|
2993
|
+
value: readyStates.indexOf("CLOSING")
|
|
2994
|
+
});
|
|
2995
|
+
Object.defineProperty(WebSocket2.prototype, "CLOSING", {
|
|
2996
|
+
enumerable: true,
|
|
2997
|
+
value: readyStates.indexOf("CLOSING")
|
|
2998
|
+
});
|
|
2999
|
+
Object.defineProperty(WebSocket2, "CLOSED", {
|
|
3000
|
+
enumerable: true,
|
|
3001
|
+
value: readyStates.indexOf("CLOSED")
|
|
3002
|
+
});
|
|
3003
|
+
Object.defineProperty(WebSocket2.prototype, "CLOSED", {
|
|
3004
|
+
enumerable: true,
|
|
3005
|
+
value: readyStates.indexOf("CLOSED")
|
|
3006
|
+
});
|
|
3007
|
+
[
|
|
3008
|
+
"binaryType",
|
|
3009
|
+
"bufferedAmount",
|
|
3010
|
+
"extensions",
|
|
3011
|
+
"isPaused",
|
|
3012
|
+
"protocol",
|
|
3013
|
+
"readyState",
|
|
3014
|
+
"url"
|
|
3015
|
+
].forEach((property) => {
|
|
3016
|
+
Object.defineProperty(WebSocket2.prototype, property, { enumerable: true });
|
|
3017
|
+
});
|
|
3018
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
3019
|
+
Object.defineProperty(WebSocket2.prototype, `on${method}`, {
|
|
3020
|
+
enumerable: true,
|
|
3021
|
+
get() {
|
|
3022
|
+
for (const listener of this.listeners(method)) {
|
|
3023
|
+
if (listener[kForOnEventAttribute]) return listener[kListener];
|
|
3024
|
+
}
|
|
3025
|
+
return null;
|
|
3026
|
+
},
|
|
3027
|
+
set(handler) {
|
|
3028
|
+
for (const listener of this.listeners(method)) {
|
|
3029
|
+
if (listener[kForOnEventAttribute]) {
|
|
3030
|
+
this.removeListener(method, listener);
|
|
3031
|
+
break;
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
if (typeof handler !== "function") return;
|
|
3035
|
+
this.addEventListener(method, handler, {
|
|
3036
|
+
[kForOnEventAttribute]: true
|
|
3037
|
+
});
|
|
3038
|
+
}
|
|
3039
|
+
});
|
|
3040
|
+
});
|
|
3041
|
+
WebSocket2.prototype.addEventListener = addEventListener;
|
|
3042
|
+
WebSocket2.prototype.removeEventListener = removeEventListener;
|
|
3043
|
+
websocket = WebSocket2;
|
|
3044
|
+
function initAsClient(websocket2, address, protocols, options) {
|
|
3045
|
+
const opts = {
|
|
3046
|
+
allowSynchronousEvents: true,
|
|
3047
|
+
autoPong: true,
|
|
3048
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
3049
|
+
protocolVersion: protocolVersions[1],
|
|
3050
|
+
maxPayload: 100 * 1024 * 1024,
|
|
3051
|
+
skipUTF8Validation: false,
|
|
3052
|
+
perMessageDeflate: true,
|
|
3053
|
+
followRedirects: false,
|
|
3054
|
+
maxRedirects: 10,
|
|
3055
|
+
...options,
|
|
3056
|
+
socketPath: void 0,
|
|
3057
|
+
hostname: void 0,
|
|
3058
|
+
protocol: void 0,
|
|
3059
|
+
timeout: void 0,
|
|
3060
|
+
method: "GET",
|
|
3061
|
+
host: void 0,
|
|
3062
|
+
path: void 0,
|
|
3063
|
+
port: void 0
|
|
3064
|
+
};
|
|
3065
|
+
websocket2._autoPong = opts.autoPong;
|
|
3066
|
+
websocket2._closeTimeout = opts.closeTimeout;
|
|
3067
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
3068
|
+
throw new RangeError(
|
|
3069
|
+
`Unsupported protocol version: ${opts.protocolVersion} (supported versions: ${protocolVersions.join(", ")})`
|
|
3070
|
+
);
|
|
3071
|
+
}
|
|
3072
|
+
let parsedUrl;
|
|
3073
|
+
if (address instanceof URL) {
|
|
3074
|
+
parsedUrl = address;
|
|
3075
|
+
} else {
|
|
3076
|
+
try {
|
|
3077
|
+
parsedUrl = new URL(address);
|
|
3078
|
+
} catch {
|
|
3079
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
3080
|
+
}
|
|
3081
|
+
}
|
|
3082
|
+
if (parsedUrl.protocol === "http:") {
|
|
3083
|
+
parsedUrl.protocol = "ws:";
|
|
3084
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
3085
|
+
parsedUrl.protocol = "wss:";
|
|
3086
|
+
}
|
|
3087
|
+
websocket2._url = parsedUrl.href;
|
|
3088
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
3089
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
3090
|
+
let invalidUrlMessage;
|
|
3091
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
3092
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", "http:", "https:", or "ws+unix:"`;
|
|
3093
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
3094
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
3095
|
+
} else if (parsedUrl.hash) {
|
|
3096
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
3097
|
+
}
|
|
3098
|
+
if (invalidUrlMessage) {
|
|
3099
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
3100
|
+
if (websocket2._redirects === 0) {
|
|
3101
|
+
throw err;
|
|
3102
|
+
} else {
|
|
3103
|
+
emitErrorAndClose(websocket2, err);
|
|
3104
|
+
return;
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
3108
|
+
const key = randomBytes(16).toString("base64");
|
|
3109
|
+
const request = isSecure ? https.request : http.request;
|
|
3110
|
+
const protocolSet = /* @__PURE__ */ new Set();
|
|
3111
|
+
let perMessageDeflate;
|
|
3112
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
3113
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
3114
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
3115
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
3116
|
+
opts.headers = {
|
|
3117
|
+
...opts.headers,
|
|
3118
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
3119
|
+
"Sec-WebSocket-Key": key,
|
|
3120
|
+
Connection: "Upgrade",
|
|
3121
|
+
Upgrade: "websocket"
|
|
3122
|
+
};
|
|
3123
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
3124
|
+
opts.timeout = opts.handshakeTimeout;
|
|
3125
|
+
if (opts.perMessageDeflate) {
|
|
3126
|
+
perMessageDeflate = new PerMessageDeflate({
|
|
3127
|
+
...opts.perMessageDeflate,
|
|
3128
|
+
isServer: false,
|
|
3129
|
+
maxPayload: opts.maxPayload
|
|
3130
|
+
});
|
|
3131
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({
|
|
3132
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
3133
|
+
});
|
|
3134
|
+
}
|
|
3135
|
+
if (protocols.length) {
|
|
3136
|
+
for (const protocol of protocols) {
|
|
3137
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
3138
|
+
throw new SyntaxError(
|
|
3139
|
+
"An invalid or duplicated subprotocol was specified"
|
|
3140
|
+
);
|
|
3141
|
+
}
|
|
3142
|
+
protocolSet.add(protocol);
|
|
3143
|
+
}
|
|
3144
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
3145
|
+
}
|
|
3146
|
+
if (opts.origin) {
|
|
3147
|
+
if (opts.protocolVersion < 13) {
|
|
3148
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
3149
|
+
} else {
|
|
3150
|
+
opts.headers.Origin = opts.origin;
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
3154
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
3155
|
+
}
|
|
3156
|
+
if (isIpcUrl) {
|
|
3157
|
+
const parts = opts.path.split(":");
|
|
3158
|
+
opts.socketPath = parts[0];
|
|
3159
|
+
opts.path = parts[1];
|
|
3160
|
+
}
|
|
3161
|
+
let req;
|
|
3162
|
+
if (opts.followRedirects) {
|
|
3163
|
+
if (websocket2._redirects === 0) {
|
|
3164
|
+
websocket2._originalIpc = isIpcUrl;
|
|
3165
|
+
websocket2._originalSecure = isSecure;
|
|
3166
|
+
websocket2._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
3167
|
+
const headers = options && options.headers;
|
|
3168
|
+
options = { ...options, headers: {} };
|
|
3169
|
+
if (headers) {
|
|
3170
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
3171
|
+
options.headers[key2.toLowerCase()] = value;
|
|
3172
|
+
}
|
|
3173
|
+
}
|
|
3174
|
+
} else if (websocket2.listenerCount("redirect") === 0) {
|
|
3175
|
+
const isSameHost = isIpcUrl ? websocket2._originalIpc ? opts.socketPath === websocket2._originalHostOrSocketPath : false : websocket2._originalIpc ? false : parsedUrl.host === websocket2._originalHostOrSocketPath;
|
|
3176
|
+
if (!isSameHost || websocket2._originalSecure && !isSecure) {
|
|
3177
|
+
delete opts.headers.authorization;
|
|
3178
|
+
delete opts.headers.cookie;
|
|
3179
|
+
if (!isSameHost) delete opts.headers.host;
|
|
3180
|
+
opts.auth = void 0;
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
if (opts.auth && !options.headers.authorization) {
|
|
3184
|
+
options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
3185
|
+
}
|
|
3186
|
+
req = websocket2._req = request(opts);
|
|
3187
|
+
if (websocket2._redirects) {
|
|
3188
|
+
websocket2.emit("redirect", websocket2.url, req);
|
|
3189
|
+
}
|
|
3190
|
+
} else {
|
|
3191
|
+
req = websocket2._req = request(opts);
|
|
3192
|
+
}
|
|
3193
|
+
if (opts.timeout) {
|
|
3194
|
+
req.on("timeout", () => {
|
|
3195
|
+
abortHandshake(websocket2, req, "Opening handshake has timed out");
|
|
3196
|
+
});
|
|
3197
|
+
}
|
|
3198
|
+
req.on("error", (err) => {
|
|
3199
|
+
if (req === null || req[kAborted]) return;
|
|
3200
|
+
req = websocket2._req = null;
|
|
3201
|
+
emitErrorAndClose(websocket2, err);
|
|
3202
|
+
});
|
|
3203
|
+
req.on("response", (res) => {
|
|
3204
|
+
const location = res.headers.location;
|
|
3205
|
+
const statusCode = res.statusCode;
|
|
3206
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
3207
|
+
if (++websocket2._redirects > opts.maxRedirects) {
|
|
3208
|
+
abortHandshake(websocket2, req, "Maximum redirects exceeded");
|
|
3209
|
+
return;
|
|
3210
|
+
}
|
|
3211
|
+
req.abort();
|
|
3212
|
+
let addr;
|
|
3213
|
+
try {
|
|
3214
|
+
addr = new URL(location, address);
|
|
3215
|
+
} catch (e) {
|
|
3216
|
+
const err = new SyntaxError(`Invalid URL: ${location}`);
|
|
3217
|
+
emitErrorAndClose(websocket2, err);
|
|
3218
|
+
return;
|
|
3219
|
+
}
|
|
3220
|
+
initAsClient(websocket2, addr, protocols, options);
|
|
3221
|
+
} else if (!websocket2.emit("unexpected-response", req, res)) {
|
|
3222
|
+
abortHandshake(
|
|
3223
|
+
websocket2,
|
|
3224
|
+
req,
|
|
3225
|
+
`Unexpected server response: ${res.statusCode}`
|
|
3226
|
+
);
|
|
3227
|
+
}
|
|
3228
|
+
});
|
|
3229
|
+
req.on("upgrade", (res, socket, head) => {
|
|
3230
|
+
websocket2.emit("upgrade", res);
|
|
3231
|
+
if (websocket2.readyState !== WebSocket2.CONNECTING) return;
|
|
3232
|
+
req = websocket2._req = null;
|
|
3233
|
+
const upgrade = res.headers.upgrade;
|
|
3234
|
+
if (upgrade === void 0 || upgrade.toLowerCase() !== "websocket") {
|
|
3235
|
+
abortHandshake(websocket2, socket, "Invalid Upgrade header");
|
|
3236
|
+
return;
|
|
3237
|
+
}
|
|
3238
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
3239
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
3240
|
+
abortHandshake(websocket2, socket, "Invalid Sec-WebSocket-Accept header");
|
|
3241
|
+
return;
|
|
3242
|
+
}
|
|
3243
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
3244
|
+
let protError;
|
|
3245
|
+
if (serverProt !== void 0) {
|
|
3246
|
+
if (!protocolSet.size) {
|
|
3247
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
3248
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
3249
|
+
protError = "Server sent an invalid subprotocol";
|
|
3250
|
+
}
|
|
3251
|
+
} else if (protocolSet.size) {
|
|
3252
|
+
protError = "Server sent no subprotocol";
|
|
3253
|
+
}
|
|
3254
|
+
if (protError) {
|
|
3255
|
+
abortHandshake(websocket2, socket, protError);
|
|
3256
|
+
return;
|
|
3257
|
+
}
|
|
3258
|
+
if (serverProt) websocket2._protocol = serverProt;
|
|
3259
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
3260
|
+
if (secWebSocketExtensions !== void 0) {
|
|
3261
|
+
if (!perMessageDeflate) {
|
|
3262
|
+
const message = "Server sent a Sec-WebSocket-Extensions header but no extension was requested";
|
|
3263
|
+
abortHandshake(websocket2, socket, message);
|
|
3264
|
+
return;
|
|
3265
|
+
}
|
|
3266
|
+
let extensions;
|
|
3267
|
+
try {
|
|
3268
|
+
extensions = parse(secWebSocketExtensions);
|
|
3269
|
+
} catch (err) {
|
|
3270
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
3271
|
+
abortHandshake(websocket2, socket, message);
|
|
3272
|
+
return;
|
|
3273
|
+
}
|
|
3274
|
+
const extensionNames = Object.keys(extensions);
|
|
3275
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
3276
|
+
const message = "Server indicated an extension that was not requested";
|
|
3277
|
+
abortHandshake(websocket2, socket, message);
|
|
3278
|
+
return;
|
|
3279
|
+
}
|
|
3280
|
+
try {
|
|
3281
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
3282
|
+
} catch (err) {
|
|
3283
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
3284
|
+
abortHandshake(websocket2, socket, message);
|
|
3285
|
+
return;
|
|
3286
|
+
}
|
|
3287
|
+
websocket2._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
3288
|
+
}
|
|
3289
|
+
websocket2.setSocket(socket, head, {
|
|
3290
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
3291
|
+
generateMask: opts.generateMask,
|
|
3292
|
+
maxPayload: opts.maxPayload,
|
|
3293
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
3294
|
+
});
|
|
3295
|
+
});
|
|
3296
|
+
if (opts.finishRequest) {
|
|
3297
|
+
opts.finishRequest(req, websocket2);
|
|
3298
|
+
} else {
|
|
3299
|
+
req.end();
|
|
3300
|
+
}
|
|
3301
|
+
}
|
|
3302
|
+
function emitErrorAndClose(websocket2, err) {
|
|
3303
|
+
websocket2._readyState = WebSocket2.CLOSING;
|
|
3304
|
+
websocket2._errorEmitted = true;
|
|
3305
|
+
websocket2.emit("error", err);
|
|
3306
|
+
websocket2.emitClose();
|
|
3307
|
+
}
|
|
3308
|
+
function netConnect(options) {
|
|
3309
|
+
options.path = options.socketPath;
|
|
3310
|
+
return net.connect(options);
|
|
3311
|
+
}
|
|
3312
|
+
function tlsConnect(options) {
|
|
3313
|
+
options.path = void 0;
|
|
3314
|
+
if (!options.servername && options.servername !== "") {
|
|
3315
|
+
options.servername = net.isIP(options.host) ? "" : options.host;
|
|
3316
|
+
}
|
|
3317
|
+
return tls.connect(options);
|
|
3318
|
+
}
|
|
3319
|
+
function abortHandshake(websocket2, stream2, message) {
|
|
3320
|
+
websocket2._readyState = WebSocket2.CLOSING;
|
|
3321
|
+
const err = new Error(message);
|
|
3322
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
3323
|
+
if (stream2.setHeader) {
|
|
3324
|
+
stream2[kAborted] = true;
|
|
3325
|
+
stream2.abort();
|
|
3326
|
+
if (stream2.socket && !stream2.socket.destroyed) {
|
|
3327
|
+
stream2.socket.destroy();
|
|
3328
|
+
}
|
|
3329
|
+
process.nextTick(emitErrorAndClose, websocket2, err);
|
|
3330
|
+
} else {
|
|
3331
|
+
stream2.destroy(err);
|
|
3332
|
+
stream2.once("error", websocket2.emit.bind(websocket2, "error"));
|
|
3333
|
+
stream2.once("close", websocket2.emitClose.bind(websocket2));
|
|
3334
|
+
}
|
|
3335
|
+
}
|
|
3336
|
+
function sendAfterClose(websocket2, data, cb) {
|
|
3337
|
+
if (data) {
|
|
3338
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
3339
|
+
if (websocket2._socket) websocket2._sender._bufferedBytes += length;
|
|
3340
|
+
else websocket2._bufferedAmount += length;
|
|
3341
|
+
}
|
|
3342
|
+
if (cb) {
|
|
3343
|
+
const err = new Error(
|
|
3344
|
+
`WebSocket is not open: readyState ${websocket2.readyState} (${readyStates[websocket2.readyState]})`
|
|
3345
|
+
);
|
|
3346
|
+
process.nextTick(cb, err);
|
|
3347
|
+
}
|
|
3348
|
+
}
|
|
3349
|
+
function receiverOnConclude(code, reason) {
|
|
3350
|
+
const websocket2 = this[kWebSocket];
|
|
3351
|
+
websocket2._closeFrameReceived = true;
|
|
3352
|
+
websocket2._closeMessage = reason;
|
|
3353
|
+
websocket2._closeCode = code;
|
|
3354
|
+
if (websocket2._socket[kWebSocket] === void 0) return;
|
|
3355
|
+
websocket2._socket.removeListener("data", socketOnData);
|
|
3356
|
+
process.nextTick(resume, websocket2._socket);
|
|
3357
|
+
if (code === 1005) websocket2.close();
|
|
3358
|
+
else websocket2.close(code, reason);
|
|
3359
|
+
}
|
|
3360
|
+
function receiverOnDrain() {
|
|
3361
|
+
const websocket2 = this[kWebSocket];
|
|
3362
|
+
if (!websocket2.isPaused) websocket2._socket.resume();
|
|
3363
|
+
}
|
|
3364
|
+
function receiverOnError(err) {
|
|
3365
|
+
const websocket2 = this[kWebSocket];
|
|
3366
|
+
if (websocket2._socket[kWebSocket] !== void 0) {
|
|
3367
|
+
websocket2._socket.removeListener("data", socketOnData);
|
|
3368
|
+
process.nextTick(resume, websocket2._socket);
|
|
3369
|
+
websocket2.close(err[kStatusCode]);
|
|
3370
|
+
}
|
|
3371
|
+
if (!websocket2._errorEmitted) {
|
|
3372
|
+
websocket2._errorEmitted = true;
|
|
3373
|
+
websocket2.emit("error", err);
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
3376
|
+
function receiverOnFinish() {
|
|
3377
|
+
this[kWebSocket].emitClose();
|
|
3378
|
+
}
|
|
3379
|
+
function receiverOnMessage(data, isBinary) {
|
|
3380
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
3381
|
+
}
|
|
3382
|
+
function receiverOnPing(data) {
|
|
3383
|
+
const websocket2 = this[kWebSocket];
|
|
3384
|
+
if (websocket2._autoPong) websocket2.pong(data, !this._isServer, NOOP);
|
|
3385
|
+
websocket2.emit("ping", data);
|
|
3386
|
+
}
|
|
3387
|
+
function receiverOnPong(data) {
|
|
3388
|
+
this[kWebSocket].emit("pong", data);
|
|
3389
|
+
}
|
|
3390
|
+
function resume(stream2) {
|
|
3391
|
+
stream2.resume();
|
|
3392
|
+
}
|
|
3393
|
+
function senderOnError(err) {
|
|
3394
|
+
const websocket2 = this[kWebSocket];
|
|
3395
|
+
if (websocket2.readyState === WebSocket2.CLOSED) return;
|
|
3396
|
+
if (websocket2.readyState === WebSocket2.OPEN) {
|
|
3397
|
+
websocket2._readyState = WebSocket2.CLOSING;
|
|
3398
|
+
setCloseTimer(websocket2);
|
|
3399
|
+
}
|
|
3400
|
+
this._socket.end();
|
|
3401
|
+
if (!websocket2._errorEmitted) {
|
|
3402
|
+
websocket2._errorEmitted = true;
|
|
3403
|
+
websocket2.emit("error", err);
|
|
3404
|
+
}
|
|
3405
|
+
}
|
|
3406
|
+
function setCloseTimer(websocket2) {
|
|
3407
|
+
websocket2._closeTimer = setTimeout(
|
|
3408
|
+
websocket2._socket.destroy.bind(websocket2._socket),
|
|
3409
|
+
websocket2._closeTimeout
|
|
3410
|
+
);
|
|
3411
|
+
}
|
|
3412
|
+
function socketOnClose() {
|
|
3413
|
+
const websocket2 = this[kWebSocket];
|
|
3414
|
+
this.removeListener("close", socketOnClose);
|
|
3415
|
+
this.removeListener("data", socketOnData);
|
|
3416
|
+
this.removeListener("end", socketOnEnd);
|
|
3417
|
+
websocket2._readyState = WebSocket2.CLOSING;
|
|
3418
|
+
if (!this._readableState.endEmitted && !websocket2._closeFrameReceived && !websocket2._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
|
|
3419
|
+
const chunk = this.read(this._readableState.length);
|
|
3420
|
+
websocket2._receiver.write(chunk);
|
|
3421
|
+
}
|
|
3422
|
+
websocket2._receiver.end();
|
|
3423
|
+
this[kWebSocket] = void 0;
|
|
3424
|
+
clearTimeout(websocket2._closeTimer);
|
|
3425
|
+
if (websocket2._receiver._writableState.finished || websocket2._receiver._writableState.errorEmitted) {
|
|
3426
|
+
websocket2.emitClose();
|
|
3427
|
+
} else {
|
|
3428
|
+
websocket2._receiver.on("error", receiverOnFinish);
|
|
3429
|
+
websocket2._receiver.on("finish", receiverOnFinish);
|
|
3430
|
+
}
|
|
3431
|
+
}
|
|
3432
|
+
function socketOnData(chunk) {
|
|
3433
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
3434
|
+
this.pause();
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
function socketOnEnd() {
|
|
3438
|
+
const websocket2 = this[kWebSocket];
|
|
3439
|
+
websocket2._readyState = WebSocket2.CLOSING;
|
|
3440
|
+
websocket2._receiver.end();
|
|
3441
|
+
this.end();
|
|
3442
|
+
}
|
|
3443
|
+
function socketOnError() {
|
|
3444
|
+
const websocket2 = this[kWebSocket];
|
|
3445
|
+
this.removeListener("error", socketOnError);
|
|
3446
|
+
this.on("error", NOOP);
|
|
3447
|
+
if (websocket2) {
|
|
3448
|
+
websocket2._readyState = WebSocket2.CLOSING;
|
|
3449
|
+
this.destroy();
|
|
3450
|
+
}
|
|
3451
|
+
}
|
|
3452
|
+
return websocket;
|
|
3453
|
+
}
|
|
3454
|
+
var stream;
|
|
3455
|
+
var hasRequiredStream;
|
|
3456
|
+
function requireStream() {
|
|
3457
|
+
if (hasRequiredStream) return stream;
|
|
3458
|
+
hasRequiredStream = 1;
|
|
3459
|
+
requireWebsocket();
|
|
3460
|
+
const { Duplex } = require$$0$2;
|
|
3461
|
+
function emitClose(stream2) {
|
|
3462
|
+
stream2.emit("close");
|
|
3463
|
+
}
|
|
3464
|
+
function duplexOnEnd() {
|
|
3465
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
3466
|
+
this.destroy();
|
|
3467
|
+
}
|
|
3468
|
+
}
|
|
3469
|
+
function duplexOnError(err) {
|
|
3470
|
+
this.removeListener("error", duplexOnError);
|
|
3471
|
+
this.destroy();
|
|
3472
|
+
if (this.listenerCount("error") === 0) {
|
|
3473
|
+
this.emit("error", err);
|
|
3474
|
+
}
|
|
3475
|
+
}
|
|
3476
|
+
function createWebSocketStream(ws, options) {
|
|
3477
|
+
let terminateOnDestroy = true;
|
|
3478
|
+
const duplex = new Duplex({
|
|
3479
|
+
...options,
|
|
3480
|
+
autoDestroy: false,
|
|
3481
|
+
emitClose: false,
|
|
3482
|
+
objectMode: false,
|
|
3483
|
+
writableObjectMode: false
|
|
3484
|
+
});
|
|
3485
|
+
ws.on("message", function message(msg, isBinary) {
|
|
3486
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
3487
|
+
if (!duplex.push(data)) ws.pause();
|
|
3488
|
+
});
|
|
3489
|
+
ws.once("error", function error(err) {
|
|
3490
|
+
if (duplex.destroyed) return;
|
|
3491
|
+
terminateOnDestroy = false;
|
|
3492
|
+
duplex.destroy(err);
|
|
3493
|
+
});
|
|
3494
|
+
ws.once("close", function close() {
|
|
3495
|
+
if (duplex.destroyed) return;
|
|
3496
|
+
duplex.push(null);
|
|
3497
|
+
});
|
|
3498
|
+
duplex._destroy = function(err, callback) {
|
|
3499
|
+
if (ws.readyState === ws.CLOSED) {
|
|
3500
|
+
callback(err);
|
|
3501
|
+
process.nextTick(emitClose, duplex);
|
|
3502
|
+
return;
|
|
3503
|
+
}
|
|
3504
|
+
let called = false;
|
|
3505
|
+
ws.once("error", function error(err2) {
|
|
3506
|
+
called = true;
|
|
3507
|
+
callback(err2);
|
|
3508
|
+
});
|
|
3509
|
+
ws.once("close", function close() {
|
|
3510
|
+
if (!called) callback(err);
|
|
3511
|
+
process.nextTick(emitClose, duplex);
|
|
3512
|
+
});
|
|
3513
|
+
if (terminateOnDestroy) ws.terminate();
|
|
3514
|
+
};
|
|
3515
|
+
duplex._final = function(callback) {
|
|
3516
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
3517
|
+
ws.once("open", function open() {
|
|
3518
|
+
duplex._final(callback);
|
|
3519
|
+
});
|
|
3520
|
+
return;
|
|
3521
|
+
}
|
|
3522
|
+
if (ws._socket === null) return;
|
|
3523
|
+
if (ws._socket._writableState.finished) {
|
|
3524
|
+
callback();
|
|
3525
|
+
if (duplex._readableState.endEmitted) duplex.destroy();
|
|
3526
|
+
} else {
|
|
3527
|
+
ws._socket.once("finish", function finish() {
|
|
3528
|
+
callback();
|
|
3529
|
+
});
|
|
3530
|
+
ws.close();
|
|
3531
|
+
}
|
|
3532
|
+
};
|
|
3533
|
+
duplex._read = function() {
|
|
3534
|
+
if (ws.isPaused) ws.resume();
|
|
3535
|
+
};
|
|
3536
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
3537
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
3538
|
+
ws.once("open", function open() {
|
|
3539
|
+
duplex._write(chunk, encoding, callback);
|
|
3540
|
+
});
|
|
3541
|
+
return;
|
|
3542
|
+
}
|
|
3543
|
+
ws.send(chunk, callback);
|
|
3544
|
+
};
|
|
3545
|
+
duplex.on("end", duplexOnEnd);
|
|
3546
|
+
duplex.on("error", duplexOnError);
|
|
3547
|
+
return duplex;
|
|
3548
|
+
}
|
|
3549
|
+
stream = createWebSocketStream;
|
|
3550
|
+
return stream;
|
|
3551
|
+
}
|
|
3552
|
+
requireStream();
|
|
3553
|
+
requireExtension();
|
|
3554
|
+
requirePermessageDeflate();
|
|
3555
|
+
requireReceiver();
|
|
3556
|
+
requireSender();
|
|
3557
|
+
var subprotocol;
|
|
3558
|
+
var hasRequiredSubprotocol;
|
|
3559
|
+
function requireSubprotocol() {
|
|
3560
|
+
if (hasRequiredSubprotocol) return subprotocol;
|
|
3561
|
+
hasRequiredSubprotocol = 1;
|
|
3562
|
+
const { tokenChars } = requireValidation();
|
|
3563
|
+
function parse(header) {
|
|
3564
|
+
const protocols = /* @__PURE__ */ new Set();
|
|
3565
|
+
let start = -1;
|
|
3566
|
+
let end = -1;
|
|
3567
|
+
let i = 0;
|
|
3568
|
+
for (i; i < header.length; i++) {
|
|
3569
|
+
const code = header.charCodeAt(i);
|
|
3570
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
3571
|
+
if (start === -1) start = i;
|
|
3572
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
3573
|
+
if (end === -1 && start !== -1) end = i;
|
|
3574
|
+
} else if (code === 44) {
|
|
3575
|
+
if (start === -1) {
|
|
3576
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
3577
|
+
}
|
|
3578
|
+
if (end === -1) end = i;
|
|
3579
|
+
const protocol2 = header.slice(start, end);
|
|
3580
|
+
if (protocols.has(protocol2)) {
|
|
3581
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
3582
|
+
}
|
|
3583
|
+
protocols.add(protocol2);
|
|
3584
|
+
start = end = -1;
|
|
3585
|
+
} else {
|
|
3586
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
3587
|
+
}
|
|
3588
|
+
}
|
|
3589
|
+
if (start === -1 || end !== -1) {
|
|
3590
|
+
throw new SyntaxError("Unexpected end of input");
|
|
3591
|
+
}
|
|
3592
|
+
const protocol = header.slice(start, i);
|
|
3593
|
+
if (protocols.has(protocol)) {
|
|
3594
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
3595
|
+
}
|
|
3596
|
+
protocols.add(protocol);
|
|
3597
|
+
return protocols;
|
|
3598
|
+
}
|
|
3599
|
+
subprotocol = { parse };
|
|
3600
|
+
return subprotocol;
|
|
3601
|
+
}
|
|
3602
|
+
requireSubprotocol();
|
|
3603
|
+
var websocketExports = requireWebsocket();
|
|
3604
|
+
const WebSocket$1 = /* @__PURE__ */ getDefaultExportFromCjs(websocketExports);
|
|
3605
|
+
var websocketServer;
|
|
3606
|
+
var hasRequiredWebsocketServer;
|
|
3607
|
+
function requireWebsocketServer() {
|
|
3608
|
+
if (hasRequiredWebsocketServer) return websocketServer;
|
|
3609
|
+
hasRequiredWebsocketServer = 1;
|
|
3610
|
+
const EventEmitter = require$$0$3;
|
|
3611
|
+
const http = require$$2$1;
|
|
3612
|
+
const { Duplex } = require$$0$2;
|
|
3613
|
+
const { createHash } = require$$1$1;
|
|
3614
|
+
const extension2 = requireExtension();
|
|
3615
|
+
const PerMessageDeflate = requirePermessageDeflate();
|
|
3616
|
+
const subprotocol2 = requireSubprotocol();
|
|
3617
|
+
const WebSocket2 = requireWebsocket();
|
|
3618
|
+
const { CLOSE_TIMEOUT, GUID, kWebSocket } = requireConstants();
|
|
3619
|
+
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
3620
|
+
const RUNNING = 0;
|
|
3621
|
+
const CLOSING = 1;
|
|
3622
|
+
const CLOSED = 2;
|
|
3623
|
+
class WebSocketServer extends EventEmitter {
|
|
3624
|
+
/**
|
|
3625
|
+
* Create a `WebSocketServer` instance.
|
|
3626
|
+
*
|
|
3627
|
+
* @param {Object} options Configuration options
|
|
3628
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
|
|
3629
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
3630
|
+
* multiple times in the same tick
|
|
3631
|
+
* @param {Boolean} [options.autoPong=true] Specifies whether or not to
|
|
3632
|
+
* automatically send a pong in response to a ping
|
|
3633
|
+
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
|
3634
|
+
* pending connections
|
|
3635
|
+
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
|
3636
|
+
* track clients
|
|
3637
|
+
* @param {Number} [options.closeTimeout=30000] Duration in milliseconds to
|
|
3638
|
+
* wait for the closing handshake to finish after `websocket.close()` is
|
|
3639
|
+
* called
|
|
3640
|
+
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
|
3641
|
+
* @param {String} [options.host] The hostname where to bind the server
|
|
3642
|
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
|
3643
|
+
* size
|
|
3644
|
+
* @param {Boolean} [options.noServer=false] Enable no server mode
|
|
3645
|
+
* @param {String} [options.path] Accept only connections matching this path
|
|
3646
|
+
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
|
3647
|
+
* permessage-deflate
|
|
3648
|
+
* @param {Number} [options.port] The port where to bind the server
|
|
3649
|
+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
|
3650
|
+
* server to use
|
|
3651
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
3652
|
+
* not to skip UTF-8 validation for text and close messages
|
|
3653
|
+
* @param {Function} [options.verifyClient] A hook to reject connections
|
|
3654
|
+
* @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
|
|
3655
|
+
* class to use. It must be the `WebSocket` class or class that extends it
|
|
3656
|
+
* @param {Function} [callback] A listener for the `listening` event
|
|
3657
|
+
*/
|
|
3658
|
+
constructor(options, callback) {
|
|
3659
|
+
super();
|
|
3660
|
+
options = {
|
|
3661
|
+
allowSynchronousEvents: true,
|
|
3662
|
+
autoPong: true,
|
|
3663
|
+
maxPayload: 100 * 1024 * 1024,
|
|
3664
|
+
skipUTF8Validation: false,
|
|
3665
|
+
perMessageDeflate: false,
|
|
3666
|
+
handleProtocols: null,
|
|
3667
|
+
clientTracking: true,
|
|
3668
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
3669
|
+
verifyClient: null,
|
|
3670
|
+
noServer: false,
|
|
3671
|
+
backlog: null,
|
|
3672
|
+
// use default (511 as implemented in net.js)
|
|
3673
|
+
server: null,
|
|
3674
|
+
host: null,
|
|
3675
|
+
path: null,
|
|
3676
|
+
port: null,
|
|
3677
|
+
WebSocket: WebSocket2,
|
|
3678
|
+
...options
|
|
3679
|
+
};
|
|
3680
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
|
|
3681
|
+
throw new TypeError(
|
|
3682
|
+
'One and only one of the "port", "server", or "noServer" options must be specified'
|
|
3683
|
+
);
|
|
3684
|
+
}
|
|
3685
|
+
if (options.port != null) {
|
|
3686
|
+
this._server = http.createServer((req, res) => {
|
|
3687
|
+
const body = http.STATUS_CODES[426];
|
|
3688
|
+
res.writeHead(426, {
|
|
3689
|
+
"Content-Length": body.length,
|
|
3690
|
+
"Content-Type": "text/plain"
|
|
3691
|
+
});
|
|
3692
|
+
res.end(body);
|
|
3693
|
+
});
|
|
3694
|
+
this._server.listen(
|
|
3695
|
+
options.port,
|
|
3696
|
+
options.host,
|
|
3697
|
+
options.backlog,
|
|
3698
|
+
callback
|
|
3699
|
+
);
|
|
3700
|
+
} else if (options.server) {
|
|
3701
|
+
this._server = options.server;
|
|
3702
|
+
}
|
|
3703
|
+
if (this._server) {
|
|
3704
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
3705
|
+
this._removeListeners = addListeners(this._server, {
|
|
3706
|
+
listening: this.emit.bind(this, "listening"),
|
|
3707
|
+
error: this.emit.bind(this, "error"),
|
|
3708
|
+
upgrade: (req, socket, head) => {
|
|
3709
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
3710
|
+
}
|
|
3711
|
+
});
|
|
3712
|
+
}
|
|
3713
|
+
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
|
|
3714
|
+
if (options.clientTracking) {
|
|
3715
|
+
this.clients = /* @__PURE__ */ new Set();
|
|
3716
|
+
this._shouldEmitClose = false;
|
|
3717
|
+
}
|
|
3718
|
+
this.options = options;
|
|
3719
|
+
this._state = RUNNING;
|
|
3720
|
+
}
|
|
3721
|
+
/**
|
|
3722
|
+
* Returns the bound address, the address family name, and port of the server
|
|
3723
|
+
* as reported by the operating system if listening on an IP socket.
|
|
3724
|
+
* If the server is listening on a pipe or UNIX domain socket, the name is
|
|
3725
|
+
* returned as a string.
|
|
3726
|
+
*
|
|
3727
|
+
* @return {(Object|String|null)} The address of the server
|
|
3728
|
+
* @public
|
|
3729
|
+
*/
|
|
3730
|
+
address() {
|
|
3731
|
+
if (this.options.noServer) {
|
|
3732
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
3733
|
+
}
|
|
3734
|
+
if (!this._server) return null;
|
|
3735
|
+
return this._server.address();
|
|
3736
|
+
}
|
|
3737
|
+
/**
|
|
3738
|
+
* Stop the server from accepting new connections and emit the `'close'` event
|
|
3739
|
+
* when all existing connections are closed.
|
|
3740
|
+
*
|
|
3741
|
+
* @param {Function} [cb] A one-time listener for the `'close'` event
|
|
3742
|
+
* @public
|
|
3743
|
+
*/
|
|
3744
|
+
close(cb) {
|
|
3745
|
+
if (this._state === CLOSED) {
|
|
3746
|
+
if (cb) {
|
|
3747
|
+
this.once("close", () => {
|
|
3748
|
+
cb(new Error("The server is not running"));
|
|
3749
|
+
});
|
|
3750
|
+
}
|
|
3751
|
+
process.nextTick(emitClose, this);
|
|
3752
|
+
return;
|
|
3753
|
+
}
|
|
3754
|
+
if (cb) this.once("close", cb);
|
|
3755
|
+
if (this._state === CLOSING) return;
|
|
3756
|
+
this._state = CLOSING;
|
|
3757
|
+
if (this.options.noServer || this.options.server) {
|
|
3758
|
+
if (this._server) {
|
|
3759
|
+
this._removeListeners();
|
|
3760
|
+
this._removeListeners = this._server = null;
|
|
3761
|
+
}
|
|
3762
|
+
if (this.clients) {
|
|
3763
|
+
if (!this.clients.size) {
|
|
3764
|
+
process.nextTick(emitClose, this);
|
|
3765
|
+
} else {
|
|
3766
|
+
this._shouldEmitClose = true;
|
|
3767
|
+
}
|
|
3768
|
+
} else {
|
|
3769
|
+
process.nextTick(emitClose, this);
|
|
3770
|
+
}
|
|
3771
|
+
} else {
|
|
3772
|
+
const server = this._server;
|
|
3773
|
+
this._removeListeners();
|
|
3774
|
+
this._removeListeners = this._server = null;
|
|
3775
|
+
server.close(() => {
|
|
3776
|
+
emitClose(this);
|
|
3777
|
+
});
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
/**
|
|
3781
|
+
* See if a given request should be handled by this server instance.
|
|
3782
|
+
*
|
|
3783
|
+
* @param {http.IncomingMessage} req Request object to inspect
|
|
3784
|
+
* @return {Boolean} `true` if the request is valid, else `false`
|
|
3785
|
+
* @public
|
|
3786
|
+
*/
|
|
3787
|
+
shouldHandle(req) {
|
|
3788
|
+
if (this.options.path) {
|
|
3789
|
+
const index = req.url.indexOf("?");
|
|
3790
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
|
3791
|
+
if (pathname !== this.options.path) return false;
|
|
3792
|
+
}
|
|
3793
|
+
return true;
|
|
3794
|
+
}
|
|
3795
|
+
/**
|
|
3796
|
+
* Handle a HTTP Upgrade request.
|
|
3797
|
+
*
|
|
3798
|
+
* @param {http.IncomingMessage} req The request object
|
|
3799
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
3800
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
3801
|
+
* @param {Function} cb Callback
|
|
3802
|
+
* @public
|
|
3803
|
+
*/
|
|
3804
|
+
handleUpgrade(req, socket, head, cb) {
|
|
3805
|
+
socket.on("error", socketOnError);
|
|
3806
|
+
const key = req.headers["sec-websocket-key"];
|
|
3807
|
+
const upgrade = req.headers.upgrade;
|
|
3808
|
+
const version = +req.headers["sec-websocket-version"];
|
|
3809
|
+
if (req.method !== "GET") {
|
|
3810
|
+
const message = "Invalid HTTP method";
|
|
3811
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
|
|
3812
|
+
return;
|
|
3813
|
+
}
|
|
3814
|
+
if (upgrade === void 0 || upgrade.toLowerCase() !== "websocket") {
|
|
3815
|
+
const message = "Invalid Upgrade header";
|
|
3816
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3817
|
+
return;
|
|
3818
|
+
}
|
|
3819
|
+
if (key === void 0 || !keyRegex.test(key)) {
|
|
3820
|
+
const message = "Missing or invalid Sec-WebSocket-Key header";
|
|
3821
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3822
|
+
return;
|
|
3823
|
+
}
|
|
3824
|
+
if (version !== 13 && version !== 8) {
|
|
3825
|
+
const message = "Missing or invalid Sec-WebSocket-Version header";
|
|
3826
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
|
|
3827
|
+
"Sec-WebSocket-Version": "13, 8"
|
|
3828
|
+
});
|
|
3829
|
+
return;
|
|
3830
|
+
}
|
|
3831
|
+
if (!this.shouldHandle(req)) {
|
|
3832
|
+
abortHandshake(socket, 400);
|
|
3833
|
+
return;
|
|
3834
|
+
}
|
|
3835
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
3836
|
+
let protocols = /* @__PURE__ */ new Set();
|
|
3837
|
+
if (secWebSocketProtocol !== void 0) {
|
|
3838
|
+
try {
|
|
3839
|
+
protocols = subprotocol2.parse(secWebSocketProtocol);
|
|
3840
|
+
} catch (err) {
|
|
3841
|
+
const message = "Invalid Sec-WebSocket-Protocol header";
|
|
3842
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3843
|
+
return;
|
|
3844
|
+
}
|
|
3845
|
+
}
|
|
3846
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
3847
|
+
const extensions = {};
|
|
3848
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== void 0) {
|
|
3849
|
+
const perMessageDeflate = new PerMessageDeflate({
|
|
3850
|
+
...this.options.perMessageDeflate,
|
|
3851
|
+
isServer: true,
|
|
3852
|
+
maxPayload: this.options.maxPayload
|
|
3853
|
+
});
|
|
3854
|
+
try {
|
|
3855
|
+
const offers = extension2.parse(secWebSocketExtensions);
|
|
3856
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
3857
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
3858
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
3859
|
+
}
|
|
3860
|
+
} catch (err) {
|
|
3861
|
+
const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
3862
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3863
|
+
return;
|
|
3864
|
+
}
|
|
3865
|
+
}
|
|
3866
|
+
if (this.options.verifyClient) {
|
|
3867
|
+
const info = {
|
|
3868
|
+
origin: req.headers[`${version === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
3869
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
3870
|
+
req
|
|
3871
|
+
};
|
|
3872
|
+
if (this.options.verifyClient.length === 2) {
|
|
3873
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
3874
|
+
if (!verified) {
|
|
3875
|
+
return abortHandshake(socket, code || 401, message, headers);
|
|
3876
|
+
}
|
|
3877
|
+
this.completeUpgrade(
|
|
3878
|
+
extensions,
|
|
3879
|
+
key,
|
|
3880
|
+
protocols,
|
|
3881
|
+
req,
|
|
3882
|
+
socket,
|
|
3883
|
+
head,
|
|
3884
|
+
cb
|
|
3885
|
+
);
|
|
3886
|
+
});
|
|
3887
|
+
return;
|
|
3888
|
+
}
|
|
3889
|
+
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
|
3890
|
+
}
|
|
3891
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
3892
|
+
}
|
|
3893
|
+
/**
|
|
3894
|
+
* Upgrade the connection to WebSocket.
|
|
3895
|
+
*
|
|
3896
|
+
* @param {Object} extensions The accepted extensions
|
|
3897
|
+
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
|
3898
|
+
* @param {Set} protocols The subprotocols
|
|
3899
|
+
* @param {http.IncomingMessage} req The request object
|
|
3900
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
3901
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
3902
|
+
* @param {Function} cb Callback
|
|
3903
|
+
* @throws {Error} If called more than once with the same socket
|
|
3904
|
+
* @private
|
|
3905
|
+
*/
|
|
3906
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
3907
|
+
if (!socket.readable || !socket.writable) return socket.destroy();
|
|
3908
|
+
if (socket[kWebSocket]) {
|
|
3909
|
+
throw new Error(
|
|
3910
|
+
"server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration"
|
|
3911
|
+
);
|
|
3912
|
+
}
|
|
3913
|
+
if (this._state > RUNNING) return abortHandshake(socket, 503);
|
|
3914
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
3915
|
+
const headers = [
|
|
3916
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
3917
|
+
"Upgrade: websocket",
|
|
3918
|
+
"Connection: Upgrade",
|
|
3919
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
3920
|
+
];
|
|
3921
|
+
const ws = new this.options.WebSocket(null, void 0, this.options);
|
|
3922
|
+
if (protocols.size) {
|
|
3923
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
3924
|
+
if (protocol) {
|
|
3925
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
3926
|
+
ws._protocol = protocol;
|
|
3927
|
+
}
|
|
3928
|
+
}
|
|
3929
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
3930
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
3931
|
+
const value = extension2.format({
|
|
3932
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
3933
|
+
});
|
|
3934
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
3935
|
+
ws._extensions = extensions;
|
|
3936
|
+
}
|
|
3937
|
+
this.emit("headers", headers, req);
|
|
3938
|
+
socket.write(headers.concat("\r\n").join("\r\n"));
|
|
3939
|
+
socket.removeListener("error", socketOnError);
|
|
3940
|
+
ws.setSocket(socket, head, {
|
|
3941
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
3942
|
+
maxPayload: this.options.maxPayload,
|
|
3943
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
3944
|
+
});
|
|
3945
|
+
if (this.clients) {
|
|
3946
|
+
this.clients.add(ws);
|
|
3947
|
+
ws.on("close", () => {
|
|
3948
|
+
this.clients.delete(ws);
|
|
3949
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
3950
|
+
process.nextTick(emitClose, this);
|
|
3951
|
+
}
|
|
3952
|
+
});
|
|
3953
|
+
}
|
|
3954
|
+
cb(ws, req);
|
|
3955
|
+
}
|
|
3956
|
+
}
|
|
3957
|
+
websocketServer = WebSocketServer;
|
|
3958
|
+
function addListeners(server, map) {
|
|
3959
|
+
for (const event of Object.keys(map)) server.on(event, map[event]);
|
|
3960
|
+
return function removeListeners() {
|
|
3961
|
+
for (const event of Object.keys(map)) {
|
|
3962
|
+
server.removeListener(event, map[event]);
|
|
3963
|
+
}
|
|
3964
|
+
};
|
|
3965
|
+
}
|
|
3966
|
+
function emitClose(server) {
|
|
3967
|
+
server._state = CLOSED;
|
|
3968
|
+
server.emit("close");
|
|
3969
|
+
}
|
|
3970
|
+
function socketOnError() {
|
|
3971
|
+
this.destroy();
|
|
3972
|
+
}
|
|
3973
|
+
function abortHandshake(socket, code, message, headers) {
|
|
3974
|
+
message = message || http.STATUS_CODES[code];
|
|
3975
|
+
headers = {
|
|
3976
|
+
Connection: "close",
|
|
3977
|
+
"Content-Type": "text/html",
|
|
3978
|
+
"Content-Length": Buffer.byteLength(message),
|
|
3979
|
+
...headers
|
|
3980
|
+
};
|
|
3981
|
+
socket.once("finish", socket.destroy);
|
|
3982
|
+
socket.end(
|
|
3983
|
+
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
3984
|
+
` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join("\r\n") + "\r\n\r\n" + message
|
|
3985
|
+
);
|
|
3986
|
+
}
|
|
3987
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
3988
|
+
if (server.listenerCount("wsClientError")) {
|
|
3989
|
+
const err = new Error(message);
|
|
3990
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
3991
|
+
server.emit("wsClientError", err, socket, req);
|
|
3992
|
+
} else {
|
|
3993
|
+
abortHandshake(socket, code, message, headers);
|
|
3994
|
+
}
|
|
3995
|
+
}
|
|
3996
|
+
return websocketServer;
|
|
3997
|
+
}
|
|
3998
|
+
requireWebsocketServer();
|
|
3999
|
+
const WS = typeof WebSocket !== "undefined" ? WebSocket : WebSocket$1;
|
|
326
4000
|
function log$2(...args) {
|
|
327
4001
|
try {
|
|
328
4002
|
console.log(...args);
|
|
@@ -364,7 +4038,7 @@ class WorkerWsClient {
|
|
|
364
4038
|
}
|
|
365
4039
|
generateFingerprint() {
|
|
366
4040
|
const data = `${os.hostname()}:${os.userInfo().username}:${os.platform()}:${os.arch()}`;
|
|
367
|
-
return
|
|
4041
|
+
return require$$1$1.createHash("sha256").update(data).digest("hex").slice(0, 32);
|
|
368
4042
|
}
|
|
369
4043
|
setStatus(status) {
|
|
370
4044
|
if (this._status !== status) {
|
|
@@ -447,7 +4121,7 @@ class WorkerWsClient {
|
|
|
447
4121
|
const url = `${wsBase}/api/ws/worker?token=${encodeURIComponent(this.apiKey)}`;
|
|
448
4122
|
log$2(`[worker-ws] Connecting to ${url.replace(/token=.*/, "token=***")}...`);
|
|
449
4123
|
try {
|
|
450
|
-
this.ws = new
|
|
4124
|
+
this.ws = new WS(url);
|
|
451
4125
|
} catch (err) {
|
|
452
4126
|
log$2("[worker-ws] Failed to create WebSocket:", err);
|
|
453
4127
|
this.scheduleReconnect();
|
|
@@ -534,7 +4208,7 @@ class WorkerWsClient {
|
|
|
534
4208
|
}
|
|
535
4209
|
}
|
|
536
4210
|
send(data) {
|
|
537
|
-
if (this.ws && this.ws.readyState ===
|
|
4211
|
+
if (this.ws && this.ws.readyState === WS.OPEN) {
|
|
538
4212
|
this.ws.send(JSON.stringify(data));
|
|
539
4213
|
}
|
|
540
4214
|
}
|