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