@rsbuild/core 2.0.0-beta.7 → 2.0.0-beta.9

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.
Files changed (59) hide show
  1. package/compiled/connect-next/index.d.ts +56 -0
  2. package/compiled/{connect → connect-next}/license +1 -0
  3. package/compiled/connect-next/package.json +1 -0
  4. package/compiled/css-loader/index.js +2 -2
  5. package/compiled/html-rspack-plugin/index.js +14 -14
  6. package/compiled/jiti/dist/babel.cjs +60 -60
  7. package/compiled/jiti/dist/jiti.cjs +2 -2
  8. package/compiled/postcss-loader/index.js +6 -6
  9. package/compiled/rslog/index.d.ts +17 -1
  10. package/compiled/rslog/package.json +1 -1
  11. package/compiled/rspack-chain/package.json +1 -1
  12. package/compiled/rspack-chain/types/index.d.ts +0 -3
  13. package/compiled/style-loader/index.js +10 -10
  14. package/dist/{131.js → 958.js} +258 -361
  15. package/dist/chokidar.js +59 -57
  16. package/dist/client/hmr.js +1 -1
  17. package/dist/client/overlay.js +1 -1
  18. package/dist/connect-next.js +268 -0
  19. package/dist/{connect.js.LICENSE.txt → connect-next.js.LICENSE.txt} +3 -13
  20. package/dist/cors.js +2 -2
  21. package/dist/http-proxy-middleware.js +57 -552
  22. package/dist/index.js +1 -1
  23. package/dist/launch-editor-middleware.js +23 -8
  24. package/dist/manifest-plugin.js +18 -18
  25. package/dist/memfs.js +178 -717
  26. package/dist/{710.js → mrmime.js} +2 -1
  27. package/dist/open.js +35 -32
  28. package/dist/range-parser.js +2 -2
  29. package/dist/remapping.js +2 -2
  30. package/dist/rslib-runtime.js +3 -3
  31. package/dist/sirv.js +14 -14
  32. package/dist/src.js +510 -0
  33. package/dist/tinyglobby.js +25 -25
  34. package/dist/transformLoader.mjs +38 -1
  35. package/dist/transformRawLoader.mjs +1 -1
  36. package/dist/ws.js +1541 -0
  37. package/dist-types/helpers/index.d.ts +1 -1
  38. package/dist-types/helpers/vendors.d.ts +0 -1
  39. package/dist-types/server/runner/asModule.d.ts +1 -1
  40. package/dist-types/server/socketServer.d.ts +1 -1
  41. package/dist-types/types/config.d.ts +3 -2
  42. package/dist-types/types/thirdParty.d.ts +1 -1
  43. package/package.json +9 -10
  44. package/compiled/connect/index.d.ts +0 -90
  45. package/compiled/connect/package.json +0 -1
  46. package/compiled/ws/index.d.ts +0 -437
  47. package/compiled/ws/index.js +0 -3166
  48. package/compiled/ws/license +0 -20
  49. package/compiled/ws/package.json +0 -1
  50. package/dist/397.js +0 -11
  51. package/dist/7.js +0 -1
  52. package/dist/712.js +0 -15
  53. package/dist/743.js +0 -7
  54. package/dist/88.js +0 -40
  55. package/dist/connect.js +0 -574
  56. package/dist-types/helpers/color.d.ts +0 -4
  57. /package/dist/{131.js.LICENSE.txt → 958.js.LICENSE.txt} +0 -0
  58. /package/dist/client/{59.js → 797.js} +0 -0
  59. /package/dist/{31.js → trace-mapping.js} +0 -0
