UrgentcareCLI 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/Gemfile.lock +60 -25
- data/Notes +1 -1
- data/README.md +5 -5
- data/Rakefile +2 -0
- data/UrgentCare.gemspec +3 -3
- data/background.jpg +0 -0
- data/lib/UrgentCare.rb +3 -0
- data/lib/UrgentCare/CLI.rb +6 -2
- data/lib/UrgentCare/version.rb +1 -1
- data/lib/Urgentcare/Scraper.rb +65 -23
- data/node_modules/.bin/rimraf +1 -0
- data/node_modules/.package-lock.json +250 -0
- data/node_modules/balanced-match/.github/FUNDING.yml +2 -0
- data/node_modules/balanced-match/LICENSE.md +21 -0
- data/node_modules/balanced-match/README.md +97 -0
- data/node_modules/balanced-match/index.js +62 -0
- data/node_modules/balanced-match/package.json +48 -0
- data/node_modules/brace-expansion/LICENSE +21 -0
- data/node_modules/brace-expansion/README.md +129 -0
- data/node_modules/brace-expansion/index.js +201 -0
- data/node_modules/brace-expansion/package.json +47 -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 +43 -0
- data/node_modules/concat-map/test/map.js +39 -0
- data/node_modules/core-util-is/LICENSE +19 -0
- data/node_modules/core-util-is/README.md +3 -0
- data/node_modules/core-util-is/float.patch +604 -0
- data/node_modules/core-util-is/lib/util.js +107 -0
- data/node_modules/core-util-is/package.json +32 -0
- data/node_modules/core-util-is/test.js +68 -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 +26 -0
- data/node_modules/glob/LICENSE +21 -0
- data/node_modules/glob/README.md +375 -0
- data/node_modules/glob/changelog.md +67 -0
- data/node_modules/glob/common.js +234 -0
- data/node_modules/glob/glob.js +788 -0
- data/node_modules/glob/package.json +51 -0
- data/node_modules/glob/sync.js +484 -0
- data/node_modules/immediate/LICENSE.txt +20 -0
- data/node_modules/immediate/README.md +93 -0
- data/node_modules/immediate/dist/immediate.js +75 -0
- data/node_modules/immediate/dist/immediate.min.js +1 -0
- data/node_modules/immediate/lib/browser.js +69 -0
- data/node_modules/immediate/lib/index.js +73 -0
- data/node_modules/immediate/package.json +42 -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 +29 -0
- data/node_modules/inherits/LICENSE +16 -0
- data/node_modules/inherits/README.md +42 -0
- data/node_modules/inherits/inherits.js +9 -0
- data/node_modules/inherits/inherits_browser.js +27 -0
- data/node_modules/inherits/package.json +29 -0
- data/node_modules/isarray/.npmignore +1 -0
- data/node_modules/isarray/.travis.yml +4 -0
- data/node_modules/isarray/Makefile +6 -0
- data/node_modules/isarray/README.md +60 -0
- data/node_modules/isarray/component.json +19 -0
- data/node_modules/isarray/index.js +5 -0
- data/node_modules/isarray/package.json +45 -0
- data/node_modules/isarray/test.js +20 -0
- data/node_modules/jszip/.codeclimate.yml +16 -0
- data/node_modules/jszip/.editorconfig +8 -0
- data/node_modules/jszip/.jshintignore +1 -0
- data/node_modules/jszip/.jshintrc +21 -0
- data/node_modules/jszip/.travis.yml +17 -0
- data/node_modules/jszip/CHANGES.md +163 -0
- data/node_modules/jszip/LICENSE.markdown +651 -0
- data/node_modules/jszip/README.markdown +35 -0
- data/node_modules/jszip/dist/jszip.js +30 -0
- data/node_modules/jszip/dist/jszip.min.js +13 -0
- data/node_modules/jszip/index.d.ts +270 -0
- data/node_modules/jszip/lib/base64.js +106 -0
- data/node_modules/jszip/lib/compressedObject.js +74 -0
- data/node_modules/jszip/lib/compressions.js +14 -0
- data/node_modules/jszip/lib/crc32.js +77 -0
- data/node_modules/jszip/lib/defaults.js +11 -0
- data/node_modules/jszip/lib/external.js +19 -0
- data/node_modules/jszip/lib/flate.js +85 -0
- data/node_modules/jszip/lib/generate/ZipFileWorker.js +540 -0
- data/node_modules/jszip/lib/generate/index.js +57 -0
- data/node_modules/jszip/lib/index.js +52 -0
- data/node_modules/jszip/lib/license_header.js +11 -0
- data/node_modules/jszip/lib/load.js +81 -0
- data/node_modules/jszip/lib/nodejs/NodejsStreamInputAdapter.js +74 -0
- data/node_modules/jszip/lib/nodejs/NodejsStreamOutputAdapter.js +42 -0
- data/node_modules/jszip/lib/nodejsUtils.js +57 -0
- data/node_modules/jszip/lib/object.js +389 -0
- data/node_modules/jszip/lib/readable-stream-browser.js +9 -0
- data/node_modules/jszip/lib/reader/ArrayReader.js +57 -0
- data/node_modules/jszip/lib/reader/DataReader.js +116 -0
- data/node_modules/jszip/lib/reader/NodeBufferReader.js +19 -0
- data/node_modules/jszip/lib/reader/StringReader.js +38 -0
- data/node_modules/jszip/lib/reader/Uint8ArrayReader.js +22 -0
- data/node_modules/jszip/lib/reader/readerFor.js +28 -0
- data/node_modules/jszip/lib/signature.js +7 -0
- data/node_modules/jszip/lib/stream/ConvertWorker.js +26 -0
- data/node_modules/jszip/lib/stream/Crc32Probe.js +24 -0
- data/node_modules/jszip/lib/stream/DataLengthProbe.js +29 -0
- data/node_modules/jszip/lib/stream/DataWorker.js +116 -0
- data/node_modules/jszip/lib/stream/GenericWorker.js +263 -0
- data/node_modules/jszip/lib/stream/StreamHelper.js +212 -0
- data/node_modules/jszip/lib/support.js +38 -0
- data/node_modules/jszip/lib/utf8.js +275 -0
- data/node_modules/jszip/lib/utils.js +476 -0
- data/node_modules/jszip/lib/zipEntries.js +262 -0
- data/node_modules/jszip/lib/zipEntry.js +294 -0
- data/node_modules/jszip/lib/zipObject.js +133 -0
- data/node_modules/jszip/package.json +63 -0
- data/node_modules/jszip/vendor/FileSaver.js +247 -0
- data/node_modules/lie/README.md +62 -0
- data/node_modules/lie/dist/lie.js +350 -0
- data/node_modules/lie/dist/lie.min.js +1 -0
- data/node_modules/lie/dist/lie.polyfill.js +358 -0
- data/node_modules/lie/dist/lie.polyfill.min.js +1 -0
- data/node_modules/lie/lib/browser.js +273 -0
- data/node_modules/lie/lib/index.js +298 -0
- data/node_modules/lie/license.md +7 -0
- data/node_modules/lie/lie.d.ts +244 -0
- data/node_modules/lie/package.json +69 -0
- data/node_modules/lie/polyfill.js +4 -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 +30 -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 +33 -0
- data/node_modules/pako/CHANGELOG.md +164 -0
- data/node_modules/pako/LICENSE +21 -0
- data/node_modules/pako/README.md +191 -0
- data/node_modules/pako/dist/pako.js +6818 -0
- data/node_modules/pako/dist/pako.min.js +1 -0
- data/node_modules/pako/dist/pako_deflate.js +3997 -0
- data/node_modules/pako/dist/pako_deflate.min.js +1 -0
- data/node_modules/pako/dist/pako_inflate.js +3300 -0
- data/node_modules/pako/dist/pako_inflate.min.js +1 -0
- data/node_modules/pako/index.js +14 -0
- data/node_modules/pako/lib/deflate.js +400 -0
- data/node_modules/pako/lib/inflate.js +423 -0
- data/node_modules/pako/lib/utils/common.js +105 -0
- data/node_modules/pako/lib/utils/strings.js +187 -0
- data/node_modules/pako/lib/zlib/README +59 -0
- data/node_modules/pako/lib/zlib/adler32.js +51 -0
- data/node_modules/pako/lib/zlib/constants.js +68 -0
- data/node_modules/pako/lib/zlib/crc32.js +59 -0
- data/node_modules/pako/lib/zlib/deflate.js +1874 -0
- data/node_modules/pako/lib/zlib/gzheader.js +58 -0
- data/node_modules/pako/lib/zlib/inffast.js +345 -0
- data/node_modules/pako/lib/zlib/inflate.js +1556 -0
- data/node_modules/pako/lib/zlib/inftrees.js +343 -0
- data/node_modules/pako/lib/zlib/messages.js +32 -0
- data/node_modules/pako/lib/zlib/trees.js +1222 -0
- data/node_modules/pako/lib/zlib/zstream.js +47 -0
- data/node_modules/pako/package.json +44 -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 +43 -0
- data/node_modules/path-is-absolute/readme.md +59 -0
- data/node_modules/process-nextick-args/index.js +45 -0
- data/node_modules/process-nextick-args/license.md +19 -0
- data/node_modules/process-nextick-args/package.json +25 -0
- data/node_modules/process-nextick-args/readme.md +18 -0
- data/node_modules/readable-stream/.travis.yml +34 -0
- data/node_modules/readable-stream/CONTRIBUTING.md +38 -0
- data/node_modules/readable-stream/GOVERNANCE.md +136 -0
- data/node_modules/readable-stream/LICENSE +47 -0
- data/node_modules/readable-stream/README.md +58 -0
- data/node_modules/readable-stream/doc/wg-meetings/2015-01-30.md +60 -0
- data/node_modules/readable-stream/duplex-browser.js +1 -0
- data/node_modules/readable-stream/duplex.js +1 -0
- data/node_modules/readable-stream/lib/_stream_duplex.js +131 -0
- data/node_modules/readable-stream/lib/_stream_passthrough.js +47 -0
- data/node_modules/readable-stream/lib/_stream_readable.js +1019 -0
- data/node_modules/readable-stream/lib/_stream_transform.js +214 -0
- data/node_modules/readable-stream/lib/_stream_writable.js +687 -0
- data/node_modules/readable-stream/lib/internal/streams/BufferList.js +79 -0
- data/node_modules/readable-stream/lib/internal/streams/destroy.js +74 -0
- data/node_modules/readable-stream/lib/internal/streams/stream-browser.js +1 -0
- data/node_modules/readable-stream/lib/internal/streams/stream.js +1 -0
- data/node_modules/readable-stream/package.json +52 -0
- data/node_modules/readable-stream/passthrough.js +1 -0
- data/node_modules/readable-stream/readable-browser.js +7 -0
- data/node_modules/readable-stream/readable.js +19 -0
- data/node_modules/readable-stream/transform.js +1 -0
- data/node_modules/readable-stream/writable-browser.js +1 -0
- data/node_modules/readable-stream/writable.js +8 -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 +29 -0
- data/node_modules/rimraf/rimraf.js +372 -0
- data/node_modules/safe-buffer/LICENSE +21 -0
- data/node_modules/safe-buffer/README.md +584 -0
- data/node_modules/safe-buffer/index.d.ts +187 -0
- data/node_modules/safe-buffer/index.js +62 -0
- data/node_modules/safe-buffer/package.json +37 -0
- data/node_modules/selenium-webdriver/CHANGES.md +1114 -0
- data/node_modules/selenium-webdriver/LICENSE +202 -0
- data/node_modules/selenium-webdriver/NOTICE +2 -0
- data/node_modules/selenium-webdriver/README.md +229 -0
- data/node_modules/selenium-webdriver/chrome.js +295 -0
- data/node_modules/selenium-webdriver/chromium.js +829 -0
- data/node_modules/selenium-webdriver/devtools/CDPConnection.js +35 -0
- data/node_modules/selenium-webdriver/edge.js +224 -0
- data/node_modules/selenium-webdriver/example/chrome_android.js +45 -0
- data/node_modules/selenium-webdriver/example/chrome_mobile_emulation.js +46 -0
- data/node_modules/selenium-webdriver/example/firefox_channels.js +84 -0
- data/node_modules/selenium-webdriver/example/google_search.js +50 -0
- data/node_modules/selenium-webdriver/example/google_search_test.js +70 -0
- data/node_modules/selenium-webdriver/example/headless.js +63 -0
- data/node_modules/selenium-webdriver/example/logging.js +64 -0
- data/node_modules/selenium-webdriver/firefox.js +789 -0
- data/node_modules/selenium-webdriver/http/index.js +324 -0
- data/node_modules/selenium-webdriver/http/util.js +172 -0
- data/node_modules/selenium-webdriver/ie.js +503 -0
- data/node_modules/selenium-webdriver/index.js +825 -0
- data/node_modules/selenium-webdriver/io/exec.js +162 -0
- data/node_modules/selenium-webdriver/io/index.js +348 -0
- data/node_modules/selenium-webdriver/io/zip.js +223 -0
- data/node_modules/selenium-webdriver/lib/atoms/find-elements.js +123 -0
- data/node_modules/selenium-webdriver/lib/atoms/get-attribute.js +101 -0
- data/node_modules/selenium-webdriver/lib/atoms/is-displayed.js +101 -0
- data/node_modules/selenium-webdriver/lib/atoms/mutation-listener.js +55 -0
- data/node_modules/selenium-webdriver/lib/by.js +415 -0
- data/node_modules/selenium-webdriver/lib/capabilities.js +553 -0
- data/node_modules/selenium-webdriver/lib/command.js +206 -0
- data/node_modules/selenium-webdriver/lib/error.js +605 -0
- data/node_modules/selenium-webdriver/lib/http.js +704 -0
- data/node_modules/selenium-webdriver/lib/input.js +946 -0
- data/node_modules/selenium-webdriver/lib/logging.js +661 -0
- data/node_modules/selenium-webdriver/lib/promise.js +285 -0
- data/node_modules/selenium-webdriver/lib/proxy.js +212 -0
- data/node_modules/selenium-webdriver/lib/session.js +77 -0
- data/node_modules/selenium-webdriver/lib/symbols.js +37 -0
- data/node_modules/selenium-webdriver/lib/until.js +429 -0
- data/node_modules/selenium-webdriver/lib/webdriver.js +2919 -0
- data/node_modules/selenium-webdriver/net/index.js +107 -0
- data/node_modules/selenium-webdriver/net/portprober.js +75 -0
- data/node_modules/selenium-webdriver/opera.js +406 -0
- data/node_modules/selenium-webdriver/package.json +54 -0
- data/node_modules/selenium-webdriver/proxy.js +32 -0
- data/node_modules/selenium-webdriver/remote/index.js +624 -0
- data/node_modules/selenium-webdriver/safari.js +168 -0
- data/node_modules/selenium-webdriver/testing/index.js +504 -0
- data/node_modules/set-immediate-shim/index.js +7 -0
- data/node_modules/set-immediate-shim/package.json +34 -0
- data/node_modules/set-immediate-shim/readme.md +31 -0
- data/node_modules/string_decoder/.travis.yml +50 -0
- data/node_modules/string_decoder/LICENSE +48 -0
- data/node_modules/string_decoder/README.md +47 -0
- data/node_modules/string_decoder/lib/string_decoder.js +296 -0
- data/node_modules/string_decoder/package.json +31 -0
- data/node_modules/tmp/CHANGELOG.md +288 -0
- data/node_modules/tmp/LICENSE +21 -0
- data/node_modules/tmp/README.md +365 -0
- data/node_modules/tmp/lib/tmp.js +780 -0
- data/node_modules/tmp/node_modules/.bin/rimraf +1 -0
- data/node_modules/tmp/node_modules/rimraf/CHANGELOG.md +65 -0
- data/node_modules/tmp/node_modules/rimraf/LICENSE +15 -0
- data/node_modules/tmp/node_modules/rimraf/README.md +101 -0
- data/node_modules/tmp/node_modules/rimraf/bin.js +68 -0
- data/node_modules/tmp/node_modules/rimraf/package.json +32 -0
- data/node_modules/tmp/node_modules/rimraf/rimraf.js +360 -0
- data/node_modules/tmp/package.json +58 -0
- data/node_modules/util-deprecate/History.md +16 -0
- data/node_modules/util-deprecate/LICENSE +24 -0
- data/node_modules/util-deprecate/README.md +53 -0
- data/node_modules/util-deprecate/browser.js +67 -0
- data/node_modules/util-deprecate/node.js +6 -0
- data/node_modules/util-deprecate/package.json +27 -0
- data/node_modules/wrappy/LICENSE +15 -0
- data/node_modules/wrappy/README.md +36 -0
- data/node_modules/wrappy/package.json +29 -0
- data/node_modules/wrappy/wrappy.js +33 -0
- data/node_modules/ws/LICENSE +21 -0
- data/node_modules/ws/README.md +496 -0
- data/node_modules/ws/browser.js +8 -0
- data/node_modules/ws/index.js +10 -0
- data/node_modules/ws/lib/buffer-util.js +129 -0
- data/node_modules/ws/lib/constants.js +10 -0
- data/node_modules/ws/lib/event-target.js +184 -0
- data/node_modules/ws/lib/extension.js +223 -0
- data/node_modules/ws/lib/limiter.js +55 -0
- data/node_modules/ws/lib/permessage-deflate.js +517 -0
- data/node_modules/ws/lib/receiver.js +507 -0
- data/node_modules/ws/lib/sender.js +405 -0
- data/node_modules/ws/lib/stream.js +165 -0
- data/node_modules/ws/lib/validation.js +104 -0
- data/node_modules/ws/lib/websocket-server.js +418 -0
- data/node_modules/ws/lib/websocket.js +942 -0
- data/node_modules/ws/package.json +56 -0
- data/package-lock.json +458 -0
- data/package.json +5 -0
- data/selenium.log +1 -0
- metadata +314 -19
@@ -0,0 +1,405 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const { randomFillSync } = require('crypto');
|
4
|
+
|
5
|
+
const PerMessageDeflate = require('./permessage-deflate');
|
6
|
+
const { EMPTY_BUFFER } = require('./constants');
|
7
|
+
const { isValidStatusCode } = require('./validation');
|
8
|
+
const { mask: applyMask, toBuffer } = require('./buffer-util');
|
9
|
+
|
10
|
+
const mask = Buffer.alloc(4);
|
11
|
+
|
12
|
+
/**
|
13
|
+
* HyBi Sender implementation.
|
14
|
+
*/
|
15
|
+
class Sender {
|
16
|
+
/**
|
17
|
+
* Creates a Sender instance.
|
18
|
+
*
|
19
|
+
* @param {net.Socket} socket The connection socket
|
20
|
+
* @param {Object} [extensions] An object containing the negotiated extensions
|
21
|
+
*/
|
22
|
+
constructor(socket, extensions) {
|
23
|
+
this._extensions = extensions || {};
|
24
|
+
this._socket = socket;
|
25
|
+
|
26
|
+
this._firstFragment = true;
|
27
|
+
this._compress = false;
|
28
|
+
|
29
|
+
this._bufferedBytes = 0;
|
30
|
+
this._deflating = false;
|
31
|
+
this._queue = [];
|
32
|
+
}
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Frames a piece of data according to the HyBi WebSocket protocol.
|
36
|
+
*
|
37
|
+
* @param {Buffer} data The data to frame
|
38
|
+
* @param {Object} options Options object
|
39
|
+
* @param {Number} options.opcode The opcode
|
40
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
41
|
+
* modified
|
42
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
43
|
+
* FIN bit
|
44
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
45
|
+
* `data`
|
46
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
47
|
+
* RSV1 bit
|
48
|
+
* @return {Buffer[]} The framed data as a list of `Buffer` instances
|
49
|
+
* @public
|
50
|
+
*/
|
51
|
+
static frame(data, options) {
|
52
|
+
const merge = options.mask && options.readOnly;
|
53
|
+
let offset = options.mask ? 6 : 2;
|
54
|
+
let payloadLength = data.length;
|
55
|
+
|
56
|
+
if (data.length >= 65536) {
|
57
|
+
offset += 8;
|
58
|
+
payloadLength = 127;
|
59
|
+
} else if (data.length > 125) {
|
60
|
+
offset += 2;
|
61
|
+
payloadLength = 126;
|
62
|
+
}
|
63
|
+
|
64
|
+
const target = Buffer.allocUnsafe(merge ? data.length + offset : offset);
|
65
|
+
|
66
|
+
target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
|
67
|
+
if (options.rsv1) target[0] |= 0x40;
|
68
|
+
|
69
|
+
target[1] = payloadLength;
|
70
|
+
|
71
|
+
if (payloadLength === 126) {
|
72
|
+
target.writeUInt16BE(data.length, 2);
|
73
|
+
} else if (payloadLength === 127) {
|
74
|
+
target.writeUInt32BE(0, 2);
|
75
|
+
target.writeUInt32BE(data.length, 6);
|
76
|
+
}
|
77
|
+
|
78
|
+
if (!options.mask) return [target, data];
|
79
|
+
|
80
|
+
randomFillSync(mask, 0, 4);
|
81
|
+
|
82
|
+
target[1] |= 0x80;
|
83
|
+
target[offset - 4] = mask[0];
|
84
|
+
target[offset - 3] = mask[1];
|
85
|
+
target[offset - 2] = mask[2];
|
86
|
+
target[offset - 1] = mask[3];
|
87
|
+
|
88
|
+
if (merge) {
|
89
|
+
applyMask(data, mask, target, offset, data.length);
|
90
|
+
return [target];
|
91
|
+
}
|
92
|
+
|
93
|
+
applyMask(data, mask, data, 0, data.length);
|
94
|
+
return [target, data];
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Sends a close message to the other peer.
|
99
|
+
*
|
100
|
+
* @param {Number} [code] The status code component of the body
|
101
|
+
* @param {String} [data] The message component of the body
|
102
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
|
103
|
+
* @param {Function} [cb] Callback
|
104
|
+
* @public
|
105
|
+
*/
|
106
|
+
close(code, data, mask, cb) {
|
107
|
+
let buf;
|
108
|
+
|
109
|
+
if (code === undefined) {
|
110
|
+
buf = EMPTY_BUFFER;
|
111
|
+
} else if (typeof code !== 'number' || !isValidStatusCode(code)) {
|
112
|
+
throw new TypeError('First argument must be a valid error code number');
|
113
|
+
} else if (data === undefined || data === '') {
|
114
|
+
buf = Buffer.allocUnsafe(2);
|
115
|
+
buf.writeUInt16BE(code, 0);
|
116
|
+
} else {
|
117
|
+
const length = Buffer.byteLength(data);
|
118
|
+
|
119
|
+
if (length > 123) {
|
120
|
+
throw new RangeError('The message must not be greater than 123 bytes');
|
121
|
+
}
|
122
|
+
|
123
|
+
buf = Buffer.allocUnsafe(2 + length);
|
124
|
+
buf.writeUInt16BE(code, 0);
|
125
|
+
buf.write(data, 2);
|
126
|
+
}
|
127
|
+
|
128
|
+
if (this._deflating) {
|
129
|
+
this.enqueue([this.doClose, buf, mask, cb]);
|
130
|
+
} else {
|
131
|
+
this.doClose(buf, mask, cb);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
/**
|
136
|
+
* Frames and sends a close message.
|
137
|
+
*
|
138
|
+
* @param {Buffer} data The message to send
|
139
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
140
|
+
* @param {Function} [cb] Callback
|
141
|
+
* @private
|
142
|
+
*/
|
143
|
+
doClose(data, mask, cb) {
|
144
|
+
this.sendFrame(
|
145
|
+
Sender.frame(data, {
|
146
|
+
fin: true,
|
147
|
+
rsv1: false,
|
148
|
+
opcode: 0x08,
|
149
|
+
mask,
|
150
|
+
readOnly: false
|
151
|
+
}),
|
152
|
+
cb
|
153
|
+
);
|
154
|
+
}
|
155
|
+
|
156
|
+
/**
|
157
|
+
* Sends a ping message to the other peer.
|
158
|
+
*
|
159
|
+
* @param {*} data The message to send
|
160
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
161
|
+
* @param {Function} [cb] Callback
|
162
|
+
* @public
|
163
|
+
*/
|
164
|
+
ping(data, mask, cb) {
|
165
|
+
const buf = toBuffer(data);
|
166
|
+
|
167
|
+
if (buf.length > 125) {
|
168
|
+
throw new RangeError('The data size must not be greater than 125 bytes');
|
169
|
+
}
|
170
|
+
|
171
|
+
if (this._deflating) {
|
172
|
+
this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
|
173
|
+
} else {
|
174
|
+
this.doPing(buf, mask, toBuffer.readOnly, cb);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
|
178
|
+
/**
|
179
|
+
* Frames and sends a ping message.
|
180
|
+
*
|
181
|
+
* @param {Buffer} data The message to send
|
182
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
183
|
+
* @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
|
184
|
+
* @param {Function} [cb] Callback
|
185
|
+
* @private
|
186
|
+
*/
|
187
|
+
doPing(data, mask, readOnly, cb) {
|
188
|
+
this.sendFrame(
|
189
|
+
Sender.frame(data, {
|
190
|
+
fin: true,
|
191
|
+
rsv1: false,
|
192
|
+
opcode: 0x09,
|
193
|
+
mask,
|
194
|
+
readOnly
|
195
|
+
}),
|
196
|
+
cb
|
197
|
+
);
|
198
|
+
}
|
199
|
+
|
200
|
+
/**
|
201
|
+
* Sends a pong message to the other peer.
|
202
|
+
*
|
203
|
+
* @param {*} data The message to send
|
204
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
205
|
+
* @param {Function} [cb] Callback
|
206
|
+
* @public
|
207
|
+
*/
|
208
|
+
pong(data, mask, cb) {
|
209
|
+
const buf = toBuffer(data);
|
210
|
+
|
211
|
+
if (buf.length > 125) {
|
212
|
+
throw new RangeError('The data size must not be greater than 125 bytes');
|
213
|
+
}
|
214
|
+
|
215
|
+
if (this._deflating) {
|
216
|
+
this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
|
217
|
+
} else {
|
218
|
+
this.doPong(buf, mask, toBuffer.readOnly, cb);
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
/**
|
223
|
+
* Frames and sends a pong message.
|
224
|
+
*
|
225
|
+
* @param {Buffer} data The message to send
|
226
|
+
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
227
|
+
* @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
|
228
|
+
* @param {Function} [cb] Callback
|
229
|
+
* @private
|
230
|
+
*/
|
231
|
+
doPong(data, mask, readOnly, cb) {
|
232
|
+
this.sendFrame(
|
233
|
+
Sender.frame(data, {
|
234
|
+
fin: true,
|
235
|
+
rsv1: false,
|
236
|
+
opcode: 0x0a,
|
237
|
+
mask,
|
238
|
+
readOnly
|
239
|
+
}),
|
240
|
+
cb
|
241
|
+
);
|
242
|
+
}
|
243
|
+
|
244
|
+
/**
|
245
|
+
* Sends a data message to the other peer.
|
246
|
+
*
|
247
|
+
* @param {*} data The message to send
|
248
|
+
* @param {Object} options Options object
|
249
|
+
* @param {Boolean} [options.compress=false] Specifies whether or not to
|
250
|
+
* compress `data`
|
251
|
+
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
|
252
|
+
* or text
|
253
|
+
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
|
254
|
+
* last one
|
255
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
256
|
+
* `data`
|
257
|
+
* @param {Function} [cb] Callback
|
258
|
+
* @public
|
259
|
+
*/
|
260
|
+
send(data, options, cb) {
|
261
|
+
const buf = toBuffer(data);
|
262
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
263
|
+
let opcode = options.binary ? 2 : 1;
|
264
|
+
let rsv1 = options.compress;
|
265
|
+
|
266
|
+
if (this._firstFragment) {
|
267
|
+
this._firstFragment = false;
|
268
|
+
if (rsv1 && perMessageDeflate) {
|
269
|
+
rsv1 = buf.length >= perMessageDeflate._threshold;
|
270
|
+
}
|
271
|
+
this._compress = rsv1;
|
272
|
+
} else {
|
273
|
+
rsv1 = false;
|
274
|
+
opcode = 0;
|
275
|
+
}
|
276
|
+
|
277
|
+
if (options.fin) this._firstFragment = true;
|
278
|
+
|
279
|
+
if (perMessageDeflate) {
|
280
|
+
const opts = {
|
281
|
+
fin: options.fin,
|
282
|
+
rsv1,
|
283
|
+
opcode,
|
284
|
+
mask: options.mask,
|
285
|
+
readOnly: toBuffer.readOnly
|
286
|
+
};
|
287
|
+
|
288
|
+
if (this._deflating) {
|
289
|
+
this.enqueue([this.dispatch, buf, this._compress, opts, cb]);
|
290
|
+
} else {
|
291
|
+
this.dispatch(buf, this._compress, opts, cb);
|
292
|
+
}
|
293
|
+
} else {
|
294
|
+
this.sendFrame(
|
295
|
+
Sender.frame(buf, {
|
296
|
+
fin: options.fin,
|
297
|
+
rsv1: false,
|
298
|
+
opcode,
|
299
|
+
mask: options.mask,
|
300
|
+
readOnly: toBuffer.readOnly
|
301
|
+
}),
|
302
|
+
cb
|
303
|
+
);
|
304
|
+
}
|
305
|
+
}
|
306
|
+
|
307
|
+
/**
|
308
|
+
* Dispatches a data message.
|
309
|
+
*
|
310
|
+
* @param {Buffer} data The message to send
|
311
|
+
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
312
|
+
* `data`
|
313
|
+
* @param {Object} options Options object
|
314
|
+
* @param {Number} options.opcode The opcode
|
315
|
+
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
316
|
+
* modified
|
317
|
+
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
318
|
+
* FIN bit
|
319
|
+
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
320
|
+
* `data`
|
321
|
+
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
322
|
+
* RSV1 bit
|
323
|
+
* @param {Function} [cb] Callback
|
324
|
+
* @private
|
325
|
+
*/
|
326
|
+
dispatch(data, compress, options, cb) {
|
327
|
+
if (!compress) {
|
328
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
329
|
+
return;
|
330
|
+
}
|
331
|
+
|
332
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
333
|
+
|
334
|
+
this._bufferedBytes += data.length;
|
335
|
+
this._deflating = true;
|
336
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
337
|
+
if (this._socket.destroyed) {
|
338
|
+
const err = new Error(
|
339
|
+
'The socket was closed while data was being compressed'
|
340
|
+
);
|
341
|
+
|
342
|
+
if (typeof cb === 'function') cb(err);
|
343
|
+
|
344
|
+
for (let i = 0; i < this._queue.length; i++) {
|
345
|
+
const callback = this._queue[i][4];
|
346
|
+
|
347
|
+
if (typeof callback === 'function') callback(err);
|
348
|
+
}
|
349
|
+
|
350
|
+
return;
|
351
|
+
}
|
352
|
+
|
353
|
+
this._bufferedBytes -= data.length;
|
354
|
+
this._deflating = false;
|
355
|
+
options.readOnly = false;
|
356
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
357
|
+
this.dequeue();
|
358
|
+
});
|
359
|
+
}
|
360
|
+
|
361
|
+
/**
|
362
|
+
* Executes queued send operations.
|
363
|
+
*
|
364
|
+
* @private
|
365
|
+
*/
|
366
|
+
dequeue() {
|
367
|
+
while (!this._deflating && this._queue.length) {
|
368
|
+
const params = this._queue.shift();
|
369
|
+
|
370
|
+
this._bufferedBytes -= params[1].length;
|
371
|
+
Reflect.apply(params[0], this, params.slice(1));
|
372
|
+
}
|
373
|
+
}
|
374
|
+
|
375
|
+
/**
|
376
|
+
* Enqueues a send operation.
|
377
|
+
*
|
378
|
+
* @param {Array} params Send operation parameters.
|
379
|
+
* @private
|
380
|
+
*/
|
381
|
+
enqueue(params) {
|
382
|
+
this._bufferedBytes += params[1].length;
|
383
|
+
this._queue.push(params);
|
384
|
+
}
|
385
|
+
|
386
|
+
/**
|
387
|
+
* Sends a frame.
|
388
|
+
*
|
389
|
+
* @param {Buffer[]} list The frame to send
|
390
|
+
* @param {Function} [cb] Callback
|
391
|
+
* @private
|
392
|
+
*/
|
393
|
+
sendFrame(list, cb) {
|
394
|
+
if (list.length === 2) {
|
395
|
+
this._socket.cork();
|
396
|
+
this._socket.write(list[0]);
|
397
|
+
this._socket.write(list[1], cb);
|
398
|
+
this._socket.uncork();
|
399
|
+
} else {
|
400
|
+
this._socket.write(list[0], cb);
|
401
|
+
}
|
402
|
+
}
|
403
|
+
}
|
404
|
+
|
405
|
+
module.exports = Sender;
|
@@ -0,0 +1,165 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const { Duplex } = require('stream');
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Emits the `'close'` event on a stream.
|
7
|
+
*
|
8
|
+
* @param {stream.Duplex} 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 {stream.Duplex} The duplex stream
|
47
|
+
* @public
|
48
|
+
*/
|
49
|
+
function createWebSocketStream(ws, options) {
|
50
|
+
let resumeOnReceiverDrain = true;
|
51
|
+
|
52
|
+
function receiverOnDrain() {
|
53
|
+
if (resumeOnReceiverDrain) ws._socket.resume();
|
54
|
+
}
|
55
|
+
|
56
|
+
if (ws.readyState === ws.CONNECTING) {
|
57
|
+
ws.once('open', function open() {
|
58
|
+
ws._receiver.removeAllListeners('drain');
|
59
|
+
ws._receiver.on('drain', receiverOnDrain);
|
60
|
+
});
|
61
|
+
} else {
|
62
|
+
ws._receiver.removeAllListeners('drain');
|
63
|
+
ws._receiver.on('drain', receiverOnDrain);
|
64
|
+
}
|
65
|
+
|
66
|
+
const duplex = new Duplex({
|
67
|
+
...options,
|
68
|
+
autoDestroy: false,
|
69
|
+
emitClose: false,
|
70
|
+
objectMode: false,
|
71
|
+
writableObjectMode: false
|
72
|
+
});
|
73
|
+
|
74
|
+
ws.on('message', function message(msg) {
|
75
|
+
if (!duplex.push(msg)) {
|
76
|
+
resumeOnReceiverDrain = false;
|
77
|
+
ws._socket.pause();
|
78
|
+
}
|
79
|
+
});
|
80
|
+
|
81
|
+
ws.once('error', function error(err) {
|
82
|
+
if (duplex.destroyed) return;
|
83
|
+
|
84
|
+
duplex.destroy(err);
|
85
|
+
});
|
86
|
+
|
87
|
+
ws.once('close', function close() {
|
88
|
+
if (duplex.destroyed) return;
|
89
|
+
|
90
|
+
duplex.push(null);
|
91
|
+
});
|
92
|
+
|
93
|
+
duplex._destroy = function (err, callback) {
|
94
|
+
if (ws.readyState === ws.CLOSED) {
|
95
|
+
callback(err);
|
96
|
+
process.nextTick(emitClose, duplex);
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
|
100
|
+
let called = false;
|
101
|
+
|
102
|
+
ws.once('error', function error(err) {
|
103
|
+
called = true;
|
104
|
+
callback(err);
|
105
|
+
});
|
106
|
+
|
107
|
+
ws.once('close', function close() {
|
108
|
+
if (!called) callback(err);
|
109
|
+
process.nextTick(emitClose, duplex);
|
110
|
+
});
|
111
|
+
ws.terminate();
|
112
|
+
};
|
113
|
+
|
114
|
+
duplex._final = function (callback) {
|
115
|
+
if (ws.readyState === ws.CONNECTING) {
|
116
|
+
ws.once('open', function open() {
|
117
|
+
duplex._final(callback);
|
118
|
+
});
|
119
|
+
return;
|
120
|
+
}
|
121
|
+
|
122
|
+
// If the value of the `_socket` property is `null` it means that `ws` is a
|
123
|
+
// client websocket and the handshake failed. In fact, when this happens, a
|
124
|
+
// socket is never assigned to the websocket. Wait for the `'error'` event
|
125
|
+
// that will be emitted by the websocket.
|
126
|
+
if (ws._socket === null) return;
|
127
|
+
|
128
|
+
if (ws._socket._writableState.finished) {
|
129
|
+
callback();
|
130
|
+
if (duplex._readableState.endEmitted) duplex.destroy();
|
131
|
+
} else {
|
132
|
+
ws._socket.once('finish', function finish() {
|
133
|
+
// `duplex` is not destroyed here because the `'end'` event will be
|
134
|
+
// emitted on `duplex` after this `'finish'` event. The EOF signaling
|
135
|
+
// `null` chunk is, in fact, pushed when the websocket emits `'close'`.
|
136
|
+
callback();
|
137
|
+
});
|
138
|
+
ws.close();
|
139
|
+
}
|
140
|
+
};
|
141
|
+
|
142
|
+
duplex._read = function () {
|
143
|
+
if (ws.readyState === ws.OPEN && !resumeOnReceiverDrain) {
|
144
|
+
resumeOnReceiverDrain = true;
|
145
|
+
if (!ws._receiver._writableState.needDrain) ws._socket.resume();
|
146
|
+
}
|
147
|
+
};
|
148
|
+
|
149
|
+
duplex._write = function (chunk, encoding, callback) {
|
150
|
+
if (ws.readyState === ws.CONNECTING) {
|
151
|
+
ws.once('open', function open() {
|
152
|
+
duplex._write(chunk, encoding, callback);
|
153
|
+
});
|
154
|
+
return;
|
155
|
+
}
|
156
|
+
|
157
|
+
ws.send(chunk, callback);
|
158
|
+
};
|
159
|
+
|
160
|
+
duplex.on('end', duplexOnEnd);
|
161
|
+
duplex.on('error', duplexOnError);
|
162
|
+
return duplex;
|
163
|
+
}
|
164
|
+
|
165
|
+
module.exports = createWebSocketStream;
|