scream-code 0.18.9 → 0.19.1
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/README.md +26 -5
- package/dist/{app-D3QT1bx1.mjs → app-p26PYlkc.mjs} +5054 -4839
- package/dist/bot-ui/assets/index-Bhcj384G.css +1 -0
- package/dist/bot-ui/assets/index-C0tnq2Bw.js +455 -0
- package/dist/bot-ui/index.html +13 -0
- package/dist/{dispatch-JYsyD0aN.mjs → dispatch-Brm4KZ8f.mjs} +1 -1
- package/dist/{dispatch-BcRHd4ya.mjs → dispatch-NoArU5dN.mjs} +3795 -46
- package/dist/main.mjs +1 -1
- package/dist/{src-B1S88rK6.mjs → src-CsT5xa84.mjs} +1 -1
- package/dist/{src-DIVvoAYO.mjs → src-Cu3sUIJK.mjs} +3 -3
- package/dist/{text-input-dialog-CgAOMXv1.mjs → text-input-dialog-BmdLwvYc.mjs} +1 -1
- package/dist/{text-input-dialog-CqagQQtW.mjs → text-input-dialog-CCDiUbcL.mjs} +2 -0
- package/package.json +27 -18
- package/dist/public/assets/astro-DAp21e1Q.js +0 -2
- package/dist/public/assets/astro-DAp21e1Q.js.map +0 -1
- package/dist/public/assets/bash-Yzrsuije.js +0 -2
- package/dist/public/assets/bash-Yzrsuije.js.map +0 -1
- package/dist/public/assets/c-BIGW1oBm.js +0 -2
- package/dist/public/assets/c-BIGW1oBm.js.map +0 -1
- package/dist/public/assets/cpp-BRuaLJcg.js +0 -2
- package/dist/public/assets/cpp-BRuaLJcg.js.map +0 -1
- package/dist/public/assets/csharp-COcwbKMJ.js +0 -2
- package/dist/public/assets/csharp-COcwbKMJ.js.map +0 -1
- package/dist/public/assets/css-DEUVOdPa.js +0 -2
- package/dist/public/assets/css-DEUVOdPa.js.map +0 -1
- package/dist/public/assets/dart-CF10PKvl.js +0 -2
- package/dist/public/assets/dart-CF10PKvl.js.map +0 -1
- package/dist/public/assets/docker-BcOcwvcX.js +0 -2
- package/dist/public/assets/docker-BcOcwvcX.js.map +0 -1
- package/dist/public/assets/github-dark-DHJKELXO.js +0 -2
- package/dist/public/assets/github-dark-DHJKELXO.js.map +0 -1
- package/dist/public/assets/github-light-DAi9KRSo.js +0 -2
- package/dist/public/assets/github-light-DAi9KRSo.js.map +0 -1
- package/dist/public/assets/go-CxLEBnE3.js +0 -2
- package/dist/public/assets/go-CxLEBnE3.js.map +0 -1
- package/dist/public/assets/graphql-ChdNCCLP.js +0 -2
- package/dist/public/assets/graphql-ChdNCCLP.js.map +0 -1
- package/dist/public/assets/html-CNxCFRDw.js +0 -2
- package/dist/public/assets/html-CNxCFRDw.js.map +0 -1
- package/dist/public/assets/index-BIs_1Cpx.js +0 -240
- package/dist/public/assets/index-BIs_1Cpx.js.map +0 -1
- package/dist/public/assets/index-bnhnZew2.css +0 -1
- package/dist/public/assets/ini-BEwlwnbL.js +0 -2
- package/dist/public/assets/ini-BEwlwnbL.js.map +0 -1
- package/dist/public/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/dist/public/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/dist/public/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/dist/public/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/dist/public/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/dist/public/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/dist/public/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/dist/public/assets/java-CylS5w8V.js +0 -2
- package/dist/public/assets/java-CylS5w8V.js.map +0 -1
- package/dist/public/assets/javascript-wDzz0qaB.js +0 -2
- package/dist/public/assets/javascript-wDzz0qaB.js.map +0 -1
- package/dist/public/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/public/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/public/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/public/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/public/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/public/assets/json-Cp-IABpG.js +0 -2
- package/dist/public/assets/json-Cp-IABpG.js.map +0 -1
- package/dist/public/assets/jsx-g9-lgVsj.js +0 -2
- package/dist/public/assets/jsx-g9-lgVsj.js.map +0 -1
- package/dist/public/assets/kotlin-BdnUsdx6.js +0 -2
- package/dist/public/assets/kotlin-BdnUsdx6.js.map +0 -1
- package/dist/public/assets/logo-v2-ChmPAIbI.svg +0 -1
- package/dist/public/assets/lua-BaeVxFsk.js +0 -2
- package/dist/public/assets/lua-BaeVxFsk.js.map +0 -1
- package/dist/public/assets/make-CHLpvVh8.js +0 -2
- package/dist/public/assets/make-CHLpvVh8.js.map +0 -1
- package/dist/public/assets/markdown-Cvjx9yec.js +0 -2
- package/dist/public/assets/markdown-Cvjx9yec.js.map +0 -1
- package/dist/public/assets/perl-CptaX86O.js +0 -2
- package/dist/public/assets/perl-CptaX86O.js.map +0 -1
- package/dist/public/assets/postcss-DnzrXTVv.js +0 -2
- package/dist/public/assets/postcss-DnzrXTVv.js.map +0 -1
- package/dist/public/assets/protobuf-C7zT0LnQ.js +0 -2
- package/dist/public/assets/protobuf-C7zT0LnQ.js.map +0 -1
- package/dist/public/assets/python-B6aJPvgy.js +0 -2
- package/dist/public/assets/python-B6aJPvgy.js.map +0 -1
- package/dist/public/assets/r-Dspwwk_N.js +0 -2
- package/dist/public/assets/r-Dspwwk_N.js.map +0 -1
- package/dist/public/assets/ruby-CcrC3V9I.js +0 -2
- package/dist/public/assets/ruby-CcrC3V9I.js.map +0 -1
- package/dist/public/assets/rust-B1yitclQ.js +0 -2
- package/dist/public/assets/rust-B1yitclQ.js.map +0 -1
- package/dist/public/assets/scala-C151Ov-r.js +0 -2
- package/dist/public/assets/scala-C151Ov-r.js.map +0 -1
- package/dist/public/assets/sql-BLtJtn59.js +0 -2
- package/dist/public/assets/sql-BLtJtn59.js.map +0 -1
- package/dist/public/assets/svelte-U4fHntn6.js +0 -2
- package/dist/public/assets/svelte-U4fHntn6.js.map +0 -1
- package/dist/public/assets/swift-D82vCrfD.js +0 -2
- package/dist/public/assets/swift-D82vCrfD.js.map +0 -1
- package/dist/public/assets/toml-vGWfd6FD.js +0 -2
- package/dist/public/assets/toml-vGWfd6FD.js.map +0 -1
- package/dist/public/assets/tsx-COt5Ahok.js +0 -2
- package/dist/public/assets/tsx-COt5Ahok.js.map +0 -1
- package/dist/public/assets/typescript-BPQ3VLAy.js +0 -2
- package/dist/public/assets/typescript-BPQ3VLAy.js.map +0 -1
- package/dist/public/assets/xml-sdJ4AIDG.js +0 -2
- package/dist/public/assets/xml-sdJ4AIDG.js.map +0 -1
- package/dist/public/assets/yaml-Buea-lGh.js +0 -2
- package/dist/public/assets/yaml-Buea-lGh.js.map +0 -1
- package/dist/public/gateway.html +0 -156
- package/dist/public/icon.ico +0 -0
- package/dist/public/index.html +0 -29
|
@@ -4,9 +4,9 @@ import { dirname as __cjsShimDirname } from 'node:path';
|
|
|
4
4
|
const __filename = __cjsShimFileURLToPath(import.meta.url);
|
|
5
5
|
const __dirname = __cjsShimDirname(__filename);
|
|
6
6
|
import { i as __require, o as __toESM, r as __exportAll, t as __commonJSMin } from "./chunk-D90kvbyJ.mjs";
|
|
7
|
-
import { A as sharedMemoStore, B as buildProjectTagCloud, C as applyConsolidation, D as parseMemoryMemos, E as buildExitExtractionPrompt, F as probeLocalEmbeddingSupport, G as join$1, H as basename$1, I as createMemoryMemo, J as relative$1, K as normalize, L as toSummary, M as clearEmbeddingModelCache, N as createEmbeddingEngine, O as stripMemoryTags, P as hasEmbeddingModelCache$1, R as generateTags, S as DreamTracker, T as EXIT_EXTRACTION_SYSTEM_PROMPT, U as dirname$2, V as rankMemos, W as isAbsolute$1, Y as resolve$2, a as isSupportedFile, b as sharedKnowledgeStoreReady, i as ingestFile, j as classifyEmbeddingFailure, k as MemoryMemoStore, q as parse$7, r as ingestDirectory, t as multiSearch, w as buildConsolidationPlan, y as sharedKnowledgeStore, z as normalizeTags } from "./src-
|
|
7
|
+
import { A as sharedMemoStore, B as buildProjectTagCloud, C as applyConsolidation, D as parseMemoryMemos, E as buildExitExtractionPrompt, F as probeLocalEmbeddingSupport, G as join$1, H as basename$1, I as createMemoryMemo, J as relative$1, K as normalize, L as toSummary, M as clearEmbeddingModelCache, N as createEmbeddingEngine, O as stripMemoryTags, P as hasEmbeddingModelCache$1, R as generateTags, S as DreamTracker, T as EXIT_EXTRACTION_SYSTEM_PROMPT, U as dirname$2, V as rankMemos, W as isAbsolute$1, Y as resolve$2, a as isSupportedFile, b as sharedKnowledgeStoreReady, i as ingestFile, j as classifyEmbeddingFailure, k as MemoryMemoStore, q as parse$7, r as ingestDirectory, t as multiSearch, w as buildConsolidationPlan, y as sharedKnowledgeStore, z as normalizeTags } from "./src-Cu3sUIJK.mjs";
|
|
8
8
|
import { n as require_base64_js, t as XMLParser } from "./XMLParser-DFAnGqkc.mjs";
|
|
9
|
-
import { a as assertScreamHostIdentity, i as t, n as getLocale, o as createScreamDefaultHeaders, r as setLocale, t as TextInputDialogComponent } from "./text-input-dialog-
|
|
9
|
+
import { a as assertScreamHostIdentity, i as t, n as getLocale, o as createScreamDefaultHeaders, r as setLocale, t as TextInputDialogComponent } from "./text-input-dialog-CCDiUbcL.mjs";
|
|
10
10
|
import { createRequire } from "node:module";
|
|
11
11
|
import { createHash, randomBytes, randomInt, randomUUID } from "node:crypto";
|
|
12
12
|
import * as fs$1 from "node:fs/promises";
|
|
@@ -20,7 +20,8 @@ import * as fs$11 from "fs/promises";
|
|
|
20
20
|
import { writeFile as writeFile$1 } from "fs/promises";
|
|
21
21
|
import Cs, { PassThrough, Readable } from "node:stream";
|
|
22
22
|
import { finished, pipeline as pipeline$1 } from "node:stream/promises";
|
|
23
|
-
import * as
|
|
23
|
+
import * as vs from "zlib";
|
|
24
|
+
import en from "zlib";
|
|
24
25
|
import * as fs$10 from "node:fs";
|
|
25
26
|
import Kt, { appendFileSync, chmodSync, closeSync, constants, createReadStream, createWriteStream as createWriteStream$1, existsSync, fchmodSync, fsyncSync, mkdirSync, openSync, promises, readFileSync, readSync, readdirSync, renameSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
|
|
26
27
|
import * as path$8 from "node:path";
|
|
@@ -33,8 +34,6 @@ import { promisify } from "node:util";
|
|
|
33
34
|
import { EventEmitter as EventEmitter$1 } from "node:events";
|
|
34
35
|
import { StringDecoder } from "node:string_decoder";
|
|
35
36
|
import zi from "assert";
|
|
36
|
-
import * as vs from "zlib";
|
|
37
|
-
import en from "zlib";
|
|
38
37
|
import co from "node:assert";
|
|
39
38
|
import { diffLines, diffWords } from "diff";
|
|
40
39
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
@@ -16807,7 +16806,7 @@ var require_certificatesubjecttokensupplier = /* @__PURE__ */ __commonJSMin(((ex
|
|
|
16807
16806
|
const util_1 = require_util$1();
|
|
16808
16807
|
const fs$14 = __require("fs");
|
|
16809
16808
|
const crypto_1 = __require("crypto");
|
|
16810
|
-
const https = __require("https");
|
|
16809
|
+
const https$1 = __require("https");
|
|
16811
16810
|
exports.CERTIFICATE_CONFIGURATION_ENV_VARIABLE = "GOOGLE_API_CERTIFICATE_CONFIG";
|
|
16812
16811
|
/**
|
|
16813
16812
|
* Thrown when the certificate source cannot be located or accessed.
|
|
@@ -16854,7 +16853,7 @@ var require_certificatesubjecttokensupplier = /* @__PURE__ */ __commonJSMin(((ex
|
|
|
16854
16853
|
*/
|
|
16855
16854
|
async createMtlsHttpsAgent() {
|
|
16856
16855
|
if (!this.key || !this.cert) throw new InvalidConfigurationError("Cannot create mTLS Agent with missing certificate or key");
|
|
16857
|
-
return new https.Agent({
|
|
16856
|
+
return new https$1.Agent({
|
|
16858
16857
|
key: this.key,
|
|
16859
16858
|
cert: this.cert
|
|
16860
16859
|
});
|
|
@@ -19253,9 +19252,3585 @@ var require_src = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
|
19253
19252
|
exports.auth = new googleauth_1.GoogleAuth();
|
|
19254
19253
|
}));
|
|
19255
19254
|
//#endregion
|
|
19256
|
-
//#region ../../node_modules/.pnpm
|
|
19255
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/constants.js
|
|
19256
|
+
var require_constants$1 = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
19257
|
+
const BINARY_TYPES = [
|
|
19258
|
+
"nodebuffer",
|
|
19259
|
+
"arraybuffer",
|
|
19260
|
+
"fragments"
|
|
19261
|
+
];
|
|
19262
|
+
const hasBlob = typeof Blob !== "undefined";
|
|
19263
|
+
if (hasBlob) BINARY_TYPES.push("blob");
|
|
19264
|
+
module.exports = {
|
|
19265
|
+
BINARY_TYPES,
|
|
19266
|
+
CLOSE_TIMEOUT: 3e4,
|
|
19267
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
19268
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
19269
|
+
hasBlob,
|
|
19270
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
19271
|
+
kListener: Symbol("kListener"),
|
|
19272
|
+
kStatusCode: Symbol("status-code"),
|
|
19273
|
+
kWebSocket: Symbol("websocket"),
|
|
19274
|
+
NOOP: () => {}
|
|
19275
|
+
};
|
|
19276
|
+
}));
|
|
19277
|
+
//#endregion
|
|
19278
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/buffer-util.js
|
|
19279
|
+
var require_buffer_util = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
19280
|
+
const { EMPTY_BUFFER } = require_constants$1();
|
|
19281
|
+
const FastBuffer = Buffer[Symbol.species];
|
|
19282
|
+
/**
|
|
19283
|
+
* Merges an array of buffers into a new buffer.
|
|
19284
|
+
*
|
|
19285
|
+
* @param {Buffer[]} list The array of buffers to concat
|
|
19286
|
+
* @param {Number} totalLength The total length of buffers in the list
|
|
19287
|
+
* @return {Buffer} The resulting buffer
|
|
19288
|
+
* @public
|
|
19289
|
+
*/
|
|
19290
|
+
function concat(list, totalLength) {
|
|
19291
|
+
if (list.length === 0) return EMPTY_BUFFER;
|
|
19292
|
+
if (list.length === 1) return list[0];
|
|
19293
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
19294
|
+
let offset = 0;
|
|
19295
|
+
for (let i = 0; i < list.length; i++) {
|
|
19296
|
+
const buf = list[i];
|
|
19297
|
+
target.set(buf, offset);
|
|
19298
|
+
offset += buf.length;
|
|
19299
|
+
}
|
|
19300
|
+
if (offset < totalLength) return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
19301
|
+
return target;
|
|
19302
|
+
}
|
|
19303
|
+
/**
|
|
19304
|
+
* Masks a buffer using the given mask.
|
|
19305
|
+
*
|
|
19306
|
+
* @param {Buffer} source The buffer to mask
|
|
19307
|
+
* @param {Buffer} mask The mask to use
|
|
19308
|
+
* @param {Buffer} output The buffer where to store the result
|
|
19309
|
+
* @param {Number} offset The offset at which to start writing
|
|
19310
|
+
* @param {Number} length The number of bytes to mask.
|
|
19311
|
+
* @public
|
|
19312
|
+
*/
|
|
19313
|
+
function _mask(source, mask, output, offset, length) {
|
|
19314
|
+
for (let i = 0; i < length; i++) output[offset + i] = source[i] ^ mask[i & 3];
|
|
19315
|
+
}
|
|
19316
|
+
/**
|
|
19317
|
+
* Unmasks a buffer using the given mask.
|
|
19318
|
+
*
|
|
19319
|
+
* @param {Buffer} buffer The buffer to unmask
|
|
19320
|
+
* @param {Buffer} mask The mask to use
|
|
19321
|
+
* @public
|
|
19322
|
+
*/
|
|
19323
|
+
function _unmask(buffer, mask) {
|
|
19324
|
+
for (let i = 0; i < buffer.length; i++) buffer[i] ^= mask[i & 3];
|
|
19325
|
+
}
|
|
19326
|
+
/**
|
|
19327
|
+
* Converts a buffer to an `ArrayBuffer`.
|
|
19328
|
+
*
|
|
19329
|
+
* @param {Buffer} buf The buffer to convert
|
|
19330
|
+
* @return {ArrayBuffer} Converted buffer
|
|
19331
|
+
* @public
|
|
19332
|
+
*/
|
|
19333
|
+
function toArrayBuffer(buf) {
|
|
19334
|
+
if (buf.length === buf.buffer.byteLength) return buf.buffer;
|
|
19335
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
19336
|
+
}
|
|
19337
|
+
/**
|
|
19338
|
+
* Converts `data` to a `Buffer`.
|
|
19339
|
+
*
|
|
19340
|
+
* @param {*} data The data to convert
|
|
19341
|
+
* @return {Buffer} The buffer
|
|
19342
|
+
* @throws {TypeError}
|
|
19343
|
+
* @public
|
|
19344
|
+
*/
|
|
19345
|
+
function toBuffer(data) {
|
|
19346
|
+
toBuffer.readOnly = true;
|
|
19347
|
+
if (Buffer.isBuffer(data)) return data;
|
|
19348
|
+
let buf;
|
|
19349
|
+
if (data instanceof ArrayBuffer) buf = new FastBuffer(data);
|
|
19350
|
+
else if (ArrayBuffer.isView(data)) buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
19351
|
+
else {
|
|
19352
|
+
buf = Buffer.from(data);
|
|
19353
|
+
toBuffer.readOnly = false;
|
|
19354
|
+
}
|
|
19355
|
+
return buf;
|
|
19356
|
+
}
|
|
19357
|
+
module.exports = {
|
|
19358
|
+
concat,
|
|
19359
|
+
mask: _mask,
|
|
19360
|
+
toArrayBuffer,
|
|
19361
|
+
toBuffer,
|
|
19362
|
+
unmask: _unmask
|
|
19363
|
+
};
|
|
19364
|
+
/* istanbul ignore else */
|
|
19365
|
+
if (!process.env.WS_NO_BUFFER_UTIL) try {
|
|
19366
|
+
const bufferUtil = __require("bufferutil");
|
|
19367
|
+
module.exports.mask = function(source, mask, output, offset, length) {
|
|
19368
|
+
if (length < 48) _mask(source, mask, output, offset, length);
|
|
19369
|
+
else bufferUtil.mask(source, mask, output, offset, length);
|
|
19370
|
+
};
|
|
19371
|
+
module.exports.unmask = function(buffer, mask) {
|
|
19372
|
+
if (buffer.length < 32) _unmask(buffer, mask);
|
|
19373
|
+
else bufferUtil.unmask(buffer, mask);
|
|
19374
|
+
};
|
|
19375
|
+
} catch (e) {}
|
|
19376
|
+
}));
|
|
19377
|
+
//#endregion
|
|
19378
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/limiter.js
|
|
19379
|
+
var require_limiter = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
19380
|
+
const kDone = Symbol("kDone");
|
|
19381
|
+
const kRun = Symbol("kRun");
|
|
19382
|
+
/**
|
|
19383
|
+
* A very simple job queue with adjustable concurrency. Adapted from
|
|
19384
|
+
* https://github.com/STRML/async-limiter
|
|
19385
|
+
*/
|
|
19386
|
+
var Limiter = class {
|
|
19387
|
+
/**
|
|
19388
|
+
* Creates a new `Limiter`.
|
|
19389
|
+
*
|
|
19390
|
+
* @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
|
|
19391
|
+
* to run concurrently
|
|
19392
|
+
*/
|
|
19393
|
+
constructor(concurrency) {
|
|
19394
|
+
this[kDone] = () => {
|
|
19395
|
+
this.pending--;
|
|
19396
|
+
this[kRun]();
|
|
19397
|
+
};
|
|
19398
|
+
this.concurrency = concurrency || Infinity;
|
|
19399
|
+
this.jobs = [];
|
|
19400
|
+
this.pending = 0;
|
|
19401
|
+
}
|
|
19402
|
+
/**
|
|
19403
|
+
* Adds a job to the queue.
|
|
19404
|
+
*
|
|
19405
|
+
* @param {Function} job The job to run
|
|
19406
|
+
* @public
|
|
19407
|
+
*/
|
|
19408
|
+
add(job) {
|
|
19409
|
+
this.jobs.push(job);
|
|
19410
|
+
this[kRun]();
|
|
19411
|
+
}
|
|
19412
|
+
/**
|
|
19413
|
+
* Removes a job from the queue and runs it if possible.
|
|
19414
|
+
*
|
|
19415
|
+
* @private
|
|
19416
|
+
*/
|
|
19417
|
+
[kRun]() {
|
|
19418
|
+
if (this.pending === this.concurrency) return;
|
|
19419
|
+
if (this.jobs.length) {
|
|
19420
|
+
const job = this.jobs.shift();
|
|
19421
|
+
this.pending++;
|
|
19422
|
+
job(this[kDone]);
|
|
19423
|
+
}
|
|
19424
|
+
}
|
|
19425
|
+
};
|
|
19426
|
+
module.exports = Limiter;
|
|
19427
|
+
}));
|
|
19428
|
+
//#endregion
|
|
19429
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/permessage-deflate.js
|
|
19430
|
+
var require_permessage_deflate = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
19431
|
+
const zlib$2 = __require("zlib");
|
|
19432
|
+
const bufferUtil = require_buffer_util();
|
|
19433
|
+
const Limiter = require_limiter();
|
|
19434
|
+
const { kStatusCode } = require_constants$1();
|
|
19435
|
+
const FastBuffer = Buffer[Symbol.species];
|
|
19436
|
+
const TRAILER = Buffer.from([
|
|
19437
|
+
0,
|
|
19438
|
+
0,
|
|
19439
|
+
255,
|
|
19440
|
+
255
|
|
19441
|
+
]);
|
|
19442
|
+
const kPerMessageDeflate = Symbol("permessage-deflate");
|
|
19443
|
+
const kTotalLength = Symbol("total-length");
|
|
19444
|
+
const kCallback = Symbol("callback");
|
|
19445
|
+
const kBuffers = Symbol("buffers");
|
|
19446
|
+
const kError = Symbol("error");
|
|
19447
|
+
let zlibLimiter;
|
|
19448
|
+
/**
|
|
19449
|
+
* permessage-deflate implementation.
|
|
19450
|
+
*/
|
|
19451
|
+
var PerMessageDeflate = class {
|
|
19452
|
+
/**
|
|
19453
|
+
* Creates a PerMessageDeflate instance.
|
|
19454
|
+
*
|
|
19455
|
+
* @param {Object} [options] Configuration options
|
|
19456
|
+
* @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
|
|
19457
|
+
* for, or request, a custom client window size
|
|
19458
|
+
* @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
|
|
19459
|
+
* acknowledge disabling of client context takeover
|
|
19460
|
+
* @param {Number} [options.concurrencyLimit=10] The number of concurrent
|
|
19461
|
+
* calls to zlib
|
|
19462
|
+
* @param {Boolean} [options.isServer=false] Create the instance in either
|
|
19463
|
+
* server or client mode
|
|
19464
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
|
19465
|
+
* @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
|
|
19466
|
+
* use of a custom server window size
|
|
19467
|
+
* @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
|
|
19468
|
+
* disabling of server context takeover
|
|
19469
|
+
* @param {Number} [options.threshold=1024] Size (in bytes) below which
|
|
19470
|
+
* messages should not be compressed if context takeover is disabled
|
|
19471
|
+
* @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
|
|
19472
|
+
* deflate
|
|
19473
|
+
* @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
|
|
19474
|
+
* inflate
|
|
19475
|
+
*/
|
|
19476
|
+
constructor(options) {
|
|
19477
|
+
this._options = options || {};
|
|
19478
|
+
this._threshold = this._options.threshold !== void 0 ? this._options.threshold : 1024;
|
|
19479
|
+
this._maxPayload = this._options.maxPayload | 0;
|
|
19480
|
+
this._isServer = !!this._options.isServer;
|
|
19481
|
+
this._deflate = null;
|
|
19482
|
+
this._inflate = null;
|
|
19483
|
+
this.params = null;
|
|
19484
|
+
if (!zlibLimiter) zlibLimiter = new Limiter(this._options.concurrencyLimit !== void 0 ? this._options.concurrencyLimit : 10);
|
|
19485
|
+
}
|
|
19486
|
+
/**
|
|
19487
|
+
* @type {String}
|
|
19488
|
+
*/
|
|
19489
|
+
static get extensionName() {
|
|
19490
|
+
return "permessage-deflate";
|
|
19491
|
+
}
|
|
19492
|
+
/**
|
|
19493
|
+
* Create an extension negotiation offer.
|
|
19494
|
+
*
|
|
19495
|
+
* @return {Object} Extension parameters
|
|
19496
|
+
* @public
|
|
19497
|
+
*/
|
|
19498
|
+
offer() {
|
|
19499
|
+
const params = {};
|
|
19500
|
+
if (this._options.serverNoContextTakeover) params.server_no_context_takeover = true;
|
|
19501
|
+
if (this._options.clientNoContextTakeover) params.client_no_context_takeover = true;
|
|
19502
|
+
if (this._options.serverMaxWindowBits) params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
19503
|
+
if (this._options.clientMaxWindowBits) params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
19504
|
+
else if (this._options.clientMaxWindowBits == null) params.client_max_window_bits = true;
|
|
19505
|
+
return params;
|
|
19506
|
+
}
|
|
19507
|
+
/**
|
|
19508
|
+
* Accept an extension negotiation offer/response.
|
|
19509
|
+
*
|
|
19510
|
+
* @param {Array} configurations The extension negotiation offers/reponse
|
|
19511
|
+
* @return {Object} Accepted configuration
|
|
19512
|
+
* @public
|
|
19513
|
+
*/
|
|
19514
|
+
accept(configurations) {
|
|
19515
|
+
configurations = this.normalizeParams(configurations);
|
|
19516
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
19517
|
+
return this.params;
|
|
19518
|
+
}
|
|
19519
|
+
/**
|
|
19520
|
+
* Releases all resources used by the extension.
|
|
19521
|
+
*
|
|
19522
|
+
* @public
|
|
19523
|
+
*/
|
|
19524
|
+
cleanup() {
|
|
19525
|
+
if (this._inflate) {
|
|
19526
|
+
this._inflate.close();
|
|
19527
|
+
this._inflate = null;
|
|
19528
|
+
}
|
|
19529
|
+
if (this._deflate) {
|
|
19530
|
+
const callback = this._deflate[kCallback];
|
|
19531
|
+
this._deflate.close();
|
|
19532
|
+
this._deflate = null;
|
|
19533
|
+
if (callback) callback(/* @__PURE__ */ new Error("The deflate stream was closed while data was being processed"));
|
|
19534
|
+
}
|
|
19535
|
+
}
|
|
19536
|
+
/**
|
|
19537
|
+
* Accept an extension negotiation offer.
|
|
19538
|
+
*
|
|
19539
|
+
* @param {Array} offers The extension negotiation offers
|
|
19540
|
+
* @return {Object} Accepted configuration
|
|
19541
|
+
* @private
|
|
19542
|
+
*/
|
|
19543
|
+
acceptAsServer(offers) {
|
|
19544
|
+
const opts = this._options;
|
|
19545
|
+
const accepted = offers.find((params) => {
|
|
19546
|
+
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) return false;
|
|
19547
|
+
return true;
|
|
19548
|
+
});
|
|
19549
|
+
if (!accepted) throw new Error("None of the extension offers can be accepted");
|
|
19550
|
+
if (opts.serverNoContextTakeover) accepted.server_no_context_takeover = true;
|
|
19551
|
+
if (opts.clientNoContextTakeover) accepted.client_no_context_takeover = true;
|
|
19552
|
+
if (typeof opts.serverMaxWindowBits === "number") accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
19553
|
+
if (typeof opts.clientMaxWindowBits === "number") accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
19554
|
+
else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) delete accepted.client_max_window_bits;
|
|
19555
|
+
return accepted;
|
|
19556
|
+
}
|
|
19557
|
+
/**
|
|
19558
|
+
* Accept the extension negotiation response.
|
|
19559
|
+
*
|
|
19560
|
+
* @param {Array} response The extension negotiation response
|
|
19561
|
+
* @return {Object} Accepted configuration
|
|
19562
|
+
* @private
|
|
19563
|
+
*/
|
|
19564
|
+
acceptAsClient(response) {
|
|
19565
|
+
const params = response[0];
|
|
19566
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) throw new Error("Unexpected parameter \"client_no_context_takeover\"");
|
|
19567
|
+
if (!params.client_max_window_bits) {
|
|
19568
|
+
if (typeof this._options.clientMaxWindowBits === "number") params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
19569
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) throw new Error("Unexpected or invalid parameter \"client_max_window_bits\"");
|
|
19570
|
+
return params;
|
|
19571
|
+
}
|
|
19572
|
+
/**
|
|
19573
|
+
* Normalize parameters.
|
|
19574
|
+
*
|
|
19575
|
+
* @param {Array} configurations The extension negotiation offers/reponse
|
|
19576
|
+
* @return {Array} The offers/response with normalized parameters
|
|
19577
|
+
* @private
|
|
19578
|
+
*/
|
|
19579
|
+
normalizeParams(configurations) {
|
|
19580
|
+
configurations.forEach((params) => {
|
|
19581
|
+
Object.keys(params).forEach((key) => {
|
|
19582
|
+
let value = params[key];
|
|
19583
|
+
if (value.length > 1) throw new Error(`Parameter "${key}" must have only a single value`);
|
|
19584
|
+
value = value[0];
|
|
19585
|
+
if (key === "client_max_window_bits") {
|
|
19586
|
+
if (value !== true) {
|
|
19587
|
+
const num = +value;
|
|
19588
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
19589
|
+
value = num;
|
|
19590
|
+
} else if (!this._isServer) throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
19591
|
+
} else if (key === "server_max_window_bits") {
|
|
19592
|
+
const num = +value;
|
|
19593
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
19594
|
+
value = num;
|
|
19595
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
19596
|
+
if (value !== true) throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
19597
|
+
} else throw new Error(`Unknown parameter "${key}"`);
|
|
19598
|
+
params[key] = value;
|
|
19599
|
+
});
|
|
19600
|
+
});
|
|
19601
|
+
return configurations;
|
|
19602
|
+
}
|
|
19603
|
+
/**
|
|
19604
|
+
* Decompress data. Concurrency limited.
|
|
19605
|
+
*
|
|
19606
|
+
* @param {Buffer} data Compressed data
|
|
19607
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
19608
|
+
* @param {Function} callback Callback
|
|
19609
|
+
* @public
|
|
19610
|
+
*/
|
|
19611
|
+
decompress(data, fin, callback) {
|
|
19612
|
+
zlibLimiter.add((done) => {
|
|
19613
|
+
this._decompress(data, fin, (err, result) => {
|
|
19614
|
+
done();
|
|
19615
|
+
callback(err, result);
|
|
19616
|
+
});
|
|
19617
|
+
});
|
|
19618
|
+
}
|
|
19619
|
+
/**
|
|
19620
|
+
* Compress data. Concurrency limited.
|
|
19621
|
+
*
|
|
19622
|
+
* @param {(Buffer|String)} data Data to compress
|
|
19623
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
19624
|
+
* @param {Function} callback Callback
|
|
19625
|
+
* @public
|
|
19626
|
+
*/
|
|
19627
|
+
compress(data, fin, callback) {
|
|
19628
|
+
zlibLimiter.add((done) => {
|
|
19629
|
+
this._compress(data, fin, (err, result) => {
|
|
19630
|
+
done();
|
|
19631
|
+
callback(err, result);
|
|
19632
|
+
});
|
|
19633
|
+
});
|
|
19634
|
+
}
|
|
19635
|
+
/**
|
|
19636
|
+
* Decompress data.
|
|
19637
|
+
*
|
|
19638
|
+
* @param {Buffer} data Compressed data
|
|
19639
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
19640
|
+
* @param {Function} callback Callback
|
|
19641
|
+
* @private
|
|
19642
|
+
*/
|
|
19643
|
+
_decompress(data, fin, callback) {
|
|
19644
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
19645
|
+
if (!this._inflate) {
|
|
19646
|
+
const key = `${endpoint}_max_window_bits`;
|
|
19647
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib$2.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
19648
|
+
this._inflate = zlib$2.createInflateRaw({
|
|
19649
|
+
...this._options.zlibInflateOptions,
|
|
19650
|
+
windowBits
|
|
19651
|
+
});
|
|
19652
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
19653
|
+
this._inflate[kTotalLength] = 0;
|
|
19654
|
+
this._inflate[kBuffers] = [];
|
|
19655
|
+
this._inflate.on("error", inflateOnError);
|
|
19656
|
+
this._inflate.on("data", inflateOnData);
|
|
19657
|
+
}
|
|
19658
|
+
this._inflate[kCallback] = callback;
|
|
19659
|
+
this._inflate.write(data);
|
|
19660
|
+
if (fin) this._inflate.write(TRAILER);
|
|
19661
|
+
this._inflate.flush(() => {
|
|
19662
|
+
const err = this._inflate[kError];
|
|
19663
|
+
if (err) {
|
|
19664
|
+
this._inflate.close();
|
|
19665
|
+
this._inflate = null;
|
|
19666
|
+
callback(err);
|
|
19667
|
+
return;
|
|
19668
|
+
}
|
|
19669
|
+
const data = bufferUtil.concat(this._inflate[kBuffers], this._inflate[kTotalLength]);
|
|
19670
|
+
if (this._inflate._readableState.endEmitted) {
|
|
19671
|
+
this._inflate.close();
|
|
19672
|
+
this._inflate = null;
|
|
19673
|
+
} else {
|
|
19674
|
+
this._inflate[kTotalLength] = 0;
|
|
19675
|
+
this._inflate[kBuffers] = [];
|
|
19676
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) this._inflate.reset();
|
|
19677
|
+
}
|
|
19678
|
+
callback(null, data);
|
|
19679
|
+
});
|
|
19680
|
+
}
|
|
19681
|
+
/**
|
|
19682
|
+
* Compress data.
|
|
19683
|
+
*
|
|
19684
|
+
* @param {(Buffer|String)} data Data to compress
|
|
19685
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
19686
|
+
* @param {Function} callback Callback
|
|
19687
|
+
* @private
|
|
19688
|
+
*/
|
|
19689
|
+
_compress(data, fin, callback) {
|
|
19690
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
19691
|
+
if (!this._deflate) {
|
|
19692
|
+
const key = `${endpoint}_max_window_bits`;
|
|
19693
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib$2.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
19694
|
+
this._deflate = zlib$2.createDeflateRaw({
|
|
19695
|
+
...this._options.zlibDeflateOptions,
|
|
19696
|
+
windowBits
|
|
19697
|
+
});
|
|
19698
|
+
this._deflate[kTotalLength] = 0;
|
|
19699
|
+
this._deflate[kBuffers] = [];
|
|
19700
|
+
this._deflate.on("data", deflateOnData);
|
|
19701
|
+
}
|
|
19702
|
+
this._deflate[kCallback] = callback;
|
|
19703
|
+
this._deflate.write(data);
|
|
19704
|
+
this._deflate.flush(zlib$2.Z_SYNC_FLUSH, () => {
|
|
19705
|
+
if (!this._deflate) return;
|
|
19706
|
+
let data = bufferUtil.concat(this._deflate[kBuffers], this._deflate[kTotalLength]);
|
|
19707
|
+
if (fin) data = new FastBuffer(data.buffer, data.byteOffset, data.length - 4);
|
|
19708
|
+
this._deflate[kCallback] = null;
|
|
19709
|
+
this._deflate[kTotalLength] = 0;
|
|
19710
|
+
this._deflate[kBuffers] = [];
|
|
19711
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) this._deflate.reset();
|
|
19712
|
+
callback(null, data);
|
|
19713
|
+
});
|
|
19714
|
+
}
|
|
19715
|
+
};
|
|
19716
|
+
module.exports = PerMessageDeflate;
|
|
19717
|
+
/**
|
|
19718
|
+
* The listener of the `zlib.DeflateRaw` stream `'data'` event.
|
|
19719
|
+
*
|
|
19720
|
+
* @param {Buffer} chunk A chunk of data
|
|
19721
|
+
* @private
|
|
19722
|
+
*/
|
|
19723
|
+
function deflateOnData(chunk) {
|
|
19724
|
+
this[kBuffers].push(chunk);
|
|
19725
|
+
this[kTotalLength] += chunk.length;
|
|
19726
|
+
}
|
|
19727
|
+
/**
|
|
19728
|
+
* The listener of the `zlib.InflateRaw` stream `'data'` event.
|
|
19729
|
+
*
|
|
19730
|
+
* @param {Buffer} chunk A chunk of data
|
|
19731
|
+
* @private
|
|
19732
|
+
*/
|
|
19733
|
+
function inflateOnData(chunk) {
|
|
19734
|
+
this[kTotalLength] += chunk.length;
|
|
19735
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
19736
|
+
this[kBuffers].push(chunk);
|
|
19737
|
+
return;
|
|
19738
|
+
}
|
|
19739
|
+
this[kError] = /* @__PURE__ */ new RangeError("Max payload size exceeded");
|
|
19740
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
19741
|
+
this[kError][kStatusCode] = 1009;
|
|
19742
|
+
this.removeListener("data", inflateOnData);
|
|
19743
|
+
this.reset();
|
|
19744
|
+
}
|
|
19745
|
+
/**
|
|
19746
|
+
* The listener of the `zlib.InflateRaw` stream `'error'` event.
|
|
19747
|
+
*
|
|
19748
|
+
* @param {Error} err The emitted error
|
|
19749
|
+
* @private
|
|
19750
|
+
*/
|
|
19751
|
+
function inflateOnError(err) {
|
|
19752
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
19753
|
+
if (this[kError]) {
|
|
19754
|
+
this[kCallback](this[kError]);
|
|
19755
|
+
return;
|
|
19756
|
+
}
|
|
19757
|
+
err[kStatusCode] = 1007;
|
|
19758
|
+
this[kCallback](err);
|
|
19759
|
+
}
|
|
19760
|
+
}));
|
|
19761
|
+
//#endregion
|
|
19762
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/validation.js
|
|
19763
|
+
var require_validation$3 = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
19764
|
+
const { isUtf8 } = __require("buffer");
|
|
19765
|
+
const { hasBlob } = require_constants$1();
|
|
19766
|
+
const tokenChars = [
|
|
19767
|
+
0,
|
|
19768
|
+
0,
|
|
19769
|
+
0,
|
|
19770
|
+
0,
|
|
19771
|
+
0,
|
|
19772
|
+
0,
|
|
19773
|
+
0,
|
|
19774
|
+
0,
|
|
19775
|
+
0,
|
|
19776
|
+
0,
|
|
19777
|
+
0,
|
|
19778
|
+
0,
|
|
19779
|
+
0,
|
|
19780
|
+
0,
|
|
19781
|
+
0,
|
|
19782
|
+
0,
|
|
19783
|
+
0,
|
|
19784
|
+
0,
|
|
19785
|
+
0,
|
|
19786
|
+
0,
|
|
19787
|
+
0,
|
|
19788
|
+
0,
|
|
19789
|
+
0,
|
|
19790
|
+
0,
|
|
19791
|
+
0,
|
|
19792
|
+
0,
|
|
19793
|
+
0,
|
|
19794
|
+
0,
|
|
19795
|
+
0,
|
|
19796
|
+
0,
|
|
19797
|
+
0,
|
|
19798
|
+
0,
|
|
19799
|
+
0,
|
|
19800
|
+
1,
|
|
19801
|
+
0,
|
|
19802
|
+
1,
|
|
19803
|
+
1,
|
|
19804
|
+
1,
|
|
19805
|
+
1,
|
|
19806
|
+
1,
|
|
19807
|
+
0,
|
|
19808
|
+
0,
|
|
19809
|
+
1,
|
|
19810
|
+
1,
|
|
19811
|
+
0,
|
|
19812
|
+
1,
|
|
19813
|
+
1,
|
|
19814
|
+
0,
|
|
19815
|
+
1,
|
|
19816
|
+
1,
|
|
19817
|
+
1,
|
|
19818
|
+
1,
|
|
19819
|
+
1,
|
|
19820
|
+
1,
|
|
19821
|
+
1,
|
|
19822
|
+
1,
|
|
19823
|
+
1,
|
|
19824
|
+
1,
|
|
19825
|
+
0,
|
|
19826
|
+
0,
|
|
19827
|
+
0,
|
|
19828
|
+
0,
|
|
19829
|
+
0,
|
|
19830
|
+
0,
|
|
19831
|
+
0,
|
|
19832
|
+
1,
|
|
19833
|
+
1,
|
|
19834
|
+
1,
|
|
19835
|
+
1,
|
|
19836
|
+
1,
|
|
19837
|
+
1,
|
|
19838
|
+
1,
|
|
19839
|
+
1,
|
|
19840
|
+
1,
|
|
19841
|
+
1,
|
|
19842
|
+
1,
|
|
19843
|
+
1,
|
|
19844
|
+
1,
|
|
19845
|
+
1,
|
|
19846
|
+
1,
|
|
19847
|
+
1,
|
|
19848
|
+
1,
|
|
19849
|
+
1,
|
|
19850
|
+
1,
|
|
19851
|
+
1,
|
|
19852
|
+
1,
|
|
19853
|
+
1,
|
|
19854
|
+
1,
|
|
19855
|
+
1,
|
|
19856
|
+
1,
|
|
19857
|
+
1,
|
|
19858
|
+
0,
|
|
19859
|
+
0,
|
|
19860
|
+
0,
|
|
19861
|
+
1,
|
|
19862
|
+
1,
|
|
19863
|
+
1,
|
|
19864
|
+
1,
|
|
19865
|
+
1,
|
|
19866
|
+
1,
|
|
19867
|
+
1,
|
|
19868
|
+
1,
|
|
19869
|
+
1,
|
|
19870
|
+
1,
|
|
19871
|
+
1,
|
|
19872
|
+
1,
|
|
19873
|
+
1,
|
|
19874
|
+
1,
|
|
19875
|
+
1,
|
|
19876
|
+
1,
|
|
19877
|
+
1,
|
|
19878
|
+
1,
|
|
19879
|
+
1,
|
|
19880
|
+
1,
|
|
19881
|
+
1,
|
|
19882
|
+
1,
|
|
19883
|
+
1,
|
|
19884
|
+
1,
|
|
19885
|
+
1,
|
|
19886
|
+
1,
|
|
19887
|
+
1,
|
|
19888
|
+
1,
|
|
19889
|
+
1,
|
|
19890
|
+
0,
|
|
19891
|
+
1,
|
|
19892
|
+
0,
|
|
19893
|
+
1,
|
|
19894
|
+
0
|
|
19895
|
+
];
|
|
19896
|
+
/**
|
|
19897
|
+
* Checks if a status code is allowed in a close frame.
|
|
19898
|
+
*
|
|
19899
|
+
* @param {Number} code The status code
|
|
19900
|
+
* @return {Boolean} `true` if the status code is valid, else `false`
|
|
19901
|
+
* @public
|
|
19902
|
+
*/
|
|
19903
|
+
function isValidStatusCode(code) {
|
|
19904
|
+
return code >= 1e3 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3e3 && code <= 4999;
|
|
19905
|
+
}
|
|
19906
|
+
/**
|
|
19907
|
+
* Checks if a given buffer contains only correct UTF-8.
|
|
19908
|
+
* Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
|
|
19909
|
+
* Markus Kuhn.
|
|
19910
|
+
*
|
|
19911
|
+
* @param {Buffer} buf The buffer to check
|
|
19912
|
+
* @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
|
|
19913
|
+
* @public
|
|
19914
|
+
*/
|
|
19915
|
+
function _isValidUTF8(buf) {
|
|
19916
|
+
const len = buf.length;
|
|
19917
|
+
let i = 0;
|
|
19918
|
+
while (i < len) if ((buf[i] & 128) === 0) i++;
|
|
19919
|
+
else if ((buf[i] & 224) === 192) {
|
|
19920
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) return false;
|
|
19921
|
+
i += 2;
|
|
19922
|
+
} else if ((buf[i] & 240) === 224) {
|
|
19923
|
+
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) return false;
|
|
19924
|
+
i += 3;
|
|
19925
|
+
} else if ((buf[i] & 248) === 240) {
|
|
19926
|
+
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) return false;
|
|
19927
|
+
i += 4;
|
|
19928
|
+
} else return false;
|
|
19929
|
+
return true;
|
|
19930
|
+
}
|
|
19931
|
+
/**
|
|
19932
|
+
* Determines whether a value is a `Blob`.
|
|
19933
|
+
*
|
|
19934
|
+
* @param {*} value The value to be tested
|
|
19935
|
+
* @return {Boolean} `true` if `value` is a `Blob`, else `false`
|
|
19936
|
+
* @private
|
|
19937
|
+
*/
|
|
19938
|
+
function isBlob(value) {
|
|
19939
|
+
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");
|
|
19940
|
+
}
|
|
19941
|
+
module.exports = {
|
|
19942
|
+
isBlob,
|
|
19943
|
+
isValidStatusCode,
|
|
19944
|
+
isValidUTF8: _isValidUTF8,
|
|
19945
|
+
tokenChars
|
|
19946
|
+
};
|
|
19947
|
+
if (isUtf8) module.exports.isValidUTF8 = function(buf) {
|
|
19948
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
19949
|
+
};
|
|
19950
|
+
else if (!process.env.WS_NO_UTF_8_VALIDATE) try {
|
|
19951
|
+
const isValidUTF8 = __require("utf-8-validate");
|
|
19952
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
19953
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
19954
|
+
};
|
|
19955
|
+
} catch (e) {}
|
|
19956
|
+
}));
|
|
19957
|
+
//#endregion
|
|
19958
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/receiver.js
|
|
19959
|
+
var require_receiver = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
19960
|
+
const { Writable: Writable$1 } = __require("stream");
|
|
19961
|
+
const PerMessageDeflate = require_permessage_deflate();
|
|
19962
|
+
const { BINARY_TYPES, EMPTY_BUFFER, kStatusCode, kWebSocket } = require_constants$1();
|
|
19963
|
+
const { concat, toArrayBuffer, unmask } = require_buffer_util();
|
|
19964
|
+
const { isValidStatusCode, isValidUTF8 } = require_validation$3();
|
|
19965
|
+
const FastBuffer = Buffer[Symbol.species];
|
|
19966
|
+
const GET_INFO = 0;
|
|
19967
|
+
const GET_PAYLOAD_LENGTH_16 = 1;
|
|
19968
|
+
const GET_PAYLOAD_LENGTH_64 = 2;
|
|
19969
|
+
const GET_MASK = 3;
|
|
19970
|
+
const GET_DATA = 4;
|
|
19971
|
+
const INFLATING = 5;
|
|
19972
|
+
const DEFER_EVENT = 6;
|
|
19973
|
+
/**
|
|
19974
|
+
* HyBi Receiver implementation.
|
|
19975
|
+
*
|
|
19976
|
+
* @extends Writable
|
|
19977
|
+
*/
|
|
19978
|
+
var Receiver = class extends Writable$1 {
|
|
19979
|
+
/**
|
|
19980
|
+
* Creates a Receiver instance.
|
|
19981
|
+
*
|
|
19982
|
+
* @param {Object} [options] Options object
|
|
19983
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
|
|
19984
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
19985
|
+
* multiple times in the same tick
|
|
19986
|
+
* @param {String} [options.binaryType=nodebuffer] The type for binary data
|
|
19987
|
+
* @param {Object} [options.extensions] An object containing the negotiated
|
|
19988
|
+
* extensions
|
|
19989
|
+
* @param {Boolean} [options.isServer=false] Specifies whether to operate in
|
|
19990
|
+
* client or server mode
|
|
19991
|
+
* @param {Number} [options.maxBufferedChunks=0] The maximum number of
|
|
19992
|
+
* buffered data chunks
|
|
19993
|
+
* @param {Number} [options.maxFragments=0] The maximum number of message
|
|
19994
|
+
* fragments
|
|
19995
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
|
19996
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
19997
|
+
* not to skip UTF-8 validation for text and close messages
|
|
19998
|
+
*/
|
|
19999
|
+
constructor(options = {}) {
|
|
20000
|
+
super();
|
|
20001
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== void 0 ? options.allowSynchronousEvents : true;
|
|
20002
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
20003
|
+
this._extensions = options.extensions || {};
|
|
20004
|
+
this._isServer = !!options.isServer;
|
|
20005
|
+
this._maxBufferedChunks = options.maxBufferedChunks | 0;
|
|
20006
|
+
this._maxFragments = options.maxFragments | 0;
|
|
20007
|
+
this._maxPayload = options.maxPayload | 0;
|
|
20008
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
20009
|
+
this[kWebSocket] = void 0;
|
|
20010
|
+
this._bufferedBytes = 0;
|
|
20011
|
+
this._buffers = [];
|
|
20012
|
+
this._compressed = false;
|
|
20013
|
+
this._payloadLength = 0;
|
|
20014
|
+
this._mask = void 0;
|
|
20015
|
+
this._fragmented = 0;
|
|
20016
|
+
this._masked = false;
|
|
20017
|
+
this._fin = false;
|
|
20018
|
+
this._opcode = 0;
|
|
20019
|
+
this._totalPayloadLength = 0;
|
|
20020
|
+
this._messageLength = 0;
|
|
20021
|
+
this._fragments = [];
|
|
20022
|
+
this._errored = false;
|
|
20023
|
+
this._loop = false;
|
|
20024
|
+
this._state = GET_INFO;
|
|
20025
|
+
}
|
|
20026
|
+
/**
|
|
20027
|
+
* Implements `Writable.prototype._write()`.
|
|
20028
|
+
*
|
|
20029
|
+
* @param {Buffer} chunk The chunk of data to write
|
|
20030
|
+
* @param {String} encoding The character encoding of `chunk`
|
|
20031
|
+
* @param {Function} cb Callback
|
|
20032
|
+
* @private
|
|
20033
|
+
*/
|
|
20034
|
+
_write(chunk, encoding, cb) {
|
|
20035
|
+
if (this._opcode === 8 && this._state == GET_INFO) return cb();
|
|
20036
|
+
if (this._maxBufferedChunks > 0 && this._buffers.length >= this._maxBufferedChunks) {
|
|
20037
|
+
cb(this.createError(RangeError, "Too many buffered chunks", false, 1008, "WS_ERR_TOO_MANY_BUFFERED_PARTS"));
|
|
20038
|
+
return;
|
|
20039
|
+
}
|
|
20040
|
+
this._bufferedBytes += chunk.length;
|
|
20041
|
+
this._buffers.push(chunk);
|
|
20042
|
+
this.startLoop(cb);
|
|
20043
|
+
}
|
|
20044
|
+
/**
|
|
20045
|
+
* Consumes `n` bytes from the buffered data.
|
|
20046
|
+
*
|
|
20047
|
+
* @param {Number} n The number of bytes to consume
|
|
20048
|
+
* @return {Buffer} The consumed bytes
|
|
20049
|
+
* @private
|
|
20050
|
+
*/
|
|
20051
|
+
consume(n) {
|
|
20052
|
+
this._bufferedBytes -= n;
|
|
20053
|
+
if (n === this._buffers[0].length) return this._buffers.shift();
|
|
20054
|
+
if (n < this._buffers[0].length) {
|
|
20055
|
+
const buf = this._buffers[0];
|
|
20056
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
20057
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
20058
|
+
}
|
|
20059
|
+
const dst = Buffer.allocUnsafe(n);
|
|
20060
|
+
do {
|
|
20061
|
+
const buf = this._buffers[0];
|
|
20062
|
+
const offset = dst.length - n;
|
|
20063
|
+
if (n >= buf.length) dst.set(this._buffers.shift(), offset);
|
|
20064
|
+
else {
|
|
20065
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
20066
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
20067
|
+
}
|
|
20068
|
+
n -= buf.length;
|
|
20069
|
+
} while (n > 0);
|
|
20070
|
+
return dst;
|
|
20071
|
+
}
|
|
20072
|
+
/**
|
|
20073
|
+
* Starts the parsing loop.
|
|
20074
|
+
*
|
|
20075
|
+
* @param {Function} cb Callback
|
|
20076
|
+
* @private
|
|
20077
|
+
*/
|
|
20078
|
+
startLoop(cb) {
|
|
20079
|
+
this._loop = true;
|
|
20080
|
+
do
|
|
20081
|
+
switch (this._state) {
|
|
20082
|
+
case GET_INFO:
|
|
20083
|
+
this.getInfo(cb);
|
|
20084
|
+
break;
|
|
20085
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
20086
|
+
this.getPayloadLength16(cb);
|
|
20087
|
+
break;
|
|
20088
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
20089
|
+
this.getPayloadLength64(cb);
|
|
20090
|
+
break;
|
|
20091
|
+
case GET_MASK:
|
|
20092
|
+
this.getMask();
|
|
20093
|
+
break;
|
|
20094
|
+
case GET_DATA:
|
|
20095
|
+
this.getData(cb);
|
|
20096
|
+
break;
|
|
20097
|
+
case INFLATING:
|
|
20098
|
+
case DEFER_EVENT:
|
|
20099
|
+
this._loop = false;
|
|
20100
|
+
return;
|
|
20101
|
+
}
|
|
20102
|
+
while (this._loop);
|
|
20103
|
+
if (!this._errored) cb();
|
|
20104
|
+
}
|
|
20105
|
+
/**
|
|
20106
|
+
* Reads the first two bytes of a frame.
|
|
20107
|
+
*
|
|
20108
|
+
* @param {Function} cb Callback
|
|
20109
|
+
* @private
|
|
20110
|
+
*/
|
|
20111
|
+
getInfo(cb) {
|
|
20112
|
+
if (this._bufferedBytes < 2) {
|
|
20113
|
+
this._loop = false;
|
|
20114
|
+
return;
|
|
20115
|
+
}
|
|
20116
|
+
const buf = this.consume(2);
|
|
20117
|
+
if ((buf[0] & 48) !== 0) {
|
|
20118
|
+
cb(this.createError(RangeError, "RSV2 and RSV3 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_2_3"));
|
|
20119
|
+
return;
|
|
20120
|
+
}
|
|
20121
|
+
const compressed = (buf[0] & 64) === 64;
|
|
20122
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
20123
|
+
cb(this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1"));
|
|
20124
|
+
return;
|
|
20125
|
+
}
|
|
20126
|
+
this._fin = (buf[0] & 128) === 128;
|
|
20127
|
+
this._opcode = buf[0] & 15;
|
|
20128
|
+
this._payloadLength = buf[1] & 127;
|
|
20129
|
+
if (this._opcode === 0) {
|
|
20130
|
+
if (compressed) {
|
|
20131
|
+
cb(this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1"));
|
|
20132
|
+
return;
|
|
20133
|
+
}
|
|
20134
|
+
if (!this._fragmented) {
|
|
20135
|
+
cb(this.createError(RangeError, "invalid opcode 0", true, 1002, "WS_ERR_INVALID_OPCODE"));
|
|
20136
|
+
return;
|
|
20137
|
+
}
|
|
20138
|
+
this._opcode = this._fragmented;
|
|
20139
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
20140
|
+
if (this._fragmented) {
|
|
20141
|
+
cb(this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE"));
|
|
20142
|
+
return;
|
|
20143
|
+
}
|
|
20144
|
+
this._compressed = compressed;
|
|
20145
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
20146
|
+
if (!this._fin) {
|
|
20147
|
+
cb(this.createError(RangeError, "FIN must be set", true, 1002, "WS_ERR_EXPECTED_FIN"));
|
|
20148
|
+
return;
|
|
20149
|
+
}
|
|
20150
|
+
if (compressed) {
|
|
20151
|
+
cb(this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1"));
|
|
20152
|
+
return;
|
|
20153
|
+
}
|
|
20154
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
20155
|
+
cb(this.createError(RangeError, `invalid payload length ${this._payloadLength}`, true, 1002, "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH"));
|
|
20156
|
+
return;
|
|
20157
|
+
}
|
|
20158
|
+
} else {
|
|
20159
|
+
cb(this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE"));
|
|
20160
|
+
return;
|
|
20161
|
+
}
|
|
20162
|
+
if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
|
|
20163
|
+
this._masked = (buf[1] & 128) === 128;
|
|
20164
|
+
if (this._isServer) {
|
|
20165
|
+
if (!this._masked) {
|
|
20166
|
+
cb(this.createError(RangeError, "MASK must be set", true, 1002, "WS_ERR_EXPECTED_MASK"));
|
|
20167
|
+
return;
|
|
20168
|
+
}
|
|
20169
|
+
} else if (this._masked) {
|
|
20170
|
+
cb(this.createError(RangeError, "MASK must be clear", true, 1002, "WS_ERR_UNEXPECTED_MASK"));
|
|
20171
|
+
return;
|
|
20172
|
+
}
|
|
20173
|
+
if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
|
|
20174
|
+
else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
|
|
20175
|
+
else this.haveLength(cb);
|
|
20176
|
+
}
|
|
20177
|
+
/**
|
|
20178
|
+
* Gets extended payload length (7+16).
|
|
20179
|
+
*
|
|
20180
|
+
* @param {Function} cb Callback
|
|
20181
|
+
* @private
|
|
20182
|
+
*/
|
|
20183
|
+
getPayloadLength16(cb) {
|
|
20184
|
+
if (this._bufferedBytes < 2) {
|
|
20185
|
+
this._loop = false;
|
|
20186
|
+
return;
|
|
20187
|
+
}
|
|
20188
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
20189
|
+
this.haveLength(cb);
|
|
20190
|
+
}
|
|
20191
|
+
/**
|
|
20192
|
+
* Gets extended payload length (7+64).
|
|
20193
|
+
*
|
|
20194
|
+
* @param {Function} cb Callback
|
|
20195
|
+
* @private
|
|
20196
|
+
*/
|
|
20197
|
+
getPayloadLength64(cb) {
|
|
20198
|
+
if (this._bufferedBytes < 8) {
|
|
20199
|
+
this._loop = false;
|
|
20200
|
+
return;
|
|
20201
|
+
}
|
|
20202
|
+
const buf = this.consume(8);
|
|
20203
|
+
const num = buf.readUInt32BE(0);
|
|
20204
|
+
if (num > Math.pow(2, 21) - 1) {
|
|
20205
|
+
cb(this.createError(RangeError, "Unsupported WebSocket frame: payload length > 2^53 - 1", false, 1009, "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH"));
|
|
20206
|
+
return;
|
|
20207
|
+
}
|
|
20208
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
20209
|
+
this.haveLength(cb);
|
|
20210
|
+
}
|
|
20211
|
+
/**
|
|
20212
|
+
* Payload length has been read.
|
|
20213
|
+
*
|
|
20214
|
+
* @param {Function} cb Callback
|
|
20215
|
+
* @private
|
|
20216
|
+
*/
|
|
20217
|
+
haveLength(cb) {
|
|
20218
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
20219
|
+
this._totalPayloadLength += this._payloadLength;
|
|
20220
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
20221
|
+
cb(this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"));
|
|
20222
|
+
return;
|
|
20223
|
+
}
|
|
20224
|
+
}
|
|
20225
|
+
if (this._masked) this._state = GET_MASK;
|
|
20226
|
+
else this._state = GET_DATA;
|
|
20227
|
+
}
|
|
20228
|
+
/**
|
|
20229
|
+
* Reads mask bytes.
|
|
20230
|
+
*
|
|
20231
|
+
* @private
|
|
20232
|
+
*/
|
|
20233
|
+
getMask() {
|
|
20234
|
+
if (this._bufferedBytes < 4) {
|
|
20235
|
+
this._loop = false;
|
|
20236
|
+
return;
|
|
20237
|
+
}
|
|
20238
|
+
this._mask = this.consume(4);
|
|
20239
|
+
this._state = GET_DATA;
|
|
20240
|
+
}
|
|
20241
|
+
/**
|
|
20242
|
+
* Reads data bytes.
|
|
20243
|
+
*
|
|
20244
|
+
* @param {Function} cb Callback
|
|
20245
|
+
* @private
|
|
20246
|
+
*/
|
|
20247
|
+
getData(cb) {
|
|
20248
|
+
let data = EMPTY_BUFFER;
|
|
20249
|
+
if (this._payloadLength) {
|
|
20250
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
20251
|
+
this._loop = false;
|
|
20252
|
+
return;
|
|
20253
|
+
}
|
|
20254
|
+
data = this.consume(this._payloadLength);
|
|
20255
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) unmask(data, this._mask);
|
|
20256
|
+
}
|
|
20257
|
+
if (this._opcode > 7) {
|
|
20258
|
+
this.controlMessage(data, cb);
|
|
20259
|
+
return;
|
|
20260
|
+
}
|
|
20261
|
+
if (this._compressed) {
|
|
20262
|
+
this._state = INFLATING;
|
|
20263
|
+
this.decompress(data, cb);
|
|
20264
|
+
return;
|
|
20265
|
+
}
|
|
20266
|
+
if (data.length) {
|
|
20267
|
+
if (this._maxFragments > 0 && this._fragments.length >= this._maxFragments) {
|
|
20268
|
+
cb(this.createError(RangeError, "Too many message fragments", false, 1008, "WS_ERR_TOO_MANY_BUFFERED_PARTS"));
|
|
20269
|
+
return;
|
|
20270
|
+
}
|
|
20271
|
+
this._messageLength = this._totalPayloadLength;
|
|
20272
|
+
this._fragments.push(data);
|
|
20273
|
+
}
|
|
20274
|
+
this.dataMessage(cb);
|
|
20275
|
+
}
|
|
20276
|
+
/**
|
|
20277
|
+
* Decompresses data.
|
|
20278
|
+
*
|
|
20279
|
+
* @param {Buffer} data Compressed data
|
|
20280
|
+
* @param {Function} cb Callback
|
|
20281
|
+
* @private
|
|
20282
|
+
*/
|
|
20283
|
+
decompress(data, cb) {
|
|
20284
|
+
this._extensions[PerMessageDeflate.extensionName].decompress(data, this._fin, (err, buf) => {
|
|
20285
|
+
if (err) return cb(err);
|
|
20286
|
+
if (buf.length) {
|
|
20287
|
+
this._messageLength += buf.length;
|
|
20288
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
20289
|
+
cb(this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"));
|
|
20290
|
+
return;
|
|
20291
|
+
}
|
|
20292
|
+
if (this._maxFragments > 0 && this._fragments.length >= this._maxFragments) {
|
|
20293
|
+
cb(this.createError(RangeError, "Too many message fragments", false, 1008, "WS_ERR_TOO_MANY_BUFFERED_PARTS"));
|
|
20294
|
+
return;
|
|
20295
|
+
}
|
|
20296
|
+
this._fragments.push(buf);
|
|
20297
|
+
}
|
|
20298
|
+
this.dataMessage(cb);
|
|
20299
|
+
if (this._state === GET_INFO) this.startLoop(cb);
|
|
20300
|
+
});
|
|
20301
|
+
}
|
|
20302
|
+
/**
|
|
20303
|
+
* Handles a data message.
|
|
20304
|
+
*
|
|
20305
|
+
* @param {Function} cb Callback
|
|
20306
|
+
* @private
|
|
20307
|
+
*/
|
|
20308
|
+
dataMessage(cb) {
|
|
20309
|
+
if (!this._fin) {
|
|
20310
|
+
this._state = GET_INFO;
|
|
20311
|
+
return;
|
|
20312
|
+
}
|
|
20313
|
+
const messageLength = this._messageLength;
|
|
20314
|
+
const fragments = this._fragments;
|
|
20315
|
+
this._totalPayloadLength = 0;
|
|
20316
|
+
this._messageLength = 0;
|
|
20317
|
+
this._fragmented = 0;
|
|
20318
|
+
this._fragments = [];
|
|
20319
|
+
if (this._opcode === 2) {
|
|
20320
|
+
let data;
|
|
20321
|
+
if (this._binaryType === "nodebuffer") data = concat(fragments, messageLength);
|
|
20322
|
+
else if (this._binaryType === "arraybuffer") data = toArrayBuffer(concat(fragments, messageLength));
|
|
20323
|
+
else if (this._binaryType === "blob") data = new Blob(fragments);
|
|
20324
|
+
else data = fragments;
|
|
20325
|
+
if (this._allowSynchronousEvents) {
|
|
20326
|
+
this.emit("message", data, true);
|
|
20327
|
+
this._state = GET_INFO;
|
|
20328
|
+
} else {
|
|
20329
|
+
this._state = DEFER_EVENT;
|
|
20330
|
+
setImmediate(() => {
|
|
20331
|
+
this.emit("message", data, true);
|
|
20332
|
+
this._state = GET_INFO;
|
|
20333
|
+
this.startLoop(cb);
|
|
20334
|
+
});
|
|
20335
|
+
}
|
|
20336
|
+
} else {
|
|
20337
|
+
const buf = concat(fragments, messageLength);
|
|
20338
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
20339
|
+
cb(this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8"));
|
|
20340
|
+
return;
|
|
20341
|
+
}
|
|
20342
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
20343
|
+
this.emit("message", buf, false);
|
|
20344
|
+
this._state = GET_INFO;
|
|
20345
|
+
} else {
|
|
20346
|
+
this._state = DEFER_EVENT;
|
|
20347
|
+
setImmediate(() => {
|
|
20348
|
+
this.emit("message", buf, false);
|
|
20349
|
+
this._state = GET_INFO;
|
|
20350
|
+
this.startLoop(cb);
|
|
20351
|
+
});
|
|
20352
|
+
}
|
|
20353
|
+
}
|
|
20354
|
+
}
|
|
20355
|
+
/**
|
|
20356
|
+
* Handles a control message.
|
|
20357
|
+
*
|
|
20358
|
+
* @param {Buffer} data Data to handle
|
|
20359
|
+
* @return {(Error|RangeError|undefined)} A possible error
|
|
20360
|
+
* @private
|
|
20361
|
+
*/
|
|
20362
|
+
controlMessage(data, cb) {
|
|
20363
|
+
if (this._opcode === 8) {
|
|
20364
|
+
if (data.length === 0) {
|
|
20365
|
+
this._loop = false;
|
|
20366
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
20367
|
+
this.end();
|
|
20368
|
+
} else {
|
|
20369
|
+
const code = data.readUInt16BE(0);
|
|
20370
|
+
if (!isValidStatusCode(code)) {
|
|
20371
|
+
cb(this.createError(RangeError, `invalid status code ${code}`, true, 1002, "WS_ERR_INVALID_CLOSE_CODE"));
|
|
20372
|
+
return;
|
|
20373
|
+
}
|
|
20374
|
+
const buf = new FastBuffer(data.buffer, data.byteOffset + 2, data.length - 2);
|
|
20375
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
20376
|
+
cb(this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8"));
|
|
20377
|
+
return;
|
|
20378
|
+
}
|
|
20379
|
+
this._loop = false;
|
|
20380
|
+
this.emit("conclude", code, buf);
|
|
20381
|
+
this.end();
|
|
20382
|
+
}
|
|
20383
|
+
this._state = GET_INFO;
|
|
20384
|
+
return;
|
|
20385
|
+
}
|
|
20386
|
+
if (this._allowSynchronousEvents) {
|
|
20387
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
20388
|
+
this._state = GET_INFO;
|
|
20389
|
+
} else {
|
|
20390
|
+
this._state = DEFER_EVENT;
|
|
20391
|
+
setImmediate(() => {
|
|
20392
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
20393
|
+
this._state = GET_INFO;
|
|
20394
|
+
this.startLoop(cb);
|
|
20395
|
+
});
|
|
20396
|
+
}
|
|
20397
|
+
}
|
|
20398
|
+
/**
|
|
20399
|
+
* Builds an error object.
|
|
20400
|
+
*
|
|
20401
|
+
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
|
|
20402
|
+
* @param {String} message The error message
|
|
20403
|
+
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
|
|
20404
|
+
* `message`
|
|
20405
|
+
* @param {Number} statusCode The status code
|
|
20406
|
+
* @param {String} errorCode The exposed error code
|
|
20407
|
+
* @return {(Error|RangeError)} The error
|
|
20408
|
+
* @private
|
|
20409
|
+
*/
|
|
20410
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
20411
|
+
this._loop = false;
|
|
20412
|
+
this._errored = true;
|
|
20413
|
+
const err = new ErrorCtor(prefix ? `Invalid WebSocket frame: ${message}` : message);
|
|
20414
|
+
Error.captureStackTrace(err, this.createError);
|
|
20415
|
+
err.code = errorCode;
|
|
20416
|
+
err[kStatusCode] = statusCode;
|
|
20417
|
+
return err;
|
|
20418
|
+
}
|
|
20419
|
+
};
|
|
20420
|
+
module.exports = Receiver;
|
|
20421
|
+
}));
|
|
20422
|
+
//#endregion
|
|
20423
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/sender.js
|
|
20424
|
+
var require_sender = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
20425
|
+
const { Duplex: Duplex$3 } = __require("stream");
|
|
20426
|
+
const { randomFillSync } = __require("crypto");
|
|
20427
|
+
const { types: { isUint8Array } } = __require("util");
|
|
20428
|
+
const PerMessageDeflate = require_permessage_deflate();
|
|
20429
|
+
const { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants$1();
|
|
20430
|
+
const { isBlob, isValidStatusCode } = require_validation$3();
|
|
20431
|
+
const { mask: applyMask, toBuffer } = require_buffer_util();
|
|
20432
|
+
const kByteLength = Symbol("kByteLength");
|
|
20433
|
+
const maskBuffer = Buffer.alloc(4);
|
|
20434
|
+
const RANDOM_POOL_SIZE = 8 * 1024;
|
|
20435
|
+
let randomPool;
|
|
20436
|
+
let randomPoolPointer = RANDOM_POOL_SIZE;
|
|
20437
|
+
const DEFAULT = 0;
|
|
20438
|
+
const DEFLATING = 1;
|
|
20439
|
+
const GET_BLOB_DATA = 2;
|
|
20440
|
+
module.exports = class Sender {
|
|
20441
|
+
/**
|
|
20442
|
+
* Creates a Sender instance.
|
|
20443
|
+
*
|
|
20444
|
+
* @param {Duplex} socket The connection socket
|
|
20445
|
+
* @param {Object} [extensions] An object containing the negotiated extensions
|
|
20446
|
+
* @param {Function} [generateMask] The function used to generate the masking
|
|
20447
|
+
* key
|
|
20448
|
+
*/
|
|
20449
|
+
constructor(socket, extensions, generateMask) {
|
|
20450
|
+
this._extensions = extensions || {};
|
|
20451
|
+
if (generateMask) {
|
|
20452
|
+
this._generateMask = generateMask;
|
|
20453
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
20454
|
+
}
|
|
20455
|
+
this._socket = socket;
|
|
20456
|
+
this._firstFragment = true;
|
|
20457
|
+
this._compress = false;
|
|
20458
|
+
this._bufferedBytes = 0;
|
|
20459
|
+
this._queue = [];
|
|
20460
|
+
this._state = DEFAULT;
|
|
20461
|
+
this.onerror = NOOP;
|
|
20462
|
+
this[kWebSocket] = void 0;
|
|
20463
|
+
}
|
|
20464
|
+
/**
|
|
20465
|
+
* Frames a piece of data according to the HyBi WebSocket protocol.
|
|
20466
|
+
*
|
|
20467
|
+
* @param {(Buffer|String)} data The data to frame
|
|
20468
|
+
* @param {Object} options Options object
|
|
20469
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
20470
|
+
* FIN bit
|
|
20471
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
20472
|
+
* masking key
|
|
20473
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
20474
|
+
* `data`
|
|
20475
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
20476
|
+
* key
|
|
20477
|
+
* @param {Number} options.opcode The opcode
|
|
20478
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
20479
|
+
* modified
|
|
20480
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
20481
|
+
* RSV1 bit
|
|
20482
|
+
* @return {(Buffer|String)[]} The framed data
|
|
20483
|
+
* @public
|
|
20484
|
+
*/
|
|
20485
|
+
static frame(data, options) {
|
|
20486
|
+
let mask;
|
|
20487
|
+
let merge = false;
|
|
20488
|
+
let offset = 2;
|
|
20489
|
+
let skipMasking = false;
|
|
20490
|
+
if (options.mask) {
|
|
20491
|
+
mask = options.maskBuffer || maskBuffer;
|
|
20492
|
+
if (options.generateMask) options.generateMask(mask);
|
|
20493
|
+
else {
|
|
20494
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
20495
|
+
/* istanbul ignore else */
|
|
20496
|
+
if (randomPool === void 0) randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
20497
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
20498
|
+
randomPoolPointer = 0;
|
|
20499
|
+
}
|
|
20500
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
20501
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
20502
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
20503
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
20504
|
+
}
|
|
20505
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
20506
|
+
offset = 6;
|
|
20507
|
+
}
|
|
20508
|
+
let dataLength;
|
|
20509
|
+
if (typeof data === "string") if ((!options.mask || skipMasking) && options[kByteLength] !== void 0) dataLength = options[kByteLength];
|
|
20510
|
+
else {
|
|
20511
|
+
data = Buffer.from(data);
|
|
20512
|
+
dataLength = data.length;
|
|
20513
|
+
}
|
|
20514
|
+
else {
|
|
20515
|
+
dataLength = data.length;
|
|
20516
|
+
merge = options.mask && options.readOnly && !skipMasking;
|
|
20517
|
+
}
|
|
20518
|
+
let payloadLength = dataLength;
|
|
20519
|
+
if (dataLength >= 65536) {
|
|
20520
|
+
offset += 8;
|
|
20521
|
+
payloadLength = 127;
|
|
20522
|
+
} else if (dataLength > 125) {
|
|
20523
|
+
offset += 2;
|
|
20524
|
+
payloadLength = 126;
|
|
20525
|
+
}
|
|
20526
|
+
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
|
20527
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
20528
|
+
if (options.rsv1) target[0] |= 64;
|
|
20529
|
+
target[1] = payloadLength;
|
|
20530
|
+
if (payloadLength === 126) target.writeUInt16BE(dataLength, 2);
|
|
20531
|
+
else if (payloadLength === 127) {
|
|
20532
|
+
target[2] = target[3] = 0;
|
|
20533
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
20534
|
+
}
|
|
20535
|
+
if (!options.mask) return [target, data];
|
|
20536
|
+
target[1] |= 128;
|
|
20537
|
+
target[offset - 4] = mask[0];
|
|
20538
|
+
target[offset - 3] = mask[1];
|
|
20539
|
+
target[offset - 2] = mask[2];
|
|
20540
|
+
target[offset - 1] = mask[3];
|
|
20541
|
+
if (skipMasking) return [target, data];
|
|
20542
|
+
if (merge) {
|
|
20543
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
20544
|
+
return [target];
|
|
20545
|
+
}
|
|
20546
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
20547
|
+
return [target, data];
|
|
20548
|
+
}
|
|
20549
|
+
/**
|
|
20550
|
+
* Sends a close message to the other peer.
|
|
20551
|
+
*
|
|
20552
|
+
* @param {Number} [code] The status code component of the body
|
|
20553
|
+
* @param {(String|Buffer)} [data] The message component of the body
|
|
20554
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
|
|
20555
|
+
* @param {Function} [cb] Callback
|
|
20556
|
+
* @public
|
|
20557
|
+
*/
|
|
20558
|
+
close(code, data, mask, cb) {
|
|
20559
|
+
let buf;
|
|
20560
|
+
if (code === void 0) buf = EMPTY_BUFFER;
|
|
20561
|
+
else if (typeof code !== "number" || !isValidStatusCode(code)) throw new TypeError("First argument must be a valid error code number");
|
|
20562
|
+
else if (data === void 0 || !data.length) {
|
|
20563
|
+
buf = Buffer.allocUnsafe(2);
|
|
20564
|
+
buf.writeUInt16BE(code, 0);
|
|
20565
|
+
} else {
|
|
20566
|
+
const length = Buffer.byteLength(data);
|
|
20567
|
+
if (length > 123) throw new RangeError("The message must not be greater than 123 bytes");
|
|
20568
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
20569
|
+
buf.writeUInt16BE(code, 0);
|
|
20570
|
+
if (typeof data === "string") buf.write(data, 2);
|
|
20571
|
+
else if (isUint8Array(data)) buf.set(data, 2);
|
|
20572
|
+
else throw new TypeError("Second argument must be a string or a Uint8Array");
|
|
20573
|
+
}
|
|
20574
|
+
const options = {
|
|
20575
|
+
[kByteLength]: buf.length,
|
|
20576
|
+
fin: true,
|
|
20577
|
+
generateMask: this._generateMask,
|
|
20578
|
+
mask,
|
|
20579
|
+
maskBuffer: this._maskBuffer,
|
|
20580
|
+
opcode: 8,
|
|
20581
|
+
readOnly: false,
|
|
20582
|
+
rsv1: false
|
|
20583
|
+
};
|
|
20584
|
+
if (this._state !== DEFAULT) this.enqueue([
|
|
20585
|
+
this.dispatch,
|
|
20586
|
+
buf,
|
|
20587
|
+
false,
|
|
20588
|
+
options,
|
|
20589
|
+
cb
|
|
20590
|
+
]);
|
|
20591
|
+
else this.sendFrame(Sender.frame(buf, options), cb);
|
|
20592
|
+
}
|
|
20593
|
+
/**
|
|
20594
|
+
* Sends a ping message to the other peer.
|
|
20595
|
+
*
|
|
20596
|
+
* @param {*} data The message to send
|
|
20597
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
|
20598
|
+
* @param {Function} [cb] Callback
|
|
20599
|
+
* @public
|
|
20600
|
+
*/
|
|
20601
|
+
ping(data, mask, cb) {
|
|
20602
|
+
let byteLength;
|
|
20603
|
+
let readOnly;
|
|
20604
|
+
if (typeof data === "string") {
|
|
20605
|
+
byteLength = Buffer.byteLength(data);
|
|
20606
|
+
readOnly = false;
|
|
20607
|
+
} else if (isBlob(data)) {
|
|
20608
|
+
byteLength = data.size;
|
|
20609
|
+
readOnly = false;
|
|
20610
|
+
} else {
|
|
20611
|
+
data = toBuffer(data);
|
|
20612
|
+
byteLength = data.length;
|
|
20613
|
+
readOnly = toBuffer.readOnly;
|
|
20614
|
+
}
|
|
20615
|
+
if (byteLength > 125) throw new RangeError("The data size must not be greater than 125 bytes");
|
|
20616
|
+
const options = {
|
|
20617
|
+
[kByteLength]: byteLength,
|
|
20618
|
+
fin: true,
|
|
20619
|
+
generateMask: this._generateMask,
|
|
20620
|
+
mask,
|
|
20621
|
+
maskBuffer: this._maskBuffer,
|
|
20622
|
+
opcode: 9,
|
|
20623
|
+
readOnly,
|
|
20624
|
+
rsv1: false
|
|
20625
|
+
};
|
|
20626
|
+
if (isBlob(data)) if (this._state !== DEFAULT) this.enqueue([
|
|
20627
|
+
this.getBlobData,
|
|
20628
|
+
data,
|
|
20629
|
+
false,
|
|
20630
|
+
options,
|
|
20631
|
+
cb
|
|
20632
|
+
]);
|
|
20633
|
+
else this.getBlobData(data, false, options, cb);
|
|
20634
|
+
else if (this._state !== DEFAULT) this.enqueue([
|
|
20635
|
+
this.dispatch,
|
|
20636
|
+
data,
|
|
20637
|
+
false,
|
|
20638
|
+
options,
|
|
20639
|
+
cb
|
|
20640
|
+
]);
|
|
20641
|
+
else this.sendFrame(Sender.frame(data, options), cb);
|
|
20642
|
+
}
|
|
20643
|
+
/**
|
|
20644
|
+
* Sends a pong message to the other peer.
|
|
20645
|
+
*
|
|
20646
|
+
* @param {*} data The message to send
|
|
20647
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
|
20648
|
+
* @param {Function} [cb] Callback
|
|
20649
|
+
* @public
|
|
20650
|
+
*/
|
|
20651
|
+
pong(data, mask, cb) {
|
|
20652
|
+
let byteLength;
|
|
20653
|
+
let readOnly;
|
|
20654
|
+
if (typeof data === "string") {
|
|
20655
|
+
byteLength = Buffer.byteLength(data);
|
|
20656
|
+
readOnly = false;
|
|
20657
|
+
} else if (isBlob(data)) {
|
|
20658
|
+
byteLength = data.size;
|
|
20659
|
+
readOnly = false;
|
|
20660
|
+
} else {
|
|
20661
|
+
data = toBuffer(data);
|
|
20662
|
+
byteLength = data.length;
|
|
20663
|
+
readOnly = toBuffer.readOnly;
|
|
20664
|
+
}
|
|
20665
|
+
if (byteLength > 125) throw new RangeError("The data size must not be greater than 125 bytes");
|
|
20666
|
+
const options = {
|
|
20667
|
+
[kByteLength]: byteLength,
|
|
20668
|
+
fin: true,
|
|
20669
|
+
generateMask: this._generateMask,
|
|
20670
|
+
mask,
|
|
20671
|
+
maskBuffer: this._maskBuffer,
|
|
20672
|
+
opcode: 10,
|
|
20673
|
+
readOnly,
|
|
20674
|
+
rsv1: false
|
|
20675
|
+
};
|
|
20676
|
+
if (isBlob(data)) if (this._state !== DEFAULT) this.enqueue([
|
|
20677
|
+
this.getBlobData,
|
|
20678
|
+
data,
|
|
20679
|
+
false,
|
|
20680
|
+
options,
|
|
20681
|
+
cb
|
|
20682
|
+
]);
|
|
20683
|
+
else this.getBlobData(data, false, options, cb);
|
|
20684
|
+
else if (this._state !== DEFAULT) this.enqueue([
|
|
20685
|
+
this.dispatch,
|
|
20686
|
+
data,
|
|
20687
|
+
false,
|
|
20688
|
+
options,
|
|
20689
|
+
cb
|
|
20690
|
+
]);
|
|
20691
|
+
else this.sendFrame(Sender.frame(data, options), cb);
|
|
20692
|
+
}
|
|
20693
|
+
/**
|
|
20694
|
+
* Sends a data message to the other peer.
|
|
20695
|
+
*
|
|
20696
|
+
* @param {*} data The message to send
|
|
20697
|
+
* @param {Object} options Options object
|
|
20698
|
+
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
|
|
20699
|
+
* or text
|
|
20700
|
+
* @param {Boolean} [options.compress=false] Specifies whether or not to
|
|
20701
|
+
* compress `data`
|
|
20702
|
+
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
|
|
20703
|
+
* last one
|
|
20704
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
20705
|
+
* `data`
|
|
20706
|
+
* @param {Function} [cb] Callback
|
|
20707
|
+
* @public
|
|
20708
|
+
*/
|
|
20709
|
+
send(data, options, cb) {
|
|
20710
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
20711
|
+
let opcode = options.binary ? 2 : 1;
|
|
20712
|
+
let rsv1 = options.compress;
|
|
20713
|
+
let byteLength;
|
|
20714
|
+
let readOnly;
|
|
20715
|
+
if (typeof data === "string") {
|
|
20716
|
+
byteLength = Buffer.byteLength(data);
|
|
20717
|
+
readOnly = false;
|
|
20718
|
+
} else if (isBlob(data)) {
|
|
20719
|
+
byteLength = data.size;
|
|
20720
|
+
readOnly = false;
|
|
20721
|
+
} else {
|
|
20722
|
+
data = toBuffer(data);
|
|
20723
|
+
byteLength = data.length;
|
|
20724
|
+
readOnly = toBuffer.readOnly;
|
|
20725
|
+
}
|
|
20726
|
+
if (this._firstFragment) {
|
|
20727
|
+
this._firstFragment = false;
|
|
20728
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
20729
|
+
this._compress = rsv1;
|
|
20730
|
+
} else {
|
|
20731
|
+
rsv1 = false;
|
|
20732
|
+
opcode = 0;
|
|
20733
|
+
}
|
|
20734
|
+
if (options.fin) this._firstFragment = true;
|
|
20735
|
+
const opts = {
|
|
20736
|
+
[kByteLength]: byteLength,
|
|
20737
|
+
fin: options.fin,
|
|
20738
|
+
generateMask: this._generateMask,
|
|
20739
|
+
mask: options.mask,
|
|
20740
|
+
maskBuffer: this._maskBuffer,
|
|
20741
|
+
opcode,
|
|
20742
|
+
readOnly,
|
|
20743
|
+
rsv1
|
|
20744
|
+
};
|
|
20745
|
+
if (isBlob(data)) if (this._state !== DEFAULT) this.enqueue([
|
|
20746
|
+
this.getBlobData,
|
|
20747
|
+
data,
|
|
20748
|
+
this._compress,
|
|
20749
|
+
opts,
|
|
20750
|
+
cb
|
|
20751
|
+
]);
|
|
20752
|
+
else this.getBlobData(data, this._compress, opts, cb);
|
|
20753
|
+
else if (this._state !== DEFAULT) this.enqueue([
|
|
20754
|
+
this.dispatch,
|
|
20755
|
+
data,
|
|
20756
|
+
this._compress,
|
|
20757
|
+
opts,
|
|
20758
|
+
cb
|
|
20759
|
+
]);
|
|
20760
|
+
else this.dispatch(data, this._compress, opts, cb);
|
|
20761
|
+
}
|
|
20762
|
+
/**
|
|
20763
|
+
* Gets the contents of a blob as binary data.
|
|
20764
|
+
*
|
|
20765
|
+
* @param {Blob} blob The blob
|
|
20766
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
|
20767
|
+
* the data
|
|
20768
|
+
* @param {Object} options Options object
|
|
20769
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
20770
|
+
* FIN bit
|
|
20771
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
20772
|
+
* masking key
|
|
20773
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
20774
|
+
* `data`
|
|
20775
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
20776
|
+
* key
|
|
20777
|
+
* @param {Number} options.opcode The opcode
|
|
20778
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
20779
|
+
* modified
|
|
20780
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
20781
|
+
* RSV1 bit
|
|
20782
|
+
* @param {Function} [cb] Callback
|
|
20783
|
+
* @private
|
|
20784
|
+
*/
|
|
20785
|
+
getBlobData(blob, compress, options, cb) {
|
|
20786
|
+
this._bufferedBytes += options[kByteLength];
|
|
20787
|
+
this._state = GET_BLOB_DATA;
|
|
20788
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
20789
|
+
if (this._socket.destroyed) {
|
|
20790
|
+
const err = /* @__PURE__ */ new Error("The socket was closed while the blob was being read");
|
|
20791
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
20792
|
+
return;
|
|
20793
|
+
}
|
|
20794
|
+
this._bufferedBytes -= options[kByteLength];
|
|
20795
|
+
const data = toBuffer(arrayBuffer);
|
|
20796
|
+
if (!compress) {
|
|
20797
|
+
this._state = DEFAULT;
|
|
20798
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
20799
|
+
this.dequeue();
|
|
20800
|
+
} else this.dispatch(data, compress, options, cb);
|
|
20801
|
+
}).catch((err) => {
|
|
20802
|
+
process.nextTick(onError, this, err, cb);
|
|
20803
|
+
});
|
|
20804
|
+
}
|
|
20805
|
+
/**
|
|
20806
|
+
* Dispatches a message.
|
|
20807
|
+
*
|
|
20808
|
+
* @param {(Buffer|String)} data The message to send
|
|
20809
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
|
20810
|
+
* `data`
|
|
20811
|
+
* @param {Object} options Options object
|
|
20812
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
20813
|
+
* FIN bit
|
|
20814
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
20815
|
+
* masking key
|
|
20816
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
20817
|
+
* `data`
|
|
20818
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
20819
|
+
* key
|
|
20820
|
+
* @param {Number} options.opcode The opcode
|
|
20821
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
20822
|
+
* modified
|
|
20823
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
20824
|
+
* RSV1 bit
|
|
20825
|
+
* @param {Function} [cb] Callback
|
|
20826
|
+
* @private
|
|
20827
|
+
*/
|
|
20828
|
+
dispatch(data, compress, options, cb) {
|
|
20829
|
+
if (!compress) {
|
|
20830
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
20831
|
+
return;
|
|
20832
|
+
}
|
|
20833
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
20834
|
+
this._bufferedBytes += options[kByteLength];
|
|
20835
|
+
this._state = DEFLATING;
|
|
20836
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
|
20837
|
+
if (this._socket.destroyed) {
|
|
20838
|
+
const err = /* @__PURE__ */ new Error("The socket was closed while data was being compressed");
|
|
20839
|
+
callCallbacks(this, err, cb);
|
|
20840
|
+
return;
|
|
20841
|
+
}
|
|
20842
|
+
this._bufferedBytes -= options[kByteLength];
|
|
20843
|
+
this._state = DEFAULT;
|
|
20844
|
+
options.readOnly = false;
|
|
20845
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
20846
|
+
this.dequeue();
|
|
20847
|
+
});
|
|
20848
|
+
}
|
|
20849
|
+
/**
|
|
20850
|
+
* Executes queued send operations.
|
|
20851
|
+
*
|
|
20852
|
+
* @private
|
|
20853
|
+
*/
|
|
20854
|
+
dequeue() {
|
|
20855
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
20856
|
+
const params = this._queue.shift();
|
|
20857
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
20858
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
20859
|
+
}
|
|
20860
|
+
}
|
|
20861
|
+
/**
|
|
20862
|
+
* Enqueues a send operation.
|
|
20863
|
+
*
|
|
20864
|
+
* @param {Array} params Send operation parameters.
|
|
20865
|
+
* @private
|
|
20866
|
+
*/
|
|
20867
|
+
enqueue(params) {
|
|
20868
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
20869
|
+
this._queue.push(params);
|
|
20870
|
+
}
|
|
20871
|
+
/**
|
|
20872
|
+
* Sends a frame.
|
|
20873
|
+
*
|
|
20874
|
+
* @param {(Buffer | String)[]} list The frame to send
|
|
20875
|
+
* @param {Function} [cb] Callback
|
|
20876
|
+
* @private
|
|
20877
|
+
*/
|
|
20878
|
+
sendFrame(list, cb) {
|
|
20879
|
+
if (list.length === 2) {
|
|
20880
|
+
this._socket.cork();
|
|
20881
|
+
this._socket.write(list[0]);
|
|
20882
|
+
this._socket.write(list[1], cb);
|
|
20883
|
+
this._socket.uncork();
|
|
20884
|
+
} else this._socket.write(list[0], cb);
|
|
20885
|
+
}
|
|
20886
|
+
};
|
|
20887
|
+
/**
|
|
20888
|
+
* Calls queued callbacks with an error.
|
|
20889
|
+
*
|
|
20890
|
+
* @param {Sender} sender The `Sender` instance
|
|
20891
|
+
* @param {Error} err The error to call the callbacks with
|
|
20892
|
+
* @param {Function} [cb] The first callback
|
|
20893
|
+
* @private
|
|
20894
|
+
*/
|
|
20895
|
+
function callCallbacks(sender, err, cb) {
|
|
20896
|
+
if (typeof cb === "function") cb(err);
|
|
20897
|
+
for (let i = 0; i < sender._queue.length; i++) {
|
|
20898
|
+
const params = sender._queue[i];
|
|
20899
|
+
const callback = params[params.length - 1];
|
|
20900
|
+
if (typeof callback === "function") callback(err);
|
|
20901
|
+
}
|
|
20902
|
+
}
|
|
20903
|
+
/**
|
|
20904
|
+
* Handles a `Sender` error.
|
|
20905
|
+
*
|
|
20906
|
+
* @param {Sender} sender The `Sender` instance
|
|
20907
|
+
* @param {Error} err The error
|
|
20908
|
+
* @param {Function} [cb] The first pending callback
|
|
20909
|
+
* @private
|
|
20910
|
+
*/
|
|
20911
|
+
function onError(sender, err, cb) {
|
|
20912
|
+
callCallbacks(sender, err, cb);
|
|
20913
|
+
sender.onerror(err);
|
|
20914
|
+
}
|
|
20915
|
+
}));
|
|
20916
|
+
//#endregion
|
|
20917
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/event-target.js
|
|
20918
|
+
var require_event_target = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
20919
|
+
const { kForOnEventAttribute, kListener } = require_constants$1();
|
|
20920
|
+
const kCode = Symbol("kCode");
|
|
20921
|
+
const kData = Symbol("kData");
|
|
20922
|
+
const kError = Symbol("kError");
|
|
20923
|
+
const kMessage = Symbol("kMessage");
|
|
20924
|
+
const kReason = Symbol("kReason");
|
|
20925
|
+
const kTarget = Symbol("kTarget");
|
|
20926
|
+
const kType = Symbol("kType");
|
|
20927
|
+
const kWasClean = Symbol("kWasClean");
|
|
20928
|
+
/**
|
|
20929
|
+
* Class representing an event.
|
|
20930
|
+
*/
|
|
20931
|
+
var Event = class {
|
|
20932
|
+
/**
|
|
20933
|
+
* Create a new `Event`.
|
|
20934
|
+
*
|
|
20935
|
+
* @param {String} type The name of the event
|
|
20936
|
+
* @throws {TypeError} If the `type` argument is not specified
|
|
20937
|
+
*/
|
|
20938
|
+
constructor(type) {
|
|
20939
|
+
this[kTarget] = null;
|
|
20940
|
+
this[kType] = type;
|
|
20941
|
+
}
|
|
20942
|
+
/**
|
|
20943
|
+
* @type {*}
|
|
20944
|
+
*/
|
|
20945
|
+
get target() {
|
|
20946
|
+
return this[kTarget];
|
|
20947
|
+
}
|
|
20948
|
+
/**
|
|
20949
|
+
* @type {String}
|
|
20950
|
+
*/
|
|
20951
|
+
get type() {
|
|
20952
|
+
return this[kType];
|
|
20953
|
+
}
|
|
20954
|
+
};
|
|
20955
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
20956
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
20957
|
+
/**
|
|
20958
|
+
* Class representing a close event.
|
|
20959
|
+
*
|
|
20960
|
+
* @extends Event
|
|
20961
|
+
*/
|
|
20962
|
+
var CloseEvent = class extends Event {
|
|
20963
|
+
/**
|
|
20964
|
+
* Create a new `CloseEvent`.
|
|
20965
|
+
*
|
|
20966
|
+
* @param {String} type The name of the event
|
|
20967
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
20968
|
+
* attributes via object members of the same name
|
|
20969
|
+
* @param {Number} [options.code=0] The status code explaining why the
|
|
20970
|
+
* connection was closed
|
|
20971
|
+
* @param {String} [options.reason=''] A human-readable string explaining why
|
|
20972
|
+
* the connection was closed
|
|
20973
|
+
* @param {Boolean} [options.wasClean=false] Indicates whether or not the
|
|
20974
|
+
* connection was cleanly closed
|
|
20975
|
+
*/
|
|
20976
|
+
constructor(type, options = {}) {
|
|
20977
|
+
super(type);
|
|
20978
|
+
this[kCode] = options.code === void 0 ? 0 : options.code;
|
|
20979
|
+
this[kReason] = options.reason === void 0 ? "" : options.reason;
|
|
20980
|
+
this[kWasClean] = options.wasClean === void 0 ? false : options.wasClean;
|
|
20981
|
+
}
|
|
20982
|
+
/**
|
|
20983
|
+
* @type {Number}
|
|
20984
|
+
*/
|
|
20985
|
+
get code() {
|
|
20986
|
+
return this[kCode];
|
|
20987
|
+
}
|
|
20988
|
+
/**
|
|
20989
|
+
* @type {String}
|
|
20990
|
+
*/
|
|
20991
|
+
get reason() {
|
|
20992
|
+
return this[kReason];
|
|
20993
|
+
}
|
|
20994
|
+
/**
|
|
20995
|
+
* @type {Boolean}
|
|
20996
|
+
*/
|
|
20997
|
+
get wasClean() {
|
|
20998
|
+
return this[kWasClean];
|
|
20999
|
+
}
|
|
21000
|
+
};
|
|
21001
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
21002
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
21003
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
21004
|
+
/**
|
|
21005
|
+
* Class representing an error event.
|
|
21006
|
+
*
|
|
21007
|
+
* @extends Event
|
|
21008
|
+
*/
|
|
21009
|
+
var ErrorEvent = class extends Event {
|
|
21010
|
+
/**
|
|
21011
|
+
* Create a new `ErrorEvent`.
|
|
21012
|
+
*
|
|
21013
|
+
* @param {String} type The name of the event
|
|
21014
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
21015
|
+
* attributes via object members of the same name
|
|
21016
|
+
* @param {*} [options.error=null] The error that generated this event
|
|
21017
|
+
* @param {String} [options.message=''] The error message
|
|
21018
|
+
*/
|
|
21019
|
+
constructor(type, options = {}) {
|
|
21020
|
+
super(type);
|
|
21021
|
+
this[kError] = options.error === void 0 ? null : options.error;
|
|
21022
|
+
this[kMessage] = options.message === void 0 ? "" : options.message;
|
|
21023
|
+
}
|
|
21024
|
+
/**
|
|
21025
|
+
* @type {*}
|
|
21026
|
+
*/
|
|
21027
|
+
get error() {
|
|
21028
|
+
return this[kError];
|
|
21029
|
+
}
|
|
21030
|
+
/**
|
|
21031
|
+
* @type {String}
|
|
21032
|
+
*/
|
|
21033
|
+
get message() {
|
|
21034
|
+
return this[kMessage];
|
|
21035
|
+
}
|
|
21036
|
+
};
|
|
21037
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
21038
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
21039
|
+
/**
|
|
21040
|
+
* Class representing a message event.
|
|
21041
|
+
*
|
|
21042
|
+
* @extends Event
|
|
21043
|
+
*/
|
|
21044
|
+
var MessageEvent = class extends Event {
|
|
21045
|
+
/**
|
|
21046
|
+
* Create a new `MessageEvent`.
|
|
21047
|
+
*
|
|
21048
|
+
* @param {String} type The name of the event
|
|
21049
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
21050
|
+
* attributes via object members of the same name
|
|
21051
|
+
* @param {*} [options.data=null] The message content
|
|
21052
|
+
*/
|
|
21053
|
+
constructor(type, options = {}) {
|
|
21054
|
+
super(type);
|
|
21055
|
+
this[kData] = options.data === void 0 ? null : options.data;
|
|
21056
|
+
}
|
|
21057
|
+
/**
|
|
21058
|
+
* @type {*}
|
|
21059
|
+
*/
|
|
21060
|
+
get data() {
|
|
21061
|
+
return this[kData];
|
|
21062
|
+
}
|
|
21063
|
+
};
|
|
21064
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
21065
|
+
module.exports = {
|
|
21066
|
+
CloseEvent,
|
|
21067
|
+
ErrorEvent,
|
|
21068
|
+
Event,
|
|
21069
|
+
EventTarget: {
|
|
21070
|
+
/**
|
|
21071
|
+
* Register an event listener.
|
|
21072
|
+
*
|
|
21073
|
+
* @param {String} type A string representing the event type to listen for
|
|
21074
|
+
* @param {(Function|Object)} handler The listener to add
|
|
21075
|
+
* @param {Object} [options] An options object specifies characteristics about
|
|
21076
|
+
* the event listener
|
|
21077
|
+
* @param {Boolean} [options.once=false] A `Boolean` indicating that the
|
|
21078
|
+
* listener should be invoked at most once after being added. If `true`,
|
|
21079
|
+
* the listener would be automatically removed when invoked.
|
|
21080
|
+
* @public
|
|
21081
|
+
*/
|
|
21082
|
+
addEventListener(type, handler, options = {}) {
|
|
21083
|
+
for (const listener of this.listeners(type)) if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) return;
|
|
21084
|
+
let wrapper;
|
|
21085
|
+
if (type === "message") wrapper = function onMessage(data, isBinary) {
|
|
21086
|
+
const event = new MessageEvent("message", { data: isBinary ? data : data.toString() });
|
|
21087
|
+
event[kTarget] = this;
|
|
21088
|
+
callListener(handler, this, event);
|
|
21089
|
+
};
|
|
21090
|
+
else if (type === "close") wrapper = function onClose(code, message) {
|
|
21091
|
+
const event = new CloseEvent("close", {
|
|
21092
|
+
code,
|
|
21093
|
+
reason: message.toString(),
|
|
21094
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
21095
|
+
});
|
|
21096
|
+
event[kTarget] = this;
|
|
21097
|
+
callListener(handler, this, event);
|
|
21098
|
+
};
|
|
21099
|
+
else if (type === "error") wrapper = function onError(error) {
|
|
21100
|
+
const event = new ErrorEvent("error", {
|
|
21101
|
+
error,
|
|
21102
|
+
message: error.message
|
|
21103
|
+
});
|
|
21104
|
+
event[kTarget] = this;
|
|
21105
|
+
callListener(handler, this, event);
|
|
21106
|
+
};
|
|
21107
|
+
else if (type === "open") wrapper = function onOpen() {
|
|
21108
|
+
const event = new Event("open");
|
|
21109
|
+
event[kTarget] = this;
|
|
21110
|
+
callListener(handler, this, event);
|
|
21111
|
+
};
|
|
21112
|
+
else return;
|
|
21113
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
21114
|
+
wrapper[kListener] = handler;
|
|
21115
|
+
if (options.once) this.once(type, wrapper);
|
|
21116
|
+
else this.on(type, wrapper);
|
|
21117
|
+
},
|
|
21118
|
+
/**
|
|
21119
|
+
* Remove an event listener.
|
|
21120
|
+
*
|
|
21121
|
+
* @param {String} type A string representing the event type to remove
|
|
21122
|
+
* @param {(Function|Object)} handler The listener to remove
|
|
21123
|
+
* @public
|
|
21124
|
+
*/
|
|
21125
|
+
removeEventListener(type, handler) {
|
|
21126
|
+
for (const listener of this.listeners(type)) if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
21127
|
+
this.removeListener(type, listener);
|
|
21128
|
+
break;
|
|
21129
|
+
}
|
|
21130
|
+
}
|
|
21131
|
+
},
|
|
21132
|
+
MessageEvent
|
|
21133
|
+
};
|
|
21134
|
+
/**
|
|
21135
|
+
* Call an event listener
|
|
21136
|
+
*
|
|
21137
|
+
* @param {(Function|Object)} listener The listener to call
|
|
21138
|
+
* @param {*} thisArg The value to use as `this`` when calling the listener
|
|
21139
|
+
* @param {Event} event The event to pass to the listener
|
|
21140
|
+
* @private
|
|
21141
|
+
*/
|
|
21142
|
+
function callListener(listener, thisArg, event) {
|
|
21143
|
+
if (typeof listener === "object" && listener.handleEvent) listener.handleEvent.call(listener, event);
|
|
21144
|
+
else listener.call(thisArg, event);
|
|
21145
|
+
}
|
|
21146
|
+
}));
|
|
21147
|
+
//#endregion
|
|
21148
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/extension.js
|
|
21149
|
+
var require_extension = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
21150
|
+
const { tokenChars } = require_validation$3();
|
|
21151
|
+
/**
|
|
21152
|
+
* Adds an offer to the map of extension offers or a parameter to the map of
|
|
21153
|
+
* parameters.
|
|
21154
|
+
*
|
|
21155
|
+
* @param {Object} dest The map of extension offers or parameters
|
|
21156
|
+
* @param {String} name The extension or parameter name
|
|
21157
|
+
* @param {(Object|Boolean|String)} elem The extension parameters or the
|
|
21158
|
+
* parameter value
|
|
21159
|
+
* @private
|
|
21160
|
+
*/
|
|
21161
|
+
function push(dest, name, elem) {
|
|
21162
|
+
if (dest[name] === void 0) dest[name] = [elem];
|
|
21163
|
+
else dest[name].push(elem);
|
|
21164
|
+
}
|
|
21165
|
+
/**
|
|
21166
|
+
* Parses the `Sec-WebSocket-Extensions` header into an object.
|
|
21167
|
+
*
|
|
21168
|
+
* @param {String} header The field value of the header
|
|
21169
|
+
* @return {Object} The parsed object
|
|
21170
|
+
* @public
|
|
21171
|
+
*/
|
|
21172
|
+
function parse(header) {
|
|
21173
|
+
const offers = Object.create(null);
|
|
21174
|
+
let params = Object.create(null);
|
|
21175
|
+
let mustUnescape = false;
|
|
21176
|
+
let isEscaping = false;
|
|
21177
|
+
let inQuotes = false;
|
|
21178
|
+
let extensionName;
|
|
21179
|
+
let paramName;
|
|
21180
|
+
let start = -1;
|
|
21181
|
+
let code = -1;
|
|
21182
|
+
let end = -1;
|
|
21183
|
+
let i = 0;
|
|
21184
|
+
for (; i < header.length; i++) {
|
|
21185
|
+
code = header.charCodeAt(i);
|
|
21186
|
+
if (extensionName === void 0) if (end === -1 && tokenChars[code] === 1) {
|
|
21187
|
+
if (start === -1) start = i;
|
|
21188
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
21189
|
+
if (end === -1 && start !== -1) end = i;
|
|
21190
|
+
} else if (code === 59 || code === 44) {
|
|
21191
|
+
if (start === -1) throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
21192
|
+
if (end === -1) end = i;
|
|
21193
|
+
const name = header.slice(start, end);
|
|
21194
|
+
if (code === 44) {
|
|
21195
|
+
push(offers, name, params);
|
|
21196
|
+
params = Object.create(null);
|
|
21197
|
+
} else extensionName = name;
|
|
21198
|
+
start = end = -1;
|
|
21199
|
+
} else throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
21200
|
+
else if (paramName === void 0) if (end === -1 && tokenChars[code] === 1) {
|
|
21201
|
+
if (start === -1) start = i;
|
|
21202
|
+
} else if (code === 32 || code === 9) {
|
|
21203
|
+
if (end === -1 && start !== -1) end = i;
|
|
21204
|
+
} else if (code === 59 || code === 44) {
|
|
21205
|
+
if (start === -1) throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
21206
|
+
if (end === -1) end = i;
|
|
21207
|
+
push(params, header.slice(start, end), true);
|
|
21208
|
+
if (code === 44) {
|
|
21209
|
+
push(offers, extensionName, params);
|
|
21210
|
+
params = Object.create(null);
|
|
21211
|
+
extensionName = void 0;
|
|
21212
|
+
}
|
|
21213
|
+
start = end = -1;
|
|
21214
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
21215
|
+
paramName = header.slice(start, i);
|
|
21216
|
+
start = end = -1;
|
|
21217
|
+
} else throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
21218
|
+
else if (isEscaping) {
|
|
21219
|
+
if (tokenChars[code] !== 1) throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
21220
|
+
if (start === -1) start = i;
|
|
21221
|
+
else if (!mustUnescape) mustUnescape = true;
|
|
21222
|
+
isEscaping = false;
|
|
21223
|
+
} else if (inQuotes) if (tokenChars[code] === 1) {
|
|
21224
|
+
if (start === -1) start = i;
|
|
21225
|
+
} else if (code === 34 && start !== -1) {
|
|
21226
|
+
inQuotes = false;
|
|
21227
|
+
end = i;
|
|
21228
|
+
} else if (code === 92) isEscaping = true;
|
|
21229
|
+
else throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
21230
|
+
else if (code === 34 && header.charCodeAt(i - 1) === 61) inQuotes = true;
|
|
21231
|
+
else if (end === -1 && tokenChars[code] === 1) {
|
|
21232
|
+
if (start === -1) start = i;
|
|
21233
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
21234
|
+
if (end === -1) end = i;
|
|
21235
|
+
} else if (code === 59 || code === 44) {
|
|
21236
|
+
if (start === -1) throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
21237
|
+
if (end === -1) end = i;
|
|
21238
|
+
let value = header.slice(start, end);
|
|
21239
|
+
if (mustUnescape) {
|
|
21240
|
+
value = value.replace(/\\/g, "");
|
|
21241
|
+
mustUnescape = false;
|
|
21242
|
+
}
|
|
21243
|
+
push(params, paramName, value);
|
|
21244
|
+
if (code === 44) {
|
|
21245
|
+
push(offers, extensionName, params);
|
|
21246
|
+
params = Object.create(null);
|
|
21247
|
+
extensionName = void 0;
|
|
21248
|
+
}
|
|
21249
|
+
paramName = void 0;
|
|
21250
|
+
start = end = -1;
|
|
21251
|
+
} else throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
21252
|
+
}
|
|
21253
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) throw new SyntaxError("Unexpected end of input");
|
|
21254
|
+
if (end === -1) end = i;
|
|
21255
|
+
const token = header.slice(start, end);
|
|
21256
|
+
if (extensionName === void 0) push(offers, token, params);
|
|
21257
|
+
else {
|
|
21258
|
+
if (paramName === void 0) push(params, token, true);
|
|
21259
|
+
else if (mustUnescape) push(params, paramName, token.replace(/\\/g, ""));
|
|
21260
|
+
else push(params, paramName, token);
|
|
21261
|
+
push(offers, extensionName, params);
|
|
21262
|
+
}
|
|
21263
|
+
return offers;
|
|
21264
|
+
}
|
|
21265
|
+
/**
|
|
21266
|
+
* Builds the `Sec-WebSocket-Extensions` header field value.
|
|
21267
|
+
*
|
|
21268
|
+
* @param {Object} extensions The map of extensions and parameters to format
|
|
21269
|
+
* @return {String} A string representing the given object
|
|
21270
|
+
* @public
|
|
21271
|
+
*/
|
|
21272
|
+
function format(extensions) {
|
|
21273
|
+
return Object.keys(extensions).map((extension) => {
|
|
21274
|
+
let configurations = extensions[extension];
|
|
21275
|
+
if (!Array.isArray(configurations)) configurations = [configurations];
|
|
21276
|
+
return configurations.map((params) => {
|
|
21277
|
+
return [extension].concat(Object.keys(params).map((k) => {
|
|
21278
|
+
let values = params[k];
|
|
21279
|
+
if (!Array.isArray(values)) values = [values];
|
|
21280
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
21281
|
+
})).join("; ");
|
|
21282
|
+
}).join(", ");
|
|
21283
|
+
}).join(", ");
|
|
21284
|
+
}
|
|
21285
|
+
module.exports = {
|
|
21286
|
+
format,
|
|
21287
|
+
parse
|
|
21288
|
+
};
|
|
21289
|
+
}));
|
|
21290
|
+
//#endregion
|
|
21291
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/websocket.js
|
|
21292
|
+
var require_websocket = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
21293
|
+
const EventEmitter$7 = __require("events");
|
|
21294
|
+
const https = __require("https");
|
|
21295
|
+
const http$2 = __require("http");
|
|
21296
|
+
const net = __require("net");
|
|
21297
|
+
const tls = __require("tls");
|
|
21298
|
+
const { randomBytes: randomBytes$1, createHash: createHash$2 } = __require("crypto");
|
|
21299
|
+
const { Duplex: Duplex$2, Readable: Readable$1 } = __require("stream");
|
|
21300
|
+
const { URL: URL$1 } = __require("url");
|
|
21301
|
+
const PerMessageDeflate = require_permessage_deflate();
|
|
21302
|
+
const Receiver = require_receiver();
|
|
21303
|
+
const Sender = require_sender();
|
|
21304
|
+
const { isBlob } = require_validation$3();
|
|
21305
|
+
const { BINARY_TYPES, CLOSE_TIMEOUT, EMPTY_BUFFER, GUID, kForOnEventAttribute, kListener, kStatusCode, kWebSocket, NOOP } = require_constants$1();
|
|
21306
|
+
const { EventTarget: { addEventListener, removeEventListener } } = require_event_target();
|
|
21307
|
+
const { format, parse } = require_extension();
|
|
21308
|
+
const { toBuffer } = require_buffer_util();
|
|
21309
|
+
const kAborted = Symbol("kAborted");
|
|
21310
|
+
const protocolVersions = [8, 13];
|
|
21311
|
+
const readyStates = [
|
|
21312
|
+
"CONNECTING",
|
|
21313
|
+
"OPEN",
|
|
21314
|
+
"CLOSING",
|
|
21315
|
+
"CLOSED"
|
|
21316
|
+
];
|
|
21317
|
+
const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
21318
|
+
/**
|
|
21319
|
+
* Class representing a WebSocket.
|
|
21320
|
+
*
|
|
21321
|
+
* @extends EventEmitter
|
|
21322
|
+
*/
|
|
21323
|
+
var WebSocket = class WebSocket extends EventEmitter$7 {
|
|
21324
|
+
/**
|
|
21325
|
+
* Create a new `WebSocket`.
|
|
21326
|
+
*
|
|
21327
|
+
* @param {(String|URL)} address The URL to which to connect
|
|
21328
|
+
* @param {(String|String[])} [protocols] The subprotocols
|
|
21329
|
+
* @param {Object} [options] Connection options
|
|
21330
|
+
*/
|
|
21331
|
+
constructor(address, protocols, options) {
|
|
21332
|
+
super();
|
|
21333
|
+
this._binaryType = BINARY_TYPES[0];
|
|
21334
|
+
this._closeCode = 1006;
|
|
21335
|
+
this._closeFrameReceived = false;
|
|
21336
|
+
this._closeFrameSent = false;
|
|
21337
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
21338
|
+
this._closeTimer = null;
|
|
21339
|
+
this._errorEmitted = false;
|
|
21340
|
+
this._extensions = {};
|
|
21341
|
+
this._paused = false;
|
|
21342
|
+
this._protocol = "";
|
|
21343
|
+
this._readyState = WebSocket.CONNECTING;
|
|
21344
|
+
this._receiver = null;
|
|
21345
|
+
this._sender = null;
|
|
21346
|
+
this._socket = null;
|
|
21347
|
+
if (address !== null) {
|
|
21348
|
+
this._bufferedAmount = 0;
|
|
21349
|
+
this._isServer = false;
|
|
21350
|
+
this._redirects = 0;
|
|
21351
|
+
if (protocols === void 0) protocols = [];
|
|
21352
|
+
else if (!Array.isArray(protocols)) if (typeof protocols === "object" && protocols !== null) {
|
|
21353
|
+
options = protocols;
|
|
21354
|
+
protocols = [];
|
|
21355
|
+
} else protocols = [protocols];
|
|
21356
|
+
initAsClient(this, address, protocols, options);
|
|
21357
|
+
} else {
|
|
21358
|
+
this._autoPong = options.autoPong;
|
|
21359
|
+
this._closeTimeout = options.closeTimeout;
|
|
21360
|
+
this._isServer = true;
|
|
21361
|
+
}
|
|
21362
|
+
}
|
|
21363
|
+
/**
|
|
21364
|
+
* For historical reasons, the custom "nodebuffer" type is used by the default
|
|
21365
|
+
* instead of "blob".
|
|
21366
|
+
*
|
|
21367
|
+
* @type {String}
|
|
21368
|
+
*/
|
|
21369
|
+
get binaryType() {
|
|
21370
|
+
return this._binaryType;
|
|
21371
|
+
}
|
|
21372
|
+
set binaryType(type) {
|
|
21373
|
+
if (!BINARY_TYPES.includes(type)) return;
|
|
21374
|
+
this._binaryType = type;
|
|
21375
|
+
if (this._receiver) this._receiver._binaryType = type;
|
|
21376
|
+
}
|
|
21377
|
+
/**
|
|
21378
|
+
* @type {Number}
|
|
21379
|
+
*/
|
|
21380
|
+
get bufferedAmount() {
|
|
21381
|
+
if (!this._socket) return this._bufferedAmount;
|
|
21382
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
21383
|
+
}
|
|
21384
|
+
/**
|
|
21385
|
+
* @type {String}
|
|
21386
|
+
*/
|
|
21387
|
+
get extensions() {
|
|
21388
|
+
return Object.keys(this._extensions).join();
|
|
21389
|
+
}
|
|
21390
|
+
/**
|
|
21391
|
+
* @type {Boolean}
|
|
21392
|
+
*/
|
|
21393
|
+
get isPaused() {
|
|
21394
|
+
return this._paused;
|
|
21395
|
+
}
|
|
21396
|
+
/**
|
|
21397
|
+
* @type {Function}
|
|
21398
|
+
*/
|
|
21399
|
+
/* istanbul ignore next */
|
|
21400
|
+
get onclose() {
|
|
21401
|
+
return null;
|
|
21402
|
+
}
|
|
21403
|
+
/**
|
|
21404
|
+
* @type {Function}
|
|
21405
|
+
*/
|
|
21406
|
+
/* istanbul ignore next */
|
|
21407
|
+
get onerror() {
|
|
21408
|
+
return null;
|
|
21409
|
+
}
|
|
21410
|
+
/**
|
|
21411
|
+
* @type {Function}
|
|
21412
|
+
*/
|
|
21413
|
+
/* istanbul ignore next */
|
|
21414
|
+
get onopen() {
|
|
21415
|
+
return null;
|
|
21416
|
+
}
|
|
21417
|
+
/**
|
|
21418
|
+
* @type {Function}
|
|
21419
|
+
*/
|
|
21420
|
+
/* istanbul ignore next */
|
|
21421
|
+
get onmessage() {
|
|
21422
|
+
return null;
|
|
21423
|
+
}
|
|
21424
|
+
/**
|
|
21425
|
+
* @type {String}
|
|
21426
|
+
*/
|
|
21427
|
+
get protocol() {
|
|
21428
|
+
return this._protocol;
|
|
21429
|
+
}
|
|
21430
|
+
/**
|
|
21431
|
+
* @type {Number}
|
|
21432
|
+
*/
|
|
21433
|
+
get readyState() {
|
|
21434
|
+
return this._readyState;
|
|
21435
|
+
}
|
|
21436
|
+
/**
|
|
21437
|
+
* @type {String}
|
|
21438
|
+
*/
|
|
21439
|
+
get url() {
|
|
21440
|
+
return this._url;
|
|
21441
|
+
}
|
|
21442
|
+
/**
|
|
21443
|
+
* Set up the socket and the internal resources.
|
|
21444
|
+
*
|
|
21445
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
21446
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
21447
|
+
* @param {Object} options Options object
|
|
21448
|
+
* @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
|
|
21449
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
21450
|
+
* multiple times in the same tick
|
|
21451
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
21452
|
+
* masking key
|
|
21453
|
+
* @param {Number} [options.maxBufferedChunks=0] The maximum number of
|
|
21454
|
+
* buffered data chunks
|
|
21455
|
+
* @param {Number} [options.maxFragments=0] The maximum number of message
|
|
21456
|
+
* fragments
|
|
21457
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message size
|
|
21458
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
21459
|
+
* not to skip UTF-8 validation for text and close messages
|
|
21460
|
+
* @private
|
|
21461
|
+
*/
|
|
21462
|
+
setSocket(socket, head, options) {
|
|
21463
|
+
const receiver = new Receiver({
|
|
21464
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
21465
|
+
binaryType: this.binaryType,
|
|
21466
|
+
extensions: this._extensions,
|
|
21467
|
+
isServer: this._isServer,
|
|
21468
|
+
maxBufferedChunks: options.maxBufferedChunks,
|
|
21469
|
+
maxFragments: options.maxFragments,
|
|
21470
|
+
maxPayload: options.maxPayload,
|
|
21471
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
21472
|
+
});
|
|
21473
|
+
const sender = new Sender(socket, this._extensions, options.generateMask);
|
|
21474
|
+
this._receiver = receiver;
|
|
21475
|
+
this._sender = sender;
|
|
21476
|
+
this._socket = socket;
|
|
21477
|
+
receiver[kWebSocket] = this;
|
|
21478
|
+
sender[kWebSocket] = this;
|
|
21479
|
+
socket[kWebSocket] = this;
|
|
21480
|
+
receiver.on("conclude", receiverOnConclude);
|
|
21481
|
+
receiver.on("drain", receiverOnDrain);
|
|
21482
|
+
receiver.on("error", receiverOnError);
|
|
21483
|
+
receiver.on("message", receiverOnMessage);
|
|
21484
|
+
receiver.on("ping", receiverOnPing);
|
|
21485
|
+
receiver.on("pong", receiverOnPong);
|
|
21486
|
+
sender.onerror = senderOnError;
|
|
21487
|
+
if (socket.setTimeout) socket.setTimeout(0);
|
|
21488
|
+
if (socket.setNoDelay) socket.setNoDelay();
|
|
21489
|
+
if (head.length > 0) socket.unshift(head);
|
|
21490
|
+
socket.on("close", socketOnClose);
|
|
21491
|
+
socket.on("data", socketOnData);
|
|
21492
|
+
socket.on("end", socketOnEnd);
|
|
21493
|
+
socket.on("error", socketOnError);
|
|
21494
|
+
this._readyState = WebSocket.OPEN;
|
|
21495
|
+
this.emit("open");
|
|
21496
|
+
}
|
|
21497
|
+
/**
|
|
21498
|
+
* Emit the `'close'` event.
|
|
21499
|
+
*
|
|
21500
|
+
* @private
|
|
21501
|
+
*/
|
|
21502
|
+
emitClose() {
|
|
21503
|
+
if (!this._socket) {
|
|
21504
|
+
this._readyState = WebSocket.CLOSED;
|
|
21505
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
21506
|
+
return;
|
|
21507
|
+
}
|
|
21508
|
+
if (this._extensions[PerMessageDeflate.extensionName]) this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
21509
|
+
this._receiver.removeAllListeners();
|
|
21510
|
+
this._readyState = WebSocket.CLOSED;
|
|
21511
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
21512
|
+
}
|
|
21513
|
+
/**
|
|
21514
|
+
* Start a closing handshake.
|
|
21515
|
+
*
|
|
21516
|
+
* +----------+ +-----------+ +----------+
|
|
21517
|
+
* - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
|
|
21518
|
+
* | +----------+ +-----------+ +----------+ |
|
|
21519
|
+
* +----------+ +-----------+ |
|
|
21520
|
+
* CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
|
|
21521
|
+
* +----------+ +-----------+ |
|
|
21522
|
+
* | | | +---+ |
|
|
21523
|
+
* +------------------------+-->|fin| - - - -
|
|
21524
|
+
* | +---+ | +---+
|
|
21525
|
+
* - - - - -|fin|<---------------------+
|
|
21526
|
+
* +---+
|
|
21527
|
+
*
|
|
21528
|
+
* @param {Number} [code] Status code explaining why the connection is closing
|
|
21529
|
+
* @param {(String|Buffer)} [data] The reason why the connection is
|
|
21530
|
+
* closing
|
|
21531
|
+
* @public
|
|
21532
|
+
*/
|
|
21533
|
+
close(code, data) {
|
|
21534
|
+
if (this.readyState === WebSocket.CLOSED) return;
|
|
21535
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
21536
|
+
abortHandshake(this, this._req, "WebSocket was closed before the connection was established");
|
|
21537
|
+
return;
|
|
21538
|
+
}
|
|
21539
|
+
if (this.readyState === WebSocket.CLOSING) {
|
|
21540
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) this._socket.end();
|
|
21541
|
+
return;
|
|
21542
|
+
}
|
|
21543
|
+
this._readyState = WebSocket.CLOSING;
|
|
21544
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
21545
|
+
if (err) return;
|
|
21546
|
+
this._closeFrameSent = true;
|
|
21547
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) this._socket.end();
|
|
21548
|
+
});
|
|
21549
|
+
setCloseTimer(this);
|
|
21550
|
+
}
|
|
21551
|
+
/**
|
|
21552
|
+
* Pause the socket.
|
|
21553
|
+
*
|
|
21554
|
+
* @public
|
|
21555
|
+
*/
|
|
21556
|
+
pause() {
|
|
21557
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) return;
|
|
21558
|
+
this._paused = true;
|
|
21559
|
+
this._socket.pause();
|
|
21560
|
+
}
|
|
21561
|
+
/**
|
|
21562
|
+
* Send a ping.
|
|
21563
|
+
*
|
|
21564
|
+
* @param {*} [data] The data to send
|
|
21565
|
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
|
21566
|
+
* @param {Function} [cb] Callback which is executed when the ping is sent
|
|
21567
|
+
* @public
|
|
21568
|
+
*/
|
|
21569
|
+
ping(data, mask, cb) {
|
|
21570
|
+
if (this.readyState === WebSocket.CONNECTING) throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
21571
|
+
if (typeof data === "function") {
|
|
21572
|
+
cb = data;
|
|
21573
|
+
data = mask = void 0;
|
|
21574
|
+
} else if (typeof mask === "function") {
|
|
21575
|
+
cb = mask;
|
|
21576
|
+
mask = void 0;
|
|
21577
|
+
}
|
|
21578
|
+
if (typeof data === "number") data = data.toString();
|
|
21579
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
21580
|
+
sendAfterClose(this, data, cb);
|
|
21581
|
+
return;
|
|
21582
|
+
}
|
|
21583
|
+
if (mask === void 0) mask = !this._isServer;
|
|
21584
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
21585
|
+
}
|
|
21586
|
+
/**
|
|
21587
|
+
* Send a pong.
|
|
21588
|
+
*
|
|
21589
|
+
* @param {*} [data] The data to send
|
|
21590
|
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
|
21591
|
+
* @param {Function} [cb] Callback which is executed when the pong is sent
|
|
21592
|
+
* @public
|
|
21593
|
+
*/
|
|
21594
|
+
pong(data, mask, cb) {
|
|
21595
|
+
if (this.readyState === WebSocket.CONNECTING) throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
21596
|
+
if (typeof data === "function") {
|
|
21597
|
+
cb = data;
|
|
21598
|
+
data = mask = void 0;
|
|
21599
|
+
} else if (typeof mask === "function") {
|
|
21600
|
+
cb = mask;
|
|
21601
|
+
mask = void 0;
|
|
21602
|
+
}
|
|
21603
|
+
if (typeof data === "number") data = data.toString();
|
|
21604
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
21605
|
+
sendAfterClose(this, data, cb);
|
|
21606
|
+
return;
|
|
21607
|
+
}
|
|
21608
|
+
if (mask === void 0) mask = !this._isServer;
|
|
21609
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
21610
|
+
}
|
|
21611
|
+
/**
|
|
21612
|
+
* Resume the socket.
|
|
21613
|
+
*
|
|
21614
|
+
* @public
|
|
21615
|
+
*/
|
|
21616
|
+
resume() {
|
|
21617
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) return;
|
|
21618
|
+
this._paused = false;
|
|
21619
|
+
if (!this._receiver._writableState.needDrain) this._socket.resume();
|
|
21620
|
+
}
|
|
21621
|
+
/**
|
|
21622
|
+
* Send a data message.
|
|
21623
|
+
*
|
|
21624
|
+
* @param {*} data The message to send
|
|
21625
|
+
* @param {Object} [options] Options object
|
|
21626
|
+
* @param {Boolean} [options.binary] Specifies whether `data` is binary or
|
|
21627
|
+
* text
|
|
21628
|
+
* @param {Boolean} [options.compress] Specifies whether or not to compress
|
|
21629
|
+
* `data`
|
|
21630
|
+
* @param {Boolean} [options.fin=true] Specifies whether the fragment is the
|
|
21631
|
+
* last one
|
|
21632
|
+
* @param {Boolean} [options.mask] Specifies whether or not to mask `data`
|
|
21633
|
+
* @param {Function} [cb] Callback which is executed when data is written out
|
|
21634
|
+
* @public
|
|
21635
|
+
*/
|
|
21636
|
+
send(data, options, cb) {
|
|
21637
|
+
if (this.readyState === WebSocket.CONNECTING) throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
21638
|
+
if (typeof options === "function") {
|
|
21639
|
+
cb = options;
|
|
21640
|
+
options = {};
|
|
21641
|
+
}
|
|
21642
|
+
if (typeof data === "number") data = data.toString();
|
|
21643
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
21644
|
+
sendAfterClose(this, data, cb);
|
|
21645
|
+
return;
|
|
21646
|
+
}
|
|
21647
|
+
const opts = {
|
|
21648
|
+
binary: typeof data !== "string",
|
|
21649
|
+
mask: !this._isServer,
|
|
21650
|
+
compress: true,
|
|
21651
|
+
fin: true,
|
|
21652
|
+
...options
|
|
21653
|
+
};
|
|
21654
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) opts.compress = false;
|
|
21655
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
21656
|
+
}
|
|
21657
|
+
/**
|
|
21658
|
+
* Forcibly close the connection.
|
|
21659
|
+
*
|
|
21660
|
+
* @public
|
|
21661
|
+
*/
|
|
21662
|
+
terminate() {
|
|
21663
|
+
if (this.readyState === WebSocket.CLOSED) return;
|
|
21664
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
21665
|
+
abortHandshake(this, this._req, "WebSocket was closed before the connection was established");
|
|
21666
|
+
return;
|
|
21667
|
+
}
|
|
21668
|
+
if (this._socket) {
|
|
21669
|
+
this._readyState = WebSocket.CLOSING;
|
|
21670
|
+
this._socket.destroy();
|
|
21671
|
+
}
|
|
21672
|
+
}
|
|
21673
|
+
};
|
|
21674
|
+
/**
|
|
21675
|
+
* @constant {Number} CONNECTING
|
|
21676
|
+
* @memberof WebSocket
|
|
21677
|
+
*/
|
|
21678
|
+
Object.defineProperty(WebSocket, "CONNECTING", {
|
|
21679
|
+
enumerable: true,
|
|
21680
|
+
value: readyStates.indexOf("CONNECTING")
|
|
21681
|
+
});
|
|
21682
|
+
/**
|
|
21683
|
+
* @constant {Number} CONNECTING
|
|
21684
|
+
* @memberof WebSocket.prototype
|
|
21685
|
+
*/
|
|
21686
|
+
Object.defineProperty(WebSocket.prototype, "CONNECTING", {
|
|
21687
|
+
enumerable: true,
|
|
21688
|
+
value: readyStates.indexOf("CONNECTING")
|
|
21689
|
+
});
|
|
21690
|
+
/**
|
|
21691
|
+
* @constant {Number} OPEN
|
|
21692
|
+
* @memberof WebSocket
|
|
21693
|
+
*/
|
|
21694
|
+
Object.defineProperty(WebSocket, "OPEN", {
|
|
21695
|
+
enumerable: true,
|
|
21696
|
+
value: readyStates.indexOf("OPEN")
|
|
21697
|
+
});
|
|
21698
|
+
/**
|
|
21699
|
+
* @constant {Number} OPEN
|
|
21700
|
+
* @memberof WebSocket.prototype
|
|
21701
|
+
*/
|
|
21702
|
+
Object.defineProperty(WebSocket.prototype, "OPEN", {
|
|
21703
|
+
enumerable: true,
|
|
21704
|
+
value: readyStates.indexOf("OPEN")
|
|
21705
|
+
});
|
|
21706
|
+
/**
|
|
21707
|
+
* @constant {Number} CLOSING
|
|
21708
|
+
* @memberof WebSocket
|
|
21709
|
+
*/
|
|
21710
|
+
Object.defineProperty(WebSocket, "CLOSING", {
|
|
21711
|
+
enumerable: true,
|
|
21712
|
+
value: readyStates.indexOf("CLOSING")
|
|
21713
|
+
});
|
|
21714
|
+
/**
|
|
21715
|
+
* @constant {Number} CLOSING
|
|
21716
|
+
* @memberof WebSocket.prototype
|
|
21717
|
+
*/
|
|
21718
|
+
Object.defineProperty(WebSocket.prototype, "CLOSING", {
|
|
21719
|
+
enumerable: true,
|
|
21720
|
+
value: readyStates.indexOf("CLOSING")
|
|
21721
|
+
});
|
|
21722
|
+
/**
|
|
21723
|
+
* @constant {Number} CLOSED
|
|
21724
|
+
* @memberof WebSocket
|
|
21725
|
+
*/
|
|
21726
|
+
Object.defineProperty(WebSocket, "CLOSED", {
|
|
21727
|
+
enumerable: true,
|
|
21728
|
+
value: readyStates.indexOf("CLOSED")
|
|
21729
|
+
});
|
|
21730
|
+
/**
|
|
21731
|
+
* @constant {Number} CLOSED
|
|
21732
|
+
* @memberof WebSocket.prototype
|
|
21733
|
+
*/
|
|
21734
|
+
Object.defineProperty(WebSocket.prototype, "CLOSED", {
|
|
21735
|
+
enumerable: true,
|
|
21736
|
+
value: readyStates.indexOf("CLOSED")
|
|
21737
|
+
});
|
|
21738
|
+
[
|
|
21739
|
+
"binaryType",
|
|
21740
|
+
"bufferedAmount",
|
|
21741
|
+
"extensions",
|
|
21742
|
+
"isPaused",
|
|
21743
|
+
"protocol",
|
|
21744
|
+
"readyState",
|
|
21745
|
+
"url"
|
|
21746
|
+
].forEach((property) => {
|
|
21747
|
+
Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
|
|
21748
|
+
});
|
|
21749
|
+
[
|
|
21750
|
+
"open",
|
|
21751
|
+
"error",
|
|
21752
|
+
"close",
|
|
21753
|
+
"message"
|
|
21754
|
+
].forEach((method) => {
|
|
21755
|
+
Object.defineProperty(WebSocket.prototype, `on${method}`, {
|
|
21756
|
+
enumerable: true,
|
|
21757
|
+
get() {
|
|
21758
|
+
for (const listener of this.listeners(method)) if (listener[kForOnEventAttribute]) return listener[kListener];
|
|
21759
|
+
return null;
|
|
21760
|
+
},
|
|
21761
|
+
set(handler) {
|
|
21762
|
+
for (const listener of this.listeners(method)) if (listener[kForOnEventAttribute]) {
|
|
21763
|
+
this.removeListener(method, listener);
|
|
21764
|
+
break;
|
|
21765
|
+
}
|
|
21766
|
+
if (typeof handler !== "function") return;
|
|
21767
|
+
this.addEventListener(method, handler, { [kForOnEventAttribute]: true });
|
|
21768
|
+
}
|
|
21769
|
+
});
|
|
21770
|
+
});
|
|
21771
|
+
WebSocket.prototype.addEventListener = addEventListener;
|
|
21772
|
+
WebSocket.prototype.removeEventListener = removeEventListener;
|
|
21773
|
+
module.exports = WebSocket;
|
|
21774
|
+
/**
|
|
21775
|
+
* Initialize a WebSocket client.
|
|
21776
|
+
*
|
|
21777
|
+
* @param {WebSocket} websocket The client to initialize
|
|
21778
|
+
* @param {(String|URL)} address The URL to which to connect
|
|
21779
|
+
* @param {Array} protocols The subprotocols
|
|
21780
|
+
* @param {Object} [options] Connection options
|
|
21781
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether any
|
|
21782
|
+
* of the `'message'`, `'ping'`, and `'pong'` events can be emitted multiple
|
|
21783
|
+
* times in the same tick
|
|
21784
|
+
* @param {Boolean} [options.autoPong=true] Specifies whether or not to
|
|
21785
|
+
* automatically send a pong in response to a ping
|
|
21786
|
+
* @param {Number} [options.closeTimeout=30000] Duration in milliseconds to wait
|
|
21787
|
+
* for the closing handshake to finish after `websocket.close()` is called
|
|
21788
|
+
* @param {Function} [options.finishRequest] A function which can be used to
|
|
21789
|
+
* customize the headers of each http request before it is sent
|
|
21790
|
+
* @param {Boolean} [options.followRedirects=false] Whether or not to follow
|
|
21791
|
+
* redirects
|
|
21792
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
21793
|
+
* masking key
|
|
21794
|
+
* @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
|
|
21795
|
+
* handshake request
|
|
21796
|
+
* @param {Number} [options.maxBufferedChunks=1048576] The maximum number of
|
|
21797
|
+
* buffered data chunks
|
|
21798
|
+
* @param {Number} [options.maxFragments=131072] The maximum number of message
|
|
21799
|
+
* fragments
|
|
21800
|
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
|
21801
|
+
* size
|
|
21802
|
+
* @param {Number} [options.maxRedirects=10] The maximum number of redirects
|
|
21803
|
+
* allowed
|
|
21804
|
+
* @param {String} [options.origin] Value of the `Origin` or
|
|
21805
|
+
* `Sec-WebSocket-Origin` header
|
|
21806
|
+
* @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
|
|
21807
|
+
* permessage-deflate
|
|
21808
|
+
* @param {Number} [options.protocolVersion=13] Value of the
|
|
21809
|
+
* `Sec-WebSocket-Version` header
|
|
21810
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
21811
|
+
* not to skip UTF-8 validation for text and close messages
|
|
21812
|
+
* @private
|
|
21813
|
+
*/
|
|
21814
|
+
function initAsClient(websocket, address, protocols, options) {
|
|
21815
|
+
const opts = {
|
|
21816
|
+
allowSynchronousEvents: true,
|
|
21817
|
+
autoPong: true,
|
|
21818
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
21819
|
+
protocolVersion: protocolVersions[1],
|
|
21820
|
+
maxBufferedChunks: 1024 * 1024,
|
|
21821
|
+
maxFragments: 128 * 1024,
|
|
21822
|
+
maxPayload: 100 * 1024 * 1024,
|
|
21823
|
+
skipUTF8Validation: false,
|
|
21824
|
+
perMessageDeflate: true,
|
|
21825
|
+
followRedirects: false,
|
|
21826
|
+
maxRedirects: 10,
|
|
21827
|
+
...options,
|
|
21828
|
+
socketPath: void 0,
|
|
21829
|
+
hostname: void 0,
|
|
21830
|
+
protocol: void 0,
|
|
21831
|
+
timeout: void 0,
|
|
21832
|
+
method: "GET",
|
|
21833
|
+
host: void 0,
|
|
21834
|
+
path: void 0,
|
|
21835
|
+
port: void 0
|
|
21836
|
+
};
|
|
21837
|
+
websocket._autoPong = opts.autoPong;
|
|
21838
|
+
websocket._closeTimeout = opts.closeTimeout;
|
|
21839
|
+
if (!protocolVersions.includes(opts.protocolVersion)) throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} (supported versions: ${protocolVersions.join(", ")})`);
|
|
21840
|
+
let parsedUrl;
|
|
21841
|
+
if (address instanceof URL$1) parsedUrl = address;
|
|
21842
|
+
else try {
|
|
21843
|
+
parsedUrl = new URL$1(address);
|
|
21844
|
+
} catch {
|
|
21845
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
21846
|
+
}
|
|
21847
|
+
if (parsedUrl.protocol === "http:") parsedUrl.protocol = "ws:";
|
|
21848
|
+
else if (parsedUrl.protocol === "https:") parsedUrl.protocol = "wss:";
|
|
21849
|
+
websocket._url = parsedUrl.href;
|
|
21850
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
21851
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
21852
|
+
let invalidUrlMessage;
|
|
21853
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) invalidUrlMessage = "The URL's protocol must be one of \"ws:\", \"wss:\", \"http:\", \"https:\", or \"ws+unix:\"";
|
|
21854
|
+
else if (isIpcUrl && !parsedUrl.pathname) invalidUrlMessage = "The URL's pathname is empty";
|
|
21855
|
+
else if (parsedUrl.hash) invalidUrlMessage = "The URL contains a fragment identifier";
|
|
21856
|
+
if (invalidUrlMessage) {
|
|
21857
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
21858
|
+
if (websocket._redirects === 0) throw err;
|
|
21859
|
+
else {
|
|
21860
|
+
emitErrorAndClose(websocket, err);
|
|
21861
|
+
return;
|
|
21862
|
+
}
|
|
21863
|
+
}
|
|
21864
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
21865
|
+
const key = randomBytes$1(16).toString("base64");
|
|
21866
|
+
const request = isSecure ? https.request : http$2.request;
|
|
21867
|
+
const protocolSet = /* @__PURE__ */ new Set();
|
|
21868
|
+
let perMessageDeflate;
|
|
21869
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
21870
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
21871
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
21872
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
21873
|
+
opts.headers = {
|
|
21874
|
+
...opts.headers,
|
|
21875
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
21876
|
+
"Sec-WebSocket-Key": key,
|
|
21877
|
+
Connection: "Upgrade",
|
|
21878
|
+
Upgrade: "websocket"
|
|
21879
|
+
};
|
|
21880
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
21881
|
+
opts.timeout = opts.handshakeTimeout;
|
|
21882
|
+
if (opts.perMessageDeflate) {
|
|
21883
|
+
perMessageDeflate = new PerMessageDeflate({
|
|
21884
|
+
...opts.perMessageDeflate,
|
|
21885
|
+
isServer: false,
|
|
21886
|
+
maxPayload: opts.maxPayload
|
|
21887
|
+
});
|
|
21888
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({ [PerMessageDeflate.extensionName]: perMessageDeflate.offer() });
|
|
21889
|
+
}
|
|
21890
|
+
if (protocols.length) {
|
|
21891
|
+
for (const protocol of protocols) {
|
|
21892
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) throw new SyntaxError("An invalid or duplicated subprotocol was specified");
|
|
21893
|
+
protocolSet.add(protocol);
|
|
21894
|
+
}
|
|
21895
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
21896
|
+
}
|
|
21897
|
+
if (opts.origin) if (opts.protocolVersion < 13) opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
21898
|
+
else opts.headers.Origin = opts.origin;
|
|
21899
|
+
if (parsedUrl.username || parsedUrl.password) opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
21900
|
+
if (isIpcUrl) {
|
|
21901
|
+
const parts = opts.path.split(":");
|
|
21902
|
+
opts.socketPath = parts[0];
|
|
21903
|
+
opts.path = parts[1];
|
|
21904
|
+
}
|
|
21905
|
+
let req;
|
|
21906
|
+
if (opts.followRedirects) {
|
|
21907
|
+
if (websocket._redirects === 0) {
|
|
21908
|
+
websocket._originalIpc = isIpcUrl;
|
|
21909
|
+
websocket._originalSecure = isSecure;
|
|
21910
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
21911
|
+
const headers = options && options.headers;
|
|
21912
|
+
options = {
|
|
21913
|
+
...options,
|
|
21914
|
+
headers: {}
|
|
21915
|
+
};
|
|
21916
|
+
if (headers) for (const [key, value] of Object.entries(headers)) options.headers[key.toLowerCase()] = value;
|
|
21917
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
21918
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
21919
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
21920
|
+
delete opts.headers.authorization;
|
|
21921
|
+
delete opts.headers.cookie;
|
|
21922
|
+
if (!isSameHost) delete opts.headers.host;
|
|
21923
|
+
opts.auth = void 0;
|
|
21924
|
+
}
|
|
21925
|
+
}
|
|
21926
|
+
if (opts.auth && !options.headers.authorization) options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
21927
|
+
req = websocket._req = request(opts);
|
|
21928
|
+
if (websocket._redirects) websocket.emit("redirect", websocket.url, req);
|
|
21929
|
+
} else req = websocket._req = request(opts);
|
|
21930
|
+
if (opts.timeout) req.on("timeout", () => {
|
|
21931
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
21932
|
+
});
|
|
21933
|
+
req.on("error", (err) => {
|
|
21934
|
+
if (req === null || req[kAborted]) return;
|
|
21935
|
+
req = websocket._req = null;
|
|
21936
|
+
emitErrorAndClose(websocket, err);
|
|
21937
|
+
});
|
|
21938
|
+
req.on("response", (res) => {
|
|
21939
|
+
const location = res.headers.location;
|
|
21940
|
+
const statusCode = res.statusCode;
|
|
21941
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
21942
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
21943
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
21944
|
+
return;
|
|
21945
|
+
}
|
|
21946
|
+
req.abort();
|
|
21947
|
+
let addr;
|
|
21948
|
+
try {
|
|
21949
|
+
addr = new URL$1(location, address);
|
|
21950
|
+
} catch (e) {
|
|
21951
|
+
emitErrorAndClose(websocket, /* @__PURE__ */ new SyntaxError(`Invalid URL: ${location}`));
|
|
21952
|
+
return;
|
|
21953
|
+
}
|
|
21954
|
+
initAsClient(websocket, addr, protocols, options);
|
|
21955
|
+
} else if (!websocket.emit("unexpected-response", req, res)) abortHandshake(websocket, req, `Unexpected server response: ${res.statusCode}`);
|
|
21956
|
+
});
|
|
21957
|
+
req.on("upgrade", (res, socket, head) => {
|
|
21958
|
+
websocket.emit("upgrade", res);
|
|
21959
|
+
if (websocket.readyState !== WebSocket.CONNECTING) return;
|
|
21960
|
+
req = websocket._req = null;
|
|
21961
|
+
const upgrade = res.headers.upgrade;
|
|
21962
|
+
if (upgrade === void 0 || upgrade.toLowerCase() !== "websocket") {
|
|
21963
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
21964
|
+
return;
|
|
21965
|
+
}
|
|
21966
|
+
const digest = createHash$2("sha1").update(key + GUID).digest("base64");
|
|
21967
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
21968
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
21969
|
+
return;
|
|
21970
|
+
}
|
|
21971
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
21972
|
+
let protError;
|
|
21973
|
+
if (serverProt !== void 0) {
|
|
21974
|
+
if (!protocolSet.size) protError = "Server sent a subprotocol but none was requested";
|
|
21975
|
+
else if (!protocolSet.has(serverProt)) protError = "Server sent an invalid subprotocol";
|
|
21976
|
+
} else if (protocolSet.size) protError = "Server sent no subprotocol";
|
|
21977
|
+
if (protError) {
|
|
21978
|
+
abortHandshake(websocket, socket, protError);
|
|
21979
|
+
return;
|
|
21980
|
+
}
|
|
21981
|
+
if (serverProt) websocket._protocol = serverProt;
|
|
21982
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
21983
|
+
if (secWebSocketExtensions !== void 0) {
|
|
21984
|
+
if (!perMessageDeflate) {
|
|
21985
|
+
abortHandshake(websocket, socket, "Server sent a Sec-WebSocket-Extensions header but no extension was requested");
|
|
21986
|
+
return;
|
|
21987
|
+
}
|
|
21988
|
+
let extensions;
|
|
21989
|
+
try {
|
|
21990
|
+
extensions = parse(secWebSocketExtensions);
|
|
21991
|
+
} catch (err) {
|
|
21992
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Extensions header");
|
|
21993
|
+
return;
|
|
21994
|
+
}
|
|
21995
|
+
const extensionNames = Object.keys(extensions);
|
|
21996
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
21997
|
+
abortHandshake(websocket, socket, "Server indicated an extension that was not requested");
|
|
21998
|
+
return;
|
|
21999
|
+
}
|
|
22000
|
+
try {
|
|
22001
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
22002
|
+
} catch (err) {
|
|
22003
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Extensions header");
|
|
22004
|
+
return;
|
|
22005
|
+
}
|
|
22006
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
22007
|
+
}
|
|
22008
|
+
websocket.setSocket(socket, head, {
|
|
22009
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
22010
|
+
generateMask: opts.generateMask,
|
|
22011
|
+
maxBufferedChunks: opts.maxBufferedChunks,
|
|
22012
|
+
maxFragments: opts.maxFragments,
|
|
22013
|
+
maxPayload: opts.maxPayload,
|
|
22014
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
22015
|
+
});
|
|
22016
|
+
});
|
|
22017
|
+
if (opts.finishRequest) opts.finishRequest(req, websocket);
|
|
22018
|
+
else req.end();
|
|
22019
|
+
}
|
|
22020
|
+
/**
|
|
22021
|
+
* Emit the `'error'` and `'close'` events.
|
|
22022
|
+
*
|
|
22023
|
+
* @param {WebSocket} websocket The WebSocket instance
|
|
22024
|
+
* @param {Error} The error to emit
|
|
22025
|
+
* @private
|
|
22026
|
+
*/
|
|
22027
|
+
function emitErrorAndClose(websocket, err) {
|
|
22028
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
22029
|
+
websocket._errorEmitted = true;
|
|
22030
|
+
websocket.emit("error", err);
|
|
22031
|
+
websocket.emitClose();
|
|
22032
|
+
}
|
|
22033
|
+
/**
|
|
22034
|
+
* Create a `net.Socket` and initiate a connection.
|
|
22035
|
+
*
|
|
22036
|
+
* @param {Object} options Connection options
|
|
22037
|
+
* @return {net.Socket} The newly created socket used to start the connection
|
|
22038
|
+
* @private
|
|
22039
|
+
*/
|
|
22040
|
+
function netConnect(options) {
|
|
22041
|
+
options.path = options.socketPath;
|
|
22042
|
+
return net.connect(options);
|
|
22043
|
+
}
|
|
22044
|
+
/**
|
|
22045
|
+
* Create a `tls.TLSSocket` and initiate a connection.
|
|
22046
|
+
*
|
|
22047
|
+
* @param {Object} options Connection options
|
|
22048
|
+
* @return {tls.TLSSocket} The newly created socket used to start the connection
|
|
22049
|
+
* @private
|
|
22050
|
+
*/
|
|
22051
|
+
function tlsConnect(options) {
|
|
22052
|
+
options.path = void 0;
|
|
22053
|
+
if (!options.servername && options.servername !== "") options.servername = net.isIP(options.host) ? "" : options.host;
|
|
22054
|
+
return tls.connect(options);
|
|
22055
|
+
}
|
|
22056
|
+
/**
|
|
22057
|
+
* Abort the handshake and emit an error.
|
|
22058
|
+
*
|
|
22059
|
+
* @param {WebSocket} websocket The WebSocket instance
|
|
22060
|
+
* @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
|
|
22061
|
+
* abort or the socket to destroy
|
|
22062
|
+
* @param {String} message The error message
|
|
22063
|
+
* @private
|
|
22064
|
+
*/
|
|
22065
|
+
function abortHandshake(websocket, stream, message) {
|
|
22066
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
22067
|
+
const err = new Error(message);
|
|
22068
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
22069
|
+
if (stream.setHeader) {
|
|
22070
|
+
stream[kAborted] = true;
|
|
22071
|
+
stream.abort();
|
|
22072
|
+
if (stream.socket && !stream.socket.destroyed) stream.socket.destroy();
|
|
22073
|
+
process.nextTick(emitErrorAndClose, websocket, err);
|
|
22074
|
+
} else {
|
|
22075
|
+
stream.destroy(err);
|
|
22076
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
22077
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
22078
|
+
}
|
|
22079
|
+
}
|
|
22080
|
+
/**
|
|
22081
|
+
* Handle cases where the `ping()`, `pong()`, or `send()` methods are called
|
|
22082
|
+
* when the `readyState` attribute is `CLOSING` or `CLOSED`.
|
|
22083
|
+
*
|
|
22084
|
+
* @param {WebSocket} websocket The WebSocket instance
|
|
22085
|
+
* @param {*} [data] The data to send
|
|
22086
|
+
* @param {Function} [cb] Callback
|
|
22087
|
+
* @private
|
|
22088
|
+
*/
|
|
22089
|
+
function sendAfterClose(websocket, data, cb) {
|
|
22090
|
+
if (data) {
|
|
22091
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
22092
|
+
if (websocket._socket) websocket._sender._bufferedBytes += length;
|
|
22093
|
+
else websocket._bufferedAmount += length;
|
|
22094
|
+
}
|
|
22095
|
+
if (cb) {
|
|
22096
|
+
const err = /* @__PURE__ */ new Error(`WebSocket is not open: readyState ${websocket.readyState} (${readyStates[websocket.readyState]})`);
|
|
22097
|
+
process.nextTick(cb, err);
|
|
22098
|
+
}
|
|
22099
|
+
}
|
|
22100
|
+
/**
|
|
22101
|
+
* The listener of the `Receiver` `'conclude'` event.
|
|
22102
|
+
*
|
|
22103
|
+
* @param {Number} code The status code
|
|
22104
|
+
* @param {Buffer} reason The reason for closing
|
|
22105
|
+
* @private
|
|
22106
|
+
*/
|
|
22107
|
+
function receiverOnConclude(code, reason) {
|
|
22108
|
+
const websocket = this[kWebSocket];
|
|
22109
|
+
websocket._closeFrameReceived = true;
|
|
22110
|
+
websocket._closeMessage = reason;
|
|
22111
|
+
websocket._closeCode = code;
|
|
22112
|
+
if (websocket._socket[kWebSocket] === void 0) return;
|
|
22113
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
22114
|
+
process.nextTick(resume, websocket._socket);
|
|
22115
|
+
if (code === 1005) websocket.close();
|
|
22116
|
+
else websocket.close(code, reason);
|
|
22117
|
+
}
|
|
22118
|
+
/**
|
|
22119
|
+
* The listener of the `Receiver` `'drain'` event.
|
|
22120
|
+
*
|
|
22121
|
+
* @private
|
|
22122
|
+
*/
|
|
22123
|
+
function receiverOnDrain() {
|
|
22124
|
+
const websocket = this[kWebSocket];
|
|
22125
|
+
if (!websocket.isPaused) websocket._socket.resume();
|
|
22126
|
+
}
|
|
22127
|
+
/**
|
|
22128
|
+
* The listener of the `Receiver` `'error'` event.
|
|
22129
|
+
*
|
|
22130
|
+
* @param {(RangeError|Error)} err The emitted error
|
|
22131
|
+
* @private
|
|
22132
|
+
*/
|
|
22133
|
+
function receiverOnError(err) {
|
|
22134
|
+
const websocket = this[kWebSocket];
|
|
22135
|
+
if (websocket._socket[kWebSocket] !== void 0) {
|
|
22136
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
22137
|
+
process.nextTick(resume, websocket._socket);
|
|
22138
|
+
websocket.close(err[kStatusCode]);
|
|
22139
|
+
}
|
|
22140
|
+
if (!websocket._errorEmitted) {
|
|
22141
|
+
websocket._errorEmitted = true;
|
|
22142
|
+
websocket.emit("error", err);
|
|
22143
|
+
}
|
|
22144
|
+
}
|
|
22145
|
+
/**
|
|
22146
|
+
* The listener of the `Receiver` `'finish'` event.
|
|
22147
|
+
*
|
|
22148
|
+
* @private
|
|
22149
|
+
*/
|
|
22150
|
+
function receiverOnFinish() {
|
|
22151
|
+
this[kWebSocket].emitClose();
|
|
22152
|
+
}
|
|
22153
|
+
/**
|
|
22154
|
+
* The listener of the `Receiver` `'message'` event.
|
|
22155
|
+
*
|
|
22156
|
+
* @param {Buffer|ArrayBuffer|Buffer[])} data The message
|
|
22157
|
+
* @param {Boolean} isBinary Specifies whether the message is binary or not
|
|
22158
|
+
* @private
|
|
22159
|
+
*/
|
|
22160
|
+
function receiverOnMessage(data, isBinary) {
|
|
22161
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
22162
|
+
}
|
|
22163
|
+
/**
|
|
22164
|
+
* The listener of the `Receiver` `'ping'` event.
|
|
22165
|
+
*
|
|
22166
|
+
* @param {Buffer} data The data included in the ping frame
|
|
22167
|
+
* @private
|
|
22168
|
+
*/
|
|
22169
|
+
function receiverOnPing(data) {
|
|
22170
|
+
const websocket = this[kWebSocket];
|
|
22171
|
+
if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP);
|
|
22172
|
+
websocket.emit("ping", data);
|
|
22173
|
+
}
|
|
22174
|
+
/**
|
|
22175
|
+
* The listener of the `Receiver` `'pong'` event.
|
|
22176
|
+
*
|
|
22177
|
+
* @param {Buffer} data The data included in the pong frame
|
|
22178
|
+
* @private
|
|
22179
|
+
*/
|
|
22180
|
+
function receiverOnPong(data) {
|
|
22181
|
+
this[kWebSocket].emit("pong", data);
|
|
22182
|
+
}
|
|
22183
|
+
/**
|
|
22184
|
+
* Resume a readable stream
|
|
22185
|
+
*
|
|
22186
|
+
* @param {Readable} stream The readable stream
|
|
22187
|
+
* @private
|
|
22188
|
+
*/
|
|
22189
|
+
function resume(stream) {
|
|
22190
|
+
stream.resume();
|
|
22191
|
+
}
|
|
22192
|
+
/**
|
|
22193
|
+
* The `Sender` error event handler.
|
|
22194
|
+
*
|
|
22195
|
+
* @param {Error} The error
|
|
22196
|
+
* @private
|
|
22197
|
+
*/
|
|
22198
|
+
function senderOnError(err) {
|
|
22199
|
+
const websocket = this[kWebSocket];
|
|
22200
|
+
if (websocket.readyState === WebSocket.CLOSED) return;
|
|
22201
|
+
if (websocket.readyState === WebSocket.OPEN) {
|
|
22202
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
22203
|
+
setCloseTimer(websocket);
|
|
22204
|
+
}
|
|
22205
|
+
this._socket.end();
|
|
22206
|
+
if (!websocket._errorEmitted) {
|
|
22207
|
+
websocket._errorEmitted = true;
|
|
22208
|
+
websocket.emit("error", err);
|
|
22209
|
+
}
|
|
22210
|
+
}
|
|
22211
|
+
/**
|
|
22212
|
+
* Set a timer to destroy the underlying raw socket of a WebSocket.
|
|
22213
|
+
*
|
|
22214
|
+
* @param {WebSocket} websocket The WebSocket instance
|
|
22215
|
+
* @private
|
|
22216
|
+
*/
|
|
22217
|
+
function setCloseTimer(websocket) {
|
|
22218
|
+
websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), websocket._closeTimeout);
|
|
22219
|
+
}
|
|
22220
|
+
/**
|
|
22221
|
+
* The listener of the socket `'close'` event.
|
|
22222
|
+
*
|
|
22223
|
+
* @private
|
|
22224
|
+
*/
|
|
22225
|
+
function socketOnClose() {
|
|
22226
|
+
const websocket = this[kWebSocket];
|
|
22227
|
+
this.removeListener("close", socketOnClose);
|
|
22228
|
+
this.removeListener("data", socketOnData);
|
|
22229
|
+
this.removeListener("end", socketOnEnd);
|
|
22230
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
22231
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
|
|
22232
|
+
const chunk = this.read(this._readableState.length);
|
|
22233
|
+
websocket._receiver.write(chunk);
|
|
22234
|
+
}
|
|
22235
|
+
websocket._receiver.end();
|
|
22236
|
+
this[kWebSocket] = void 0;
|
|
22237
|
+
clearTimeout(websocket._closeTimer);
|
|
22238
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) websocket.emitClose();
|
|
22239
|
+
else {
|
|
22240
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
22241
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
22242
|
+
}
|
|
22243
|
+
}
|
|
22244
|
+
/**
|
|
22245
|
+
* The listener of the socket `'data'` event.
|
|
22246
|
+
*
|
|
22247
|
+
* @param {Buffer} chunk A chunk of data
|
|
22248
|
+
* @private
|
|
22249
|
+
*/
|
|
22250
|
+
function socketOnData(chunk) {
|
|
22251
|
+
if (!this[kWebSocket]._receiver.write(chunk)) this.pause();
|
|
22252
|
+
}
|
|
22253
|
+
/**
|
|
22254
|
+
* The listener of the socket `'end'` event.
|
|
22255
|
+
*
|
|
22256
|
+
* @private
|
|
22257
|
+
*/
|
|
22258
|
+
function socketOnEnd() {
|
|
22259
|
+
const websocket = this[kWebSocket];
|
|
22260
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
22261
|
+
websocket._receiver.end();
|
|
22262
|
+
this.end();
|
|
22263
|
+
}
|
|
22264
|
+
/**
|
|
22265
|
+
* The listener of the socket `'error'` event.
|
|
22266
|
+
*
|
|
22267
|
+
* @private
|
|
22268
|
+
*/
|
|
22269
|
+
function socketOnError() {
|
|
22270
|
+
const websocket = this[kWebSocket];
|
|
22271
|
+
this.removeListener("error", socketOnError);
|
|
22272
|
+
this.on("error", NOOP);
|
|
22273
|
+
if (websocket) {
|
|
22274
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
22275
|
+
this.destroy();
|
|
22276
|
+
}
|
|
22277
|
+
}
|
|
22278
|
+
}));
|
|
22279
|
+
//#endregion
|
|
22280
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/stream.js
|
|
22281
|
+
var require_stream = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
22282
|
+
require_websocket();
|
|
22283
|
+
const { Duplex: Duplex$1 } = __require("stream");
|
|
22284
|
+
/**
|
|
22285
|
+
* Emits the `'close'` event on a stream.
|
|
22286
|
+
*
|
|
22287
|
+
* @param {Duplex} stream The stream.
|
|
22288
|
+
* @private
|
|
22289
|
+
*/
|
|
22290
|
+
function emitClose(stream) {
|
|
22291
|
+
stream.emit("close");
|
|
22292
|
+
}
|
|
22293
|
+
/**
|
|
22294
|
+
* The listener of the `'end'` event.
|
|
22295
|
+
*
|
|
22296
|
+
* @private
|
|
22297
|
+
*/
|
|
22298
|
+
function duplexOnEnd() {
|
|
22299
|
+
if (!this.destroyed && this._writableState.finished) this.destroy();
|
|
22300
|
+
}
|
|
22301
|
+
/**
|
|
22302
|
+
* The listener of the `'error'` event.
|
|
22303
|
+
*
|
|
22304
|
+
* @param {Error} err The error
|
|
22305
|
+
* @private
|
|
22306
|
+
*/
|
|
22307
|
+
function duplexOnError(err) {
|
|
22308
|
+
this.removeListener("error", duplexOnError);
|
|
22309
|
+
this.destroy();
|
|
22310
|
+
if (this.listenerCount("error") === 0) this.emit("error", err);
|
|
22311
|
+
}
|
|
22312
|
+
/**
|
|
22313
|
+
* Wraps a `WebSocket` in a duplex stream.
|
|
22314
|
+
*
|
|
22315
|
+
* @param {WebSocket} ws The `WebSocket` to wrap
|
|
22316
|
+
* @param {Object} [options] The options for the `Duplex` constructor
|
|
22317
|
+
* @return {Duplex} The duplex stream
|
|
22318
|
+
* @public
|
|
22319
|
+
*/
|
|
22320
|
+
function createWebSocketStream(ws, options) {
|
|
22321
|
+
let terminateOnDestroy = true;
|
|
22322
|
+
const duplex = new Duplex$1({
|
|
22323
|
+
...options,
|
|
22324
|
+
autoDestroy: false,
|
|
22325
|
+
emitClose: false,
|
|
22326
|
+
objectMode: false,
|
|
22327
|
+
writableObjectMode: false
|
|
22328
|
+
});
|
|
22329
|
+
ws.on("message", function message(msg, isBinary) {
|
|
22330
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
22331
|
+
if (!duplex.push(data)) ws.pause();
|
|
22332
|
+
});
|
|
22333
|
+
ws.once("error", function error(err) {
|
|
22334
|
+
if (duplex.destroyed) return;
|
|
22335
|
+
terminateOnDestroy = false;
|
|
22336
|
+
duplex.destroy(err);
|
|
22337
|
+
});
|
|
22338
|
+
ws.once("close", function close() {
|
|
22339
|
+
if (duplex.destroyed) return;
|
|
22340
|
+
duplex.push(null);
|
|
22341
|
+
});
|
|
22342
|
+
duplex._destroy = function(err, callback) {
|
|
22343
|
+
if (ws.readyState === ws.CLOSED) {
|
|
22344
|
+
callback(err);
|
|
22345
|
+
process.nextTick(emitClose, duplex);
|
|
22346
|
+
return;
|
|
22347
|
+
}
|
|
22348
|
+
let called = false;
|
|
22349
|
+
ws.once("error", function error(err) {
|
|
22350
|
+
called = true;
|
|
22351
|
+
callback(err);
|
|
22352
|
+
});
|
|
22353
|
+
ws.once("close", function close() {
|
|
22354
|
+
if (!called) callback(err);
|
|
22355
|
+
process.nextTick(emitClose, duplex);
|
|
22356
|
+
});
|
|
22357
|
+
if (terminateOnDestroy) ws.terminate();
|
|
22358
|
+
};
|
|
22359
|
+
duplex._final = function(callback) {
|
|
22360
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
22361
|
+
ws.once("open", function open() {
|
|
22362
|
+
duplex._final(callback);
|
|
22363
|
+
});
|
|
22364
|
+
return;
|
|
22365
|
+
}
|
|
22366
|
+
if (ws._socket === null) return;
|
|
22367
|
+
if (ws._socket._writableState.finished) {
|
|
22368
|
+
callback();
|
|
22369
|
+
if (duplex._readableState.endEmitted) duplex.destroy();
|
|
22370
|
+
} else {
|
|
22371
|
+
ws._socket.once("finish", function finish() {
|
|
22372
|
+
callback();
|
|
22373
|
+
});
|
|
22374
|
+
ws.close();
|
|
22375
|
+
}
|
|
22376
|
+
};
|
|
22377
|
+
duplex._read = function() {
|
|
22378
|
+
if (ws.isPaused) ws.resume();
|
|
22379
|
+
};
|
|
22380
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
22381
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
22382
|
+
ws.once("open", function open() {
|
|
22383
|
+
duplex._write(chunk, encoding, callback);
|
|
22384
|
+
});
|
|
22385
|
+
return;
|
|
22386
|
+
}
|
|
22387
|
+
ws.send(chunk, callback);
|
|
22388
|
+
};
|
|
22389
|
+
duplex.on("end", duplexOnEnd);
|
|
22390
|
+
duplex.on("error", duplexOnError);
|
|
22391
|
+
return duplex;
|
|
22392
|
+
}
|
|
22393
|
+
module.exports = createWebSocketStream;
|
|
22394
|
+
}));
|
|
22395
|
+
//#endregion
|
|
22396
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/subprotocol.js
|
|
22397
|
+
var require_subprotocol = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
22398
|
+
const { tokenChars } = require_validation$3();
|
|
22399
|
+
/**
|
|
22400
|
+
* Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.
|
|
22401
|
+
*
|
|
22402
|
+
* @param {String} header The field value of the header
|
|
22403
|
+
* @return {Set} The subprotocol names
|
|
22404
|
+
* @public
|
|
22405
|
+
*/
|
|
22406
|
+
function parse(header) {
|
|
22407
|
+
const protocols = /* @__PURE__ */ new Set();
|
|
22408
|
+
let start = -1;
|
|
22409
|
+
let end = -1;
|
|
22410
|
+
let i = 0;
|
|
22411
|
+
for (; i < header.length; i++) {
|
|
22412
|
+
const code = header.charCodeAt(i);
|
|
22413
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
22414
|
+
if (start === -1) start = i;
|
|
22415
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
22416
|
+
if (end === -1 && start !== -1) end = i;
|
|
22417
|
+
} else if (code === 44) {
|
|
22418
|
+
if (start === -1) throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
22419
|
+
if (end === -1) end = i;
|
|
22420
|
+
const protocol = header.slice(start, end);
|
|
22421
|
+
if (protocols.has(protocol)) throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
22422
|
+
protocols.add(protocol);
|
|
22423
|
+
start = end = -1;
|
|
22424
|
+
} else throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
22425
|
+
}
|
|
22426
|
+
if (start === -1 || end !== -1) throw new SyntaxError("Unexpected end of input");
|
|
22427
|
+
const protocol = header.slice(start, i);
|
|
22428
|
+
if (protocols.has(protocol)) throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
22429
|
+
protocols.add(protocol);
|
|
22430
|
+
return protocols;
|
|
22431
|
+
}
|
|
22432
|
+
module.exports = { parse };
|
|
22433
|
+
}));
|
|
22434
|
+
//#endregion
|
|
22435
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/lib/websocket-server.js
|
|
22436
|
+
var require_websocket_server = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
22437
|
+
const EventEmitter$6 = __require("events");
|
|
22438
|
+
const http$1 = __require("http");
|
|
22439
|
+
const { Duplex } = __require("stream");
|
|
22440
|
+
const { createHash: createHash$1 } = __require("crypto");
|
|
22441
|
+
const extension = require_extension();
|
|
22442
|
+
const PerMessageDeflate = require_permessage_deflate();
|
|
22443
|
+
const subprotocol = require_subprotocol();
|
|
22444
|
+
const WebSocket = require_websocket();
|
|
22445
|
+
const { CLOSE_TIMEOUT, GUID, kWebSocket } = require_constants$1();
|
|
22446
|
+
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
22447
|
+
const RUNNING = 0;
|
|
22448
|
+
const CLOSING = 1;
|
|
22449
|
+
const CLOSED = 2;
|
|
22450
|
+
/**
|
|
22451
|
+
* Class representing a WebSocket server.
|
|
22452
|
+
*
|
|
22453
|
+
* @extends EventEmitter
|
|
22454
|
+
*/
|
|
22455
|
+
var WebSocketServer = class extends EventEmitter$6 {
|
|
22456
|
+
/**
|
|
22457
|
+
* Create a `WebSocketServer` instance.
|
|
22458
|
+
*
|
|
22459
|
+
* @param {Object} options Configuration options
|
|
22460
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
|
|
22461
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
22462
|
+
* multiple times in the same tick
|
|
22463
|
+
* @param {Boolean} [options.autoPong=true] Specifies whether or not to
|
|
22464
|
+
* automatically send a pong in response to a ping
|
|
22465
|
+
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
|
22466
|
+
* pending connections
|
|
22467
|
+
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
|
22468
|
+
* track clients
|
|
22469
|
+
* @param {Number} [options.closeTimeout=30000] Duration in milliseconds to
|
|
22470
|
+
* wait for the closing handshake to finish after `websocket.close()` is
|
|
22471
|
+
* called
|
|
22472
|
+
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
|
22473
|
+
* @param {String} [options.host] The hostname where to bind the server
|
|
22474
|
+
* @param {Number} [options.maxBufferedChunks=1048576] The maximum number of
|
|
22475
|
+
* buffered data chunks
|
|
22476
|
+
* @param {Number} [options.maxFragments=131072] The maximum number of message
|
|
22477
|
+
* fragments
|
|
22478
|
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
|
22479
|
+
* size
|
|
22480
|
+
* @param {Boolean} [options.noServer=false] Enable no server mode
|
|
22481
|
+
* @param {String} [options.path] Accept only connections matching this path
|
|
22482
|
+
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
|
22483
|
+
* permessage-deflate
|
|
22484
|
+
* @param {Number} [options.port] The port where to bind the server
|
|
22485
|
+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
|
22486
|
+
* server to use
|
|
22487
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
22488
|
+
* not to skip UTF-8 validation for text and close messages
|
|
22489
|
+
* @param {Function} [options.verifyClient] A hook to reject connections
|
|
22490
|
+
* @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
|
|
22491
|
+
* class to use. It must be the `WebSocket` class or class that extends it
|
|
22492
|
+
* @param {Function} [callback] A listener for the `listening` event
|
|
22493
|
+
*/
|
|
22494
|
+
constructor(options, callback) {
|
|
22495
|
+
super();
|
|
22496
|
+
options = {
|
|
22497
|
+
allowSynchronousEvents: true,
|
|
22498
|
+
autoPong: true,
|
|
22499
|
+
maxBufferedChunks: 1024 * 1024,
|
|
22500
|
+
maxFragments: 128 * 1024,
|
|
22501
|
+
maxPayload: 100 * 1024 * 1024,
|
|
22502
|
+
skipUTF8Validation: false,
|
|
22503
|
+
perMessageDeflate: false,
|
|
22504
|
+
handleProtocols: null,
|
|
22505
|
+
clientTracking: true,
|
|
22506
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
22507
|
+
verifyClient: null,
|
|
22508
|
+
noServer: false,
|
|
22509
|
+
backlog: null,
|
|
22510
|
+
server: null,
|
|
22511
|
+
host: null,
|
|
22512
|
+
path: null,
|
|
22513
|
+
port: null,
|
|
22514
|
+
WebSocket,
|
|
22515
|
+
...options
|
|
22516
|
+
};
|
|
22517
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) throw new TypeError("One and only one of the \"port\", \"server\", or \"noServer\" options must be specified");
|
|
22518
|
+
if (options.port != null) {
|
|
22519
|
+
this._server = http$1.createServer((req, res) => {
|
|
22520
|
+
const body = http$1.STATUS_CODES[426];
|
|
22521
|
+
res.writeHead(426, {
|
|
22522
|
+
"Content-Length": body.length,
|
|
22523
|
+
"Content-Type": "text/plain"
|
|
22524
|
+
});
|
|
22525
|
+
res.end(body);
|
|
22526
|
+
});
|
|
22527
|
+
this._server.listen(options.port, options.host, options.backlog, callback);
|
|
22528
|
+
} else if (options.server) this._server = options.server;
|
|
22529
|
+
if (this._server) {
|
|
22530
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
22531
|
+
this._removeListeners = addListeners(this._server, {
|
|
22532
|
+
listening: this.emit.bind(this, "listening"),
|
|
22533
|
+
error: this.emit.bind(this, "error"),
|
|
22534
|
+
upgrade: (req, socket, head) => {
|
|
22535
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
22536
|
+
}
|
|
22537
|
+
});
|
|
22538
|
+
}
|
|
22539
|
+
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
|
|
22540
|
+
if (options.clientTracking) {
|
|
22541
|
+
this.clients = /* @__PURE__ */ new Set();
|
|
22542
|
+
this._shouldEmitClose = false;
|
|
22543
|
+
}
|
|
22544
|
+
this.options = options;
|
|
22545
|
+
this._state = RUNNING;
|
|
22546
|
+
}
|
|
22547
|
+
/**
|
|
22548
|
+
* Returns the bound address, the address family name, and port of the server
|
|
22549
|
+
* as reported by the operating system if listening on an IP socket.
|
|
22550
|
+
* If the server is listening on a pipe or UNIX domain socket, the name is
|
|
22551
|
+
* returned as a string.
|
|
22552
|
+
*
|
|
22553
|
+
* @return {(Object|String|null)} The address of the server
|
|
22554
|
+
* @public
|
|
22555
|
+
*/
|
|
22556
|
+
address() {
|
|
22557
|
+
if (this.options.noServer) throw new Error("The server is operating in \"noServer\" mode");
|
|
22558
|
+
if (!this._server) return null;
|
|
22559
|
+
return this._server.address();
|
|
22560
|
+
}
|
|
22561
|
+
/**
|
|
22562
|
+
* Stop the server from accepting new connections and emit the `'close'` event
|
|
22563
|
+
* when all existing connections are closed.
|
|
22564
|
+
*
|
|
22565
|
+
* @param {Function} [cb] A one-time listener for the `'close'` event
|
|
22566
|
+
* @public
|
|
22567
|
+
*/
|
|
22568
|
+
close(cb) {
|
|
22569
|
+
if (this._state === CLOSED) {
|
|
22570
|
+
if (cb) this.once("close", () => {
|
|
22571
|
+
cb(/* @__PURE__ */ new Error("The server is not running"));
|
|
22572
|
+
});
|
|
22573
|
+
process.nextTick(emitClose, this);
|
|
22574
|
+
return;
|
|
22575
|
+
}
|
|
22576
|
+
if (cb) this.once("close", cb);
|
|
22577
|
+
if (this._state === CLOSING) return;
|
|
22578
|
+
this._state = CLOSING;
|
|
22579
|
+
if (this.options.noServer || this.options.server) {
|
|
22580
|
+
if (this._server) {
|
|
22581
|
+
this._removeListeners();
|
|
22582
|
+
this._removeListeners = this._server = null;
|
|
22583
|
+
}
|
|
22584
|
+
if (this.clients) if (!this.clients.size) process.nextTick(emitClose, this);
|
|
22585
|
+
else this._shouldEmitClose = true;
|
|
22586
|
+
else process.nextTick(emitClose, this);
|
|
22587
|
+
} else {
|
|
22588
|
+
const server = this._server;
|
|
22589
|
+
this._removeListeners();
|
|
22590
|
+
this._removeListeners = this._server = null;
|
|
22591
|
+
server.close(() => {
|
|
22592
|
+
emitClose(this);
|
|
22593
|
+
});
|
|
22594
|
+
}
|
|
22595
|
+
}
|
|
22596
|
+
/**
|
|
22597
|
+
* See if a given request should be handled by this server instance.
|
|
22598
|
+
*
|
|
22599
|
+
* @param {http.IncomingMessage} req Request object to inspect
|
|
22600
|
+
* @return {Boolean} `true` if the request is valid, else `false`
|
|
22601
|
+
* @public
|
|
22602
|
+
*/
|
|
22603
|
+
shouldHandle(req) {
|
|
22604
|
+
if (this.options.path) {
|
|
22605
|
+
const index = req.url.indexOf("?");
|
|
22606
|
+
if ((index !== -1 ? req.url.slice(0, index) : req.url) !== this.options.path) return false;
|
|
22607
|
+
}
|
|
22608
|
+
return true;
|
|
22609
|
+
}
|
|
22610
|
+
/**
|
|
22611
|
+
* Handle a HTTP Upgrade request.
|
|
22612
|
+
*
|
|
22613
|
+
* @param {http.IncomingMessage} req The request object
|
|
22614
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
22615
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
22616
|
+
* @param {Function} cb Callback
|
|
22617
|
+
* @public
|
|
22618
|
+
*/
|
|
22619
|
+
handleUpgrade(req, socket, head, cb) {
|
|
22620
|
+
socket.on("error", socketOnError);
|
|
22621
|
+
const key = req.headers["sec-websocket-key"];
|
|
22622
|
+
const upgrade = req.headers.upgrade;
|
|
22623
|
+
const version = +req.headers["sec-websocket-version"];
|
|
22624
|
+
if (req.method !== "GET") {
|
|
22625
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, "Invalid HTTP method");
|
|
22626
|
+
return;
|
|
22627
|
+
}
|
|
22628
|
+
if (upgrade === void 0 || upgrade.toLowerCase() !== "websocket") {
|
|
22629
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, "Invalid Upgrade header");
|
|
22630
|
+
return;
|
|
22631
|
+
}
|
|
22632
|
+
if (key === void 0 || !keyRegex.test(key)) {
|
|
22633
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, "Missing or invalid Sec-WebSocket-Key header");
|
|
22634
|
+
return;
|
|
22635
|
+
}
|
|
22636
|
+
if (version !== 13 && version !== 8) {
|
|
22637
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, "Missing or invalid Sec-WebSocket-Version header", { "Sec-WebSocket-Version": "13, 8" });
|
|
22638
|
+
return;
|
|
22639
|
+
}
|
|
22640
|
+
if (!this.shouldHandle(req)) {
|
|
22641
|
+
abortHandshake(socket, 400);
|
|
22642
|
+
return;
|
|
22643
|
+
}
|
|
22644
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
22645
|
+
let protocols = /* @__PURE__ */ new Set();
|
|
22646
|
+
if (secWebSocketProtocol !== void 0) try {
|
|
22647
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
22648
|
+
} catch (err) {
|
|
22649
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, "Invalid Sec-WebSocket-Protocol header");
|
|
22650
|
+
return;
|
|
22651
|
+
}
|
|
22652
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
22653
|
+
const extensions = {};
|
|
22654
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== void 0) {
|
|
22655
|
+
const perMessageDeflate = new PerMessageDeflate({
|
|
22656
|
+
...this.options.perMessageDeflate,
|
|
22657
|
+
isServer: true,
|
|
22658
|
+
maxPayload: this.options.maxPayload
|
|
22659
|
+
});
|
|
22660
|
+
try {
|
|
22661
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
22662
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
22663
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
22664
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
22665
|
+
}
|
|
22666
|
+
} catch (err) {
|
|
22667
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, "Invalid or unacceptable Sec-WebSocket-Extensions header");
|
|
22668
|
+
return;
|
|
22669
|
+
}
|
|
22670
|
+
}
|
|
22671
|
+
if (this.options.verifyClient) {
|
|
22672
|
+
const info = {
|
|
22673
|
+
origin: req.headers[`${version === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
22674
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
22675
|
+
req
|
|
22676
|
+
};
|
|
22677
|
+
if (this.options.verifyClient.length === 2) {
|
|
22678
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
22679
|
+
if (!verified) return abortHandshake(socket, code || 401, message, headers);
|
|
22680
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
22681
|
+
});
|
|
22682
|
+
return;
|
|
22683
|
+
}
|
|
22684
|
+
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
|
22685
|
+
}
|
|
22686
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
22687
|
+
}
|
|
22688
|
+
/**
|
|
22689
|
+
* Upgrade the connection to WebSocket.
|
|
22690
|
+
*
|
|
22691
|
+
* @param {Object} extensions The accepted extensions
|
|
22692
|
+
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
|
22693
|
+
* @param {Set} protocols The subprotocols
|
|
22694
|
+
* @param {http.IncomingMessage} req The request object
|
|
22695
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
22696
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
22697
|
+
* @param {Function} cb Callback
|
|
22698
|
+
* @throws {Error} If called more than once with the same socket
|
|
22699
|
+
* @private
|
|
22700
|
+
*/
|
|
22701
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
22702
|
+
if (!socket.readable || !socket.writable) return socket.destroy();
|
|
22703
|
+
if (socket[kWebSocket]) throw new Error("server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration");
|
|
22704
|
+
if (this._state > RUNNING) return abortHandshake(socket, 503);
|
|
22705
|
+
const headers = [
|
|
22706
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
22707
|
+
"Upgrade: websocket",
|
|
22708
|
+
"Connection: Upgrade",
|
|
22709
|
+
`Sec-WebSocket-Accept: ${createHash$1("sha1").update(key + GUID).digest("base64")}`
|
|
22710
|
+
];
|
|
22711
|
+
const ws = new this.options.WebSocket(null, void 0, this.options);
|
|
22712
|
+
if (protocols.size) {
|
|
22713
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
22714
|
+
if (protocol) {
|
|
22715
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
22716
|
+
ws._protocol = protocol;
|
|
22717
|
+
}
|
|
22718
|
+
}
|
|
22719
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
22720
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
22721
|
+
const value = extension.format({ [PerMessageDeflate.extensionName]: [params] });
|
|
22722
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
22723
|
+
ws._extensions = extensions;
|
|
22724
|
+
}
|
|
22725
|
+
this.emit("headers", headers, req);
|
|
22726
|
+
socket.write(headers.concat("\r\n").join("\r\n"));
|
|
22727
|
+
socket.removeListener("error", socketOnError);
|
|
22728
|
+
ws.setSocket(socket, head, {
|
|
22729
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
22730
|
+
maxBufferedChunks: this.options.maxBufferedChunks,
|
|
22731
|
+
maxFragments: this.options.maxFragments,
|
|
22732
|
+
maxPayload: this.options.maxPayload,
|
|
22733
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
22734
|
+
});
|
|
22735
|
+
if (this.clients) {
|
|
22736
|
+
this.clients.add(ws);
|
|
22737
|
+
ws.on("close", () => {
|
|
22738
|
+
this.clients.delete(ws);
|
|
22739
|
+
if (this._shouldEmitClose && !this.clients.size) process.nextTick(emitClose, this);
|
|
22740
|
+
});
|
|
22741
|
+
}
|
|
22742
|
+
cb(ws, req);
|
|
22743
|
+
}
|
|
22744
|
+
};
|
|
22745
|
+
module.exports = WebSocketServer;
|
|
22746
|
+
/**
|
|
22747
|
+
* Add event listeners on an `EventEmitter` using a map of <event, listener>
|
|
22748
|
+
* pairs.
|
|
22749
|
+
*
|
|
22750
|
+
* @param {EventEmitter} server The event emitter
|
|
22751
|
+
* @param {Object.<String, Function>} map The listeners to add
|
|
22752
|
+
* @return {Function} A function that will remove the added listeners when
|
|
22753
|
+
* called
|
|
22754
|
+
* @private
|
|
22755
|
+
*/
|
|
22756
|
+
function addListeners(server, map) {
|
|
22757
|
+
for (const event of Object.keys(map)) server.on(event, map[event]);
|
|
22758
|
+
return function removeListeners() {
|
|
22759
|
+
for (const event of Object.keys(map)) server.removeListener(event, map[event]);
|
|
22760
|
+
};
|
|
22761
|
+
}
|
|
22762
|
+
/**
|
|
22763
|
+
* Emit a `'close'` event on an `EventEmitter`.
|
|
22764
|
+
*
|
|
22765
|
+
* @param {EventEmitter} server The event emitter
|
|
22766
|
+
* @private
|
|
22767
|
+
*/
|
|
22768
|
+
function emitClose(server) {
|
|
22769
|
+
server._state = CLOSED;
|
|
22770
|
+
server.emit("close");
|
|
22771
|
+
}
|
|
22772
|
+
/**
|
|
22773
|
+
* Handle socket errors.
|
|
22774
|
+
*
|
|
22775
|
+
* @private
|
|
22776
|
+
*/
|
|
22777
|
+
function socketOnError() {
|
|
22778
|
+
this.destroy();
|
|
22779
|
+
}
|
|
22780
|
+
/**
|
|
22781
|
+
* Close the connection when preconditions are not fulfilled.
|
|
22782
|
+
*
|
|
22783
|
+
* @param {Duplex} socket The socket of the upgrade request
|
|
22784
|
+
* @param {Number} code The HTTP response status code
|
|
22785
|
+
* @param {String} [message] The HTTP response body
|
|
22786
|
+
* @param {Object} [headers] Additional HTTP response headers
|
|
22787
|
+
* @private
|
|
22788
|
+
*/
|
|
22789
|
+
function abortHandshake(socket, code, message, headers) {
|
|
22790
|
+
message = message || http$1.STATUS_CODES[code];
|
|
22791
|
+
headers = {
|
|
22792
|
+
Connection: "close",
|
|
22793
|
+
"Content-Type": "text/html",
|
|
22794
|
+
"Content-Length": Buffer.byteLength(message),
|
|
22795
|
+
...headers
|
|
22796
|
+
};
|
|
22797
|
+
socket.once("finish", socket.destroy);
|
|
22798
|
+
socket.end(`HTTP/1.1 ${code} ${http$1.STATUS_CODES[code]}\r\n` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join("\r\n") + "\r\n\r\n" + message);
|
|
22799
|
+
}
|
|
22800
|
+
/**
|
|
22801
|
+
* Emit a `'wsClientError'` event on a `WebSocketServer` if there is at least
|
|
22802
|
+
* one listener for it, otherwise call `abortHandshake()`.
|
|
22803
|
+
*
|
|
22804
|
+
* @param {WebSocketServer} server The WebSocket server
|
|
22805
|
+
* @param {http.IncomingMessage} req The request object
|
|
22806
|
+
* @param {Duplex} socket The socket of the upgrade request
|
|
22807
|
+
* @param {Number} code The HTTP response status code
|
|
22808
|
+
* @param {String} message The HTTP response body
|
|
22809
|
+
* @param {Object} [headers] The HTTP response headers
|
|
22810
|
+
* @private
|
|
22811
|
+
*/
|
|
22812
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
22813
|
+
if (server.listenerCount("wsClientError")) {
|
|
22814
|
+
const err = new Error(message);
|
|
22815
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
22816
|
+
server.emit("wsClientError", err, socket, req);
|
|
22817
|
+
} else abortHandshake(socket, code, message, headers);
|
|
22818
|
+
}
|
|
22819
|
+
}));
|
|
22820
|
+
//#endregion
|
|
22821
|
+
//#region ../../node_modules/.pnpm/ws@8.21.0/node_modules/ws/wrapper.mjs
|
|
19257
22822
|
var import_p_retry = /* @__PURE__ */ __toESM(require_p_retry(), 1);
|
|
19258
22823
|
var import_src = require_src();
|
|
22824
|
+
require_stream();
|
|
22825
|
+
require_extension();
|
|
22826
|
+
require_permessage_deflate();
|
|
22827
|
+
require_receiver();
|
|
22828
|
+
require_sender();
|
|
22829
|
+
require_subprotocol();
|
|
22830
|
+
var import_websocket = /* @__PURE__ */ __toESM(require_websocket(), 1);
|
|
22831
|
+
require_websocket_server();
|
|
22832
|
+
//#endregion
|
|
22833
|
+
//#region ../../node_modules/.pnpm/@google+genai@1.52.0_@modelcontextprotocol+sdk@1.29.0_zod@4.4.3_/node_modules/@google/genai/dist/node/index.mjs
|
|
19259
22834
|
/**
|
|
19260
22835
|
* @license
|
|
19261
22836
|
* Copyright 2025 Google LLC
|
|
@@ -32940,7 +36515,7 @@ var NodeWebSocket = class {
|
|
|
32940
36515
|
this.callbacks = callbacks;
|
|
32941
36516
|
}
|
|
32942
36517
|
connect() {
|
|
32943
|
-
this.ws = new
|
|
36518
|
+
this.ws = new import_websocket.default(this.url, { headers: this.headers });
|
|
32944
36519
|
this.ws.onopen = this.callbacks.onopen;
|
|
32945
36520
|
this.ws.onerror = this.callbacks.onerror;
|
|
32946
36521
|
this.ws.onclose = this.callbacks.onclose;
|
|
@@ -62281,7 +65856,7 @@ var KnowledgeLookupTool = class {
|
|
|
62281
65856
|
const llm = { generate: async (systemPrompt, userPrompt) => {
|
|
62282
65857
|
return this.agent.generateText(systemPrompt, userPrompt);
|
|
62283
65858
|
} };
|
|
62284
|
-
const { multiSearchWithTrace } = await import("./src-
|
|
65859
|
+
const { multiSearchWithTrace } = await import("./src-CsT5xa84.mjs");
|
|
62285
65860
|
const { results, trace } = await multiSearchWithTrace(store, llm, query, {
|
|
62286
65861
|
topK,
|
|
62287
65862
|
skipLlm: args.deep !== true
|
|
@@ -67196,7 +70771,7 @@ var import_yauzl = (/* @__PURE__ */ __commonJSMin(((exports) => {
|
|
|
67196
70771
|
var EventEmitter$3 = __require("events").EventEmitter;
|
|
67197
70772
|
var Transform$1 = __require("stream").Transform;
|
|
67198
70773
|
var PassThrough$2 = __require("stream").PassThrough;
|
|
67199
|
-
var Writable
|
|
70774
|
+
var Writable = __require("stream").Writable;
|
|
67200
70775
|
exports.fromBuffer = fromBuffer;
|
|
67201
70776
|
function fromBuffer(buffer, options, callback) {
|
|
67202
70777
|
if (typeof options === "function") {
|
|
@@ -67720,7 +71295,7 @@ var import_yauzl = (/* @__PURE__ */ __commonJSMin(((exports) => {
|
|
|
67720
71295
|
start: position,
|
|
67721
71296
|
end: position + length
|
|
67722
71297
|
});
|
|
67723
|
-
var writeStream = new Writable
|
|
71298
|
+
var writeStream = new Writable();
|
|
67724
71299
|
var written = 0;
|
|
67725
71300
|
writeStream._write = function(chunk, encoding, cb) {
|
|
67726
71301
|
chunk.copy(buffer, offset + written, 0, chunk.length);
|
|
@@ -87979,18 +91554,17 @@ const REVERSIBLE_TOOLS = new Set([
|
|
|
87979
91554
|
"TodoList",
|
|
87980
91555
|
"TaskList",
|
|
87981
91556
|
"TaskOutput",
|
|
91557
|
+
"AskUserQuestion",
|
|
87982
91558
|
"ContactParent",
|
|
87983
91559
|
"ReportFinding",
|
|
87984
91560
|
"SendSubagentMessage",
|
|
87985
91561
|
"Agent",
|
|
87986
91562
|
"WolfPack",
|
|
91563
|
+
"message_bot",
|
|
87987
91564
|
"CreateGoal",
|
|
87988
91565
|
"GetGoal",
|
|
87989
91566
|
"UpdateGoal",
|
|
87990
91567
|
"WriteGoalNote",
|
|
87991
|
-
"CronCreate",
|
|
87992
|
-
"CronList",
|
|
87993
|
-
"CronDelete",
|
|
87994
91568
|
"Write",
|
|
87995
91569
|
"Edit",
|
|
87996
91570
|
"LSP",
|
|
@@ -88001,10 +91575,20 @@ const REVERSIBLE_TOOLS = new Set([
|
|
|
88001
91575
|
* substitution, backticks, chaining, redirection and newline injection cannot
|
|
88002
91576
|
* ride along on an allowlisted head word. Mutable git subcommands
|
|
88003
91577
|
* (branch/tag/config/remote/stash/symbolic-ref), `find` (-exec/-delete) and
|
|
88004
|
-
* `sort` (-o writes files) are intentionally absent.
|
|
88005
|
-
|
|
91578
|
+
* `sort` (-o writes files) are intentionally absent. Word separators are
|
|
91579
|
+
* space/tab only: `\s` also matches a newline, so `ls\nrm -rf /` would otherwise
|
|
91580
|
+
* read as one allowlisted command with an argument. */
|
|
91581
|
+
const READONLY_BASH = /^(git[ \t]+(status|diff|log|show|rev-parse|ls-files)\b|ls|cat|head|tail|grep|echo|pwd|which|wc|uniq|date|printf|tr|cut|jq|node[ \t]+(-v|--version)|python3?[ \t]+(-V|--version)|npm[ \t]+ls|pnpm[ \t]+ls)([ \t]+[^&|;<>$`(){}[\]\\\n]*)?$/;
|
|
88006
91582
|
/** Bash commands with consequences that cannot be safely undone unattended. */
|
|
88007
91583
|
const DANGEROUS_BASH = /\b(rm\s+-rf|git\s+push|git\s+reset\s+--hard|npm\s+(publish|unpublish|install\s+-g)|pnpm\s+(publish|add\s+-g)|yarn\s+(publish|global)|chmod|chown|sudo|kill\s+-9|pkill|curl\s+[^|]*\s+-o|wget\s+[^|]*\s+-O|dd\s+|mkfs|shutdown|reboot)\b/;
|
|
91584
|
+
/** Test/build/lint commands: they reproduce a result from the workspace and are
|
|
91585
|
+
* the only non-read-only commands bot mode approves. The script head is pinned
|
|
91586
|
+
* to five names, so `pnpm run publish` or a free-form script cannot ride along
|
|
91587
|
+
* on the runner. Separators and arguments use the same space/tab-only class as
|
|
91588
|
+
* `READONLY_BASH`, so a newline, a metacharacter or shell substitution cannot
|
|
91589
|
+
* smuggle in a second command; `tsc -p` is limited to that class for the same
|
|
91590
|
+
* reason. */
|
|
91591
|
+
const REVERSIBLE_BASH = /^(?:pnpm[ \t]+(?:test|lint|build|typecheck|check)|(?:npm|yarn)[ \t]+test|(?:pnpm|npm|yarn)[ \t]+run[ \t]+(?:test|lint|build|typecheck|check)(?::[a-z-]+)?|node[ \t]+--test|vitest[ \t]+run|tsc[ \t]+--noEmit(?:[ \t]+-p[ \t]+[^&|;<>$`(){}[\]\\\n]+)?|eslint|oxlint)(?:[ \t]+[^&|;<>$`(){}[\]\\\n]*)?$/;
|
|
88008
91592
|
/**
|
|
88009
91593
|
* Bot mode (unattended): reversible actions auto-approve; everything else is
|
|
88010
91594
|
* denied and parked — the loop must not block on a human who is absent.
|
|
@@ -88016,9 +91600,18 @@ var BotModePermissionPolicy = class BotModePermissionPolicy {
|
|
|
88016
91600
|
this.agent = agent;
|
|
88017
91601
|
}
|
|
88018
91602
|
static SENSITIVE_WRITE = /(^|[/\\])(\.env(\.|$)|.*\.(pem|key|p12)(\.|$)|credential|token|secret|authorized_keys|id_rsa|id_ed25519|bash_history|zsh_history)/i;
|
|
91603
|
+
/**
|
|
91604
|
+
* `.git` is a privilege boundary: anything under `.git/hooks` becomes code
|
|
91605
|
+
* that git executes later, outside every policy this file enforces. Matched
|
|
91606
|
+
* case-insensitively because macOS and Windows resolve `.GIT` to `.git`.
|
|
91607
|
+
*/
|
|
91608
|
+
static hasGitSegment(rawPath) {
|
|
91609
|
+
return rawPath.split(/[/\\]+/).some((segment) => segment.toLowerCase() === ".git");
|
|
91610
|
+
}
|
|
88019
91611
|
isAllowedWritePath(rawPath) {
|
|
88020
91612
|
if (rawPath.trim().length === 0) return false;
|
|
88021
91613
|
if (rawPath.startsWith("~")) return false;
|
|
91614
|
+
if (BotModePermissionPolicy.hasGitSegment(rawPath)) return false;
|
|
88022
91615
|
if (!isPathInside(this.agent.config?.cwd ?? ".", rawPath)) return false;
|
|
88023
91616
|
if (BotModePermissionPolicy.SENSITIVE_WRITE.test(basename$1(normalize(rawPath)))) return false;
|
|
88024
91617
|
return true;
|
|
@@ -88028,13 +91621,17 @@ var BotModePermissionPolicy = class BotModePermissionPolicy {
|
|
|
88028
91621
|
const tool = context.toolCall.name;
|
|
88029
91622
|
if (tool === "Bash") {
|
|
88030
91623
|
const command = context.args?.command ?? "";
|
|
91624
|
+
if (DANGEROUS_BASH.test(command)) return {
|
|
91625
|
+
kind: "deny",
|
|
91626
|
+
reason: { reason: "bot: irreversible command parked for human review" }
|
|
91627
|
+
};
|
|
88031
91628
|
if (READONLY_BASH.test(command)) return {
|
|
88032
91629
|
kind: "approve",
|
|
88033
91630
|
reason: { reason: "bot: read-only command" }
|
|
88034
91631
|
};
|
|
88035
|
-
if (
|
|
88036
|
-
kind: "
|
|
88037
|
-
reason: { reason: "bot:
|
|
91632
|
+
if (REVERSIBLE_BASH.test(command)) return {
|
|
91633
|
+
kind: "approve",
|
|
91634
|
+
reason: { reason: "bot: reversible test/build command" }
|
|
88038
91635
|
};
|
|
88039
91636
|
return {
|
|
88040
91637
|
kind: "deny",
|
|
@@ -105306,6 +108903,7 @@ var ToolManager = class {
|
|
|
105306
108903
|
return noteOutcome(await rpc.toolCall({
|
|
105307
108904
|
turnId: Number(context.turnId),
|
|
105308
108905
|
toolCallId: context.toolCallId,
|
|
108906
|
+
name,
|
|
105309
108907
|
args
|
|
105310
108908
|
}, { signal: context.signal }));
|
|
105311
108909
|
}
|
|
@@ -113526,6 +117124,61 @@ var SessionDisposables = class {
|
|
|
113526
117124
|
};
|
|
113527
117125
|
//#endregion
|
|
113528
117126
|
//#region ../../packages/agent-core/src/session/index.ts
|
|
117127
|
+
/** Session-metadata key holding a serialized {@link SessionAgentProfile}. */
|
|
117128
|
+
const SESSION_AGENT_PROFILE_METADATA_KEY = "agentProfile";
|
|
117129
|
+
/**
|
|
117130
|
+
* Validates an untrusted serializable profile — an RPC payload or a persisted
|
|
117131
|
+
* `state.json` entry — and normalizes it. Returns `undefined` when nothing
|
|
117132
|
+
* usable is present, so a hand-edited or older metadata file degrades to the
|
|
117133
|
+
* default agent profile instead of failing the resume.
|
|
117134
|
+
*/
|
|
117135
|
+
function readSessionAgentProfile(value) {
|
|
117136
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
117137
|
+
const record = value;
|
|
117138
|
+
const name = readProfileText(record["name"]);
|
|
117139
|
+
const systemPrompt = readProfileText(record["systemPrompt"]);
|
|
117140
|
+
const activeTools = readProfileToolNames(record["activeTools"]);
|
|
117141
|
+
if (name === void 0 && systemPrompt === void 0 && activeTools === void 0) return;
|
|
117142
|
+
return {
|
|
117143
|
+
...name !== void 0 ? { name } : {},
|
|
117144
|
+
...systemPrompt !== void 0 ? { systemPrompt } : {},
|
|
117145
|
+
...activeTools !== void 0 ? { activeTools } : {}
|
|
117146
|
+
};
|
|
117147
|
+
}
|
|
117148
|
+
/**
|
|
117149
|
+
* Composes a serializable session profile onto the built-in main-agent
|
|
117150
|
+
* profile: the base renderer runs first and the role text is appended after
|
|
117151
|
+
* it, so a bot keeps the built-in operating instructions and adds its own
|
|
117152
|
+
* role on top instead of replacing them.
|
|
117153
|
+
*/
|
|
117154
|
+
function composeSessionAgentProfile(profile) {
|
|
117155
|
+
const base = DEFAULT_AGENT_PROFILES["agent"];
|
|
117156
|
+
if (base === void 0) return void 0;
|
|
117157
|
+
const baseRenderer = base.systemPrompt;
|
|
117158
|
+
const roleText = profile.systemPrompt?.trim();
|
|
117159
|
+
const systemPrompt = (context) => {
|
|
117160
|
+
const rendered = baseRenderer(context);
|
|
117161
|
+
return roleText === void 0 || roleText.length === 0 ? rendered : `${rendered}\n\n${roleText}`;
|
|
117162
|
+
};
|
|
117163
|
+
return {
|
|
117164
|
+
name: profile.name ?? base.name,
|
|
117165
|
+
description: base.description,
|
|
117166
|
+
systemPrompt,
|
|
117167
|
+
tools: [...profile.activeTools ?? base.tools],
|
|
117168
|
+
whenToUse: base.whenToUse,
|
|
117169
|
+
subagents: base.subagents,
|
|
117170
|
+
spawns: base.spawns
|
|
117171
|
+
};
|
|
117172
|
+
}
|
|
117173
|
+
function readProfileText(value) {
|
|
117174
|
+
if (typeof value !== "string") return void 0;
|
|
117175
|
+
const trimmed = value.trim();
|
|
117176
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
117177
|
+
}
|
|
117178
|
+
function readProfileToolNames(value) {
|
|
117179
|
+
if (!Array.isArray(value)) return void 0;
|
|
117180
|
+
return value.every((name) => typeof name === "string") ? value : void 0;
|
|
117181
|
+
}
|
|
113529
117182
|
const BACKGROUND_KEEP_ALIVE_ON_EXIT_ENV = "SCREAM_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT";
|
|
113530
117183
|
var Session$1 = class {
|
|
113531
117184
|
options;
|
|
@@ -113638,8 +117291,8 @@ var Session$1 = class {
|
|
|
113638
117291
|
this.subagentMessages.clear();
|
|
113639
117292
|
});
|
|
113640
117293
|
}
|
|
113641
|
-
async createMain() {
|
|
113642
|
-
const { agent } = await this.createAgent({ type: "main" }, DEFAULT_AGENT_PROFILES["agent"]);
|
|
117294
|
+
async createMain(profile) {
|
|
117295
|
+
const { agent } = await this.createAgent({ type: "main" }, profile ?? DEFAULT_AGENT_PROFILES["agent"]);
|
|
113643
117296
|
await agent.memoStoreReady;
|
|
113644
117297
|
await agent.knowledgeStoreReady;
|
|
113645
117298
|
await this.triggerSessionStart("startup");
|
|
@@ -113660,12 +117313,16 @@ var Session$1 = class {
|
|
|
113660
117313
|
await Promise.all(resumeTasks);
|
|
113661
117314
|
const resumeWarning = warning;
|
|
113662
117315
|
const main = this.agents.get("main");
|
|
113663
|
-
if (main !== void 0)
|
|
113664
|
-
const
|
|
113665
|
-
|
|
113666
|
-
|
|
113667
|
-
|
|
113668
|
-
|
|
117316
|
+
if (main !== void 0) {
|
|
117317
|
+
const pinned = readSessionAgentProfile(this.metadata.custom?.[SESSION_AGENT_PROFILE_METADATA_KEY]);
|
|
117318
|
+
const pinnedProfile = pinned === void 0 ? void 0 : composeSessionAgentProfile(pinned);
|
|
117319
|
+
let profile;
|
|
117320
|
+
if (pinnedProfile !== void 0) profile = pinnedProfile;
|
|
117321
|
+
else if (main.context.history.length === 0) profile = DEFAULT_AGENT_PROFILES["agent"];
|
|
117322
|
+
else {
|
|
117323
|
+
const profileName = main.config.profileName;
|
|
117324
|
+
profile = (profileName !== void 0 ? DEFAULT_AGENT_PROFILES[profileName] : void 0) ?? DEFAULT_AGENT_PROFILES["agent"];
|
|
117325
|
+
}
|
|
113669
117326
|
if (profile !== void 0) await this.bootstrapAgentProfile(main, profile);
|
|
113670
117327
|
}
|
|
113671
117328
|
if (main !== void 0 && this.metadata.custom?.["recap"]) main.context.appendSystemReminder(this.metadata.custom["recap"], {
|
|
@@ -130279,8 +133936,19 @@ function normalizeWorkDir(workDir) {
|
|
|
130279
133936
|
}
|
|
130280
133937
|
return resolved;
|
|
130281
133938
|
}
|
|
130282
|
-
|
|
130283
|
-
|
|
133939
|
+
/**
|
|
133940
|
+
* Resolves the work directory a session is keyed and rooted on.
|
|
133941
|
+
*
|
|
133942
|
+
* Default behavior (no `exactWorkDir`) walks up to the nearest `.git` /
|
|
133943
|
+
* `package.json` ancestor via {@link normalizeWorkDir}. `exactWorkDir: true`
|
|
133944
|
+
* keeps the directory the caller named — bots get a workspace root of their
|
|
133945
|
+
* own instead of being folded into the enclosing repository.
|
|
133946
|
+
*/
|
|
133947
|
+
function resolveWorkDir(workDir, exactWorkDir) {
|
|
133948
|
+
return exactWorkDir === true ? resolve$2(workDir) : normalizeWorkDir(workDir);
|
|
133949
|
+
}
|
|
133950
|
+
function encodeWorkDirKey(workDir, exactWorkDir) {
|
|
133951
|
+
const normalized = resolveWorkDir(workDir, exactWorkDir);
|
|
130284
133952
|
return `${WORKDIR_KEY_PREFIX}${slugifyWorkDirName(basename$1(normalized))}_${createHash("sha256").update(normalized).digest("hex").slice(0, HASH_LENGTH)}`;
|
|
130285
133953
|
}
|
|
130286
133954
|
//#endregion
|
|
@@ -130301,14 +133969,15 @@ var SessionStore = class {
|
|
|
130301
133969
|
}
|
|
130302
133970
|
sessionDirFor(input) {
|
|
130303
133971
|
assertSafeSessionId(input.id);
|
|
130304
|
-
return join$1(this.sessionsDir, encodeWorkDirKey(
|
|
133972
|
+
return join$1(this.sessionsDir, encodeWorkDirKey(input.workDir, input.exactWorkDir), input.id);
|
|
130305
133973
|
}
|
|
130306
133974
|
async create(input) {
|
|
130307
133975
|
assertSafeSessionId(input.id);
|
|
130308
|
-
const workDir =
|
|
133976
|
+
const workDir = resolveWorkDir(input.workDir, input.exactWorkDir);
|
|
130309
133977
|
const dir = this.sessionDirFor({
|
|
130310
133978
|
id: input.id,
|
|
130311
|
-
workDir
|
|
133979
|
+
workDir,
|
|
133980
|
+
exactWorkDir: input.exactWorkDir
|
|
130312
133981
|
});
|
|
130313
133982
|
if (await this.findSessionEntry(input.id) !== void 0) await this.delete(input.id);
|
|
130314
133983
|
else if (await isDirectory(dir)) await rmWithRetry(dir);
|
|
@@ -130724,7 +134393,9 @@ var ScreamCore = class {
|
|
|
130724
134393
|
}
|
|
130725
134394
|
async createSession(input) {
|
|
130726
134395
|
const options = input;
|
|
130727
|
-
const workDir = requiredWorkDir("createSession", options.workDir);
|
|
134396
|
+
const workDir = requiredWorkDir("createSession", options.workDir, options.exactWorkDir);
|
|
134397
|
+
const pinnedProfile = readSessionAgentProfile(options.profile);
|
|
134398
|
+
const agentProfile = pinnedProfile === void 0 ? void 0 : composeSessionAgentProfile(pinnedProfile);
|
|
130728
134399
|
const config = this.reloadProviderManager();
|
|
130729
134400
|
const id = options.id ?? createSessionId();
|
|
130730
134401
|
const thinkingLevel = resolveThinkingLevel(options.thinking, config);
|
|
@@ -130735,7 +134406,8 @@ var ScreamCore = class {
|
|
|
130735
134406
|
});
|
|
130736
134407
|
const summary = await this.sessionStore.create({
|
|
130737
134408
|
id,
|
|
130738
|
-
workDir
|
|
134409
|
+
workDir,
|
|
134410
|
+
exactWorkDir: options.exactWorkDir
|
|
130739
134411
|
});
|
|
130740
134412
|
const result = {
|
|
130741
134413
|
...summary,
|
|
@@ -130774,7 +134446,8 @@ var ScreamCore = class {
|
|
|
130774
134446
|
} : {},
|
|
130775
134447
|
custom: options.metadata === void 0 ? {} : { ...options.metadata }
|
|
130776
134448
|
};
|
|
130777
|
-
|
|
134449
|
+
if (pinnedProfile !== void 0) session.metadata.custom[SESSION_AGENT_PROFILE_METADATA_KEY] = pinnedProfile;
|
|
134450
|
+
const mainAgent = await session.createMain(agentProfile);
|
|
130778
134451
|
mainAgent.config.update({
|
|
130779
134452
|
modelAlias: options.model ?? config.defaultModel,
|
|
130780
134453
|
thinkingLevel
|
|
@@ -131533,9 +135206,9 @@ function buildWebSearcher(input) {
|
|
|
131533
135206
|
if (providers.length === 0) return void 0;
|
|
131534
135207
|
return providers.length === 1 ? providers[0] : new FallbackSearchProvider(providers);
|
|
131535
135208
|
}
|
|
131536
|
-
function requiredWorkDir(operation, value) {
|
|
135209
|
+
function requiredWorkDir(operation, value, exactWorkDir) {
|
|
131537
135210
|
if (typeof value !== "string" || value.trim() === "") throw new ScreamError(ErrorCodes.REQUEST_WORK_DIR_REQUIRED, `${operation} requires workDir`);
|
|
131538
|
-
return
|
|
135211
|
+
return resolveWorkDir(value, exactWorkDir);
|
|
131539
135212
|
}
|
|
131540
135213
|
function createSessionId() {
|
|
131541
135214
|
return `session_${randomUUID()}`;
|
|
@@ -131740,6 +135413,7 @@ var SDKRpcClient = class {
|
|
|
131740
135413
|
eventListeners = /* @__PURE__ */ new Set();
|
|
131741
135414
|
approvalHandlers = /* @__PURE__ */ new Map();
|
|
131742
135415
|
questionHandlers = /* @__PURE__ */ new Map();
|
|
135416
|
+
toolCallHandler;
|
|
131743
135417
|
constructor(options = {}) {
|
|
131744
135418
|
const [coreRpc, sdkRpc] = createRPC();
|
|
131745
135419
|
const homeDir = resolveScreamHome(options.homeDir);
|
|
@@ -131758,6 +135432,7 @@ var SDKRpcClient = class {
|
|
|
131758
135432
|
this.ready = sdkRpc(new ClientAPI(this)).then((rpc) => {
|
|
131759
135433
|
this.rpc = rpc;
|
|
131760
135434
|
});
|
|
135435
|
+
this.toolCallHandler = options.onToolCall;
|
|
131761
135436
|
}
|
|
131762
135437
|
get homeDir() {
|
|
131763
135438
|
return this.core.homeDir;
|
|
@@ -132285,6 +135960,34 @@ var SDKRpcClient = class {
|
|
|
132285
135960
|
this.approvalHandlers.delete(sessionId);
|
|
132286
135961
|
this.questionHandlers.delete(sessionId);
|
|
132287
135962
|
}
|
|
135963
|
+
/**
|
|
135964
|
+
* Register a host-executed tool on this session's interactive agent. The
|
|
135965
|
+
* host services its calls through `ScreamHarnessOptions.onToolCall`.
|
|
135966
|
+
*/
|
|
135967
|
+
async registerTool(input) {
|
|
135968
|
+
return (await this.getRpc()).registerTool({
|
|
135969
|
+
sessionId: input.sessionId,
|
|
135970
|
+
agentId: this.interactiveAgentId,
|
|
135971
|
+
name: input.name,
|
|
135972
|
+
description: input.description,
|
|
135973
|
+
parameters: input.parameters
|
|
135974
|
+
});
|
|
135975
|
+
}
|
|
135976
|
+
async unregisterTool(input) {
|
|
135977
|
+
return (await this.getRpc()).unregisterTool({
|
|
135978
|
+
sessionId: input.sessionId,
|
|
135979
|
+
agentId: this.interactiveAgentId,
|
|
135980
|
+
name: input.name
|
|
135981
|
+
});
|
|
135982
|
+
}
|
|
135983
|
+
/** Replace the interactive agent's active tool set. */
|
|
135984
|
+
async setActiveTools(input) {
|
|
135985
|
+
return (await this.getRpc()).setActiveTools({
|
|
135986
|
+
sessionId: input.sessionId,
|
|
135987
|
+
agentId: this.interactiveAgentId,
|
|
135988
|
+
names: input.names
|
|
135989
|
+
});
|
|
135990
|
+
}
|
|
132288
135991
|
async extractMemoriesOnExit(input) {
|
|
132289
135992
|
return (await this.getRpc()).extractMemoriesOnExit({
|
|
132290
135993
|
sessionId: input.sessionId,
|
|
@@ -132343,10 +136046,19 @@ var SDKRpcClient = class {
|
|
|
132343
136046
|
}
|
|
132344
136047
|
}
|
|
132345
136048
|
async toolCall(request) {
|
|
132346
|
-
|
|
136049
|
+
const handler = this.toolCallHandler;
|
|
136050
|
+
if (handler === void 0) return {
|
|
132347
136051
|
output: `SDK custom tool calls are not supported: ${request.toolCallId}`,
|
|
132348
136052
|
isError: true
|
|
132349
136053
|
};
|
|
136054
|
+
try {
|
|
136055
|
+
return await handler(request);
|
|
136056
|
+
} catch (error) {
|
|
136057
|
+
return {
|
|
136058
|
+
output: `SDK custom tool handler failed: ${errorMessage(error)}`,
|
|
136059
|
+
isError: true
|
|
136060
|
+
};
|
|
136061
|
+
}
|
|
132350
136062
|
}
|
|
132351
136063
|
async getRpc() {
|
|
132352
136064
|
await this.ready;
|
|
@@ -132454,6 +136166,37 @@ var Session = class {
|
|
|
132454
136166
|
prompt
|
|
132455
136167
|
});
|
|
132456
136168
|
}
|
|
136169
|
+
/**
|
|
136170
|
+
* Register a host-executed tool on this session's agent. The host services
|
|
136171
|
+
* its calls through `ScreamHarnessOptions.onToolCall`; without that handler
|
|
136172
|
+
* the call is reported as a tool error.
|
|
136173
|
+
*/
|
|
136174
|
+
async registerTool(payload) {
|
|
136175
|
+
this.ensureOpen();
|
|
136176
|
+
await this.rpc.registerTool({
|
|
136177
|
+
sessionId: this.id,
|
|
136178
|
+
...payload
|
|
136179
|
+
});
|
|
136180
|
+
}
|
|
136181
|
+
async unregisterTool(name) {
|
|
136182
|
+
this.ensureOpen();
|
|
136183
|
+
const toolName = normalizeRequiredString(name, "Tool name cannot be empty", ErrorCodes.REQUEST_INVALID);
|
|
136184
|
+
await this.rpc.unregisterTool({
|
|
136185
|
+
sessionId: this.id,
|
|
136186
|
+
name: toolName
|
|
136187
|
+
});
|
|
136188
|
+
}
|
|
136189
|
+
/**
|
|
136190
|
+
* Replace the agent's active tool set. Names are exact builtin/user tool
|
|
136191
|
+
* names plus optional MCP glob patterns (`mcp__*`).
|
|
136192
|
+
*/
|
|
136193
|
+
async setActiveTools(names) {
|
|
136194
|
+
this.ensureOpen();
|
|
136195
|
+
await this.rpc.setActiveTools({
|
|
136196
|
+
sessionId: this.id,
|
|
136197
|
+
names
|
|
136198
|
+
});
|
|
136199
|
+
}
|
|
132457
136200
|
async setModel(model) {
|
|
132458
136201
|
this.ensureOpen();
|
|
132459
136202
|
const normalized = normalizeRequiredString(model, "Session model cannot be empty", ErrorCodes.SESSION_MODEL_EMPTY);
|
|
@@ -132985,7 +136728,8 @@ var ScreamHarness = class {
|
|
|
132985
136728
|
configPath: this.configPath,
|
|
132986
136729
|
identity: this.identity,
|
|
132987
136730
|
resolveOAuthTokenProvider: this.auth.resolveOAuthTokenProvider,
|
|
132988
|
-
skillDirs: options.skillDirs
|
|
136731
|
+
skillDirs: options.skillDirs,
|
|
136732
|
+
onToolCall: options.onToolCall
|
|
132989
136733
|
});
|
|
132990
136734
|
}
|
|
132991
136735
|
configureLogging() {
|
|
@@ -137586,8 +141330,8 @@ function lerpGradient(t) {
|
|
|
137586
141330
|
*
|
|
137587
141331
|
* `lerpGradient` above walks a fixed brand cycle that never touches red. This
|
|
137588
141332
|
* takes both endpoints instead, so callers can ramp between semantic tokens:
|
|
137589
|
-
* the status-bar wave walks green →
|
|
137590
|
-
* dim grey toward the theme accent.
|
|
141333
|
+
* the status-bar wave walks green → yellow → deep red, and the speed badge
|
|
141334
|
+
* fades a dim grey toward the theme accent.
|
|
137591
141335
|
*/
|
|
137592
141336
|
function lerpHex(from, to, t) {
|
|
137593
141337
|
const k = t < 0 ? 0 : Math.min(1, t);
|
|
@@ -137845,6 +141589,7 @@ function buildStatusLine(streamingPhase, streamingStartTime, statusFlags) {
|
|
|
137845
141589
|
});
|
|
137846
141590
|
let label;
|
|
137847
141591
|
if (streamingPhase === "tool") label = t("status.tool");
|
|
141592
|
+
else if (streamingPhase === "tool-args") label = t("status.calling_tool");
|
|
137848
141593
|
else if (streamingPhase === "waiting") label = t("status.waiting");
|
|
137849
141594
|
else if (streamingPhase === "thinking") label = t("status.thinking");
|
|
137850
141595
|
else if (streamingPhase === "composing") label = t("status.composing");
|
|
@@ -137999,7 +141744,7 @@ var FooterComponent = class {
|
|
|
137999
141744
|
}
|
|
138000
141745
|
const model = shortenModel(modelDisplayName(state));
|
|
138001
141746
|
if (model) {
|
|
138002
|
-
if (state.streamingPhase === "thinking" || state.streamingPhase === "waiting" || state.streamingPhase === "composing") left.push(shimmerText(model, colors));
|
|
141747
|
+
if (state.streamingPhase === "thinking" || state.streamingPhase === "waiting" || state.streamingPhase === "composing" || state.streamingPhase === "tool-args") left.push(shimmerText(model, colors));
|
|
138003
141748
|
else if (state.streamingPhase === "tool") left.push(shimmerTextWithPalette(model, {
|
|
138004
141749
|
low: colors.textDim,
|
|
138005
141750
|
mid: colors.textMuted,
|
|
@@ -138330,6 +142075,8 @@ const darkColors = {
|
|
|
138330
142075
|
success: dark.yellowGreen,
|
|
138331
142076
|
warning: dark.amber400,
|
|
138332
142077
|
error: dark.red,
|
|
142078
|
+
waveMid: "#FFD400",
|
|
142079
|
+
wavePeak: "#D32F2F",
|
|
138333
142080
|
diffAdded: dark.yellowGreen,
|
|
138334
142081
|
diffRemoved: dark.red,
|
|
138335
142082
|
diffAddedStrong: dark.yellowGreenLight,
|
|
@@ -138370,6 +142117,8 @@ const lightColors = {
|
|
|
138370
142117
|
success: light.yellowGreen700,
|
|
138371
142118
|
warning: light.amber800,
|
|
138372
142119
|
error: light.red,
|
|
142120
|
+
waveMid: "#A88400",
|
|
142121
|
+
wavePeak: "#8B0000",
|
|
138373
142122
|
diffAdded: light.yellowGreen700,
|
|
138374
142123
|
diffRemoved: light.red,
|
|
138375
142124
|
diffAddedStrong: light.yellowGreen700,
|
|
@@ -140819,7 +144568,7 @@ async function guidedGoalSetup(host) {
|
|
|
140819
144568
|
host.showNotice(t("goal.storm_breaker"), t("goal.conflict_loop"));
|
|
140820
144569
|
return;
|
|
140821
144570
|
}
|
|
140822
|
-
const { TextInputDialogComponent } = await import("./text-input-dialog-
|
|
144571
|
+
const { TextInputDialogComponent } = await import("./text-input-dialog-BmdLwvYc.mjs");
|
|
140823
144572
|
const initialDesc = await promptText(host, TextInputDialogComponent, {
|
|
140824
144573
|
title: t("goal.setup_title_initial"),
|
|
140825
144574
|
subtitle: t("goal.setup_desc_hint"),
|
|
@@ -140840,7 +144589,7 @@ async function guidedGoalSetup(host) {
|
|
|
140840
144589
|
await showGoalConfigWizard(host, session, confirmed.trim() || objective, false);
|
|
140841
144590
|
}
|
|
140842
144591
|
async function showGoalConfigWizard(host, session, objective, replace) {
|
|
140843
|
-
const { TextInputDialogComponent } = await import("./text-input-dialog-
|
|
144592
|
+
const { TextInputDialogComponent } = await import("./text-input-dialog-BmdLwvYc.mjs");
|
|
140844
144593
|
const turnInput = await promptNumber(host, TextInputDialogComponent, {
|
|
140845
144594
|
title: t("goal.wizard_title", { objective }),
|
|
140846
144595
|
subtitle: t("goal.budget_turns_hint"),
|
|
@@ -153187,4 +156936,4 @@ async function handleBuiltInSlashCommand(host, name, args) {
|
|
|
153187
156936
|
}
|
|
153188
156937
|
}
|
|
153189
156938
|
//#endregion
|
|
153190
|
-
export {
|
|
156939
|
+
export { handleExportDebugZipCommand as $, normalizeWorkDir as $n, lerpHex as $t, renderDiffLinesClustered as A, embeddingProgressLabel as An, toggleEmptySessionHint as At, AssistantMessageComponent as B, detectShellEnvironment as Bn, OSC11_QUERY as Bt, readJsonlFile as C, getLlmNotSetMessage as Cn, showStatusReport as Ct, UserMessageComponent as D, sortSlashCommands as Dn, createMarkdownTheme as Dt, handleRevokeCommand as E, BUILTIN_SLASH_COMMANDS as En, createEditorTheme as Et, NoticeMessageComponent as F, PIXEL_PULSE_FRAMES as Fn, parseOsc11BackgroundTheme as Ft, BackgroundAgentStatusComponent as G, CLI_COMMAND_NAME as Gn, TERMINAL_FOCUS_IN as Gt, charsForTokenBudget as H, getInputHistoryFile as Hn, OSC11_RESPONSE_PREFIX as Ht, StatusMessageComponent as I, PULSE_WAVE_FRAMES as In, DISABLE_TERMINAL_FOCUS_REPORTING as It, BREATHE_CYCLE_MS as J, PRODUCT_NAME as Jn, TERMINAL_THEME_LIGHT as Jt, AgentGroupComponent as K, CLI_UI_MODE as Kn, TERMINAL_FOCUS_OUT as Kt, replaceTabs as L, TuiConfigParseError as Ln, DISABLE_TERMINAL_THEME_REPORTING as Lt, langFromPath as M, getKnowledgeStore as Mn, contrastTextHex as Mt, CachedContainer as N, hasEmbeddingModelCache as Nn, getColorPalette as Nt, ToolCallComponent as O, isExperimentalFlagEnabled as On, isEmptySessionHintDismissed as Ot, ThinkingComponent as P, startManualEmbeddingDownload as Pn, detectTerminalTheme as Pt, clearGoalState as Q, ScreamHarness as Qn, lerpGradient as Qt, sanitizeShellOutput as R, loadTuiConfig as Rn, ENABLE_TERMINAL_FOCUS_REPORTING as Rt, appendJsonlLine as S, getCtrlDHint as Sn, clearInfoPanelState as St, getDaemonInstructions as T, buildSkillSlashCommands as Tn, resolveThemeSync as Tt, estimateTokens as U, getLogDir as Un, OSC11_RESPONSE_PREFIX_NO_ESC as Ut, ActivityGroupComponent as V, getDataDir as Vn, OSC11_RESPONSE as Vt, getSharedSpeedTracker as W, detectInstallSource as Wn, QUERY_TERMINAL_THEME as Wt, resetBreathingClock as X, fetchCatalog as Xn, isStreaming as Xt, getBreathingFrame as Y, DEFAULT_CATALOG_URL as Yn, isBusy as Yt, CompactionComponent as Z, saveCatalogCache as Zn, FooterComponent as Zt, handleMcpCommand as _, EXIT_CONFIRM_WINDOW_MS as _n, showModelPicker as _t, writeTextClipboard as a, printableChar as an, resolveGlobalLogPath as ar, changeThinkingLevel as at, refreshUpdateCache as b, TIP_ROTATION_INTERVAL_MS as bn, refreshProviderBalance as bt, handleSkillCommand as c, appendStreamingArgsPreview as cn, SCREAM_ERROR_INFO as cr, handleBotCommand as ct, isExpandable as d, isTodoItemShape as dn, handleFusionPlanCommand as dt, SubagentSlots as en, MCP_OAUTH_AUTHORIZATION_URL_TOOL_UPDATE as er, handleExportMdCommand as et, isPlanExpandable as f, parseStreamingArgs as fn, handleModelCommand as ft, handleChannelCommand as g, EMPTY_SESSION_HINT_URL as gn, handleYoloCommand as gt, handleMemoryCommand as h, truncateErrorMessage as hn, handleWolfpackCommand as ht, openUrl as i, handleLogoutCommand as in, log as ir, toTerminalHyperlink as it, highlightLines as j, embeddingFailureDetail as jn, createThemeStyles as jt, renderDiffLines as k, setExperimentalFlags as kn, isTurnElapsedEnabled as kt, disposeChildren as l, argsRecord as ln, handleCompactCommand as lt, formatMemoryMemoForInjection as m, stringValue as mn, handleThemeCommand as mt, clearEvalPanelState as n, handleSearchCommand as nn, resolveScreamHome as nr, handleInitCommand as nt, syncDiagramCapabilityPrompt as o, STATUS_BULLET as on, isScreamError as or, getModelCycleLevel as ot, MoonLoader as p, serializeToolResultOutput as pn, handlePlanCommand as pt, WelcomeComponent as q, CLI_USER_AGENT_PRODUCT as qn, TERMINAL_THEME_DARK as qt, handleGrantsCommand as r, handleConnectCommand as rn, flushDiagnosticLogs as rr, handleTitleCommand as rt, handleExtensionCommand as s, appendSessionHeaderHint as sn, isOrphanedToolCallError as sr, handleAutoCommand as st, dispatchInput as t, handleTraceCommand as tn, isRealUserPrompt as tr, handleForkCommand as tt, hasDispose as u, formatErrorMessage as un, handleEditorCommand as ut, handleUpdateCommand as v, MAIN_AGENT_ID$1 as vn, showPermissionPicker as vt, handleCcCommand as w, getNoActiveSessionMessage as wn, showUsage as wt, readUpdateCache as x, getCtrlCHint as xn, supportsBalance as xt, selectUpdateTarget as y, SESSION_TIPS as yn, showSettingsSelector as yt, SkillActivationComponent as z, saveTuiConfig as zn, ENABLE_TERMINAL_THEME_REPORTING as zt };
|