h2lean 0.19.5 → 0.19.6

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.
@@ -2180,10 +2180,17 @@ class Http2ClientSession extends EventEmitter {
2180
2180
  this._hdrLen += len;
2181
2181
  return;
2182
2182
  }
2183
+ // Size the first buffer for the frame in hand: a 16 KiB HEADERS would
2184
+ // otherwise allocate the 8 KiB default and immediately discard it for a
2185
+ // doubled one. The backing store stays with the session, so the first
2186
+ // capacity is the one the whole block is assembled into.
2183
2187
  if (this._hdr === null) {
2184
- this._hdr = Buffer.allocUnsafe(8192);
2185
- }
2186
- if (this._hdrLen + len > this._hdr.length) {
2188
+ let cap = STAGE_MIN;
2189
+ while (cap < len) {
2190
+ cap *= 2;
2191
+ }
2192
+ this._hdr = Buffer.allocUnsafe(cap);
2193
+ } else if (this._hdrLen + len > this._hdr.length) {
2187
2194
  let cap = this._hdr.length * 2;
2188
2195
  while (cap < this._hdrLen + len) {
2189
2196
  cap *= 2;
@@ -1043,14 +1043,22 @@ const DEFAULT_MAX_OUTSTANDING_PINGS = 10;
1043
1043
  // (util.js:279, MathMax(1, ...)) and multiplied by 1 000 000 by the C++ layer
1044
1044
  // (node_http2.cc:210), whose default is 10 000 000 (node_http2.h:40).
1045
1045
  const DEFAULT_MAX_SESSION_MEMORY = 10 * 1000 * 1000;
1046
- // node charges sizeof(Http2Stream) for each live stream (node_http2.cc:915/925)
1047
- // plus name.length + value.length + 32 for each header (:2745). sizeof is a C++
1048
- // compile-time constant not present in the JS source; it was derived from a
1049
- // live node 26 server at a 1 MB budget with a minimal request block: 1028
1050
- // streams were accepted, and the block costs ~180 bytes, leaving ~794 for the
1051
- // stream record. 800 is the rounded charge; the budget is a bound, not a byte
1052
- // counter, and the test pins the refusal shape rather than the exact stream.
1053
- const STREAM_MEM_CHARGE = 800;
1046
+ // node checks and charges sizeof(Http2Stream) per live stream
1047
+ // (node_http2.cc:915/925) plus name.length + value.length + 32 per header
1048
+ // (:2745). sizeof is a C++ compile-time constant: compiled against the v26.12.1
1049
+ // headers it is 360 octets, not the ~800 an earlier indirect estimate assigned
1050
+ // it. nghttp2 also charges its own per-stream allocation to the same budget
1051
+ // (current_session_memory(), node_http2.h:792); h2lean has no nghttp2, so it
1052
+ // adds a 131-octet allowance derived from the live trip point with this test's
1053
+ // header block. The sum is a bound, not a byte-exact model: indexed
1054
+ // pseudo-headers and nghttp2's hash-table growth move node's trip point.
1055
+ const STREAM_MEM_CHARGE = 360 + 131;
1056
+ // The session's own persistent buffers count against node's budget too: node
1057
+ // includes sizeof(Http2Session) (760 octets in the same probe) in
1058
+ // current_session_memory(). h2lean's JS counterpart is the pair of scratch
1059
+ // buffers plus the first staging buffer; the header assembly buffer is charged
1060
+ // when it is actually allocated, in _appendHdr.
1061
+ const SESSION_MEM_CHARGE = HEADER_LEN + 6 + STAGE_MIN;
1054
1062
  // nghttp2 answers a failed stream-open callback with this session error
1055
1063
  // (node_http2.cc:1073-1085, NGHTTP2_ERR_CALLBACK_FAILURE). Measured on a live
1056
1064
  // node 26 server: ERR_HTTP2_ERROR with this message on both 'sessionError' and
@@ -1335,10 +1343,12 @@ class Http2ServerSession extends EventEmitter {
1335
1343
  // would cross the budget (node_http2.cc CanAddStream / OnHeader). The charge
1336
1344
  // leaves with the stream in _dropStream, so a connection that drains can open
1337
1345
  // new streams again. node counts queued outbound bytes too; those are already
1338
- // bounded separately by _heldCost (MAX_SESSION_HOLD).
1346
+ // bounded separately by _heldCost (MAX_SESSION_HOLD). The session's own
1347
+ // scratch and staging buffers open the budget, as sizeof(Http2Session) does
1348
+ // on node; _appendHdr adds the header assembly buffer when it exists.
1339
1349
  this._memLimit = typeof opts.maxSessionMemory === 'number'
1340
1350
  ? Math.max(1, opts.maxSessionMemory) * 1000000 : DEFAULT_MAX_SESSION_MEMORY;
1341
- this._memUsed = 0;
1351
+ this._memUsed = SESSION_MEM_CHARGE;
1342
1352
  // Outbound padding: node's C++ switch only acts on 0/1/2; every other
1343
1353
  // number is an unpadded session (measured on a live node 26 server).
1344
1354
  this._padding = opts.paddingStrategy === f.PADDING_ALIGNED ? f.PADDING_ALIGNED
@@ -2912,9 +2922,6 @@ class Http2ServerSession extends EventEmitter {
2912
2922
  this._hdrLen += len;
2913
2923
  return;
2914
2924
  }
2915
- if (this._hdr === null) {
2916
- this._hdr = Buffer.allocUnsafe(8192);
2917
- }
2918
2925
  // No maxHeaderListSize test here, although this is where the octets are counted: the limit
2919
2926
  // is about the size of the header LIST, which is only known once the block is decoded, and
2920
2927
  // h2lean applies it there (_processHeaders). Measured against node, an over-limit block
@@ -2923,14 +2930,28 @@ class Http2ServerSession extends EventEmitter {
2923
2930
  // which turns one bad request into an error for every other client on the session. The
2924
2931
  // buffer is bounded without it: MAX_CONTINUATIONS frames of at most
2925
2932
  // localSettings.maxFrameSize octets each, 8 x 16 KiB by default.
2926
- if (this._hdrLen + len > this._hdr.length) {
2927
- let cap = this._hdr.length * 2;
2933
+ // Size the first buffer for the frame in hand: a 16 KiB HEADERS would
2934
+ // otherwise allocate the 8 KiB default and immediately throw it away for
2935
+ // a doubled one. STAGE_MIN is the same starting size as the staging buffer,
2936
+ // and the backing store stays with the session for its whole life, so its
2937
+ // octets join the other persistent buffers in the maxSessionMemory budget.
2938
+ if (this._hdr === null) {
2939
+ let cap = STAGE_MIN;
2940
+ while (cap < len) {
2941
+ cap *= 2;
2942
+ }
2943
+ this._hdr = Buffer.allocUnsafe(cap);
2944
+ this._memUsed += cap;
2945
+ } else if (this._hdrLen + len > this._hdr.length) {
2946
+ const old = this._hdr.length;
2947
+ let cap = old * 2;
2928
2948
  while (cap < this._hdrLen + len) {
2929
2949
  cap *= 2;
2930
2950
  }
2931
2951
  const bigger = Buffer.allocUnsafe(cap);
2932
2952
  f.copyInto(bigger, 0, this._hdr, 0, this._hdrLen);
2933
2953
  this._hdr = bigger;
2954
+ this._memUsed += cap - old;
2934
2955
  }
2935
2956
  f.copyInto(this._hdr, this._hdrLen, buf, start, start + len);
2936
2957
  this._hdrLen += len;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.19.5",
3
+ "version": "0.19.6",
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": [