h2lean 0.10.2 → 0.11.0

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.
@@ -116,6 +116,7 @@ const tls = require('node:tls');
116
116
  const f = require('./frames');
117
117
  const { Decoder, Encoder, HPackError, latin1 } = require('./hpack');
118
118
  const FlatList = require('./flat-list');
119
+ const fastTick = require('./fast-tick');
119
120
  // The process-wide inbound decode scratch, same shape and size as the server
120
121
  // session's; the full why-sharing-is-safe argument lives with the server's
121
122
  // instance (lib/session.js). A module-level instance here too, not the server's
@@ -258,6 +259,11 @@ class ClientHttp2Session extends EventEmitter {
258
259
  }
259
260
  socket[kBoundSession] = this;
260
261
  this._socket = socket;
262
+ // Every deferred callback this session schedules runs through ONE route:
263
+ // node's own tick queue (default) or the shared batched pump of
264
+ // lib/fast-tick.js (`batchNextTick: true`) -- see session.js. Streams and
265
+ // anything else derived from the session copy this reference.
266
+ this._tick = fastTick.resolve(opts);
261
267
  // public `socket` returns node's safe Proxy, built lazily; close drops it (see session.js)
262
268
  this._proxySocket = null;
263
269
  this.type = 1; // NGHTTP2_SESSION_CLIENT
