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,409 @@
|
|
1
|
+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */
|
2
|
+
|
3
|
+
'use strict';
|
4
|
+
|
5
|
+
const net = require('net');
|
6
|
+
const tls = require('tls');
|
7
|
+
const { randomFillSync } = require('crypto');
|
8
|
+
|
9
|
+
const PerMessageDeflate = require('./permessage-deflate');
|
10
|
+
const { EMPTY_BUFFER } = require('./constants');
|
11
|
+
const { isValidStatusCode } = require('./validation');
|
12
|
+
const { mask: applyMask, toBuffer } = require('./buffer-util');
|
13
|
+
|
14
|
+
const mask = Buffer.alloc(4);
|
15
|
+
|
16
|
+
/**
|
17
|
+
* HyBi Sender implementation.
|
18
|
+
*/
|
19
|
+
class Sender {
|
20
|
+
/**
|
21
|
+
* Creates a Sender instance.
|
22
|
+
*
|
23
|
+
* @param {(net.Socket|tls.Socket)} socket The connection socket
|
24
|
+
* @param {Object} [extensions] An object containing the negotiated extensions
|
25
|
+
*/
|
26
|
+
constructor(socket, extensions) {
|
27
|
+
this._extensions = extensions || {};
|
28
|
+
this._socket = socket;
|
29
|
+
|
30
|
+
this._firstFragment = true;
|
31
|
+
this._compress = false;
|
32
|
+
|
33
|
+
this._bufferedBytes = 0;
|
34
|
+
this._deflating = false;
|
35
|
+
this._queue = [];
|
36
|
+
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Frames a piece of data according to the HyBi WebSocket protocol.
|
40
|
+
*
|
41
|
+
* @param {Buffer} data The data to frame
|
42
|
+
* @param {Object} options Options object
|
43
|
+
* @param {Number} options.opcode The opcode
|
44
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
45
|
+
* modified
|
46
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
47
|
+
* FIN bit
|
48
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
49
|
+
* `data`
|
50
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
51
|
+
* RSV1 bit
|
52
|
+
* @return {Buffer[]} The framed data as a list of `Buffer` instances
|
53
|
+
* @public
|
54
|
+
*/
|
55
|
+
static frame(data, options) {
|
56
|
+
const merge = options.mask && options.readOnly;
|
57
|
+
let offset = options.mask ? 6 : 2;
|
58
|
+
let payloadLength = data.length;
|
59
|
+
|
60
|
+
if (data.length >= 65536) {
|
61
|
+
offset += 8;
|
62
|
+
payloadLength = 127;
|
63
|
+
} else if (data.length > 125) {
|
64
|
+
offset += 2;
|
65
|
+
payloadLength = 126;
|
66
|
+
}
|
67
|
+
|
68
|
+
const target = Buffer.allocUnsafe(merge ? data.length + offset : offset);
|
69
|
+
|
70
|
+
target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
|
71
|
+
if (options.rsv1) target[0] |= 0x40;
|
72
|
+
|
73
|
+
target[1] = payloadLength;
|
74
|
+
|
75
|
+
if (payloadLength === 126) {
|
76
|
+
target.writeUInt16BE(data.length, 2);
|
77
|
+
} else if (payloadLength === 127) {
|
78
|
+
target.writeUInt32BE(0, 2);
|
79
|
+
target.writeUInt32BE(data.length, 6);
|
80
|
+
}
|
81
|
+
|
82
|
+
if (!options.mask) return [target, data];
|
83
|
+
|
84
|
+
randomFillSync(mask, 0, 4);
|
85
|
+
|
86
|
+
target[1] |= 0x80;
|
87
|
+
target[offset - 4] = mask[0];
|
88
|
+
target[offset - 3] = mask[1];
|
89
|
+
target[offset - 2] = mask[2];
|
90
|
+
target[offset - 1] = mask[3];
|
91
|
+
|
92
|
+
if (merge) {
|
93
|
+
applyMask(data, mask, target, offset, data.length);
|
94
|
+
return [target];
|
95
|
+
}
|
96
|
+
|
97
|
+
applyMask(data, mask, data, 0, data.length);
|
98
|
+
return [target, data];
|
99
|
+
}
|
100
|
+
|
101
|
+
/**
|
102
|
+
* Sends a close message to the other peer.
|
103
|
+
*
|
104
|
+
* @param {Number} [code] The status code component of the body
|
105
|
+
* @param {String} [data] The message component of the body
|
106
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
|
107
|
+
* @param {Function} [cb] Callback
|
108
|
+
* @public
|
109
|
+
*/
|
110
|
+
close(code, data, mask, cb) {
|
111
|
+
let buf;
|
112
|
+
|
113
|
+
if (code === undefined) {
|
114
|
+
buf = EMPTY_BUFFER;
|
115
|
+
} else if (typeof code !== 'number' || !isValidStatusCode(code)) {
|
116
|
+
throw new TypeError('First argument must be a valid error code number');
|
117
|
+
} else if (data === undefined || data === '') {
|
118
|
+
buf = Buffer.allocUnsafe(2);
|
119
|
+
buf.writeUInt16BE(code, 0);
|
120
|
+
} else {
|
121
|
+
const length = Buffer.byteLength(data);
|
122
|
+
|
123
|
+
if (length > 123) {
|
124
|
+
throw new RangeError('The message must not be greater than 123 bytes');
|
125
|
+
}
|
126
|
+
|
127
|
+
buf = Buffer.allocUnsafe(2 + length);
|
128
|
+
buf.writeUInt16BE(code, 0);
|
129
|
+
buf.write(data, 2);
|
130
|
+
}
|
131
|
+
|
132
|
+
if (this._deflating) {
|
133
|
+
this.enqueue([this.doClose, buf, mask, cb]);
|
134
|
+
} else {
|
135
|
+
this.doClose(buf, mask, cb);
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
/**
|
140
|
+
* Frames and sends a close message.
|
141
|
+
*
|
142
|
+
* @param {Buffer} data The message to send
|
143
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
144
|
+
* @param {Function} [cb] Callback
|
145
|
+
* @private
|
146
|
+
*/
|
147
|
+
doClose(data, mask, cb) {
|
148
|
+
this.sendFrame(
|
149
|
+
Sender.frame(data, {
|
150
|
+
fin: true,
|
151
|
+
rsv1: false,
|
152
|
+
opcode: 0x08,
|
153
|
+
mask,
|
154
|
+
readOnly: false
|
155
|
+
}),
|
156
|
+
cb
|
157
|
+
);
|
158
|
+
}
|
159
|
+
|
160
|
+
/**
|
161
|
+
* Sends a ping message to the other peer.
|
162
|
+
*
|
163
|
+
* @param {*} data The message to send
|
164
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
165
|
+
* @param {Function} [cb] Callback
|
166
|
+
* @public
|
167
|
+
*/
|
168
|
+
ping(data, mask, cb) {
|
169
|
+
const buf = toBuffer(data);
|
170
|
+
|
171
|
+
if (buf.length > 125) {
|
172
|
+
throw new RangeError('The data size must not be greater than 125 bytes');
|
173
|
+
}
|
174
|
+
|
175
|
+
if (this._deflating) {
|
176
|
+
this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
|
177
|
+
} else {
|
178
|
+
this.doPing(buf, mask, toBuffer.readOnly, cb);
|
179
|
+
}
|
180
|
+
}
|
181
|
+
|
182
|
+
/**
|
183
|
+
* Frames and sends a ping message.
|
184
|
+
*
|
185
|
+
* @param {Buffer} data The message to send
|
186
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
187
|
+
* @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
|
188
|
+
* @param {Function} [cb] Callback
|
189
|
+
* @private
|
190
|
+
*/
|
191
|
+
doPing(data, mask, readOnly, cb) {
|
192
|
+
this.sendFrame(
|
193
|
+
Sender.frame(data, {
|
194
|
+
fin: true,
|
195
|
+
rsv1: false,
|
196
|
+
opcode: 0x09,
|
197
|
+
mask,
|
198
|
+
readOnly
|
199
|
+
}),
|
200
|
+
cb
|
201
|
+
);
|
202
|
+
}
|
203
|
+
|
204
|
+
/**
|
205
|
+
* Sends a pong message to the other peer.
|
206
|
+
*
|
207
|
+
* @param {*} data The message to send
|
208
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
209
|
+
* @param {Function} [cb] Callback
|
210
|
+
* @public
|
211
|
+
*/
|
212
|
+
pong(data, mask, cb) {
|
213
|
+
const buf = toBuffer(data);
|
214
|
+
|
215
|
+
if (buf.length > 125) {
|
216
|
+
throw new RangeError('The data size must not be greater than 125 bytes');
|
217
|
+
}
|
218
|
+
|
219
|
+
if (this._deflating) {
|
220
|
+
this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
|
221
|
+
} else {
|
222
|
+
this.doPong(buf, mask, toBuffer.readOnly, cb);
|
223
|
+
}
|
224
|
+
}
|
225
|
+
|
226
|
+
/**
|
227
|
+
* Frames and sends a pong message.
|
228
|
+
*
|
229
|
+
* @param {Buffer} data The message to send
|
230
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
231
|
+
* @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
|
232
|
+
* @param {Function} [cb] Callback
|
233
|
+
* @private
|
234
|
+
*/
|
235
|
+
doPong(data, mask, readOnly, cb) {
|
236
|
+
this.sendFrame(
|
237
|
+
Sender.frame(data, {
|
238
|
+
fin: true,
|
239
|
+
rsv1: false,
|
240
|
+
opcode: 0x0a,
|
241
|
+
mask,
|
242
|
+
readOnly
|
243
|
+
}),
|
244
|
+
cb
|
245
|
+
);
|
246
|
+
}
|
247
|
+
|
248
|
+
/**
|
249
|
+
* Sends a data message to the other peer.
|
250
|
+
*
|
251
|
+
* @param {*} data The message to send
|
252
|
+
* @param {Object} options Options object
|
253
|
+
* @param {Boolean} [options.compress=false] Specifies whether or not to
|
254
|
+
* compress `data`
|
255
|
+
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
|
256
|
+
* or text
|
257
|
+
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
|
258
|
+
* last one
|
259
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
260
|
+
* `data`
|
261
|
+
* @param {Function} [cb] Callback
|
262
|
+
* @public
|
263
|
+
*/
|
264
|
+
send(data, options, cb) {
|
265
|
+
const buf = toBuffer(data);
|
266
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
267
|
+
let opcode = options.binary ? 2 : 1;
|
268
|
+
let rsv1 = options.compress;
|
269
|
+
|
270
|
+
if (this._firstFragment) {
|
271
|
+
this._firstFragment = false;
|
272
|
+
if (rsv1 && perMessageDeflate) {
|
273
|
+
rsv1 = buf.length >= perMessageDeflate._threshold;
|
274
|
+
}
|
275
|
+
this._compress = rsv1;
|
276
|
+
} else {
|
277
|
+
rsv1 = false;
|
278
|
+
opcode = 0;
|
279
|
+
}
|
280
|
+
|
281
|
+
if (options.fin) this._firstFragment = true;
|
282
|
+
|
283
|
+
if (perMessageDeflate) {
|
284
|
+
const opts = {
|
285
|
+
fin: options.fin,
|
286
|
+
rsv1,
|
287
|
+
opcode,
|
288
|
+
mask: options.mask,
|
289
|
+
readOnly: toBuffer.readOnly
|
290
|
+
};
|
291
|
+
|
292
|
+
if (this._deflating) {
|
293
|
+
this.enqueue([this.dispatch, buf, this._compress, opts, cb]);
|
294
|
+
} else {
|
295
|
+
this.dispatch(buf, this._compress, opts, cb);
|
296
|
+
}
|
297
|
+
} else {
|
298
|
+
this.sendFrame(
|
299
|
+
Sender.frame(buf, {
|
300
|
+
fin: options.fin,
|
301
|
+
rsv1: false,
|
302
|
+
opcode,
|
303
|
+
mask: options.mask,
|
304
|
+
readOnly: toBuffer.readOnly
|
305
|
+
}),
|
306
|
+
cb
|
307
|
+
);
|
308
|
+
}
|
309
|
+
}
|
310
|
+
|
311
|
+
/**
|
312
|
+
* Dispatches a data message.
|
313
|
+
*
|
314
|
+
* @param {Buffer} data The message to send
|
315
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
316
|
+
* `data`
|
317
|
+
* @param {Object} options Options object
|
318
|
+
* @param {Number} options.opcode The opcode
|
319
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
320
|
+
* modified
|
321
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
322
|
+
* FIN bit
|
323
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
324
|
+
* `data`
|
325
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
326
|
+
* RSV1 bit
|
327
|
+
* @param {Function} [cb] Callback
|
328
|
+
* @private
|
329
|
+
*/
|
330
|
+
dispatch(data, compress, options, cb) {
|
331
|
+
if (!compress) {
|
332
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
333
|
+
return;
|
334
|
+
}
|
335
|
+
|
336
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
337
|
+
|
338
|
+
this._bufferedBytes += data.length;
|
339
|
+
this._deflating = true;
|
340
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
341
|
+
if (this._socket.destroyed) {
|
342
|
+
const err = new Error(
|
343
|
+
'The socket was closed while data was being compressed'
|
344
|
+
);
|
345
|
+
|
346
|
+
if (typeof cb === 'function') cb(err);
|
347
|
+
|
348
|
+
for (let i = 0; i < this._queue.length; i++) {
|
349
|
+
const callback = this._queue[i][4];
|
350
|
+
|
351
|
+
if (typeof callback === 'function') callback(err);
|
352
|
+
}
|
353
|
+
|
354
|
+
return;
|
355
|
+
}
|
356
|
+
|
357
|
+
this._bufferedBytes -= data.length;
|
358
|
+
this._deflating = false;
|
359
|
+
options.readOnly = false;
|
360
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
361
|
+
this.dequeue();
|
362
|
+
});
|
363
|
+
}
|
364
|
+
|
365
|
+
/**
|
366
|
+
* Executes queued send operations.
|
367
|
+
*
|
368
|
+
* @private
|
369
|
+
*/
|
370
|
+
dequeue() {
|
371
|
+
while (!this._deflating && this._queue.length) {
|
372
|
+
const params = this._queue.shift();
|
373
|
+
|
374
|
+
this._bufferedBytes -= params[1].length;
|
375
|
+
Reflect.apply(params[0], this, params.slice(1));
|
376
|
+
}
|
377
|
+
}
|
378
|
+
|
379
|
+
/**
|
380
|
+
* Enqueues a send operation.
|
381
|
+
*
|
382
|
+
* @param {Array} params Send operation parameters.
|
383
|
+
* @private
|
384
|
+
*/
|
385
|
+
enqueue(params) {
|
386
|
+
this._bufferedBytes += params[1].length;
|
387
|
+
this._queue.push(params);
|
388
|
+
}
|
389
|
+
|
390
|
+
/**
|
391
|
+
* Sends a frame.
|
392
|
+
*
|
393
|
+
* @param {Buffer[]} list The frame to send
|
394
|
+
* @param {Function} [cb] Callback
|
395
|
+
* @private
|
396
|
+
*/
|
397
|
+
sendFrame(list, cb) {
|
398
|
+
if (list.length === 2) {
|
399
|
+
this._socket.cork();
|
400
|
+
this._socket.write(list[0]);
|
401
|
+
this._socket.write(list[1], cb);
|
402
|
+
this._socket.uncork();
|
403
|
+
} else {
|
404
|
+
this._socket.write(list[0], cb);
|
405
|
+
}
|
406
|
+
}
|
407
|
+
}
|
408
|
+
|
409
|
+
module.exports = Sender;
|
@@ -0,0 +1,180 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const { Duplex } = require('stream');
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Emits the `'close'` event on a stream.
|
7
|
+
*
|
8
|
+
* @param {Duplex} stream The stream.
|
9
|
+
* @private
|
10
|
+
*/
|
11
|
+
function emitClose(stream) {
|
12
|
+
stream.emit('close');
|
13
|
+
}
|
14
|
+
|
15
|
+
/**
|
16
|
+
* The listener of the `'end'` event.
|
17
|
+
*
|
18
|
+
* @private
|
19
|
+
*/
|
20
|
+
function duplexOnEnd() {
|
21
|
+
if (!this.destroyed && this._writableState.finished) {
|
22
|
+
this.destroy();
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
/**
|
27
|
+
* The listener of the `'error'` event.
|
28
|
+
*
|
29
|
+
* @param {Error} err The error
|
30
|
+
* @private
|
31
|
+
*/
|
32
|
+
function duplexOnError(err) {
|
33
|
+
this.removeListener('error', duplexOnError);
|
34
|
+
this.destroy();
|
35
|
+
if (this.listenerCount('error') === 0) {
|
36
|
+
// Do not suppress the throwing behavior.
|
37
|
+
this.emit('error', err);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Wraps a `WebSocket` in a duplex stream.
|
43
|
+
*
|
44
|
+
* @param {WebSocket} ws The `WebSocket` to wrap
|
45
|
+
* @param {Object} [options] The options for the `Duplex` constructor
|
46
|
+
* @return {Duplex} The duplex stream
|
47
|
+
* @public
|
48
|
+
*/
|
49
|
+
function createWebSocketStream(ws, options) {
|
50
|
+
let resumeOnReceiverDrain = true;
|
51
|
+
let terminateOnDestroy = true;
|
52
|
+
|
53
|
+
function receiverOnDrain() {
|
54
|
+
if (resumeOnReceiverDrain) ws._socket.resume();
|
55
|
+
}
|
56
|
+
|
57
|
+
if (ws.readyState === ws.CONNECTING) {
|
58
|
+
ws.once('open', function open() {
|
59
|
+
ws._receiver.removeAllListeners('drain');
|
60
|
+
ws._receiver.on('drain', receiverOnDrain);
|
61
|
+
});
|
62
|
+
} else {
|
63
|
+
ws._receiver.removeAllListeners('drain');
|
64
|
+
ws._receiver.on('drain', receiverOnDrain);
|
65
|
+
}
|
66
|
+
|
67
|
+
const duplex = new Duplex({
|
68
|
+
...options,
|
69
|
+
autoDestroy: false,
|
70
|
+
emitClose: false,
|
71
|
+
objectMode: false,
|
72
|
+
writableObjectMode: false
|
73
|
+
});
|
74
|
+
|
75
|
+
ws.on('message', function message(msg) {
|
76
|
+
if (!duplex.push(msg)) {
|
77
|
+
resumeOnReceiverDrain = false;
|
78
|
+
ws._socket.pause();
|
79
|
+
}
|
80
|
+
});
|
81
|
+
|
82
|
+
ws.once('error', function error(err) {
|
83
|
+
if (duplex.destroyed) return;
|
84
|
+
|
85
|
+
// Prevent `ws.terminate()` from being called by `duplex._destroy()`.
|
86
|
+
//
|
87
|
+
// - If the `'error'` event is emitted before the `'open'` event, then
|
88
|
+
// `ws.terminate()` is a noop as no socket is assigned.
|
89
|
+
// - Otherwise, the error is re-emitted by the listener of the `'error'`
|
90
|
+
// event of the `Receiver` object. The listener already closes the
|
91
|
+
// connection by calling `ws.close()`. This allows a close frame to be
|
92
|
+
// sent to the other peer. If `ws.terminate()` is called right after this,
|
93
|
+
// then the close frame might not be sent.
|
94
|
+
terminateOnDestroy = false;
|
95
|
+
duplex.destroy(err);
|
96
|
+
});
|
97
|
+
|
98
|
+
ws.once('close', function close() {
|
99
|
+
if (duplex.destroyed) return;
|
100
|
+
|
101
|
+
duplex.push(null);
|
102
|
+
});
|
103
|
+
|
104
|
+
duplex._destroy = function (err, callback) {
|
105
|
+
if (ws.readyState === ws.CLOSED) {
|
106
|
+
callback(err);
|
107
|
+
process.nextTick(emitClose, duplex);
|
108
|
+
return;
|
109
|
+
}
|
110
|
+
|
111
|
+
let called = false;
|
112
|
+
|
113
|
+
ws.once('error', function error(err) {
|
114
|
+
called = true;
|
115
|
+
callback(err);
|
116
|
+
});
|
117
|
+
|
118
|
+
ws.once('close', function close() {
|
119
|
+
if (!called) callback(err);
|
120
|
+
process.nextTick(emitClose, duplex);
|
121
|
+
});
|
122
|
+
|
123
|
+
if (terminateOnDestroy) ws.terminate();
|
124
|
+
};
|
125
|
+
|
126
|
+
duplex._final = function (callback) {
|
127
|
+
if (ws.readyState === ws.CONNECTING) {
|
128
|
+
ws.once('open', function open() {
|
129
|
+
duplex._final(callback);
|
130
|
+
});
|
131
|
+
return;
|
132
|
+
}
|
133
|
+
|
134
|
+
// If the value of the `_socket` property is `null` it means that `ws` is a
|
135
|
+
// client websocket and the handshake failed. In fact, when this happens, a
|
136
|
+
// socket is never assigned to the websocket. Wait for the `'error'` event
|
137
|
+
// that will be emitted by the websocket.
|
138
|
+
if (ws._socket === null) return;
|
139
|
+
|
140
|
+
if (ws._socket._writableState.finished) {
|
141
|
+
callback();
|
142
|
+
if (duplex._readableState.endEmitted) duplex.destroy();
|
143
|
+
} else {
|
144
|
+
ws._socket.once('finish', function finish() {
|
145
|
+
// `duplex` is not destroyed here because the `'end'` event will be
|
146
|
+
// emitted on `duplex` after this `'finish'` event. The EOF signaling
|
147
|
+
// `null` chunk is, in fact, pushed when the websocket emits `'close'`.
|
148
|
+
callback();
|
149
|
+
});
|
150
|
+
ws.close();
|
151
|
+
}
|
152
|
+
};
|
153
|
+
|
154
|
+
duplex._read = function () {
|
155
|
+
if (
|
156
|
+
(ws.readyState === ws.OPEN || ws.readyState === ws.CLOSING) &&
|
157
|
+
!resumeOnReceiverDrain
|
158
|
+
) {
|
159
|
+
resumeOnReceiverDrain = true;
|
160
|
+
if (!ws._receiver._writableState.needDrain) ws._socket.resume();
|
161
|
+
}
|
162
|
+
};
|
163
|
+
|
164
|
+
duplex._write = function (chunk, encoding, callback) {
|
165
|
+
if (ws.readyState === ws.CONNECTING) {
|
166
|
+
ws.once('open', function open() {
|
167
|
+
duplex._write(chunk, encoding, callback);
|
168
|
+
});
|
169
|
+
return;
|
170
|
+
}
|
171
|
+
|
172
|
+
ws.send(chunk, callback);
|
173
|
+
};
|
174
|
+
|
175
|
+
duplex.on('end', duplexOnEnd);
|
176
|
+
duplex.on('error', duplexOnError);
|
177
|
+
return duplex;
|
178
|
+
}
|
179
|
+
|
180
|
+
module.exports = createWebSocketStream;
|
@@ -0,0 +1,104 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Checks if a status code is allowed in a close frame.
|
5
|
+
*
|
6
|
+
* @param {Number} code The status code
|
7
|
+
* @return {Boolean} `true` if the status code is valid, else `false`
|
8
|
+
* @public
|
9
|
+
*/
|
10
|
+
function isValidStatusCode(code) {
|
11
|
+
return (
|
12
|
+
(code >= 1000 &&
|
13
|
+
code <= 1014 &&
|
14
|
+
code !== 1004 &&
|
15
|
+
code !== 1005 &&
|
16
|
+
code !== 1006) ||
|
17
|
+
(code >= 3000 && code <= 4999)
|
18
|
+
);
|
19
|
+
}
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Checks if a given buffer contains only correct UTF-8.
|
23
|
+
* Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
|
24
|
+
* Markus Kuhn.
|
25
|
+
*
|
26
|
+
* @param {Buffer} buf The buffer to check
|
27
|
+
* @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
|
28
|
+
* @public
|
29
|
+
*/
|
30
|
+
function _isValidUTF8(buf) {
|
31
|
+
const len = buf.length;
|
32
|
+
let i = 0;
|
33
|
+
|
34
|
+
while (i < len) {
|
35
|
+
if ((buf[i] & 0x80) === 0) {
|
36
|
+
// 0xxxxxxx
|
37
|
+
i++;
|
38
|
+
} else if ((buf[i] & 0xe0) === 0xc0) {
|
39
|
+
// 110xxxxx 10xxxxxx
|
40
|
+
if (
|
41
|
+
i + 1 === len ||
|
42
|
+
(buf[i + 1] & 0xc0) !== 0x80 ||
|
43
|
+
(buf[i] & 0xfe) === 0xc0 // Overlong
|
44
|
+
) {
|
45
|
+
return false;
|
46
|
+
}
|
47
|
+
|
48
|
+
i += 2;
|
49
|
+
} else if ((buf[i] & 0xf0) === 0xe0) {
|
50
|
+
// 1110xxxx 10xxxxxx 10xxxxxx
|
51
|
+
if (
|
52
|
+
i + 2 >= len ||
|
53
|
+
(buf[i + 1] & 0xc0) !== 0x80 ||
|
54
|
+
(buf[i + 2] & 0xc0) !== 0x80 ||
|
55
|
+
(buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
|
56
|
+
(buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
|
57
|
+
) {
|
58
|
+
return false;
|
59
|
+
}
|
60
|
+
|
61
|
+
i += 3;
|
62
|
+
} else if ((buf[i] & 0xf8) === 0xf0) {
|
63
|
+
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
64
|
+
if (
|
65
|
+
i + 3 >= len ||
|
66
|
+
(buf[i + 1] & 0xc0) !== 0x80 ||
|
67
|
+
(buf[i + 2] & 0xc0) !== 0x80 ||
|
68
|
+
(buf[i + 3] & 0xc0) !== 0x80 ||
|
69
|
+
(buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
|
70
|
+
(buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
|
71
|
+
buf[i] > 0xf4 // > U+10FFFF
|
72
|
+
) {
|
73
|
+
return false;
|
74
|
+
}
|
75
|
+
|
76
|
+
i += 4;
|
77
|
+
} else {
|
78
|
+
return false;
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
return true;
|
83
|
+
}
|
84
|
+
|
85
|
+
try {
|
86
|
+
let isValidUTF8 = require('utf-8-validate');
|
87
|
+
|
88
|
+
/* istanbul ignore if */
|
89
|
+
if (typeof isValidUTF8 === 'object') {
|
90
|
+
isValidUTF8 = isValidUTF8.Validation.isValidUTF8; // utf-8-validate@<3.0.0
|
91
|
+
}
|
92
|
+
|
93
|
+
module.exports = {
|
94
|
+
isValidStatusCode,
|
95
|
+
isValidUTF8(buf) {
|
96
|
+
return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
97
|
+
}
|
98
|
+
};
|
99
|
+
} catch (e) /* istanbul ignore next */ {
|
100
|
+
module.exports = {
|
101
|
+
isValidStatusCode,
|
102
|
+
isValidUTF8: _isValidUTF8
|
103
|
+
};
|
104
|
+
}
|