h2lean 0.19.6 → 0.19.7

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.
@@ -61,13 +61,15 @@
61
61
  // (measured, tools/probe-client-status.js; this closes the empty-block
62
62
  // divergence).
63
63
  //
64
- // KNOWN DIVERGENCE: a stream leaves _streams when its response ends (_dropStream on
65
- // END_STREAM), while node keeps it in state.streams until the Duplex is destroyed. A
66
- // RST_STREAM that arrives after END_STREAM therefore finds nothing here and is ignored,
67
- // so a request whose body was never ended -- or whose response body is still buffered
68
- // unread -- never closes: node reports aborted/finish/close, h2lean waits forever.
69
- // _wireDone already tracks these streams for the graceful close; the reset path does not
70
- // consult it. Pinned as the excluded shape in test/client/client-rst.test.js.
64
+ // A stream leaves _streams only when its Duplex closes (_trackClose), the way node
65
+ // removes it from state.streams only in the stream's destroy path (core.js:2531). END_STREAM
66
+ // ends the wire half but not the stream: a request whose writable was never ended, or
67
+ // whose response body is still buffered unread, stays reachable so a later RST_STREAM
68
+ // still reaches onStreamClose (core.js:607) -- aborted/finish/close with rstCode 0 on a
69
+ // NO_ERROR reset, or a deferred close once the buffered body is consumed. _dropStream's
70
+ // `keep` argument is that half-closed state; the bookkeeping it still does is what the
71
+ // graceful close needs. Measured with tools/probe-client-unfinished-post.js and pinned in
72
+ // test/client/client-rst.test.js.
71
73
  //
72
74
  // settings() and the initial submission take customSettings, node-shaped and measured on
73
75
  // a live node 26 client against a raw frame-decoding peer (test/custom-settings.test.js):
