herdr-remote 0.2.39 → 0.3.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/README.md +4 -2
  2. package/README.zh-CN.md +2 -2
  3. package/dist/chunks/3FQAXVGC.js +3 -0
  4. package/dist/chunks/42KARAL4.js +31 -0
  5. package/dist/chunks/44IGVPMA.js +5 -0
  6. package/dist/chunks/5LZFJ6UZ.js +3 -0
  7. package/dist/chunks/7SMZ43LI.js +9 -0
  8. package/dist/chunks/AX4EPJCP.js +4 -0
  9. package/dist/chunks/B76DFI4H.js +16 -0
  10. package/dist/chunks/BYGDPZZD.js +3 -0
  11. package/dist/chunks/C2UUZY6J.js +2 -0
  12. package/dist/chunks/CVJ4HBMP.js +3 -0
  13. package/dist/chunks/DGUZCGQN.js +3 -0
  14. package/dist/chunks/FONUO5O5.js +4 -0
  15. package/dist/chunks/H24ZP5D5.js +16 -0
  16. package/dist/chunks/I6NRCYLI.js +3 -0
  17. package/dist/chunks/JJGEV4EZ.js +3 -0
  18. package/dist/chunks/K5RFPHSF.js +5 -0
  19. package/dist/chunks/KGOQKOHH.js +5 -0
  20. package/dist/chunks/KRVF37OW.js +3 -0
  21. package/dist/chunks/KUDCC7ZO.js +6 -0
  22. package/dist/chunks/LPR662JH.js +77 -0
  23. package/dist/chunks/LY2ZGCXN.js +4 -0
  24. package/dist/chunks/M52LJEES.js +6 -0
  25. package/dist/chunks/MCUVFSSK.js +3 -0
  26. package/dist/chunks/MEFDV375.js +10 -0
  27. package/dist/chunks/MKAEXKXG.js +10 -0
  28. package/dist/chunks/O2LNVFUT.js +3 -0
  29. package/dist/chunks/PBMBFWLQ.js +3 -0
  30. package/dist/chunks/PBRQ3HEG.js +3 -0
  31. package/dist/chunks/QCSILMFB.js +3 -0
  32. package/dist/chunks/QRGNEKME.js +5 -0
  33. package/dist/chunks/RMHH3IAX.js +63 -0
  34. package/dist/chunks/TNLCNVA2.js +3 -0
  35. package/dist/chunks/U6QAYG3E.js +3 -0
  36. package/dist/chunks/VD54DF7R.js +45 -0
  37. package/dist/chunks/WY7WHEJ4.js +3 -0
  38. package/dist/cli.js +21 -294
  39. package/dist/connector/admin-broker.js +3 -303
  40. package/dist/connector/main.js +20 -18
  41. package/dist/harfbuzz-subset.wasm +0 -0
  42. package/dist/postinstall.js +17 -0
  43. package/herdr-plugin.toml +1 -1
  44. package/node_modules/herdr-remote-relay/bin/herdr-remote-relay.js +33 -92
  45. package/node_modules/herdr-remote-relay/package.json +3 -46
  46. package/package.json +8 -27
  47. package/dist/agent-status.js +0 -130
  48. package/dist/binary-plist.js +0 -157
  49. package/dist/config.js +0 -278
  50. package/dist/connector/admin-broker-setup.js +0 -40
  51. package/dist/connector/admin-shell.js +0 -67
  52. package/dist/connector/admin-tab.js +0 -90
  53. package/dist/connector/adminBrokerEndpoint.js +0 -68
  54. package/dist/connector/agent-watch.js +0 -123
  55. package/dist/connector/elevatedPty.js +0 -141
  56. package/dist/connector/font-server.js +0 -165
  57. package/dist/connector/herdr-prefix-keys.js +0 -28
  58. package/dist/connector/host-connector.js +0 -433
  59. package/dist/connector/host-hello.js +0 -32
  60. package/dist/connector/lock.js +0 -73
  61. package/dist/connector/sessions.js +0 -381
  62. package/dist/connector/update-report.js +0 -70
  63. package/dist/connector/wire.js +0 -42
  64. package/dist/exit-codes.js +0 -13
  65. package/dist/font-subset.js +0 -116
  66. package/dist/herdr-api.js +0 -254
  67. package/dist/herdr-command.js +0 -245
  68. package/dist/herdr-plugin.js +0 -120
  69. package/dist/herdr-server.js +0 -190
  70. package/dist/i18n/en.js +0 -257
  71. package/dist/i18n/index.js +0 -48
  72. package/dist/i18n/zh.js +0 -256
  73. package/dist/keepalive/detached.js +0 -97
  74. package/dist/keepalive/environment.js +0 -44
  75. package/dist/keepalive/index.js +0 -129
  76. package/dist/keepalive/launchd.js +0 -117
  77. package/dist/keepalive/systemd.js +0 -112
  78. package/dist/keepalive/types.js +0 -1
  79. package/dist/keepalive/windows-service-setup.js +0 -138
  80. package/dist/keepalive/windows-service.js +0 -272
  81. package/dist/keepalive/windows-task.js +0 -191
  82. package/dist/lib/process.js +0 -132
  83. package/dist/lifecycle.js +0 -79
  84. package/dist/net-interfaces.js +0 -82
  85. package/dist/node-pty-loader.js +0 -80
  86. package/dist/pasted-files.js +0 -115
  87. package/dist/paths.js +0 -74
  88. package/dist/pty-session.js +0 -133
  89. package/dist/relay-client.js +0 -106
  90. package/dist/relay-urls.js +0 -99
  91. package/dist/runtime.js +0 -107
  92. package/dist/service.js +0 -267
  93. package/dist/settings-model.js +0 -298
  94. package/dist/socket-discovery.js +0 -70
  95. package/dist/supervisor.js +0 -323
  96. package/dist/terminal-font/deps.js +0 -83
  97. package/dist/terminal-font/files.js +0 -357
  98. package/dist/terminal-font/formats.js +0 -355
  99. package/dist/terminal-font/index.js +0 -187
  100. package/dist/terminal-font/readers.js +0 -373
  101. package/dist/terminal-font/terminals.js +0 -147
  102. package/dist/terminal-palette.js +0 -399
  103. package/dist/tui/App.js +0 -199
  104. package/dist/tui/api.js +0 -91
  105. package/dist/tui/components/TextField.js +0 -50
  106. package/dist/tui/components/common.js +0 -80
  107. package/dist/tui/index.js +0 -36
  108. package/dist/tui/mouse/index.js +0 -164
  109. package/dist/tui/mouse/protocol.js +0 -96
  110. package/dist/tui/mouse/source.js +0 -79
  111. package/dist/tui/screens/About.js +0 -172
  112. package/dist/tui/screens/Herdr.js +0 -75
  113. package/dist/tui/screens/Keepalive.js +0 -141
  114. package/dist/tui/screens/Overview.js +0 -36
  115. package/dist/tui/screens/Pair.js +0 -74
  116. package/dist/tui/screens/Relay.js +0 -243
  117. package/dist/tui/screens/Services.js +0 -63
  118. package/dist/tui/screens/Wizard.js +0 -241
  119. package/dist/tui/terminal.js +0 -39
  120. package/dist/tui/theme.js +0 -33
  121. package/dist/update-command.js +0 -118
  122. package/dist/updater/in-place.js +0 -83
  123. package/dist/updater/npm.js +0 -62
  124. package/dist/updater.js +0 -401
  125. package/node_modules/herdr-remote-relay/README.md +0 -76
  126. package/node_modules/herdr-remote-relay/README.zh-CN.md +0 -76
  127. package/node_modules/herdr-remote-relay/config.example.json +0 -18
  128. package/node_modules/herdr-remote-relay/deploy/cloudflared/config.yml.example +0 -9
  129. package/node_modules/herdr-remote-relay/deploy/docker/compose.yaml +0 -23
  130. package/node_modules/herdr-remote-relay/deploy/nginx/herdr-remote-relay.conf.example +0 -49
  131. package/node_modules/herdr-remote-relay/deploy/systemd/herdr-remote-relay.service +0 -41
  132. package/node_modules/herdr-remote-relay/deploy/systemd/relay.env.example +0 -25
  133. package/node_modules/herdr-remote-relay/dist/auth-store.d.ts +0 -114
  134. package/node_modules/herdr-remote-relay/dist/auth-store.js +0 -289
  135. package/node_modules/herdr-remote-relay/dist/metrics.d.ts +0 -76
  136. package/node_modules/herdr-remote-relay/dist/metrics.js +0 -249
  137. package/node_modules/herdr-remote-relay/dist/protocol/frames.d.ts +0 -27
  138. package/node_modules/herdr-remote-relay/dist/protocol/frames.js +0 -110
  139. package/node_modules/herdr-remote-relay/dist/protocol/herdr-keys.d.ts +0 -8
  140. package/node_modules/herdr-remote-relay/dist/protocol/herdr-keys.js +0 -34
  141. package/node_modules/herdr-remote-relay/dist/protocol/http.d.ts +0 -165
  142. package/node_modules/herdr-remote-relay/dist/protocol/http.js +0 -3
  143. package/node_modules/herdr-remote-relay/dist/protocol/index.d.ts +0 -7
  144. package/node_modules/herdr-remote-relay/dist/protocol/index.js +0 -9
  145. package/node_modules/herdr-remote-relay/dist/protocol/messages.d.ts +0 -362
  146. package/node_modules/herdr-remote-relay/dist/protocol/messages.js +0 -20
  147. package/node_modules/herdr-remote-relay/dist/protocol/paste.d.ts +0 -14
  148. package/node_modules/herdr-remote-relay/dist/protocol/paste.js +0 -50
  149. package/node_modules/herdr-remote-relay/dist/protocol/release.d.ts +0 -8
  150. package/node_modules/herdr-remote-relay/dist/protocol/release.js +0 -17
  151. package/node_modules/herdr-remote-relay/dist/protocol/terminal.d.ts +0 -79
  152. package/node_modules/herdr-remote-relay/dist/protocol/terminal.js +0 -150
  153. package/node_modules/herdr-remote-relay/dist/relay-config.d.ts +0 -74
  154. package/node_modules/herdr-remote-relay/dist/relay-config.js +0 -316
  155. package/node_modules/herdr-remote-relay/dist/relay-server.d.ts +0 -67
  156. package/node_modules/herdr-remote-relay/dist/relay-server.js +0 -196
  157. package/node_modules/herdr-remote-relay/dist/scroll-input.d.ts +0 -17
  158. package/node_modules/herdr-remote-relay/dist/scroll-input.js +0 -124
  159. package/node_modules/herdr-remote-relay/dist/server/client-channel.d.ts +0 -3
  160. package/node_modules/herdr-remote-relay/dist/server/client-channel.js +0 -396
  161. package/node_modules/herdr-remote-relay/dist/server/font-proxy.d.ts +0 -19
  162. package/node_modules/herdr-remote-relay/dist/server/font-proxy.js +0 -155
  163. package/node_modules/herdr-remote-relay/dist/server/host-broadcasts.d.ts +0 -27
  164. package/node_modules/herdr-remote-relay/dist/server/host-broadcasts.js +0 -83
  165. package/node_modules/herdr-remote-relay/dist/server/host-channel.d.ts +0 -3
  166. package/node_modules/herdr-remote-relay/dist/server/host-channel.js +0 -271
  167. package/node_modules/herdr-remote-relay/dist/server/http-api.d.ts +0 -4
  168. package/node_modules/herdr-remote-relay/dist/server/http-api.js +0 -219
  169. package/node_modules/herdr-remote-relay/dist/server/maintenance.d.ts +0 -5
  170. package/node_modules/herdr-remote-relay/dist/server/maintenance.js +0 -64
  171. package/node_modules/herdr-remote-relay/dist/server/requests.d.ts +0 -34
  172. package/node_modules/herdr-remote-relay/dist/server/requests.js +0 -142
  173. package/node_modules/herdr-remote-relay/dist/server/sessions.d.ts +0 -36
  174. package/node_modules/herdr-remote-relay/dist/server/sessions.js +0 -230
  175. package/node_modules/herdr-remote-relay/dist/server/sockets.d.ts +0 -12
  176. package/node_modules/herdr-remote-relay/dist/server/sockets.js +0 -64
  177. package/node_modules/herdr-remote-relay/dist/server/status.d.ts +0 -11
  178. package/node_modules/herdr-remote-relay/dist/server/status.js +0 -99
  179. package/node_modules/herdr-remote-relay/dist/server/transport.d.ts +0 -16
  180. package/node_modules/herdr-remote-relay/dist/server/transport.js +0 -110
  181. package/node_modules/herdr-remote-relay/dist/server/types.d.ts +0 -133
  182. package/node_modules/herdr-remote-relay/dist/server/types.js +0 -3
  183. package/node_modules/herdr-remote-relay/dist/server/version.d.ts +0 -2
  184. package/node_modules/herdr-remote-relay/dist/server/version.js +0 -5
  185. package/node_modules/herdr-remote-relay/dist/state.d.ts +0 -6
  186. package/node_modules/herdr-remote-relay/dist/state.js +0 -47
  187. package/node_modules/ws/LICENSE +0 -20
  188. package/node_modules/ws/README.md +0 -548
  189. package/node_modules/ws/browser.js +0 -8
  190. package/node_modules/ws/index.js +0 -22
  191. package/node_modules/ws/lib/buffer-util.js +0 -131
  192. package/node_modules/ws/lib/constants.js +0 -19
  193. package/node_modules/ws/lib/event-target.js +0 -292
  194. package/node_modules/ws/lib/extension.js +0 -203
  195. package/node_modules/ws/lib/limiter.js +0 -55
  196. package/node_modules/ws/lib/permessage-deflate.js +0 -530
  197. package/node_modules/ws/lib/receiver.js +0 -743
  198. package/node_modules/ws/lib/sender.js +0 -607
  199. package/node_modules/ws/lib/stream.js +0 -161
  200. package/node_modules/ws/lib/subprotocol.js +0 -62
  201. package/node_modules/ws/lib/validation.js +0 -152
  202. package/node_modules/ws/lib/websocket-server.js +0 -562
  203. package/node_modules/ws/lib/websocket.js +0 -1407
  204. package/node_modules/ws/package.json +0 -74
  205. package/node_modules/ws/wrapper.mjs +0 -21
