dragonfly_chrome_headless 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +12 -0
- data/Gemfile +4 -0
- data/README.md +53 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dragonfly_chrome_headless.gemspec +27 -0
- data/lib/dragonfly_chrome_headless.rb +9 -0
- data/lib/dragonfly_chrome_headless/plugin.rb +17 -0
- data/lib/dragonfly_chrome_headless/processors/rasterize.rb +33 -0
- data/lib/dragonfly_chrome_headless/version.rb +3 -0
- data/node_modules/.bin/chrome-remote-interface +1 -0
- data/node_modules/.bin/mkdirp +1 -0
- data/node_modules/.bin/rimraf +1 -0
- data/node_modules/@types/core-js/LICENSE +21 -0
- data/node_modules/@types/core-js/README.md +16 -0
- data/node_modules/@types/core-js/index.d.ts +2452 -0
- data/node_modules/@types/core-js/package.json +85 -0
- data/node_modules/@types/mkdirp/README.md +18 -0
- data/node_modules/@types/mkdirp/index.d.ts +14 -0
- data/node_modules/@types/mkdirp/package.json +77 -0
- data/node_modules/@types/mkdirp/types-metadata.json +25 -0
- data/node_modules/@types/node/README.md +16 -0
- data/node_modules/@types/node/index.d.ts +4132 -0
- data/node_modules/@types/node/package.json +84 -0
- data/node_modules/balanced-match/.npmignore +5 -0
- data/node_modules/balanced-match/LICENSE.md +21 -0
- data/node_modules/balanced-match/README.md +91 -0
- data/node_modules/balanced-match/index.js +59 -0
- data/node_modules/balanced-match/package.json +112 -0
- data/node_modules/brace-expansion/README.md +123 -0
- data/node_modules/brace-expansion/index.js +201 -0
- data/node_modules/brace-expansion/package.json +114 -0
- data/node_modules/chrome-launcher/.clang-format +6 -0
- data/node_modules/chrome-launcher/.npmignore +11 -0
- data/node_modules/chrome-launcher/README.md +123 -0
- data/node_modules/chrome-launcher/ask.js +32 -0
- data/node_modules/chrome-launcher/ask.ts +35 -0
- data/node_modules/chrome-launcher/chrome-finder.js +157 -0
- data/node_modules/chrome-launcher/chrome-finder.ts +186 -0
- data/node_modules/chrome-launcher/chrome-launcher.js +245 -0
- data/node_modules/chrome-launcher/chrome-launcher.ts +312 -0
- data/node_modules/chrome-launcher/compiled-check.js +14 -0
- data/node_modules/chrome-launcher/flags.js +27 -0
- data/node_modules/chrome-launcher/flags.ts +26 -0
- data/node_modules/chrome-launcher/index.js +7 -0
- data/node_modules/chrome-launcher/index.ts +1 -0
- data/node_modules/chrome-launcher/manual-chrome-launcher.js +30 -0
- data/node_modules/chrome-launcher/package.json +116 -0
- data/node_modules/chrome-launcher/random-port.js +24 -0
- data/node_modules/chrome-launcher/random-port.ts +23 -0
- data/node_modules/chrome-launcher/tsconfig.json +19 -0
- data/node_modules/chrome-launcher/utils.js +52 -0
- data/node_modules/chrome-launcher/utils.ts +44 -0
- data/node_modules/chrome-launcher/yarn.lock +1486 -0
- data/node_modules/chrome-remote-interface/LICENSE +18 -0
- data/node_modules/chrome-remote-interface/README.md +849 -0
- data/node_modules/chrome-remote-interface/bin/client.js +337 -0
- data/node_modules/chrome-remote-interface/chrome-remote-interface.js +11 -0
- data/node_modules/chrome-remote-interface/index.js +39 -0
- data/node_modules/chrome-remote-interface/lib/api.js +84 -0
- data/node_modules/chrome-remote-interface/lib/chrome.js +307 -0
- data/node_modules/chrome-remote-interface/lib/defaults.js +4 -0
- data/node_modules/chrome-remote-interface/lib/devtools.js +245 -0
- data/node_modules/chrome-remote-interface/lib/external-request.js +28 -0
- data/node_modules/chrome-remote-interface/lib/protocol.json +13780 -0
- data/node_modules/chrome-remote-interface/lib/websocket-wrapper.js +32 -0
- data/node_modules/chrome-remote-interface/package.json +128 -0
- data/node_modules/chrome-remote-interface/webpack.config.js +55 -0
- data/node_modules/commander/Readme.md +195 -0
- data/node_modules/commander/index.js +851 -0
- data/node_modules/commander/package.json +92 -0
- data/node_modules/concat-map/.travis.yml +4 -0
- data/node_modules/concat-map/LICENSE +18 -0
- data/node_modules/concat-map/README.markdown +62 -0
- data/node_modules/concat-map/example/map.js +6 -0
- data/node_modules/concat-map/index.js +13 -0
- data/node_modules/concat-map/package.json +117 -0
- data/node_modules/concat-map/test/map.js +39 -0
- data/node_modules/debug/.coveralls.yml +1 -0
- data/node_modules/debug/.eslintrc +11 -0
- data/node_modules/debug/.npmignore +9 -0
- data/node_modules/debug/.travis.yml +14 -0
- data/node_modules/debug/CHANGELOG.md +357 -0
- data/node_modules/debug/LICENSE +19 -0
- data/node_modules/debug/Makefile +50 -0
- data/node_modules/debug/Readme.md +312 -0
- data/node_modules/debug/component.json +19 -0
- data/node_modules/debug/karma.conf.js +70 -0
- data/node_modules/debug/node.js +1 -0
- data/node_modules/debug/package.json +124 -0
- data/node_modules/debug/src/browser.js +185 -0
- data/node_modules/debug/src/debug.js +202 -0
- data/node_modules/debug/src/index.js +10 -0
- data/node_modules/debug/src/node.js +246 -0
- data/node_modules/fs.realpath/LICENSE +43 -0
- data/node_modules/fs.realpath/README.md +33 -0
- data/node_modules/fs.realpath/index.js +66 -0
- data/node_modules/fs.realpath/old.js +303 -0
- data/node_modules/fs.realpath/package.json +94 -0
- data/node_modules/glob/LICENSE +15 -0
- data/node_modules/glob/README.md +368 -0
- data/node_modules/glob/changelog.md +67 -0
- data/node_modules/glob/common.js +240 -0
- data/node_modules/glob/glob.js +790 -0
- data/node_modules/glob/package.json +112 -0
- data/node_modules/glob/sync.js +486 -0
- data/node_modules/html-pdf-chrome/.npmignore +9 -0
- data/node_modules/html-pdf-chrome/LICENSE +21 -0
- data/node_modules/html-pdf-chrome/README.md +165 -0
- data/node_modules/html-pdf-chrome/lib/src/ChromePrintOptions.d.ts +87 -0
- data/node_modules/html-pdf-chrome/lib/src/ChromePrintOptions.js +4 -0
- data/node_modules/html-pdf-chrome/lib/src/ChromePrintOptions.js.map +1 -0
- data/node_modules/html-pdf-chrome/lib/src/CompletionTrigger.d.ts +120 -0
- data/node_modules/html-pdf-chrome/lib/src/CompletionTrigger.js +206 -0
- data/node_modules/html-pdf-chrome/lib/src/CompletionTrigger.js.map +1 -0
- data/node_modules/html-pdf-chrome/lib/src/CreateResult.d.ts +70 -0
- data/node_modules/html-pdf-chrome/lib/src/CreateResult.js +98 -0
- data/node_modules/html-pdf-chrome/lib/src/CreateResult.js.map +1 -0
- data/node_modules/html-pdf-chrome/lib/src/index.d.ts +72 -0
- data/node_modules/html-pdf-chrome/lib/src/index.js +123 -0
- data/node_modules/html-pdf-chrome/lib/src/index.js.map +1 -0
- data/node_modules/html-pdf-chrome/package.json +133 -0
- data/node_modules/html-pdf-chrome/src/ChromePrintOptions.ts +99 -0
- data/node_modules/html-pdf-chrome/src/CompletionTrigger.ts +201 -0
- data/node_modules/html-pdf-chrome/src/CreateResult.ts +100 -0
- data/node_modules/html-pdf-chrome/src/index.ts +179 -0
- data/node_modules/inflight/LICENSE +15 -0
- data/node_modules/inflight/README.md +37 -0
- data/node_modules/inflight/inflight.js +54 -0
- data/node_modules/inflight/package.json +105 -0
- data/node_modules/inherits/LICENSE +16 -0
- data/node_modules/inherits/README.md +42 -0
- data/node_modules/inherits/inherits.js +7 -0
- data/node_modules/inherits/inherits_browser.js +23 -0
- data/node_modules/inherits/package.json +97 -0
- data/node_modules/lighthouse-logger/README.md +4 -0
- data/node_modules/lighthouse-logger/index.js +212 -0
- data/node_modules/lighthouse-logger/package.json +69 -0
- data/node_modules/lighthouse-logger/yarn.lock +13 -0
- data/node_modules/minimatch/LICENSE +15 -0
- data/node_modules/minimatch/README.md +209 -0
- data/node_modules/minimatch/minimatch.js +923 -0
- data/node_modules/minimatch/package.json +99 -0
- data/node_modules/minimist/.travis.yml +4 -0
- data/node_modules/minimist/LICENSE +18 -0
- data/node_modules/minimist/example/parse.js +2 -0
- data/node_modules/minimist/index.js +187 -0
- data/node_modules/minimist/package.json +101 -0
- data/node_modules/minimist/readme.markdown +73 -0
- data/node_modules/minimist/test/dash.js +24 -0
- data/node_modules/minimist/test/default_bool.js +20 -0
- data/node_modules/minimist/test/dotted.js +16 -0
- data/node_modules/minimist/test/long.js +31 -0
- data/node_modules/minimist/test/parse.js +318 -0
- data/node_modules/minimist/test/parse_modified.js +9 -0
- data/node_modules/minimist/test/short.js +67 -0
- data/node_modules/minimist/test/whitespace.js +8 -0
- data/node_modules/mkdirp/.travis.yml +8 -0
- data/node_modules/mkdirp/LICENSE +21 -0
- data/node_modules/mkdirp/bin/cmd.js +33 -0
- data/node_modules/mkdirp/bin/usage.txt +12 -0
- data/node_modules/mkdirp/examples/pow.js +6 -0
- data/node_modules/mkdirp/index.js +98 -0
- data/node_modules/mkdirp/package.json +93 -0
- data/node_modules/mkdirp/readme.markdown +100 -0
- data/node_modules/mkdirp/test/chmod.js +41 -0
- data/node_modules/mkdirp/test/clobber.js +38 -0
- data/node_modules/mkdirp/test/mkdirp.js +28 -0
- data/node_modules/mkdirp/test/opts_fs.js +29 -0
- data/node_modules/mkdirp/test/opts_fs_sync.js +27 -0
- data/node_modules/mkdirp/test/perm.js +32 -0
- data/node_modules/mkdirp/test/perm_sync.js +36 -0
- data/node_modules/mkdirp/test/race.js +37 -0
- data/node_modules/mkdirp/test/rel.js +32 -0
- data/node_modules/mkdirp/test/return.js +25 -0
- data/node_modules/mkdirp/test/return_sync.js +24 -0
- data/node_modules/mkdirp/test/root.js +19 -0
- data/node_modules/mkdirp/test/sync.js +32 -0
- data/node_modules/mkdirp/test/umask.js +28 -0
- data/node_modules/mkdirp/test/umask_sync.js +32 -0
- data/node_modules/ms/README.md +51 -0
- data/node_modules/ms/index.js +152 -0
- data/node_modules/ms/license.md +21 -0
- data/node_modules/ms/package.json +109 -0
- data/node_modules/once/LICENSE +15 -0
- data/node_modules/once/README.md +79 -0
- data/node_modules/once/once.js +42 -0
- data/node_modules/once/package.json +101 -0
- data/node_modules/path-is-absolute/index.js +20 -0
- data/node_modules/path-is-absolute/license +21 -0
- data/node_modules/path-is-absolute/package.json +111 -0
- data/node_modules/path-is-absolute/readme.md +59 -0
- data/node_modules/rimraf/LICENSE +15 -0
- data/node_modules/rimraf/README.md +101 -0
- data/node_modules/rimraf/bin.js +50 -0
- data/node_modules/rimraf/package.json +99 -0
- data/node_modules/rimraf/rimraf.js +363 -0
- data/node_modules/ultron/LICENSE +22 -0
- data/node_modules/ultron/index.js +138 -0
- data/node_modules/ultron/package.json +112 -0
- data/node_modules/wrappy/LICENSE +15 -0
- data/node_modules/wrappy/README.md +36 -0
- data/node_modules/wrappy/package.json +97 -0
- data/node_modules/wrappy/wrappy.js +33 -0
- data/node_modules/ws/LICENSE +21 -0
- data/node_modules/ws/README.md +259 -0
- data/node_modules/ws/SECURITY.md +33 -0
- data/node_modules/ws/index.js +15 -0
- data/node_modules/ws/lib/BufferUtil.fallback.js +56 -0
- data/node_modules/ws/lib/BufferUtil.js +15 -0
- data/node_modules/ws/lib/ErrorCodes.js +28 -0
- data/node_modules/ws/lib/EventTarget.js +158 -0
- data/node_modules/ws/lib/Extensions.js +69 -0
- data/node_modules/ws/lib/PerMessageDeflate.js +339 -0
- data/node_modules/ws/lib/Receiver.js +520 -0
- data/node_modules/ws/lib/Sender.js +438 -0
- data/node_modules/ws/lib/Validation.fallback.js +9 -0
- data/node_modules/ws/lib/Validation.js +17 -0
- data/node_modules/ws/lib/WebSocket.js +705 -0
- data/node_modules/ws/lib/WebSocketServer.js +336 -0
- data/node_modules/ws/package.json +122 -0
- data/package.json +26 -0
- data/samples/sample.html +13 -0
- data/script/rasterize.js +18 -0
- metadata +325 -0
@@ -0,0 +1,438 @@
|
|
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
|
+
}
|
@@ -0,0 +1,17 @@
|
|
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
|
+
}
|