h2lean 0.18.1 → 0.19.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.
@@ -125,6 +125,7 @@
125
125
  'use strict';
126
126
 
127
127
  const { EventEmitter } = require('node:events');
128
+ const net = require('node:net');
128
129
  const tls = require('node:tls');
129
130
  const f = require('../frames');
130
131
  const { Decoder, Encoder, HPackError } = require('../hpack');
@@ -162,6 +163,9 @@ const {
162
163
 
163
164
  const MAX_SETTINGS_ENTRIES = 32; // CVE-2020-11080; same bound as the server
164
165
  const MAX_CONTINUATIONS = 8; // CVE-2024-28182; same bound as the server
166
+ // Cap on the pool of completed staging buffers, so what one client retains for reuse is
167
+ // bounded independently of how many batches it sends. Same number as the server's.
168
+ const MAX_STAGE_POOL = 32;
165
169
  // nghttp2 refuses a SETTINGS submit once ten frames are awaiting an ACK, and node
166
170
  // turns that refusal into a destroyed session (core.js submitSettings: a false return
167
171
  // from handle.settings() -> destroy(ERR_HTTP2_MAX_PENDING_SETTINGS_ACK)). Measured on
@@ -428,6 +432,25 @@ class Http2ClientSession extends EventEmitter {
428
432
  this._stage = null;
429
433
  this._stageLen = 0; // bytes of _stage not yet queued
430
434
  this._stageCap = STAGE_MIN; // size of the next staging buffer
435
+ this._reuseBuffers = opts.reuseBuffers !== false;
436
+ // A pool is per connection here: connect() has no server object to own a shared one,
437
+ // so a caller that wants one across sessions passes it as `stagingPool`. Default on;
438
+ // `false` keeps the allocate-per-batch behaviour (`_pool === null`).
439
+ this._pool = this._reuseBuffers
440
+ ? (opts.stagingPool === undefined ? [] : opts.stagingPool) : null;
441
+ // _pending collects the full stage buffers of a batch; _returning is the batch whose
442
+ // write callback is still owed, so only one list is in flight at a time (server's shape).
443
+ this._pending = [];
444
+ this._returning = null;
445
+ // A net/TLS socket copies (or encrypts) the bytes at flush, so its write callback is
446
+ // a real ownership boundary and a completed stage can be written into again. A
447
+ // generic stream (stream.duplexPair, a custom createConnection) can push the very
448
+ // buffer we hand it to its peer and hold it until the peer reads -- the callback then
449
+ // fires while the peer is still parsing, and a reused stage would rewrite bytes
450
+ // already promised. Measured on the paused-duplexPair case in
451
+ // test/server-handshake.test.js: GOAWAY(COMPRESSION_ERROR). Those sockets keep the
452
+ // old allocate-per-batch path; TLSSocket is a net.Socket, so one check covers both.
453
+ this._canPool = this._pool !== null && socket instanceof net.Socket;
431
454
  this._outLen = 0; // bytes the socket has not been given
432
455
  this._writing = false; // one batch in flight per connection
433
456
  this._flushScheduled = false;
@@ -666,7 +689,9 @@ class Http2ClientSession extends EventEmitter {
666
689
  cap *= 2;
667
690
  }
668
691
  this._stageCap = Math.min(cap, MAX_OUT_BATCH);
669
- this._stage = Buffer.allocUnsafe(cap);
692
+ const reused = !this._canPool ? undefined : this._pool.pop();
693
+ this._stage = reused !== undefined && reused.length >= cap
694
+ ? reused : Buffer.allocUnsafe(cap);
670
695
  }
671
696
 
672
697
  _commit() {
@@ -674,6 +699,7 @@ class Http2ClientSession extends EventEmitter {
674
699
  return;
675
700
  }
676
701
  this._outQ.push(this._stage.subarray(0, this._stageLen));
702
+ this._pending.push(this._stage); // keep the whole buffer, for reuse after the write
677
703
  this._stage = null; // the queued bytes are not ours to write
678
704
  this._stageLen = 0;
679
705
  }
@@ -726,6 +752,9 @@ class Http2ClientSession extends EventEmitter {
726
752
  this._commit();
727
753
  const batch = this._outQ;
728
754
  this._outQ = [];
755
+ const buffers = this._pending;
756
+ this._pending = [];
757
+ this._returning = buffers;
729
758
  this._outLen = 0;
730
759
  const s = this._socket;
731
760
  if (batch.length === 1) {
@@ -747,6 +776,19 @@ class Http2ClientSession extends EventEmitter {
747
776
 
748
777
  _onWritten() {
749
778
  this._writing = false;
779
+ // The callback is the ownership boundary: every buffer of the batch is the socket's
780
+ // no longer, so _need can write into one of them again. Cap what we keep so a quiet
781
+ // connection cannot grow the pool without bound.
782
+ const done = this._returning;
783
+ this._returning = null;
784
+ if (done !== null && this._canPool) {
785
+ for (const b of done) {
786
+ if (this._pool.length >= MAX_STAGE_POOL) {
787
+ break;
788
+ }
789
+ this._pool.push(b);
790
+ }
791
+ }
750
792
  // Dropping the batch is the whole release: the frames were never assembled into
751
793
  // anything shared, so there is no tail to move down and nothing to reset.
752
794
  this._writePending();
@@ -759,9 +801,16 @@ class Http2ClientSession extends EventEmitter {
759
801
  }
760
802
 
761
803
  _discardOut() {
804
+ // The uncommitted stage never reached the socket, so it is ours to hand back; the
805
+ // committed ones in _pending/_returning may be in flight and are dropped, not reused.
806
+ if (this._stage !== null && this._canPool && this._pool.length < MAX_STAGE_POOL) {
807
+ this._pool.push(this._stage);
808
+ }
762
809
  this._outQ = [];
763
810
  this._stage = null;
764
811
  this._stageLen = 0;
812
+ this._pending = [];
813
+ this._returning = null;
765
814
  this._outLen = 0;
766
815
  }
767
816
 
@@ -2999,6 +3048,11 @@ class Http2ClientSession extends EventEmitter {
2999
3048
  const batch = this._outQ;
3000
3049
  this._outQ = [];
3001
3050
  this._outLen = 0;
3051
+ // These buffers are handed to the socket with NO callback and a terminal close
3052
+ // follows, so the socket may still be reading them after this returns. Never put
3053
+ // them back in the pool: drop both lists so the dying session retains nothing.
3054
+ this._pending = [];
3055
+ this._returning = null;
3002
3056
  // _commit drops the staging buffer only when it held bytes; _discardOut dropped it
3003
3057
  // either way, and so does this.
3004
3058
  this._stage = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.18.1",
3
+ "version": "0.19.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": [