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