@kodama-run/sdk 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +3061 -181
- package/bin/mcp.js +3037 -167
- package/package.json +3 -3
- package/src/adapters/mcp.ts +13 -23
- package/src/room.ts +9 -3
package/bin/mcp.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
2
|
-
|
|
1
|
+
#!/usr/bin/env bun
|
|
3
2
|
// @bun
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
4
|
var __create = Object.create;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __defProp = Object.defineProperty;
|
|
@@ -45,6 +45,7 @@ var __export = (target, all) => {
|
|
|
45
45
|
set: __exportSetter.bind(all, name)
|
|
46
46
|
});
|
|
47
47
|
};
|
|
48
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
48
49
|
|
|
49
50
|
// ../../node_modules/.pnpm/ajv@8.18.0/node_modules/ajv/dist/compile/codegen/code.js
|
|
50
51
|
var require_code = __commonJS((exports) => {
|
|
@@ -6349,173 +6350,3047 @@ var require_formats = __commonJS((exports) => {
|
|
|
6349
6350
|
return -1;
|
|
6350
6351
|
return 0;
|
|
6351
6352
|
}
|
|
6352
|
-
var DATE_TIME_SEPARATOR = /t|\s/i;
|
|
6353
|
-
function getDateTime(strictTimeZone) {
|
|
6354
|
-
const time3 = getTime(strictTimeZone);
|
|
6355
|
-
return function date_time(str) {
|
|
6356
|
-
const dateTime = str.split(DATE_TIME_SEPARATOR);
|
|
6357
|
-
return dateTime.length === 2 && date6(dateTime[0]) && time3(dateTime[1]);
|
|
6358
|
-
};
|
|
6353
|
+
var DATE_TIME_SEPARATOR = /t|\s/i;
|
|
6354
|
+
function getDateTime(strictTimeZone) {
|
|
6355
|
+
const time3 = getTime(strictTimeZone);
|
|
6356
|
+
return function date_time(str) {
|
|
6357
|
+
const dateTime = str.split(DATE_TIME_SEPARATOR);
|
|
6358
|
+
return dateTime.length === 2 && date6(dateTime[0]) && time3(dateTime[1]);
|
|
6359
|
+
};
|
|
6360
|
+
}
|
|
6361
|
+
function compareDateTime(dt1, dt2) {
|
|
6362
|
+
if (!(dt1 && dt2))
|
|
6363
|
+
return;
|
|
6364
|
+
const d1 = new Date(dt1).valueOf();
|
|
6365
|
+
const d2 = new Date(dt2).valueOf();
|
|
6366
|
+
if (!(d1 && d2))
|
|
6367
|
+
return;
|
|
6368
|
+
return d1 - d2;
|
|
6369
|
+
}
|
|
6370
|
+
function compareIsoDateTime(dt1, dt2) {
|
|
6371
|
+
if (!(dt1 && dt2))
|
|
6372
|
+
return;
|
|
6373
|
+
const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR);
|
|
6374
|
+
const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR);
|
|
6375
|
+
const res = compareDate(d1, d2);
|
|
6376
|
+
if (res === undefined)
|
|
6377
|
+
return;
|
|
6378
|
+
return res || compareTime(t1, t2);
|
|
6379
|
+
}
|
|
6380
|
+
var NOT_URI_FRAGMENT = /\/|:/;
|
|
6381
|
+
var URI = /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
|
|
6382
|
+
function uri(str) {
|
|
6383
|
+
return NOT_URI_FRAGMENT.test(str) && URI.test(str);
|
|
6384
|
+
}
|
|
6385
|
+
var BYTE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm;
|
|
6386
|
+
function byte(str) {
|
|
6387
|
+
BYTE.lastIndex = 0;
|
|
6388
|
+
return BYTE.test(str);
|
|
6389
|
+
}
|
|
6390
|
+
var MIN_INT32 = -(2 ** 31);
|
|
6391
|
+
var MAX_INT32 = 2 ** 31 - 1;
|
|
6392
|
+
function validateInt32(value) {
|
|
6393
|
+
return Number.isInteger(value) && value <= MAX_INT32 && value >= MIN_INT32;
|
|
6394
|
+
}
|
|
6395
|
+
function validateInt64(value) {
|
|
6396
|
+
return Number.isInteger(value);
|
|
6397
|
+
}
|
|
6398
|
+
function validateNumber() {
|
|
6399
|
+
return true;
|
|
6400
|
+
}
|
|
6401
|
+
var Z_ANCHOR = /[^\\]\\Z/;
|
|
6402
|
+
function regex(str) {
|
|
6403
|
+
if (Z_ANCHOR.test(str))
|
|
6404
|
+
return false;
|
|
6405
|
+
try {
|
|
6406
|
+
new RegExp(str);
|
|
6407
|
+
return true;
|
|
6408
|
+
} catch (e) {
|
|
6409
|
+
return false;
|
|
6410
|
+
}
|
|
6411
|
+
}
|
|
6412
|
+
});
|
|
6413
|
+
|
|
6414
|
+
// ../../node_modules/.pnpm/ajv-formats@3.0.1_ajv@8.18.0/node_modules/ajv-formats/dist/limit.js
|
|
6415
|
+
var require_limit = __commonJS((exports) => {
|
|
6416
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6417
|
+
exports.formatLimitDefinition = undefined;
|
|
6418
|
+
var ajv_1 = require_ajv();
|
|
6419
|
+
var codegen_1 = require_codegen();
|
|
6420
|
+
var ops = codegen_1.operators;
|
|
6421
|
+
var KWDs = {
|
|
6422
|
+
formatMaximum: { okStr: "<=", ok: ops.LTE, fail: ops.GT },
|
|
6423
|
+
formatMinimum: { okStr: ">=", ok: ops.GTE, fail: ops.LT },
|
|
6424
|
+
formatExclusiveMaximum: { okStr: "<", ok: ops.LT, fail: ops.GTE },
|
|
6425
|
+
formatExclusiveMinimum: { okStr: ">", ok: ops.GT, fail: ops.LTE }
|
|
6426
|
+
};
|
|
6427
|
+
var error48 = {
|
|
6428
|
+
message: ({ keyword, schemaCode }) => (0, codegen_1.str)`should be ${KWDs[keyword].okStr} ${schemaCode}`,
|
|
6429
|
+
params: ({ keyword, schemaCode }) => (0, codegen_1._)`{comparison: ${KWDs[keyword].okStr}, limit: ${schemaCode}}`
|
|
6430
|
+
};
|
|
6431
|
+
exports.formatLimitDefinition = {
|
|
6432
|
+
keyword: Object.keys(KWDs),
|
|
6433
|
+
type: "string",
|
|
6434
|
+
schemaType: "string",
|
|
6435
|
+
$data: true,
|
|
6436
|
+
error: error48,
|
|
6437
|
+
code(cxt) {
|
|
6438
|
+
const { gen, data, schemaCode, keyword, it } = cxt;
|
|
6439
|
+
const { opts, self } = it;
|
|
6440
|
+
if (!opts.validateFormats)
|
|
6441
|
+
return;
|
|
6442
|
+
const fCxt = new ajv_1.KeywordCxt(it, self.RULES.all.format.definition, "format");
|
|
6443
|
+
if (fCxt.$data)
|
|
6444
|
+
validate$DataFormat();
|
|
6445
|
+
else
|
|
6446
|
+
validateFormat();
|
|
6447
|
+
function validate$DataFormat() {
|
|
6448
|
+
const fmts = gen.scopeValue("formats", {
|
|
6449
|
+
ref: self.formats,
|
|
6450
|
+
code: opts.code.formats
|
|
6451
|
+
});
|
|
6452
|
+
const fmt = gen.const("fmt", (0, codegen_1._)`${fmts}[${fCxt.schemaCode}]`);
|
|
6453
|
+
cxt.fail$data((0, codegen_1.or)((0, codegen_1._)`typeof ${fmt} != "object"`, (0, codegen_1._)`${fmt} instanceof RegExp`, (0, codegen_1._)`typeof ${fmt}.compare != "function"`, compareCode(fmt)));
|
|
6454
|
+
}
|
|
6455
|
+
function validateFormat() {
|
|
6456
|
+
const format = fCxt.schema;
|
|
6457
|
+
const fmtDef = self.formats[format];
|
|
6458
|
+
if (!fmtDef || fmtDef === true)
|
|
6459
|
+
return;
|
|
6460
|
+
if (typeof fmtDef != "object" || fmtDef instanceof RegExp || typeof fmtDef.compare != "function") {
|
|
6461
|
+
throw new Error(`"${keyword}": format "${format}" does not define "compare" function`);
|
|
6462
|
+
}
|
|
6463
|
+
const fmt = gen.scopeValue("formats", {
|
|
6464
|
+
key: format,
|
|
6465
|
+
ref: fmtDef,
|
|
6466
|
+
code: opts.code.formats ? (0, codegen_1._)`${opts.code.formats}${(0, codegen_1.getProperty)(format)}` : undefined
|
|
6467
|
+
});
|
|
6468
|
+
cxt.fail$data(compareCode(fmt));
|
|
6469
|
+
}
|
|
6470
|
+
function compareCode(fmt) {
|
|
6471
|
+
return (0, codegen_1._)`${fmt}.compare(${data}, ${schemaCode}) ${KWDs[keyword].fail} 0`;
|
|
6472
|
+
}
|
|
6473
|
+
},
|
|
6474
|
+
dependencies: ["format"]
|
|
6475
|
+
};
|
|
6476
|
+
var formatLimitPlugin = (ajv) => {
|
|
6477
|
+
ajv.addKeyword(exports.formatLimitDefinition);
|
|
6478
|
+
return ajv;
|
|
6479
|
+
};
|
|
6480
|
+
exports.default = formatLimitPlugin;
|
|
6481
|
+
});
|
|
6482
|
+
|
|
6483
|
+
// ../../node_modules/.pnpm/ajv-formats@3.0.1_ajv@8.18.0/node_modules/ajv-formats/dist/index.js
|
|
6484
|
+
var require_dist = __commonJS((exports, module) => {
|
|
6485
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6486
|
+
var formats_1 = require_formats();
|
|
6487
|
+
var limit_1 = require_limit();
|
|
6488
|
+
var codegen_1 = require_codegen();
|
|
6489
|
+
var fullName = new codegen_1.Name("fullFormats");
|
|
6490
|
+
var fastName = new codegen_1.Name("fastFormats");
|
|
6491
|
+
var formatsPlugin = (ajv, opts = { keywords: true }) => {
|
|
6492
|
+
if (Array.isArray(opts)) {
|
|
6493
|
+
addFormats(ajv, opts, formats_1.fullFormats, fullName);
|
|
6494
|
+
return ajv;
|
|
6495
|
+
}
|
|
6496
|
+
const [formats, exportName] = opts.mode === "fast" ? [formats_1.fastFormats, fastName] : [formats_1.fullFormats, fullName];
|
|
6497
|
+
const list = opts.formats || formats_1.formatNames;
|
|
6498
|
+
addFormats(ajv, list, formats, exportName);
|
|
6499
|
+
if (opts.keywords)
|
|
6500
|
+
(0, limit_1.default)(ajv);
|
|
6501
|
+
return ajv;
|
|
6502
|
+
};
|
|
6503
|
+
formatsPlugin.get = (name, mode = "full") => {
|
|
6504
|
+
const formats = mode === "fast" ? formats_1.fastFormats : formats_1.fullFormats;
|
|
6505
|
+
const f = formats[name];
|
|
6506
|
+
if (!f)
|
|
6507
|
+
throw new Error(`Unknown format "${name}"`);
|
|
6508
|
+
return f;
|
|
6509
|
+
};
|
|
6510
|
+
function addFormats(ajv, list, fs, exportName) {
|
|
6511
|
+
var _a2;
|
|
6512
|
+
var _b;
|
|
6513
|
+
(_a2 = (_b = ajv.opts.code).formats) !== null && _a2 !== undefined || (_b.formats = (0, codegen_1._)`require("ajv-formats/dist/formats").${exportName}`);
|
|
6514
|
+
for (const f of list)
|
|
6515
|
+
ajv.addFormat(f, fs[f]);
|
|
6516
|
+
}
|
|
6517
|
+
module.exports = exports = formatsPlugin;
|
|
6518
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6519
|
+
exports.default = formatsPlugin;
|
|
6520
|
+
});
|
|
6521
|
+
|
|
6522
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/constants.js
|
|
6523
|
+
var require_constants = __commonJS((exports, module) => {
|
|
6524
|
+
var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
6525
|
+
var hasBlob = typeof Blob !== "undefined";
|
|
6526
|
+
if (hasBlob)
|
|
6527
|
+
BINARY_TYPES.push("blob");
|
|
6528
|
+
module.exports = {
|
|
6529
|
+
BINARY_TYPES,
|
|
6530
|
+
CLOSE_TIMEOUT: 30000,
|
|
6531
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
6532
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
6533
|
+
hasBlob,
|
|
6534
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
6535
|
+
kListener: Symbol("kListener"),
|
|
6536
|
+
kStatusCode: Symbol("status-code"),
|
|
6537
|
+
kWebSocket: Symbol("websocket"),
|
|
6538
|
+
NOOP: () => {}
|
|
6539
|
+
};
|
|
6540
|
+
});
|
|
6541
|
+
|
|
6542
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/buffer-util.js
|
|
6543
|
+
var require_buffer_util = __commonJS((exports, module) => {
|
|
6544
|
+
var { EMPTY_BUFFER } = require_constants();
|
|
6545
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
6546
|
+
function concat(list, totalLength) {
|
|
6547
|
+
if (list.length === 0)
|
|
6548
|
+
return EMPTY_BUFFER;
|
|
6549
|
+
if (list.length === 1)
|
|
6550
|
+
return list[0];
|
|
6551
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
6552
|
+
let offset = 0;
|
|
6553
|
+
for (let i = 0;i < list.length; i++) {
|
|
6554
|
+
const buf = list[i];
|
|
6555
|
+
target.set(buf, offset);
|
|
6556
|
+
offset += buf.length;
|
|
6557
|
+
}
|
|
6558
|
+
if (offset < totalLength) {
|
|
6559
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
6560
|
+
}
|
|
6561
|
+
return target;
|
|
6562
|
+
}
|
|
6563
|
+
function _mask(source, mask, output, offset, length) {
|
|
6564
|
+
for (let i = 0;i < length; i++) {
|
|
6565
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
6566
|
+
}
|
|
6567
|
+
}
|
|
6568
|
+
function _unmask(buffer, mask) {
|
|
6569
|
+
for (let i = 0;i < buffer.length; i++) {
|
|
6570
|
+
buffer[i] ^= mask[i & 3];
|
|
6571
|
+
}
|
|
6572
|
+
}
|
|
6573
|
+
function toArrayBuffer(buf) {
|
|
6574
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
6575
|
+
return buf.buffer;
|
|
6576
|
+
}
|
|
6577
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
6578
|
+
}
|
|
6579
|
+
function toBuffer(data) {
|
|
6580
|
+
toBuffer.readOnly = true;
|
|
6581
|
+
if (Buffer.isBuffer(data))
|
|
6582
|
+
return data;
|
|
6583
|
+
let buf;
|
|
6584
|
+
if (data instanceof ArrayBuffer) {
|
|
6585
|
+
buf = new FastBuffer(data);
|
|
6586
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
6587
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
6588
|
+
} else {
|
|
6589
|
+
buf = Buffer.from(data);
|
|
6590
|
+
toBuffer.readOnly = false;
|
|
6591
|
+
}
|
|
6592
|
+
return buf;
|
|
6593
|
+
}
|
|
6594
|
+
module.exports = {
|
|
6595
|
+
concat,
|
|
6596
|
+
mask: _mask,
|
|
6597
|
+
toArrayBuffer,
|
|
6598
|
+
toBuffer,
|
|
6599
|
+
unmask: _unmask
|
|
6600
|
+
};
|
|
6601
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
6602
|
+
try {
|
|
6603
|
+
const bufferUtil = (()=>{throw new Error("Cannot require module "+"bufferutil");})();
|
|
6604
|
+
module.exports.mask = function(source, mask, output, offset, length) {
|
|
6605
|
+
if (length < 48)
|
|
6606
|
+
_mask(source, mask, output, offset, length);
|
|
6607
|
+
else
|
|
6608
|
+
bufferUtil.mask(source, mask, output, offset, length);
|
|
6609
|
+
};
|
|
6610
|
+
module.exports.unmask = function(buffer, mask) {
|
|
6611
|
+
if (buffer.length < 32)
|
|
6612
|
+
_unmask(buffer, mask);
|
|
6613
|
+
else
|
|
6614
|
+
bufferUtil.unmask(buffer, mask);
|
|
6615
|
+
};
|
|
6616
|
+
} catch (e) {}
|
|
6617
|
+
}
|
|
6618
|
+
});
|
|
6619
|
+
|
|
6620
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/limiter.js
|
|
6621
|
+
var require_limiter = __commonJS((exports, module) => {
|
|
6622
|
+
var kDone = Symbol("kDone");
|
|
6623
|
+
var kRun = Symbol("kRun");
|
|
6624
|
+
|
|
6625
|
+
class Limiter {
|
|
6626
|
+
constructor(concurrency) {
|
|
6627
|
+
this[kDone] = () => {
|
|
6628
|
+
this.pending--;
|
|
6629
|
+
this[kRun]();
|
|
6630
|
+
};
|
|
6631
|
+
this.concurrency = concurrency || Infinity;
|
|
6632
|
+
this.jobs = [];
|
|
6633
|
+
this.pending = 0;
|
|
6634
|
+
}
|
|
6635
|
+
add(job) {
|
|
6636
|
+
this.jobs.push(job);
|
|
6637
|
+
this[kRun]();
|
|
6638
|
+
}
|
|
6639
|
+
[kRun]() {
|
|
6640
|
+
if (this.pending === this.concurrency)
|
|
6641
|
+
return;
|
|
6642
|
+
if (this.jobs.length) {
|
|
6643
|
+
const job = this.jobs.shift();
|
|
6644
|
+
this.pending++;
|
|
6645
|
+
job(this[kDone]);
|
|
6646
|
+
}
|
|
6647
|
+
}
|
|
6648
|
+
}
|
|
6649
|
+
module.exports = Limiter;
|
|
6650
|
+
});
|
|
6651
|
+
|
|
6652
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/permessage-deflate.js
|
|
6653
|
+
var require_permessage_deflate = __commonJS((exports, module) => {
|
|
6654
|
+
var zlib = __require("zlib");
|
|
6655
|
+
var bufferUtil = require_buffer_util();
|
|
6656
|
+
var Limiter = require_limiter();
|
|
6657
|
+
var { kStatusCode } = require_constants();
|
|
6658
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
6659
|
+
var TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
6660
|
+
var kPerMessageDeflate = Symbol("permessage-deflate");
|
|
6661
|
+
var kTotalLength = Symbol("total-length");
|
|
6662
|
+
var kCallback = Symbol("callback");
|
|
6663
|
+
var kBuffers = Symbol("buffers");
|
|
6664
|
+
var kError = Symbol("error");
|
|
6665
|
+
var zlibLimiter;
|
|
6666
|
+
|
|
6667
|
+
class PerMessageDeflate {
|
|
6668
|
+
constructor(options) {
|
|
6669
|
+
this._options = options || {};
|
|
6670
|
+
this._threshold = this._options.threshold !== undefined ? this._options.threshold : 1024;
|
|
6671
|
+
this._maxPayload = this._options.maxPayload | 0;
|
|
6672
|
+
this._isServer = !!this._options.isServer;
|
|
6673
|
+
this._deflate = null;
|
|
6674
|
+
this._inflate = null;
|
|
6675
|
+
this.params = null;
|
|
6676
|
+
if (!zlibLimiter) {
|
|
6677
|
+
const concurrency = this._options.concurrencyLimit !== undefined ? this._options.concurrencyLimit : 10;
|
|
6678
|
+
zlibLimiter = new Limiter(concurrency);
|
|
6679
|
+
}
|
|
6680
|
+
}
|
|
6681
|
+
static get extensionName() {
|
|
6682
|
+
return "permessage-deflate";
|
|
6683
|
+
}
|
|
6684
|
+
offer() {
|
|
6685
|
+
const params = {};
|
|
6686
|
+
if (this._options.serverNoContextTakeover) {
|
|
6687
|
+
params.server_no_context_takeover = true;
|
|
6688
|
+
}
|
|
6689
|
+
if (this._options.clientNoContextTakeover) {
|
|
6690
|
+
params.client_no_context_takeover = true;
|
|
6691
|
+
}
|
|
6692
|
+
if (this._options.serverMaxWindowBits) {
|
|
6693
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
6694
|
+
}
|
|
6695
|
+
if (this._options.clientMaxWindowBits) {
|
|
6696
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
6697
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
6698
|
+
params.client_max_window_bits = true;
|
|
6699
|
+
}
|
|
6700
|
+
return params;
|
|
6701
|
+
}
|
|
6702
|
+
accept(configurations) {
|
|
6703
|
+
configurations = this.normalizeParams(configurations);
|
|
6704
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
6705
|
+
return this.params;
|
|
6706
|
+
}
|
|
6707
|
+
cleanup() {
|
|
6708
|
+
if (this._inflate) {
|
|
6709
|
+
this._inflate.close();
|
|
6710
|
+
this._inflate = null;
|
|
6711
|
+
}
|
|
6712
|
+
if (this._deflate) {
|
|
6713
|
+
const callback = this._deflate[kCallback];
|
|
6714
|
+
this._deflate.close();
|
|
6715
|
+
this._deflate = null;
|
|
6716
|
+
if (callback) {
|
|
6717
|
+
callback(new Error("The deflate stream was closed while data was being processed"));
|
|
6718
|
+
}
|
|
6719
|
+
}
|
|
6720
|
+
}
|
|
6721
|
+
acceptAsServer(offers) {
|
|
6722
|
+
const opts = this._options;
|
|
6723
|
+
const accepted = offers.find((params) => {
|
|
6724
|
+
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) {
|
|
6725
|
+
return false;
|
|
6726
|
+
}
|
|
6727
|
+
return true;
|
|
6728
|
+
});
|
|
6729
|
+
if (!accepted) {
|
|
6730
|
+
throw new Error("None of the extension offers can be accepted");
|
|
6731
|
+
}
|
|
6732
|
+
if (opts.serverNoContextTakeover) {
|
|
6733
|
+
accepted.server_no_context_takeover = true;
|
|
6734
|
+
}
|
|
6735
|
+
if (opts.clientNoContextTakeover) {
|
|
6736
|
+
accepted.client_no_context_takeover = true;
|
|
6737
|
+
}
|
|
6738
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
6739
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
6740
|
+
}
|
|
6741
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
6742
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
6743
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
6744
|
+
delete accepted.client_max_window_bits;
|
|
6745
|
+
}
|
|
6746
|
+
return accepted;
|
|
6747
|
+
}
|
|
6748
|
+
acceptAsClient(response) {
|
|
6749
|
+
const params = response[0];
|
|
6750
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
6751
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
6752
|
+
}
|
|
6753
|
+
if (!params.client_max_window_bits) {
|
|
6754
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
6755
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
6756
|
+
}
|
|
6757
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
6758
|
+
throw new Error('Unexpected or invalid parameter "client_max_window_bits"');
|
|
6759
|
+
}
|
|
6760
|
+
return params;
|
|
6761
|
+
}
|
|
6762
|
+
normalizeParams(configurations) {
|
|
6763
|
+
configurations.forEach((params) => {
|
|
6764
|
+
Object.keys(params).forEach((key) => {
|
|
6765
|
+
let value = params[key];
|
|
6766
|
+
if (value.length > 1) {
|
|
6767
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
6768
|
+
}
|
|
6769
|
+
value = value[0];
|
|
6770
|
+
if (key === "client_max_window_bits") {
|
|
6771
|
+
if (value !== true) {
|
|
6772
|
+
const num = +value;
|
|
6773
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
6774
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
6775
|
+
}
|
|
6776
|
+
value = num;
|
|
6777
|
+
} else if (!this._isServer) {
|
|
6778
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
6779
|
+
}
|
|
6780
|
+
} else if (key === "server_max_window_bits") {
|
|
6781
|
+
const num = +value;
|
|
6782
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
6783
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
6784
|
+
}
|
|
6785
|
+
value = num;
|
|
6786
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
6787
|
+
if (value !== true) {
|
|
6788
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
6789
|
+
}
|
|
6790
|
+
} else {
|
|
6791
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
6792
|
+
}
|
|
6793
|
+
params[key] = value;
|
|
6794
|
+
});
|
|
6795
|
+
});
|
|
6796
|
+
return configurations;
|
|
6797
|
+
}
|
|
6798
|
+
decompress(data, fin, callback) {
|
|
6799
|
+
zlibLimiter.add((done) => {
|
|
6800
|
+
this._decompress(data, fin, (err, result) => {
|
|
6801
|
+
done();
|
|
6802
|
+
callback(err, result);
|
|
6803
|
+
});
|
|
6804
|
+
});
|
|
6805
|
+
}
|
|
6806
|
+
compress(data, fin, callback) {
|
|
6807
|
+
zlibLimiter.add((done) => {
|
|
6808
|
+
this._compress(data, fin, (err, result) => {
|
|
6809
|
+
done();
|
|
6810
|
+
callback(err, result);
|
|
6811
|
+
});
|
|
6812
|
+
});
|
|
6813
|
+
}
|
|
6814
|
+
_decompress(data, fin, callback) {
|
|
6815
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
6816
|
+
if (!this._inflate) {
|
|
6817
|
+
const key = `${endpoint}_max_window_bits`;
|
|
6818
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
6819
|
+
this._inflate = zlib.createInflateRaw({
|
|
6820
|
+
...this._options.zlibInflateOptions,
|
|
6821
|
+
windowBits
|
|
6822
|
+
});
|
|
6823
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
6824
|
+
this._inflate[kTotalLength] = 0;
|
|
6825
|
+
this._inflate[kBuffers] = [];
|
|
6826
|
+
this._inflate.on("error", inflateOnError);
|
|
6827
|
+
this._inflate.on("data", inflateOnData);
|
|
6828
|
+
}
|
|
6829
|
+
this._inflate[kCallback] = callback;
|
|
6830
|
+
this._inflate.write(data);
|
|
6831
|
+
if (fin)
|
|
6832
|
+
this._inflate.write(TRAILER);
|
|
6833
|
+
this._inflate.flush(() => {
|
|
6834
|
+
const err = this._inflate[kError];
|
|
6835
|
+
if (err) {
|
|
6836
|
+
this._inflate.close();
|
|
6837
|
+
this._inflate = null;
|
|
6838
|
+
callback(err);
|
|
6839
|
+
return;
|
|
6840
|
+
}
|
|
6841
|
+
const data2 = bufferUtil.concat(this._inflate[kBuffers], this._inflate[kTotalLength]);
|
|
6842
|
+
if (this._inflate._readableState.endEmitted) {
|
|
6843
|
+
this._inflate.close();
|
|
6844
|
+
this._inflate = null;
|
|
6845
|
+
} else {
|
|
6846
|
+
this._inflate[kTotalLength] = 0;
|
|
6847
|
+
this._inflate[kBuffers] = [];
|
|
6848
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
6849
|
+
this._inflate.reset();
|
|
6850
|
+
}
|
|
6851
|
+
}
|
|
6852
|
+
callback(null, data2);
|
|
6853
|
+
});
|
|
6854
|
+
}
|
|
6855
|
+
_compress(data, fin, callback) {
|
|
6856
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
6857
|
+
if (!this._deflate) {
|
|
6858
|
+
const key = `${endpoint}_max_window_bits`;
|
|
6859
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
6860
|
+
this._deflate = zlib.createDeflateRaw({
|
|
6861
|
+
...this._options.zlibDeflateOptions,
|
|
6862
|
+
windowBits
|
|
6863
|
+
});
|
|
6864
|
+
this._deflate[kTotalLength] = 0;
|
|
6865
|
+
this._deflate[kBuffers] = [];
|
|
6866
|
+
this._deflate.on("data", deflateOnData);
|
|
6867
|
+
}
|
|
6868
|
+
this._deflate[kCallback] = callback;
|
|
6869
|
+
this._deflate.write(data);
|
|
6870
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
6871
|
+
if (!this._deflate) {
|
|
6872
|
+
return;
|
|
6873
|
+
}
|
|
6874
|
+
let data2 = bufferUtil.concat(this._deflate[kBuffers], this._deflate[kTotalLength]);
|
|
6875
|
+
if (fin) {
|
|
6876
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
6877
|
+
}
|
|
6878
|
+
this._deflate[kCallback] = null;
|
|
6879
|
+
this._deflate[kTotalLength] = 0;
|
|
6880
|
+
this._deflate[kBuffers] = [];
|
|
6881
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
6882
|
+
this._deflate.reset();
|
|
6883
|
+
}
|
|
6884
|
+
callback(null, data2);
|
|
6885
|
+
});
|
|
6886
|
+
}
|
|
6887
|
+
}
|
|
6888
|
+
module.exports = PerMessageDeflate;
|
|
6889
|
+
function deflateOnData(chunk) {
|
|
6890
|
+
this[kBuffers].push(chunk);
|
|
6891
|
+
this[kTotalLength] += chunk.length;
|
|
6892
|
+
}
|
|
6893
|
+
function inflateOnData(chunk) {
|
|
6894
|
+
this[kTotalLength] += chunk.length;
|
|
6895
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
6896
|
+
this[kBuffers].push(chunk);
|
|
6897
|
+
return;
|
|
6898
|
+
}
|
|
6899
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
6900
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
6901
|
+
this[kError][kStatusCode] = 1009;
|
|
6902
|
+
this.removeListener("data", inflateOnData);
|
|
6903
|
+
this.reset();
|
|
6904
|
+
}
|
|
6905
|
+
function inflateOnError(err) {
|
|
6906
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
6907
|
+
if (this[kError]) {
|
|
6908
|
+
this[kCallback](this[kError]);
|
|
6909
|
+
return;
|
|
6910
|
+
}
|
|
6911
|
+
err[kStatusCode] = 1007;
|
|
6912
|
+
this[kCallback](err);
|
|
6913
|
+
}
|
|
6914
|
+
});
|
|
6915
|
+
|
|
6916
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/validation.js
|
|
6917
|
+
var require_validation2 = __commonJS((exports, module) => {
|
|
6918
|
+
var { isUtf8 } = __require("buffer");
|
|
6919
|
+
var { hasBlob } = require_constants();
|
|
6920
|
+
var tokenChars = [
|
|
6921
|
+
0,
|
|
6922
|
+
0,
|
|
6923
|
+
0,
|
|
6924
|
+
0,
|
|
6925
|
+
0,
|
|
6926
|
+
0,
|
|
6927
|
+
0,
|
|
6928
|
+
0,
|
|
6929
|
+
0,
|
|
6930
|
+
0,
|
|
6931
|
+
0,
|
|
6932
|
+
0,
|
|
6933
|
+
0,
|
|
6934
|
+
0,
|
|
6935
|
+
0,
|
|
6936
|
+
0,
|
|
6937
|
+
0,
|
|
6938
|
+
0,
|
|
6939
|
+
0,
|
|
6940
|
+
0,
|
|
6941
|
+
0,
|
|
6942
|
+
0,
|
|
6943
|
+
0,
|
|
6944
|
+
0,
|
|
6945
|
+
0,
|
|
6946
|
+
0,
|
|
6947
|
+
0,
|
|
6948
|
+
0,
|
|
6949
|
+
0,
|
|
6950
|
+
0,
|
|
6951
|
+
0,
|
|
6952
|
+
0,
|
|
6953
|
+
0,
|
|
6954
|
+
1,
|
|
6955
|
+
0,
|
|
6956
|
+
1,
|
|
6957
|
+
1,
|
|
6958
|
+
1,
|
|
6959
|
+
1,
|
|
6960
|
+
1,
|
|
6961
|
+
0,
|
|
6962
|
+
0,
|
|
6963
|
+
1,
|
|
6964
|
+
1,
|
|
6965
|
+
0,
|
|
6966
|
+
1,
|
|
6967
|
+
1,
|
|
6968
|
+
0,
|
|
6969
|
+
1,
|
|
6970
|
+
1,
|
|
6971
|
+
1,
|
|
6972
|
+
1,
|
|
6973
|
+
1,
|
|
6974
|
+
1,
|
|
6975
|
+
1,
|
|
6976
|
+
1,
|
|
6977
|
+
1,
|
|
6978
|
+
1,
|
|
6979
|
+
0,
|
|
6980
|
+
0,
|
|
6981
|
+
0,
|
|
6982
|
+
0,
|
|
6983
|
+
0,
|
|
6984
|
+
0,
|
|
6985
|
+
0,
|
|
6986
|
+
1,
|
|
6987
|
+
1,
|
|
6988
|
+
1,
|
|
6989
|
+
1,
|
|
6990
|
+
1,
|
|
6991
|
+
1,
|
|
6992
|
+
1,
|
|
6993
|
+
1,
|
|
6994
|
+
1,
|
|
6995
|
+
1,
|
|
6996
|
+
1,
|
|
6997
|
+
1,
|
|
6998
|
+
1,
|
|
6999
|
+
1,
|
|
7000
|
+
1,
|
|
7001
|
+
1,
|
|
7002
|
+
1,
|
|
7003
|
+
1,
|
|
7004
|
+
1,
|
|
7005
|
+
1,
|
|
7006
|
+
1,
|
|
7007
|
+
1,
|
|
7008
|
+
1,
|
|
7009
|
+
1,
|
|
7010
|
+
1,
|
|
7011
|
+
1,
|
|
7012
|
+
0,
|
|
7013
|
+
0,
|
|
7014
|
+
0,
|
|
7015
|
+
1,
|
|
7016
|
+
1,
|
|
7017
|
+
1,
|
|
7018
|
+
1,
|
|
7019
|
+
1,
|
|
7020
|
+
1,
|
|
7021
|
+
1,
|
|
7022
|
+
1,
|
|
7023
|
+
1,
|
|
7024
|
+
1,
|
|
7025
|
+
1,
|
|
7026
|
+
1,
|
|
7027
|
+
1,
|
|
7028
|
+
1,
|
|
7029
|
+
1,
|
|
7030
|
+
1,
|
|
7031
|
+
1,
|
|
7032
|
+
1,
|
|
7033
|
+
1,
|
|
7034
|
+
1,
|
|
7035
|
+
1,
|
|
7036
|
+
1,
|
|
7037
|
+
1,
|
|
7038
|
+
1,
|
|
7039
|
+
1,
|
|
7040
|
+
1,
|
|
7041
|
+
1,
|
|
7042
|
+
1,
|
|
7043
|
+
1,
|
|
7044
|
+
0,
|
|
7045
|
+
1,
|
|
7046
|
+
0,
|
|
7047
|
+
1,
|
|
7048
|
+
0
|
|
7049
|
+
];
|
|
7050
|
+
function isValidStatusCode(code) {
|
|
7051
|
+
return code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3000 && code <= 4999;
|
|
7052
|
+
}
|
|
7053
|
+
function _isValidUTF8(buf) {
|
|
7054
|
+
const len = buf.length;
|
|
7055
|
+
let i = 0;
|
|
7056
|
+
while (i < len) {
|
|
7057
|
+
if ((buf[i] & 128) === 0) {
|
|
7058
|
+
i++;
|
|
7059
|
+
} else if ((buf[i] & 224) === 192) {
|
|
7060
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) {
|
|
7061
|
+
return false;
|
|
7062
|
+
}
|
|
7063
|
+
i += 2;
|
|
7064
|
+
} else if ((buf[i] & 240) === 224) {
|
|
7065
|
+
if (i + 2 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || buf[i] === 224 && (buf[i + 1] & 224) === 128 || buf[i] === 237 && (buf[i + 1] & 224) === 160) {
|
|
7066
|
+
return false;
|
|
7067
|
+
}
|
|
7068
|
+
i += 3;
|
|
7069
|
+
} else if ((buf[i] & 248) === 240) {
|
|
7070
|
+
if (i + 3 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || (buf[i + 3] & 192) !== 128 || buf[i] === 240 && (buf[i + 1] & 240) === 128 || buf[i] === 244 && buf[i + 1] > 143 || buf[i] > 244) {
|
|
7071
|
+
return false;
|
|
7072
|
+
}
|
|
7073
|
+
i += 4;
|
|
7074
|
+
} else {
|
|
7075
|
+
return false;
|
|
7076
|
+
}
|
|
7077
|
+
}
|
|
7078
|
+
return true;
|
|
7079
|
+
}
|
|
7080
|
+
function isBlob(value) {
|
|
7081
|
+
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");
|
|
7082
|
+
}
|
|
7083
|
+
module.exports = {
|
|
7084
|
+
isBlob,
|
|
7085
|
+
isValidStatusCode,
|
|
7086
|
+
isValidUTF8: _isValidUTF8,
|
|
7087
|
+
tokenChars
|
|
7088
|
+
};
|
|
7089
|
+
if (isUtf8) {
|
|
7090
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
7091
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
7092
|
+
};
|
|
7093
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
7094
|
+
try {
|
|
7095
|
+
const isValidUTF8 = (()=>{throw new Error("Cannot require module "+"utf-8-validate");})();
|
|
7096
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
7097
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
7098
|
+
};
|
|
7099
|
+
} catch (e) {}
|
|
7100
|
+
}
|
|
7101
|
+
});
|
|
7102
|
+
|
|
7103
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/receiver.js
|
|
7104
|
+
var require_receiver = __commonJS((exports, module) => {
|
|
7105
|
+
var { Writable } = __require("stream");
|
|
7106
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
7107
|
+
var {
|
|
7108
|
+
BINARY_TYPES,
|
|
7109
|
+
EMPTY_BUFFER,
|
|
7110
|
+
kStatusCode,
|
|
7111
|
+
kWebSocket
|
|
7112
|
+
} = require_constants();
|
|
7113
|
+
var { concat, toArrayBuffer, unmask } = require_buffer_util();
|
|
7114
|
+
var { isValidStatusCode, isValidUTF8 } = require_validation2();
|
|
7115
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
7116
|
+
var GET_INFO = 0;
|
|
7117
|
+
var GET_PAYLOAD_LENGTH_16 = 1;
|
|
7118
|
+
var GET_PAYLOAD_LENGTH_64 = 2;
|
|
7119
|
+
var GET_MASK = 3;
|
|
7120
|
+
var GET_DATA = 4;
|
|
7121
|
+
var INFLATING = 5;
|
|
7122
|
+
var DEFER_EVENT = 6;
|
|
7123
|
+
|
|
7124
|
+
class Receiver extends Writable {
|
|
7125
|
+
constructor(options = {}) {
|
|
7126
|
+
super();
|
|
7127
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== undefined ? options.allowSynchronousEvents : true;
|
|
7128
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
7129
|
+
this._extensions = options.extensions || {};
|
|
7130
|
+
this._isServer = !!options.isServer;
|
|
7131
|
+
this._maxPayload = options.maxPayload | 0;
|
|
7132
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
7133
|
+
this[kWebSocket] = undefined;
|
|
7134
|
+
this._bufferedBytes = 0;
|
|
7135
|
+
this._buffers = [];
|
|
7136
|
+
this._compressed = false;
|
|
7137
|
+
this._payloadLength = 0;
|
|
7138
|
+
this._mask = undefined;
|
|
7139
|
+
this._fragmented = 0;
|
|
7140
|
+
this._masked = false;
|
|
7141
|
+
this._fin = false;
|
|
7142
|
+
this._opcode = 0;
|
|
7143
|
+
this._totalPayloadLength = 0;
|
|
7144
|
+
this._messageLength = 0;
|
|
7145
|
+
this._fragments = [];
|
|
7146
|
+
this._errored = false;
|
|
7147
|
+
this._loop = false;
|
|
7148
|
+
this._state = GET_INFO;
|
|
7149
|
+
}
|
|
7150
|
+
_write(chunk, encoding, cb) {
|
|
7151
|
+
if (this._opcode === 8 && this._state == GET_INFO)
|
|
7152
|
+
return cb();
|
|
7153
|
+
this._bufferedBytes += chunk.length;
|
|
7154
|
+
this._buffers.push(chunk);
|
|
7155
|
+
this.startLoop(cb);
|
|
7156
|
+
}
|
|
7157
|
+
consume(n) {
|
|
7158
|
+
this._bufferedBytes -= n;
|
|
7159
|
+
if (n === this._buffers[0].length)
|
|
7160
|
+
return this._buffers.shift();
|
|
7161
|
+
if (n < this._buffers[0].length) {
|
|
7162
|
+
const buf = this._buffers[0];
|
|
7163
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
7164
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
7165
|
+
}
|
|
7166
|
+
const dst = Buffer.allocUnsafe(n);
|
|
7167
|
+
do {
|
|
7168
|
+
const buf = this._buffers[0];
|
|
7169
|
+
const offset = dst.length - n;
|
|
7170
|
+
if (n >= buf.length) {
|
|
7171
|
+
dst.set(this._buffers.shift(), offset);
|
|
7172
|
+
} else {
|
|
7173
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
7174
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
7175
|
+
}
|
|
7176
|
+
n -= buf.length;
|
|
7177
|
+
} while (n > 0);
|
|
7178
|
+
return dst;
|
|
7179
|
+
}
|
|
7180
|
+
startLoop(cb) {
|
|
7181
|
+
this._loop = true;
|
|
7182
|
+
do {
|
|
7183
|
+
switch (this._state) {
|
|
7184
|
+
case GET_INFO:
|
|
7185
|
+
this.getInfo(cb);
|
|
7186
|
+
break;
|
|
7187
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
7188
|
+
this.getPayloadLength16(cb);
|
|
7189
|
+
break;
|
|
7190
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
7191
|
+
this.getPayloadLength64(cb);
|
|
7192
|
+
break;
|
|
7193
|
+
case GET_MASK:
|
|
7194
|
+
this.getMask();
|
|
7195
|
+
break;
|
|
7196
|
+
case GET_DATA:
|
|
7197
|
+
this.getData(cb);
|
|
7198
|
+
break;
|
|
7199
|
+
case INFLATING:
|
|
7200
|
+
case DEFER_EVENT:
|
|
7201
|
+
this._loop = false;
|
|
7202
|
+
return;
|
|
7203
|
+
}
|
|
7204
|
+
} while (this._loop);
|
|
7205
|
+
if (!this._errored)
|
|
7206
|
+
cb();
|
|
7207
|
+
}
|
|
7208
|
+
getInfo(cb) {
|
|
7209
|
+
if (this._bufferedBytes < 2) {
|
|
7210
|
+
this._loop = false;
|
|
7211
|
+
return;
|
|
7212
|
+
}
|
|
7213
|
+
const buf = this.consume(2);
|
|
7214
|
+
if ((buf[0] & 48) !== 0) {
|
|
7215
|
+
const error48 = this.createError(RangeError, "RSV2 and RSV3 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_2_3");
|
|
7216
|
+
cb(error48);
|
|
7217
|
+
return;
|
|
7218
|
+
}
|
|
7219
|
+
const compressed = (buf[0] & 64) === 64;
|
|
7220
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
7221
|
+
const error48 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
7222
|
+
cb(error48);
|
|
7223
|
+
return;
|
|
7224
|
+
}
|
|
7225
|
+
this._fin = (buf[0] & 128) === 128;
|
|
7226
|
+
this._opcode = buf[0] & 15;
|
|
7227
|
+
this._payloadLength = buf[1] & 127;
|
|
7228
|
+
if (this._opcode === 0) {
|
|
7229
|
+
if (compressed) {
|
|
7230
|
+
const error48 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
7231
|
+
cb(error48);
|
|
7232
|
+
return;
|
|
7233
|
+
}
|
|
7234
|
+
if (!this._fragmented) {
|
|
7235
|
+
const error48 = this.createError(RangeError, "invalid opcode 0", true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
7236
|
+
cb(error48);
|
|
7237
|
+
return;
|
|
7238
|
+
}
|
|
7239
|
+
this._opcode = this._fragmented;
|
|
7240
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
7241
|
+
if (this._fragmented) {
|
|
7242
|
+
const error48 = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
7243
|
+
cb(error48);
|
|
7244
|
+
return;
|
|
7245
|
+
}
|
|
7246
|
+
this._compressed = compressed;
|
|
7247
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
7248
|
+
if (!this._fin) {
|
|
7249
|
+
const error48 = this.createError(RangeError, "FIN must be set", true, 1002, "WS_ERR_EXPECTED_FIN");
|
|
7250
|
+
cb(error48);
|
|
7251
|
+
return;
|
|
7252
|
+
}
|
|
7253
|
+
if (compressed) {
|
|
7254
|
+
const error48 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
7255
|
+
cb(error48);
|
|
7256
|
+
return;
|
|
7257
|
+
}
|
|
7258
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
7259
|
+
const error48 = this.createError(RangeError, `invalid payload length ${this._payloadLength}`, true, 1002, "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");
|
|
7260
|
+
cb(error48);
|
|
7261
|
+
return;
|
|
7262
|
+
}
|
|
7263
|
+
} else {
|
|
7264
|
+
const error48 = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
7265
|
+
cb(error48);
|
|
7266
|
+
return;
|
|
7267
|
+
}
|
|
7268
|
+
if (!this._fin && !this._fragmented)
|
|
7269
|
+
this._fragmented = this._opcode;
|
|
7270
|
+
this._masked = (buf[1] & 128) === 128;
|
|
7271
|
+
if (this._isServer) {
|
|
7272
|
+
if (!this._masked) {
|
|
7273
|
+
const error48 = this.createError(RangeError, "MASK must be set", true, 1002, "WS_ERR_EXPECTED_MASK");
|
|
7274
|
+
cb(error48);
|
|
7275
|
+
return;
|
|
7276
|
+
}
|
|
7277
|
+
} else if (this._masked) {
|
|
7278
|
+
const error48 = this.createError(RangeError, "MASK must be clear", true, 1002, "WS_ERR_UNEXPECTED_MASK");
|
|
7279
|
+
cb(error48);
|
|
7280
|
+
return;
|
|
7281
|
+
}
|
|
7282
|
+
if (this._payloadLength === 126)
|
|
7283
|
+
this._state = GET_PAYLOAD_LENGTH_16;
|
|
7284
|
+
else if (this._payloadLength === 127)
|
|
7285
|
+
this._state = GET_PAYLOAD_LENGTH_64;
|
|
7286
|
+
else
|
|
7287
|
+
this.haveLength(cb);
|
|
7288
|
+
}
|
|
7289
|
+
getPayloadLength16(cb) {
|
|
7290
|
+
if (this._bufferedBytes < 2) {
|
|
7291
|
+
this._loop = false;
|
|
7292
|
+
return;
|
|
7293
|
+
}
|
|
7294
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
7295
|
+
this.haveLength(cb);
|
|
7296
|
+
}
|
|
7297
|
+
getPayloadLength64(cb) {
|
|
7298
|
+
if (this._bufferedBytes < 8) {
|
|
7299
|
+
this._loop = false;
|
|
7300
|
+
return;
|
|
7301
|
+
}
|
|
7302
|
+
const buf = this.consume(8);
|
|
7303
|
+
const num = buf.readUInt32BE(0);
|
|
7304
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
7305
|
+
const error48 = this.createError(RangeError, "Unsupported WebSocket frame: payload length > 2^53 - 1", false, 1009, "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");
|
|
7306
|
+
cb(error48);
|
|
7307
|
+
return;
|
|
7308
|
+
}
|
|
7309
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
7310
|
+
this.haveLength(cb);
|
|
7311
|
+
}
|
|
7312
|
+
haveLength(cb) {
|
|
7313
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
7314
|
+
this._totalPayloadLength += this._payloadLength;
|
|
7315
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
7316
|
+
const error48 = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
7317
|
+
cb(error48);
|
|
7318
|
+
return;
|
|
7319
|
+
}
|
|
7320
|
+
}
|
|
7321
|
+
if (this._masked)
|
|
7322
|
+
this._state = GET_MASK;
|
|
7323
|
+
else
|
|
7324
|
+
this._state = GET_DATA;
|
|
7325
|
+
}
|
|
7326
|
+
getMask() {
|
|
7327
|
+
if (this._bufferedBytes < 4) {
|
|
7328
|
+
this._loop = false;
|
|
7329
|
+
return;
|
|
7330
|
+
}
|
|
7331
|
+
this._mask = this.consume(4);
|
|
7332
|
+
this._state = GET_DATA;
|
|
7333
|
+
}
|
|
7334
|
+
getData(cb) {
|
|
7335
|
+
let data = EMPTY_BUFFER;
|
|
7336
|
+
if (this._payloadLength) {
|
|
7337
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
7338
|
+
this._loop = false;
|
|
7339
|
+
return;
|
|
7340
|
+
}
|
|
7341
|
+
data = this.consume(this._payloadLength);
|
|
7342
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
7343
|
+
unmask(data, this._mask);
|
|
7344
|
+
}
|
|
7345
|
+
}
|
|
7346
|
+
if (this._opcode > 7) {
|
|
7347
|
+
this.controlMessage(data, cb);
|
|
7348
|
+
return;
|
|
7349
|
+
}
|
|
7350
|
+
if (this._compressed) {
|
|
7351
|
+
this._state = INFLATING;
|
|
7352
|
+
this.decompress(data, cb);
|
|
7353
|
+
return;
|
|
7354
|
+
}
|
|
7355
|
+
if (data.length) {
|
|
7356
|
+
this._messageLength = this._totalPayloadLength;
|
|
7357
|
+
this._fragments.push(data);
|
|
7358
|
+
}
|
|
7359
|
+
this.dataMessage(cb);
|
|
7360
|
+
}
|
|
7361
|
+
decompress(data, cb) {
|
|
7362
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
7363
|
+
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
|
7364
|
+
if (err)
|
|
7365
|
+
return cb(err);
|
|
7366
|
+
if (buf.length) {
|
|
7367
|
+
this._messageLength += buf.length;
|
|
7368
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
7369
|
+
const error48 = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
7370
|
+
cb(error48);
|
|
7371
|
+
return;
|
|
7372
|
+
}
|
|
7373
|
+
this._fragments.push(buf);
|
|
7374
|
+
}
|
|
7375
|
+
this.dataMessage(cb);
|
|
7376
|
+
if (this._state === GET_INFO)
|
|
7377
|
+
this.startLoop(cb);
|
|
7378
|
+
});
|
|
7379
|
+
}
|
|
7380
|
+
dataMessage(cb) {
|
|
7381
|
+
if (!this._fin) {
|
|
7382
|
+
this._state = GET_INFO;
|
|
7383
|
+
return;
|
|
7384
|
+
}
|
|
7385
|
+
const messageLength = this._messageLength;
|
|
7386
|
+
const fragments = this._fragments;
|
|
7387
|
+
this._totalPayloadLength = 0;
|
|
7388
|
+
this._messageLength = 0;
|
|
7389
|
+
this._fragmented = 0;
|
|
7390
|
+
this._fragments = [];
|
|
7391
|
+
if (this._opcode === 2) {
|
|
7392
|
+
let data;
|
|
7393
|
+
if (this._binaryType === "nodebuffer") {
|
|
7394
|
+
data = concat(fragments, messageLength);
|
|
7395
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
7396
|
+
data = toArrayBuffer(concat(fragments, messageLength));
|
|
7397
|
+
} else if (this._binaryType === "blob") {
|
|
7398
|
+
data = new Blob(fragments);
|
|
7399
|
+
} else {
|
|
7400
|
+
data = fragments;
|
|
7401
|
+
}
|
|
7402
|
+
if (this._allowSynchronousEvents) {
|
|
7403
|
+
this.emit("message", data, true);
|
|
7404
|
+
this._state = GET_INFO;
|
|
7405
|
+
} else {
|
|
7406
|
+
this._state = DEFER_EVENT;
|
|
7407
|
+
setImmediate(() => {
|
|
7408
|
+
this.emit("message", data, true);
|
|
7409
|
+
this._state = GET_INFO;
|
|
7410
|
+
this.startLoop(cb);
|
|
7411
|
+
});
|
|
7412
|
+
}
|
|
7413
|
+
} else {
|
|
7414
|
+
const buf = concat(fragments, messageLength);
|
|
7415
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
7416
|
+
const error48 = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
7417
|
+
cb(error48);
|
|
7418
|
+
return;
|
|
7419
|
+
}
|
|
7420
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
7421
|
+
this.emit("message", buf, false);
|
|
7422
|
+
this._state = GET_INFO;
|
|
7423
|
+
} else {
|
|
7424
|
+
this._state = DEFER_EVENT;
|
|
7425
|
+
setImmediate(() => {
|
|
7426
|
+
this.emit("message", buf, false);
|
|
7427
|
+
this._state = GET_INFO;
|
|
7428
|
+
this.startLoop(cb);
|
|
7429
|
+
});
|
|
7430
|
+
}
|
|
7431
|
+
}
|
|
7432
|
+
}
|
|
7433
|
+
controlMessage(data, cb) {
|
|
7434
|
+
if (this._opcode === 8) {
|
|
7435
|
+
if (data.length === 0) {
|
|
7436
|
+
this._loop = false;
|
|
7437
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
7438
|
+
this.end();
|
|
7439
|
+
} else {
|
|
7440
|
+
const code = data.readUInt16BE(0);
|
|
7441
|
+
if (!isValidStatusCode(code)) {
|
|
7442
|
+
const error48 = this.createError(RangeError, `invalid status code ${code}`, true, 1002, "WS_ERR_INVALID_CLOSE_CODE");
|
|
7443
|
+
cb(error48);
|
|
7444
|
+
return;
|
|
7445
|
+
}
|
|
7446
|
+
const buf = new FastBuffer(data.buffer, data.byteOffset + 2, data.length - 2);
|
|
7447
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
7448
|
+
const error48 = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
7449
|
+
cb(error48);
|
|
7450
|
+
return;
|
|
7451
|
+
}
|
|
7452
|
+
this._loop = false;
|
|
7453
|
+
this.emit("conclude", code, buf);
|
|
7454
|
+
this.end();
|
|
7455
|
+
}
|
|
7456
|
+
this._state = GET_INFO;
|
|
7457
|
+
return;
|
|
7458
|
+
}
|
|
7459
|
+
if (this._allowSynchronousEvents) {
|
|
7460
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
7461
|
+
this._state = GET_INFO;
|
|
7462
|
+
} else {
|
|
7463
|
+
this._state = DEFER_EVENT;
|
|
7464
|
+
setImmediate(() => {
|
|
7465
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
7466
|
+
this._state = GET_INFO;
|
|
7467
|
+
this.startLoop(cb);
|
|
7468
|
+
});
|
|
7469
|
+
}
|
|
7470
|
+
}
|
|
7471
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
7472
|
+
this._loop = false;
|
|
7473
|
+
this._errored = true;
|
|
7474
|
+
const err = new ErrorCtor(prefix ? `Invalid WebSocket frame: ${message}` : message);
|
|
7475
|
+
Error.captureStackTrace(err, this.createError);
|
|
7476
|
+
err.code = errorCode;
|
|
7477
|
+
err[kStatusCode] = statusCode;
|
|
7478
|
+
return err;
|
|
7479
|
+
}
|
|
7480
|
+
}
|
|
7481
|
+
module.exports = Receiver;
|
|
7482
|
+
});
|
|
7483
|
+
|
|
7484
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/sender.js
|
|
7485
|
+
var require_sender = __commonJS((exports, module) => {
|
|
7486
|
+
var { Duplex } = __require("stream");
|
|
7487
|
+
var { randomFillSync } = __require("crypto");
|
|
7488
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
7489
|
+
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
|
|
7490
|
+
var { isBlob, isValidStatusCode } = require_validation2();
|
|
7491
|
+
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
7492
|
+
var kByteLength = Symbol("kByteLength");
|
|
7493
|
+
var maskBuffer = Buffer.alloc(4);
|
|
7494
|
+
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
7495
|
+
var randomPool;
|
|
7496
|
+
var randomPoolPointer = RANDOM_POOL_SIZE;
|
|
7497
|
+
var DEFAULT = 0;
|
|
7498
|
+
var DEFLATING = 1;
|
|
7499
|
+
var GET_BLOB_DATA = 2;
|
|
7500
|
+
|
|
7501
|
+
class Sender {
|
|
7502
|
+
constructor(socket, extensions, generateMask) {
|
|
7503
|
+
this._extensions = extensions || {};
|
|
7504
|
+
if (generateMask) {
|
|
7505
|
+
this._generateMask = generateMask;
|
|
7506
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
7507
|
+
}
|
|
7508
|
+
this._socket = socket;
|
|
7509
|
+
this._firstFragment = true;
|
|
7510
|
+
this._compress = false;
|
|
7511
|
+
this._bufferedBytes = 0;
|
|
7512
|
+
this._queue = [];
|
|
7513
|
+
this._state = DEFAULT;
|
|
7514
|
+
this.onerror = NOOP;
|
|
7515
|
+
this[kWebSocket] = undefined;
|
|
7516
|
+
}
|
|
7517
|
+
static frame(data, options) {
|
|
7518
|
+
let mask;
|
|
7519
|
+
let merge2 = false;
|
|
7520
|
+
let offset = 2;
|
|
7521
|
+
let skipMasking = false;
|
|
7522
|
+
if (options.mask) {
|
|
7523
|
+
mask = options.maskBuffer || maskBuffer;
|
|
7524
|
+
if (options.generateMask) {
|
|
7525
|
+
options.generateMask(mask);
|
|
7526
|
+
} else {
|
|
7527
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
7528
|
+
if (randomPool === undefined) {
|
|
7529
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
7530
|
+
}
|
|
7531
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
7532
|
+
randomPoolPointer = 0;
|
|
7533
|
+
}
|
|
7534
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
7535
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
7536
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
7537
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
7538
|
+
}
|
|
7539
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
7540
|
+
offset = 6;
|
|
7541
|
+
}
|
|
7542
|
+
let dataLength;
|
|
7543
|
+
if (typeof data === "string") {
|
|
7544
|
+
if ((!options.mask || skipMasking) && options[kByteLength] !== undefined) {
|
|
7545
|
+
dataLength = options[kByteLength];
|
|
7546
|
+
} else {
|
|
7547
|
+
data = Buffer.from(data);
|
|
7548
|
+
dataLength = data.length;
|
|
7549
|
+
}
|
|
7550
|
+
} else {
|
|
7551
|
+
dataLength = data.length;
|
|
7552
|
+
merge2 = options.mask && options.readOnly && !skipMasking;
|
|
7553
|
+
}
|
|
7554
|
+
let payloadLength = dataLength;
|
|
7555
|
+
if (dataLength >= 65536) {
|
|
7556
|
+
offset += 8;
|
|
7557
|
+
payloadLength = 127;
|
|
7558
|
+
} else if (dataLength > 125) {
|
|
7559
|
+
offset += 2;
|
|
7560
|
+
payloadLength = 126;
|
|
7561
|
+
}
|
|
7562
|
+
const target = Buffer.allocUnsafe(merge2 ? dataLength + offset : offset);
|
|
7563
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
7564
|
+
if (options.rsv1)
|
|
7565
|
+
target[0] |= 64;
|
|
7566
|
+
target[1] = payloadLength;
|
|
7567
|
+
if (payloadLength === 126) {
|
|
7568
|
+
target.writeUInt16BE(dataLength, 2);
|
|
7569
|
+
} else if (payloadLength === 127) {
|
|
7570
|
+
target[2] = target[3] = 0;
|
|
7571
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
7572
|
+
}
|
|
7573
|
+
if (!options.mask)
|
|
7574
|
+
return [target, data];
|
|
7575
|
+
target[1] |= 128;
|
|
7576
|
+
target[offset - 4] = mask[0];
|
|
7577
|
+
target[offset - 3] = mask[1];
|
|
7578
|
+
target[offset - 2] = mask[2];
|
|
7579
|
+
target[offset - 1] = mask[3];
|
|
7580
|
+
if (skipMasking)
|
|
7581
|
+
return [target, data];
|
|
7582
|
+
if (merge2) {
|
|
7583
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
7584
|
+
return [target];
|
|
7585
|
+
}
|
|
7586
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
7587
|
+
return [target, data];
|
|
7588
|
+
}
|
|
7589
|
+
close(code, data, mask, cb) {
|
|
7590
|
+
let buf;
|
|
7591
|
+
if (code === undefined) {
|
|
7592
|
+
buf = EMPTY_BUFFER;
|
|
7593
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
7594
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
7595
|
+
} else if (data === undefined || !data.length) {
|
|
7596
|
+
buf = Buffer.allocUnsafe(2);
|
|
7597
|
+
buf.writeUInt16BE(code, 0);
|
|
7598
|
+
} else {
|
|
7599
|
+
const length = Buffer.byteLength(data);
|
|
7600
|
+
if (length > 123) {
|
|
7601
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
7602
|
+
}
|
|
7603
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
7604
|
+
buf.writeUInt16BE(code, 0);
|
|
7605
|
+
if (typeof data === "string") {
|
|
7606
|
+
buf.write(data, 2);
|
|
7607
|
+
} else {
|
|
7608
|
+
buf.set(data, 2);
|
|
7609
|
+
}
|
|
7610
|
+
}
|
|
7611
|
+
const options = {
|
|
7612
|
+
[kByteLength]: buf.length,
|
|
7613
|
+
fin: true,
|
|
7614
|
+
generateMask: this._generateMask,
|
|
7615
|
+
mask,
|
|
7616
|
+
maskBuffer: this._maskBuffer,
|
|
7617
|
+
opcode: 8,
|
|
7618
|
+
readOnly: false,
|
|
7619
|
+
rsv1: false
|
|
7620
|
+
};
|
|
7621
|
+
if (this._state !== DEFAULT) {
|
|
7622
|
+
this.enqueue([this.dispatch, buf, false, options, cb]);
|
|
7623
|
+
} else {
|
|
7624
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
7625
|
+
}
|
|
7626
|
+
}
|
|
7627
|
+
ping(data, mask, cb) {
|
|
7628
|
+
let byteLength;
|
|
7629
|
+
let readOnly;
|
|
7630
|
+
if (typeof data === "string") {
|
|
7631
|
+
byteLength = Buffer.byteLength(data);
|
|
7632
|
+
readOnly = false;
|
|
7633
|
+
} else if (isBlob(data)) {
|
|
7634
|
+
byteLength = data.size;
|
|
7635
|
+
readOnly = false;
|
|
7636
|
+
} else {
|
|
7637
|
+
data = toBuffer(data);
|
|
7638
|
+
byteLength = data.length;
|
|
7639
|
+
readOnly = toBuffer.readOnly;
|
|
7640
|
+
}
|
|
7641
|
+
if (byteLength > 125) {
|
|
7642
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
7643
|
+
}
|
|
7644
|
+
const options = {
|
|
7645
|
+
[kByteLength]: byteLength,
|
|
7646
|
+
fin: true,
|
|
7647
|
+
generateMask: this._generateMask,
|
|
7648
|
+
mask,
|
|
7649
|
+
maskBuffer: this._maskBuffer,
|
|
7650
|
+
opcode: 9,
|
|
7651
|
+
readOnly,
|
|
7652
|
+
rsv1: false
|
|
7653
|
+
};
|
|
7654
|
+
if (isBlob(data)) {
|
|
7655
|
+
if (this._state !== DEFAULT) {
|
|
7656
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
7657
|
+
} else {
|
|
7658
|
+
this.getBlobData(data, false, options, cb);
|
|
7659
|
+
}
|
|
7660
|
+
} else if (this._state !== DEFAULT) {
|
|
7661
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
7662
|
+
} else {
|
|
7663
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
7664
|
+
}
|
|
7665
|
+
}
|
|
7666
|
+
pong(data, mask, cb) {
|
|
7667
|
+
let byteLength;
|
|
7668
|
+
let readOnly;
|
|
7669
|
+
if (typeof data === "string") {
|
|
7670
|
+
byteLength = Buffer.byteLength(data);
|
|
7671
|
+
readOnly = false;
|
|
7672
|
+
} else if (isBlob(data)) {
|
|
7673
|
+
byteLength = data.size;
|
|
7674
|
+
readOnly = false;
|
|
7675
|
+
} else {
|
|
7676
|
+
data = toBuffer(data);
|
|
7677
|
+
byteLength = data.length;
|
|
7678
|
+
readOnly = toBuffer.readOnly;
|
|
7679
|
+
}
|
|
7680
|
+
if (byteLength > 125) {
|
|
7681
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
7682
|
+
}
|
|
7683
|
+
const options = {
|
|
7684
|
+
[kByteLength]: byteLength,
|
|
7685
|
+
fin: true,
|
|
7686
|
+
generateMask: this._generateMask,
|
|
7687
|
+
mask,
|
|
7688
|
+
maskBuffer: this._maskBuffer,
|
|
7689
|
+
opcode: 10,
|
|
7690
|
+
readOnly,
|
|
7691
|
+
rsv1: false
|
|
7692
|
+
};
|
|
7693
|
+
if (isBlob(data)) {
|
|
7694
|
+
if (this._state !== DEFAULT) {
|
|
7695
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
7696
|
+
} else {
|
|
7697
|
+
this.getBlobData(data, false, options, cb);
|
|
7698
|
+
}
|
|
7699
|
+
} else if (this._state !== DEFAULT) {
|
|
7700
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
7701
|
+
} else {
|
|
7702
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
7703
|
+
}
|
|
7704
|
+
}
|
|
7705
|
+
send(data, options, cb) {
|
|
7706
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
7707
|
+
let opcode = options.binary ? 2 : 1;
|
|
7708
|
+
let rsv1 = options.compress;
|
|
7709
|
+
let byteLength;
|
|
7710
|
+
let readOnly;
|
|
7711
|
+
if (typeof data === "string") {
|
|
7712
|
+
byteLength = Buffer.byteLength(data);
|
|
7713
|
+
readOnly = false;
|
|
7714
|
+
} else if (isBlob(data)) {
|
|
7715
|
+
byteLength = data.size;
|
|
7716
|
+
readOnly = false;
|
|
7717
|
+
} else {
|
|
7718
|
+
data = toBuffer(data);
|
|
7719
|
+
byteLength = data.length;
|
|
7720
|
+
readOnly = toBuffer.readOnly;
|
|
7721
|
+
}
|
|
7722
|
+
if (this._firstFragment) {
|
|
7723
|
+
this._firstFragment = false;
|
|
7724
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
7725
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
7726
|
+
}
|
|
7727
|
+
this._compress = rsv1;
|
|
7728
|
+
} else {
|
|
7729
|
+
rsv1 = false;
|
|
7730
|
+
opcode = 0;
|
|
7731
|
+
}
|
|
7732
|
+
if (options.fin)
|
|
7733
|
+
this._firstFragment = true;
|
|
7734
|
+
const opts = {
|
|
7735
|
+
[kByteLength]: byteLength,
|
|
7736
|
+
fin: options.fin,
|
|
7737
|
+
generateMask: this._generateMask,
|
|
7738
|
+
mask: options.mask,
|
|
7739
|
+
maskBuffer: this._maskBuffer,
|
|
7740
|
+
opcode,
|
|
7741
|
+
readOnly,
|
|
7742
|
+
rsv1
|
|
7743
|
+
};
|
|
7744
|
+
if (isBlob(data)) {
|
|
7745
|
+
if (this._state !== DEFAULT) {
|
|
7746
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
7747
|
+
} else {
|
|
7748
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
7749
|
+
}
|
|
7750
|
+
} else if (this._state !== DEFAULT) {
|
|
7751
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
7752
|
+
} else {
|
|
7753
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
7754
|
+
}
|
|
7755
|
+
}
|
|
7756
|
+
getBlobData(blob, compress, options, cb) {
|
|
7757
|
+
this._bufferedBytes += options[kByteLength];
|
|
7758
|
+
this._state = GET_BLOB_DATA;
|
|
7759
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
7760
|
+
if (this._socket.destroyed) {
|
|
7761
|
+
const err = new Error("The socket was closed while the blob was being read");
|
|
7762
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
7763
|
+
return;
|
|
7764
|
+
}
|
|
7765
|
+
this._bufferedBytes -= options[kByteLength];
|
|
7766
|
+
const data = toBuffer(arrayBuffer);
|
|
7767
|
+
if (!compress) {
|
|
7768
|
+
this._state = DEFAULT;
|
|
7769
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
7770
|
+
this.dequeue();
|
|
7771
|
+
} else {
|
|
7772
|
+
this.dispatch(data, compress, options, cb);
|
|
7773
|
+
}
|
|
7774
|
+
}).catch((err) => {
|
|
7775
|
+
process.nextTick(onError, this, err, cb);
|
|
7776
|
+
});
|
|
7777
|
+
}
|
|
7778
|
+
dispatch(data, compress, options, cb) {
|
|
7779
|
+
if (!compress) {
|
|
7780
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
7781
|
+
return;
|
|
7782
|
+
}
|
|
7783
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
7784
|
+
this._bufferedBytes += options[kByteLength];
|
|
7785
|
+
this._state = DEFLATING;
|
|
7786
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
|
7787
|
+
if (this._socket.destroyed) {
|
|
7788
|
+
const err = new Error("The socket was closed while data was being compressed");
|
|
7789
|
+
callCallbacks(this, err, cb);
|
|
7790
|
+
return;
|
|
7791
|
+
}
|
|
7792
|
+
this._bufferedBytes -= options[kByteLength];
|
|
7793
|
+
this._state = DEFAULT;
|
|
7794
|
+
options.readOnly = false;
|
|
7795
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
7796
|
+
this.dequeue();
|
|
7797
|
+
});
|
|
7798
|
+
}
|
|
7799
|
+
dequeue() {
|
|
7800
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
7801
|
+
const params = this._queue.shift();
|
|
7802
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
7803
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
7804
|
+
}
|
|
7805
|
+
}
|
|
7806
|
+
enqueue(params) {
|
|
7807
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
7808
|
+
this._queue.push(params);
|
|
7809
|
+
}
|
|
7810
|
+
sendFrame(list, cb) {
|
|
7811
|
+
if (list.length === 2) {
|
|
7812
|
+
this._socket.cork();
|
|
7813
|
+
this._socket.write(list[0]);
|
|
7814
|
+
this._socket.write(list[1], cb);
|
|
7815
|
+
this._socket.uncork();
|
|
7816
|
+
} else {
|
|
7817
|
+
this._socket.write(list[0], cb);
|
|
7818
|
+
}
|
|
7819
|
+
}
|
|
7820
|
+
}
|
|
7821
|
+
module.exports = Sender;
|
|
7822
|
+
function callCallbacks(sender, err, cb) {
|
|
7823
|
+
if (typeof cb === "function")
|
|
7824
|
+
cb(err);
|
|
7825
|
+
for (let i = 0;i < sender._queue.length; i++) {
|
|
7826
|
+
const params = sender._queue[i];
|
|
7827
|
+
const callback = params[params.length - 1];
|
|
7828
|
+
if (typeof callback === "function")
|
|
7829
|
+
callback(err);
|
|
7830
|
+
}
|
|
7831
|
+
}
|
|
7832
|
+
function onError(sender, err, cb) {
|
|
7833
|
+
callCallbacks(sender, err, cb);
|
|
7834
|
+
sender.onerror(err);
|
|
7835
|
+
}
|
|
7836
|
+
});
|
|
7837
|
+
|
|
7838
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/event-target.js
|
|
7839
|
+
var require_event_target = __commonJS((exports, module) => {
|
|
7840
|
+
var { kForOnEventAttribute, kListener } = require_constants();
|
|
7841
|
+
var kCode = Symbol("kCode");
|
|
7842
|
+
var kData = Symbol("kData");
|
|
7843
|
+
var kError = Symbol("kError");
|
|
7844
|
+
var kMessage = Symbol("kMessage");
|
|
7845
|
+
var kReason = Symbol("kReason");
|
|
7846
|
+
var kTarget = Symbol("kTarget");
|
|
7847
|
+
var kType = Symbol("kType");
|
|
7848
|
+
var kWasClean = Symbol("kWasClean");
|
|
7849
|
+
|
|
7850
|
+
class Event {
|
|
7851
|
+
constructor(type) {
|
|
7852
|
+
this[kTarget] = null;
|
|
7853
|
+
this[kType] = type;
|
|
7854
|
+
}
|
|
7855
|
+
get target() {
|
|
7856
|
+
return this[kTarget];
|
|
7857
|
+
}
|
|
7858
|
+
get type() {
|
|
7859
|
+
return this[kType];
|
|
7860
|
+
}
|
|
7861
|
+
}
|
|
7862
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
7863
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
7864
|
+
|
|
7865
|
+
class CloseEvent extends Event {
|
|
7866
|
+
constructor(type, options = {}) {
|
|
7867
|
+
super(type);
|
|
7868
|
+
this[kCode] = options.code === undefined ? 0 : options.code;
|
|
7869
|
+
this[kReason] = options.reason === undefined ? "" : options.reason;
|
|
7870
|
+
this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
|
|
7871
|
+
}
|
|
7872
|
+
get code() {
|
|
7873
|
+
return this[kCode];
|
|
7874
|
+
}
|
|
7875
|
+
get reason() {
|
|
7876
|
+
return this[kReason];
|
|
7877
|
+
}
|
|
7878
|
+
get wasClean() {
|
|
7879
|
+
return this[kWasClean];
|
|
7880
|
+
}
|
|
7881
|
+
}
|
|
7882
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
7883
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
7884
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
7885
|
+
|
|
7886
|
+
class ErrorEvent extends Event {
|
|
7887
|
+
constructor(type, options = {}) {
|
|
7888
|
+
super(type);
|
|
7889
|
+
this[kError] = options.error === undefined ? null : options.error;
|
|
7890
|
+
this[kMessage] = options.message === undefined ? "" : options.message;
|
|
7891
|
+
}
|
|
7892
|
+
get error() {
|
|
7893
|
+
return this[kError];
|
|
7894
|
+
}
|
|
7895
|
+
get message() {
|
|
7896
|
+
return this[kMessage];
|
|
7897
|
+
}
|
|
7898
|
+
}
|
|
7899
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
7900
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
7901
|
+
|
|
7902
|
+
class MessageEvent extends Event {
|
|
7903
|
+
constructor(type, options = {}) {
|
|
7904
|
+
super(type);
|
|
7905
|
+
this[kData] = options.data === undefined ? null : options.data;
|
|
7906
|
+
}
|
|
7907
|
+
get data() {
|
|
7908
|
+
return this[kData];
|
|
7909
|
+
}
|
|
7910
|
+
}
|
|
7911
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
7912
|
+
var EventTarget = {
|
|
7913
|
+
addEventListener(type, handler, options = {}) {
|
|
7914
|
+
for (const listener of this.listeners(type)) {
|
|
7915
|
+
if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
7916
|
+
return;
|
|
7917
|
+
}
|
|
7918
|
+
}
|
|
7919
|
+
let wrapper;
|
|
7920
|
+
if (type === "message") {
|
|
7921
|
+
wrapper = function onMessage(data, isBinary) {
|
|
7922
|
+
const event = new MessageEvent("message", {
|
|
7923
|
+
data: isBinary ? data : data.toString()
|
|
7924
|
+
});
|
|
7925
|
+
event[kTarget] = this;
|
|
7926
|
+
callListener(handler, this, event);
|
|
7927
|
+
};
|
|
7928
|
+
} else if (type === "close") {
|
|
7929
|
+
wrapper = function onClose(code, message) {
|
|
7930
|
+
const event = new CloseEvent("close", {
|
|
7931
|
+
code,
|
|
7932
|
+
reason: message.toString(),
|
|
7933
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
7934
|
+
});
|
|
7935
|
+
event[kTarget] = this;
|
|
7936
|
+
callListener(handler, this, event);
|
|
7937
|
+
};
|
|
7938
|
+
} else if (type === "error") {
|
|
7939
|
+
wrapper = function onError(error48) {
|
|
7940
|
+
const event = new ErrorEvent("error", {
|
|
7941
|
+
error: error48,
|
|
7942
|
+
message: error48.message
|
|
7943
|
+
});
|
|
7944
|
+
event[kTarget] = this;
|
|
7945
|
+
callListener(handler, this, event);
|
|
7946
|
+
};
|
|
7947
|
+
} else if (type === "open") {
|
|
7948
|
+
wrapper = function onOpen() {
|
|
7949
|
+
const event = new Event("open");
|
|
7950
|
+
event[kTarget] = this;
|
|
7951
|
+
callListener(handler, this, event);
|
|
7952
|
+
};
|
|
7953
|
+
} else {
|
|
7954
|
+
return;
|
|
7955
|
+
}
|
|
7956
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
7957
|
+
wrapper[kListener] = handler;
|
|
7958
|
+
if (options.once) {
|
|
7959
|
+
this.once(type, wrapper);
|
|
7960
|
+
} else {
|
|
7961
|
+
this.on(type, wrapper);
|
|
7962
|
+
}
|
|
7963
|
+
},
|
|
7964
|
+
removeEventListener(type, handler) {
|
|
7965
|
+
for (const listener of this.listeners(type)) {
|
|
7966
|
+
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
7967
|
+
this.removeListener(type, listener);
|
|
7968
|
+
break;
|
|
7969
|
+
}
|
|
7970
|
+
}
|
|
7971
|
+
}
|
|
7972
|
+
};
|
|
7973
|
+
module.exports = {
|
|
7974
|
+
CloseEvent,
|
|
7975
|
+
ErrorEvent,
|
|
7976
|
+
Event,
|
|
7977
|
+
EventTarget,
|
|
7978
|
+
MessageEvent
|
|
7979
|
+
};
|
|
7980
|
+
function callListener(listener, thisArg, event) {
|
|
7981
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
7982
|
+
listener.handleEvent.call(listener, event);
|
|
7983
|
+
} else {
|
|
7984
|
+
listener.call(thisArg, event);
|
|
7985
|
+
}
|
|
7986
|
+
}
|
|
7987
|
+
});
|
|
7988
|
+
|
|
7989
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/extension.js
|
|
7990
|
+
var require_extension = __commonJS((exports, module) => {
|
|
7991
|
+
var { tokenChars } = require_validation2();
|
|
7992
|
+
function push(dest, name, elem) {
|
|
7993
|
+
if (dest[name] === undefined)
|
|
7994
|
+
dest[name] = [elem];
|
|
7995
|
+
else
|
|
7996
|
+
dest[name].push(elem);
|
|
7997
|
+
}
|
|
7998
|
+
function parse6(header) {
|
|
7999
|
+
const offers = Object.create(null);
|
|
8000
|
+
let params = Object.create(null);
|
|
8001
|
+
let mustUnescape = false;
|
|
8002
|
+
let isEscaping = false;
|
|
8003
|
+
let inQuotes = false;
|
|
8004
|
+
let extensionName;
|
|
8005
|
+
let paramName;
|
|
8006
|
+
let start = -1;
|
|
8007
|
+
let code = -1;
|
|
8008
|
+
let end = -1;
|
|
8009
|
+
let i = 0;
|
|
8010
|
+
for (;i < header.length; i++) {
|
|
8011
|
+
code = header.charCodeAt(i);
|
|
8012
|
+
if (extensionName === undefined) {
|
|
8013
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
8014
|
+
if (start === -1)
|
|
8015
|
+
start = i;
|
|
8016
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
8017
|
+
if (end === -1 && start !== -1)
|
|
8018
|
+
end = i;
|
|
8019
|
+
} else if (code === 59 || code === 44) {
|
|
8020
|
+
if (start === -1) {
|
|
8021
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8022
|
+
}
|
|
8023
|
+
if (end === -1)
|
|
8024
|
+
end = i;
|
|
8025
|
+
const name = header.slice(start, end);
|
|
8026
|
+
if (code === 44) {
|
|
8027
|
+
push(offers, name, params);
|
|
8028
|
+
params = Object.create(null);
|
|
8029
|
+
} else {
|
|
8030
|
+
extensionName = name;
|
|
8031
|
+
}
|
|
8032
|
+
start = end = -1;
|
|
8033
|
+
} else {
|
|
8034
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8035
|
+
}
|
|
8036
|
+
} else if (paramName === undefined) {
|
|
8037
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
8038
|
+
if (start === -1)
|
|
8039
|
+
start = i;
|
|
8040
|
+
} else if (code === 32 || code === 9) {
|
|
8041
|
+
if (end === -1 && start !== -1)
|
|
8042
|
+
end = i;
|
|
8043
|
+
} else if (code === 59 || code === 44) {
|
|
8044
|
+
if (start === -1) {
|
|
8045
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8046
|
+
}
|
|
8047
|
+
if (end === -1)
|
|
8048
|
+
end = i;
|
|
8049
|
+
push(params, header.slice(start, end), true);
|
|
8050
|
+
if (code === 44) {
|
|
8051
|
+
push(offers, extensionName, params);
|
|
8052
|
+
params = Object.create(null);
|
|
8053
|
+
extensionName = undefined;
|
|
8054
|
+
}
|
|
8055
|
+
start = end = -1;
|
|
8056
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
8057
|
+
paramName = header.slice(start, i);
|
|
8058
|
+
start = end = -1;
|
|
8059
|
+
} else {
|
|
8060
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8061
|
+
}
|
|
8062
|
+
} else {
|
|
8063
|
+
if (isEscaping) {
|
|
8064
|
+
if (tokenChars[code] !== 1) {
|
|
8065
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8066
|
+
}
|
|
8067
|
+
if (start === -1)
|
|
8068
|
+
start = i;
|
|
8069
|
+
else if (!mustUnescape)
|
|
8070
|
+
mustUnescape = true;
|
|
8071
|
+
isEscaping = false;
|
|
8072
|
+
} else if (inQuotes) {
|
|
8073
|
+
if (tokenChars[code] === 1) {
|
|
8074
|
+
if (start === -1)
|
|
8075
|
+
start = i;
|
|
8076
|
+
} else if (code === 34 && start !== -1) {
|
|
8077
|
+
inQuotes = false;
|
|
8078
|
+
end = i;
|
|
8079
|
+
} else if (code === 92) {
|
|
8080
|
+
isEscaping = true;
|
|
8081
|
+
} else {
|
|
8082
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8083
|
+
}
|
|
8084
|
+
} else if (code === 34 && header.charCodeAt(i - 1) === 61) {
|
|
8085
|
+
inQuotes = true;
|
|
8086
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
8087
|
+
if (start === -1)
|
|
8088
|
+
start = i;
|
|
8089
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
8090
|
+
if (end === -1)
|
|
8091
|
+
end = i;
|
|
8092
|
+
} else if (code === 59 || code === 44) {
|
|
8093
|
+
if (start === -1) {
|
|
8094
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8095
|
+
}
|
|
8096
|
+
if (end === -1)
|
|
8097
|
+
end = i;
|
|
8098
|
+
let value = header.slice(start, end);
|
|
8099
|
+
if (mustUnescape) {
|
|
8100
|
+
value = value.replace(/\\/g, "");
|
|
8101
|
+
mustUnescape = false;
|
|
8102
|
+
}
|
|
8103
|
+
push(params, paramName, value);
|
|
8104
|
+
if (code === 44) {
|
|
8105
|
+
push(offers, extensionName, params);
|
|
8106
|
+
params = Object.create(null);
|
|
8107
|
+
extensionName = undefined;
|
|
8108
|
+
}
|
|
8109
|
+
paramName = undefined;
|
|
8110
|
+
start = end = -1;
|
|
8111
|
+
} else {
|
|
8112
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8113
|
+
}
|
|
8114
|
+
}
|
|
8115
|
+
}
|
|
8116
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) {
|
|
8117
|
+
throw new SyntaxError("Unexpected end of input");
|
|
8118
|
+
}
|
|
8119
|
+
if (end === -1)
|
|
8120
|
+
end = i;
|
|
8121
|
+
const token = header.slice(start, end);
|
|
8122
|
+
if (extensionName === undefined) {
|
|
8123
|
+
push(offers, token, params);
|
|
8124
|
+
} else {
|
|
8125
|
+
if (paramName === undefined) {
|
|
8126
|
+
push(params, token, true);
|
|
8127
|
+
} else if (mustUnescape) {
|
|
8128
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
8129
|
+
} else {
|
|
8130
|
+
push(params, paramName, token);
|
|
8131
|
+
}
|
|
8132
|
+
push(offers, extensionName, params);
|
|
8133
|
+
}
|
|
8134
|
+
return offers;
|
|
8135
|
+
}
|
|
8136
|
+
function format(extensions) {
|
|
8137
|
+
return Object.keys(extensions).map((extension) => {
|
|
8138
|
+
let configurations = extensions[extension];
|
|
8139
|
+
if (!Array.isArray(configurations))
|
|
8140
|
+
configurations = [configurations];
|
|
8141
|
+
return configurations.map((params) => {
|
|
8142
|
+
return [extension].concat(Object.keys(params).map((k) => {
|
|
8143
|
+
let values = params[k];
|
|
8144
|
+
if (!Array.isArray(values))
|
|
8145
|
+
values = [values];
|
|
8146
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
8147
|
+
})).join("; ");
|
|
8148
|
+
}).join(", ");
|
|
8149
|
+
}).join(", ");
|
|
8150
|
+
}
|
|
8151
|
+
module.exports = { format, parse: parse6 };
|
|
8152
|
+
});
|
|
8153
|
+
|
|
8154
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/websocket.js
|
|
8155
|
+
var require_websocket = __commonJS((exports, module) => {
|
|
8156
|
+
var EventEmitter = __require("events");
|
|
8157
|
+
var https = __require("https");
|
|
8158
|
+
var http = __require("http");
|
|
8159
|
+
var net = __require("net");
|
|
8160
|
+
var tls = __require("tls");
|
|
8161
|
+
var { randomBytes, createHash } = __require("crypto");
|
|
8162
|
+
var { Duplex, Readable } = __require("stream");
|
|
8163
|
+
var { URL: URL2 } = __require("url");
|
|
8164
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
8165
|
+
var Receiver = require_receiver();
|
|
8166
|
+
var Sender = require_sender();
|
|
8167
|
+
var { isBlob } = require_validation2();
|
|
8168
|
+
var {
|
|
8169
|
+
BINARY_TYPES,
|
|
8170
|
+
CLOSE_TIMEOUT,
|
|
8171
|
+
EMPTY_BUFFER,
|
|
8172
|
+
GUID,
|
|
8173
|
+
kForOnEventAttribute,
|
|
8174
|
+
kListener,
|
|
8175
|
+
kStatusCode,
|
|
8176
|
+
kWebSocket,
|
|
8177
|
+
NOOP
|
|
8178
|
+
} = require_constants();
|
|
8179
|
+
var {
|
|
8180
|
+
EventTarget: { addEventListener, removeEventListener }
|
|
8181
|
+
} = require_event_target();
|
|
8182
|
+
var { format, parse: parse6 } = require_extension();
|
|
8183
|
+
var { toBuffer } = require_buffer_util();
|
|
8184
|
+
var kAborted = Symbol("kAborted");
|
|
8185
|
+
var protocolVersions = [8, 13];
|
|
8186
|
+
var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
8187
|
+
var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
8188
|
+
|
|
8189
|
+
class WebSocket extends EventEmitter {
|
|
8190
|
+
constructor(address, protocols, options) {
|
|
8191
|
+
super();
|
|
8192
|
+
this._binaryType = BINARY_TYPES[0];
|
|
8193
|
+
this._closeCode = 1006;
|
|
8194
|
+
this._closeFrameReceived = false;
|
|
8195
|
+
this._closeFrameSent = false;
|
|
8196
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
8197
|
+
this._closeTimer = null;
|
|
8198
|
+
this._errorEmitted = false;
|
|
8199
|
+
this._extensions = {};
|
|
8200
|
+
this._paused = false;
|
|
8201
|
+
this._protocol = "";
|
|
8202
|
+
this._readyState = WebSocket.CONNECTING;
|
|
8203
|
+
this._receiver = null;
|
|
8204
|
+
this._sender = null;
|
|
8205
|
+
this._socket = null;
|
|
8206
|
+
if (address !== null) {
|
|
8207
|
+
this._bufferedAmount = 0;
|
|
8208
|
+
this._isServer = false;
|
|
8209
|
+
this._redirects = 0;
|
|
8210
|
+
if (protocols === undefined) {
|
|
8211
|
+
protocols = [];
|
|
8212
|
+
} else if (!Array.isArray(protocols)) {
|
|
8213
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
8214
|
+
options = protocols;
|
|
8215
|
+
protocols = [];
|
|
8216
|
+
} else {
|
|
8217
|
+
protocols = [protocols];
|
|
8218
|
+
}
|
|
8219
|
+
}
|
|
8220
|
+
initAsClient(this, address, protocols, options);
|
|
8221
|
+
} else {
|
|
8222
|
+
this._autoPong = options.autoPong;
|
|
8223
|
+
this._closeTimeout = options.closeTimeout;
|
|
8224
|
+
this._isServer = true;
|
|
8225
|
+
}
|
|
8226
|
+
}
|
|
8227
|
+
get binaryType() {
|
|
8228
|
+
return this._binaryType;
|
|
8229
|
+
}
|
|
8230
|
+
set binaryType(type) {
|
|
8231
|
+
if (!BINARY_TYPES.includes(type))
|
|
8232
|
+
return;
|
|
8233
|
+
this._binaryType = type;
|
|
8234
|
+
if (this._receiver)
|
|
8235
|
+
this._receiver._binaryType = type;
|
|
8236
|
+
}
|
|
8237
|
+
get bufferedAmount() {
|
|
8238
|
+
if (!this._socket)
|
|
8239
|
+
return this._bufferedAmount;
|
|
8240
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
8241
|
+
}
|
|
8242
|
+
get extensions() {
|
|
8243
|
+
return Object.keys(this._extensions).join();
|
|
8244
|
+
}
|
|
8245
|
+
get isPaused() {
|
|
8246
|
+
return this._paused;
|
|
8247
|
+
}
|
|
8248
|
+
get onclose() {
|
|
8249
|
+
return null;
|
|
8250
|
+
}
|
|
8251
|
+
get onerror() {
|
|
8252
|
+
return null;
|
|
8253
|
+
}
|
|
8254
|
+
get onopen() {
|
|
8255
|
+
return null;
|
|
8256
|
+
}
|
|
8257
|
+
get onmessage() {
|
|
8258
|
+
return null;
|
|
8259
|
+
}
|
|
8260
|
+
get protocol() {
|
|
8261
|
+
return this._protocol;
|
|
8262
|
+
}
|
|
8263
|
+
get readyState() {
|
|
8264
|
+
return this._readyState;
|
|
8265
|
+
}
|
|
8266
|
+
get url() {
|
|
8267
|
+
return this._url;
|
|
8268
|
+
}
|
|
8269
|
+
setSocket(socket, head, options) {
|
|
8270
|
+
const receiver = new Receiver({
|
|
8271
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
8272
|
+
binaryType: this.binaryType,
|
|
8273
|
+
extensions: this._extensions,
|
|
8274
|
+
isServer: this._isServer,
|
|
8275
|
+
maxPayload: options.maxPayload,
|
|
8276
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
8277
|
+
});
|
|
8278
|
+
const sender = new Sender(socket, this._extensions, options.generateMask);
|
|
8279
|
+
this._receiver = receiver;
|
|
8280
|
+
this._sender = sender;
|
|
8281
|
+
this._socket = socket;
|
|
8282
|
+
receiver[kWebSocket] = this;
|
|
8283
|
+
sender[kWebSocket] = this;
|
|
8284
|
+
socket[kWebSocket] = this;
|
|
8285
|
+
receiver.on("conclude", receiverOnConclude);
|
|
8286
|
+
receiver.on("drain", receiverOnDrain);
|
|
8287
|
+
receiver.on("error", receiverOnError);
|
|
8288
|
+
receiver.on("message", receiverOnMessage);
|
|
8289
|
+
receiver.on("ping", receiverOnPing);
|
|
8290
|
+
receiver.on("pong", receiverOnPong);
|
|
8291
|
+
sender.onerror = senderOnError;
|
|
8292
|
+
if (socket.setTimeout)
|
|
8293
|
+
socket.setTimeout(0);
|
|
8294
|
+
if (socket.setNoDelay)
|
|
8295
|
+
socket.setNoDelay();
|
|
8296
|
+
if (head.length > 0)
|
|
8297
|
+
socket.unshift(head);
|
|
8298
|
+
socket.on("close", socketOnClose);
|
|
8299
|
+
socket.on("data", socketOnData);
|
|
8300
|
+
socket.on("end", socketOnEnd);
|
|
8301
|
+
socket.on("error", socketOnError);
|
|
8302
|
+
this._readyState = WebSocket.OPEN;
|
|
8303
|
+
this.emit("open");
|
|
8304
|
+
}
|
|
8305
|
+
emitClose() {
|
|
8306
|
+
if (!this._socket) {
|
|
8307
|
+
this._readyState = WebSocket.CLOSED;
|
|
8308
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
8309
|
+
return;
|
|
8310
|
+
}
|
|
8311
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
8312
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
8313
|
+
}
|
|
8314
|
+
this._receiver.removeAllListeners();
|
|
8315
|
+
this._readyState = WebSocket.CLOSED;
|
|
8316
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
8317
|
+
}
|
|
8318
|
+
close(code, data) {
|
|
8319
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
8320
|
+
return;
|
|
8321
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8322
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
8323
|
+
abortHandshake(this, this._req, msg);
|
|
8324
|
+
return;
|
|
8325
|
+
}
|
|
8326
|
+
if (this.readyState === WebSocket.CLOSING) {
|
|
8327
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
8328
|
+
this._socket.end();
|
|
8329
|
+
}
|
|
8330
|
+
return;
|
|
8331
|
+
}
|
|
8332
|
+
this._readyState = WebSocket.CLOSING;
|
|
8333
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
8334
|
+
if (err)
|
|
8335
|
+
return;
|
|
8336
|
+
this._closeFrameSent = true;
|
|
8337
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
8338
|
+
this._socket.end();
|
|
8339
|
+
}
|
|
8340
|
+
});
|
|
8341
|
+
setCloseTimer(this);
|
|
8342
|
+
}
|
|
8343
|
+
pause() {
|
|
8344
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
8345
|
+
return;
|
|
8346
|
+
}
|
|
8347
|
+
this._paused = true;
|
|
8348
|
+
this._socket.pause();
|
|
8349
|
+
}
|
|
8350
|
+
ping(data, mask, cb) {
|
|
8351
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8352
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
8353
|
+
}
|
|
8354
|
+
if (typeof data === "function") {
|
|
8355
|
+
cb = data;
|
|
8356
|
+
data = mask = undefined;
|
|
8357
|
+
} else if (typeof mask === "function") {
|
|
8358
|
+
cb = mask;
|
|
8359
|
+
mask = undefined;
|
|
8360
|
+
}
|
|
8361
|
+
if (typeof data === "number")
|
|
8362
|
+
data = data.toString();
|
|
8363
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
8364
|
+
sendAfterClose(this, data, cb);
|
|
8365
|
+
return;
|
|
8366
|
+
}
|
|
8367
|
+
if (mask === undefined)
|
|
8368
|
+
mask = !this._isServer;
|
|
8369
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
8370
|
+
}
|
|
8371
|
+
pong(data, mask, cb) {
|
|
8372
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8373
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
8374
|
+
}
|
|
8375
|
+
if (typeof data === "function") {
|
|
8376
|
+
cb = data;
|
|
8377
|
+
data = mask = undefined;
|
|
8378
|
+
} else if (typeof mask === "function") {
|
|
8379
|
+
cb = mask;
|
|
8380
|
+
mask = undefined;
|
|
8381
|
+
}
|
|
8382
|
+
if (typeof data === "number")
|
|
8383
|
+
data = data.toString();
|
|
8384
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
8385
|
+
sendAfterClose(this, data, cb);
|
|
8386
|
+
return;
|
|
8387
|
+
}
|
|
8388
|
+
if (mask === undefined)
|
|
8389
|
+
mask = !this._isServer;
|
|
8390
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
8391
|
+
}
|
|
8392
|
+
resume() {
|
|
8393
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
8394
|
+
return;
|
|
8395
|
+
}
|
|
8396
|
+
this._paused = false;
|
|
8397
|
+
if (!this._receiver._writableState.needDrain)
|
|
8398
|
+
this._socket.resume();
|
|
8399
|
+
}
|
|
8400
|
+
send(data, options, cb) {
|
|
8401
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8402
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
8403
|
+
}
|
|
8404
|
+
if (typeof options === "function") {
|
|
8405
|
+
cb = options;
|
|
8406
|
+
options = {};
|
|
8407
|
+
}
|
|
8408
|
+
if (typeof data === "number")
|
|
8409
|
+
data = data.toString();
|
|
8410
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
8411
|
+
sendAfterClose(this, data, cb);
|
|
8412
|
+
return;
|
|
8413
|
+
}
|
|
8414
|
+
const opts = {
|
|
8415
|
+
binary: typeof data !== "string",
|
|
8416
|
+
mask: !this._isServer,
|
|
8417
|
+
compress: true,
|
|
8418
|
+
fin: true,
|
|
8419
|
+
...options
|
|
8420
|
+
};
|
|
8421
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
8422
|
+
opts.compress = false;
|
|
8423
|
+
}
|
|
8424
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
8425
|
+
}
|
|
8426
|
+
terminate() {
|
|
8427
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
8428
|
+
return;
|
|
8429
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8430
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
8431
|
+
abortHandshake(this, this._req, msg);
|
|
8432
|
+
return;
|
|
8433
|
+
}
|
|
8434
|
+
if (this._socket) {
|
|
8435
|
+
this._readyState = WebSocket.CLOSING;
|
|
8436
|
+
this._socket.destroy();
|
|
8437
|
+
}
|
|
8438
|
+
}
|
|
8439
|
+
}
|
|
8440
|
+
Object.defineProperty(WebSocket, "CONNECTING", {
|
|
8441
|
+
enumerable: true,
|
|
8442
|
+
value: readyStates.indexOf("CONNECTING")
|
|
8443
|
+
});
|
|
8444
|
+
Object.defineProperty(WebSocket.prototype, "CONNECTING", {
|
|
8445
|
+
enumerable: true,
|
|
8446
|
+
value: readyStates.indexOf("CONNECTING")
|
|
8447
|
+
});
|
|
8448
|
+
Object.defineProperty(WebSocket, "OPEN", {
|
|
8449
|
+
enumerable: true,
|
|
8450
|
+
value: readyStates.indexOf("OPEN")
|
|
8451
|
+
});
|
|
8452
|
+
Object.defineProperty(WebSocket.prototype, "OPEN", {
|
|
8453
|
+
enumerable: true,
|
|
8454
|
+
value: readyStates.indexOf("OPEN")
|
|
8455
|
+
});
|
|
8456
|
+
Object.defineProperty(WebSocket, "CLOSING", {
|
|
8457
|
+
enumerable: true,
|
|
8458
|
+
value: readyStates.indexOf("CLOSING")
|
|
8459
|
+
});
|
|
8460
|
+
Object.defineProperty(WebSocket.prototype, "CLOSING", {
|
|
8461
|
+
enumerable: true,
|
|
8462
|
+
value: readyStates.indexOf("CLOSING")
|
|
8463
|
+
});
|
|
8464
|
+
Object.defineProperty(WebSocket, "CLOSED", {
|
|
8465
|
+
enumerable: true,
|
|
8466
|
+
value: readyStates.indexOf("CLOSED")
|
|
8467
|
+
});
|
|
8468
|
+
Object.defineProperty(WebSocket.prototype, "CLOSED", {
|
|
8469
|
+
enumerable: true,
|
|
8470
|
+
value: readyStates.indexOf("CLOSED")
|
|
8471
|
+
});
|
|
8472
|
+
[
|
|
8473
|
+
"binaryType",
|
|
8474
|
+
"bufferedAmount",
|
|
8475
|
+
"extensions",
|
|
8476
|
+
"isPaused",
|
|
8477
|
+
"protocol",
|
|
8478
|
+
"readyState",
|
|
8479
|
+
"url"
|
|
8480
|
+
].forEach((property) => {
|
|
8481
|
+
Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
|
|
8482
|
+
});
|
|
8483
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
8484
|
+
Object.defineProperty(WebSocket.prototype, `on${method}`, {
|
|
8485
|
+
enumerable: true,
|
|
8486
|
+
get() {
|
|
8487
|
+
for (const listener of this.listeners(method)) {
|
|
8488
|
+
if (listener[kForOnEventAttribute])
|
|
8489
|
+
return listener[kListener];
|
|
8490
|
+
}
|
|
8491
|
+
return null;
|
|
8492
|
+
},
|
|
8493
|
+
set(handler) {
|
|
8494
|
+
for (const listener of this.listeners(method)) {
|
|
8495
|
+
if (listener[kForOnEventAttribute]) {
|
|
8496
|
+
this.removeListener(method, listener);
|
|
8497
|
+
break;
|
|
8498
|
+
}
|
|
8499
|
+
}
|
|
8500
|
+
if (typeof handler !== "function")
|
|
8501
|
+
return;
|
|
8502
|
+
this.addEventListener(method, handler, {
|
|
8503
|
+
[kForOnEventAttribute]: true
|
|
8504
|
+
});
|
|
8505
|
+
}
|
|
8506
|
+
});
|
|
8507
|
+
});
|
|
8508
|
+
WebSocket.prototype.addEventListener = addEventListener;
|
|
8509
|
+
WebSocket.prototype.removeEventListener = removeEventListener;
|
|
8510
|
+
module.exports = WebSocket;
|
|
8511
|
+
function initAsClient(websocket, address, protocols, options) {
|
|
8512
|
+
const opts = {
|
|
8513
|
+
allowSynchronousEvents: true,
|
|
8514
|
+
autoPong: true,
|
|
8515
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
8516
|
+
protocolVersion: protocolVersions[1],
|
|
8517
|
+
maxPayload: 100 * 1024 * 1024,
|
|
8518
|
+
skipUTF8Validation: false,
|
|
8519
|
+
perMessageDeflate: true,
|
|
8520
|
+
followRedirects: false,
|
|
8521
|
+
maxRedirects: 10,
|
|
8522
|
+
...options,
|
|
8523
|
+
socketPath: undefined,
|
|
8524
|
+
hostname: undefined,
|
|
8525
|
+
protocol: undefined,
|
|
8526
|
+
timeout: undefined,
|
|
8527
|
+
method: "GET",
|
|
8528
|
+
host: undefined,
|
|
8529
|
+
path: undefined,
|
|
8530
|
+
port: undefined
|
|
8531
|
+
};
|
|
8532
|
+
websocket._autoPong = opts.autoPong;
|
|
8533
|
+
websocket._closeTimeout = opts.closeTimeout;
|
|
8534
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
8535
|
+
throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} ` + `(supported versions: ${protocolVersions.join(", ")})`);
|
|
8536
|
+
}
|
|
8537
|
+
let parsedUrl;
|
|
8538
|
+
if (address instanceof URL2) {
|
|
8539
|
+
parsedUrl = address;
|
|
8540
|
+
} else {
|
|
8541
|
+
try {
|
|
8542
|
+
parsedUrl = new URL2(address);
|
|
8543
|
+
} catch {
|
|
8544
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
8545
|
+
}
|
|
8546
|
+
}
|
|
8547
|
+
if (parsedUrl.protocol === "http:") {
|
|
8548
|
+
parsedUrl.protocol = "ws:";
|
|
8549
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
8550
|
+
parsedUrl.protocol = "wss:";
|
|
8551
|
+
}
|
|
8552
|
+
websocket._url = parsedUrl.href;
|
|
8553
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
8554
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
8555
|
+
let invalidUrlMessage;
|
|
8556
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
8557
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", ` + '"http:", "https:", or "ws+unix:"';
|
|
8558
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
8559
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
8560
|
+
} else if (parsedUrl.hash) {
|
|
8561
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
8562
|
+
}
|
|
8563
|
+
if (invalidUrlMessage) {
|
|
8564
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
8565
|
+
if (websocket._redirects === 0) {
|
|
8566
|
+
throw err;
|
|
8567
|
+
} else {
|
|
8568
|
+
emitErrorAndClose(websocket, err);
|
|
8569
|
+
return;
|
|
8570
|
+
}
|
|
8571
|
+
}
|
|
8572
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
8573
|
+
const key = randomBytes(16).toString("base64");
|
|
8574
|
+
const request = isSecure ? https.request : http.request;
|
|
8575
|
+
const protocolSet = new Set;
|
|
8576
|
+
let perMessageDeflate;
|
|
8577
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
8578
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
8579
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
8580
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
8581
|
+
opts.headers = {
|
|
8582
|
+
...opts.headers,
|
|
8583
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
8584
|
+
"Sec-WebSocket-Key": key,
|
|
8585
|
+
Connection: "Upgrade",
|
|
8586
|
+
Upgrade: "websocket"
|
|
8587
|
+
};
|
|
8588
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
8589
|
+
opts.timeout = opts.handshakeTimeout;
|
|
8590
|
+
if (opts.perMessageDeflate) {
|
|
8591
|
+
perMessageDeflate = new PerMessageDeflate({
|
|
8592
|
+
...opts.perMessageDeflate,
|
|
8593
|
+
isServer: false,
|
|
8594
|
+
maxPayload: opts.maxPayload
|
|
8595
|
+
});
|
|
8596
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({
|
|
8597
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
8598
|
+
});
|
|
8599
|
+
}
|
|
8600
|
+
if (protocols.length) {
|
|
8601
|
+
for (const protocol of protocols) {
|
|
8602
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
8603
|
+
throw new SyntaxError("An invalid or duplicated subprotocol was specified");
|
|
8604
|
+
}
|
|
8605
|
+
protocolSet.add(protocol);
|
|
8606
|
+
}
|
|
8607
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
8608
|
+
}
|
|
8609
|
+
if (opts.origin) {
|
|
8610
|
+
if (opts.protocolVersion < 13) {
|
|
8611
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
8612
|
+
} else {
|
|
8613
|
+
opts.headers.Origin = opts.origin;
|
|
8614
|
+
}
|
|
8615
|
+
}
|
|
8616
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
8617
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
8618
|
+
}
|
|
8619
|
+
if (isIpcUrl) {
|
|
8620
|
+
const parts = opts.path.split(":");
|
|
8621
|
+
opts.socketPath = parts[0];
|
|
8622
|
+
opts.path = parts[1];
|
|
8623
|
+
}
|
|
8624
|
+
let req;
|
|
8625
|
+
if (opts.followRedirects) {
|
|
8626
|
+
if (websocket._redirects === 0) {
|
|
8627
|
+
websocket._originalIpc = isIpcUrl;
|
|
8628
|
+
websocket._originalSecure = isSecure;
|
|
8629
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
8630
|
+
const headers = options && options.headers;
|
|
8631
|
+
options = { ...options, headers: {} };
|
|
8632
|
+
if (headers) {
|
|
8633
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
8634
|
+
options.headers[key2.toLowerCase()] = value;
|
|
8635
|
+
}
|
|
8636
|
+
}
|
|
8637
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
8638
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
8639
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
8640
|
+
delete opts.headers.authorization;
|
|
8641
|
+
delete opts.headers.cookie;
|
|
8642
|
+
if (!isSameHost)
|
|
8643
|
+
delete opts.headers.host;
|
|
8644
|
+
opts.auth = undefined;
|
|
8645
|
+
}
|
|
8646
|
+
}
|
|
8647
|
+
if (opts.auth && !options.headers.authorization) {
|
|
8648
|
+
options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
8649
|
+
}
|
|
8650
|
+
req = websocket._req = request(opts);
|
|
8651
|
+
if (websocket._redirects) {
|
|
8652
|
+
websocket.emit("redirect", websocket.url, req);
|
|
8653
|
+
}
|
|
8654
|
+
} else {
|
|
8655
|
+
req = websocket._req = request(opts);
|
|
8656
|
+
}
|
|
8657
|
+
if (opts.timeout) {
|
|
8658
|
+
req.on("timeout", () => {
|
|
8659
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
8660
|
+
});
|
|
8661
|
+
}
|
|
8662
|
+
req.on("error", (err) => {
|
|
8663
|
+
if (req === null || req[kAborted])
|
|
8664
|
+
return;
|
|
8665
|
+
req = websocket._req = null;
|
|
8666
|
+
emitErrorAndClose(websocket, err);
|
|
8667
|
+
});
|
|
8668
|
+
req.on("response", (res) => {
|
|
8669
|
+
const location = res.headers.location;
|
|
8670
|
+
const statusCode = res.statusCode;
|
|
8671
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
8672
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
8673
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
8674
|
+
return;
|
|
8675
|
+
}
|
|
8676
|
+
req.abort();
|
|
8677
|
+
let addr;
|
|
8678
|
+
try {
|
|
8679
|
+
addr = new URL2(location, address);
|
|
8680
|
+
} catch (e) {
|
|
8681
|
+
const err = new SyntaxError(`Invalid URL: ${location}`);
|
|
8682
|
+
emitErrorAndClose(websocket, err);
|
|
8683
|
+
return;
|
|
8684
|
+
}
|
|
8685
|
+
initAsClient(websocket, addr, protocols, options);
|
|
8686
|
+
} else if (!websocket.emit("unexpected-response", req, res)) {
|
|
8687
|
+
abortHandshake(websocket, req, `Unexpected server response: ${res.statusCode}`);
|
|
8688
|
+
}
|
|
8689
|
+
});
|
|
8690
|
+
req.on("upgrade", (res, socket, head) => {
|
|
8691
|
+
websocket.emit("upgrade", res);
|
|
8692
|
+
if (websocket.readyState !== WebSocket.CONNECTING)
|
|
8693
|
+
return;
|
|
8694
|
+
req = websocket._req = null;
|
|
8695
|
+
const upgrade = res.headers.upgrade;
|
|
8696
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
8697
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
8698
|
+
return;
|
|
8699
|
+
}
|
|
8700
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
8701
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
8702
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
8703
|
+
return;
|
|
8704
|
+
}
|
|
8705
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
8706
|
+
let protError;
|
|
8707
|
+
if (serverProt !== undefined) {
|
|
8708
|
+
if (!protocolSet.size) {
|
|
8709
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
8710
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
8711
|
+
protError = "Server sent an invalid subprotocol";
|
|
8712
|
+
}
|
|
8713
|
+
} else if (protocolSet.size) {
|
|
8714
|
+
protError = "Server sent no subprotocol";
|
|
8715
|
+
}
|
|
8716
|
+
if (protError) {
|
|
8717
|
+
abortHandshake(websocket, socket, protError);
|
|
8718
|
+
return;
|
|
8719
|
+
}
|
|
8720
|
+
if (serverProt)
|
|
8721
|
+
websocket._protocol = serverProt;
|
|
8722
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
8723
|
+
if (secWebSocketExtensions !== undefined) {
|
|
8724
|
+
if (!perMessageDeflate) {
|
|
8725
|
+
const message = "Server sent a Sec-WebSocket-Extensions header but no extension " + "was requested";
|
|
8726
|
+
abortHandshake(websocket, socket, message);
|
|
8727
|
+
return;
|
|
8728
|
+
}
|
|
8729
|
+
let extensions;
|
|
8730
|
+
try {
|
|
8731
|
+
extensions = parse6(secWebSocketExtensions);
|
|
8732
|
+
} catch (err) {
|
|
8733
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
8734
|
+
abortHandshake(websocket, socket, message);
|
|
8735
|
+
return;
|
|
8736
|
+
}
|
|
8737
|
+
const extensionNames = Object.keys(extensions);
|
|
8738
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
8739
|
+
const message = "Server indicated an extension that was not requested";
|
|
8740
|
+
abortHandshake(websocket, socket, message);
|
|
8741
|
+
return;
|
|
8742
|
+
}
|
|
8743
|
+
try {
|
|
8744
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
8745
|
+
} catch (err) {
|
|
8746
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
8747
|
+
abortHandshake(websocket, socket, message);
|
|
8748
|
+
return;
|
|
8749
|
+
}
|
|
8750
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
8751
|
+
}
|
|
8752
|
+
websocket.setSocket(socket, head, {
|
|
8753
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
8754
|
+
generateMask: opts.generateMask,
|
|
8755
|
+
maxPayload: opts.maxPayload,
|
|
8756
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
8757
|
+
});
|
|
8758
|
+
});
|
|
8759
|
+
if (opts.finishRequest) {
|
|
8760
|
+
opts.finishRequest(req, websocket);
|
|
8761
|
+
} else {
|
|
8762
|
+
req.end();
|
|
8763
|
+
}
|
|
8764
|
+
}
|
|
8765
|
+
function emitErrorAndClose(websocket, err) {
|
|
8766
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8767
|
+
websocket._errorEmitted = true;
|
|
8768
|
+
websocket.emit("error", err);
|
|
8769
|
+
websocket.emitClose();
|
|
8770
|
+
}
|
|
8771
|
+
function netConnect(options) {
|
|
8772
|
+
options.path = options.socketPath;
|
|
8773
|
+
return net.connect(options);
|
|
8774
|
+
}
|
|
8775
|
+
function tlsConnect(options) {
|
|
8776
|
+
options.path = undefined;
|
|
8777
|
+
if (!options.servername && options.servername !== "") {
|
|
8778
|
+
options.servername = net.isIP(options.host) ? "" : options.host;
|
|
8779
|
+
}
|
|
8780
|
+
return tls.connect(options);
|
|
8781
|
+
}
|
|
8782
|
+
function abortHandshake(websocket, stream, message) {
|
|
8783
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8784
|
+
const err = new Error(message);
|
|
8785
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
8786
|
+
if (stream.setHeader) {
|
|
8787
|
+
stream[kAborted] = true;
|
|
8788
|
+
stream.abort();
|
|
8789
|
+
if (stream.socket && !stream.socket.destroyed) {
|
|
8790
|
+
stream.socket.destroy();
|
|
8791
|
+
}
|
|
8792
|
+
process.nextTick(emitErrorAndClose, websocket, err);
|
|
8793
|
+
} else {
|
|
8794
|
+
stream.destroy(err);
|
|
8795
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
8796
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
8797
|
+
}
|
|
8798
|
+
}
|
|
8799
|
+
function sendAfterClose(websocket, data, cb) {
|
|
8800
|
+
if (data) {
|
|
8801
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
8802
|
+
if (websocket._socket)
|
|
8803
|
+
websocket._sender._bufferedBytes += length;
|
|
8804
|
+
else
|
|
8805
|
+
websocket._bufferedAmount += length;
|
|
8806
|
+
}
|
|
8807
|
+
if (cb) {
|
|
8808
|
+
const err = new Error(`WebSocket is not open: readyState ${websocket.readyState} ` + `(${readyStates[websocket.readyState]})`);
|
|
8809
|
+
process.nextTick(cb, err);
|
|
8810
|
+
}
|
|
8811
|
+
}
|
|
8812
|
+
function receiverOnConclude(code, reason) {
|
|
8813
|
+
const websocket = this[kWebSocket];
|
|
8814
|
+
websocket._closeFrameReceived = true;
|
|
8815
|
+
websocket._closeMessage = reason;
|
|
8816
|
+
websocket._closeCode = code;
|
|
8817
|
+
if (websocket._socket[kWebSocket] === undefined)
|
|
8818
|
+
return;
|
|
8819
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
8820
|
+
process.nextTick(resume, websocket._socket);
|
|
8821
|
+
if (code === 1005)
|
|
8822
|
+
websocket.close();
|
|
8823
|
+
else
|
|
8824
|
+
websocket.close(code, reason);
|
|
8825
|
+
}
|
|
8826
|
+
function receiverOnDrain() {
|
|
8827
|
+
const websocket = this[kWebSocket];
|
|
8828
|
+
if (!websocket.isPaused)
|
|
8829
|
+
websocket._socket.resume();
|
|
8830
|
+
}
|
|
8831
|
+
function receiverOnError(err) {
|
|
8832
|
+
const websocket = this[kWebSocket];
|
|
8833
|
+
if (websocket._socket[kWebSocket] !== undefined) {
|
|
8834
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
8835
|
+
process.nextTick(resume, websocket._socket);
|
|
8836
|
+
websocket.close(err[kStatusCode]);
|
|
8837
|
+
}
|
|
8838
|
+
if (!websocket._errorEmitted) {
|
|
8839
|
+
websocket._errorEmitted = true;
|
|
8840
|
+
websocket.emit("error", err);
|
|
8841
|
+
}
|
|
8842
|
+
}
|
|
8843
|
+
function receiverOnFinish() {
|
|
8844
|
+
this[kWebSocket].emitClose();
|
|
8845
|
+
}
|
|
8846
|
+
function receiverOnMessage(data, isBinary) {
|
|
8847
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
8848
|
+
}
|
|
8849
|
+
function receiverOnPing(data) {
|
|
8850
|
+
const websocket = this[kWebSocket];
|
|
8851
|
+
if (websocket._autoPong)
|
|
8852
|
+
websocket.pong(data, !this._isServer, NOOP);
|
|
8853
|
+
websocket.emit("ping", data);
|
|
8854
|
+
}
|
|
8855
|
+
function receiverOnPong(data) {
|
|
8856
|
+
this[kWebSocket].emit("pong", data);
|
|
6359
8857
|
}
|
|
6360
|
-
function
|
|
6361
|
-
|
|
6362
|
-
return;
|
|
6363
|
-
const d1 = new Date(dt1).valueOf();
|
|
6364
|
-
const d2 = new Date(dt2).valueOf();
|
|
6365
|
-
if (!(d1 && d2))
|
|
6366
|
-
return;
|
|
6367
|
-
return d1 - d2;
|
|
8858
|
+
function resume(stream) {
|
|
8859
|
+
stream.resume();
|
|
6368
8860
|
}
|
|
6369
|
-
function
|
|
6370
|
-
|
|
6371
|
-
|
|
6372
|
-
const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR);
|
|
6373
|
-
const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR);
|
|
6374
|
-
const res = compareDate(d1, d2);
|
|
6375
|
-
if (res === undefined)
|
|
8861
|
+
function senderOnError(err) {
|
|
8862
|
+
const websocket = this[kWebSocket];
|
|
8863
|
+
if (websocket.readyState === WebSocket.CLOSED)
|
|
6376
8864
|
return;
|
|
6377
|
-
|
|
6378
|
-
|
|
6379
|
-
|
|
6380
|
-
|
|
6381
|
-
|
|
6382
|
-
|
|
8865
|
+
if (websocket.readyState === WebSocket.OPEN) {
|
|
8866
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8867
|
+
setCloseTimer(websocket);
|
|
8868
|
+
}
|
|
8869
|
+
this._socket.end();
|
|
8870
|
+
if (!websocket._errorEmitted) {
|
|
8871
|
+
websocket._errorEmitted = true;
|
|
8872
|
+
websocket.emit("error", err);
|
|
8873
|
+
}
|
|
8874
|
+
}
|
|
8875
|
+
function setCloseTimer(websocket) {
|
|
8876
|
+
websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), websocket._closeTimeout);
|
|
8877
|
+
}
|
|
8878
|
+
function socketOnClose() {
|
|
8879
|
+
const websocket = this[kWebSocket];
|
|
8880
|
+
this.removeListener("close", socketOnClose);
|
|
8881
|
+
this.removeListener("data", socketOnData);
|
|
8882
|
+
this.removeListener("end", socketOnEnd);
|
|
8883
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8884
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
|
|
8885
|
+
const chunk = this.read(this._readableState.length);
|
|
8886
|
+
websocket._receiver.write(chunk);
|
|
8887
|
+
}
|
|
8888
|
+
websocket._receiver.end();
|
|
8889
|
+
this[kWebSocket] = undefined;
|
|
8890
|
+
clearTimeout(websocket._closeTimer);
|
|
8891
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
|
|
8892
|
+
websocket.emitClose();
|
|
8893
|
+
} else {
|
|
8894
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
8895
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
8896
|
+
}
|
|
6383
8897
|
}
|
|
6384
|
-
|
|
6385
|
-
|
|
6386
|
-
|
|
6387
|
-
|
|
8898
|
+
function socketOnData(chunk) {
|
|
8899
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
8900
|
+
this.pause();
|
|
8901
|
+
}
|
|
6388
8902
|
}
|
|
6389
|
-
|
|
6390
|
-
|
|
6391
|
-
|
|
6392
|
-
|
|
8903
|
+
function socketOnEnd() {
|
|
8904
|
+
const websocket = this[kWebSocket];
|
|
8905
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8906
|
+
websocket._receiver.end();
|
|
8907
|
+
this.end();
|
|
6393
8908
|
}
|
|
6394
|
-
function
|
|
6395
|
-
|
|
8909
|
+
function socketOnError() {
|
|
8910
|
+
const websocket = this[kWebSocket];
|
|
8911
|
+
this.removeListener("error", socketOnError);
|
|
8912
|
+
this.on("error", NOOP);
|
|
8913
|
+
if (websocket) {
|
|
8914
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8915
|
+
this.destroy();
|
|
8916
|
+
}
|
|
6396
8917
|
}
|
|
6397
|
-
|
|
6398
|
-
|
|
8918
|
+
});
|
|
8919
|
+
|
|
8920
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/stream.js
|
|
8921
|
+
var require_stream = __commonJS((exports, module) => {
|
|
8922
|
+
var WebSocket = require_websocket();
|
|
8923
|
+
var { Duplex } = __require("stream");
|
|
8924
|
+
function emitClose(stream) {
|
|
8925
|
+
stream.emit("close");
|
|
8926
|
+
}
|
|
8927
|
+
function duplexOnEnd() {
|
|
8928
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
8929
|
+
this.destroy();
|
|
8930
|
+
}
|
|
8931
|
+
}
|
|
8932
|
+
function duplexOnError(err) {
|
|
8933
|
+
this.removeListener("error", duplexOnError);
|
|
8934
|
+
this.destroy();
|
|
8935
|
+
if (this.listenerCount("error") === 0) {
|
|
8936
|
+
this.emit("error", err);
|
|
8937
|
+
}
|
|
8938
|
+
}
|
|
8939
|
+
function createWebSocketStream(ws, options) {
|
|
8940
|
+
let terminateOnDestroy = true;
|
|
8941
|
+
const duplex = new Duplex({
|
|
8942
|
+
...options,
|
|
8943
|
+
autoDestroy: false,
|
|
8944
|
+
emitClose: false,
|
|
8945
|
+
objectMode: false,
|
|
8946
|
+
writableObjectMode: false
|
|
8947
|
+
});
|
|
8948
|
+
ws.on("message", function message(msg, isBinary) {
|
|
8949
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
8950
|
+
if (!duplex.push(data))
|
|
8951
|
+
ws.pause();
|
|
8952
|
+
});
|
|
8953
|
+
ws.once("error", function error48(err) {
|
|
8954
|
+
if (duplex.destroyed)
|
|
8955
|
+
return;
|
|
8956
|
+
terminateOnDestroy = false;
|
|
8957
|
+
duplex.destroy(err);
|
|
8958
|
+
});
|
|
8959
|
+
ws.once("close", function close() {
|
|
8960
|
+
if (duplex.destroyed)
|
|
8961
|
+
return;
|
|
8962
|
+
duplex.push(null);
|
|
8963
|
+
});
|
|
8964
|
+
duplex._destroy = function(err, callback) {
|
|
8965
|
+
if (ws.readyState === ws.CLOSED) {
|
|
8966
|
+
callback(err);
|
|
8967
|
+
process.nextTick(emitClose, duplex);
|
|
8968
|
+
return;
|
|
8969
|
+
}
|
|
8970
|
+
let called = false;
|
|
8971
|
+
ws.once("error", function error48(err2) {
|
|
8972
|
+
called = true;
|
|
8973
|
+
callback(err2);
|
|
8974
|
+
});
|
|
8975
|
+
ws.once("close", function close() {
|
|
8976
|
+
if (!called)
|
|
8977
|
+
callback(err);
|
|
8978
|
+
process.nextTick(emitClose, duplex);
|
|
8979
|
+
});
|
|
8980
|
+
if (terminateOnDestroy)
|
|
8981
|
+
ws.terminate();
|
|
8982
|
+
};
|
|
8983
|
+
duplex._final = function(callback) {
|
|
8984
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
8985
|
+
ws.once("open", function open() {
|
|
8986
|
+
duplex._final(callback);
|
|
8987
|
+
});
|
|
8988
|
+
return;
|
|
8989
|
+
}
|
|
8990
|
+
if (ws._socket === null)
|
|
8991
|
+
return;
|
|
8992
|
+
if (ws._socket._writableState.finished) {
|
|
8993
|
+
callback();
|
|
8994
|
+
if (duplex._readableState.endEmitted)
|
|
8995
|
+
duplex.destroy();
|
|
8996
|
+
} else {
|
|
8997
|
+
ws._socket.once("finish", function finish() {
|
|
8998
|
+
callback();
|
|
8999
|
+
});
|
|
9000
|
+
ws.close();
|
|
9001
|
+
}
|
|
9002
|
+
};
|
|
9003
|
+
duplex._read = function() {
|
|
9004
|
+
if (ws.isPaused)
|
|
9005
|
+
ws.resume();
|
|
9006
|
+
};
|
|
9007
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
9008
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
9009
|
+
ws.once("open", function open() {
|
|
9010
|
+
duplex._write(chunk, encoding, callback);
|
|
9011
|
+
});
|
|
9012
|
+
return;
|
|
9013
|
+
}
|
|
9014
|
+
ws.send(chunk, callback);
|
|
9015
|
+
};
|
|
9016
|
+
duplex.on("end", duplexOnEnd);
|
|
9017
|
+
duplex.on("error", duplexOnError);
|
|
9018
|
+
return duplex;
|
|
6399
9019
|
}
|
|
6400
|
-
|
|
6401
|
-
|
|
6402
|
-
|
|
6403
|
-
|
|
6404
|
-
|
|
6405
|
-
|
|
6406
|
-
|
|
6407
|
-
|
|
6408
|
-
|
|
9020
|
+
module.exports = createWebSocketStream;
|
|
9021
|
+
});
|
|
9022
|
+
|
|
9023
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/subprotocol.js
|
|
9024
|
+
var require_subprotocol = __commonJS((exports, module) => {
|
|
9025
|
+
var { tokenChars } = require_validation2();
|
|
9026
|
+
function parse6(header) {
|
|
9027
|
+
const protocols = new Set;
|
|
9028
|
+
let start = -1;
|
|
9029
|
+
let end = -1;
|
|
9030
|
+
let i = 0;
|
|
9031
|
+
for (i;i < header.length; i++) {
|
|
9032
|
+
const code = header.charCodeAt(i);
|
|
9033
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
9034
|
+
if (start === -1)
|
|
9035
|
+
start = i;
|
|
9036
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
9037
|
+
if (end === -1 && start !== -1)
|
|
9038
|
+
end = i;
|
|
9039
|
+
} else if (code === 44) {
|
|
9040
|
+
if (start === -1) {
|
|
9041
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
9042
|
+
}
|
|
9043
|
+
if (end === -1)
|
|
9044
|
+
end = i;
|
|
9045
|
+
const protocol2 = header.slice(start, end);
|
|
9046
|
+
if (protocols.has(protocol2)) {
|
|
9047
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
9048
|
+
}
|
|
9049
|
+
protocols.add(protocol2);
|
|
9050
|
+
start = end = -1;
|
|
9051
|
+
} else {
|
|
9052
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
9053
|
+
}
|
|
6409
9054
|
}
|
|
9055
|
+
if (start === -1 || end !== -1) {
|
|
9056
|
+
throw new SyntaxError("Unexpected end of input");
|
|
9057
|
+
}
|
|
9058
|
+
const protocol = header.slice(start, i);
|
|
9059
|
+
if (protocols.has(protocol)) {
|
|
9060
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
9061
|
+
}
|
|
9062
|
+
protocols.add(protocol);
|
|
9063
|
+
return protocols;
|
|
6410
9064
|
}
|
|
9065
|
+
module.exports = { parse: parse6 };
|
|
6411
9066
|
});
|
|
6412
9067
|
|
|
6413
|
-
// ../../node_modules/.pnpm/
|
|
6414
|
-
var
|
|
6415
|
-
|
|
6416
|
-
|
|
6417
|
-
var
|
|
6418
|
-
var
|
|
6419
|
-
var
|
|
6420
|
-
var
|
|
6421
|
-
|
|
6422
|
-
|
|
6423
|
-
|
|
6424
|
-
|
|
6425
|
-
|
|
6426
|
-
var
|
|
6427
|
-
|
|
6428
|
-
|
|
6429
|
-
|
|
6430
|
-
|
|
6431
|
-
|
|
6432
|
-
|
|
6433
|
-
|
|
6434
|
-
|
|
6435
|
-
|
|
6436
|
-
|
|
6437
|
-
|
|
6438
|
-
|
|
6439
|
-
|
|
9068
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/lib/websocket-server.js
|
|
9069
|
+
var require_websocket_server = __commonJS((exports, module) => {
|
|
9070
|
+
var EventEmitter = __require("events");
|
|
9071
|
+
var http = __require("http");
|
|
9072
|
+
var { Duplex } = __require("stream");
|
|
9073
|
+
var { createHash } = __require("crypto");
|
|
9074
|
+
var extension = require_extension();
|
|
9075
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
9076
|
+
var subprotocol = require_subprotocol();
|
|
9077
|
+
var WebSocket = require_websocket();
|
|
9078
|
+
var { CLOSE_TIMEOUT, GUID, kWebSocket } = require_constants();
|
|
9079
|
+
var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
9080
|
+
var RUNNING = 0;
|
|
9081
|
+
var CLOSING = 1;
|
|
9082
|
+
var CLOSED = 2;
|
|
9083
|
+
|
|
9084
|
+
class WebSocketServer extends EventEmitter {
|
|
9085
|
+
constructor(options, callback) {
|
|
9086
|
+
super();
|
|
9087
|
+
options = {
|
|
9088
|
+
allowSynchronousEvents: true,
|
|
9089
|
+
autoPong: true,
|
|
9090
|
+
maxPayload: 100 * 1024 * 1024,
|
|
9091
|
+
skipUTF8Validation: false,
|
|
9092
|
+
perMessageDeflate: false,
|
|
9093
|
+
handleProtocols: null,
|
|
9094
|
+
clientTracking: true,
|
|
9095
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
9096
|
+
verifyClient: null,
|
|
9097
|
+
noServer: false,
|
|
9098
|
+
backlog: null,
|
|
9099
|
+
server: null,
|
|
9100
|
+
host: null,
|
|
9101
|
+
path: null,
|
|
9102
|
+
port: null,
|
|
9103
|
+
WebSocket,
|
|
9104
|
+
...options
|
|
9105
|
+
};
|
|
9106
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
|
|
9107
|
+
throw new TypeError('One and only one of the "port", "server", or "noServer" options ' + "must be specified");
|
|
9108
|
+
}
|
|
9109
|
+
if (options.port != null) {
|
|
9110
|
+
this._server = http.createServer((req, res) => {
|
|
9111
|
+
const body = http.STATUS_CODES[426];
|
|
9112
|
+
res.writeHead(426, {
|
|
9113
|
+
"Content-Length": body.length,
|
|
9114
|
+
"Content-Type": "text/plain"
|
|
9115
|
+
});
|
|
9116
|
+
res.end(body);
|
|
9117
|
+
});
|
|
9118
|
+
this._server.listen(options.port, options.host, options.backlog, callback);
|
|
9119
|
+
} else if (options.server) {
|
|
9120
|
+
this._server = options.server;
|
|
9121
|
+
}
|
|
9122
|
+
if (this._server) {
|
|
9123
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
9124
|
+
this._removeListeners = addListeners(this._server, {
|
|
9125
|
+
listening: this.emit.bind(this, "listening"),
|
|
9126
|
+
error: this.emit.bind(this, "error"),
|
|
9127
|
+
upgrade: (req, socket, head) => {
|
|
9128
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
9129
|
+
}
|
|
9130
|
+
});
|
|
9131
|
+
}
|
|
9132
|
+
if (options.perMessageDeflate === true)
|
|
9133
|
+
options.perMessageDeflate = {};
|
|
9134
|
+
if (options.clientTracking) {
|
|
9135
|
+
this.clients = new Set;
|
|
9136
|
+
this._shouldEmitClose = false;
|
|
9137
|
+
}
|
|
9138
|
+
this.options = options;
|
|
9139
|
+
this._state = RUNNING;
|
|
9140
|
+
}
|
|
9141
|
+
address() {
|
|
9142
|
+
if (this.options.noServer) {
|
|
9143
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
9144
|
+
}
|
|
9145
|
+
if (!this._server)
|
|
9146
|
+
return null;
|
|
9147
|
+
return this._server.address();
|
|
9148
|
+
}
|
|
9149
|
+
close(cb) {
|
|
9150
|
+
if (this._state === CLOSED) {
|
|
9151
|
+
if (cb) {
|
|
9152
|
+
this.once("close", () => {
|
|
9153
|
+
cb(new Error("The server is not running"));
|
|
9154
|
+
});
|
|
9155
|
+
}
|
|
9156
|
+
process.nextTick(emitClose, this);
|
|
6440
9157
|
return;
|
|
6441
|
-
|
|
6442
|
-
if (
|
|
6443
|
-
|
|
6444
|
-
|
|
6445
|
-
|
|
6446
|
-
|
|
6447
|
-
|
|
6448
|
-
|
|
6449
|
-
|
|
9158
|
+
}
|
|
9159
|
+
if (cb)
|
|
9160
|
+
this.once("close", cb);
|
|
9161
|
+
if (this._state === CLOSING)
|
|
9162
|
+
return;
|
|
9163
|
+
this._state = CLOSING;
|
|
9164
|
+
if (this.options.noServer || this.options.server) {
|
|
9165
|
+
if (this._server) {
|
|
9166
|
+
this._removeListeners();
|
|
9167
|
+
this._removeListeners = this._server = null;
|
|
9168
|
+
}
|
|
9169
|
+
if (this.clients) {
|
|
9170
|
+
if (!this.clients.size) {
|
|
9171
|
+
process.nextTick(emitClose, this);
|
|
9172
|
+
} else {
|
|
9173
|
+
this._shouldEmitClose = true;
|
|
9174
|
+
}
|
|
9175
|
+
} else {
|
|
9176
|
+
process.nextTick(emitClose, this);
|
|
9177
|
+
}
|
|
9178
|
+
} else {
|
|
9179
|
+
const server = this._server;
|
|
9180
|
+
this._removeListeners();
|
|
9181
|
+
this._removeListeners = this._server = null;
|
|
9182
|
+
server.close(() => {
|
|
9183
|
+
emitClose(this);
|
|
6450
9184
|
});
|
|
6451
|
-
const fmt = gen.const("fmt", (0, codegen_1._)`${fmts}[${fCxt.schemaCode}]`);
|
|
6452
|
-
cxt.fail$data((0, codegen_1.or)((0, codegen_1._)`typeof ${fmt} != "object"`, (0, codegen_1._)`${fmt} instanceof RegExp`, (0, codegen_1._)`typeof ${fmt}.compare != "function"`, compareCode(fmt)));
|
|
6453
9185
|
}
|
|
6454
|
-
|
|
6455
|
-
|
|
6456
|
-
|
|
6457
|
-
|
|
9186
|
+
}
|
|
9187
|
+
shouldHandle(req) {
|
|
9188
|
+
if (this.options.path) {
|
|
9189
|
+
const index = req.url.indexOf("?");
|
|
9190
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
|
9191
|
+
if (pathname !== this.options.path)
|
|
9192
|
+
return false;
|
|
9193
|
+
}
|
|
9194
|
+
return true;
|
|
9195
|
+
}
|
|
9196
|
+
handleUpgrade(req, socket, head, cb) {
|
|
9197
|
+
socket.on("error", socketOnError);
|
|
9198
|
+
const key = req.headers["sec-websocket-key"];
|
|
9199
|
+
const upgrade = req.headers.upgrade;
|
|
9200
|
+
const version2 = +req.headers["sec-websocket-version"];
|
|
9201
|
+
if (req.method !== "GET") {
|
|
9202
|
+
const message = "Invalid HTTP method";
|
|
9203
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
|
|
9204
|
+
return;
|
|
9205
|
+
}
|
|
9206
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
9207
|
+
const message = "Invalid Upgrade header";
|
|
9208
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
9209
|
+
return;
|
|
9210
|
+
}
|
|
9211
|
+
if (key === undefined || !keyRegex.test(key)) {
|
|
9212
|
+
const message = "Missing or invalid Sec-WebSocket-Key header";
|
|
9213
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
9214
|
+
return;
|
|
9215
|
+
}
|
|
9216
|
+
if (version2 !== 13 && version2 !== 8) {
|
|
9217
|
+
const message = "Missing or invalid Sec-WebSocket-Version header";
|
|
9218
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
|
|
9219
|
+
"Sec-WebSocket-Version": "13, 8"
|
|
9220
|
+
});
|
|
9221
|
+
return;
|
|
9222
|
+
}
|
|
9223
|
+
if (!this.shouldHandle(req)) {
|
|
9224
|
+
abortHandshake(socket, 400);
|
|
9225
|
+
return;
|
|
9226
|
+
}
|
|
9227
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
9228
|
+
let protocols = new Set;
|
|
9229
|
+
if (secWebSocketProtocol !== undefined) {
|
|
9230
|
+
try {
|
|
9231
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
9232
|
+
} catch (err) {
|
|
9233
|
+
const message = "Invalid Sec-WebSocket-Protocol header";
|
|
9234
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
6458
9235
|
return;
|
|
6459
|
-
if (typeof fmtDef != "object" || fmtDef instanceof RegExp || typeof fmtDef.compare != "function") {
|
|
6460
|
-
throw new Error(`"${keyword}": format "${format}" does not define "compare" function`);
|
|
6461
9236
|
}
|
|
6462
|
-
|
|
6463
|
-
|
|
6464
|
-
|
|
6465
|
-
|
|
9237
|
+
}
|
|
9238
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
9239
|
+
const extensions = {};
|
|
9240
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== undefined) {
|
|
9241
|
+
const perMessageDeflate = new PerMessageDeflate({
|
|
9242
|
+
...this.options.perMessageDeflate,
|
|
9243
|
+
isServer: true,
|
|
9244
|
+
maxPayload: this.options.maxPayload
|
|
6466
9245
|
});
|
|
6467
|
-
|
|
9246
|
+
try {
|
|
9247
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
9248
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
9249
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
9250
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
9251
|
+
}
|
|
9252
|
+
} catch (err) {
|
|
9253
|
+
const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
9254
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
9255
|
+
return;
|
|
9256
|
+
}
|
|
6468
9257
|
}
|
|
6469
|
-
|
|
6470
|
-
|
|
9258
|
+
if (this.options.verifyClient) {
|
|
9259
|
+
const info = {
|
|
9260
|
+
origin: req.headers[`${version2 === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
9261
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
9262
|
+
req
|
|
9263
|
+
};
|
|
9264
|
+
if (this.options.verifyClient.length === 2) {
|
|
9265
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
9266
|
+
if (!verified) {
|
|
9267
|
+
return abortHandshake(socket, code || 401, message, headers);
|
|
9268
|
+
}
|
|
9269
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
9270
|
+
});
|
|
9271
|
+
return;
|
|
9272
|
+
}
|
|
9273
|
+
if (!this.options.verifyClient(info))
|
|
9274
|
+
return abortHandshake(socket, 401);
|
|
9275
|
+
}
|
|
9276
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
9277
|
+
}
|
|
9278
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
9279
|
+
if (!socket.readable || !socket.writable)
|
|
9280
|
+
return socket.destroy();
|
|
9281
|
+
if (socket[kWebSocket]) {
|
|
9282
|
+
throw new Error("server.handleUpgrade() was called more than once with the same " + "socket, possibly due to a misconfiguration");
|
|
9283
|
+
}
|
|
9284
|
+
if (this._state > RUNNING)
|
|
9285
|
+
return abortHandshake(socket, 503);
|
|
9286
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
9287
|
+
const headers = [
|
|
9288
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
9289
|
+
"Upgrade: websocket",
|
|
9290
|
+
"Connection: Upgrade",
|
|
9291
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
9292
|
+
];
|
|
9293
|
+
const ws = new this.options.WebSocket(null, undefined, this.options);
|
|
9294
|
+
if (protocols.size) {
|
|
9295
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
9296
|
+
if (protocol) {
|
|
9297
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
9298
|
+
ws._protocol = protocol;
|
|
9299
|
+
}
|
|
9300
|
+
}
|
|
9301
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
9302
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
9303
|
+
const value = extension.format({
|
|
9304
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
9305
|
+
});
|
|
9306
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
9307
|
+
ws._extensions = extensions;
|
|
6471
9308
|
}
|
|
6472
|
-
|
|
6473
|
-
|
|
6474
|
-
|
|
6475
|
-
|
|
6476
|
-
|
|
6477
|
-
|
|
6478
|
-
|
|
6479
|
-
|
|
6480
|
-
|
|
6481
|
-
|
|
6482
|
-
|
|
6483
|
-
|
|
6484
|
-
|
|
6485
|
-
|
|
6486
|
-
|
|
6487
|
-
|
|
6488
|
-
|
|
6489
|
-
|
|
6490
|
-
|
|
6491
|
-
|
|
6492
|
-
addFormats(ajv, opts, formats_1.fullFormats, fullName);
|
|
6493
|
-
return ajv;
|
|
9309
|
+
this.emit("headers", headers, req);
|
|
9310
|
+
socket.write(headers.concat(`\r
|
|
9311
|
+
`).join(`\r
|
|
9312
|
+
`));
|
|
9313
|
+
socket.removeListener("error", socketOnError);
|
|
9314
|
+
ws.setSocket(socket, head, {
|
|
9315
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
9316
|
+
maxPayload: this.options.maxPayload,
|
|
9317
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
9318
|
+
});
|
|
9319
|
+
if (this.clients) {
|
|
9320
|
+
this.clients.add(ws);
|
|
9321
|
+
ws.on("close", () => {
|
|
9322
|
+
this.clients.delete(ws);
|
|
9323
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
9324
|
+
process.nextTick(emitClose, this);
|
|
9325
|
+
}
|
|
9326
|
+
});
|
|
9327
|
+
}
|
|
9328
|
+
cb(ws, req);
|
|
6494
9329
|
}
|
|
6495
|
-
const [formats, exportName] = opts.mode === "fast" ? [formats_1.fastFormats, fastName] : [formats_1.fullFormats, fullName];
|
|
6496
|
-
const list = opts.formats || formats_1.formatNames;
|
|
6497
|
-
addFormats(ajv, list, formats, exportName);
|
|
6498
|
-
if (opts.keywords)
|
|
6499
|
-
(0, limit_1.default)(ajv);
|
|
6500
|
-
return ajv;
|
|
6501
|
-
};
|
|
6502
|
-
formatsPlugin.get = (name, mode = "full") => {
|
|
6503
|
-
const formats = mode === "fast" ? formats_1.fastFormats : formats_1.fullFormats;
|
|
6504
|
-
const f = formats[name];
|
|
6505
|
-
if (!f)
|
|
6506
|
-
throw new Error(`Unknown format "${name}"`);
|
|
6507
|
-
return f;
|
|
6508
|
-
};
|
|
6509
|
-
function addFormats(ajv, list, fs, exportName) {
|
|
6510
|
-
var _a2;
|
|
6511
|
-
var _b;
|
|
6512
|
-
(_a2 = (_b = ajv.opts.code).formats) !== null && _a2 !== undefined || (_b.formats = (0, codegen_1._)`require("ajv-formats/dist/formats").${exportName}`);
|
|
6513
|
-
for (const f of list)
|
|
6514
|
-
ajv.addFormat(f, fs[f]);
|
|
6515
9330
|
}
|
|
6516
|
-
module.exports =
|
|
6517
|
-
|
|
6518
|
-
|
|
9331
|
+
module.exports = WebSocketServer;
|
|
9332
|
+
function addListeners(server, map3) {
|
|
9333
|
+
for (const event of Object.keys(map3))
|
|
9334
|
+
server.on(event, map3[event]);
|
|
9335
|
+
return function removeListeners() {
|
|
9336
|
+
for (const event of Object.keys(map3)) {
|
|
9337
|
+
server.removeListener(event, map3[event]);
|
|
9338
|
+
}
|
|
9339
|
+
};
|
|
9340
|
+
}
|
|
9341
|
+
function emitClose(server) {
|
|
9342
|
+
server._state = CLOSED;
|
|
9343
|
+
server.emit("close");
|
|
9344
|
+
}
|
|
9345
|
+
function socketOnError() {
|
|
9346
|
+
this.destroy();
|
|
9347
|
+
}
|
|
9348
|
+
function abortHandshake(socket, code, message, headers) {
|
|
9349
|
+
message = message || http.STATUS_CODES[code];
|
|
9350
|
+
headers = {
|
|
9351
|
+
Connection: "close",
|
|
9352
|
+
"Content-Type": "text/html",
|
|
9353
|
+
"Content-Length": Buffer.byteLength(message),
|
|
9354
|
+
...headers
|
|
9355
|
+
};
|
|
9356
|
+
socket.once("finish", socket.destroy);
|
|
9357
|
+
socket.end(`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
9358
|
+
` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join(`\r
|
|
9359
|
+
`) + `\r
|
|
9360
|
+
\r
|
|
9361
|
+
` + message);
|
|
9362
|
+
}
|
|
9363
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
9364
|
+
if (server.listenerCount("wsClientError")) {
|
|
9365
|
+
const err = new Error(message);
|
|
9366
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
9367
|
+
server.emit("wsClientError", err, socket, req);
|
|
9368
|
+
} else {
|
|
9369
|
+
abortHandshake(socket, code, message, headers);
|
|
9370
|
+
}
|
|
9371
|
+
}
|
|
9372
|
+
});
|
|
9373
|
+
|
|
9374
|
+
// ../../node_modules/.pnpm/ws@8.20.0/node_modules/ws/index.js
|
|
9375
|
+
var require_ws = __commonJS((exports, module) => {
|
|
9376
|
+
var createWebSocketStream = require_stream();
|
|
9377
|
+
var extension = require_extension();
|
|
9378
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
9379
|
+
var Receiver = require_receiver();
|
|
9380
|
+
var Sender = require_sender();
|
|
9381
|
+
var subprotocol = require_subprotocol();
|
|
9382
|
+
var WebSocket = require_websocket();
|
|
9383
|
+
var WebSocketServer = require_websocket_server();
|
|
9384
|
+
WebSocket.createWebSocketStream = createWebSocketStream;
|
|
9385
|
+
WebSocket.extension = extension;
|
|
9386
|
+
WebSocket.PerMessageDeflate = PerMessageDeflate;
|
|
9387
|
+
WebSocket.Receiver = Receiver;
|
|
9388
|
+
WebSocket.Sender = Sender;
|
|
9389
|
+
WebSocket.Server = WebSocketServer;
|
|
9390
|
+
WebSocket.subprotocol = subprotocol;
|
|
9391
|
+
WebSocket.WebSocket = WebSocket;
|
|
9392
|
+
WebSocket.WebSocketServer = WebSocketServer;
|
|
9393
|
+
module.exports = WebSocket;
|
|
6519
9394
|
});
|
|
6520
9395
|
|
|
6521
9396
|
// ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v3/helpers/util.js
|
|
@@ -28572,6 +31447,13 @@ class PermissionChecker {
|
|
|
28572
31447
|
}
|
|
28573
31448
|
|
|
28574
31449
|
// src/room.ts
|
|
31450
|
+
var WS = typeof globalThis.WebSocket !== "undefined" ? globalThis.WebSocket : (() => {
|
|
31451
|
+
try {
|
|
31452
|
+
return require_ws();
|
|
31453
|
+
} catch {
|
|
31454
|
+
return null;
|
|
31455
|
+
}
|
|
31456
|
+
})();
|
|
28575
31457
|
var INITIAL_BACKOFF_MS = 1000;
|
|
28576
31458
|
var MAX_BACKOFF_MS = 30000;
|
|
28577
31459
|
var MAX_RECONNECT_ATTEMPTS = 10;
|
|
@@ -28619,7 +31501,7 @@ class Kodama {
|
|
|
28619
31501
|
}
|
|
28620
31502
|
leave() {
|
|
28621
31503
|
this.closed = true;
|
|
28622
|
-
if (this.ws && this.ws.readyState ===
|
|
31504
|
+
if (this.ws && this.ws.readyState === WS.OPEN) {
|
|
28623
31505
|
this.ws.send(JSON.stringify({ action: "leave" }));
|
|
28624
31506
|
this.ws.close();
|
|
28625
31507
|
}
|
|
@@ -28635,7 +31517,7 @@ class Kodama {
|
|
|
28635
31517
|
return this.roomState;
|
|
28636
31518
|
}
|
|
28637
31519
|
connect() {
|
|
28638
|
-
const ws = new
|
|
31520
|
+
const ws = new WS(this.relayUrl);
|
|
28639
31521
|
this.ws = ws;
|
|
28640
31522
|
ws.addEventListener("open", () => {
|
|
28641
31523
|
this.reconnectAttempts = 0;
|
|
@@ -28763,7 +31645,7 @@ class Kodama {
|
|
|
28763
31645
|
const content = typeof response === "string" ? response : response.content;
|
|
28764
31646
|
const done = typeof response === "string" ? undefined : response.done;
|
|
28765
31647
|
const filtered = this.permissions.filterMessage(content);
|
|
28766
|
-
if (this.ws && this.ws.readyState ===
|
|
31648
|
+
if (this.ws && this.ws.readyState === WS.OPEN) {
|
|
28767
31649
|
this.ws.send(JSON.stringify({
|
|
28768
31650
|
action: "message",
|
|
28769
31651
|
content: filtered,
|
|
@@ -28906,7 +31788,7 @@ var server = new McpServer({ name: "kodama", version: "1.0.0" }, {
|
|
|
28906
31788
|
"Kodama is a chatroom where AI agents have conversations on behalf of their owners.",
|
|
28907
31789
|
"",
|
|
28908
31790
|
"WORKFLOW:",
|
|
28909
|
-
"1.
|
|
31791
|
+
"1. kodama_create_and_join_room \u2014 creates a room AND auto-joins. Display the COMPLETE output to the user verbatim \u2014 ALL links (invite, owner view, spectate) and room code. Do NOT summarize or rephrase. Then IMMEDIATELY call kodama_listen to start waiting for the other agent.",
|
|
28910
31792
|
"2. kodama_listen \u2014 wait for messages. If status is 'waiting', call kodama_listen again. If status is 'your_turn', respond with kodama_say.",
|
|
28911
31793
|
"3. After kodama_say: immediately call kodama_listen again.",
|
|
28912
31794
|
"4. REPEAT steps 2-3. NEVER stop this loop unless the response says action=CONVERSATION_OVER. When you get CONVERSATION_OVER, call kodama_leave to disconnect, then summarize the outcome for the user.",
|
|
@@ -28916,7 +31798,7 @@ var server = new McpServer({ name: "kodama", version: "1.0.0" }, {
|
|
|
28916
31798
|
].join(`
|
|
28917
31799
|
`)
|
|
28918
31800
|
});
|
|
28919
|
-
server.tool("
|
|
31801
|
+
server.tool("kodama_create_and_join_room", "Create a Kodama chatroom and automatically join it. IMPORTANT: Display the returned output VERBATIM to the user \u2014 do not rephrase or summarize. Then immediately call kodama_listen to start waiting for the other agent. Do NOT wait for the user to tell you to proceed.", {
|
|
28920
31802
|
topic: exports_external.string().optional().describe("What this conversation is about, e.g. 'Dinner planning with Brandon'"),
|
|
28921
31803
|
agent_name: exports_external.string().optional().describe("Display name for your agent. Use the user's name + the AI model name, e.g. 'Yining's Claude', 'Brandon's Codex'. Infer from context."),
|
|
28922
31804
|
max_rounds: exports_external.number().optional().describe("Turn cap (1-100, default 15). Each agent speaking once = 1 round.")
|
|
@@ -28961,19 +31843,7 @@ server.tool("kodama_create_room", "Create a Kodama chatroom and automatically jo
|
|
|
28961
31843
|
await room.join();
|
|
28962
31844
|
} catch (err) {
|
|
28963
31845
|
turnHandlerInstalled = false;
|
|
28964
|
-
|
|
28965
|
-
try {
|
|
28966
|
-
const inviteRes = await fetch(`${DEFAULT_RELAY}/api/rooms/${data.code}/invite`, {
|
|
28967
|
-
method: "POST",
|
|
28968
|
-
headers: { "Content-Type": "application/json" },
|
|
28969
|
-
body: JSON.stringify({})
|
|
28970
|
-
});
|
|
28971
|
-
if (inviteRes.ok) {
|
|
28972
|
-
const inviteData = await inviteRes.json();
|
|
28973
|
-
newToken = inviteData.inviteToken;
|
|
28974
|
-
}
|
|
28975
|
-
} catch {}
|
|
28976
|
-
savedInviteToken = newToken;
|
|
31846
|
+
savedInviteToken = null;
|
|
28977
31847
|
const msg = err.message;
|
|
28978
31848
|
return {
|
|
28979
31849
|
content: [{
|
|
@@ -28987,7 +31857,7 @@ server.tool("kodama_create_room", "Create a Kodama chatroom and automatically jo
|
|
|
28987
31857
|
`Share with your friend:`,
|
|
28988
31858
|
`${WEB_URL}/room/${data.code}#invite=${friendToken}`,
|
|
28989
31859
|
``,
|
|
28990
|
-
|
|
31860
|
+
`Call kodama_create_and_join_room again to start fresh.`
|
|
28991
31861
|
].join(`
|
|
28992
31862
|
`)
|
|
28993
31863
|
}],
|
|
@@ -29034,7 +31904,7 @@ server.tool("kodama_join", "Join a Kodama chatroom. If you just created the room
|
|
|
29034
31904
|
const token = invite_token ?? savedInviteToken;
|
|
29035
31905
|
if (!code || !token) {
|
|
29036
31906
|
return {
|
|
29037
|
-
content: [{ type: "text", text: "Missing room_code or invite_token. Create a room first with
|
|
31907
|
+
content: [{ type: "text", text: "Missing room_code or invite_token. Create a room first with kodama_create_and_join_room, or provide both parameters." }],
|
|
29038
31908
|
isError: true
|
|
29039
31909
|
};
|
|
29040
31910
|
}
|