-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
1585 lines (1315 loc) · 47.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
# bpmux [![Build Status](https://github.com/davedoesdev/bpmux/workflows/ci/badge.svg)](https://github.com/davedoesdev/bpmux/actions) [![Coverage Status](https://coveralls.io/repos/davedoesdev/bpmux/badge.png?branch=master&service=github)](https://coveralls.io/r/davedoesdev/bpmux?branch=master) [![NPM version](https://badge.fury.io/js/bpmux.png)](http://badge.fury.io/js/bpmux)
Node stream multiplexing with back-pressure on each stream.
- Run more than one [`stream.Duplex`](https://nodejs.org/api/stream.html#stream_class_stream_duplex) over a carrier `Duplex`.
- Exerts back-pressure on each multiplexed stream and the underlying carrier stream.
- Each multiplexed stream's back-pressure is handled separately while respecting the carrier's capacity. [This prevents a slow or paused stream affecting other streams](#comparison). This does incur an overhead so if you don't care about this feature you might want to look elsewhere.
- Unit tests with 100% coverage.
- Tested with TCP streams. You'll get better performance if you [disable Nagle](https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_setnodelay_nodelay).
- Works in the browser!
- Tested with [Primus](https://github.com/primus/primus) (using [primus-backpressure](https://github.com/davedoesdev/primus-backpressure)).
- Tested with HTTP/2 streams (using [browser-http2-duplex](https://github.com/davedoesdev/browser-http2-duplex)). Also tested Node-to-Node using `http2`.
- Browser unit tests using [webpack](http://webpack.github.io/) and [nwjs](http://nwjs.io/).
- **See the [errors](#errors) section for information on why multiplexed streams error when their carrier stream closes before they do.**
The API is described [here](#api).
## Example
Multiplexing multiple streams over a single TCP stream:
```javascript
var net = require('net'),
crypto = require('crypto'),
assert = require('assert'),
BPMux = require('bpmux').BPMux,
sent = [];
net.createServer(function (c)
{
var received = [], ended = 0;
new BPMux(c).on('handshake', function (duplex)
{
var accum = '';
duplex.on('readable', function ()
{
var data = this.read();
if (data)
{
accum += data.toString('hex');
}
});
duplex.on('end', function ()
{
received.push(accum);
ended += 1;
assert(ended <= 10);
if (ended === 10)
{
assert.deepEqual(received.sort(), sent.sort());
}
});
});
}).listen(7000, function ()
{
var mux = new BPMux(net.createConnection(7000)), i;
function multiplex(n)
{
var data = crypto.randomBytes(n * 100);
mux.multiplex().end(data);
sent.push(data.toString('hex'));
}
for (i = 1; i <= 10; i += 1)
{
multiplex(i);
}
});
```
## Another Example
Multiple return pipes to the browser, multiplexed over a single Primus connection:
```javascript
var PrimusDuplex = require('primus-backpressure').PrimusDuplex,
BPMux = require('bpmux').BPMux,
http = require('http'),
path = require('path'),
crypto = require('crypto'),
stream = require('stream'),
assert = require('assert'),
finalhandler = require('finalhandler'),
serve_static = require('serve-static'),
Primus = require('primus'),
serve = serve_static(__dirname);
http.createServer(function (req, res)
{
serve(req, res, finalhandler(req, res));
}).listen(7500, function ()
{
var primus = new Primus(this);
primus.on('connection', function (spark)
{
var mux = new BPMux(new PrimusDuplex(spark)), ended = 0, i;
function multiplex(n)
{
var buf = crypto.randomBytes(10 * 1024),
buf_stream = new stream.PassThrough(),
bufs = [],
duplex = mux.multiplex({ handshake_data: Buffer.from([n]) });
buf_stream.end(buf);
buf_stream.pipe(duplex);
duplex.on('readable', function ()
{
var data;
while (true)
{
data = this.read();
if (data === null)
{
break;
}
bufs.push(data);
}
});
duplex.on('end', function ()
{
console.log('end', n);
ended += 1;
assert(ended <= 10);
assert.deepEqual(Buffer.concat(bufs), buf);
});
}
for (i = 0; i < 10; i += 1)
{
multiplex(i);
}
});
console.log('Point your browser to http://localhost:7500/loader.html');
});
```
The HTML (`loader.html`) for the browser-side of this example:
```html
<html>
<head>
<title>BPMux Test Runner</title>
<script type="text/javascript" src="/primus/primus.js"></script>
<script type="text/javascript" src="bundle.js"></script>
<script type="text/javascript" src="loader.js"></script>
</head>
<body onload='doit()'>
</body>
</html>
```
The browser-side code (`loader.js`):
```javascript
function doit()
{
var mux = new BPMux(new PrimusDuplex(new Primus({ strategy: false })));
mux.on('handshake', function (duplex, handshake_data)
{
console.log("handshake", handshake_data[0]);
duplex.pipe(duplex);
duplex.on('end', function ()
{
console.log('end', handshake_data[0]);
});
});
}
```
The browser-side dependencies (`bundle.js`) can be produced by webpack from:
```javascript
PrimusDuplex = require('primus-backpressure').PrimusDuplex;
BPMux = require('bpmux').BPMux;
```
## Comparison
### [multiplex](https://github.com/maxogden/multiplex) library
Multiplexing libraries which don't exert backpressure on individual streams
suffer from starvation. A stream which doesn't read its data stops other streams
on the multiplex getting their data.
Here's a test using the [multiplex](https://github.com/maxogden/multiplex)
library:
```javascript
// Uses https://github.com/maxogden/multiplex (npm install multiplex)
// Backpressure is exerted across the multiplex as a whole, not individual streams.
// This means a stream which doesn't read its data starves the other streams.
const fs = require('fs');
const net = require('net');
const multiplex = require('multiplex');
require('net').createServer(c => {
c.pipe(multiplex((stream, id) => {
stream.on('data', function(d) {
console.log('data', id, d.length);
if (id === '0') {
this.pause();
}
});
}));
}).listen(7000, () => {
const plex = multiplex();
plex.pipe(net.createConnection(7000));
const stream1 = plex.createStream();
const stream2 = plex.createStream();
fs.createReadStream('/dev/urandom').pipe(stream1);
fs.createReadStream('/dev/urandom').pipe(stream2);
});
```
When the first stream is paused, backpressure is applied to the second stream
too, even though it hasn't been paused. If you run this example, you'll see:
```bash
$ node multiplex.js
data 0 65536
data 1 65536
```
bpmux doesn't suffer from this problem since backpressure is exerted on each
stream separately. Here's the same test:
```javascript
// BPMux exerts backpressure on individual streams so a stream which doesn't
// read its data doesn't starve the other streams.
const fs = require('fs');
const net = require('net');
const { BPMux } = require('bpmux');
require('net').createServer(c => {
new BPMux(c).on('handshake', stream => {
stream.on('data', function (d) {
console.log('data', stream._chan, d.length);
if (stream._chan === 0) {
this.pause();
}
});
});
}).listen(7000, () => {
const mux = new BPMux(net.createConnection(7000));
const stream1 = mux.multiplex();
const stream2 = mux.multiplex();
fs.createReadStream('/dev/urandom').pipe(stream1);
fs.createReadStream('/dev/urandom').pipe(stream2);
});
```
The second stream continues to receive data when the first stream is paused:
```bash
data 0 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
...
```
### HTTP/2 sessions
[HTTP/2 sessions](https://nodejs.org/dist/latest-v16.x/docs/api/http2.html#class-http2session)
do exert backpressure on individual streams, as this test shows:
```javascript
const fs = require('fs');
const http2 = require('http2');
const server = http2.createServer();
server.on('stream', (stream, headers) => {
stream.on('data', function (d) {
console.log('data', headers[':path'], d.length);
if (headers[':path'] === '/stream1') {
this.pause();
}
});
});
server.listen(8000);
const client = http2.connect('http://localhost:8000');
const stream1 = client.request({ ':path': '/stream1' }, { endStream: false });
const stream2 = client.request({ ':path': '/stream2' }, { endStream: false });
fs.createReadStream('/dev/urandom').pipe(stream1);
fs.createReadStream('/dev/urandom').pipe(stream2);
```
```
data /stream1 16384
data /stream2 16384
data /stream2 16348
data /stream2 35
data /stream2 16384
data /stream2 16384
data /stream2 1
data /stream2 16384
data /stream2 16366
data /stream2 18
data /stream2 16384
data /stream2 16382
data /stream2 2
data /stream2 16384
...
```
If you pass a pair of sessions (one client, one server) to [`BPMux()`](#bpmuxcarrier-options),
they will be used for multiplexing streams, with no additional overhead. This is useful if
you want to use the bpmux API.
## Errors
bpmux will emit `error` events on multiplexed streams if their underlying
(carrier) stream closes before they have closed. The error object will have one
of the following messages:
```
carrier stream finished before duplex finished
carrier stream ended before end message received
```
and have a property `carrier_done` set to `true`.
As this is an `error` event, you must register an event listener on multiplexed
streams [if you don't want the Node process to exit](https://nodejs.org/dist/latest-v13.x/docs/api/events.html#events_error_events).
The reasoning behind emitting `error` events on open multiplexed streams when
their carrier closes is:
- If you're reading from a stream and it hasn't ended before the carrier closes then there may be some data that you'll never receive. This is an error state.
- If you're writing to a stream and it hasn't finished before the carrier closes then your application should be informed about it straight away. If it's performing some heavy calculation, for example, then it has a chance to cancel it before writing the result to the stream.
If you do register `error` event listeners, make sure you do so for streams
you multiplex using [`multiplex()`](#bpmuxprototypemultiplexoptions) _and_
for streams you receive using the [`handshake`](#bpmuxeventshandshakeduplex-handshake_data-delay_handshake) or [`peer_multiplex`](#bpmuxeventspeer_multiplexduplex) events.
`BPMux` objects will also re-emit any `error` events their carrier stream emits.
## Installation
```shell
npm install bpmux
```
## Licence
[MIT](LICENCE)
## Test
Over TCP (long test):
```shell
grunt test
```
Over TCP (quick test):
```shell
grunt test-fast
```
Over Primus (using nwjs to run browser- and server-side):
```shell
grunt test-browser
```
The examples at the top of this page:
```shell
grunt test-examples
```
## Code Coverage
```shell
grunt coverage
```
[c8](https://github.com/bcoe/c8) results are available [here](http://rawgit.davedoesdev.com/davedoesdev/bpmux/master/coverage/lcov-report/index.html).
Coveralls page is [here](https://coveralls.io/r/davedoesdev/bpmux).
## Lint
```shell
grunt lint
```
# API
*/
/*eslint-env node */
'use strict';
var util = require('util'),
stream = require('stream'),
Duplex = stream.Duplex,
Writable = stream.Writable,
EventEmitter = require('events').EventEmitter,
frame = require('frame-stream'),
max_seq = Math.pow(2, 32),
TYPE_END = 0,
TYPE_HANDSHAKE = 1,
TYPE_STATUS = 2,
TYPE_FINISHED_STATUS = 3,
TYPE_DATA = 4,
TYPE_PRE_HANDSHAKE = 5,
TYPE_ERROR_END = 6,
TYPE_KEEP_ALIVE = 7;
/**
Class for holding a pair of HTTP/2 sessions.
Pass this to [BPMux()](#bpmuxcarrier-options) and it will use the sessions'
existing support for multiplexing streams. Both [client](https://nodejs.org/dist/latest-v16.x/docs/api/http2.html#class-clienthttp2session) and [server](https://nodejs.org/dist/latest-v16.x/docs/api/http2.html#class-serverhttp2session) sessions
are required because HTTP/2 push streams are unidirectional.
@param {ClientHttp2Session} client Client session
@param {ServerHttp2Session} server Server session
*/
class Http2Sessions
{
constructor(client, server)
{
this._client = client;
this._server = server;
}
get client()
{
return this._client;
}
get server()
{
return this._server;
}
}
function BPDuplex(options, mux, chan)
{
Duplex.call(this, options);
options = Object.assign(
{
max_write_size: 0
}, options);
this._mux = mux;
this.mux = mux;
this._chan = chan;
this._max_write_size = options.max_write_size;
this._check_read_overflow = options.check_read_overflow !== false;
this._seq = 0;
this._remote_free = 0;
this._set_remote_free = false;
this._data = null;
this._cb = null;
this._index = 0;
this._finished = false;
this._ended = false;
this._removed = false;
this._handshake_sent = false;
this._handshake_received = false;
this._end_pending = false;
this._error_end = false;
this._error_end_pending = false;
function finish()
{
this._finished = true;
this._mux._send_end(this);
this._check_remove();
}
this.once('finish', finish);
this.once('close', function ()
{
this.removeListener('finish', finish);
if (!this._finished)
{
this._finished = true;
this._mux._send_end(this);
}
this._check_remove();
});
mux.duplexes.set(chan, this);
if ((mux._max_open > 0) && (mux.duplexes.size === mux._max_open))
{
setImmediate(function ()
{
mux.emit('full');
});
}
}
util.inherits(BPDuplex, Duplex);
BPDuplex.prototype._check_remove = function ()
{
// Don't call _remove if not ended because the duplex may have closed
// due to a local destroy and so data may still come from the peer
// (but be ignored because we don't push to destroyed streams).
// Duplex will be removed when TYPE_END is received.
if (this._finished && this._ended && !this._removed)
{
this._mux._remove(this);
}
};
BPDuplex.prototype.get_channel = function ()
{
return this._chan;
};
BPDuplex.prototype._send_handshake = function (handshake_data)
{
this._mux._send_handshake(this, handshake_data || Buffer.alloc(0));
};
BPDuplex.prototype._read = function () { return undefined; };
BPDuplex.prototype.read = function (size, send_status)
{
var r = Duplex.prototype.read.call(this, size);
if (send_status !== false)
{
this._mux._send_status(this);
}
return r;
};
BPDuplex.prototype._write = function (data, encoding, cb)
{
if (data.length === 0)
{
return cb();
}
this._data = data;
this._cb = cb;
this._mux._send();
};
BPDuplex.prototype.peer_error_then_end = function (chunk, encoding, cb)
{
this._error_end = true;
return this.end(chunk, encoding, cb);
};
/**
Constructor for a `BPMux` object which multiplexes more than one [`stream.Duplex`](https://nodejs.org/api/stream.html#stream_class_stream_duplex) over a carrier `Duplex`.
@constructor
@extends events.EventEmitter
@param {Duplex|Http2Sessions} carrier The `Duplex` stream over which other `Duplex` streams will be multiplexed.
@param {Object} [options] Configuration options. This is passed down to [`frame-stream`](https://github.com/davedoesdev/frame-stream). It also supports the following additional properties:
- `{Object} [peer_multiplex_options]` When your `BPMux` object detects a new multiplexed stream from the peer on the carrier, it creates a new `Duplex` and emits a [`peer_multiplex`](#bpmuxeventspeer_multiplexduplex) event. When it creates the `Duplex`, it uses `peer_multiplex_options` to configure it with the following options:
- `{Integer} [max_write_size]` Maximum number of bytes to write to the `Duplex` at once, regardless of how many bytes the peer is free to receive. Defaults to 0 (no limit).
- `{Boolean} [check_read_overflow]` Whether to check if more data than expected is being received. If `true` and the `Duplex`'s high-water mark for reading is exceeded then the `Duplex` emits an `error` event. This should not normally occur unless you add data yourself using [`readable.unshift`](http://nodejs.org/api/stream.html#stream_readable_unshift_chunk) — in which case you should set `check_read_overflow` to `false`. Defaults to `true`.
- `{Function} [parse_handshake_data(handshake_data)]` When a new stream is multiplexed, the `BPMux` objects at each end of the carrier exchange a handshake message. You can supply application-specific handshake data to add to the handshake message (see [`BPMux.prototype.multiplex`](#bpmuxprototypemultiplexoptions) and [`BPMux.events.handshake`](#bpmuxeventshandshakeduplex-handshake_data-delay_handshake)). By default, when handshake data from the peer is received, it's passed to your application as a raw [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer). Use `parse_handshake_data` to specify a custom parser. It will receive the `Buffer` as an argument and should return a value which makes sense to your application.
- `{Boolean} [coalesce_writes]` Whether to batch together writes to the carrier. When the carrier indicates it's ready to receive data, its spare capacity is shared equally between the multiplexed streams. By default, the data from each stream is written separately to the carrier. Specify `true` to write all the data to the carrier in a single write. Depending on the carrier, this can be more performant.
- `{Boolean} [high_channels]` `BPMux` assigns unique channel numbers to multiplexed streams. By default, it assigns numbers in the range [0..2^31). If your application can synchronise the two `BPMux` instances on each end of the carrier stream so they never call [`multiplex`](https://github.com/davedoesdev/bpmux#bpmuxprototypemultiplexoptions) at the same time then you don't need to worry about channel number clashes. For example, one side of the carrier could always call [`multiplex`](https://github.com/davedoesdev/bpmux#bpmuxprototypemultiplexoptions) and the other listen for [`handshake`](https://github.com/davedoesdev/bpmux#bpmuxeventshandshakeduplex-handshake_data-delay_handshake) events. Or they could take it in turns. If you can't synchronise both sides of the carrier, you can get one side to use a different range by specifying `high_channels` as `true`. The `BPMux` with `high_channels` set to `true` will assign channel numbers in the range [2^31..2^32).
- `{Integer} [max_open]` Maximum number of multiplexed streams that can be open at a time. Defaults to 0 (no maximum).
- `{Integer} [max_header_size]` `BPMux` adds a control header to each message it sends, which the receiver reads into memory. The header is of variable length — for example, handshake messages contain handshake data which can be supplied by the application. `max_header_size` is the maximum number of header bytes to read into memory. If a larger header is received, `BPMux` emits an `error` event. Defaults to 0 (no limit).
- `{Integer|false}` `keep_alive` Send a single byte keep-alive message every N milliseconds. Defaults to 30000 (30 seconds). Pass `false` to disable.
*/
function BPMux(carrier, options)
{
EventEmitter.call(this, options);
options = Object.assign(
{
max_open: 0,
max_header_size: 0,
keep_alive: 30 * 1000
}, options);
this._max_duplexes = Math.pow(2, 31);
this._max_open = options.max_open;
this.duplexes = new Map();
this._chan = 0;
this._chan_offset = options.high_channels ? this._max_duplexes : 0;
this._parse_handshake_data = options.parse_handshake_data;
this.carrier = carrier;
if (carrier instanceof Http2Sessions)
{
const http2 = require('http2');
const http2_options = options.http2 || {};
const response_headers = {
...http2_options.headers,
[http2.constants.HTTP2_HEADER_STATUS]: 200,
[http2.constants.HTTP2_HEADER_CONTENT_TYPE]: 'application/octet-stream',
};
carrier.server.on('stream', (duplex, headers) =>
{
if ((this._max_open > 0) && (this.duplexes.size === this._max_open))
{
this.emit('full');
return duplex.respond({
[http2.constants.HTTP2_HEADER_STATUS]: 503
}, {
endStream: true
});
}
const channel = Buffer.from(headers['bpmux-channel'], 'base64').readUint32BE();
if (this.duplexes.has(channel))
{
return duplex.respond({
[http2.constants.HTTP2_HEADER_STATUS]: 409
}, {
endStream: true
});
}
duplex.cork();
this._add_http2_duplex(duplex, channel);
this.emit('peer_multiplex', duplex);
let handshake_delayed = false;
this._parse_http2_handshake(duplex, headers, () =>
{
handshake_delayed = true;
let delayed_handshake;
const uncork = duplex.uncork;
duplex.uncork = () =>
{
duplex.uncork = uncork;
if (!duplex.destroyed) // Node 12 calls uncork on end even if destroyed
{
duplex.respond({
...response_headers,
...this._make_http2_handshake(delayed_handshake)
});
duplex._handshake_sent = true;
this.emit('handshake_sent', duplex, true);
duplex.emit('handshake_sent', true);
duplex.uncork();
}
};
return handshake =>
{
delayed_handshake = handshake;
duplex.uncork();
};
});
if (handshake_delayed)
{
this.emit('pre_handshake_sent', duplex, true);
duplex.emit('pre_handshake_sent', true);
}
else
{
duplex.respond({
...response_headers,
...this._make_http2_handshake()
});
duplex._handshake_sent = true;
this.emit('handshake_sent', duplex, true);
duplex.emit('handshake_sent', true);
duplex.uncork();
}
});
let closed = 0;
for (const session of [carrier.client, carrier.server])
{
session.on('close', () =>
{
// Note: http2 sessions only close once all their
// streams have closed so we don't need to go
// through the duplexes here and close them
if (++closed === 2)
{
this.emit('finish');
this.emit('end');
this.emit('close');
}
});
session.on('error', err =>
{
for (const duplex of this.duplexes.values())
{
if ((duplex.session === session) &&
!duplex.destroyed &&
(duplex.listenerCount('error') > 0))
{
duplex.emit('error', err);
}
}
this.emit('error', err);
});
}
return;
}
this._max_header_size = options.max_header_size;
this._finished = false;
this._ended = false;
this._header_buffers = [];
this._header_buffer_len = 0;
this._reading_duplex = null;
this._peer_multiplex_options = options.peer_multiplex_options;
this._coalesce_writes = options.coalesce_writes;
this._sending = false;
this._send_requested = false;
this._keep_alive_id = null;
this._keep_alive_paused = false;
this._out_stream = frame.encode(options);
if (this._coalesce_writes)
{
this._out_stream._pushFrameData = function (bufs)
{
var i;
for (i = 0; i < bufs.length; i += 1)
{
this.push(bufs[i]);
}
};
}
this._out_stream.pipe(carrier);
this._in_stream = frame.decode(Object.assign({}, options,
{
unbuffered: true
}));
carrier.pipe(this._in_stream);
var ths = this;
function check_close()
{
if (ths._finished && ths._ended)
{
ths.emit('close');
}
}
function finish()
{
if (ths._finished) { return; }
ths._finished = true;
clearInterval(ths._keep_alive_id);
for (var duplex of ths.duplexes.values())
{
if (!duplex._finished && !duplex.destroyed)
{
const err = new Error('carrier stream finished before duplex finished');
err.carrier_done = true;
duplex.destroy(err);
}
}
ths.emit('finish');
check_close();
}
function end()
{
if (ths._ended) { return; }
ths._ended = true;
for (var duplex of ths.duplexes.values())
{
if (!duplex._ended && !duplex.destroyed)
{
duplex._ended = true; // we won't get any more messages for it
const err = new Error('carrier stream ended before end message received');
err.carrier_done = true;
duplex.destroy(err);
}
}
ths.emit('end');
check_close();
}
carrier.on('finish', finish);
carrier.on('close', finish);
this._in_stream.on('end', end);
carrier.on('close', end);
function error(err)
{
// check_remove() is always called when _finished or _ended is set on a duplex,
// so it will have been removed from duplex. Apart from above in end(),
// where the duplex is destroyed, which we check below anyway (check_remove()
// will eventually be called there too via the duplex's 'close' handler).
for (var duplex of ths.duplexes.values())
{
if (!duplex._removed && // in case destroyed by previous iteration's error handler
!duplex.destroyed &&
(duplex.listenerCount('error') > 0))
{
duplex.emit('error', err);
}
}
ths.emit('error', err);
}
carrier.on('error', error);
this._in_stream.on('error', error);
this._out_stream.on('error', error);
this._out_stream.on('drain', function ()
{
ths._send();
if (this._writableState.length < this._writableState.highWaterMark)
{
ths.emit('drain');
}
});
this._in_stream.pipe(new Writable(
{
write: function (data, encoding, cb)
{
var duplex = ths._reading_duplex;
if (duplex)
{
if (data.frameEnd)
{
ths._reading_duplex = null;
}
if (!duplex._readableState.ended && !duplex.destroyed)
{
if (duplex._check_read_overflow &&
((duplex._readableState.length + data.length) >
duplex._readableState.highWaterMark))
{
duplex.emit('error', new Error('too much data'));
}
else
{
duplex.push(data);
}
}
}
else
{
if ((ths._max_header_size <= 0) ||
(ths._header_buffer_len < ths._max_header_size))
{
ths._header_buffers.push(data);
ths._header_buffer_len += data.length;
}
if (data.frameEnd)
{
if ((ths._max_header_size <= 0) ||
(ths._header_buffer_len < ths._max_header_size))
{
ths._process_header(Buffer.concat(ths._header_buffers,
ths._header_buffer_len));
}
else
{
ths.emit('error', new Error('header too big'));
}
ths._header_buffers = [];
ths._header_buffer_len = 0;
}
}
cb();
}
}));
if (options.keep_alive !== false)
{
const orig_end = this.carrier.end;
this.carrier.end = function ()
{
// If an error stops all data being written, we may not get 'finish'
clearInterval(ths._keep_alive_id);
return orig_end.apply(this, arguments);
};
this._out_stream.on('drain', function ()
{
ths._keep_alive_paused = false;
});
this._keep_alive_id = setInterval(function ()
{
ths._send_keep_alive();
}, options.keep_alive);
}
}
util.inherits(BPMux, EventEmitter);
BPMux.prototype._check_buffer = function (buf, size)
{
if (buf.length < size)
{
this.emit('error', new Error('short buffer length ' + buf.length + ' < ' + size));
return false;
}
return true;
};
BPMux.prototype._process_header = function (buf)
{
if ((buf.length > 0) && (buf[0] === TYPE_KEEP_ALIVE))
{
return this.emit('keep_alive');
}
if (!this._check_buffer(buf, 5)) { return; }
var ths = this,
type = buf.readUInt8(0, true),
chan = buf.readUInt32BE(1, true),
duplex = this.duplexes.get(chan);
function handle_status()
{
let free = buf.readUInt32BE(5, true);
const seq = buf.length === 13 ? buf.readUInt32BE(9, true) : 0;
free = duplex._max_write_size > 0 ?
Math.min(free, duplex._max_write_size) : free;
duplex._remote_free = seq + free - duplex._seq;
if (duplex._seq < seq)
{
duplex._remote_free -= max_seq;
}
duplex._set_remote_free = true;
ths._send();
}
if ((type !== TYPE_FINISHED_STATUS) && !duplex)
{