opal 1.6.0 → 1.7.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/build.yml +17 -0
- data/CHANGELOG.md +15 -1
- data/Gemfile +2 -0
- data/HACKING.md +47 -26
- data/UNRELEASED.md +27 -0
- data/benchmark/benchmarks +415 -103
- data/benchmark/bm_call_overhead.yml +28 -0
- data/benchmark/run.rb +61 -40
- data/docs/cdp_common.json +3364 -0
- data/docs/cdp_common.md +18 -0
- data/docs/{headless_chrome.md → headless_browsers.md} +31 -12
- data/lib/opal/ast/builder.rb +1 -1
- data/lib/opal/builder.rb +8 -2
- data/lib/opal/builder_processors.rb +5 -3
- data/lib/opal/builder_scheduler.rb +1 -1
- data/lib/opal/cache.rb +1 -7
- data/lib/opal/cli_options.rb +72 -58
- data/lib/opal/cli_runners/chrome.rb +47 -9
- data/lib/opal/cli_runners/chrome_cdp_interface.rb +238 -112
- data/lib/opal/cli_runners/compiler.rb +146 -13
- data/lib/opal/cli_runners/deno.rb +32 -0
- data/lib/opal/cli_runners/firefox.rb +350 -0
- data/lib/opal/cli_runners/firefox_cdp_interface.rb +212 -0
- data/lib/opal/cli_runners/node_modules/.bin/chrome-remote-interface.cmd +17 -0
- data/lib/opal/cli_runners/node_modules/.bin/chrome-remote-interface.ps1 +28 -0
- data/lib/opal/cli_runners/node_modules/.package-lock.json +41 -0
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/LICENSE +1 -1
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/README.md +322 -182
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/bin/client.js +99 -114
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/chrome-remote-interface.js +1 -11
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/index.js +16 -11
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/lib/api.js +41 -33
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/lib/chrome.js +224 -214
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/lib/devtools.js +71 -191
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/lib/external-request.js +26 -6
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/lib/protocol.json +20788 -9049
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/lib/websocket-wrapper.js +10 -3
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/package.json +59 -123
- data/lib/opal/cli_runners/node_modules/chrome-remote-interface/webpack.config.js +25 -32
- data/lib/opal/cli_runners/node_modules/commander/History.md +298 -0
- data/lib/opal/cli_runners/node_modules/commander/LICENSE +22 -0
- data/lib/opal/cli_runners/node_modules/commander/Readme.md +217 -61
- data/lib/opal/cli_runners/node_modules/commander/index.js +431 -145
- data/lib/opal/cli_runners/node_modules/commander/package.json +16 -79
- data/lib/opal/cli_runners/node_modules/ws/README.md +334 -98
- data/lib/opal/cli_runners/node_modules/ws/browser.js +8 -0
- data/lib/opal/cli_runners/node_modules/ws/index.js +5 -10
- data/lib/opal/cli_runners/node_modules/ws/lib/buffer-util.js +129 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/constants.js +10 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/event-target.js +184 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/extension.js +223 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/limiter.js +55 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/permessage-deflate.js +518 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/receiver.js +607 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/sender.js +409 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/stream.js +180 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/validation.js +104 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/websocket-server.js +447 -0
- data/lib/opal/cli_runners/node_modules/ws/lib/websocket.js +1195 -0
- data/lib/opal/cli_runners/node_modules/ws/package.json +40 -106
- data/lib/opal/cli_runners/package-lock.json +62 -0
- data/lib/opal/cli_runners/package.json +1 -1
- data/lib/opal/cli_runners.rb +26 -4
- data/lib/opal/nodes/args/prepare_post_args.rb +2 -2
- data/lib/opal/nodes/def.rb +8 -8
- data/lib/opal/nodes/iter.rb +12 -12
- data/lib/opal/nodes/logic.rb +1 -1
- data/lib/opal/nodes/masgn.rb +2 -2
- data/lib/opal/parser/with_ruby_lexer.rb +1 -1
- data/lib/opal/paths.rb +14 -0
- data/lib/opal/rewriter.rb +2 -0
- data/lib/opal/rewriters/forward_args.rb +52 -4
- data/lib/opal/rewriters/targeted_patches.rb +94 -0
- data/lib/opal/version.rb +1 -1
- data/opal/corelib/basic_object.rb +1 -1
- data/opal/corelib/boolean.rb +2 -2
- data/opal/corelib/class.rb +11 -0
- data/opal/corelib/constants.rb +3 -3
- data/opal/corelib/enumerable.rb +4 -0
- data/opal/corelib/enumerator.rb +1 -1
- data/opal/corelib/hash.rb +2 -2
- data/opal/corelib/helpers.rb +1 -1
- data/opal/corelib/kernel.rb +3 -3
- data/opal/corelib/method.rb +1 -1
- data/opal/corelib/module.rb +29 -8
- data/opal/corelib/proc.rb +7 -5
- data/opal/corelib/runtime.js +141 -78
- data/opal/corelib/set.rb +252 -0
- data/opal/corelib/string.rb +2 -1
- data/opal/corelib/time.rb +2 -2
- data/opal/opal.rb +1 -0
- data/opal.gemspec +1 -0
- data/spec/filters/bugs/array.rb +22 -13
- data/spec/filters/bugs/base64.rb +5 -5
- data/spec/filters/bugs/basicobject.rb +16 -8
- data/spec/filters/bugs/bigdecimal.rb +161 -160
- data/spec/filters/bugs/binding.rb +10 -10
- data/spec/filters/bugs/class.rb +8 -8
- data/spec/filters/bugs/complex.rb +2 -1
- data/spec/filters/bugs/date.rb +79 -81
- data/spec/filters/bugs/datetime.rb +29 -29
- data/spec/filters/bugs/delegate.rb +1 -3
- data/spec/filters/bugs/encoding.rb +69 -69
- data/spec/filters/bugs/enumerable.rb +22 -20
- data/spec/filters/bugs/enumerator.rb +88 -85
- data/spec/filters/bugs/exception.rb +46 -40
- data/spec/filters/bugs/file.rb +32 -32
- data/spec/filters/bugs/float.rb +26 -21
- data/spec/filters/bugs/freeze.rb +88 -0
- data/spec/filters/bugs/hash.rb +39 -38
- data/spec/filters/bugs/integer.rb +57 -44
- data/spec/filters/bugs/io.rb +1 -1
- data/spec/filters/bugs/kernel.rb +349 -269
- data/spec/filters/bugs/language.rb +220 -188
- data/spec/filters/bugs/main.rb +5 -3
- data/spec/filters/bugs/marshal.rb +38 -38
- data/spec/filters/bugs/math.rb +2 -1
- data/spec/filters/bugs/method.rb +73 -62
- data/spec/filters/bugs/module.rb +163 -143
- data/spec/filters/bugs/numeric.rb +6 -6
- data/spec/filters/bugs/objectspace.rb +16 -16
- data/spec/filters/bugs/openstruct.rb +1 -1
- data/spec/filters/bugs/pack_unpack.rb +51 -51
- data/spec/filters/bugs/pathname.rb +7 -7
- data/spec/filters/bugs/proc.rb +63 -63
- data/spec/filters/bugs/random.rb +7 -6
- data/spec/filters/bugs/range.rb +12 -9
- data/spec/filters/bugs/rational.rb +8 -7
- data/spec/filters/bugs/regexp.rb +49 -48
- data/spec/filters/bugs/ruby-32.rb +56 -0
- data/spec/filters/bugs/set.rb +30 -30
- data/spec/filters/bugs/singleton.rb +4 -4
- data/spec/filters/bugs/string.rb +187 -99
- data/spec/filters/bugs/stringio.rb +7 -0
- data/spec/filters/bugs/stringscanner.rb +68 -68
- data/spec/filters/bugs/struct.rb +11 -9
- data/spec/filters/bugs/symbol.rb +1 -1
- data/spec/filters/bugs/time.rb +78 -63
- data/spec/filters/bugs/trace_point.rb +4 -4
- data/spec/filters/bugs/unboundmethod.rb +32 -17
- data/spec/filters/bugs/warnings.rb +8 -12
- data/spec/filters/unsupported/array.rb +24 -107
- data/spec/filters/unsupported/basicobject.rb +12 -12
- data/spec/filters/unsupported/bignum.rb +27 -52
- data/spec/filters/unsupported/class.rb +1 -2
- data/spec/filters/unsupported/delegator.rb +3 -3
- data/spec/filters/unsupported/enumerable.rb +2 -9
- data/spec/filters/unsupported/enumerator.rb +2 -11
- data/spec/filters/unsupported/file.rb +1 -1
- data/spec/filters/unsupported/float.rb +28 -47
- data/spec/filters/unsupported/hash.rb +8 -14
- data/spec/filters/unsupported/integer.rb +75 -91
- data/spec/filters/unsupported/kernel.rb +17 -35
- data/spec/filters/unsupported/language.rb +11 -19
- data/spec/filters/unsupported/marshal.rb +22 -41
- data/spec/filters/unsupported/matchdata.rb +28 -52
- data/spec/filters/unsupported/math.rb +1 -1
- data/spec/filters/unsupported/privacy.rb +229 -285
- data/spec/filters/unsupported/range.rb +1 -5
- data/spec/filters/unsupported/regexp.rb +40 -66
- data/spec/filters/unsupported/set.rb +2 -2
- data/spec/filters/unsupported/singleton.rb +4 -4
- data/spec/filters/unsupported/string.rb +305 -508
- data/spec/filters/unsupported/struct.rb +3 -4
- data/spec/filters/unsupported/symbol.rb +15 -18
- data/spec/filters/unsupported/thread.rb +1 -7
- data/spec/filters/unsupported/time.rb +159 -202
- data/spec/filters/unsupported/usage_of_files.rb +170 -259
- data/spec/lib/builder_spec.rb +14 -4
- data/spec/lib/rewriters/forward_args_spec.rb +32 -12
- data/spec/mspec-opal/runner.rb +2 -0
- data/spec/ruby_specs +4 -0
- data/stdlib/deno/base.rb +28 -0
- data/stdlib/deno/file.rb +340 -0
- data/stdlib/{headless_chrome.rb → headless_browser/base.rb} +1 -1
- data/stdlib/headless_browser/file.rb +15 -0
- data/stdlib/headless_browser.rb +4 -0
- data/stdlib/native.rb +1 -1
- data/stdlib/nodejs/file.rb +5 -0
- data/stdlib/opal/platform.rb +8 -6
- data/stdlib/opal-platform.rb +14 -8
- data/stdlib/set.rb +1 -258
- data/tasks/benchmarking.rake +62 -19
- data/tasks/building.rake +6 -2
- data/tasks/performance.rake +1 -1
- data/tasks/testing.rake +5 -3
- data/test/nodejs/test_file.rb +29 -10
- data/test/opal/http_server.rb +28 -11
- data/test/opal/unsupported_and_bugs.rb +2 -1
- metadata +92 -53
- data/lib/opal/cli_runners/node_modules/ultron/LICENSE +0 -22
- data/lib/opal/cli_runners/node_modules/ultron/index.js +0 -138
- data/lib/opal/cli_runners/node_modules/ultron/package.json +0 -112
- data/lib/opal/cli_runners/node_modules/ws/SECURITY.md +0 -33
- data/lib/opal/cli_runners/node_modules/ws/lib/BufferUtil.fallback.js +0 -56
- data/lib/opal/cli_runners/node_modules/ws/lib/BufferUtil.js +0 -15
- data/lib/opal/cli_runners/node_modules/ws/lib/ErrorCodes.js +0 -28
- data/lib/opal/cli_runners/node_modules/ws/lib/EventTarget.js +0 -158
- data/lib/opal/cli_runners/node_modules/ws/lib/Extensions.js +0 -69
- data/lib/opal/cli_runners/node_modules/ws/lib/PerMessageDeflate.js +0 -339
- data/lib/opal/cli_runners/node_modules/ws/lib/Receiver.js +0 -520
- data/lib/opal/cli_runners/node_modules/ws/lib/Sender.js +0 -438
- data/lib/opal/cli_runners/node_modules/ws/lib/Validation.fallback.js +0 -9
- data/lib/opal/cli_runners/node_modules/ws/lib/Validation.js +0 -17
- data/lib/opal/cli_runners/node_modules/ws/lib/WebSocket.js +0 -705
- data/lib/opal/cli_runners/node_modules/ws/lib/WebSocketServer.js +0 -336
- data/spec/filters/bugs/boolean.rb +0 -3
- data/spec/filters/bugs/matrix.rb +0 -3
- data/spec/filters/unsupported/fixnum.rb +0 -15
- data/spec/filters/unsupported/freeze.rb +0 -102
- data/spec/filters/unsupported/pathname.rb +0 -4
- data/spec/filters/unsupported/proc.rb +0 -4
- data/spec/filters/unsupported/random.rb +0 -5
- data/spec/filters/unsupported/taint.rb +0 -162
@@ -0,0 +1,447 @@
|
|
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 PerMessageDeflate = require('./permessage-deflate');
|
13
|
+
const WebSocket = require('./websocket');
|
14
|
+
const { format, parse } = require('./extension');
|
15
|
+
const { GUID, kWebSocket } = require('./constants');
|
16
|
+
|
17
|
+
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
18
|
+
|
19
|
+
const RUNNING = 0;
|
20
|
+
const CLOSING = 1;
|
21
|
+
const CLOSED = 2;
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Class representing a WebSocket server.
|
25
|
+
*
|
26
|
+
* @extends EventEmitter
|
27
|
+
*/
|
28
|
+
class WebSocketServer extends EventEmitter {
|
29
|
+
/**
|
30
|
+
* Create a `WebSocketServer` instance.
|
31
|
+
*
|
32
|
+
* @param {Object} options Configuration options
|
33
|
+
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
34
|
+
* pending connections
|
35
|
+
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
36
|
+
* track clients
|
37
|
+
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
38
|
+
* @param {String} [options.host] The hostname where to bind the server
|
39
|
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
40
|
+
* size
|
41
|
+
* @param {Boolean} [options.noServer=false] Enable no server mode
|
42
|
+
* @param {String} [options.path] Accept only connections matching this path
|
43
|
+
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
44
|
+
* permessage-deflate
|
45
|
+
* @param {Number} [options.port] The port where to bind the server
|
46
|
+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
47
|
+
* server to use
|
48
|
+
* @param {Function} [options.verifyClient] A hook to reject connections
|
49
|
+
* @param {Function} [callback] A listener for the `listening` event
|
50
|
+
*/
|
51
|
+
constructor(options, callback) {
|
52
|
+
super();
|
53
|
+
|
54
|
+
options = {
|
55
|
+
maxPayload: 100 * 1024 * 1024,
|
56
|
+
perMessageDeflate: false,
|
57
|
+
handleProtocols: null,
|
58
|
+
clientTracking: true,
|
59
|
+
verifyClient: null,
|
60
|
+
noServer: false,
|
61
|
+
backlog: null, // use default (511 as implemented in net.js)
|
62
|
+
server: null,
|
63
|
+
host: null,
|
64
|
+
path: null,
|
65
|
+
port: null,
|
66
|
+
...options
|
67
|
+
};
|
68
|
+
|
69
|
+
if (
|
70
|
+
(options.port == null && !options.server && !options.noServer) ||
|
71
|
+
(options.port != null && (options.server || options.noServer)) ||
|
72
|
+
(options.server && options.noServer)
|
73
|
+
) {
|
74
|
+
throw new TypeError(
|
75
|
+
'One and only one of the "port", "server", or "noServer" options ' +
|
76
|
+
'must be specified'
|
77
|
+
);
|
78
|
+
}
|
79
|
+
|
80
|
+
if (options.port != null) {
|
81
|
+
this._server = http.createServer((req, res) => {
|
82
|
+
const body = http.STATUS_CODES[426];
|
83
|
+
|
84
|
+
res.writeHead(426, {
|
85
|
+
'Content-Length': body.length,
|
86
|
+
'Content-Type': 'text/plain'
|
87
|
+
});
|
88
|
+
res.end(body);
|
89
|
+
});
|
90
|
+
this._server.listen(
|
91
|
+
options.port,
|
92
|
+
options.host,
|
93
|
+
options.backlog,
|
94
|
+
callback
|
95
|
+
);
|
96
|
+
} else if (options.server) {
|
97
|
+
this._server = options.server;
|
98
|
+
}
|
99
|
+
|
100
|
+
if (this._server) {
|
101
|
+
const emitConnection = this.emit.bind(this, 'connection');
|
102
|
+
|
103
|
+
this._removeListeners = addListeners(this._server, {
|
104
|
+
listening: this.emit.bind(this, 'listening'),
|
105
|
+
error: this.emit.bind(this, 'error'),
|
106
|
+
upgrade: (req, socket, head) => {
|
107
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
108
|
+
}
|
109
|
+
});
|
110
|
+
}
|
111
|
+
|
112
|
+
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
|
113
|
+
if (options.clientTracking) this.clients = new Set();
|
114
|
+
this.options = options;
|
115
|
+
this._state = RUNNING;
|
116
|
+
}
|
117
|
+
|
118
|
+
/**
|
119
|
+
* Returns the bound address, the address family name, and port of the server
|
120
|
+
* as reported by the operating system if listening on an IP socket.
|
121
|
+
* If the server is listening on a pipe or UNIX domain socket, the name is
|
122
|
+
* returned as a string.
|
123
|
+
*
|
124
|
+
* @return {(Object|String|null)} The address of the server
|
125
|
+
* @public
|
126
|
+
*/
|
127
|
+
address() {
|
128
|
+
if (this.options.noServer) {
|
129
|
+
throw new Error('The server is operating in "noServer" mode');
|
130
|
+
}
|
131
|
+
|
132
|
+
if (!this._server) return null;
|
133
|
+
return this._server.address();
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
* Close the server.
|
138
|
+
*
|
139
|
+
* @param {Function} [cb] Callback
|
140
|
+
* @public
|
141
|
+
*/
|
142
|
+
close(cb) {
|
143
|
+
if (cb) this.once('close', cb);
|
144
|
+
|
145
|
+
if (this._state === CLOSED) {
|
146
|
+
process.nextTick(emitClose, this);
|
147
|
+
return;
|
148
|
+
}
|
149
|
+
|
150
|
+
if (this._state === CLOSING) return;
|
151
|
+
this._state = CLOSING;
|
152
|
+
|
153
|
+
//
|
154
|
+
// Terminate all associated clients.
|
155
|
+
//
|
156
|
+
if (this.clients) {
|
157
|
+
for (const client of this.clients) client.terminate();
|
158
|
+
}
|
159
|
+
|
160
|
+
const server = this._server;
|
161
|
+
|
162
|
+
if (server) {
|
163
|
+
this._removeListeners();
|
164
|
+
this._removeListeners = this._server = null;
|
165
|
+
|
166
|
+
//
|
167
|
+
// Close the http server if it was internally created.
|
168
|
+
//
|
169
|
+
if (this.options.port != null) {
|
170
|
+
server.close(emitClose.bind(undefined, this));
|
171
|
+
return;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
process.nextTick(emitClose, this);
|
176
|
+
}
|
177
|
+
|
178
|
+
/**
|
179
|
+
* See if a given request should be handled by this server instance.
|
180
|
+
*
|
181
|
+
* @param {http.IncomingMessage} req Request object to inspect
|
182
|
+
* @return {Boolean} `true` if the request is valid, else `false`
|
183
|
+
* @public
|
184
|
+
*/
|
185
|
+
shouldHandle(req) {
|
186
|
+
if (this.options.path) {
|
187
|
+
const index = req.url.indexOf('?');
|
188
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
189
|
+
|
190
|
+
if (pathname !== this.options.path) return false;
|
191
|
+
}
|
192
|
+
|
193
|
+
return true;
|
194
|
+
}
|
195
|
+
|
196
|
+
/**
|
197
|
+
* Handle a HTTP Upgrade request.
|
198
|
+
*
|
199
|
+
* @param {http.IncomingMessage} req The request object
|
200
|
+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
201
|
+
* server and client
|
202
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
203
|
+
* @param {Function} cb Callback
|
204
|
+
* @public
|
205
|
+
*/
|
206
|
+
handleUpgrade(req, socket, head, cb) {
|
207
|
+
socket.on('error', socketOnError);
|
208
|
+
|
209
|
+
const key =
|
210
|
+
req.headers['sec-websocket-key'] !== undefined
|
211
|
+
? req.headers['sec-websocket-key'].trim()
|
212
|
+
: false;
|
213
|
+
const version = +req.headers['sec-websocket-version'];
|
214
|
+
const extensions = {};
|
215
|
+
|
216
|
+
if (
|
217
|
+
req.method !== 'GET' ||
|
218
|
+
req.headers.upgrade.toLowerCase() !== 'websocket' ||
|
219
|
+
!key ||
|
220
|
+
!keyRegex.test(key) ||
|
221
|
+
(version !== 8 && version !== 13) ||
|
222
|
+
!this.shouldHandle(req)
|
223
|
+
) {
|
224
|
+
return abortHandshake(socket, 400);
|
225
|
+
}
|
226
|
+
|
227
|
+
if (this.options.perMessageDeflate) {
|
228
|
+
const perMessageDeflate = new PerMessageDeflate(
|
229
|
+
this.options.perMessageDeflate,
|
230
|
+
true,
|
231
|
+
this.options.maxPayload
|
232
|
+
);
|
233
|
+
|
234
|
+
try {
|
235
|
+
const offers = parse(req.headers['sec-websocket-extensions']);
|
236
|
+
|
237
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
238
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
239
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
240
|
+
}
|
241
|
+
} catch (err) {
|
242
|
+
return abortHandshake(socket, 400);
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
246
|
+
//
|
247
|
+
// Optionally call external client verification handler.
|
248
|
+
//
|
249
|
+
if (this.options.verifyClient) {
|
250
|
+
const info = {
|
251
|
+
origin:
|
252
|
+
req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
|
253
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
254
|
+
req
|
255
|
+
};
|
256
|
+
|
257
|
+
if (this.options.verifyClient.length === 2) {
|
258
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
259
|
+
if (!verified) {
|
260
|
+
return abortHandshake(socket, code || 401, message, headers);
|
261
|
+
}
|
262
|
+
|
263
|
+
this.completeUpgrade(key, extensions, req, socket, head, cb);
|
264
|
+
});
|
265
|
+
return;
|
266
|
+
}
|
267
|
+
|
268
|
+
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
269
|
+
}
|
270
|
+
|
271
|
+
this.completeUpgrade(key, extensions, req, socket, head, cb);
|
272
|
+
}
|
273
|
+
|
274
|
+
/**
|
275
|
+
* Upgrade the connection to WebSocket.
|
276
|
+
*
|
277
|
+
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
278
|
+
* @param {Object} extensions The accepted extensions
|
279
|
+
* @param {http.IncomingMessage} req The request object
|
280
|
+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
281
|
+
* server and client
|
282
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
283
|
+
* @param {Function} cb Callback
|
284
|
+
* @throws {Error} If called more than once with the same socket
|
285
|
+
* @private
|
286
|
+
*/
|
287
|
+
completeUpgrade(key, extensions, req, socket, head, cb) {
|
288
|
+
//
|
289
|
+
// Destroy the socket if the client has already sent a FIN packet.
|
290
|
+
//
|
291
|
+
if (!socket.readable || !socket.writable) return socket.destroy();
|
292
|
+
|
293
|
+
if (socket[kWebSocket]) {
|
294
|
+
throw new Error(
|
295
|
+
'server.handleUpgrade() was called more than once with the same ' +
|
296
|
+
'socket, possibly due to a misconfiguration'
|
297
|
+
);
|
298
|
+
}
|
299
|
+
|
300
|
+
if (this._state > RUNNING) return abortHandshake(socket, 503);
|
301
|
+
|
302
|
+
const digest = createHash('sha1')
|
303
|
+
.update(key + GUID)
|
304
|
+
.digest('base64');
|
305
|
+
|
306
|
+
const headers = [
|
307
|
+
'HTTP/1.1 101 Switching Protocols',
|
308
|
+
'Upgrade: websocket',
|
309
|
+
'Connection: Upgrade',
|
310
|
+
`Sec-WebSocket-Accept: ${digest}`
|
311
|
+
];
|
312
|
+
|
313
|
+
const ws = new WebSocket(null);
|
314
|
+
let protocol = req.headers['sec-websocket-protocol'];
|
315
|
+
|
316
|
+
if (protocol) {
|
317
|
+
protocol = protocol.split(',').map(trim);
|
318
|
+
|
319
|
+
//
|
320
|
+
// Optionally call external protocol selection handler.
|
321
|
+
//
|
322
|
+
if (this.options.handleProtocols) {
|
323
|
+
protocol = this.options.handleProtocols(protocol, req);
|
324
|
+
} else {
|
325
|
+
protocol = protocol[0];
|
326
|
+
}
|
327
|
+
|
328
|
+
if (protocol) {
|
329
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
330
|
+
ws._protocol = protocol;
|
331
|
+
}
|
332
|
+
}
|
333
|
+
|
334
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
335
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
336
|
+
const value = format({
|
337
|
+
[PerMessageDeflate.extensionName]: [params]
|
338
|
+
});
|
339
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
340
|
+
ws._extensions = extensions;
|
341
|
+
}
|
342
|
+
|
343
|
+
//
|
344
|
+
// Allow external modification/inspection of handshake headers.
|
345
|
+
//
|
346
|
+
this.emit('headers', headers, req);
|
347
|
+
|
348
|
+
socket.write(headers.concat('\r\n').join('\r\n'));
|
349
|
+
socket.removeListener('error', socketOnError);
|
350
|
+
|
351
|
+
ws.setSocket(socket, head, this.options.maxPayload);
|
352
|
+
|
353
|
+
if (this.clients) {
|
354
|
+
this.clients.add(ws);
|
355
|
+
ws.on('close', () => this.clients.delete(ws));
|
356
|
+
}
|
357
|
+
|
358
|
+
cb(ws, req);
|
359
|
+
}
|
360
|
+
}
|
361
|
+
|
362
|
+
module.exports = WebSocketServer;
|
363
|
+
|
364
|
+
/**
|
365
|
+
* Add event listeners on an `EventEmitter` using a map of <event, listener>
|
366
|
+
* pairs.
|
367
|
+
*
|
368
|
+
* @param {EventEmitter} server The event emitter
|
369
|
+
* @param {Object.<String, Function>} map The listeners to add
|
370
|
+
* @return {Function} A function that will remove the added listeners when
|
371
|
+
* called
|
372
|
+
* @private
|
373
|
+
*/
|
374
|
+
function addListeners(server, map) {
|
375
|
+
for (const event of Object.keys(map)) server.on(event, map[event]);
|
376
|
+
|
377
|
+
return function removeListeners() {
|
378
|
+
for (const event of Object.keys(map)) {
|
379
|
+
server.removeListener(event, map[event]);
|
380
|
+
}
|
381
|
+
};
|
382
|
+
}
|
383
|
+
|
384
|
+
/**
|
385
|
+
* Emit a `'close'` event on an `EventEmitter`.
|
386
|
+
*
|
387
|
+
* @param {EventEmitter} server The event emitter
|
388
|
+
* @private
|
389
|
+
*/
|
390
|
+
function emitClose(server) {
|
391
|
+
server._state = CLOSED;
|
392
|
+
server.emit('close');
|
393
|
+
}
|
394
|
+
|
395
|
+
/**
|
396
|
+
* Handle premature socket errors.
|
397
|
+
*
|
398
|
+
* @private
|
399
|
+
*/
|
400
|
+
function socketOnError() {
|
401
|
+
this.destroy();
|
402
|
+
}
|
403
|
+
|
404
|
+
/**
|
405
|
+
* Close the connection when preconditions are not fulfilled.
|
406
|
+
*
|
407
|
+
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
|
408
|
+
* @param {Number} code The HTTP response status code
|
409
|
+
* @param {String} [message] The HTTP response body
|
410
|
+
* @param {Object} [headers] Additional HTTP response headers
|
411
|
+
* @private
|
412
|
+
*/
|
413
|
+
function abortHandshake(socket, code, message, headers) {
|
414
|
+
if (socket.writable) {
|
415
|
+
message = message || http.STATUS_CODES[code];
|
416
|
+
headers = {
|
417
|
+
Connection: 'close',
|
418
|
+
'Content-Type': 'text/html',
|
419
|
+
'Content-Length': Buffer.byteLength(message),
|
420
|
+
...headers
|
421
|
+
};
|
422
|
+
|
423
|
+
socket.write(
|
424
|
+
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
|
425
|
+
Object.keys(headers)
|
426
|
+
.map((h) => `${h}: ${headers[h]}`)
|
427
|
+
.join('\r\n') +
|
428
|
+
'\r\n\r\n' +
|
429
|
+
message
|
430
|
+
);
|
431
|
+
}
|
432
|
+
|
433
|
+
socket.removeListener('error', socketOnError);
|
434
|
+
socket.destroy();
|
435
|
+
}
|
436
|
+
|
437
|
+
/**
|
438
|
+
* Remove whitespace characters from both ends of a string.
|
439
|
+
*
|
440
|
+
* @param {String} str The string
|
441
|
+
* @return {String} A new string representing `str` stripped of whitespace
|
442
|
+
* characters from both its beginning and end
|
443
|
+
* @private
|
444
|
+
*/
|
445
|
+
function trim(str) {
|
446
|
+
return str.trim();
|
447
|
+
}
|