h2lean 0.10.0 → 0.10.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/compat.js CHANGED
@@ -619,6 +619,15 @@ class Http2ServerResponse extends Stream {
619
619
 
620
620
  setTrailer(name, value) {
621
621
  checkName(name);
622
+ // node's setTrailer has no end() guard (internal/http2/compat.js:580) -- trailers
623
+ // added after end() but before the 'wantTrailers' tick reach the wire there,
624
+ // because nghttp2 withholds END_STREAM while waitForTrailers is set. The staged
625
+ // fast path must recall its frame so the same shape holds here (item 44); after
626
+ // the flush the frame is gone and the trailer is lost on node too, its tick
627
+ // having already answered the empty block.
628
+ if (this._stream._esStaged) {
629
+ this._stream._recallStagedEnd();
630
+ }
622
631
  this._trailers[trimName(name)] = value;
623
632
  }
624
633
 
@@ -811,6 +820,25 @@ class Http2ServerResponse extends Stream {
811
820
  // END_STREAM), the recorded fields when it added some. Calling sendTrailers() here
812
821
  // instead put the block in front of that machinery -- and threw NOT_READY as soon as
813
822
  // sendTrailers() grew node's own trailersReady guard.
823
+ let recorded = false;
824
+ for (const key in this._trailers) {
825
+ if (Object.prototype.hasOwnProperty.call(this._trailers, key)) {
826
+ recorded = true;
827
+ break;
828
+ }
829
+ }
830
+ // The trailer-less response -- the shape of every response that never called
831
+ // addTrailers -- stages its END_STREAM inside the read batch instead of from the
832
+ // tick (item 44). The event path is untouched: _waitForTrailers stays set and the
833
+ // tick still fires 'wantTrailers', where sendTrailers closes the stream without
834
+ // staging the frame twice. A body parked on window credit, a refused block and a
835
+ // HEAD-request close keep the deferred path -- the staged frame would land before
836
+ // the queued DATA, and the refused shape records the end without wire work.
837
+ if (!recorded && stream._waitForTrailers && stream._queued === null &&
838
+ !stream._blockRefused) {
839
+ stream.endWithStagedEnd();
840
+ return this;
841
+ }
814
842
  stream.end();
815
843
  return this;
816
844
  }
package/lib/session.js CHANGED
@@ -1414,6 +1414,28 @@ class Http2Session extends EventEmitter {
1414
1414
  }
1415
1415
  }
1416
1416
 
1417
+ // Remove a not-yet-committed END_STREAM frame that _frame staged for sid, the
1418
+ // stage-form half of the pushStream cut above: the frame has not left in a batch,
1419
+ // so a trailer added after end() can supersede it (stream._recallStagedEnd). False
1420
+ // when there is no marker, or the frame already moved into the queue or onto the
1421
+ // wire -- past that point the octets are gone and the caller keeps its state.
1422
+ // While stream._esStaged is set the marker is always in the stage form: the frame
1423
+ // is staged by compat's end(), whose caller may only run its own synchronous turn
1424
+ // and nextTicks before the 'wantTrailers' tick -- write() after end() throws and
1425
+ // pushStream cuts its own END_STREAM -- and the flush that would commit the stage
1426
+ // is a microtask behind both of those.
1427
+ _cutStagedES(sid) {
1428
+ const es = this._esMarkers.get(sid);
1429
+ if (es === undefined || !es.stage) {
1430
+ return false;
1431
+ }
1432
+ this._esMarkers.delete(sid);
1433
+ this._stage.copyWithin(es.off, es.off + es.len, this._stageLen);
1434
+ this._stageLen -= es.len;
1435
+ this._outLen -= es.len;
1436
+ return true;
1437
+ }
1438
+
1417
1439
  // Writes DATA within the stream and connection windows. Returns true when
1418
1440
  // everything went out; false means the caller should wait for WINDOW_UPDATE.
1419
1441
  sendData(stream, buf, end) {
package/lib/stream.js CHANGED
@@ -174,6 +174,23 @@ function wantTrailersNT(stream) {
174
174
  }
175
175
  }
176
176
 
