isomorfeus-transport 2.0.9 → 2.0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/isomorfeus/transport/version.rb +1 -1
- data/node_modules/.package-lock.json +27 -27
- data/node_modules/ws/LICENSE +19 -19
- data/node_modules/ws/README.md +489 -496
- data/node_modules/ws/browser.js +8 -8
- data/node_modules/ws/index.js +13 -13
- data/node_modules/ws/lib/buffer-util.js +126 -126
- data/node_modules/ws/lib/constants.js +12 -12
- data/node_modules/ws/lib/event-target.js +266 -266
- data/node_modules/ws/lib/extension.js +203 -203
- data/node_modules/ws/lib/limiter.js +55 -55
- data/node_modules/ws/lib/permessage-deflate.js +511 -511
- data/node_modules/ws/lib/receiver.js +618 -612
- data/node_modules/ws/lib/sender.js +478 -414
- data/node_modules/ws/lib/stream.js +159 -180
- data/node_modules/ws/lib/subprotocol.js +62 -62
- data/node_modules/ws/lib/validation.js +124 -124
- data/node_modules/ws/lib/websocket-server.js +485 -485
- data/node_modules/ws/lib/websocket.js +1225 -1144
- data/node_modules/ws/package.json +61 -61
- data/node_modules/ws/wrapper.mjs +8 -8
- data/package.json +1 -1
- metadata +12 -12
@@ -1,485 +1,485 @@
|
|
1
|
-
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
|
2
|
-
|
3
|
-
'use strict';
|
4
|
-
|
5
|
-
const EventEmitter = require('events');
|
6
|
-
const http = require('http');
|
7
|
-
const https = require('https');
|
8
|
-
const net = require('net');
|
9
|
-
const tls = require('tls');
|
10
|
-
const { createHash } = require('crypto');
|
11
|
-
|
12
|
-
const extension = require('./extension');
|
13
|
-
const PerMessageDeflate = require('./permessage-deflate');
|
14
|
-
const subprotocol = require('./subprotocol');
|
15
|
-
const WebSocket = require('./websocket');
|
16
|
-
const { GUID, kWebSocket } = require('./constants');
|
17
|
-
|
18
|
-
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
19
|
-
|
20
|
-
const RUNNING = 0;
|
21
|
-
const CLOSING = 1;
|
22
|
-
const CLOSED = 2;
|
23
|
-
|
24
|
-
/**
|
25
|
-
* Class representing a WebSocket server.
|
26
|
-
*
|
27
|
-
* @extends EventEmitter
|
28
|
-
*/
|
29
|
-
class WebSocketServer extends EventEmitter {
|
30
|
-
/**
|
31
|
-
* Create a `WebSocketServer` instance.
|
32
|
-
*
|
33
|
-
* @param {Object} options Configuration options
|
34
|
-
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
35
|
-
* pending connections
|
36
|
-
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
37
|
-
* track clients
|
38
|
-
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
39
|
-
* @param {String} [options.host] The hostname where to bind the server
|
40
|
-
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
41
|
-
* size
|
42
|
-
* @param {Boolean} [options.noServer=false] Enable no server mode
|
43
|
-
* @param {String} [options.path] Accept only connections matching this path
|
44
|
-
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
45
|
-
* permessage-deflate
|
46
|
-
* @param {Number} [options.port] The port where to bind the server
|
47
|
-
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
48
|
-
* server to use
|
49
|
-
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
50
|
-
* not to skip UTF-8 validation for text and close messages
|
51
|
-
* @param {Function} [options.verifyClient] A hook to reject connections
|
52
|
-
* @param {Function} [callback] A listener for the `listening` event
|
53
|
-
*/
|
54
|
-
constructor(options, callback) {
|
55
|
-
super();
|
56
|
-
|
57
|
-
options = {
|
58
|
-
maxPayload: 100 * 1024 * 1024,
|
59
|
-
skipUTF8Validation: false,
|
60
|
-
perMessageDeflate: false,
|
61
|
-
handleProtocols: null,
|
62
|
-
clientTracking: true,
|
63
|
-
verifyClient: null,
|
64
|
-
noServer: false,
|
65
|
-
backlog: null, // use default (511 as implemented in net.js)
|
66
|
-
server: null,
|
67
|
-
host: null,
|
68
|
-
path: null,
|
69
|
-
port: null,
|
70
|
-
...options
|
71
|
-
};
|
72
|
-
|
73
|
-
if (
|
74
|
-
(options.port == null && !options.server && !options.noServer) ||
|
75
|
-
(options.port != null && (options.server || options.noServer)) ||
|
76
|
-
(options.server && options.noServer)
|
77
|
-
) {
|
78
|
-
throw new TypeError(
|
79
|
-
'One and only one of the "port", "server", or "noServer" options ' +
|
80
|
-
'must be specified'
|
81
|
-
);
|
82
|
-
}
|
83
|
-
|
84
|
-
if (options.port != null) {
|
85
|
-
this._server = http.createServer((req, res) => {
|
86
|
-
const body = http.STATUS_CODES[426];
|
87
|
-
|
88
|
-
res.writeHead(426, {
|
89
|
-
'Content-Length': body.length,
|
90
|
-
'Content-Type': 'text/plain'
|
91
|
-
});
|
92
|
-
res.end(body);
|
93
|
-
});
|
94
|
-
this._server.listen(
|
95
|
-
options.port,
|
96
|
-
options.host,
|
97
|
-
options.backlog,
|
98
|
-
callback
|
99
|
-
);
|
100
|
-
} else if (options.server) {
|
101
|
-
this._server = options.server;
|
102
|
-
}
|
103
|
-
|
104
|
-
if (this._server) {
|
105
|
-
const emitConnection = this.emit.bind(this, 'connection');
|
106
|
-
|
107
|
-
this._removeListeners = addListeners(this._server, {
|
108
|
-
listening: this.emit.bind(this, 'listening'),
|
109
|
-
error: this.emit.bind(this, 'error'),
|
110
|
-
upgrade: (req, socket, head) => {
|
111
|
-
this.handleUpgrade(req, socket, head, emitConnection);
|
112
|
-
}
|
113
|
-
});
|
114
|
-
}
|
115
|
-
|
116
|
-
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
|
117
|
-
if (options.clientTracking) {
|
118
|
-
this.clients = new Set();
|
119
|
-
this._shouldEmitClose = false;
|
120
|
-
}
|
121
|
-
|
122
|
-
this.options = options;
|
123
|
-
this._state = RUNNING;
|
124
|
-
}
|
125
|
-
|
126
|
-
/**
|
127
|
-
* Returns the bound address, the address family name, and port of the server
|
128
|
-
* as reported by the operating system if listening on an IP socket.
|
129
|
-
* If the server is listening on a pipe or UNIX domain socket, the name is
|
130
|
-
* returned as a string.
|
131
|
-
*
|
132
|
-
* @return {(Object|String|null)} The address of the server
|
133
|
-
* @public
|
134
|
-
*/
|
135
|
-
address() {
|
136
|
-
if (this.options.noServer) {
|
137
|
-
throw new Error('The server is operating in "noServer" mode');
|
138
|
-
}
|
139
|
-
|
140
|
-
if (!this._server) return null;
|
141
|
-
return this._server.address();
|
142
|
-
}
|
143
|
-
|
144
|
-
/**
|
145
|
-
* Stop the server from accepting new connections and emit the `'close'` event
|
146
|
-
* when all existing connections are closed.
|
147
|
-
*
|
148
|
-
* @param {Function} [cb] A one-time listener for the `'close'` event
|
149
|
-
* @public
|
150
|
-
*/
|
151
|
-
close(cb) {
|
152
|
-
if (this._state === CLOSED) {
|
153
|
-
if (cb) {
|
154
|
-
this.once('close', () => {
|
155
|
-
cb(new Error('The server is not running'));
|
156
|
-
});
|
157
|
-
}
|
158
|
-
|
159
|
-
process.nextTick(emitClose, this);
|
160
|
-
return;
|
161
|
-
}
|
162
|
-
|
163
|
-
if (cb) this.once('close', cb);
|
164
|
-
|
165
|
-
if (this._state === CLOSING) return;
|
166
|
-
this._state = CLOSING;
|
167
|
-
|
168
|
-
if (this.options.noServer || this.options.server) {
|
169
|
-
if (this._server) {
|
170
|
-
this._removeListeners();
|
171
|
-
this._removeListeners = this._server = null;
|
172
|
-
}
|
173
|
-
|
174
|
-
if (this.clients) {
|
175
|
-
if (!this.clients.size) {
|
176
|
-
process.nextTick(emitClose, this);
|
177
|
-
} else {
|
178
|
-
this._shouldEmitClose = true;
|
179
|
-
}
|
180
|
-
} else {
|
181
|
-
process.nextTick(emitClose, this);
|
182
|
-
}
|
183
|
-
} else {
|
184
|
-
const server = this._server;
|
185
|
-
|
186
|
-
this._removeListeners();
|
187
|
-
this._removeListeners = this._server = null;
|
188
|
-
|
189
|
-
//
|
190
|
-
// The HTTP/S server was created internally. Close it, and rely on its
|
191
|
-
// `'close'` event.
|
192
|
-
//
|
193
|
-
server.close(() => {
|
194
|
-
emitClose(this);
|
195
|
-
});
|
196
|
-
}
|
197
|
-
}
|
198
|
-
|
199
|
-
/**
|
200
|
-
* See if a given request should be handled by this server instance.
|
201
|
-
*
|
202
|
-
* @param {http.IncomingMessage} req Request object to inspect
|
203
|
-
* @return {Boolean} `true` if the request is valid, else `false`
|
204
|
-
* @public
|
205
|
-
*/
|
206
|
-
shouldHandle(req) {
|
207
|
-
if (this.options.path) {
|
208
|
-
const index = req.url.indexOf('?');
|
209
|
-
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
210
|
-
|
211
|
-
if (pathname !== this.options.path) return false;
|
212
|
-
}
|
213
|
-
|
214
|
-
return true;
|
215
|
-
}
|
216
|
-
|
217
|
-
/**
|
218
|
-
* Handle a HTTP Upgrade request.
|
219
|
-
*
|
220
|
-
* @param {http.IncomingMessage} req The request object
|
221
|
-
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
222
|
-
* server and client
|
223
|
-
* @param {Buffer} head The first packet of the upgraded stream
|
224
|
-
* @param {Function} cb Callback
|
225
|
-
* @public
|
226
|
-
*/
|
227
|
-
handleUpgrade(req, socket, head, cb) {
|
228
|
-
socket.on('error', socketOnError);
|
229
|
-
|
230
|
-
const key =
|
231
|
-
req.headers['sec-websocket-key'] !== undefined
|
232
|
-
? req.headers['sec-websocket-key']
|
233
|
-
: false;
|
234
|
-
const version = +req.headers['sec-websocket-version'];
|
235
|
-
|
236
|
-
if (
|
237
|
-
req.method !== 'GET' ||
|
238
|
-
req.headers.upgrade.toLowerCase() !== 'websocket' ||
|
239
|
-
!key ||
|
240
|
-
!keyRegex.test(key) ||
|
241
|
-
(version !== 8 && version !== 13) ||
|
242
|
-
!this.shouldHandle(req)
|
243
|
-
) {
|
244
|
-
return abortHandshake(socket, 400);
|
245
|
-
}
|
246
|
-
|
247
|
-
const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
|
248
|
-
let protocols = new Set();
|
249
|
-
|
250
|
-
if (secWebSocketProtocol !== undefined) {
|
251
|
-
try {
|
252
|
-
protocols = subprotocol.parse(secWebSocketProtocol);
|
253
|
-
} catch (err) {
|
254
|
-
return abortHandshake(socket, 400);
|
255
|
-
}
|
256
|
-
}
|
257
|
-
|
258
|
-
const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
|
259
|
-
const extensions = {};
|
260
|
-
|
261
|
-
if (
|
262
|
-
this.options.perMessageDeflate &&
|
263
|
-
secWebSocketExtensions !== undefined
|
264
|
-
) {
|
265
|
-
const perMessageDeflate = new PerMessageDeflate(
|
266
|
-
this.options.perMessageDeflate,
|
267
|
-
true,
|
268
|
-
this.options.maxPayload
|
269
|
-
);
|
270
|
-
|
271
|
-
try {
|
272
|
-
const offers = extension.parse(secWebSocketExtensions);
|
273
|
-
|
274
|
-
if (offers[PerMessageDeflate.extensionName]) {
|
275
|
-
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
276
|
-
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
277
|
-
}
|
278
|
-
} catch (err) {
|
279
|
-
return abortHandshake(socket, 400);
|
280
|
-
}
|
281
|
-
}
|
282
|
-
|
283
|
-
//
|
284
|
-
// Optionally call external client verification handler.
|
285
|
-
//
|
286
|
-
if (this.options.verifyClient) {
|
287
|
-
const info = {
|
288
|
-
origin:
|
289
|
-
req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
|
290
|
-
secure: !!(req.socket.authorized || req.socket.encrypted),
|
291
|
-
req
|
292
|
-
};
|
293
|
-
|
294
|
-
if (this.options.verifyClient.length === 2) {
|
295
|
-
this.options.verifyClient(info, (verified, code, message, headers) => {
|
296
|
-
if (!verified) {
|
297
|
-
return abortHandshake(socket, code || 401, message, headers);
|
298
|
-
}
|
299
|
-
|
300
|
-
this.completeUpgrade(
|
301
|
-
extensions,
|
302
|
-
key,
|
303
|
-
protocols,
|
304
|
-
req,
|
305
|
-
socket,
|
306
|
-
head,
|
307
|
-
cb
|
308
|
-
);
|
309
|
-
});
|
310
|
-
return;
|
311
|
-
}
|
312
|
-
|
313
|
-
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
314
|
-
}
|
315
|
-
|
316
|
-
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
317
|
-
}
|
318
|
-
|
319
|
-
/**
|
320
|
-
* Upgrade the connection to WebSocket.
|
321
|
-
*
|
322
|
-
* @param {Object} extensions The accepted extensions
|
323
|
-
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
324
|
-
* @param {Set} protocols The subprotocols
|
325
|
-
* @param {http.IncomingMessage} req The request object
|
326
|
-
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
327
|
-
* server and client
|
328
|
-
* @param {Buffer} head The first packet of the upgraded stream
|
329
|
-
* @param {Function} cb Callback
|
330
|
-
* @throws {Error} If called more than once with the same socket
|
331
|
-
* @private
|
332
|
-
*/
|
333
|
-
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
334
|
-
//
|
335
|
-
// Destroy the socket if the client has already sent a FIN packet.
|
336
|
-
//
|
337
|
-
if (!socket.readable || !socket.writable) return socket.destroy();
|
338
|
-
|
339
|
-
if (socket[kWebSocket]) {
|
340
|
-
throw new Error(
|
341
|
-
'server.handleUpgrade() was called more than once with the same ' +
|
342
|
-
'socket, possibly due to a misconfiguration'
|
343
|
-
);
|
344
|
-
}
|
345
|
-
|
346
|
-
if (this._state > RUNNING) return abortHandshake(socket, 503);
|
347
|
-
|
348
|
-
const digest = createHash('sha1')
|
349
|
-
.update(key + GUID)
|
350
|
-
.digest('base64');
|
351
|
-
|
352
|
-
const headers = [
|
353
|
-
'HTTP/1.1 101 Switching Protocols',
|
354
|
-
'Upgrade: websocket',
|
355
|
-
'Connection: Upgrade',
|
356
|
-
`Sec-WebSocket-Accept: ${digest}`
|
357
|
-
];
|
358
|
-
|
359
|
-
const ws = new WebSocket(null);
|
360
|
-
|
361
|
-
if (protocols.size) {
|
362
|
-
//
|
363
|
-
// Optionally call external protocol selection handler.
|
364
|
-
//
|
365
|
-
const protocol = this.options.handleProtocols
|
366
|
-
? this.options.handleProtocols(protocols, req)
|
367
|
-
: protocols.values().next().value;
|
368
|
-
|
369
|
-
if (protocol) {
|
370
|
-
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
371
|
-
ws._protocol = protocol;
|
372
|
-
}
|
373
|
-
}
|
374
|
-
|
375
|
-
if (extensions[PerMessageDeflate.extensionName]) {
|
376
|
-
const params = extensions[PerMessageDeflate.extensionName].params;
|
377
|
-
const value = extension.format({
|
378
|
-
[PerMessageDeflate.extensionName]: [params]
|
379
|
-
});
|
380
|
-
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
381
|
-
ws._extensions = extensions;
|
382
|
-
}
|
383
|
-
|
384
|
-
//
|
385
|
-
// Allow external modification/inspection of handshake headers.
|
386
|
-
//
|
387
|
-
this.emit('headers', headers, req);
|
388
|
-
|
389
|
-
socket.write(headers.concat('\r\n').join('\r\n'));
|
390
|
-
socket.removeListener('error', socketOnError);
|
391
|
-
|
392
|
-
ws.setSocket(socket, head, {
|
393
|
-
maxPayload: this.options.maxPayload,
|
394
|
-
skipUTF8Validation: this.options.skipUTF8Validation
|
395
|
-
});
|
396
|
-
|
397
|
-
if (this.clients) {
|
398
|
-
this.clients.add(ws);
|
399
|
-
ws.on('close', () => {
|
400
|
-
this.clients.delete(ws);
|
401
|
-
|
402
|
-
if (this._shouldEmitClose && !this.clients.size) {
|
403
|
-
process.nextTick(emitClose, this);
|
404
|
-
}
|
405
|
-
});
|
406
|
-
}
|
407
|
-
|
408
|
-
cb(ws, req);
|
409
|
-
}
|
410
|
-
}
|
411
|
-
|
412
|
-
module.exports = WebSocketServer;
|
413
|
-
|
414
|
-
/**
|
415
|
-
* Add event listeners on an `EventEmitter` using a map of <event, listener>
|
416
|
-
* pairs.
|
417
|
-
*
|
418
|
-
* @param {EventEmitter} server The event emitter
|
419
|
-
* @param {Object.<String, Function>} map The listeners to add
|
420
|
-
* @return {Function} A function that will remove the added listeners when
|
421
|
-
* called
|
422
|
-
* @private
|
423
|
-
*/
|
424
|
-
function addListeners(server, map) {
|
425
|
-
for (const event of Object.keys(map)) server.on(event, map[event]);
|
426
|
-
|
427
|
-
return function removeListeners() {
|
428
|
-
for (const event of Object.keys(map)) {
|
429
|
-
server.removeListener(event, map[event]);
|
430
|
-
}
|
431
|
-
};
|
432
|
-
}
|
433
|
-
|
434
|
-
/**
|
435
|
-
* Emit a `'close'` event on an `EventEmitter`.
|
436
|
-
*
|
437
|
-
* @param {EventEmitter} server The event emitter
|
438
|
-
* @private
|
439
|
-
*/
|
440
|
-
function emitClose(server) {
|
441
|
-
server._state = CLOSED;
|
442
|
-
server.emit('close');
|
443
|
-
}
|
444
|
-
|
445
|
-
/**
|
446
|
-
* Handle premature socket errors.
|
447
|
-
*
|
448
|
-
* @private
|
449
|
-
*/
|
450
|
-
function socketOnError() {
|
451
|
-
this.destroy();
|
452
|
-
}
|
453
|
-
|
454
|
-
/**
|
455
|
-
* Close the connection when preconditions are not fulfilled.
|
456
|
-
*
|
457
|
-
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
|
458
|
-
* @param {Number} code The HTTP response status code
|
459
|
-
* @param {String} [message] The HTTP response body
|
460
|
-
* @param {Object} [headers] Additional HTTP response headers
|
461
|
-
* @private
|
462
|
-
*/
|
463
|
-
function abortHandshake(socket, code, message, headers) {
|
464
|
-
if (socket.writable) {
|
465
|
-
message = message || http.STATUS_CODES[code];
|
466
|
-
headers = {
|
467
|
-
Connection: 'close',
|
468
|
-
'Content-Type': 'text/html',
|
469
|
-
'Content-Length': Buffer.byteLength(message),
|
470
|
-
...headers
|
471
|
-
};
|
472
|
-
|
473
|
-
socket.write(
|
474
|
-
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
|
475
|
-
Object.keys(headers)
|
476
|
-
.map((h) => `${h}: ${headers[h]}`)
|
477
|
-
.join('\r\n') +
|
478
|
-
'\r\n\r\n' +
|
479
|
-
message
|
480
|
-
);
|
481
|
-
}
|
482
|
-
|
483
|
-
socket.removeListener('error', socketOnError);
|
484
|
-
socket.destroy();
|
485
|
-
}
|
1
|
+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
|
2
|
+
|
3
|
+
'use strict';
|
4
|
+
|
5
|
+
const EventEmitter = require('events');
|
6
|
+
const http = require('http');
|
7
|
+
const https = require('https');
|
8
|
+
const net = require('net');
|
9
|
+
const tls = require('tls');
|
10
|
+
const { createHash } = require('crypto');
|
11
|
+
|
12
|
+
const extension = require('./extension');
|
13
|
+
const PerMessageDeflate = require('./permessage-deflate');
|
14
|
+
const subprotocol = require('./subprotocol');
|
15
|
+
const WebSocket = require('./websocket');
|
16
|
+
const { GUID, kWebSocket } = require('./constants');
|
17
|
+
|
18
|
+
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
19
|
+
|
20
|
+
const RUNNING = 0;
|
21
|
+
const CLOSING = 1;
|
22
|
+
const CLOSED = 2;
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Class representing a WebSocket server.
|
26
|
+
*
|
27
|
+
* @extends EventEmitter
|
28
|
+
*/
|
29
|
+
class WebSocketServer extends EventEmitter {
|
30
|
+
/**
|
31
|
+
* Create a `WebSocketServer` instance.
|
32
|
+
*
|
33
|
+
* @param {Object} options Configuration options
|
34
|
+
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
35
|
+
* pending connections
|
36
|
+
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
37
|
+
* track clients
|
38
|
+
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
39
|
+
* @param {String} [options.host] The hostname where to bind the server
|
40
|
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
41
|
+
* size
|
42
|
+
* @param {Boolean} [options.noServer=false] Enable no server mode
|
43
|
+
* @param {String} [options.path] Accept only connections matching this path
|
44
|
+
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
45
|
+
* permessage-deflate
|
46
|
+
* @param {Number} [options.port] The port where to bind the server
|
47
|
+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
48
|
+
* server to use
|
49
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
50
|
+
* not to skip UTF-8 validation for text and close messages
|
51
|
+
* @param {Function} [options.verifyClient] A hook to reject connections
|
52
|
+
* @param {Function} [callback] A listener for the `listening` event
|
53
|
+
*/
|
54
|
+
constructor(options, callback) {
|
55
|
+
super();
|
56
|
+
|
57
|
+
options = {
|
58
|
+
maxPayload: 100 * 1024 * 1024,
|
59
|
+
skipUTF8Validation: false,
|
60
|
+
perMessageDeflate: false,
|
61
|
+
handleProtocols: null,
|
62
|
+
clientTracking: true,
|
63
|
+
verifyClient: null,
|
64
|
+
noServer: false,
|
65
|
+
backlog: null, // use default (511 as implemented in net.js)
|
66
|
+
server: null,
|
67
|
+
host: null,
|
68
|
+
path: null,
|
69
|
+
port: null,
|
70
|
+
...options
|
71
|
+
};
|
72
|
+
|
73
|
+
if (
|
74
|
+
(options.port == null && !options.server && !options.noServer) ||
|
75
|
+
(options.port != null && (options.server || options.noServer)) ||
|
76
|
+
(options.server && options.noServer)
|
77
|
+
) {
|
78
|
+
throw new TypeError(
|
79
|
+
'One and only one of the "port", "server", or "noServer" options ' +
|
80
|
+
'must be specified'
|
81
|
+
);
|
82
|
+
}
|
83
|
+
|
84
|
+
if (options.port != null) {
|
85
|
+
this._server = http.createServer((req, res) => {
|
86
|
+
const body = http.STATUS_CODES[426];
|
87
|
+
|
88
|
+
res.writeHead(426, {
|
89
|
+
'Content-Length': body.length,
|
90
|
+
'Content-Type': 'text/plain'
|
91
|
+
});
|
92
|
+
res.end(body);
|
93
|
+
});
|
94
|
+
this._server.listen(
|
95
|
+
options.port,
|
96
|
+
options.host,
|
97
|
+
options.backlog,
|
98
|
+
callback
|
99
|
+
);
|
100
|
+
} else if (options.server) {
|
101
|
+
this._server = options.server;
|
102
|
+
}
|
103
|
+
|
104
|
+
if (this._server) {
|
105
|
+
const emitConnection = this.emit.bind(this, 'connection');
|
106
|
+
|
107
|
+
this._removeListeners = addListeners(this._server, {
|
108
|
+
listening: this.emit.bind(this, 'listening'),
|
109
|
+
error: this.emit.bind(this, 'error'),
|
110
|
+
upgrade: (req, socket, head) => {
|
111
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
112
|
+
}
|
113
|
+
});
|
114
|
+
}
|
115
|
+
|
116
|
+
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
|
117
|
+
if (options.clientTracking) {
|
118
|
+
this.clients = new Set();
|
119
|
+
this._shouldEmitClose = false;
|
120
|
+
}
|
121
|
+
|
122
|
+
this.options = options;
|
123
|
+
this._state = RUNNING;
|
124
|
+
}
|
125
|
+
|
126
|
+
/**
|
127
|
+
* Returns the bound address, the address family name, and port of the server
|
128
|
+
* as reported by the operating system if listening on an IP socket.
|
129
|
+
* If the server is listening on a pipe or UNIX domain socket, the name is
|
130
|
+
* returned as a string.
|
131
|
+
*
|
132
|
+
* @return {(Object|String|null)} The address of the server
|
133
|
+
* @public
|
134
|
+
*/
|
135
|
+
address() {
|
136
|
+
if (this.options.noServer) {
|
137
|
+
throw new Error('The server is operating in "noServer" mode');
|
138
|
+
}
|
139
|
+
|
140
|
+
if (!this._server) return null;
|
141
|
+
return this._server.address();
|
142
|
+
}
|
143
|
+
|
144
|
+
/**
|
145
|
+
* Stop the server from accepting new connections and emit the `'close'` event
|
146
|
+
* when all existing connections are closed.
|
147
|
+
*
|
148
|
+
* @param {Function} [cb] A one-time listener for the `'close'` event
|
149
|
+
* @public
|
150
|
+
*/
|
151
|
+
close(cb) {
|
152
|
+
if (this._state === CLOSED) {
|
153
|
+
if (cb) {
|
154
|
+
this.once('close', () => {
|
155
|
+
cb(new Error('The server is not running'));
|
156
|
+
});
|
157
|
+
}
|
158
|
+
|
159
|
+
process.nextTick(emitClose, this);
|
160
|
+
return;
|
161
|
+
}
|
162
|
+
|
163
|
+
if (cb) this.once('close', cb);
|
164
|
+
|
165
|
+
if (this._state === CLOSING) return;
|
166
|
+
this._state = CLOSING;
|
167
|
+
|
168
|
+
if (this.options.noServer || this.options.server) {
|
169
|
+
if (this._server) {
|
170
|
+
this._removeListeners();
|
171
|
+
this._removeListeners = this._server = null;
|
172
|
+
}
|
173
|
+
|
174
|
+
if (this.clients) {
|
175
|
+
if (!this.clients.size) {
|
176
|
+
process.nextTick(emitClose, this);
|
177
|
+
} else {
|
178
|
+
this._shouldEmitClose = true;
|
179
|
+
}
|
180
|
+
} else {
|
181
|
+
process.nextTick(emitClose, this);
|
182
|
+
}
|
183
|
+
} else {
|
184
|
+
const server = this._server;
|
185
|
+
|
186
|
+
this._removeListeners();
|
187
|
+
this._removeListeners = this._server = null;
|
188
|
+
|
189
|
+
//
|
190
|
+
// The HTTP/S server was created internally. Close it, and rely on its
|
191
|
+
// `'close'` event.
|
192
|
+
//
|
193
|
+
server.close(() => {
|
194
|
+
emitClose(this);
|
195
|
+
});
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
/**
|
200
|
+
* See if a given request should be handled by this server instance.
|
201
|
+
*
|
202
|
+
* @param {http.IncomingMessage} req Request object to inspect
|
203
|
+
* @return {Boolean} `true` if the request is valid, else `false`
|
204
|
+
* @public
|
205
|
+
*/
|
206
|
+
shouldHandle(req) {
|
207
|
+
if (this.options.path) {
|
208
|
+
const index = req.url.indexOf('?');
|
209
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
210
|
+
|
211
|
+
if (pathname !== this.options.path) return false;
|
212
|
+
}
|
213
|
+
|
214
|
+
return true;
|
215
|
+
}
|
216
|
+
|
217
|
+
/**
|
218
|
+
* Handle a HTTP Upgrade request.
|
219
|
+
*
|
220
|
+
* @param {http.IncomingMessage} req The request object
|
221
|
+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
222
|
+
* server and client
|
223
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
224
|
+
* @param {Function} cb Callback
|
225
|
+
* @public
|
226
|
+
*/
|
227
|
+
handleUpgrade(req, socket, head, cb) {
|
228
|
+
socket.on('error', socketOnError);
|
229
|
+
|
230
|
+
const key =
|
231
|
+
req.headers['sec-websocket-key'] !== undefined
|
232
|
+
? req.headers['sec-websocket-key']
|
233
|
+
: false;
|
234
|
+
const version = +req.headers['sec-websocket-version'];
|
235
|
+
|
236
|
+
if (
|
237
|
+
req.method !== 'GET' ||
|
238
|
+
req.headers.upgrade.toLowerCase() !== 'websocket' ||
|
239
|
+
!key ||
|
240
|
+
!keyRegex.test(key) ||
|
241
|
+
(version !== 8 && version !== 13) ||
|
242
|
+
!this.shouldHandle(req)
|
243
|
+
) {
|
244
|
+
return abortHandshake(socket, 400);
|
245
|
+
}
|
246
|
+
|
247
|
+
const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
|
248
|
+
let protocols = new Set();
|
249
|
+
|
250
|
+
if (secWebSocketProtocol !== undefined) {
|
251
|
+
try {
|
252
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
253
|
+
} catch (err) {
|
254
|
+
return abortHandshake(socket, 400);
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
|
259
|
+
const extensions = {};
|
260
|
+
|
261
|
+
if (
|
262
|
+
this.options.perMessageDeflate &&
|
263
|
+
secWebSocketExtensions !== undefined
|
264
|
+
) {
|
265
|
+
const perMessageDeflate = new PerMessageDeflate(
|
266
|
+
this.options.perMessageDeflate,
|
267
|
+
true,
|
268
|
+
this.options.maxPayload
|
269
|
+
);
|
270
|
+
|
271
|
+
try {
|
272
|
+
const offers = extension.parse(secWebSocketExtensions);
|
273
|
+
|
274
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
275
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
276
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
277
|
+
}
|
278
|
+
} catch (err) {
|
279
|
+
return abortHandshake(socket, 400);
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
//
|
284
|
+
// Optionally call external client verification handler.
|
285
|
+
//
|
286
|
+
if (this.options.verifyClient) {
|
287
|
+
const info = {
|
288
|
+
origin:
|
289
|
+
req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
|
290
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
291
|
+
req
|
292
|
+
};
|
293
|
+
|
294
|
+
if (this.options.verifyClient.length === 2) {
|
295
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
296
|
+
if (!verified) {
|
297
|
+
return abortHandshake(socket, code || 401, message, headers);
|
298
|
+
}
|
299
|
+
|
300
|
+
this.completeUpgrade(
|
301
|
+
extensions,
|
302
|
+
key,
|
303
|
+
protocols,
|
304
|
+
req,
|
305
|
+
socket,
|
306
|
+
head,
|
307
|
+
cb
|
308
|
+
);
|
309
|
+
});
|
310
|
+
return;
|
311
|
+
}
|
312
|
+
|
313
|
+
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
314
|
+
}
|
315
|
+
|
316
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
317
|
+
}
|
318
|
+
|
319
|
+
/**
|
320
|
+
* Upgrade the connection to WebSocket.
|
321
|
+
*
|
322
|
+
* @param {Object} extensions The accepted extensions
|
323
|
+
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
324
|
+
* @param {Set} protocols The subprotocols
|
325
|
+
* @param {http.IncomingMessage} req The request object
|
326
|
+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
327
|
+
* server and client
|
328
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
329
|
+
* @param {Function} cb Callback
|
330
|
+
* @throws {Error} If called more than once with the same socket
|
331
|
+
* @private
|
332
|
+
*/
|
333
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
334
|
+
//
|
335
|
+
// Destroy the socket if the client has already sent a FIN packet.
|
336
|
+
//
|
337
|
+
if (!socket.readable || !socket.writable) return socket.destroy();
|
338
|
+
|
339
|
+
if (socket[kWebSocket]) {
|
340
|
+
throw new Error(
|
341
|
+
'server.handleUpgrade() was called more than once with the same ' +
|
342
|
+
'socket, possibly due to a misconfiguration'
|
343
|
+
);
|
344
|
+
}
|
345
|
+
|
346
|
+
if (this._state > RUNNING) return abortHandshake(socket, 503);
|
347
|
+
|
348
|
+
const digest = createHash('sha1')
|
349
|
+
.update(key + GUID)
|
350
|
+
.digest('base64');
|
351
|
+
|
352
|
+
const headers = [
|
353
|
+
'HTTP/1.1 101 Switching Protocols',
|
354
|
+
'Upgrade: websocket',
|
355
|
+
'Connection: Upgrade',
|
356
|
+
`Sec-WebSocket-Accept: ${digest}`
|
357
|
+
];
|
358
|
+
|
359
|
+
const ws = new WebSocket(null);
|
360
|
+
|
361
|
+
if (protocols.size) {
|
362
|
+
//
|
363
|
+
// Optionally call external protocol selection handler.
|
364
|
+
//
|
365
|
+
const protocol = this.options.handleProtocols
|
366
|
+
? this.options.handleProtocols(protocols, req)
|
367
|
+
: protocols.values().next().value;
|
368
|
+
|
369
|
+
if (protocol) {
|
370
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
371
|
+
ws._protocol = protocol;
|
372
|
+
}
|
373
|
+
}
|
374
|
+
|
375
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
376
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
377
|
+
const value = extension.format({
|
378
|
+
[PerMessageDeflate.extensionName]: [params]
|
379
|
+
});
|
380
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
381
|
+
ws._extensions = extensions;
|
382
|
+
}
|
383
|
+
|
384
|
+
//
|
385
|
+
// Allow external modification/inspection of handshake headers.
|
386
|
+
//
|
387
|
+
this.emit('headers', headers, req);
|
388
|
+
|
389
|
+
socket.write(headers.concat('\r\n').join('\r\n'));
|
390
|
+
socket.removeListener('error', socketOnError);
|
391
|
+
|
392
|
+
ws.setSocket(socket, head, {
|
393
|
+
maxPayload: this.options.maxPayload,
|
394
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
395
|
+
});
|
396
|
+
|
397
|
+
if (this.clients) {
|
398
|
+
this.clients.add(ws);
|
399
|
+
ws.on('close', () => {
|
400
|
+
this.clients.delete(ws);
|
401
|
+
|
402
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
403
|
+
process.nextTick(emitClose, this);
|
404
|
+
}
|
405
|
+
});
|
406
|
+
}
|
407
|
+
|
408
|
+
cb(ws, req);
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
module.exports = WebSocketServer;
|
413
|
+
|
414
|
+
/**
|
415
|
+
* Add event listeners on an `EventEmitter` using a map of <event, listener>
|
416
|
+
* pairs.
|
417
|
+
*
|
418
|
+
* @param {EventEmitter} server The event emitter
|
419
|
+
* @param {Object.<String, Function>} map The listeners to add
|
420
|
+
* @return {Function} A function that will remove the added listeners when
|
421
|
+
* called
|
422
|
+
* @private
|
423
|
+
*/
|
424
|
+
function addListeners(server, map) {
|
425
|
+
for (const event of Object.keys(map)) server.on(event, map[event]);
|
426
|
+
|
427
|
+
return function removeListeners() {
|
428
|
+
for (const event of Object.keys(map)) {
|
429
|
+
server.removeListener(event, map[event]);
|
430
|
+
}
|
431
|
+
};
|
432
|
+
}
|
433
|
+
|
434
|
+
/**
|
435
|
+
* Emit a `'close'` event on an `EventEmitter`.
|
436
|
+
*
|
437
|
+
* @param {EventEmitter} server The event emitter
|
438
|
+
* @private
|
439
|
+
*/
|
440
|
+
function emitClose(server) {
|
441
|
+
server._state = CLOSED;
|
442
|
+
server.emit('close');
|
443
|
+
}
|
444
|
+
|
445
|
+
/**
|
446
|
+
* Handle premature socket errors.
|
447
|
+
*
|
448
|
+
* @private
|
449
|
+
*/
|
450
|
+
function socketOnError() {
|
451
|
+
this.destroy();
|
452
|
+
}
|
453
|
+
|
454
|
+
/**
|
455
|
+
* Close the connection when preconditions are not fulfilled.
|
456
|
+
*
|
457
|
+
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
|
458
|
+
* @param {Number} code The HTTP response status code
|
459
|
+
* @param {String} [message] The HTTP response body
|
460
|
+
* @param {Object} [headers] Additional HTTP response headers
|
461
|
+
* @private
|
462
|
+
*/
|
463
|
+
function abortHandshake(socket, code, message, headers) {
|
464
|
+
if (socket.writable) {
|
465
|
+
message = message || http.STATUS_CODES[code];
|
466
|
+
headers = {
|
467
|
+
Connection: 'close',
|
468
|
+
'Content-Type': 'text/html',
|
469
|
+
'Content-Length': Buffer.byteLength(message),
|
470
|
+
...headers
|
471
|
+
};
|
472
|
+
|
473
|
+
socket.write(
|
474
|
+
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
|
475
|
+
Object.keys(headers)
|
476
|
+
.map((h) => `${h}: ${headers[h]}`)
|
477
|
+
.join('\r\n') +
|
478
|
+
'\r\n\r\n' +
|
479
|
+
message
|
480
|
+
);
|
481
|
+
}
|
482
|
+
|
483
|
+
socket.removeListener('error', socketOnError);
|
484
|
+
socket.destroy();
|
485
|
+
}
|