h2lean 0.10.0 → 0.10.2
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 +28 -0
- package/lib/session.js +22 -0
- package/lib/stream.js +127 -17
- package/package.json +1 -1
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,10 +174,34 @@ 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.
|
|
180
197
|
function emitEndCloseNT(target) {
|
|
198
|
+
if (target._finishFuse) {
|
|
199
|
+
// the finish work rides here, ahead of every state this tick flips: the separate
|
|
200
|
+
// tick it replaces ran in the same drain, directly before this callback, when the
|
|
201
|
+
// session reference and _closeDelivered were still what end() left them.
|
|
202
|
+
target._finishFuse = false;
|
|
203
|
+
finishNT(target);
|
|
204
|
+
}
|
|
181
205
|
target._closeDelivered = true;
|
|
182
206
|
// node clears the stream's session reference in _destroy, before any of the close
|
|
183
207
|
// events go out (internal/http2/core.js:2546) -- see _closeNow for why the drop waits
|
|
@@ -194,6 +218,10 @@ function emitEndCloseNT(target) {
|
|
|
194
218
|
// in one turn, with no 'end' owed. Marks the node-equivalent close moment -- see
|
|
195
219
|
// _closeDelivered above.
|
|
196
220
|
function emitCloseDeliveredNT(target) {
|
|
221
|
+
if (target._finishFuse) {
|
|
222
|
+
target._finishFuse = false;
|
|
223
|
+
finishNT(target);
|
|
224
|
+
}
|
|
197
225
|
target._closeDelivered = true;
|
|
198
226
|
target.session = undefined;
|
|
199
227
|
target.emit('close');
|
|
@@ -404,6 +432,10 @@ class Http2Stream extends Stream {
|
|
|
404
432
|
// sendTrailers() after respond({waitForTrailers:true}) but before 'wantTrailers'
|
|
405
433
|
// has fired throws ERR_HTTP2_TRAILERS_NOT_READY, measured live.
|
|
406
434
|
this._trailersReady = false;
|
|
435
|
+
// set by endWithStagedEnd(): the END_STREAM octets are already in the outbound
|
|
436
|
+
// batch, so sendTrailers() closes the stream without staging a second one, and
|
|
437
|
+
// addTrailers() after end() recalls the frame instead
|
|
438
|
+
this._esStaged = false;
|
|
407
439
|
this._pendingTrailers = null; // trailer block waiting behind a queued body
|
|
408
440
|
this._pendingTrailersBound = 0; // ...and its outbound estimate, checked when it drains
|
|
409
441
|
// A refused outbound HEADERS (session._refuseBlock) left the wire a step behind the
|
|
@@ -433,6 +465,9 @@ class Http2Stream extends Stream {
|
|
|
433
465
|
// 'close' with writableFinished false and no 'finish').
|
|
434
466
|
this._writableFinished = false;
|
|
435
467
|
this._finishQueued = false;
|
|
468
|
+
// The finish work carried into a close-delivery tick scheduled by the same call
|
|
469
|
+
// of _endLocalSide (see the fuse there) instead of a nextTick of its own.
|
|
470
|
+
this._finishFuse = false;
|
|
436
471
|
}
|
|
437
472
|
|
|
438
473
|
get destroyed() {
|
|
@@ -1090,6 +1125,41 @@ class Http2Stream extends Stream {
|
|
|
1090
1125
|
return this;
|
|
1091
1126
|
}
|
|
1092
1127
|
|
|
1128
|
+
// The compat layer's empty-trailer finish: stage the END_STREAM frame HERE, inside
|
|
1129
|
+
// the read batch that produced the response, instead of from the 'wantTrailers'
|
|
1130
|
+
// tick after the batch has left. The octets, the frame order and every event are
|
|
1131
|
+
// the ones the deferred path produces -- _waitForTrailers stays set, so the tick
|
|
1132
|
+
// still fires 'wantTrailers', the compat handler still answers through
|
|
1133
|
+
// sendTrailers(), and the close machinery still runs from that tick; sendTrailers
|
|
1134
|
+
// sees _esStaged and skips only the second staging. What changes is which socket
|
|
1135
|
+
// batch the frame leaves in: measured, moving it out of the tick was worth ~2.4
|
|
1136
|
+
// microseconds of cpuPerReq on the compat showcase arm (item 44). Only for a stream
|
|
1137
|
+
// with nothing queued and nothing refused -- a body parked on window credit needs
|
|
1138
|
+
// its END_STREAM behind the queued DATA, which the deferred path already arranges.
|
|
1139
|
+
// addTrailers() after end() recalls the frame the way the pushStream cut does:
|
|
1140
|
+
// node withholds END_STREAM until the tick, so a trailer legally added in the
|
|
1141
|
+
// same turn as end() must supersede it; after the flush the frame is gone and the
|
|
1142
|
+
// trailer is lost on node too, because its tick already answered the empty block.
|
|
1143
|
+
endWithStagedEnd() {
|
|
1144
|
+
// end() sets _ending before anything is sent, and the peer-reset 'aborted' rule
|
|
1145
|
+
// reads it -- the fast path must carry the same state (test/compat.test.js).
|
|
1146
|
+
this._ending = true;
|
|
1147
|
+
this.session._frame(frames.DATA, frames.F_END_STREAM, this.id, null);
|
|
1148
|
+
this._esStaged = true;
|
|
1149
|
+
this._endLocalSide();
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
// Undo endWithStagedEnd() while the frame is still in the staging buffer: cut it
|
|
1153
|
+
// out the way the pushStream path cuts a parent's not-yet-written END_STREAM, and
|
|
1154
|
+
// re-arm the deferred machinery. Returns false -- leaving everything as it is --
|
|
1155
|
+
// when the frame has already been committed to the queue or written.
|
|
1156
|
+
_recallStagedEnd() {
|
|
1157
|
+
if (this.session._cutStagedES(this.id)) {
|
|
1158
|
+
this._esStaged = false;
|
|
1159
|
+
this._waitForTrailers = true;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1093
1163
|
// Writable-side corking, as a counting stub. node's Http2Stream inherits Writable.cork,
|
|
1094
1164
|
// which buffers write()/end() and defers the DATA to the wire until corked reaches 0;
|
|
1095
1165
|
// h2lean's write() sends straight through (the per-read socket batch already coalesces a
|
|
@@ -1138,8 +1208,30 @@ class Http2Stream extends Stream {
|
|
|
1138
1208
|
throw errorOf('ERR_HTTP2_TRAILERS_NOT_READY',
|
|
1139
1209
|
'Trailing headers cannot be sent until after the wantTrailers event is emitted');
|
|
1140
1210
|
}
|
|
1141
|
-
|
|
1142
|
-
|
|
1211
|
+
let block;
|
|
1212
|
+
let bound;
|
|
1213
|
+
// An empty trailer map is skipped, not run through block(): nothing in it can fail
|
|
1214
|
+
// validation (no name to check, no single-value rule to trip) and it encodes to zero
|
|
1215
|
+
// octets, while block() allocates the parse key list, the strict-singles scratch and
|
|
1216
|
+
// the parse result for every one of them -- and every compat response ends with an
|
|
1217
|
+
// empty trailer map, because the (req,res) layer always answers 'wantTrailers' with
|
|
1218
|
+
// what it recorded: nothing at all when the application added none (item 44).
|
|
1219
|
+
// hdr.sent must not be read for the skipped case: it still holds the previous
|
|
1220
|
+
// block's result.
|
|
1221
|
+
let empty = true;
|
|
1222
|
+
for (const key in trailers) {
|
|
1223
|
+
if (Object.prototype.hasOwnProperty.call(trailers, key)) {
|
|
1224
|
+
empty = false;
|
|
1225
|
+
break;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
if (empty) {
|
|
1229
|
+
block = EMPTY;
|
|
1230
|
+
bound = 0;
|
|
1231
|
+
} else {
|
|
1232
|
+
block = hdr.block(trailers, { trailer: true }, this.session);
|
|
1233
|
+
bound = hdr.sent.bound;
|
|
1234
|
+
}
|
|
1143
1235
|
// node records a null-prototype copy, not the caller's map (`ObjectAssign({
|
|
1144
1236
|
// __proto__: null }, headers)`, internal/http2/core.js sendTrailers -- the shared
|
|
1145
1237
|
// base method, so the client stream records the same shape).
|
|
@@ -1147,6 +1239,15 @@ class Http2Stream extends Stream {
|
|
|
1147
1239
|
// the trailers are out: the flag that permitted this call must not keep asking for
|
|
1148
1240
|
// wantTrailers, whatever happens to the bytes below
|
|
1149
1241
|
this._waitForTrailers = false;
|
|
1242
|
+
if (this._esStaged) {
|
|
1243
|
+
// endWithStagedEnd() already put the END_STREAM octets into the batch that
|
|
1244
|
+
// produced this response (item 44); the wire half is done, only the
|
|
1245
|
+
// application-level half-close -- finish, wantTrailers were already fired, and
|
|
1246
|
+
// the close pair -- remains, exactly where it fires without the staging.
|
|
1247
|
+
this._esStaged = false;
|
|
1248
|
+
this._endLocalSide();
|
|
1249
|
+
return this;
|
|
1250
|
+
}
|
|
1150
1251
|
// An empty trailer block does not become a HEADERS frame. nghttp2 has nothing to
|
|
1151
1252
|
// put in a fieldless trailing header section, so node lets the END_STREAM the body
|
|
1152
1253
|
// withheld land on a zero-length DATA instead -- measured: HEADERS/EH, DATA len4
|
|
@@ -1154,7 +1255,7 @@ class Http2Stream extends Stream {
|
|
|
1154
1255
|
// because node's (req,res) layer always asks for trailers (internal/http2/compat.js
|
|
1155
1256
|
// kBeginSend passes waitForTrailers: true) and answers with the empty trailer map
|
|
1156
1257
|
// when the application added none.
|
|
1157
|
-
|
|
1258
|
+
empty = block.length === 0;
|
|
1158
1259
|
if (this._queued !== null) {
|
|
1159
1260
|
// The body is still waiting for credit or for the socket, and this block has to
|
|
1160
1261
|
// follow it: END_STREAM rides on the trailers, so a HEADERS sent now would outrun
|
|
@@ -2322,19 +2423,7 @@ class Http2Stream extends Stream {
|
|
|
2322
2423
|
return;
|
|
2323
2424
|
}
|
|
2324
2425
|
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
|
-
});
|
|
2426
|
+
process.nextTick(finishNT, this);
|
|
2338
2427
|
}
|
|
2339
2428
|
|
|
2340
2429
|
_endLocalSide() {
|
|
@@ -2352,7 +2441,19 @@ class Http2Stream extends Stream {
|
|
|
2352
2441
|
if (this._queued === null) {
|
|
2353
2442
|
// every octet handed over has been accepted by the session: node's condition
|
|
2354
2443
|
// for the writable 'finish'
|
|
2355
|
-
this.
|
|
2444
|
+
if (!this._waitForTrailers && this._state.remoteClose === 1 && !this._destroyed) {
|
|
2445
|
+
// The graceful close below schedules a close-delivery tick in this same call,
|
|
2446
|
+
// and finishNT would have run in the same tick drain directly before it: the
|
|
2447
|
+
// finish work rides at the head of that tick instead of paying one of its own.
|
|
2448
|
+
// Measured (item 45): the tick of its own is ~0.05 us/req on the GET/empty-
|
|
2449
|
+
// response shape and the fuse recovers ~0.03 of it; what is left is the flag
|
|
2450
|
+
// flip and the emit, which the unpatched shape paid too. The trailers and
|
|
2451
|
+
// auto-close shapes keep their own tick: there the ordering against
|
|
2452
|
+
// 'wantTrailers'/autoCloseNT's setImmediate is what node pins.
|
|
2453
|
+
this._finishFuse = true;
|
|
2454
|
+
} else {
|
|
2455
|
+
this._scheduleFinish();
|
|
2456
|
+
}
|
|
2356
2457
|
}
|
|
2357
2458
|
if (this._state.state === ST_OPEN) {
|
|
2358
2459
|
this._state.state = ST_HALF_CLOSED_LOCAL;
|
|
@@ -2444,6 +2545,15 @@ class Http2Stream extends Stream {
|
|
|
2444
2545
|
if (this._destroyed) {
|
|
2445
2546
|
return;
|
|
2446
2547
|
}
|
|
2548
|
+
if (this._finishFuse) {
|
|
2549
|
+
// The end() in this turn fused its finish into a close-delivery tick that this
|
|
2550
|
+
// destroy now replaces. The destroy is not the hot shape: hand the finish back the
|
|
2551
|
+
// separate tick it would have had, scheduled before the close ticks below -- the
|
|
2552
|
+
// position (and the post-destroy session view) finishNT had when it was always
|
|
2553
|
+
// scheduled from _endLocalSide.
|
|
2554
|
+
this._finishFuse = false;
|
|
2555
|
+
process.nextTick(finishNT, this);
|
|
2556
|
+
}
|
|
2447
2557
|
this._destroyed = true;
|
|
2448
2558
|
// node's destroy does not touch state.flowing — the readableFlowing getter keeps
|
|
2449
2559
|
// reporting the last resume()/pause()/listener verdict even after the destroy
|
package/package.json
CHANGED