h2lean 0.3.0 → 0.4.0
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/index.js +1 -1
- package/lib/client-session.js +133 -7
- package/lib/client-stream.js +5 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Servers: h2c and h2s (prior-knowledge, ALPN 'h2'), the `stream` and compat
|
|
4
4
|
// (`request`) APIs. Client: `connect()` + `session.request()` over h2c and h2s
|
|
5
|
-
// (see lib/client.js); server push
|
|
5
|
+
// (see lib/client.js); server push (pushStream / PUSH_PROMISE) on both sides.
|
|
6
6
|
// Deliberately reused from node:http2, because re-typing them would only create
|
|
7
7
|
// drift: the 240 protocol constants and the `sensitiveHeaders` symbol. A peer
|
|
8
8
|
// must see the same rstCode/goawayCode numbers as with node, and user code
|
package/lib/client-session.js
CHANGED
|
@@ -143,7 +143,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
143
143
|
// before that node's getters read undefined.
|
|
144
144
|
this.pendingSettingsAck = true;
|
|
145
145
|
this.originSet = [];
|
|
146
|
-
|
|
146
|
+
// enablePush defaults to true on the client (node's initializeOptions sets
|
|
147
|
+
// NGHTTP2_SETTINGS_ENABLE_PUSH = 1 for clients; the server's OUR_DEFAULTS has
|
|
148
|
+
// false because a server does not receive pushes). The application can override
|
|
149
|
+
// via opts.settings.enablePush.
|
|
150
|
+
this.localSettings = { ...OUR_DEFAULTS, enablePush: true, ...opts.settings };
|
|
147
151
|
this.remoteSettings = { ...PEER_DEFAULTS };
|
|
148
152
|
this._scheme = scheme || 'http';
|
|
149
153
|
this._authority = authority || 'localhost';
|
|
@@ -156,6 +160,10 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
156
160
|
.filter((k) => ID_BY_SETTING[k] !== undefined));
|
|
157
161
|
|
|
158
162
|
this._nextSid = 1; // client owns odd ids
|
|
163
|
+
// Highest even id promised by the peer via PUSH_PROMISE. Used to silently
|
|
164
|
+
// ignore late frames for a pushed stream the application already destroyed
|
|
165
|
+
// (node does the same: session stays alive, measured v26.12.1).
|
|
166
|
+
this._maxPushSid = 0;
|
|
159
167
|
this._streams = new Map();
|
|
160
168
|
this._outboundQueue = []; // requests waiting for a concurrency slot
|
|
161
169
|
this._decoder = new Decoder(this.localSettings.headerTableSize);
|
|
@@ -237,6 +245,9 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
237
245
|
this._hdrSid = -1;
|
|
238
246
|
this._hdrEnd = false;
|
|
239
247
|
this._hdrCont = 0;
|
|
248
|
+
// Non-zero while a PUSH_PROMISE block is being assembled: the promised even
|
|
249
|
+
// stream id. _finishHeaders routes to _processPushPromise when this is set.
|
|
250
|
+
this._hdrPush = 0;
|
|
240
251
|
// The first octets of a HEADERS payload -- the pad length and the priority
|
|
241
252
|
// block -- are at most six, and a payload that arrived in pieces has no
|
|
242
253
|
// buffer to read them from: they are copied into this scratch.
|
|
@@ -1170,10 +1181,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1170
1181
|
}
|
|
1171
1182
|
return this._open; // contents deprecated (RFC 9218)
|
|
1172
1183
|
case PUSH_PROMISE:
|
|
1173
|
-
|
|
1174
|
-
// is for the server side); RFC 9113 6.6 makes an unexpected PUSH_PROMISE a
|
|
1175
|
-
// connection error of type PROTOCOL_ERROR, which is what node answers too.
|
|
1176
|
-
return this._fatal(PROTOCOL_ERROR, 'peer sent PUSH_PROMISE');
|
|
1184
|
+
return this._beginPushPromise(sid, flags, buf, start, len);
|
|
1177
1185
|
default:
|
|
1178
1186
|
return this._open; // RFC 9113 5.5: discard unknown
|
|
1179
1187
|
}
|
|
@@ -1385,19 +1393,132 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1385
1393
|
|
|
1386
1394
|
_finishHeaders() {
|
|
1387
1395
|
const sid = this._hdrSid, end = this._hdrEnd;
|
|
1396
|
+
const push = this._hdrPush;
|
|
1397
|
+
this._hdrPush = 0;
|
|
1388
1398
|
if (this._hdrPieces !== null) {
|
|
1389
1399
|
const pieces = this._hdrPieces;
|
|
1390
1400
|
const len = this._hdrLen;
|
|
1391
1401
|
this._hdrSid = -1;
|
|
1392
1402
|
this._hdrLen = 0;
|
|
1393
1403
|
this._hdrPieces = null;
|
|
1394
|
-
|
|
1404
|
+
if (push !== 0) {
|
|
1405
|
+
this._processPushPromise(sid, push, pieceConcat(pieces, 0, len));
|
|
1406
|
+
} else {
|
|
1407
|
+
this._processBlockPieces(sid, end, pieces, 0, len);
|
|
1408
|
+
}
|
|
1395
1409
|
return;
|
|
1396
1410
|
}
|
|
1397
1411
|
const block = this._hdr.subarray(0, this._hdrLen);
|
|
1398
1412
|
this._hdrSid = -1;
|
|
1399
1413
|
this._hdrLen = 0;
|
|
1400
|
-
|
|
1414
|
+
if (push !== 0) {
|
|
1415
|
+
this._processPushPromise(sid, push, block);
|
|
1416
|
+
} else {
|
|
1417
|
+
this._processHeaders(sid, end, block);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
// ---- PUSH_PROMISE (RFC 9113 6.6) --------------------------------------------
|
|
1422
|
+
// A server promising a resource on a client-owned (odd) stream. The payload is
|
|
1423
|
+
// a 4-octet promised even stream id followed by an HPACK request-header block.
|
|
1424
|
+
// The block obeys the same CONTINUATION rules as HEADERS, so the assembly
|
|
1425
|
+
// machinery (_hdrSid/_hdrLen/_appendHdr/_finishHeaders) is reused with _hdrPush
|
|
1426
|
+
// holding the promised id to route the finish to _processPushPromise.
|
|
1427
|
+
_beginPushPromise(sid, flags, buf, start, len) {
|
|
1428
|
+
// RFC 9113 6.6: PUSH_PROMISE MUST be sent on a client-initiated (odd) stream
|
|
1429
|
+
// that is open or half-closed(remote). Stream 0 or an even id is a connection
|
|
1430
|
+
// error. Measured on node v26.12.1: GOAWAY(PROTOCOL_ERROR).
|
|
1431
|
+
if (sid === 0 || (sid & 1) === 0) {
|
|
1432
|
+
return this._fatal(PROTOCOL_ERROR, 'PUSH_PROMISE on a non-client stream');
|
|
1433
|
+
}
|
|
1434
|
+
// RFC 9113 6.6: a client that has set SETTINGS_ENABLE_PUSH to 0 MUST treat
|
|
1435
|
+
// receipt as a connection error of type PROTOCOL_ERROR (measured on node).
|
|
1436
|
+
if (!this.localSettings.enablePush) {
|
|
1437
|
+
return this._fatal(PROTOCOL_ERROR, 'PUSH_PROMISE with push disabled');
|
|
1438
|
+
}
|
|
1439
|
+
if (len < 4) {
|
|
1440
|
+
return this._fatal(FRAME_SIZE_ERROR, 'PUSH_PROMISE payload too short');
|
|
1441
|
+
}
|
|
1442
|
+
let p = start;
|
|
1443
|
+
let l = len;
|
|
1444
|
+
if (flags & F_PADDED) {
|
|
1445
|
+
if (len < 1) {
|
|
1446
|
+
return this._fatal(PROTOCOL_ERROR, 'padded PUSH_PROMISE with an empty payload');
|
|
1447
|
+
}
|
|
1448
|
+
const pad = buf[p];
|
|
1449
|
+
p += 1;
|
|
1450
|
+
l -= 1 + pad;
|
|
1451
|
+
if (l < 4) {
|
|
1452
|
+
return this._fatal(PROTOCOL_ERROR, 'pad larger than the PUSH_PROMISE payload');
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
const promisedSid = buf.readUInt32BE(p) & 0x7fffffff;
|
|
1456
|
+
p += 4;
|
|
1457
|
+
l -= 4;
|
|
1458
|
+
// The promised id must be even and non-zero (RFC 9113 6.6).
|
|
1459
|
+
if (promisedSid === 0 || (promisedSid & 1) !== 0) {
|
|
1460
|
+
return this._fatal(PROTOCOL_ERROR, 'PUSH_PROMISE promised id is not even');
|
|
1461
|
+
}
|
|
1462
|
+
// A promised id already in use is a connection error (RFC 9113 5.1.1).
|
|
1463
|
+
if (this._streams.has(promisedSid)) {
|
|
1464
|
+
return this._fatal(PROTOCOL_ERROR, 'PUSH_PROMISE promised id already in use');
|
|
1465
|
+
}
|
|
1466
|
+
this._hdrCont = 0;
|
|
1467
|
+
if (flags & F_END_HEADERS) {
|
|
1468
|
+
this._processPushPromise(sid, promisedSid, buf.subarray(p, p + l));
|
|
1469
|
+
return this._open;
|
|
1470
|
+
}
|
|
1471
|
+
this._hdrSid = sid;
|
|
1472
|
+
this._hdrEnd = false;
|
|
1473
|
+
this._hdrPush = promisedSid;
|
|
1474
|
+
this._hdrLen = 0;
|
|
1475
|
+
this._appendHdr(buf, p, l);
|
|
1476
|
+
return this._open;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
// Decode the promise block and create the pushed stream. The HPACK block MUST
|
|
1480
|
+
// be decoded even if the application will refuse the push, because the dynamic
|
|
1481
|
+
// table state advances for every block on the connection.
|
|
1482
|
+
_processPushPromise(parentSid, promisedSid, block) {
|
|
1483
|
+
const flat = FLAT;
|
|
1484
|
+
try {
|
|
1485
|
+
this._decoder.decode(block, 0, block.length, flat);
|
|
1486
|
+
} catch (err) {
|
|
1487
|
+
if (err instanceof HPackError) {
|
|
1488
|
+
this._fatal(COMPRESSION_ERROR, err.message);
|
|
1489
|
+
} else {
|
|
1490
|
+
throw err;
|
|
1491
|
+
}
|
|
1492
|
+
return;
|
|
1493
|
+
}
|
|
1494
|
+
const fields = flat.items;
|
|
1495
|
+
const fcount = flat.count;
|
|
1496
|
+
const headers = this._toObject(fields, fcount);
|
|
1497
|
+
// Create the pushed stream. Its writable half is pre-ended: the client never
|
|
1498
|
+
// sends a request body for a push (node: writableEnded=true, writableFinished=true
|
|
1499
|
+
// at the 'stream' event, measured v26.12.1).
|
|
1500
|
+
const stream = new ClientHttp2Stream(this, promisedSid);
|
|
1501
|
+
stream._pushed = true;
|
|
1502
|
+
stream.rstCode = 0;
|
|
1503
|
+
stream._headersSent = true;
|
|
1504
|
+
stream._endSent = true;
|
|
1505
|
+
if (promisedSid > this._maxPushSid) {
|
|
1506
|
+
this._maxPushSid = promisedSid;
|
|
1507
|
+
}
|
|
1508
|
+
this._streams.set(promisedSid, stream);
|
|
1509
|
+
// End the writable without putting anything on the wire: _pump sees
|
|
1510
|
+
// _endSent=true and does nothing; _writableDone fires the Duplex final callback.
|
|
1511
|
+
stream.end();
|
|
1512
|
+
stream._writableDone();
|
|
1513
|
+
// The Duplex defers writableFinished to nextTick, but the application must see
|
|
1514
|
+
// it true at the 'stream' event (node: measured v26.12.1). Settle it now.
|
|
1515
|
+
stream._writableState.finished = true;
|
|
1516
|
+
// node emits session 'stream' with (pushedStream, requestHeaders, flags=4).
|
|
1517
|
+
// flags 4 = NGHTTP2_FLAG_END_HEADERS, the only flag PUSH_PROMISE carries that
|
|
1518
|
+
// the JS layer surfaces (measured v26.12.1). Synchronous so the application's
|
|
1519
|
+
// 'push' subscription is in place before the response HEADERS arrive in the
|
|
1520
|
+
// same read batch.
|
|
1521
|
+
this.emit('stream', stream, headers, 4);
|
|
1401
1522
|
}
|
|
1402
1523
|
|
|
1403
1524
|
// The shape cache: the server's design, ported. A block whose bytes have been
|
|
@@ -1504,6 +1625,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1504
1625
|
if ((sid & 1) === 1 && sid < this._nextSid) {
|
|
1505
1626
|
return;
|
|
1506
1627
|
}
|
|
1628
|
+
// A pushed stream the application destroyed: late frames are silently
|
|
1629
|
+
// ignored, the session lives (measured on node v26.12.1).
|
|
1630
|
+
if ((sid & 1) === 0 && sid <= this._maxPushSid) {
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1507
1633
|
return this._fatal(PROTOCOL_ERROR, 'response HEADERS on an unopened stream id');
|
|
1508
1634
|
}
|
|
1509
1635
|
if (this._refuseHeaders(sid, stream, fields, fcount)) {
|
package/lib/client-stream.js
CHANGED
|
@@ -59,6 +59,10 @@ class ClientHttp2Stream extends Duplex {
|
|
|
59
59
|
this.sentHeaders = null;
|
|
60
60
|
this.sentStatus = null;
|
|
61
61
|
this.responseEmitted = false;
|
|
62
|
+
// Set by the session for a stream created from PUSH_PROMISE: the first final
|
|
63
|
+
// response block emits 'push' instead of 'response' (node's event model,
|
|
64
|
+
// measured v26.12.1).
|
|
65
|
+
this._pushed = false;
|
|
62
66
|
this.additionalHeaders = [];
|
|
63
67
|
this.trailers = null;
|
|
64
68
|
}
|
|
@@ -118,7 +122,7 @@ class ClientHttp2Stream extends Duplex {
|
|
|
118
122
|
this.responseEmitted = true;
|
|
119
123
|
this.responseHeaders = headers;
|
|
120
124
|
this.sentStatus = status;
|
|
121
|
-
this.emit('response', headers);
|
|
125
|
+
this.emit(this._pushed ? 'push' : 'response', headers);
|
|
122
126
|
} else {
|
|
123
127
|
// A second final-status block after the response is the trailer block.
|
|
124
128
|
this.trailers = headers;
|
package/package.json
CHANGED