h2lean 0.19.3 → 0.19.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.
@@ -82,7 +82,7 @@
82
82
  // ERR_HTTP2_STREAM_ERROR(NGHTTP2_INTERNAL_ERROR) rather than throwing out of the call,
83
83
  // matching node's buildNgHeaderString/processRespondWithFD teardown: a bad field name
84
84
  // leaves headersSent true (node sets HEADERS_SENT before validating fields) and takes
85
- // the destroy path. An array-form headers argument passes through hdr.block as-is
85
+ // the destroy path. An array-form headers argument passes through hdr.prepare() as-is
86
86
  // instead of node's for..in over indices (which sends field names '0', '1', ...);
87
87
  // the content-length statCheck injects is only applied to object-form headers.
88
88
  // * 'finish' and writableFinished exist on the stream: 'finish' is queued on a
@@ -345,11 +345,11 @@ function fdOptions(options) {
345
345
  // node's prepareResponseHeadersObject (internal/http2/core.js): a null-prototype copy
346
346
  // of the caller's object with ':status' coerced in, so a statCheck that rewrites
347
347
  // 'content-length' lands on the copy -- which is also what sentHeaders reports,
348
- // exactly where node's kSentHeaders has it. The generated date is left to hdr.block,
349
- // whose sendDate rule is the one prepare applies too. Status validation happens here,
348
+ // exactly where node's kSentHeaders has it. The generated date is left to hdr.prepare(),
349
+ // whose sendDate rule governs the encode path too. Status validation happens here,
350
350
  // before the payload-forbidden check -- measured: ':status': 700 throws
351
351
  // ERR_HTTP2_STATUS_INVALID out of the call, and ':status': '700' is coerced to 700
352
- // first. The array form passes through untouched (hdr.block handles pairs natively);
352
+ // first. The array form passes through untouched (hdr.prepare() handles pairs natively);
353
353
  // node's for..in over an array copies the index keys into the copy and sends field
354
354
  // names '0', '1', ... instead -- the junk shape the header comment documents.
355
355
  function prepareResponseHeadersObject(headersParam) {
@@ -1230,10 +1230,10 @@ class Http2ClientSession extends EventEmitter {
1230
1230
  const enc = this._encoder;
1231
1231
  // The dynamic-table-size announcement has to precede the block it applies to, and its
1232
1232
  // length is not known before the block is written: the field lines are what change the
1233
- // table, and the announcement reports where the table ended up. So the take happens
1234
- // first and its octets are copied into the headroom of the same buffer the block is
1235
- // encoded into -- never into a second one.
1236
- const update = enc.takeSizeUpdate();
1233
+ // table, and the announcement reports where the table ended up. Five octets are
1234
+ // reserved at the front of the one buffer this block is encoded into, and
1235
+ // takeSizeUpdateInto() writes the announcement there and returns the exact offset
1236
+ // the field lines start at -- no intermediate Buffer and no copy.
1237
1237
  let cap = 0;
1238
1238
  // nghttp2's outbound estimate, accumulated in the pass that already sizes the
1239
1239
  // scratch -- the same formula and the same string-length-not-octets rule
@@ -1246,26 +1246,19 @@ class Http2ClientSession extends EventEmitter {
1246
1246
  bound += 12 + list[i].length + list[i + 1].length;
1247
1247
  }
1248
1248
  sentBound = bound;
1249
- // Without a size update the block IS the scratch: _sendHeaders runs before any later
1249
+ // Without a size update the block IS the buffer: _sendHeaders runs before any later
1250
1250
  // encode can rewrite it, and _frame(owned=false) puts a stable copy in the batch.
1251
1251
  // With one, the old form was Buffer.concat([update, block]) -- a second allocation,
1252
- // a second copy of the whole block and a temporary array per request. Reserving the
1253
- // update's length up front costs one memcpy of the update itself
1254
- // (OPTIMIZATION.md item 14).
1255
- // The headroom exists on this branch only, and the returned view then starts past
1256
- // the front of its buffer -- which every consumer here reads as
1252
+ // a second copy of the whole block and a temporary array per request. The announcement
1253
+ // now goes straight into the reserved headroom (OPTIMIZATION.md item 14). The returned
1254
+ // view then starts past the front of its buffer -- every consumer here reads
1257
1255
  // .length/.copy/write(), all of which honour byteOffset.
1258
- const ul = update === null ? 0 : update.length;
1259
- const buf = Buffer.allocUnsafe(ul + cap);
1256
+ const buf = Buffer.allocUnsafe(5 + cap);
1260
1257
  enc.unsettled = false;
1261
- let off = ul;
1258
+ let off = enc.takeSizeUpdateInto(buf, 0);
1262
1259
  for (let i = 0; i < list.length; i += 3) {
1263
1260
  off = enc.write(buf, off, list[i], list[i + 1], list[i + 2]);
1264
1261
  }
1265
- if (ul === 0) {
1266
- return buf.subarray(0, off);
1267
- }
1268
- f.copyInto(buf, 0, update, 0, update.length);
1269
1262
  return buf.subarray(0, off);
1270
1263
  }
1271
1264
 
package/lib/headers.js CHANGED
@@ -44,11 +44,13 @@ function nullProto() {
44
44
  }
45
45
 
46
46
  // Encoded bytes for one response, reused between calls. The encoder that fills it
47
- // belongs to the session (lib/session), because a dynamic table is per-connection
48
- // state, so nothing on this path is shared between sessions.
49
- // A response block lives for microseconds: _frame() copies it into the staging buffer
50
- // of the outbound queue, so the queue never holds a view of this scratch. See block()
51
- // before holding on to the returned view across another call.
47
+ // belongs to the session (lib/server/Http2ServerSession.js), because a dynamic table
48
+ // is per-connection state, so nothing on this path is shared between sessions.
49
+ // The server path does not touch this buffer: prepare()/writePrepared() write straight
50
+ // into the session's staging buffer (OPTIMIZATION.md item 68), including the refused
51
+ // PUSH_PROMISE response, which is parsed but never encoded. block() survives for the
52
+ // tests and microbenches that need a standalone block and exercise the block cache. A
53
+ // caller must finish with that view before the next block() call rewrites the scratch.
52
54
  let scratch = Buffer.allocUnsafe(4096);
53
55
 
54
56
  const isToken = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
@@ -220,13 +222,6 @@ let singlesSeen = [];
220
222
  const CONNECTION_SPECIFIC = new Set(['connection', 'keep-alive', 'proxy-connection',
221
223
  'transfer-encoding', 'upgrade']);
222
224
 
223
- // Whether a field value carries a character that may not appear in one. The server's
224
- // inbound path asks the same question (lib/server/Http2ServerSession.js) and answers it the way nghttp2
225
- // does: the field is ignored, the stream lives.
226
- function hasBadValue(value) {
227
- return badValue.test(value);
228
- }
229
-
230
225
  // The names of the field lines that arrived as never-indexed HPACK literals,
231
226
  // one entry per delivered line, duplicates and arrival order kept. node hangs
232
227
  // this under its `sensitiveHeaders` symbol on every inbound header object --
@@ -548,7 +543,6 @@ function block(headers, opts, session) {
548
543
  }
549
544
  }
550
545
 
551
- const update = enc.takeSizeUpdate();
552
546
  const parsed = parse(headers,
553
547
  { trailer: !!(opts && opts.trailer), sendDate, info: !!(opts && opts.info),
554
548
  strict: session._strictSingle });
@@ -566,13 +560,17 @@ function block(headers, opts, session) {
566
560
  const epoch = enc.epoch;
567
561
  // The announcement goes in front of the block, in the same buffer: the old form was
568
562
  // Buffer.concat([update, out]) -- a second allocation, a copy of the whole block and a
569
- // temporary array, on the one path where the peer changed the table size. The client
570
- // does the same thing in its own _encodeRequest (lib/client/Http2ClientSession.js).
571
- const at = update === null ? 0 : update.length;
563
+ // temporary array, on the one path where the peer changed the table size. Reserve the
564
+ // largest 5-bit-prefix announcement before writing so encodeAt() cannot move the
565
+ // scratch under it, then let takeSizeUpdateInto() consume the pending change and use
566
+ // its exact length as the block's start offset. The client does the same in its own
567
+ // _encodeRequest (lib/client/Http2ClientSession.js).
568
+ const need = capOf(parsed, 5);
569
+ if (scratch.length < need) {
570
+ scratch = Buffer.allocUnsafe(need);
571
+ }
572
+ const at = enc.takeSizeUpdateInto(scratch, 0);
572
573
  const end = encodeAt(parsed, enc, at);
573
- if (at !== 0) {
574
- copyInto(scratch, 0, update, 0, update.length);
575
- }
576
574
  if (cacheable && enc.epoch === epoch && !enc.unsettled) {
577
575
  // a cached block has to outlive the scratch, so only the cached path copies --
578
576
  // allocUnsafe, not Buffer.from(subarray): at the sizes a response block has the
@@ -697,8 +695,8 @@ function prepare(headers, opts, session) {
697
695
  sent.status = parsed.status;
698
696
  sent.date = parsed.genDate;
699
697
  // The size update is NOT consumed here: writePrepared() writes its octets into the
700
- // destination itself. Reserve the maximum the 5-bit prefix can need (the same
701
- // bound takeSizeUpdate() allocates) so the encoder has room before a byte is written.
698
+ // destination itself. Reserve the maximum the 5-bit prefix can need (the room
699
+ // takeSizeUpdateInto() checks) so the encoder has room before a byte is written.
702
700
  const at = enc.pendingSize === null ? 0 : 5;
703
701
  let bound = 12;
704
702
  let cap = 16 + at;
@@ -719,7 +717,7 @@ function prepare(headers, opts, session) {
719
717
 
720
718
  function writePrepared(prep, session, dst, off) {
721
719
  const enc = session._encoder;
722
- // Same octets as takeSizeUpdate(), written into the caller's buffer: no 5-octet
720
+ // The announcement goes straight into the caller's buffer: no standalone 5-octet
723
721
  // Buffer and no subarray. The encoder consumes the pending update here, so the
724
722
  // announcement cannot be repeated by a later block.
725
723
  const at = enc.takeSizeUpdateInto(dst, off) - off;
@@ -875,5 +873,5 @@ function requestView(src, scheme, authority) {
875
873
 
876
874
  module.exports = { block, encode, parse, prepare, writePrepared, capOf, boundOf,
877
875
  nullProto, isFieldName, trimName, lowerName,
878
- nameCacheSize, hasBadValue, neverNames, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS,
876
+ nameCacheSize, neverNames, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS,
879
877
  sent, responseView, requestView };
package/lib/hpack.js CHANGED
@@ -1226,21 +1226,9 @@ class Encoder {
1226
1226
  } // the indices the caches name just shifted
1227
1227
  }
1228
1228
 
1229
- // The size update octets for the next field block, or null. Consumed here so a block
1230
- // never announces the same change twice.
1231
- takeSizeUpdate() {
1232
- const n = this.pendingSize;
1233
- if (n === null) {
1234
- return null;
1235
- }
1236
- this.pendingSize = null;
1237
- const b = Buffer.allocUnsafe(5);
1238
- return b.subarray(0, writeInt(b, 0, n, 5, 0x20));
1239
- }
1240
-
1241
- // The size update written straight into the caller's destination. Same octets as
1242
- // takeSizeUpdate(), but the direct-write path has no intermediate 5-octet Buffer
1243
- // and no subarray to hand to the staging buffer.
1229
+ // The size update written straight into the caller's destination, consumed here so
1230
+ // a block never announces the same change twice. No intermediate 5-octet Buffer and
1231
+ // no subarray to hand to the staging buffer. Returns the offset after the octets.
1244
1232
  takeSizeUpdateInto(dst, off) {
1245
1233
  const n = this.pendingSize;
1246
1234
  if (n === null) {
@@ -74,7 +74,7 @@
74
74
  // ERR_HTTP2_STREAM_ERROR(NGHTTP2_INTERNAL_ERROR) rather than throwing out of the call,
75
75
  // matching node's buildNgHeaderString/processRespondWithFD teardown: a bad field name
76
76
  // leaves headersSent true (node sets HEADERS_SENT before validating fields) and takes
77
- // the destroy path. An array-form headers argument passes through hdr.block as-is
77
+ // the destroy path. An array-form headers argument passes through hdr.prepare() as-is
78
78
  // instead of node's for..in over indices (which sends field names '0', '1', ...);
79
79
  // the content-length statCheck injects is only applied to object-form headers.
80
80
  // * 'finish' and writableFinished exist on the stream: 'finish' is queued on a
@@ -354,13 +354,15 @@ class Http2ServerStream extends Http2Stream {
354
354
  // internal/http2/util.js) -- there is no options.sensitiveHeaders for node to ignore.
355
355
  // A refused PUSH_PROMISE closed the promised stream at submit time: node never
356
356
  // submits its response block, so sending it here would put HEADERS on a stream
357
- // the peer never opened (GOAWAY(PROTOCOL_ERROR) there). Keep building the block
358
- // -- it sets sentHeaders and advances the encoder as block() does -- but do not
359
- // write it: sendHeadersAt() writes straight into the session's stage and cannot
360
- // be called and then discarded.
357
+ // the peer never opened (GOAWAY(PROTOCOL_ERROR) there). Keep parsing the block so
358
+ // sentHeaders still reports this response, but do not encode or write it: a live
359
+ // node probe measured the parent's next HEADERS byte-identical to a session that
360
+ // never built the discarded block, so our encoder must not advance either, and
361
+ // sendHeadersAt() writes straight into the session's stage where it cannot be
362
+ // called and then discarded.
361
363
  let refused = this._blockRefused;
362
364
  if (refused) {
363
- hdr.block(headers, { sendDate }, this.session);
365
+ hdr.prepare(headers, { sendDate }, this.session);
364
366
  } else {
365
367
  refused = this.session.sendHeadersAt(this.id, headers, { sendDate },
366
368
  opts.endStream === true || head || this._endQueued);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.19.3",
3
+ "version": "0.19.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": [