@@ -334,6 +336,12 @@ class Http2ClientSession extends EventEmitter {
334
336
  this._maxReservedRemoteStreams = opts.maxReservedRemoteStreams === undefined
335
337
  ? 200 : opts.maxReservedRemoteStreams;
336
338
  this._streams = new Map();
339
+ // Streams whose wire life is still open. A stream that took END_STREAM stops
340
+ // counting against the peer's max_concurrent_streams as soon as nghttp2 closes
341
+ // its C-side handle, even while its Duplex is finishing on the JS side. The id
342
+ // stays in _streams until 'close' (so a late RST can find it), so concurrency
343
+ // has to use this count rather than the map's size.
344
+ this._liveWire = 0;
337
345
  // Streams whose wire life is over (_dropStream) but whose Duplex has not
338
346
  // emitted 'close' yet -- an unread response body keeps the readable half
339
347
  // open. node's session counts a stream until it is destroyed, not until
@@ -1062,7 +1070,7 @@ class Http2ClientSession extends EventEmitter {
1062
1070
  _enqueue(req) {
1063
1071
  const max = this.remoteSettings.maxConcurrentStreams;
1064
1072
  if (this._outboundQueue.length === 0
1065
- && (max === undefined || this._streams.size < max)) {
1073
+ && (max === undefined || this._liveWire < max)) {
1066
1074
  this._openStream(req);
1067
1075
  } else {
1068
1076
  this._outboundQueue.push(req);
@@ -1078,6 +1086,7 @@ class Http2ClientSession extends EventEmitter {
1078
1086
  // the peer's number and not the 65535 default the peer may never have chosen.
1079
1087
  req.stream._sendWin = this.remoteSettings.initialWindowSize;
1080
1088
  req.stream._opened = true;
1089
+ this._liveWire++;
1081
1090
  this._streams.set(req.stream.id, req.stream);
1082
1091
  this._sendRequest(req.stream, req.list, req.nl, req.end, req.hp);
1083
1092
  }
@@ -1089,7 +1098,7 @@ class Http2ClientSession extends EventEmitter {
1089
1098
  return;
1090
1099
  }
1091
1100
  const max = this.remoteSettings.maxConcurrentStreams;
1092
- while (this._outboundQueue.length > 0 && (max === undefined || this._streams.size < max)) {
1101
+ while (this._outboundQueue.length > 0 && (max === undefined || this._liveWire < max)) {
1093
1102
  const req = this._outboundQueue.shift();
1094
1103
  if (this.closed && req.stream.id > (this._goawayLastStreamID || 0)) {
1095
1104
  // past the peer's GOAWAY mark: this request will not be carried; surface it.
@@ -2504,6 +2513,13 @@ class Http2ClientSession extends EventEmitter {
2504
2513
  }
2505
2514
  return this._fatal(PROTOCOL_ERROR, 'response HEADERS on an unopened stream id');
2506
2515
  }
2516
+ // nghttp2 silently drops a block for a stream both of whose wire halves are
2517
+ // done (client-fatal.test.js: a second HEADERS/ES on a completed id). The id
2518
+ // stays in _streams until the Duplex closes, so the old "no stream" branch no
2519
+ // longer catches this shape; without this guard it would read as trailers.
2520
+ if (stream._rEof && stream._endSent) {
2521
+ return;
2522
+ }
2507
2523
  if (this._refuseHeaders(sid, stream, fields, fcount)) {
2508
2524
  return;
2509
2525
  }
@@ -2534,7 +2550,7 @@ class Http2ClientSession extends EventEmitter {
2534
2550
  }
2535
2551
  stream._onHeaders(status, fields, fcount, sensitive, end, flags);
2536
2552
  if (end) {
2537
- this._dropStream(sid);
2553
+ this._dropStream(sid, true);
2538
2554
  }
2539
2555
  }
2540
2556
 
@@ -2660,7 +2676,7 @@ class Http2ClientSession extends EventEmitter {
2660
2676
  // shared buffer it reused -- that buffer is gone.
2661
2677
  stream._onData(buf.subarray(p, p + l));
2662
2678
  if (flags & F_END_STREAM) {
2663
- stream._endRead(); this._dropStream(sid);
2679
+ stream._endRead(); this._dropStream(sid, true);
2664
2680
  }
2665
2681
  }
2666
2682
 
@@ -2689,7 +2705,7 @@ class Http2ClientSession extends EventEmitter {
2689
2705
  }
2690
2706
  }
2691
2707
  if (flags & F_END_STREAM) {
2692
- stream._endRead(); this._dropStream(sid);
2708
+ stream._endRead(); this._dropStream(sid, true);
2693
2709
  }
2694
2710
  }
2695
2711
 
@@ -2727,9 +2743,17 @@ class Http2ClientSession extends EventEmitter {
2727
2743
 
2728
2744
  _onRst(sid, code) {
2729
2745
  const stream = this._streams.get(sid);
2730
- if (stream === undefined) {
2746
+ // node's onStreamClose guard (core.js:609): a stream already destroyed -- including
2747
+ // one whose END_STREAM and finished writable just auto-destroyed it on a tick that
2748
+ // has not reached its 'close' yet -- is gone at the C layer; its reset is ignored.
2749
+ // So is one whose two wire halves are both done: nghttp2 has closed that stream and
2750
+ // hands no further close callback to JS, which is why the reset after a completed
2751
+ // request keeps rstCode 0 (test/client/client-rst.test.js).
2752
+ if (stream === undefined || stream._destroyFlag ||
2753
+ (stream._rEof && stream._endSent)) {
2731
2754
  return;
2732
2755
  }
2756
+ stream._flushWireEnd();
2733
2757
  this._dropStream(sid);
2734
2758
  stream._onRst(code);
2735
2759
  }
@@ -2739,7 +2763,12 @@ class Http2ClientSession extends EventEmitter {
2739
2763
  // is gone -- including the inbound-RST path, where _onRst has already dropped it,
2740
2764
  // so we never answer a reset with a reset of our own.
2741
2765
  rstStream(sid, code) {
2742
- if (this.destroyed || !this._open || !this._streams.has(sid)) {
2766
+ const stream = this._streams.get(sid);
2767
+ // A stream whose two wire halves are done is closed in nghttp2 even while its
2768
+ // Duplex is finishing: an application destroy then finds no handle to submit a
2769
+ // reset through (client-destroy-code.test.js, the closed-stream cases).
2770
+ if (this.destroyed || !this._open || stream === undefined ||
2771
+ (stream._rEof && stream._endSent)) {
2743
2772
  return;
2744
2773
  }
2745
2774
  const body = Buffer.allocUnsafe(4);
@@ -3303,11 +3332,18 @@ class Http2ClientSession extends EventEmitter {
3303
3332
 
3304
3333
  // The 'close' subscription is installed at construction, before any event can
3305
3334
  // fire, so the wire-done bookkeeping never races the Duplex's own teardown.
3306
- // It is the one subscription per stream the session keeps: the mark it clears
3307
- // is what lets a graceful close outlive an unread response body (see _wireDone).
3335
+ // It is the one subscription per stream the session keeps: the marks it clears are
3336
+ // what let a graceful close outlive an unread response body (see _wireDone) and what
3337
+ // finally retire a half-closed stream from the id map (see _dropStream's `keep`).
3308
3338
  _trackClose(stream) {
3309
3339
  stream.on('close', () => {
3310
3340
  this._wireDone.delete(stream);
3341
+ if (this._streams.get(stream.id) === stream) {
3342
+ this._streams.delete(stream.id);
3343
+ // A queued request was waiting for this stream's concurrency slot. The old
3344
+ // _dropStream did the draining at END_STREAM; now the id is retired here.
3345
+ this._drainQueue();
3346
+ }
3311
3347
  if (this.destroyed && !this._closeEmitted && !this._destroyScheduled
3312
3348
  && this._wireDone.size === 0) {
3313
3349
  // The socket is already gone (_onSocketClose deferred to this moment):
@@ -3344,16 +3380,28 @@ class Http2ClientSession extends EventEmitter {
3344
3380
  }
3345
3381
  }
3346
3382
 
3347
- _dropStream(sid) {
3383
+ // `keep` is the inbound END_STREAM shape: the remote half is done but the Duplex may
3384
+ // still owe its own 'finish'/'close', and a RST_STREAM can arrive in that window. node
3385
+ // keeps the id in state.streams until the stream's destroy (core.js:2531), so the id
3386
+ // stays here too; _trackClose retires it. Every other caller has finished the stream's
3387
+ // wire life for good and drops the id at once.
3388
+ _dropStream(sid, keep) {
3348
3389
  const stream = this._streams.get(sid);
3349
3390
  if (stream !== undefined && stream._debtMarked) {
3350
3391
  this._removeDebt(stream);
3351
3392
  }
3352
- this._streams.delete(sid);
3393
+ if (!keep) {
3394
+ this._streams.delete(sid);
3395
+ }
3353
3396
  if (stream !== undefined) {
3354
- // The wire life is over; the Duplex may still owe 'end'/'close' (an unread
3355
- // body). The session's graceful close waits for that 'close' (_wireDone).
3356
- this._wireDone.add(stream);
3397
+ if (!this._wireDone.has(stream)) {
3398
+ // Counted once: a second _dropStream for the same half-closed stream must
3399
+ // not free the concurrency slot twice.
3400
+ this._liveWire--;
3401
+ // The wire life is over; the Duplex may still owe 'end'/'close' (an unread
3402
+ // body). The session's graceful close waits for that 'close' (_wireDone).
3403
+ this._wireDone.add(stream);
3404
+ }
3357
3405
  }
3358
3406
  this._drainQueue();
3359
3407
  this._maybeClose();
@@ -924,7 +924,7 @@ class Http2ClientStream extends Http2Stream {
924
924
  // iterator's settle(null) outruns the destroy's 'error' pair and a
925
925
  // helper started mid-teardown answers a clean done (the some/every
926
926
  // shape, measured on node v26.12.1: every() rejects ABORT_ERR).
927
- if (this._errored !== undefined) {
927
+ if (this._errored !== undefined || this._endDelivered === true) {
928
928
  return;
929
929
  }
930
930
  this._endDelivered = true;
@@ -933,6 +933,19 @@ class Http2ClientStream extends Http2Stream {
933
933
  });
934
934
  }
935
935
 
936
+ // The wire's END_STREAM is delivered inside node's socket read (onStreamClose calls
937
+ // stream.read(0) there), so a RST_STREAM in the same read batch is reported after 'end'.
938
+ // h2lean queues that 'end' on a tick; flush it first when the reset arrives, or the
939
+ // reset's 'aborted' would invert the pair. The scheduled tick then sees _endDelivered
940
+ // and delivers nothing twice.
941
+ _flushWireEnd() {
942
+ if (this._endEmitted === true && this._endDelivered !== true) {
943
+ this._endDelivered = true;
944
+ this.emit('end');
945
+ this._maybeAutoClose();
946
+ }
947
+ }
948
+
936
949
  _endRead() {
937
950
  if (this._remoteEnded) {
938
951
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.19.6",
3
+ "version": "0.19.7",
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": [