isomorfeus-transport 1.0.0.zeta23 → 2.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -21
  3. data/README.md +27 -36
  4. data/lib/isomorfeus/transport/client_processor.rb +35 -30
  5. data/lib/isomorfeus/transport/config.rb +182 -158
  6. data/lib/isomorfeus/transport/hamster_session_store.rb +96 -0
  7. data/lib/isomorfeus/transport/handler/authentication_handler.rb +70 -70
  8. data/lib/isomorfeus/transport/imports.rb +9 -0
  9. data/lib/isomorfeus/transport/middlewares.rb +13 -13
  10. data/lib/isomorfeus/transport/rack_middleware.rb +59 -55
  11. data/lib/isomorfeus/transport/request_agent.rb +34 -34
  12. data/lib/isomorfeus/transport/response_agent.rb +23 -23
  13. data/lib/isomorfeus/transport/server_processor.rb +129 -101
  14. data/lib/isomorfeus/transport/server_socket_processor.rb +54 -54
  15. data/lib/isomorfeus/transport/ssr_login.rb +28 -28
  16. data/lib/isomorfeus/transport/version.rb +5 -5
  17. data/lib/isomorfeus/transport/{websocket.rb → websocket_client.rb} +123 -123
  18. data/lib/isomorfeus/transport.rb +200 -213
  19. data/lib/isomorfeus-transport.rb +70 -62
  20. data/lib/lucid_authentication/mixin.rb +122 -124
  21. data/lib/lucid_channel/base.rb +8 -11
  22. data/lib/lucid_channel/mixin.rb +105 -50
  23. data/lib/lucid_handler/base.rb +8 -9
  24. data/lib/lucid_handler/mixin.rb +27 -27
  25. data/node_modules/.package-lock.json +27 -0
  26. data/node_modules/ws/LICENSE +19 -0
  27. data/node_modules/ws/README.md +496 -0
  28. data/node_modules/ws/browser.js +8 -0
  29. data/node_modules/ws/index.js +13 -0
  30. data/node_modules/ws/lib/buffer-util.js +126 -0
  31. data/node_modules/ws/lib/constants.js +12 -0
  32. data/node_modules/ws/lib/event-target.js +266 -0
  33. data/node_modules/ws/lib/extension.js +203 -0
  34. data/node_modules/ws/lib/limiter.js +55 -0
  35. data/node_modules/ws/lib/permessage-deflate.js +511 -0
  36. data/node_modules/ws/lib/receiver.js +612 -0
  37. data/node_modules/ws/lib/sender.js +414 -0
  38. data/node_modules/ws/lib/stream.js +180 -0
  39. data/node_modules/ws/lib/subprotocol.js +62 -0
  40. data/node_modules/ws/lib/validation.js +124 -0
  41. data/node_modules/ws/lib/websocket-server.js +485 -0
  42. data/node_modules/ws/lib/websocket.js +1144 -0
  43. data/node_modules/ws/package.json +61 -0
  44. data/node_modules/ws/wrapper.mjs +8 -0
  45. data/package.json +6 -0
  46. metadata +76 -54
  47. data/lib/isomorfeus/transport/dbm_session_store.rb +0 -51
