bb-browser 0.2.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -130
- package/README.zh-CN.md +169 -0
- package/dist/chunk-CWLDHQGR.js +60 -0
- package/dist/chunk-CWLDHQGR.js.map +1 -0
- package/dist/cli.js +4236 -25
- package/dist/cli.js.map +1 -1
- package/dist/daemon.js +1 -1
- package/dist/mcp.js +21240 -0
- package/dist/mcp.js.map +1 -0
- package/extension/dist/background.js +700 -543
- package/extension/dist/background.js.map +1 -1
- package/extension/dist/manifest.json +1 -1
- package/package.json +12 -7
- package/dist/chunk-TUO443YI.js +0 -21
- package/dist/chunk-TUO443YI.js.map +0 -1
package/dist/cli.js
CHANGED
|
@@ -2,8 +2,3627 @@
|
|
|
2
2
|
import {
|
|
3
3
|
COMMAND_TIMEOUT,
|
|
4
4
|
DAEMON_BASE_URL,
|
|
5
|
+
__commonJS,
|
|
6
|
+
__require,
|
|
7
|
+
__toESM,
|
|
5
8
|
generateId
|
|
6
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-CWLDHQGR.js";
|
|
10
|
+
|
|
11
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/constants.js
|
|
12
|
+
var require_constants = __commonJS({
|
|
13
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/constants.js"(exports, module) {
|
|
14
|
+
"use strict";
|
|
15
|
+
var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
16
|
+
var hasBlob = typeof Blob !== "undefined";
|
|
17
|
+
if (hasBlob) BINARY_TYPES.push("blob");
|
|
18
|
+
module.exports = {
|
|
19
|
+
BINARY_TYPES,
|
|
20
|
+
CLOSE_TIMEOUT: 3e4,
|
|
21
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
22
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
23
|
+
hasBlob,
|
|
24
|
+
kForOnEventAttribute: /* @__PURE__ */ Symbol("kIsForOnEventAttribute"),
|
|
25
|
+
kListener: /* @__PURE__ */ Symbol("kListener"),
|
|
26
|
+
kStatusCode: /* @__PURE__ */ Symbol("status-code"),
|
|
27
|
+
kWebSocket: /* @__PURE__ */ Symbol("websocket"),
|
|
28
|
+
NOOP: () => {
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/buffer-util.js
|
|
35
|
+
var require_buffer_util = __commonJS({
|
|
36
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/buffer-util.js"(exports, module) {
|
|
37
|
+
"use strict";
|
|
38
|
+
var { EMPTY_BUFFER } = require_constants();
|
|
39
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
40
|
+
function concat(list, totalLength) {
|
|
41
|
+
if (list.length === 0) return EMPTY_BUFFER;
|
|
42
|
+
if (list.length === 1) return list[0];
|
|
43
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
44
|
+
let offset = 0;
|
|
45
|
+
for (let i = 0; i < list.length; i++) {
|
|
46
|
+
const buf = list[i];
|
|
47
|
+
target.set(buf, offset);
|
|
48
|
+
offset += buf.length;
|
|
49
|
+
}
|
|
50
|
+
if (offset < totalLength) {
|
|
51
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
52
|
+
}
|
|
53
|
+
return target;
|
|
54
|
+
}
|
|
55
|
+
function _mask(source, mask, output, offset, length) {
|
|
56
|
+
for (let i = 0; i < length; i++) {
|
|
57
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function _unmask(buffer, mask) {
|
|
61
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
62
|
+
buffer[i] ^= mask[i & 3];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function toArrayBuffer(buf) {
|
|
66
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
67
|
+
return buf.buffer;
|
|
68
|
+
}
|
|
69
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
70
|
+
}
|
|
71
|
+
function toBuffer(data) {
|
|
72
|
+
toBuffer.readOnly = true;
|
|
73
|
+
if (Buffer.isBuffer(data)) return data;
|
|
74
|
+
let buf;
|
|
75
|
+
if (data instanceof ArrayBuffer) {
|
|
76
|
+
buf = new FastBuffer(data);
|
|
77
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
78
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
79
|
+
} else {
|
|
80
|
+
buf = Buffer.from(data);
|
|
81
|
+
toBuffer.readOnly = false;
|
|
82
|
+
}
|
|
83
|
+
return buf;
|
|
84
|
+
}
|
|
85
|
+
module.exports = {
|
|
86
|
+
concat,
|
|
87
|
+
mask: _mask,
|
|
88
|
+
toArrayBuffer,
|
|
89
|
+
toBuffer,
|
|
90
|
+
unmask: _unmask
|
|
91
|
+
};
|
|
92
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
93
|
+
try {
|
|
94
|
+
const bufferUtil = __require("bufferutil");
|
|
95
|
+
module.exports.mask = function(source, mask, output, offset, length) {
|
|
96
|
+
if (length < 48) _mask(source, mask, output, offset, length);
|
|
97
|
+
else bufferUtil.mask(source, mask, output, offset, length);
|
|
98
|
+
};
|
|
99
|
+
module.exports.unmask = function(buffer, mask) {
|
|
100
|
+
if (buffer.length < 32) _unmask(buffer, mask);
|
|
101
|
+
else bufferUtil.unmask(buffer, mask);
|
|
102
|
+
};
|
|
103
|
+
} catch (e) {
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/limiter.js
|
|
110
|
+
var require_limiter = __commonJS({
|
|
111
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/limiter.js"(exports, module) {
|
|
112
|
+
"use strict";
|
|
113
|
+
var kDone = /* @__PURE__ */ Symbol("kDone");
|
|
114
|
+
var kRun = /* @__PURE__ */ Symbol("kRun");
|
|
115
|
+
var Limiter = class {
|
|
116
|
+
/**
|
|
117
|
+
* Creates a new `Limiter`.
|
|
118
|
+
*
|
|
119
|
+
* @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
|
|
120
|
+
* to run concurrently
|
|
121
|
+
*/
|
|
122
|
+
constructor(concurrency) {
|
|
123
|
+
this[kDone] = () => {
|
|
124
|
+
this.pending--;
|
|
125
|
+
this[kRun]();
|
|
126
|
+
};
|
|
127
|
+
this.concurrency = concurrency || Infinity;
|
|
128
|
+
this.jobs = [];
|
|
129
|
+
this.pending = 0;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Adds a job to the queue.
|
|
133
|
+
*
|
|
134
|
+
* @param {Function} job The job to run
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
add(job) {
|
|
138
|
+
this.jobs.push(job);
|
|
139
|
+
this[kRun]();
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Removes a job from the queue and runs it if possible.
|
|
143
|
+
*
|
|
144
|
+
* @private
|
|
145
|
+
*/
|
|
146
|
+
[kRun]() {
|
|
147
|
+
if (this.pending === this.concurrency) return;
|
|
148
|
+
if (this.jobs.length) {
|
|
149
|
+
const job = this.jobs.shift();
|
|
150
|
+
this.pending++;
|
|
151
|
+
job(this[kDone]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
module.exports = Limiter;
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/permessage-deflate.js
|
|
160
|
+
var require_permessage_deflate = __commonJS({
|
|
161
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/permessage-deflate.js"(exports, module) {
|
|
162
|
+
"use strict";
|
|
163
|
+
var zlib = __require("zlib");
|
|
164
|
+
var bufferUtil = require_buffer_util();
|
|
165
|
+
var Limiter = require_limiter();
|
|
166
|
+
var { kStatusCode } = require_constants();
|
|
167
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
168
|
+
var TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
169
|
+
var kPerMessageDeflate = /* @__PURE__ */ Symbol("permessage-deflate");
|
|
170
|
+
var kTotalLength = /* @__PURE__ */ Symbol("total-length");
|
|
171
|
+
var kCallback = /* @__PURE__ */ Symbol("callback");
|
|
172
|
+
var kBuffers = /* @__PURE__ */ Symbol("buffers");
|
|
173
|
+
var kError = /* @__PURE__ */ Symbol("error");
|
|
174
|
+
var zlibLimiter;
|
|
175
|
+
var PerMessageDeflate = class {
|
|
176
|
+
/**
|
|
177
|
+
* Creates a PerMessageDeflate instance.
|
|
178
|
+
*
|
|
179
|
+
* @param {Object} [options] Configuration options
|
|
180
|
+
* @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
|
|
181
|
+
* for, or request, a custom client window size
|
|
182
|
+
* @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
|
|
183
|
+
* acknowledge disabling of client context takeover
|
|
184
|
+
* @param {Number} [options.concurrencyLimit=10] The number of concurrent
|
|
185
|
+
* calls to zlib
|
|
186
|
+
* @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
|
|
187
|
+
* use of a custom server window size
|
|
188
|
+
* @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
|
|
189
|
+
* disabling of server context takeover
|
|
190
|
+
* @param {Number} [options.threshold=1024] Size (in bytes) below which
|
|
191
|
+
* messages should not be compressed if context takeover is disabled
|
|
192
|
+
* @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
|
|
193
|
+
* deflate
|
|
194
|
+
* @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
|
|
195
|
+
* inflate
|
|
196
|
+
* @param {Boolean} [isServer=false] Create the instance in either server or
|
|
197
|
+
* client mode
|
|
198
|
+
* @param {Number} [maxPayload=0] The maximum allowed message length
|
|
199
|
+
*/
|
|
200
|
+
constructor(options, isServer, maxPayload) {
|
|
201
|
+
this._maxPayload = maxPayload | 0;
|
|
202
|
+
this._options = options || {};
|
|
203
|
+
this._threshold = this._options.threshold !== void 0 ? this._options.threshold : 1024;
|
|
204
|
+
this._isServer = !!isServer;
|
|
205
|
+
this._deflate = null;
|
|
206
|
+
this._inflate = null;
|
|
207
|
+
this.params = null;
|
|
208
|
+
if (!zlibLimiter) {
|
|
209
|
+
const concurrency = this._options.concurrencyLimit !== void 0 ? this._options.concurrencyLimit : 10;
|
|
210
|
+
zlibLimiter = new Limiter(concurrency);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* @type {String}
|
|
215
|
+
*/
|
|
216
|
+
static get extensionName() {
|
|
217
|
+
return "permessage-deflate";
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Create an extension negotiation offer.
|
|
221
|
+
*
|
|
222
|
+
* @return {Object} Extension parameters
|
|
223
|
+
* @public
|
|
224
|
+
*/
|
|
225
|
+
offer() {
|
|
226
|
+
const params = {};
|
|
227
|
+
if (this._options.serverNoContextTakeover) {
|
|
228
|
+
params.server_no_context_takeover = true;
|
|
229
|
+
}
|
|
230
|
+
if (this._options.clientNoContextTakeover) {
|
|
231
|
+
params.client_no_context_takeover = true;
|
|
232
|
+
}
|
|
233
|
+
if (this._options.serverMaxWindowBits) {
|
|
234
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
235
|
+
}
|
|
236
|
+
if (this._options.clientMaxWindowBits) {
|
|
237
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
238
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
239
|
+
params.client_max_window_bits = true;
|
|
240
|
+
}
|
|
241
|
+
return params;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Accept an extension negotiation offer/response.
|
|
245
|
+
*
|
|
246
|
+
* @param {Array} configurations The extension negotiation offers/reponse
|
|
247
|
+
* @return {Object} Accepted configuration
|
|
248
|
+
* @public
|
|
249
|
+
*/
|
|
250
|
+
accept(configurations) {
|
|
251
|
+
configurations = this.normalizeParams(configurations);
|
|
252
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
253
|
+
return this.params;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Releases all resources used by the extension.
|
|
257
|
+
*
|
|
258
|
+
* @public
|
|
259
|
+
*/
|
|
260
|
+
cleanup() {
|
|
261
|
+
if (this._inflate) {
|
|
262
|
+
this._inflate.close();
|
|
263
|
+
this._inflate = null;
|
|
264
|
+
}
|
|
265
|
+
if (this._deflate) {
|
|
266
|
+
const callback = this._deflate[kCallback];
|
|
267
|
+
this._deflate.close();
|
|
268
|
+
this._deflate = null;
|
|
269
|
+
if (callback) {
|
|
270
|
+
callback(
|
|
271
|
+
new Error(
|
|
272
|
+
"The deflate stream was closed while data was being processed"
|
|
273
|
+
)
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Accept an extension negotiation offer.
|
|
280
|
+
*
|
|
281
|
+
* @param {Array} offers The extension negotiation offers
|
|
282
|
+
* @return {Object} Accepted configuration
|
|
283
|
+
* @private
|
|
284
|
+
*/
|
|
285
|
+
acceptAsServer(offers) {
|
|
286
|
+
const opts = this._options;
|
|
287
|
+
const accepted = offers.find((params) => {
|
|
288
|
+
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) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
return true;
|
|
292
|
+
});
|
|
293
|
+
if (!accepted) {
|
|
294
|
+
throw new Error("None of the extension offers can be accepted");
|
|
295
|
+
}
|
|
296
|
+
if (opts.serverNoContextTakeover) {
|
|
297
|
+
accepted.server_no_context_takeover = true;
|
|
298
|
+
}
|
|
299
|
+
if (opts.clientNoContextTakeover) {
|
|
300
|
+
accepted.client_no_context_takeover = true;
|
|
301
|
+
}
|
|
302
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
303
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
304
|
+
}
|
|
305
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
306
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
307
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
308
|
+
delete accepted.client_max_window_bits;
|
|
309
|
+
}
|
|
310
|
+
return accepted;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Accept the extension negotiation response.
|
|
314
|
+
*
|
|
315
|
+
* @param {Array} response The extension negotiation response
|
|
316
|
+
* @return {Object} Accepted configuration
|
|
317
|
+
* @private
|
|
318
|
+
*/
|
|
319
|
+
acceptAsClient(response) {
|
|
320
|
+
const params = response[0];
|
|
321
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
322
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
323
|
+
}
|
|
324
|
+
if (!params.client_max_window_bits) {
|
|
325
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
326
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
327
|
+
}
|
|
328
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
329
|
+
throw new Error(
|
|
330
|
+
'Unexpected or invalid parameter "client_max_window_bits"'
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
return params;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Normalize parameters.
|
|
337
|
+
*
|
|
338
|
+
* @param {Array} configurations The extension negotiation offers/reponse
|
|
339
|
+
* @return {Array} The offers/response with normalized parameters
|
|
340
|
+
* @private
|
|
341
|
+
*/
|
|
342
|
+
normalizeParams(configurations) {
|
|
343
|
+
configurations.forEach((params) => {
|
|
344
|
+
Object.keys(params).forEach((key) => {
|
|
345
|
+
let value = params[key];
|
|
346
|
+
if (value.length > 1) {
|
|
347
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
348
|
+
}
|
|
349
|
+
value = value[0];
|
|
350
|
+
if (key === "client_max_window_bits") {
|
|
351
|
+
if (value !== true) {
|
|
352
|
+
const num = +value;
|
|
353
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
354
|
+
throw new TypeError(
|
|
355
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
value = num;
|
|
359
|
+
} else if (!this._isServer) {
|
|
360
|
+
throw new TypeError(
|
|
361
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
} else if (key === "server_max_window_bits") {
|
|
365
|
+
const num = +value;
|
|
366
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
367
|
+
throw new TypeError(
|
|
368
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
value = num;
|
|
372
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
373
|
+
if (value !== true) {
|
|
374
|
+
throw new TypeError(
|
|
375
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
} else {
|
|
379
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
380
|
+
}
|
|
381
|
+
params[key] = value;
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
return configurations;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Decompress data. Concurrency limited.
|
|
388
|
+
*
|
|
389
|
+
* @param {Buffer} data Compressed data
|
|
390
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
391
|
+
* @param {Function} callback Callback
|
|
392
|
+
* @public
|
|
393
|
+
*/
|
|
394
|
+
decompress(data, fin, callback) {
|
|
395
|
+
zlibLimiter.add((done) => {
|
|
396
|
+
this._decompress(data, fin, (err, result) => {
|
|
397
|
+
done();
|
|
398
|
+
callback(err, result);
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Compress data. Concurrency limited.
|
|
404
|
+
*
|
|
405
|
+
* @param {(Buffer|String)} data Data to compress
|
|
406
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
407
|
+
* @param {Function} callback Callback
|
|
408
|
+
* @public
|
|
409
|
+
*/
|
|
410
|
+
compress(data, fin, callback) {
|
|
411
|
+
zlibLimiter.add((done) => {
|
|
412
|
+
this._compress(data, fin, (err, result) => {
|
|
413
|
+
done();
|
|
414
|
+
callback(err, result);
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Decompress data.
|
|
420
|
+
*
|
|
421
|
+
* @param {Buffer} data Compressed data
|
|
422
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
423
|
+
* @param {Function} callback Callback
|
|
424
|
+
* @private
|
|
425
|
+
*/
|
|
426
|
+
_decompress(data, fin, callback) {
|
|
427
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
428
|
+
if (!this._inflate) {
|
|
429
|
+
const key = `${endpoint}_max_window_bits`;
|
|
430
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
431
|
+
this._inflate = zlib.createInflateRaw({
|
|
432
|
+
...this._options.zlibInflateOptions,
|
|
433
|
+
windowBits
|
|
434
|
+
});
|
|
435
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
436
|
+
this._inflate[kTotalLength] = 0;
|
|
437
|
+
this._inflate[kBuffers] = [];
|
|
438
|
+
this._inflate.on("error", inflateOnError);
|
|
439
|
+
this._inflate.on("data", inflateOnData);
|
|
440
|
+
}
|
|
441
|
+
this._inflate[kCallback] = callback;
|
|
442
|
+
this._inflate.write(data);
|
|
443
|
+
if (fin) this._inflate.write(TRAILER);
|
|
444
|
+
this._inflate.flush(() => {
|
|
445
|
+
const err = this._inflate[kError];
|
|
446
|
+
if (err) {
|
|
447
|
+
this._inflate.close();
|
|
448
|
+
this._inflate = null;
|
|
449
|
+
callback(err);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const data2 = bufferUtil.concat(
|
|
453
|
+
this._inflate[kBuffers],
|
|
454
|
+
this._inflate[kTotalLength]
|
|
455
|
+
);
|
|
456
|
+
if (this._inflate._readableState.endEmitted) {
|
|
457
|
+
this._inflate.close();
|
|
458
|
+
this._inflate = null;
|
|
459
|
+
} else {
|
|
460
|
+
this._inflate[kTotalLength] = 0;
|
|
461
|
+
this._inflate[kBuffers] = [];
|
|
462
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
463
|
+
this._inflate.reset();
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
callback(null, data2);
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Compress data.
|
|
471
|
+
*
|
|
472
|
+
* @param {(Buffer|String)} data Data to compress
|
|
473
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
474
|
+
* @param {Function} callback Callback
|
|
475
|
+
* @private
|
|
476
|
+
*/
|
|
477
|
+
_compress(data, fin, callback) {
|
|
478
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
479
|
+
if (!this._deflate) {
|
|
480
|
+
const key = `${endpoint}_max_window_bits`;
|
|
481
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
482
|
+
this._deflate = zlib.createDeflateRaw({
|
|
483
|
+
...this._options.zlibDeflateOptions,
|
|
484
|
+
windowBits
|
|
485
|
+
});
|
|
486
|
+
this._deflate[kTotalLength] = 0;
|
|
487
|
+
this._deflate[kBuffers] = [];
|
|
488
|
+
this._deflate.on("data", deflateOnData);
|
|
489
|
+
}
|
|
490
|
+
this._deflate[kCallback] = callback;
|
|
491
|
+
this._deflate.write(data);
|
|
492
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
493
|
+
if (!this._deflate) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
let data2 = bufferUtil.concat(
|
|
497
|
+
this._deflate[kBuffers],
|
|
498
|
+
this._deflate[kTotalLength]
|
|
499
|
+
);
|
|
500
|
+
if (fin) {
|
|
501
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
502
|
+
}
|
|
503
|
+
this._deflate[kCallback] = null;
|
|
504
|
+
this._deflate[kTotalLength] = 0;
|
|
505
|
+
this._deflate[kBuffers] = [];
|
|
506
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
507
|
+
this._deflate.reset();
|
|
508
|
+
}
|
|
509
|
+
callback(null, data2);
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
module.exports = PerMessageDeflate;
|
|
514
|
+
function deflateOnData(chunk) {
|
|
515
|
+
this[kBuffers].push(chunk);
|
|
516
|
+
this[kTotalLength] += chunk.length;
|
|
517
|
+
}
|
|
518
|
+
function inflateOnData(chunk) {
|
|
519
|
+
this[kTotalLength] += chunk.length;
|
|
520
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
521
|
+
this[kBuffers].push(chunk);
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
525
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
526
|
+
this[kError][kStatusCode] = 1009;
|
|
527
|
+
this.removeListener("data", inflateOnData);
|
|
528
|
+
this.reset();
|
|
529
|
+
}
|
|
530
|
+
function inflateOnError(err) {
|
|
531
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
532
|
+
if (this[kError]) {
|
|
533
|
+
this[kCallback](this[kError]);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
err[kStatusCode] = 1007;
|
|
537
|
+
this[kCallback](err);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/validation.js
|
|
543
|
+
var require_validation = __commonJS({
|
|
544
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/validation.js"(exports, module) {
|
|
545
|
+
"use strict";
|
|
546
|
+
var { isUtf8 } = __require("buffer");
|
|
547
|
+
var { hasBlob } = require_constants();
|
|
548
|
+
var tokenChars = [
|
|
549
|
+
0,
|
|
550
|
+
0,
|
|
551
|
+
0,
|
|
552
|
+
0,
|
|
553
|
+
0,
|
|
554
|
+
0,
|
|
555
|
+
0,
|
|
556
|
+
0,
|
|
557
|
+
0,
|
|
558
|
+
0,
|
|
559
|
+
0,
|
|
560
|
+
0,
|
|
561
|
+
0,
|
|
562
|
+
0,
|
|
563
|
+
0,
|
|
564
|
+
0,
|
|
565
|
+
// 0 - 15
|
|
566
|
+
0,
|
|
567
|
+
0,
|
|
568
|
+
0,
|
|
569
|
+
0,
|
|
570
|
+
0,
|
|
571
|
+
0,
|
|
572
|
+
0,
|
|
573
|
+
0,
|
|
574
|
+
0,
|
|
575
|
+
0,
|
|
576
|
+
0,
|
|
577
|
+
0,
|
|
578
|
+
0,
|
|
579
|
+
0,
|
|
580
|
+
0,
|
|
581
|
+
0,
|
|
582
|
+
// 16 - 31
|
|
583
|
+
0,
|
|
584
|
+
1,
|
|
585
|
+
0,
|
|
586
|
+
1,
|
|
587
|
+
1,
|
|
588
|
+
1,
|
|
589
|
+
1,
|
|
590
|
+
1,
|
|
591
|
+
0,
|
|
592
|
+
0,
|
|
593
|
+
1,
|
|
594
|
+
1,
|
|
595
|
+
0,
|
|
596
|
+
1,
|
|
597
|
+
1,
|
|
598
|
+
0,
|
|
599
|
+
// 32 - 47
|
|
600
|
+
1,
|
|
601
|
+
1,
|
|
602
|
+
1,
|
|
603
|
+
1,
|
|
604
|
+
1,
|
|
605
|
+
1,
|
|
606
|
+
1,
|
|
607
|
+
1,
|
|
608
|
+
1,
|
|
609
|
+
1,
|
|
610
|
+
0,
|
|
611
|
+
0,
|
|
612
|
+
0,
|
|
613
|
+
0,
|
|
614
|
+
0,
|
|
615
|
+
0,
|
|
616
|
+
// 48 - 63
|
|
617
|
+
0,
|
|
618
|
+
1,
|
|
619
|
+
1,
|
|
620
|
+
1,
|
|
621
|
+
1,
|
|
622
|
+
1,
|
|
623
|
+
1,
|
|
624
|
+
1,
|
|
625
|
+
1,
|
|
626
|
+
1,
|
|
627
|
+
1,
|
|
628
|
+
1,
|
|
629
|
+
1,
|
|
630
|
+
1,
|
|
631
|
+
1,
|
|
632
|
+
1,
|
|
633
|
+
// 64 - 79
|
|
634
|
+
1,
|
|
635
|
+
1,
|
|
636
|
+
1,
|
|
637
|
+
1,
|
|
638
|
+
1,
|
|
639
|
+
1,
|
|
640
|
+
1,
|
|
641
|
+
1,
|
|
642
|
+
1,
|
|
643
|
+
1,
|
|
644
|
+
1,
|
|
645
|
+
0,
|
|
646
|
+
0,
|
|
647
|
+
0,
|
|
648
|
+
1,
|
|
649
|
+
1,
|
|
650
|
+
// 80 - 95
|
|
651
|
+
1,
|
|
652
|
+
1,
|
|
653
|
+
1,
|
|
654
|
+
1,
|
|
655
|
+
1,
|
|
656
|
+
1,
|
|
657
|
+
1,
|
|
658
|
+
1,
|
|
659
|
+
1,
|
|
660
|
+
1,
|
|
661
|
+
1,
|
|
662
|
+
1,
|
|
663
|
+
1,
|
|
664
|
+
1,
|
|
665
|
+
1,
|
|
666
|
+
1,
|
|
667
|
+
// 96 - 111
|
|
668
|
+
1,
|
|
669
|
+
1,
|
|
670
|
+
1,
|
|
671
|
+
1,
|
|
672
|
+
1,
|
|
673
|
+
1,
|
|
674
|
+
1,
|
|
675
|
+
1,
|
|
676
|
+
1,
|
|
677
|
+
1,
|
|
678
|
+
1,
|
|
679
|
+
0,
|
|
680
|
+
1,
|
|
681
|
+
0,
|
|
682
|
+
1,
|
|
683
|
+
0
|
|
684
|
+
// 112 - 127
|
|
685
|
+
];
|
|
686
|
+
function isValidStatusCode(code) {
|
|
687
|
+
return code >= 1e3 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3e3 && code <= 4999;
|
|
688
|
+
}
|
|
689
|
+
function _isValidUTF8(buf) {
|
|
690
|
+
const len = buf.length;
|
|
691
|
+
let i = 0;
|
|
692
|
+
while (i < len) {
|
|
693
|
+
if ((buf[i] & 128) === 0) {
|
|
694
|
+
i++;
|
|
695
|
+
} else if ((buf[i] & 224) === 192) {
|
|
696
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) {
|
|
697
|
+
return false;
|
|
698
|
+
}
|
|
699
|
+
i += 2;
|
|
700
|
+
} else if ((buf[i] & 240) === 224) {
|
|
701
|
+
if (i + 2 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || buf[i] === 224 && (buf[i + 1] & 224) === 128 || // Overlong
|
|
702
|
+
buf[i] === 237 && (buf[i + 1] & 224) === 160) {
|
|
703
|
+
return false;
|
|
704
|
+
}
|
|
705
|
+
i += 3;
|
|
706
|
+
} else if ((buf[i] & 248) === 240) {
|
|
707
|
+
if (i + 3 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || (buf[i + 3] & 192) !== 128 || buf[i] === 240 && (buf[i + 1] & 240) === 128 || // Overlong
|
|
708
|
+
buf[i] === 244 && buf[i + 1] > 143 || buf[i] > 244) {
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
i += 4;
|
|
712
|
+
} else {
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return true;
|
|
717
|
+
}
|
|
718
|
+
function isBlob(value) {
|
|
719
|
+
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");
|
|
720
|
+
}
|
|
721
|
+
module.exports = {
|
|
722
|
+
isBlob,
|
|
723
|
+
isValidStatusCode,
|
|
724
|
+
isValidUTF8: _isValidUTF8,
|
|
725
|
+
tokenChars
|
|
726
|
+
};
|
|
727
|
+
if (isUtf8) {
|
|
728
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
729
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
730
|
+
};
|
|
731
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
732
|
+
try {
|
|
733
|
+
const isValidUTF8 = __require("utf-8-validate");
|
|
734
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
735
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
736
|
+
};
|
|
737
|
+
} catch (e) {
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/receiver.js
|
|
744
|
+
var require_receiver = __commonJS({
|
|
745
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/receiver.js"(exports, module) {
|
|
746
|
+
"use strict";
|
|
747
|
+
var { Writable } = __require("stream");
|
|
748
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
749
|
+
var {
|
|
750
|
+
BINARY_TYPES,
|
|
751
|
+
EMPTY_BUFFER,
|
|
752
|
+
kStatusCode,
|
|
753
|
+
kWebSocket
|
|
754
|
+
} = require_constants();
|
|
755
|
+
var { concat, toArrayBuffer, unmask } = require_buffer_util();
|
|
756
|
+
var { isValidStatusCode, isValidUTF8 } = require_validation();
|
|
757
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
758
|
+
var GET_INFO = 0;
|
|
759
|
+
var GET_PAYLOAD_LENGTH_16 = 1;
|
|
760
|
+
var GET_PAYLOAD_LENGTH_64 = 2;
|
|
761
|
+
var GET_MASK = 3;
|
|
762
|
+
var GET_DATA = 4;
|
|
763
|
+
var INFLATING = 5;
|
|
764
|
+
var DEFER_EVENT = 6;
|
|
765
|
+
var Receiver2 = class extends Writable {
|
|
766
|
+
/**
|
|
767
|
+
* Creates a Receiver instance.
|
|
768
|
+
*
|
|
769
|
+
* @param {Object} [options] Options object
|
|
770
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
|
|
771
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
772
|
+
* multiple times in the same tick
|
|
773
|
+
* @param {String} [options.binaryType=nodebuffer] The type for binary data
|
|
774
|
+
* @param {Object} [options.extensions] An object containing the negotiated
|
|
775
|
+
* extensions
|
|
776
|
+
* @param {Boolean} [options.isServer=false] Specifies whether to operate in
|
|
777
|
+
* client or server mode
|
|
778
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
|
779
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
780
|
+
* not to skip UTF-8 validation for text and close messages
|
|
781
|
+
*/
|
|
782
|
+
constructor(options = {}) {
|
|
783
|
+
super();
|
|
784
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== void 0 ? options.allowSynchronousEvents : true;
|
|
785
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
786
|
+
this._extensions = options.extensions || {};
|
|
787
|
+
this._isServer = !!options.isServer;
|
|
788
|
+
this._maxPayload = options.maxPayload | 0;
|
|
789
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
790
|
+
this[kWebSocket] = void 0;
|
|
791
|
+
this._bufferedBytes = 0;
|
|
792
|
+
this._buffers = [];
|
|
793
|
+
this._compressed = false;
|
|
794
|
+
this._payloadLength = 0;
|
|
795
|
+
this._mask = void 0;
|
|
796
|
+
this._fragmented = 0;
|
|
797
|
+
this._masked = false;
|
|
798
|
+
this._fin = false;
|
|
799
|
+
this._opcode = 0;
|
|
800
|
+
this._totalPayloadLength = 0;
|
|
801
|
+
this._messageLength = 0;
|
|
802
|
+
this._fragments = [];
|
|
803
|
+
this._errored = false;
|
|
804
|
+
this._loop = false;
|
|
805
|
+
this._state = GET_INFO;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Implements `Writable.prototype._write()`.
|
|
809
|
+
*
|
|
810
|
+
* @param {Buffer} chunk The chunk of data to write
|
|
811
|
+
* @param {String} encoding The character encoding of `chunk`
|
|
812
|
+
* @param {Function} cb Callback
|
|
813
|
+
* @private
|
|
814
|
+
*/
|
|
815
|
+
_write(chunk, encoding, cb) {
|
|
816
|
+
if (this._opcode === 8 && this._state == GET_INFO) return cb();
|
|
817
|
+
this._bufferedBytes += chunk.length;
|
|
818
|
+
this._buffers.push(chunk);
|
|
819
|
+
this.startLoop(cb);
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Consumes `n` bytes from the buffered data.
|
|
823
|
+
*
|
|
824
|
+
* @param {Number} n The number of bytes to consume
|
|
825
|
+
* @return {Buffer} The consumed bytes
|
|
826
|
+
* @private
|
|
827
|
+
*/
|
|
828
|
+
consume(n) {
|
|
829
|
+
this._bufferedBytes -= n;
|
|
830
|
+
if (n === this._buffers[0].length) return this._buffers.shift();
|
|
831
|
+
if (n < this._buffers[0].length) {
|
|
832
|
+
const buf = this._buffers[0];
|
|
833
|
+
this._buffers[0] = new FastBuffer(
|
|
834
|
+
buf.buffer,
|
|
835
|
+
buf.byteOffset + n,
|
|
836
|
+
buf.length - n
|
|
837
|
+
);
|
|
838
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
839
|
+
}
|
|
840
|
+
const dst = Buffer.allocUnsafe(n);
|
|
841
|
+
do {
|
|
842
|
+
const buf = this._buffers[0];
|
|
843
|
+
const offset = dst.length - n;
|
|
844
|
+
if (n >= buf.length) {
|
|
845
|
+
dst.set(this._buffers.shift(), offset);
|
|
846
|
+
} else {
|
|
847
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
848
|
+
this._buffers[0] = new FastBuffer(
|
|
849
|
+
buf.buffer,
|
|
850
|
+
buf.byteOffset + n,
|
|
851
|
+
buf.length - n
|
|
852
|
+
);
|
|
853
|
+
}
|
|
854
|
+
n -= buf.length;
|
|
855
|
+
} while (n > 0);
|
|
856
|
+
return dst;
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* Starts the parsing loop.
|
|
860
|
+
*
|
|
861
|
+
* @param {Function} cb Callback
|
|
862
|
+
* @private
|
|
863
|
+
*/
|
|
864
|
+
startLoop(cb) {
|
|
865
|
+
this._loop = true;
|
|
866
|
+
do {
|
|
867
|
+
switch (this._state) {
|
|
868
|
+
case GET_INFO:
|
|
869
|
+
this.getInfo(cb);
|
|
870
|
+
break;
|
|
871
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
872
|
+
this.getPayloadLength16(cb);
|
|
873
|
+
break;
|
|
874
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
875
|
+
this.getPayloadLength64(cb);
|
|
876
|
+
break;
|
|
877
|
+
case GET_MASK:
|
|
878
|
+
this.getMask();
|
|
879
|
+
break;
|
|
880
|
+
case GET_DATA:
|
|
881
|
+
this.getData(cb);
|
|
882
|
+
break;
|
|
883
|
+
case INFLATING:
|
|
884
|
+
case DEFER_EVENT:
|
|
885
|
+
this._loop = false;
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
} while (this._loop);
|
|
889
|
+
if (!this._errored) cb();
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Reads the first two bytes of a frame.
|
|
893
|
+
*
|
|
894
|
+
* @param {Function} cb Callback
|
|
895
|
+
* @private
|
|
896
|
+
*/
|
|
897
|
+
getInfo(cb) {
|
|
898
|
+
if (this._bufferedBytes < 2) {
|
|
899
|
+
this._loop = false;
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
const buf = this.consume(2);
|
|
903
|
+
if ((buf[0] & 48) !== 0) {
|
|
904
|
+
const error = this.createError(
|
|
905
|
+
RangeError,
|
|
906
|
+
"RSV2 and RSV3 must be clear",
|
|
907
|
+
true,
|
|
908
|
+
1002,
|
|
909
|
+
"WS_ERR_UNEXPECTED_RSV_2_3"
|
|
910
|
+
);
|
|
911
|
+
cb(error);
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
const compressed = (buf[0] & 64) === 64;
|
|
915
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
916
|
+
const error = this.createError(
|
|
917
|
+
RangeError,
|
|
918
|
+
"RSV1 must be clear",
|
|
919
|
+
true,
|
|
920
|
+
1002,
|
|
921
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
922
|
+
);
|
|
923
|
+
cb(error);
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
926
|
+
this._fin = (buf[0] & 128) === 128;
|
|
927
|
+
this._opcode = buf[0] & 15;
|
|
928
|
+
this._payloadLength = buf[1] & 127;
|
|
929
|
+
if (this._opcode === 0) {
|
|
930
|
+
if (compressed) {
|
|
931
|
+
const error = this.createError(
|
|
932
|
+
RangeError,
|
|
933
|
+
"RSV1 must be clear",
|
|
934
|
+
true,
|
|
935
|
+
1002,
|
|
936
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
937
|
+
);
|
|
938
|
+
cb(error);
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
if (!this._fragmented) {
|
|
942
|
+
const error = this.createError(
|
|
943
|
+
RangeError,
|
|
944
|
+
"invalid opcode 0",
|
|
945
|
+
true,
|
|
946
|
+
1002,
|
|
947
|
+
"WS_ERR_INVALID_OPCODE"
|
|
948
|
+
);
|
|
949
|
+
cb(error);
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
this._opcode = this._fragmented;
|
|
953
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
954
|
+
if (this._fragmented) {
|
|
955
|
+
const error = this.createError(
|
|
956
|
+
RangeError,
|
|
957
|
+
`invalid opcode ${this._opcode}`,
|
|
958
|
+
true,
|
|
959
|
+
1002,
|
|
960
|
+
"WS_ERR_INVALID_OPCODE"
|
|
961
|
+
);
|
|
962
|
+
cb(error);
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
this._compressed = compressed;
|
|
966
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
967
|
+
if (!this._fin) {
|
|
968
|
+
const error = this.createError(
|
|
969
|
+
RangeError,
|
|
970
|
+
"FIN must be set",
|
|
971
|
+
true,
|
|
972
|
+
1002,
|
|
973
|
+
"WS_ERR_EXPECTED_FIN"
|
|
974
|
+
);
|
|
975
|
+
cb(error);
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
if (compressed) {
|
|
979
|
+
const error = this.createError(
|
|
980
|
+
RangeError,
|
|
981
|
+
"RSV1 must be clear",
|
|
982
|
+
true,
|
|
983
|
+
1002,
|
|
984
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
985
|
+
);
|
|
986
|
+
cb(error);
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
990
|
+
const error = this.createError(
|
|
991
|
+
RangeError,
|
|
992
|
+
`invalid payload length ${this._payloadLength}`,
|
|
993
|
+
true,
|
|
994
|
+
1002,
|
|
995
|
+
"WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH"
|
|
996
|
+
);
|
|
997
|
+
cb(error);
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
} else {
|
|
1001
|
+
const error = this.createError(
|
|
1002
|
+
RangeError,
|
|
1003
|
+
`invalid opcode ${this._opcode}`,
|
|
1004
|
+
true,
|
|
1005
|
+
1002,
|
|
1006
|
+
"WS_ERR_INVALID_OPCODE"
|
|
1007
|
+
);
|
|
1008
|
+
cb(error);
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
|
|
1012
|
+
this._masked = (buf[1] & 128) === 128;
|
|
1013
|
+
if (this._isServer) {
|
|
1014
|
+
if (!this._masked) {
|
|
1015
|
+
const error = this.createError(
|
|
1016
|
+
RangeError,
|
|
1017
|
+
"MASK must be set",
|
|
1018
|
+
true,
|
|
1019
|
+
1002,
|
|
1020
|
+
"WS_ERR_EXPECTED_MASK"
|
|
1021
|
+
);
|
|
1022
|
+
cb(error);
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
} else if (this._masked) {
|
|
1026
|
+
const error = this.createError(
|
|
1027
|
+
RangeError,
|
|
1028
|
+
"MASK must be clear",
|
|
1029
|
+
true,
|
|
1030
|
+
1002,
|
|
1031
|
+
"WS_ERR_UNEXPECTED_MASK"
|
|
1032
|
+
);
|
|
1033
|
+
cb(error);
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
|
|
1037
|
+
else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
|
|
1038
|
+
else this.haveLength(cb);
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* Gets extended payload length (7+16).
|
|
1042
|
+
*
|
|
1043
|
+
* @param {Function} cb Callback
|
|
1044
|
+
* @private
|
|
1045
|
+
*/
|
|
1046
|
+
getPayloadLength16(cb) {
|
|
1047
|
+
if (this._bufferedBytes < 2) {
|
|
1048
|
+
this._loop = false;
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
1052
|
+
this.haveLength(cb);
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* Gets extended payload length (7+64).
|
|
1056
|
+
*
|
|
1057
|
+
* @param {Function} cb Callback
|
|
1058
|
+
* @private
|
|
1059
|
+
*/
|
|
1060
|
+
getPayloadLength64(cb) {
|
|
1061
|
+
if (this._bufferedBytes < 8) {
|
|
1062
|
+
this._loop = false;
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
const buf = this.consume(8);
|
|
1066
|
+
const num = buf.readUInt32BE(0);
|
|
1067
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
1068
|
+
const error = this.createError(
|
|
1069
|
+
RangeError,
|
|
1070
|
+
"Unsupported WebSocket frame: payload length > 2^53 - 1",
|
|
1071
|
+
false,
|
|
1072
|
+
1009,
|
|
1073
|
+
"WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH"
|
|
1074
|
+
);
|
|
1075
|
+
cb(error);
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
1079
|
+
this.haveLength(cb);
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Payload length has been read.
|
|
1083
|
+
*
|
|
1084
|
+
* @param {Function} cb Callback
|
|
1085
|
+
* @private
|
|
1086
|
+
*/
|
|
1087
|
+
haveLength(cb) {
|
|
1088
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
1089
|
+
this._totalPayloadLength += this._payloadLength;
|
|
1090
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
1091
|
+
const error = this.createError(
|
|
1092
|
+
RangeError,
|
|
1093
|
+
"Max payload size exceeded",
|
|
1094
|
+
false,
|
|
1095
|
+
1009,
|
|
1096
|
+
"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
|
|
1097
|
+
);
|
|
1098
|
+
cb(error);
|
|
1099
|
+
return;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
if (this._masked) this._state = GET_MASK;
|
|
1103
|
+
else this._state = GET_DATA;
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Reads mask bytes.
|
|
1107
|
+
*
|
|
1108
|
+
* @private
|
|
1109
|
+
*/
|
|
1110
|
+
getMask() {
|
|
1111
|
+
if (this._bufferedBytes < 4) {
|
|
1112
|
+
this._loop = false;
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
this._mask = this.consume(4);
|
|
1116
|
+
this._state = GET_DATA;
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Reads data bytes.
|
|
1120
|
+
*
|
|
1121
|
+
* @param {Function} cb Callback
|
|
1122
|
+
* @private
|
|
1123
|
+
*/
|
|
1124
|
+
getData(cb) {
|
|
1125
|
+
let data = EMPTY_BUFFER;
|
|
1126
|
+
if (this._payloadLength) {
|
|
1127
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
1128
|
+
this._loop = false;
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
data = this.consume(this._payloadLength);
|
|
1132
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
1133
|
+
unmask(data, this._mask);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
if (this._opcode > 7) {
|
|
1137
|
+
this.controlMessage(data, cb);
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
if (this._compressed) {
|
|
1141
|
+
this._state = INFLATING;
|
|
1142
|
+
this.decompress(data, cb);
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
if (data.length) {
|
|
1146
|
+
this._messageLength = this._totalPayloadLength;
|
|
1147
|
+
this._fragments.push(data);
|
|
1148
|
+
}
|
|
1149
|
+
this.dataMessage(cb);
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* Decompresses data.
|
|
1153
|
+
*
|
|
1154
|
+
* @param {Buffer} data Compressed data
|
|
1155
|
+
* @param {Function} cb Callback
|
|
1156
|
+
* @private
|
|
1157
|
+
*/
|
|
1158
|
+
decompress(data, cb) {
|
|
1159
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1160
|
+
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
|
1161
|
+
if (err) return cb(err);
|
|
1162
|
+
if (buf.length) {
|
|
1163
|
+
this._messageLength += buf.length;
|
|
1164
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
1165
|
+
const error = this.createError(
|
|
1166
|
+
RangeError,
|
|
1167
|
+
"Max payload size exceeded",
|
|
1168
|
+
false,
|
|
1169
|
+
1009,
|
|
1170
|
+
"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
|
|
1171
|
+
);
|
|
1172
|
+
cb(error);
|
|
1173
|
+
return;
|
|
1174
|
+
}
|
|
1175
|
+
this._fragments.push(buf);
|
|
1176
|
+
}
|
|
1177
|
+
this.dataMessage(cb);
|
|
1178
|
+
if (this._state === GET_INFO) this.startLoop(cb);
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
/**
|
|
1182
|
+
* Handles a data message.
|
|
1183
|
+
*
|
|
1184
|
+
* @param {Function} cb Callback
|
|
1185
|
+
* @private
|
|
1186
|
+
*/
|
|
1187
|
+
dataMessage(cb) {
|
|
1188
|
+
if (!this._fin) {
|
|
1189
|
+
this._state = GET_INFO;
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
const messageLength = this._messageLength;
|
|
1193
|
+
const fragments = this._fragments;
|
|
1194
|
+
this._totalPayloadLength = 0;
|
|
1195
|
+
this._messageLength = 0;
|
|
1196
|
+
this._fragmented = 0;
|
|
1197
|
+
this._fragments = [];
|
|
1198
|
+
if (this._opcode === 2) {
|
|
1199
|
+
let data;
|
|
1200
|
+
if (this._binaryType === "nodebuffer") {
|
|
1201
|
+
data = concat(fragments, messageLength);
|
|
1202
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
1203
|
+
data = toArrayBuffer(concat(fragments, messageLength));
|
|
1204
|
+
} else if (this._binaryType === "blob") {
|
|
1205
|
+
data = new Blob(fragments);
|
|
1206
|
+
} else {
|
|
1207
|
+
data = fragments;
|
|
1208
|
+
}
|
|
1209
|
+
if (this._allowSynchronousEvents) {
|
|
1210
|
+
this.emit("message", data, true);
|
|
1211
|
+
this._state = GET_INFO;
|
|
1212
|
+
} else {
|
|
1213
|
+
this._state = DEFER_EVENT;
|
|
1214
|
+
setImmediate(() => {
|
|
1215
|
+
this.emit("message", data, true);
|
|
1216
|
+
this._state = GET_INFO;
|
|
1217
|
+
this.startLoop(cb);
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
} else {
|
|
1221
|
+
const buf = concat(fragments, messageLength);
|
|
1222
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
1223
|
+
const error = this.createError(
|
|
1224
|
+
Error,
|
|
1225
|
+
"invalid UTF-8 sequence",
|
|
1226
|
+
true,
|
|
1227
|
+
1007,
|
|
1228
|
+
"WS_ERR_INVALID_UTF8"
|
|
1229
|
+
);
|
|
1230
|
+
cb(error);
|
|
1231
|
+
return;
|
|
1232
|
+
}
|
|
1233
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
1234
|
+
this.emit("message", buf, false);
|
|
1235
|
+
this._state = GET_INFO;
|
|
1236
|
+
} else {
|
|
1237
|
+
this._state = DEFER_EVENT;
|
|
1238
|
+
setImmediate(() => {
|
|
1239
|
+
this.emit("message", buf, false);
|
|
1240
|
+
this._state = GET_INFO;
|
|
1241
|
+
this.startLoop(cb);
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1247
|
+
* Handles a control message.
|
|
1248
|
+
*
|
|
1249
|
+
* @param {Buffer} data Data to handle
|
|
1250
|
+
* @return {(Error|RangeError|undefined)} A possible error
|
|
1251
|
+
* @private
|
|
1252
|
+
*/
|
|
1253
|
+
controlMessage(data, cb) {
|
|
1254
|
+
if (this._opcode === 8) {
|
|
1255
|
+
if (data.length === 0) {
|
|
1256
|
+
this._loop = false;
|
|
1257
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
1258
|
+
this.end();
|
|
1259
|
+
} else {
|
|
1260
|
+
const code = data.readUInt16BE(0);
|
|
1261
|
+
if (!isValidStatusCode(code)) {
|
|
1262
|
+
const error = this.createError(
|
|
1263
|
+
RangeError,
|
|
1264
|
+
`invalid status code ${code}`,
|
|
1265
|
+
true,
|
|
1266
|
+
1002,
|
|
1267
|
+
"WS_ERR_INVALID_CLOSE_CODE"
|
|
1268
|
+
);
|
|
1269
|
+
cb(error);
|
|
1270
|
+
return;
|
|
1271
|
+
}
|
|
1272
|
+
const buf = new FastBuffer(
|
|
1273
|
+
data.buffer,
|
|
1274
|
+
data.byteOffset + 2,
|
|
1275
|
+
data.length - 2
|
|
1276
|
+
);
|
|
1277
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
1278
|
+
const error = this.createError(
|
|
1279
|
+
Error,
|
|
1280
|
+
"invalid UTF-8 sequence",
|
|
1281
|
+
true,
|
|
1282
|
+
1007,
|
|
1283
|
+
"WS_ERR_INVALID_UTF8"
|
|
1284
|
+
);
|
|
1285
|
+
cb(error);
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
this._loop = false;
|
|
1289
|
+
this.emit("conclude", code, buf);
|
|
1290
|
+
this.end();
|
|
1291
|
+
}
|
|
1292
|
+
this._state = GET_INFO;
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1295
|
+
if (this._allowSynchronousEvents) {
|
|
1296
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
1297
|
+
this._state = GET_INFO;
|
|
1298
|
+
} else {
|
|
1299
|
+
this._state = DEFER_EVENT;
|
|
1300
|
+
setImmediate(() => {
|
|
1301
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
1302
|
+
this._state = GET_INFO;
|
|
1303
|
+
this.startLoop(cb);
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
/**
|
|
1308
|
+
* Builds an error object.
|
|
1309
|
+
*
|
|
1310
|
+
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
|
|
1311
|
+
* @param {String} message The error message
|
|
1312
|
+
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
|
|
1313
|
+
* `message`
|
|
1314
|
+
* @param {Number} statusCode The status code
|
|
1315
|
+
* @param {String} errorCode The exposed error code
|
|
1316
|
+
* @return {(Error|RangeError)} The error
|
|
1317
|
+
* @private
|
|
1318
|
+
*/
|
|
1319
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
1320
|
+
this._loop = false;
|
|
1321
|
+
this._errored = true;
|
|
1322
|
+
const err = new ErrorCtor(
|
|
1323
|
+
prefix ? `Invalid WebSocket frame: ${message}` : message
|
|
1324
|
+
);
|
|
1325
|
+
Error.captureStackTrace(err, this.createError);
|
|
1326
|
+
err.code = errorCode;
|
|
1327
|
+
err[kStatusCode] = statusCode;
|
|
1328
|
+
return err;
|
|
1329
|
+
}
|
|
1330
|
+
};
|
|
1331
|
+
module.exports = Receiver2;
|
|
1332
|
+
}
|
|
1333
|
+
});
|
|
1334
|
+
|
|
1335
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/sender.js
|
|
1336
|
+
var require_sender = __commonJS({
|
|
1337
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/sender.js"(exports, module) {
|
|
1338
|
+
"use strict";
|
|
1339
|
+
var { Duplex } = __require("stream");
|
|
1340
|
+
var { randomFillSync } = __require("crypto");
|
|
1341
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
1342
|
+
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
|
|
1343
|
+
var { isBlob, isValidStatusCode } = require_validation();
|
|
1344
|
+
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
1345
|
+
var kByteLength = /* @__PURE__ */ Symbol("kByteLength");
|
|
1346
|
+
var maskBuffer = Buffer.alloc(4);
|
|
1347
|
+
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
1348
|
+
var randomPool;
|
|
1349
|
+
var randomPoolPointer = RANDOM_POOL_SIZE;
|
|
1350
|
+
var DEFAULT = 0;
|
|
1351
|
+
var DEFLATING = 1;
|
|
1352
|
+
var GET_BLOB_DATA = 2;
|
|
1353
|
+
var Sender2 = class _Sender {
|
|
1354
|
+
/**
|
|
1355
|
+
* Creates a Sender instance.
|
|
1356
|
+
*
|
|
1357
|
+
* @param {Duplex} socket The connection socket
|
|
1358
|
+
* @param {Object} [extensions] An object containing the negotiated extensions
|
|
1359
|
+
* @param {Function} [generateMask] The function used to generate the masking
|
|
1360
|
+
* key
|
|
1361
|
+
*/
|
|
1362
|
+
constructor(socket, extensions, generateMask) {
|
|
1363
|
+
this._extensions = extensions || {};
|
|
1364
|
+
if (generateMask) {
|
|
1365
|
+
this._generateMask = generateMask;
|
|
1366
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
1367
|
+
}
|
|
1368
|
+
this._socket = socket;
|
|
1369
|
+
this._firstFragment = true;
|
|
1370
|
+
this._compress = false;
|
|
1371
|
+
this._bufferedBytes = 0;
|
|
1372
|
+
this._queue = [];
|
|
1373
|
+
this._state = DEFAULT;
|
|
1374
|
+
this.onerror = NOOP;
|
|
1375
|
+
this[kWebSocket] = void 0;
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* Frames a piece of data according to the HyBi WebSocket protocol.
|
|
1379
|
+
*
|
|
1380
|
+
* @param {(Buffer|String)} data The data to frame
|
|
1381
|
+
* @param {Object} options Options object
|
|
1382
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
1383
|
+
* FIN bit
|
|
1384
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
1385
|
+
* masking key
|
|
1386
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1387
|
+
* `data`
|
|
1388
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
1389
|
+
* key
|
|
1390
|
+
* @param {Number} options.opcode The opcode
|
|
1391
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
1392
|
+
* modified
|
|
1393
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
1394
|
+
* RSV1 bit
|
|
1395
|
+
* @return {(Buffer|String)[]} The framed data
|
|
1396
|
+
* @public
|
|
1397
|
+
*/
|
|
1398
|
+
static frame(data, options) {
|
|
1399
|
+
let mask;
|
|
1400
|
+
let merge = false;
|
|
1401
|
+
let offset = 2;
|
|
1402
|
+
let skipMasking = false;
|
|
1403
|
+
if (options.mask) {
|
|
1404
|
+
mask = options.maskBuffer || maskBuffer;
|
|
1405
|
+
if (options.generateMask) {
|
|
1406
|
+
options.generateMask(mask);
|
|
1407
|
+
} else {
|
|
1408
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
1409
|
+
if (randomPool === void 0) {
|
|
1410
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
1411
|
+
}
|
|
1412
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
1413
|
+
randomPoolPointer = 0;
|
|
1414
|
+
}
|
|
1415
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
1416
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
1417
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
1418
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
1419
|
+
}
|
|
1420
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
1421
|
+
offset = 6;
|
|
1422
|
+
}
|
|
1423
|
+
let dataLength;
|
|
1424
|
+
if (typeof data === "string") {
|
|
1425
|
+
if ((!options.mask || skipMasking) && options[kByteLength] !== void 0) {
|
|
1426
|
+
dataLength = options[kByteLength];
|
|
1427
|
+
} else {
|
|
1428
|
+
data = Buffer.from(data);
|
|
1429
|
+
dataLength = data.length;
|
|
1430
|
+
}
|
|
1431
|
+
} else {
|
|
1432
|
+
dataLength = data.length;
|
|
1433
|
+
merge = options.mask && options.readOnly && !skipMasking;
|
|
1434
|
+
}
|
|
1435
|
+
let payloadLength = dataLength;
|
|
1436
|
+
if (dataLength >= 65536) {
|
|
1437
|
+
offset += 8;
|
|
1438
|
+
payloadLength = 127;
|
|
1439
|
+
} else if (dataLength > 125) {
|
|
1440
|
+
offset += 2;
|
|
1441
|
+
payloadLength = 126;
|
|
1442
|
+
}
|
|
1443
|
+
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
|
1444
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
1445
|
+
if (options.rsv1) target[0] |= 64;
|
|
1446
|
+
target[1] = payloadLength;
|
|
1447
|
+
if (payloadLength === 126) {
|
|
1448
|
+
target.writeUInt16BE(dataLength, 2);
|
|
1449
|
+
} else if (payloadLength === 127) {
|
|
1450
|
+
target[2] = target[3] = 0;
|
|
1451
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
1452
|
+
}
|
|
1453
|
+
if (!options.mask) return [target, data];
|
|
1454
|
+
target[1] |= 128;
|
|
1455
|
+
target[offset - 4] = mask[0];
|
|
1456
|
+
target[offset - 3] = mask[1];
|
|
1457
|
+
target[offset - 2] = mask[2];
|
|
1458
|
+
target[offset - 1] = mask[3];
|
|
1459
|
+
if (skipMasking) return [target, data];
|
|
1460
|
+
if (merge) {
|
|
1461
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
1462
|
+
return [target];
|
|
1463
|
+
}
|
|
1464
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
1465
|
+
return [target, data];
|
|
1466
|
+
}
|
|
1467
|
+
/**
|
|
1468
|
+
* Sends a close message to the other peer.
|
|
1469
|
+
*
|
|
1470
|
+
* @param {Number} [code] The status code component of the body
|
|
1471
|
+
* @param {(String|Buffer)} [data] The message component of the body
|
|
1472
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
|
|
1473
|
+
* @param {Function} [cb] Callback
|
|
1474
|
+
* @public
|
|
1475
|
+
*/
|
|
1476
|
+
close(code, data, mask, cb) {
|
|
1477
|
+
let buf;
|
|
1478
|
+
if (code === void 0) {
|
|
1479
|
+
buf = EMPTY_BUFFER;
|
|
1480
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
1481
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
1482
|
+
} else if (data === void 0 || !data.length) {
|
|
1483
|
+
buf = Buffer.allocUnsafe(2);
|
|
1484
|
+
buf.writeUInt16BE(code, 0);
|
|
1485
|
+
} else {
|
|
1486
|
+
const length = Buffer.byteLength(data);
|
|
1487
|
+
if (length > 123) {
|
|
1488
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
1489
|
+
}
|
|
1490
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
1491
|
+
buf.writeUInt16BE(code, 0);
|
|
1492
|
+
if (typeof data === "string") {
|
|
1493
|
+
buf.write(data, 2);
|
|
1494
|
+
} else {
|
|
1495
|
+
buf.set(data, 2);
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
const options = {
|
|
1499
|
+
[kByteLength]: buf.length,
|
|
1500
|
+
fin: true,
|
|
1501
|
+
generateMask: this._generateMask,
|
|
1502
|
+
mask,
|
|
1503
|
+
maskBuffer: this._maskBuffer,
|
|
1504
|
+
opcode: 8,
|
|
1505
|
+
readOnly: false,
|
|
1506
|
+
rsv1: false
|
|
1507
|
+
};
|
|
1508
|
+
if (this._state !== DEFAULT) {
|
|
1509
|
+
this.enqueue([this.dispatch, buf, false, options, cb]);
|
|
1510
|
+
} else {
|
|
1511
|
+
this.sendFrame(_Sender.frame(buf, options), cb);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* Sends a ping message to the other peer.
|
|
1516
|
+
*
|
|
1517
|
+
* @param {*} data The message to send
|
|
1518
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
|
1519
|
+
* @param {Function} [cb] Callback
|
|
1520
|
+
* @public
|
|
1521
|
+
*/
|
|
1522
|
+
ping(data, mask, cb) {
|
|
1523
|
+
let byteLength;
|
|
1524
|
+
let readOnly;
|
|
1525
|
+
if (typeof data === "string") {
|
|
1526
|
+
byteLength = Buffer.byteLength(data);
|
|
1527
|
+
readOnly = false;
|
|
1528
|
+
} else if (isBlob(data)) {
|
|
1529
|
+
byteLength = data.size;
|
|
1530
|
+
readOnly = false;
|
|
1531
|
+
} else {
|
|
1532
|
+
data = toBuffer(data);
|
|
1533
|
+
byteLength = data.length;
|
|
1534
|
+
readOnly = toBuffer.readOnly;
|
|
1535
|
+
}
|
|
1536
|
+
if (byteLength > 125) {
|
|
1537
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
1538
|
+
}
|
|
1539
|
+
const options = {
|
|
1540
|
+
[kByteLength]: byteLength,
|
|
1541
|
+
fin: true,
|
|
1542
|
+
generateMask: this._generateMask,
|
|
1543
|
+
mask,
|
|
1544
|
+
maskBuffer: this._maskBuffer,
|
|
1545
|
+
opcode: 9,
|
|
1546
|
+
readOnly,
|
|
1547
|
+
rsv1: false
|
|
1548
|
+
};
|
|
1549
|
+
if (isBlob(data)) {
|
|
1550
|
+
if (this._state !== DEFAULT) {
|
|
1551
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
1552
|
+
} else {
|
|
1553
|
+
this.getBlobData(data, false, options, cb);
|
|
1554
|
+
}
|
|
1555
|
+
} else if (this._state !== DEFAULT) {
|
|
1556
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
1557
|
+
} else {
|
|
1558
|
+
this.sendFrame(_Sender.frame(data, options), cb);
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
/**
|
|
1562
|
+
* Sends a pong message to the other peer.
|
|
1563
|
+
*
|
|
1564
|
+
* @param {*} data The message to send
|
|
1565
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
|
1566
|
+
* @param {Function} [cb] Callback
|
|
1567
|
+
* @public
|
|
1568
|
+
*/
|
|
1569
|
+
pong(data, mask, cb) {
|
|
1570
|
+
let byteLength;
|
|
1571
|
+
let readOnly;
|
|
1572
|
+
if (typeof data === "string") {
|
|
1573
|
+
byteLength = Buffer.byteLength(data);
|
|
1574
|
+
readOnly = false;
|
|
1575
|
+
} else if (isBlob(data)) {
|
|
1576
|
+
byteLength = data.size;
|
|
1577
|
+
readOnly = false;
|
|
1578
|
+
} else {
|
|
1579
|
+
data = toBuffer(data);
|
|
1580
|
+
byteLength = data.length;
|
|
1581
|
+
readOnly = toBuffer.readOnly;
|
|
1582
|
+
}
|
|
1583
|
+
if (byteLength > 125) {
|
|
1584
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
1585
|
+
}
|
|
1586
|
+
const options = {
|
|
1587
|
+
[kByteLength]: byteLength,
|
|
1588
|
+
fin: true,
|
|
1589
|
+
generateMask: this._generateMask,
|
|
1590
|
+
mask,
|
|
1591
|
+
maskBuffer: this._maskBuffer,
|
|
1592
|
+
opcode: 10,
|
|
1593
|
+
readOnly,
|
|
1594
|
+
rsv1: false
|
|
1595
|
+
};
|
|
1596
|
+
if (isBlob(data)) {
|
|
1597
|
+
if (this._state !== DEFAULT) {
|
|
1598
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
1599
|
+
} else {
|
|
1600
|
+
this.getBlobData(data, false, options, cb);
|
|
1601
|
+
}
|
|
1602
|
+
} else if (this._state !== DEFAULT) {
|
|
1603
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
1604
|
+
} else {
|
|
1605
|
+
this.sendFrame(_Sender.frame(data, options), cb);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Sends a data message to the other peer.
|
|
1610
|
+
*
|
|
1611
|
+
* @param {*} data The message to send
|
|
1612
|
+
* @param {Object} options Options object
|
|
1613
|
+
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
|
|
1614
|
+
* or text
|
|
1615
|
+
* @param {Boolean} [options.compress=false] Specifies whether or not to
|
|
1616
|
+
* compress `data`
|
|
1617
|
+
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
|
|
1618
|
+
* last one
|
|
1619
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1620
|
+
* `data`
|
|
1621
|
+
* @param {Function} [cb] Callback
|
|
1622
|
+
* @public
|
|
1623
|
+
*/
|
|
1624
|
+
send(data, options, cb) {
|
|
1625
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1626
|
+
let opcode = options.binary ? 2 : 1;
|
|
1627
|
+
let rsv1 = options.compress;
|
|
1628
|
+
let byteLength;
|
|
1629
|
+
let readOnly;
|
|
1630
|
+
if (typeof data === "string") {
|
|
1631
|
+
byteLength = Buffer.byteLength(data);
|
|
1632
|
+
readOnly = false;
|
|
1633
|
+
} else if (isBlob(data)) {
|
|
1634
|
+
byteLength = data.size;
|
|
1635
|
+
readOnly = false;
|
|
1636
|
+
} else {
|
|
1637
|
+
data = toBuffer(data);
|
|
1638
|
+
byteLength = data.length;
|
|
1639
|
+
readOnly = toBuffer.readOnly;
|
|
1640
|
+
}
|
|
1641
|
+
if (this._firstFragment) {
|
|
1642
|
+
this._firstFragment = false;
|
|
1643
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
1644
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
1645
|
+
}
|
|
1646
|
+
this._compress = rsv1;
|
|
1647
|
+
} else {
|
|
1648
|
+
rsv1 = false;
|
|
1649
|
+
opcode = 0;
|
|
1650
|
+
}
|
|
1651
|
+
if (options.fin) this._firstFragment = true;
|
|
1652
|
+
const opts = {
|
|
1653
|
+
[kByteLength]: byteLength,
|
|
1654
|
+
fin: options.fin,
|
|
1655
|
+
generateMask: this._generateMask,
|
|
1656
|
+
mask: options.mask,
|
|
1657
|
+
maskBuffer: this._maskBuffer,
|
|
1658
|
+
opcode,
|
|
1659
|
+
readOnly,
|
|
1660
|
+
rsv1
|
|
1661
|
+
};
|
|
1662
|
+
if (isBlob(data)) {
|
|
1663
|
+
if (this._state !== DEFAULT) {
|
|
1664
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
1665
|
+
} else {
|
|
1666
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
1667
|
+
}
|
|
1668
|
+
} else if (this._state !== DEFAULT) {
|
|
1669
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
1670
|
+
} else {
|
|
1671
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
/**
|
|
1675
|
+
* Gets the contents of a blob as binary data.
|
|
1676
|
+
*
|
|
1677
|
+
* @param {Blob} blob The blob
|
|
1678
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
|
1679
|
+
* the data
|
|
1680
|
+
* @param {Object} options Options object
|
|
1681
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
1682
|
+
* FIN bit
|
|
1683
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
1684
|
+
* masking key
|
|
1685
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1686
|
+
* `data`
|
|
1687
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
1688
|
+
* key
|
|
1689
|
+
* @param {Number} options.opcode The opcode
|
|
1690
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
1691
|
+
* modified
|
|
1692
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
1693
|
+
* RSV1 bit
|
|
1694
|
+
* @param {Function} [cb] Callback
|
|
1695
|
+
* @private
|
|
1696
|
+
*/
|
|
1697
|
+
getBlobData(blob, compress, options, cb) {
|
|
1698
|
+
this._bufferedBytes += options[kByteLength];
|
|
1699
|
+
this._state = GET_BLOB_DATA;
|
|
1700
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
1701
|
+
if (this._socket.destroyed) {
|
|
1702
|
+
const err = new Error(
|
|
1703
|
+
"The socket was closed while the blob was being read"
|
|
1704
|
+
);
|
|
1705
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
1706
|
+
return;
|
|
1707
|
+
}
|
|
1708
|
+
this._bufferedBytes -= options[kByteLength];
|
|
1709
|
+
const data = toBuffer(arrayBuffer);
|
|
1710
|
+
if (!compress) {
|
|
1711
|
+
this._state = DEFAULT;
|
|
1712
|
+
this.sendFrame(_Sender.frame(data, options), cb);
|
|
1713
|
+
this.dequeue();
|
|
1714
|
+
} else {
|
|
1715
|
+
this.dispatch(data, compress, options, cb);
|
|
1716
|
+
}
|
|
1717
|
+
}).catch((err) => {
|
|
1718
|
+
process.nextTick(onError, this, err, cb);
|
|
1719
|
+
});
|
|
1720
|
+
}
|
|
1721
|
+
/**
|
|
1722
|
+
* Dispatches a message.
|
|
1723
|
+
*
|
|
1724
|
+
* @param {(Buffer|String)} data The message to send
|
|
1725
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
|
1726
|
+
* `data`
|
|
1727
|
+
* @param {Object} options Options object
|
|
1728
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
1729
|
+
* FIN bit
|
|
1730
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
1731
|
+
* masking key
|
|
1732
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1733
|
+
* `data`
|
|
1734
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
1735
|
+
* key
|
|
1736
|
+
* @param {Number} options.opcode The opcode
|
|
1737
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
1738
|
+
* modified
|
|
1739
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
1740
|
+
* RSV1 bit
|
|
1741
|
+
* @param {Function} [cb] Callback
|
|
1742
|
+
* @private
|
|
1743
|
+
*/
|
|
1744
|
+
dispatch(data, compress, options, cb) {
|
|
1745
|
+
if (!compress) {
|
|
1746
|
+
this.sendFrame(_Sender.frame(data, options), cb);
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1750
|
+
this._bufferedBytes += options[kByteLength];
|
|
1751
|
+
this._state = DEFLATING;
|
|
1752
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
|
1753
|
+
if (this._socket.destroyed) {
|
|
1754
|
+
const err = new Error(
|
|
1755
|
+
"The socket was closed while data was being compressed"
|
|
1756
|
+
);
|
|
1757
|
+
callCallbacks(this, err, cb);
|
|
1758
|
+
return;
|
|
1759
|
+
}
|
|
1760
|
+
this._bufferedBytes -= options[kByteLength];
|
|
1761
|
+
this._state = DEFAULT;
|
|
1762
|
+
options.readOnly = false;
|
|
1763
|
+
this.sendFrame(_Sender.frame(buf, options), cb);
|
|
1764
|
+
this.dequeue();
|
|
1765
|
+
});
|
|
1766
|
+
}
|
|
1767
|
+
/**
|
|
1768
|
+
* Executes queued send operations.
|
|
1769
|
+
*
|
|
1770
|
+
* @private
|
|
1771
|
+
*/
|
|
1772
|
+
dequeue() {
|
|
1773
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
1774
|
+
const params = this._queue.shift();
|
|
1775
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
1776
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
/**
|
|
1780
|
+
* Enqueues a send operation.
|
|
1781
|
+
*
|
|
1782
|
+
* @param {Array} params Send operation parameters.
|
|
1783
|
+
* @private
|
|
1784
|
+
*/
|
|
1785
|
+
enqueue(params) {
|
|
1786
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
1787
|
+
this._queue.push(params);
|
|
1788
|
+
}
|
|
1789
|
+
/**
|
|
1790
|
+
* Sends a frame.
|
|
1791
|
+
*
|
|
1792
|
+
* @param {(Buffer | String)[]} list The frame to send
|
|
1793
|
+
* @param {Function} [cb] Callback
|
|
1794
|
+
* @private
|
|
1795
|
+
*/
|
|
1796
|
+
sendFrame(list, cb) {
|
|
1797
|
+
if (list.length === 2) {
|
|
1798
|
+
this._socket.cork();
|
|
1799
|
+
this._socket.write(list[0]);
|
|
1800
|
+
this._socket.write(list[1], cb);
|
|
1801
|
+
this._socket.uncork();
|
|
1802
|
+
} else {
|
|
1803
|
+
this._socket.write(list[0], cb);
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
};
|
|
1807
|
+
module.exports = Sender2;
|
|
1808
|
+
function callCallbacks(sender, err, cb) {
|
|
1809
|
+
if (typeof cb === "function") cb(err);
|
|
1810
|
+
for (let i = 0; i < sender._queue.length; i++) {
|
|
1811
|
+
const params = sender._queue[i];
|
|
1812
|
+
const callback = params[params.length - 1];
|
|
1813
|
+
if (typeof callback === "function") callback(err);
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
function onError(sender, err, cb) {
|
|
1817
|
+
callCallbacks(sender, err, cb);
|
|
1818
|
+
sender.onerror(err);
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
});
|
|
1822
|
+
|
|
1823
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/event-target.js
|
|
1824
|
+
var require_event_target = __commonJS({
|
|
1825
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/event-target.js"(exports, module) {
|
|
1826
|
+
"use strict";
|
|
1827
|
+
var { kForOnEventAttribute, kListener } = require_constants();
|
|
1828
|
+
var kCode = /* @__PURE__ */ Symbol("kCode");
|
|
1829
|
+
var kData = /* @__PURE__ */ Symbol("kData");
|
|
1830
|
+
var kError = /* @__PURE__ */ Symbol("kError");
|
|
1831
|
+
var kMessage = /* @__PURE__ */ Symbol("kMessage");
|
|
1832
|
+
var kReason = /* @__PURE__ */ Symbol("kReason");
|
|
1833
|
+
var kTarget = /* @__PURE__ */ Symbol("kTarget");
|
|
1834
|
+
var kType = /* @__PURE__ */ Symbol("kType");
|
|
1835
|
+
var kWasClean = /* @__PURE__ */ Symbol("kWasClean");
|
|
1836
|
+
var Event = class {
|
|
1837
|
+
/**
|
|
1838
|
+
* Create a new `Event`.
|
|
1839
|
+
*
|
|
1840
|
+
* @param {String} type The name of the event
|
|
1841
|
+
* @throws {TypeError} If the `type` argument is not specified
|
|
1842
|
+
*/
|
|
1843
|
+
constructor(type) {
|
|
1844
|
+
this[kTarget] = null;
|
|
1845
|
+
this[kType] = type;
|
|
1846
|
+
}
|
|
1847
|
+
/**
|
|
1848
|
+
* @type {*}
|
|
1849
|
+
*/
|
|
1850
|
+
get target() {
|
|
1851
|
+
return this[kTarget];
|
|
1852
|
+
}
|
|
1853
|
+
/**
|
|
1854
|
+
* @type {String}
|
|
1855
|
+
*/
|
|
1856
|
+
get type() {
|
|
1857
|
+
return this[kType];
|
|
1858
|
+
}
|
|
1859
|
+
};
|
|
1860
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
1861
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
1862
|
+
var CloseEvent = class extends Event {
|
|
1863
|
+
/**
|
|
1864
|
+
* Create a new `CloseEvent`.
|
|
1865
|
+
*
|
|
1866
|
+
* @param {String} type The name of the event
|
|
1867
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
1868
|
+
* attributes via object members of the same name
|
|
1869
|
+
* @param {Number} [options.code=0] The status code explaining why the
|
|
1870
|
+
* connection was closed
|
|
1871
|
+
* @param {String} [options.reason=''] A human-readable string explaining why
|
|
1872
|
+
* the connection was closed
|
|
1873
|
+
* @param {Boolean} [options.wasClean=false] Indicates whether or not the
|
|
1874
|
+
* connection was cleanly closed
|
|
1875
|
+
*/
|
|
1876
|
+
constructor(type, options = {}) {
|
|
1877
|
+
super(type);
|
|
1878
|
+
this[kCode] = options.code === void 0 ? 0 : options.code;
|
|
1879
|
+
this[kReason] = options.reason === void 0 ? "" : options.reason;
|
|
1880
|
+
this[kWasClean] = options.wasClean === void 0 ? false : options.wasClean;
|
|
1881
|
+
}
|
|
1882
|
+
/**
|
|
1883
|
+
* @type {Number}
|
|
1884
|
+
*/
|
|
1885
|
+
get code() {
|
|
1886
|
+
return this[kCode];
|
|
1887
|
+
}
|
|
1888
|
+
/**
|
|
1889
|
+
* @type {String}
|
|
1890
|
+
*/
|
|
1891
|
+
get reason() {
|
|
1892
|
+
return this[kReason];
|
|
1893
|
+
}
|
|
1894
|
+
/**
|
|
1895
|
+
* @type {Boolean}
|
|
1896
|
+
*/
|
|
1897
|
+
get wasClean() {
|
|
1898
|
+
return this[kWasClean];
|
|
1899
|
+
}
|
|
1900
|
+
};
|
|
1901
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
1902
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
1903
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
1904
|
+
var ErrorEvent = class extends Event {
|
|
1905
|
+
/**
|
|
1906
|
+
* Create a new `ErrorEvent`.
|
|
1907
|
+
*
|
|
1908
|
+
* @param {String} type The name of the event
|
|
1909
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
1910
|
+
* attributes via object members of the same name
|
|
1911
|
+
* @param {*} [options.error=null] The error that generated this event
|
|
1912
|
+
* @param {String} [options.message=''] The error message
|
|
1913
|
+
*/
|
|
1914
|
+
constructor(type, options = {}) {
|
|
1915
|
+
super(type);
|
|
1916
|
+
this[kError] = options.error === void 0 ? null : options.error;
|
|
1917
|
+
this[kMessage] = options.message === void 0 ? "" : options.message;
|
|
1918
|
+
}
|
|
1919
|
+
/**
|
|
1920
|
+
* @type {*}
|
|
1921
|
+
*/
|
|
1922
|
+
get error() {
|
|
1923
|
+
return this[kError];
|
|
1924
|
+
}
|
|
1925
|
+
/**
|
|
1926
|
+
* @type {String}
|
|
1927
|
+
*/
|
|
1928
|
+
get message() {
|
|
1929
|
+
return this[kMessage];
|
|
1930
|
+
}
|
|
1931
|
+
};
|
|
1932
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
1933
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
1934
|
+
var MessageEvent = class extends Event {
|
|
1935
|
+
/**
|
|
1936
|
+
* Create a new `MessageEvent`.
|
|
1937
|
+
*
|
|
1938
|
+
* @param {String} type The name of the event
|
|
1939
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
1940
|
+
* attributes via object members of the same name
|
|
1941
|
+
* @param {*} [options.data=null] The message content
|
|
1942
|
+
*/
|
|
1943
|
+
constructor(type, options = {}) {
|
|
1944
|
+
super(type);
|
|
1945
|
+
this[kData] = options.data === void 0 ? null : options.data;
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* @type {*}
|
|
1949
|
+
*/
|
|
1950
|
+
get data() {
|
|
1951
|
+
return this[kData];
|
|
1952
|
+
}
|
|
1953
|
+
};
|
|
1954
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
1955
|
+
var EventTarget = {
|
|
1956
|
+
/**
|
|
1957
|
+
* Register an event listener.
|
|
1958
|
+
*
|
|
1959
|
+
* @param {String} type A string representing the event type to listen for
|
|
1960
|
+
* @param {(Function|Object)} handler The listener to add
|
|
1961
|
+
* @param {Object} [options] An options object specifies characteristics about
|
|
1962
|
+
* the event listener
|
|
1963
|
+
* @param {Boolean} [options.once=false] A `Boolean` indicating that the
|
|
1964
|
+
* listener should be invoked at most once after being added. If `true`,
|
|
1965
|
+
* the listener would be automatically removed when invoked.
|
|
1966
|
+
* @public
|
|
1967
|
+
*/
|
|
1968
|
+
addEventListener(type, handler, options = {}) {
|
|
1969
|
+
for (const listener of this.listeners(type)) {
|
|
1970
|
+
if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
1971
|
+
return;
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
let wrapper;
|
|
1975
|
+
if (type === "message") {
|
|
1976
|
+
wrapper = function onMessage(data, isBinary) {
|
|
1977
|
+
const event = new MessageEvent("message", {
|
|
1978
|
+
data: isBinary ? data : data.toString()
|
|
1979
|
+
});
|
|
1980
|
+
event[kTarget] = this;
|
|
1981
|
+
callListener(handler, this, event);
|
|
1982
|
+
};
|
|
1983
|
+
} else if (type === "close") {
|
|
1984
|
+
wrapper = function onClose(code, message) {
|
|
1985
|
+
const event = new CloseEvent("close", {
|
|
1986
|
+
code,
|
|
1987
|
+
reason: message.toString(),
|
|
1988
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
1989
|
+
});
|
|
1990
|
+
event[kTarget] = this;
|
|
1991
|
+
callListener(handler, this, event);
|
|
1992
|
+
};
|
|
1993
|
+
} else if (type === "error") {
|
|
1994
|
+
wrapper = function onError(error) {
|
|
1995
|
+
const event = new ErrorEvent("error", {
|
|
1996
|
+
error,
|
|
1997
|
+
message: error.message
|
|
1998
|
+
});
|
|
1999
|
+
event[kTarget] = this;
|
|
2000
|
+
callListener(handler, this, event);
|
|
2001
|
+
};
|
|
2002
|
+
} else if (type === "open") {
|
|
2003
|
+
wrapper = function onOpen() {
|
|
2004
|
+
const event = new Event("open");
|
|
2005
|
+
event[kTarget] = this;
|
|
2006
|
+
callListener(handler, this, event);
|
|
2007
|
+
};
|
|
2008
|
+
} else {
|
|
2009
|
+
return;
|
|
2010
|
+
}
|
|
2011
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
2012
|
+
wrapper[kListener] = handler;
|
|
2013
|
+
if (options.once) {
|
|
2014
|
+
this.once(type, wrapper);
|
|
2015
|
+
} else {
|
|
2016
|
+
this.on(type, wrapper);
|
|
2017
|
+
}
|
|
2018
|
+
},
|
|
2019
|
+
/**
|
|
2020
|
+
* Remove an event listener.
|
|
2021
|
+
*
|
|
2022
|
+
* @param {String} type A string representing the event type to remove
|
|
2023
|
+
* @param {(Function|Object)} handler The listener to remove
|
|
2024
|
+
* @public
|
|
2025
|
+
*/
|
|
2026
|
+
removeEventListener(type, handler) {
|
|
2027
|
+
for (const listener of this.listeners(type)) {
|
|
2028
|
+
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
2029
|
+
this.removeListener(type, listener);
|
|
2030
|
+
break;
|
|
2031
|
+
}
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
};
|
|
2035
|
+
module.exports = {
|
|
2036
|
+
CloseEvent,
|
|
2037
|
+
ErrorEvent,
|
|
2038
|
+
Event,
|
|
2039
|
+
EventTarget,
|
|
2040
|
+
MessageEvent
|
|
2041
|
+
};
|
|
2042
|
+
function callListener(listener, thisArg, event) {
|
|
2043
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
2044
|
+
listener.handleEvent.call(listener, event);
|
|
2045
|
+
} else {
|
|
2046
|
+
listener.call(thisArg, event);
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
});
|
|
2051
|
+
|
|
2052
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/extension.js
|
|
2053
|
+
var require_extension = __commonJS({
|
|
2054
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/extension.js"(exports, module) {
|
|
2055
|
+
"use strict";
|
|
2056
|
+
var { tokenChars } = require_validation();
|
|
2057
|
+
function push(dest, name, elem) {
|
|
2058
|
+
if (dest[name] === void 0) dest[name] = [elem];
|
|
2059
|
+
else dest[name].push(elem);
|
|
2060
|
+
}
|
|
2061
|
+
function parse(header) {
|
|
2062
|
+
const offers = /* @__PURE__ */ Object.create(null);
|
|
2063
|
+
let params = /* @__PURE__ */ Object.create(null);
|
|
2064
|
+
let mustUnescape = false;
|
|
2065
|
+
let isEscaping = false;
|
|
2066
|
+
let inQuotes = false;
|
|
2067
|
+
let extensionName;
|
|
2068
|
+
let paramName;
|
|
2069
|
+
let start = -1;
|
|
2070
|
+
let code = -1;
|
|
2071
|
+
let end = -1;
|
|
2072
|
+
let i = 0;
|
|
2073
|
+
for (; i < header.length; i++) {
|
|
2074
|
+
code = header.charCodeAt(i);
|
|
2075
|
+
if (extensionName === void 0) {
|
|
2076
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
2077
|
+
if (start === -1) start = i;
|
|
2078
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
2079
|
+
if (end === -1 && start !== -1) end = i;
|
|
2080
|
+
} else if (code === 59 || code === 44) {
|
|
2081
|
+
if (start === -1) {
|
|
2082
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2083
|
+
}
|
|
2084
|
+
if (end === -1) end = i;
|
|
2085
|
+
const name = header.slice(start, end);
|
|
2086
|
+
if (code === 44) {
|
|
2087
|
+
push(offers, name, params);
|
|
2088
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2089
|
+
} else {
|
|
2090
|
+
extensionName = name;
|
|
2091
|
+
}
|
|
2092
|
+
start = end = -1;
|
|
2093
|
+
} else {
|
|
2094
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2095
|
+
}
|
|
2096
|
+
} else if (paramName === void 0) {
|
|
2097
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
2098
|
+
if (start === -1) start = i;
|
|
2099
|
+
} else if (code === 32 || code === 9) {
|
|
2100
|
+
if (end === -1 && start !== -1) end = i;
|
|
2101
|
+
} else if (code === 59 || code === 44) {
|
|
2102
|
+
if (start === -1) {
|
|
2103
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2104
|
+
}
|
|
2105
|
+
if (end === -1) end = i;
|
|
2106
|
+
push(params, header.slice(start, end), true);
|
|
2107
|
+
if (code === 44) {
|
|
2108
|
+
push(offers, extensionName, params);
|
|
2109
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2110
|
+
extensionName = void 0;
|
|
2111
|
+
}
|
|
2112
|
+
start = end = -1;
|
|
2113
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
2114
|
+
paramName = header.slice(start, i);
|
|
2115
|
+
start = end = -1;
|
|
2116
|
+
} else {
|
|
2117
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2118
|
+
}
|
|
2119
|
+
} else {
|
|
2120
|
+
if (isEscaping) {
|
|
2121
|
+
if (tokenChars[code] !== 1) {
|
|
2122
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2123
|
+
}
|
|
2124
|
+
if (start === -1) start = i;
|
|
2125
|
+
else if (!mustUnescape) mustUnescape = true;
|
|
2126
|
+
isEscaping = false;
|
|
2127
|
+
} else if (inQuotes) {
|
|
2128
|
+
if (tokenChars[code] === 1) {
|
|
2129
|
+
if (start === -1) start = i;
|
|
2130
|
+
} else if (code === 34 && start !== -1) {
|
|
2131
|
+
inQuotes = false;
|
|
2132
|
+
end = i;
|
|
2133
|
+
} else if (code === 92) {
|
|
2134
|
+
isEscaping = true;
|
|
2135
|
+
} else {
|
|
2136
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2137
|
+
}
|
|
2138
|
+
} else if (code === 34 && header.charCodeAt(i - 1) === 61) {
|
|
2139
|
+
inQuotes = true;
|
|
2140
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
2141
|
+
if (start === -1) start = i;
|
|
2142
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
2143
|
+
if (end === -1) end = i;
|
|
2144
|
+
} else if (code === 59 || code === 44) {
|
|
2145
|
+
if (start === -1) {
|
|
2146
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2147
|
+
}
|
|
2148
|
+
if (end === -1) end = i;
|
|
2149
|
+
let value = header.slice(start, end);
|
|
2150
|
+
if (mustUnescape) {
|
|
2151
|
+
value = value.replace(/\\/g, "");
|
|
2152
|
+
mustUnescape = false;
|
|
2153
|
+
}
|
|
2154
|
+
push(params, paramName, value);
|
|
2155
|
+
if (code === 44) {
|
|
2156
|
+
push(offers, extensionName, params);
|
|
2157
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2158
|
+
extensionName = void 0;
|
|
2159
|
+
}
|
|
2160
|
+
paramName = void 0;
|
|
2161
|
+
start = end = -1;
|
|
2162
|
+
} else {
|
|
2163
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) {
|
|
2168
|
+
throw new SyntaxError("Unexpected end of input");
|
|
2169
|
+
}
|
|
2170
|
+
if (end === -1) end = i;
|
|
2171
|
+
const token = header.slice(start, end);
|
|
2172
|
+
if (extensionName === void 0) {
|
|
2173
|
+
push(offers, token, params);
|
|
2174
|
+
} else {
|
|
2175
|
+
if (paramName === void 0) {
|
|
2176
|
+
push(params, token, true);
|
|
2177
|
+
} else if (mustUnescape) {
|
|
2178
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
2179
|
+
} else {
|
|
2180
|
+
push(params, paramName, token);
|
|
2181
|
+
}
|
|
2182
|
+
push(offers, extensionName, params);
|
|
2183
|
+
}
|
|
2184
|
+
return offers;
|
|
2185
|
+
}
|
|
2186
|
+
function format(extensions) {
|
|
2187
|
+
return Object.keys(extensions).map((extension) => {
|
|
2188
|
+
let configurations = extensions[extension];
|
|
2189
|
+
if (!Array.isArray(configurations)) configurations = [configurations];
|
|
2190
|
+
return configurations.map((params) => {
|
|
2191
|
+
return [extension].concat(
|
|
2192
|
+
Object.keys(params).map((k) => {
|
|
2193
|
+
let values = params[k];
|
|
2194
|
+
if (!Array.isArray(values)) values = [values];
|
|
2195
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
2196
|
+
})
|
|
2197
|
+
).join("; ");
|
|
2198
|
+
}).join(", ");
|
|
2199
|
+
}).join(", ");
|
|
2200
|
+
}
|
|
2201
|
+
module.exports = { format, parse };
|
|
2202
|
+
}
|
|
2203
|
+
});
|
|
2204
|
+
|
|
2205
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/websocket.js
|
|
2206
|
+
var require_websocket = __commonJS({
|
|
2207
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/websocket.js"(exports, module) {
|
|
2208
|
+
"use strict";
|
|
2209
|
+
var EventEmitter = __require("events");
|
|
2210
|
+
var https = __require("https");
|
|
2211
|
+
var http = __require("http");
|
|
2212
|
+
var net = __require("net");
|
|
2213
|
+
var tls = __require("tls");
|
|
2214
|
+
var { randomBytes, createHash } = __require("crypto");
|
|
2215
|
+
var { Duplex, Readable } = __require("stream");
|
|
2216
|
+
var { URL: URL2 } = __require("url");
|
|
2217
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
2218
|
+
var Receiver2 = require_receiver();
|
|
2219
|
+
var Sender2 = require_sender();
|
|
2220
|
+
var { isBlob } = require_validation();
|
|
2221
|
+
var {
|
|
2222
|
+
BINARY_TYPES,
|
|
2223
|
+
CLOSE_TIMEOUT,
|
|
2224
|
+
EMPTY_BUFFER,
|
|
2225
|
+
GUID,
|
|
2226
|
+
kForOnEventAttribute,
|
|
2227
|
+
kListener,
|
|
2228
|
+
kStatusCode,
|
|
2229
|
+
kWebSocket,
|
|
2230
|
+
NOOP
|
|
2231
|
+
} = require_constants();
|
|
2232
|
+
var {
|
|
2233
|
+
EventTarget: { addEventListener, removeEventListener }
|
|
2234
|
+
} = require_event_target();
|
|
2235
|
+
var { format, parse } = require_extension();
|
|
2236
|
+
var { toBuffer } = require_buffer_util();
|
|
2237
|
+
var kAborted = /* @__PURE__ */ Symbol("kAborted");
|
|
2238
|
+
var protocolVersions = [8, 13];
|
|
2239
|
+
var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
2240
|
+
var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
2241
|
+
var WebSocket2 = class _WebSocket extends EventEmitter {
|
|
2242
|
+
/**
|
|
2243
|
+
* Create a new `WebSocket`.
|
|
2244
|
+
*
|
|
2245
|
+
* @param {(String|URL)} address The URL to which to connect
|
|
2246
|
+
* @param {(String|String[])} [protocols] The subprotocols
|
|
2247
|
+
* @param {Object} [options] Connection options
|
|
2248
|
+
*/
|
|
2249
|
+
constructor(address, protocols, options) {
|
|
2250
|
+
super();
|
|
2251
|
+
this._binaryType = BINARY_TYPES[0];
|
|
2252
|
+
this._closeCode = 1006;
|
|
2253
|
+
this._closeFrameReceived = false;
|
|
2254
|
+
this._closeFrameSent = false;
|
|
2255
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
2256
|
+
this._closeTimer = null;
|
|
2257
|
+
this._errorEmitted = false;
|
|
2258
|
+
this._extensions = {};
|
|
2259
|
+
this._paused = false;
|
|
2260
|
+
this._protocol = "";
|
|
2261
|
+
this._readyState = _WebSocket.CONNECTING;
|
|
2262
|
+
this._receiver = null;
|
|
2263
|
+
this._sender = null;
|
|
2264
|
+
this._socket = null;
|
|
2265
|
+
if (address !== null) {
|
|
2266
|
+
this._bufferedAmount = 0;
|
|
2267
|
+
this._isServer = false;
|
|
2268
|
+
this._redirects = 0;
|
|
2269
|
+
if (protocols === void 0) {
|
|
2270
|
+
protocols = [];
|
|
2271
|
+
} else if (!Array.isArray(protocols)) {
|
|
2272
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
2273
|
+
options = protocols;
|
|
2274
|
+
protocols = [];
|
|
2275
|
+
} else {
|
|
2276
|
+
protocols = [protocols];
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
initAsClient(this, address, protocols, options);
|
|
2280
|
+
} else {
|
|
2281
|
+
this._autoPong = options.autoPong;
|
|
2282
|
+
this._closeTimeout = options.closeTimeout;
|
|
2283
|
+
this._isServer = true;
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
/**
|
|
2287
|
+
* For historical reasons, the custom "nodebuffer" type is used by the default
|
|
2288
|
+
* instead of "blob".
|
|
2289
|
+
*
|
|
2290
|
+
* @type {String}
|
|
2291
|
+
*/
|
|
2292
|
+
get binaryType() {
|
|
2293
|
+
return this._binaryType;
|
|
2294
|
+
}
|
|
2295
|
+
set binaryType(type) {
|
|
2296
|
+
if (!BINARY_TYPES.includes(type)) return;
|
|
2297
|
+
this._binaryType = type;
|
|
2298
|
+
if (this._receiver) this._receiver._binaryType = type;
|
|
2299
|
+
}
|
|
2300
|
+
/**
|
|
2301
|
+
* @type {Number}
|
|
2302
|
+
*/
|
|
2303
|
+
get bufferedAmount() {
|
|
2304
|
+
if (!this._socket) return this._bufferedAmount;
|
|
2305
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
2306
|
+
}
|
|
2307
|
+
/**
|
|
2308
|
+
* @type {String}
|
|
2309
|
+
*/
|
|
2310
|
+
get extensions() {
|
|
2311
|
+
return Object.keys(this._extensions).join();
|
|
2312
|
+
}
|
|
2313
|
+
/**
|
|
2314
|
+
* @type {Boolean}
|
|
2315
|
+
*/
|
|
2316
|
+
get isPaused() {
|
|
2317
|
+
return this._paused;
|
|
2318
|
+
}
|
|
2319
|
+
/**
|
|
2320
|
+
* @type {Function}
|
|
2321
|
+
*/
|
|
2322
|
+
/* istanbul ignore next */
|
|
2323
|
+
get onclose() {
|
|
2324
|
+
return null;
|
|
2325
|
+
}
|
|
2326
|
+
/**
|
|
2327
|
+
* @type {Function}
|
|
2328
|
+
*/
|
|
2329
|
+
/* istanbul ignore next */
|
|
2330
|
+
get onerror() {
|
|
2331
|
+
return null;
|
|
2332
|
+
}
|
|
2333
|
+
/**
|
|
2334
|
+
* @type {Function}
|
|
2335
|
+
*/
|
|
2336
|
+
/* istanbul ignore next */
|
|
2337
|
+
get onopen() {
|
|
2338
|
+
return null;
|
|
2339
|
+
}
|
|
2340
|
+
/**
|
|
2341
|
+
* @type {Function}
|
|
2342
|
+
*/
|
|
2343
|
+
/* istanbul ignore next */
|
|
2344
|
+
get onmessage() {
|
|
2345
|
+
return null;
|
|
2346
|
+
}
|
|
2347
|
+
/**
|
|
2348
|
+
* @type {String}
|
|
2349
|
+
*/
|
|
2350
|
+
get protocol() {
|
|
2351
|
+
return this._protocol;
|
|
2352
|
+
}
|
|
2353
|
+
/**
|
|
2354
|
+
* @type {Number}
|
|
2355
|
+
*/
|
|
2356
|
+
get readyState() {
|
|
2357
|
+
return this._readyState;
|
|
2358
|
+
}
|
|
2359
|
+
/**
|
|
2360
|
+
* @type {String}
|
|
2361
|
+
*/
|
|
2362
|
+
get url() {
|
|
2363
|
+
return this._url;
|
|
2364
|
+
}
|
|
2365
|
+
/**
|
|
2366
|
+
* Set up the socket and the internal resources.
|
|
2367
|
+
*
|
|
2368
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
2369
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
2370
|
+
* @param {Object} options Options object
|
|
2371
|
+
* @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
|
|
2372
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
2373
|
+
* multiple times in the same tick
|
|
2374
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
2375
|
+
* masking key
|
|
2376
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message size
|
|
2377
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
2378
|
+
* not to skip UTF-8 validation for text and close messages
|
|
2379
|
+
* @private
|
|
2380
|
+
*/
|
|
2381
|
+
setSocket(socket, head, options) {
|
|
2382
|
+
const receiver = new Receiver2({
|
|
2383
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
2384
|
+
binaryType: this.binaryType,
|
|
2385
|
+
extensions: this._extensions,
|
|
2386
|
+
isServer: this._isServer,
|
|
2387
|
+
maxPayload: options.maxPayload,
|
|
2388
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
2389
|
+
});
|
|
2390
|
+
const sender = new Sender2(socket, this._extensions, options.generateMask);
|
|
2391
|
+
this._receiver = receiver;
|
|
2392
|
+
this._sender = sender;
|
|
2393
|
+
this._socket = socket;
|
|
2394
|
+
receiver[kWebSocket] = this;
|
|
2395
|
+
sender[kWebSocket] = this;
|
|
2396
|
+
socket[kWebSocket] = this;
|
|
2397
|
+
receiver.on("conclude", receiverOnConclude);
|
|
2398
|
+
receiver.on("drain", receiverOnDrain);
|
|
2399
|
+
receiver.on("error", receiverOnError);
|
|
2400
|
+
receiver.on("message", receiverOnMessage);
|
|
2401
|
+
receiver.on("ping", receiverOnPing);
|
|
2402
|
+
receiver.on("pong", receiverOnPong);
|
|
2403
|
+
sender.onerror = senderOnError;
|
|
2404
|
+
if (socket.setTimeout) socket.setTimeout(0);
|
|
2405
|
+
if (socket.setNoDelay) socket.setNoDelay();
|
|
2406
|
+
if (head.length > 0) socket.unshift(head);
|
|
2407
|
+
socket.on("close", socketOnClose);
|
|
2408
|
+
socket.on("data", socketOnData);
|
|
2409
|
+
socket.on("end", socketOnEnd);
|
|
2410
|
+
socket.on("error", socketOnError);
|
|
2411
|
+
this._readyState = _WebSocket.OPEN;
|
|
2412
|
+
this.emit("open");
|
|
2413
|
+
}
|
|
2414
|
+
/**
|
|
2415
|
+
* Emit the `'close'` event.
|
|
2416
|
+
*
|
|
2417
|
+
* @private
|
|
2418
|
+
*/
|
|
2419
|
+
emitClose() {
|
|
2420
|
+
if (!this._socket) {
|
|
2421
|
+
this._readyState = _WebSocket.CLOSED;
|
|
2422
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
2423
|
+
return;
|
|
2424
|
+
}
|
|
2425
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
2426
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
2427
|
+
}
|
|
2428
|
+
this._receiver.removeAllListeners();
|
|
2429
|
+
this._readyState = _WebSocket.CLOSED;
|
|
2430
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
2431
|
+
}
|
|
2432
|
+
/**
|
|
2433
|
+
* Start a closing handshake.
|
|
2434
|
+
*
|
|
2435
|
+
* +----------+ +-----------+ +----------+
|
|
2436
|
+
* - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
|
|
2437
|
+
* | +----------+ +-----------+ +----------+ |
|
|
2438
|
+
* +----------+ +-----------+ |
|
|
2439
|
+
* CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
|
|
2440
|
+
* +----------+ +-----------+ |
|
|
2441
|
+
* | | | +---+ |
|
|
2442
|
+
* +------------------------+-->|fin| - - - -
|
|
2443
|
+
* | +---+ | +---+
|
|
2444
|
+
* - - - - -|fin|<---------------------+
|
|
2445
|
+
* +---+
|
|
2446
|
+
*
|
|
2447
|
+
* @param {Number} [code] Status code explaining why the connection is closing
|
|
2448
|
+
* @param {(String|Buffer)} [data] The reason why the connection is
|
|
2449
|
+
* closing
|
|
2450
|
+
* @public
|
|
2451
|
+
*/
|
|
2452
|
+
close(code, data) {
|
|
2453
|
+
if (this.readyState === _WebSocket.CLOSED) return;
|
|
2454
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2455
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
2456
|
+
abortHandshake(this, this._req, msg);
|
|
2457
|
+
return;
|
|
2458
|
+
}
|
|
2459
|
+
if (this.readyState === _WebSocket.CLOSING) {
|
|
2460
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
2461
|
+
this._socket.end();
|
|
2462
|
+
}
|
|
2463
|
+
return;
|
|
2464
|
+
}
|
|
2465
|
+
this._readyState = _WebSocket.CLOSING;
|
|
2466
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
2467
|
+
if (err) return;
|
|
2468
|
+
this._closeFrameSent = true;
|
|
2469
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
2470
|
+
this._socket.end();
|
|
2471
|
+
}
|
|
2472
|
+
});
|
|
2473
|
+
setCloseTimer(this);
|
|
2474
|
+
}
|
|
2475
|
+
/**
|
|
2476
|
+
* Pause the socket.
|
|
2477
|
+
*
|
|
2478
|
+
* @public
|
|
2479
|
+
*/
|
|
2480
|
+
pause() {
|
|
2481
|
+
if (this.readyState === _WebSocket.CONNECTING || this.readyState === _WebSocket.CLOSED) {
|
|
2482
|
+
return;
|
|
2483
|
+
}
|
|
2484
|
+
this._paused = true;
|
|
2485
|
+
this._socket.pause();
|
|
2486
|
+
}
|
|
2487
|
+
/**
|
|
2488
|
+
* Send a ping.
|
|
2489
|
+
*
|
|
2490
|
+
* @param {*} [data] The data to send
|
|
2491
|
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
|
2492
|
+
* @param {Function} [cb] Callback which is executed when the ping is sent
|
|
2493
|
+
* @public
|
|
2494
|
+
*/
|
|
2495
|
+
ping(data, mask, cb) {
|
|
2496
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2497
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2498
|
+
}
|
|
2499
|
+
if (typeof data === "function") {
|
|
2500
|
+
cb = data;
|
|
2501
|
+
data = mask = void 0;
|
|
2502
|
+
} else if (typeof mask === "function") {
|
|
2503
|
+
cb = mask;
|
|
2504
|
+
mask = void 0;
|
|
2505
|
+
}
|
|
2506
|
+
if (typeof data === "number") data = data.toString();
|
|
2507
|
+
if (this.readyState !== _WebSocket.OPEN) {
|
|
2508
|
+
sendAfterClose(this, data, cb);
|
|
2509
|
+
return;
|
|
2510
|
+
}
|
|
2511
|
+
if (mask === void 0) mask = !this._isServer;
|
|
2512
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
2513
|
+
}
|
|
2514
|
+
/**
|
|
2515
|
+
* Send a pong.
|
|
2516
|
+
*
|
|
2517
|
+
* @param {*} [data] The data to send
|
|
2518
|
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
|
2519
|
+
* @param {Function} [cb] Callback which is executed when the pong is sent
|
|
2520
|
+
* @public
|
|
2521
|
+
*/
|
|
2522
|
+
pong(data, mask, cb) {
|
|
2523
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2524
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2525
|
+
}
|
|
2526
|
+
if (typeof data === "function") {
|
|
2527
|
+
cb = data;
|
|
2528
|
+
data = mask = void 0;
|
|
2529
|
+
} else if (typeof mask === "function") {
|
|
2530
|
+
cb = mask;
|
|
2531
|
+
mask = void 0;
|
|
2532
|
+
}
|
|
2533
|
+
if (typeof data === "number") data = data.toString();
|
|
2534
|
+
if (this.readyState !== _WebSocket.OPEN) {
|
|
2535
|
+
sendAfterClose(this, data, cb);
|
|
2536
|
+
return;
|
|
2537
|
+
}
|
|
2538
|
+
if (mask === void 0) mask = !this._isServer;
|
|
2539
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
2540
|
+
}
|
|
2541
|
+
/**
|
|
2542
|
+
* Resume the socket.
|
|
2543
|
+
*
|
|
2544
|
+
* @public
|
|
2545
|
+
*/
|
|
2546
|
+
resume() {
|
|
2547
|
+
if (this.readyState === _WebSocket.CONNECTING || this.readyState === _WebSocket.CLOSED) {
|
|
2548
|
+
return;
|
|
2549
|
+
}
|
|
2550
|
+
this._paused = false;
|
|
2551
|
+
if (!this._receiver._writableState.needDrain) this._socket.resume();
|
|
2552
|
+
}
|
|
2553
|
+
/**
|
|
2554
|
+
* Send a data message.
|
|
2555
|
+
*
|
|
2556
|
+
* @param {*} data The message to send
|
|
2557
|
+
* @param {Object} [options] Options object
|
|
2558
|
+
* @param {Boolean} [options.binary] Specifies whether `data` is binary or
|
|
2559
|
+
* text
|
|
2560
|
+
* @param {Boolean} [options.compress] Specifies whether or not to compress
|
|
2561
|
+
* `data`
|
|
2562
|
+
* @param {Boolean} [options.fin=true] Specifies whether the fragment is the
|
|
2563
|
+
* last one
|
|
2564
|
+
* @param {Boolean} [options.mask] Specifies whether or not to mask `data`
|
|
2565
|
+
* @param {Function} [cb] Callback which is executed when data is written out
|
|
2566
|
+
* @public
|
|
2567
|
+
*/
|
|
2568
|
+
send(data, options, cb) {
|
|
2569
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2570
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2571
|
+
}
|
|
2572
|
+
if (typeof options === "function") {
|
|
2573
|
+
cb = options;
|
|
2574
|
+
options = {};
|
|
2575
|
+
}
|
|
2576
|
+
if (typeof data === "number") data = data.toString();
|
|
2577
|
+
if (this.readyState !== _WebSocket.OPEN) {
|
|
2578
|
+
sendAfterClose(this, data, cb);
|
|
2579
|
+
return;
|
|
2580
|
+
}
|
|
2581
|
+
const opts = {
|
|
2582
|
+
binary: typeof data !== "string",
|
|
2583
|
+
mask: !this._isServer,
|
|
2584
|
+
compress: true,
|
|
2585
|
+
fin: true,
|
|
2586
|
+
...options
|
|
2587
|
+
};
|
|
2588
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
2589
|
+
opts.compress = false;
|
|
2590
|
+
}
|
|
2591
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
2592
|
+
}
|
|
2593
|
+
/**
|
|
2594
|
+
* Forcibly close the connection.
|
|
2595
|
+
*
|
|
2596
|
+
* @public
|
|
2597
|
+
*/
|
|
2598
|
+
terminate() {
|
|
2599
|
+
if (this.readyState === _WebSocket.CLOSED) return;
|
|
2600
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2601
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
2602
|
+
abortHandshake(this, this._req, msg);
|
|
2603
|
+
return;
|
|
2604
|
+
}
|
|
2605
|
+
if (this._socket) {
|
|
2606
|
+
this._readyState = _WebSocket.CLOSING;
|
|
2607
|
+
this._socket.destroy();
|
|
2608
|
+
}
|
|
2609
|
+
}
|
|
2610
|
+
};
|
|
2611
|
+
Object.defineProperty(WebSocket2, "CONNECTING", {
|
|
2612
|
+
enumerable: true,
|
|
2613
|
+
value: readyStates.indexOf("CONNECTING")
|
|
2614
|
+
});
|
|
2615
|
+
Object.defineProperty(WebSocket2.prototype, "CONNECTING", {
|
|
2616
|
+
enumerable: true,
|
|
2617
|
+
value: readyStates.indexOf("CONNECTING")
|
|
2618
|
+
});
|
|
2619
|
+
Object.defineProperty(WebSocket2, "OPEN", {
|
|
2620
|
+
enumerable: true,
|
|
2621
|
+
value: readyStates.indexOf("OPEN")
|
|
2622
|
+
});
|
|
2623
|
+
Object.defineProperty(WebSocket2.prototype, "OPEN", {
|
|
2624
|
+
enumerable: true,
|
|
2625
|
+
value: readyStates.indexOf("OPEN")
|
|
2626
|
+
});
|
|
2627
|
+
Object.defineProperty(WebSocket2, "CLOSING", {
|
|
2628
|
+
enumerable: true,
|
|
2629
|
+
value: readyStates.indexOf("CLOSING")
|
|
2630
|
+
});
|
|
2631
|
+
Object.defineProperty(WebSocket2.prototype, "CLOSING", {
|
|
2632
|
+
enumerable: true,
|
|
2633
|
+
value: readyStates.indexOf("CLOSING")
|
|
2634
|
+
});
|
|
2635
|
+
Object.defineProperty(WebSocket2, "CLOSED", {
|
|
2636
|
+
enumerable: true,
|
|
2637
|
+
value: readyStates.indexOf("CLOSED")
|
|
2638
|
+
});
|
|
2639
|
+
Object.defineProperty(WebSocket2.prototype, "CLOSED", {
|
|
2640
|
+
enumerable: true,
|
|
2641
|
+
value: readyStates.indexOf("CLOSED")
|
|
2642
|
+
});
|
|
2643
|
+
[
|
|
2644
|
+
"binaryType",
|
|
2645
|
+
"bufferedAmount",
|
|
2646
|
+
"extensions",
|
|
2647
|
+
"isPaused",
|
|
2648
|
+
"protocol",
|
|
2649
|
+
"readyState",
|
|
2650
|
+
"url"
|
|
2651
|
+
].forEach((property) => {
|
|
2652
|
+
Object.defineProperty(WebSocket2.prototype, property, { enumerable: true });
|
|
2653
|
+
});
|
|
2654
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
2655
|
+
Object.defineProperty(WebSocket2.prototype, `on${method}`, {
|
|
2656
|
+
enumerable: true,
|
|
2657
|
+
get() {
|
|
2658
|
+
for (const listener of this.listeners(method)) {
|
|
2659
|
+
if (listener[kForOnEventAttribute]) return listener[kListener];
|
|
2660
|
+
}
|
|
2661
|
+
return null;
|
|
2662
|
+
},
|
|
2663
|
+
set(handler) {
|
|
2664
|
+
for (const listener of this.listeners(method)) {
|
|
2665
|
+
if (listener[kForOnEventAttribute]) {
|
|
2666
|
+
this.removeListener(method, listener);
|
|
2667
|
+
break;
|
|
2668
|
+
}
|
|
2669
|
+
}
|
|
2670
|
+
if (typeof handler !== "function") return;
|
|
2671
|
+
this.addEventListener(method, handler, {
|
|
2672
|
+
[kForOnEventAttribute]: true
|
|
2673
|
+
});
|
|
2674
|
+
}
|
|
2675
|
+
});
|
|
2676
|
+
});
|
|
2677
|
+
WebSocket2.prototype.addEventListener = addEventListener;
|
|
2678
|
+
WebSocket2.prototype.removeEventListener = removeEventListener;
|
|
2679
|
+
module.exports = WebSocket2;
|
|
2680
|
+
function initAsClient(websocket, address, protocols, options) {
|
|
2681
|
+
const opts = {
|
|
2682
|
+
allowSynchronousEvents: true,
|
|
2683
|
+
autoPong: true,
|
|
2684
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
2685
|
+
protocolVersion: protocolVersions[1],
|
|
2686
|
+
maxPayload: 100 * 1024 * 1024,
|
|
2687
|
+
skipUTF8Validation: false,
|
|
2688
|
+
perMessageDeflate: true,
|
|
2689
|
+
followRedirects: false,
|
|
2690
|
+
maxRedirects: 10,
|
|
2691
|
+
...options,
|
|
2692
|
+
socketPath: void 0,
|
|
2693
|
+
hostname: void 0,
|
|
2694
|
+
protocol: void 0,
|
|
2695
|
+
timeout: void 0,
|
|
2696
|
+
method: "GET",
|
|
2697
|
+
host: void 0,
|
|
2698
|
+
path: void 0,
|
|
2699
|
+
port: void 0
|
|
2700
|
+
};
|
|
2701
|
+
websocket._autoPong = opts.autoPong;
|
|
2702
|
+
websocket._closeTimeout = opts.closeTimeout;
|
|
2703
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
2704
|
+
throw new RangeError(
|
|
2705
|
+
`Unsupported protocol version: ${opts.protocolVersion} (supported versions: ${protocolVersions.join(", ")})`
|
|
2706
|
+
);
|
|
2707
|
+
}
|
|
2708
|
+
let parsedUrl;
|
|
2709
|
+
if (address instanceof URL2) {
|
|
2710
|
+
parsedUrl = address;
|
|
2711
|
+
} else {
|
|
2712
|
+
try {
|
|
2713
|
+
parsedUrl = new URL2(address);
|
|
2714
|
+
} catch (e) {
|
|
2715
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
2718
|
+
if (parsedUrl.protocol === "http:") {
|
|
2719
|
+
parsedUrl.protocol = "ws:";
|
|
2720
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
2721
|
+
parsedUrl.protocol = "wss:";
|
|
2722
|
+
}
|
|
2723
|
+
websocket._url = parsedUrl.href;
|
|
2724
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
2725
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
2726
|
+
let invalidUrlMessage;
|
|
2727
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
2728
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", "http:", "https:", or "ws+unix:"`;
|
|
2729
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
2730
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
2731
|
+
} else if (parsedUrl.hash) {
|
|
2732
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
2733
|
+
}
|
|
2734
|
+
if (invalidUrlMessage) {
|
|
2735
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
2736
|
+
if (websocket._redirects === 0) {
|
|
2737
|
+
throw err;
|
|
2738
|
+
} else {
|
|
2739
|
+
emitErrorAndClose(websocket, err);
|
|
2740
|
+
return;
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
2744
|
+
const key = randomBytes(16).toString("base64");
|
|
2745
|
+
const request = isSecure ? https.request : http.request;
|
|
2746
|
+
const protocolSet = /* @__PURE__ */ new Set();
|
|
2747
|
+
let perMessageDeflate;
|
|
2748
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
2749
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
2750
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
2751
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
2752
|
+
opts.headers = {
|
|
2753
|
+
...opts.headers,
|
|
2754
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
2755
|
+
"Sec-WebSocket-Key": key,
|
|
2756
|
+
Connection: "Upgrade",
|
|
2757
|
+
Upgrade: "websocket"
|
|
2758
|
+
};
|
|
2759
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
2760
|
+
opts.timeout = opts.handshakeTimeout;
|
|
2761
|
+
if (opts.perMessageDeflate) {
|
|
2762
|
+
perMessageDeflate = new PerMessageDeflate(
|
|
2763
|
+
opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
|
|
2764
|
+
false,
|
|
2765
|
+
opts.maxPayload
|
|
2766
|
+
);
|
|
2767
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({
|
|
2768
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
2769
|
+
});
|
|
2770
|
+
}
|
|
2771
|
+
if (protocols.length) {
|
|
2772
|
+
for (const protocol of protocols) {
|
|
2773
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
2774
|
+
throw new SyntaxError(
|
|
2775
|
+
"An invalid or duplicated subprotocol was specified"
|
|
2776
|
+
);
|
|
2777
|
+
}
|
|
2778
|
+
protocolSet.add(protocol);
|
|
2779
|
+
}
|
|
2780
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
2781
|
+
}
|
|
2782
|
+
if (opts.origin) {
|
|
2783
|
+
if (opts.protocolVersion < 13) {
|
|
2784
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
2785
|
+
} else {
|
|
2786
|
+
opts.headers.Origin = opts.origin;
|
|
2787
|
+
}
|
|
2788
|
+
}
|
|
2789
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
2790
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
2791
|
+
}
|
|
2792
|
+
if (isIpcUrl) {
|
|
2793
|
+
const parts = opts.path.split(":");
|
|
2794
|
+
opts.socketPath = parts[0];
|
|
2795
|
+
opts.path = parts[1];
|
|
2796
|
+
}
|
|
2797
|
+
let req;
|
|
2798
|
+
if (opts.followRedirects) {
|
|
2799
|
+
if (websocket._redirects === 0) {
|
|
2800
|
+
websocket._originalIpc = isIpcUrl;
|
|
2801
|
+
websocket._originalSecure = isSecure;
|
|
2802
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
2803
|
+
const headers = options && options.headers;
|
|
2804
|
+
options = { ...options, headers: {} };
|
|
2805
|
+
if (headers) {
|
|
2806
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
2807
|
+
options.headers[key2.toLowerCase()] = value;
|
|
2808
|
+
}
|
|
2809
|
+
}
|
|
2810
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
2811
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
2812
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
2813
|
+
delete opts.headers.authorization;
|
|
2814
|
+
delete opts.headers.cookie;
|
|
2815
|
+
if (!isSameHost) delete opts.headers.host;
|
|
2816
|
+
opts.auth = void 0;
|
|
2817
|
+
}
|
|
2818
|
+
}
|
|
2819
|
+
if (opts.auth && !options.headers.authorization) {
|
|
2820
|
+
options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
2821
|
+
}
|
|
2822
|
+
req = websocket._req = request(opts);
|
|
2823
|
+
if (websocket._redirects) {
|
|
2824
|
+
websocket.emit("redirect", websocket.url, req);
|
|
2825
|
+
}
|
|
2826
|
+
} else {
|
|
2827
|
+
req = websocket._req = request(opts);
|
|
2828
|
+
}
|
|
2829
|
+
if (opts.timeout) {
|
|
2830
|
+
req.on("timeout", () => {
|
|
2831
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
2832
|
+
});
|
|
2833
|
+
}
|
|
2834
|
+
req.on("error", (err) => {
|
|
2835
|
+
if (req === null || req[kAborted]) return;
|
|
2836
|
+
req = websocket._req = null;
|
|
2837
|
+
emitErrorAndClose(websocket, err);
|
|
2838
|
+
});
|
|
2839
|
+
req.on("response", (res) => {
|
|
2840
|
+
const location = res.headers.location;
|
|
2841
|
+
const statusCode = res.statusCode;
|
|
2842
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
2843
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
2844
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
2845
|
+
return;
|
|
2846
|
+
}
|
|
2847
|
+
req.abort();
|
|
2848
|
+
let addr;
|
|
2849
|
+
try {
|
|
2850
|
+
addr = new URL2(location, address);
|
|
2851
|
+
} catch (e) {
|
|
2852
|
+
const err = new SyntaxError(`Invalid URL: ${location}`);
|
|
2853
|
+
emitErrorAndClose(websocket, err);
|
|
2854
|
+
return;
|
|
2855
|
+
}
|
|
2856
|
+
initAsClient(websocket, addr, protocols, options);
|
|
2857
|
+
} else if (!websocket.emit("unexpected-response", req, res)) {
|
|
2858
|
+
abortHandshake(
|
|
2859
|
+
websocket,
|
|
2860
|
+
req,
|
|
2861
|
+
`Unexpected server response: ${res.statusCode}`
|
|
2862
|
+
);
|
|
2863
|
+
}
|
|
2864
|
+
});
|
|
2865
|
+
req.on("upgrade", (res, socket, head) => {
|
|
2866
|
+
websocket.emit("upgrade", res);
|
|
2867
|
+
if (websocket.readyState !== WebSocket2.CONNECTING) return;
|
|
2868
|
+
req = websocket._req = null;
|
|
2869
|
+
const upgrade = res.headers.upgrade;
|
|
2870
|
+
if (upgrade === void 0 || upgrade.toLowerCase() !== "websocket") {
|
|
2871
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
2872
|
+
return;
|
|
2873
|
+
}
|
|
2874
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
2875
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
2876
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
2877
|
+
return;
|
|
2878
|
+
}
|
|
2879
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
2880
|
+
let protError;
|
|
2881
|
+
if (serverProt !== void 0) {
|
|
2882
|
+
if (!protocolSet.size) {
|
|
2883
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
2884
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
2885
|
+
protError = "Server sent an invalid subprotocol";
|
|
2886
|
+
}
|
|
2887
|
+
} else if (protocolSet.size) {
|
|
2888
|
+
protError = "Server sent no subprotocol";
|
|
2889
|
+
}
|
|
2890
|
+
if (protError) {
|
|
2891
|
+
abortHandshake(websocket, socket, protError);
|
|
2892
|
+
return;
|
|
2893
|
+
}
|
|
2894
|
+
if (serverProt) websocket._protocol = serverProt;
|
|
2895
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
2896
|
+
if (secWebSocketExtensions !== void 0) {
|
|
2897
|
+
if (!perMessageDeflate) {
|
|
2898
|
+
const message = "Server sent a Sec-WebSocket-Extensions header but no extension was requested";
|
|
2899
|
+
abortHandshake(websocket, socket, message);
|
|
2900
|
+
return;
|
|
2901
|
+
}
|
|
2902
|
+
let extensions;
|
|
2903
|
+
try {
|
|
2904
|
+
extensions = parse(secWebSocketExtensions);
|
|
2905
|
+
} catch (err) {
|
|
2906
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
2907
|
+
abortHandshake(websocket, socket, message);
|
|
2908
|
+
return;
|
|
2909
|
+
}
|
|
2910
|
+
const extensionNames = Object.keys(extensions);
|
|
2911
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
2912
|
+
const message = "Server indicated an extension that was not requested";
|
|
2913
|
+
abortHandshake(websocket, socket, message);
|
|
2914
|
+
return;
|
|
2915
|
+
}
|
|
2916
|
+
try {
|
|
2917
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
2918
|
+
} catch (err) {
|
|
2919
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
2920
|
+
abortHandshake(websocket, socket, message);
|
|
2921
|
+
return;
|
|
2922
|
+
}
|
|
2923
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
2924
|
+
}
|
|
2925
|
+
websocket.setSocket(socket, head, {
|
|
2926
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
2927
|
+
generateMask: opts.generateMask,
|
|
2928
|
+
maxPayload: opts.maxPayload,
|
|
2929
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
2930
|
+
});
|
|
2931
|
+
});
|
|
2932
|
+
if (opts.finishRequest) {
|
|
2933
|
+
opts.finishRequest(req, websocket);
|
|
2934
|
+
} else {
|
|
2935
|
+
req.end();
|
|
2936
|
+
}
|
|
2937
|
+
}
|
|
2938
|
+
function emitErrorAndClose(websocket, err) {
|
|
2939
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
2940
|
+
websocket._errorEmitted = true;
|
|
2941
|
+
websocket.emit("error", err);
|
|
2942
|
+
websocket.emitClose();
|
|
2943
|
+
}
|
|
2944
|
+
function netConnect(options) {
|
|
2945
|
+
options.path = options.socketPath;
|
|
2946
|
+
return net.connect(options);
|
|
2947
|
+
}
|
|
2948
|
+
function tlsConnect(options) {
|
|
2949
|
+
options.path = void 0;
|
|
2950
|
+
if (!options.servername && options.servername !== "") {
|
|
2951
|
+
options.servername = net.isIP(options.host) ? "" : options.host;
|
|
2952
|
+
}
|
|
2953
|
+
return tls.connect(options);
|
|
2954
|
+
}
|
|
2955
|
+
function abortHandshake(websocket, stream, message) {
|
|
2956
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
2957
|
+
const err = new Error(message);
|
|
2958
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
2959
|
+
if (stream.setHeader) {
|
|
2960
|
+
stream[kAborted] = true;
|
|
2961
|
+
stream.abort();
|
|
2962
|
+
if (stream.socket && !stream.socket.destroyed) {
|
|
2963
|
+
stream.socket.destroy();
|
|
2964
|
+
}
|
|
2965
|
+
process.nextTick(emitErrorAndClose, websocket, err);
|
|
2966
|
+
} else {
|
|
2967
|
+
stream.destroy(err);
|
|
2968
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
2969
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
2970
|
+
}
|
|
2971
|
+
}
|
|
2972
|
+
function sendAfterClose(websocket, data, cb) {
|
|
2973
|
+
if (data) {
|
|
2974
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
2975
|
+
if (websocket._socket) websocket._sender._bufferedBytes += length;
|
|
2976
|
+
else websocket._bufferedAmount += length;
|
|
2977
|
+
}
|
|
2978
|
+
if (cb) {
|
|
2979
|
+
const err = new Error(
|
|
2980
|
+
`WebSocket is not open: readyState ${websocket.readyState} (${readyStates[websocket.readyState]})`
|
|
2981
|
+
);
|
|
2982
|
+
process.nextTick(cb, err);
|
|
2983
|
+
}
|
|
2984
|
+
}
|
|
2985
|
+
function receiverOnConclude(code, reason) {
|
|
2986
|
+
const websocket = this[kWebSocket];
|
|
2987
|
+
websocket._closeFrameReceived = true;
|
|
2988
|
+
websocket._closeMessage = reason;
|
|
2989
|
+
websocket._closeCode = code;
|
|
2990
|
+
if (websocket._socket[kWebSocket] === void 0) return;
|
|
2991
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
2992
|
+
process.nextTick(resume, websocket._socket);
|
|
2993
|
+
if (code === 1005) websocket.close();
|
|
2994
|
+
else websocket.close(code, reason);
|
|
2995
|
+
}
|
|
2996
|
+
function receiverOnDrain() {
|
|
2997
|
+
const websocket = this[kWebSocket];
|
|
2998
|
+
if (!websocket.isPaused) websocket._socket.resume();
|
|
2999
|
+
}
|
|
3000
|
+
function receiverOnError(err) {
|
|
3001
|
+
const websocket = this[kWebSocket];
|
|
3002
|
+
if (websocket._socket[kWebSocket] !== void 0) {
|
|
3003
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
3004
|
+
process.nextTick(resume, websocket._socket);
|
|
3005
|
+
websocket.close(err[kStatusCode]);
|
|
3006
|
+
}
|
|
3007
|
+
if (!websocket._errorEmitted) {
|
|
3008
|
+
websocket._errorEmitted = true;
|
|
3009
|
+
websocket.emit("error", err);
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
function receiverOnFinish() {
|
|
3013
|
+
this[kWebSocket].emitClose();
|
|
3014
|
+
}
|
|
3015
|
+
function receiverOnMessage(data, isBinary) {
|
|
3016
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
3017
|
+
}
|
|
3018
|
+
function receiverOnPing(data) {
|
|
3019
|
+
const websocket = this[kWebSocket];
|
|
3020
|
+
if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP);
|
|
3021
|
+
websocket.emit("ping", data);
|
|
3022
|
+
}
|
|
3023
|
+
function receiverOnPong(data) {
|
|
3024
|
+
this[kWebSocket].emit("pong", data);
|
|
3025
|
+
}
|
|
3026
|
+
function resume(stream) {
|
|
3027
|
+
stream.resume();
|
|
3028
|
+
}
|
|
3029
|
+
function senderOnError(err) {
|
|
3030
|
+
const websocket = this[kWebSocket];
|
|
3031
|
+
if (websocket.readyState === WebSocket2.CLOSED) return;
|
|
3032
|
+
if (websocket.readyState === WebSocket2.OPEN) {
|
|
3033
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3034
|
+
setCloseTimer(websocket);
|
|
3035
|
+
}
|
|
3036
|
+
this._socket.end();
|
|
3037
|
+
if (!websocket._errorEmitted) {
|
|
3038
|
+
websocket._errorEmitted = true;
|
|
3039
|
+
websocket.emit("error", err);
|
|
3040
|
+
}
|
|
3041
|
+
}
|
|
3042
|
+
function setCloseTimer(websocket) {
|
|
3043
|
+
websocket._closeTimer = setTimeout(
|
|
3044
|
+
websocket._socket.destroy.bind(websocket._socket),
|
|
3045
|
+
websocket._closeTimeout
|
|
3046
|
+
);
|
|
3047
|
+
}
|
|
3048
|
+
function socketOnClose() {
|
|
3049
|
+
const websocket = this[kWebSocket];
|
|
3050
|
+
this.removeListener("close", socketOnClose);
|
|
3051
|
+
this.removeListener("data", socketOnData);
|
|
3052
|
+
this.removeListener("end", socketOnEnd);
|
|
3053
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3054
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
|
|
3055
|
+
const chunk = this.read(this._readableState.length);
|
|
3056
|
+
websocket._receiver.write(chunk);
|
|
3057
|
+
}
|
|
3058
|
+
websocket._receiver.end();
|
|
3059
|
+
this[kWebSocket] = void 0;
|
|
3060
|
+
clearTimeout(websocket._closeTimer);
|
|
3061
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
|
|
3062
|
+
websocket.emitClose();
|
|
3063
|
+
} else {
|
|
3064
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
3065
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
function socketOnData(chunk) {
|
|
3069
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
3070
|
+
this.pause();
|
|
3071
|
+
}
|
|
3072
|
+
}
|
|
3073
|
+
function socketOnEnd() {
|
|
3074
|
+
const websocket = this[kWebSocket];
|
|
3075
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3076
|
+
websocket._receiver.end();
|
|
3077
|
+
this.end();
|
|
3078
|
+
}
|
|
3079
|
+
function socketOnError() {
|
|
3080
|
+
const websocket = this[kWebSocket];
|
|
3081
|
+
this.removeListener("error", socketOnError);
|
|
3082
|
+
this.on("error", NOOP);
|
|
3083
|
+
if (websocket) {
|
|
3084
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3085
|
+
this.destroy();
|
|
3086
|
+
}
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
});
|
|
3090
|
+
|
|
3091
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/stream.js
|
|
3092
|
+
var require_stream = __commonJS({
|
|
3093
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/stream.js"(exports, module) {
|
|
3094
|
+
"use strict";
|
|
3095
|
+
var WebSocket2 = require_websocket();
|
|
3096
|
+
var { Duplex } = __require("stream");
|
|
3097
|
+
function emitClose(stream) {
|
|
3098
|
+
stream.emit("close");
|
|
3099
|
+
}
|
|
3100
|
+
function duplexOnEnd() {
|
|
3101
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
3102
|
+
this.destroy();
|
|
3103
|
+
}
|
|
3104
|
+
}
|
|
3105
|
+
function duplexOnError(err) {
|
|
3106
|
+
this.removeListener("error", duplexOnError);
|
|
3107
|
+
this.destroy();
|
|
3108
|
+
if (this.listenerCount("error") === 0) {
|
|
3109
|
+
this.emit("error", err);
|
|
3110
|
+
}
|
|
3111
|
+
}
|
|
3112
|
+
function createWebSocketStream2(ws, options) {
|
|
3113
|
+
let terminateOnDestroy = true;
|
|
3114
|
+
const duplex = new Duplex({
|
|
3115
|
+
...options,
|
|
3116
|
+
autoDestroy: false,
|
|
3117
|
+
emitClose: false,
|
|
3118
|
+
objectMode: false,
|
|
3119
|
+
writableObjectMode: false
|
|
3120
|
+
});
|
|
3121
|
+
ws.on("message", function message(msg, isBinary) {
|
|
3122
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
3123
|
+
if (!duplex.push(data)) ws.pause();
|
|
3124
|
+
});
|
|
3125
|
+
ws.once("error", function error(err) {
|
|
3126
|
+
if (duplex.destroyed) return;
|
|
3127
|
+
terminateOnDestroy = false;
|
|
3128
|
+
duplex.destroy(err);
|
|
3129
|
+
});
|
|
3130
|
+
ws.once("close", function close() {
|
|
3131
|
+
if (duplex.destroyed) return;
|
|
3132
|
+
duplex.push(null);
|
|
3133
|
+
});
|
|
3134
|
+
duplex._destroy = function(err, callback) {
|
|
3135
|
+
if (ws.readyState === ws.CLOSED) {
|
|
3136
|
+
callback(err);
|
|
3137
|
+
process.nextTick(emitClose, duplex);
|
|
3138
|
+
return;
|
|
3139
|
+
}
|
|
3140
|
+
let called = false;
|
|
3141
|
+
ws.once("error", function error(err2) {
|
|
3142
|
+
called = true;
|
|
3143
|
+
callback(err2);
|
|
3144
|
+
});
|
|
3145
|
+
ws.once("close", function close() {
|
|
3146
|
+
if (!called) callback(err);
|
|
3147
|
+
process.nextTick(emitClose, duplex);
|
|
3148
|
+
});
|
|
3149
|
+
if (terminateOnDestroy) ws.terminate();
|
|
3150
|
+
};
|
|
3151
|
+
duplex._final = function(callback) {
|
|
3152
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
3153
|
+
ws.once("open", function open() {
|
|
3154
|
+
duplex._final(callback);
|
|
3155
|
+
});
|
|
3156
|
+
return;
|
|
3157
|
+
}
|
|
3158
|
+
if (ws._socket === null) return;
|
|
3159
|
+
if (ws._socket._writableState.finished) {
|
|
3160
|
+
callback();
|
|
3161
|
+
if (duplex._readableState.endEmitted) duplex.destroy();
|
|
3162
|
+
} else {
|
|
3163
|
+
ws._socket.once("finish", function finish() {
|
|
3164
|
+
callback();
|
|
3165
|
+
});
|
|
3166
|
+
ws.close();
|
|
3167
|
+
}
|
|
3168
|
+
};
|
|
3169
|
+
duplex._read = function() {
|
|
3170
|
+
if (ws.isPaused) ws.resume();
|
|
3171
|
+
};
|
|
3172
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
3173
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
3174
|
+
ws.once("open", function open() {
|
|
3175
|
+
duplex._write(chunk, encoding, callback);
|
|
3176
|
+
});
|
|
3177
|
+
return;
|
|
3178
|
+
}
|
|
3179
|
+
ws.send(chunk, callback);
|
|
3180
|
+
};
|
|
3181
|
+
duplex.on("end", duplexOnEnd);
|
|
3182
|
+
duplex.on("error", duplexOnError);
|
|
3183
|
+
return duplex;
|
|
3184
|
+
}
|
|
3185
|
+
module.exports = createWebSocketStream2;
|
|
3186
|
+
}
|
|
3187
|
+
});
|
|
3188
|
+
|
|
3189
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/subprotocol.js
|
|
3190
|
+
var require_subprotocol = __commonJS({
|
|
3191
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/subprotocol.js"(exports, module) {
|
|
3192
|
+
"use strict";
|
|
3193
|
+
var { tokenChars } = require_validation();
|
|
3194
|
+
function parse(header) {
|
|
3195
|
+
const protocols = /* @__PURE__ */ new Set();
|
|
3196
|
+
let start = -1;
|
|
3197
|
+
let end = -1;
|
|
3198
|
+
let i = 0;
|
|
3199
|
+
for (i; i < header.length; i++) {
|
|
3200
|
+
const code = header.charCodeAt(i);
|
|
3201
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
3202
|
+
if (start === -1) start = i;
|
|
3203
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
3204
|
+
if (end === -1 && start !== -1) end = i;
|
|
3205
|
+
} else if (code === 44) {
|
|
3206
|
+
if (start === -1) {
|
|
3207
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
3208
|
+
}
|
|
3209
|
+
if (end === -1) end = i;
|
|
3210
|
+
const protocol2 = header.slice(start, end);
|
|
3211
|
+
if (protocols.has(protocol2)) {
|
|
3212
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
3213
|
+
}
|
|
3214
|
+
protocols.add(protocol2);
|
|
3215
|
+
start = end = -1;
|
|
3216
|
+
} else {
|
|
3217
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
3218
|
+
}
|
|
3219
|
+
}
|
|
3220
|
+
if (start === -1 || end !== -1) {
|
|
3221
|
+
throw new SyntaxError("Unexpected end of input");
|
|
3222
|
+
}
|
|
3223
|
+
const protocol = header.slice(start, i);
|
|
3224
|
+
if (protocols.has(protocol)) {
|
|
3225
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
3226
|
+
}
|
|
3227
|
+
protocols.add(protocol);
|
|
3228
|
+
return protocols;
|
|
3229
|
+
}
|
|
3230
|
+
module.exports = { parse };
|
|
3231
|
+
}
|
|
3232
|
+
});
|
|
3233
|
+
|
|
3234
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/websocket-server.js
|
|
3235
|
+
var require_websocket_server = __commonJS({
|
|
3236
|
+
"node_modules/.pnpm/ws@8.19.0/node_modules/ws/lib/websocket-server.js"(exports, module) {
|
|
3237
|
+
"use strict";
|
|
3238
|
+
var EventEmitter = __require("events");
|
|
3239
|
+
var http = __require("http");
|
|
3240
|
+
var { Duplex } = __require("stream");
|
|
3241
|
+
var { createHash } = __require("crypto");
|
|
3242
|
+
var extension = require_extension();
|
|
3243
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
3244
|
+
var subprotocol = require_subprotocol();
|
|
3245
|
+
var WebSocket2 = require_websocket();
|
|
3246
|
+
var { CLOSE_TIMEOUT, GUID, kWebSocket } = require_constants();
|
|
3247
|
+
var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
3248
|
+
var RUNNING = 0;
|
|
3249
|
+
var CLOSING = 1;
|
|
3250
|
+
var CLOSED = 2;
|
|
3251
|
+
var WebSocketServer2 = class extends EventEmitter {
|
|
3252
|
+
/**
|
|
3253
|
+
* Create a `WebSocketServer` instance.
|
|
3254
|
+
*
|
|
3255
|
+
* @param {Object} options Configuration options
|
|
3256
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
|
|
3257
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
3258
|
+
* multiple times in the same tick
|
|
3259
|
+
* @param {Boolean} [options.autoPong=true] Specifies whether or not to
|
|
3260
|
+
* automatically send a pong in response to a ping
|
|
3261
|
+
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
|
3262
|
+
* pending connections
|
|
3263
|
+
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
|
3264
|
+
* track clients
|
|
3265
|
+
* @param {Number} [options.closeTimeout=30000] Duration in milliseconds to
|
|
3266
|
+
* wait for the closing handshake to finish after `websocket.close()` is
|
|
3267
|
+
* called
|
|
3268
|
+
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
|
3269
|
+
* @param {String} [options.host] The hostname where to bind the server
|
|
3270
|
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
|
3271
|
+
* size
|
|
3272
|
+
* @param {Boolean} [options.noServer=false] Enable no server mode
|
|
3273
|
+
* @param {String} [options.path] Accept only connections matching this path
|
|
3274
|
+
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
|
3275
|
+
* permessage-deflate
|
|
3276
|
+
* @param {Number} [options.port] The port where to bind the server
|
|
3277
|
+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
|
3278
|
+
* server to use
|
|
3279
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
3280
|
+
* not to skip UTF-8 validation for text and close messages
|
|
3281
|
+
* @param {Function} [options.verifyClient] A hook to reject connections
|
|
3282
|
+
* @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
|
|
3283
|
+
* class to use. It must be the `WebSocket` class or class that extends it
|
|
3284
|
+
* @param {Function} [callback] A listener for the `listening` event
|
|
3285
|
+
*/
|
|
3286
|
+
constructor(options, callback) {
|
|
3287
|
+
super();
|
|
3288
|
+
options = {
|
|
3289
|
+
allowSynchronousEvents: true,
|
|
3290
|
+
autoPong: true,
|
|
3291
|
+
maxPayload: 100 * 1024 * 1024,
|
|
3292
|
+
skipUTF8Validation: false,
|
|
3293
|
+
perMessageDeflate: false,
|
|
3294
|
+
handleProtocols: null,
|
|
3295
|
+
clientTracking: true,
|
|
3296
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
3297
|
+
verifyClient: null,
|
|
3298
|
+
noServer: false,
|
|
3299
|
+
backlog: null,
|
|
3300
|
+
// use default (511 as implemented in net.js)
|
|
3301
|
+
server: null,
|
|
3302
|
+
host: null,
|
|
3303
|
+
path: null,
|
|
3304
|
+
port: null,
|
|
3305
|
+
WebSocket: WebSocket2,
|
|
3306
|
+
...options
|
|
3307
|
+
};
|
|
3308
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
|
|
3309
|
+
throw new TypeError(
|
|
3310
|
+
'One and only one of the "port", "server", or "noServer" options must be specified'
|
|
3311
|
+
);
|
|
3312
|
+
}
|
|
3313
|
+
if (options.port != null) {
|
|
3314
|
+
this._server = http.createServer((req, res) => {
|
|
3315
|
+
const body = http.STATUS_CODES[426];
|
|
3316
|
+
res.writeHead(426, {
|
|
3317
|
+
"Content-Length": body.length,
|
|
3318
|
+
"Content-Type": "text/plain"
|
|
3319
|
+
});
|
|
3320
|
+
res.end(body);
|
|
3321
|
+
});
|
|
3322
|
+
this._server.listen(
|
|
3323
|
+
options.port,
|
|
3324
|
+
options.host,
|
|
3325
|
+
options.backlog,
|
|
3326
|
+
callback
|
|
3327
|
+
);
|
|
3328
|
+
} else if (options.server) {
|
|
3329
|
+
this._server = options.server;
|
|
3330
|
+
}
|
|
3331
|
+
if (this._server) {
|
|
3332
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
3333
|
+
this._removeListeners = addListeners(this._server, {
|
|
3334
|
+
listening: this.emit.bind(this, "listening"),
|
|
3335
|
+
error: this.emit.bind(this, "error"),
|
|
3336
|
+
upgrade: (req, socket, head) => {
|
|
3337
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
3338
|
+
}
|
|
3339
|
+
});
|
|
3340
|
+
}
|
|
3341
|
+
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
|
|
3342
|
+
if (options.clientTracking) {
|
|
3343
|
+
this.clients = /* @__PURE__ */ new Set();
|
|
3344
|
+
this._shouldEmitClose = false;
|
|
3345
|
+
}
|
|
3346
|
+
this.options = options;
|
|
3347
|
+
this._state = RUNNING;
|
|
3348
|
+
}
|
|
3349
|
+
/**
|
|
3350
|
+
* Returns the bound address, the address family name, and port of the server
|
|
3351
|
+
* as reported by the operating system if listening on an IP socket.
|
|
3352
|
+
* If the server is listening on a pipe or UNIX domain socket, the name is
|
|
3353
|
+
* returned as a string.
|
|
3354
|
+
*
|
|
3355
|
+
* @return {(Object|String|null)} The address of the server
|
|
3356
|
+
* @public
|
|
3357
|
+
*/
|
|
3358
|
+
address() {
|
|
3359
|
+
if (this.options.noServer) {
|
|
3360
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
3361
|
+
}
|
|
3362
|
+
if (!this._server) return null;
|
|
3363
|
+
return this._server.address();
|
|
3364
|
+
}
|
|
3365
|
+
/**
|
|
3366
|
+
* Stop the server from accepting new connections and emit the `'close'` event
|
|
3367
|
+
* when all existing connections are closed.
|
|
3368
|
+
*
|
|
3369
|
+
* @param {Function} [cb] A one-time listener for the `'close'` event
|
|
3370
|
+
* @public
|
|
3371
|
+
*/
|
|
3372
|
+
close(cb) {
|
|
3373
|
+
if (this._state === CLOSED) {
|
|
3374
|
+
if (cb) {
|
|
3375
|
+
this.once("close", () => {
|
|
3376
|
+
cb(new Error("The server is not running"));
|
|
3377
|
+
});
|
|
3378
|
+
}
|
|
3379
|
+
process.nextTick(emitClose, this);
|
|
3380
|
+
return;
|
|
3381
|
+
}
|
|
3382
|
+
if (cb) this.once("close", cb);
|
|
3383
|
+
if (this._state === CLOSING) return;
|
|
3384
|
+
this._state = CLOSING;
|
|
3385
|
+
if (this.options.noServer || this.options.server) {
|
|
3386
|
+
if (this._server) {
|
|
3387
|
+
this._removeListeners();
|
|
3388
|
+
this._removeListeners = this._server = null;
|
|
3389
|
+
}
|
|
3390
|
+
if (this.clients) {
|
|
3391
|
+
if (!this.clients.size) {
|
|
3392
|
+
process.nextTick(emitClose, this);
|
|
3393
|
+
} else {
|
|
3394
|
+
this._shouldEmitClose = true;
|
|
3395
|
+
}
|
|
3396
|
+
} else {
|
|
3397
|
+
process.nextTick(emitClose, this);
|
|
3398
|
+
}
|
|
3399
|
+
} else {
|
|
3400
|
+
const server = this._server;
|
|
3401
|
+
this._removeListeners();
|
|
3402
|
+
this._removeListeners = this._server = null;
|
|
3403
|
+
server.close(() => {
|
|
3404
|
+
emitClose(this);
|
|
3405
|
+
});
|
|
3406
|
+
}
|
|
3407
|
+
}
|
|
3408
|
+
/**
|
|
3409
|
+
* See if a given request should be handled by this server instance.
|
|
3410
|
+
*
|
|
3411
|
+
* @param {http.IncomingMessage} req Request object to inspect
|
|
3412
|
+
* @return {Boolean} `true` if the request is valid, else `false`
|
|
3413
|
+
* @public
|
|
3414
|
+
*/
|
|
3415
|
+
shouldHandle(req) {
|
|
3416
|
+
if (this.options.path) {
|
|
3417
|
+
const index = req.url.indexOf("?");
|
|
3418
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
|
3419
|
+
if (pathname !== this.options.path) return false;
|
|
3420
|
+
}
|
|
3421
|
+
return true;
|
|
3422
|
+
}
|
|
3423
|
+
/**
|
|
3424
|
+
* Handle a HTTP Upgrade request.
|
|
3425
|
+
*
|
|
3426
|
+
* @param {http.IncomingMessage} req The request object
|
|
3427
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
3428
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
3429
|
+
* @param {Function} cb Callback
|
|
3430
|
+
* @public
|
|
3431
|
+
*/
|
|
3432
|
+
handleUpgrade(req, socket, head, cb) {
|
|
3433
|
+
socket.on("error", socketOnError);
|
|
3434
|
+
const key = req.headers["sec-websocket-key"];
|
|
3435
|
+
const upgrade = req.headers.upgrade;
|
|
3436
|
+
const version = +req.headers["sec-websocket-version"];
|
|
3437
|
+
if (req.method !== "GET") {
|
|
3438
|
+
const message = "Invalid HTTP method";
|
|
3439
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
|
|
3440
|
+
return;
|
|
3441
|
+
}
|
|
3442
|
+
if (upgrade === void 0 || upgrade.toLowerCase() !== "websocket") {
|
|
3443
|
+
const message = "Invalid Upgrade header";
|
|
3444
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3445
|
+
return;
|
|
3446
|
+
}
|
|
3447
|
+
if (key === void 0 || !keyRegex.test(key)) {
|
|
3448
|
+
const message = "Missing or invalid Sec-WebSocket-Key header";
|
|
3449
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3450
|
+
return;
|
|
3451
|
+
}
|
|
3452
|
+
if (version !== 13 && version !== 8) {
|
|
3453
|
+
const message = "Missing or invalid Sec-WebSocket-Version header";
|
|
3454
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
|
|
3455
|
+
"Sec-WebSocket-Version": "13, 8"
|
|
3456
|
+
});
|
|
3457
|
+
return;
|
|
3458
|
+
}
|
|
3459
|
+
if (!this.shouldHandle(req)) {
|
|
3460
|
+
abortHandshake(socket, 400);
|
|
3461
|
+
return;
|
|
3462
|
+
}
|
|
3463
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
3464
|
+
let protocols = /* @__PURE__ */ new Set();
|
|
3465
|
+
if (secWebSocketProtocol !== void 0) {
|
|
3466
|
+
try {
|
|
3467
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
3468
|
+
} catch (err) {
|
|
3469
|
+
const message = "Invalid Sec-WebSocket-Protocol header";
|
|
3470
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3471
|
+
return;
|
|
3472
|
+
}
|
|
3473
|
+
}
|
|
3474
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
3475
|
+
const extensions = {};
|
|
3476
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== void 0) {
|
|
3477
|
+
const perMessageDeflate = new PerMessageDeflate(
|
|
3478
|
+
this.options.perMessageDeflate,
|
|
3479
|
+
true,
|
|
3480
|
+
this.options.maxPayload
|
|
3481
|
+
);
|
|
3482
|
+
try {
|
|
3483
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
3484
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
3485
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
3486
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
3487
|
+
}
|
|
3488
|
+
} catch (err) {
|
|
3489
|
+
const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
3490
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3491
|
+
return;
|
|
3492
|
+
}
|
|
3493
|
+
}
|
|
3494
|
+
if (this.options.verifyClient) {
|
|
3495
|
+
const info = {
|
|
3496
|
+
origin: req.headers[`${version === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
3497
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
3498
|
+
req
|
|
3499
|
+
};
|
|
3500
|
+
if (this.options.verifyClient.length === 2) {
|
|
3501
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
3502
|
+
if (!verified) {
|
|
3503
|
+
return abortHandshake(socket, code || 401, message, headers);
|
|
3504
|
+
}
|
|
3505
|
+
this.completeUpgrade(
|
|
3506
|
+
extensions,
|
|
3507
|
+
key,
|
|
3508
|
+
protocols,
|
|
3509
|
+
req,
|
|
3510
|
+
socket,
|
|
3511
|
+
head,
|
|
3512
|
+
cb
|
|
3513
|
+
);
|
|
3514
|
+
});
|
|
3515
|
+
return;
|
|
3516
|
+
}
|
|
3517
|
+
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
|
3518
|
+
}
|
|
3519
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
3520
|
+
}
|
|
3521
|
+
/**
|
|
3522
|
+
* Upgrade the connection to WebSocket.
|
|
3523
|
+
*
|
|
3524
|
+
* @param {Object} extensions The accepted extensions
|
|
3525
|
+
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
|
3526
|
+
* @param {Set} protocols The subprotocols
|
|
3527
|
+
* @param {http.IncomingMessage} req The request object
|
|
3528
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
3529
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
3530
|
+
* @param {Function} cb Callback
|
|
3531
|
+
* @throws {Error} If called more than once with the same socket
|
|
3532
|
+
* @private
|
|
3533
|
+
*/
|
|
3534
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
3535
|
+
if (!socket.readable || !socket.writable) return socket.destroy();
|
|
3536
|
+
if (socket[kWebSocket]) {
|
|
3537
|
+
throw new Error(
|
|
3538
|
+
"server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration"
|
|
3539
|
+
);
|
|
3540
|
+
}
|
|
3541
|
+
if (this._state > RUNNING) return abortHandshake(socket, 503);
|
|
3542
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
3543
|
+
const headers = [
|
|
3544
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
3545
|
+
"Upgrade: websocket",
|
|
3546
|
+
"Connection: Upgrade",
|
|
3547
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
3548
|
+
];
|
|
3549
|
+
const ws = new this.options.WebSocket(null, void 0, this.options);
|
|
3550
|
+
if (protocols.size) {
|
|
3551
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
3552
|
+
if (protocol) {
|
|
3553
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
3554
|
+
ws._protocol = protocol;
|
|
3555
|
+
}
|
|
3556
|
+
}
|
|
3557
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
3558
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
3559
|
+
const value = extension.format({
|
|
3560
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
3561
|
+
});
|
|
3562
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
3563
|
+
ws._extensions = extensions;
|
|
3564
|
+
}
|
|
3565
|
+
this.emit("headers", headers, req);
|
|
3566
|
+
socket.write(headers.concat("\r\n").join("\r\n"));
|
|
3567
|
+
socket.removeListener("error", socketOnError);
|
|
3568
|
+
ws.setSocket(socket, head, {
|
|
3569
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
3570
|
+
maxPayload: this.options.maxPayload,
|
|
3571
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
3572
|
+
});
|
|
3573
|
+
if (this.clients) {
|
|
3574
|
+
this.clients.add(ws);
|
|
3575
|
+
ws.on("close", () => {
|
|
3576
|
+
this.clients.delete(ws);
|
|
3577
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
3578
|
+
process.nextTick(emitClose, this);
|
|
3579
|
+
}
|
|
3580
|
+
});
|
|
3581
|
+
}
|
|
3582
|
+
cb(ws, req);
|
|
3583
|
+
}
|
|
3584
|
+
};
|
|
3585
|
+
module.exports = WebSocketServer2;
|
|
3586
|
+
function addListeners(server, map) {
|
|
3587
|
+
for (const event of Object.keys(map)) server.on(event, map[event]);
|
|
3588
|
+
return function removeListeners() {
|
|
3589
|
+
for (const event of Object.keys(map)) {
|
|
3590
|
+
server.removeListener(event, map[event]);
|
|
3591
|
+
}
|
|
3592
|
+
};
|
|
3593
|
+
}
|
|
3594
|
+
function emitClose(server) {
|
|
3595
|
+
server._state = CLOSED;
|
|
3596
|
+
server.emit("close");
|
|
3597
|
+
}
|
|
3598
|
+
function socketOnError() {
|
|
3599
|
+
this.destroy();
|
|
3600
|
+
}
|
|
3601
|
+
function abortHandshake(socket, code, message, headers) {
|
|
3602
|
+
message = message || http.STATUS_CODES[code];
|
|
3603
|
+
headers = {
|
|
3604
|
+
Connection: "close",
|
|
3605
|
+
"Content-Type": "text/html",
|
|
3606
|
+
"Content-Length": Buffer.byteLength(message),
|
|
3607
|
+
...headers
|
|
3608
|
+
};
|
|
3609
|
+
socket.once("finish", socket.destroy);
|
|
3610
|
+
socket.end(
|
|
3611
|
+
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
3612
|
+
` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join("\r\n") + "\r\n\r\n" + message
|
|
3613
|
+
);
|
|
3614
|
+
}
|
|
3615
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
3616
|
+
if (server.listenerCount("wsClientError")) {
|
|
3617
|
+
const err = new Error(message);
|
|
3618
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
3619
|
+
server.emit("wsClientError", err, socket, req);
|
|
3620
|
+
} else {
|
|
3621
|
+
abortHandshake(socket, code, message, headers);
|
|
3622
|
+
}
|
|
3623
|
+
}
|
|
3624
|
+
}
|
|
3625
|
+
});
|
|
7
3626
|
|
|
8
3627
|
// packages/cli/src/client.ts
|
|
9
3628
|
async function sendCommand(request) {
|
|
@@ -187,6 +3806,9 @@ async function snapshotCommand(options = {}) {
|
|
|
187
3806
|
id: generateId(),
|
|
188
3807
|
action: "snapshot",
|
|
189
3808
|
interactive: options.interactive,
|
|
3809
|
+
compact: options.compact,
|
|
3810
|
+
maxDepth: options.maxDepth,
|
|
3811
|
+
selector: options.selector,
|
|
190
3812
|
tabId: options.tabId
|
|
191
3813
|
};
|
|
192
3814
|
const response = await sendCommand(request);
|
|
@@ -597,6 +4219,7 @@ async function scrollCommand(direction, pixels, options = {}) {
|
|
|
597
4219
|
}
|
|
598
4220
|
|
|
599
4221
|
// packages/cli/src/commands/daemon.ts
|
|
4222
|
+
import { spawn as spawn2 } from "child_process";
|
|
600
4223
|
async function daemonCommand(options = {}) {
|
|
601
4224
|
if (await isDaemonRunning()) {
|
|
602
4225
|
if (options.json) {
|
|
@@ -606,23 +4229,29 @@ async function daemonCommand(options = {}) {
|
|
|
606
4229
|
}
|
|
607
4230
|
return;
|
|
608
4231
|
}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
619
|
-
if (options.json) {
|
|
620
|
-
console.log(JSON.stringify({ success: false, error: message }));
|
|
621
|
-
} else {
|
|
622
|
-
console.error(`\u542F\u52A8\u5931\u8D25: ${message}`);
|
|
623
|
-
}
|
|
624
|
-
process.exit(1);
|
|
4232
|
+
const daemonPath = getDaemonPath();
|
|
4233
|
+
const args = [daemonPath];
|
|
4234
|
+
if (options.host) {
|
|
4235
|
+
args.push("--host", options.host);
|
|
4236
|
+
}
|
|
4237
|
+
if (options.json) {
|
|
4238
|
+
console.log(JSON.stringify({ success: true, message: "Daemon \u542F\u52A8\u4E2D..." }));
|
|
4239
|
+
} else {
|
|
4240
|
+
console.log("Daemon \u542F\u52A8\u4E2D...");
|
|
625
4241
|
}
|
|
4242
|
+
await new Promise((resolve2, reject) => {
|
|
4243
|
+
const child = spawn2(process.execPath, args, {
|
|
4244
|
+
stdio: "inherit"
|
|
4245
|
+
});
|
|
4246
|
+
child.on("exit", (code) => {
|
|
4247
|
+
if (code && code !== 0) {
|
|
4248
|
+
reject(new Error(`Daemon exited with code ${code}`));
|
|
4249
|
+
} else {
|
|
4250
|
+
resolve2();
|
|
4251
|
+
}
|
|
4252
|
+
});
|
|
4253
|
+
child.on("error", reject);
|
|
4254
|
+
});
|
|
626
4255
|
}
|
|
627
4256
|
async function stopCommand(options = {}) {
|
|
628
4257
|
if (!await isDaemonRunning()) {
|
|
@@ -658,8 +4287,15 @@ async function statusCommand(options = {}) {
|
|
|
658
4287
|
}
|
|
659
4288
|
}
|
|
660
4289
|
|
|
4290
|
+
// node_modules/.pnpm/ws@8.19.0/node_modules/ws/wrapper.mjs
|
|
4291
|
+
var import_stream = __toESM(require_stream(), 1);
|
|
4292
|
+
var import_receiver = __toESM(require_receiver(), 1);
|
|
4293
|
+
var import_sender = __toESM(require_sender(), 1);
|
|
4294
|
+
var import_websocket = __toESM(require_websocket(), 1);
|
|
4295
|
+
var import_websocket_server = __toESM(require_websocket_server(), 1);
|
|
4296
|
+
var wrapper_default = import_websocket.default;
|
|
4297
|
+
|
|
661
4298
|
// packages/cli/src/commands/reload.ts
|
|
662
|
-
import WebSocket from "ws";
|
|
663
4299
|
var EXTENSION_NAME = "bb-browser";
|
|
664
4300
|
async function reloadCommand(options = {}) {
|
|
665
4301
|
const port = options.port || 9222;
|
|
@@ -676,7 +4312,7 @@ async function reloadCommand(options = {}) {
|
|
|
676
4312
|
throw new Error("\u8BF7\u5148\u6253\u5F00 chrome://extensions \u9875\u9762");
|
|
677
4313
|
}
|
|
678
4314
|
const result = await new Promise((resolve2, reject) => {
|
|
679
|
-
const ws = new
|
|
4315
|
+
const ws = new wrapper_default(extPage.webSocketDebuggerUrl);
|
|
680
4316
|
let resolved = false;
|
|
681
4317
|
const timeout = setTimeout(() => {
|
|
682
4318
|
if (!resolved) {
|
|
@@ -1197,6 +4833,7 @@ async function networkCommand(subCommand, urlOrFilter, options = {}) {
|
|
|
1197
4833
|
abort: options.abort,
|
|
1198
4834
|
body: options.body
|
|
1199
4835
|
} : void 0,
|
|
4836
|
+
withBody: subCommand === "requests" ? options.withBody : void 0,
|
|
1200
4837
|
tabId: options.tabId
|
|
1201
4838
|
});
|
|
1202
4839
|
if (options.json) {
|
|
@@ -1220,6 +4857,22 @@ async function networkCommand(subCommand, urlOrFilter, options = {}) {
|
|
|
1220
4857
|
const status = req.failed ? `FAILED (${req.failureReason})` : req.status ? `${req.status} ${req.statusText || ""}` : "pending";
|
|
1221
4858
|
console.log(`${req.method} ${req.url}`);
|
|
1222
4859
|
console.log(` \u7C7B\u578B: ${req.type}, \u72B6\u6001: ${status}`);
|
|
4860
|
+
if (options.withBody) {
|
|
4861
|
+
const requestHeaderCount = req.requestHeaders ? Object.keys(req.requestHeaders).length : 0;
|
|
4862
|
+
const responseHeaderCount = req.responseHeaders ? Object.keys(req.responseHeaders).length : 0;
|
|
4863
|
+
console.log(` \u8BF7\u6C42\u5934: ${requestHeaderCount}, \u54CD\u5E94\u5934: ${responseHeaderCount}`);
|
|
4864
|
+
if (req.requestBody !== void 0) {
|
|
4865
|
+
const preview = req.requestBody.length > 200 ? `${req.requestBody.slice(0, 200)}...` : req.requestBody;
|
|
4866
|
+
console.log(` \u8BF7\u6C42\u4F53: ${preview}`);
|
|
4867
|
+
}
|
|
4868
|
+
if (req.responseBody !== void 0) {
|
|
4869
|
+
const preview = req.responseBody.length > 200 ? `${req.responseBody.slice(0, 200)}...` : req.responseBody;
|
|
4870
|
+
console.log(` \u54CD\u5E94\u4F53: ${preview}`);
|
|
4871
|
+
}
|
|
4872
|
+
if (req.bodyError) {
|
|
4873
|
+
console.log(` Body\u9519\u8BEF: ${req.bodyError}`);
|
|
4874
|
+
}
|
|
4875
|
+
}
|
|
1223
4876
|
console.log("");
|
|
1224
4877
|
}
|
|
1225
4878
|
}
|
|
@@ -1421,8 +5074,502 @@ async function traceCommand(subCommand, options = {}) {
|
|
|
1421
5074
|
}
|
|
1422
5075
|
}
|
|
1423
5076
|
|
|
5077
|
+
// packages/cli/src/commands/fetch.ts
|
|
5078
|
+
function matchTabOrigin(tabUrl, targetHostname) {
|
|
5079
|
+
try {
|
|
5080
|
+
const tabHostname = new URL(tabUrl).hostname;
|
|
5081
|
+
return tabHostname === targetHostname || tabHostname.endsWith("." + targetHostname);
|
|
5082
|
+
} catch {
|
|
5083
|
+
return false;
|
|
5084
|
+
}
|
|
5085
|
+
}
|
|
5086
|
+
async function ensureTabForOrigin(origin, hostname) {
|
|
5087
|
+
const listReq = { id: generateId(), action: "tab_list" };
|
|
5088
|
+
const listResp = await sendCommand(listReq);
|
|
5089
|
+
if (listResp.success && listResp.data?.tabs) {
|
|
5090
|
+
const matchingTab = listResp.data.tabs.find(
|
|
5091
|
+
(tab) => matchTabOrigin(tab.url, hostname)
|
|
5092
|
+
);
|
|
5093
|
+
if (matchingTab) {
|
|
5094
|
+
return matchingTab.tabId;
|
|
5095
|
+
}
|
|
5096
|
+
}
|
|
5097
|
+
const newResp = await sendCommand({ id: generateId(), action: "tab_new", url: origin });
|
|
5098
|
+
if (!newResp.success) {
|
|
5099
|
+
throw new Error(`\u65E0\u6CD5\u6253\u5F00 ${origin}: ${newResp.error}`);
|
|
5100
|
+
}
|
|
5101
|
+
await new Promise((resolve2) => setTimeout(resolve2, 3e3));
|
|
5102
|
+
return newResp.data?.tabId;
|
|
5103
|
+
}
|
|
5104
|
+
function buildFetchScript(url, options) {
|
|
5105
|
+
const method = (options.method || "GET").toUpperCase();
|
|
5106
|
+
const hasBody = options.body && method !== "GET" && method !== "HEAD";
|
|
5107
|
+
let headersExpr = "{}";
|
|
5108
|
+
if (options.headers) {
|
|
5109
|
+
try {
|
|
5110
|
+
JSON.parse(options.headers);
|
|
5111
|
+
headersExpr = options.headers;
|
|
5112
|
+
} catch {
|
|
5113
|
+
throw new Error(`--headers must be valid JSON. Got: ${options.headers}`);
|
|
5114
|
+
}
|
|
5115
|
+
}
|
|
5116
|
+
return `(async () => {
|
|
5117
|
+
try {
|
|
5118
|
+
const resp = await fetch(${JSON.stringify(url)}, {
|
|
5119
|
+
method: ${JSON.stringify(method)},
|
|
5120
|
+
credentials: 'include',
|
|
5121
|
+
headers: ${headersExpr}${hasBody ? `,
|
|
5122
|
+
body: ${JSON.stringify(options.body)}` : ""}
|
|
5123
|
+
});
|
|
5124
|
+
const contentType = resp.headers.get('content-type') || '';
|
|
5125
|
+
let body;
|
|
5126
|
+
if (contentType.includes('application/json') && resp.status !== 204) {
|
|
5127
|
+
try { body = await resp.json(); } catch { body = await resp.text(); }
|
|
5128
|
+
} else {
|
|
5129
|
+
body = await resp.text();
|
|
5130
|
+
}
|
|
5131
|
+
return JSON.stringify({
|
|
5132
|
+
status: resp.status,
|
|
5133
|
+
contentType,
|
|
5134
|
+
body
|
|
5135
|
+
});
|
|
5136
|
+
} catch (e) {
|
|
5137
|
+
return JSON.stringify({ error: e.message });
|
|
5138
|
+
}
|
|
5139
|
+
})()`;
|
|
5140
|
+
}
|
|
5141
|
+
async function fetchCommand(url, options = {}) {
|
|
5142
|
+
if (!url) {
|
|
5143
|
+
throw new Error(
|
|
5144
|
+
"\u7F3A\u5C11 URL \u53C2\u6570\n \u7528\u6CD5: bb-browser fetch <url> [--json] [--method POST] [--body '{...}']\n \u793A\u4F8B: bb-browser fetch https://www.reddit.com/api/me.json --json"
|
|
5145
|
+
);
|
|
5146
|
+
}
|
|
5147
|
+
await ensureDaemonRunning();
|
|
5148
|
+
const isAbsolute = url.startsWith("http://") || url.startsWith("https://");
|
|
5149
|
+
let targetTabId = options.tabId;
|
|
5150
|
+
if (isAbsolute) {
|
|
5151
|
+
let origin;
|
|
5152
|
+
let hostname;
|
|
5153
|
+
try {
|
|
5154
|
+
const parsed = new URL(url);
|
|
5155
|
+
origin = parsed.origin;
|
|
5156
|
+
hostname = parsed.hostname;
|
|
5157
|
+
} catch {
|
|
5158
|
+
throw new Error(`\u65E0\u6548\u7684 URL: ${url}`);
|
|
5159
|
+
}
|
|
5160
|
+
if (!targetTabId) {
|
|
5161
|
+
targetTabId = await ensureTabForOrigin(origin, hostname);
|
|
5162
|
+
}
|
|
5163
|
+
}
|
|
5164
|
+
const script = buildFetchScript(url, options);
|
|
5165
|
+
const evalReq = { id: generateId(), action: "eval", script, tabId: targetTabId };
|
|
5166
|
+
const evalResp = await sendCommand(evalReq);
|
|
5167
|
+
if (!evalResp.success) {
|
|
5168
|
+
throw new Error(`Fetch \u5931\u8D25: ${evalResp.error}`);
|
|
5169
|
+
}
|
|
5170
|
+
const rawResult = evalResp.data?.result;
|
|
5171
|
+
if (rawResult === void 0 || rawResult === null) {
|
|
5172
|
+
throw new Error("Fetch \u672A\u8FD4\u56DE\u7ED3\u679C");
|
|
5173
|
+
}
|
|
5174
|
+
let result;
|
|
5175
|
+
try {
|
|
5176
|
+
result = typeof rawResult === "string" ? JSON.parse(rawResult) : rawResult;
|
|
5177
|
+
} catch {
|
|
5178
|
+
console.log(rawResult);
|
|
5179
|
+
return;
|
|
5180
|
+
}
|
|
5181
|
+
if (result.error) {
|
|
5182
|
+
throw new Error(`Fetch error: ${result.error}`);
|
|
5183
|
+
}
|
|
5184
|
+
if (options.output) {
|
|
5185
|
+
const { writeFileSync } = await import("fs");
|
|
5186
|
+
const content = typeof result.body === "object" ? JSON.stringify(result.body, null, 2) : String(result.body);
|
|
5187
|
+
writeFileSync(options.output, content, "utf-8");
|
|
5188
|
+
console.log(`\u5DF2\u5199\u5165 ${options.output} (${result.status}, ${content.length} bytes)`);
|
|
5189
|
+
return;
|
|
5190
|
+
}
|
|
5191
|
+
if (typeof result.body === "object") {
|
|
5192
|
+
console.log(JSON.stringify(result.body, null, 2));
|
|
5193
|
+
} else {
|
|
5194
|
+
console.log(result.body);
|
|
5195
|
+
}
|
|
5196
|
+
}
|
|
5197
|
+
|
|
5198
|
+
// packages/cli/src/commands/site.ts
|
|
5199
|
+
import { readFileSync, readdirSync, existsSync as existsSync2, mkdirSync } from "fs";
|
|
5200
|
+
import { join, relative } from "path";
|
|
5201
|
+
import { homedir } from "os";
|
|
5202
|
+
import { execSync } from "child_process";
|
|
5203
|
+
var BB_DIR = join(homedir(), ".bb-browser");
|
|
5204
|
+
var LOCAL_SITES_DIR = join(BB_DIR, "sites");
|
|
5205
|
+
var COMMUNITY_SITES_DIR = join(BB_DIR, "bb-sites");
|
|
5206
|
+
var COMMUNITY_REPO = "https://github.com/epiral/bb-sites.git";
|
|
5207
|
+
function parseSiteMeta(filePath, source) {
|
|
5208
|
+
let content;
|
|
5209
|
+
try {
|
|
5210
|
+
content = readFileSync(filePath, "utf-8");
|
|
5211
|
+
} catch {
|
|
5212
|
+
return null;
|
|
5213
|
+
}
|
|
5214
|
+
const sitesDir = source === "local" ? LOCAL_SITES_DIR : COMMUNITY_SITES_DIR;
|
|
5215
|
+
const relPath = relative(sitesDir, filePath);
|
|
5216
|
+
const defaultName = relPath.replace(/\.js$/, "").replace(/\\/g, "/");
|
|
5217
|
+
const metaMatch = content.match(/\/\*\s*@meta\s*\n([\s\S]*?)\*\//);
|
|
5218
|
+
if (metaMatch) {
|
|
5219
|
+
try {
|
|
5220
|
+
const metaJson = JSON.parse(metaMatch[1]);
|
|
5221
|
+
return {
|
|
5222
|
+
name: metaJson.name || defaultName,
|
|
5223
|
+
description: metaJson.description || "",
|
|
5224
|
+
domain: metaJson.domain || "",
|
|
5225
|
+
args: metaJson.args || {},
|
|
5226
|
+
capabilities: metaJson.capabilities,
|
|
5227
|
+
readOnly: metaJson.readOnly,
|
|
5228
|
+
example: metaJson.example,
|
|
5229
|
+
filePath,
|
|
5230
|
+
source
|
|
5231
|
+
};
|
|
5232
|
+
} catch {
|
|
5233
|
+
}
|
|
5234
|
+
}
|
|
5235
|
+
const meta = {
|
|
5236
|
+
name: defaultName,
|
|
5237
|
+
description: "",
|
|
5238
|
+
domain: "",
|
|
5239
|
+
args: {},
|
|
5240
|
+
filePath,
|
|
5241
|
+
source
|
|
5242
|
+
};
|
|
5243
|
+
const tagPattern = /\/\/\s*@(\w+)[ \t]+(.*)/g;
|
|
5244
|
+
let match;
|
|
5245
|
+
while ((match = tagPattern.exec(content)) !== null) {
|
|
5246
|
+
const [, key, value] = match;
|
|
5247
|
+
switch (key) {
|
|
5248
|
+
case "name":
|
|
5249
|
+
meta.name = value.trim();
|
|
5250
|
+
break;
|
|
5251
|
+
case "description":
|
|
5252
|
+
meta.description = value.trim();
|
|
5253
|
+
break;
|
|
5254
|
+
case "domain":
|
|
5255
|
+
meta.domain = value.trim();
|
|
5256
|
+
break;
|
|
5257
|
+
case "args":
|
|
5258
|
+
for (const arg of value.trim().split(/[,\s]+/).filter(Boolean)) {
|
|
5259
|
+
meta.args[arg] = { required: true };
|
|
5260
|
+
}
|
|
5261
|
+
break;
|
|
5262
|
+
case "example":
|
|
5263
|
+
meta.example = value.trim();
|
|
5264
|
+
break;
|
|
5265
|
+
}
|
|
5266
|
+
}
|
|
5267
|
+
return meta;
|
|
5268
|
+
}
|
|
5269
|
+
function scanSites(dir, source) {
|
|
5270
|
+
if (!existsSync2(dir)) return [];
|
|
5271
|
+
const sites = [];
|
|
5272
|
+
function walk(currentDir) {
|
|
5273
|
+
let entries;
|
|
5274
|
+
try {
|
|
5275
|
+
entries = readdirSync(currentDir, { withFileTypes: true });
|
|
5276
|
+
} catch {
|
|
5277
|
+
return;
|
|
5278
|
+
}
|
|
5279
|
+
for (const entry of entries) {
|
|
5280
|
+
const fullPath = join(currentDir, entry.name);
|
|
5281
|
+
if (entry.isDirectory() && !entry.name.startsWith(".")) {
|
|
5282
|
+
walk(fullPath);
|
|
5283
|
+
} else if (entry.isFile() && entry.name.endsWith(".js")) {
|
|
5284
|
+
const meta = parseSiteMeta(fullPath, source);
|
|
5285
|
+
if (meta) sites.push(meta);
|
|
5286
|
+
}
|
|
5287
|
+
}
|
|
5288
|
+
}
|
|
5289
|
+
walk(dir);
|
|
5290
|
+
return sites;
|
|
5291
|
+
}
|
|
5292
|
+
function getAllSites() {
|
|
5293
|
+
const community = scanSites(COMMUNITY_SITES_DIR, "community");
|
|
5294
|
+
const local = scanSites(LOCAL_SITES_DIR, "local");
|
|
5295
|
+
const byName = /* @__PURE__ */ new Map();
|
|
5296
|
+
for (const s of community) byName.set(s.name, s);
|
|
5297
|
+
for (const s of local) byName.set(s.name, s);
|
|
5298
|
+
return Array.from(byName.values()).sort((a, b) => a.name.localeCompare(b.name));
|
|
5299
|
+
}
|
|
5300
|
+
function matchTabOrigin2(tabUrl, domain) {
|
|
5301
|
+
try {
|
|
5302
|
+
const tabOrigin = new URL(tabUrl).hostname;
|
|
5303
|
+
return tabOrigin === domain || tabOrigin.endsWith("." + domain);
|
|
5304
|
+
} catch {
|
|
5305
|
+
return false;
|
|
5306
|
+
}
|
|
5307
|
+
}
|
|
5308
|
+
function siteList(options) {
|
|
5309
|
+
const sites = getAllSites();
|
|
5310
|
+
if (sites.length === 0) {
|
|
5311
|
+
console.log("\u672A\u627E\u5230\u4EFB\u4F55 site adapter\u3002");
|
|
5312
|
+
console.log(" \u5B89\u88C5\u793E\u533A adapter: bb-browser site update");
|
|
5313
|
+
console.log(` \u79C1\u6709 adapter \u76EE\u5F55: ${LOCAL_SITES_DIR}`);
|
|
5314
|
+
return;
|
|
5315
|
+
}
|
|
5316
|
+
if (options.json) {
|
|
5317
|
+
console.log(JSON.stringify(sites.map((s) => ({
|
|
5318
|
+
name: s.name,
|
|
5319
|
+
description: s.description,
|
|
5320
|
+
domain: s.domain,
|
|
5321
|
+
args: s.args,
|
|
5322
|
+
source: s.source
|
|
5323
|
+
})), null, 2));
|
|
5324
|
+
return;
|
|
5325
|
+
}
|
|
5326
|
+
const groups = /* @__PURE__ */ new Map();
|
|
5327
|
+
for (const s of sites) {
|
|
5328
|
+
const platform = s.name.split("/")[0];
|
|
5329
|
+
if (!groups.has(platform)) groups.set(platform, []);
|
|
5330
|
+
groups.get(platform).push(s);
|
|
5331
|
+
}
|
|
5332
|
+
for (const [platform, items] of groups) {
|
|
5333
|
+
console.log(`
|
|
5334
|
+
${platform}/`);
|
|
5335
|
+
for (const s of items) {
|
|
5336
|
+
const cmd = s.name.split("/").slice(1).join("/");
|
|
5337
|
+
const src = s.source === "local" ? " (local)" : "";
|
|
5338
|
+
const desc = s.description ? ` - ${s.description}` : "";
|
|
5339
|
+
console.log(` ${cmd.padEnd(20)}${desc}${src}`);
|
|
5340
|
+
}
|
|
5341
|
+
}
|
|
5342
|
+
console.log();
|
|
5343
|
+
}
|
|
5344
|
+
function siteSearch(query, options) {
|
|
5345
|
+
const sites = getAllSites();
|
|
5346
|
+
const q = query.toLowerCase();
|
|
5347
|
+
const matches = sites.filter(
|
|
5348
|
+
(s) => s.name.toLowerCase().includes(q) || s.description.toLowerCase().includes(q) || s.domain.toLowerCase().includes(q)
|
|
5349
|
+
);
|
|
5350
|
+
if (matches.length === 0) {
|
|
5351
|
+
console.log(`\u672A\u627E\u5230\u5339\u914D "${query}" \u7684 adapter\u3002`);
|
|
5352
|
+
console.log(" \u67E5\u770B\u6240\u6709: bb-browser site list");
|
|
5353
|
+
return;
|
|
5354
|
+
}
|
|
5355
|
+
if (options.json) {
|
|
5356
|
+
console.log(JSON.stringify(matches.map((s) => ({
|
|
5357
|
+
name: s.name,
|
|
5358
|
+
description: s.description,
|
|
5359
|
+
domain: s.domain,
|
|
5360
|
+
source: s.source
|
|
5361
|
+
})), null, 2));
|
|
5362
|
+
return;
|
|
5363
|
+
}
|
|
5364
|
+
for (const s of matches) {
|
|
5365
|
+
const src = s.source === "local" ? " (local)" : "";
|
|
5366
|
+
console.log(`${s.name.padEnd(24)} ${s.description}${src}`);
|
|
5367
|
+
}
|
|
5368
|
+
}
|
|
5369
|
+
function siteUpdate() {
|
|
5370
|
+
mkdirSync(BB_DIR, { recursive: true });
|
|
5371
|
+
if (existsSync2(join(COMMUNITY_SITES_DIR, ".git"))) {
|
|
5372
|
+
console.log("\u66F4\u65B0\u793E\u533A site adapter \u5E93...");
|
|
5373
|
+
try {
|
|
5374
|
+
execSync("git pull --ff-only", { cwd: COMMUNITY_SITES_DIR, stdio: "pipe" });
|
|
5375
|
+
console.log("\u66F4\u65B0\u5B8C\u6210\u3002");
|
|
5376
|
+
} catch (e) {
|
|
5377
|
+
console.error(`\u66F4\u65B0\u5931\u8D25: ${e instanceof Error ? e.message : e}`);
|
|
5378
|
+
console.error(" \u624B\u52A8\u4FEE\u590D: cd ~/.bb-browser/bb-sites && git pull");
|
|
5379
|
+
process.exit(1);
|
|
5380
|
+
}
|
|
5381
|
+
} else {
|
|
5382
|
+
console.log(`\u514B\u9686\u793E\u533A adapter \u5E93: ${COMMUNITY_REPO}`);
|
|
5383
|
+
try {
|
|
5384
|
+
execSync(`git clone ${COMMUNITY_REPO} ${COMMUNITY_SITES_DIR}`, { stdio: "pipe" });
|
|
5385
|
+
console.log("\u514B\u9686\u5B8C\u6210\u3002");
|
|
5386
|
+
} catch (e) {
|
|
5387
|
+
console.error(`\u514B\u9686\u5931\u8D25: ${e instanceof Error ? e.message : e}`);
|
|
5388
|
+
console.error(` \u624B\u52A8\u4FEE\u590D: git clone ${COMMUNITY_REPO} ~/.bb-browser/bb-sites`);
|
|
5389
|
+
process.exit(1);
|
|
5390
|
+
}
|
|
5391
|
+
}
|
|
5392
|
+
const sites = scanSites(COMMUNITY_SITES_DIR, "community");
|
|
5393
|
+
console.log(`\u5DF2\u5B89\u88C5 ${sites.length} \u4E2A\u793E\u533A adapter\u3002`);
|
|
5394
|
+
}
|
|
5395
|
+
async function siteRun(name, args, options) {
|
|
5396
|
+
const sites = getAllSites();
|
|
5397
|
+
const site = sites.find((s) => s.name === name);
|
|
5398
|
+
if (!site) {
|
|
5399
|
+
const fuzzy = sites.filter((s) => s.name.includes(name));
|
|
5400
|
+
console.error(`[error] site: "${name}" not found.`);
|
|
5401
|
+
if (fuzzy.length > 0) {
|
|
5402
|
+
console.error(" Did you mean:");
|
|
5403
|
+
for (const s of fuzzy.slice(0, 5)) {
|
|
5404
|
+
console.error(` bb-browser site ${s.name}`);
|
|
5405
|
+
}
|
|
5406
|
+
} else {
|
|
5407
|
+
console.error(" Try: bb-browser site list");
|
|
5408
|
+
console.error(" Or: bb-browser site update");
|
|
5409
|
+
}
|
|
5410
|
+
process.exit(1);
|
|
5411
|
+
}
|
|
5412
|
+
const argNames = Object.keys(site.args);
|
|
5413
|
+
const argMap = {};
|
|
5414
|
+
const positionalArgs = [];
|
|
5415
|
+
for (let i = 0; i < args.length; i++) {
|
|
5416
|
+
if (args[i].startsWith("--")) {
|
|
5417
|
+
const flagName = args[i].slice(2);
|
|
5418
|
+
if (flagName in site.args && args[i + 1]) {
|
|
5419
|
+
argMap[flagName] = args[i + 1];
|
|
5420
|
+
i++;
|
|
5421
|
+
}
|
|
5422
|
+
} else {
|
|
5423
|
+
positionalArgs.push(args[i]);
|
|
5424
|
+
}
|
|
5425
|
+
}
|
|
5426
|
+
let posIdx = 0;
|
|
5427
|
+
for (const argName of argNames) {
|
|
5428
|
+
if (!argMap[argName] && posIdx < positionalArgs.length) {
|
|
5429
|
+
argMap[argName] = positionalArgs[posIdx++];
|
|
5430
|
+
}
|
|
5431
|
+
}
|
|
5432
|
+
for (const [argName, argDef] of Object.entries(site.args)) {
|
|
5433
|
+
if (argDef.required && !argMap[argName]) {
|
|
5434
|
+
console.error(`[error] site ${name}: missing required argument "${argName}".`);
|
|
5435
|
+
const usage = argNames.map((a) => {
|
|
5436
|
+
const def = site.args[a];
|
|
5437
|
+
return def.required ? `<${a}>` : `[${a}]`;
|
|
5438
|
+
}).join(" ");
|
|
5439
|
+
console.error(` Usage: bb-browser site ${name} ${usage}`);
|
|
5440
|
+
if (site.example) console.error(` Example: ${site.example}`);
|
|
5441
|
+
process.exit(1);
|
|
5442
|
+
}
|
|
5443
|
+
}
|
|
5444
|
+
const jsContent = readFileSync(site.filePath, "utf-8");
|
|
5445
|
+
const jsBody = jsContent.replace(/\/\*\s*@meta[\s\S]*?\*\//, "").trim();
|
|
5446
|
+
const argsJson = JSON.stringify(argMap);
|
|
5447
|
+
const script = `(${jsBody})(${argsJson})`;
|
|
5448
|
+
await ensureDaemonRunning();
|
|
5449
|
+
let targetTabId = options.tabId;
|
|
5450
|
+
if (!targetTabId && site.domain) {
|
|
5451
|
+
const listReq = { id: generateId(), action: "tab_list" };
|
|
5452
|
+
const listResp = await sendCommand(listReq);
|
|
5453
|
+
if (listResp.success && listResp.data?.tabs) {
|
|
5454
|
+
const matchingTab = listResp.data.tabs.find(
|
|
5455
|
+
(tab) => matchTabOrigin2(tab.url, site.domain)
|
|
5456
|
+
);
|
|
5457
|
+
if (matchingTab) {
|
|
5458
|
+
targetTabId = matchingTab.tabId;
|
|
5459
|
+
}
|
|
5460
|
+
}
|
|
5461
|
+
if (!targetTabId) {
|
|
5462
|
+
const newResp = await sendCommand({
|
|
5463
|
+
id: generateId(),
|
|
5464
|
+
action: "tab_new",
|
|
5465
|
+
url: `https://${site.domain}`
|
|
5466
|
+
});
|
|
5467
|
+
targetTabId = newResp.data?.tabId;
|
|
5468
|
+
await new Promise((resolve2) => setTimeout(resolve2, 3e3));
|
|
5469
|
+
}
|
|
5470
|
+
}
|
|
5471
|
+
const evalReq = { id: generateId(), action: "eval", script, tabId: targetTabId };
|
|
5472
|
+
const evalResp = await sendCommand(evalReq);
|
|
5473
|
+
if (!evalResp.success) {
|
|
5474
|
+
console.error(`[error] site ${name}: eval failed.`);
|
|
5475
|
+
console.error(` ${evalResp.error}`);
|
|
5476
|
+
console.error(` Check: is ${site.domain} open and logged in?`);
|
|
5477
|
+
process.exit(1);
|
|
5478
|
+
}
|
|
5479
|
+
const result = evalResp.data?.result;
|
|
5480
|
+
if (result === void 0 || result === null) {
|
|
5481
|
+
if (options.json) {
|
|
5482
|
+
console.log(JSON.stringify({ id: evalReq.id, success: true, data: null }));
|
|
5483
|
+
} else {
|
|
5484
|
+
console.log("(no output)");
|
|
5485
|
+
}
|
|
5486
|
+
return;
|
|
5487
|
+
}
|
|
5488
|
+
let parsed;
|
|
5489
|
+
try {
|
|
5490
|
+
parsed = typeof result === "string" ? JSON.parse(result) : result;
|
|
5491
|
+
} catch {
|
|
5492
|
+
parsed = result;
|
|
5493
|
+
}
|
|
5494
|
+
if (typeof parsed === "object" && parsed !== null && "error" in parsed) {
|
|
5495
|
+
const errObj = parsed;
|
|
5496
|
+
if (options.json) {
|
|
5497
|
+
console.log(JSON.stringify({ id: evalReq.id, success: false, error: errObj.error, hint: errObj.hint }));
|
|
5498
|
+
} else {
|
|
5499
|
+
console.error(`[error] site ${name}: ${errObj.error}`);
|
|
5500
|
+
if (errObj.hint) console.error(` Hint: ${errObj.hint}`);
|
|
5501
|
+
}
|
|
5502
|
+
process.exit(1);
|
|
5503
|
+
}
|
|
5504
|
+
if (options.json) {
|
|
5505
|
+
console.log(JSON.stringify({ id: evalReq.id, success: true, data: parsed }));
|
|
5506
|
+
} else {
|
|
5507
|
+
console.log(JSON.stringify(parsed, null, 2));
|
|
5508
|
+
}
|
|
5509
|
+
}
|
|
5510
|
+
async function siteCommand(args, options = {}) {
|
|
5511
|
+
const subCommand = args[0];
|
|
5512
|
+
if (!subCommand || subCommand === "--help" || subCommand === "-h") {
|
|
5513
|
+
console.log(`bb-browser site - \u7F51\u7AD9 CLI \u5316\uFF08\u7BA1\u7406\u548C\u8FD0\u884C site adapter\uFF09
|
|
5514
|
+
|
|
5515
|
+
\u7528\u6CD5:
|
|
5516
|
+
bb-browser site list \u5217\u51FA\u6240\u6709\u53EF\u7528 adapter
|
|
5517
|
+
bb-browser site search <query> \u641C\u7D22 adapter
|
|
5518
|
+
bb-browser site <name> [args...] \u8FD0\u884C adapter\uFF08\u7B80\u5199\uFF09
|
|
5519
|
+
bb-browser site run <name> [args...] \u8FD0\u884C adapter
|
|
5520
|
+
bb-browser site update \u66F4\u65B0\u793E\u533A adapter \u5E93 (git clone/pull)
|
|
5521
|
+
|
|
5522
|
+
\u76EE\u5F55:
|
|
5523
|
+
${LOCAL_SITES_DIR} \u79C1\u6709 adapter\uFF08\u4F18\u5148\uFF09
|
|
5524
|
+
${COMMUNITY_SITES_DIR} \u793E\u533A adapter
|
|
5525
|
+
|
|
5526
|
+
\u793A\u4F8B:
|
|
5527
|
+
bb-browser site update
|
|
5528
|
+
bb-browser site list
|
|
5529
|
+
bb-browser site reddit/thread https://www.reddit.com/r/LocalLLaMA/comments/...
|
|
5530
|
+
bb-browser site twitter/user yan5xu
|
|
5531
|
+
bb-browser site search reddit`);
|
|
5532
|
+
return;
|
|
5533
|
+
}
|
|
5534
|
+
switch (subCommand) {
|
|
5535
|
+
case "list":
|
|
5536
|
+
siteList(options);
|
|
5537
|
+
break;
|
|
5538
|
+
case "search":
|
|
5539
|
+
if (!args[1]) {
|
|
5540
|
+
console.error("[error] site search: <query> is required.");
|
|
5541
|
+
console.error(" Usage: bb-browser site search <query>");
|
|
5542
|
+
process.exit(1);
|
|
5543
|
+
}
|
|
5544
|
+
siteSearch(args[1], options);
|
|
5545
|
+
break;
|
|
5546
|
+
case "update":
|
|
5547
|
+
siteUpdate();
|
|
5548
|
+
break;
|
|
5549
|
+
case "run":
|
|
5550
|
+
if (!args[1]) {
|
|
5551
|
+
console.error("[error] site run: <name> is required.");
|
|
5552
|
+
console.error(" Usage: bb-browser site run <name> [args...]");
|
|
5553
|
+
console.error(" Try: bb-browser site list");
|
|
5554
|
+
process.exit(1);
|
|
5555
|
+
}
|
|
5556
|
+
await siteRun(args[1], args.slice(2), options);
|
|
5557
|
+
break;
|
|
5558
|
+
default:
|
|
5559
|
+
if (subCommand.includes("/")) {
|
|
5560
|
+
await siteRun(subCommand, args.slice(1), options);
|
|
5561
|
+
} else {
|
|
5562
|
+
console.error(`[error] site: unknown subcommand "${subCommand}".`);
|
|
5563
|
+
console.error(" Available: list, search, run, update");
|
|
5564
|
+
console.error(" Try: bb-browser site --help");
|
|
5565
|
+
process.exit(1);
|
|
5566
|
+
}
|
|
5567
|
+
break;
|
|
5568
|
+
}
|
|
5569
|
+
}
|
|
5570
|
+
|
|
1424
5571
|
// packages/cli/src/index.ts
|
|
1425
|
-
var VERSION = "0.
|
|
5572
|
+
var VERSION = "0.3.0";
|
|
1426
5573
|
var HELP_TEXT = `
|
|
1427
5574
|
bb-browser - AI Agent \u6D4F\u89C8\u5668\u81EA\u52A8\u5316\u5DE5\u5177
|
|
1428
5575
|
|
|
@@ -1477,10 +5624,19 @@ bb-browser - AI Agent \u6D4F\u89C8\u5668\u81EA\u52A8\u5316\u5DE5\u5177
|
|
|
1477
5624
|
trace start \u5F00\u59CB\u5F55\u5236\u7528\u6237\u64CD\u4F5C
|
|
1478
5625
|
trace stop \u505C\u6B62\u5F55\u5236\uFF0C\u8F93\u51FA\u4E8B\u4EF6\u5217\u8868
|
|
1479
5626
|
trace status \u67E5\u770B\u5F55\u5236\u72B6\u6001
|
|
5627
|
+
fetch <url> \u5728\u6D4F\u89C8\u5668\u4E0A\u4E0B\u6587\u4E2D fetch\uFF08\u81EA\u52A8\u540C\u6E90\u8DEF\u7531\uFF0C\u5E26\u767B\u5F55\u6001\uFF09
|
|
5628
|
+
site \u7F51\u7AD9 CLI \u5316 \u2014 \u7BA1\u7406\u548C\u8FD0\u884C site adapter
|
|
5629
|
+
site list \u5217\u51FA\u6240\u6709\u53EF\u7528 adapter
|
|
5630
|
+
site search <q> \u641C\u7D22 adapter
|
|
5631
|
+
site <name> \u8FD0\u884C adapter\uFF08\u5982 site reddit/thread <url>\uFF09
|
|
5632
|
+
site update \u66F4\u65B0\u793E\u533A adapter \u5E93
|
|
1480
5633
|
|
|
1481
5634
|
\u9009\u9879\uFF1A
|
|
1482
5635
|
--json \u4EE5 JSON \u683C\u5F0F\u8F93\u51FA
|
|
1483
5636
|
-i, --interactive \u53EA\u8F93\u51FA\u53EF\u4EA4\u4E92\u5143\u7D20\uFF08snapshot \u547D\u4EE4\uFF09
|
|
5637
|
+
-c, --compact \u79FB\u9664\u7A7A\u7ED3\u6784\u8282\u70B9\uFF08snapshot \u547D\u4EE4\uFF09
|
|
5638
|
+
-d, --depth <n> \u9650\u5236\u6811\u6DF1\u5EA6\uFF08snapshot \u547D\u4EE4\uFF09
|
|
5639
|
+
-s, --selector <sel> \u9650\u5B9A CSS \u9009\u62E9\u5668\u8303\u56F4\uFF08snapshot \u547D\u4EE4\uFF09
|
|
1484
5640
|
--tab <tabId> \u6307\u5B9A\u64CD\u4F5C\u7684\u6807\u7B7E\u9875 ID
|
|
1485
5641
|
--help, -h \u663E\u793A\u5E2E\u52A9\u4FE1\u606F
|
|
1486
5642
|
--version, -v \u663E\u793A\u7248\u672C\u53F7
|
|
@@ -1507,7 +5663,8 @@ function parseArgs(argv) {
|
|
|
1507
5663
|
json: false,
|
|
1508
5664
|
help: false,
|
|
1509
5665
|
version: false,
|
|
1510
|
-
interactive: false
|
|
5666
|
+
interactive: false,
|
|
5667
|
+
compact: false
|
|
1511
5668
|
}
|
|
1512
5669
|
};
|
|
1513
5670
|
let skipNext = false;
|
|
@@ -1524,9 +5681,23 @@ function parseArgs(argv) {
|
|
|
1524
5681
|
result.flags.version = true;
|
|
1525
5682
|
} else if (arg === "--interactive" || arg === "-i") {
|
|
1526
5683
|
result.flags.interactive = true;
|
|
5684
|
+
} else if (arg === "--compact" || arg === "-c") {
|
|
5685
|
+
result.flags.compact = true;
|
|
5686
|
+
} else if (arg === "--depth" || arg === "-d") {
|
|
5687
|
+
skipNext = true;
|
|
5688
|
+
const nextIdx = args.indexOf(arg) + 1;
|
|
5689
|
+
if (nextIdx < args.length) {
|
|
5690
|
+
result.flags.depth = parseInt(args[nextIdx], 10);
|
|
5691
|
+
}
|
|
5692
|
+
} else if (arg === "--selector" || arg === "-s") {
|
|
5693
|
+
skipNext = true;
|
|
5694
|
+
const nextIdx = args.indexOf(arg) + 1;
|
|
5695
|
+
if (nextIdx < args.length) {
|
|
5696
|
+
result.flags.selector = args[nextIdx];
|
|
5697
|
+
}
|
|
1527
5698
|
} else if (arg === "--id") {
|
|
1528
5699
|
skipNext = true;
|
|
1529
|
-
} else if (arg === "--tab"
|
|
5700
|
+
} else if (arg === "--tab") {
|
|
1530
5701
|
skipNext = true;
|
|
1531
5702
|
} else if (arg.startsWith("-")) {
|
|
1532
5703
|
} else if (result.command === null) {
|
|
@@ -1564,7 +5735,14 @@ async function main() {
|
|
|
1564
5735
|
break;
|
|
1565
5736
|
}
|
|
1566
5737
|
case "snapshot": {
|
|
1567
|
-
await snapshotCommand({
|
|
5738
|
+
await snapshotCommand({
|
|
5739
|
+
json: parsed.flags.json,
|
|
5740
|
+
interactive: parsed.flags.interactive,
|
|
5741
|
+
compact: parsed.flags.compact,
|
|
5742
|
+
maxDepth: parsed.flags.depth,
|
|
5743
|
+
selector: parsed.flags.selector,
|
|
5744
|
+
tabId: globalTabId
|
|
5745
|
+
});
|
|
1568
5746
|
break;
|
|
1569
5747
|
}
|
|
1570
5748
|
case "click": {
|
|
@@ -1696,7 +5874,9 @@ async function main() {
|
|
|
1696
5874
|
}
|
|
1697
5875
|
case "daemon":
|
|
1698
5876
|
case "start": {
|
|
1699
|
-
|
|
5877
|
+
const hostIdx = process.argv.findIndex((a) => a === "--host");
|
|
5878
|
+
const host = hostIdx >= 0 ? process.argv[hostIdx + 1] : void 0;
|
|
5879
|
+
await daemonCommand({ json: parsed.flags.json, host });
|
|
1700
5880
|
break;
|
|
1701
5881
|
}
|
|
1702
5882
|
case "stop": {
|
|
@@ -1807,9 +5987,10 @@ async function main() {
|
|
|
1807
5987
|
const subCommand = parsed.args[0] || "requests";
|
|
1808
5988
|
const urlOrFilter = parsed.args[1];
|
|
1809
5989
|
const abort = process.argv.includes("--abort");
|
|
5990
|
+
const withBody = process.argv.includes("--with-body");
|
|
1810
5991
|
const bodyIndex = process.argv.findIndex((a) => a === "--body");
|
|
1811
5992
|
const body = bodyIndex >= 0 ? process.argv[bodyIndex + 1] : void 0;
|
|
1812
|
-
await networkCommand(subCommand, urlOrFilter, { json: parsed.flags.json, abort, body, tabId: globalTabId });
|
|
5993
|
+
await networkCommand(subCommand, urlOrFilter, { json: parsed.flags.json, abort, body, withBody, tabId: globalTabId });
|
|
1813
5994
|
break;
|
|
1814
5995
|
}
|
|
1815
5996
|
case "console": {
|
|
@@ -1835,6 +6016,36 @@ async function main() {
|
|
|
1835
6016
|
await traceCommand(subCmd, { json: parsed.flags.json, tabId: globalTabId });
|
|
1836
6017
|
break;
|
|
1837
6018
|
}
|
|
6019
|
+
case "fetch": {
|
|
6020
|
+
const fetchUrl = parsed.args[0];
|
|
6021
|
+
if (!fetchUrl) {
|
|
6022
|
+
console.error("[error] fetch: <url> is required.");
|
|
6023
|
+
console.error(" Usage: bb-browser fetch <url> [--json] [--method POST] [--body '{...}']");
|
|
6024
|
+
console.error(" Example: bb-browser fetch https://www.reddit.com/api/me.json --json");
|
|
6025
|
+
process.exit(1);
|
|
6026
|
+
}
|
|
6027
|
+
const methodIdx = process.argv.findIndex((a) => a === "--method");
|
|
6028
|
+
const fetchMethod = methodIdx >= 0 ? process.argv[methodIdx + 1] : void 0;
|
|
6029
|
+
const fetchBodyIdx = process.argv.findIndex((a) => a === "--body");
|
|
6030
|
+
const fetchBody = fetchBodyIdx >= 0 ? process.argv[fetchBodyIdx + 1] : void 0;
|
|
6031
|
+
const headersIdx = process.argv.findIndex((a) => a === "--headers");
|
|
6032
|
+
const fetchHeaders = headersIdx >= 0 ? process.argv[headersIdx + 1] : void 0;
|
|
6033
|
+
const outputIdx = process.argv.findIndex((a) => a === "--output");
|
|
6034
|
+
const fetchOutput = outputIdx >= 0 ? process.argv[outputIdx + 1] : void 0;
|
|
6035
|
+
await fetchCommand(fetchUrl, {
|
|
6036
|
+
json: parsed.flags.json,
|
|
6037
|
+
method: fetchMethod,
|
|
6038
|
+
body: fetchBody,
|
|
6039
|
+
headers: fetchHeaders,
|
|
6040
|
+
output: fetchOutput,
|
|
6041
|
+
tabId: globalTabId
|
|
6042
|
+
});
|
|
6043
|
+
break;
|
|
6044
|
+
}
|
|
6045
|
+
case "site": {
|
|
6046
|
+
await siteCommand(parsed.args, { json: parsed.flags.json, tabId: globalTabId });
|
|
6047
|
+
break;
|
|
6048
|
+
}
|
|
1838
6049
|
default: {
|
|
1839
6050
|
console.error(`\u9519\u8BEF\uFF1A\u672A\u77E5\u547D\u4EE4 "${parsed.command}"`);
|
|
1840
6051
|
console.error("\u8FD0\u884C bb-browser --help \u67E5\u770B\u53EF\u7528\u547D\u4EE4");
|