h2lean 0.2.2 → 0.2.4

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.
@@ -519,8 +519,9 @@ class ClientHttp2Session extends EventEmitter {
519
519
  if (this._strictSingle) {
520
520
  // the duplicate check rides _addField's own loop, the way node's
521
521
  // processHeader runs it inside buildNgHeaderString's one pass; the scratch
522
- // survives between calls and is cleared here
523
- this._singlesSeen.length = 0;
522
+ // is dropped here -- reassigned, not length-cleared, for the same
523
+ // right-trim reason as the module scratch in headers.js (OPTIMIZATION.md item 38)
524
+ this._singlesSeen = [];
524
525
  }
525
526
  if (Array.isArray(src)) {
526
527
  for (let i = 0; i + 1 < src.length; i += 2) {
@@ -1855,7 +1856,8 @@ class ClientHttp2Session extends EventEmitter {
1855
1856
  r.stream.destroy(cancelErr(err));
1856
1857
  }
1857
1858
  }
1858
- this._outboundQueue.length = 0;
1859
+ // reassigned, not length-cleared (OPTIMIZATION.md item 38)
1860
+ this._outboundQueue = [];
1859
1861
  const socket = this.socket;
1860
1862
  this._discardOut();
1861
1863
  if (!socket.destroyed) {
package/lib/headers.js CHANGED
@@ -195,6 +195,22 @@ function nameCacheSize() {
195
195
  // node does not make.
196
196
  const badValue = /[\u0000-\u0008\u000a-\u001f\u007f]/;
197
197
 
198
+ // The strict-single-value scratch for parse(): one array for the whole module,
199
+ // cleared at entry and never held across a call -- parse() is synchronous and
200
+ // runs no callback, so no call can observe another's list. The shape is node's
201
+ // own (buildNgHeaderString checks a lazily-grown array inline in its one pass,
202
+ // internal/http2/util.js) and the one the client's request() carries
203
+ // (lib/client-session.js, OPTIMIZATION.md item 36). Measured honestly: at the
204
+ // granularity parse() acts on, the swap from a per-call Set is inside the noise
205
+ // -- item 36's win came from removing request()'s second pass over the flat
206
+ // list, which parse() never had; the component costs are in OPTIMIZATION.md
207
+ // item 37. It is kept for the shape parity, not for speed
208
+ // (bench/micro/parse-singles.js).
209
+ // Reassigned, not length-cleared: `length = 0` right-trims the backing store so the
210
+ // next fill re-grows it, and the fresh allocation measures cheaper on both time and,
211
+ // at the sizes this sees, GC (OPTIMIZATION.md item 38).
212
+ let singlesSeen = [];
213
+
198
214
  // Kept as a Set: at five names the shape of the operation decides it, not the
199
215
  // size (OPTIMIZATION.md item 10).
200
216
  // RFC 9113 8.2.2 forbids these on an HTTP/2 connection: they are hop-by-hop in HTTP/1.1,
@@ -271,9 +287,16 @@ function parse(headers, sensitive,
271
287
  throw httpError('ERR_HTTP2_INVALID_HEADER_VALUE', 'headers must be an object');
272
288
  }
273
289
  const neverSet = sensitive || headers[KSensitiveHeaders];
274
- const neverList = Array.isArray(neverSet) && neverSet.length > 0
275
- ? new Set(neverSet.map((s) => lowerName(s)))
276
- : null;
290
+ // Inserted in one loop, not new Set(neverSet.map(...)): the map's intermediate array
291
+ // and its closure are one allocation each per sensitive response, measured at
292
+ // bench/micro/parse-singles.js.
293
+ let neverList = null;
294
+ if (Array.isArray(neverSet) && neverSet.length > 0) {
295
+ neverList = new Set();
296
+ for (let i = 0; i < neverSet.length; i++) {
297
+ neverList.add(lowerName(neverSet[i]));
298
+ }
299
+ }
277
300
 
278
301
  const pseudo = [];
279
302
  const fields = [];
@@ -294,7 +317,9 @@ function parse(headers, sensitive,
294
317
  // copy -- regular field or pseudo-header alike -- or for a multi-element array on
295
318
  // one, before any stream or octet exists. The singles walk stays in field order, so
296
319
  // the first problem in the caller's order is the one reported.
297
- const singles = strict ? new Set() : null;
320
+ if (strict) {
321
+ singlesSeen = [];
322
+ }
298
323
  for (let i = 0; i < keys.length; i++) {
299
324
  let key;
300
325
  let raw;
@@ -325,11 +350,11 @@ function parse(headers, sensitive,
325
350
  throw httpError('ERR_HTTP2_HEADER_SINGLE_VALUE',
326
351
  `Header field "${key}" must only have a single value`);
327
352
  }
328
- if (singles.has(key)) {
353
+ if (singlesSeen.indexOf(key) !== -1) {
329
354
  throw httpError('ERR_HTTP2_HEADER_SINGLE_VALUE',
330
355
  `Header field "${key}" must only have a single value`);
331
356
  }
332
- singles.add(key);
357
+ singlesSeen.push(key);
333
358
  }
334
359
 
335
360
  if (key[0] === ':') {
package/lib/session.js CHANGED
@@ -469,6 +469,23 @@ class Http2Session extends EventEmitter {
469
469
  this._rstSec = (Date.now() / 1000) | 0;
470
470
 
471
471
  this._maxInboundSid = 0;
472
+ // The highest id of a HEADERS frame this side has accepted -- nghttp2 opens the
473
+ // stream in prep_frame, after the length, padding and priority checks but BEFORE
474
+ // the block is decoded or any verdict on it is given, and its last-proc-stream-id
475
+ // (which node reports as state.lastProcStreamID and puts in GOAWAY's last-stream
476
+ // id) moves there. A GOAWAY sent for a frame refused after that point therefore
477
+ // names the stream of the refused frame, measured on v26.12.1 for an undecodable
478
+ // block and a header list over maxHeaderListSize (last=1 against h2lean's 0,
479
+ // out/probe-last-id.js) -- the shape a client reads as its retry boundary, so a
480
+ // lower id makes it resend a request that was already acted on. Control frames
481
+ // never move it -- measured on the same probe: WINDOW_UPDATE, RST_STREAM and DATA
482
+ // refused above the line all answer last=1, the served stream, on BOTH stacks --
483
+ // and frames refused before the acceptance point (over max frame size, malformed
484
+ // padding or priority, an even id) do not either: nghttp2 fails them before the
485
+ // open. Only _fatal reads it; the idle-state rules below stay keyed on
486
+ // _maxInboundSid, whose raise waits for the skip-record check (5.1.1) that must
487
+ // see the line as it was when the frame arrived.
488
+ this._reachedSid = 0;
472
489
  this._refused = [];
473
490
  this._dropped = null; // ids a reset ended, newest last
474
491
  // ids whose message ended in the read batch being processed, see _dropStream. Cleared by the
@@ -823,7 +840,9 @@ class Http2Session extends EventEmitter {
823
840
  const n = chunk.length;
824
841
  let i = 0;
825
842
  this._inBatch = true;
826
- this._ended.length = 0; // the last batch's finished streams are no longer here
843
+ // dropped, not length-cleared -- reassignment measures cheaper than the
844
+ // right-trim the length store does (OPTIMIZATION.md item 38)
845
+ this._ended = []; // the last batch's finished streams are no longer here
827
846
  this._touchIdleTimer();
828
847
 
829
848
  if (this._prefaceSeen < PREFACE_LEN) {
@@ -1297,6 +1316,11 @@ class Http2Session extends EventEmitter {
1297
1316
  return;
1298
1317
  }
1299
1318
  const l = this._hdrFrag;
1319
+ // the frame is accepted: nghttp2 opens its stream here, before the block is
1320
+ // decoded -- see _reachedSid
1321
+ if (sid > this._reachedSid) {
1322
+ this._reachedSid = sid;
1323
+ }
1300
1324
  if (flags & F_END_HEADERS) {
1301
1325
  this._processHeaders(sid, (flags & F_END_STREAM) !== 0, buf.subarray(start + p, start + p + l));
1302
1326
  return;
@@ -1324,6 +1348,11 @@ class Http2Session extends EventEmitter {
1324
1348
  return;
1325
1349
  }
1326
1350
  const l = this._hdrFrag;
1351
+ // the frame is accepted: nghttp2 opens its stream here, before the block is
1352
+ // decoded -- see _reachedSid
1353
+ if (sid > this._reachedSid) {
1354
+ this._reachedSid = sid;
1355
+ }
1327
1356
  if (flags & F_END_HEADERS) {
1328
1357
  this._processBlockPieces(sid, (flags & F_END_STREAM) !== 0, pieces, p, p + l);
1329
1358
  return;
@@ -2396,7 +2425,12 @@ class Http2Session extends EventEmitter {
2396
2425
  if (!this._open) {
2397
2426
  return;
2398
2427
  }
2399
- this.sendGoaway(this._maxInboundSid, code, msg);
2428
+ // last-stream id names the far side of the retry boundary: a stream whose HEADERS
2429
+ // was accepted but whose block was refused was reached, and node names it (see
2430
+ // _reachedSid) -- naming only the streams a stream object was made for would tell
2431
+ // the peer it may retry a request that was already acted on
2432
+ const reached = this._maxInboundSid > this._reachedSid ? this._maxInboundSid : this._reachedSid;
2433
+ this.sendGoaway(reached, code, msg);
2400
2434
  this._flush();
2401
2435
  const err = errorOf('ERR_HTTP2_SESSION_ERROR', msg);
2402
2436
  err.http2Code = code;
package/lib/stream.js CHANGED
@@ -106,10 +106,14 @@ class Http2Stream extends EventEmitter {
106
106
  }
107
107
 
108
108
  respond(headers, options) {
109
+ // node only throws here (internal/http2/core.js respond(): `if (this.headersSent)
110
+ // throw new ERR_HTTP2_HEADERS_SENT()`) and does not emit 'error' on the stream --
111
+ // measured on v26.12.1: a second respond() throws ERR_HTTP2_HEADERS_SENT
112
+ // ("Response has already been initiated.") with no 'error' event. An emit here
113
+ // throws out of a stream that has no 'error' listener, which turns the
114
+ // application's own throw into an uncaught exception node never produces.
109
115
  if (this.headersSent) {
110
- const err = errorOf('ERR_HTTP2_HEADERS_SENT', 'stream has already sent HEADERS');
111
- this.emit('error', err);
112
- throw err;
116
+ throw errorOf('ERR_HTTP2_HEADERS_SENT', 'Response has already been initiated.');
113
117
  }
114
118
  const opts = options || {};
115
119
  // node puts a `date` on every response of this API too: prepareResponseHeadersObject()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
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": [