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