h2lean 0.4.2 → 0.8.1

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.
package/lib/headers.js CHANGED
@@ -226,6 +226,27 @@ function hasBadValue(value) {
226
226
  return badValue.test(value);
227
227
  }
228
228
 
229
+ // The names of the field lines that arrived as never-indexed HPACK literals,
230
+ // one entry per delivered line, duplicates and arrival order kept. node hangs
231
+ // this under its `sensitiveHeaders` symbol on every inbound header object --
232
+ // stream headers, compat req.headers, trailers, client responses -- and always
233
+ // allocates a fresh array, [] when no line was never-indexed, so the
234
+ // application may mutate it without touching any other request's list
235
+ // (toHeaderObject in internal/util.js, measured on node v26.12.1). `flags` is
236
+ // the decoder's neverFlags for the same block: null means there was none, and
237
+ // the whole walk is skipped.
238
+ function neverNames(fields, count, flags) {
239
+ const names = [];
240
+ if (flags !== null) {
241
+ for (let i = 0; i < count; i += 2) {
242
+ if (flags[i >> 1] === 1) {
243
+ names.push(fields[i]);
244
+ }
245
+ }
246
+ }
247
+ return names;
248
+ }
249
+
229
250
  function httpError(code, msg) {
230
251
  const err = new Error(msg);
231
252
  err.code = code;
@@ -466,8 +487,10 @@ function parse(headers, sensitive,
466
487
  // ERR_HTTP2_STATUS_INVALID), and an informational block goes through additionalHeaders(),
467
488
  // which asks for no date. Appended last, because that is where node's respond() puts it
468
489
  // and header order is visible on the wire.
490
+ let genDate;
469
491
  if (!trailer && !info && sendDate && status >= 200 && !hasDate) {
470
- fields.push('date', httpDate());
492
+ genDate = httpDate();
493
+ fields.push('date', genDate);
471
494
  }
472
495
  if (status === 204 || status === 304 || (status >= 100 && status < 200)) {
473
496
  for (let i = 0; i < fields.length; i += 3) {
@@ -477,7 +500,7 @@ function parse(headers, sensitive,
477
500
  }
478
501
  }
479
502
  }
480
- return { status, pseudo, fields };
503
+ return { status, pseudo, fields, genDate };
481
504
  }
482
505
 
483
506
  function checked(key, value) {
@@ -490,6 +513,15 @@ function checked(key, value) {
490
513
  return v;
491
514
  }
492
515
 
516
+ // What the block just built resolved to, for `stream.sentHeaders`: the ':status' that went
517
+ // out and the `date` this module generated for it (undefined when the block carries none,
518
+ // when the application supplied its own, or when sendDate was off). A module-level result
519
+ // slot like frames.js's, written in place and read by respond() immediately after block()
520
+ // returns, so the value reported is the value that left -- including on a cache hit, where
521
+ // the entry carries it and no parse runs. `bound` is the same shape of result for the
522
+ // outbound size estimate below.
523
+ const sent = { status: 0, date: undefined, bound: 0 };
524
+
493
525
  // The HPACK bytes for one HEADERS payload. The encoder carries a dynamic table, which
494
526
  // is per-connection state, so the block cache lives on the session and is stamped with
495
527
  // the table's epoch: cached bytes may name an index that a later insertion moved.
@@ -508,6 +540,9 @@ function block(headers, sensitive, opts, session) {
508
540
  if (cacheable) {
509
541
  const hit = session._blocks.get(headers);
510
542
  if (hit !== undefined && hit.epoch === enc.epoch && hit.stamp === stamp) {
543
+ sent.status = hit.status;
544
+ sent.date = hit.date;
545
+ sent.bound = hit.bound;
511
546
  return hit.bytes;
512
547
  }
513
548
  }
@@ -516,6 +551,8 @@ function block(headers, sensitive, opts, session) {
516
551
  const parsed = parse(headers, sensitive,
517
552
  { trailer: !!(opts && opts.trailer), sendDate, info: !!(opts && opts.info),
518
553
  strict: session._strictSingle });
554
+ sent.status = parsed.status;
555
+ sent.date = parsed.genDate;
519
556
 
520
557
  // A shape reaches its final form over two or three responses, the way any HPACK
521
558
  // encoder warms up: the first sends a line as a literal and remembers it, the second
@@ -541,7 +578,9 @@ function block(headers, sensitive, opts, session) {
541
578
  // pool copy is the cheaper allocation (OPTIMIZATION.md item 18)
542
579
  const bytes = Buffer.allocUnsafe(end - at);
543
580
  scratch.copy(bytes, 0, at, end);
544
- session._blocks.set(headers, { bytes, epoch: enc.epoch, stamp });
581
+ session._blocks.set(headers,
582
+ { bytes, epoch: enc.epoch, stamp, status: parsed.status, date: parsed.genDate,
583
+ bound: sent.bound });
545
584
  }
546
585
  return scratch.subarray(0, end);
547
586
  }
@@ -560,12 +599,23 @@ function encodeAt(parsed, enc, at) {
560
599
  const pseudo = parsed.pseudo;
561
600
  const fields = parsed.fields;
562
601
  let cap = 16 + at;
602
+ // nghttp2's outbound estimate, accumulated in the same pass that sizes the scratch:
603
+ // nghttp2_hd_deflate_bound (nghttp2_hd.c) is `12 + 6*2*nvlen + sum(namelen+valuelen)`,
604
+ // the 12 being two possible 6-octet table-size updates and the 6*2 the two literal
605
+ // length prefixes per field. It counts the *string lengths* node handed down, not the
606
+ // UTF-8 octets the encoder writes -- measured on v26.12.1: a value of n 'é' (2 octets
607
+ // each) and of n '€' (3 each) hit the same boundary as n 'X'. The caller adds the
608
+ // per-frame-kind `additional` and compares against maxSendHeaderBlockLength.
609
+ let bound = 12;
563
610
  for (let i = 0; i < pseudo.length; i += 2) {
564
611
  cap += Encoder.maxBytesOf(pseudo[i], pseudo[i + 1]);
612
+ bound += 12 + pseudo[i].length + pseudo[i + 1].length;
565
613
  }
566
614
  for (let i = 0; i < fields.length; i += 3) {
567
615
  cap += Encoder.maxBytesOf(fields[i], fields[i + 1]);
616
+ bound += 12 + fields[i].length + fields[i + 1].length;
568
617
  }
618
+ sent.bound = bound;
569
619
  if (scratch.length < cap) {
570
620
  scratch = Buffer.allocUnsafe(cap);
571
621
  }
@@ -596,5 +646,141 @@ function pushBlock(headers, sensitive, session) {
596
646
  return scratch.subarray(0, end);
597
647
  }
598
648
 
649
+ // The maps node hands back as `stream.sentHeaders` and `req.sentHeaders`: the message that
650
+ // went out, not the object the application passed in. node copies into a null-prototype map
651
+ // and fills the pseudo-headers itself (prepareResponseHeadersObject, internal/http2/core.js
652
+ // :2677; prepareRequestHeadersObject/Array, internal/http2/util.js:717/:627), and for a flat
653
+ // array its getter folds the pairs, a repeated name becoming an array of values
654
+ // (core.js:2225-2254). Measured on a live node server: keys keep the caller's spelling and
655
+ // order, ':status' is the coerced NUMBER even when '201' was passed, the generated `date`
656
+ // lands last, and a request's missing pseudo-headers are appended in the order :method,
657
+ // :authority, :scheme, :path for an object but PREPENDED in that order for an array.
658
+ // Built on read rather than in respond()/request(): node makes its copy eagerly because its
659
+ // serializer encodes from it, while h2lean encodes from the caller's object and would pay
660
+ // for a map nobody asks for. The caller's object is never mutated either way -- node's array
661
+ // path unshifts onto it, which is the one shape difference left here.
662
+ function foldPairs(src, view) {
663
+ for (let i = 0; i + 1 < src.length; i += 2) {
664
+ const key = src[i];
665
+ const value = src[i + 1];
666
+ const had = view[key];
667
+ if (had === undefined) {
668
+ view[key] = value;
669
+ } else if (Array.isArray(had)) {
670
+ had.push(value);
671
+ } else {
672
+ view[key] = [had, value];
673
+ }
674
+ }
675
+ }
676
+
677
+ function copyOwn(src, view) {
678
+ if (src === null || typeof src !== 'object') {
679
+ return;
680
+ }
681
+ for (const key in src) {
682
+ if (Object.hasOwn(src, key)) {
683
+ view[key] = src[key];
684
+ }
685
+ }
686
+ // node's object path assigns the symbol unconditionally (core.js:2697), so the key is
687
+ // there with an undefined value when the application marked nothing sensitive.
688
+ view[KSensitiveHeaders] = src[KSensitiveHeaders];
689
+ }
690
+
691
+ function responseView(src, status, date) {
692
+ const view = { __proto__: null };
693
+ if (Array.isArray(src)) {
694
+ let sawStatus = false;
695
+ for (let i = 0; i + 1 < src.length; i += 2) {
696
+ if (lowerName(src[i]) === ':status') {
697
+ sawStatus = true;
698
+ break;
699
+ }
700
+ }
701
+ if (!sawStatus) {
702
+ view[':status'] = status;
703
+ }
704
+ foldPairs(src, view);
705
+ if (src[KSensitiveHeaders] !== undefined) {
706
+ view[KSensitiveHeaders] = src[KSensitiveHeaders];
707
+ }
708
+ } else {
709
+ copyOwn(src, view);
710
+ }
711
+ view[':status'] = status;
712
+ if (date !== undefined && view.date === undefined) {
713
+ view.date = date;
714
+ }
715
+ return view;
716
+ }
717
+
718
+ function requestView(src, scheme, authority) {
719
+ const view = { __proto__: null };
720
+ if (Array.isArray(src)) {
721
+ let method;
722
+ let protocol;
723
+ let sawAuthority = false;
724
+ let sawScheme = false;
725
+ let sawPath = false;
726
+ for (let i = 0; i + 1 < src.length; i += 2) {
727
+ const key = src[i];
728
+ if (key[0] !== ':') {
729
+ continue;
730
+ }
731
+ const name = lowerName(key);
732
+ if (name === ':method') {
733
+ method = src[i + 1];
734
+ } else if (name === ':protocol') {
735
+ protocol = src[i + 1];
736
+ } else if (name === ':authority') {
737
+ sawAuthority = true;
738
+ } else if (name === ':scheme') {
739
+ sawScheme = true;
740
+ } else if (name === ':path') {
741
+ sawPath = true;
742
+ }
743
+ }
744
+ if (method === undefined) {
745
+ view[':method'] = 'GET';
746
+ }
747
+ // node's `host` test on the array form reads a property of the array object, not a pair
748
+ // (util.js:668), so a 'host' field inside the pairs does not suppress ':authority'.
749
+ if (method !== 'CONNECT' || protocol !== undefined) {
750
+ if (!sawAuthority && src.host === undefined) {
751
+ view[':authority'] = authority;
752
+ }
753
+ if (!sawScheme) {
754
+ view[':scheme'] = scheme;
755
+ }
756
+ if (!sawPath) {
757
+ view[':path'] = '/';
758
+ }
759
+ }
760
+ foldPairs(src, view);
761
+ if (src[KSensitiveHeaders] !== undefined) {
762
+ view[KSensitiveHeaders] = src[KSensitiveHeaders];
763
+ }
764
+ return view;
765
+ }
766
+ copyOwn(src, view);
767
+ if (view[':method'] === undefined) {
768
+ view[':method'] = 'GET';
769
+ }
770
+ if (view[':method'] !== 'CONNECT' || view[':protocol'] !== undefined) {
771
+ if (view[':authority'] === undefined && view.host === undefined) {
772
+ view[':authority'] = authority;
773
+ }
774
+ if (view[':scheme'] === undefined) {
775
+ view[':scheme'] = scheme;
776
+ }
777
+ if (view[':path'] === undefined) {
778
+ view[':path'] = '/';
779
+ }
780
+ }
781
+ return view;
782
+ }
783
+
599
784
  module.exports = { block, pushBlock, encode, parse, nullProto, isFieldName, trimName, lowerName,
600
- nameCacheSize, hasBadValue, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS };
785
+ nameCacheSize, hasBadValue, neverNames, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS,
786
+ sent, responseView, requestView };
package/lib/hpack.js CHANGED
@@ -526,6 +526,33 @@ class Decoder {
526
526
  this.epoch = 0; // bumped on every table change (see §4.4, 4.3)
527
527
  this.names = new Map(); // interned field names
528
528
  this.scratch = Buffer.allocUnsafe(4096);
529
+ // The never-indexed markers of the block being decoded, one slot per field
530
+ // line (slot i = the line at pair index 2i), kept beside the field list
531
+ // because the decoder itself must not touch the caller's output shape: a
532
+ // plain array and a FlatList both arrive here. node's toHeaderObject hangs
533
+ // the names of the lines that arrived as never-indexed literals (RFC 7541
534
+ // 6.2.3, prefix 0001 -- the N bit exists only on the two literal forms) on
535
+ // the header object under its `sensitiveHeaders` symbol, one entry per
536
+ // delivered line, duplicates and arrival order kept (measured on node
537
+ // v26.12.1). The array is the decoder's own: a caller that retains the
538
+ // flags past the next decode copies them, because reset() clears in place.
539
+ this.neverFlags = null;
540
+ this.neverCount = 0;
541
+ }
542
+
543
+ // Record one never-indexed line. Grown on demand and cleared in bulk at the
544
+ // start of the next decode: the common block has none, so the array is
545
+ // usually never allocated at all.
546
+ neverLine(line) {
547
+ if (this.neverFlags === null) {
548
+ this.neverFlags = new Uint8Array(16);
549
+ } else if (line >= this.neverFlags.length) {
550
+ const grown = new Uint8Array(this.neverFlags.length * 2);
551
+ grown.set(this.neverFlags);
552
+ this.neverFlags = grown;
553
+ }
554
+ this.neverFlags[line] = 1;
555
+ this.neverCount++;
529
556
  }
530
557
 
531
558
  // HPACK index (1-based, newest dynamic entry first) -> [name, value]
@@ -594,6 +621,10 @@ class Decoder {
594
621
  if (scratch) {
595
622
  out.reset();
596
623
  }
624
+ if (this.neverCount > 0) {
625
+ this.neverFlags.fill(0);
626
+ this.neverCount = 0;
627
+ }
597
628
  let n = 0;
598
629
  let p = start;
599
630
  while (p < end) {
@@ -631,6 +662,9 @@ class Decoder {
631
662
  this.setMaxSize(RI);
632
663
  p = RP;
633
664
  } else { // 0000/0001 literal
665
+ if ((b & 0x10) !== 0) {
666
+ this.neverLine(n >> 1);
667
+ }
634
668
  p = this.literal(buf, p, end, out, scratch, n, 4);
635
669
  n += 2;
636
670
  }
@@ -709,6 +743,10 @@ class Decoder {
709
743
  if (scratch) {
710
744
  out.reset();
711
745
  }
746
+ if (this.neverCount > 0) {
747
+ this.neverFlags.fill(0);
748
+ this.neverCount = 0;
749
+ }
712
750
  let n = 0;
713
751
  const cur = new PieceCursor(pieces, start, end);
714
752
  while (cur.left > 0) {
@@ -739,6 +777,9 @@ class Decoder {
739
777
  cur.readInt(5);
740
778
  this.setMaxSize(cur.value);
741
779
  } else { // 0000/0001 literal
780
+ if ((b & 0x10) !== 0) {
781
+ this.neverLine(n >> 1);
782
+ }
742
783
  n = this.literalPieces(cur, out, scratch, n, 4);
743
784
  }
744
785
  }
@@ -884,7 +925,7 @@ function nameBytes(name) {
884
925
  if (huff) {
885
926
  huffEncodeStr(name, b, off);
886
927
  } else {
887
- b.write(name, off, 'latin1');
928
+ latin1Into(b, off, name);
888
929
  }
889
930
  if (NAME_BYTES.size < NAME_BYTES_MAX) {
890
931
  NAME_BYTES.set(name, b);
@@ -1336,5 +1377,11 @@ module.exports = {
1336
1377
  huffDecode,
1337
1378
  huffBitLen,
1338
1379
  writeInt,
1339
- readInt
1380
+ readInt,
1381
+ // The latin1 codec helpers are exported for the rest of lib/: every field value
1382
+ // the HPACK paths touch is latin1, and so are the ORIGIN/ALTSVC/PING octets the
1383
+ // sessions read and write, so the one measured conversion (items 13, 35) is the
1384
+ // only implementation rather than a per-call-site Buffer#toString/#from.
1385
+ latin1,
1386
+ latin1Into
1340
1387
  };
package/lib/server.js CHANGED
@@ -102,7 +102,15 @@ function initHttp2Server(server, options) {
102
102
  shareHeaders: opts.shareHeaders === true,
103
103
  // Validated here, at createServer time, where node's validateSessionOptions runs;
104
104
  // the value also rides to each session, whose constructor re-checks it.
105
- strictSingleValueFields: strictSingleValueFields(opts)
105
+ strictSingleValueFields: strictSingleValueFields(opts),
106
+ // The outbound header-block bound. node does not validate this one -- updateOptionsBuffer
107
+ // (internal/http2/util.js) takes it on a `typeof === 'number'` test alone -- so it rides
108
+ // through raw and the session constructor applies the same test and the same coercion.
109
+ maxSendHeaderBlockLength: opts.maxSendHeaderBlockLength,
110
+ // The server `origins` option: each session sends one ORIGIN frame with these origins at
111
+ // connect. node only does it when the value is an array (core.js setupHandle), so it rides
112
+ // through unvalidated and Http2Session._start applies the same Array.isArray test.
113
+ origins: opts.origins
106
114
  };
107
115
  server._sessions = new Set();
108
116
  server[kCompat] = null;