@blinkdotnew/sdk 2.4.0 → 2.5.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/dist/index.d.mts +142 -1
- package/dist/index.d.ts +142 -1
- package/dist/index.js +4007 -96
- package/dist/index.mjs +4006 -97
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,3724 @@
|
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
1
2
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
3
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
4
|
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined")
|
|
5
|
+
if (typeof require !== "undefined")
|
|
6
|
+
return require.apply(this, arguments);
|
|
5
7
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
8
|
});
|
|
9
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
10
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// ../../../node_modules/ws/lib/constants.js
|
|
14
|
+
var require_constants = __commonJS({
|
|
15
|
+
"../../../node_modules/ws/lib/constants.js"(exports$1, module) {
|
|
16
|
+
var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
17
|
+
var hasBlob = typeof Blob !== "undefined";
|
|
18
|
+
if (hasBlob)
|
|
19
|
+
BINARY_TYPES.push("blob");
|
|
20
|
+
module.exports = {
|
|
21
|
+
BINARY_TYPES,
|
|
22
|
+
CLOSE_TIMEOUT: 3e4,
|
|
23
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
24
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
25
|
+
hasBlob,
|
|
26
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
27
|
+
kListener: Symbol("kListener"),
|
|
28
|
+
kStatusCode: Symbol("status-code"),
|
|
29
|
+
kWebSocket: Symbol("websocket"),
|
|
30
|
+
NOOP: () => {
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// ../../../node_modules/ws/lib/buffer-util.js
|
|
37
|
+
var require_buffer_util = __commonJS({
|
|
38
|
+
"../../../node_modules/ws/lib/buffer-util.js"(exports$1, module) {
|
|
39
|
+
var { EMPTY_BUFFER } = require_constants();
|
|
40
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
41
|
+
function concat(list, totalLength) {
|
|
42
|
+
if (list.length === 0)
|
|
43
|
+
return EMPTY_BUFFER;
|
|
44
|
+
if (list.length === 1)
|
|
45
|
+
return list[0];
|
|
46
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
47
|
+
let offset = 0;
|
|
48
|
+
for (let i = 0; i < list.length; i++) {
|
|
49
|
+
const buf = list[i];
|
|
50
|
+
target.set(buf, offset);
|
|
51
|
+
offset += buf.length;
|
|
52
|
+
}
|
|
53
|
+
if (offset < totalLength) {
|
|
54
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
55
|
+
}
|
|
56
|
+
return target;
|
|
57
|
+
}
|
|
58
|
+
function _mask(source, mask, output, offset, length) {
|
|
59
|
+
for (let i = 0; i < length; i++) {
|
|
60
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function _unmask(buffer, mask) {
|
|
64
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
65
|
+
buffer[i] ^= mask[i & 3];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function toArrayBuffer(buf) {
|
|
69
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
70
|
+
return buf.buffer;
|
|
71
|
+
}
|
|
72
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
73
|
+
}
|
|
74
|
+
function toBuffer(data) {
|
|
75
|
+
toBuffer.readOnly = true;
|
|
76
|
+
if (Buffer.isBuffer(data))
|
|
77
|
+
return data;
|
|
78
|
+
let buf;
|
|
79
|
+
if (data instanceof ArrayBuffer) {
|
|
80
|
+
buf = new FastBuffer(data);
|
|
81
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
82
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
83
|
+
} else {
|
|
84
|
+
buf = Buffer.from(data);
|
|
85
|
+
toBuffer.readOnly = false;
|
|
86
|
+
}
|
|
87
|
+
return buf;
|
|
88
|
+
}
|
|
89
|
+
module.exports = {
|
|
90
|
+
concat,
|
|
91
|
+
mask: _mask,
|
|
92
|
+
toArrayBuffer,
|
|
93
|
+
toBuffer,
|
|
94
|
+
unmask: _unmask
|
|
95
|
+
};
|
|
96
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
97
|
+
try {
|
|
98
|
+
const bufferUtil = __require("bufferutil");
|
|
99
|
+
module.exports.mask = function(source, mask, output, offset, length) {
|
|
100
|
+
if (length < 48)
|
|
101
|
+
_mask(source, mask, output, offset, length);
|
|
102
|
+
else
|
|
103
|
+
bufferUtil.mask(source, mask, output, offset, length);
|
|
104
|
+
};
|
|
105
|
+
module.exports.unmask = function(buffer, mask) {
|
|
106
|
+
if (buffer.length < 32)
|
|
107
|
+
_unmask(buffer, mask);
|
|
108
|
+
else
|
|
109
|
+
bufferUtil.unmask(buffer, mask);
|
|
110
|
+
};
|
|
111
|
+
} catch (e) {
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// ../../../node_modules/ws/lib/limiter.js
|
|
118
|
+
var require_limiter = __commonJS({
|
|
119
|
+
"../../../node_modules/ws/lib/limiter.js"(exports$1, module) {
|
|
120
|
+
var kDone = Symbol("kDone");
|
|
121
|
+
var kRun = Symbol("kRun");
|
|
122
|
+
var Limiter = class {
|
|
123
|
+
/**
|
|
124
|
+
* Creates a new `Limiter`.
|
|
125
|
+
*
|
|
126
|
+
* @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
|
|
127
|
+
* to run concurrently
|
|
128
|
+
*/
|
|
129
|
+
constructor(concurrency) {
|
|
130
|
+
this[kDone] = () => {
|
|
131
|
+
this.pending--;
|
|
132
|
+
this[kRun]();
|
|
133
|
+
};
|
|
134
|
+
this.concurrency = concurrency || Infinity;
|
|
135
|
+
this.jobs = [];
|
|
136
|
+
this.pending = 0;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Adds a job to the queue.
|
|
140
|
+
*
|
|
141
|
+
* @param {Function} job The job to run
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
add(job) {
|
|
145
|
+
this.jobs.push(job);
|
|
146
|
+
this[kRun]();
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Removes a job from the queue and runs it if possible.
|
|
150
|
+
*
|
|
151
|
+
* @private
|
|
152
|
+
*/
|
|
153
|
+
[kRun]() {
|
|
154
|
+
if (this.pending === this.concurrency)
|
|
155
|
+
return;
|
|
156
|
+
if (this.jobs.length) {
|
|
157
|
+
const job = this.jobs.shift();
|
|
158
|
+
this.pending++;
|
|
159
|
+
job(this[kDone]);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
module.exports = Limiter;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// ../../../node_modules/ws/lib/permessage-deflate.js
|
|
168
|
+
var require_permessage_deflate = __commonJS({
|
|
169
|
+
"../../../node_modules/ws/lib/permessage-deflate.js"(exports$1, module) {
|
|
170
|
+
var zlib = __require("zlib");
|
|
171
|
+
var bufferUtil = require_buffer_util();
|
|
172
|
+
var Limiter = require_limiter();
|
|
173
|
+
var { kStatusCode } = require_constants();
|
|
174
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
175
|
+
var TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
176
|
+
var kPerMessageDeflate = Symbol("permessage-deflate");
|
|
177
|
+
var kTotalLength = Symbol("total-length");
|
|
178
|
+
var kCallback = Symbol("callback");
|
|
179
|
+
var kBuffers = Symbol("buffers");
|
|
180
|
+
var kError = Symbol("error");
|
|
181
|
+
var zlibLimiter;
|
|
182
|
+
var PerMessageDeflate = class {
|
|
183
|
+
/**
|
|
184
|
+
* Creates a PerMessageDeflate instance.
|
|
185
|
+
*
|
|
186
|
+
* @param {Object} [options] Configuration options
|
|
187
|
+
* @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
|
|
188
|
+
* for, or request, a custom client window size
|
|
189
|
+
* @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
|
|
190
|
+
* acknowledge disabling of client context takeover
|
|
191
|
+
* @param {Number} [options.concurrencyLimit=10] The number of concurrent
|
|
192
|
+
* calls to zlib
|
|
193
|
+
* @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
|
|
194
|
+
* use of a custom server window size
|
|
195
|
+
* @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
|
|
196
|
+
* disabling of server context takeover
|
|
197
|
+
* @param {Number} [options.threshold=1024] Size (in bytes) below which
|
|
198
|
+
* messages should not be compressed if context takeover is disabled
|
|
199
|
+
* @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
|
|
200
|
+
* deflate
|
|
201
|
+
* @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
|
|
202
|
+
* inflate
|
|
203
|
+
* @param {Boolean} [isServer=false] Create the instance in either server or
|
|
204
|
+
* client mode
|
|
205
|
+
* @param {Number} [maxPayload=0] The maximum allowed message length
|
|
206
|
+
*/
|
|
207
|
+
constructor(options, isServer2, maxPayload) {
|
|
208
|
+
this._maxPayload = maxPayload | 0;
|
|
209
|
+
this._options = options || {};
|
|
210
|
+
this._threshold = this._options.threshold !== void 0 ? this._options.threshold : 1024;
|
|
211
|
+
this._isServer = !!isServer2;
|
|
212
|
+
this._deflate = null;
|
|
213
|
+
this._inflate = null;
|
|
214
|
+
this.params = null;
|
|
215
|
+
if (!zlibLimiter) {
|
|
216
|
+
const concurrency = this._options.concurrencyLimit !== void 0 ? this._options.concurrencyLimit : 10;
|
|
217
|
+
zlibLimiter = new Limiter(concurrency);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* @type {String}
|
|
222
|
+
*/
|
|
223
|
+
static get extensionName() {
|
|
224
|
+
return "permessage-deflate";
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Create an extension negotiation offer.
|
|
228
|
+
*
|
|
229
|
+
* @return {Object} Extension parameters
|
|
230
|
+
* @public
|
|
231
|
+
*/
|
|
232
|
+
offer() {
|
|
233
|
+
const params = {};
|
|
234
|
+
if (this._options.serverNoContextTakeover) {
|
|
235
|
+
params.server_no_context_takeover = true;
|
|
236
|
+
}
|
|
237
|
+
if (this._options.clientNoContextTakeover) {
|
|
238
|
+
params.client_no_context_takeover = true;
|
|
239
|
+
}
|
|
240
|
+
if (this._options.serverMaxWindowBits) {
|
|
241
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
242
|
+
}
|
|
243
|
+
if (this._options.clientMaxWindowBits) {
|
|
244
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
245
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
246
|
+
params.client_max_window_bits = true;
|
|
247
|
+
}
|
|
248
|
+
return params;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Accept an extension negotiation offer/response.
|
|
252
|
+
*
|
|
253
|
+
* @param {Array} configurations The extension negotiation offers/reponse
|
|
254
|
+
* @return {Object} Accepted configuration
|
|
255
|
+
* @public
|
|
256
|
+
*/
|
|
257
|
+
accept(configurations) {
|
|
258
|
+
configurations = this.normalizeParams(configurations);
|
|
259
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
260
|
+
return this.params;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Releases all resources used by the extension.
|
|
264
|
+
*
|
|
265
|
+
* @public
|
|
266
|
+
*/
|
|
267
|
+
cleanup() {
|
|
268
|
+
if (this._inflate) {
|
|
269
|
+
this._inflate.close();
|
|
270
|
+
this._inflate = null;
|
|
271
|
+
}
|
|
272
|
+
if (this._deflate) {
|
|
273
|
+
const callback = this._deflate[kCallback];
|
|
274
|
+
this._deflate.close();
|
|
275
|
+
this._deflate = null;
|
|
276
|
+
if (callback) {
|
|
277
|
+
callback(
|
|
278
|
+
new Error(
|
|
279
|
+
"The deflate stream was closed while data was being processed"
|
|
280
|
+
)
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Accept an extension negotiation offer.
|
|
287
|
+
*
|
|
288
|
+
* @param {Array} offers The extension negotiation offers
|
|
289
|
+
* @return {Object} Accepted configuration
|
|
290
|
+
* @private
|
|
291
|
+
*/
|
|
292
|
+
acceptAsServer(offers) {
|
|
293
|
+
const opts = this._options;
|
|
294
|
+
const accepted = offers.find((params) => {
|
|
295
|
+
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) {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
return true;
|
|
299
|
+
});
|
|
300
|
+
if (!accepted) {
|
|
301
|
+
throw new Error("None of the extension offers can be accepted");
|
|
302
|
+
}
|
|
303
|
+
if (opts.serverNoContextTakeover) {
|
|
304
|
+
accepted.server_no_context_takeover = true;
|
|
305
|
+
}
|
|
306
|
+
if (opts.clientNoContextTakeover) {
|
|
307
|
+
accepted.client_no_context_takeover = true;
|
|
308
|
+
}
|
|
309
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
310
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
311
|
+
}
|
|
312
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
313
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
314
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
315
|
+
delete accepted.client_max_window_bits;
|
|
316
|
+
}
|
|
317
|
+
return accepted;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Accept the extension negotiation response.
|
|
321
|
+
*
|
|
322
|
+
* @param {Array} response The extension negotiation response
|
|
323
|
+
* @return {Object} Accepted configuration
|
|
324
|
+
* @private
|
|
325
|
+
*/
|
|
326
|
+
acceptAsClient(response) {
|
|
327
|
+
const params = response[0];
|
|
328
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
329
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
330
|
+
}
|
|
331
|
+
if (!params.client_max_window_bits) {
|
|
332
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
333
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
334
|
+
}
|
|
335
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
336
|
+
throw new Error(
|
|
337
|
+
'Unexpected or invalid parameter "client_max_window_bits"'
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
return params;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Normalize parameters.
|
|
344
|
+
*
|
|
345
|
+
* @param {Array} configurations The extension negotiation offers/reponse
|
|
346
|
+
* @return {Array} The offers/response with normalized parameters
|
|
347
|
+
* @private
|
|
348
|
+
*/
|
|
349
|
+
normalizeParams(configurations) {
|
|
350
|
+
configurations.forEach((params) => {
|
|
351
|
+
Object.keys(params).forEach((key) => {
|
|
352
|
+
let value = params[key];
|
|
353
|
+
if (value.length > 1) {
|
|
354
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
355
|
+
}
|
|
356
|
+
value = value[0];
|
|
357
|
+
if (key === "client_max_window_bits") {
|
|
358
|
+
if (value !== true) {
|
|
359
|
+
const num = +value;
|
|
360
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
361
|
+
throw new TypeError(
|
|
362
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
value = num;
|
|
366
|
+
} else if (!this._isServer) {
|
|
367
|
+
throw new TypeError(
|
|
368
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
} else if (key === "server_max_window_bits") {
|
|
372
|
+
const num = +value;
|
|
373
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
374
|
+
throw new TypeError(
|
|
375
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
value = num;
|
|
379
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
380
|
+
if (value !== true) {
|
|
381
|
+
throw new TypeError(
|
|
382
|
+
`Invalid value for parameter "${key}": ${value}`
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
} else {
|
|
386
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
387
|
+
}
|
|
388
|
+
params[key] = value;
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
return configurations;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Decompress data. Concurrency limited.
|
|
395
|
+
*
|
|
396
|
+
* @param {Buffer} data Compressed data
|
|
397
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
398
|
+
* @param {Function} callback Callback
|
|
399
|
+
* @public
|
|
400
|
+
*/
|
|
401
|
+
decompress(data, fin, callback) {
|
|
402
|
+
zlibLimiter.add((done) => {
|
|
403
|
+
this._decompress(data, fin, (err, result) => {
|
|
404
|
+
done();
|
|
405
|
+
callback(err, result);
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Compress data. Concurrency limited.
|
|
411
|
+
*
|
|
412
|
+
* @param {(Buffer|String)} data Data to compress
|
|
413
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
414
|
+
* @param {Function} callback Callback
|
|
415
|
+
* @public
|
|
416
|
+
*/
|
|
417
|
+
compress(data, fin, callback) {
|
|
418
|
+
zlibLimiter.add((done) => {
|
|
419
|
+
this._compress(data, fin, (err, result) => {
|
|
420
|
+
done();
|
|
421
|
+
callback(err, result);
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Decompress data.
|
|
427
|
+
*
|
|
428
|
+
* @param {Buffer} data Compressed data
|
|
429
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
430
|
+
* @param {Function} callback Callback
|
|
431
|
+
* @private
|
|
432
|
+
*/
|
|
433
|
+
_decompress(data, fin, callback) {
|
|
434
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
435
|
+
if (!this._inflate) {
|
|
436
|
+
const key = `${endpoint}_max_window_bits`;
|
|
437
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
438
|
+
this._inflate = zlib.createInflateRaw({
|
|
439
|
+
...this._options.zlibInflateOptions,
|
|
440
|
+
windowBits
|
|
441
|
+
});
|
|
442
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
443
|
+
this._inflate[kTotalLength] = 0;
|
|
444
|
+
this._inflate[kBuffers] = [];
|
|
445
|
+
this._inflate.on("error", inflateOnError);
|
|
446
|
+
this._inflate.on("data", inflateOnData);
|
|
447
|
+
}
|
|
448
|
+
this._inflate[kCallback] = callback;
|
|
449
|
+
this._inflate.write(data);
|
|
450
|
+
if (fin)
|
|
451
|
+
this._inflate.write(TRAILER);
|
|
452
|
+
this._inflate.flush(() => {
|
|
453
|
+
const err = this._inflate[kError];
|
|
454
|
+
if (err) {
|
|
455
|
+
this._inflate.close();
|
|
456
|
+
this._inflate = null;
|
|
457
|
+
callback(err);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const data2 = bufferUtil.concat(
|
|
461
|
+
this._inflate[kBuffers],
|
|
462
|
+
this._inflate[kTotalLength]
|
|
463
|
+
);
|
|
464
|
+
if (this._inflate._readableState.endEmitted) {
|
|
465
|
+
this._inflate.close();
|
|
466
|
+
this._inflate = null;
|
|
467
|
+
} else {
|
|
468
|
+
this._inflate[kTotalLength] = 0;
|
|
469
|
+
this._inflate[kBuffers] = [];
|
|
470
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
471
|
+
this._inflate.reset();
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
callback(null, data2);
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Compress data.
|
|
479
|
+
*
|
|
480
|
+
* @param {(Buffer|String)} data Data to compress
|
|
481
|
+
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
|
482
|
+
* @param {Function} callback Callback
|
|
483
|
+
* @private
|
|
484
|
+
*/
|
|
485
|
+
_compress(data, fin, callback) {
|
|
486
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
487
|
+
if (!this._deflate) {
|
|
488
|
+
const key = `${endpoint}_max_window_bits`;
|
|
489
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
490
|
+
this._deflate = zlib.createDeflateRaw({
|
|
491
|
+
...this._options.zlibDeflateOptions,
|
|
492
|
+
windowBits
|
|
493
|
+
});
|
|
494
|
+
this._deflate[kTotalLength] = 0;
|
|
495
|
+
this._deflate[kBuffers] = [];
|
|
496
|
+
this._deflate.on("data", deflateOnData);
|
|
497
|
+
}
|
|
498
|
+
this._deflate[kCallback] = callback;
|
|
499
|
+
this._deflate.write(data);
|
|
500
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
501
|
+
if (!this._deflate) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
let data2 = bufferUtil.concat(
|
|
505
|
+
this._deflate[kBuffers],
|
|
506
|
+
this._deflate[kTotalLength]
|
|
507
|
+
);
|
|
508
|
+
if (fin) {
|
|
509
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
510
|
+
}
|
|
511
|
+
this._deflate[kCallback] = null;
|
|
512
|
+
this._deflate[kTotalLength] = 0;
|
|
513
|
+
this._deflate[kBuffers] = [];
|
|
514
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
515
|
+
this._deflate.reset();
|
|
516
|
+
}
|
|
517
|
+
callback(null, data2);
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
module.exports = PerMessageDeflate;
|
|
522
|
+
function deflateOnData(chunk) {
|
|
523
|
+
this[kBuffers].push(chunk);
|
|
524
|
+
this[kTotalLength] += chunk.length;
|
|
525
|
+
}
|
|
526
|
+
function inflateOnData(chunk) {
|
|
527
|
+
this[kTotalLength] += chunk.length;
|
|
528
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
529
|
+
this[kBuffers].push(chunk);
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
533
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
534
|
+
this[kError][kStatusCode] = 1009;
|
|
535
|
+
this.removeListener("data", inflateOnData);
|
|
536
|
+
this.reset();
|
|
537
|
+
}
|
|
538
|
+
function inflateOnError(err) {
|
|
539
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
540
|
+
if (this[kError]) {
|
|
541
|
+
this[kCallback](this[kError]);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
err[kStatusCode] = 1007;
|
|
545
|
+
this[kCallback](err);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
// ../../../node_modules/ws/lib/validation.js
|
|
551
|
+
var require_validation = __commonJS({
|
|
552
|
+
"../../../node_modules/ws/lib/validation.js"(exports$1, module) {
|
|
553
|
+
var { isUtf8 } = __require("buffer");
|
|
554
|
+
var { hasBlob } = require_constants();
|
|
555
|
+
var tokenChars = [
|
|
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 - 15
|
|
573
|
+
0,
|
|
574
|
+
0,
|
|
575
|
+
0,
|
|
576
|
+
0,
|
|
577
|
+
0,
|
|
578
|
+
0,
|
|
579
|
+
0,
|
|
580
|
+
0,
|
|
581
|
+
0,
|
|
582
|
+
0,
|
|
583
|
+
0,
|
|
584
|
+
0,
|
|
585
|
+
0,
|
|
586
|
+
0,
|
|
587
|
+
0,
|
|
588
|
+
0,
|
|
589
|
+
// 16 - 31
|
|
590
|
+
0,
|
|
591
|
+
1,
|
|
592
|
+
0,
|
|
593
|
+
1,
|
|
594
|
+
1,
|
|
595
|
+
1,
|
|
596
|
+
1,
|
|
597
|
+
1,
|
|
598
|
+
0,
|
|
599
|
+
0,
|
|
600
|
+
1,
|
|
601
|
+
1,
|
|
602
|
+
0,
|
|
603
|
+
1,
|
|
604
|
+
1,
|
|
605
|
+
0,
|
|
606
|
+
// 32 - 47
|
|
607
|
+
1,
|
|
608
|
+
1,
|
|
609
|
+
1,
|
|
610
|
+
1,
|
|
611
|
+
1,
|
|
612
|
+
1,
|
|
613
|
+
1,
|
|
614
|
+
1,
|
|
615
|
+
1,
|
|
616
|
+
1,
|
|
617
|
+
0,
|
|
618
|
+
0,
|
|
619
|
+
0,
|
|
620
|
+
0,
|
|
621
|
+
0,
|
|
622
|
+
0,
|
|
623
|
+
// 48 - 63
|
|
624
|
+
0,
|
|
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
|
+
// 64 - 79
|
|
641
|
+
1,
|
|
642
|
+
1,
|
|
643
|
+
1,
|
|
644
|
+
1,
|
|
645
|
+
1,
|
|
646
|
+
1,
|
|
647
|
+
1,
|
|
648
|
+
1,
|
|
649
|
+
1,
|
|
650
|
+
1,
|
|
651
|
+
1,
|
|
652
|
+
0,
|
|
653
|
+
0,
|
|
654
|
+
0,
|
|
655
|
+
1,
|
|
656
|
+
1,
|
|
657
|
+
// 80 - 95
|
|
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
|
+
1,
|
|
674
|
+
// 96 - 111
|
|
675
|
+
1,
|
|
676
|
+
1,
|
|
677
|
+
1,
|
|
678
|
+
1,
|
|
679
|
+
1,
|
|
680
|
+
1,
|
|
681
|
+
1,
|
|
682
|
+
1,
|
|
683
|
+
1,
|
|
684
|
+
1,
|
|
685
|
+
1,
|
|
686
|
+
0,
|
|
687
|
+
1,
|
|
688
|
+
0,
|
|
689
|
+
1,
|
|
690
|
+
0
|
|
691
|
+
// 112 - 127
|
|
692
|
+
];
|
|
693
|
+
function isValidStatusCode(code) {
|
|
694
|
+
return code >= 1e3 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3e3 && code <= 4999;
|
|
695
|
+
}
|
|
696
|
+
function _isValidUTF8(buf) {
|
|
697
|
+
const len = buf.length;
|
|
698
|
+
let i = 0;
|
|
699
|
+
while (i < len) {
|
|
700
|
+
if ((buf[i] & 128) === 0) {
|
|
701
|
+
i++;
|
|
702
|
+
} else if ((buf[i] & 224) === 192) {
|
|
703
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) {
|
|
704
|
+
return false;
|
|
705
|
+
}
|
|
706
|
+
i += 2;
|
|
707
|
+
} else if ((buf[i] & 240) === 224) {
|
|
708
|
+
if (i + 2 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || buf[i] === 224 && (buf[i + 1] & 224) === 128 || // Overlong
|
|
709
|
+
buf[i] === 237 && (buf[i + 1] & 224) === 160) {
|
|
710
|
+
return false;
|
|
711
|
+
}
|
|
712
|
+
i += 3;
|
|
713
|
+
} else if ((buf[i] & 248) === 240) {
|
|
714
|
+
if (i + 3 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || (buf[i + 3] & 192) !== 128 || buf[i] === 240 && (buf[i + 1] & 240) === 128 || // Overlong
|
|
715
|
+
buf[i] === 244 && buf[i + 1] > 143 || buf[i] > 244) {
|
|
716
|
+
return false;
|
|
717
|
+
}
|
|
718
|
+
i += 4;
|
|
719
|
+
} else {
|
|
720
|
+
return false;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
return true;
|
|
724
|
+
}
|
|
725
|
+
function isBlob(value) {
|
|
726
|
+
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");
|
|
727
|
+
}
|
|
728
|
+
module.exports = {
|
|
729
|
+
isBlob,
|
|
730
|
+
isValidStatusCode,
|
|
731
|
+
isValidUTF8: _isValidUTF8,
|
|
732
|
+
tokenChars
|
|
733
|
+
};
|
|
734
|
+
if (isUtf8) {
|
|
735
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
736
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
737
|
+
};
|
|
738
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
739
|
+
try {
|
|
740
|
+
const isValidUTF8 = __require("utf-8-validate");
|
|
741
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
742
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
743
|
+
};
|
|
744
|
+
} catch (e) {
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
// ../../../node_modules/ws/lib/receiver.js
|
|
751
|
+
var require_receiver = __commonJS({
|
|
752
|
+
"../../../node_modules/ws/lib/receiver.js"(exports$1, module) {
|
|
753
|
+
var { Writable } = __require("stream");
|
|
754
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
755
|
+
var {
|
|
756
|
+
BINARY_TYPES,
|
|
757
|
+
EMPTY_BUFFER,
|
|
758
|
+
kStatusCode,
|
|
759
|
+
kWebSocket
|
|
760
|
+
} = require_constants();
|
|
761
|
+
var { concat, toArrayBuffer, unmask } = require_buffer_util();
|
|
762
|
+
var { isValidStatusCode, isValidUTF8 } = require_validation();
|
|
763
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
764
|
+
var GET_INFO = 0;
|
|
765
|
+
var GET_PAYLOAD_LENGTH_16 = 1;
|
|
766
|
+
var GET_PAYLOAD_LENGTH_64 = 2;
|
|
767
|
+
var GET_MASK = 3;
|
|
768
|
+
var GET_DATA = 4;
|
|
769
|
+
var INFLATING = 5;
|
|
770
|
+
var DEFER_EVENT = 6;
|
|
771
|
+
var Receiver = class extends Writable {
|
|
772
|
+
/**
|
|
773
|
+
* Creates a Receiver instance.
|
|
774
|
+
*
|
|
775
|
+
* @param {Object} [options] Options object
|
|
776
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
|
|
777
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
778
|
+
* multiple times in the same tick
|
|
779
|
+
* @param {String} [options.binaryType=nodebuffer] The type for binary data
|
|
780
|
+
* @param {Object} [options.extensions] An object containing the negotiated
|
|
781
|
+
* extensions
|
|
782
|
+
* @param {Boolean} [options.isServer=false] Specifies whether to operate in
|
|
783
|
+
* client or server mode
|
|
784
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
|
785
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
786
|
+
* not to skip UTF-8 validation for text and close messages
|
|
787
|
+
*/
|
|
788
|
+
constructor(options = {}) {
|
|
789
|
+
super();
|
|
790
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== void 0 ? options.allowSynchronousEvents : true;
|
|
791
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
792
|
+
this._extensions = options.extensions || {};
|
|
793
|
+
this._isServer = !!options.isServer;
|
|
794
|
+
this._maxPayload = options.maxPayload | 0;
|
|
795
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
796
|
+
this[kWebSocket] = void 0;
|
|
797
|
+
this._bufferedBytes = 0;
|
|
798
|
+
this._buffers = [];
|
|
799
|
+
this._compressed = false;
|
|
800
|
+
this._payloadLength = 0;
|
|
801
|
+
this._mask = void 0;
|
|
802
|
+
this._fragmented = 0;
|
|
803
|
+
this._masked = false;
|
|
804
|
+
this._fin = false;
|
|
805
|
+
this._opcode = 0;
|
|
806
|
+
this._totalPayloadLength = 0;
|
|
807
|
+
this._messageLength = 0;
|
|
808
|
+
this._fragments = [];
|
|
809
|
+
this._errored = false;
|
|
810
|
+
this._loop = false;
|
|
811
|
+
this._state = GET_INFO;
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Implements `Writable.prototype._write()`.
|
|
815
|
+
*
|
|
816
|
+
* @param {Buffer} chunk The chunk of data to write
|
|
817
|
+
* @param {String} encoding The character encoding of `chunk`
|
|
818
|
+
* @param {Function} cb Callback
|
|
819
|
+
* @private
|
|
820
|
+
*/
|
|
821
|
+
_write(chunk, encoding, cb) {
|
|
822
|
+
if (this._opcode === 8 && this._state == GET_INFO)
|
|
823
|
+
return cb();
|
|
824
|
+
this._bufferedBytes += chunk.length;
|
|
825
|
+
this._buffers.push(chunk);
|
|
826
|
+
this.startLoop(cb);
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Consumes `n` bytes from the buffered data.
|
|
830
|
+
*
|
|
831
|
+
* @param {Number} n The number of bytes to consume
|
|
832
|
+
* @return {Buffer} The consumed bytes
|
|
833
|
+
* @private
|
|
834
|
+
*/
|
|
835
|
+
consume(n) {
|
|
836
|
+
this._bufferedBytes -= n;
|
|
837
|
+
if (n === this._buffers[0].length)
|
|
838
|
+
return this._buffers.shift();
|
|
839
|
+
if (n < this._buffers[0].length) {
|
|
840
|
+
const buf = this._buffers[0];
|
|
841
|
+
this._buffers[0] = new FastBuffer(
|
|
842
|
+
buf.buffer,
|
|
843
|
+
buf.byteOffset + n,
|
|
844
|
+
buf.length - n
|
|
845
|
+
);
|
|
846
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
847
|
+
}
|
|
848
|
+
const dst = Buffer.allocUnsafe(n);
|
|
849
|
+
do {
|
|
850
|
+
const buf = this._buffers[0];
|
|
851
|
+
const offset = dst.length - n;
|
|
852
|
+
if (n >= buf.length) {
|
|
853
|
+
dst.set(this._buffers.shift(), offset);
|
|
854
|
+
} else {
|
|
855
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
856
|
+
this._buffers[0] = new FastBuffer(
|
|
857
|
+
buf.buffer,
|
|
858
|
+
buf.byteOffset + n,
|
|
859
|
+
buf.length - n
|
|
860
|
+
);
|
|
861
|
+
}
|
|
862
|
+
n -= buf.length;
|
|
863
|
+
} while (n > 0);
|
|
864
|
+
return dst;
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Starts the parsing loop.
|
|
868
|
+
*
|
|
869
|
+
* @param {Function} cb Callback
|
|
870
|
+
* @private
|
|
871
|
+
*/
|
|
872
|
+
startLoop(cb) {
|
|
873
|
+
this._loop = true;
|
|
874
|
+
do {
|
|
875
|
+
switch (this._state) {
|
|
876
|
+
case GET_INFO:
|
|
877
|
+
this.getInfo(cb);
|
|
878
|
+
break;
|
|
879
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
880
|
+
this.getPayloadLength16(cb);
|
|
881
|
+
break;
|
|
882
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
883
|
+
this.getPayloadLength64(cb);
|
|
884
|
+
break;
|
|
885
|
+
case GET_MASK:
|
|
886
|
+
this.getMask();
|
|
887
|
+
break;
|
|
888
|
+
case GET_DATA:
|
|
889
|
+
this.getData(cb);
|
|
890
|
+
break;
|
|
891
|
+
case INFLATING:
|
|
892
|
+
case DEFER_EVENT:
|
|
893
|
+
this._loop = false;
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
} while (this._loop);
|
|
897
|
+
if (!this._errored)
|
|
898
|
+
cb();
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Reads the first two bytes of a frame.
|
|
902
|
+
*
|
|
903
|
+
* @param {Function} cb Callback
|
|
904
|
+
* @private
|
|
905
|
+
*/
|
|
906
|
+
getInfo(cb) {
|
|
907
|
+
if (this._bufferedBytes < 2) {
|
|
908
|
+
this._loop = false;
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
const buf = this.consume(2);
|
|
912
|
+
if ((buf[0] & 48) !== 0) {
|
|
913
|
+
const error = this.createError(
|
|
914
|
+
RangeError,
|
|
915
|
+
"RSV2 and RSV3 must be clear",
|
|
916
|
+
true,
|
|
917
|
+
1002,
|
|
918
|
+
"WS_ERR_UNEXPECTED_RSV_2_3"
|
|
919
|
+
);
|
|
920
|
+
cb(error);
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
const compressed = (buf[0] & 64) === 64;
|
|
924
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
925
|
+
const error = this.createError(
|
|
926
|
+
RangeError,
|
|
927
|
+
"RSV1 must be clear",
|
|
928
|
+
true,
|
|
929
|
+
1002,
|
|
930
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
931
|
+
);
|
|
932
|
+
cb(error);
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
this._fin = (buf[0] & 128) === 128;
|
|
936
|
+
this._opcode = buf[0] & 15;
|
|
937
|
+
this._payloadLength = buf[1] & 127;
|
|
938
|
+
if (this._opcode === 0) {
|
|
939
|
+
if (compressed) {
|
|
940
|
+
const error = this.createError(
|
|
941
|
+
RangeError,
|
|
942
|
+
"RSV1 must be clear",
|
|
943
|
+
true,
|
|
944
|
+
1002,
|
|
945
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
946
|
+
);
|
|
947
|
+
cb(error);
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
if (!this._fragmented) {
|
|
951
|
+
const error = this.createError(
|
|
952
|
+
RangeError,
|
|
953
|
+
"invalid opcode 0",
|
|
954
|
+
true,
|
|
955
|
+
1002,
|
|
956
|
+
"WS_ERR_INVALID_OPCODE"
|
|
957
|
+
);
|
|
958
|
+
cb(error);
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
961
|
+
this._opcode = this._fragmented;
|
|
962
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
963
|
+
if (this._fragmented) {
|
|
964
|
+
const error = this.createError(
|
|
965
|
+
RangeError,
|
|
966
|
+
`invalid opcode ${this._opcode}`,
|
|
967
|
+
true,
|
|
968
|
+
1002,
|
|
969
|
+
"WS_ERR_INVALID_OPCODE"
|
|
970
|
+
);
|
|
971
|
+
cb(error);
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
this._compressed = compressed;
|
|
975
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
976
|
+
if (!this._fin) {
|
|
977
|
+
const error = this.createError(
|
|
978
|
+
RangeError,
|
|
979
|
+
"FIN must be set",
|
|
980
|
+
true,
|
|
981
|
+
1002,
|
|
982
|
+
"WS_ERR_EXPECTED_FIN"
|
|
983
|
+
);
|
|
984
|
+
cb(error);
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
if (compressed) {
|
|
988
|
+
const error = this.createError(
|
|
989
|
+
RangeError,
|
|
990
|
+
"RSV1 must be clear",
|
|
991
|
+
true,
|
|
992
|
+
1002,
|
|
993
|
+
"WS_ERR_UNEXPECTED_RSV_1"
|
|
994
|
+
);
|
|
995
|
+
cb(error);
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
999
|
+
const error = this.createError(
|
|
1000
|
+
RangeError,
|
|
1001
|
+
`invalid payload length ${this._payloadLength}`,
|
|
1002
|
+
true,
|
|
1003
|
+
1002,
|
|
1004
|
+
"WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH"
|
|
1005
|
+
);
|
|
1006
|
+
cb(error);
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
} else {
|
|
1010
|
+
const error = this.createError(
|
|
1011
|
+
RangeError,
|
|
1012
|
+
`invalid opcode ${this._opcode}`,
|
|
1013
|
+
true,
|
|
1014
|
+
1002,
|
|
1015
|
+
"WS_ERR_INVALID_OPCODE"
|
|
1016
|
+
);
|
|
1017
|
+
cb(error);
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
if (!this._fin && !this._fragmented)
|
|
1021
|
+
this._fragmented = this._opcode;
|
|
1022
|
+
this._masked = (buf[1] & 128) === 128;
|
|
1023
|
+
if (this._isServer) {
|
|
1024
|
+
if (!this._masked) {
|
|
1025
|
+
const error = this.createError(
|
|
1026
|
+
RangeError,
|
|
1027
|
+
"MASK must be set",
|
|
1028
|
+
true,
|
|
1029
|
+
1002,
|
|
1030
|
+
"WS_ERR_EXPECTED_MASK"
|
|
1031
|
+
);
|
|
1032
|
+
cb(error);
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
} else if (this._masked) {
|
|
1036
|
+
const error = this.createError(
|
|
1037
|
+
RangeError,
|
|
1038
|
+
"MASK must be clear",
|
|
1039
|
+
true,
|
|
1040
|
+
1002,
|
|
1041
|
+
"WS_ERR_UNEXPECTED_MASK"
|
|
1042
|
+
);
|
|
1043
|
+
cb(error);
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
if (this._payloadLength === 126)
|
|
1047
|
+
this._state = GET_PAYLOAD_LENGTH_16;
|
|
1048
|
+
else if (this._payloadLength === 127)
|
|
1049
|
+
this._state = GET_PAYLOAD_LENGTH_64;
|
|
1050
|
+
else
|
|
1051
|
+
this.haveLength(cb);
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Gets extended payload length (7+16).
|
|
1055
|
+
*
|
|
1056
|
+
* @param {Function} cb Callback
|
|
1057
|
+
* @private
|
|
1058
|
+
*/
|
|
1059
|
+
getPayloadLength16(cb) {
|
|
1060
|
+
if (this._bufferedBytes < 2) {
|
|
1061
|
+
this._loop = false;
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
1065
|
+
this.haveLength(cb);
|
|
1066
|
+
}
|
|
1067
|
+
/**
|
|
1068
|
+
* Gets extended payload length (7+64).
|
|
1069
|
+
*
|
|
1070
|
+
* @param {Function} cb Callback
|
|
1071
|
+
* @private
|
|
1072
|
+
*/
|
|
1073
|
+
getPayloadLength64(cb) {
|
|
1074
|
+
if (this._bufferedBytes < 8) {
|
|
1075
|
+
this._loop = false;
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
const buf = this.consume(8);
|
|
1079
|
+
const num = buf.readUInt32BE(0);
|
|
1080
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
1081
|
+
const error = this.createError(
|
|
1082
|
+
RangeError,
|
|
1083
|
+
"Unsupported WebSocket frame: payload length > 2^53 - 1",
|
|
1084
|
+
false,
|
|
1085
|
+
1009,
|
|
1086
|
+
"WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH"
|
|
1087
|
+
);
|
|
1088
|
+
cb(error);
|
|
1089
|
+
return;
|
|
1090
|
+
}
|
|
1091
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
1092
|
+
this.haveLength(cb);
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Payload length has been read.
|
|
1096
|
+
*
|
|
1097
|
+
* @param {Function} cb Callback
|
|
1098
|
+
* @private
|
|
1099
|
+
*/
|
|
1100
|
+
haveLength(cb) {
|
|
1101
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
1102
|
+
this._totalPayloadLength += this._payloadLength;
|
|
1103
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
1104
|
+
const error = this.createError(
|
|
1105
|
+
RangeError,
|
|
1106
|
+
"Max payload size exceeded",
|
|
1107
|
+
false,
|
|
1108
|
+
1009,
|
|
1109
|
+
"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
|
|
1110
|
+
);
|
|
1111
|
+
cb(error);
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
if (this._masked)
|
|
1116
|
+
this._state = GET_MASK;
|
|
1117
|
+
else
|
|
1118
|
+
this._state = GET_DATA;
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Reads mask bytes.
|
|
1122
|
+
*
|
|
1123
|
+
* @private
|
|
1124
|
+
*/
|
|
1125
|
+
getMask() {
|
|
1126
|
+
if (this._bufferedBytes < 4) {
|
|
1127
|
+
this._loop = false;
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
this._mask = this.consume(4);
|
|
1131
|
+
this._state = GET_DATA;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Reads data bytes.
|
|
1135
|
+
*
|
|
1136
|
+
* @param {Function} cb Callback
|
|
1137
|
+
* @private
|
|
1138
|
+
*/
|
|
1139
|
+
getData(cb) {
|
|
1140
|
+
let data = EMPTY_BUFFER;
|
|
1141
|
+
if (this._payloadLength) {
|
|
1142
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
1143
|
+
this._loop = false;
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
data = this.consume(this._payloadLength);
|
|
1147
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
1148
|
+
unmask(data, this._mask);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
if (this._opcode > 7) {
|
|
1152
|
+
this.controlMessage(data, cb);
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
if (this._compressed) {
|
|
1156
|
+
this._state = INFLATING;
|
|
1157
|
+
this.decompress(data, cb);
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
if (data.length) {
|
|
1161
|
+
this._messageLength = this._totalPayloadLength;
|
|
1162
|
+
this._fragments.push(data);
|
|
1163
|
+
}
|
|
1164
|
+
this.dataMessage(cb);
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* Decompresses data.
|
|
1168
|
+
*
|
|
1169
|
+
* @param {Buffer} data Compressed data
|
|
1170
|
+
* @param {Function} cb Callback
|
|
1171
|
+
* @private
|
|
1172
|
+
*/
|
|
1173
|
+
decompress(data, cb) {
|
|
1174
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1175
|
+
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
|
1176
|
+
if (err)
|
|
1177
|
+
return cb(err);
|
|
1178
|
+
if (buf.length) {
|
|
1179
|
+
this._messageLength += buf.length;
|
|
1180
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
1181
|
+
const error = this.createError(
|
|
1182
|
+
RangeError,
|
|
1183
|
+
"Max payload size exceeded",
|
|
1184
|
+
false,
|
|
1185
|
+
1009,
|
|
1186
|
+
"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
|
|
1187
|
+
);
|
|
1188
|
+
cb(error);
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
this._fragments.push(buf);
|
|
1192
|
+
}
|
|
1193
|
+
this.dataMessage(cb);
|
|
1194
|
+
if (this._state === GET_INFO)
|
|
1195
|
+
this.startLoop(cb);
|
|
1196
|
+
});
|
|
1197
|
+
}
|
|
1198
|
+
/**
|
|
1199
|
+
* Handles a data message.
|
|
1200
|
+
*
|
|
1201
|
+
* @param {Function} cb Callback
|
|
1202
|
+
* @private
|
|
1203
|
+
*/
|
|
1204
|
+
dataMessage(cb) {
|
|
1205
|
+
if (!this._fin) {
|
|
1206
|
+
this._state = GET_INFO;
|
|
1207
|
+
return;
|
|
1208
|
+
}
|
|
1209
|
+
const messageLength = this._messageLength;
|
|
1210
|
+
const fragments = this._fragments;
|
|
1211
|
+
this._totalPayloadLength = 0;
|
|
1212
|
+
this._messageLength = 0;
|
|
1213
|
+
this._fragmented = 0;
|
|
1214
|
+
this._fragments = [];
|
|
1215
|
+
if (this._opcode === 2) {
|
|
1216
|
+
let data;
|
|
1217
|
+
if (this._binaryType === "nodebuffer") {
|
|
1218
|
+
data = concat(fragments, messageLength);
|
|
1219
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
1220
|
+
data = toArrayBuffer(concat(fragments, messageLength));
|
|
1221
|
+
} else if (this._binaryType === "blob") {
|
|
1222
|
+
data = new Blob(fragments);
|
|
1223
|
+
} else {
|
|
1224
|
+
data = fragments;
|
|
1225
|
+
}
|
|
1226
|
+
if (this._allowSynchronousEvents) {
|
|
1227
|
+
this.emit("message", data, true);
|
|
1228
|
+
this._state = GET_INFO;
|
|
1229
|
+
} else {
|
|
1230
|
+
this._state = DEFER_EVENT;
|
|
1231
|
+
setImmediate(() => {
|
|
1232
|
+
this.emit("message", data, true);
|
|
1233
|
+
this._state = GET_INFO;
|
|
1234
|
+
this.startLoop(cb);
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
} else {
|
|
1238
|
+
const buf = concat(fragments, messageLength);
|
|
1239
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
1240
|
+
const error = this.createError(
|
|
1241
|
+
Error,
|
|
1242
|
+
"invalid UTF-8 sequence",
|
|
1243
|
+
true,
|
|
1244
|
+
1007,
|
|
1245
|
+
"WS_ERR_INVALID_UTF8"
|
|
1246
|
+
);
|
|
1247
|
+
cb(error);
|
|
1248
|
+
return;
|
|
1249
|
+
}
|
|
1250
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
1251
|
+
this.emit("message", buf, false);
|
|
1252
|
+
this._state = GET_INFO;
|
|
1253
|
+
} else {
|
|
1254
|
+
this._state = DEFER_EVENT;
|
|
1255
|
+
setImmediate(() => {
|
|
1256
|
+
this.emit("message", buf, false);
|
|
1257
|
+
this._state = GET_INFO;
|
|
1258
|
+
this.startLoop(cb);
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Handles a control message.
|
|
1265
|
+
*
|
|
1266
|
+
* @param {Buffer} data Data to handle
|
|
1267
|
+
* @return {(Error|RangeError|undefined)} A possible error
|
|
1268
|
+
* @private
|
|
1269
|
+
*/
|
|
1270
|
+
controlMessage(data, cb) {
|
|
1271
|
+
if (this._opcode === 8) {
|
|
1272
|
+
if (data.length === 0) {
|
|
1273
|
+
this._loop = false;
|
|
1274
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
1275
|
+
this.end();
|
|
1276
|
+
} else {
|
|
1277
|
+
const code = data.readUInt16BE(0);
|
|
1278
|
+
if (!isValidStatusCode(code)) {
|
|
1279
|
+
const error = this.createError(
|
|
1280
|
+
RangeError,
|
|
1281
|
+
`invalid status code ${code}`,
|
|
1282
|
+
true,
|
|
1283
|
+
1002,
|
|
1284
|
+
"WS_ERR_INVALID_CLOSE_CODE"
|
|
1285
|
+
);
|
|
1286
|
+
cb(error);
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
const buf = new FastBuffer(
|
|
1290
|
+
data.buffer,
|
|
1291
|
+
data.byteOffset + 2,
|
|
1292
|
+
data.length - 2
|
|
1293
|
+
);
|
|
1294
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
1295
|
+
const error = this.createError(
|
|
1296
|
+
Error,
|
|
1297
|
+
"invalid UTF-8 sequence",
|
|
1298
|
+
true,
|
|
1299
|
+
1007,
|
|
1300
|
+
"WS_ERR_INVALID_UTF8"
|
|
1301
|
+
);
|
|
1302
|
+
cb(error);
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
this._loop = false;
|
|
1306
|
+
this.emit("conclude", code, buf);
|
|
1307
|
+
this.end();
|
|
1308
|
+
}
|
|
1309
|
+
this._state = GET_INFO;
|
|
1310
|
+
return;
|
|
1311
|
+
}
|
|
1312
|
+
if (this._allowSynchronousEvents) {
|
|
1313
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
1314
|
+
this._state = GET_INFO;
|
|
1315
|
+
} else {
|
|
1316
|
+
this._state = DEFER_EVENT;
|
|
1317
|
+
setImmediate(() => {
|
|
1318
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
1319
|
+
this._state = GET_INFO;
|
|
1320
|
+
this.startLoop(cb);
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
/**
|
|
1325
|
+
* Builds an error object.
|
|
1326
|
+
*
|
|
1327
|
+
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
|
|
1328
|
+
* @param {String} message The error message
|
|
1329
|
+
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
|
|
1330
|
+
* `message`
|
|
1331
|
+
* @param {Number} statusCode The status code
|
|
1332
|
+
* @param {String} errorCode The exposed error code
|
|
1333
|
+
* @return {(Error|RangeError)} The error
|
|
1334
|
+
* @private
|
|
1335
|
+
*/
|
|
1336
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
1337
|
+
this._loop = false;
|
|
1338
|
+
this._errored = true;
|
|
1339
|
+
const err = new ErrorCtor(
|
|
1340
|
+
prefix ? `Invalid WebSocket frame: ${message}` : message
|
|
1341
|
+
);
|
|
1342
|
+
Error.captureStackTrace(err, this.createError);
|
|
1343
|
+
err.code = errorCode;
|
|
1344
|
+
err[kStatusCode] = statusCode;
|
|
1345
|
+
return err;
|
|
1346
|
+
}
|
|
1347
|
+
};
|
|
1348
|
+
module.exports = Receiver;
|
|
1349
|
+
}
|
|
1350
|
+
});
|
|
1351
|
+
|
|
1352
|
+
// ../../../node_modules/ws/lib/sender.js
|
|
1353
|
+
var require_sender = __commonJS({
|
|
1354
|
+
"../../../node_modules/ws/lib/sender.js"(exports$1, module) {
|
|
1355
|
+
var { Duplex } = __require("stream");
|
|
1356
|
+
var { randomFillSync } = __require("crypto");
|
|
1357
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
1358
|
+
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
|
|
1359
|
+
var { isBlob, isValidStatusCode } = require_validation();
|
|
1360
|
+
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
1361
|
+
var kByteLength = Symbol("kByteLength");
|
|
1362
|
+
var maskBuffer = Buffer.alloc(4);
|
|
1363
|
+
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
1364
|
+
var randomPool;
|
|
1365
|
+
var randomPoolPointer = RANDOM_POOL_SIZE;
|
|
1366
|
+
var DEFAULT = 0;
|
|
1367
|
+
var DEFLATING = 1;
|
|
1368
|
+
var GET_BLOB_DATA = 2;
|
|
1369
|
+
var Sender = class _Sender {
|
|
1370
|
+
/**
|
|
1371
|
+
* Creates a Sender instance.
|
|
1372
|
+
*
|
|
1373
|
+
* @param {Duplex} socket The connection socket
|
|
1374
|
+
* @param {Object} [extensions] An object containing the negotiated extensions
|
|
1375
|
+
* @param {Function} [generateMask] The function used to generate the masking
|
|
1376
|
+
* key
|
|
1377
|
+
*/
|
|
1378
|
+
constructor(socket, extensions, generateMask) {
|
|
1379
|
+
this._extensions = extensions || {};
|
|
1380
|
+
if (generateMask) {
|
|
1381
|
+
this._generateMask = generateMask;
|
|
1382
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
1383
|
+
}
|
|
1384
|
+
this._socket = socket;
|
|
1385
|
+
this._firstFragment = true;
|
|
1386
|
+
this._compress = false;
|
|
1387
|
+
this._bufferedBytes = 0;
|
|
1388
|
+
this._queue = [];
|
|
1389
|
+
this._state = DEFAULT;
|
|
1390
|
+
this.onerror = NOOP;
|
|
1391
|
+
this[kWebSocket] = void 0;
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Frames a piece of data according to the HyBi WebSocket protocol.
|
|
1395
|
+
*
|
|
1396
|
+
* @param {(Buffer|String)} data The data to frame
|
|
1397
|
+
* @param {Object} options Options object
|
|
1398
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
1399
|
+
* FIN bit
|
|
1400
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
1401
|
+
* masking key
|
|
1402
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1403
|
+
* `data`
|
|
1404
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
1405
|
+
* key
|
|
1406
|
+
* @param {Number} options.opcode The opcode
|
|
1407
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
1408
|
+
* modified
|
|
1409
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
1410
|
+
* RSV1 bit
|
|
1411
|
+
* @return {(Buffer|String)[]} The framed data
|
|
1412
|
+
* @public
|
|
1413
|
+
*/
|
|
1414
|
+
static frame(data, options) {
|
|
1415
|
+
let mask;
|
|
1416
|
+
let merge = false;
|
|
1417
|
+
let offset = 2;
|
|
1418
|
+
let skipMasking = false;
|
|
1419
|
+
if (options.mask) {
|
|
1420
|
+
mask = options.maskBuffer || maskBuffer;
|
|
1421
|
+
if (options.generateMask) {
|
|
1422
|
+
options.generateMask(mask);
|
|
1423
|
+
} else {
|
|
1424
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
1425
|
+
if (randomPool === void 0) {
|
|
1426
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
1427
|
+
}
|
|
1428
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
1429
|
+
randomPoolPointer = 0;
|
|
1430
|
+
}
|
|
1431
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
1432
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
1433
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
1434
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
1435
|
+
}
|
|
1436
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
1437
|
+
offset = 6;
|
|
1438
|
+
}
|
|
1439
|
+
let dataLength;
|
|
1440
|
+
if (typeof data === "string") {
|
|
1441
|
+
if ((!options.mask || skipMasking) && options[kByteLength] !== void 0) {
|
|
1442
|
+
dataLength = options[kByteLength];
|
|
1443
|
+
} else {
|
|
1444
|
+
data = Buffer.from(data);
|
|
1445
|
+
dataLength = data.length;
|
|
1446
|
+
}
|
|
1447
|
+
} else {
|
|
1448
|
+
dataLength = data.length;
|
|
1449
|
+
merge = options.mask && options.readOnly && !skipMasking;
|
|
1450
|
+
}
|
|
1451
|
+
let payloadLength = dataLength;
|
|
1452
|
+
if (dataLength >= 65536) {
|
|
1453
|
+
offset += 8;
|
|
1454
|
+
payloadLength = 127;
|
|
1455
|
+
} else if (dataLength > 125) {
|
|
1456
|
+
offset += 2;
|
|
1457
|
+
payloadLength = 126;
|
|
1458
|
+
}
|
|
1459
|
+
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
|
1460
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
1461
|
+
if (options.rsv1)
|
|
1462
|
+
target[0] |= 64;
|
|
1463
|
+
target[1] = payloadLength;
|
|
1464
|
+
if (payloadLength === 126) {
|
|
1465
|
+
target.writeUInt16BE(dataLength, 2);
|
|
1466
|
+
} else if (payloadLength === 127) {
|
|
1467
|
+
target[2] = target[3] = 0;
|
|
1468
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
1469
|
+
}
|
|
1470
|
+
if (!options.mask)
|
|
1471
|
+
return [target, data];
|
|
1472
|
+
target[1] |= 128;
|
|
1473
|
+
target[offset - 4] = mask[0];
|
|
1474
|
+
target[offset - 3] = mask[1];
|
|
1475
|
+
target[offset - 2] = mask[2];
|
|
1476
|
+
target[offset - 1] = mask[3];
|
|
1477
|
+
if (skipMasking)
|
|
1478
|
+
return [target, data];
|
|
1479
|
+
if (merge) {
|
|
1480
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
1481
|
+
return [target];
|
|
1482
|
+
}
|
|
1483
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
1484
|
+
return [target, data];
|
|
1485
|
+
}
|
|
1486
|
+
/**
|
|
1487
|
+
* Sends a close message to the other peer.
|
|
1488
|
+
*
|
|
1489
|
+
* @param {Number} [code] The status code component of the body
|
|
1490
|
+
* @param {(String|Buffer)} [data] The message component of the body
|
|
1491
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
|
|
1492
|
+
* @param {Function} [cb] Callback
|
|
1493
|
+
* @public
|
|
1494
|
+
*/
|
|
1495
|
+
close(code, data, mask, cb) {
|
|
1496
|
+
let buf;
|
|
1497
|
+
if (code === void 0) {
|
|
1498
|
+
buf = EMPTY_BUFFER;
|
|
1499
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
1500
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
1501
|
+
} else if (data === void 0 || !data.length) {
|
|
1502
|
+
buf = Buffer.allocUnsafe(2);
|
|
1503
|
+
buf.writeUInt16BE(code, 0);
|
|
1504
|
+
} else {
|
|
1505
|
+
const length = Buffer.byteLength(data);
|
|
1506
|
+
if (length > 123) {
|
|
1507
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
1508
|
+
}
|
|
1509
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
1510
|
+
buf.writeUInt16BE(code, 0);
|
|
1511
|
+
if (typeof data === "string") {
|
|
1512
|
+
buf.write(data, 2);
|
|
1513
|
+
} else {
|
|
1514
|
+
buf.set(data, 2);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
const options = {
|
|
1518
|
+
[kByteLength]: buf.length,
|
|
1519
|
+
fin: true,
|
|
1520
|
+
generateMask: this._generateMask,
|
|
1521
|
+
mask,
|
|
1522
|
+
maskBuffer: this._maskBuffer,
|
|
1523
|
+
opcode: 8,
|
|
1524
|
+
readOnly: false,
|
|
1525
|
+
rsv1: false
|
|
1526
|
+
};
|
|
1527
|
+
if (this._state !== DEFAULT) {
|
|
1528
|
+
this.enqueue([this.dispatch, buf, false, options, cb]);
|
|
1529
|
+
} else {
|
|
1530
|
+
this.sendFrame(_Sender.frame(buf, options), cb);
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
/**
|
|
1534
|
+
* Sends a ping message to the other peer.
|
|
1535
|
+
*
|
|
1536
|
+
* @param {*} data The message to send
|
|
1537
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
|
1538
|
+
* @param {Function} [cb] Callback
|
|
1539
|
+
* @public
|
|
1540
|
+
*/
|
|
1541
|
+
ping(data, mask, cb) {
|
|
1542
|
+
let byteLength;
|
|
1543
|
+
let readOnly;
|
|
1544
|
+
if (typeof data === "string") {
|
|
1545
|
+
byteLength = Buffer.byteLength(data);
|
|
1546
|
+
readOnly = false;
|
|
1547
|
+
} else if (isBlob(data)) {
|
|
1548
|
+
byteLength = data.size;
|
|
1549
|
+
readOnly = false;
|
|
1550
|
+
} else {
|
|
1551
|
+
data = toBuffer(data);
|
|
1552
|
+
byteLength = data.length;
|
|
1553
|
+
readOnly = toBuffer.readOnly;
|
|
1554
|
+
}
|
|
1555
|
+
if (byteLength > 125) {
|
|
1556
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
1557
|
+
}
|
|
1558
|
+
const options = {
|
|
1559
|
+
[kByteLength]: byteLength,
|
|
1560
|
+
fin: true,
|
|
1561
|
+
generateMask: this._generateMask,
|
|
1562
|
+
mask,
|
|
1563
|
+
maskBuffer: this._maskBuffer,
|
|
1564
|
+
opcode: 9,
|
|
1565
|
+
readOnly,
|
|
1566
|
+
rsv1: false
|
|
1567
|
+
};
|
|
1568
|
+
if (isBlob(data)) {
|
|
1569
|
+
if (this._state !== DEFAULT) {
|
|
1570
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
1571
|
+
} else {
|
|
1572
|
+
this.getBlobData(data, false, options, cb);
|
|
1573
|
+
}
|
|
1574
|
+
} else if (this._state !== DEFAULT) {
|
|
1575
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
1576
|
+
} else {
|
|
1577
|
+
this.sendFrame(_Sender.frame(data, options), cb);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Sends a pong message to the other peer.
|
|
1582
|
+
*
|
|
1583
|
+
* @param {*} data The message to send
|
|
1584
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
|
1585
|
+
* @param {Function} [cb] Callback
|
|
1586
|
+
* @public
|
|
1587
|
+
*/
|
|
1588
|
+
pong(data, mask, cb) {
|
|
1589
|
+
let byteLength;
|
|
1590
|
+
let readOnly;
|
|
1591
|
+
if (typeof data === "string") {
|
|
1592
|
+
byteLength = Buffer.byteLength(data);
|
|
1593
|
+
readOnly = false;
|
|
1594
|
+
} else if (isBlob(data)) {
|
|
1595
|
+
byteLength = data.size;
|
|
1596
|
+
readOnly = false;
|
|
1597
|
+
} else {
|
|
1598
|
+
data = toBuffer(data);
|
|
1599
|
+
byteLength = data.length;
|
|
1600
|
+
readOnly = toBuffer.readOnly;
|
|
1601
|
+
}
|
|
1602
|
+
if (byteLength > 125) {
|
|
1603
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
1604
|
+
}
|
|
1605
|
+
const options = {
|
|
1606
|
+
[kByteLength]: byteLength,
|
|
1607
|
+
fin: true,
|
|
1608
|
+
generateMask: this._generateMask,
|
|
1609
|
+
mask,
|
|
1610
|
+
maskBuffer: this._maskBuffer,
|
|
1611
|
+
opcode: 10,
|
|
1612
|
+
readOnly,
|
|
1613
|
+
rsv1: false
|
|
1614
|
+
};
|
|
1615
|
+
if (isBlob(data)) {
|
|
1616
|
+
if (this._state !== DEFAULT) {
|
|
1617
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
1618
|
+
} else {
|
|
1619
|
+
this.getBlobData(data, false, options, cb);
|
|
1620
|
+
}
|
|
1621
|
+
} else if (this._state !== DEFAULT) {
|
|
1622
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
1623
|
+
} else {
|
|
1624
|
+
this.sendFrame(_Sender.frame(data, options), cb);
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
/**
|
|
1628
|
+
* Sends a data message to the other peer.
|
|
1629
|
+
*
|
|
1630
|
+
* @param {*} data The message to send
|
|
1631
|
+
* @param {Object} options Options object
|
|
1632
|
+
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
|
|
1633
|
+
* or text
|
|
1634
|
+
* @param {Boolean} [options.compress=false] Specifies whether or not to
|
|
1635
|
+
* compress `data`
|
|
1636
|
+
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
|
|
1637
|
+
* last one
|
|
1638
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1639
|
+
* `data`
|
|
1640
|
+
* @param {Function} [cb] Callback
|
|
1641
|
+
* @public
|
|
1642
|
+
*/
|
|
1643
|
+
send(data, options, cb) {
|
|
1644
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1645
|
+
let opcode = options.binary ? 2 : 1;
|
|
1646
|
+
let rsv1 = options.compress;
|
|
1647
|
+
let byteLength;
|
|
1648
|
+
let readOnly;
|
|
1649
|
+
if (typeof data === "string") {
|
|
1650
|
+
byteLength = Buffer.byteLength(data);
|
|
1651
|
+
readOnly = false;
|
|
1652
|
+
} else if (isBlob(data)) {
|
|
1653
|
+
byteLength = data.size;
|
|
1654
|
+
readOnly = false;
|
|
1655
|
+
} else {
|
|
1656
|
+
data = toBuffer(data);
|
|
1657
|
+
byteLength = data.length;
|
|
1658
|
+
readOnly = toBuffer.readOnly;
|
|
1659
|
+
}
|
|
1660
|
+
if (this._firstFragment) {
|
|
1661
|
+
this._firstFragment = false;
|
|
1662
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
1663
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
1664
|
+
}
|
|
1665
|
+
this._compress = rsv1;
|
|
1666
|
+
} else {
|
|
1667
|
+
rsv1 = false;
|
|
1668
|
+
opcode = 0;
|
|
1669
|
+
}
|
|
1670
|
+
if (options.fin)
|
|
1671
|
+
this._firstFragment = true;
|
|
1672
|
+
const opts = {
|
|
1673
|
+
[kByteLength]: byteLength,
|
|
1674
|
+
fin: options.fin,
|
|
1675
|
+
generateMask: this._generateMask,
|
|
1676
|
+
mask: options.mask,
|
|
1677
|
+
maskBuffer: this._maskBuffer,
|
|
1678
|
+
opcode,
|
|
1679
|
+
readOnly,
|
|
1680
|
+
rsv1
|
|
1681
|
+
};
|
|
1682
|
+
if (isBlob(data)) {
|
|
1683
|
+
if (this._state !== DEFAULT) {
|
|
1684
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
1685
|
+
} else {
|
|
1686
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
1687
|
+
}
|
|
1688
|
+
} else if (this._state !== DEFAULT) {
|
|
1689
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
1690
|
+
} else {
|
|
1691
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* Gets the contents of a blob as binary data.
|
|
1696
|
+
*
|
|
1697
|
+
* @param {Blob} blob The blob
|
|
1698
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
|
1699
|
+
* the data
|
|
1700
|
+
* @param {Object} options Options object
|
|
1701
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
1702
|
+
* FIN bit
|
|
1703
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
1704
|
+
* masking key
|
|
1705
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1706
|
+
* `data`
|
|
1707
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
1708
|
+
* key
|
|
1709
|
+
* @param {Number} options.opcode The opcode
|
|
1710
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
1711
|
+
* modified
|
|
1712
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
1713
|
+
* RSV1 bit
|
|
1714
|
+
* @param {Function} [cb] Callback
|
|
1715
|
+
* @private
|
|
1716
|
+
*/
|
|
1717
|
+
getBlobData(blob, compress, options, cb) {
|
|
1718
|
+
this._bufferedBytes += options[kByteLength];
|
|
1719
|
+
this._state = GET_BLOB_DATA;
|
|
1720
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
1721
|
+
if (this._socket.destroyed) {
|
|
1722
|
+
const err = new Error(
|
|
1723
|
+
"The socket was closed while the blob was being read"
|
|
1724
|
+
);
|
|
1725
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
1726
|
+
return;
|
|
1727
|
+
}
|
|
1728
|
+
this._bufferedBytes -= options[kByteLength];
|
|
1729
|
+
const data = toBuffer(arrayBuffer);
|
|
1730
|
+
if (!compress) {
|
|
1731
|
+
this._state = DEFAULT;
|
|
1732
|
+
this.sendFrame(_Sender.frame(data, options), cb);
|
|
1733
|
+
this.dequeue();
|
|
1734
|
+
} else {
|
|
1735
|
+
this.dispatch(data, compress, options, cb);
|
|
1736
|
+
}
|
|
1737
|
+
}).catch((err) => {
|
|
1738
|
+
process.nextTick(onError, this, err, cb);
|
|
1739
|
+
});
|
|
1740
|
+
}
|
|
1741
|
+
/**
|
|
1742
|
+
* Dispatches a message.
|
|
1743
|
+
*
|
|
1744
|
+
* @param {(Buffer|String)} data The message to send
|
|
1745
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
|
1746
|
+
* `data`
|
|
1747
|
+
* @param {Object} options Options object
|
|
1748
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
|
1749
|
+
* FIN bit
|
|
1750
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
1751
|
+
* masking key
|
|
1752
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
|
1753
|
+
* `data`
|
|
1754
|
+
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
|
1755
|
+
* key
|
|
1756
|
+
* @param {Number} options.opcode The opcode
|
|
1757
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
|
1758
|
+
* modified
|
|
1759
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
|
1760
|
+
* RSV1 bit
|
|
1761
|
+
* @param {Function} [cb] Callback
|
|
1762
|
+
* @private
|
|
1763
|
+
*/
|
|
1764
|
+
dispatch(data, compress, options, cb) {
|
|
1765
|
+
if (!compress) {
|
|
1766
|
+
this.sendFrame(_Sender.frame(data, options), cb);
|
|
1767
|
+
return;
|
|
1768
|
+
}
|
|
1769
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1770
|
+
this._bufferedBytes += options[kByteLength];
|
|
1771
|
+
this._state = DEFLATING;
|
|
1772
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
|
1773
|
+
if (this._socket.destroyed) {
|
|
1774
|
+
const err = new Error(
|
|
1775
|
+
"The socket was closed while data was being compressed"
|
|
1776
|
+
);
|
|
1777
|
+
callCallbacks(this, err, cb);
|
|
1778
|
+
return;
|
|
1779
|
+
}
|
|
1780
|
+
this._bufferedBytes -= options[kByteLength];
|
|
1781
|
+
this._state = DEFAULT;
|
|
1782
|
+
options.readOnly = false;
|
|
1783
|
+
this.sendFrame(_Sender.frame(buf, options), cb);
|
|
1784
|
+
this.dequeue();
|
|
1785
|
+
});
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* Executes queued send operations.
|
|
1789
|
+
*
|
|
1790
|
+
* @private
|
|
1791
|
+
*/
|
|
1792
|
+
dequeue() {
|
|
1793
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
1794
|
+
const params = this._queue.shift();
|
|
1795
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
1796
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
/**
|
|
1800
|
+
* Enqueues a send operation.
|
|
1801
|
+
*
|
|
1802
|
+
* @param {Array} params Send operation parameters.
|
|
1803
|
+
* @private
|
|
1804
|
+
*/
|
|
1805
|
+
enqueue(params) {
|
|
1806
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
1807
|
+
this._queue.push(params);
|
|
1808
|
+
}
|
|
1809
|
+
/**
|
|
1810
|
+
* Sends a frame.
|
|
1811
|
+
*
|
|
1812
|
+
* @param {(Buffer | String)[]} list The frame to send
|
|
1813
|
+
* @param {Function} [cb] Callback
|
|
1814
|
+
* @private
|
|
1815
|
+
*/
|
|
1816
|
+
sendFrame(list, cb) {
|
|
1817
|
+
if (list.length === 2) {
|
|
1818
|
+
this._socket.cork();
|
|
1819
|
+
this._socket.write(list[0]);
|
|
1820
|
+
this._socket.write(list[1], cb);
|
|
1821
|
+
this._socket.uncork();
|
|
1822
|
+
} else {
|
|
1823
|
+
this._socket.write(list[0], cb);
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
};
|
|
1827
|
+
module.exports = Sender;
|
|
1828
|
+
function callCallbacks(sender, err, cb) {
|
|
1829
|
+
if (typeof cb === "function")
|
|
1830
|
+
cb(err);
|
|
1831
|
+
for (let i = 0; i < sender._queue.length; i++) {
|
|
1832
|
+
const params = sender._queue[i];
|
|
1833
|
+
const callback = params[params.length - 1];
|
|
1834
|
+
if (typeof callback === "function")
|
|
1835
|
+
callback(err);
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
function onError(sender, err, cb) {
|
|
1839
|
+
callCallbacks(sender, err, cb);
|
|
1840
|
+
sender.onerror(err);
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
});
|
|
1844
|
+
|
|
1845
|
+
// ../../../node_modules/ws/lib/event-target.js
|
|
1846
|
+
var require_event_target = __commonJS({
|
|
1847
|
+
"../../../node_modules/ws/lib/event-target.js"(exports$1, module) {
|
|
1848
|
+
var { kForOnEventAttribute, kListener } = require_constants();
|
|
1849
|
+
var kCode = Symbol("kCode");
|
|
1850
|
+
var kData = Symbol("kData");
|
|
1851
|
+
var kError = Symbol("kError");
|
|
1852
|
+
var kMessage = Symbol("kMessage");
|
|
1853
|
+
var kReason = Symbol("kReason");
|
|
1854
|
+
var kTarget = Symbol("kTarget");
|
|
1855
|
+
var kType = Symbol("kType");
|
|
1856
|
+
var kWasClean = Symbol("kWasClean");
|
|
1857
|
+
var Event = class {
|
|
1858
|
+
/**
|
|
1859
|
+
* Create a new `Event`.
|
|
1860
|
+
*
|
|
1861
|
+
* @param {String} type The name of the event
|
|
1862
|
+
* @throws {TypeError} If the `type` argument is not specified
|
|
1863
|
+
*/
|
|
1864
|
+
constructor(type) {
|
|
1865
|
+
this[kTarget] = null;
|
|
1866
|
+
this[kType] = type;
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* @type {*}
|
|
1870
|
+
*/
|
|
1871
|
+
get target() {
|
|
1872
|
+
return this[kTarget];
|
|
1873
|
+
}
|
|
1874
|
+
/**
|
|
1875
|
+
* @type {String}
|
|
1876
|
+
*/
|
|
1877
|
+
get type() {
|
|
1878
|
+
return this[kType];
|
|
1879
|
+
}
|
|
1880
|
+
};
|
|
1881
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
1882
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
1883
|
+
var CloseEvent = class extends Event {
|
|
1884
|
+
/**
|
|
1885
|
+
* Create a new `CloseEvent`.
|
|
1886
|
+
*
|
|
1887
|
+
* @param {String} type The name of the event
|
|
1888
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
1889
|
+
* attributes via object members of the same name
|
|
1890
|
+
* @param {Number} [options.code=0] The status code explaining why the
|
|
1891
|
+
* connection was closed
|
|
1892
|
+
* @param {String} [options.reason=''] A human-readable string explaining why
|
|
1893
|
+
* the connection was closed
|
|
1894
|
+
* @param {Boolean} [options.wasClean=false] Indicates whether or not the
|
|
1895
|
+
* connection was cleanly closed
|
|
1896
|
+
*/
|
|
1897
|
+
constructor(type, options = {}) {
|
|
1898
|
+
super(type);
|
|
1899
|
+
this[kCode] = options.code === void 0 ? 0 : options.code;
|
|
1900
|
+
this[kReason] = options.reason === void 0 ? "" : options.reason;
|
|
1901
|
+
this[kWasClean] = options.wasClean === void 0 ? false : options.wasClean;
|
|
1902
|
+
}
|
|
1903
|
+
/**
|
|
1904
|
+
* @type {Number}
|
|
1905
|
+
*/
|
|
1906
|
+
get code() {
|
|
1907
|
+
return this[kCode];
|
|
1908
|
+
}
|
|
1909
|
+
/**
|
|
1910
|
+
* @type {String}
|
|
1911
|
+
*/
|
|
1912
|
+
get reason() {
|
|
1913
|
+
return this[kReason];
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* @type {Boolean}
|
|
1917
|
+
*/
|
|
1918
|
+
get wasClean() {
|
|
1919
|
+
return this[kWasClean];
|
|
1920
|
+
}
|
|
1921
|
+
};
|
|
1922
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
1923
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
1924
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
1925
|
+
var ErrorEvent = class extends Event {
|
|
1926
|
+
/**
|
|
1927
|
+
* Create a new `ErrorEvent`.
|
|
1928
|
+
*
|
|
1929
|
+
* @param {String} type The name of the event
|
|
1930
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
1931
|
+
* attributes via object members of the same name
|
|
1932
|
+
* @param {*} [options.error=null] The error that generated this event
|
|
1933
|
+
* @param {String} [options.message=''] The error message
|
|
1934
|
+
*/
|
|
1935
|
+
constructor(type, options = {}) {
|
|
1936
|
+
super(type);
|
|
1937
|
+
this[kError] = options.error === void 0 ? null : options.error;
|
|
1938
|
+
this[kMessage] = options.message === void 0 ? "" : options.message;
|
|
1939
|
+
}
|
|
1940
|
+
/**
|
|
1941
|
+
* @type {*}
|
|
1942
|
+
*/
|
|
1943
|
+
get error() {
|
|
1944
|
+
return this[kError];
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* @type {String}
|
|
1948
|
+
*/
|
|
1949
|
+
get message() {
|
|
1950
|
+
return this[kMessage];
|
|
1951
|
+
}
|
|
1952
|
+
};
|
|
1953
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
1954
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
1955
|
+
var MessageEvent = class extends Event {
|
|
1956
|
+
/**
|
|
1957
|
+
* Create a new `MessageEvent`.
|
|
1958
|
+
*
|
|
1959
|
+
* @param {String} type The name of the event
|
|
1960
|
+
* @param {Object} [options] A dictionary object that allows for setting
|
|
1961
|
+
* attributes via object members of the same name
|
|
1962
|
+
* @param {*} [options.data=null] The message content
|
|
1963
|
+
*/
|
|
1964
|
+
constructor(type, options = {}) {
|
|
1965
|
+
super(type);
|
|
1966
|
+
this[kData] = options.data === void 0 ? null : options.data;
|
|
1967
|
+
}
|
|
1968
|
+
/**
|
|
1969
|
+
* @type {*}
|
|
1970
|
+
*/
|
|
1971
|
+
get data() {
|
|
1972
|
+
return this[kData];
|
|
1973
|
+
}
|
|
1974
|
+
};
|
|
1975
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
1976
|
+
var EventTarget = {
|
|
1977
|
+
/**
|
|
1978
|
+
* Register an event listener.
|
|
1979
|
+
*
|
|
1980
|
+
* @param {String} type A string representing the event type to listen for
|
|
1981
|
+
* @param {(Function|Object)} handler The listener to add
|
|
1982
|
+
* @param {Object} [options] An options object specifies characteristics about
|
|
1983
|
+
* the event listener
|
|
1984
|
+
* @param {Boolean} [options.once=false] A `Boolean` indicating that the
|
|
1985
|
+
* listener should be invoked at most once after being added. If `true`,
|
|
1986
|
+
* the listener would be automatically removed when invoked.
|
|
1987
|
+
* @public
|
|
1988
|
+
*/
|
|
1989
|
+
addEventListener(type, handler, options = {}) {
|
|
1990
|
+
for (const listener of this.listeners(type)) {
|
|
1991
|
+
if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
1992
|
+
return;
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
let wrapper;
|
|
1996
|
+
if (type === "message") {
|
|
1997
|
+
wrapper = function onMessage(data, isBinary) {
|
|
1998
|
+
const event = new MessageEvent("message", {
|
|
1999
|
+
data: isBinary ? data : data.toString()
|
|
2000
|
+
});
|
|
2001
|
+
event[kTarget] = this;
|
|
2002
|
+
callListener(handler, this, event);
|
|
2003
|
+
};
|
|
2004
|
+
} else if (type === "close") {
|
|
2005
|
+
wrapper = function onClose(code, message) {
|
|
2006
|
+
const event = new CloseEvent("close", {
|
|
2007
|
+
code,
|
|
2008
|
+
reason: message.toString(),
|
|
2009
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
2010
|
+
});
|
|
2011
|
+
event[kTarget] = this;
|
|
2012
|
+
callListener(handler, this, event);
|
|
2013
|
+
};
|
|
2014
|
+
} else if (type === "error") {
|
|
2015
|
+
wrapper = function onError(error) {
|
|
2016
|
+
const event = new ErrorEvent("error", {
|
|
2017
|
+
error,
|
|
2018
|
+
message: error.message
|
|
2019
|
+
});
|
|
2020
|
+
event[kTarget] = this;
|
|
2021
|
+
callListener(handler, this, event);
|
|
2022
|
+
};
|
|
2023
|
+
} else if (type === "open") {
|
|
2024
|
+
wrapper = function onOpen() {
|
|
2025
|
+
const event = new Event("open");
|
|
2026
|
+
event[kTarget] = this;
|
|
2027
|
+
callListener(handler, this, event);
|
|
2028
|
+
};
|
|
2029
|
+
} else {
|
|
2030
|
+
return;
|
|
2031
|
+
}
|
|
2032
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
2033
|
+
wrapper[kListener] = handler;
|
|
2034
|
+
if (options.once) {
|
|
2035
|
+
this.once(type, wrapper);
|
|
2036
|
+
} else {
|
|
2037
|
+
this.on(type, wrapper);
|
|
2038
|
+
}
|
|
2039
|
+
},
|
|
2040
|
+
/**
|
|
2041
|
+
* Remove an event listener.
|
|
2042
|
+
*
|
|
2043
|
+
* @param {String} type A string representing the event type to remove
|
|
2044
|
+
* @param {(Function|Object)} handler The listener to remove
|
|
2045
|
+
* @public
|
|
2046
|
+
*/
|
|
2047
|
+
removeEventListener(type, handler) {
|
|
2048
|
+
for (const listener of this.listeners(type)) {
|
|
2049
|
+
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
2050
|
+
this.removeListener(type, listener);
|
|
2051
|
+
break;
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
};
|
|
2056
|
+
module.exports = {
|
|
2057
|
+
CloseEvent,
|
|
2058
|
+
ErrorEvent,
|
|
2059
|
+
Event,
|
|
2060
|
+
EventTarget,
|
|
2061
|
+
MessageEvent
|
|
2062
|
+
};
|
|
2063
|
+
function callListener(listener, thisArg, event) {
|
|
2064
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
2065
|
+
listener.handleEvent.call(listener, event);
|
|
2066
|
+
} else {
|
|
2067
|
+
listener.call(thisArg, event);
|
|
2068
|
+
}
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
});
|
|
2072
|
+
|
|
2073
|
+
// ../../../node_modules/ws/lib/extension.js
|
|
2074
|
+
var require_extension = __commonJS({
|
|
2075
|
+
"../../../node_modules/ws/lib/extension.js"(exports$1, module) {
|
|
2076
|
+
var { tokenChars } = require_validation();
|
|
2077
|
+
function push(dest, name, elem) {
|
|
2078
|
+
if (dest[name] === void 0)
|
|
2079
|
+
dest[name] = [elem];
|
|
2080
|
+
else
|
|
2081
|
+
dest[name].push(elem);
|
|
2082
|
+
}
|
|
2083
|
+
function parse(header) {
|
|
2084
|
+
const offers = /* @__PURE__ */ Object.create(null);
|
|
2085
|
+
let params = /* @__PURE__ */ Object.create(null);
|
|
2086
|
+
let mustUnescape = false;
|
|
2087
|
+
let isEscaping = false;
|
|
2088
|
+
let inQuotes = false;
|
|
2089
|
+
let extensionName;
|
|
2090
|
+
let paramName;
|
|
2091
|
+
let start = -1;
|
|
2092
|
+
let code = -1;
|
|
2093
|
+
let end = -1;
|
|
2094
|
+
let i = 0;
|
|
2095
|
+
for (; i < header.length; i++) {
|
|
2096
|
+
code = header.charCodeAt(i);
|
|
2097
|
+
if (extensionName === void 0) {
|
|
2098
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
2099
|
+
if (start === -1)
|
|
2100
|
+
start = i;
|
|
2101
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
2102
|
+
if (end === -1 && start !== -1)
|
|
2103
|
+
end = i;
|
|
2104
|
+
} else if (code === 59 || code === 44) {
|
|
2105
|
+
if (start === -1) {
|
|
2106
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2107
|
+
}
|
|
2108
|
+
if (end === -1)
|
|
2109
|
+
end = i;
|
|
2110
|
+
const name = header.slice(start, end);
|
|
2111
|
+
if (code === 44) {
|
|
2112
|
+
push(offers, name, params);
|
|
2113
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2114
|
+
} else {
|
|
2115
|
+
extensionName = name;
|
|
2116
|
+
}
|
|
2117
|
+
start = end = -1;
|
|
2118
|
+
} else {
|
|
2119
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2120
|
+
}
|
|
2121
|
+
} else if (paramName === void 0) {
|
|
2122
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
2123
|
+
if (start === -1)
|
|
2124
|
+
start = i;
|
|
2125
|
+
} else if (code === 32 || code === 9) {
|
|
2126
|
+
if (end === -1 && start !== -1)
|
|
2127
|
+
end = i;
|
|
2128
|
+
} else if (code === 59 || code === 44) {
|
|
2129
|
+
if (start === -1) {
|
|
2130
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2131
|
+
}
|
|
2132
|
+
if (end === -1)
|
|
2133
|
+
end = i;
|
|
2134
|
+
push(params, header.slice(start, end), true);
|
|
2135
|
+
if (code === 44) {
|
|
2136
|
+
push(offers, extensionName, params);
|
|
2137
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2138
|
+
extensionName = void 0;
|
|
2139
|
+
}
|
|
2140
|
+
start = end = -1;
|
|
2141
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
2142
|
+
paramName = header.slice(start, i);
|
|
2143
|
+
start = end = -1;
|
|
2144
|
+
} else {
|
|
2145
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2146
|
+
}
|
|
2147
|
+
} else {
|
|
2148
|
+
if (isEscaping) {
|
|
2149
|
+
if (tokenChars[code] !== 1) {
|
|
2150
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2151
|
+
}
|
|
2152
|
+
if (start === -1)
|
|
2153
|
+
start = i;
|
|
2154
|
+
else if (!mustUnescape)
|
|
2155
|
+
mustUnescape = true;
|
|
2156
|
+
isEscaping = false;
|
|
2157
|
+
} else if (inQuotes) {
|
|
2158
|
+
if (tokenChars[code] === 1) {
|
|
2159
|
+
if (start === -1)
|
|
2160
|
+
start = i;
|
|
2161
|
+
} else if (code === 34 && start !== -1) {
|
|
2162
|
+
inQuotes = false;
|
|
2163
|
+
end = i;
|
|
2164
|
+
} else if (code === 92) {
|
|
2165
|
+
isEscaping = true;
|
|
2166
|
+
} else {
|
|
2167
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2168
|
+
}
|
|
2169
|
+
} else if (code === 34 && header.charCodeAt(i - 1) === 61) {
|
|
2170
|
+
inQuotes = true;
|
|
2171
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
2172
|
+
if (start === -1)
|
|
2173
|
+
start = i;
|
|
2174
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
2175
|
+
if (end === -1)
|
|
2176
|
+
end = i;
|
|
2177
|
+
} else if (code === 59 || code === 44) {
|
|
2178
|
+
if (start === -1) {
|
|
2179
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2180
|
+
}
|
|
2181
|
+
if (end === -1)
|
|
2182
|
+
end = i;
|
|
2183
|
+
let value = header.slice(start, end);
|
|
2184
|
+
if (mustUnescape) {
|
|
2185
|
+
value = value.replace(/\\/g, "");
|
|
2186
|
+
mustUnescape = false;
|
|
2187
|
+
}
|
|
2188
|
+
push(params, paramName, value);
|
|
2189
|
+
if (code === 44) {
|
|
2190
|
+
push(offers, extensionName, params);
|
|
2191
|
+
params = /* @__PURE__ */ Object.create(null);
|
|
2192
|
+
extensionName = void 0;
|
|
2193
|
+
}
|
|
2194
|
+
paramName = void 0;
|
|
2195
|
+
start = end = -1;
|
|
2196
|
+
} else {
|
|
2197
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) {
|
|
2202
|
+
throw new SyntaxError("Unexpected end of input");
|
|
2203
|
+
}
|
|
2204
|
+
if (end === -1)
|
|
2205
|
+
end = i;
|
|
2206
|
+
const token = header.slice(start, end);
|
|
2207
|
+
if (extensionName === void 0) {
|
|
2208
|
+
push(offers, token, params);
|
|
2209
|
+
} else {
|
|
2210
|
+
if (paramName === void 0) {
|
|
2211
|
+
push(params, token, true);
|
|
2212
|
+
} else if (mustUnescape) {
|
|
2213
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
2214
|
+
} else {
|
|
2215
|
+
push(params, paramName, token);
|
|
2216
|
+
}
|
|
2217
|
+
push(offers, extensionName, params);
|
|
2218
|
+
}
|
|
2219
|
+
return offers;
|
|
2220
|
+
}
|
|
2221
|
+
function format(extensions) {
|
|
2222
|
+
return Object.keys(extensions).map((extension) => {
|
|
2223
|
+
let configurations = extensions[extension];
|
|
2224
|
+
if (!Array.isArray(configurations))
|
|
2225
|
+
configurations = [configurations];
|
|
2226
|
+
return configurations.map((params) => {
|
|
2227
|
+
return [extension].concat(
|
|
2228
|
+
Object.keys(params).map((k) => {
|
|
2229
|
+
let values = params[k];
|
|
2230
|
+
if (!Array.isArray(values))
|
|
2231
|
+
values = [values];
|
|
2232
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
2233
|
+
})
|
|
2234
|
+
).join("; ");
|
|
2235
|
+
}).join(", ");
|
|
2236
|
+
}).join(", ");
|
|
2237
|
+
}
|
|
2238
|
+
module.exports = { format, parse };
|
|
2239
|
+
}
|
|
2240
|
+
});
|
|
2241
|
+
|
|
2242
|
+
// ../../../node_modules/ws/lib/websocket.js
|
|
2243
|
+
var require_websocket = __commonJS({
|
|
2244
|
+
"../../../node_modules/ws/lib/websocket.js"(exports$1, module) {
|
|
2245
|
+
var EventEmitter = __require("events");
|
|
2246
|
+
var https = __require("https");
|
|
2247
|
+
var http = __require("http");
|
|
2248
|
+
var net = __require("net");
|
|
2249
|
+
var tls = __require("tls");
|
|
2250
|
+
var { randomBytes, createHash } = __require("crypto");
|
|
2251
|
+
var { Duplex, Readable } = __require("stream");
|
|
2252
|
+
var { URL: URL2 } = __require("url");
|
|
2253
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
2254
|
+
var Receiver = require_receiver();
|
|
2255
|
+
var Sender = require_sender();
|
|
2256
|
+
var { isBlob } = require_validation();
|
|
2257
|
+
var {
|
|
2258
|
+
BINARY_TYPES,
|
|
2259
|
+
CLOSE_TIMEOUT,
|
|
2260
|
+
EMPTY_BUFFER,
|
|
2261
|
+
GUID,
|
|
2262
|
+
kForOnEventAttribute,
|
|
2263
|
+
kListener,
|
|
2264
|
+
kStatusCode,
|
|
2265
|
+
kWebSocket,
|
|
2266
|
+
NOOP
|
|
2267
|
+
} = require_constants();
|
|
2268
|
+
var {
|
|
2269
|
+
EventTarget: { addEventListener, removeEventListener }
|
|
2270
|
+
} = require_event_target();
|
|
2271
|
+
var { format, parse } = require_extension();
|
|
2272
|
+
var { toBuffer } = require_buffer_util();
|
|
2273
|
+
var kAborted = Symbol("kAborted");
|
|
2274
|
+
var protocolVersions = [8, 13];
|
|
2275
|
+
var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
2276
|
+
var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
2277
|
+
var WebSocket2 = class _WebSocket extends EventEmitter {
|
|
2278
|
+
/**
|
|
2279
|
+
* Create a new `WebSocket`.
|
|
2280
|
+
*
|
|
2281
|
+
* @param {(String|URL)} address The URL to which to connect
|
|
2282
|
+
* @param {(String|String[])} [protocols] The subprotocols
|
|
2283
|
+
* @param {Object} [options] Connection options
|
|
2284
|
+
*/
|
|
2285
|
+
constructor(address, protocols, options) {
|
|
2286
|
+
super();
|
|
2287
|
+
this._binaryType = BINARY_TYPES[0];
|
|
2288
|
+
this._closeCode = 1006;
|
|
2289
|
+
this._closeFrameReceived = false;
|
|
2290
|
+
this._closeFrameSent = false;
|
|
2291
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
2292
|
+
this._closeTimer = null;
|
|
2293
|
+
this._errorEmitted = false;
|
|
2294
|
+
this._extensions = {};
|
|
2295
|
+
this._paused = false;
|
|
2296
|
+
this._protocol = "";
|
|
2297
|
+
this._readyState = _WebSocket.CONNECTING;
|
|
2298
|
+
this._receiver = null;
|
|
2299
|
+
this._sender = null;
|
|
2300
|
+
this._socket = null;
|
|
2301
|
+
if (address !== null) {
|
|
2302
|
+
this._bufferedAmount = 0;
|
|
2303
|
+
this._isServer = false;
|
|
2304
|
+
this._redirects = 0;
|
|
2305
|
+
if (protocols === void 0) {
|
|
2306
|
+
protocols = [];
|
|
2307
|
+
} else if (!Array.isArray(protocols)) {
|
|
2308
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
2309
|
+
options = protocols;
|
|
2310
|
+
protocols = [];
|
|
2311
|
+
} else {
|
|
2312
|
+
protocols = [protocols];
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
initAsClient(this, address, protocols, options);
|
|
2316
|
+
} else {
|
|
2317
|
+
this._autoPong = options.autoPong;
|
|
2318
|
+
this._closeTimeout = options.closeTimeout;
|
|
2319
|
+
this._isServer = true;
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
/**
|
|
2323
|
+
* For historical reasons, the custom "nodebuffer" type is used by the default
|
|
2324
|
+
* instead of "blob".
|
|
2325
|
+
*
|
|
2326
|
+
* @type {String}
|
|
2327
|
+
*/
|
|
2328
|
+
get binaryType() {
|
|
2329
|
+
return this._binaryType;
|
|
2330
|
+
}
|
|
2331
|
+
set binaryType(type) {
|
|
2332
|
+
if (!BINARY_TYPES.includes(type))
|
|
2333
|
+
return;
|
|
2334
|
+
this._binaryType = type;
|
|
2335
|
+
if (this._receiver)
|
|
2336
|
+
this._receiver._binaryType = type;
|
|
2337
|
+
}
|
|
2338
|
+
/**
|
|
2339
|
+
* @type {Number}
|
|
2340
|
+
*/
|
|
2341
|
+
get bufferedAmount() {
|
|
2342
|
+
if (!this._socket)
|
|
2343
|
+
return this._bufferedAmount;
|
|
2344
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
2345
|
+
}
|
|
2346
|
+
/**
|
|
2347
|
+
* @type {String}
|
|
2348
|
+
*/
|
|
2349
|
+
get extensions() {
|
|
2350
|
+
return Object.keys(this._extensions).join();
|
|
2351
|
+
}
|
|
2352
|
+
/**
|
|
2353
|
+
* @type {Boolean}
|
|
2354
|
+
*/
|
|
2355
|
+
get isPaused() {
|
|
2356
|
+
return this._paused;
|
|
2357
|
+
}
|
|
2358
|
+
/**
|
|
2359
|
+
* @type {Function}
|
|
2360
|
+
*/
|
|
2361
|
+
/* istanbul ignore next */
|
|
2362
|
+
get onclose() {
|
|
2363
|
+
return null;
|
|
2364
|
+
}
|
|
2365
|
+
/**
|
|
2366
|
+
* @type {Function}
|
|
2367
|
+
*/
|
|
2368
|
+
/* istanbul ignore next */
|
|
2369
|
+
get onerror() {
|
|
2370
|
+
return null;
|
|
2371
|
+
}
|
|
2372
|
+
/**
|
|
2373
|
+
* @type {Function}
|
|
2374
|
+
*/
|
|
2375
|
+
/* istanbul ignore next */
|
|
2376
|
+
get onopen() {
|
|
2377
|
+
return null;
|
|
2378
|
+
}
|
|
2379
|
+
/**
|
|
2380
|
+
* @type {Function}
|
|
2381
|
+
*/
|
|
2382
|
+
/* istanbul ignore next */
|
|
2383
|
+
get onmessage() {
|
|
2384
|
+
return null;
|
|
2385
|
+
}
|
|
2386
|
+
/**
|
|
2387
|
+
* @type {String}
|
|
2388
|
+
*/
|
|
2389
|
+
get protocol() {
|
|
2390
|
+
return this._protocol;
|
|
2391
|
+
}
|
|
2392
|
+
/**
|
|
2393
|
+
* @type {Number}
|
|
2394
|
+
*/
|
|
2395
|
+
get readyState() {
|
|
2396
|
+
return this._readyState;
|
|
2397
|
+
}
|
|
2398
|
+
/**
|
|
2399
|
+
* @type {String}
|
|
2400
|
+
*/
|
|
2401
|
+
get url() {
|
|
2402
|
+
return this._url;
|
|
2403
|
+
}
|
|
2404
|
+
/**
|
|
2405
|
+
* Set up the socket and the internal resources.
|
|
2406
|
+
*
|
|
2407
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
2408
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
2409
|
+
* @param {Object} options Options object
|
|
2410
|
+
* @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
|
|
2411
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
2412
|
+
* multiple times in the same tick
|
|
2413
|
+
* @param {Function} [options.generateMask] The function used to generate the
|
|
2414
|
+
* masking key
|
|
2415
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message size
|
|
2416
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
2417
|
+
* not to skip UTF-8 validation for text and close messages
|
|
2418
|
+
* @private
|
|
2419
|
+
*/
|
|
2420
|
+
setSocket(socket, head, options) {
|
|
2421
|
+
const receiver = new Receiver({
|
|
2422
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
2423
|
+
binaryType: this.binaryType,
|
|
2424
|
+
extensions: this._extensions,
|
|
2425
|
+
isServer: this._isServer,
|
|
2426
|
+
maxPayload: options.maxPayload,
|
|
2427
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
2428
|
+
});
|
|
2429
|
+
const sender = new Sender(socket, this._extensions, options.generateMask);
|
|
2430
|
+
this._receiver = receiver;
|
|
2431
|
+
this._sender = sender;
|
|
2432
|
+
this._socket = socket;
|
|
2433
|
+
receiver[kWebSocket] = this;
|
|
2434
|
+
sender[kWebSocket] = this;
|
|
2435
|
+
socket[kWebSocket] = this;
|
|
2436
|
+
receiver.on("conclude", receiverOnConclude);
|
|
2437
|
+
receiver.on("drain", receiverOnDrain);
|
|
2438
|
+
receiver.on("error", receiverOnError);
|
|
2439
|
+
receiver.on("message", receiverOnMessage);
|
|
2440
|
+
receiver.on("ping", receiverOnPing);
|
|
2441
|
+
receiver.on("pong", receiverOnPong);
|
|
2442
|
+
sender.onerror = senderOnError;
|
|
2443
|
+
if (socket.setTimeout)
|
|
2444
|
+
socket.setTimeout(0);
|
|
2445
|
+
if (socket.setNoDelay)
|
|
2446
|
+
socket.setNoDelay();
|
|
2447
|
+
if (head.length > 0)
|
|
2448
|
+
socket.unshift(head);
|
|
2449
|
+
socket.on("close", socketOnClose);
|
|
2450
|
+
socket.on("data", socketOnData);
|
|
2451
|
+
socket.on("end", socketOnEnd);
|
|
2452
|
+
socket.on("error", socketOnError);
|
|
2453
|
+
this._readyState = _WebSocket.OPEN;
|
|
2454
|
+
this.emit("open");
|
|
2455
|
+
}
|
|
2456
|
+
/**
|
|
2457
|
+
* Emit the `'close'` event.
|
|
2458
|
+
*
|
|
2459
|
+
* @private
|
|
2460
|
+
*/
|
|
2461
|
+
emitClose() {
|
|
2462
|
+
if (!this._socket) {
|
|
2463
|
+
this._readyState = _WebSocket.CLOSED;
|
|
2464
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
2465
|
+
return;
|
|
2466
|
+
}
|
|
2467
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
2468
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
2469
|
+
}
|
|
2470
|
+
this._receiver.removeAllListeners();
|
|
2471
|
+
this._readyState = _WebSocket.CLOSED;
|
|
2472
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
2473
|
+
}
|
|
2474
|
+
/**
|
|
2475
|
+
* Start a closing handshake.
|
|
2476
|
+
*
|
|
2477
|
+
* +----------+ +-----------+ +----------+
|
|
2478
|
+
* - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
|
|
2479
|
+
* | +----------+ +-----------+ +----------+ |
|
|
2480
|
+
* +----------+ +-----------+ |
|
|
2481
|
+
* CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
|
|
2482
|
+
* +----------+ +-----------+ |
|
|
2483
|
+
* | | | +---+ |
|
|
2484
|
+
* +------------------------+-->|fin| - - - -
|
|
2485
|
+
* | +---+ | +---+
|
|
2486
|
+
* - - - - -|fin|<---------------------+
|
|
2487
|
+
* +---+
|
|
2488
|
+
*
|
|
2489
|
+
* @param {Number} [code] Status code explaining why the connection is closing
|
|
2490
|
+
* @param {(String|Buffer)} [data] The reason why the connection is
|
|
2491
|
+
* closing
|
|
2492
|
+
* @public
|
|
2493
|
+
*/
|
|
2494
|
+
close(code, data) {
|
|
2495
|
+
if (this.readyState === _WebSocket.CLOSED)
|
|
2496
|
+
return;
|
|
2497
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2498
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
2499
|
+
abortHandshake(this, this._req, msg);
|
|
2500
|
+
return;
|
|
2501
|
+
}
|
|
2502
|
+
if (this.readyState === _WebSocket.CLOSING) {
|
|
2503
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
2504
|
+
this._socket.end();
|
|
2505
|
+
}
|
|
2506
|
+
return;
|
|
2507
|
+
}
|
|
2508
|
+
this._readyState = _WebSocket.CLOSING;
|
|
2509
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
2510
|
+
if (err)
|
|
2511
|
+
return;
|
|
2512
|
+
this._closeFrameSent = true;
|
|
2513
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
2514
|
+
this._socket.end();
|
|
2515
|
+
}
|
|
2516
|
+
});
|
|
2517
|
+
setCloseTimer(this);
|
|
2518
|
+
}
|
|
2519
|
+
/**
|
|
2520
|
+
* Pause the socket.
|
|
2521
|
+
*
|
|
2522
|
+
* @public
|
|
2523
|
+
*/
|
|
2524
|
+
pause() {
|
|
2525
|
+
if (this.readyState === _WebSocket.CONNECTING || this.readyState === _WebSocket.CLOSED) {
|
|
2526
|
+
return;
|
|
2527
|
+
}
|
|
2528
|
+
this._paused = true;
|
|
2529
|
+
this._socket.pause();
|
|
2530
|
+
}
|
|
2531
|
+
/**
|
|
2532
|
+
* Send a ping.
|
|
2533
|
+
*
|
|
2534
|
+
* @param {*} [data] The data to send
|
|
2535
|
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
|
2536
|
+
* @param {Function} [cb] Callback which is executed when the ping is sent
|
|
2537
|
+
* @public
|
|
2538
|
+
*/
|
|
2539
|
+
ping(data, mask, cb) {
|
|
2540
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2541
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2542
|
+
}
|
|
2543
|
+
if (typeof data === "function") {
|
|
2544
|
+
cb = data;
|
|
2545
|
+
data = mask = void 0;
|
|
2546
|
+
} else if (typeof mask === "function") {
|
|
2547
|
+
cb = mask;
|
|
2548
|
+
mask = void 0;
|
|
2549
|
+
}
|
|
2550
|
+
if (typeof data === "number")
|
|
2551
|
+
data = data.toString();
|
|
2552
|
+
if (this.readyState !== _WebSocket.OPEN) {
|
|
2553
|
+
sendAfterClose(this, data, cb);
|
|
2554
|
+
return;
|
|
2555
|
+
}
|
|
2556
|
+
if (mask === void 0)
|
|
2557
|
+
mask = !this._isServer;
|
|
2558
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
2559
|
+
}
|
|
2560
|
+
/**
|
|
2561
|
+
* Send a pong.
|
|
2562
|
+
*
|
|
2563
|
+
* @param {*} [data] The data to send
|
|
2564
|
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
|
2565
|
+
* @param {Function} [cb] Callback which is executed when the pong is sent
|
|
2566
|
+
* @public
|
|
2567
|
+
*/
|
|
2568
|
+
pong(data, mask, cb) {
|
|
2569
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2570
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2571
|
+
}
|
|
2572
|
+
if (typeof data === "function") {
|
|
2573
|
+
cb = data;
|
|
2574
|
+
data = mask = void 0;
|
|
2575
|
+
} else if (typeof mask === "function") {
|
|
2576
|
+
cb = mask;
|
|
2577
|
+
mask = void 0;
|
|
2578
|
+
}
|
|
2579
|
+
if (typeof data === "number")
|
|
2580
|
+
data = data.toString();
|
|
2581
|
+
if (this.readyState !== _WebSocket.OPEN) {
|
|
2582
|
+
sendAfterClose(this, data, cb);
|
|
2583
|
+
return;
|
|
2584
|
+
}
|
|
2585
|
+
if (mask === void 0)
|
|
2586
|
+
mask = !this._isServer;
|
|
2587
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
2588
|
+
}
|
|
2589
|
+
/**
|
|
2590
|
+
* Resume the socket.
|
|
2591
|
+
*
|
|
2592
|
+
* @public
|
|
2593
|
+
*/
|
|
2594
|
+
resume() {
|
|
2595
|
+
if (this.readyState === _WebSocket.CONNECTING || this.readyState === _WebSocket.CLOSED) {
|
|
2596
|
+
return;
|
|
2597
|
+
}
|
|
2598
|
+
this._paused = false;
|
|
2599
|
+
if (!this._receiver._writableState.needDrain)
|
|
2600
|
+
this._socket.resume();
|
|
2601
|
+
}
|
|
2602
|
+
/**
|
|
2603
|
+
* Send a data message.
|
|
2604
|
+
*
|
|
2605
|
+
* @param {*} data The message to send
|
|
2606
|
+
* @param {Object} [options] Options object
|
|
2607
|
+
* @param {Boolean} [options.binary] Specifies whether `data` is binary or
|
|
2608
|
+
* text
|
|
2609
|
+
* @param {Boolean} [options.compress] Specifies whether or not to compress
|
|
2610
|
+
* `data`
|
|
2611
|
+
* @param {Boolean} [options.fin=true] Specifies whether the fragment is the
|
|
2612
|
+
* last one
|
|
2613
|
+
* @param {Boolean} [options.mask] Specifies whether or not to mask `data`
|
|
2614
|
+
* @param {Function} [cb] Callback which is executed when data is written out
|
|
2615
|
+
* @public
|
|
2616
|
+
*/
|
|
2617
|
+
send(data, options, cb) {
|
|
2618
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2619
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
2620
|
+
}
|
|
2621
|
+
if (typeof options === "function") {
|
|
2622
|
+
cb = options;
|
|
2623
|
+
options = {};
|
|
2624
|
+
}
|
|
2625
|
+
if (typeof data === "number")
|
|
2626
|
+
data = data.toString();
|
|
2627
|
+
if (this.readyState !== _WebSocket.OPEN) {
|
|
2628
|
+
sendAfterClose(this, data, cb);
|
|
2629
|
+
return;
|
|
2630
|
+
}
|
|
2631
|
+
const opts = {
|
|
2632
|
+
binary: typeof data !== "string",
|
|
2633
|
+
mask: !this._isServer,
|
|
2634
|
+
compress: true,
|
|
2635
|
+
fin: true,
|
|
2636
|
+
...options
|
|
2637
|
+
};
|
|
2638
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
2639
|
+
opts.compress = false;
|
|
2640
|
+
}
|
|
2641
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
2642
|
+
}
|
|
2643
|
+
/**
|
|
2644
|
+
* Forcibly close the connection.
|
|
2645
|
+
*
|
|
2646
|
+
* @public
|
|
2647
|
+
*/
|
|
2648
|
+
terminate() {
|
|
2649
|
+
if (this.readyState === _WebSocket.CLOSED)
|
|
2650
|
+
return;
|
|
2651
|
+
if (this.readyState === _WebSocket.CONNECTING) {
|
|
2652
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
2653
|
+
abortHandshake(this, this._req, msg);
|
|
2654
|
+
return;
|
|
2655
|
+
}
|
|
2656
|
+
if (this._socket) {
|
|
2657
|
+
this._readyState = _WebSocket.CLOSING;
|
|
2658
|
+
this._socket.destroy();
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
};
|
|
2662
|
+
Object.defineProperty(WebSocket2, "CONNECTING", {
|
|
2663
|
+
enumerable: true,
|
|
2664
|
+
value: readyStates.indexOf("CONNECTING")
|
|
2665
|
+
});
|
|
2666
|
+
Object.defineProperty(WebSocket2.prototype, "CONNECTING", {
|
|
2667
|
+
enumerable: true,
|
|
2668
|
+
value: readyStates.indexOf("CONNECTING")
|
|
2669
|
+
});
|
|
2670
|
+
Object.defineProperty(WebSocket2, "OPEN", {
|
|
2671
|
+
enumerable: true,
|
|
2672
|
+
value: readyStates.indexOf("OPEN")
|
|
2673
|
+
});
|
|
2674
|
+
Object.defineProperty(WebSocket2.prototype, "OPEN", {
|
|
2675
|
+
enumerable: true,
|
|
2676
|
+
value: readyStates.indexOf("OPEN")
|
|
2677
|
+
});
|
|
2678
|
+
Object.defineProperty(WebSocket2, "CLOSING", {
|
|
2679
|
+
enumerable: true,
|
|
2680
|
+
value: readyStates.indexOf("CLOSING")
|
|
2681
|
+
});
|
|
2682
|
+
Object.defineProperty(WebSocket2.prototype, "CLOSING", {
|
|
2683
|
+
enumerable: true,
|
|
2684
|
+
value: readyStates.indexOf("CLOSING")
|
|
2685
|
+
});
|
|
2686
|
+
Object.defineProperty(WebSocket2, "CLOSED", {
|
|
2687
|
+
enumerable: true,
|
|
2688
|
+
value: readyStates.indexOf("CLOSED")
|
|
2689
|
+
});
|
|
2690
|
+
Object.defineProperty(WebSocket2.prototype, "CLOSED", {
|
|
2691
|
+
enumerable: true,
|
|
2692
|
+
value: readyStates.indexOf("CLOSED")
|
|
2693
|
+
});
|
|
2694
|
+
[
|
|
2695
|
+
"binaryType",
|
|
2696
|
+
"bufferedAmount",
|
|
2697
|
+
"extensions",
|
|
2698
|
+
"isPaused",
|
|
2699
|
+
"protocol",
|
|
2700
|
+
"readyState",
|
|
2701
|
+
"url"
|
|
2702
|
+
].forEach((property) => {
|
|
2703
|
+
Object.defineProperty(WebSocket2.prototype, property, { enumerable: true });
|
|
2704
|
+
});
|
|
2705
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
2706
|
+
Object.defineProperty(WebSocket2.prototype, `on${method}`, {
|
|
2707
|
+
enumerable: true,
|
|
2708
|
+
get() {
|
|
2709
|
+
for (const listener of this.listeners(method)) {
|
|
2710
|
+
if (listener[kForOnEventAttribute])
|
|
2711
|
+
return listener[kListener];
|
|
2712
|
+
}
|
|
2713
|
+
return null;
|
|
2714
|
+
},
|
|
2715
|
+
set(handler) {
|
|
2716
|
+
for (const listener of this.listeners(method)) {
|
|
2717
|
+
if (listener[kForOnEventAttribute]) {
|
|
2718
|
+
this.removeListener(method, listener);
|
|
2719
|
+
break;
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
if (typeof handler !== "function")
|
|
2723
|
+
return;
|
|
2724
|
+
this.addEventListener(method, handler, {
|
|
2725
|
+
[kForOnEventAttribute]: true
|
|
2726
|
+
});
|
|
2727
|
+
}
|
|
2728
|
+
});
|
|
2729
|
+
});
|
|
2730
|
+
WebSocket2.prototype.addEventListener = addEventListener;
|
|
2731
|
+
WebSocket2.prototype.removeEventListener = removeEventListener;
|
|
2732
|
+
module.exports = WebSocket2;
|
|
2733
|
+
function initAsClient(websocket, address, protocols, options) {
|
|
2734
|
+
const opts = {
|
|
2735
|
+
allowSynchronousEvents: true,
|
|
2736
|
+
autoPong: true,
|
|
2737
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
2738
|
+
protocolVersion: protocolVersions[1],
|
|
2739
|
+
maxPayload: 100 * 1024 * 1024,
|
|
2740
|
+
skipUTF8Validation: false,
|
|
2741
|
+
perMessageDeflate: true,
|
|
2742
|
+
followRedirects: false,
|
|
2743
|
+
maxRedirects: 10,
|
|
2744
|
+
...options,
|
|
2745
|
+
socketPath: void 0,
|
|
2746
|
+
hostname: void 0,
|
|
2747
|
+
protocol: void 0,
|
|
2748
|
+
timeout: void 0,
|
|
2749
|
+
method: "GET",
|
|
2750
|
+
host: void 0,
|
|
2751
|
+
path: void 0,
|
|
2752
|
+
port: void 0
|
|
2753
|
+
};
|
|
2754
|
+
websocket._autoPong = opts.autoPong;
|
|
2755
|
+
websocket._closeTimeout = opts.closeTimeout;
|
|
2756
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
2757
|
+
throw new RangeError(
|
|
2758
|
+
`Unsupported protocol version: ${opts.protocolVersion} (supported versions: ${protocolVersions.join(", ")})`
|
|
2759
|
+
);
|
|
2760
|
+
}
|
|
2761
|
+
let parsedUrl;
|
|
2762
|
+
if (address instanceof URL2) {
|
|
2763
|
+
parsedUrl = address;
|
|
2764
|
+
} else {
|
|
2765
|
+
try {
|
|
2766
|
+
parsedUrl = new URL2(address);
|
|
2767
|
+
} catch (e) {
|
|
2768
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
2769
|
+
}
|
|
2770
|
+
}
|
|
2771
|
+
if (parsedUrl.protocol === "http:") {
|
|
2772
|
+
parsedUrl.protocol = "ws:";
|
|
2773
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
2774
|
+
parsedUrl.protocol = "wss:";
|
|
2775
|
+
}
|
|
2776
|
+
websocket._url = parsedUrl.href;
|
|
2777
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
2778
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
2779
|
+
let invalidUrlMessage;
|
|
2780
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
2781
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", "http:", "https:", or "ws+unix:"`;
|
|
2782
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
2783
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
2784
|
+
} else if (parsedUrl.hash) {
|
|
2785
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
2786
|
+
}
|
|
2787
|
+
if (invalidUrlMessage) {
|
|
2788
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
2789
|
+
if (websocket._redirects === 0) {
|
|
2790
|
+
throw err;
|
|
2791
|
+
} else {
|
|
2792
|
+
emitErrorAndClose(websocket, err);
|
|
2793
|
+
return;
|
|
2794
|
+
}
|
|
2795
|
+
}
|
|
2796
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
2797
|
+
const key = randomBytes(16).toString("base64");
|
|
2798
|
+
const request = isSecure ? https.request : http.request;
|
|
2799
|
+
const protocolSet = /* @__PURE__ */ new Set();
|
|
2800
|
+
let perMessageDeflate;
|
|
2801
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
2802
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
2803
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
2804
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
2805
|
+
opts.headers = {
|
|
2806
|
+
...opts.headers,
|
|
2807
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
2808
|
+
"Sec-WebSocket-Key": key,
|
|
2809
|
+
Connection: "Upgrade",
|
|
2810
|
+
Upgrade: "websocket"
|
|
2811
|
+
};
|
|
2812
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
2813
|
+
opts.timeout = opts.handshakeTimeout;
|
|
2814
|
+
if (opts.perMessageDeflate) {
|
|
2815
|
+
perMessageDeflate = new PerMessageDeflate(
|
|
2816
|
+
opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
|
|
2817
|
+
false,
|
|
2818
|
+
opts.maxPayload
|
|
2819
|
+
);
|
|
2820
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({
|
|
2821
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
2822
|
+
});
|
|
2823
|
+
}
|
|
2824
|
+
if (protocols.length) {
|
|
2825
|
+
for (const protocol of protocols) {
|
|
2826
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
2827
|
+
throw new SyntaxError(
|
|
2828
|
+
"An invalid or duplicated subprotocol was specified"
|
|
2829
|
+
);
|
|
2830
|
+
}
|
|
2831
|
+
protocolSet.add(protocol);
|
|
2832
|
+
}
|
|
2833
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
2834
|
+
}
|
|
2835
|
+
if (opts.origin) {
|
|
2836
|
+
if (opts.protocolVersion < 13) {
|
|
2837
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
2838
|
+
} else {
|
|
2839
|
+
opts.headers.Origin = opts.origin;
|
|
2840
|
+
}
|
|
2841
|
+
}
|
|
2842
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
2843
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
2844
|
+
}
|
|
2845
|
+
if (isIpcUrl) {
|
|
2846
|
+
const parts = opts.path.split(":");
|
|
2847
|
+
opts.socketPath = parts[0];
|
|
2848
|
+
opts.path = parts[1];
|
|
2849
|
+
}
|
|
2850
|
+
let req;
|
|
2851
|
+
if (opts.followRedirects) {
|
|
2852
|
+
if (websocket._redirects === 0) {
|
|
2853
|
+
websocket._originalIpc = isIpcUrl;
|
|
2854
|
+
websocket._originalSecure = isSecure;
|
|
2855
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
2856
|
+
const headers = options && options.headers;
|
|
2857
|
+
options = { ...options, headers: {} };
|
|
2858
|
+
if (headers) {
|
|
2859
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
2860
|
+
options.headers[key2.toLowerCase()] = value;
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
2864
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
2865
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
2866
|
+
delete opts.headers.authorization;
|
|
2867
|
+
delete opts.headers.cookie;
|
|
2868
|
+
if (!isSameHost)
|
|
2869
|
+
delete opts.headers.host;
|
|
2870
|
+
opts.auth = void 0;
|
|
2871
|
+
}
|
|
2872
|
+
}
|
|
2873
|
+
if (opts.auth && !options.headers.authorization) {
|
|
2874
|
+
options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
2875
|
+
}
|
|
2876
|
+
req = websocket._req = request(opts);
|
|
2877
|
+
if (websocket._redirects) {
|
|
2878
|
+
websocket.emit("redirect", websocket.url, req);
|
|
2879
|
+
}
|
|
2880
|
+
} else {
|
|
2881
|
+
req = websocket._req = request(opts);
|
|
2882
|
+
}
|
|
2883
|
+
if (opts.timeout) {
|
|
2884
|
+
req.on("timeout", () => {
|
|
2885
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
2886
|
+
});
|
|
2887
|
+
}
|
|
2888
|
+
req.on("error", (err) => {
|
|
2889
|
+
if (req === null || req[kAborted])
|
|
2890
|
+
return;
|
|
2891
|
+
req = websocket._req = null;
|
|
2892
|
+
emitErrorAndClose(websocket, err);
|
|
2893
|
+
});
|
|
2894
|
+
req.on("response", (res) => {
|
|
2895
|
+
const location = res.headers.location;
|
|
2896
|
+
const statusCode = res.statusCode;
|
|
2897
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
2898
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
2899
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
2900
|
+
return;
|
|
2901
|
+
}
|
|
2902
|
+
req.abort();
|
|
2903
|
+
let addr;
|
|
2904
|
+
try {
|
|
2905
|
+
addr = new URL2(location, address);
|
|
2906
|
+
} catch (e) {
|
|
2907
|
+
const err = new SyntaxError(`Invalid URL: ${location}`);
|
|
2908
|
+
emitErrorAndClose(websocket, err);
|
|
2909
|
+
return;
|
|
2910
|
+
}
|
|
2911
|
+
initAsClient(websocket, addr, protocols, options);
|
|
2912
|
+
} else if (!websocket.emit("unexpected-response", req, res)) {
|
|
2913
|
+
abortHandshake(
|
|
2914
|
+
websocket,
|
|
2915
|
+
req,
|
|
2916
|
+
`Unexpected server response: ${res.statusCode}`
|
|
2917
|
+
);
|
|
2918
|
+
}
|
|
2919
|
+
});
|
|
2920
|
+
req.on("upgrade", (res, socket, head) => {
|
|
2921
|
+
websocket.emit("upgrade", res);
|
|
2922
|
+
if (websocket.readyState !== WebSocket2.CONNECTING)
|
|
2923
|
+
return;
|
|
2924
|
+
req = websocket._req = null;
|
|
2925
|
+
const upgrade = res.headers.upgrade;
|
|
2926
|
+
if (upgrade === void 0 || upgrade.toLowerCase() !== "websocket") {
|
|
2927
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
2928
|
+
return;
|
|
2929
|
+
}
|
|
2930
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
2931
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
2932
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
2933
|
+
return;
|
|
2934
|
+
}
|
|
2935
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
2936
|
+
let protError;
|
|
2937
|
+
if (serverProt !== void 0) {
|
|
2938
|
+
if (!protocolSet.size) {
|
|
2939
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
2940
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
2941
|
+
protError = "Server sent an invalid subprotocol";
|
|
2942
|
+
}
|
|
2943
|
+
} else if (protocolSet.size) {
|
|
2944
|
+
protError = "Server sent no subprotocol";
|
|
2945
|
+
}
|
|
2946
|
+
if (protError) {
|
|
2947
|
+
abortHandshake(websocket, socket, protError);
|
|
2948
|
+
return;
|
|
2949
|
+
}
|
|
2950
|
+
if (serverProt)
|
|
2951
|
+
websocket._protocol = serverProt;
|
|
2952
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
2953
|
+
if (secWebSocketExtensions !== void 0) {
|
|
2954
|
+
if (!perMessageDeflate) {
|
|
2955
|
+
const message = "Server sent a Sec-WebSocket-Extensions header but no extension was requested";
|
|
2956
|
+
abortHandshake(websocket, socket, message);
|
|
2957
|
+
return;
|
|
2958
|
+
}
|
|
2959
|
+
let extensions;
|
|
2960
|
+
try {
|
|
2961
|
+
extensions = parse(secWebSocketExtensions);
|
|
2962
|
+
} catch (err) {
|
|
2963
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
2964
|
+
abortHandshake(websocket, socket, message);
|
|
2965
|
+
return;
|
|
2966
|
+
}
|
|
2967
|
+
const extensionNames = Object.keys(extensions);
|
|
2968
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
2969
|
+
const message = "Server indicated an extension that was not requested";
|
|
2970
|
+
abortHandshake(websocket, socket, message);
|
|
2971
|
+
return;
|
|
2972
|
+
}
|
|
2973
|
+
try {
|
|
2974
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
2975
|
+
} catch (err) {
|
|
2976
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
2977
|
+
abortHandshake(websocket, socket, message);
|
|
2978
|
+
return;
|
|
2979
|
+
}
|
|
2980
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
2981
|
+
}
|
|
2982
|
+
websocket.setSocket(socket, head, {
|
|
2983
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
2984
|
+
generateMask: opts.generateMask,
|
|
2985
|
+
maxPayload: opts.maxPayload,
|
|
2986
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
2987
|
+
});
|
|
2988
|
+
});
|
|
2989
|
+
if (opts.finishRequest) {
|
|
2990
|
+
opts.finishRequest(req, websocket);
|
|
2991
|
+
} else {
|
|
2992
|
+
req.end();
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
function emitErrorAndClose(websocket, err) {
|
|
2996
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
2997
|
+
websocket._errorEmitted = true;
|
|
2998
|
+
websocket.emit("error", err);
|
|
2999
|
+
websocket.emitClose();
|
|
3000
|
+
}
|
|
3001
|
+
function netConnect(options) {
|
|
3002
|
+
options.path = options.socketPath;
|
|
3003
|
+
return net.connect(options);
|
|
3004
|
+
}
|
|
3005
|
+
function tlsConnect(options) {
|
|
3006
|
+
options.path = void 0;
|
|
3007
|
+
if (!options.servername && options.servername !== "") {
|
|
3008
|
+
options.servername = net.isIP(options.host) ? "" : options.host;
|
|
3009
|
+
}
|
|
3010
|
+
return tls.connect(options);
|
|
3011
|
+
}
|
|
3012
|
+
function abortHandshake(websocket, stream, message) {
|
|
3013
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3014
|
+
const err = new Error(message);
|
|
3015
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
3016
|
+
if (stream.setHeader) {
|
|
3017
|
+
stream[kAborted] = true;
|
|
3018
|
+
stream.abort();
|
|
3019
|
+
if (stream.socket && !stream.socket.destroyed) {
|
|
3020
|
+
stream.socket.destroy();
|
|
3021
|
+
}
|
|
3022
|
+
process.nextTick(emitErrorAndClose, websocket, err);
|
|
3023
|
+
} else {
|
|
3024
|
+
stream.destroy(err);
|
|
3025
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
3026
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
3027
|
+
}
|
|
3028
|
+
}
|
|
3029
|
+
function sendAfterClose(websocket, data, cb) {
|
|
3030
|
+
if (data) {
|
|
3031
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
3032
|
+
if (websocket._socket)
|
|
3033
|
+
websocket._sender._bufferedBytes += length;
|
|
3034
|
+
else
|
|
3035
|
+
websocket._bufferedAmount += length;
|
|
3036
|
+
}
|
|
3037
|
+
if (cb) {
|
|
3038
|
+
const err = new Error(
|
|
3039
|
+
`WebSocket is not open: readyState ${websocket.readyState} (${readyStates[websocket.readyState]})`
|
|
3040
|
+
);
|
|
3041
|
+
process.nextTick(cb, err);
|
|
3042
|
+
}
|
|
3043
|
+
}
|
|
3044
|
+
function receiverOnConclude(code, reason) {
|
|
3045
|
+
const websocket = this[kWebSocket];
|
|
3046
|
+
websocket._closeFrameReceived = true;
|
|
3047
|
+
websocket._closeMessage = reason;
|
|
3048
|
+
websocket._closeCode = code;
|
|
3049
|
+
if (websocket._socket[kWebSocket] === void 0)
|
|
3050
|
+
return;
|
|
3051
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
3052
|
+
process.nextTick(resume, websocket._socket);
|
|
3053
|
+
if (code === 1005)
|
|
3054
|
+
websocket.close();
|
|
3055
|
+
else
|
|
3056
|
+
websocket.close(code, reason);
|
|
3057
|
+
}
|
|
3058
|
+
function receiverOnDrain() {
|
|
3059
|
+
const websocket = this[kWebSocket];
|
|
3060
|
+
if (!websocket.isPaused)
|
|
3061
|
+
websocket._socket.resume();
|
|
3062
|
+
}
|
|
3063
|
+
function receiverOnError(err) {
|
|
3064
|
+
const websocket = this[kWebSocket];
|
|
3065
|
+
if (websocket._socket[kWebSocket] !== void 0) {
|
|
3066
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
3067
|
+
process.nextTick(resume, websocket._socket);
|
|
3068
|
+
websocket.close(err[kStatusCode]);
|
|
3069
|
+
}
|
|
3070
|
+
if (!websocket._errorEmitted) {
|
|
3071
|
+
websocket._errorEmitted = true;
|
|
3072
|
+
websocket.emit("error", err);
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
function receiverOnFinish() {
|
|
3076
|
+
this[kWebSocket].emitClose();
|
|
3077
|
+
}
|
|
3078
|
+
function receiverOnMessage(data, isBinary) {
|
|
3079
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
3080
|
+
}
|
|
3081
|
+
function receiverOnPing(data) {
|
|
3082
|
+
const websocket = this[kWebSocket];
|
|
3083
|
+
if (websocket._autoPong)
|
|
3084
|
+
websocket.pong(data, !this._isServer, NOOP);
|
|
3085
|
+
websocket.emit("ping", data);
|
|
3086
|
+
}
|
|
3087
|
+
function receiverOnPong(data) {
|
|
3088
|
+
this[kWebSocket].emit("pong", data);
|
|
3089
|
+
}
|
|
3090
|
+
function resume(stream) {
|
|
3091
|
+
stream.resume();
|
|
3092
|
+
}
|
|
3093
|
+
function senderOnError(err) {
|
|
3094
|
+
const websocket = this[kWebSocket];
|
|
3095
|
+
if (websocket.readyState === WebSocket2.CLOSED)
|
|
3096
|
+
return;
|
|
3097
|
+
if (websocket.readyState === WebSocket2.OPEN) {
|
|
3098
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3099
|
+
setCloseTimer(websocket);
|
|
3100
|
+
}
|
|
3101
|
+
this._socket.end();
|
|
3102
|
+
if (!websocket._errorEmitted) {
|
|
3103
|
+
websocket._errorEmitted = true;
|
|
3104
|
+
websocket.emit("error", err);
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
function setCloseTimer(websocket) {
|
|
3108
|
+
websocket._closeTimer = setTimeout(
|
|
3109
|
+
websocket._socket.destroy.bind(websocket._socket),
|
|
3110
|
+
websocket._closeTimeout
|
|
3111
|
+
);
|
|
3112
|
+
}
|
|
3113
|
+
function socketOnClose() {
|
|
3114
|
+
const websocket = this[kWebSocket];
|
|
3115
|
+
this.removeListener("close", socketOnClose);
|
|
3116
|
+
this.removeListener("data", socketOnData);
|
|
3117
|
+
this.removeListener("end", socketOnEnd);
|
|
3118
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3119
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
|
|
3120
|
+
const chunk = this.read(this._readableState.length);
|
|
3121
|
+
websocket._receiver.write(chunk);
|
|
3122
|
+
}
|
|
3123
|
+
websocket._receiver.end();
|
|
3124
|
+
this[kWebSocket] = void 0;
|
|
3125
|
+
clearTimeout(websocket._closeTimer);
|
|
3126
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
|
|
3127
|
+
websocket.emitClose();
|
|
3128
|
+
} else {
|
|
3129
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
3130
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
function socketOnData(chunk) {
|
|
3134
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
3135
|
+
this.pause();
|
|
3136
|
+
}
|
|
3137
|
+
}
|
|
3138
|
+
function socketOnEnd() {
|
|
3139
|
+
const websocket = this[kWebSocket];
|
|
3140
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3141
|
+
websocket._receiver.end();
|
|
3142
|
+
this.end();
|
|
3143
|
+
}
|
|
3144
|
+
function socketOnError() {
|
|
3145
|
+
const websocket = this[kWebSocket];
|
|
3146
|
+
this.removeListener("error", socketOnError);
|
|
3147
|
+
this.on("error", NOOP);
|
|
3148
|
+
if (websocket) {
|
|
3149
|
+
websocket._readyState = WebSocket2.CLOSING;
|
|
3150
|
+
this.destroy();
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
});
|
|
3155
|
+
|
|
3156
|
+
// ../../../node_modules/ws/lib/stream.js
|
|
3157
|
+
var require_stream = __commonJS({
|
|
3158
|
+
"../../../node_modules/ws/lib/stream.js"(exports$1, module) {
|
|
3159
|
+
require_websocket();
|
|
3160
|
+
var { Duplex } = __require("stream");
|
|
3161
|
+
function emitClose(stream) {
|
|
3162
|
+
stream.emit("close");
|
|
3163
|
+
}
|
|
3164
|
+
function duplexOnEnd() {
|
|
3165
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
3166
|
+
this.destroy();
|
|
3167
|
+
}
|
|
3168
|
+
}
|
|
3169
|
+
function duplexOnError(err) {
|
|
3170
|
+
this.removeListener("error", duplexOnError);
|
|
3171
|
+
this.destroy();
|
|
3172
|
+
if (this.listenerCount("error") === 0) {
|
|
3173
|
+
this.emit("error", err);
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3176
|
+
function createWebSocketStream(ws, options) {
|
|
3177
|
+
let terminateOnDestroy = true;
|
|
3178
|
+
const duplex = new Duplex({
|
|
3179
|
+
...options,
|
|
3180
|
+
autoDestroy: false,
|
|
3181
|
+
emitClose: false,
|
|
3182
|
+
objectMode: false,
|
|
3183
|
+
writableObjectMode: false
|
|
3184
|
+
});
|
|
3185
|
+
ws.on("message", function message(msg, isBinary) {
|
|
3186
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
3187
|
+
if (!duplex.push(data))
|
|
3188
|
+
ws.pause();
|
|
3189
|
+
});
|
|
3190
|
+
ws.once("error", function error(err) {
|
|
3191
|
+
if (duplex.destroyed)
|
|
3192
|
+
return;
|
|
3193
|
+
terminateOnDestroy = false;
|
|
3194
|
+
duplex.destroy(err);
|
|
3195
|
+
});
|
|
3196
|
+
ws.once("close", function close() {
|
|
3197
|
+
if (duplex.destroyed)
|
|
3198
|
+
return;
|
|
3199
|
+
duplex.push(null);
|
|
3200
|
+
});
|
|
3201
|
+
duplex._destroy = function(err, callback) {
|
|
3202
|
+
if (ws.readyState === ws.CLOSED) {
|
|
3203
|
+
callback(err);
|
|
3204
|
+
process.nextTick(emitClose, duplex);
|
|
3205
|
+
return;
|
|
3206
|
+
}
|
|
3207
|
+
let called = false;
|
|
3208
|
+
ws.once("error", function error(err2) {
|
|
3209
|
+
called = true;
|
|
3210
|
+
callback(err2);
|
|
3211
|
+
});
|
|
3212
|
+
ws.once("close", function close() {
|
|
3213
|
+
if (!called)
|
|
3214
|
+
callback(err);
|
|
3215
|
+
process.nextTick(emitClose, duplex);
|
|
3216
|
+
});
|
|
3217
|
+
if (terminateOnDestroy)
|
|
3218
|
+
ws.terminate();
|
|
3219
|
+
};
|
|
3220
|
+
duplex._final = function(callback) {
|
|
3221
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
3222
|
+
ws.once("open", function open() {
|
|
3223
|
+
duplex._final(callback);
|
|
3224
|
+
});
|
|
3225
|
+
return;
|
|
3226
|
+
}
|
|
3227
|
+
if (ws._socket === null)
|
|
3228
|
+
return;
|
|
3229
|
+
if (ws._socket._writableState.finished) {
|
|
3230
|
+
callback();
|
|
3231
|
+
if (duplex._readableState.endEmitted)
|
|
3232
|
+
duplex.destroy();
|
|
3233
|
+
} else {
|
|
3234
|
+
ws._socket.once("finish", function finish() {
|
|
3235
|
+
callback();
|
|
3236
|
+
});
|
|
3237
|
+
ws.close();
|
|
3238
|
+
}
|
|
3239
|
+
};
|
|
3240
|
+
duplex._read = function() {
|
|
3241
|
+
if (ws.isPaused)
|
|
3242
|
+
ws.resume();
|
|
3243
|
+
};
|
|
3244
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
3245
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
3246
|
+
ws.once("open", function open() {
|
|
3247
|
+
duplex._write(chunk, encoding, callback);
|
|
3248
|
+
});
|
|
3249
|
+
return;
|
|
3250
|
+
}
|
|
3251
|
+
ws.send(chunk, callback);
|
|
3252
|
+
};
|
|
3253
|
+
duplex.on("end", duplexOnEnd);
|
|
3254
|
+
duplex.on("error", duplexOnError);
|
|
3255
|
+
return duplex;
|
|
3256
|
+
}
|
|
3257
|
+
module.exports = createWebSocketStream;
|
|
3258
|
+
}
|
|
3259
|
+
});
|
|
3260
|
+
|
|
3261
|
+
// ../../../node_modules/ws/lib/subprotocol.js
|
|
3262
|
+
var require_subprotocol = __commonJS({
|
|
3263
|
+
"../../../node_modules/ws/lib/subprotocol.js"(exports$1, module) {
|
|
3264
|
+
var { tokenChars } = require_validation();
|
|
3265
|
+
function parse(header) {
|
|
3266
|
+
const protocols = /* @__PURE__ */ new Set();
|
|
3267
|
+
let start = -1;
|
|
3268
|
+
let end = -1;
|
|
3269
|
+
let i = 0;
|
|
3270
|
+
for (i; i < header.length; i++) {
|
|
3271
|
+
const code = header.charCodeAt(i);
|
|
3272
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
3273
|
+
if (start === -1)
|
|
3274
|
+
start = i;
|
|
3275
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
3276
|
+
if (end === -1 && start !== -1)
|
|
3277
|
+
end = i;
|
|
3278
|
+
} else if (code === 44) {
|
|
3279
|
+
if (start === -1) {
|
|
3280
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
3281
|
+
}
|
|
3282
|
+
if (end === -1)
|
|
3283
|
+
end = i;
|
|
3284
|
+
const protocol2 = header.slice(start, end);
|
|
3285
|
+
if (protocols.has(protocol2)) {
|
|
3286
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
3287
|
+
}
|
|
3288
|
+
protocols.add(protocol2);
|
|
3289
|
+
start = end = -1;
|
|
3290
|
+
} else {
|
|
3291
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
3292
|
+
}
|
|
3293
|
+
}
|
|
3294
|
+
if (start === -1 || end !== -1) {
|
|
3295
|
+
throw new SyntaxError("Unexpected end of input");
|
|
3296
|
+
}
|
|
3297
|
+
const protocol = header.slice(start, i);
|
|
3298
|
+
if (protocols.has(protocol)) {
|
|
3299
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
3300
|
+
}
|
|
3301
|
+
protocols.add(protocol);
|
|
3302
|
+
return protocols;
|
|
3303
|
+
}
|
|
3304
|
+
module.exports = { parse };
|
|
3305
|
+
}
|
|
3306
|
+
});
|
|
3307
|
+
|
|
3308
|
+
// ../../../node_modules/ws/lib/websocket-server.js
|
|
3309
|
+
var require_websocket_server = __commonJS({
|
|
3310
|
+
"../../../node_modules/ws/lib/websocket-server.js"(exports$1, module) {
|
|
3311
|
+
var EventEmitter = __require("events");
|
|
3312
|
+
var http = __require("http");
|
|
3313
|
+
var { Duplex } = __require("stream");
|
|
3314
|
+
var { createHash } = __require("crypto");
|
|
3315
|
+
var extension = require_extension();
|
|
3316
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
3317
|
+
var subprotocol = require_subprotocol();
|
|
3318
|
+
var WebSocket2 = require_websocket();
|
|
3319
|
+
var { CLOSE_TIMEOUT, GUID, kWebSocket } = require_constants();
|
|
3320
|
+
var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
3321
|
+
var RUNNING = 0;
|
|
3322
|
+
var CLOSING = 1;
|
|
3323
|
+
var CLOSED = 2;
|
|
3324
|
+
var WebSocketServer = class extends EventEmitter {
|
|
3325
|
+
/**
|
|
3326
|
+
* Create a `WebSocketServer` instance.
|
|
3327
|
+
*
|
|
3328
|
+
* @param {Object} options Configuration options
|
|
3329
|
+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
|
|
3330
|
+
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
|
|
3331
|
+
* multiple times in the same tick
|
|
3332
|
+
* @param {Boolean} [options.autoPong=true] Specifies whether or not to
|
|
3333
|
+
* automatically send a pong in response to a ping
|
|
3334
|
+
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
|
3335
|
+
* pending connections
|
|
3336
|
+
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
|
3337
|
+
* track clients
|
|
3338
|
+
* @param {Number} [options.closeTimeout=30000] Duration in milliseconds to
|
|
3339
|
+
* wait for the closing handshake to finish after `websocket.close()` is
|
|
3340
|
+
* called
|
|
3341
|
+
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
|
3342
|
+
* @param {String} [options.host] The hostname where to bind the server
|
|
3343
|
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
|
3344
|
+
* size
|
|
3345
|
+
* @param {Boolean} [options.noServer=false] Enable no server mode
|
|
3346
|
+
* @param {String} [options.path] Accept only connections matching this path
|
|
3347
|
+
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
|
3348
|
+
* permessage-deflate
|
|
3349
|
+
* @param {Number} [options.port] The port where to bind the server
|
|
3350
|
+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
|
3351
|
+
* server to use
|
|
3352
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
3353
|
+
* not to skip UTF-8 validation for text and close messages
|
|
3354
|
+
* @param {Function} [options.verifyClient] A hook to reject connections
|
|
3355
|
+
* @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
|
|
3356
|
+
* class to use. It must be the `WebSocket` class or class that extends it
|
|
3357
|
+
* @param {Function} [callback] A listener for the `listening` event
|
|
3358
|
+
*/
|
|
3359
|
+
constructor(options, callback) {
|
|
3360
|
+
super();
|
|
3361
|
+
options = {
|
|
3362
|
+
allowSynchronousEvents: true,
|
|
3363
|
+
autoPong: true,
|
|
3364
|
+
maxPayload: 100 * 1024 * 1024,
|
|
3365
|
+
skipUTF8Validation: false,
|
|
3366
|
+
perMessageDeflate: false,
|
|
3367
|
+
handleProtocols: null,
|
|
3368
|
+
clientTracking: true,
|
|
3369
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
3370
|
+
verifyClient: null,
|
|
3371
|
+
noServer: false,
|
|
3372
|
+
backlog: null,
|
|
3373
|
+
// use default (511 as implemented in net.js)
|
|
3374
|
+
server: null,
|
|
3375
|
+
host: null,
|
|
3376
|
+
path: null,
|
|
3377
|
+
port: null,
|
|
3378
|
+
WebSocket: WebSocket2,
|
|
3379
|
+
...options
|
|
3380
|
+
};
|
|
3381
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
|
|
3382
|
+
throw new TypeError(
|
|
3383
|
+
'One and only one of the "port", "server", or "noServer" options must be specified'
|
|
3384
|
+
);
|
|
3385
|
+
}
|
|
3386
|
+
if (options.port != null) {
|
|
3387
|
+
this._server = http.createServer((req, res) => {
|
|
3388
|
+
const body = http.STATUS_CODES[426];
|
|
3389
|
+
res.writeHead(426, {
|
|
3390
|
+
"Content-Length": body.length,
|
|
3391
|
+
"Content-Type": "text/plain"
|
|
3392
|
+
});
|
|
3393
|
+
res.end(body);
|
|
3394
|
+
});
|
|
3395
|
+
this._server.listen(
|
|
3396
|
+
options.port,
|
|
3397
|
+
options.host,
|
|
3398
|
+
options.backlog,
|
|
3399
|
+
callback
|
|
3400
|
+
);
|
|
3401
|
+
} else if (options.server) {
|
|
3402
|
+
this._server = options.server;
|
|
3403
|
+
}
|
|
3404
|
+
if (this._server) {
|
|
3405
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
3406
|
+
this._removeListeners = addListeners(this._server, {
|
|
3407
|
+
listening: this.emit.bind(this, "listening"),
|
|
3408
|
+
error: this.emit.bind(this, "error"),
|
|
3409
|
+
upgrade: (req, socket, head) => {
|
|
3410
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
3411
|
+
}
|
|
3412
|
+
});
|
|
3413
|
+
}
|
|
3414
|
+
if (options.perMessageDeflate === true)
|
|
3415
|
+
options.perMessageDeflate = {};
|
|
3416
|
+
if (options.clientTracking) {
|
|
3417
|
+
this.clients = /* @__PURE__ */ new Set();
|
|
3418
|
+
this._shouldEmitClose = false;
|
|
3419
|
+
}
|
|
3420
|
+
this.options = options;
|
|
3421
|
+
this._state = RUNNING;
|
|
3422
|
+
}
|
|
3423
|
+
/**
|
|
3424
|
+
* Returns the bound address, the address family name, and port of the server
|
|
3425
|
+
* as reported by the operating system if listening on an IP socket.
|
|
3426
|
+
* If the server is listening on a pipe or UNIX domain socket, the name is
|
|
3427
|
+
* returned as a string.
|
|
3428
|
+
*
|
|
3429
|
+
* @return {(Object|String|null)} The address of the server
|
|
3430
|
+
* @public
|
|
3431
|
+
*/
|
|
3432
|
+
address() {
|
|
3433
|
+
if (this.options.noServer) {
|
|
3434
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
3435
|
+
}
|
|
3436
|
+
if (!this._server)
|
|
3437
|
+
return null;
|
|
3438
|
+
return this._server.address();
|
|
3439
|
+
}
|
|
3440
|
+
/**
|
|
3441
|
+
* Stop the server from accepting new connections and emit the `'close'` event
|
|
3442
|
+
* when all existing connections are closed.
|
|
3443
|
+
*
|
|
3444
|
+
* @param {Function} [cb] A one-time listener for the `'close'` event
|
|
3445
|
+
* @public
|
|
3446
|
+
*/
|
|
3447
|
+
close(cb) {
|
|
3448
|
+
if (this._state === CLOSED) {
|
|
3449
|
+
if (cb) {
|
|
3450
|
+
this.once("close", () => {
|
|
3451
|
+
cb(new Error("The server is not running"));
|
|
3452
|
+
});
|
|
3453
|
+
}
|
|
3454
|
+
process.nextTick(emitClose, this);
|
|
3455
|
+
return;
|
|
3456
|
+
}
|
|
3457
|
+
if (cb)
|
|
3458
|
+
this.once("close", cb);
|
|
3459
|
+
if (this._state === CLOSING)
|
|
3460
|
+
return;
|
|
3461
|
+
this._state = CLOSING;
|
|
3462
|
+
if (this.options.noServer || this.options.server) {
|
|
3463
|
+
if (this._server) {
|
|
3464
|
+
this._removeListeners();
|
|
3465
|
+
this._removeListeners = this._server = null;
|
|
3466
|
+
}
|
|
3467
|
+
if (this.clients) {
|
|
3468
|
+
if (!this.clients.size) {
|
|
3469
|
+
process.nextTick(emitClose, this);
|
|
3470
|
+
} else {
|
|
3471
|
+
this._shouldEmitClose = true;
|
|
3472
|
+
}
|
|
3473
|
+
} else {
|
|
3474
|
+
process.nextTick(emitClose, this);
|
|
3475
|
+
}
|
|
3476
|
+
} else {
|
|
3477
|
+
const server = this._server;
|
|
3478
|
+
this._removeListeners();
|
|
3479
|
+
this._removeListeners = this._server = null;
|
|
3480
|
+
server.close(() => {
|
|
3481
|
+
emitClose(this);
|
|
3482
|
+
});
|
|
3483
|
+
}
|
|
3484
|
+
}
|
|
3485
|
+
/**
|
|
3486
|
+
* See if a given request should be handled by this server instance.
|
|
3487
|
+
*
|
|
3488
|
+
* @param {http.IncomingMessage} req Request object to inspect
|
|
3489
|
+
* @return {Boolean} `true` if the request is valid, else `false`
|
|
3490
|
+
* @public
|
|
3491
|
+
*/
|
|
3492
|
+
shouldHandle(req) {
|
|
3493
|
+
if (this.options.path) {
|
|
3494
|
+
const index = req.url.indexOf("?");
|
|
3495
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
|
3496
|
+
if (pathname !== this.options.path)
|
|
3497
|
+
return false;
|
|
3498
|
+
}
|
|
3499
|
+
return true;
|
|
3500
|
+
}
|
|
3501
|
+
/**
|
|
3502
|
+
* Handle a HTTP Upgrade request.
|
|
3503
|
+
*
|
|
3504
|
+
* @param {http.IncomingMessage} req The request object
|
|
3505
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
3506
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
3507
|
+
* @param {Function} cb Callback
|
|
3508
|
+
* @public
|
|
3509
|
+
*/
|
|
3510
|
+
handleUpgrade(req, socket, head, cb) {
|
|
3511
|
+
socket.on("error", socketOnError);
|
|
3512
|
+
const key = req.headers["sec-websocket-key"];
|
|
3513
|
+
const upgrade = req.headers.upgrade;
|
|
3514
|
+
const version = +req.headers["sec-websocket-version"];
|
|
3515
|
+
if (req.method !== "GET") {
|
|
3516
|
+
const message = "Invalid HTTP method";
|
|
3517
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
|
|
3518
|
+
return;
|
|
3519
|
+
}
|
|
3520
|
+
if (upgrade === void 0 || upgrade.toLowerCase() !== "websocket") {
|
|
3521
|
+
const message = "Invalid Upgrade header";
|
|
3522
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3523
|
+
return;
|
|
3524
|
+
}
|
|
3525
|
+
if (key === void 0 || !keyRegex.test(key)) {
|
|
3526
|
+
const message = "Missing or invalid Sec-WebSocket-Key header";
|
|
3527
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3528
|
+
return;
|
|
3529
|
+
}
|
|
3530
|
+
if (version !== 13 && version !== 8) {
|
|
3531
|
+
const message = "Missing or invalid Sec-WebSocket-Version header";
|
|
3532
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
|
|
3533
|
+
"Sec-WebSocket-Version": "13, 8"
|
|
3534
|
+
});
|
|
3535
|
+
return;
|
|
3536
|
+
}
|
|
3537
|
+
if (!this.shouldHandle(req)) {
|
|
3538
|
+
abortHandshake(socket, 400);
|
|
3539
|
+
return;
|
|
3540
|
+
}
|
|
3541
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
3542
|
+
let protocols = /* @__PURE__ */ new Set();
|
|
3543
|
+
if (secWebSocketProtocol !== void 0) {
|
|
3544
|
+
try {
|
|
3545
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
3546
|
+
} catch (err) {
|
|
3547
|
+
const message = "Invalid Sec-WebSocket-Protocol header";
|
|
3548
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3549
|
+
return;
|
|
3550
|
+
}
|
|
3551
|
+
}
|
|
3552
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
3553
|
+
const extensions = {};
|
|
3554
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== void 0) {
|
|
3555
|
+
const perMessageDeflate = new PerMessageDeflate(
|
|
3556
|
+
this.options.perMessageDeflate,
|
|
3557
|
+
true,
|
|
3558
|
+
this.options.maxPayload
|
|
3559
|
+
);
|
|
3560
|
+
try {
|
|
3561
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
3562
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
3563
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
3564
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
3565
|
+
}
|
|
3566
|
+
} catch (err) {
|
|
3567
|
+
const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
3568
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
3569
|
+
return;
|
|
3570
|
+
}
|
|
3571
|
+
}
|
|
3572
|
+
if (this.options.verifyClient) {
|
|
3573
|
+
const info = {
|
|
3574
|
+
origin: req.headers[`${version === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
3575
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
3576
|
+
req
|
|
3577
|
+
};
|
|
3578
|
+
if (this.options.verifyClient.length === 2) {
|
|
3579
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
3580
|
+
if (!verified) {
|
|
3581
|
+
return abortHandshake(socket, code || 401, message, headers);
|
|
3582
|
+
}
|
|
3583
|
+
this.completeUpgrade(
|
|
3584
|
+
extensions,
|
|
3585
|
+
key,
|
|
3586
|
+
protocols,
|
|
3587
|
+
req,
|
|
3588
|
+
socket,
|
|
3589
|
+
head,
|
|
3590
|
+
cb
|
|
3591
|
+
);
|
|
3592
|
+
});
|
|
3593
|
+
return;
|
|
3594
|
+
}
|
|
3595
|
+
if (!this.options.verifyClient(info))
|
|
3596
|
+
return abortHandshake(socket, 401);
|
|
3597
|
+
}
|
|
3598
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
3599
|
+
}
|
|
3600
|
+
/**
|
|
3601
|
+
* Upgrade the connection to WebSocket.
|
|
3602
|
+
*
|
|
3603
|
+
* @param {Object} extensions The accepted extensions
|
|
3604
|
+
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
|
3605
|
+
* @param {Set} protocols The subprotocols
|
|
3606
|
+
* @param {http.IncomingMessage} req The request object
|
|
3607
|
+
* @param {Duplex} socket The network socket between the server and client
|
|
3608
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
|
3609
|
+
* @param {Function} cb Callback
|
|
3610
|
+
* @throws {Error} If called more than once with the same socket
|
|
3611
|
+
* @private
|
|
3612
|
+
*/
|
|
3613
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
3614
|
+
if (!socket.readable || !socket.writable)
|
|
3615
|
+
return socket.destroy();
|
|
3616
|
+
if (socket[kWebSocket]) {
|
|
3617
|
+
throw new Error(
|
|
3618
|
+
"server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration"
|
|
3619
|
+
);
|
|
3620
|
+
}
|
|
3621
|
+
if (this._state > RUNNING)
|
|
3622
|
+
return abortHandshake(socket, 503);
|
|
3623
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
3624
|
+
const headers = [
|
|
3625
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
3626
|
+
"Upgrade: websocket",
|
|
3627
|
+
"Connection: Upgrade",
|
|
3628
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
3629
|
+
];
|
|
3630
|
+
const ws = new this.options.WebSocket(null, void 0, this.options);
|
|
3631
|
+
if (protocols.size) {
|
|
3632
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
3633
|
+
if (protocol) {
|
|
3634
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
3635
|
+
ws._protocol = protocol;
|
|
3636
|
+
}
|
|
3637
|
+
}
|
|
3638
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
3639
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
3640
|
+
const value = extension.format({
|
|
3641
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
3642
|
+
});
|
|
3643
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
3644
|
+
ws._extensions = extensions;
|
|
3645
|
+
}
|
|
3646
|
+
this.emit("headers", headers, req);
|
|
3647
|
+
socket.write(headers.concat("\r\n").join("\r\n"));
|
|
3648
|
+
socket.removeListener("error", socketOnError);
|
|
3649
|
+
ws.setSocket(socket, head, {
|
|
3650
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
3651
|
+
maxPayload: this.options.maxPayload,
|
|
3652
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
3653
|
+
});
|
|
3654
|
+
if (this.clients) {
|
|
3655
|
+
this.clients.add(ws);
|
|
3656
|
+
ws.on("close", () => {
|
|
3657
|
+
this.clients.delete(ws);
|
|
3658
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
3659
|
+
process.nextTick(emitClose, this);
|
|
3660
|
+
}
|
|
3661
|
+
});
|
|
3662
|
+
}
|
|
3663
|
+
cb(ws, req);
|
|
3664
|
+
}
|
|
3665
|
+
};
|
|
3666
|
+
module.exports = WebSocketServer;
|
|
3667
|
+
function addListeners(server, map) {
|
|
3668
|
+
for (const event of Object.keys(map))
|
|
3669
|
+
server.on(event, map[event]);
|
|
3670
|
+
return function removeListeners() {
|
|
3671
|
+
for (const event of Object.keys(map)) {
|
|
3672
|
+
server.removeListener(event, map[event]);
|
|
3673
|
+
}
|
|
3674
|
+
};
|
|
3675
|
+
}
|
|
3676
|
+
function emitClose(server) {
|
|
3677
|
+
server._state = CLOSED;
|
|
3678
|
+
server.emit("close");
|
|
3679
|
+
}
|
|
3680
|
+
function socketOnError() {
|
|
3681
|
+
this.destroy();
|
|
3682
|
+
}
|
|
3683
|
+
function abortHandshake(socket, code, message, headers) {
|
|
3684
|
+
message = message || http.STATUS_CODES[code];
|
|
3685
|
+
headers = {
|
|
3686
|
+
Connection: "close",
|
|
3687
|
+
"Content-Type": "text/html",
|
|
3688
|
+
"Content-Length": Buffer.byteLength(message),
|
|
3689
|
+
...headers
|
|
3690
|
+
};
|
|
3691
|
+
socket.once("finish", socket.destroy);
|
|
3692
|
+
socket.end(
|
|
3693
|
+
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
3694
|
+
` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join("\r\n") + "\r\n\r\n" + message
|
|
3695
|
+
);
|
|
3696
|
+
}
|
|
3697
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
3698
|
+
if (server.listenerCount("wsClientError")) {
|
|
3699
|
+
const err = new Error(message);
|
|
3700
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
3701
|
+
server.emit("wsClientError", err, socket, req);
|
|
3702
|
+
} else {
|
|
3703
|
+
abortHandshake(socket, code, message, headers);
|
|
3704
|
+
}
|
|
3705
|
+
}
|
|
3706
|
+
}
|
|
3707
|
+
});
|
|
3708
|
+
|
|
3709
|
+
// ../../../node_modules/ws/index.js
|
|
3710
|
+
var require_ws = __commonJS({
|
|
3711
|
+
"../../../node_modules/ws/index.js"(exports$1, module) {
|
|
3712
|
+
var WebSocket2 = require_websocket();
|
|
3713
|
+
WebSocket2.createWebSocketStream = require_stream();
|
|
3714
|
+
WebSocket2.Server = require_websocket_server();
|
|
3715
|
+
WebSocket2.Receiver = require_receiver();
|
|
3716
|
+
WebSocket2.Sender = require_sender();
|
|
3717
|
+
WebSocket2.WebSocket = WebSocket2;
|
|
3718
|
+
WebSocket2.WebSocketServer = WebSocket2.Server;
|
|
3719
|
+
module.exports = WebSocket2;
|
|
3720
|
+
}
|
|
3721
|
+
});
|
|
7
3722
|
|
|
8
3723
|
// ../core/src/platform.ts
|
|
9
3724
|
function detectPlatform() {
|
|
@@ -36,7 +3751,8 @@ var isServer = isNode || isDeno;
|
|
|
36
3751
|
var WebStorageAdapter = class {
|
|
37
3752
|
getItem(key) {
|
|
38
3753
|
try {
|
|
39
|
-
if (typeof localStorage === "undefined")
|
|
3754
|
+
if (typeof localStorage === "undefined")
|
|
3755
|
+
return null;
|
|
40
3756
|
return localStorage.getItem(key);
|
|
41
3757
|
} catch (error) {
|
|
42
3758
|
console.warn("Failed to get item from localStorage:", error);
|
|
@@ -45,7 +3761,8 @@ var WebStorageAdapter = class {
|
|
|
45
3761
|
}
|
|
46
3762
|
setItem(key, value) {
|
|
47
3763
|
try {
|
|
48
|
-
if (typeof localStorage === "undefined")
|
|
3764
|
+
if (typeof localStorage === "undefined")
|
|
3765
|
+
return;
|
|
49
3766
|
localStorage.setItem(key, value);
|
|
50
3767
|
} catch (error) {
|
|
51
3768
|
console.warn("Failed to set item in localStorage:", error);
|
|
@@ -53,7 +3770,8 @@ var WebStorageAdapter = class {
|
|
|
53
3770
|
}
|
|
54
3771
|
removeItem(key) {
|
|
55
3772
|
try {
|
|
56
|
-
if (typeof localStorage === "undefined")
|
|
3773
|
+
if (typeof localStorage === "undefined")
|
|
3774
|
+
return;
|
|
57
3775
|
localStorage.removeItem(key);
|
|
58
3776
|
} catch (error) {
|
|
59
3777
|
console.warn("Failed to remove item from localStorage:", error);
|
|
@@ -61,7 +3779,8 @@ var WebStorageAdapter = class {
|
|
|
61
3779
|
}
|
|
62
3780
|
clear() {
|
|
63
3781
|
try {
|
|
64
|
-
if (typeof localStorage === "undefined")
|
|
3782
|
+
if (typeof localStorage === "undefined")
|
|
3783
|
+
return;
|
|
65
3784
|
localStorage.clear();
|
|
66
3785
|
} catch (error) {
|
|
67
3786
|
console.warn("Failed to clear localStorage:", error);
|
|
@@ -235,7 +3954,8 @@ function camelToSnake(str) {
|
|
|
235
3954
|
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
236
3955
|
}
|
|
237
3956
|
function convertFilterKeysToSnakeCase(condition) {
|
|
238
|
-
if (!condition)
|
|
3957
|
+
if (!condition)
|
|
3958
|
+
return condition;
|
|
239
3959
|
if ("AND" in condition) {
|
|
240
3960
|
return {
|
|
241
3961
|
AND: condition.AND?.map(convertFilterKeysToSnakeCase)
|
|
@@ -254,7 +3974,8 @@ function convertFilterKeysToSnakeCase(condition) {
|
|
|
254
3974
|
return converted;
|
|
255
3975
|
}
|
|
256
3976
|
function buildFilterQuery(condition) {
|
|
257
|
-
if (!condition)
|
|
3977
|
+
if (!condition)
|
|
3978
|
+
return "";
|
|
258
3979
|
if ("AND" in condition) {
|
|
259
3980
|
const andConditions = condition.AND?.map(buildFilterQuery).filter(Boolean) || [];
|
|
260
3981
|
return andConditions.length > 0 ? `and=(${andConditions.join(",")})` : "";
|
|
@@ -265,11 +3986,13 @@ function buildFilterQuery(condition) {
|
|
|
265
3986
|
}
|
|
266
3987
|
const params = [];
|
|
267
3988
|
for (const [field, value] of Object.entries(condition)) {
|
|
268
|
-
if (value === void 0 || value === null)
|
|
3989
|
+
if (value === void 0 || value === null)
|
|
3990
|
+
continue;
|
|
269
3991
|
if (typeof value === "object" && !Array.isArray(value)) {
|
|
270
3992
|
for (const [operator, operatorValue] of Object.entries(value)) {
|
|
271
3993
|
const param = buildOperatorQuery(field, operator, operatorValue);
|
|
272
|
-
if (param)
|
|
3994
|
+
if (param)
|
|
3995
|
+
params.push(param);
|
|
273
3996
|
}
|
|
274
3997
|
} else {
|
|
275
3998
|
params.push(`${field}=eq.${encodeQueryValue(value)}`);
|
|
@@ -316,12 +4039,46 @@ function buildOperatorQuery(field, operator, value) {
|
|
|
316
4039
|
}
|
|
317
4040
|
}
|
|
318
4041
|
function encodeQueryValue(value) {
|
|
319
|
-
if (value === null)
|
|
4042
|
+
if (value === null)
|
|
4043
|
+
return "null";
|
|
320
4044
|
if (typeof value === "boolean") {
|
|
321
4045
|
return value ? "1" : "0";
|
|
322
4046
|
}
|
|
323
|
-
if (typeof value === "number")
|
|
324
|
-
|
|
4047
|
+
if (typeof value === "number")
|
|
4048
|
+
return value.toString();
|
|
4049
|
+
return String(value);
|
|
4050
|
+
}
|
|
4051
|
+
function collectFilterParams(condition, params) {
|
|
4052
|
+
if (!condition)
|
|
4053
|
+
return;
|
|
4054
|
+
if ("AND" in condition) {
|
|
4055
|
+
const andConditions = condition.AND?.map(buildFilterQuery).filter(Boolean) || [];
|
|
4056
|
+
if (andConditions.length > 0)
|
|
4057
|
+
params["and"] = `(${andConditions.join(",")})`;
|
|
4058
|
+
return;
|
|
4059
|
+
}
|
|
4060
|
+
if ("OR" in condition) {
|
|
4061
|
+
const orConditions = condition.OR?.map(buildFilterQuery).filter(Boolean) || [];
|
|
4062
|
+
if (orConditions.length > 0)
|
|
4063
|
+
params["or"] = `(${orConditions.join(",")})`;
|
|
4064
|
+
return;
|
|
4065
|
+
}
|
|
4066
|
+
for (const [field, value] of Object.entries(condition)) {
|
|
4067
|
+
if (value === void 0 || value === null)
|
|
4068
|
+
continue;
|
|
4069
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
4070
|
+
for (const [operator, operatorValue] of Object.entries(value)) {
|
|
4071
|
+
const param = buildOperatorQuery(field, operator, operatorValue);
|
|
4072
|
+
if (param) {
|
|
4073
|
+
const eqIdx = param.indexOf("=");
|
|
4074
|
+
if (eqIdx !== -1)
|
|
4075
|
+
params[param.slice(0, eqIdx)] = param.slice(eqIdx + 1);
|
|
4076
|
+
}
|
|
4077
|
+
}
|
|
4078
|
+
} else {
|
|
4079
|
+
params[field] = `eq.${encodeQueryValue(value)}`;
|
|
4080
|
+
}
|
|
4081
|
+
}
|
|
325
4082
|
}
|
|
326
4083
|
function buildQuery(options = {}) {
|
|
327
4084
|
const params = {};
|
|
@@ -332,17 +4089,7 @@ function buildQuery(options = {}) {
|
|
|
332
4089
|
params.select = "*";
|
|
333
4090
|
}
|
|
334
4091
|
if (options.where) {
|
|
335
|
-
|
|
336
|
-
const filterQuery = buildFilterQuery(convertedWhere);
|
|
337
|
-
if (filterQuery) {
|
|
338
|
-
const filterParams = filterQuery.split("&");
|
|
339
|
-
for (const param of filterParams) {
|
|
340
|
-
const [key, value] = param.split("=", 2);
|
|
341
|
-
if (key && value) {
|
|
342
|
-
params[key] = value;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
4092
|
+
collectFilterParams(convertFilterKeysToSnakeCase(options.where), params);
|
|
346
4093
|
}
|
|
347
4094
|
if (options.orderBy) {
|
|
348
4095
|
if (typeof options.orderBy === "string") {
|
|
@@ -372,9 +4119,12 @@ function snakeToCamel(str) {
|
|
|
372
4119
|
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
373
4120
|
}
|
|
374
4121
|
function convertKeysToSnakeCase(obj) {
|
|
375
|
-
if (obj === null || obj === void 0)
|
|
376
|
-
|
|
377
|
-
if (
|
|
4122
|
+
if (obj === null || obj === void 0)
|
|
4123
|
+
return obj;
|
|
4124
|
+
if (typeof obj !== "object")
|
|
4125
|
+
return obj;
|
|
4126
|
+
if (Array.isArray(obj))
|
|
4127
|
+
return obj.map(convertKeysToSnakeCase);
|
|
378
4128
|
const converted = {};
|
|
379
4129
|
for (const [key, value] of Object.entries(obj)) {
|
|
380
4130
|
const snakeKey = camelToSnake2(key);
|
|
@@ -383,9 +4133,12 @@ function convertKeysToSnakeCase(obj) {
|
|
|
383
4133
|
return converted;
|
|
384
4134
|
}
|
|
385
4135
|
function convertKeysToCamelCase(obj) {
|
|
386
|
-
if (obj === null || obj === void 0)
|
|
387
|
-
|
|
388
|
-
if (
|
|
4136
|
+
if (obj === null || obj === void 0)
|
|
4137
|
+
return obj;
|
|
4138
|
+
if (typeof obj !== "object")
|
|
4139
|
+
return obj;
|
|
4140
|
+
if (Array.isArray(obj))
|
|
4141
|
+
return obj.map(convertKeysToCamelCase);
|
|
389
4142
|
const converted = {};
|
|
390
4143
|
for (const [key, value] of Object.entries(obj)) {
|
|
391
4144
|
const camelKey = snakeToCamel(key);
|
|
@@ -410,10 +4163,14 @@ var HttpClient = class {
|
|
|
410
4163
|
this.getValidToken = getValidToken;
|
|
411
4164
|
}
|
|
412
4165
|
shouldAttachPublishableKey(path, method) {
|
|
413
|
-
if (method !== "GET" && method !== "POST")
|
|
414
|
-
|
|
415
|
-
if (path.includes("/api/
|
|
416
|
-
|
|
4166
|
+
if (method !== "GET" && method !== "POST")
|
|
4167
|
+
return false;
|
|
4168
|
+
if (path.includes("/api/analytics/"))
|
|
4169
|
+
return true;
|
|
4170
|
+
if (path.includes("/api/storage/"))
|
|
4171
|
+
return true;
|
|
4172
|
+
if (path.includes("/api/db/") && path.includes("/rest/v1/"))
|
|
4173
|
+
return method === "GET";
|
|
417
4174
|
return false;
|
|
418
4175
|
}
|
|
419
4176
|
shouldSkipSecretKey(url) {
|
|
@@ -746,7 +4503,8 @@ var HttpClient = class {
|
|
|
746
4503
|
"Content-Type": "application/json"
|
|
747
4504
|
};
|
|
748
4505
|
const auth = this.getAuthorizationHeader(url, token);
|
|
749
|
-
if (auth)
|
|
4506
|
+
if (auth)
|
|
4507
|
+
headers.Authorization = auth;
|
|
750
4508
|
const body = {
|
|
751
4509
|
prompt,
|
|
752
4510
|
stream: true,
|
|
@@ -800,7 +4558,8 @@ var HttpClient = class {
|
|
|
800
4558
|
"Content-Type": "application/json"
|
|
801
4559
|
};
|
|
802
4560
|
const auth = this.getAuthorizationHeader(url, token);
|
|
803
|
-
if (auth)
|
|
4561
|
+
if (auth)
|
|
4562
|
+
headers.Authorization = auth;
|
|
804
4563
|
const body = {
|
|
805
4564
|
prompt,
|
|
806
4565
|
stream: true,
|
|
@@ -827,7 +4586,8 @@ var HttpClient = class {
|
|
|
827
4586
|
try {
|
|
828
4587
|
while (true) {
|
|
829
4588
|
const { done, value } = await reader.read();
|
|
830
|
-
if (done)
|
|
4589
|
+
if (done)
|
|
4590
|
+
break;
|
|
831
4591
|
const chunk = decoder.decode(value, { stream: true });
|
|
832
4592
|
buffer += chunk;
|
|
833
4593
|
try {
|
|
@@ -938,7 +4698,8 @@ var HttpClient = class {
|
|
|
938
4698
|
"Content-Type": "application/json"
|
|
939
4699
|
};
|
|
940
4700
|
const auth = this.getAuthorizationHeader(url, token);
|
|
941
|
-
if (auth)
|
|
4701
|
+
if (auth)
|
|
4702
|
+
headers.Authorization = auth;
|
|
942
4703
|
const response = await fetch(url, {
|
|
943
4704
|
method: "POST",
|
|
944
4705
|
headers,
|
|
@@ -961,7 +4722,8 @@ var HttpClient = class {
|
|
|
961
4722
|
"Content-Type": "application/json"
|
|
962
4723
|
};
|
|
963
4724
|
const auth = this.getAuthorizationHeader(url, token);
|
|
964
|
-
if (auth)
|
|
4725
|
+
if (auth)
|
|
4726
|
+
headers.Authorization = auth;
|
|
965
4727
|
const response = await fetch(url, {
|
|
966
4728
|
method: "POST",
|
|
967
4729
|
headers,
|
|
@@ -1129,16 +4891,19 @@ var HttpClient = class {
|
|
|
1129
4891
|
try {
|
|
1130
4892
|
while (true) {
|
|
1131
4893
|
const { done, value } = await reader.read();
|
|
1132
|
-
if (done)
|
|
4894
|
+
if (done)
|
|
4895
|
+
break;
|
|
1133
4896
|
buffer += decoder.decode(value, { stream: true });
|
|
1134
4897
|
const lines = buffer.split("\n");
|
|
1135
4898
|
buffer = lines.pop() || "";
|
|
1136
4899
|
for (const line of lines) {
|
|
1137
|
-
if (!line.trim())
|
|
4900
|
+
if (!line.trim())
|
|
4901
|
+
continue;
|
|
1138
4902
|
if (line === "[DONE]") {
|
|
1139
4903
|
continue;
|
|
1140
4904
|
}
|
|
1141
|
-
if (!line.startsWith("data: "))
|
|
4905
|
+
if (!line.startsWith("data: "))
|
|
4906
|
+
continue;
|
|
1142
4907
|
try {
|
|
1143
4908
|
const jsonStr = line.slice(6);
|
|
1144
4909
|
const part = JSON.parse(jsonStr);
|
|
@@ -1148,11 +4913,13 @@ var HttpClient = class {
|
|
|
1148
4913
|
case "text-delta":
|
|
1149
4914
|
if (part.delta) {
|
|
1150
4915
|
finalResult.text += part.delta;
|
|
1151
|
-
if (onChunk)
|
|
4916
|
+
if (onChunk)
|
|
4917
|
+
onChunk(part.delta);
|
|
1152
4918
|
}
|
|
1153
4919
|
if (part.textDelta) {
|
|
1154
4920
|
finalResult.text += part.textDelta;
|
|
1155
|
-
if (onChunk)
|
|
4921
|
+
if (onChunk)
|
|
4922
|
+
onChunk(part.textDelta);
|
|
1156
4923
|
}
|
|
1157
4924
|
break;
|
|
1158
4925
|
case "text-end":
|
|
@@ -1187,13 +4954,15 @@ var HttpClient = class {
|
|
|
1187
4954
|
case "finish":
|
|
1188
4955
|
finalResult.finishReason = part.finishReason;
|
|
1189
4956
|
finalResult.usage = part.usage;
|
|
1190
|
-
if (part.response)
|
|
4957
|
+
if (part.response)
|
|
4958
|
+
finalResult.response = part.response;
|
|
1191
4959
|
break;
|
|
1192
4960
|
case "error":
|
|
1193
4961
|
finalResult.error = part.error;
|
|
1194
4962
|
throw new Error(part.error);
|
|
1195
4963
|
case "data":
|
|
1196
|
-
if (!finalResult.customData)
|
|
4964
|
+
if (!finalResult.customData)
|
|
4965
|
+
finalResult.customData = [];
|
|
1197
4966
|
finalResult.customData.push(part.value);
|
|
1198
4967
|
break;
|
|
1199
4968
|
}
|
|
@@ -1222,7 +4991,8 @@ function isReactNative2() {
|
|
|
1222
4991
|
return typeof navigator !== "undefined" && navigator.product === "ReactNative";
|
|
1223
4992
|
}
|
|
1224
4993
|
function getWindowLocation() {
|
|
1225
|
-
if (!hasWindow())
|
|
4994
|
+
if (!hasWindow())
|
|
4995
|
+
return null;
|
|
1226
4996
|
try {
|
|
1227
4997
|
return window.location;
|
|
1228
4998
|
} catch {
|
|
@@ -1231,7 +5001,8 @@ function getWindowLocation() {
|
|
|
1231
5001
|
}
|
|
1232
5002
|
function getLocationHref() {
|
|
1233
5003
|
const loc = getWindowLocation();
|
|
1234
|
-
if (!loc)
|
|
5004
|
+
if (!loc)
|
|
5005
|
+
return null;
|
|
1235
5006
|
try {
|
|
1236
5007
|
return loc.href;
|
|
1237
5008
|
} catch {
|
|
@@ -1240,7 +5011,8 @@ function getLocationHref() {
|
|
|
1240
5011
|
}
|
|
1241
5012
|
function getLocationOrigin() {
|
|
1242
5013
|
const loc = getWindowLocation();
|
|
1243
|
-
if (!loc)
|
|
5014
|
+
if (!loc)
|
|
5015
|
+
return null;
|
|
1244
5016
|
try {
|
|
1245
5017
|
return loc.origin;
|
|
1246
5018
|
} catch {
|
|
@@ -1249,7 +5021,8 @@ function getLocationOrigin() {
|
|
|
1249
5021
|
}
|
|
1250
5022
|
function getLocationHostname() {
|
|
1251
5023
|
const loc = getWindowLocation();
|
|
1252
|
-
if (!loc)
|
|
5024
|
+
if (!loc)
|
|
5025
|
+
return null;
|
|
1253
5026
|
try {
|
|
1254
5027
|
return loc.hostname;
|
|
1255
5028
|
} catch {
|
|
@@ -1258,7 +5031,8 @@ function getLocationHostname() {
|
|
|
1258
5031
|
}
|
|
1259
5032
|
function getLocationPathname() {
|
|
1260
5033
|
const loc = getWindowLocation();
|
|
1261
|
-
if (!loc)
|
|
5034
|
+
if (!loc)
|
|
5035
|
+
return null;
|
|
1262
5036
|
try {
|
|
1263
5037
|
return loc.pathname;
|
|
1264
5038
|
} catch {
|
|
@@ -1267,7 +5041,8 @@ function getLocationPathname() {
|
|
|
1267
5041
|
}
|
|
1268
5042
|
function getLocationSearch() {
|
|
1269
5043
|
const loc = getWindowLocation();
|
|
1270
|
-
if (!loc)
|
|
5044
|
+
if (!loc)
|
|
5045
|
+
return null;
|
|
1271
5046
|
try {
|
|
1272
5047
|
return loc.search;
|
|
1273
5048
|
} catch {
|
|
@@ -1276,7 +5051,8 @@ function getLocationSearch() {
|
|
|
1276
5051
|
}
|
|
1277
5052
|
function getLocationHash() {
|
|
1278
5053
|
const loc = getWindowLocation();
|
|
1279
|
-
if (!loc)
|
|
5054
|
+
if (!loc)
|
|
5055
|
+
return null;
|
|
1280
5056
|
try {
|
|
1281
5057
|
return loc.hash;
|
|
1282
5058
|
} catch {
|
|
@@ -1285,7 +5061,8 @@ function getLocationHash() {
|
|
|
1285
5061
|
}
|
|
1286
5062
|
function getLocationProtocol() {
|
|
1287
5063
|
const loc = getWindowLocation();
|
|
1288
|
-
if (!loc)
|
|
5064
|
+
if (!loc)
|
|
5065
|
+
return null;
|
|
1289
5066
|
try {
|
|
1290
5067
|
return loc.protocol;
|
|
1291
5068
|
} catch {
|
|
@@ -1294,7 +5071,8 @@ function getLocationProtocol() {
|
|
|
1294
5071
|
}
|
|
1295
5072
|
function getLocationHost() {
|
|
1296
5073
|
const loc = getWindowLocation();
|
|
1297
|
-
if (!loc)
|
|
5074
|
+
if (!loc)
|
|
5075
|
+
return null;
|
|
1298
5076
|
try {
|
|
1299
5077
|
return loc.host;
|
|
1300
5078
|
} catch {
|
|
@@ -1302,17 +5080,20 @@ function getLocationHost() {
|
|
|
1302
5080
|
}
|
|
1303
5081
|
}
|
|
1304
5082
|
function constructFullUrl() {
|
|
1305
|
-
if (!hasWindow())
|
|
5083
|
+
if (!hasWindow())
|
|
5084
|
+
return null;
|
|
1306
5085
|
const protocol = getLocationProtocol();
|
|
1307
5086
|
const host = getLocationHost();
|
|
1308
5087
|
const pathname = getLocationPathname();
|
|
1309
5088
|
const search = getLocationSearch();
|
|
1310
5089
|
const hash = getLocationHash();
|
|
1311
|
-
if (!protocol || !host)
|
|
5090
|
+
if (!protocol || !host)
|
|
5091
|
+
return null;
|
|
1312
5092
|
return `${protocol}//${host}${pathname || ""}${search || ""}${hash || ""}`;
|
|
1313
5093
|
}
|
|
1314
5094
|
function getDocumentReferrer() {
|
|
1315
|
-
if (!hasDocument())
|
|
5095
|
+
if (!hasDocument())
|
|
5096
|
+
return null;
|
|
1316
5097
|
try {
|
|
1317
5098
|
return document.referrer || null;
|
|
1318
5099
|
} catch {
|
|
@@ -1320,7 +5101,8 @@ function getDocumentReferrer() {
|
|
|
1320
5101
|
}
|
|
1321
5102
|
}
|
|
1322
5103
|
function getWindowInnerWidth() {
|
|
1323
|
-
if (!hasWindow())
|
|
5104
|
+
if (!hasWindow())
|
|
5105
|
+
return null;
|
|
1324
5106
|
try {
|
|
1325
5107
|
return window.innerWidth;
|
|
1326
5108
|
} catch {
|
|
@@ -1328,7 +5110,8 @@ function getWindowInnerWidth() {
|
|
|
1328
5110
|
}
|
|
1329
5111
|
}
|
|
1330
5112
|
function isIframe() {
|
|
1331
|
-
if (!hasWindow())
|
|
5113
|
+
if (!hasWindow())
|
|
5114
|
+
return false;
|
|
1332
5115
|
try {
|
|
1333
5116
|
return window.self !== window.top;
|
|
1334
5117
|
} catch {
|
|
@@ -1336,7 +5119,8 @@ function isIframe() {
|
|
|
1336
5119
|
}
|
|
1337
5120
|
}
|
|
1338
5121
|
function getSessionStorage() {
|
|
1339
|
-
if (!hasWindow())
|
|
5122
|
+
if (!hasWindow())
|
|
5123
|
+
return null;
|
|
1340
5124
|
try {
|
|
1341
5125
|
return window.sessionStorage;
|
|
1342
5126
|
} catch {
|
|
@@ -1415,7 +5199,8 @@ var BlinkAuth = class {
|
|
|
1415
5199
|
* Wait for authentication initialization to complete
|
|
1416
5200
|
*/
|
|
1417
5201
|
async waitForInitialization() {
|
|
1418
|
-
if (this.isInitialized)
|
|
5202
|
+
if (this.isInitialized)
|
|
5203
|
+
return;
|
|
1419
5204
|
if (this.initializationPromise) {
|
|
1420
5205
|
await this.initializationPromise;
|
|
1421
5206
|
}
|
|
@@ -1424,7 +5209,8 @@ var BlinkAuth = class {
|
|
|
1424
5209
|
* Setup listener for tokens from parent window
|
|
1425
5210
|
*/
|
|
1426
5211
|
setupParentWindowListener() {
|
|
1427
|
-
if (!isWeb || !this.isIframe || !hasWindow())
|
|
5212
|
+
if (!isWeb || !this.isIframe || !hasWindow())
|
|
5213
|
+
return;
|
|
1428
5214
|
window.addEventListener("message", (event) => {
|
|
1429
5215
|
if (event.origin !== "https://blink.new" && event.origin !== "http://localhost:3000" && event.origin !== "http://localhost:3001") {
|
|
1430
5216
|
return;
|
|
@@ -2133,21 +5919,26 @@ var BlinkAuth = class {
|
|
|
2133
5919
|
let closedIntervalId;
|
|
2134
5920
|
let cleanedUp = false;
|
|
2135
5921
|
const cleanup = () => {
|
|
2136
|
-
if (cleanedUp)
|
|
5922
|
+
if (cleanedUp)
|
|
5923
|
+
return;
|
|
2137
5924
|
cleanedUp = true;
|
|
2138
5925
|
clearTimeout(timeoutId);
|
|
2139
|
-
if (closedIntervalId)
|
|
5926
|
+
if (closedIntervalId)
|
|
5927
|
+
clearInterval(closedIntervalId);
|
|
2140
5928
|
window.removeEventListener("message", messageListener);
|
|
2141
5929
|
};
|
|
2142
5930
|
const messageListener = (event) => {
|
|
2143
5931
|
let allowed = false;
|
|
2144
5932
|
try {
|
|
2145
5933
|
const authOrigin = new URL(this.authUrl).origin;
|
|
2146
|
-
if (event.origin === authOrigin)
|
|
5934
|
+
if (event.origin === authOrigin)
|
|
5935
|
+
allowed = true;
|
|
2147
5936
|
} catch {
|
|
2148
5937
|
}
|
|
2149
|
-
if (event.origin === "http://localhost:3000" || event.origin === "http://localhost:3001")
|
|
2150
|
-
|
|
5938
|
+
if (event.origin === "http://localhost:3000" || event.origin === "http://localhost:3001")
|
|
5939
|
+
allowed = true;
|
|
5940
|
+
if (!allowed)
|
|
5941
|
+
return;
|
|
2151
5942
|
if (event.data?.type === "BLINK_AUTH_TOKENS") {
|
|
2152
5943
|
const { access_token, refresh_token, token_type, expires_in, refresh_expires_in, projectId, state: returnedState } = event.data;
|
|
2153
5944
|
try {
|
|
@@ -2589,21 +6380,27 @@ var BlinkAuth = class {
|
|
|
2589
6380
|
}
|
|
2590
6381
|
const visited = /* @__PURE__ */ new Set();
|
|
2591
6382
|
const hasPermissionInRole = (roleName) => {
|
|
2592
|
-
if (visited.has(roleName))
|
|
6383
|
+
if (visited.has(roleName))
|
|
6384
|
+
return false;
|
|
2593
6385
|
visited.add(roleName);
|
|
2594
6386
|
const rc = roles[roleName];
|
|
2595
|
-
if (!rc)
|
|
2596
|
-
|
|
6387
|
+
if (!rc)
|
|
6388
|
+
return false;
|
|
6389
|
+
if (rc.permissions.includes("*"))
|
|
6390
|
+
return true;
|
|
2597
6391
|
const fullPermission2 = resource ? `${permission}.${resource}` : permission;
|
|
2598
|
-
if (rc.permissions.includes(fullPermission2) || rc.permissions.includes(permission))
|
|
6392
|
+
if (rc.permissions.includes(fullPermission2) || rc.permissions.includes(permission))
|
|
6393
|
+
return true;
|
|
2599
6394
|
if (rc.inherit) {
|
|
2600
6395
|
for (const parent of rc.inherit) {
|
|
2601
|
-
if (hasPermissionInRole(parent))
|
|
6396
|
+
if (hasPermissionInRole(parent))
|
|
6397
|
+
return true;
|
|
2602
6398
|
}
|
|
2603
6399
|
}
|
|
2604
6400
|
return false;
|
|
2605
6401
|
};
|
|
2606
|
-
if (hasPermissionInRole(user.role))
|
|
6402
|
+
if (hasPermissionInRole(user.role))
|
|
6403
|
+
return true;
|
|
2607
6404
|
return false;
|
|
2608
6405
|
}
|
|
2609
6406
|
/**
|
|
@@ -3042,7 +6839,8 @@ var BlinkAuth = class {
|
|
|
3042
6839
|
}
|
|
3043
6840
|
extractTokensFromUrl() {
|
|
3044
6841
|
const search = getLocationSearch();
|
|
3045
|
-
if (!search)
|
|
6842
|
+
if (!search)
|
|
6843
|
+
return null;
|
|
3046
6844
|
const params = new URLSearchParams(search);
|
|
3047
6845
|
const accessToken = params.get("access_token");
|
|
3048
6846
|
const refreshToken = params.get("refresh_token");
|
|
@@ -3075,7 +6873,8 @@ var BlinkAuth = class {
|
|
|
3075
6873
|
}
|
|
3076
6874
|
clearUrlTokens() {
|
|
3077
6875
|
const href = getLocationHref();
|
|
3078
|
-
if (!href || !hasWindowLocation())
|
|
6876
|
+
if (!href || !hasWindowLocation())
|
|
6877
|
+
return;
|
|
3079
6878
|
const url = new URL(href);
|
|
3080
6879
|
url.searchParams.delete("access_token");
|
|
3081
6880
|
url.searchParams.delete("refresh_token");
|
|
@@ -3140,7 +6939,8 @@ var BlinkAuth = class {
|
|
|
3140
6939
|
*/
|
|
3141
6940
|
extractMagicTokenFromUrl() {
|
|
3142
6941
|
const search = getLocationSearch();
|
|
3143
|
-
if (!search)
|
|
6942
|
+
if (!search)
|
|
6943
|
+
return null;
|
|
3144
6944
|
const params = new URLSearchParams(search);
|
|
3145
6945
|
return params.get("magic_token") || params.get("token");
|
|
3146
6946
|
}
|
|
@@ -3197,7 +6997,8 @@ var BlinkAuth = class {
|
|
|
3197
6997
|
* Setup cross-tab authentication synchronization
|
|
3198
6998
|
*/
|
|
3199
6999
|
setupCrossTabSync() {
|
|
3200
|
-
if (!isWeb || !hasWindow())
|
|
7000
|
+
if (!isWeb || !hasWindow())
|
|
7001
|
+
return;
|
|
3201
7002
|
window.addEventListener("storage", (e) => {
|
|
3202
7003
|
if (e.key === this.getStorageKey("tokens")) {
|
|
3203
7004
|
const newTokens = e.newValue ? JSON.parse(e.newValue) : null;
|
|
@@ -5243,7 +9044,7 @@ var getWebSocketClass = () => {
|
|
|
5243
9044
|
return WebSocket;
|
|
5244
9045
|
}
|
|
5245
9046
|
try {
|
|
5246
|
-
const WS =
|
|
9047
|
+
const WS = require_ws();
|
|
5247
9048
|
return WS;
|
|
5248
9049
|
} catch (error) {
|
|
5249
9050
|
throw new BlinkRealtimeError('WebSocket is not available. Install "ws" package for Node.js environments.');
|
|
@@ -5568,7 +9369,8 @@ var RealtimeConnection = class {
|
|
|
5568
9369
|
});
|
|
5569
9370
|
}
|
|
5570
9371
|
flushMessageQueue() {
|
|
5571
|
-
if (!this.websocket || this.websocket.readyState !== 1)
|
|
9372
|
+
if (!this.websocket || this.websocket.readyState !== 1)
|
|
9373
|
+
return;
|
|
5572
9374
|
const queue = [...this.messageQueue];
|
|
5573
9375
|
this.messageQueue = [];
|
|
5574
9376
|
queue.forEach((q) => {
|
|
@@ -5611,7 +9413,8 @@ var RealtimeConnection = class {
|
|
|
5611
9413
|
const delay = baseDelay + jitter;
|
|
5612
9414
|
console.log(`\u{1F504} Scheduling reconnect attempt ${this.reconnectAttempts} in ${Math.round(delay)}ms`);
|
|
5613
9415
|
this.reconnectTimer = globalThis.setTimeout(async () => {
|
|
5614
|
-
if (this.channels.size === 0)
|
|
9416
|
+
if (this.channels.size === 0)
|
|
9417
|
+
return;
|
|
5615
9418
|
try {
|
|
5616
9419
|
await this.connectWebSocket();
|
|
5617
9420
|
await this.resubscribeAllChannels();
|
|
@@ -6130,7 +9933,8 @@ var BlinkAnalyticsImpl = class {
|
|
|
6130
9933
|
}
|
|
6131
9934
|
}
|
|
6132
9935
|
setupRouteChangeListener() {
|
|
6133
|
-
if (!isWeb)
|
|
9936
|
+
if (!isWeb)
|
|
9937
|
+
return;
|
|
6134
9938
|
if (!window.__blinkAnalyticsSetup) {
|
|
6135
9939
|
const originalPushState = history.pushState;
|
|
6136
9940
|
const originalReplaceState = history.replaceState;
|
|
@@ -6164,7 +9968,8 @@ var BlinkAnalyticsImpl = class {
|
|
|
6164
9968
|
window.__blinkAnalyticsInstances?.add(this);
|
|
6165
9969
|
}
|
|
6166
9970
|
setupUnloadListener() {
|
|
6167
|
-
if (!isWeb || !hasWindow())
|
|
9971
|
+
if (!isWeb || !hasWindow())
|
|
9972
|
+
return;
|
|
6168
9973
|
window.addEventListener("pagehide", () => {
|
|
6169
9974
|
this.flush();
|
|
6170
9975
|
});
|
|
@@ -6173,7 +9978,8 @@ var BlinkAnalyticsImpl = class {
|
|
|
6173
9978
|
});
|
|
6174
9979
|
}
|
|
6175
9980
|
captureUTMParams() {
|
|
6176
|
-
if (!isWeb)
|
|
9981
|
+
if (!isWeb)
|
|
9982
|
+
return;
|
|
6177
9983
|
const search = getLocationSearch();
|
|
6178
9984
|
if (!search) {
|
|
6179
9985
|
this.utmParams = {};
|
|
@@ -6220,14 +10026,21 @@ var BlinkAnalyticsImpl = class {
|
|
|
6220
10026
|
const utmMedium = this.utmParams.utm_medium;
|
|
6221
10027
|
this.utmParams.utm_source;
|
|
6222
10028
|
if (utmMedium) {
|
|
6223
|
-
if (utmMedium === "cpc" || utmMedium === "ppc")
|
|
6224
|
-
|
|
6225
|
-
if (utmMedium === "
|
|
6226
|
-
|
|
6227
|
-
if (utmMedium === "
|
|
6228
|
-
|
|
6229
|
-
|
|
6230
|
-
|
|
10029
|
+
if (utmMedium === "cpc" || utmMedium === "ppc")
|
|
10030
|
+
return "Paid Search";
|
|
10031
|
+
if (utmMedium === "email")
|
|
10032
|
+
return "Email";
|
|
10033
|
+
if (utmMedium === "social")
|
|
10034
|
+
return "Social";
|
|
10035
|
+
if (utmMedium === "referral")
|
|
10036
|
+
return "Referral";
|
|
10037
|
+
if (utmMedium === "display")
|
|
10038
|
+
return "Display";
|
|
10039
|
+
if (utmMedium === "affiliate")
|
|
10040
|
+
return "Affiliate";
|
|
10041
|
+
}
|
|
10042
|
+
if (!referrer)
|
|
10043
|
+
return "Direct";
|
|
6231
10044
|
try {
|
|
6232
10045
|
const referrerUrl = new URL(referrer);
|
|
6233
10046
|
const referrerDomain = referrerUrl.hostname.toLowerCase();
|
|
@@ -6358,8 +10171,10 @@ function convertDocument(api) {
|
|
|
6358
10171
|
}
|
|
6359
10172
|
function convertPartialDocument(api, options) {
|
|
6360
10173
|
let sourceType = "text";
|
|
6361
|
-
if (options.url)
|
|
6362
|
-
|
|
10174
|
+
if (options.url)
|
|
10175
|
+
sourceType = "url";
|
|
10176
|
+
if (options.file)
|
|
10177
|
+
sourceType = "file";
|
|
6363
10178
|
return {
|
|
6364
10179
|
id: api.id || "",
|
|
6365
10180
|
collectionId: api.collection_id || options.collectionId || "",
|
|
@@ -6534,8 +10349,10 @@ var BlinkRAGImpl = class {
|
|
|
6534
10349
|
*/
|
|
6535
10350
|
async listDocuments(options) {
|
|
6536
10351
|
const params = {};
|
|
6537
|
-
if (options?.collectionId)
|
|
6538
|
-
|
|
10352
|
+
if (options?.collectionId)
|
|
10353
|
+
params.collection_id = options.collectionId;
|
|
10354
|
+
if (options?.status)
|
|
10355
|
+
params.status = options.status;
|
|
6539
10356
|
const queryString = Object.keys(params).length > 0 ? `?${new URLSearchParams(params).toString()}` : "";
|
|
6540
10357
|
const response = await this.httpClient.get(
|
|
6541
10358
|
this.url(`/documents${queryString}`)
|
|
@@ -6719,6 +10536,96 @@ var BlinkSandboxImpl = class {
|
|
|
6719
10536
|
}
|
|
6720
10537
|
};
|
|
6721
10538
|
|
|
10539
|
+
// src/queue.ts
|
|
10540
|
+
var BlinkQueueCreditError = class extends Error {
|
|
10541
|
+
code = "INSUFFICIENT_CREDITS";
|
|
10542
|
+
constructor() {
|
|
10543
|
+
super("Insufficient credits to enqueue task. Add credits at https://blink.new/settings?tab=billing");
|
|
10544
|
+
this.name = "BlinkQueueCreditError";
|
|
10545
|
+
}
|
|
10546
|
+
};
|
|
10547
|
+
var BlinkQueueImpl = class {
|
|
10548
|
+
constructor(httpClient) {
|
|
10549
|
+
this.httpClient = httpClient;
|
|
10550
|
+
}
|
|
10551
|
+
get basePath() {
|
|
10552
|
+
return `/api/queue/${this.httpClient.projectId}`;
|
|
10553
|
+
}
|
|
10554
|
+
async enqueue(taskName, payload, options) {
|
|
10555
|
+
return this.httpClient.post(
|
|
10556
|
+
`${this.basePath}/enqueue`,
|
|
10557
|
+
{ taskName, payload, options }
|
|
10558
|
+
).then((res) => res.data).catch((err) => {
|
|
10559
|
+
if (err?.status === 402)
|
|
10560
|
+
throw new BlinkQueueCreditError();
|
|
10561
|
+
throw err;
|
|
10562
|
+
});
|
|
10563
|
+
}
|
|
10564
|
+
async list(filter) {
|
|
10565
|
+
const params = {};
|
|
10566
|
+
if (filter?.status)
|
|
10567
|
+
params.status = filter.status;
|
|
10568
|
+
if (filter?.queue)
|
|
10569
|
+
params.queue = filter.queue;
|
|
10570
|
+
if (filter?.limit)
|
|
10571
|
+
params.limit = String(filter.limit);
|
|
10572
|
+
const res = await this.httpClient.get(`${this.basePath}/tasks`, params);
|
|
10573
|
+
return res.data.tasks;
|
|
10574
|
+
}
|
|
10575
|
+
async get(taskId) {
|
|
10576
|
+
const res = await this.httpClient.get(`${this.basePath}/tasks/${taskId}`);
|
|
10577
|
+
return res.data;
|
|
10578
|
+
}
|
|
10579
|
+
async cancel(taskId) {
|
|
10580
|
+
await this.httpClient.delete(`${this.basePath}/tasks/${taskId}`);
|
|
10581
|
+
}
|
|
10582
|
+
async schedule(name, cron, payload, options) {
|
|
10583
|
+
const res = await this.httpClient.post(
|
|
10584
|
+
`${this.basePath}/schedule`,
|
|
10585
|
+
{ name, cron, payload, options }
|
|
10586
|
+
);
|
|
10587
|
+
return res.data;
|
|
10588
|
+
}
|
|
10589
|
+
async listSchedules() {
|
|
10590
|
+
const res = await this.httpClient.get(`${this.basePath}/schedules`);
|
|
10591
|
+
return res.data.schedules;
|
|
10592
|
+
}
|
|
10593
|
+
async pauseSchedule(name) {
|
|
10594
|
+
await this.httpClient.post(`${this.basePath}/schedules/${encodeURIComponent(name)}/pause`);
|
|
10595
|
+
}
|
|
10596
|
+
async resumeSchedule(name) {
|
|
10597
|
+
await this.httpClient.post(`${this.basePath}/schedules/${encodeURIComponent(name)}/resume`);
|
|
10598
|
+
}
|
|
10599
|
+
async deleteSchedule(name) {
|
|
10600
|
+
await this.httpClient.delete(`${this.basePath}/schedules/${encodeURIComponent(name)}`);
|
|
10601
|
+
}
|
|
10602
|
+
async createQueue(name, options) {
|
|
10603
|
+
await this.httpClient.post(`${this.basePath}/queues`, { name, ...options });
|
|
10604
|
+
}
|
|
10605
|
+
async listQueues() {
|
|
10606
|
+
const res = await this.httpClient.get(`${this.basePath}/queues`);
|
|
10607
|
+
return res.data.queues;
|
|
10608
|
+
}
|
|
10609
|
+
async deleteQueue(name) {
|
|
10610
|
+
await this.httpClient.delete(`${this.basePath}/queues/${encodeURIComponent(name)}`);
|
|
10611
|
+
}
|
|
10612
|
+
async listDead() {
|
|
10613
|
+
const res = await this.httpClient.get(`${this.basePath}/dlq`);
|
|
10614
|
+
return res.data.messages;
|
|
10615
|
+
}
|
|
10616
|
+
async retryDead(dlqId) {
|
|
10617
|
+
const res = await this.httpClient.post(`${this.basePath}/dlq/${dlqId}/retry`);
|
|
10618
|
+
return res.data;
|
|
10619
|
+
}
|
|
10620
|
+
async purgeDead() {
|
|
10621
|
+
await this.httpClient.delete(`${this.basePath}/dlq`);
|
|
10622
|
+
}
|
|
10623
|
+
async stats() {
|
|
10624
|
+
const res = await this.httpClient.get(`${this.basePath}/stats`);
|
|
10625
|
+
return res.data;
|
|
10626
|
+
}
|
|
10627
|
+
};
|
|
10628
|
+
|
|
6722
10629
|
// src/client.ts
|
|
6723
10630
|
var defaultClient = null;
|
|
6724
10631
|
function getDefaultClient() {
|
|
@@ -6745,6 +10652,7 @@ var BlinkClientImpl = class {
|
|
|
6745
10652
|
functions;
|
|
6746
10653
|
rag;
|
|
6747
10654
|
sandbox;
|
|
10655
|
+
queue;
|
|
6748
10656
|
/** @internal HTTP client for Agent auto-binding */
|
|
6749
10657
|
_httpClient;
|
|
6750
10658
|
constructor(config) {
|
|
@@ -6772,6 +10680,7 @@ var BlinkClientImpl = class {
|
|
|
6772
10680
|
);
|
|
6773
10681
|
this.rag = new BlinkRAGImpl(this._httpClient);
|
|
6774
10682
|
this.sandbox = new BlinkSandboxImpl(this._httpClient);
|
|
10683
|
+
this.queue = new BlinkQueueImpl(this._httpClient);
|
|
6775
10684
|
this.auth.onAuthStateChanged((state) => {
|
|
6776
10685
|
if (state.isAuthenticated && state.user) {
|
|
6777
10686
|
this.analytics.setUserId(state.user.id);
|
|
@@ -6792,6 +10701,6 @@ function createClient(config) {
|
|
|
6792
10701
|
return client;
|
|
6793
10702
|
}
|
|
6794
10703
|
|
|
6795
|
-
export { Agent, AsyncStorageAdapter, BlinkAIImpl, BlinkAnalyticsImpl, BlinkConnectorsImpl, BlinkDataImpl, BlinkDatabase, BlinkRAGImpl, BlinkRealtimeChannel, BlinkRealtimeImpl, BlinkSandboxImpl, BlinkStorageImpl, BlinkTable, NoOpStorageAdapter, SANDBOX_TEMPLATES, SandboxConnectionError, WebStorageAdapter, coreTools, createClient, dbDelete, dbGet, dbInsert, dbList, dbTools, dbUpdate, editImage, fetchUrl, generateImage, generateVideo, getDefaultClient, getDefaultStorageAdapter, getHost, globFileSearch, grep, imageToVideo, isBrowser, isDeno, isNode, isReactNative, isServer, isWeb, listDir, mediaTools, platform, ragSearch, ragTools, readFile, runCode, runTerminalCmd, sandboxTools, searchReplace, serializeTools, stepCountIs, storageCopy, storageDelete, storageDownload, storageList, storageMove, storagePublicUrl, storageTools, storageUpload, webSearch, writeFile };
|
|
6796
|
-
//# sourceMappingURL=
|
|
10704
|
+
export { Agent, AsyncStorageAdapter, BlinkAIImpl, BlinkAnalyticsImpl, BlinkConnectorsImpl, BlinkDataImpl, BlinkDatabase, BlinkQueueCreditError, BlinkQueueImpl, BlinkRAGImpl, BlinkRealtimeChannel, BlinkRealtimeImpl, BlinkSandboxImpl, BlinkStorageImpl, BlinkTable, NoOpStorageAdapter, SANDBOX_TEMPLATES, SandboxConnectionError, WebStorageAdapter, coreTools, createClient, dbDelete, dbGet, dbInsert, dbList, dbTools, dbUpdate, editImage, fetchUrl, generateImage, generateVideo, getDefaultClient, getDefaultStorageAdapter, getHost, globFileSearch, grep, imageToVideo, isBrowser, isDeno, isNode, isReactNative, isServer, isWeb, listDir, mediaTools, platform, ragSearch, ragTools, readFile, runCode, runTerminalCmd, sandboxTools, searchReplace, serializeTools, stepCountIs, storageCopy, storageDelete, storageDownload, storageList, storageMove, storagePublicUrl, storageTools, storageUpload, webSearch, writeFile };
|
|
10705
|
+
//# sourceMappingURL=out.js.map
|
|
6797
10706
|
//# sourceMappingURL=index.mjs.map
|