@@ -1,607 +0,0 @@
1
- /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex" }] */
2
-
3
- 'use strict';
4
-
5
- const { Duplex } = require('stream');
6
- const { randomFillSync } = require('crypto');
7
- const {
8
- types: { isUint8Array }
9
- } = require('util');
10
-
11
- const PerMessageDeflate = require('./permessage-deflate');
12
- const { EMPTY_BUFFER, kWebSocket, NOOP } = require('./constants');
13
- const { isBlob, isValidStatusCode } = require('./validation');
14
- const { mask: applyMask, toBuffer } = require('./buffer-util');
15
-
16
- const kByteLength = Symbol('kByteLength');
17
- const maskBuffer = Buffer.alloc(4);
18
- const RANDOM_POOL_SIZE = 8 * 1024;
19
- let randomPool;
20
- let randomPoolPointer = RANDOM_POOL_SIZE;
21
-
22
- const DEFAULT = 0;
23
- const DEFLATING = 1;
24
- const GET_BLOB_DATA = 2;
25
-
26
- /**
27
- * HyBi Sender implementation.
28
- */
29
- class Sender {
30
- /**
31
- * Creates a Sender instance.
32
- *
33
- * @param {Duplex} socket The connection socket
34
- * @param {Object} [extensions] An object containing the negotiated extensions
35
- * @param {Function} [generateMask] The function used to generate the masking
36
- * key
37
- */
38
- constructor(socket, extensions, generateMask) {
39
- this._extensions = extensions || {};
40
-
41
- if (generateMask) {
42
- this._generateMask = generateMask;
43
- this._maskBuffer = Buffer.alloc(4);
44
- }
45
-
46
- this._socket = socket;
47
-
48
- this._firstFragment = true;
49
- this._compress = false;
50
-
51
- this._bufferedBytes = 0;
52
- this._queue = [];
53
- this._state = DEFAULT;
54
- this.onerror = NOOP;
55
- this[kWebSocket] = undefined;
56
- }
57
-
58
- /**
59
- * Frames a piece of data according to the HyBi WebSocket protocol.
60
- *
61
- * @param {(Buffer|String)} data The data to frame
62
- * @param {Object} options Options object
63
- * @param {Boolean} [options.fin=false] Specifies whether or not to set the
64
- * FIN bit
65
- * @param {Function} [options.generateMask] The function used to generate the
66
- * masking key
67
- * @param {Boolean} [options.mask=false] Specifies whether or not to mask
68
- * `data`
69
- * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
70
- * key
71
- * @param {Number} options.opcode The opcode
72
- * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
73
- * modified
74
- * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
75
- * RSV1 bit
76
- * @return {(Buffer|String)[]} The framed data
77
- * @public
78
- */
79
- static frame(data, options) {
80
- let mask;
81
- let merge = false;
82
- let offset = 2;
83
- let skipMasking = false;
84
-
85
- if (options.mask) {
86
- mask = options.maskBuffer || maskBuffer;
87
-
88
- if (options.generateMask) {
89
- options.generateMask(mask);
90
- } else {
91
- if (randomPoolPointer === RANDOM_POOL_SIZE) {
92
- /* istanbul ignore else */
93
- if (randomPool === undefined) {
94
- //
95
- // This is lazily initialized because server-sent frames must not
96
- // be masked so it may never be used.
97
- //
98
- randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
99
- }
100
-
101
- randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
102
- randomPoolPointer = 0;
103
- }
104
-
105
- mask[0] = randomPool[randomPoolPointer++];
106
- mask[1] = randomPool[randomPoolPointer++];
107
- mask[2] = randomPool[randomPoolPointer++];
108
- mask[3] = randomPool[randomPoolPointer++];
109
- }
110
-
111
- skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
112
- offset = 6;
113
- }
114
-
115
- let dataLength;
116
-
117
- if (typeof data === 'string') {
118
- if (
119
- (!options.mask || skipMasking) &&
120
- options[kByteLength] !== undefined
121
- ) {
122
- dataLength = options[kByteLength];
123
- } else {
124
- data = Buffer.from(data);
125
- dataLength = data.length;
126
- }
127
- } else {
128
- dataLength = data.length;
129
- merge = options.mask && options.readOnly && !skipMasking;
130
- }
131
-
132
- let payloadLength = dataLength;
133
-
134
- if (dataLength >= 65536) {
135
- offset += 8;
136
- payloadLength = 127;
137
- } else if (dataLength > 125) {
138
- offset += 2;
139
- payloadLength = 126;
140
- }
141
-
142
- const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
143
-
144
- target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
145
- if (options.rsv1) target[0] |= 0x40;
146
-
147
- target[1] = payloadLength;
148
-
149
- if (payloadLength === 126) {
150
- target.writeUInt16BE(dataLength, 2);
151
- } else if (payloadLength === 127) {
152
- target[2] = target[3] = 0;
153
- target.writeUIntBE(dataLength, 4, 6);
154
- }
155
-
156
- if (!options.mask) return [target, data];
157
-
158
- target[1] |= 0x80;
159
- target[offset - 4] = mask[0];
160
- target[offset - 3] = mask[1];
161
- target[offset - 2] = mask[2];
162
- target[offset - 1] = mask[3];
163
-
164
- if (skipMasking) return [target, data];
165
-
166
- if (merge) {
167
- applyMask(data, mask, target, offset, dataLength);
168
- return [target];
169
- }
170
-
171
- applyMask(data, mask, data, 0, dataLength);
172
- return [target, data];
173
- }
174
-
175
- /**
176
- * Sends a close message to the other peer.
177
- *
178
- * @param {Number} [code] The status code component of the body
179
- * @param {(String|Buffer)} [data] The message component of the body
180
- * @param {Boolean} [mask=false] Specifies whether or not to mask the message
181
- * @param {Function} [cb] Callback
182
- * @public
183
- */
184
- close(code, data, mask, cb) {
185
- let buf;
186
-
187
- if (code === undefined) {
188
- buf = EMPTY_BUFFER;
189
- } else if (typeof code !== 'number' || !isValidStatusCode(code)) {
190
- throw new TypeError('First argument must be a valid error code number');
191
- } else if (data === undefined || !data.length) {
192
- buf = Buffer.allocUnsafe(2);
193
- buf.writeUInt16BE(code, 0);
194
- } else {
195
- const length = Buffer.byteLength(data);
196
-
197
- if (length > 123) {
198
- throw new RangeError('The message must not be greater than 123 bytes');
199
- }
200
-
201
- buf = Buffer.allocUnsafe(2 + length);
202
- buf.writeUInt16BE(code, 0);
203
-
204
- if (typeof data === 'string') {
205
- buf.write(data, 2);
206
- } else if (isUint8Array(data)) {
207
- buf.set(data, 2);
208
- } else {
209
- throw new TypeError('Second argument must be a string or a Uint8Array');
210
- }
211
- }
212
-
213
- const options = {
214
- [kByteLength]: buf.length,
215
- fin: true,
216
- generateMask: this._generateMask,
217
- mask,
218
- maskBuffer: this._maskBuffer,
219
- opcode: 0x08,
220
- readOnly: false,
221
- rsv1: false
222
- };
223
-
224
- if (this._state !== DEFAULT) {
225
- this.enqueue([this.dispatch, buf, false, options, cb]);
226
- } else {
227
- this.sendFrame(Sender.frame(buf, options), cb);
228
- }
229
- }
230
-
231
- /**
232
- * Sends a ping message to the other peer.
233
- *
234
- * @param {*} data The message to send
235
- * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
236
- * @param {Function} [cb] Callback
237
- * @public
238
- */
239
- ping(data, mask, cb) {
240
- let byteLength;
241
- let readOnly;
242
-
243
- if (typeof data === 'string') {
244
- byteLength = Buffer.byteLength(data);
245
- readOnly = false;
246
- } else if (isBlob(data)) {
247
- byteLength = data.size;
248
- readOnly = false;
249
- } else {
250
- data = toBuffer(data);
251
- byteLength = data.length;
252
- readOnly = toBuffer.readOnly;
253
- }
254
-
255
- if (byteLength > 125) {
256
- throw new RangeError('The data size must not be greater than 125 bytes');
257
- }
258
-
259
- const options = {
260
- [kByteLength]: byteLength,
261
- fin: true,
262
- generateMask: this._generateMask,
263
- mask,
264
- maskBuffer: this._maskBuffer,
265
- opcode: 0x09,
266
- readOnly,
267
- rsv1: false
268
- };
269
-
270
- if (isBlob(data)) {
271
- if (this._state !== DEFAULT) {
272
- this.enqueue([this.getBlobData, data, false, options, cb]);
273
- } else {
274
- this.getBlobData(data, false, options, cb);
275
- }
276
- } else if (this._state !== DEFAULT) {
277
- this.enqueue([this.dispatch, data, false, options, cb]);
278
- } else {
279
- this.sendFrame(Sender.frame(data, options), cb);
280
- }
281
- }
282
-
283
- /**
284
- * Sends a pong message to the other peer.
285
- *
286
- * @param {*} data The message to send
287
- * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
288
- * @param {Function} [cb] Callback
289
- * @public
290
- */
291
- pong(data, mask, cb) {
292
- let byteLength;
293
- let readOnly;
294
-
295
- if (typeof data === 'string') {
296
- byteLength = Buffer.byteLength(data);
297
- readOnly = false;
298
- } else if (isBlob(data)) {
299
- byteLength = data.size;
300
- readOnly = false;
301
- } else {
302
- data = toBuffer(data);
303
- byteLength = data.length;
304
- readOnly = toBuffer.readOnly;
305
- }
306
-
307
- if (byteLength > 125) {
308
- throw new RangeError('The data size must not be greater than 125 bytes');
309
- }
310
-
311
- const options = {
312
- [kByteLength]: byteLength,
313
- fin: true,
314
- generateMask: this._generateMask,
315
- mask,
316
- maskBuffer: this._maskBuffer,
317
- opcode: 0x0a,
318
- readOnly,
319
- rsv1: false
320
- };
321
-
322
- if (isBlob(data)) {
323
- if (this._state !== DEFAULT) {
324
- this.enqueue([this.getBlobData, data, false, options, cb]);
325
- } else {
326
- this.getBlobData(data, false, options, cb);
327
- }
328
- } else if (this._state !== DEFAULT) {
329
- this.enqueue([this.dispatch, data, false, options, cb]);
330
- } else {
331
- this.sendFrame(Sender.frame(data, options), cb);
332
- }
333
- }
334
-
335
- /**
336
- * Sends a data message to the other peer.
337
- *
338
- * @param {*} data The message to send
339
- * @param {Object} options Options object
340
- * @param {Boolean} [options.binary=false] Specifies whether `data` is binary
341
- * or text
342
- * @param {Boolean} [options.compress=false] Specifies whether or not to
343
- * compress `data`
344
- * @param {Boolean} [options.fin=false] Specifies whether the fragment is the
345
- * last one
346
- * @param {Boolean} [options.mask=false] Specifies whether or not to mask
347
- * `data`
348
- * @param {Function} [cb] Callback
349
- * @public
350
- */
351
- send(data, options, cb) {
352
- const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
353
- let opcode = options.binary ? 2 : 1;
354
- let rsv1 = options.compress;
355
-
356
- let byteLength;
357
- let readOnly;
358
-
359
- if (typeof data === 'string') {
360
- byteLength = Buffer.byteLength(data);
361
- readOnly = false;
362
- } else if (isBlob(data)) {
363
- byteLength = data.size;
364
- readOnly = false;
365
- } else {
366
- data = toBuffer(data);
367
- byteLength = data.length;
368
- readOnly = toBuffer.readOnly;
369
- }
370
-
371
- if (this._firstFragment) {
372
- this._firstFragment = false;
373
- if (
374
- rsv1 &&
375
- perMessageDeflate &&
376
- perMessageDeflate.params[
377
- perMessageDeflate._isServer
378
- ? 'server_no_context_takeover'
379
- : 'client_no_context_takeover'
380
- ]
381
- ) {
382
- rsv1 = byteLength >= perMessageDeflate._threshold;
383
- }
384
- this._compress = rsv1;
385
- } else {
386
- rsv1 = false;
387
- opcode = 0;
388
- }
389
-
390
- if (options.fin) this._firstFragment = true;
391
-
392
- const opts = {
393
- [kByteLength]: byteLength,
394
- fin: options.fin,
395
- generateMask: this._generateMask,
396
- mask: options.mask,
397
- maskBuffer: this._maskBuffer,
398
- opcode,
399
- readOnly,
400
- rsv1
401
- };
402
-
403
- if (isBlob(data)) {
404
- if (this._state !== DEFAULT) {
405
- this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
406
- } else {
407
- this.getBlobData(data, this._compress, opts, cb);
408
- }
409
- } else if (this._state !== DEFAULT) {
410
- this.enqueue([this.dispatch, data, this._compress, opts, cb]);
411
- } else {
412
- this.dispatch(data, this._compress, opts, cb);
413
- }
414
- }
415
-
416
- /**
417
- * Gets the contents of a blob as binary data.
418
- *
419
- * @param {Blob} blob The blob
420
- * @param {Boolean} [compress=false] Specifies whether or not to compress
421
- * the data
422
- * @param {Object} options Options object
423
- * @param {Boolean} [options.fin=false] Specifies whether or not to set the
424
- * FIN bit
425
- * @param {Function} [options.generateMask] The function used to generate the
426
- * masking key
427
- * @param {Boolean} [options.mask=false] Specifies whether or not to mask
428
- * `data`
429
- * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
430
- * key
431
- * @param {Number} options.opcode The opcode
432
- * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
433
- * modified
434
- * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
435
- * RSV1 bit
436
- * @param {Function} [cb] Callback
437
- * @private
438
- */
439
- getBlobData(blob, compress, options, cb) {
440
- this._bufferedBytes += options[kByteLength];
441
- this._state = GET_BLOB_DATA;
442
-
443
- blob
444
- .arrayBuffer()
445
- .then((arrayBuffer) => {
446
- if (this._socket.destroyed) {
447
- const err = new Error(
448
- 'The socket was closed while the blob was being read'
449
- );
450
-
451
- //
452
- // `callCallbacks` is called in the next tick to ensure that errors
453
- // that might be thrown in the callbacks behave like errors thrown
454
- // outside the promise chain.
455
- //
456
- process.nextTick(callCallbacks, this, err, cb);
457
- return;
458
- }
459
-
460
- this._bufferedBytes -= options[kByteLength];
461
- const data = toBuffer(arrayBuffer);
462
-
463
- if (!compress) {
464
- this._state = DEFAULT;
465
- this.sendFrame(Sender.frame(data, options), cb);
466
- this.dequeue();
467
- } else {
468
- this.dispatch(data, compress, options, cb);
469
- }
470
- })
471
- .catch((err) => {
472
- //
473
- // `onError` is called in the next tick for the same reason that
474
- // `callCallbacks` above is.
475
- //
476
- process.nextTick(onError, this, err, cb);
477
- });
478
- }
479
-
480
- /**
481
- * Dispatches a message.
482
- *
483
- * @param {(Buffer|String)} data The message to send
484
- * @param {Boolean} [compress=false] Specifies whether or not to compress
485
- * `data`
486
- * @param {Object} options Options object
487
- * @param {Boolean} [options.fin=false] Specifies whether or not to set the
488
- * FIN bit
489
- * @param {Function} [options.generateMask] The function used to generate the
490
- * masking key
491
- * @param {Boolean} [options.mask=false] Specifies whether or not to mask
492
- * `data`
493
- * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
494
- * key
495
- * @param {Number} options.opcode The opcode
496
- * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
497
- * modified
498
- * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
499
- * RSV1 bit
500
- * @param {Function} [cb] Callback
501
- * @private
502
- */
503
- dispatch(data, compress, options, cb) {
504
- if (!compress) {
505
- this.sendFrame(Sender.frame(data, options), cb);
506
- return;
507
- }
508
-
509
- const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
510
-
511
- this._bufferedBytes += options[kByteLength];
512
- this._state = DEFLATING;
513
- perMessageDeflate.compress(data, options.fin, (_, buf) => {
514
- if (this._socket.destroyed) {
515
- const err = new Error(
516
- 'The socket was closed while data was being compressed'
517
- );
518
-
519
- callCallbacks(this, err, cb);
520
- return;
521
- }
522
-
523
- this._bufferedBytes -= options[kByteLength];
524
- this._state = DEFAULT;
525
- options.readOnly = false;
526
- this.sendFrame(Sender.frame(buf, options), cb);
527
- this.dequeue();
528
- });
529
- }
530
-
531
- /**
532
- * Executes queued send operations.
533
- *
534
- * @private
535
- */
536
- dequeue() {
537
- while (this._state === DEFAULT && this._queue.length) {
538
- const params = this._queue.shift();
539
-
540
- this._bufferedBytes -= params[3][kByteLength];
541
- Reflect.apply(params[0], this, params.slice(1));
542
- }
543
- }
544
-
545
- /**
546
- * Enqueues a send operation.
547
- *
548
- * @param {Array} params Send operation parameters.
549
- * @private
550
- */
551
- enqueue(params) {
552
- this._bufferedBytes += params[3][kByteLength];
553
- this._queue.push(params);
554
- }
555
-
556
- /**
557
- * Sends a frame.
558
- *
559
- * @param {(Buffer | String)[]} list The frame to send
560
- * @param {Function} [cb] Callback
561
- * @private
562
- */
563
- sendFrame(list, cb) {
564
- if (list.length === 2) {
565
- this._socket.cork();
566
- this._socket.write(list[0]);
567
- this._socket.write(list[1], cb);
568
- this._socket.uncork();
569
- } else {
570
- this._socket.write(list[0], cb);
571
- }
572
- }
573
- }
574
-
575
- module.exports = Sender;
576
-
577
- /**
578
- * Calls queued callbacks with an error.
579
- *
580
- * @param {Sender} sender The `Sender` instance
581
- * @param {Error} err The error to call the callbacks with
582
- * @param {Function} [cb] The first callback
583
- * @private
584
- */
585
- function callCallbacks(sender, err, cb) {
586
- if (typeof cb === 'function') cb(err);
587
-
588
- for (let i = 0; i < sender._queue.length; i++) {
589
- const params = sender._queue[i];
590
- const callback = params[params.length - 1];
591
-
592
- if (typeof callback === 'function') callback(err);
593
- }
594
- }
595
-
596
- /**
597
- * Handles a `Sender` error.
598
- *
599
- * @param {Sender} sender The `Sender` instance
600
- * @param {Error} err The error
601
- * @param {Function} [cb] The first pending callback
602
- * @private
603
- */
604
- function onError(sender, err, cb) {
605
- callCallbacks(sender, err, cb);
606
- sender.onerror(err);
607
- }