h2lean 0.8.1 → 0.9.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/client-session.js +120 -45
- package/lib/headers.js +7 -7
- package/lib/hpack.js +15 -0
- package/lib/server.js +27 -7
- package/lib/session.js +112 -8
- package/lib/stream.js +562 -20
- package/package.json +1 -1
package/lib/session.js
CHANGED
|
@@ -570,10 +570,6 @@ function hashPieces(pieces, start, end) {
|
|
|
570
570
|
function onIdleTimeout(session) {
|
|
571
571
|
session._idleTimer = null;
|
|
572
572
|
session.emit('timeout');
|
|
573
|
-
const cb = session._idleCb;
|
|
574
|
-
if (cb !== null) {
|
|
575
|
-
cb.call(session);
|
|
576
|
-
}
|
|
577
573
|
}
|
|
578
574
|
|
|
579
575
|
// node's proxySocketHandler (internal/http2/core.js): `session.socket` is a Proxy over the
|
|
@@ -784,6 +780,20 @@ class Http2Session extends EventEmitter {
|
|
|
784
780
|
this._heldBytes = 0; // bytes of response parked on streams that lack credit
|
|
785
781
|
this._frozenLen = 0; // bytes handed over, callback still owed
|
|
786
782
|
this._writing = false;
|
|
783
|
+
// Where a stream's END_STREAM DATA frame sits in the not-yet-written outbound
|
|
784
|
+
// queue, keyed by stream id. nghttp2 queues PUSH_PROMISE as a control frame and
|
|
785
|
+
// its send cycle flushes control frames before DATA (nghttp2_session.c
|
|
786
|
+
// nghttp2_session_send), so on node's wire a PUSH_PROMISE opened in the same turn
|
|
787
|
+
// as the parent's end() -- upstream's respond({})/end()/pushStream() shape --
|
|
788
|
+
// precedes the parent's END_STREAM. An nghttp2 client refuses a push whose parent
|
|
789
|
+
// stream it already closed, i.e. one whose END_STREAM arrived first (measured:
|
|
790
|
+
// PUSH_PROMISE before the parent's DATA/ES surfaces the push, the same frame after
|
|
791
|
+
// it draws RST_STREAM(CANCEL) for the promised stream and no push event), so
|
|
792
|
+
// sendPushPromise moves the parent's END_STREAM behind itself while the bytes are
|
|
793
|
+
// still ours to reorder. Cleared when the batch leaves for the socket. Frames
|
|
794
|
+
// already cut out of the queue wait in _esRequeue for the same reason.
|
|
795
|
+
this._esMarkers = new Map();
|
|
796
|
+
this._esRequeue = [];
|
|
787
797
|
this._connSendWin = PEER_DEFAULTS.initialWindowSize;
|
|
788
798
|
this._prevInitialWin = PEER_DEFAULTS.initialWindowSize;
|
|
789
799
|
this._connRecvUsed = 0; // consumed bytes owed back to the connection
|
|
@@ -888,7 +898,6 @@ class Http2Session extends EventEmitter {
|
|
|
888
898
|
this._liveStreams = 0;
|
|
889
899
|
this._goawaySent = false;
|
|
890
900
|
this._idleTimer = null;
|
|
891
|
-
this._idleCb = null;
|
|
892
901
|
this._inBatch = false;
|
|
893
902
|
this._flushScheduled = false;
|
|
894
903
|
this._flushSoon = () => {
|
|
@@ -1009,6 +1018,9 @@ class Http2Session extends EventEmitter {
|
|
|
1009
1018
|
const head = Buffer.allocUnsafe(HEADER_LEN);
|
|
1010
1019
|
writeHeader(head, 0, len, type, flags, sid);
|
|
1011
1020
|
this._outQ.push(head, payload);
|
|
1021
|
+
if (type === DATA && (flags & F_END_STREAM)) {
|
|
1022
|
+
this._esMarkers.set(sid, { q0: this._outQ.length - 2, head });
|
|
1023
|
+
}
|
|
1012
1024
|
} else {
|
|
1013
1025
|
this._need(HEADER_LEN + len);
|
|
1014
1026
|
const off = this._stageLen;
|
|
@@ -1017,6 +1029,9 @@ class Http2Session extends EventEmitter {
|
|
|
1017
1029
|
payload.copy(this._stage, off + HEADER_LEN);
|
|
1018
1030
|
}
|
|
1019
1031
|
this._stageLen = off + HEADER_LEN + len;
|
|
1032
|
+
if (type === DATA && (flags & F_END_STREAM)) {
|
|
1033
|
+
this._esMarkers.set(sid, { stage: true, off, len: HEADER_LEN + len });
|
|
1034
|
+
}
|
|
1020
1035
|
}
|
|
1021
1036
|
this._outLen += HEADER_LEN + len;
|
|
1022
1037
|
if (!this._inBatch) {
|
|
@@ -1046,7 +1061,18 @@ class Http2Session extends EventEmitter {
|
|
|
1046
1061
|
if (this._stageLen === 0) {
|
|
1047
1062
|
return;
|
|
1048
1063
|
}
|
|
1049
|
-
this.
|
|
1064
|
+
const chunk = this._stage.subarray(0, this._stageLen);
|
|
1065
|
+
// stage-marker forms point into the buffer that is about to leave; turn them into
|
|
1066
|
+
// buffer-relative ones so a later pushStream can still cut the frame out
|
|
1067
|
+
if (this._esMarkers.size) {
|
|
1068
|
+
for (const m of this._esMarkers.values()) {
|
|
1069
|
+
if (m.stage) {
|
|
1070
|
+
m.buf = chunk;
|
|
1071
|
+
m.stage = false;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
this._outQ.push(chunk);
|
|
1050
1076
|
this._stage = null; // the queued bytes are not ours to write
|
|
1051
1077
|
this._stageLen = 0;
|
|
1052
1078
|
}
|
|
@@ -1093,6 +1119,8 @@ class Http2Session extends EventEmitter {
|
|
|
1093
1119
|
this._stageLen = 0;
|
|
1094
1120
|
this._outLen = 0;
|
|
1095
1121
|
this._writing = false;
|
|
1122
|
+
this._esMarkers.clear();
|
|
1123
|
+
this._esRequeue = [];
|
|
1096
1124
|
return;
|
|
1097
1125
|
}
|
|
1098
1126
|
if (this._writing) {
|
|
@@ -1102,6 +1130,22 @@ class Http2Session extends EventEmitter {
|
|
|
1102
1130
|
this._commit();
|
|
1103
1131
|
const batch = this._outQ;
|
|
1104
1132
|
this._outQ = [];
|
|
1133
|
+
// from here the bytes belong to the socket; there is nothing left to reorder
|
|
1134
|
+
this._esMarkers.clear();
|
|
1135
|
+
// END_STREAM frames cut out for a push go last: nghttp2's cycle flushes control
|
|
1136
|
+
// frames before DATA, and this point -- after the staging buffer's commit -- is
|
|
1137
|
+
// the tail of whatever the turn produced
|
|
1138
|
+
if (this._esRequeue.length) {
|
|
1139
|
+
const tail = this._esRequeue;
|
|
1140
|
+
this._esRequeue = [];
|
|
1141
|
+
for (const t of tail) {
|
|
1142
|
+
if (t.bytes !== null) {
|
|
1143
|
+
this._outQ.push(t.bytes);
|
|
1144
|
+
} else {
|
|
1145
|
+
this._outQ.push(t.pair[0], t.pair[1]);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1105
1149
|
this._frozenLen = this._outLen;
|
|
1106
1150
|
this._outLen = 0;
|
|
1107
1151
|
const socket = this._socket;
|
|
@@ -1285,6 +1329,48 @@ class Http2Session extends EventEmitter {
|
|
|
1285
1329
|
// Returns true when the block was refused, so pushStream can mark the promised stream
|
|
1286
1330
|
// before its callback runs (the same contract sendHeaders has with respond()).
|
|
1287
1331
|
sendPushPromise(parentSid, promisedSid, block, bound) {
|
|
1332
|
+
// nghttp2's send cycle flushes control frames before DATA, so a PUSH_PROMISE
|
|
1333
|
+
// opened in the same turn as the parent's end() rides ahead of the parent's
|
|
1334
|
+
// END_STREAM on node's wire. An nghttp2 client refuses a push whose parent is
|
|
1335
|
+
// already closed -- the RST(CANCEL) measured in the marker comment above -- so
|
|
1336
|
+
// the parent's not-yet-written END_STREAM moves behind the promise frames here.
|
|
1337
|
+
// After the batch leaves for the socket (_writePending clears the markers) the
|
|
1338
|
+
// stream is closed nghttp2-side too and pushStream no longer reaches this line.
|
|
1339
|
+
const es = this._esMarkers.get(parentSid);
|
|
1340
|
+
let esBytes = null;
|
|
1341
|
+
let esPair = null;
|
|
1342
|
+
if (es) {
|
|
1343
|
+
this._esMarkers.delete(parentSid);
|
|
1344
|
+
if (es.buf !== undefined) {
|
|
1345
|
+
const i = this._outQ.indexOf(es.buf);
|
|
1346
|
+
if (i !== -1) {
|
|
1347
|
+
const b = es.buf;
|
|
1348
|
+
esBytes = b.subarray(es.off, es.off + es.len);
|
|
1349
|
+
// split the shared buffer so the frame can leave as its own entry
|
|
1350
|
+
const parts = [];
|
|
1351
|
+
if (es.off > 0) {
|
|
1352
|
+
parts.push(b.subarray(0, es.off));
|
|
1353
|
+
}
|
|
1354
|
+
if (es.off + es.len < b.length) {
|
|
1355
|
+
parts.push(b.subarray(es.off + es.len));
|
|
1356
|
+
}
|
|
1357
|
+
this._outQ.splice(i, 1, ...parts);
|
|
1358
|
+
}
|
|
1359
|
+
} else if (es.stage) {
|
|
1360
|
+
esBytes = Buffer.allocUnsafe(es.len);
|
|
1361
|
+
this._stage.copy(esBytes, 0, es.off, es.off + es.len);
|
|
1362
|
+
this._stage.copyWithin(es.off, es.off + es.len, this._stageLen);
|
|
1363
|
+
this._stageLen -= es.len;
|
|
1364
|
+
} else {
|
|
1365
|
+
const i = this._outQ.indexOf(es.head);
|
|
1366
|
+
if (i !== -1) {
|
|
1367
|
+
esPair = this._outQ.splice(i, 2);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
if (esBytes !== null || esPair !== null) {
|
|
1371
|
+
this._esRequeue.push(esBytes !== null ? { bytes: esBytes } : { pair: esPair });
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1288
1374
|
// The same prep-time estimate sendHeaders refuses on, with no priority spec:
|
|
1289
1375
|
// session_estimate_headers_payload passes 0 for PUSH_PROMISE. Measured on v26.12.1
|
|
1290
1376
|
// with a push of {':path':'/pushed', ':authority':'a', 'x-big':'X'.repeat(n)} -- node
|
|
@@ -2860,7 +2946,13 @@ class Http2Session extends EventEmitter {
|
|
|
2860
2946
|
}
|
|
2861
2947
|
const next = stream._sendWin + inc;
|
|
2862
2948
|
if (next > 0x7fffffff) {
|
|
2863
|
-
|
|
2949
|
+
// RFC 9113 6.9.1 makes a send-window overflow a connection error, and nghttp2
|
|
2950
|
+
// agrees for the per-stream window exactly as for the connection's (measured on
|
|
2951
|
+
// a live node v26.12.1 server: GOAWAY(FLOW_CONTROL_ERROR) with lastStreamID
|
|
2952
|
+
// naming the stream, no RST_STREAM, ERR_HTTP2_ERROR on the stream and the
|
|
2953
|
+
// session). A stream-level reset here would leave the connection alive for a
|
|
2954
|
+
// peer that has already broken its window budget.
|
|
2955
|
+
this._fatal(FLOW_CONTROL_ERROR, 'stream window overflow');
|
|
2864
2956
|
return;
|
|
2865
2957
|
}
|
|
2866
2958
|
stream._sendWin = next;
|
|
@@ -3448,10 +3540,22 @@ class Http2Session extends EventEmitter {
|
|
|
3448
3540
|
idleClearTimeout(this._idleTimer);
|
|
3449
3541
|
this._idleTimer = null;
|
|
3450
3542
|
}
|
|
3451
|
-
this._idleCb = cb === undefined ? null : cb;
|
|
3452
3543
|
if (ms === 0) {
|
|
3544
|
+
// node's setStreamTimeout removes the passed callback when disarming; re-arming with
|
|
3545
|
+
// a new callback instead leaves the old once-listener standing, the same way node's
|
|
3546
|
+
// clearTimeout leaves it -- only the timer is replaced, never the listeners.
|
|
3547
|
+
if (cb !== undefined) {
|
|
3548
|
+
this.removeListener('timeout', cb);
|
|
3549
|
+
}
|
|
3453
3550
|
return this;
|
|
3454
3551
|
}
|
|
3552
|
+
// node's setStreamTimeout registers the callback as a once('timeout') listener rather
|
|
3553
|
+
// than keeping it off to the side, so it runs in listener order, in the middle of the
|
|
3554
|
+
// emission. The server's arming (attachSession) depends on that position: node's
|
|
3555
|
+
// sessionOnTimeout reports to the server before any listener registered after it.
|
|
3556
|
+
if (cb !== undefined) {
|
|
3557
|
+
this.once('timeout', cb);
|
|
3558
|
+
}
|
|
3455
3559
|
// the session is passed as the timer argument, which both the native and the
|
|
3456
3560
|
// fast path forward to the callback: nothing is allocated per connection here
|
|
3457
3561
|
const timer = idleSetTimeout(onIdleTimeout, ms, this);
|