h2lean 0.19.1 → 0.19.3

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.
@@ -1140,6 +1140,18 @@ class Http2ClientSession extends EventEmitter {
1140
1140
  }
1141
1141
  }
1142
1142
 
1143
+ // The bound node checks before submitting a HEADERS block, from the same list the
1144
+ // encoder consumes. Computed separately so a refused request keeps the old
1145
+ // encode-then-refuse path and its encoder state, while an accepted one can skip the
1146
+ // per-request buffer entirely.
1147
+ _boundOfList(list) {
1148
+ let bound = 12;
1149
+ for (let i = 0; i < list.length; i += 3) {
1150
+ bound += 12 + list[i].length + list[i + 1].length;
1151
+ }
1152
+ return bound;
1153
+ }
1154
+
1143
1155
  _sendRequest(stream, list, neverList, end, hp) {
1144
1156
  // Fill the pseudo-headers the application omitted, in the canonical order, then
1145
1157
  // hand the whole list to HPACK. node completes :path to '/', :scheme and
@@ -1177,6 +1189,16 @@ class Http2ClientSession extends EventEmitter {
1177
1189
  neverList !== null && neverList.has(':authority'));
1178
1190
  }
1179
1191
  orderPseudo(list); // in place: `list` is built by _addField for this request only
1192
+ bound = this._boundOfList(list);
1193
+ if (bound + PRIORITY_SPECLEN <= this._maxSendBlock) {
1194
+ // Accepted: encode straight into the session's staging buffer -- no
1195
+ // per-request block allocation and no block copy. The rare refusal below
1196
+ // keeps the old path, so the encoder advances there exactly as it did before.
1197
+ stream._sid = stream.id;
1198
+ this._writeRequestBlockAt(stream.id, list, end, hp);
1199
+ this._afterRequestHeaders(stream);
1200
+ return;
1201
+ }
1180
1202
  block = this._encodeRequest(list);
1181
1203
  bound = sentBound;
