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