@@ -557,7 +563,7 @@ class ClientHttp2Session extends EventEmitter {
557
563
  // event fires. Without the deferral, this pattern misses the event:
558
564
  // const sess = lite.connect(url, { createConnection: () => alreadyOpenSocket });
559
565
  // sess.on('connect', handler); // attached after emit already ran synchronously
560
- process.nextTick(() => {
566
+ this._tick(() => {
561
567
  if (this.destroyed || this._closeEmitted) {
562
568
  return;
563
569
  }
@@ -916,7 +922,7 @@ class ClientHttp2Session extends EventEmitter {
916
922
  // node's requestOnError: destroy the stream with the captured error on the
917
923
  // next tick, and submit nothing. The id reserved above is never used -- the
918
924
  // session is destroyed or past its GOAWAY mark, so no later request takes it.
919
- process.nextTick(() => stream.destroy(requestError));
925
+ this._tick(() => stream.destroy(requestError));
920
926
  } else if (this._started) {
921
927
  this._enqueue(req);
922
928
  } else {
@@ -2743,7 +2749,7 @@ class ClientHttp2Session extends EventEmitter {
2743
2749
  this._maybeClose();
2744
2750
  if (typeof cb === 'function') {
2745
2751
  if (this._streams.size === 0) {
2746
- process.nextTick(cb);
2752
+ this._tick(cb);
2747
2753
  } else {
2748
2754
  this.once('close', cb);
2749
2755
  }
@@ -2926,7 +2932,7 @@ class ClientHttp2Session extends EventEmitter {
2926
2932
  socket.destroy();
2927
2933
  }
2928
2934
  }
2929
- process.nextTick(() => {
2935
+ this._tick(() => {
2930
2936
  if (!this._closeEmitted) {
2931
2937
  this._emitClose(err);
2932
2938
  }
@@ -47,6 +47,9 @@ class ClientHttp2Stream extends Duplex {
47
47
  super({ allowHalfOpen: true, decodeStrings: false });
48
48
  this.id = id;
49
49
  this.session = session;
50
+ // The session's one tick route -- node's queue or the batched pump of
51
+ // lib/fast-tick.js; see stream.js.
52
+ this._tick = session._tick;
50
53
  // Per-stream flow-control state; the connection window lives on the session.
51
54
  // Both start at the peer's advertised initial window (65535 until its SETTINGS
52
55
  // land) exactly as the server side does. The value written here is the
package/lib/compat.js CHANGED
@@ -53,7 +53,7 @@
53
53
  'use strict';
54
54
 
55
55
  const { Readable, Stream } = require('node:stream');
56
- const { emitNT } = require('./stream');
56
+
57
57
  const { nullProto, trimName } = require('./headers');
58
58
 
59
59
  function err(code, msg) {
@@ -201,13 +201,17 @@ function onRequestClose() {
201
201
  // 'close' after 'end', so queue it behind the end the push above scheduled.
202
202
  // node emits it right here instead, so the two stacks differ in the order of
203
203
  // these two: see the difference list at the top of this file.
204
- process.nextTick(emitCloseNT, req);
204
+ req._tick(emitCloseNT, req);
205
205
  }
206
206
 
207
207
  function emitCloseNT(target) {
208
208
  target.emit('close');
209
209
  }
210
210
 
211
+ function emitFinishNT(target) {
212
+ target.emit('finish');
213
+ }
214
+
211
215
  function onRequestTimeout() {
212
216
  const req = this._request;
213
217
  if (req !== undefined && !req._closed) {
@@ -256,6 +260,9 @@ class Http2ServerRequest extends Readable {
256
260
  constructor(stream, headers, rawHeaders) {
257
261
  super({ autoDestroy: false });
258
262
  this._stream = stream;
263
+ // The stream's one tick route -- node's queue or the batched pump of
264
+ // lib/fast-tick.js; the deferred callbacks of the compat layer go through it.
265
+ this._tick = stream._tick;
259
266
  this._headers = headers;
260
267
  this._rawHeaders = rawHeaders === null ? [] : rawHeaders;
261
268
  this._trailers = { __proto__: null };
@@ -446,6 +453,9 @@ function onResponseTimeout() {
446
453
  class Http2ServerResponse extends Stream {
447
454
  constructor(stream) {
448
455
  super();
456
+ // The stream's one tick route -- node's queue or the batched pump of
457
+ // lib/fast-tick.js; the deferred callbacks of the compat layer go through it.
458
+ this._tick = stream._tick;
449
459
  this._state = {
450
460
  closed: false,
451
461
  ending: false,
@@ -758,14 +768,14 @@ class Http2ServerResponse extends Stream {
758
768
  }
759
769
  if (failure) {
760
770
  if (typeof cb === 'function') {
761
- process.nextTick(cb, failure);
771
+ this._tick(cb, failure);
762
772
  }
763
773
  this.destroy(failure);
764
774
  return false;
765
775
  }
766
776
  if (state.headRequest) {
767
777
  if (typeof cb === 'function') {
768
- process.nextTick(cb);
778
+ this._tick(cb);
769
779
  }
770
780
  return true; // a HEAD response has no body
771
781
  }
@@ -786,7 +796,7 @@ class Http2ServerResponse extends Stream {
786
796
 
787
797
  if (state.closed || state.ending) {
788
798
  if (typeof cb === 'function') {
789
- process.nextTick(cb);
799
+ this._tick(cb);
790
800
  }
791
801
  return this;
792
802
  }
@@ -807,7 +817,7 @@ class Http2ServerResponse extends Stream {
807
817
  }
808
818
  if (!state.closed && !state.finishEmitted) {
809
819
  state.finishEmitted = true;
810
- process.nextTick(emitNT, this, 'finish');
820
+ this._tick(emitFinishNT, this);
811
821
  }
812
822
 
813
823
  // node's compat end() ends the stream and does nothing else
@@ -892,7 +902,7 @@ class Http2ServerResponse extends Stream {
892
902
  throw e;
893
903
  }
894
904
  if (this._state.closed) {
895
- process.nextTick(callback, err('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed'));
905
+ this._tick(callback, err('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed'));
896
906
  return;
897
907
  }
898
908
  this._stream.pushStream(headers, {}, (e, stream) => {
@@ -0,0 +1,115 @@
1
+ 'use strict';
2
+
3
+ // A batched replacement for process.nextTick, built like lib/fast-timer.js is
4
+ // built: one module, one pump shared by every session in the process, and the
5
+ // callback order as the contract. The application opts in per server or client
6
+ // with `batchNextTick: true` (default false = node's own process.nextTick);
7
+ // H2LEAN_BATCH_TICKS=1 turns it on wherever the option is left undefined, so
8
+ // the whole suite can run under the pump and pin its event orders.
9
+ //
10
+ // What it changes: instead of one tick-queue entry per callback, callbacks
11
+ // land in a flat array and ONE real nextTick -- the pump -- drains them, so a
12
+ // busy server pays the scheduler entry once per event-loop turn instead of
13
+ // once per request. bench/micro/tick-cost.js prices the entry at ~0.1 us
14
+ // isolated while the server-measured marginal of one merged tick is
15
+ // 0.03-0.05 us (OPTIMIZATION.md item 46, the pump's paired numbers in item
16
+ // 47), which is why this removes ALL of a session's entries rather than
17
+ // merging two.
18
+ //
19
+ // What it keeps: FIFO order exactly as node's single queue gives it --
20
+ // including callbacks scheduled while the pump drains, which land in the next
21
+ // generation (the double buffer below). That reproduces node's tail-of-the-
22
+ // queue semantics for everything h2lean schedules, which is why the suite
23
+ // passes with the pump on: test/event-order.test.js and the teardown traces
24
+ // pin the orders this depends on.
25
+ //
26
+ // What it cannot keep, and the option documents: node's ONE queue interleaves
27
+ // foreign ticks -- an application's own process.nextTick called from inside
28
+ // an event handler -- with ours in schedule order, while a pump generation
29
+ // runs as one queue entry, so such a foreign tick can observe a different
30
+ // position between two of our callbacks. And async-context propagation: a
31
+ // callback runs in the pump's context, not in the context that was current
32
+ // when it was scheduled, so an application reading AsyncLocalStorage inside
33
+ // a deferred 'finish' handler needs the default mode.
34
+ //
35
+ // On an exception inside the pump, the throwing pair is dropped and the
36
+ // callbacks after it are carried into the next generation, whose pump entry
37
+ // runs at the next turn. Measured on a live node: an exception escaping a
38
+ // tick aborts that turn's drain -- the ticks behind it stay in the queue and
39
+ // run at the next tick phase only if the event loop gets there (a pending
40
+ // tick queue alone does not start a turn, and without an uncaughtException
41
+ // handler the process dies), so the pump reproduces exactly that shape.
42
+ //
43
+ // const fastTick = require('./fast-tick');
44
+ // this._tick = fastTick.resolve(opts); // schedule | process.nextTick
45
+ // this._tick(fn, arg); // (fn, arg) shape, like nextTick
46
+
47
+ let ready = []; // callbacks awaiting the next pump run: [fn, arg, fn, arg, ...]
48
+ let idle = []; // the recycled array of the generation just drained
49
+ let scheduled = false; // a pump entry exists in the real tick queue
50
+
51
+ // Read at resolve() time, not at module load, so a process can turn the mode
52
+ // on after the library is required (the suite gate runs the whole tree under
53
+ // the pump this way). One env getter per session construction, never per tick.
54
+ function envForced() {
55
+ return process.env.H2LEAN_BATCH_TICKS === '1';
56
+ }
57
+
58
+ function pump() {
59
+ scheduled = false;
60
+ const list = ready;
61
+ // Re-entrant schedules during the drain push into `ready` -- the next
62
+ // generation -- and queue the next pump, which is node's tail-of-the-queue
63
+ // semantics: a callback scheduled while tick A runs executes after the
64
+ // ticks that were queued before A did.
65
+ ready = idle;
66
+ idle = list;
67
+ let i = 0;
68
+ try {
69
+ while (i < list.length) {
70
+ const fn = list[i];
71
+ fn(list[i + 1]);
72
+ i += 2;
73
+ }
74
+ } catch (err) {
75
+ // The ticks behind the throwing one stay queued on node too -- the drain
76
+ // aborts, the leftovers run at the next tick phase, if the loop gets
77
+ // there. Carry ours into the next generation, ahead of whatever the
78
+ // aborted drain scheduled there, and make sure a pump is queued. The
79
+ // failed pair is gone the way node's throwing tick is gone -- carrying it
80
+ // too re-runs the thrower at the head of every later generation and
81
+ // starves the survivors.
82
+ if (i + 2 < list.length) {
83
+ const rest = list.slice(i + 2);
84
+ for (let k = rest.length - 1; k >= 0; k--) {
85
+ ready.unshift(rest[k]);
86
+ }
87
+ }
88
+ if (!scheduled && ready.length !== 0) {
89
+ scheduled = true;
90
+ process.nextTick(pump);
91
+ }
92
+ throw err;
93
+ } finally {
94
+ list.length = 0;
95
+ }
96
+ }
97
+
98
+ function schedule(fn, arg) {
99
+ ready.push(fn, arg);
100
+ if (!scheduled) {
101
+ scheduled = true;
102
+ process.nextTick(pump);
103
+ }
104
+ }
105
+
106
+ // The scheduler a session runs all of its ticks through. One reference per
107
+ // session keeps that session coherent -- its ticks must not mix routes, or
108
+ // a pump generation could reorder against its own native entries. The pump
109
+ // itself is one instance for the whole process: sessions in batch mode share
110
+ // it, sessions in the default mode never meet it.
111
+ function resolve(opts) {
112
+ return ((opts && opts.batchNextTick) ?? envForced()) ? schedule : process.nextTick;
113
+ }
114
+
115
+ module.exports = { schedule, resolve };
package/lib/server.js CHANGED
@@ -15,6 +15,15 @@
15
15
  // keep-alive policy it served is session.setTimeout() plus
16
16
  // the application destroying the session, which both stacks
17
17
  // do implement. Harmless to keep, surprising to drop.
18
+ // * batchNextTick (createServer and createSecureServer; connect takes it too) is
19
+ // additive: with true, every deferred callback the session schedules runs through the
20
+ // shared pump of lib/fast-tick.js -- one real process.nextTick per event-loop turn
21
+ // instead of one per callback (OPTIMIZATION.md item 47: the stream arm -3.8%, the
22
+ // compat arm no measurable gain at saturation, which is why the default is node's own
23
+ // tick). The two behaviours the pump cannot reproduce -- a foreign tick's position
24
+ // between two of ours, and async-context propagation into the deferred callbacks --
25
+ // are in the fast-tick.js header; H2LEAN_BATCH_TICKS=1 fills in for an undefined
26
+ // option, so the suite pins the event orders with the pump on as well.
18
27
  // * what this server advertises on the wire matches node: the first SETTINGS frame carries only
19
28
  // the values the application asked for -- an EMPTY payload when it asked for nothing -- and a
20
29
  // later server.updateSettings() reaches the NEXT connection, not the live sessions. The local
@@ -107,6 +116,10 @@ function initHttp2Server(server, options) {
107
116
  // (internal/http2/util.js) takes it on a `typeof === 'number'` test alone -- so it rides
108
117
  // through raw and the session constructor applies the same test and the same coercion.
109
118
  maxSendHeaderBlockLength: opts.maxSendHeaderBlockLength,
119
+ // Rides through raw: the session resolves undefined (the default, node's own
120
+ // process.nextTick), true (the shared batched pump of lib/fast-tick.js) and
121
+ // explicit false alike, and H2LEAN_BATCH_TICKS=1 fills in for undefined only.
122
+ batchNextTick: opts.batchNextTick,
110
123
  // The server `origins` option: each session sends one ORIGIN frame with these origins at
111
124
  // connect. node only does it when the value is an array (core.js setupHandle), so it rides
112
125
  // through unvalidated and Http2Session._start applies the same Array.isArray test.
package/lib/session.js CHANGED
@@ -22,8 +22,9 @@ const { EventEmitter } = require('node:events');
22
22
  const tls = require('node:tls');
23
23
  const f = require('./frames');
24
24
  const { Decoder, Encoder, HPackError, latin1, latin1Into } = require('./hpack');
25
- const { Http2Stream, errorOf, streamErrorOf, emitNT } = require('./stream');
25
+ const { Http2Stream, errorOf, streamErrorOf, emitCloseNT, emitDrainNT } = require('./stream');
26
26
  const FlatList = require('./flat-list');
27
+ const fastTick = require('./fast-tick');
27
28
  // The process-wide inbound decode scratch: one list per module instance, shared by
28
29
  // every session it serves, instead of one array per live session. Why sharing is
29
30
  // safe: a header block's fill and its consumption are synchronous -- they happen
@@ -659,6 +660,12 @@ class Http2Session extends EventEmitter {
659
660
  }
660
661
  socket[kBoundSession] = this;
661
662
  this._socket = socket;
663
+ // Every deferred callback this session schedules runs through ONE route:
664
+ // node's own tick queue (default) or the shared batched pump of
665
+ // lib/fast-tick.js (`batchNextTick: true`). Mixing routes inside one
666
+ // session would let a pump generation reorder against its own native
667
+ // entries, so streams and the compat objects copy this reference.
668
+ this._tick = fastTick.resolve(opts);
662
669
  // The public `socket` accessor builds node's safe Proxy lazily and caches it here;
663
670
  // close sets it to undefined so `session.socket` reads gone afterwards, as node does.
664
671
  this._proxySocket = null;
@@ -946,7 +953,7 @@ class Http2Session extends EventEmitter {
946
953
  // do the same, then subscribe. 'connect' was queued by _start ahead of this drain, so
947
954
  // it fires first. An accepted createServer socket has nothing buffered here and takes
948
955
  // the synchronous subscription below, the read path this file always had.
949
- process.nextTick(() => {
956
+ this._tick(() => {
950
957
  if (this.destroyed) {
951
958
  return;
952
959
  }
@@ -976,7 +983,7 @@ class Http2Session extends EventEmitter {
976
983
  // (setupHandle's destroyed branch), so the handlers at least run and clear out.
977
984
  if (!this._connectEmitted) {
978
985
  this._connectEmitted = true;
979
- process.nextTick(() => this.emit('connect', this, this._socket));
986
+ this._tick(() => this.emit('connect', this, this._socket));
980
987
  }
981
988
  return;
982
989
  }
@@ -998,7 +1005,7 @@ class Http2Session extends EventEmitter {
998
1005
  this.origin(...this._origins);
999
1006
  }
1000
1007
  this._connectEmitted = true;
1001
- process.nextTick(() => this.emit('connect', this, this._socket));
1008
+ this._tick(() => this.emit('connect', this, this._socket));
1002
1009
  }
1003
1010
 
1004
1011
  // ---- outbound queue --------------------------------------------------------
@@ -3065,7 +3072,7 @@ class Http2Session extends EventEmitter {
3065
3072
  }
3066
3073
  // the writable side accepted bytes again: res.on('drain') is how an
3067
3074
  // application is allowed to resume writing after write() returned false
3068
- process.nextTick(emitNT, stream, 'drain');
3075
+ stream._tick(emitDrainNT, stream);
3069
3076
  if (stream._endQueued) {
3070
3077
  stream._endLocalSide();
3071
3078
  }
@@ -3291,7 +3298,7 @@ class Http2Session extends EventEmitter {
3291
3298
  // any of the five (out/probe-frameerror.js). This used to emit one on the session with
3292
3299
  // the id and the code swapped and DATA hardcoded as the type; the outbound sites
3293
3300
  // (_refuseSend, the PUSH_PROMISE bound) are the ones that match node's event.
3294
- process.nextTick(emitNT, stream, 'error', errorOf('ERR_HTTP2_STREAM_ERROR', msg));
3301
+ stream._tick(() => stream.emit('error', errorOf('ERR_HTTP2_STREAM_ERROR', msg)));
3295
3302
  }
3296
3303
 
3297
3304
  _fatal(code, msg) {
@@ -3497,7 +3504,7 @@ class Http2Session extends EventEmitter {
3497
3504
  // finishSessionClose() afterwards, which is the order the application sees: stream 'error',
3498
3505
  // stream 'close', session 'error', session 'close'.
3499
3506
  if (appError !== undefined) {
3500
- process.nextTick(emitNT, this, 'error', appError);
3507
+ this._tick(() => this.emit('error', appError));
3501
3508
  }
3502
3509
  return this;
3503
3510
  }
@@ -3517,7 +3524,7 @@ class Http2Session extends EventEmitter {
3517
3524
  return;
3518
3525
  }
3519
3526
  this._closeEmitted = true;
3520
- process.nextTick(emitNT, this, 'close');
3527
+ this._tick(emitCloseNT, this);
3521
3528
  }
3522
3529
 
3523
3530
  _onSocketError(err) {
@@ -3623,7 +3630,7 @@ class Http2Session extends EventEmitter {
3623
3630
  }
3624
3631
  this._sendSettings();
3625
3632
  if (cb) {
3626
- process.nextTick(cb);
3633
+ this._tick(cb);
3627
3634
  }
3628
3635
  }
3629
3636
 
package/lib/stream.js CHANGED
@@ -120,6 +120,18 @@ function emitNT(target, name, arg) {
120
120
  }
121
121
  }
122
122
 
123
+ // One-arg emitters for the per-request deliveries: the batched tick route of
124
+ // lib/fast-tick.js carries (fn, arg) pairs, so these replace the
125
+ // `emitNT(target, 'close' | 'drain')` calls the hot paths used, without
126
+ // paying a closure per event (the reason emitNT exists, item 41).
127
+ function emitCloseNT(target) {
128
+ target.emit('close');
129
+ }
130
+
131
+ function emitDrainNT(target) {
132
+ target.emit('drain');
133
+ }
134
+
123
135
  function emitErrorCloseNT(target, err) {
124
136
  target._errorEmitted = true;
125
137
  target._streamErrored = err;
@@ -236,7 +248,7 @@ function autoCloseNT(stream) {
236
248
  if (stream._destroyed) {
237
249
  return;
238
250
  }
239
- process.nextTick(emitEndNT, stream);
251
+ stream._tick(emitEndNT, stream);
240
252
  stream._destroy(NO_ERROR, undefined, true);
241
253
  }
242
254
 
@@ -382,6 +394,9 @@ class Http2Stream extends Stream {
382
394
  super();
383
395
  this.id = id;
384
396
  this.session = session;
397
+ // The session's one tick route -- node's queue or the batched pump of
398
+ // lib/fast-tick.js; every deferred callback of this stream goes through it.
399
+ this._tick = session._tick;
385
400
  this.rstCode = 0;
386
401
  // What respond() put on the wire, for the `sentHeaders` getter below: the caller's map
387
402
  // plus the two values the encoder resolved. node keeps the same three pieces
@@ -1034,7 +1049,7 @@ class Http2Stream extends Stream {
1034
1049
  // test/upstream-push.test.js, session-destroy).
1035
1050
  const err = errorOf('ERR_STREAM_WRITE_AFTER_END', 'write after end');
1036
1051
  if (cb) {
1037
- process.nextTick(cb, err);
1052
+ this._tick(cb, err);
1038
1053
  }
1039
1054
  if (!this._destroyed) {
1040
1055
  this.destroy(err);
@@ -1046,7 +1061,7 @@ class Http2Stream extends Stream {
1046
1061
  // the event is owed here instead. A reset or a session destroy is not
1047
1062
  // this shape -- there node's errorOrDestroy is a no-op and no 'error'
1048
1063
  // goes out (the session-destroy case of test/upstream-push.test.js).
1049
- process.nextTick(emitErrorOnlyNT, this, err);
1064
+ this._tick(() => emitErrorOnlyNT(this, err));
1050
1065
  }
1051
1066
  return false;
1052
1067
  }
@@ -1060,7 +1075,7 @@ class Http2Stream extends Stream {
1060
1075
  // session's drain path). It reports the credit stall, nothing else.
1061
1076
  this._needDrain = !canContinue;
1062
1077
  if (cb) {
1063
- process.nextTick(cb);
1078
+ this._tick(cb);
1064
1079
  }
1065
1080
  return canContinue;
1066
1081
  }
@@ -1083,7 +1098,7 @@ class Http2Stream extends Stream {
1083
1098
  // _pumpEnd() for exactly that reason; write() after end IS the error -- see
1084
1099
  // write() above.
1085
1100
  if (cb) {
1086
- process.nextTick(cb);
1101
+ this._tick(cb);
1087
1102
  }
1088
1103
  return this;
1089
1104
  }
@@ -1102,7 +1117,7 @@ class Http2Stream extends Stream {
1102
1117
  // the auto-respond above already carried END_STREAM (a HEAD request):
1103
1118
  // there is no body to send, RFC 9113 8.1 forbids one on a HEAD answer
1104
1119
  if (cb) {
1105
- process.nextTick(cb);
1120
+ this._tick(cb);
1106
1121
  }
1107
1122
  return this;
1108
1123
  }
@@ -1120,7 +1135,7 @@ class Http2Stream extends Stream {
1120
1135
  this._endLocalSide();
1121
1136
  }
1122
1137
  if (cb) {
1123
- process.nextTick(cb);
1138
+ this._tick(cb);
1124
1139
  }
1125
1140
  return this;
1126
1141
  }
@@ -1405,7 +1420,7 @@ class Http2Stream extends Stream {
1405
1420
  try {
1406
1421
  promisedId = this.session._allocPushSid();
1407
1422
  } catch (err) {
1408
- process.nextTick(callback, err);
1423
+ this._tick(callback, err);
1409
1424
  return;
1410
1425
  }
1411
1426
 
@@ -1445,7 +1460,7 @@ class Http2Stream extends Stream {
1445
1460
  // what keeps that shape; respond() reads _endQueued, write() refuses it.
1446
1461
  stream._endQueued = true;
1447
1462
  }
1448
- process.nextTick(callback, null, stream, h, 0);
1463
+ this._tick(() => callback(null, stream, h, 0));
1449
1464
  }
1450
1465
 
1451
1466
  // Readable-side control, node's contract: while paused no credit is released,
@@ -1474,7 +1489,7 @@ class Http2Stream extends Stream {
1474
1489
  return;
1475
1490
  }
1476
1491
  this._resumeScheduled = true;
1477
- process.nextTick(() => {
1492
+ this._tick(() => {
1478
1493
  this._resumeScheduled = false;
1479
1494
  if (this._destroyed || this._flowing === false) {
1480
1495
  return;
@@ -1893,7 +1908,7 @@ class Http2Stream extends Stream {
1893
1908
  dest !== process.stdout && dest !== process.stderr;
1894
1909
  const endFn = doEnd ? onend : unpipe;
1895
1910
  if (src._eofEmitted === true) {
1896
- process.nextTick(endFn);
1911
+ src._tick(endFn);
1897
1912
  } else {
1898
1913
  src.once('end', endFn);
1899
1914
  }
@@ -2294,7 +2309,7 @@ class Http2Stream extends Stream {
2294
2309
  return;
2295
2310
  }
2296
2311
  this._readableScheduled = true;
2297
- process.nextTick(() => {
2312
+ this._tick(() => {
2298
2313
  this._readableScheduled = false;
2299
2314
  if (this._destroyed || this._flowing === true ||
2300
2315
  this.listenerCount('readable') === 0) {
@@ -2316,7 +2331,7 @@ class Http2Stream extends Stream {
2316
2331
  return;
2317
2332
  }
2318
2333
  this._eofScheduled = true;
2319
- process.nextTick(() => {
2334
+ this._tick(() => {
2320
2335
  this._eofScheduled = false;
2321
2336
  if (this._destroyed || this._eofEmitted === true) {
2322
2337
  return;
@@ -2384,7 +2399,7 @@ class Http2Stream extends Stream {
2384
2399
  // the stream close after both; ending here sent the HEADERS only and the client
2385
2400
  // waited for an answer that never came.
2386
2401
  this._closeNow();
2387
- process.nextTick(emitEndCloseNT, this);
2402
+ this._tick(emitEndCloseNT, this);
2388
2403
  return;
2389
2404
  }
2390
2405
  if (this._state.state === ST_OPEN) {
@@ -2406,7 +2421,7 @@ class Http2Stream extends Stream {
2406
2421
  }
2407
2422
  return;
2408
2423
  }
2409
- process.nextTick(emitEndNT, this);
2424
+ this._tick(emitEndNT, this);
2410
2425
  }
2411
2426
 
2412
2427
  // The writable-side 'finish' -- node's Writable fires it once every write has been
@@ -2423,7 +2438,7 @@ class Http2Stream extends Stream {
2423
2438
  return;
2424
2439
  }
2425
2440
  this._finishQueued = true;
2426
- process.nextTick(finishNT, this);
2441
+ this._tick(finishNT, this);
2427
2442
  }
2428
2443
 
2429
2444
  _endLocalSide() {
@@ -2462,7 +2477,7 @@ class Http2Stream extends Stream {
2462
2477
  // one shot: a later end() must not ask for trailers a second time. The flag
2463
2478
  // itself stays set, because sendTrailers() is only allowed while it is set.
2464
2479
  if (first) {
2465
- process.nextTick(wantTrailersNT, this);
2480
+ this._tick(wantTrailersNT, this);
2466
2481
  }
2467
2482
  return;
2468
2483
  }
@@ -2520,10 +2535,10 @@ class Http2Stream extends Stream {
2520
2535
  // stream.end then stream.close (test/event-order.test.js, scenario eof).
2521
2536
  if (this._endPending || this._eofSeen === true) {
2522
2537
  this._endPending = false;
2523
- process.nextTick(emitEndCloseNT, this);
2538
+ this._tick(emitEndCloseNT, this);
2524
2539
  return;
2525
2540
  }
2526
- process.nextTick(emitCloseDeliveredNT, this);
2541
+ this._tick(emitCloseDeliveredNT, this);
2527
2542
  }
2528
2543
 
2529
2544
  _closeNow() {
@@ -2552,7 +2567,7 @@ class Http2Stream extends Stream {
2552
2567
  // position (and the post-destroy session view) finishNT had when it was always
2553
2568
  // scheduled from _endLocalSide.
2554
2569
  this._finishFuse = false;
2555
- process.nextTick(finishNT, this);
2570
+ this._tick(finishNT, this);
2556
2571
  }
2557
2572
  this._destroyed = true;
2558
2573
  // node's destroy does not touch state.flowing — the readableFlowing getter keeps
@@ -2597,7 +2612,7 @@ class Http2Stream extends Stream {
2597
2612
  // node's closeStream emits 'aborted' before _destroy runs.
2598
2613
  this.session = undefined;
2599
2614
  if (err) {
2600
- process.nextTick(emitErrorCloseNT, this, err);
2615
+ this._tick(() => emitErrorCloseNT(this, err));
2601
2616
  } else if (this._isConsuming() &&
2602
2617
  (this._endPending || code === NO_ERROR || code === frames.CANCEL)) {
2603
2618
  // node's _destroy (internal/http2/core.js:2527) calls `this.push(null)`
@@ -2615,9 +2630,9 @@ class Http2Stream extends Stream {
2615
2630
  // reader is cancelled by the destroy bit and the owed 'end' never goes out
2616
2631
  // (test/event-order.test.js, scenario reset).
2617
2632
  this._endPending = false;
2618
- process.nextTick(emitEndCloseNT, this);
2633
+ this._tick(emitEndCloseNT, this);
2619
2634
  } else {
2620
- process.nextTick(emitNT, this, 'close');
2635
+ this._tick(emitCloseNT, this);
2621
2636
  }
2622
2637
  }
2623
2638
 
@@ -2984,9 +2999,9 @@ function createStreamIterator(stream, options) {
2984
2999
  // The one-shot trio below can never fire again, so this is where that
2985
3000
  // verdict is reproduced.
2986
3001
  if (stream.readableEnded === true) {
2987
- process.nextTick(() => settle(null));
3002
+ stream._tick(() => settle(null));
2988
3003
  } else if (stream.destroyed === true) {
2989
- process.nextTick(() => settle(errorOf('ERR_STREAM_PREMATURE_CLOSE', 'Premature close')));
3004
+ stream._tick(() => settle(errorOf('ERR_STREAM_PREMATURE_CLOSE', 'Premature close')));
2990
3005
  }
2991
3006
  }
2992
3007
 
@@ -3468,4 +3483,4 @@ function streamErrorOf(code) {
3468
3483
  `Stream closed with error code ${frames.ERROR_NAMES[code] || code}`);
3469
3484
  }
3470
3485
 
3471
- module.exports = { Http2Stream, errorOf, streamErrorOf, emitNT };
3486
+ module.exports = { Http2Stream, errorOf, streamErrorOf, emitNT, emitCloseNT, emitDrainNT };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.10.2",
3
+ "version": "0.11.0",
4
4
  "description": "Pure-JavaScript HTTP/2 for Node.js — a drop-in node:http2 replacement (server and client, h2c and TLS) with zero dependencies",
5
5
  "main": "index.js",
6
6
  "files": [