1182
1204
  if (hp !== undefined && hp !== null && typeof hp === 'object') {
@@ -1190,9 +1212,13 @@ class Http2ClientSession extends EventEmitter {
1190
1212
  // what finishes the Duplex.
1191
1213
  return;
1192
1214
  }
1193
- // HEADERS is now queued; release any body the application wrote before the
1194
- // socket connected (or before this queued request was framed). The DATA frames
1195
- // land in _out behind the HEADERS, so the wire order stays HEADERS -> DATA.
1215
+ this._afterRequestHeaders(stream);
1216
+ }
1217
+
1218
+ // HEADERS is now queued; release any body the application wrote before the socket
1219
+ // connected (or before this queued request was framed). The DATA frames land in _out
1220
+ // behind the HEADERS, so the wire order stays HEADERS -> DATA.
1221
+ _afterRequestHeaders(stream) {
1196
1222
  stream._headersSent = true;
1197
1223
  stream.headersSent = true;
1198
1224
  if (stream._pending !== null || stream._finalCb !== null) {
@@ -1243,6 +1269,60 @@ class Http2ClientSession extends EventEmitter {
1243
1269
  return buf.subarray(0, off);
1244
1270
  }
1245
1271
 
1272
+ // The accepted-request path: the size update is consumed into the destination, every
1273
+ // field line is encoded there, and the 9-octet frame headers are inserted in place
1274
+ // from the last CONTINUATION backwards -- the same shape as the server's
1275
+ // _writeBlockAt (OPTIMIZATION.md items 14, 68). The cache gets one owned snapshot;
1276
+ // later sightings of the same object replay it through _sendHeaders as before.
1277
+ _writeRequestBlockAt(sid, list, end, hp) {
1278
+ const enc = this._encoder;
1279
+ let cap = 5; // room for the largest 5-bit-prefix size update
1280
+ let bound = 12;
1281
+ for (let i = 0; i < list.length; i += 3) {
1282
+ cap += Encoder.maxBytesOf(list[i], list[i + 1]);
1283
+ bound += 12 + list[i].length + list[i + 1].length;
1284
+ }
1285
+ sentBound = bound;
1286
+ const max = this.remoteSettings.maxFrameSize;
1287
+ const frames = Math.max(1, Math.ceil(cap / max));
1288
+ this._need(HEADER_LEN + cap + HEADER_LEN * (frames - 1));
1289
+ const dst = this._stage;
1290
+ const off = this._stageLen;
1291
+ const payloadOff = off + HEADER_LEN;
1292
+ let o = enc.takeSizeUpdateInto(dst, payloadOff);
1293
+ enc.unsettled = false;
1294
+ for (let i = 0; i < list.length; i += 3) {
1295
+ o = enc.write(dst, o, list[i], list[i + 1], list[i + 2]);
1296
+ }
1297
+ const n = o - payloadOff;
1298
+ const used = Math.max(1, Math.ceil(n / max));
1299
+ const es = end ? F_END_STREAM : 0;
1300
+ if (used === 1) {
1301
+ writeHeader(dst, off, n, HEADERS, F_END_HEADERS | es, sid);
1302
+ } else {
1303
+ for (let i = used - 1; i >= 1; i--) {
1304
+ const src = payloadOff + i * max;
1305
+ dst.copyWithin(src + i * HEADER_LEN, src, Math.min(o, src + max));
1306
+ }
1307
+ for (let i = 0; i < used; i++) {
1308
+ const take = Math.min(n - i * max, max);
1309
+ const flags = i === 0 ? es : (i === used - 1 ? F_END_HEADERS : 0);
1310
+ writeHeader(dst, off + i * (HEADER_LEN + max), take,
1311
+ i === 0 ? HEADERS : CONTINUATION, flags, sid);
1312
+ }
1313
+ }
1314
+ const total = HEADER_LEN * used + n;
1315
+ this._stageLen = off + total;
1316
+ this._outLen += total;
1317
+ if (hp !== undefined && hp !== null && typeof hp === 'object') {
1318
+ this._blocks.set(hp, { epoch: enc.epoch,
1319
+ block: Buffer.from(dst.subarray(payloadOff, o)), bound });
1320
+ }
1321
+ if (!this._inBatch) {
1322
+ this._schedule();
1323
+ }
1324
+ }
1325
+
1246
1326
  // Returns true when the block was refused. `opening` says whether this HEADERS is the
1247
1327
  // one that opens the stream on the wire, which is what decides the refusal's shape:
1248
1328
  // nghttp2's prep-error path closes a stream-opening request HEADERS itself with
package/lib/headers.js CHANGED
@@ -738,23 +738,6 @@ function writePrepared(prep, session, dst, off) {
738
738
  return off + end;
739
739
  }
740
740
 
741
- // The HPACK block for a PUSH_PROMISE payload: request-style pseudo-headers, no date,
742
- // no status default, no cache (a promise is one-shot). The encoder is the session's,
743
- // so the block shares the connection's dynamic table with every other outbound block.
744
- // Returns a view into the shared scratch, valid until the next encode call -- the
745
- // caller (session.sendPushPromise) copies it into the frame immediately.
746
- function pushBlock(headers, session) {
747
- const enc = session._encoder;
748
- const update = enc.takeSizeUpdate();
749
- const parsed = parse(headers, { request: true, strict: session._strictSingle });
750
- const at = update === null ? 0 : update.length;
751
- const end = encodeAt(parsed, enc, at);
752
- if (at !== 0) {
753
- copyInto(scratch, 0, update, 0, update.length);
754
- }
755
- return scratch.subarray(0, end);
756
- }
757
-
758
741
  // The maps node hands back as `stream.sentHeaders` and `req.sentHeaders`: the message that
759
742
  // went out, not the object the application passed in. node copies into a null-prototype map
760
743
  // and fills the pseudo-headers itself (prepareResponseHeadersObject, internal/http2/core.js
@@ -890,7 +873,7 @@ function requestView(src, scheme, authority) {
890
873
  return view;
891
874
  }
892
875
 
893
- module.exports = { block, pushBlock, encode, parse, prepare, writePrepared, capOf, boundOf,
876
+ module.exports = { block, encode, parse, prepare, writePrepared, capOf, boundOf,
894
877
  nullProto, isFieldName, trimName, lowerName,
895
878
  nameCacheSize, hasBadValue, neverNames, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS,
896
879
  sent, responseView, requestView };
package/lib/hpack.js CHANGED
@@ -1431,11 +1431,6 @@ class Encoder {
1431
1431
  }
1432
1432
 
1433
1433
 
1434
- indexed(index) {
1435
- const b = Buffer.allocUnsafe(4);
1436
- return b.subarray(0, writeInt(b, 0, index, 7, 0x80));
1437
- }
1438
-
1439
1434
  // An indexed reference is one prefixed integer and nothing else. Writing it into
1440
1435
  // the destination removes both the 4-octet Buffer and the memcpy from the cached
1441
1436
  // bytes -- and the cache entry itself, which cannot survive an epoch change while
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.19.1",
3
+ "version": "0.19.3",
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": [