177
+ // The tick body of _scheduleFinish, a named function rather than a closure: it runs
178
+ // once per finished stream, and a per-response allocation on the compat finish path
179
+ // was measured into the regression item 44 closes.
180
+ // Not _destroyed here: a stream whose both halves close in the same turn the
181
+ // writable ends flips ST_CLOSED synchronously (_closeNow) while the close EVENTS
182
+ // are deferred -- and node's order for that shape is 'finish' before 'close',
183
+ // with writableFinished true at the close (measured). The only cancel is a
184
+ // writable that never drained: octets still parked on window credit.
185
+ function finishNT(stream) {
186
+ stream._finishQueued = false;
187
+ if (stream._writableFinished || stream._queued !== null) {
188
+ return;
189
+ }
190
+ stream._writableFinished = true;
191
+ stream.emit('finish');
192
+ }
193
+
177
194
  // 'end' and 'close' for a stream whose two halves finished in the same turn.
178
195
  // They were already going to run in the same tick drain, so folding them into
179
196
  // one callback only removes one TickObject per request.
@@ -404,6 +421,10 @@ class Http2Stream extends Stream {
404
421
  // sendTrailers() after respond({waitForTrailers:true}) but before 'wantTrailers'
405
422
  // has fired throws ERR_HTTP2_TRAILERS_NOT_READY, measured live.
406
423
  this._trailersReady = false;
424
+ // set by endWithStagedEnd(): the END_STREAM octets are already in the outbound
425
+ // batch, so sendTrailers() closes the stream without staging a second one, and
426
+ // addTrailers() after end() recalls the frame instead
427
+ this._esStaged = false;
407
428
  this._pendingTrailers = null; // trailer block waiting behind a queued body
408
429
  this._pendingTrailersBound = 0; // ...and its outbound estimate, checked when it drains
409
430
  // A refused outbound HEADERS (session._refuseBlock) left the wire a step behind the
@@ -1090,6 +1111,41 @@ class Http2Stream extends Stream {
1090
1111
  return this;
1091
1112
  }
1092
1113
 
1114
+ // The compat layer's empty-trailer finish: stage the END_STREAM frame HERE, inside
1115
+ // the read batch that produced the response, instead of from the 'wantTrailers'
1116
+ // tick after the batch has left. The octets, the frame order and every event are
1117
+ // the ones the deferred path produces -- _waitForTrailers stays set, so the tick
1118
+ // still fires 'wantTrailers', the compat handler still answers through
1119
+ // sendTrailers(), and the close machinery still runs from that tick; sendTrailers
1120
+ // sees _esStaged and skips only the second staging. What changes is which socket
1121
+ // batch the frame leaves in: measured, moving it out of the tick was worth ~2.4
1122
+ // microseconds of cpuPerReq on the compat showcase arm (item 44). Only for a stream
1123
+ // with nothing queued and nothing refused -- a body parked on window credit needs
1124
+ // its END_STREAM behind the queued DATA, which the deferred path already arranges.
1125
+ // addTrailers() after end() recalls the frame the way the pushStream cut does:
1126
+ // node withholds END_STREAM until the tick, so a trailer legally added in the
1127
+ // same turn as end() must supersede it; after the flush the frame is gone and the
1128
+ // trailer is lost on node too, because its tick already answered the empty block.
1129
+ endWithStagedEnd() {
1130
+ // end() sets _ending before anything is sent, and the peer-reset 'aborted' rule
1131
+ // reads it -- the fast path must carry the same state (test/compat.test.js).
1132
+ this._ending = true;
1133
+ this.session._frame(frames.DATA, frames.F_END_STREAM, this.id, null);
1134
+ this._esStaged = true;
1135
+ this._endLocalSide();
1136
+ }
1137
+
1138
+ // Undo endWithStagedEnd() while the frame is still in the staging buffer: cut it
1139
+ // out the way the pushStream path cuts a parent's not-yet-written END_STREAM, and
1140
+ // re-arm the deferred machinery. Returns false -- leaving everything as it is --
1141
+ // when the frame has already been committed to the queue or written.
1142
+ _recallStagedEnd() {
1143
+ if (this.session._cutStagedES(this.id)) {
1144
+ this._esStaged = false;
1145
+ this._waitForTrailers = true;
1146
+ }
1147
+ }
1148
+
1093
1149
  // Writable-side corking, as a counting stub. node's Http2Stream inherits Writable.cork,
1094
1150
  // which buffers write()/end() and defers the DATA to the wire until corked reaches 0;
1095
1151
  // h2lean's write() sends straight through (the per-read socket batch already coalesces a
@@ -1138,8 +1194,30 @@ class Http2Stream extends Stream {
1138
1194
  throw errorOf('ERR_HTTP2_TRAILERS_NOT_READY',
1139
1195
  'Trailing headers cannot be sent until after the wantTrailers event is emitted');
1140
1196
  }
1141
- const block = hdr.block(trailers, { trailer: true }, this.session);
1142
- const bound = hdr.sent.bound;
1197
+ let block;
1198
+ let bound;
1199
+ // An empty trailer map is skipped, not run through block(): nothing in it can fail
1200
+ // validation (no name to check, no single-value rule to trip) and it encodes to zero
1201
+ // octets, while block() allocates the parse key list, the strict-singles scratch and
1202
+ // the parse result for every one of them -- and every compat response ends with an
1203
+ // empty trailer map, because the (req,res) layer always answers 'wantTrailers' with
1204
+ // what it recorded: nothing at all when the application added none (item 44).
1205
+ // hdr.sent must not be read for the skipped case: it still holds the previous
1206
+ // block's result.
1207
+ let empty = true;
1208
+ for (const key in trailers) {
1209
+ if (Object.prototype.hasOwnProperty.call(trailers, key)) {
1210
+ empty = false;
1211
+ break;
1212
+ }
1213
+ }
1214
+ if (empty) {
1215
+ block = EMPTY;
1216
+ bound = 0;
1217
+ } else {
1218
+ block = hdr.block(trailers, { trailer: true }, this.session);
1219
+ bound = hdr.sent.bound;
1220
+ }
1143
1221
  // node records a null-prototype copy, not the caller's map (`ObjectAssign({
1144
1222
  // __proto__: null }, headers)`, internal/http2/core.js sendTrailers -- the shared
1145
1223
  // base method, so the client stream records the same shape).
@@ -1147,6 +1225,15 @@ class Http2Stream extends Stream {
1147
1225
  // the trailers are out: the flag that permitted this call must not keep asking for
1148
1226
  // wantTrailers, whatever happens to the bytes below
1149
1227
  this._waitForTrailers = false;
1228
+ if (this._esStaged) {
1229
+ // endWithStagedEnd() already put the END_STREAM octets into the batch that
1230
+ // produced this response (item 44); the wire half is done, only the
1231
+ // application-level half-close -- finish, wantTrailers were already fired, and
1232
+ // the close pair -- remains, exactly where it fires without the staging.
1233
+ this._esStaged = false;
1234
+ this._endLocalSide();
1235
+ return this;
1236
+ }
1150
1237
  // An empty trailer block does not become a HEADERS frame. nghttp2 has nothing to
1151
1238
  // put in a fieldless trailing header section, so node lets the END_STREAM the body
1152
1239
  // withheld land on a zero-length DATA instead -- measured: HEADERS/EH, DATA len4
@@ -1154,7 +1241,7 @@ class Http2Stream extends Stream {
1154
1241
  // because node's (req,res) layer always asks for trailers (internal/http2/compat.js
1155
1242
  // kBeginSend passes waitForTrailers: true) and answers with the empty trailer map
1156
1243
  // when the application added none.
1157
- const empty = block.length === 0;
1244
+ empty = block.length === 0;
1158
1245
  if (this._queued !== null) {
1159
1246
  // The body is still waiting for credit or for the socket, and this block has to
1160
1247
  // follow it: END_STREAM rides on the trailers, so a HEADERS sent now would outrun
@@ -2322,19 +2409,7 @@ class Http2Stream extends Stream {
2322
2409
  return;
2323
2410
  }
2324
2411
  this._finishQueued = true;
2325
- process.nextTick(() => {
2326
- this._finishQueued = false;
2327
- // not _destroyed here: a stream whose both halves close in the same turn the
2328
- // writable ends flips ST_CLOSED synchronously (_closeNow) while the close EVENTS
2329
- // are deferred -- and node's order for that shape is 'finish' before 'close',
2330
- // with writableFinished true at the close (measured). The only cancel is a
2331
- // writable that never drained: octets still parked on window credit.
2332
- if (this._writableFinished || this._queued !== null) {
2333
- return;
2334
- }
2335
- this._writableFinished = true;
2336
- this.emit('finish');
2337
- });
2412
+ process.nextTick(finishNT, this);
2338
2413
  }
2339
2414
 
2340
2415
  _endLocalSide() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
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": [