@@ -0,0 +1,414 @@
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 {Boolean} [options.fin=false] Specifies whether or not to set the
44
+ * FIN bit
45
+ * @param {Boolean} [options.mask=false] Specifies whether or not to mask
46
+ * `data`
47
+ * @param {Number} options.opcode The opcode
48
+ * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
49
+ * modified
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|Buffer)} [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.length) {
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
+
130
+ if (typeof data === 'string') {
131
+ buf.write(data, 2);
132
+ } else {
133
+ buf.set(data, 2);
134
+ }
135
+ }
136
+
137
+ if (this._deflating) {
138
+ this.enqueue([this.doClose, buf, mask, cb]);
139
+ } else {
140
+ this.doClose(buf, mask, cb);
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Frames and sends a close message.
146
+ *
147
+ * @param {Buffer} data The message to send
148
+ * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
149
+ * @param {Function} [cb] Callback
150
+ * @private
151
+ */
152
+ doClose(data, mask, cb) {
153
+ this.sendFrame(
154
+ Sender.frame(data, {
155
+ fin: true,
156
+ rsv1: false,
157
+ opcode: 0x08,
158
+ mask,
159
+ readOnly: false
160
+ }),
161
+ cb
162
+ );
163
+ }
164
+
165
+ /**
166
+ * Sends a ping message to the other peer.
167
+ *
168
+ * @param {*} data The message to send
169
+ * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
170
+ * @param {Function} [cb] Callback
171
+ * @public
172
+ */
173
+ ping(data, mask, cb) {
174
+ const buf = toBuffer(data);
175
+
176
+ if (buf.length > 125) {
177
+ throw new RangeError('The data size must not be greater than 125 bytes');
178
+ }
179
+
180
+ if (this._deflating) {
181
+ this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
182
+ } else {
183
+ this.doPing(buf, mask, toBuffer.readOnly, cb);
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Frames and sends a ping message.
189
+ *
190
+ * @param {Buffer} data The message to send
191
+ * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
192
+ * @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
193
+ * @param {Function} [cb] Callback
194
+ * @private
195
+ */
196
+ doPing(data, mask, readOnly, cb) {
197
+ this.sendFrame(
198
+ Sender.frame(data, {
199
+ fin: true,
200
+ rsv1: false,
201
+ opcode: 0x09,
202
+ mask,
203
+ readOnly
204
+ }),
205
+ cb
206
+ );
207
+ }
208
+
209
+ /**
210
+ * Sends a pong message to the other peer.
211
+ *
212
+ * @param {*} data The message to send
213
+ * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
214
+ * @param {Function} [cb] Callback
215
+ * @public
216
+ */
217
+ pong(data, mask, cb) {
218
+ const buf = toBuffer(data);
219
+
220
+ if (buf.length > 125) {
221
+ throw new RangeError('The data size must not be greater than 125 bytes');
222
+ }
223
+
224
+ if (this._deflating) {
225
+ this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
226
+ } else {
227
+ this.doPong(buf, mask, toBuffer.readOnly, cb);
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Frames and sends a pong message.
233
+ *
234
+ * @param {Buffer} data The message to send
235
+ * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
236
+ * @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
237
+ * @param {Function} [cb] Callback
238
+ * @private
239
+ */
240
+ doPong(data, mask, readOnly, cb) {
241
+ this.sendFrame(
242
+ Sender.frame(data, {
243
+ fin: true,
244
+ rsv1: false,
245
+ opcode: 0x0a,
246
+ mask,
247
+ readOnly
248
+ }),
249
+ cb
250
+ );
251
+ }
252
+
253
+ /**
254
+ * Sends a data message to the other peer.
255
+ *
256
+ * @param {*} data The message to send
257
+ * @param {Object} options Options object
258
+ * @param {Boolean} [options.binary=false] Specifies whether `data` is binary
259
+ * or text
260
+ * @param {Boolean} [options.compress=false] Specifies whether or not to
261
+ * compress `data`
262
+ * @param {Boolean} [options.fin=false] Specifies whether the fragment is the
263
+ * last one
264
+ * @param {Boolean} [options.mask=false] Specifies whether or not to mask
265
+ * `data`
266
+ * @param {Function} [cb] Callback
267
+ * @public
268
+ */
269
+ send(data, options, cb) {
270
+ const buf = toBuffer(data);
271
+ const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
272
+ let opcode = options.binary ? 2 : 1;
273
+ let rsv1 = options.compress;
274
+
275
+ if (this._firstFragment) {
276
+ this._firstFragment = false;
277
+ if (rsv1 && perMessageDeflate) {
278
+ rsv1 = buf.length >= perMessageDeflate._threshold;
279
+ }
280
+ this._compress = rsv1;
281
+ } else {
282
+ rsv1 = false;
283
+ opcode = 0;
284
+ }
285
+
286
+ if (options.fin) this._firstFragment = true;
287
+
288
+ if (perMessageDeflate) {
289
+ const opts = {
290
+ fin: options.fin,
291
+ rsv1,
292
+ opcode,
293
+ mask: options.mask,
294
+ readOnly: toBuffer.readOnly
295
+ };
296
+
297
+ if (this._deflating) {
298
+ this.enqueue([this.dispatch, buf, this._compress, opts, cb]);
299
+ } else {
300
+ this.dispatch(buf, this._compress, opts, cb);
301
+ }
302
+ } else {
303
+ this.sendFrame(
304
+ Sender.frame(buf, {
305
+ fin: options.fin,
306
+ rsv1: false,
307
+ opcode,
308
+ mask: options.mask,
309
+ readOnly: toBuffer.readOnly
310
+ }),
311
+ cb
312
+ );
313
+ }
314
+ }
315
+
316
+ /**
317
+ * Dispatches a data message.
318
+ *
319
+ * @param {Buffer} data The message to send
320
+ * @param {Boolean} [compress=false] Specifies whether or not to compress
321
+ * `data`
322
+ * @param {Object} options Options object
323
+ * @param {Number} options.opcode The opcode
324
+ * @param {Boolean} [options.fin=false] Specifies whether or not to set the
325
+ * FIN bit
326
+ * @param {Boolean} [options.mask=false] Specifies whether or not to mask
327
+ * `data`
328
+ * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
329
+ * modified
330
+ * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
331
+ * RSV1 bit
332
+ * @param {Function} [cb] Callback
333
+ * @private
334
+ */
335
+ dispatch(data, compress, options, cb) {
336
+ if (!compress) {
337
+ this.sendFrame(Sender.frame(data, options), cb);
338
+ return;
339
+ }
340
+
341
+ const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
342
+
343
+ this._bufferedBytes += data.length;
344
+ this._deflating = true;
345
+ perMessageDeflate.compress(data, options.fin, (_, buf) => {
346
+ if (this._socket.destroyed) {
347
+ const err = new Error(
348
+ 'The socket was closed while data was being compressed'
349
+ );
350
+
351
+ if (typeof cb === 'function') cb(err);
352
+
353
+ for (let i = 0; i < this._queue.length; i++) {
354
+ const callback = this._queue[i][4];
355
+
356
+ if (typeof callback === 'function') callback(err);
357
+ }
358
+
359
+ return;
360
+ }
361
+
362
+ this._bufferedBytes -= data.length;
363
+ this._deflating = false;
364
+ options.readOnly = false;
365
+ this.sendFrame(Sender.frame(buf, options), cb);
366
+ this.dequeue();
367
+ });
368
+ }
369
+
370
+ /**
371
+ * Executes queued send operations.
372
+ *
373
+ * @private
374
+ */
375
+ dequeue() {
376
+ while (!this._deflating && this._queue.length) {
377
+ const params = this._queue.shift();
378
+
379
+ this._bufferedBytes -= params[1].length;
380
+ Reflect.apply(params[0], this, params.slice(1));
381
+ }
382
+ }
383
+
384
+ /**
385
+ * Enqueues a send operation.
386
+ *
387
+ * @param {Array} params Send operation parameters.
388
+ * @private
389
+ */
390
+ enqueue(params) {
391
+ this._bufferedBytes += params[1].length;
392
+ this._queue.push(params);
393
+ }
394
+
395
+ /**
396
+ * Sends a frame.
397
+ *
398
+ * @param {Buffer[]} list The frame to send
399
+ * @param {Function} [cb] Callback
400
+ * @private
401
+ */
402
+ sendFrame(list, cb) {
403
+ if (list.length === 2) {
404
+ this._socket.cork();
405
+ this._socket.write(list[0]);
406
+ this._socket.write(list[1], cb);
407
+ this._socket.uncork();
408
+ } else {
409
+ this._socket.write(list[0], cb);
410
+ }
411
+ }
412
+ }
413
+
414
+ module.exports = Sender;
@@ -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, isBinary) {
76
+ const data =
77
+ !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
78
+
79
+ if (!duplex.push(data)) {
80
+ resumeOnReceiverDrain = false;
81
+ ws._socket.pause();
82
+ }
83
+ });
84
+
85
+ ws.once('error', function error(err) {
86
+ if (duplex.destroyed) return;
87
+
88
+ // Prevent `ws.terminate()` from being called by `duplex._destroy()`.
89
+ //
90
+ // - If the `'error'` event is emitted before the `'open'` event, then
91
+ // `ws.terminate()` is a noop as no socket is assigned.
92
+ // - Otherwise, the error is re-emitted by the listener of the `'error'`
93
+ // event of the `Receiver` object. The listener already closes the
94
+ // connection by calling `ws.close()`. This allows a close frame to be
95
+ // sent to the other peer. If `ws.terminate()` is called right after this,
96
+ // then the close frame might not be sent.
97
+ terminateOnDestroy = false;
98
+ duplex.destroy(err);
99
+ });
100
+
101
+ ws.once('close', function close() {
102
+ if (duplex.destroyed) return;
103
+
104
+ duplex.push(null);
105
+ });
106
+
107
+ duplex._destroy = function (err, callback) {
108
+ if (ws.readyState === ws.CLOSED) {
109
+ callback(err);
110
+ process.nextTick(emitClose, duplex);
111
+ return;
112
+ }
113
+
114
+ let called = false;
115
+
116
+ ws.once('error', function error(err) {
117
+ called = true;
118
+ callback(err);
119
+ });
120
+
121
+ ws.once('close', function close() {
122
+ if (!called) callback(err);
123
+ process.nextTick(emitClose, duplex);
124
+ });
125
+
126
+ if (terminateOnDestroy) ws.terminate();
127
+ };
128
+
129
+ duplex._final = function (callback) {
130
+ if (ws.readyState === ws.CONNECTING) {
131
+ ws.once('open', function open() {
132
+ duplex._final(callback);
133
+ });
134
+ return;
135
+ }
136
+
137
+ // If the value of the `_socket` property is `null` it means that `ws` is a
138
+ // client websocket and the handshake failed. In fact, when this happens, a
139
+ // socket is never assigned to the websocket. Wait for the `'error'` event
140
+ // that will be emitted by the websocket.
141
+ if (ws._socket === null) return;
142
+
143
+ if (ws._socket._writableState.finished) {
144
+ callback();
145
+ if (duplex._readableState.endEmitted) duplex.destroy();
146
+ } else {
147
+ ws._socket.once('finish', function finish() {
148
+ // `duplex` is not destroyed here because the `'end'` event will be
149
+ // emitted on `duplex` after this `'finish'` event. The EOF signaling
150
+ // `null` chunk is, in fact, pushed when the websocket emits `'close'`.
151
+ callback();
152
+ });
153
+ ws.close();
154
+ }
155
+ };
156
+
157
+ duplex._read = function () {
158
+ if (ws.readyState === ws.OPEN && !resumeOnReceiverDrain) {
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,62 @@
1
+ 'use strict';
2
+
3
+ const { tokenChars } = require('./validation');
4
+
5
+ /**
6
+ * Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.
7
+ *
8
+ * @param {String} header The field value of the header
9
+ * @return {Set} The subprotocol names
10
+ * @public
11
+ */
12
+ function parse(header) {
13
+ const protocols = new Set();
14
+ let start = -1;
15
+ let end = -1;
16
+ let i = 0;
17
+
18
+ for (i; i < header.length; i++) {
19
+ const code = header.charCodeAt(i);
20
+
21
+ if (end === -1 && tokenChars[code] === 1) {
22
+ if (start === -1) start = i;
23
+ } else if (
24
+ i !== 0 &&
25
+ (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
26
+ ) {
27
+ if (end === -1 && start !== -1) end = i;
28
+ } else if (code === 0x2c /* ',' */) {
29
+ if (start === -1) {
30
+ throw new SyntaxError(`Unexpected character at index ${i}`);
31
+ }
32
+
33
+ if (end === -1) end = i;
34
+
35
+ const protocol = header.slice(start, end);
36
+
37
+ if (protocols.has(protocol)) {
38
+ throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
39
+ }
40
+
41
+ protocols.add(protocol);
42
+ start = end = -1;
43
+ } else {
44
+ throw new SyntaxError(`Unexpected character at index ${i}`);
45
+ }
46
+ }
47
+
48
+ if (start === -1 || end !== -1) {
49
+ throw new SyntaxError('Unexpected end of input');
50
+ }
51
+
52
+ const protocol = header.slice(start, i);
53
+
54
+ if (protocols.has(protocol)) {
55
+ throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
56
+ }
57
+
58
+ protocols.add(protocol);
59
+ return protocols;
60
+ }
61
+
62
+ module.exports = { parse };