isomorfeus-transport 2.5.4 → 22.9.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/isomorfeus/core_ext/hash/deep_merge.rb +34 -0
- data/lib/isomorfeus/core_ext/kernel.rb +56 -0
- data/lib/isomorfeus/core_ext/object/deep_dup.rb +53 -0
- data/lib/isomorfeus/core_ext/object/duplicable.rb +60 -0
- data/lib/isomorfeus/transport/compressor_rack.rb +1 -1
- data/lib/isomorfeus/transport/config.rb +37 -0
- data/lib/isomorfeus/transport/rack_middleware.rb +16 -9
- data/lib/isomorfeus/transport/request_agent.rb +1 -0
- data/lib/isomorfeus/transport/server_socket_processor.rb +13 -1
- data/lib/isomorfeus/transport/version.rb +1 -1
- data/lib/isomorfeus/transport.rb +45 -38
- data/lib/isomorfeus-transport.rb +25 -16
- data/lib/lucid_channel.rb +103 -0
- data/lib/lucid_handler.rb +25 -0
- metadata +40 -90
- data/lib/isomorfeus/transport/handler/authentication_handler.rb +0 -55
- data/lib/isomorfeus/transport/imports.rb +0 -10
- data/lib/isomorfeus/transport/ssr_login.rb +0 -30
- data/lib/lucid_channel/base.rb +0 -8
- data/lib/lucid_channel/mixin.rb +0 -105
- data/lib/lucid_handler/base.rb +0 -8
- data/lib/lucid_handler/mixin.rb +0 -27
- data/node_modules/.package-lock.json +0 -27
- data/node_modules/ws/LICENSE +0 -19
- data/node_modules/ws/README.md +0 -489
- data/node_modules/ws/browser.js +0 -8
- data/node_modules/ws/index.js +0 -13
- data/node_modules/ws/lib/buffer-util.js +0 -126
- data/node_modules/ws/lib/constants.js +0 -12
- data/node_modules/ws/lib/event-target.js +0 -266
- data/node_modules/ws/lib/extension.js +0 -203
- data/node_modules/ws/lib/limiter.js +0 -55
- data/node_modules/ws/lib/permessage-deflate.js +0 -511
- data/node_modules/ws/lib/receiver.js +0 -618
- data/node_modules/ws/lib/sender.js +0 -478
- data/node_modules/ws/lib/stream.js +0 -159
- data/node_modules/ws/lib/subprotocol.js +0 -62
- data/node_modules/ws/lib/validation.js +0 -124
- data/node_modules/ws/lib/websocket-server.js +0 -488
- data/node_modules/ws/lib/websocket.js +0 -1264
- data/node_modules/ws/package.json +0 -61
- data/node_modules/ws/wrapper.mjs +0 -8
- data/package.json +0 -6
@@ -1,1264 +0,0 @@
|
|
1
|
-
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Readable$" }] */
|
2
|
-
|
3
|
-
'use strict';
|
4
|
-
|
5
|
-
const EventEmitter = require('events');
|
6
|
-
const https = require('https');
|
7
|
-
const http = require('http');
|
8
|
-
const net = require('net');
|
9
|
-
const tls = require('tls');
|
10
|
-
const { randomBytes, createHash } = require('crypto');
|
11
|
-
const { Readable } = require('stream');
|
12
|
-
const { URL } = require('url');
|
13
|
-
|
14
|
-
const PerMessageDeflate = require('./permessage-deflate');
|
15
|
-
const Receiver = require('./receiver');
|
16
|
-
const Sender = require('./sender');
|
17
|
-
const {
|
18
|
-
BINARY_TYPES,
|
19
|
-
EMPTY_BUFFER,
|
20
|
-
GUID,
|
21
|
-
kForOnEventAttribute,
|
22
|
-
kListener,
|
23
|
-
kStatusCode,
|
24
|
-
kWebSocket,
|
25
|
-
NOOP
|
26
|
-
} = require('./constants');
|
27
|
-
const {
|
28
|
-
EventTarget: { addEventListener, removeEventListener }
|
29
|
-
} = require('./event-target');
|
30
|
-
const { format, parse } = require('./extension');
|
31
|
-
const { toBuffer } = require('./buffer-util');
|
32
|
-
|
33
|
-
const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
|
34
|
-
const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
35
|
-
const protocolVersions = [8, 13];
|
36
|
-
const closeTimeout = 30 * 1000;
|
37
|
-
|
38
|
-
/**
|
39
|
-
* Class representing a WebSocket.
|
40
|
-
*
|
41
|
-
* @extends EventEmitter
|
42
|
-
*/
|
43
|
-
class WebSocket extends EventEmitter {
|
44
|
-
/**
|
45
|
-
* Create a new `WebSocket`.
|
46
|
-
*
|
47
|
-
* @param {(String|URL)} address The URL to which to connect
|
48
|
-
* @param {(String|String[])} [protocols] The subprotocols
|
49
|
-
* @param {Object} [options] Connection options
|
50
|
-
*/
|
51
|
-
constructor(address, protocols, options) {
|
52
|
-
super();
|
53
|
-
|
54
|
-
this._binaryType = BINARY_TYPES[0];
|
55
|
-
this._closeCode = 1006;
|
56
|
-
this._closeFrameReceived = false;
|
57
|
-
this._closeFrameSent = false;
|
58
|
-
this._closeMessage = EMPTY_BUFFER;
|
59
|
-
this._closeTimer = null;
|
60
|
-
this._extensions = {};
|
61
|
-
this._paused = false;
|
62
|
-
this._protocol = '';
|
63
|
-
this._readyState = WebSocket.CONNECTING;
|
64
|
-
this._receiver = null;
|
65
|
-
this._sender = null;
|
66
|
-
this._socket = null;
|
67
|
-
|
68
|
-
if (address !== null) {
|
69
|
-
this._bufferedAmount = 0;
|
70
|
-
this._isServer = false;
|
71
|
-
this._redirects = 0;
|
72
|
-
|
73
|
-
if (protocols === undefined) {
|
74
|
-
protocols = [];
|
75
|
-
} else if (!Array.isArray(protocols)) {
|
76
|
-
if (typeof protocols === 'object' && protocols !== null) {
|
77
|
-
options = protocols;
|
78
|
-
protocols = [];
|
79
|
-
} else {
|
80
|
-
protocols = [protocols];
|
81
|
-
}
|
82
|
-
}
|
83
|
-
|
84
|
-
initAsClient(this, address, protocols, options);
|
85
|
-
} else {
|
86
|
-
this._isServer = true;
|
87
|
-
}
|
88
|
-
}
|
89
|
-
|
90
|
-
/**
|
91
|
-
* This deviates from the WHATWG interface since ws doesn't support the
|
92
|
-
* required default "blob" type (instead we define a custom "nodebuffer"
|
93
|
-
* type).
|
94
|
-
*
|
95
|
-
* @type {String}
|
96
|
-
*/
|
97
|
-
get binaryType() {
|
98
|
-
return this._binaryType;
|
99
|
-
}
|
100
|
-
|
101
|
-
set binaryType(type) {
|
102
|
-
if (!BINARY_TYPES.includes(type)) return;
|
103
|
-
|
104
|
-
this._binaryType = type;
|
105
|
-
|
106
|
-
//
|
107
|
-
// Allow to change `binaryType` on the fly.
|
108
|
-
//
|
109
|
-
if (this._receiver) this._receiver._binaryType = type;
|
110
|
-
}
|
111
|
-
|
112
|
-
/**
|
113
|
-
* @type {Number}
|
114
|
-
*/
|
115
|
-
get bufferedAmount() {
|
116
|
-
if (!this._socket) return this._bufferedAmount;
|
117
|
-
|
118
|
-
return this._socket._writableState.length + this._sender._bufferedBytes;
|
119
|
-
}
|
120
|
-
|
121
|
-
/**
|
122
|
-
* @type {String}
|
123
|
-
*/
|
124
|
-
get extensions() {
|
125
|
-
return Object.keys(this._extensions).join();
|
126
|
-
}
|
127
|
-
|
128
|
-
/**
|
129
|
-
* @type {Boolean}
|
130
|
-
*/
|
131
|
-
get isPaused() {
|
132
|
-
return this._paused;
|
133
|
-
}
|
134
|
-
|
135
|
-
/**
|
136
|
-
* @type {Function}
|
137
|
-
*/
|
138
|
-
/* istanbul ignore next */
|
139
|
-
get onclose() {
|
140
|
-
return null;
|
141
|
-
}
|
142
|
-
|
143
|
-
/**
|
144
|
-
* @type {Function}
|
145
|
-
*/
|
146
|
-
/* istanbul ignore next */
|
147
|
-
get onerror() {
|
148
|
-
return null;
|
149
|
-
}
|
150
|
-
|
151
|
-
/**
|
152
|
-
* @type {Function}
|
153
|
-
*/
|
154
|
-
/* istanbul ignore next */
|
155
|
-
get onopen() {
|
156
|
-
return null;
|
157
|
-
}
|
158
|
-
|
159
|
-
/**
|
160
|
-
* @type {Function}
|
161
|
-
*/
|
162
|
-
/* istanbul ignore next */
|
163
|
-
get onmessage() {
|
164
|
-
return null;
|
165
|
-
}
|
166
|
-
|
167
|
-
/**
|
168
|
-
* @type {String}
|
169
|
-
*/
|
170
|
-
get protocol() {
|
171
|
-
return this._protocol;
|
172
|
-
}
|
173
|
-
|
174
|
-
/**
|
175
|
-
* @type {Number}
|
176
|
-
*/
|
177
|
-
get readyState() {
|
178
|
-
return this._readyState;
|
179
|
-
}
|
180
|
-
|
181
|
-
/**
|
182
|
-
* @type {String}
|
183
|
-
*/
|
184
|
-
get url() {
|
185
|
-
return this._url;
|
186
|
-
}
|
187
|
-
|
188
|
-
/**
|
189
|
-
* Set up the socket and the internal resources.
|
190
|
-
*
|
191
|
-
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
192
|
-
* server and client
|
193
|
-
* @param {Buffer} head The first packet of the upgraded stream
|
194
|
-
* @param {Object} options Options object
|
195
|
-
* @param {Function} [options.generateMask] The function used to generate the
|
196
|
-
* masking key
|
197
|
-
* @param {Number} [options.maxPayload=0] The maximum allowed message size
|
198
|
-
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
199
|
-
* not to skip UTF-8 validation for text and close messages
|
200
|
-
* @private
|
201
|
-
*/
|
202
|
-
setSocket(socket, head, options) {
|
203
|
-
const receiver = new Receiver({
|
204
|
-
binaryType: this.binaryType,
|
205
|
-
extensions: this._extensions,
|
206
|
-
isServer: this._isServer,
|
207
|
-
maxPayload: options.maxPayload,
|
208
|
-
skipUTF8Validation: options.skipUTF8Validation
|
209
|
-
});
|
210
|
-
|
211
|
-
this._sender = new Sender(socket, this._extensions, options.generateMask);
|
212
|
-
this._receiver = receiver;
|
213
|
-
this._socket = socket;
|
214
|
-
|
215
|
-
receiver[kWebSocket] = this;
|
216
|
-
socket[kWebSocket] = this;
|
217
|
-
|
218
|
-
receiver.on('conclude', receiverOnConclude);
|
219
|
-
receiver.on('drain', receiverOnDrain);
|
220
|
-
receiver.on('error', receiverOnError);
|
221
|
-
receiver.on('message', receiverOnMessage);
|
222
|
-
receiver.on('ping', receiverOnPing);
|
223
|
-
receiver.on('pong', receiverOnPong);
|
224
|
-
|
225
|
-
socket.setTimeout(0);
|
226
|
-
socket.setNoDelay();
|
227
|
-
|
228
|
-
if (head.length > 0) socket.unshift(head);
|
229
|
-
|
230
|
-
socket.on('close', socketOnClose);
|
231
|
-
socket.on('data', socketOnData);
|
232
|
-
socket.on('end', socketOnEnd);
|
233
|
-
socket.on('error', socketOnError);
|
234
|
-
|
235
|
-
this._readyState = WebSocket.OPEN;
|
236
|
-
this.emit('open');
|
237
|
-
}
|
238
|
-
|
239
|
-
/**
|
240
|
-
* Emit the `'close'` event.
|
241
|
-
*
|
242
|
-
* @private
|
243
|
-
*/
|
244
|
-
emitClose() {
|
245
|
-
if (!this._socket) {
|
246
|
-
this._readyState = WebSocket.CLOSED;
|
247
|
-
this.emit('close', this._closeCode, this._closeMessage);
|
248
|
-
return;
|
249
|
-
}
|
250
|
-
|
251
|
-
if (this._extensions[PerMessageDeflate.extensionName]) {
|
252
|
-
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
253
|
-
}
|
254
|
-
|
255
|
-
this._receiver.removeAllListeners();
|
256
|
-
this._readyState = WebSocket.CLOSED;
|
257
|
-
this.emit('close', this._closeCode, this._closeMessage);
|
258
|
-
}
|
259
|
-
|
260
|
-
/**
|
261
|
-
* Start a closing handshake.
|
262
|
-
*
|
263
|
-
* +----------+ +-----------+ +----------+
|
264
|
-
* - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
|
265
|
-
* | +----------+ +-----------+ +----------+ |
|
266
|
-
* +----------+ +-----------+ |
|
267
|
-
* CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
|
268
|
-
* +----------+ +-----------+ |
|
269
|
-
* | | | +---+ |
|
270
|
-
* +------------------------+-->|fin| - - - -
|
271
|
-
* | +---+ | +---+
|
272
|
-
* - - - - -|fin|<---------------------+
|
273
|
-
* +---+
|
274
|
-
*
|
275
|
-
* @param {Number} [code] Status code explaining why the connection is closing
|
276
|
-
* @param {(String|Buffer)} [data] The reason why the connection is
|
277
|
-
* closing
|
278
|
-
* @public
|
279
|
-
*/
|
280
|
-
close(code, data) {
|
281
|
-
if (this.readyState === WebSocket.CLOSED) return;
|
282
|
-
if (this.readyState === WebSocket.CONNECTING) {
|
283
|
-
const msg = 'WebSocket was closed before the connection was established';
|
284
|
-
return abortHandshake(this, this._req, msg);
|
285
|
-
}
|
286
|
-
|
287
|
-
if (this.readyState === WebSocket.CLOSING) {
|
288
|
-
if (
|
289
|
-
this._closeFrameSent &&
|
290
|
-
(this._closeFrameReceived || this._receiver._writableState.errorEmitted)
|
291
|
-
) {
|
292
|
-
this._socket.end();
|
293
|
-
}
|
294
|
-
|
295
|
-
return;
|
296
|
-
}
|
297
|
-
|
298
|
-
this._readyState = WebSocket.CLOSING;
|
299
|
-
this._sender.close(code, data, !this._isServer, (err) => {
|
300
|
-
//
|
301
|
-
// This error is handled by the `'error'` listener on the socket. We only
|
302
|
-
// want to know if the close frame has been sent here.
|
303
|
-
//
|
304
|
-
if (err) return;
|
305
|
-
|
306
|
-
this._closeFrameSent = true;
|
307
|
-
|
308
|
-
if (
|
309
|
-
this._closeFrameReceived ||
|
310
|
-
this._receiver._writableState.errorEmitted
|
311
|
-
) {
|
312
|
-
this._socket.end();
|
313
|
-
}
|
314
|
-
});
|
315
|
-
|
316
|
-
//
|
317
|
-
// Specify a timeout for the closing handshake to complete.
|
318
|
-
//
|
319
|
-
this._closeTimer = setTimeout(
|
320
|
-
this._socket.destroy.bind(this._socket),
|
321
|
-
closeTimeout
|
322
|
-
);
|
323
|
-
}
|
324
|
-
|
325
|
-
/**
|
326
|
-
* Pause the socket.
|
327
|
-
*
|
328
|
-
* @public
|
329
|
-
*/
|
330
|
-
pause() {
|
331
|
-
if (
|
332
|
-
this.readyState === WebSocket.CONNECTING ||
|
333
|
-
this.readyState === WebSocket.CLOSED
|
334
|
-
) {
|
335
|
-
return;
|
336
|
-
}
|
337
|
-
|
338
|
-
this._paused = true;
|
339
|
-
this._socket.pause();
|
340
|
-
}
|
341
|
-
|
342
|
-
/**
|
343
|
-
* Send a ping.
|
344
|
-
*
|
345
|
-
* @param {*} [data] The data to send
|
346
|
-
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
347
|
-
* @param {Function} [cb] Callback which is executed when the ping is sent
|
348
|
-
* @public
|
349
|
-
*/
|
350
|
-
ping(data, mask, cb) {
|
351
|
-
if (this.readyState === WebSocket.CONNECTING) {
|
352
|
-
throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
|
353
|
-
}
|
354
|
-
|
355
|
-
if (typeof data === 'function') {
|
356
|
-
cb = data;
|
357
|
-
data = mask = undefined;
|
358
|
-
} else if (typeof mask === 'function') {
|
359
|
-
cb = mask;
|
360
|
-
mask = undefined;
|
361
|
-
}
|
362
|
-
|
363
|
-
if (typeof data === 'number') data = data.toString();
|
364
|
-
|
365
|
-
if (this.readyState !== WebSocket.OPEN) {
|
366
|
-
sendAfterClose(this, data, cb);
|
367
|
-
return;
|
368
|
-
}
|
369
|
-
|
370
|
-
if (mask === undefined) mask = !this._isServer;
|
371
|
-
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
372
|
-
}
|
373
|
-
|
374
|
-
/**
|
375
|
-
* Send a pong.
|
376
|
-
*
|
377
|
-
* @param {*} [data] The data to send
|
378
|
-
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
379
|
-
* @param {Function} [cb] Callback which is executed when the pong is sent
|
380
|
-
* @public
|
381
|
-
*/
|
382
|
-
pong(data, mask, cb) {
|
383
|
-
if (this.readyState === WebSocket.CONNECTING) {
|
384
|
-
throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
|
385
|
-
}
|
386
|
-
|
387
|
-
if (typeof data === 'function') {
|
388
|
-
cb = data;
|
389
|
-
data = mask = undefined;
|
390
|
-
} else if (typeof mask === 'function') {
|
391
|
-
cb = mask;
|
392
|
-
mask = undefined;
|
393
|
-
}
|
394
|
-
|
395
|
-
if (typeof data === 'number') data = data.toString();
|
396
|
-
|
397
|
-
if (this.readyState !== WebSocket.OPEN) {
|
398
|
-
sendAfterClose(this, data, cb);
|
399
|
-
return;
|
400
|
-
}
|
401
|
-
|
402
|
-
if (mask === undefined) mask = !this._isServer;
|
403
|
-
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
404
|
-
}
|
405
|
-
|
406
|
-
/**
|
407
|
-
* Resume the socket.
|
408
|
-
*
|
409
|
-
* @public
|
410
|
-
*/
|
411
|
-
resume() {
|
412
|
-
if (
|
413
|
-
this.readyState === WebSocket.CONNECTING ||
|
414
|
-
this.readyState === WebSocket.CLOSED
|
415
|
-
) {
|
416
|
-
return;
|
417
|
-
}
|
418
|
-
|
419
|
-
this._paused = false;
|
420
|
-
if (!this._receiver._writableState.needDrain) this._socket.resume();
|
421
|
-
}
|
422
|
-
|
423
|
-
/**
|
424
|
-
* Send a data message.
|
425
|
-
*
|
426
|
-
* @param {*} data The message to send
|
427
|
-
* @param {Object} [options] Options object
|
428
|
-
* @param {Boolean} [options.binary] Specifies whether `data` is binary or
|
429
|
-
* text
|
430
|
-
* @param {Boolean} [options.compress] Specifies whether or not to compress
|
431
|
-
* `data`
|
432
|
-
* @param {Boolean} [options.fin=true] Specifies whether the fragment is the
|
433
|
-
* last one
|
434
|
-
* @param {Boolean} [options.mask] Specifies whether or not to mask `data`
|
435
|
-
* @param {Function} [cb] Callback which is executed when data is written out
|
436
|
-
* @public
|
437
|
-
*/
|
438
|
-
send(data, options, cb) {
|
439
|
-
if (this.readyState === WebSocket.CONNECTING) {
|
440
|
-
throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
|
441
|
-
}
|
442
|
-
|
443
|
-
if (typeof options === 'function') {
|
444
|
-
cb = options;
|
445
|
-
options = {};
|
446
|
-
}
|
447
|
-
|
448
|
-
if (typeof data === 'number') data = data.toString();
|
449
|
-
|
450
|
-
if (this.readyState !== WebSocket.OPEN) {
|
451
|
-
sendAfterClose(this, data, cb);
|
452
|
-
return;
|
453
|
-
}
|
454
|
-
|
455
|
-
const opts = {
|
456
|
-
binary: typeof data !== 'string',
|
457
|
-
mask: !this._isServer,
|
458
|
-
compress: true,
|
459
|
-
fin: true,
|
460
|
-
...options
|
461
|
-
};
|
462
|
-
|
463
|
-
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
464
|
-
opts.compress = false;
|
465
|
-
}
|
466
|
-
|
467
|
-
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
468
|
-
}
|
469
|
-
|
470
|
-
/**
|
471
|
-
* Forcibly close the connection.
|
472
|
-
*
|
473
|
-
* @public
|
474
|
-
*/
|
475
|
-
terminate() {
|
476
|
-
if (this.readyState === WebSocket.CLOSED) return;
|
477
|
-
if (this.readyState === WebSocket.CONNECTING) {
|
478
|
-
const msg = 'WebSocket was closed before the connection was established';
|
479
|
-
return abortHandshake(this, this._req, msg);
|
480
|
-
}
|
481
|
-
|
482
|
-
if (this._socket) {
|
483
|
-
this._readyState = WebSocket.CLOSING;
|
484
|
-
this._socket.destroy();
|
485
|
-
}
|
486
|
-
}
|
487
|
-
}
|
488
|
-
|
489
|
-
/**
|
490
|
-
* @constant {Number} CONNECTING
|
491
|
-
* @memberof WebSocket
|
492
|
-
*/
|
493
|
-
Object.defineProperty(WebSocket, 'CONNECTING', {
|
494
|
-
enumerable: true,
|
495
|
-
value: readyStates.indexOf('CONNECTING')
|
496
|
-
});
|
497
|
-
|
498
|
-
/**
|
499
|
-
* @constant {Number} CONNECTING
|
500
|
-
* @memberof WebSocket.prototype
|
501
|
-
*/
|
502
|
-
Object.defineProperty(WebSocket.prototype, 'CONNECTING', {
|
503
|
-
enumerable: true,
|
504
|
-
value: readyStates.indexOf('CONNECTING')
|
505
|
-
});
|
506
|
-
|
507
|
-
/**
|
508
|
-
* @constant {Number} OPEN
|
509
|
-
* @memberof WebSocket
|
510
|
-
*/
|
511
|
-
Object.defineProperty(WebSocket, 'OPEN', {
|
512
|
-
enumerable: true,
|
513
|
-
value: readyStates.indexOf('OPEN')
|
514
|
-
});
|
515
|
-
|
516
|
-
/**
|
517
|
-
* @constant {Number} OPEN
|
518
|
-
* @memberof WebSocket.prototype
|
519
|
-
*/
|
520
|
-
Object.defineProperty(WebSocket.prototype, 'OPEN', {
|
521
|
-
enumerable: true,
|
522
|
-
value: readyStates.indexOf('OPEN')
|
523
|
-
});
|
524
|
-
|
525
|
-
/**
|
526
|
-
* @constant {Number} CLOSING
|
527
|
-
* @memberof WebSocket
|
528
|
-
*/
|
529
|
-
Object.defineProperty(WebSocket, 'CLOSING', {
|
530
|
-
enumerable: true,
|
531
|
-
value: readyStates.indexOf('CLOSING')
|
532
|
-
});
|
533
|
-
|
534
|
-
/**
|
535
|
-
* @constant {Number} CLOSING
|
536
|
-
* @memberof WebSocket.prototype
|
537
|
-
*/
|
538
|
-
Object.defineProperty(WebSocket.prototype, 'CLOSING', {
|
539
|
-
enumerable: true,
|
540
|
-
value: readyStates.indexOf('CLOSING')
|
541
|
-
});
|
542
|
-
|
543
|
-
/**
|
544
|
-
* @constant {Number} CLOSED
|
545
|
-
* @memberof WebSocket
|
546
|
-
*/
|
547
|
-
Object.defineProperty(WebSocket, 'CLOSED', {
|
548
|
-
enumerable: true,
|
549
|
-
value: readyStates.indexOf('CLOSED')
|
550
|
-
});
|
551
|
-
|
552
|
-
/**
|
553
|
-
* @constant {Number} CLOSED
|
554
|
-
* @memberof WebSocket.prototype
|
555
|
-
*/
|
556
|
-
Object.defineProperty(WebSocket.prototype, 'CLOSED', {
|
557
|
-
enumerable: true,
|
558
|
-
value: readyStates.indexOf('CLOSED')
|
559
|
-
});
|
560
|
-
|
561
|
-
[
|
562
|
-
'binaryType',
|
563
|
-
'bufferedAmount',
|
564
|
-
'extensions',
|
565
|
-
'isPaused',
|
566
|
-
'protocol',
|
567
|
-
'readyState',
|
568
|
-
'url'
|
569
|
-
].forEach((property) => {
|
570
|
-
Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
|
571
|
-
});
|
572
|
-
|
573
|
-
//
|
574
|
-
// Add the `onopen`, `onerror`, `onclose`, and `onmessage` attributes.
|
575
|
-
// See https://html.spec.whatwg.org/multipage/comms.html#the-websocket-interface
|
576
|
-
//
|
577
|
-
['open', 'error', 'close', 'message'].forEach((method) => {
|
578
|
-
Object.defineProperty(WebSocket.prototype, `on${method}`, {
|
579
|
-
enumerable: true,
|
580
|
-
get() {
|
581
|
-
for (const listener of this.listeners(method)) {
|
582
|
-
if (listener[kForOnEventAttribute]) return listener[kListener];
|
583
|
-
}
|
584
|
-
|
585
|
-
return null;
|
586
|
-
},
|
587
|
-
set(handler) {
|
588
|
-
for (const listener of this.listeners(method)) {
|
589
|
-
if (listener[kForOnEventAttribute]) {
|
590
|
-
this.removeListener(method, listener);
|
591
|
-
break;
|
592
|
-
}
|
593
|
-
}
|
594
|
-
|
595
|
-
if (typeof handler !== 'function') return;
|
596
|
-
|
597
|
-
this.addEventListener(method, handler, {
|
598
|
-
[kForOnEventAttribute]: true
|
599
|
-
});
|
600
|
-
}
|
601
|
-
});
|
602
|
-
});
|
603
|
-
|
604
|
-
WebSocket.prototype.addEventListener = addEventListener;
|
605
|
-
WebSocket.prototype.removeEventListener = removeEventListener;
|
606
|
-
|
607
|
-
module.exports = WebSocket;
|
608
|
-
|
609
|
-
/**
|
610
|
-
* Initialize a WebSocket client.
|
611
|
-
*
|
612
|
-
* @param {WebSocket} websocket The client to initialize
|
613
|
-
* @param {(String|URL)} address The URL to which to connect
|
614
|
-
* @param {Array} protocols The subprotocols
|
615
|
-
* @param {Object} [options] Connection options
|
616
|
-
* @param {Boolean} [options.followRedirects=false] Whether or not to follow
|
617
|
-
* redirects
|
618
|
-
* @param {Function} [options.generateMask] The function used to generate the
|
619
|
-
* masking key
|
620
|
-
* @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
|
621
|
-
* handshake request
|
622
|
-
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
623
|
-
* size
|
624
|
-
* @param {Number} [options.maxRedirects=10] The maximum number of redirects
|
625
|
-
* allowed
|
626
|
-
* @param {String} [options.origin] Value of the `Origin` or
|
627
|
-
* `Sec-WebSocket-Origin` header
|
628
|
-
* @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
|
629
|
-
* permessage-deflate
|
630
|
-
* @param {Number} [options.protocolVersion=13] Value of the
|
631
|
-
* `Sec-WebSocket-Version` header
|
632
|
-
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
633
|
-
* not to skip UTF-8 validation for text and close messages
|
634
|
-
* @private
|
635
|
-
*/
|
636
|
-
function initAsClient(websocket, address, protocols, options) {
|
637
|
-
const opts = {
|
638
|
-
protocolVersion: protocolVersions[1],
|
639
|
-
maxPayload: 100 * 1024 * 1024,
|
640
|
-
skipUTF8Validation: false,
|
641
|
-
perMessageDeflate: true,
|
642
|
-
followRedirects: false,
|
643
|
-
maxRedirects: 10,
|
644
|
-
...options,
|
645
|
-
createConnection: undefined,
|
646
|
-
socketPath: undefined,
|
647
|
-
hostname: undefined,
|
648
|
-
protocol: undefined,
|
649
|
-
timeout: undefined,
|
650
|
-
method: undefined,
|
651
|
-
host: undefined,
|
652
|
-
path: undefined,
|
653
|
-
port: undefined
|
654
|
-
};
|
655
|
-
|
656
|
-
if (!protocolVersions.includes(opts.protocolVersion)) {
|
657
|
-
throw new RangeError(
|
658
|
-
`Unsupported protocol version: ${opts.protocolVersion} ` +
|
659
|
-
`(supported versions: ${protocolVersions.join(', ')})`
|
660
|
-
);
|
661
|
-
}
|
662
|
-
|
663
|
-
let parsedUrl;
|
664
|
-
|
665
|
-
if (address instanceof URL) {
|
666
|
-
parsedUrl = address;
|
667
|
-
websocket._url = address.href;
|
668
|
-
} else {
|
669
|
-
try {
|
670
|
-
parsedUrl = new URL(address);
|
671
|
-
} catch (e) {
|
672
|
-
throw new SyntaxError(`Invalid URL: ${address}`);
|
673
|
-
}
|
674
|
-
|
675
|
-
websocket._url = address;
|
676
|
-
}
|
677
|
-
|
678
|
-
const isSecure = parsedUrl.protocol === 'wss:';
|
679
|
-
const isUnixSocket = parsedUrl.protocol === 'ws+unix:';
|
680
|
-
let invalidURLMessage;
|
681
|
-
|
682
|
-
if (parsedUrl.protocol !== 'ws:' && !isSecure && !isUnixSocket) {
|
683
|
-
invalidURLMessage =
|
684
|
-
'The URL\'s protocol must be one of "ws:", "wss:", or "ws+unix:"';
|
685
|
-
} else if (isUnixSocket && !parsedUrl.pathname) {
|
686
|
-
invalidURLMessage = "The URL's pathname is empty";
|
687
|
-
} else if (parsedUrl.hash) {
|
688
|
-
invalidURLMessage = 'The URL contains a fragment identifier';
|
689
|
-
}
|
690
|
-
|
691
|
-
if (invalidURLMessage) {
|
692
|
-
const err = new SyntaxError(invalidURLMessage);
|
693
|
-
|
694
|
-
if (websocket._redirects === 0) {
|
695
|
-
throw err;
|
696
|
-
} else {
|
697
|
-
emitErrorAndClose(websocket, err);
|
698
|
-
return;
|
699
|
-
}
|
700
|
-
}
|
701
|
-
|
702
|
-
const defaultPort = isSecure ? 443 : 80;
|
703
|
-
const key = randomBytes(16).toString('base64');
|
704
|
-
const get = isSecure ? https.get : http.get;
|
705
|
-
const protocolSet = new Set();
|
706
|
-
let perMessageDeflate;
|
707
|
-
|
708
|
-
opts.createConnection = isSecure ? tlsConnect : netConnect;
|
709
|
-
opts.defaultPort = opts.defaultPort || defaultPort;
|
710
|
-
opts.port = parsedUrl.port || defaultPort;
|
711
|
-
opts.host = parsedUrl.hostname.startsWith('[')
|
712
|
-
? parsedUrl.hostname.slice(1, -1)
|
713
|
-
: parsedUrl.hostname;
|
714
|
-
opts.headers = {
|
715
|
-
'Sec-WebSocket-Version': opts.protocolVersion,
|
716
|
-
'Sec-WebSocket-Key': key,
|
717
|
-
Connection: 'Upgrade',
|
718
|
-
Upgrade: 'websocket',
|
719
|
-
...opts.headers
|
720
|
-
};
|
721
|
-
opts.path = parsedUrl.pathname + parsedUrl.search;
|
722
|
-
opts.timeout = opts.handshakeTimeout;
|
723
|
-
|
724
|
-
if (opts.perMessageDeflate) {
|
725
|
-
perMessageDeflate = new PerMessageDeflate(
|
726
|
-
opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
|
727
|
-
false,
|
728
|
-
opts.maxPayload
|
729
|
-
);
|
730
|
-
opts.headers['Sec-WebSocket-Extensions'] = format({
|
731
|
-
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
732
|
-
});
|
733
|
-
}
|
734
|
-
if (protocols.length) {
|
735
|
-
for (const protocol of protocols) {
|
736
|
-
if (
|
737
|
-
typeof protocol !== 'string' ||
|
738
|
-
!subprotocolRegex.test(protocol) ||
|
739
|
-
protocolSet.has(protocol)
|
740
|
-
) {
|
741
|
-
throw new SyntaxError(
|
742
|
-
'An invalid or duplicated subprotocol was specified'
|
743
|
-
);
|
744
|
-
}
|
745
|
-
|
746
|
-
protocolSet.add(protocol);
|
747
|
-
}
|
748
|
-
|
749
|
-
opts.headers['Sec-WebSocket-Protocol'] = protocols.join(',');
|
750
|
-
}
|
751
|
-
if (opts.origin) {
|
752
|
-
if (opts.protocolVersion < 13) {
|
753
|
-
opts.headers['Sec-WebSocket-Origin'] = opts.origin;
|
754
|
-
} else {
|
755
|
-
opts.headers.Origin = opts.origin;
|
756
|
-
}
|
757
|
-
}
|
758
|
-
if (parsedUrl.username || parsedUrl.password) {
|
759
|
-
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
760
|
-
}
|
761
|
-
|
762
|
-
if (isUnixSocket) {
|
763
|
-
const parts = opts.path.split(':');
|
764
|
-
|
765
|
-
opts.socketPath = parts[0];
|
766
|
-
opts.path = parts[1];
|
767
|
-
}
|
768
|
-
|
769
|
-
if (opts.followRedirects) {
|
770
|
-
if (websocket._redirects === 0) {
|
771
|
-
websocket._originalHost = parsedUrl.host;
|
772
|
-
|
773
|
-
const headers = options && options.headers;
|
774
|
-
|
775
|
-
//
|
776
|
-
// Shallow copy the user provided options so that headers can be changed
|
777
|
-
// without mutating the original object.
|
778
|
-
//
|
779
|
-
options = { ...options, headers: {} };
|
780
|
-
|
781
|
-
if (headers) {
|
782
|
-
for (const [key, value] of Object.entries(headers)) {
|
783
|
-
options.headers[key.toLowerCase()] = value;
|
784
|
-
}
|
785
|
-
}
|
786
|
-
} else if (parsedUrl.host !== websocket._originalHost) {
|
787
|
-
//
|
788
|
-
// Match curl 7.77.0 behavior and drop the following headers. These
|
789
|
-
// headers are also dropped when following a redirect to a subdomain.
|
790
|
-
//
|
791
|
-
delete opts.headers.authorization;
|
792
|
-
delete opts.headers.cookie;
|
793
|
-
delete opts.headers.host;
|
794
|
-
opts.auth = undefined;
|
795
|
-
}
|
796
|
-
|
797
|
-
//
|
798
|
-
// Match curl 7.77.0 behavior and make the first `Authorization` header win.
|
799
|
-
// If the `Authorization` header is set, then there is nothing to do as it
|
800
|
-
// will take precedence.
|
801
|
-
//
|
802
|
-
if (opts.auth && !options.headers.authorization) {
|
803
|
-
options.headers.authorization =
|
804
|
-
'Basic ' + Buffer.from(opts.auth).toString('base64');
|
805
|
-
}
|
806
|
-
}
|
807
|
-
|
808
|
-
let req = (websocket._req = get(opts));
|
809
|
-
|
810
|
-
if (opts.timeout) {
|
811
|
-
req.on('timeout', () => {
|
812
|
-
abortHandshake(websocket, req, 'Opening handshake has timed out');
|
813
|
-
});
|
814
|
-
}
|
815
|
-
|
816
|
-
req.on('error', (err) => {
|
817
|
-
if (req === null || req.aborted) return;
|
818
|
-
|
819
|
-
req = websocket._req = null;
|
820
|
-
emitErrorAndClose(websocket, err);
|
821
|
-
});
|
822
|
-
|
823
|
-
req.on('response', (res) => {
|
824
|
-
const location = res.headers.location;
|
825
|
-
const statusCode = res.statusCode;
|
826
|
-
|
827
|
-
if (
|
828
|
-
location &&
|
829
|
-
opts.followRedirects &&
|
830
|
-
statusCode >= 300 &&
|
831
|
-
statusCode < 400
|
832
|
-
) {
|
833
|
-
if (++websocket._redirects > opts.maxRedirects) {
|
834
|
-
abortHandshake(websocket, req, 'Maximum redirects exceeded');
|
835
|
-
return;
|
836
|
-
}
|
837
|
-
|
838
|
-
req.abort();
|
839
|
-
|
840
|
-
let addr;
|
841
|
-
|
842
|
-
try {
|
843
|
-
addr = new URL(location, address);
|
844
|
-
} catch (e) {
|
845
|
-
const err = new SyntaxError(`Invalid URL: ${location}`);
|
846
|
-
emitErrorAndClose(websocket, err);
|
847
|
-
return;
|
848
|
-
}
|
849
|
-
|
850
|
-
initAsClient(websocket, addr, protocols, options);
|
851
|
-
} else if (!websocket.emit('unexpected-response', req, res)) {
|
852
|
-
abortHandshake(
|
853
|
-
websocket,
|
854
|
-
req,
|
855
|
-
`Unexpected server response: ${res.statusCode}`
|
856
|
-
);
|
857
|
-
}
|
858
|
-
});
|
859
|
-
|
860
|
-
req.on('upgrade', (res, socket, head) => {
|
861
|
-
websocket.emit('upgrade', res);
|
862
|
-
|
863
|
-
//
|
864
|
-
// The user may have closed the connection from a listener of the `upgrade`
|
865
|
-
// event.
|
866
|
-
//
|
867
|
-
if (websocket.readyState !== WebSocket.CONNECTING) return;
|
868
|
-
|
869
|
-
req = websocket._req = null;
|
870
|
-
|
871
|
-
const digest = createHash('sha1')
|
872
|
-
.update(key + GUID)
|
873
|
-
.digest('base64');
|
874
|
-
|
875
|
-
if (res.headers['sec-websocket-accept'] !== digest) {
|
876
|
-
abortHandshake(websocket, socket, 'Invalid Sec-WebSocket-Accept header');
|
877
|
-
return;
|
878
|
-
}
|
879
|
-
|
880
|
-
const serverProt = res.headers['sec-websocket-protocol'];
|
881
|
-
let protError;
|
882
|
-
|
883
|
-
if (serverProt !== undefined) {
|
884
|
-
if (!protocolSet.size) {
|
885
|
-
protError = 'Server sent a subprotocol but none was requested';
|
886
|
-
} else if (!protocolSet.has(serverProt)) {
|
887
|
-
protError = 'Server sent an invalid subprotocol';
|
888
|
-
}
|
889
|
-
} else if (protocolSet.size) {
|
890
|
-
protError = 'Server sent no subprotocol';
|
891
|
-
}
|
892
|
-
|
893
|
-
if (protError) {
|
894
|
-
abortHandshake(websocket, socket, protError);
|
895
|
-
return;
|
896
|
-
}
|
897
|
-
|
898
|
-
if (serverProt) websocket._protocol = serverProt;
|
899
|
-
|
900
|
-
const secWebSocketExtensions = res.headers['sec-websocket-extensions'];
|
901
|
-
|
902
|
-
if (secWebSocketExtensions !== undefined) {
|
903
|
-
if (!perMessageDeflate) {
|
904
|
-
const message =
|
905
|
-
'Server sent a Sec-WebSocket-Extensions header but no extension ' +
|
906
|
-
'was requested';
|
907
|
-
abortHandshake(websocket, socket, message);
|
908
|
-
return;
|
909
|
-
}
|
910
|
-
|
911
|
-
let extensions;
|
912
|
-
|
913
|
-
try {
|
914
|
-
extensions = parse(secWebSocketExtensions);
|
915
|
-
} catch (err) {
|
916
|
-
const message = 'Invalid Sec-WebSocket-Extensions header';
|
917
|
-
abortHandshake(websocket, socket, message);
|
918
|
-
return;
|
919
|
-
}
|
920
|
-
|
921
|
-
const extensionNames = Object.keys(extensions);
|
922
|
-
|
923
|
-
if (
|
924
|
-
extensionNames.length !== 1 ||
|
925
|
-
extensionNames[0] !== PerMessageDeflate.extensionName
|
926
|
-
) {
|
927
|
-
const message = 'Server indicated an extension that was not requested';
|
928
|
-
abortHandshake(websocket, socket, message);
|
929
|
-
return;
|
930
|
-
}
|
931
|
-
|
932
|
-
try {
|
933
|
-
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
934
|
-
} catch (err) {
|
935
|
-
const message = 'Invalid Sec-WebSocket-Extensions header';
|
936
|
-
abortHandshake(websocket, socket, message);
|
937
|
-
return;
|
938
|
-
}
|
939
|
-
|
940
|
-
websocket._extensions[PerMessageDeflate.extensionName] =
|
941
|
-
perMessageDeflate;
|
942
|
-
}
|
943
|
-
|
944
|
-
websocket.setSocket(socket, head, {
|
945
|
-
generateMask: opts.generateMask,
|
946
|
-
maxPayload: opts.maxPayload,
|
947
|
-
skipUTF8Validation: opts.skipUTF8Validation
|
948
|
-
});
|
949
|
-
});
|
950
|
-
}
|
951
|
-
|
952
|
-
/**
|
953
|
-
* Emit the `'error'` and `'close'` event.
|
954
|
-
*
|
955
|
-
* @param {WebSocket} websocket The WebSocket instance
|
956
|
-
* @param {Error} The error to emit
|
957
|
-
* @private
|
958
|
-
*/
|
959
|
-
function emitErrorAndClose(websocket, err) {
|
960
|
-
websocket._readyState = WebSocket.CLOSING;
|
961
|
-
websocket.emit('error', err);
|
962
|
-
websocket.emitClose();
|
963
|
-
}
|
964
|
-
|
965
|
-
/**
|
966
|
-
* Create a `net.Socket` and initiate a connection.
|
967
|
-
*
|
968
|
-
* @param {Object} options Connection options
|
969
|
-
* @return {net.Socket} The newly created socket used to start the connection
|
970
|
-
* @private
|
971
|
-
*/
|
972
|
-
function netConnect(options) {
|
973
|
-
options.path = options.socketPath;
|
974
|
-
return net.connect(options);
|
975
|
-
}
|
976
|
-
|
977
|
-
/**
|
978
|
-
* Create a `tls.TLSSocket` and initiate a connection.
|
979
|
-
*
|
980
|
-
* @param {Object} options Connection options
|
981
|
-
* @return {tls.TLSSocket} The newly created socket used to start the connection
|
982
|
-
* @private
|
983
|
-
*/
|
984
|
-
function tlsConnect(options) {
|
985
|
-
options.path = undefined;
|
986
|
-
|
987
|
-
if (!options.servername && options.servername !== '') {
|
988
|
-
options.servername = net.isIP(options.host) ? '' : options.host;
|
989
|
-
}
|
990
|
-
|
991
|
-
return tls.connect(options);
|
992
|
-
}
|
993
|
-
|
994
|
-
/**
|
995
|
-
* Abort the handshake and emit an error.
|
996
|
-
*
|
997
|
-
* @param {WebSocket} websocket The WebSocket instance
|
998
|
-
* @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
|
999
|
-
* abort or the socket to destroy
|
1000
|
-
* @param {String} message The error message
|
1001
|
-
* @private
|
1002
|
-
*/
|
1003
|
-
function abortHandshake(websocket, stream, message) {
|
1004
|
-
websocket._readyState = WebSocket.CLOSING;
|
1005
|
-
|
1006
|
-
const err = new Error(message);
|
1007
|
-
Error.captureStackTrace(err, abortHandshake);
|
1008
|
-
|
1009
|
-
if (stream.setHeader) {
|
1010
|
-
stream.abort();
|
1011
|
-
|
1012
|
-
if (stream.socket && !stream.socket.destroyed) {
|
1013
|
-
//
|
1014
|
-
// On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if
|
1015
|
-
// called after the request completed. See
|
1016
|
-
// https://github.com/websockets/ws/issues/1869.
|
1017
|
-
//
|
1018
|
-
stream.socket.destroy();
|
1019
|
-
}
|
1020
|
-
|
1021
|
-
stream.once('abort', websocket.emitClose.bind(websocket));
|
1022
|
-
websocket.emit('error', err);
|
1023
|
-
} else {
|
1024
|
-
stream.destroy(err);
|
1025
|
-
stream.once('error', websocket.emit.bind(websocket, 'error'));
|
1026
|
-
stream.once('close', websocket.emitClose.bind(websocket));
|
1027
|
-
}
|
1028
|
-
}
|
1029
|
-
|
1030
|
-
/**
|
1031
|
-
* Handle cases where the `ping()`, `pong()`, or `send()` methods are called
|
1032
|
-
* when the `readyState` attribute is `CLOSING` or `CLOSED`.
|
1033
|
-
*
|
1034
|
-
* @param {WebSocket} websocket The WebSocket instance
|
1035
|
-
* @param {*} [data] The data to send
|
1036
|
-
* @param {Function} [cb] Callback
|
1037
|
-
* @private
|
1038
|
-
*/
|
1039
|
-
function sendAfterClose(websocket, data, cb) {
|
1040
|
-
if (data) {
|
1041
|
-
const length = toBuffer(data).length;
|
1042
|
-
|
1043
|
-
//
|
1044
|
-
// The `_bufferedAmount` property is used only when the peer is a client and
|
1045
|
-
// the opening handshake fails. Under these circumstances, in fact, the
|
1046
|
-
// `setSocket()` method is not called, so the `_socket` and `_sender`
|
1047
|
-
// properties are set to `null`.
|
1048
|
-
//
|
1049
|
-
if (websocket._socket) websocket._sender._bufferedBytes += length;
|
1050
|
-
else websocket._bufferedAmount += length;
|
1051
|
-
}
|
1052
|
-
|
1053
|
-
if (cb) {
|
1054
|
-
const err = new Error(
|
1055
|
-
`WebSocket is not open: readyState ${websocket.readyState} ` +
|
1056
|
-
`(${readyStates[websocket.readyState]})`
|
1057
|
-
);
|
1058
|
-
cb(err);
|
1059
|
-
}
|
1060
|
-
}
|
1061
|
-
|
1062
|
-
/**
|
1063
|
-
* The listener of the `Receiver` `'conclude'` event.
|
1064
|
-
*
|
1065
|
-
* @param {Number} code The status code
|
1066
|
-
* @param {Buffer} reason The reason for closing
|
1067
|
-
* @private
|
1068
|
-
*/
|
1069
|
-
function receiverOnConclude(code, reason) {
|
1070
|
-
const websocket = this[kWebSocket];
|
1071
|
-
|
1072
|
-
websocket._closeFrameReceived = true;
|
1073
|
-
websocket._closeMessage = reason;
|
1074
|
-
websocket._closeCode = code;
|
1075
|
-
|
1076
|
-
if (websocket._socket[kWebSocket] === undefined) return;
|
1077
|
-
|
1078
|
-
websocket._socket.removeListener('data', socketOnData);
|
1079
|
-
process.nextTick(resume, websocket._socket);
|
1080
|
-
|
1081
|
-
if (code === 1005) websocket.close();
|
1082
|
-
else websocket.close(code, reason);
|
1083
|
-
}
|
1084
|
-
|
1085
|
-
/**
|
1086
|
-
* The listener of the `Receiver` `'drain'` event.
|
1087
|
-
*
|
1088
|
-
* @private
|
1089
|
-
*/
|
1090
|
-
function receiverOnDrain() {
|
1091
|
-
const websocket = this[kWebSocket];
|
1092
|
-
|
1093
|
-
if (!websocket.isPaused) websocket._socket.resume();
|
1094
|
-
}
|
1095
|
-
|
1096
|
-
/**
|
1097
|
-
* The listener of the `Receiver` `'error'` event.
|
1098
|
-
*
|
1099
|
-
* @param {(RangeError|Error)} err The emitted error
|
1100
|
-
* @private
|
1101
|
-
*/
|
1102
|
-
function receiverOnError(err) {
|
1103
|
-
const websocket = this[kWebSocket];
|
1104
|
-
|
1105
|
-
if (websocket._socket[kWebSocket] !== undefined) {
|
1106
|
-
websocket._socket.removeListener('data', socketOnData);
|
1107
|
-
|
1108
|
-
//
|
1109
|
-
// On Node.js < 14.0.0 the `'error'` event is emitted synchronously. See
|
1110
|
-
// https://github.com/websockets/ws/issues/1940.
|
1111
|
-
//
|
1112
|
-
process.nextTick(resume, websocket._socket);
|
1113
|
-
|
1114
|
-
websocket.close(err[kStatusCode]);
|
1115
|
-
}
|
1116
|
-
|
1117
|
-
websocket.emit('error', err);
|
1118
|
-
}
|
1119
|
-
|
1120
|
-
/**
|
1121
|
-
* The listener of the `Receiver` `'finish'` event.
|
1122
|
-
*
|
1123
|
-
* @private
|
1124
|
-
*/
|
1125
|
-
function receiverOnFinish() {
|
1126
|
-
this[kWebSocket].emitClose();
|
1127
|
-
}
|
1128
|
-
|
1129
|
-
/**
|
1130
|
-
* The listener of the `Receiver` `'message'` event.
|
1131
|
-
*
|
1132
|
-
* @param {Buffer|ArrayBuffer|Buffer[])} data The message
|
1133
|
-
* @param {Boolean} isBinary Specifies whether the message is binary or not
|
1134
|
-
* @private
|
1135
|
-
*/
|
1136
|
-
function receiverOnMessage(data, isBinary) {
|
1137
|
-
this[kWebSocket].emit('message', data, isBinary);
|
1138
|
-
}
|
1139
|
-
|
1140
|
-
/**
|
1141
|
-
* The listener of the `Receiver` `'ping'` event.
|
1142
|
-
*
|
1143
|
-
* @param {Buffer} data The data included in the ping frame
|
1144
|
-
* @private
|
1145
|
-
*/
|
1146
|
-
function receiverOnPing(data) {
|
1147
|
-
const websocket = this[kWebSocket];
|
1148
|
-
|
1149
|
-
websocket.pong(data, !websocket._isServer, NOOP);
|
1150
|
-
websocket.emit('ping', data);
|
1151
|
-
}
|
1152
|
-
|
1153
|
-
/**
|
1154
|
-
* The listener of the `Receiver` `'pong'` event.
|
1155
|
-
*
|
1156
|
-
* @param {Buffer} data The data included in the pong frame
|
1157
|
-
* @private
|
1158
|
-
*/
|
1159
|
-
function receiverOnPong(data) {
|
1160
|
-
this[kWebSocket].emit('pong', data);
|
1161
|
-
}
|
1162
|
-
|
1163
|
-
/**
|
1164
|
-
* Resume a readable stream
|
1165
|
-
*
|
1166
|
-
* @param {Readable} stream The readable stream
|
1167
|
-
* @private
|
1168
|
-
*/
|
1169
|
-
function resume(stream) {
|
1170
|
-
stream.resume();
|
1171
|
-
}
|
1172
|
-
|
1173
|
-
/**
|
1174
|
-
* The listener of the `net.Socket` `'close'` event.
|
1175
|
-
*
|
1176
|
-
* @private
|
1177
|
-
*/
|
1178
|
-
function socketOnClose() {
|
1179
|
-
const websocket = this[kWebSocket];
|
1180
|
-
|
1181
|
-
this.removeListener('close', socketOnClose);
|
1182
|
-
this.removeListener('data', socketOnData);
|
1183
|
-
this.removeListener('end', socketOnEnd);
|
1184
|
-
|
1185
|
-
websocket._readyState = WebSocket.CLOSING;
|
1186
|
-
|
1187
|
-
let chunk;
|
1188
|
-
|
1189
|
-
//
|
1190
|
-
// The close frame might not have been received or the `'end'` event emitted,
|
1191
|
-
// for example, if the socket was destroyed due to an error. Ensure that the
|
1192
|
-
// `receiver` stream is closed after writing any remaining buffered data to
|
1193
|
-
// it. If the readable side of the socket is in flowing mode then there is no
|
1194
|
-
// buffered data as everything has been already written and `readable.read()`
|
1195
|
-
// will return `null`. If instead, the socket is paused, any possible buffered
|
1196
|
-
// data will be read as a single chunk.
|
1197
|
-
//
|
1198
|
-
if (
|
1199
|
-
!this._readableState.endEmitted &&
|
1200
|
-
!websocket._closeFrameReceived &&
|
1201
|
-
!websocket._receiver._writableState.errorEmitted &&
|
1202
|
-
(chunk = websocket._socket.read()) !== null
|
1203
|
-
) {
|
1204
|
-
websocket._receiver.write(chunk);
|
1205
|
-
}
|
1206
|
-
|
1207
|
-
websocket._receiver.end();
|
1208
|
-
|
1209
|
-
this[kWebSocket] = undefined;
|
1210
|
-
|
1211
|
-
clearTimeout(websocket._closeTimer);
|
1212
|
-
|
1213
|
-
if (
|
1214
|
-
websocket._receiver._writableState.finished ||
|
1215
|
-
websocket._receiver._writableState.errorEmitted
|
1216
|
-
) {
|
1217
|
-
websocket.emitClose();
|
1218
|
-
} else {
|
1219
|
-
websocket._receiver.on('error', receiverOnFinish);
|
1220
|
-
websocket._receiver.on('finish', receiverOnFinish);
|
1221
|
-
}
|
1222
|
-
}
|
1223
|
-
|
1224
|
-
/**
|
1225
|
-
* The listener of the `net.Socket` `'data'` event.
|
1226
|
-
*
|
1227
|
-
* @param {Buffer} chunk A chunk of data
|
1228
|
-
* @private
|
1229
|
-
*/
|
1230
|
-
function socketOnData(chunk) {
|
1231
|
-
if (!this[kWebSocket]._receiver.write(chunk)) {
|
1232
|
-
this.pause();
|
1233
|
-
}
|
1234
|
-
}
|
1235
|
-
|
1236
|
-
/**
|
1237
|
-
* The listener of the `net.Socket` `'end'` event.
|
1238
|
-
*
|
1239
|
-
* @private
|
1240
|
-
*/
|
1241
|
-
function socketOnEnd() {
|
1242
|
-
const websocket = this[kWebSocket];
|
1243
|
-
|
1244
|
-
websocket._readyState = WebSocket.CLOSING;
|
1245
|
-
websocket._receiver.end();
|
1246
|
-
this.end();
|
1247
|
-
}
|
1248
|
-
|
1249
|
-
/**
|
1250
|
-
* The listener of the `net.Socket` `'error'` event.
|
1251
|
-
*
|
1252
|
-
* @private
|
1253
|
-
*/
|
1254
|
-
function socketOnError() {
|
1255
|
-
const websocket = this[kWebSocket];
|
1256
|
-
|
1257
|
-
this.removeListener('error', socketOnError);
|
1258
|
-
this.on('error', NOOP);
|
1259
|
-
|
1260
|
-
if (websocket) {
|
1261
|
-
websocket._readyState = WebSocket.CLOSING;
|
1262
|
-
this.destroy();
|
1263
|
-
}
|
1264
|
-
}
|