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
@@ -1,438 +0,0 @@
|
|
1
|
-
/*!
|
2
|
-
* ws: a node.js websocket client
|
3
|
-
* Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
|
4
|
-
* MIT Licensed
|
5
|
-
*/
|
6
|
-
|
7
|
-
'use strict';
|
8
|
-
|
9
|
-
const crypto = require('crypto');
|
10
|
-
|
11
|
-
const PerMessageDeflate = require('./PerMessageDeflate');
|
12
|
-
const bufferUtil = require('./BufferUtil');
|
13
|
-
const ErrorCodes = require('./ErrorCodes');
|
14
|
-
|
15
|
-
/**
|
16
|
-
* HyBi Sender implementation.
|
17
|
-
*/
|
18
|
-
class Sender {
|
19
|
-
/**
|
20
|
-
* Creates a Sender instance.
|
21
|
-
*
|
22
|
-
* @param {net.Socket} socket The connection socket
|
23
|
-
* @param {Object} extensions An object containing the negotiated extensions
|
24
|
-
*/
|
25
|
-
constructor (socket, extensions) {
|
26
|
-
this.perMessageDeflate = (extensions || {})[PerMessageDeflate.extensionName];
|
27
|
-
this._socket = socket;
|
28
|
-
|
29
|
-
this.firstFragment = true;
|
30
|
-
this.compress = false;
|
31
|
-
|
32
|
-
this.processing = false;
|
33
|
-
this.bufferedBytes = 0;
|
34
|
-
this.queue = [];
|
35
|
-
|
36
|
-
this.onerror = null;
|
37
|
-
}
|
38
|
-
|
39
|
-
/**
|
40
|
-
* Sends a close message to the other peer.
|
41
|
-
*
|
42
|
-
* @param {(Number|undefined)} code The status code component of the body
|
43
|
-
* @param {String} data The message component of the body
|
44
|
-
* @param {Boolean} mask Specifies whether or not to mask the message
|
45
|
-
* @param {Function} cb Callback
|
46
|
-
* @public
|
47
|
-
*/
|
48
|
-
close (code, data, mask, cb) {
|
49
|
-
if (code !== undefined && (typeof code !== 'number' || !ErrorCodes.isValidErrorCode(code))) {
|
50
|
-
throw new Error('first argument must be a valid error code number');
|
51
|
-
}
|
52
|
-
|
53
|
-
const buf = Buffer.allocUnsafe(2 + (data ? Buffer.byteLength(data) : 0));
|
54
|
-
|
55
|
-
buf.writeUInt16BE(code || 1000, 0, true);
|
56
|
-
if (buf.length > 2) buf.write(data, 2);
|
57
|
-
|
58
|
-
if (this.perMessageDeflate) {
|
59
|
-
this.enqueue([this.doClose, buf, mask, cb]);
|
60
|
-
} else {
|
61
|
-
this.doClose(buf, mask, cb);
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
/**
|
66
|
-
* Frames and sends a close message.
|
67
|
-
*
|
68
|
-
* @param {Buffer} data The message to send
|
69
|
-
* @param {Boolean} mask Specifies whether or not to mask `data`
|
70
|
-
* @param {Function} cb Callback
|
71
|
-
* @private
|
72
|
-
*/
|
73
|
-
doClose (data, mask, cb) {
|
74
|
-
this.frameAndSend(data, {
|
75
|
-
readOnly: false,
|
76
|
-
opcode: 0x08,
|
77
|
-
rsv1: false,
|
78
|
-
fin: true,
|
79
|
-
mask
|
80
|
-
}, cb);
|
81
|
-
|
82
|
-
if (this.perMessageDeflate) this.continue();
|
83
|
-
}
|
84
|
-
|
85
|
-
/**
|
86
|
-
* Sends a ping message to the other peer.
|
87
|
-
*
|
88
|
-
* @param {*} data The message to send
|
89
|
-
* @param {Boolean} mask Specifies whether or not to mask `data`
|
90
|
-
* @public
|
91
|
-
*/
|
92
|
-
ping (data, mask) {
|
93
|
-
var readOnly = true;
|
94
|
-
|
95
|
-
if (data && !Buffer.isBuffer(data)) {
|
96
|
-
if (data instanceof ArrayBuffer) {
|
97
|
-
data = Buffer.from(data);
|
98
|
-
} else if (ArrayBuffer.isView(data)) {
|
99
|
-
data = viewToBuffer(data);
|
100
|
-
} else {
|
101
|
-
data = Buffer.from(data);
|
102
|
-
readOnly = false;
|
103
|
-
}
|
104
|
-
}
|
105
|
-
|
106
|
-
if (this.perMessageDeflate) {
|
107
|
-
this.enqueue([this.doPing, data, mask, readOnly]);
|
108
|
-
} else {
|
109
|
-
this.doPing(data, mask, readOnly);
|
110
|
-
}
|
111
|
-
}
|
112
|
-
|
113
|
-
/**
|
114
|
-
* Frames and sends a ping message.
|
115
|
-
*
|
116
|
-
* @param {*} data The message to send
|
117
|
-
* @param {Boolean} mask Specifies whether or not to mask `data`
|
118
|
-
* @param {Boolean} readOnly Specifies whether `data` can be modified
|
119
|
-
* @private
|
120
|
-
*/
|
121
|
-
doPing (data, mask, readOnly) {
|
122
|
-
this.frameAndSend(data, {
|
123
|
-
opcode: 0x09,
|
124
|
-
rsv1: false,
|
125
|
-
fin: true,
|
126
|
-
readOnly,
|
127
|
-
mask
|
128
|
-
});
|
129
|
-
|
130
|
-
if (this.perMessageDeflate) this.continue();
|
131
|
-
}
|
132
|
-
|
133
|
-
/**
|
134
|
-
* Sends a pong message to the other peer.
|
135
|
-
*
|
136
|
-
* @param {*} data The message to send
|
137
|
-
* @param {Boolean} mask Specifies whether or not to mask `data`
|
138
|
-
* @public
|
139
|
-
*/
|
140
|
-
pong (data, mask) {
|
141
|
-
var readOnly = true;
|
142
|
-
|
143
|
-
if (data && !Buffer.isBuffer(data)) {
|
144
|
-
if (data instanceof ArrayBuffer) {
|
145
|
-
data = Buffer.from(data);
|
146
|
-
} else if (ArrayBuffer.isView(data)) {
|
147
|
-
data = viewToBuffer(data);
|
148
|
-
} else {
|
149
|
-
data = Buffer.from(data);
|
150
|
-
readOnly = false;
|
151
|
-
}
|
152
|
-
}
|
153
|
-
|
154
|
-
if (this.perMessageDeflate) {
|
155
|
-
this.enqueue([this.doPong, data, mask, readOnly]);
|
156
|
-
} else {
|
157
|
-
this.doPong(data, mask, readOnly);
|
158
|
-
}
|
159
|
-
}
|
160
|
-
|
161
|
-
/**
|
162
|
-
* Frames and sends a pong message.
|
163
|
-
*
|
164
|
-
* @param {*} data The message to send
|
165
|
-
* @param {Boolean} mask Specifies whether or not to mask `data`
|
166
|
-
* @param {Boolean} readOnly Specifies whether `data` can be modified
|
167
|
-
* @private
|
168
|
-
*/
|
169
|
-
doPong (data, mask, readOnly) {
|
170
|
-
this.frameAndSend(data, {
|
171
|
-
opcode: 0x0a,
|
172
|
-
rsv1: false,
|
173
|
-
fin: true,
|
174
|
-
readOnly,
|
175
|
-
mask
|
176
|
-
});
|
177
|
-
|
178
|
-
if (this.perMessageDeflate) this.continue();
|
179
|
-
}
|
180
|
-
|
181
|
-
/**
|
182
|
-
* Sends a data message to the other peer.
|
183
|
-
*
|
184
|
-
* @param {*} data The message to send
|
185
|
-
* @param {Object} options Options object
|
186
|
-
* @param {Boolean} options.compress Specifies whether or not to compress `data`
|
187
|
-
* @param {Boolean} options.binary Specifies whether `data` is binary or text
|
188
|
-
* @param {Boolean} options.fin Specifies whether the fragment is the last one
|
189
|
-
* @param {Boolean} options.mask Specifies whether or not to mask `data`
|
190
|
-
* @param {Function} cb Callback
|
191
|
-
* @public
|
192
|
-
*/
|
193
|
-
send (data, options, cb) {
|
194
|
-
var opcode = options.binary ? 2 : 1;
|
195
|
-
var rsv1 = options.compress;
|
196
|
-
var readOnly = true;
|
197
|
-
|
198
|
-
if (data && !Buffer.isBuffer(data)) {
|
199
|
-
if (data instanceof ArrayBuffer) {
|
200
|
-
data = Buffer.from(data);
|
201
|
-
} else if (ArrayBuffer.isView(data)) {
|
202
|
-
data = viewToBuffer(data);
|
203
|
-
} else {
|
204
|
-
data = Buffer.from(data);
|
205
|
-
readOnly = false;
|
206
|
-
}
|
207
|
-
}
|
208
|
-
|
209
|
-
if (this.firstFragment) {
|
210
|
-
this.firstFragment = false;
|
211
|
-
if (rsv1 && data && this.perMessageDeflate) {
|
212
|
-
rsv1 = data.length >= this.perMessageDeflate.threshold;
|
213
|
-
}
|
214
|
-
this.compress = rsv1;
|
215
|
-
} else {
|
216
|
-
rsv1 = false;
|
217
|
-
opcode = 0;
|
218
|
-
}
|
219
|
-
|
220
|
-
if (options.fin) this.firstFragment = true;
|
221
|
-
|
222
|
-
if (this.perMessageDeflate) {
|
223
|
-
this.enqueue([this.dispatch, data, {
|
224
|
-
compress: this.compress,
|
225
|
-
mask: options.mask,
|
226
|
-
fin: options.fin,
|
227
|
-
readOnly,
|
228
|
-
opcode,
|
229
|
-
rsv1
|
230
|
-
}, cb]);
|
231
|
-
} else {
|
232
|
-
this.frameAndSend(data, {
|
233
|
-
mask: options.mask,
|
234
|
-
fin: options.fin,
|
235
|
-
rsv1: false,
|
236
|
-
readOnly,
|
237
|
-
opcode
|
238
|
-
}, cb);
|
239
|
-
}
|
240
|
-
}
|
241
|
-
|
242
|
-
/**
|
243
|
-
* Dispatches a data message.
|
244
|
-
*
|
245
|
-
* @param {Buffer} data The message to send
|
246
|
-
* @param {Object} options Options object
|
247
|
-
* @param {Number} options.opcode The opcode
|
248
|
-
* @param {Boolean} options.readOnly Specifies whether `data` can be modified
|
249
|
-
* @param {Boolean} options.fin Specifies whether or not to set the FIN bit
|
250
|
-
* @param {Boolean} options.compress Specifies whether or not to compress `data`
|
251
|
-
* @param {Boolean} options.mask Specifies whether or not to mask `data`
|
252
|
-
* @param {Boolean} options.rsv1 Specifies whether or not to set the RSV1 bit
|
253
|
-
* @param {Function} cb Callback
|
254
|
-
* @private
|
255
|
-
*/
|
256
|
-
dispatch (data, options, cb) {
|
257
|
-
if (!options.compress) {
|
258
|
-
this.frameAndSend(data, options, cb);
|
259
|
-
this.continue();
|
260
|
-
return;
|
261
|
-
}
|
262
|
-
|
263
|
-
this.perMessageDeflate.compress(data, options.fin, (err, buf) => {
|
264
|
-
if (err) {
|
265
|
-
if (cb) cb(err);
|
266
|
-
else this.onerror(err);
|
267
|
-
return;
|
268
|
-
}
|
269
|
-
|
270
|
-
options.readOnly = false;
|
271
|
-
this.frameAndSend(buf, options, cb);
|
272
|
-
this.continue();
|
273
|
-
});
|
274
|
-
}
|
275
|
-
|
276
|
-
/**
|
277
|
-
* Frames and sends a piece of data according to the HyBi WebSocket protocol.
|
278
|
-
*
|
279
|
-
* @param {Buffer} data The data to send
|
280
|
-
* @param {Object} options Options object
|
281
|
-
* @param {Number} options.opcode The opcode
|
282
|
-
* @param {Boolean} options.readOnly Specifies whether `data` can be modified
|
283
|
-
* @param {Boolean} options.fin Specifies whether or not to set the FIN bit
|
284
|
-
* @param {Boolean} options.mask Specifies whether or not to mask `data`
|
285
|
-
* @param {Boolean} options.rsv1 Specifies whether or not to set the RSV1 bit
|
286
|
-
* @param {Function} cb Callback
|
287
|
-
* @private
|
288
|
-
*/
|
289
|
-
frameAndSend (data, options, cb) {
|
290
|
-
if (!data) {
|
291
|
-
const bytes = [options.opcode, 0];
|
292
|
-
|
293
|
-
if (options.fin) bytes[0] |= 0x80;
|
294
|
-
if (options.mask) {
|
295
|
-
bytes[1] |= 0x80;
|
296
|
-
bytes.push(0, 0, 0, 0);
|
297
|
-
}
|
298
|
-
|
299
|
-
sendFramedData(this, Buffer.from(bytes), null, cb);
|
300
|
-
return;
|
301
|
-
}
|
302
|
-
|
303
|
-
const mergeBuffers = data.length < 1024 || options.mask && options.readOnly;
|
304
|
-
var dataOffset = options.mask ? 6 : 2;
|
305
|
-
var payloadLength = data.length;
|
306
|
-
|
307
|
-
if (data.length >= 65536) {
|
308
|
-
dataOffset += 8;
|
309
|
-
payloadLength = 127;
|
310
|
-
} else if (data.length > 125) {
|
311
|
-
dataOffset += 2;
|
312
|
-
payloadLength = 126;
|
313
|
-
}
|
314
|
-
|
315
|
-
const outputBuffer = Buffer.allocUnsafe(
|
316
|
-
mergeBuffers ? data.length + dataOffset : dataOffset
|
317
|
-
);
|
318
|
-
|
319
|
-
outputBuffer[0] = options.fin ? options.opcode | 0x80 : options.opcode;
|
320
|
-
if (options.rsv1) outputBuffer[0] |= 0x40;
|
321
|
-
|
322
|
-
if (payloadLength === 126) {
|
323
|
-
outputBuffer.writeUInt16BE(data.length, 2, true);
|
324
|
-
} else if (payloadLength === 127) {
|
325
|
-
outputBuffer.writeUInt32BE(0, 2, true);
|
326
|
-
outputBuffer.writeUInt32BE(data.length, 6, true);
|
327
|
-
}
|
328
|
-
|
329
|
-
if (options.mask) {
|
330
|
-
const mask = getRandomMask();
|
331
|
-
|
332
|
-
outputBuffer[1] = payloadLength | 0x80;
|
333
|
-
outputBuffer[dataOffset - 4] = mask[0];
|
334
|
-
outputBuffer[dataOffset - 3] = mask[1];
|
335
|
-
outputBuffer[dataOffset - 2] = mask[2];
|
336
|
-
outputBuffer[dataOffset - 1] = mask[3];
|
337
|
-
|
338
|
-
if (mergeBuffers) {
|
339
|
-
bufferUtil.mask(data, mask, outputBuffer, dataOffset, data.length);
|
340
|
-
} else {
|
341
|
-
bufferUtil.mask(data, mask, data, 0, data.length);
|
342
|
-
}
|
343
|
-
} else {
|
344
|
-
outputBuffer[1] = payloadLength;
|
345
|
-
if (mergeBuffers) data.copy(outputBuffer, dataOffset);
|
346
|
-
}
|
347
|
-
|
348
|
-
sendFramedData(this, outputBuffer, mergeBuffers ? null : data, cb);
|
349
|
-
}
|
350
|
-
|
351
|
-
/**
|
352
|
-
* Executes a queued send operation.
|
353
|
-
*
|
354
|
-
* @private
|
355
|
-
*/
|
356
|
-
dequeue () {
|
357
|
-
if (this.processing) return;
|
358
|
-
|
359
|
-
const params = this.queue.shift();
|
360
|
-
if (!params) return;
|
361
|
-
|
362
|
-
if (params[1]) this.bufferedBytes -= params[1].length;
|
363
|
-
this.processing = true;
|
364
|
-
|
365
|
-
params[0].apply(this, params.slice(1));
|
366
|
-
}
|
367
|
-
|
368
|
-
/**
|
369
|
-
* Signals the completion of a send operation.
|
370
|
-
*
|
371
|
-
* @private
|
372
|
-
*/
|
373
|
-
continue () {
|
374
|
-
process.nextTick(() => {
|
375
|
-
this.processing = false;
|
376
|
-
this.dequeue();
|
377
|
-
});
|
378
|
-
}
|
379
|
-
|
380
|
-
/**
|
381
|
-
* Enqueues a send operation.
|
382
|
-
*
|
383
|
-
* @param {Array} params Send operation parameters.
|
384
|
-
* @private
|
385
|
-
*/
|
386
|
-
enqueue (params) {
|
387
|
-
if (params[1]) this.bufferedBytes += params[1].length;
|
388
|
-
this.queue.push(params);
|
389
|
-
this.dequeue();
|
390
|
-
}
|
391
|
-
}
|
392
|
-
|
393
|
-
module.exports = Sender;
|
394
|
-
|
395
|
-
/**
|
396
|
-
* Converts an `ArrayBuffer` view into a buffer.
|
397
|
-
*
|
398
|
-
* @param {(DataView|TypedArray)} view The view to convert
|
399
|
-
* @return {Buffer} Converted view
|
400
|
-
* @private
|
401
|
-
*/
|
402
|
-
function viewToBuffer (view) {
|
403
|
-
const buf = Buffer.from(view.buffer);
|
404
|
-
|
405
|
-
if (view.byteLength !== view.buffer.byteLength) {
|
406
|
-
return buf.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
407
|
-
}
|
408
|
-
|
409
|
-
return buf;
|
410
|
-
}
|
411
|
-
|
412
|
-
/**
|
413
|
-
* Generates a random mask.
|
414
|
-
*
|
415
|
-
* @return {Buffer} The mask
|
416
|
-
* @private
|
417
|
-
*/
|
418
|
-
function getRandomMask () {
|
419
|
-
return crypto.randomBytes(4);
|
420
|
-
}
|
421
|
-
|
422
|
-
/**
|
423
|
-
* Sends a frame.
|
424
|
-
*
|
425
|
-
* @param {Sender} sender Sender instance
|
426
|
-
* @param {Buffer} outputBuffer The data to send
|
427
|
-
* @param {Buffer} data Additional data to send if frame is split into two buffers
|
428
|
-
* @param {Function} cb Callback
|
429
|
-
* @private
|
430
|
-
*/
|
431
|
-
function sendFramedData (sender, outputBuffer, data, cb) {
|
432
|
-
if (data) {
|
433
|
-
sender._socket.write(outputBuffer);
|
434
|
-
sender._socket.write(data, cb);
|
435
|
-
} else {
|
436
|
-
sender._socket.write(outputBuffer, cb);
|
437
|
-
}
|
438
|
-
}
|
@@ -1,17 +0,0 @@
|
|
1
|
-
/*!
|
2
|
-
* ws: a node.js websocket client
|
3
|
-
* Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
|
4
|
-
* MIT Licensed
|
5
|
-
*/
|
6
|
-
|
7
|
-
'use strict';
|
8
|
-
|
9
|
-
try {
|
10
|
-
const isValidUTF8 = require('utf-8-validate');
|
11
|
-
|
12
|
-
module.exports = typeof isValidUTF8 === 'object'
|
13
|
-
? isValidUTF8.Validation.isValidUTF8 // utf-8-validate@<3.0.0
|
14
|
-
: isValidUTF8;
|
15
|
-
} catch (e) {
|
16
|
-
module.exports = require('./Validation.fallback');
|
17
|
-
}
|