@@ -1,3166 +0,0 @@
1
- (() => {
2
- var __webpack_modules__ = {
3
- 159: (module, __unused_webpack_exports, __nccwpck_require__) => {
4
- "use strict";
5
- const WebSocket = __nccwpck_require__(640);
6
- WebSocket.createWebSocketStream = __nccwpck_require__(283);
7
- WebSocket.Server = __nccwpck_require__(686);
8
- WebSocket.Receiver = __nccwpck_require__(522);
9
- WebSocket.Sender = __nccwpck_require__(814);
10
- WebSocket.WebSocket = WebSocket;
11
- WebSocket.WebSocketServer = WebSocket.Server;
12
- module.exports = WebSocket;
13
- },
14
- 398: (module, __unused_webpack_exports, __nccwpck_require__) => {
15
- "use strict";
16
- const { EMPTY_BUFFER } = __nccwpck_require__(146);
17
- const FastBuffer = Buffer[Symbol.species];
18
- function concat(list, totalLength) {
19
- if (list.length === 0) return EMPTY_BUFFER;
20
- if (list.length === 1) return list[0];
21
- const target = Buffer.allocUnsafe(totalLength);
22
- let offset = 0;
23
- for (let i = 0; i < list.length; i++) {
24
- const buf = list[i];
25
- target.set(buf, offset);
26
- offset += buf.length;
27
- }
28
- if (offset < totalLength) {
29
- return new FastBuffer(target.buffer, target.byteOffset, offset);
30
- }
31
- return target;
32
- }
33
- function _mask(source, mask, output, offset, length) {
34
- for (let i = 0; i < length; i++) {
35
- output[offset + i] = source[i] ^ mask[i & 3];
36
- }
37
- }
38
- function _unmask(buffer, mask) {
39
- for (let i = 0; i < buffer.length; i++) {
40
- buffer[i] ^= mask[i & 3];
41
- }
42
- }
43
- function toArrayBuffer(buf) {
44
- if (buf.length === buf.buffer.byteLength) {
45
- return buf.buffer;
46
- }
47
- return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
48
- }
49
- function toBuffer(data) {
50
- toBuffer.readOnly = true;
51
- if (Buffer.isBuffer(data)) return data;
52
- let buf;
53
- if (data instanceof ArrayBuffer) {
54
- buf = new FastBuffer(data);
55
- } else if (ArrayBuffer.isView(data)) {
56
- buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
57
- } else {
58
- buf = Buffer.from(data);
59
- toBuffer.readOnly = false;
60
- }
61
- return buf;
62
- }
63
- module.exports = {
64
- concat,
65
- mask: _mask,
66
- toArrayBuffer,
67
- toBuffer,
68
- unmask: _unmask,
69
- };
70
- if (!process.env.WS_NO_BUFFER_UTIL) {
71
- try {
72
- const bufferUtil = __nccwpck_require__(431);
73
- module.exports.mask = function (
74
- source,
75
- mask,
76
- output,
77
- offset,
78
- length,
79
- ) {
80
- if (length < 48) _mask(source, mask, output, offset, length);
81
- else bufferUtil.mask(source, mask, output, offset, length);
82
- };
83
- module.exports.unmask = function (buffer, mask) {
84
- if (buffer.length < 32) _unmask(buffer, mask);
85
- else bufferUtil.unmask(buffer, mask);
86
- };
87
- } catch (e) {}
88
- }
89
- },
90
- 146: (module) => {
91
- "use strict";
92
- const BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
93
- const hasBlob = typeof Blob !== "undefined";
94
- if (hasBlob) BINARY_TYPES.push("blob");
95
- module.exports = {
96
- BINARY_TYPES,
97
- CLOSE_TIMEOUT: 3e4,
98
- EMPTY_BUFFER: Buffer.alloc(0),
99
- GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
100
- hasBlob,
101
- kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
102
- kListener: Symbol("kListener"),
103
- kStatusCode: Symbol("status-code"),
104
- kWebSocket: Symbol("websocket"),
105
- NOOP: () => {},
106
- };
107
- },
108
- 233: (module, __unused_webpack_exports, __nccwpck_require__) => {
109
- "use strict";
110
- const { kForOnEventAttribute, kListener } = __nccwpck_require__(146);
111
- const kCode = Symbol("kCode");
112
- const kData = Symbol("kData");
113
- const kError = Symbol("kError");
114
- const kMessage = Symbol("kMessage");
115
- const kReason = Symbol("kReason");
116
- const kTarget = Symbol("kTarget");
117
- const kType = Symbol("kType");
118
- const kWasClean = Symbol("kWasClean");
119
- class Event {
120
- constructor(type) {
121
- this[kTarget] = null;
122
- this[kType] = type;
123
- }
124
- get target() {
125
- return this[kTarget];
126
- }
127
- get type() {
128
- return this[kType];
129
- }
130
- }
131
- Object.defineProperty(Event.prototype, "target", { enumerable: true });
132
- Object.defineProperty(Event.prototype, "type", { enumerable: true });
133
- class CloseEvent extends Event {
134
- constructor(type, options = {}) {
135
- super(type);
136
- this[kCode] = options.code === undefined ? 0 : options.code;
137
- this[kReason] = options.reason === undefined ? "" : options.reason;
138
- this[kWasClean] =
139
- options.wasClean === undefined ? false : options.wasClean;
140
- }
141
- get code() {
142
- return this[kCode];
143
- }
144
- get reason() {
145
- return this[kReason];
146
- }
147
- get wasClean() {
148
- return this[kWasClean];
149
- }
150
- }
151
- Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
152
- Object.defineProperty(CloseEvent.prototype, "reason", {
153
- enumerable: true,
154
- });
155
- Object.defineProperty(CloseEvent.prototype, "wasClean", {
156
- enumerable: true,
157
- });
158
- class ErrorEvent extends Event {
159
- constructor(type, options = {}) {
160
- super(type);
161
- this[kError] = options.error === undefined ? null : options.error;
162
- this[kMessage] = options.message === undefined ? "" : options.message;
163
- }
164
- get error() {
165
- return this[kError];
166
- }
167
- get message() {
168
- return this[kMessage];
169
- }
170
- }
171
- Object.defineProperty(ErrorEvent.prototype, "error", {
172
- enumerable: true,
173
- });
174
- Object.defineProperty(ErrorEvent.prototype, "message", {
175
- enumerable: true,
176
- });
177
- class MessageEvent extends Event {
178
- constructor(type, options = {}) {
179
- super(type);
180
- this[kData] = options.data === undefined ? null : options.data;
181
- }
182
- get data() {
183
- return this[kData];
184
- }
185
- }
186
- Object.defineProperty(MessageEvent.prototype, "data", {
187
- enumerable: true,
188
- });
189
- const EventTarget = {
190
- addEventListener(type, handler, options = {}) {
191
- for (const listener of this.listeners(type)) {
192
- if (
193
- !options[kForOnEventAttribute] &&
194
- listener[kListener] === handler &&
195
- !listener[kForOnEventAttribute]
196
- ) {
197
- return;
198
- }
199
- }
200
- let wrapper;
201
- if (type === "message") {
202
- wrapper = function onMessage(data, isBinary) {
203
- const event = new MessageEvent("message", {
204
- data: isBinary ? data : data.toString(),
205
- });
206
- event[kTarget] = this;
207
- callListener(handler, this, event);
208
- };
209
- } else if (type === "close") {
210
- wrapper = function onClose(code, message) {
211
- const event = new CloseEvent("close", {
212
- code,
213
- reason: message.toString(),
214
- wasClean: this._closeFrameReceived && this._closeFrameSent,
215
- });
216
- event[kTarget] = this;
217
- callListener(handler, this, event);
218
- };
219
- } else if (type === "error") {
220
- wrapper = function onError(error) {
221
- const event = new ErrorEvent("error", {
222
- error,
223
- message: error.message,
224
- });
225
- event[kTarget] = this;
226
- callListener(handler, this, event);
227
- };
228
- } else if (type === "open") {
229
- wrapper = function onOpen() {
230
- const event = new Event("open");
231
- event[kTarget] = this;
232
- callListener(handler, this, event);
233
- };
234
- } else {
235
- return;
236
- }
237
- wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
238
- wrapper[kListener] = handler;
239
- if (options.once) {
240
- this.once(type, wrapper);
241
- } else {
242
- this.on(type, wrapper);
243
- }
244
- },
245
- removeEventListener(type, handler) {
246
- for (const listener of this.listeners(type)) {
247
- if (
248
- listener[kListener] === handler &&
249
- !listener[kForOnEventAttribute]
250
- ) {
251
- this.removeListener(type, listener);
252
- break;
253
- }
254
- }
255
- },
256
- };
257
- module.exports = {
258
- CloseEvent,
259
- ErrorEvent,
260
- Event,
261
- EventTarget,
262
- MessageEvent,
263
- };
264
- function callListener(listener, thisArg, event) {
265
- if (typeof listener === "object" && listener.handleEvent) {
266
- listener.handleEvent.call(listener, event);
267
- } else {
268
- listener.call(thisArg, event);
269
- }
270
- }
271
- },
272
- 994: (module, __unused_webpack_exports, __nccwpck_require__) => {
273
- "use strict";
274
- const { tokenChars } = __nccwpck_require__(788);
275
- function push(dest, name, elem) {
276
- if (dest[name] === undefined) dest[name] = [elem];
277
- else dest[name].push(elem);
278
- }
279
- function parse(header) {
280
- const offers = Object.create(null);
281
- let params = Object.create(null);
282
- let mustUnescape = false;
283
- let isEscaping = false;
284
- let inQuotes = false;
285
- let extensionName;
286
- let paramName;
287
- let start = -1;
288
- let code = -1;
289
- let end = -1;
290
- let i = 0;
291
- for (; i < header.length; i++) {
292
- code = header.charCodeAt(i);
293
- if (extensionName === undefined) {
294
- if (end === -1 && tokenChars[code] === 1) {
295
- if (start === -1) start = i;
296
- } else if (i !== 0 && (code === 32 || code === 9)) {
297
- if (end === -1 && start !== -1) end = i;
298
- } else if (code === 59 || code === 44) {
299
- if (start === -1) {
300
- throw new SyntaxError(`Unexpected character at index ${i}`);
301
- }
302
- if (end === -1) end = i;
303
- const name = header.slice(start, end);
304
- if (code === 44) {
305
- push(offers, name, params);
306
- params = Object.create(null);
307
- } else {
308
- extensionName = name;
309
- }
310
- start = end = -1;
311
- } else {
312
- throw new SyntaxError(`Unexpected character at index ${i}`);
313
- }
314
- } else if (paramName === undefined) {
315
- if (end === -1 && tokenChars[code] === 1) {
316
- if (start === -1) start = i;
317
- } else if (code === 32 || code === 9) {
318
- if (end === -1 && start !== -1) end = i;
319
- } else if (code === 59 || code === 44) {
320
- if (start === -1) {
321
- throw new SyntaxError(`Unexpected character at index ${i}`);
322
- }
323
- if (end === -1) end = i;
324
- push(params, header.slice(start, end), true);
325
- if (code === 44) {
326
- push(offers, extensionName, params);
327
- params = Object.create(null);
328
- extensionName = undefined;
329
- }
330
- start = end = -1;
331
- } else if (code === 61 && start !== -1 && end === -1) {
332
- paramName = header.slice(start, i);
333
- start = end = -1;
334
- } else {
335
- throw new SyntaxError(`Unexpected character at index ${i}`);
336
- }
337
- } else {
338
- if (isEscaping) {
339
- if (tokenChars[code] !== 1) {
340
- throw new SyntaxError(`Unexpected character at index ${i}`);
341
- }
342
- if (start === -1) start = i;
343
- else if (!mustUnescape) mustUnescape = true;
344
- isEscaping = false;
345
- } else if (inQuotes) {
346
- if (tokenChars[code] === 1) {
347
- if (start === -1) start = i;
348
- } else if (code === 34 && start !== -1) {
349
- inQuotes = false;
350
- end = i;
351
- } else if (code === 92) {
352
- isEscaping = true;
353
- } else {
354
- throw new SyntaxError(`Unexpected character at index ${i}`);
355
- }
356
- } else if (code === 34 && header.charCodeAt(i - 1) === 61) {
357
- inQuotes = true;
358
- } else if (end === -1 && tokenChars[code] === 1) {
359
- if (start === -1) start = i;
360
- } else if (start !== -1 && (code === 32 || code === 9)) {
361
- if (end === -1) end = i;
362
- } else if (code === 59 || code === 44) {
363
- if (start === -1) {
364
- throw new SyntaxError(`Unexpected character at index ${i}`);
365
- }
366
- if (end === -1) end = i;
367
- let value = header.slice(start, end);
368
- if (mustUnescape) {
369
- value = value.replace(/\\/g, "");
370
- mustUnescape = false;
371
- }
372
- push(params, paramName, value);
373
- if (code === 44) {
374
- push(offers, extensionName, params);
375
- params = Object.create(null);
376
- extensionName = undefined;
377
- }
378
- paramName = undefined;
379
- start = end = -1;
380
- } else {
381
- throw new SyntaxError(`Unexpected character at index ${i}`);
382
- }
383
- }
384
- }
385
- if (start === -1 || inQuotes || code === 32 || code === 9) {
386
- throw new SyntaxError("Unexpected end of input");
387
- }
388
- if (end === -1) end = i;
389
- const token = header.slice(start, end);
390
- if (extensionName === undefined) {
391
- push(offers, token, params);
392
- } else {
393
- if (paramName === undefined) {
394
- push(params, token, true);
395
- } else if (mustUnescape) {
396
- push(params, paramName, token.replace(/\\/g, ""));
397
- } else {
398
- push(params, paramName, token);
399
- }
400
- push(offers, extensionName, params);
401
- }
402
- return offers;
403
- }
404
- function format(extensions) {
405
- return Object.keys(extensions)
406
- .map((extension) => {
407
- let configurations = extensions[extension];
408
- if (!Array.isArray(configurations))
409
- configurations = [configurations];
410
- return configurations
411
- .map((params) =>
412
- [extension]
413
- .concat(
414
- Object.keys(params).map((k) => {
415
- let values = params[k];
416
- if (!Array.isArray(values)) values = [values];
417
- return values
418
- .map((v) => (v === true ? k : `${k}=${v}`))
419
- .join("; ");
420
- }),
421
- )
422
- .join("; "),
423
- )
424
- .join(", ");
425
- })
426
- .join(", ");
427
- }
428
- module.exports = { format, parse };
429
- },
430
- 51: (module) => {
431
- "use strict";
432
- const kDone = Symbol("kDone");
433
- const kRun = Symbol("kRun");
434
- class Limiter {
435
- constructor(concurrency) {
436
- this[kDone] = () => {
437
- this.pending--;
438
- this[kRun]();
439
- };
440
- this.concurrency = concurrency || Infinity;
441
- this.jobs = [];
442
- this.pending = 0;
443
- }
444
- add(job) {
445
- this.jobs.push(job);
446
- this[kRun]();
447
- }
448
- [kRun]() {
449
- if (this.pending === this.concurrency) return;
450
- if (this.jobs.length) {
451
- const job = this.jobs.shift();
452
- this.pending++;
453
- job(this[kDone]);
454
- }
455
- }
456
- }
457
- module.exports = Limiter;
458
- },
459
- 279: (module, __unused_webpack_exports, __nccwpck_require__) => {
460
- "use strict";
461
- const zlib = __nccwpck_require__(106);
462
- const bufferUtil = __nccwpck_require__(398);
463
- const Limiter = __nccwpck_require__(51);
464
- const { kStatusCode } = __nccwpck_require__(146);
465
- const FastBuffer = Buffer[Symbol.species];
466
- const TRAILER = Buffer.from([0, 0, 255, 255]);
467
- const kPerMessageDeflate = Symbol("permessage-deflate");
468
- const kTotalLength = Symbol("total-length");
469
- const kCallback = Symbol("callback");
470
- const kBuffers = Symbol("buffers");
471
- const kError = Symbol("error");
472
- let zlibLimiter;
473
- class PerMessageDeflate {
474
- constructor(options, isServer, maxPayload) {
475
- this._maxPayload = maxPayload | 0;
476
- this._options = options || {};
477
- this._threshold =
478
- this._options.threshold !== undefined
479
- ? this._options.threshold
480
- : 1024;
481
- this._isServer = !!isServer;
482
- this._deflate = null;
483
- this._inflate = null;
484
- this.params = null;
485
- if (!zlibLimiter) {
486
- const concurrency =
487
- this._options.concurrencyLimit !== undefined
488
- ? this._options.concurrencyLimit
489
- : 10;
490
- zlibLimiter = new Limiter(concurrency);
491
- }
492
- }
493
- static get extensionName() {
494
- return "permessage-deflate";
495
- }
496
- offer() {
497
- const params = {};
498
- if (this._options.serverNoContextTakeover) {
499
- params.server_no_context_takeover = true;
500
- }
501
- if (this._options.clientNoContextTakeover) {
502
- params.client_no_context_takeover = true;
503
- }
504
- if (this._options.serverMaxWindowBits) {
505
- params.server_max_window_bits = this._options.serverMaxWindowBits;
506
- }
507
- if (this._options.clientMaxWindowBits) {
508
- params.client_max_window_bits = this._options.clientMaxWindowBits;
509
- } else if (this._options.clientMaxWindowBits == null) {
510
- params.client_max_window_bits = true;
511
- }
512
- return params;
513
- }
514
- accept(configurations) {
515
- configurations = this.normalizeParams(configurations);
516
- this.params = this._isServer
517
- ? this.acceptAsServer(configurations)
518
- : this.acceptAsClient(configurations);
519
- return this.params;
520
- }
521
- cleanup() {
522
- if (this._inflate) {
523
- this._inflate.close();
524
- this._inflate = null;
525
- }
526
- if (this._deflate) {
527
- const callback = this._deflate[kCallback];
528
- this._deflate.close();
529
- this._deflate = null;
530
- if (callback) {
531
- callback(
532
- new Error(
533
- "The deflate stream was closed while data was being processed",
534
- ),
535
- );
536
- }
537
- }
538
- }
539
- acceptAsServer(offers) {
540
- const opts = this._options;
541
- const accepted = offers.find((params) => {
542
- if (
543
- (opts.serverNoContextTakeover === false &&
544
- params.server_no_context_takeover) ||
545
- (params.server_max_window_bits &&
546
- (opts.serverMaxWindowBits === false ||
547
- (typeof opts.serverMaxWindowBits === "number" &&
548
- opts.serverMaxWindowBits >
549
- params.server_max_window_bits))) ||
550
- (typeof opts.clientMaxWindowBits === "number" &&
551
- !params.client_max_window_bits)
552
- ) {
553
- return false;
554
- }
555
- return true;
556
- });
557
- if (!accepted) {
558
- throw new Error("None of the extension offers can be accepted");
559
- }
560
- if (opts.serverNoContextTakeover) {
561
- accepted.server_no_context_takeover = true;
562
- }
563
- if (opts.clientNoContextTakeover) {
564
- accepted.client_no_context_takeover = true;
565
- }
566
- if (typeof opts.serverMaxWindowBits === "number") {
567
- accepted.server_max_window_bits = opts.serverMaxWindowBits;
568
- }
569
- if (typeof opts.clientMaxWindowBits === "number") {
570
- accepted.client_max_window_bits = opts.clientMaxWindowBits;
571
- } else if (
572
- accepted.client_max_window_bits === true ||
573
- opts.clientMaxWindowBits === false
574
- ) {
575
- delete accepted.client_max_window_bits;
576
- }
577
- return accepted;
578
- }
579
- acceptAsClient(response) {
580
- const params = response[0];
581
- if (
582
- this._options.clientNoContextTakeover === false &&
583
- params.client_no_context_takeover
584
- ) {
585
- throw new Error(
586
- 'Unexpected parameter "client_no_context_takeover"',
587
- );
588
- }
589
- if (!params.client_max_window_bits) {
590
- if (typeof this._options.clientMaxWindowBits === "number") {
591
- params.client_max_window_bits = this._options.clientMaxWindowBits;
592
- }
593
- } else if (
594
- this._options.clientMaxWindowBits === false ||
595
- (typeof this._options.clientMaxWindowBits === "number" &&
596
- params.client_max_window_bits > this._options.clientMaxWindowBits)
597
- ) {
598
- throw new Error(
599
- 'Unexpected or invalid parameter "client_max_window_bits"',
600
- );
601
- }
602
- return params;
603
- }
604
- normalizeParams(configurations) {
605
- configurations.forEach((params) => {
606
- Object.keys(params).forEach((key) => {
607
- let value = params[key];
608
- if (value.length > 1) {
609
- throw new Error(
610
- `Parameter "${key}" must have only a single value`,
611
- );
612
- }
613
- value = value[0];
614
- if (key === "client_max_window_bits") {
615
- if (value !== true) {
616
- const num = +value;
617
- if (!Number.isInteger(num) || num < 8 || num > 15) {
618
- throw new TypeError(
619
- `Invalid value for parameter "${key}": ${value}`,
620
- );
621
- }
622
- value = num;
623
- } else if (!this._isServer) {
624
- throw new TypeError(
625
- `Invalid value for parameter "${key}": ${value}`,
626
- );
627
- }
628
- } else if (key === "server_max_window_bits") {
629
- const num = +value;
630
- if (!Number.isInteger(num) || num < 8 || num > 15) {
631
- throw new TypeError(
632
- `Invalid value for parameter "${key}": ${value}`,
633
- );
634
- }
635
- value = num;
636
- } else if (
637
- key === "client_no_context_takeover" ||
638
- key === "server_no_context_takeover"
639
- ) {
640
- if (value !== true) {
641
- throw new TypeError(
642
- `Invalid value for parameter "${key}": ${value}`,
643
- );
644
- }
645
- } else {
646
- throw new Error(`Unknown parameter "${key}"`);
647
- }
648
- params[key] = value;
649
- });
650
- });
651
- return configurations;
652
- }
653
- decompress(data, fin, callback) {
654
- zlibLimiter.add((done) => {
655
- this._decompress(data, fin, (err, result) => {
656
- done();
657
- callback(err, result);
658
- });
659
- });
660
- }
661
- compress(data, fin, callback) {
662
- zlibLimiter.add((done) => {
663
- this._compress(data, fin, (err, result) => {
664
- done();
665
- callback(err, result);
666
- });
667
- });
668
- }
669
- _decompress(data, fin, callback) {
670
- const endpoint = this._isServer ? "client" : "server";
671
- if (!this._inflate) {
672
- const key = `${endpoint}_max_window_bits`;
673
- const windowBits =
674
- typeof this.params[key] !== "number"
675
- ? zlib.Z_DEFAULT_WINDOWBITS
676
- : this.params[key];
677
- this._inflate = zlib.createInflateRaw({
678
- ...this._options.zlibInflateOptions,
679
- windowBits,
680
- });
681
- this._inflate[kPerMessageDeflate] = this;
682
- this._inflate[kTotalLength] = 0;
683
- this._inflate[kBuffers] = [];
684
- this._inflate.on("error", inflateOnError);
685
- this._inflate.on("data", inflateOnData);
686
- }
687
- this._inflate[kCallback] = callback;
688
- this._inflate.write(data);
689
- if (fin) this._inflate.write(TRAILER);
690
- this._inflate.flush(() => {
691
- const err = this._inflate[kError];
692
- if (err) {
693
- this._inflate.close();
694
- this._inflate = null;
695
- callback(err);
696
- return;
697
- }
698
- const data = bufferUtil.concat(
699
- this._inflate[kBuffers],
700
- this._inflate[kTotalLength],
701
- );
702
- if (this._inflate._readableState.endEmitted) {
703
- this._inflate.close();
704
- this._inflate = null;
705
- } else {
706
- this._inflate[kTotalLength] = 0;
707
- this._inflate[kBuffers] = [];
708
- if (fin && this.params[`${endpoint}_no_context_takeover`]) {
709
- this._inflate.reset();
710
- }
711
- }
712
- callback(null, data);
713
- });
714
- }
715
- _compress(data, fin, callback) {
716
- const endpoint = this._isServer ? "server" : "client";
717
- if (!this._deflate) {
718
- const key = `${endpoint}_max_window_bits`;
719
- const windowBits =
720
- typeof this.params[key] !== "number"
721
- ? zlib.Z_DEFAULT_WINDOWBITS
722
- : this.params[key];
723
- this._deflate = zlib.createDeflateRaw({
724
- ...this._options.zlibDeflateOptions,
725
- windowBits,
726
- });
727
- this._deflate[kTotalLength] = 0;
728
- this._deflate[kBuffers] = [];
729
- this._deflate.on("data", deflateOnData);
730
- }
731
- this._deflate[kCallback] = callback;
732
- this._deflate.write(data);
733
- this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
734
- if (!this._deflate) {
735
- return;
736
- }
737
- let data = bufferUtil.concat(
738
- this._deflate[kBuffers],
739
- this._deflate[kTotalLength],
740
- );
741
- if (fin) {
742
- data = new FastBuffer(
743
- data.buffer,
744
- data.byteOffset,
745
- data.length - 4,
746
- );
747
- }
748
- this._deflate[kCallback] = null;
749
- this._deflate[kTotalLength] = 0;
750
- this._deflate[kBuffers] = [];
751
- if (fin && this.params[`${endpoint}_no_context_takeover`]) {
752
- this._deflate.reset();
753
- }
754
- callback(null, data);
755
- });
756
- }
757
- }
758
- module.exports = PerMessageDeflate;
759
- function deflateOnData(chunk) {
760
- this[kBuffers].push(chunk);
761
- this[kTotalLength] += chunk.length;
762
- }
763
- function inflateOnData(chunk) {
764
- this[kTotalLength] += chunk.length;
765
- if (
766
- this[kPerMessageDeflate]._maxPayload < 1 ||
767
- this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
768
- ) {
769
- this[kBuffers].push(chunk);
770
- return;
771
- }
772
- this[kError] = new RangeError("Max payload size exceeded");
773
- this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
774
- this[kError][kStatusCode] = 1009;
775
- this.removeListener("data", inflateOnData);
776
- this.reset();
777
- }
778
- function inflateOnError(err) {
779
- this[kPerMessageDeflate]._inflate = null;
780
- if (this[kError]) {
781
- this[kCallback](this[kError]);
782
- return;
783
- }
784
- err[kStatusCode] = 1007;
785
- this[kCallback](err);
786
- }
787
- },
788
- 522: (module, __unused_webpack_exports, __nccwpck_require__) => {
789
- "use strict";
790
- const { Writable } = __nccwpck_require__(203);
791
- const PerMessageDeflate = __nccwpck_require__(279);
792
- const { BINARY_TYPES, EMPTY_BUFFER, kStatusCode, kWebSocket } =
793
- __nccwpck_require__(146);
794
- const { concat, toArrayBuffer, unmask } = __nccwpck_require__(398);
795
- const { isValidStatusCode, isValidUTF8 } = __nccwpck_require__(788);
796
- const FastBuffer = Buffer[Symbol.species];
797
- const GET_INFO = 0;
798
- const GET_PAYLOAD_LENGTH_16 = 1;
799
- const GET_PAYLOAD_LENGTH_64 = 2;
800
- const GET_MASK = 3;
801
- const GET_DATA = 4;
802
- const INFLATING = 5;
803
- const DEFER_EVENT = 6;
804
- class Receiver extends Writable {
805
- constructor(options = {}) {
806
- super();
807
- this._allowSynchronousEvents =
808
- options.allowSynchronousEvents !== undefined
809
- ? options.allowSynchronousEvents
810
- : true;
811
- this._binaryType = options.binaryType || BINARY_TYPES[0];
812
- this._extensions = options.extensions || {};
813
- this._isServer = !!options.isServer;
814
- this._maxPayload = options.maxPayload | 0;
815
- this._skipUTF8Validation = !!options.skipUTF8Validation;
816
- this[kWebSocket] = undefined;
817
- this._bufferedBytes = 0;
818
- this._buffers = [];
819
- this._compressed = false;
820
- this._payloadLength = 0;
821
- this._mask = undefined;
822
- this._fragmented = 0;
823
- this._masked = false;
824
- this._fin = false;
825
- this._opcode = 0;
826
- this._totalPayloadLength = 0;
827
- this._messageLength = 0;
828
- this._fragments = [];
829
- this._errored = false;
830
- this._loop = false;
831
- this._state = GET_INFO;
832
- }
833
- _write(chunk, encoding, cb) {
834
- if (this._opcode === 8 && this._state == GET_INFO) return cb();
835
- this._bufferedBytes += chunk.length;
836
- this._buffers.push(chunk);
837
- this.startLoop(cb);
838
- }
839
- consume(n) {
840
- this._bufferedBytes -= n;
841
- if (n === this._buffers[0].length) return this._buffers.shift();
842
- if (n < this._buffers[0].length) {
843
- const buf = this._buffers[0];
844
- this._buffers[0] = new FastBuffer(
845
- buf.buffer,
846
- buf.byteOffset + n,
847
- buf.length - n,
848
- );
849
- return new FastBuffer(buf.buffer, buf.byteOffset, n);
850
- }
851
- const dst = Buffer.allocUnsafe(n);
852
- do {
853
- const buf = this._buffers[0];
854
- const offset = dst.length - n;
855
- if (n >= buf.length) {
856
- dst.set(this._buffers.shift(), offset);
857
- } else {
858
- dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
859
- this._buffers[0] = new FastBuffer(
860
- buf.buffer,
861
- buf.byteOffset + n,
862
- buf.length - n,
863
- );
864
- }
865
- n -= buf.length;
866
- } while (n > 0);
867
- return dst;
868
- }
869
- startLoop(cb) {
870
- this._loop = true;
871
- do {
872
- switch (this._state) {
873
- case GET_INFO:
874
- this.getInfo(cb);
875
- break;
876
- case GET_PAYLOAD_LENGTH_16:
877
- this.getPayloadLength16(cb);
878
- break;
879
- case GET_PAYLOAD_LENGTH_64:
880
- this.getPayloadLength64(cb);
881
- break;
882
- case GET_MASK:
883
- this.getMask();
884
- break;
885
- case GET_DATA:
886
- this.getData(cb);
887
- break;
888
- case INFLATING:
889
- case DEFER_EVENT:
890
- this._loop = false;
891
- return;
892
- }
893
- } while (this._loop);
894
- if (!this._errored) cb();
895
- }
896
- getInfo(cb) {
897
- if (this._bufferedBytes < 2) {
898
- this._loop = false;
899
- return;
900
- }
901
- const buf = this.consume(2);
902
- if ((buf[0] & 48) !== 0) {
903
- const error = this.createError(
904
- RangeError,
905
- "RSV2 and RSV3 must be clear",
906
- true,
907
- 1002,
908
- "WS_ERR_UNEXPECTED_RSV_2_3",
909
- );
910
- cb(error);
911
- return;
912
- }
913
- const compressed = (buf[0] & 64) === 64;
914
- if (
915
- compressed &&
916
- !this._extensions[PerMessageDeflate.extensionName]
917
- ) {
918
- const error = this.createError(
919
- RangeError,
920
- "RSV1 must be clear",
921
- true,
922
- 1002,
923
- "WS_ERR_UNEXPECTED_RSV_1",
924
- );
925
- cb(error);
926
- return;
927
- }
928
- this._fin = (buf[0] & 128) === 128;
929
- this._opcode = buf[0] & 15;
930
- this._payloadLength = buf[1] & 127;
931
- if (this._opcode === 0) {
932
- if (compressed) {
933
- const error = this.createError(
934
- RangeError,
935
- "RSV1 must be clear",
936
- true,
937
- 1002,
938
- "WS_ERR_UNEXPECTED_RSV_1",
939
- );
940
- cb(error);
941
- return;
942
- }
943
- if (!this._fragmented) {
944
- const error = this.createError(
945
- RangeError,
946
- "invalid opcode 0",
947
- true,
948
- 1002,
949
- "WS_ERR_INVALID_OPCODE",
950
- );
951
- cb(error);
952
- return;
953
- }
954
- this._opcode = this._fragmented;
955
- } else if (this._opcode === 1 || this._opcode === 2) {
956
- if (this._fragmented) {
957
- const error = this.createError(
958
- RangeError,
959
- `invalid opcode ${this._opcode}`,
960
- true,
961
- 1002,
962
- "WS_ERR_INVALID_OPCODE",
963
- );
964
- cb(error);
965
- return;
966
- }
967
- this._compressed = compressed;
968
- } else if (this._opcode > 7 && this._opcode < 11) {
969
- if (!this._fin) {
970
- const error = this.createError(
971
- RangeError,
972
- "FIN must be set",
973
- true,
974
- 1002,
975
- "WS_ERR_EXPECTED_FIN",
976
- );
977
- cb(error);
978
- return;
979
- }
980
- if (compressed) {
981
- const error = this.createError(
982
- RangeError,
983
- "RSV1 must be clear",
984
- true,
985
- 1002,
986
- "WS_ERR_UNEXPECTED_RSV_1",
987
- );
988
- cb(error);
989
- return;
990
- }
991
- if (
992
- this._payloadLength > 125 ||
993
- (this._opcode === 8 && this._payloadLength === 1)
994
- ) {
995
- const error = this.createError(
996
- RangeError,
997
- `invalid payload length ${this._payloadLength}`,
998
- true,
999
- 1002,
1000
- "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH",
1001
- );
1002
- cb(error);
1003
- return;
1004
- }
1005
- } else {
1006
- const error = this.createError(
1007
- RangeError,
1008
- `invalid opcode ${this._opcode}`,
1009
- true,
1010
- 1002,
1011
- "WS_ERR_INVALID_OPCODE",
1012
- );
1013
- cb(error);
1014
- return;
1015
- }
1016
- if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
1017
- this._masked = (buf[1] & 128) === 128;
1018
- if (this._isServer) {
1019
- if (!this._masked) {
1020
- const error = this.createError(
1021
- RangeError,
1022
- "MASK must be set",
1023
- true,
1024
- 1002,
1025
- "WS_ERR_EXPECTED_MASK",
1026
- );
1027
- cb(error);
1028
- return;
1029
- }
1030
- } else if (this._masked) {
1031
- const error = this.createError(
1032
- RangeError,
1033
- "MASK must be clear",
1034
- true,
1035
- 1002,
1036
- "WS_ERR_UNEXPECTED_MASK",
1037
- );
1038
- cb(error);
1039
- return;
1040
- }
1041
- if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
1042
- else if (this._payloadLength === 127)
1043
- this._state = GET_PAYLOAD_LENGTH_64;
1044
- else this.haveLength(cb);
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
- getPayloadLength64(cb) {
1055
- if (this._bufferedBytes < 8) {
1056
- this._loop = false;
1057
- return;
1058
- }
1059
- const buf = this.consume(8);
1060
- const num = buf.readUInt32BE(0);
1061
- if (num > Math.pow(2, 53 - 32) - 1) {
1062
- const error = this.createError(
1063
- RangeError,
1064
- "Unsupported WebSocket frame: payload length > 2^53 - 1",
1065
- false,
1066
- 1009,
1067
- "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH",
1068
- );
1069
- cb(error);
1070
- return;
1071
- }
1072
- this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
1073
- this.haveLength(cb);
1074
- }
1075
- haveLength(cb) {
1076
- if (this._payloadLength && this._opcode < 8) {
1077
- this._totalPayloadLength += this._payloadLength;
1078
- if (
1079
- this._totalPayloadLength > this._maxPayload &&
1080
- this._maxPayload > 0
1081
- ) {
1082
- const error = this.createError(
1083
- RangeError,
1084
- "Max payload size exceeded",
1085
- false,
1086
- 1009,
1087
- "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH",
1088
- );
1089
- cb(error);
1090
- return;
1091
- }
1092
- }
1093
- if (this._masked) this._state = GET_MASK;
1094
- else this._state = GET_DATA;
1095
- }
1096
- getMask() {
1097
- if (this._bufferedBytes < 4) {
1098
- this._loop = false;
1099
- return;
1100
- }
1101
- this._mask = this.consume(4);
1102
- this._state = GET_DATA;
1103
- }
1104
- getData(cb) {
1105
- let data = EMPTY_BUFFER;
1106
- if (this._payloadLength) {
1107
- if (this._bufferedBytes < this._payloadLength) {
1108
- this._loop = false;
1109
- return;
1110
- }
1111
- data = this.consume(this._payloadLength);
1112
- if (
1113
- this._masked &&
1114
- (this._mask[0] |
1115
- this._mask[1] |
1116
- this._mask[2] |
1117
- this._mask[3]) !==
1118
- 0
1119
- ) {
1120
- unmask(data, this._mask);
1121
- }
1122
- }
1123
- if (this._opcode > 7) {
1124
- this.controlMessage(data, cb);
1125
- return;
1126
- }
1127
- if (this._compressed) {
1128
- this._state = INFLATING;
1129
- this.decompress(data, cb);
1130
- return;
1131
- }
1132
- if (data.length) {
1133
- this._messageLength = this._totalPayloadLength;
1134
- this._fragments.push(data);
1135
- }
1136
- this.dataMessage(cb);
1137
- }
1138
- decompress(data, cb) {
1139
- const perMessageDeflate =
1140
- this._extensions[PerMessageDeflate.extensionName];
1141
- perMessageDeflate.decompress(data, this._fin, (err, buf) => {
1142
- if (err) return cb(err);
1143
- if (buf.length) {
1144
- this._messageLength += buf.length;
1145
- if (
1146
- this._messageLength > this._maxPayload &&
1147
- this._maxPayload > 0
1148
- ) {
1149
- const error = this.createError(
1150
- RangeError,
1151
- "Max payload size exceeded",
1152
- false,
1153
- 1009,
1154
- "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH",
1155
- );
1156
- cb(error);
1157
- return;
1158
- }
1159
- this._fragments.push(buf);
1160
- }
1161
- this.dataMessage(cb);
1162
- if (this._state === GET_INFO) this.startLoop(cb);
1163
- });
1164
- }
1165
- dataMessage(cb) {
1166
- if (!this._fin) {
1167
- this._state = GET_INFO;
1168
- return;
1169
- }
1170
- const messageLength = this._messageLength;
1171
- const fragments = this._fragments;
1172
- this._totalPayloadLength = 0;
1173
- this._messageLength = 0;
1174
- this._fragmented = 0;
1175
- this._fragments = [];
1176
- if (this._opcode === 2) {
1177
- let data;
1178
- if (this._binaryType === "nodebuffer") {
1179
- data = concat(fragments, messageLength);
1180
- } else if (this._binaryType === "arraybuffer") {
1181
- data = toArrayBuffer(concat(fragments, messageLength));
1182
- } else if (this._binaryType === "blob") {
1183
- data = new Blob(fragments);
1184
- } else {
1185
- data = fragments;
1186
- }
1187
- if (this._allowSynchronousEvents) {
1188
- this.emit("message", data, true);
1189
- this._state = GET_INFO;
1190
- } else {
1191
- this._state = DEFER_EVENT;
1192
- setImmediate(() => {
1193
- this.emit("message", data, true);
1194
- this._state = GET_INFO;
1195
- this.startLoop(cb);
1196
- });
1197
- }
1198
- } else {
1199
- const buf = concat(fragments, messageLength);
1200
- if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
1201
- const error = this.createError(
1202
- Error,
1203
- "invalid UTF-8 sequence",
1204
- true,
1205
- 1007,
1206
- "WS_ERR_INVALID_UTF8",
1207
- );
1208
- cb(error);
1209
- return;
1210
- }
1211
- if (this._state === INFLATING || this._allowSynchronousEvents) {
1212
- this.emit("message", buf, false);
1213
- this._state = GET_INFO;
1214
- } else {
1215
- this._state = DEFER_EVENT;
1216
- setImmediate(() => {
1217
- this.emit("message", buf, false);
1218
- this._state = GET_INFO;
1219
- this.startLoop(cb);
1220
- });
1221
- }
1222
- }
1223
- }
1224
- controlMessage(data, cb) {
1225
- if (this._opcode === 8) {
1226
- if (data.length === 0) {
1227
- this._loop = false;
1228
- this.emit("conclude", 1005, EMPTY_BUFFER);
1229
- this.end();
1230
- } else {
1231
- const code = data.readUInt16BE(0);
1232
- if (!isValidStatusCode(code)) {
1233
- const error = this.createError(
1234
- RangeError,
1235
- `invalid status code ${code}`,
1236
- true,
1237
- 1002,
1238
- "WS_ERR_INVALID_CLOSE_CODE",
1239
- );
1240
- cb(error);
1241
- return;
1242
- }
1243
- const buf = new FastBuffer(
1244
- data.buffer,
1245
- data.byteOffset + 2,
1246
- data.length - 2,
1247
- );
1248
- if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
1249
- const error = this.createError(
1250
- Error,
1251
- "invalid UTF-8 sequence",
1252
- true,
1253
- 1007,
1254
- "WS_ERR_INVALID_UTF8",
1255
- );
1256
- cb(error);
1257
- return;
1258
- }
1259
- this._loop = false;
1260
- this.emit("conclude", code, buf);
1261
- this.end();
1262
- }
1263
- this._state = GET_INFO;
1264
- return;
1265
- }
1266
- if (this._allowSynchronousEvents) {
1267
- this.emit(this._opcode === 9 ? "ping" : "pong", data);
1268
- this._state = GET_INFO;
1269
- } else {
1270
- this._state = DEFER_EVENT;
1271
- setImmediate(() => {
1272
- this.emit(this._opcode === 9 ? "ping" : "pong", data);
1273
- this._state = GET_INFO;
1274
- this.startLoop(cb);
1275
- });
1276
- }
1277
- }
1278
- createError(ErrorCtor, message, prefix, statusCode, errorCode) {
1279
- this._loop = false;
1280
- this._errored = true;
1281
- const err = new ErrorCtor(
1282
- prefix ? `Invalid WebSocket frame: ${message}` : message,
1283
- );
1284
- Error.captureStackTrace(err, this.createError);
1285
- err.code = errorCode;
1286
- err[kStatusCode] = statusCode;
1287
- return err;
1288
- }
1289
- }
1290
- module.exports = Receiver;
1291
- },
1292
- 814: (module, __unused_webpack_exports, __nccwpck_require__) => {
1293
- "use strict";
1294
- const { Duplex } = __nccwpck_require__(203);
1295
- const { randomFillSync } = __nccwpck_require__(982);
1296
- const PerMessageDeflate = __nccwpck_require__(279);
1297
- const { EMPTY_BUFFER, kWebSocket, NOOP } = __nccwpck_require__(146);
1298
- const { isBlob, isValidStatusCode } = __nccwpck_require__(788);
1299
- const { mask: applyMask, toBuffer } = __nccwpck_require__(398);
1300
- const kByteLength = Symbol("kByteLength");
1301
- const maskBuffer = Buffer.alloc(4);
1302
- const RANDOM_POOL_SIZE = 8 * 1024;
1303
- let randomPool;
1304
- let randomPoolPointer = RANDOM_POOL_SIZE;
1305
- const DEFAULT = 0;
1306
- const DEFLATING = 1;
1307
- const GET_BLOB_DATA = 2;
1308
- class Sender {
1309
- constructor(socket, extensions, generateMask) {
1310
- this._extensions = extensions || {};
1311
- if (generateMask) {
1312
- this._generateMask = generateMask;
1313
- this._maskBuffer = Buffer.alloc(4);
1314
- }
1315
- this._socket = socket;
1316
- this._firstFragment = true;
1317
- this._compress = false;
1318
- this._bufferedBytes = 0;
1319
- this._queue = [];
1320
- this._state = DEFAULT;
1321
- this.onerror = NOOP;
1322
- this[kWebSocket] = undefined;
1323
- }
1324
- static frame(data, options) {
1325
- let mask;
1326
- let merge = false;
1327
- let offset = 2;
1328
- let skipMasking = false;
1329
- if (options.mask) {
1330
- mask = options.maskBuffer || maskBuffer;
1331
- if (options.generateMask) {
1332
- options.generateMask(mask);
1333
- } else {
1334
- if (randomPoolPointer === RANDOM_POOL_SIZE) {
1335
- if (randomPool === undefined) {
1336
- randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
1337
- }
1338
- randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
1339
- randomPoolPointer = 0;
1340
- }
1341
- mask[0] = randomPool[randomPoolPointer++];
1342
- mask[1] = randomPool[randomPoolPointer++];
1343
- mask[2] = randomPool[randomPoolPointer++];
1344
- mask[3] = randomPool[randomPoolPointer++];
1345
- }
1346
- skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
1347
- offset = 6;
1348
- }
1349
- let dataLength;
1350
- if (typeof data === "string") {
1351
- if (
1352
- (!options.mask || skipMasking) &&
1353
- options[kByteLength] !== undefined
1354
- ) {
1355
- dataLength = options[kByteLength];
1356
- } else {
1357
- data = Buffer.from(data);
1358
- dataLength = data.length;
1359
- }
1360
- } else {
1361
- dataLength = data.length;
1362
- merge = options.mask && options.readOnly && !skipMasking;
1363
- }
1364
- let payloadLength = dataLength;
1365
- if (dataLength >= 65536) {
1366
- offset += 8;
1367
- payloadLength = 127;
1368
- } else if (dataLength > 125) {
1369
- offset += 2;
1370
- payloadLength = 126;
1371
- }
1372
- const target = Buffer.allocUnsafe(
1373
- merge ? dataLength + offset : offset,
1374
- );
1375
- target[0] = options.fin ? options.opcode | 128 : options.opcode;
1376
- if (options.rsv1) target[0] |= 64;
1377
- target[1] = payloadLength;
1378
- if (payloadLength === 126) {
1379
- target.writeUInt16BE(dataLength, 2);
1380
- } else if (payloadLength === 127) {
1381
- target[2] = target[3] = 0;
1382
- target.writeUIntBE(dataLength, 4, 6);
1383
- }
1384
- if (!options.mask) return [target, data];
1385
- target[1] |= 128;
1386
- target[offset - 4] = mask[0];
1387
- target[offset - 3] = mask[1];
1388
- target[offset - 2] = mask[2];
1389
- target[offset - 1] = mask[3];
1390
- if (skipMasking) return [target, data];
1391
- if (merge) {
1392
- applyMask(data, mask, target, offset, dataLength);
1393
- return [target];
1394
- }
1395
- applyMask(data, mask, data, 0, dataLength);
1396
- return [target, data];
1397
- }
1398
- close(code, data, mask, cb) {
1399
- let buf;
1400
- if (code === undefined) {
1401
- buf = EMPTY_BUFFER;
1402
- } else if (typeof code !== "number" || !isValidStatusCode(code)) {
1403
- throw new TypeError(
1404
- "First argument must be a valid error code number",
1405
- );
1406
- } else if (data === undefined || !data.length) {
1407
- buf = Buffer.allocUnsafe(2);
1408
- buf.writeUInt16BE(code, 0);
1409
- } else {
1410
- const length = Buffer.byteLength(data);
1411
- if (length > 123) {
1412
- throw new RangeError(
1413
- "The message must not be greater than 123 bytes",
1414
- );
1415
- }
1416
- buf = Buffer.allocUnsafe(2 + length);
1417
- buf.writeUInt16BE(code, 0);
1418
- if (typeof data === "string") {
1419
- buf.write(data, 2);
1420
- } else {
1421
- buf.set(data, 2);
1422
- }
1423
- }
1424
- const options = {
1425
- [kByteLength]: buf.length,
1426
- fin: true,
1427
- generateMask: this._generateMask,
1428
- mask,
1429
- maskBuffer: this._maskBuffer,
1430
- opcode: 8,
1431
- readOnly: false,
1432
- rsv1: false,
1433
- };
1434
- if (this._state !== DEFAULT) {
1435
- this.enqueue([this.dispatch, buf, false, options, cb]);
1436
- } else {
1437
- this.sendFrame(Sender.frame(buf, options), cb);
1438
- }
1439
- }
1440
- ping(data, mask, cb) {
1441
- let byteLength;
1442
- let readOnly;
1443
- if (typeof data === "string") {
1444
- byteLength = Buffer.byteLength(data);
1445
- readOnly = false;
1446
- } else if (isBlob(data)) {
1447
- byteLength = data.size;
1448
- readOnly = false;
1449
- } else {
1450
- data = toBuffer(data);
1451
- byteLength = data.length;
1452
- readOnly = toBuffer.readOnly;
1453
- }
1454
- if (byteLength > 125) {
1455
- throw new RangeError(
1456
- "The data size must not be greater than 125 bytes",
1457
- );
1458
- }
1459
- const options = {
1460
- [kByteLength]: byteLength,
1461
- fin: true,
1462
- generateMask: this._generateMask,
1463
- mask,
1464
- maskBuffer: this._maskBuffer,
1465
- opcode: 9,
1466
- readOnly,
1467
- rsv1: false,
1468
- };
1469
- if (isBlob(data)) {
1470
- if (this._state !== DEFAULT) {
1471
- this.enqueue([this.getBlobData, data, false, options, cb]);
1472
- } else {
1473
- this.getBlobData(data, false, options, cb);
1474
- }
1475
- } else if (this._state !== DEFAULT) {
1476
- this.enqueue([this.dispatch, data, false, options, cb]);
1477
- } else {
1478
- this.sendFrame(Sender.frame(data, options), cb);
1479
- }
1480
- }
1481
- pong(data, mask, cb) {
1482
- let byteLength;
1483
- let readOnly;
1484
- if (typeof data === "string") {
1485
- byteLength = Buffer.byteLength(data);
1486
- readOnly = false;
1487
- } else if (isBlob(data)) {
1488
- byteLength = data.size;
1489
- readOnly = false;
1490
- } else {
1491
- data = toBuffer(data);
1492
- byteLength = data.length;
1493
- readOnly = toBuffer.readOnly;
1494
- }
1495
- if (byteLength > 125) {
1496
- throw new RangeError(
1497
- "The data size must not be greater than 125 bytes",
1498
- );
1499
- }
1500
- const options = {
1501
- [kByteLength]: byteLength,
1502
- fin: true,
1503
- generateMask: this._generateMask,
1504
- mask,
1505
- maskBuffer: this._maskBuffer,
1506
- opcode: 10,
1507
- readOnly,
1508
- rsv1: false,
1509
- };
1510
- if (isBlob(data)) {
1511
- if (this._state !== DEFAULT) {
1512
- this.enqueue([this.getBlobData, data, false, options, cb]);
1513
- } else {
1514
- this.getBlobData(data, false, options, cb);
1515
- }
1516
- } else if (this._state !== DEFAULT) {
1517
- this.enqueue([this.dispatch, data, false, options, cb]);
1518
- } else {
1519
- this.sendFrame(Sender.frame(data, options), cb);
1520
- }
1521
- }
1522
- send(data, options, cb) {
1523
- const perMessageDeflate =
1524
- this._extensions[PerMessageDeflate.extensionName];
1525
- let opcode = options.binary ? 2 : 1;
1526
- let rsv1 = options.compress;
1527
- let byteLength;
1528
- let readOnly;
1529
- if (typeof data === "string") {
1530
- byteLength = Buffer.byteLength(data);
1531
- readOnly = false;
1532
- } else if (isBlob(data)) {
1533
- byteLength = data.size;
1534
- readOnly = false;
1535
- } else {
1536
- data = toBuffer(data);
1537
- byteLength = data.length;
1538
- readOnly = toBuffer.readOnly;
1539
- }
1540
- if (this._firstFragment) {
1541
- this._firstFragment = false;
1542
- if (
1543
- rsv1 &&
1544
- perMessageDeflate &&
1545
- perMessageDeflate.params[
1546
- perMessageDeflate._isServer
1547
- ? "server_no_context_takeover"
1548
- : "client_no_context_takeover"
1549
- ]
1550
- ) {
1551
- rsv1 = byteLength >= perMessageDeflate._threshold;
1552
- }
1553
- this._compress = rsv1;
1554
- } else {
1555
- rsv1 = false;
1556
- opcode = 0;
1557
- }
1558
- if (options.fin) this._firstFragment = true;
1559
- const opts = {
1560
- [kByteLength]: byteLength,
1561
- fin: options.fin,
1562
- generateMask: this._generateMask,
1563
- mask: options.mask,
1564
- maskBuffer: this._maskBuffer,
1565
- opcode,
1566
- readOnly,
1567
- rsv1,
1568
- };
1569
- if (isBlob(data)) {
1570
- if (this._state !== DEFAULT) {
1571
- this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
1572
- } else {
1573
- this.getBlobData(data, this._compress, opts, cb);
1574
- }
1575
- } else if (this._state !== DEFAULT) {
1576
- this.enqueue([this.dispatch, data, this._compress, opts, cb]);
1577
- } else {
1578
- this.dispatch(data, this._compress, opts, cb);
1579
- }
1580
- }
1581
- getBlobData(blob, compress, options, cb) {
1582
- this._bufferedBytes += options[kByteLength];
1583
- this._state = GET_BLOB_DATA;
1584
- blob
1585
- .arrayBuffer()
1586
- .then((arrayBuffer) => {
1587
- if (this._socket.destroyed) {
1588
- const err = new Error(
1589
- "The socket was closed while the blob was being read",
1590
- );
1591
- process.nextTick(callCallbacks, this, err, cb);
1592
- return;
1593
- }
1594
- this._bufferedBytes -= options[kByteLength];
1595
- const data = toBuffer(arrayBuffer);
1596
- if (!compress) {
1597
- this._state = DEFAULT;
1598
- this.sendFrame(Sender.frame(data, options), cb);
1599
- this.dequeue();
1600
- } else {
1601
- this.dispatch(data, compress, options, cb);
1602
- }
1603
- })
1604
- .catch((err) => {
1605
- process.nextTick(onError, this, err, cb);
1606
- });
1607
- }
1608
- dispatch(data, compress, options, cb) {
1609
- if (!compress) {
1610
- this.sendFrame(Sender.frame(data, options), cb);
1611
- return;
1612
- }
1613
- const perMessageDeflate =
1614
- this._extensions[PerMessageDeflate.extensionName];
1615
- this._bufferedBytes += options[kByteLength];
1616
- this._state = DEFLATING;
1617
- perMessageDeflate.compress(data, options.fin, (_, buf) => {
1618
- if (this._socket.destroyed) {
1619
- const err = new Error(
1620
- "The socket was closed while data was being compressed",
1621
- );
1622
- callCallbacks(this, err, cb);
1623
- return;
1624
- }
1625
- this._bufferedBytes -= options[kByteLength];
1626
- this._state = DEFAULT;
1627
- options.readOnly = false;
1628
- this.sendFrame(Sender.frame(buf, options), cb);
1629
- this.dequeue();
1630
- });
1631
- }
1632
- dequeue() {
1633
- while (this._state === DEFAULT && this._queue.length) {
1634
- const params = this._queue.shift();
1635
- this._bufferedBytes -= params[3][kByteLength];
1636
- Reflect.apply(params[0], this, params.slice(1));
1637
- }
1638
- }
1639
- enqueue(params) {
1640
- this._bufferedBytes += params[3][kByteLength];
1641
- this._queue.push(params);
1642
- }
1643
- sendFrame(list, cb) {
1644
- if (list.length === 2) {
1645
- this._socket.cork();
1646
- this._socket.write(list[0]);
1647
- this._socket.write(list[1], cb);
1648
- this._socket.uncork();
1649
- } else {
1650
- this._socket.write(list[0], cb);
1651
- }
1652
- }
1653
- }
1654
- module.exports = Sender;
1655
- function callCallbacks(sender, err, cb) {
1656
- if (typeof cb === "function") cb(err);
1657
- for (let i = 0; i < sender._queue.length; i++) {
1658
- const params = sender._queue[i];
1659
- const callback = params[params.length - 1];
1660
- if (typeof callback === "function") callback(err);
1661
- }
1662
- }
1663
- function onError(sender, err, cb) {
1664
- callCallbacks(sender, err, cb);
1665
- sender.onerror(err);
1666
- }
1667
- },
1668
- 283: (module, __unused_webpack_exports, __nccwpck_require__) => {
1669
- "use strict";
1670
- const WebSocket = __nccwpck_require__(640);
1671
- const { Duplex } = __nccwpck_require__(203);
1672
- function emitClose(stream) {
1673
- stream.emit("close");
1674
- }
1675
- function duplexOnEnd() {
1676
- if (!this.destroyed && this._writableState.finished) {
1677
- this.destroy();
1678
- }
1679
- }
1680
- function duplexOnError(err) {
1681
- this.removeListener("error", duplexOnError);
1682
- this.destroy();
1683
- if (this.listenerCount("error") === 0) {
1684
- this.emit("error", err);
1685
- }
1686
- }
1687
- function createWebSocketStream(ws, options) {
1688
- let terminateOnDestroy = true;
1689
- const duplex = new Duplex({
1690
- ...options,
1691
- autoDestroy: false,
1692
- emitClose: false,
1693
- objectMode: false,
1694
- writableObjectMode: false,
1695
- });
1696
- ws.on("message", function message(msg, isBinary) {
1697
- const data =
1698
- !isBinary && duplex._readableState.objectMode
1699
- ? msg.toString()
1700
- : msg;
1701
- if (!duplex.push(data)) ws.pause();
1702
- });
1703
- ws.once("error", function error(err) {
1704
- if (duplex.destroyed) return;
1705
- terminateOnDestroy = false;
1706
- duplex.destroy(err);
1707
- });
1708
- ws.once("close", function close() {
1709
- if (duplex.destroyed) return;
1710
- duplex.push(null);
1711
- });
1712
- duplex._destroy = function (err, callback) {
1713
- if (ws.readyState === ws.CLOSED) {
1714
- callback(err);
1715
- process.nextTick(emitClose, duplex);
1716
- return;
1717
- }
1718
- let called = false;
1719
- ws.once("error", function error(err) {
1720
- called = true;
1721
- callback(err);
1722
- });
1723
- ws.once("close", function close() {
1724
- if (!called) callback(err);
1725
- process.nextTick(emitClose, duplex);
1726
- });
1727
- if (terminateOnDestroy) ws.terminate();
1728
- };
1729
- duplex._final = function (callback) {
1730
- if (ws.readyState === ws.CONNECTING) {
1731
- ws.once("open", function open() {
1732
- duplex._final(callback);
1733
- });
1734
- return;
1735
- }
1736
- if (ws._socket === null) return;
1737
- if (ws._socket._writableState.finished) {
1738
- callback();
1739
- if (duplex._readableState.endEmitted) duplex.destroy();
1740
- } else {
1741
- ws._socket.once("finish", function finish() {
1742
- callback();
1743
- });
1744
- ws.close();
1745
- }
1746
- };
1747
- duplex._read = function () {
1748
- if (ws.isPaused) ws.resume();
1749
- };
1750
- duplex._write = function (chunk, encoding, callback) {
1751
- if (ws.readyState === ws.CONNECTING) {
1752
- ws.once("open", function open() {
1753
- duplex._write(chunk, encoding, callback);
1754
- });
1755
- return;
1756
- }
1757
- ws.send(chunk, callback);
1758
- };
1759
- duplex.on("end", duplexOnEnd);
1760
- duplex.on("error", duplexOnError);
1761
- return duplex;
1762
- }
1763
- module.exports = createWebSocketStream;
1764
- },
1765
- 417: (module, __unused_webpack_exports, __nccwpck_require__) => {
1766
- "use strict";
1767
- const { tokenChars } = __nccwpck_require__(788);
1768
- function parse(header) {
1769
- const protocols = new Set();
1770
- let start = -1;
1771
- let end = -1;
1772
- let i = 0;
1773
- for (i; i < header.length; i++) {
1774
- const code = header.charCodeAt(i);
1775
- if (end === -1 && tokenChars[code] === 1) {
1776
- if (start === -1) start = i;
1777
- } else if (i !== 0 && (code === 32 || code === 9)) {
1778
- if (end === -1 && start !== -1) end = i;
1779
- } else if (code === 44) {
1780
- if (start === -1) {
1781
- throw new SyntaxError(`Unexpected character at index ${i}`);
1782
- }
1783
- if (end === -1) end = i;
1784
- const protocol = header.slice(start, end);
1785
- if (protocols.has(protocol)) {
1786
- throw new SyntaxError(
1787
- `The "${protocol}" subprotocol is duplicated`,
1788
- );
1789
- }
1790
- protocols.add(protocol);
1791
- start = end = -1;
1792
- } else {
1793
- throw new SyntaxError(`Unexpected character at index ${i}`);
1794
- }
1795
- }
1796
- if (start === -1 || end !== -1) {
1797
- throw new SyntaxError("Unexpected end of input");
1798
- }
1799
- const protocol = header.slice(start, i);
1800
- if (protocols.has(protocol)) {
1801
- throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
1802
- }
1803
- protocols.add(protocol);
1804
- return protocols;
1805
- }
1806
- module.exports = { parse };
1807
- },
1808
- 788: (module, __unused_webpack_exports, __nccwpck_require__) => {
1809
- "use strict";
1810
- const { isUtf8 } = __nccwpck_require__(181);
1811
- const { hasBlob } = __nccwpck_require__(146);
1812
- const tokenChars = [
1813
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1814
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0,
1815
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1816
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
1817
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1818
- 1, 1, 1, 0, 1, 0, 1, 0,
1819
- ];
1820
- function isValidStatusCode(code) {
1821
- return (
1822
- (code >= 1e3 &&
1823
- code <= 1014 &&
1824
- code !== 1004 &&
1825
- code !== 1005 &&
1826
- code !== 1006) ||
1827
- (code >= 3e3 && code <= 4999)
1828
- );
1829
- }
1830
- function _isValidUTF8(buf) {
1831
- const len = buf.length;
1832
- let i = 0;
1833
- while (i < len) {
1834
- if ((buf[i] & 128) === 0) {
1835
- i++;
1836
- } else if ((buf[i] & 224) === 192) {
1837
- if (
1838
- i + 1 === len ||
1839
- (buf[i + 1] & 192) !== 128 ||
1840
- (buf[i] & 254) === 192
1841
- ) {
1842
- return false;
1843
- }
1844
- i += 2;
1845
- } else if ((buf[i] & 240) === 224) {
1846
- if (
1847
- i + 2 >= len ||
1848
- (buf[i + 1] & 192) !== 128 ||
1849
- (buf[i + 2] & 192) !== 128 ||
1850
- (buf[i] === 224 && (buf[i + 1] & 224) === 128) ||
1851
- (buf[i] === 237 && (buf[i + 1] & 224) === 160)
1852
- ) {
1853
- return false;
1854
- }
1855
- i += 3;
1856
- } else if ((buf[i] & 248) === 240) {
1857
- if (
1858
- i + 3 >= len ||
1859
- (buf[i + 1] & 192) !== 128 ||
1860
- (buf[i + 2] & 192) !== 128 ||
1861
- (buf[i + 3] & 192) !== 128 ||
1862
- (buf[i] === 240 && (buf[i + 1] & 240) === 128) ||
1863
- (buf[i] === 244 && buf[i + 1] > 143) ||
1864
- buf[i] > 244
1865
- ) {
1866
- return false;
1867
- }
1868
- i += 4;
1869
- } else {
1870
- return false;
1871
- }
1872
- }
1873
- return true;
1874
- }
1875
- function isBlob(value) {
1876
- return (
1877
- hasBlob &&
1878
- typeof value === "object" &&
1879
- typeof value.arrayBuffer === "function" &&
1880
- typeof value.type === "string" &&
1881
- typeof value.stream === "function" &&
1882
- (value[Symbol.toStringTag] === "Blob" ||
1883
- value[Symbol.toStringTag] === "File")
1884
- );
1885
- }
1886
- module.exports = {
1887
- isBlob,
1888
- isValidStatusCode,
1889
- isValidUTF8: _isValidUTF8,
1890
- tokenChars,
1891
- };
1892
- if (isUtf8) {
1893
- module.exports.isValidUTF8 = function (buf) {
1894
- return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
1895
- };
1896
- } else if (!process.env.WS_NO_UTF_8_VALIDATE) {
1897
- try {
1898
- const isValidUTF8 = __nccwpck_require__(710);
1899
- module.exports.isValidUTF8 = function (buf) {
1900
- return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
1901
- };
1902
- } catch (e) {}
1903
- }
1904
- },
1905
- 686: (module, __unused_webpack_exports, __nccwpck_require__) => {
1906
- "use strict";
1907
- const EventEmitter = __nccwpck_require__(434);
1908
- const http = __nccwpck_require__(611);
1909
- const { Duplex } = __nccwpck_require__(203);
1910
- const { createHash } = __nccwpck_require__(982);
1911
- const extension = __nccwpck_require__(994);
1912
- const PerMessageDeflate = __nccwpck_require__(279);
1913
- const subprotocol = __nccwpck_require__(417);
1914
- const WebSocket = __nccwpck_require__(640);
1915
- const { CLOSE_TIMEOUT, GUID, kWebSocket } = __nccwpck_require__(146);
1916
- const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
1917
- const RUNNING = 0;
1918
- const CLOSING = 1;
1919
- const CLOSED = 2;
1920
- class WebSocketServer extends EventEmitter {
1921
- constructor(options, callback) {
1922
- super();
1923
- options = {
1924
- allowSynchronousEvents: true,
1925
- autoPong: true,
1926
- maxPayload: 100 * 1024 * 1024,
1927
- skipUTF8Validation: false,
1928
- perMessageDeflate: false,
1929
- handleProtocols: null,
1930
- clientTracking: true,
1931
- closeTimeout: CLOSE_TIMEOUT,
1932
- verifyClient: null,
1933
- noServer: false,
1934
- backlog: null,
1935
- server: null,
1936
- host: null,
1937
- path: null,
1938
- port: null,
1939
- WebSocket,
1940
- ...options,
1941
- };
1942
- if (
1943
- (options.port == null && !options.server && !options.noServer) ||
1944
- (options.port != null && (options.server || options.noServer)) ||
1945
- (options.server && options.noServer)
1946
- ) {
1947
- throw new TypeError(
1948
- 'One and only one of the "port", "server", or "noServer" options ' +
1949
- "must be specified",
1950
- );
1951
- }
1952
- if (options.port != null) {
1953
- this._server = http.createServer((req, res) => {
1954
- const body = http.STATUS_CODES[426];
1955
- res.writeHead(426, {
1956
- "Content-Length": body.length,
1957
- "Content-Type": "text/plain",
1958
- });
1959
- res.end(body);
1960
- });
1961
- this._server.listen(
1962
- options.port,
1963
- options.host,
1964
- options.backlog,
1965
- callback,
1966
- );
1967
- } else if (options.server) {
1968
- this._server = options.server;
1969
- }
1970
- if (this._server) {
1971
- const emitConnection = this.emit.bind(this, "connection");
1972
- this._removeListeners = addListeners(this._server, {
1973
- listening: this.emit.bind(this, "listening"),
1974
- error: this.emit.bind(this, "error"),
1975
- upgrade: (req, socket, head) => {
1976
- this.handleUpgrade(req, socket, head, emitConnection);
1977
- },
1978
- });
1979
- }
1980
- if (options.perMessageDeflate === true)
1981
- options.perMessageDeflate = {};
1982
- if (options.clientTracking) {
1983
- this.clients = new Set();
1984
- this._shouldEmitClose = false;
1985
- }
1986
- this.options = options;
1987
- this._state = RUNNING;
1988
- }
1989
- address() {
1990
- if (this.options.noServer) {
1991
- throw new Error('The server is operating in "noServer" mode');
1992
- }
1993
- if (!this._server) return null;
1994
- return this._server.address();
1995
- }
1996
- close(cb) {
1997
- if (this._state === CLOSED) {
1998
- if (cb) {
1999
- this.once("close", () => {
2000
- cb(new Error("The server is not running"));
2001
- });
2002
- }
2003
- process.nextTick(emitClose, this);
2004
- return;
2005
- }
2006
- if (cb) this.once("close", cb);
2007
- if (this._state === CLOSING) return;
2008
- this._state = CLOSING;
2009
- if (this.options.noServer || this.options.server) {
2010
- if (this._server) {
2011
- this._removeListeners();
2012
- this._removeListeners = this._server = null;
2013
- }
2014
- if (this.clients) {
2015
- if (!this.clients.size) {
2016
- process.nextTick(emitClose, this);
2017
- } else {
2018
- this._shouldEmitClose = true;
2019
- }
2020
- } else {
2021
- process.nextTick(emitClose, this);
2022
- }
2023
- } else {
2024
- const server = this._server;
2025
- this._removeListeners();
2026
- this._removeListeners = this._server = null;
2027
- server.close(() => {
2028
- emitClose(this);
2029
- });
2030
- }
2031
- }
2032
- shouldHandle(req) {
2033
- if (this.options.path) {
2034
- const index = req.url.indexOf("?");
2035
- const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
2036
- if (pathname !== this.options.path) return false;
2037
- }
2038
- return true;
2039
- }
2040
- handleUpgrade(req, socket, head, cb) {
2041
- socket.on("error", socketOnError);
2042
- const key = req.headers["sec-websocket-key"];
2043
- const upgrade = req.headers.upgrade;
2044
- const version = +req.headers["sec-websocket-version"];
2045
- if (req.method !== "GET") {
2046
- const message = "Invalid HTTP method";
2047
- abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
2048
- return;
2049
- }
2050
- if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
2051
- const message = "Invalid Upgrade header";
2052
- abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
2053
- return;
2054
- }
2055
- if (key === undefined || !keyRegex.test(key)) {
2056
- const message = "Missing or invalid Sec-WebSocket-Key header";
2057
- abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
2058
- return;
2059
- }
2060
- if (version !== 13 && version !== 8) {
2061
- const message = "Missing or invalid Sec-WebSocket-Version header";
2062
- abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
2063
- "Sec-WebSocket-Version": "13, 8",
2064
- });
2065
- return;
2066
- }
2067
- if (!this.shouldHandle(req)) {
2068
- abortHandshake(socket, 400);
2069
- return;
2070
- }
2071
- const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
2072
- let protocols = new Set();
2073
- if (secWebSocketProtocol !== undefined) {
2074
- try {
2075
- protocols = subprotocol.parse(secWebSocketProtocol);
2076
- } catch (err) {
2077
- const message = "Invalid Sec-WebSocket-Protocol header";
2078
- abortHandshakeOrEmitwsClientError(
2079
- this,
2080
- req,
2081
- socket,
2082
- 400,
2083
- message,
2084
- );
2085
- return;
2086
- }
2087
- }
2088
- const secWebSocketExtensions =
2089
- req.headers["sec-websocket-extensions"];
2090
- const extensions = {};
2091
- if (
2092
- this.options.perMessageDeflate &&
2093
- secWebSocketExtensions !== undefined
2094
- ) {
2095
- const perMessageDeflate = new PerMessageDeflate(
2096
- this.options.perMessageDeflate,
2097
- true,
2098
- this.options.maxPayload,
2099
- );
2100
- try {
2101
- const offers = extension.parse(secWebSocketExtensions);
2102
- if (offers[PerMessageDeflate.extensionName]) {
2103
- perMessageDeflate.accept(
2104
- offers[PerMessageDeflate.extensionName],
2105
- );
2106
- extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
2107
- }
2108
- } catch (err) {
2109
- const message =
2110
- "Invalid or unacceptable Sec-WebSocket-Extensions header";
2111
- abortHandshakeOrEmitwsClientError(
2112
- this,
2113
- req,
2114
- socket,
2115
- 400,
2116
- message,
2117
- );
2118
- return;
2119
- }
2120
- }
2121
- if (this.options.verifyClient) {
2122
- const info = {
2123
- origin:
2124
- req.headers[
2125
- `${version === 8 ? "sec-websocket-origin" : "origin"}`
2126
- ],
2127
- secure: !!(req.socket.authorized || req.socket.encrypted),
2128
- req,
2129
- };
2130
- if (this.options.verifyClient.length === 2) {
2131
- this.options.verifyClient(
2132
- info,
2133
- (verified, code, message, headers) => {
2134
- if (!verified) {
2135
- return abortHandshake(
2136
- socket,
2137
- code || 401,
2138
- message,
2139
- headers,
2140
- );
2141
- }
2142
- this.completeUpgrade(
2143
- extensions,
2144
- key,
2145
- protocols,
2146
- req,
2147
- socket,
2148
- head,
2149
- cb,
2150
- );
2151
- },
2152
- );
2153
- return;
2154
- }
2155
- if (!this.options.verifyClient(info))
2156
- return abortHandshake(socket, 401);
2157
- }
2158
- this.completeUpgrade(
2159
- extensions,
2160
- key,
2161
- protocols,
2162
- req,
2163
- socket,
2164
- head,
2165
- cb,
2166
- );
2167
- }
2168
- completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
2169
- if (!socket.readable || !socket.writable) return socket.destroy();
2170
- if (socket[kWebSocket]) {
2171
- throw new Error(
2172
- "server.handleUpgrade() was called more than once with the same " +
2173
- "socket, possibly due to a misconfiguration",
2174
- );
2175
- }
2176
- if (this._state > RUNNING) return abortHandshake(socket, 503);
2177
- const digest = createHash("sha1")
2178
- .update(key + GUID)
2179
- .digest("base64");
2180
- const headers = [
2181
- "HTTP/1.1 101 Switching Protocols",
2182
- "Upgrade: websocket",
2183
- "Connection: Upgrade",
2184
- `Sec-WebSocket-Accept: ${digest}`,
2185
- ];
2186
- const ws = new this.options.WebSocket(null, undefined, this.options);
2187
- if (protocols.size) {
2188
- const protocol = this.options.handleProtocols
2189
- ? this.options.handleProtocols(protocols, req)
2190
- : protocols.values().next().value;
2191
- if (protocol) {
2192
- headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
2193
- ws._protocol = protocol;
2194
- }
2195
- }
2196
- if (extensions[PerMessageDeflate.extensionName]) {
2197
- const params = extensions[PerMessageDeflate.extensionName].params;
2198
- const value = extension.format({
2199
- [PerMessageDeflate.extensionName]: [params],
2200
- });
2201
- headers.push(`Sec-WebSocket-Extensions: ${value}`);
2202
- ws._extensions = extensions;
2203
- }
2204
- this.emit("headers", headers, req);
2205
- socket.write(headers.concat("\r\n").join("\r\n"));
2206
- socket.removeListener("error", socketOnError);
2207
- ws.setSocket(socket, head, {
2208
- allowSynchronousEvents: this.options.allowSynchronousEvents,
2209
- maxPayload: this.options.maxPayload,
2210
- skipUTF8Validation: this.options.skipUTF8Validation,
2211
- });
2212
- if (this.clients) {
2213
- this.clients.add(ws);
2214
- ws.on("close", () => {
2215
- this.clients.delete(ws);
2216
- if (this._shouldEmitClose && !this.clients.size) {
2217
- process.nextTick(emitClose, this);
2218
- }
2219
- });
2220
- }
2221
- cb(ws, req);
2222
- }
2223
- }
2224
- module.exports = WebSocketServer;
2225
- function addListeners(server, map) {
2226
- for (const event of Object.keys(map)) server.on(event, map[event]);
2227
- return function removeListeners() {
2228
- for (const event of Object.keys(map)) {
2229
- server.removeListener(event, map[event]);
2230
- }
2231
- };
2232
- }
2233
- function emitClose(server) {
2234
- server._state = CLOSED;
2235
- server.emit("close");
2236
- }
2237
- function socketOnError() {
2238
- this.destroy();
2239
- }
2240
- function abortHandshake(socket, code, message, headers) {
2241
- message = message || http.STATUS_CODES[code];
2242
- headers = {
2243
- Connection: "close",
2244
- "Content-Type": "text/html",
2245
- "Content-Length": Buffer.byteLength(message),
2246
- ...headers,
2247
- };
2248
- socket.once("finish", socket.destroy);
2249
- socket.end(
2250
- `HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
2251
- Object.keys(headers)
2252
- .map((h) => `${h}: ${headers[h]}`)
2253
- .join("\r\n") +
2254
- "\r\n\r\n" +
2255
- message,
2256
- );
2257
- }
2258
- function abortHandshakeOrEmitwsClientError(
2259
- server,
2260
- req,
2261
- socket,
2262
- code,
2263
- message,
2264
- headers,
2265
- ) {
2266
- if (server.listenerCount("wsClientError")) {
2267
- const err = new Error(message);
2268
- Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
2269
- server.emit("wsClientError", err, socket, req);
2270
- } else {
2271
- abortHandshake(socket, code, message, headers);
2272
- }
2273
- }
2274
- },
2275
- 640: (module, __unused_webpack_exports, __nccwpck_require__) => {
2276
- "use strict";
2277
- const EventEmitter = __nccwpck_require__(434);
2278
- const https = __nccwpck_require__(692);
2279
- const http = __nccwpck_require__(611);
2280
- const net = __nccwpck_require__(278);
2281
- const tls = __nccwpck_require__(756);
2282
- const { randomBytes, createHash } = __nccwpck_require__(982);
2283
- const { Duplex, Readable } = __nccwpck_require__(203);
2284
- const { URL } = __nccwpck_require__(16);
2285
- const PerMessageDeflate = __nccwpck_require__(279);
2286
- const Receiver = __nccwpck_require__(522);
2287
- const Sender = __nccwpck_require__(814);
2288
- const { isBlob } = __nccwpck_require__(788);
2289
- const {
2290
- BINARY_TYPES,
2291
- CLOSE_TIMEOUT,
2292
- EMPTY_BUFFER,
2293
- GUID,
2294
- kForOnEventAttribute,
2295
- kListener,
2296
- kStatusCode,
2297
- kWebSocket,
2298
- NOOP,
2299
- } = __nccwpck_require__(146);
2300
- const {
2301
- EventTarget: { addEventListener, removeEventListener },
2302
- } = __nccwpck_require__(233);
2303
- const { format, parse } = __nccwpck_require__(994);
2304
- const { toBuffer } = __nccwpck_require__(398);
2305
- const kAborted = Symbol("kAborted");
2306
- const protocolVersions = [8, 13];
2307
- const readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
2308
- const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
2309
- class WebSocket extends EventEmitter {
2310
- constructor(address, protocols, options) {
2311
- super();
2312
- this._binaryType = BINARY_TYPES[0];
2313
- this._closeCode = 1006;
2314
- this._closeFrameReceived = false;
2315
- this._closeFrameSent = false;
2316
- this._closeMessage = EMPTY_BUFFER;
2317
- this._closeTimer = null;
2318
- this._errorEmitted = false;
2319
- this._extensions = {};
2320
- this._paused = false;
2321
- this._protocol = "";
2322
- this._readyState = WebSocket.CONNECTING;
2323
- this._receiver = null;
2324
- this._sender = null;
2325
- this._socket = null;
2326
- if (address !== null) {
2327
- this._bufferedAmount = 0;
2328
- this._isServer = false;
2329
- this._redirects = 0;
2330
- if (protocols === undefined) {
2331
- protocols = [];
2332
- } else if (!Array.isArray(protocols)) {
2333
- if (typeof protocols === "object" && protocols !== null) {
2334
- options = protocols;
2335
- protocols = [];
2336
- } else {
2337
- protocols = [protocols];
2338
- }
2339
- }
2340
- initAsClient(this, address, protocols, options);
2341
- } else {
2342
- this._autoPong = options.autoPong;
2343
- this._closeTimeout = options.closeTimeout;
2344
- this._isServer = true;
2345
- }
2346
- }
2347
- get binaryType() {
2348
- return this._binaryType;
2349
- }
2350
- set binaryType(type) {
2351
- if (!BINARY_TYPES.includes(type)) return;
2352
- this._binaryType = type;
2353
- if (this._receiver) this._receiver._binaryType = type;
2354
- }
2355
- get bufferedAmount() {
2356
- if (!this._socket) return this._bufferedAmount;
2357
- return (
2358
- this._socket._writableState.length + this._sender._bufferedBytes
2359
- );
2360
- }
2361
- get extensions() {
2362
- return Object.keys(this._extensions).join();
2363
- }
2364
- get isPaused() {
2365
- return this._paused;
2366
- }
2367
- get onclose() {
2368
- return null;
2369
- }
2370
- get onerror() {
2371
- return null;
2372
- }
2373
- get onopen() {
2374
- return null;
2375
- }
2376
- get onmessage() {
2377
- return null;
2378
- }
2379
- get protocol() {
2380
- return this._protocol;
2381
- }
2382
- get readyState() {
2383
- return this._readyState;
2384
- }
2385
- get url() {
2386
- return this._url;
2387
- }
2388
- setSocket(socket, head, options) {
2389
- const receiver = new Receiver({
2390
- allowSynchronousEvents: options.allowSynchronousEvents,
2391
- binaryType: this.binaryType,
2392
- extensions: this._extensions,
2393
- isServer: this._isServer,
2394
- maxPayload: options.maxPayload,
2395
- skipUTF8Validation: options.skipUTF8Validation,
2396
- });
2397
- const sender = new Sender(
2398
- socket,
2399
- this._extensions,
2400
- options.generateMask,
2401
- );
2402
- this._receiver = receiver;
2403
- this._sender = sender;
2404
- this._socket = socket;
2405
- receiver[kWebSocket] = this;
2406
- sender[kWebSocket] = this;
2407
- socket[kWebSocket] = this;
2408
- receiver.on("conclude", receiverOnConclude);
2409
- receiver.on("drain", receiverOnDrain);
2410
- receiver.on("error", receiverOnError);
2411
- receiver.on("message", receiverOnMessage);
2412
- receiver.on("ping", receiverOnPing);
2413
- receiver.on("pong", receiverOnPong);
2414
- sender.onerror = senderOnError;
2415
- if (socket.setTimeout) socket.setTimeout(0);
2416
- if (socket.setNoDelay) socket.setNoDelay();
2417
- if (head.length > 0) socket.unshift(head);
2418
- socket.on("close", socketOnClose);
2419
- socket.on("data", socketOnData);
2420
- socket.on("end", socketOnEnd);
2421
- socket.on("error", socketOnError);
2422
- this._readyState = WebSocket.OPEN;
2423
- this.emit("open");
2424
- }
2425
- emitClose() {
2426
- if (!this._socket) {
2427
- this._readyState = WebSocket.CLOSED;
2428
- this.emit("close", this._closeCode, this._closeMessage);
2429
- return;
2430
- }
2431
- if (this._extensions[PerMessageDeflate.extensionName]) {
2432
- this._extensions[PerMessageDeflate.extensionName].cleanup();
2433
- }
2434
- this._receiver.removeAllListeners();
2435
- this._readyState = WebSocket.CLOSED;
2436
- this.emit("close", this._closeCode, this._closeMessage);
2437
- }
2438
- close(code, data) {
2439
- if (this.readyState === WebSocket.CLOSED) return;
2440
- if (this.readyState === WebSocket.CONNECTING) {
2441
- const msg =
2442
- "WebSocket was closed before the connection was established";
2443
- abortHandshake(this, this._req, msg);
2444
- return;
2445
- }
2446
- if (this.readyState === WebSocket.CLOSING) {
2447
- if (
2448
- this._closeFrameSent &&
2449
- (this._closeFrameReceived ||
2450
- this._receiver._writableState.errorEmitted)
2451
- ) {
2452
- this._socket.end();
2453
- }
2454
- return;
2455
- }
2456
- this._readyState = WebSocket.CLOSING;
2457
- this._sender.close(code, data, !this._isServer, (err) => {
2458
- if (err) return;
2459
- this._closeFrameSent = true;
2460
- if (
2461
- this._closeFrameReceived ||
2462
- this._receiver._writableState.errorEmitted
2463
- ) {
2464
- this._socket.end();
2465
- }
2466
- });
2467
- setCloseTimer(this);
2468
- }
2469
- pause() {
2470
- if (
2471
- this.readyState === WebSocket.CONNECTING ||
2472
- this.readyState === WebSocket.CLOSED
2473
- ) {
2474
- return;
2475
- }
2476
- this._paused = true;
2477
- this._socket.pause();
2478
- }
2479
- ping(data, mask, cb) {
2480
- if (this.readyState === WebSocket.CONNECTING) {
2481
- throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
2482
- }
2483
- if (typeof data === "function") {
2484
- cb = data;
2485
- data = mask = undefined;
2486
- } else if (typeof mask === "function") {
2487
- cb = mask;
2488
- mask = undefined;
2489
- }
2490
- if (typeof data === "number") data = data.toString();
2491
- if (this.readyState !== WebSocket.OPEN) {
2492
- sendAfterClose(this, data, cb);
2493
- return;
2494
- }
2495
- if (mask === undefined) mask = !this._isServer;
2496
- this._sender.ping(data || EMPTY_BUFFER, mask, cb);
2497
- }
2498
- pong(data, mask, cb) {
2499
- if (this.readyState === WebSocket.CONNECTING) {
2500
- throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
2501
- }
2502
- if (typeof data === "function") {
2503
- cb = data;
2504
- data = mask = undefined;
2505
- } else if (typeof mask === "function") {
2506
- cb = mask;
2507
- mask = undefined;
2508
- }
2509
- if (typeof data === "number") data = data.toString();
2510
- if (this.readyState !== WebSocket.OPEN) {
2511
- sendAfterClose(this, data, cb);
2512
- return;
2513
- }
2514
- if (mask === undefined) mask = !this._isServer;
2515
- this._sender.pong(data || EMPTY_BUFFER, mask, cb);
2516
- }
2517
- resume() {
2518
- if (
2519
- this.readyState === WebSocket.CONNECTING ||
2520
- this.readyState === WebSocket.CLOSED
2521
- ) {
2522
- return;
2523
- }
2524
- this._paused = false;
2525
- if (!this._receiver._writableState.needDrain) this._socket.resume();
2526
- }
2527
- send(data, options, cb) {
2528
- if (this.readyState === WebSocket.CONNECTING) {
2529
- throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
2530
- }
2531
- if (typeof options === "function") {
2532
- cb = options;
2533
- options = {};
2534
- }
2535
- if (typeof data === "number") data = data.toString();
2536
- if (this.readyState !== WebSocket.OPEN) {
2537
- sendAfterClose(this, data, cb);
2538
- return;
2539
- }
2540
- const opts = {
2541
- binary: typeof data !== "string",
2542
- mask: !this._isServer,
2543
- compress: true,
2544
- fin: true,
2545
- ...options,
2546
- };
2547
- if (!this._extensions[PerMessageDeflate.extensionName]) {
2548
- opts.compress = false;
2549
- }
2550
- this._sender.send(data || EMPTY_BUFFER, opts, cb);
2551
- }
2552
- terminate() {
2553
- if (this.readyState === WebSocket.CLOSED) return;
2554
- if (this.readyState === WebSocket.CONNECTING) {
2555
- const msg =
2556
- "WebSocket was closed before the connection was established";
2557
- abortHandshake(this, this._req, msg);
2558
- return;
2559
- }
2560
- if (this._socket) {
2561
- this._readyState = WebSocket.CLOSING;
2562
- this._socket.destroy();
2563
- }
2564
- }
2565
- }
2566
- Object.defineProperty(WebSocket, "CONNECTING", {
2567
- enumerable: true,
2568
- value: readyStates.indexOf("CONNECTING"),
2569
- });
2570
- Object.defineProperty(WebSocket.prototype, "CONNECTING", {
2571
- enumerable: true,
2572
- value: readyStates.indexOf("CONNECTING"),
2573
- });
2574
- Object.defineProperty(WebSocket, "OPEN", {
2575
- enumerable: true,
2576
- value: readyStates.indexOf("OPEN"),
2577
- });
2578
- Object.defineProperty(WebSocket.prototype, "OPEN", {
2579
- enumerable: true,
2580
- value: readyStates.indexOf("OPEN"),
2581
- });
2582
- Object.defineProperty(WebSocket, "CLOSING", {
2583
- enumerable: true,
2584
- value: readyStates.indexOf("CLOSING"),
2585
- });
2586
- Object.defineProperty(WebSocket.prototype, "CLOSING", {
2587
- enumerable: true,
2588
- value: readyStates.indexOf("CLOSING"),
2589
- });
2590
- Object.defineProperty(WebSocket, "CLOSED", {
2591
- enumerable: true,
2592
- value: readyStates.indexOf("CLOSED"),
2593
- });
2594
- Object.defineProperty(WebSocket.prototype, "CLOSED", {
2595
- enumerable: true,
2596
- value: readyStates.indexOf("CLOSED"),
2597
- });
2598
- [
2599
- "binaryType",
2600
- "bufferedAmount",
2601
- "extensions",
2602
- "isPaused",
2603
- "protocol",
2604
- "readyState",
2605
- "url",
2606
- ].forEach((property) => {
2607
- Object.defineProperty(WebSocket.prototype, property, {
2608
- enumerable: true,
2609
- });
2610
- });
2611
- ["open", "error", "close", "message"].forEach((method) => {
2612
- Object.defineProperty(WebSocket.prototype, `on${method}`, {
2613
- enumerable: true,
2614
- get() {
2615
- for (const listener of this.listeners(method)) {
2616
- if (listener[kForOnEventAttribute]) return listener[kListener];
2617
- }
2618
- return null;
2619
- },
2620
- set(handler) {
2621
- for (const listener of this.listeners(method)) {
2622
- if (listener[kForOnEventAttribute]) {
2623
- this.removeListener(method, listener);
2624
- break;
2625
- }
2626
- }
2627
- if (typeof handler !== "function") return;
2628
- this.addEventListener(method, handler, {
2629
- [kForOnEventAttribute]: true,
2630
- });
2631
- },
2632
- });
2633
- });
2634
- WebSocket.prototype.addEventListener = addEventListener;
2635
- WebSocket.prototype.removeEventListener = removeEventListener;
2636
- module.exports = WebSocket;
2637
- function initAsClient(websocket, address, protocols, options) {
2638
- const opts = {
2639
- allowSynchronousEvents: true,
2640
- autoPong: true,
2641
- closeTimeout: CLOSE_TIMEOUT,
2642
- protocolVersion: protocolVersions[1],
2643
- maxPayload: 100 * 1024 * 1024,
2644
- skipUTF8Validation: false,
2645
- perMessageDeflate: true,
2646
- followRedirects: false,
2647
- maxRedirects: 10,
2648
- ...options,
2649
- socketPath: undefined,
2650
- hostname: undefined,
2651
- protocol: undefined,
2652
- timeout: undefined,
2653
- method: "GET",
2654
- host: undefined,
2655
- path: undefined,
2656
- port: undefined,
2657
- };
2658
- websocket._autoPong = opts.autoPong;
2659
- websocket._closeTimeout = opts.closeTimeout;
2660
- if (!protocolVersions.includes(opts.protocolVersion)) {
2661
- throw new RangeError(
2662
- `Unsupported protocol version: ${opts.protocolVersion} ` +
2663
- `(supported versions: ${protocolVersions.join(", ")})`,
2664
- );
2665
- }
2666
- let parsedUrl;
2667
- if (address instanceof URL) {
2668
- parsedUrl = address;
2669
- } else {
2670
- try {
2671
- parsedUrl = new URL(address);
2672
- } catch (e) {
2673
- throw new SyntaxError(`Invalid URL: ${address}`);
2674
- }
2675
- }
2676
- if (parsedUrl.protocol === "http:") {
2677
- parsedUrl.protocol = "ws:";
2678
- } else if (parsedUrl.protocol === "https:") {
2679
- parsedUrl.protocol = "wss:";
2680
- }
2681
- websocket._url = parsedUrl.href;
2682
- const isSecure = parsedUrl.protocol === "wss:";
2683
- const isIpcUrl = parsedUrl.protocol === "ws+unix:";
2684
- let invalidUrlMessage;
2685
- if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
2686
- invalidUrlMessage =
2687
- 'The URL\'s protocol must be one of "ws:", "wss:", ' +
2688
- '"http:", "https:", or "ws+unix:"';
2689
- } else if (isIpcUrl && !parsedUrl.pathname) {
2690
- invalidUrlMessage = "The URL's pathname is empty";
2691
- } else if (parsedUrl.hash) {
2692
- invalidUrlMessage = "The URL contains a fragment identifier";
2693
- }
2694
- if (invalidUrlMessage) {
2695
- const err = new SyntaxError(invalidUrlMessage);
2696
- if (websocket._redirects === 0) {
2697
- throw err;
2698
- } else {
2699
- emitErrorAndClose(websocket, err);
2700
- return;
2701
- }
2702
- }
2703
- const defaultPort = isSecure ? 443 : 80;
2704
- const key = randomBytes(16).toString("base64");
2705
- const request = isSecure ? https.request : http.request;
2706
- const protocolSet = new Set();
2707
- let perMessageDeflate;
2708
- opts.createConnection =
2709
- opts.createConnection || (isSecure ? tlsConnect : netConnect);
2710
- opts.defaultPort = opts.defaultPort || defaultPort;
2711
- opts.port = parsedUrl.port || defaultPort;
2712
- opts.host = parsedUrl.hostname.startsWith("[")
2713
- ? parsedUrl.hostname.slice(1, -1)
2714
- : parsedUrl.hostname;
2715
- opts.headers = {
2716
- ...opts.headers,
2717
- "Sec-WebSocket-Version": opts.protocolVersion,
2718
- "Sec-WebSocket-Key": key,
2719
- Connection: "Upgrade",
2720
- Upgrade: "websocket",
2721
- };
2722
- opts.path = parsedUrl.pathname + parsedUrl.search;
2723
- opts.timeout = opts.handshakeTimeout;
2724
- if (opts.perMessageDeflate) {
2725
- perMessageDeflate = new PerMessageDeflate(
2726
- opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
2727
- false,
2728
- opts.maxPayload,
2729
- );
2730
- opts.headers["Sec-WebSocket-Extensions"] = format({
2731
- [PerMessageDeflate.extensionName]: perMessageDeflate.offer(),
2732
- });
2733
- }
2734
- if (protocols.length) {
2735
- for (const protocol of protocols) {
2736
- if (
2737
- typeof protocol !== "string" ||
2738
- !subprotocolRegex.test(protocol) ||
2739
- protocolSet.has(protocol)
2740
- ) {
2741
- throw new SyntaxError(
2742
- "An invalid or duplicated subprotocol was specified",
2743
- );
2744
- }
2745
- protocolSet.add(protocol);
2746
- }
2747
- opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
2748
- }
2749
- if (opts.origin) {
2750
- if (opts.protocolVersion < 13) {
2751
- opts.headers["Sec-WebSocket-Origin"] = opts.origin;
2752
- } else {
2753
- opts.headers.Origin = opts.origin;
2754
- }
2755
- }
2756
- if (parsedUrl.username || parsedUrl.password) {
2757
- opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
2758
- }
2759
- if (isIpcUrl) {
2760
- const parts = opts.path.split(":");
2761
- opts.socketPath = parts[0];
2762
- opts.path = parts[1];
2763
- }
2764
- let req;
2765
- if (opts.followRedirects) {
2766
- if (websocket._redirects === 0) {
2767
- websocket._originalIpc = isIpcUrl;
2768
- websocket._originalSecure = isSecure;
2769
- websocket._originalHostOrSocketPath = isIpcUrl
2770
- ? opts.socketPath
2771
- : parsedUrl.host;
2772
- const headers = options && options.headers;
2773
- options = { ...options, headers: {} };
2774
- if (headers) {
2775
- for (const [key, value] of Object.entries(headers)) {
2776
- options.headers[key.toLowerCase()] = value;
2777
- }
2778
- }
2779
- } else if (websocket.listenerCount("redirect") === 0) {
2780
- const isSameHost = isIpcUrl
2781
- ? websocket._originalIpc
2782
- ? opts.socketPath === websocket._originalHostOrSocketPath
2783
- : false
2784
- : websocket._originalIpc
2785
- ? false
2786
- : parsedUrl.host === websocket._originalHostOrSocketPath;
2787
- if (!isSameHost || (websocket._originalSecure && !isSecure)) {
2788
- delete opts.headers.authorization;
2789
- delete opts.headers.cookie;
2790
- if (!isSameHost) delete opts.headers.host;
2791
- opts.auth = undefined;
2792
- }
2793
- }
2794
- if (opts.auth && !options.headers.authorization) {
2795
- options.headers.authorization =
2796
- "Basic " + Buffer.from(opts.auth).toString("base64");
2797
- }
2798
- req = websocket._req = request(opts);
2799
- if (websocket._redirects) {
2800
- websocket.emit("redirect", websocket.url, req);
2801
- }
2802
- } else {
2803
- req = websocket._req = request(opts);
2804
- }
2805
- if (opts.timeout) {
2806
- req.on("timeout", () => {
2807
- abortHandshake(websocket, req, "Opening handshake has timed out");
2808
- });
2809
- }
2810
- req.on("error", (err) => {
2811
- if (req === null || req[kAborted]) return;
2812
- req = websocket._req = null;
2813
- emitErrorAndClose(websocket, err);
2814
- });
2815
- req.on("response", (res) => {
2816
- const location = res.headers.location;
2817
- const statusCode = res.statusCode;
2818
- if (
2819
- location &&
2820
- opts.followRedirects &&
2821
- statusCode >= 300 &&
2822
- statusCode < 400
2823
- ) {
2824
- if (++websocket._redirects > opts.maxRedirects) {
2825
- abortHandshake(websocket, req, "Maximum redirects exceeded");
2826
- return;
2827
- }
2828
- req.abort();
2829
- let addr;
2830
- try {
2831
- addr = new URL(location, address);
2832
- } catch (e) {
2833
- const err = new SyntaxError(`Invalid URL: ${location}`);
2834
- emitErrorAndClose(websocket, err);
2835
- return;
2836
- }
2837
- initAsClient(websocket, addr, protocols, options);
2838
- } else if (!websocket.emit("unexpected-response", req, res)) {
2839
- abortHandshake(
2840
- websocket,
2841
- req,
2842
- `Unexpected server response: ${res.statusCode}`,
2843
- );
2844
- }
2845
- });
2846
- req.on("upgrade", (res, socket, head) => {
2847
- websocket.emit("upgrade", res);
2848
- if (websocket.readyState !== WebSocket.CONNECTING) return;
2849
- req = websocket._req = null;
2850
- const upgrade = res.headers.upgrade;
2851
- if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
2852
- abortHandshake(websocket, socket, "Invalid Upgrade header");
2853
- return;
2854
- }
2855
- const digest = createHash("sha1")
2856
- .update(key + GUID)
2857
- .digest("base64");
2858
- if (res.headers["sec-websocket-accept"] !== digest) {
2859
- abortHandshake(
2860
- websocket,
2861
- socket,
2862
- "Invalid Sec-WebSocket-Accept header",
2863
- );
2864
- return;
2865
- }
2866
- const serverProt = res.headers["sec-websocket-protocol"];
2867
- let protError;
2868
- if (serverProt !== undefined) {
2869
- if (!protocolSet.size) {
2870
- protError = "Server sent a subprotocol but none was requested";
2871
- } else if (!protocolSet.has(serverProt)) {
2872
- protError = "Server sent an invalid subprotocol";
2873
- }
2874
- } else if (protocolSet.size) {
2875
- protError = "Server sent no subprotocol";
2876
- }
2877
- if (protError) {
2878
- abortHandshake(websocket, socket, protError);
2879
- return;
2880
- }
2881
- if (serverProt) websocket._protocol = serverProt;
2882
- const secWebSocketExtensions =
2883
- res.headers["sec-websocket-extensions"];
2884
- if (secWebSocketExtensions !== undefined) {
2885
- if (!perMessageDeflate) {
2886
- const message =
2887
- "Server sent a Sec-WebSocket-Extensions header but no extension " +
2888
- "was requested";
2889
- abortHandshake(websocket, socket, message);
2890
- return;
2891
- }
2892
- let extensions;
2893
- try {
2894
- extensions = parse(secWebSocketExtensions);
2895
- } catch (err) {
2896
- const message = "Invalid Sec-WebSocket-Extensions header";
2897
- abortHandshake(websocket, socket, message);
2898
- return;
2899
- }
2900
- const extensionNames = Object.keys(extensions);
2901
- if (
2902
- extensionNames.length !== 1 ||
2903
- extensionNames[0] !== PerMessageDeflate.extensionName
2904
- ) {
2905
- const message =
2906
- "Server indicated an extension that was not requested";
2907
- abortHandshake(websocket, socket, message);
2908
- return;
2909
- }
2910
- try {
2911
- perMessageDeflate.accept(
2912
- extensions[PerMessageDeflate.extensionName],
2913
- );
2914
- } catch (err) {
2915
- const message = "Invalid Sec-WebSocket-Extensions header";
2916
- abortHandshake(websocket, socket, message);
2917
- return;
2918
- }
2919
- websocket._extensions[PerMessageDeflate.extensionName] =
2920
- perMessageDeflate;
2921
- }
2922
- websocket.setSocket(socket, head, {
2923
- allowSynchronousEvents: opts.allowSynchronousEvents,
2924
- generateMask: opts.generateMask,
2925
- maxPayload: opts.maxPayload,
2926
- skipUTF8Validation: opts.skipUTF8Validation,
2927
- });
2928
- });
2929
- if (opts.finishRequest) {
2930
- opts.finishRequest(req, websocket);
2931
- } else {
2932
- req.end();
2933
- }
2934
- }
2935
- function emitErrorAndClose(websocket, err) {
2936
- websocket._readyState = WebSocket.CLOSING;
2937
- websocket._errorEmitted = true;
2938
- websocket.emit("error", err);
2939
- websocket.emitClose();
2940
- }
2941
- function netConnect(options) {
2942
- options.path = options.socketPath;
2943
- return net.connect(options);
2944
- }
2945
- function tlsConnect(options) {
2946
- options.path = undefined;
2947
- if (!options.servername && options.servername !== "") {
2948
- options.servername = net.isIP(options.host) ? "" : options.host;
2949
- }
2950
- return tls.connect(options);
2951
- }
2952
- function abortHandshake(websocket, stream, message) {
2953
- websocket._readyState = WebSocket.CLOSING;
2954
- const err = new Error(message);
2955
- Error.captureStackTrace(err, abortHandshake);
2956
- if (stream.setHeader) {
2957
- stream[kAborted] = true;
2958
- stream.abort();
2959
- if (stream.socket && !stream.socket.destroyed) {
2960
- stream.socket.destroy();
2961
- }
2962
- process.nextTick(emitErrorAndClose, websocket, err);
2963
- } else {
2964
- stream.destroy(err);
2965
- stream.once("error", websocket.emit.bind(websocket, "error"));
2966
- stream.once("close", websocket.emitClose.bind(websocket));
2967
- }
2968
- }
2969
- function sendAfterClose(websocket, data, cb) {
2970
- if (data) {
2971
- const length = isBlob(data) ? data.size : toBuffer(data).length;
2972
- if (websocket._socket) websocket._sender._bufferedBytes += length;
2973
- else websocket._bufferedAmount += length;
2974
- }
2975
- if (cb) {
2976
- const err = new Error(
2977
- `WebSocket is not open: readyState ${websocket.readyState} ` +
2978
- `(${readyStates[websocket.readyState]})`,
2979
- );
2980
- process.nextTick(cb, err);
2981
- }
2982
- }
2983
- function receiverOnConclude(code, reason) {
2984
- const websocket = this[kWebSocket];
2985
- websocket._closeFrameReceived = true;
2986
- websocket._closeMessage = reason;
2987
- websocket._closeCode = code;
2988
- if (websocket._socket[kWebSocket] === undefined) return;
2989
- websocket._socket.removeListener("data", socketOnData);
2990
- process.nextTick(resume, websocket._socket);
2991
- if (code === 1005) websocket.close();
2992
- else websocket.close(code, reason);
2993
- }
2994
- function receiverOnDrain() {
2995
- const websocket = this[kWebSocket];
2996
- if (!websocket.isPaused) websocket._socket.resume();
2997
- }
2998
- function receiverOnError(err) {
2999
- const websocket = this[kWebSocket];
3000
- if (websocket._socket[kWebSocket] !== undefined) {
3001
- websocket._socket.removeListener("data", socketOnData);
3002
- process.nextTick(resume, websocket._socket);
3003
- websocket.close(err[kStatusCode]);
3004
- }
3005
- if (!websocket._errorEmitted) {
3006
- websocket._errorEmitted = true;
3007
- websocket.emit("error", err);
3008
- }
3009
- }
3010
- function receiverOnFinish() {
3011
- this[kWebSocket].emitClose();
3012
- }
3013
- function receiverOnMessage(data, isBinary) {
3014
- this[kWebSocket].emit("message", data, isBinary);
3015
- }
3016
- function receiverOnPing(data) {
3017
- const websocket = this[kWebSocket];
3018
- if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP);
3019
- websocket.emit("ping", data);
3020
- }
3021
- function receiverOnPong(data) {
3022
- this[kWebSocket].emit("pong", data);
3023
- }
3024
- function resume(stream) {
3025
- stream.resume();
3026
- }
3027
- function senderOnError(err) {
3028
- const websocket = this[kWebSocket];
3029
- if (websocket.readyState === WebSocket.CLOSED) return;
3030
- if (websocket.readyState === WebSocket.OPEN) {
3031
- websocket._readyState = WebSocket.CLOSING;
3032
- setCloseTimer(websocket);
3033
- }
3034
- this._socket.end();
3035
- if (!websocket._errorEmitted) {
3036
- websocket._errorEmitted = true;
3037
- websocket.emit("error", err);
3038
- }
3039
- }
3040
- function setCloseTimer(websocket) {
3041
- websocket._closeTimer = setTimeout(
3042
- websocket._socket.destroy.bind(websocket._socket),
3043
- websocket._closeTimeout,
3044
- );
3045
- }
3046
- function socketOnClose() {
3047
- const websocket = this[kWebSocket];
3048
- this.removeListener("close", socketOnClose);
3049
- this.removeListener("data", socketOnData);
3050
- this.removeListener("end", socketOnEnd);
3051
- websocket._readyState = WebSocket.CLOSING;
3052
- if (
3053
- !this._readableState.endEmitted &&
3054
- !websocket._closeFrameReceived &&
3055
- !websocket._receiver._writableState.errorEmitted &&
3056
- this._readableState.length !== 0
3057
- ) {
3058
- const chunk = this.read(this._readableState.length);
3059
- websocket._receiver.write(chunk);
3060
- }
3061
- websocket._receiver.end();
3062
- this[kWebSocket] = undefined;
3063
- clearTimeout(websocket._closeTimer);
3064
- if (
3065
- websocket._receiver._writableState.finished ||
3066
- websocket._receiver._writableState.errorEmitted
3067
- ) {
3068
- websocket.emitClose();
3069
- } else {
3070
- websocket._receiver.on("error", receiverOnFinish);
3071
- websocket._receiver.on("finish", receiverOnFinish);
3072
- }
3073
- }
3074
- function socketOnData(chunk) {
3075
- if (!this[kWebSocket]._receiver.write(chunk)) {
3076
- this.pause();
3077
- }
3078
- }
3079
- function socketOnEnd() {
3080
- const websocket = this[kWebSocket];
3081
- websocket._readyState = WebSocket.CLOSING;
3082
- websocket._receiver.end();
3083
- this.end();
3084
- }
3085
- function socketOnError() {
3086
- const websocket = this[kWebSocket];
3087
- this.removeListener("error", socketOnError);
3088
- this.on("error", NOOP);
3089
- if (websocket) {
3090
- websocket._readyState = WebSocket.CLOSING;
3091
- this.destroy();
3092
- }
3093
- }
3094
- },
3095
- 431: (module) => {
3096
- module.exports = eval("require")("bufferutil");
3097
- },
3098
- 710: (module) => {
3099
- module.exports = eval("require")("utf-8-validate");
3100
- },
3101
- 181: (module) => {
3102
- "use strict";
3103
- module.exports = require("buffer");
3104
- },
3105
- 982: (module) => {
3106
- "use strict";
3107
- module.exports = require("crypto");
3108
- },
3109
- 434: (module) => {
3110
- "use strict";
3111
- module.exports = require("events");
3112
- },
3113
- 611: (module) => {
3114
- "use strict";
3115
- module.exports = require("http");
3116
- },
3117
- 692: (module) => {
3118
- "use strict";
3119
- module.exports = require("https");
3120
- },
3121
- 278: (module) => {
3122
- "use strict";
3123
- module.exports = require("net");
3124
- },
3125
- 203: (module) => {
3126
- "use strict";
3127
- module.exports = require("stream");
3128
- },
3129
- 756: (module) => {
3130
- "use strict";
3131
- module.exports = require("tls");
3132
- },
3133
- 16: (module) => {
3134
- "use strict";
3135
- module.exports = require("url");
3136
- },
3137
- 106: (module) => {
3138
- "use strict";
3139
- module.exports = require("zlib");
3140
- },
3141
- };
3142
- var __webpack_module_cache__ = {};
3143
- function __nccwpck_require__(moduleId) {
3144
- var cachedModule = __webpack_module_cache__[moduleId];
3145
- if (cachedModule !== undefined) {
3146
- return cachedModule.exports;
3147
- }
3148
- var module = (__webpack_module_cache__[moduleId] = { exports: {} });
3149
- var threw = true;
3150
- try {
3151
- __webpack_modules__[moduleId](
3152
- module,
3153
- module.exports,
3154
- __nccwpck_require__,
3155
- );
3156
- threw = false;
3157
- } finally {
3158
- if (threw) delete __webpack_module_cache__[moduleId];
3159
- }
3160
- return module.exports;
3161
- }
3162
- if (typeof __nccwpck_require__ !== "undefined")
3163
- __nccwpck_require__.ab = __dirname + "/";
3164
- var __webpack_exports__ = __nccwpck_require__(159);
3165
- module.exports = __webpack_exports__;
3166
- })();