h2lean 0.19.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.19.2",
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": [