h2lean 0.19.8 → 0.19.9
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/README.md +12 -22
- package/lib/Http2Stream.js +13 -5
- package/lib/client/Http2ClientSession.js +215 -42
- package/lib/client/Http2ClientStream.js +61 -8
- package/lib/client/client.js +24 -2
- package/lib/server/Http2ServerSession.js +52 -5
- package/lib/server/Http2ServerStream.js +4 -4
- package/lib/server/compat.js +150 -47
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ Up to **8× less CPU per request** and **6.8× more requests per second** than `
|
|
|
33
33
|
- [Monkey-patching for frameworks without injection](#monkey-patching-for-frameworks-without-injection)
|
|
34
34
|
- 🛠️ [Advanced Tuning](#️-advanced-tuning)
|
|
35
35
|
- 🔒 [Security](#-security)
|
|
36
|
-
- ⚠️ [
|
|
36
|
+
- ⚠️ [Known Divergences](#️-known-divergences)
|
|
37
37
|
- 📚 [Documentation](#-documentation)
|
|
38
38
|
- 🤝 [Contributing](#-contributing)
|
|
39
39
|
- ⚖️ [License](#️-license)
|
|
@@ -93,7 +93,7 @@ Compatibility target is the **public surface** of `node:http2`: names, argument
|
|
|
93
93
|
|
|
94
94
|
Same class names (`Http2Session`, `Http2Stream`, `Http2ServerRequest`, `Http2ServerResponse`), same event names, same `Http2Session` and settings semantics, all 240 protocol constants re-exported from `node:http2` itself — user code that compares `error.code !== http2.constants.NGHTTP2_ENHANCE_YOUR_CALM` keeps working unchanged. Both transports on both sides: `createServer` (h2c), `createSecureServer` (h2s), `connect()` over cleartext and TLS with node's ALPN rules, and `performServerHandshake()` to run a server session over a socket you already hold.
|
|
95
95
|
|
|
96
|
-
Where behavior deliberately differs from node, it is **documented, measured, and pinned by tests** — never silent. Known divergences: [
|
|
96
|
+
Where behavior deliberately differs from node, it is **documented, measured, and pinned by tests** — never silent. Known divergences: [DIVERGENCES.md](./DIVERGENCES.md).
|
|
97
97
|
|
|
98
98
|
## 📦 Installation
|
|
99
99
|
|
|
@@ -266,40 +266,30 @@ nodeHttp2.connect = h2lean.connect;
|
|
|
266
266
|
h2lean terminates **untrusted bytes from the internet** and turns them into objects your application trusts — the threat model is treated as the primary design constraint ([SECURITY.md](./SECURITY.md)):
|
|
267
267
|
|
|
268
268
|
- 🛡️ **Every denial-of-service limit matches nghttp2** (the engine inside node) or was measured against a live node server — a peer's budget against node is its budget against h2lean: SETTINGS-entry cap (CVE-2020-11080), CONTINUATION cap (CVE-2024-28182), rapid-reset budget (CVE-2023-44487), outbound staging, parked-stream and history bounds.
|
|
269
|
-
- 🚫 **Request-smuggling defense by construction** (RFC 9113 §10.3): field names must be lower-case tokens, values may not carry NUL/CR/LF, connection-specific fields are refused in both directions — all refused at encode time, *before a single octet reaches the wire*.
|
|
269
|
+
- 🚫 **Request-smuggling defense by construction** (RFC 9113 §10.3): field names must be lower-case tokens, values may not carry NUL/CR/LF, connection-specific fields are refused in both directions — all refused at encode time, *before a single octet reaches the wire*. This is a deliberate, measured divergence from node in the strict direction ([DIVERGENCES.md](./DIVERGENCES.md), entry 19).
|
|
270
270
|
- ✅ **Verified, not assumed:**
|
|
271
271
|
- **300+ differential tests** — application scripts run against both `node:http2` and h2lean, transcripts compared;
|
|
272
272
|
- **h2spec 146/146** — full RFC 9113/7541 conformance, same verdict as node's own HTTP/2 (`npm run spec`);
|
|
273
273
|
- **mutation-tested guards** — every defensive limit is rewritten in place and proven to fail the suite ([tools/mutate-guards.js](./tools/mutate-guards.js)).
|
|
274
274
|
|
|
275
|
-
## ⚠️ Known Divergences
|
|
275
|
+
## ⚠️ Known Divergences
|
|
276
276
|
|
|
277
277
|
The core surface is implemented: server (stream + compat), client (`connect`),
|
|
278
|
-
`performServerHandshake`, and server push (`pushStream` / PUSH_PROMISE reception).
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
None silently returns a wrong answer: a missing method is `undefined`, so calling it throws
|
|
282
|
-
`TypeError`, never a fabricated result.
|
|
278
|
+
`performServerHandshake`, and server push (`pushStream` / PUSH_PROMISE reception).
|
|
279
|
+
None of the remaining gaps silently returns a wrong answer: a missing method is
|
|
280
|
+
`undefined`, so calling it throws `TypeError`, never a fabricated result.
|
|
283
281
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
**Events not emitted:**
|
|
290
|
-
|
|
291
|
-
- 🪤 **`'ready'`** on a stream (node emits it when the stream id is assigned).
|
|
292
|
-
|
|
293
|
-
**Behavioural differences** (full reasoning in the header comment of the affected file and [AGENTS.md](./AGENTS.md) §10):
|
|
294
|
-
|
|
295
|
-
- 🎛️ **Options accepted but ignored:** `headerHintTimeout`/`headerHintSize` (no 1xx informational-flood guard). `paddingStrategy` (0/1/2, with node's 8-octet-aligned and 255-octet-pad wire shapes) and `maxSessionMemory` (per-stream and retained-header budget with node's ENHANCE_YOUR_CALM refusal and callback-failure teardown) are now implemented; see [SECURITY.md](./SECURITY.md).
|
|
296
|
-
- 🚪 The **graceful-close predicate**, the **spurious `200` on `end()` with no body**, a **response queued in the same batch as a refusal**, the **`COMPRESSION_ERROR` vs `FRAME_SIZE_ERROR`** verdict, the **teardown event order**, and **`req.pause()/read()` not slowing the peer** (credit is window-bounded, not per-`read()`) differ from node — each pinned as a KNOWN DIVERGENCE in the test suite.
|
|
282
|
+
Every known divergence — its scenario, node's measured behaviour, h2lean's
|
|
283
|
+
behaviour and the reason it is not implemented — is recorded in
|
|
284
|
+
[DIVERGENCES.md](./DIVERGENCES.md). That file is the single home for this
|
|
285
|
+
material; it is a decision record, not a to-do list.
|
|
297
286
|
|
|
298
287
|
## 📚 Documentation
|
|
299
288
|
|
|
300
289
|
| Document | What it holds |
|
|
301
290
|
|---|---|
|
|
302
291
|
| [PERFORMANCE.md](./PERFORMANCE.md) | 🏎️ Benchmarks: methodology, full tables, caveats, re-run commands |
|
|
292
|
+
| [DIVERGENCES.md](./DIVERGENCES.md) | 🔀 Every known divergence from node, measured and explained |
|
|
303
293
|
| [SECURITY.md](./SECURITY.md) | 🔒 Threat model, every hardening limit, known gaps — stated plainly |
|
|
304
294
|
| [OPTIMIZATION.md](./OPTIMIZATION.md) | 🔬 All 35 measured optimization techniques (and rejected ones) |
|
|
305
295
|
| [CHANGELOG.md](./CHANGELOG.md) | 📋 Release history, Keep-a-Changelog format |
|
package/lib/Http2Stream.js
CHANGED
|
@@ -673,22 +673,30 @@ function specType(v) {
|
|
|
673
673
|
// ("must be of type number"), a non-integer number is ERR_OUT_OF_RANGE ("It must be an
|
|
674
674
|
// integer"), and a code outside [0, 2^32-1] is ERR_OUT_OF_RANGE ("It must be >= 0 &&
|
|
675
675
|
// <= 4294967295"). The callback's own message names "callback" and follows the same
|
|
676
|
-
// determineSpecificType shapes (measured: string, null, number).
|
|
676
|
+
// determineSpecificType shapes (measured: string, null, number). node builds the
|
|
677
|
+
// validators off TypeError (ERR_INVALID_ARG_TYPE) and RangeError (ERR_OUT_OF_RANGE)
|
|
678
|
+
// in internal/errors.js, not off plain Error -- the class is observable on a
|
|
679
|
+
// caught throw (test/upstream/client-rststream-before-connect.js).
|
|
680
|
+
function rstValidatorErr(code, msg) {
|
|
681
|
+
const err = code === 'ERR_INVALID_ARG_TYPE' ? new TypeError(msg) : new RangeError(msg);
|
|
682
|
+
err.code = code;
|
|
683
|
+
return err;
|
|
684
|
+
}
|
|
677
685
|
function validateRstCode(code, callback) {
|
|
678
686
|
if (typeof code !== 'number') {
|
|
679
|
-
throw
|
|
687
|
+
throw rstValidatorErr('ERR_INVALID_ARG_TYPE',
|
|
680
688
|
`The "code" argument must be of type number. Received ${specType(code)}`);
|
|
681
689
|
}
|
|
682
690
|
if (!Number.isInteger(code)) {
|
|
683
|
-
throw
|
|
691
|
+
throw rstValidatorErr('ERR_OUT_OF_RANGE',
|
|
684
692
|
`The value of "code" is out of range. It must be an integer. Received ${code}`);
|
|
685
693
|
}
|
|
686
694
|
if (code < 0 || code > 4294967295) {
|
|
687
|
-
throw
|
|
695
|
+
throw rstValidatorErr('ERR_OUT_OF_RANGE',
|
|
688
696
|
`The value of "code" is out of range. It must be >= 0 && <= 4294967295. Received ${code}`);
|
|
689
697
|
}
|
|
690
698
|
if (callback !== undefined && typeof callback !== 'function') {
|
|
691
|
-
throw
|
|
699
|
+
throw rstValidatorErr('ERR_INVALID_ARG_TYPE',
|
|
692
700
|
`The "callback" argument must be of type function. Received ${specType(callback)}`);
|
|
693
701
|
}
|
|
694
702
|
}
|
|
@@ -129,6 +129,7 @@
|
|
|
129
129
|
const { EventEmitter } = require('node:events');
|
|
130
130
|
const net = require('node:net');
|
|
131
131
|
const tls = require('node:tls');
|
|
132
|
+
const { deprecate } = require('node:util');
|
|
132
133
|
const f = require('../frames');
|
|
133
134
|
const { Decoder, Encoder, HPackError } = require('../hpack');
|
|
134
135
|
const FlatList = require('../flat-list');
|
|
@@ -142,6 +143,7 @@ const FLAT = new FlatList(64);
|
|
|
142
143
|
const { nullProto, isFieldName, lowerName, neverNames, CONNECTION_SPECIFIC, SINGLE_VALUE_FIELDS,
|
|
143
144
|
KSensitiveHeaders } = require('../headers');
|
|
144
145
|
const { Http2ClientStream, AbortError } = require('./Http2ClientStream');
|
|
146
|
+
const { streamErrorOf } = require('../Http2Stream');
|
|
145
147
|
const { pieceByteAt, pieceSlice, pieceConcat, pieceEquals } = require('../pieces');
|
|
146
148
|
// The settings tables, defaults and buffer-path constants are the server's, reused
|
|
147
149
|
// verbatim so a client and a server in this one library never disagree about what a
|
|
@@ -150,7 +152,8 @@ const {
|
|
|
150
152
|
OUR_DEFAULTS, PEER_DEFAULTS, SETTINGS_BY_ID, ID_BY_SETTING, BODY_MIN, MAX_OUT_BATCH,
|
|
151
153
|
STAGE_MIN, MAX_SHAPES, MAX_SEND_BLOCK, PRIORITY_SPECLEN,
|
|
152
154
|
strictSingleValueFields, hashBlock, hashPieces, kBoundSession,
|
|
153
|
-
proxySocketHandler, urlOrigin, parseAltSvc, parseOrigins, received,
|
|
155
|
+
proxySocketHandler, urlOrigin, parseAltSvc, parseOrigins, received, outOfRangeValue,
|
|
156
|
+
extError, checkPing,
|
|
154
157
|
pingCancelled, checkSettings, buildSubmission, submissionValues, accumulateCustom,
|
|
155
158
|
customView, checkRemoteCustomEntries
|
|
156
159
|
} = require('../server/Http2ServerSession');
|
|
@@ -190,11 +193,13 @@ const DEFAULT_WINDOW = 65535;
|
|
|
190
193
|
// carries). Re-running an uncached regex here would be the same work the response
|
|
191
194
|
// path has already learned to skip.
|
|
192
195
|
|
|
193
|
-
//
|
|
194
|
-
//
|
|
196
|
+
// node's kNoPayloadMethods (internal/http2/util.js) is exactly DELETE, GET and
|
|
197
|
+
// HEAD. CONNECT leaves the writable half open so the tunnel can carry bytes;
|
|
198
|
+
// trailing CONNECT on END_STREAM stopped the peer from emitting 'connect'
|
|
199
|
+
// (test/upstream/client-connection-tunnelling). OPTIONS and TRACE are not in
|
|
200
|
+
// node's set either -- a request body on them is legal and must stay open.
|
|
195
201
|
function payloadMeaningless(method) {
|
|
196
|
-
return method === 'GET' || method === 'HEAD' || method === 'DELETE'
|
|
197
|
-
|| method === 'OPTIONS' || method === 'TRACE';
|
|
202
|
+
return method === 'GET' || method === 'HEAD' || method === 'DELETE';
|
|
198
203
|
}
|
|
199
204
|
|
|
200
205
|
// assertIsObject + validateSettings live in lib/server/Http2ServerSession.js as checkSettings, beside checkPing:
|
|
@@ -206,6 +211,13 @@ function payloadMeaningless(method) {
|
|
|
206
211
|
// measured position is unchanged (OPTIMIZATION.md item 36).
|
|
207
212
|
const DEFAULT_METHOD = 'GET';
|
|
208
213
|
|
|
214
|
+
// node's request() deprecates the priority `weight` option with DEP0194
|
|
215
|
+
// (internal/http2/core.js setAndValidatePriorityOptions). A module-level
|
|
216
|
+
// util.deprecate shares node's process-wide codesWarned, so the warning fires
|
|
217
|
+
// once no matter how many sessions pass the option.
|
|
218
|
+
const deprecateWeight = deprecate(() => {},
|
|
219
|
+
'Priority signaling has been deprecated as of RFC 9113.', 'DEP0194');
|
|
220
|
+
|
|
209
221
|
// The estimate _encodeRequest just computed, read by its caller immediately after --
|
|
210
222
|
// the result-slot shape lib/frames.js and lib/headers.js use, for the same reason: no
|
|
211
223
|
// allocation per block. Only _encodeRequest writes it.
|
|
@@ -381,6 +393,10 @@ class Http2ClientSession extends EventEmitter {
|
|
|
381
393
|
// the only value we can promise it credit against -- see _creditRecv(). At the
|
|
382
394
|
// default this is the same number the old `>= advertised >> 1` test produced.
|
|
383
395
|
this._connRecvWin = Math.min(this.localSettings.initialWindowSize, DEFAULT_WINDOW);
|
|
396
|
+
// The window last advertised through SETTINGS. node keeps it apart from the
|
|
397
|
+
// connection's available window: setLocalWindowSize lowers only this one, while
|
|
398
|
+
// the connection window never shrinks (core.js:1457, measured on v26.12.1).
|
|
399
|
+
this._effectiveLocalWindowSize = this.localSettings.initialWindowSize;
|
|
384
400
|
// Streams that took DATA since the last credit pass. _creditRecv() visits only
|
|
385
401
|
// these instead of scanning every open stream: with the default 100 concurrent
|
|
386
402
|
// streams a download that touches one of them used to pay ~100 property reads per
|
|
@@ -630,9 +646,10 @@ class Http2ClientSession extends EventEmitter {
|
|
|
630
646
|
// const sess = lite.connect(url, { createConnection: () => alreadyOpenSocket });
|
|
631
647
|
// sess.on('connect', handler); // attached after emit already ran synchronously
|
|
632
648
|
this._tick(() => {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
649
|
+
// node's setupHandle emits 'connect' on the next tick even when close() or
|
|
650
|
+
// destroy() already happened while the socket was connecting (core.js:1110).
|
|
651
|
+
// The queued requests below keep their own liveness checks, so a session
|
|
652
|
+
// that died in the meantime submits nothing.
|
|
636
653
|
this.emit('connect');
|
|
637
654
|
if (this._queuedReq !== null) {
|
|
638
655
|
const q = this._queuedReq;
|
|
@@ -645,8 +662,13 @@ class Http2ClientSession extends EventEmitter {
|
|
|
645
662
|
// connecting (req.destroy(), an aborted signal) submits nothing: the
|
|
646
663
|
// server never sees a stream for it. A session closed in the meantime
|
|
647
664
|
// refuses the request instead, with the same GOAWAY_SESSION error
|
|
648
|
-
// request() itself would have produced.
|
|
649
|
-
|
|
665
|
+
// request() itself would have produced. A close() parked before connect
|
|
666
|
+
// is the exception: it is not destroyed and still owes the peer its
|
|
667
|
+
// HEADERS and RST_STREAM (core.js:2113-2122).
|
|
668
|
+
if (this.destroyed) {
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
if (r.stream.destroyed && r.stream._pendingCloseCode === undefined) {
|
|
650
672
|
continue;
|
|
651
673
|
}
|
|
652
674
|
if (this.closed) {
|
|
@@ -891,7 +913,15 @@ class Http2ClientSession extends EventEmitter {
|
|
|
891
913
|
} else if (this.closed) {
|
|
892
914
|
requestError = httpErr('ERR_HTTP2_GOAWAY_SESSION',
|
|
893
915
|
'New streams cannot be created after receiving a GOAWAY');
|
|
894
|
-
}
|
|
916
|
+
} else if (this._nextSid > 0x7fffffff) {
|
|
917
|
+
// node's binding refuses the 2^31st client id with STREAM_ID_NOT_AVAILABLE,
|
|
918
|
+
// which requestOnConnect reports on the stream (internal/http2/core.js).
|
|
919
|
+
// The reserved id is not advanced by a refusal -- state.nextStreamID stays
|
|
920
|
+
// 2147483649 after setNextStreamID(0x7fffffff) and two requests.
|
|
921
|
+
requestError = httpErr('ERR_HTTP2_OUT_OF_STREAMS',
|
|
922
|
+
'No stream ID is available because maximum stream ID has been reached');
|
|
923
|
+
}
|
|
924
|
+
checkRequestOptions(options);
|
|
895
925
|
// No defensive copy of options: node spreads it because it validates and
|
|
896
926
|
// then MUTATES the copy (setAndValidatePriorityOptions, options.endStream =
|
|
897
927
|
// ... in internal/http2/core.js request()); h2lean has no priority options
|
|
@@ -948,6 +978,48 @@ class Http2ClientSession extends EventEmitter {
|
|
|
948
978
|
// applies neverIndex.includes(key) to pseudo lines as well).
|
|
949
979
|
list.push(':method', method, neverList !== null && neverList.has(':method'));
|
|
950
980
|
}
|
|
981
|
+
// A plain CONNECT (no :protocol) gets no default :path/:scheme/:authority:
|
|
982
|
+
// node's prepareRequestHeadersObject/Array (internal/http2/util.js) requires
|
|
983
|
+
// the application to supply :authority and refuses a :scheme or :path. An
|
|
984
|
+
// extended CONNECT with :protocol keeps the ordinary pseudo-header fills.
|
|
985
|
+
let plainConnect = false;
|
|
986
|
+
if (method === 'CONNECT') {
|
|
987
|
+
let hasProtocol = false;
|
|
988
|
+
for (let i = 0; i < list.length; i += 3) {
|
|
989
|
+
if (list[i] === ':protocol') {
|
|
990
|
+
hasProtocol = true;
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
plainConnect = !hasProtocol;
|
|
995
|
+
}
|
|
996
|
+
if (plainConnect) {
|
|
997
|
+
let hasAuthority = false;
|
|
998
|
+
let hasScheme = false;
|
|
999
|
+
let hasPath = false;
|
|
1000
|
+
for (let i = 0; i < list.length; i += 3) {
|
|
1001
|
+
const key = list[i];
|
|
1002
|
+
if (key === ':authority') {
|
|
1003
|
+
hasAuthority = true;
|
|
1004
|
+
} else if (key === ':scheme') {
|
|
1005
|
+
hasScheme = true;
|
|
1006
|
+
} else if (key === ':path') {
|
|
1007
|
+
hasPath = true;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
if (!hasAuthority) {
|
|
1011
|
+
throw httpErr('ERR_HTTP2_CONNECT_AUTHORITY',
|
|
1012
|
+
':authority header is required for CONNECT requests');
|
|
1013
|
+
}
|
|
1014
|
+
if (hasScheme) {
|
|
1015
|
+
throw httpErr('ERR_HTTP2_CONNECT_SCHEME',
|
|
1016
|
+
'The :scheme header is forbidden for CONNECT requests');
|
|
1017
|
+
}
|
|
1018
|
+
if (hasPath) {
|
|
1019
|
+
throw httpErr('ERR_HTTP2_CONNECT_PATH',
|
|
1020
|
+
'The :path header is forbidden for CONNECT requests');
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
951
1023
|
|
|
952
1024
|
let endStream = options === undefined ? undefined : options.endStream;
|
|
953
1025
|
if (endStream === undefined) {
|
|
@@ -959,7 +1031,11 @@ class Http2ClientSession extends EventEmitter {
|
|
|
959
1031
|
// internal/http2/core.js:1902-1904); the getter folds it into the request map on read.
|
|
960
1032
|
stream._sentSrc = headersParam;
|
|
961
1033
|
this._trackClose(stream);
|
|
962
|
-
|
|
1034
|
+
// A refusal reserves no id: node's binding never submits the request, so a
|
|
1035
|
+
// later request sees the same nextStreamID (client-onconnect-errors).
|
|
1036
|
+
if (requestError === undefined) {
|
|
1037
|
+
this._nextSid += 2; // the id is reserved now, HEADERS may wait
|
|
1038
|
+
}
|
|
963
1039
|
// RFC 8336: a 421 (Misdirected Request) answer removes the origin that request
|
|
964
1040
|
// was sent to from `originSet`. node records the origin on the stream at
|
|
965
1041
|
// request() time -- `stream[kOrigin] = ${scheme}://${authority}` (core.js), where
|
|
@@ -1035,11 +1111,12 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1035
1111
|
// object across requests, the HPACK encoder produces identical bytes every time
|
|
1036
1112
|
// after the dynamic table stabilises, and _sendRequest can use _blocks to replay
|
|
1037
1113
|
// the encoded block without re-encoding. (The server's shareHeaders equivalent.)
|
|
1038
|
-
const req = { stream, list, nl: neverList, end: endStream, hp: headersParam };
|
|
1114
|
+
const req = { stream, list, nl: neverList, end: endStream, hp: headersParam, plainConnect };
|
|
1039
1115
|
if (requestError !== undefined) {
|
|
1040
1116
|
// node's requestOnError: destroy the stream with the captured error on the
|
|
1041
|
-
// next tick, and submit nothing. The id
|
|
1042
|
-
// session
|
|
1117
|
+
// next tick, and submit nothing. The id is never used here: a destroyed or
|
|
1118
|
+
// GOAWAYed session submits nothing, and at the ID ceiling no id was
|
|
1119
|
+
// reserved at all.
|
|
1043
1120
|
this._tick(() => stream.destroy(requestError));
|
|
1044
1121
|
} else if (this._started) {
|
|
1045
1122
|
this._enqueue(req);
|
|
@@ -1088,7 +1165,20 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1088
1165
|
req.stream._opened = true;
|
|
1089
1166
|
this._liveWire++;
|
|
1090
1167
|
this._streams.set(req.stream.id, req.stream);
|
|
1091
|
-
this._sendRequest(req.stream, req.list, req.nl, req.end, req.hp);
|
|
1168
|
+
this._sendRequest(req.stream, req.list, req.nl, req.end, req.hp, req.plainConnect);
|
|
1169
|
+
if (req.stream._pendingCloseCode !== undefined) {
|
|
1170
|
+
// node's finishCloseStream defers the reset to 'ready' for a stream closed
|
|
1171
|
+
// while pending (core.js:2113). _sendRequest has framed the HEADERS by now;
|
|
1172
|
+
// rstStream() would skip the stream because push(null) set _rEof and the
|
|
1173
|
+
// empty body set _endSent, so submit the frame here and retire the id the
|
|
1174
|
+
// same way rstStream does, before the stream's own teardown runs.
|
|
1175
|
+
const code = req.stream._pendingCloseCode;
|
|
1176
|
+
const body = Buffer.allocUnsafe(4);
|
|
1177
|
+
body.writeUInt32BE(code >>> 0, 0);
|
|
1178
|
+
this._frame(RST_STREAM, 0, req.stream.id, body, true);
|
|
1179
|
+
this._dropStream(req.stream.id);
|
|
1180
|
+
req.stream.destroy(streamErrorOf(code));
|
|
1181
|
+
}
|
|
1092
1182
|
}
|
|
1093
1183
|
|
|
1094
1184
|
// A slot just freed (a stream closed): start queued requests while the peer's
|
|
@@ -1197,7 +1287,7 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1197
1287
|
return bound;
|
|
1198
1288
|
}
|
|
1199
1289
|
|
|
1200
|
-
_sendRequest(stream, list, neverList, end, hp) {
|
|
1290
|
+
_sendRequest(stream, list, neverList, end, hp, plainConnect) {
|
|
1201
1291
|
// Fill the pseudo-headers the application omitted, in the canonical order, then
|
|
1202
1292
|
// hand the whole list to HPACK. node completes :path to '/', :scheme and
|
|
1203
1293
|
// :authority from the session; we mirror that so `request({})` is a GET /.
|
|
@@ -1223,15 +1313,17 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1223
1313
|
block = cached.block;
|
|
1224
1314
|
bound = cached.bound;
|
|
1225
1315
|
} else {
|
|
1226
|
-
if (!
|
|
1227
|
-
list.
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
list.
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
list.
|
|
1234
|
-
|
|
1316
|
+
if (!plainConnect) {
|
|
1317
|
+
if (!list.includes(':path')) {
|
|
1318
|
+
list.push(':path', '/', neverList !== null && neverList.has(':path'));
|
|
1319
|
+
}
|
|
1320
|
+
if (!list.includes(':scheme')) {
|
|
1321
|
+
list.push(':scheme', this._scheme, neverList !== null && neverList.has(':scheme'));
|
|
1322
|
+
}
|
|
1323
|
+
if (!list.includes(':authority')) {
|
|
1324
|
+
list.push(':authority', this._authority,
|
|
1325
|
+
neverList !== null && neverList.has(':authority'));
|
|
1326
|
+
}
|
|
1235
1327
|
}
|
|
1236
1328
|
orderPseudo(list); // in place: `list` is built by _addField for this request only
|
|
1237
1329
|
bound = this._boundOfList(list);
|
|
@@ -1750,7 +1842,7 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1750
1842
|
// out: the release is due when EITHER the half-window step says so or the window the
|
|
1751
1843
|
// peer may fill without hearing from us drops under the step.
|
|
1752
1844
|
_creditRecv() {
|
|
1753
|
-
const step = Math.min(this.
|
|
1845
|
+
const step = Math.min(this._effectiveLocalWindowSize, DEFAULT_WINDOW) >> 1;
|
|
1754
1846
|
const debt = this._debt;
|
|
1755
1847
|
// Compacted in place: a stream whose bytes have not yet crossed the half-window
|
|
1756
1848
|
// step STAYS in the list (and stays marked), because the credit it owes is only
|
|
@@ -3058,11 +3150,10 @@ class Http2ClientSession extends EventEmitter {
|
|
|
3058
3150
|
this.goaway(code === undefined ? NO_ERROR : code);
|
|
3059
3151
|
this._maybeClose();
|
|
3060
3152
|
if (typeof cb === 'function') {
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
}
|
|
3153
|
+
// node's close() always subscribes with once('close', cb) (core.js:1652):
|
|
3154
|
+
// the callback follows the session's 'close' event, even when no stream
|
|
3155
|
+
// was ever opened (test/upstream/client-shutdown-before-connect.test.js).
|
|
3156
|
+
this.once('close', cb);
|
|
3066
3157
|
}
|
|
3067
3158
|
}
|
|
3068
3159
|
|
|
@@ -3294,18 +3385,51 @@ class Http2ClientSession extends EventEmitter {
|
|
|
3294
3385
|
if (this.destroyed) {
|
|
3295
3386
|
throw httpErr('ERR_HTTP2_INVALID_SESSION', 'The session has been destroyed');
|
|
3296
3387
|
}
|
|
3297
|
-
|
|
3388
|
+
// node's Http2Session.setNextStreamID (core.js:1444) runs validateNumber and
|
|
3389
|
+
// then ERR_OUT_OF_RANGE before touching the binding. The destroyed guard is
|
|
3390
|
+
// first there too, so a destroyed session still reports INVALID_SESSION.
|
|
3391
|
+
if (typeof id !== 'number') {
|
|
3392
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
3393
|
+
`The "id" argument must be of type number. Received ${received(id)}`);
|
|
3394
|
+
}
|
|
3395
|
+
if (id <= 0 || id > 0xffffffff) {
|
|
3396
|
+
const err = new RangeError('The value of "id" is out of range. '
|
|
3397
|
+
+ `It must be > 0 and <= 4294967295. Received ${outOfRangeValue(id)}`);
|
|
3398
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
3399
|
+
throw err;
|
|
3400
|
+
}
|
|
3401
|
+
this._nextSid = id;
|
|
3298
3402
|
}
|
|
3299
3403
|
setLocalWindowSize(size) {
|
|
3300
3404
|
if (this.destroyed) {
|
|
3301
3405
|
throw httpErr('ERR_HTTP2_INVALID_SESSION', 'The session has been destroyed');
|
|
3302
3406
|
}
|
|
3303
|
-
//
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3407
|
+
// validateInt32(windowSize, 'windowSize', 0) in node's core.js:1457.
|
|
3408
|
+
if (typeof size !== 'number') {
|
|
3409
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
3410
|
+
`The "windowSize" argument must be of type number. Received ${received(size)}`);
|
|
3411
|
+
}
|
|
3412
|
+
if (!Number.isInteger(size)) {
|
|
3413
|
+
const err = new RangeError('The value of "windowSize" is out of range. '
|
|
3414
|
+
+ `It must be an integer. Received ${outOfRangeValue(size)}`);
|
|
3415
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
3416
|
+
throw err;
|
|
3417
|
+
}
|
|
3418
|
+
if (size < 0 || size > 0x7fffffff) {
|
|
3419
|
+
const err = new RangeError('The value of "windowSize" is out of range. '
|
|
3420
|
+
+ `It must be >= 0 && <= 2147483647. Received ${outOfRangeValue(size)}`);
|
|
3421
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
3422
|
+
throw err;
|
|
3423
|
+
}
|
|
3424
|
+
// core.js:1462 hands the size to nghttp2, which rescales the peer's send windows
|
|
3425
|
+
// retroactively (RFC 9113 6.9.2) but never reclaims the connection's available
|
|
3426
|
+
// credit: a decrease moves only the advertised window that _creditRecv()'s step
|
|
3427
|
+
// is derived from. Measured on v26.12.1: 2**20 then 20 reports 20/1048576/65535,
|
|
3428
|
+
// and a fresh session set to 20 reports 20/65535/65535.
|
|
3429
|
+
this._effectiveLocalWindowSize = size;
|
|
3430
|
+
if (size > this._connRecvWin) {
|
|
3431
|
+
this._connRecvWin = size;
|
|
3432
|
+
}
|
|
3309
3433
|
}
|
|
3310
3434
|
|
|
3311
3435
|
// ---- teardown ---------------------------------------------------------------
|
|
@@ -3438,7 +3562,12 @@ class Http2ClientSession extends EventEmitter {
|
|
|
3438
3562
|
socket.uncork();
|
|
3439
3563
|
}
|
|
3440
3564
|
}
|
|
3441
|
-
|
|
3565
|
+
// node's C layer names the two fatal classes, not the frame that triggered
|
|
3566
|
+
// them: measured off v26.12.1, every fatal code reports 'Protocol error'
|
|
3567
|
+
// except FLOW_CONTROL_ERROR's 'Flow control error' (the GOAWAY debug string
|
|
3568
|
+
// still carries this frame's own reason).
|
|
3569
|
+
const err = httpErr('ERR_HTTP2_ERROR',
|
|
3570
|
+
code === FLOW_CONTROL_ERROR ? 'Flow control error' : 'Protocol error');
|
|
3442
3571
|
err.http2Code = code;
|
|
3443
3572
|
this.destroy(err);
|
|
3444
3573
|
return false;
|
|
@@ -3537,10 +3666,10 @@ class Http2ClientSession extends EventEmitter {
|
|
|
3537
3666
|
// are provided; the dynamic-table figures are reported live off the codecs.
|
|
3538
3667
|
get state() {
|
|
3539
3668
|
return {
|
|
3540
|
-
effectiveLocalWindowSize: this.
|
|
3669
|
+
effectiveLocalWindowSize: this._effectiveLocalWindowSize,
|
|
3541
3670
|
effectiveRecvDataLength: 0,
|
|
3542
3671
|
nextStreamID: this._nextSid,
|
|
3543
|
-
localWindowSize: this.
|
|
3672
|
+
localWindowSize: this._connRecvWin - this._connRecvUsed,
|
|
3544
3673
|
lastProcStreamID: this._goawayLastStreamID === null ? 0 : this._goawayLastStreamID,
|
|
3545
3674
|
remoteWindowSize: this._connSendWin,
|
|
3546
3675
|
outboundQueueSize: this._outboundQueue.length,
|
|
@@ -3634,6 +3763,50 @@ function orderPseudo(list) {
|
|
|
3634
3763
|
}
|
|
3635
3764
|
}
|
|
3636
3765
|
|
|
3766
|
+
// node's ClientHttp2Session.request() validates the four request options before
|
|
3767
|
+
// it reserves a stream (internal/http2/core.js setAndValidatePriorityOptions and
|
|
3768
|
+
// validateBoolean(options.endStream, 'options.endStream')). h2lean ignores the
|
|
3769
|
+
// priority VALUES -- RFC 9113 removed priority signalling -- but the type checks
|
|
3770
|
+
// are public surface: a wrong type must throw node's error, not be accepted. The
|
|
3771
|
+
// checks are kept out of request() itself so the common no-options path pays for
|
|
3772
|
+
// one call with an immediate return.
|
|
3773
|
+
function checkRequestOptions(options) {
|
|
3774
|
+
if (options === undefined) {
|
|
3775
|
+
return;
|
|
3776
|
+
}
|
|
3777
|
+
if ('weight' in options) {
|
|
3778
|
+
deprecateWeight();
|
|
3779
|
+
}
|
|
3780
|
+
const parent = options.parent;
|
|
3781
|
+
if (parent !== undefined) {
|
|
3782
|
+
if (typeof parent !== 'number') {
|
|
3783
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
3784
|
+
`The "options.parent" property must be of type number. Received ${received(parent)}`);
|
|
3785
|
+
}
|
|
3786
|
+
if (parent < 0) {
|
|
3787
|
+
const err = new RangeError('The value of "options.parent" is out of range. '
|
|
3788
|
+
+ `It must be >= 0. Received ${parent}`);
|
|
3789
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
3790
|
+
throw err;
|
|
3791
|
+
}
|
|
3792
|
+
}
|
|
3793
|
+
const exclusive = options.exclusive;
|
|
3794
|
+
if (exclusive !== undefined && typeof exclusive !== 'boolean') {
|
|
3795
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
3796
|
+
`The "options.exclusive" property must be of type boolean. Received ${received(exclusive)}`);
|
|
3797
|
+
}
|
|
3798
|
+
const silent = options.silent;
|
|
3799
|
+
if (silent !== undefined && typeof silent !== 'boolean') {
|
|
3800
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
3801
|
+
`The "options.silent" property must be of type boolean. Received ${received(silent)}`);
|
|
3802
|
+
}
|
|
3803
|
+
const endStream = options.endStream;
|
|
3804
|
+
if (endStream !== undefined && typeof endStream !== 'boolean') {
|
|
3805
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
3806
|
+
`The "options.endStream" property must be of type boolean. Received ${received(endStream)}`);
|
|
3807
|
+
}
|
|
3808
|
+
}
|
|
3809
|
+
|
|
3637
3810
|
function httpErr(code, msg) {
|
|
3638
3811
|
const err = new Error(msg);
|
|
3639
3812
|
err.code = code;
|
|
@@ -59,19 +59,18 @@
|
|
|
59
59
|
// END_STREAM" knowledge lives in `_endOnHeaders`, which the request pump
|
|
60
60
|
// reads to suppress an empty trailing DATA.
|
|
61
61
|
// * `state`, `headRequest` and `close(code, callback)` are node's Http2Stream
|
|
62
|
-
// surface, provided here too.
|
|
62
|
+
// surface, provided here too. Two divergences, both on shapes node can
|
|
63
63
|
// observe and h2lean cannot produce: node spends one loop turn reporting
|
|
64
64
|
// state:1/weight:0/localWindowSize:0 between request() and the session's
|
|
65
65
|
// first flush -- the HEADERS are framed inside the call here, so the getter
|
|
66
|
-
// reports the post-HEADERS state immediately;
|
|
67
|
-
// behind a still-connecting session destroys it without anything going out,
|
|
68
|
-
// where node sends the HEADERS once the session is ready and resets the
|
|
69
|
-
// stream right after; and `state.localWindowSize` shrinks when DATA ARRIVES
|
|
66
|
+
// reports the post-HEADERS state immediately; and `state.localWindowSize` shrinks when DATA ARRIVES
|
|
70
67
|
// rather than when the application consumes it (node consumes inside
|
|
71
68
|
// nghttp2_session_consume, called at delivery), so an unread body already
|
|
72
69
|
// counts against the reported number -- a reading that also stands behind
|
|
73
70
|
// the client's no-partial-credit accounting, the same deliberate shape the
|
|
74
|
-
// server documents in lib/server/compat.js.
|
|
71
|
+
// server documents in lib/server/compat.js. A close() before connect is no
|
|
72
|
+
// longer one of them: it parks the code on the stream and the session submits
|
|
73
|
+
// RST_STREAM right after the queued HEADERS, node's finishCloseStream shape.
|
|
75
74
|
//
|
|
76
75
|
// The ported surface below -- pipe()/unpipe() (the Readable.prototype.pipe
|
|
77
76
|
// port, not the legacy base pipe), push(), compose(), wrap(),
|
|
@@ -437,6 +436,10 @@ class Http2ClientStream extends Http2Stream {
|
|
|
437
436
|
// reserved here at request() time, which is the one queued-shape
|
|
438
437
|
// divergence and it is carried on the id, not on pending).
|
|
439
438
|
this._opened = false;
|
|
439
|
+
// A close() on a request that has no id on the wire yet parks its reset here;
|
|
440
|
+
// the session submits it in _openStream once the id is assigned, the way node's
|
|
441
|
+
// finishCloseStream waits for 'ready' (core.js:2113-2122).
|
|
442
|
+
this._pendingCloseCode = undefined;
|
|
440
443
|
// Whether this stream is already in the session's _debt list, i.e. whether the
|
|
441
444
|
// next credit pass has to visit it. Declared here so every stream shares one
|
|
442
445
|
// V8 object shape; only lib/client/Http2ClientSession.js reads or writes it.
|
|
@@ -695,6 +698,34 @@ class Http2ClientStream extends Http2Stream {
|
|
|
695
698
|
return this;
|
|
696
699
|
}
|
|
697
700
|
|
|
701
|
+
// node's nghttp2 discards a body still queued when closeStream leaves the
|
|
702
|
+
// writable unfinished: measured on the wire for close(0) on a 1 MB body behind
|
|
703
|
+
// a 16384-octet window (captured frame by frame), no DATA or
|
|
704
|
+
// END_STREAM follows the close and the deferred reset never reaches the wire,
|
|
705
|
+
// because the 'finish' it waits for never arrives. Settle the parked callbacks
|
|
706
|
+
// here so the application is not left waiting for bytes that will not go out.
|
|
707
|
+
_dropQueuedBody() {
|
|
708
|
+
this._pending = null;
|
|
709
|
+
this._pendingOff = 0;
|
|
710
|
+
this._wq = null;
|
|
711
|
+
this._endSent = true;
|
|
712
|
+
const cb = this._writeCb;
|
|
713
|
+
this._writeCb = null;
|
|
714
|
+
if (cb !== null) {
|
|
715
|
+
cb();
|
|
716
|
+
}
|
|
717
|
+
const final = this._finalCb;
|
|
718
|
+
this._finalCb = null;
|
|
719
|
+
if (final !== null) {
|
|
720
|
+
final();
|
|
721
|
+
}
|
|
722
|
+
const end = this._endCb;
|
|
723
|
+
this._endCb = undefined;
|
|
724
|
+
if (end !== undefined) {
|
|
725
|
+
end();
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
698
729
|
_driveW() {
|
|
699
730
|
if (this._pending !== null) {
|
|
700
731
|
return;
|
|
@@ -1268,9 +1299,31 @@ class Http2ClientStream extends Http2Stream {
|
|
|
1268
1299
|
}
|
|
1269
1300
|
const ending = this._endQueued === true;
|
|
1270
1301
|
this._closeStream(code);
|
|
1302
|
+
// node's submitRstStream (core.js:512) checks kHandle: a destroy() from the
|
|
1303
|
+
// 'aborted' event closeStream just emitted removes the handle before
|
|
1304
|
+
// finishCloseStream runs, so no RST_STREAM goes out. _destroyFlag is h2lean's
|
|
1305
|
+
// handle-gone reading (the destroy already skipped a reset of its own here).
|
|
1306
|
+
if (this._destroyFlag) {
|
|
1307
|
+
return;
|
|
1308
|
+
}
|
|
1309
|
+
// node's finishCloseStream (core.js:2113) pushes the EOF first and queues the
|
|
1310
|
+
// reset on 'ready' while the handle is unassigned: HEADERS still go out at
|
|
1311
|
+
// connect, followed by RST_STREAM, and the application sees 'end' before the
|
|
1312
|
+
// error. Keep the stream in the session's connect queue instead of destroying
|
|
1313
|
+
// it here -- a pending stream has no id to reset yet.
|
|
1314
|
+
if (!this._opened) {
|
|
1315
|
+
this._pendingCloseCode = code;
|
|
1316
|
+
this.push(null);
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1271
1319
|
if (ending && this.writableFinished !== true && code === NO_ERROR) {
|
|
1320
|
+
this._dropQueuedBody();
|
|
1272
1321
|
this.once('finish', () => {
|
|
1273
|
-
this
|
|
1322
|
+
// A destroy() on this turn (or before this tick) is node's handle gone:
|
|
1323
|
+
// submitRstStream's guard then finds nothing to reset.
|
|
1324
|
+
if (!this._destroyFlag) {
|
|
1325
|
+
this.session.rstStream(this.id, code);
|
|
1326
|
+
}
|
|
1274
1327
|
this._tick(() => this.destroy());
|
|
1275
1328
|
});
|
|
1276
1329
|
return;
|
|
@@ -1555,7 +1608,7 @@ class Http2ClientStream extends Http2Stream {
|
|
|
1555
1608
|
if (this._destroyFlag) {
|
|
1556
1609
|
throw errorOf('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed');
|
|
1557
1610
|
}
|
|
1558
|
-
return
|
|
1611
|
+
// node's prototype method has no return statement; measured v26.12.1.
|
|
1559
1612
|
}
|
|
1560
1613
|
|
|
1561
1614
|
get destroyed() {
|
package/lib/client/client.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
const net = require('node:net');
|
|
18
18
|
const tls = require('node:tls');
|
|
19
|
+
const { promisify } = require('node:util');
|
|
19
20
|
const { Http2ClientSession } = require('./Http2ClientSession');
|
|
20
21
|
const { strictSingleValueFields, received, outOfRangeValue, validateRemoteCustomSettings } = require('../server/Http2ServerSession');
|
|
21
22
|
|
|
@@ -101,7 +102,7 @@ function connect(authority, options, listener) {
|
|
|
101
102
|
if (typeof options.createConnection === 'function') {
|
|
102
103
|
socket = options.createConnection(authority, options);
|
|
103
104
|
} else if (protocol === 'http:') {
|
|
104
|
-
socket = net.connect({ port
|
|
105
|
+
socket = net.connect({ port, host, ...options });
|
|
105
106
|
} else if (protocol === 'https:') {
|
|
106
107
|
// core.js initializeTLSOptions: the ALPN list belongs to the protocol, not
|
|
107
108
|
// to the application -- only an ALPNCallback keeps ours out. servername is
|
|
@@ -114,7 +115,7 @@ function connect(authority, options, listener) {
|
|
|
114
115
|
if (!tlsOptions.servername && !net.isIP(host)) {
|
|
115
116
|
tlsOptions.servername = host;
|
|
116
117
|
}
|
|
117
|
-
socket = tls.connect({ port
|
|
118
|
+
socket = tls.connect({ port, host, ...tlsOptions });
|
|
118
119
|
} else {
|
|
119
120
|
throw notImplemented(`connect over ${protocol}`);
|
|
120
121
|
}
|
|
@@ -129,4 +130,25 @@ function connect(authority, options, listener) {
|
|
|
129
130
|
return session;
|
|
130
131
|
}
|
|
131
132
|
|
|
133
|
+
// node's connect carries util.promisify.custom (internal/http2/core.js): the
|
|
134
|
+
// promisified form resolves with the session on 'connect' and rejects with the
|
|
135
|
+
// very same error object on its first 'error'. Without it util.promisify installs
|
|
136
|
+
// the callback as a third-argument 'connect' listener and resolves undefined.
|
|
137
|
+
Object.defineProperty(connect, promisify.custom, {
|
|
138
|
+
value: function connectAsync(authority, options) {
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
const session = connect(authority, options, () => {
|
|
141
|
+
session.removeListener('error', reject);
|
|
142
|
+
resolve(session);
|
|
143
|
+
});
|
|
144
|
+
session.once('error', reject);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
// node names the custom function `connect` (assignFunctionName in core.js), and
|
|
149
|
+
// that name is visible through util.promisify(connect).name.
|
|
150
|
+
Object.defineProperty(connect[promisify.custom], 'name', {
|
|
151
|
+
value: 'connect', configurable: true
|
|
152
|
+
});
|
|
153
|
+
|
|
132
154
|
module.exports = { connect };
|
|
@@ -57,7 +57,7 @@ const {
|
|
|
57
57
|
DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY,
|
|
58
58
|
WINDOW_UPDATE, CONTINUATION, ALTSVC, ORIGIN,
|
|
59
59
|
F_END_STREAM, F_ACK, F_END_HEADERS, F_PADDED, F_PRIORITY,
|
|
60
|
-
NO_ERROR, PROTOCOL_ERROR, INTERNAL_ERROR, FLOW_CONTROL_ERROR, STREAM_CLOSED,
|
|
60
|
+
NO_ERROR, CANCEL, PROTOCOL_ERROR, INTERNAL_ERROR, FLOW_CONTROL_ERROR, STREAM_CLOSED,
|
|
61
61
|
FRAME_SIZE_ERROR, REFUSED_STREAM, COMPRESSION_ERROR, ENHANCE_YOUR_CALM,
|
|
62
62
|
PREFACE_LEN, HEADER_LEN, readHeader, writeHeader, trimPadding
|
|
63
63
|
} = f;
|
|
@@ -1302,10 +1302,13 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1302
1302
|
this._addCustomEntries(built.rest);
|
|
1303
1303
|
this.remoteSettings = { ...PEER_DEFAULTS };
|
|
1304
1304
|
this.state = {
|
|
1305
|
-
|
|
1305
|
+
// SETTINGS_INITIAL_WINDOW_SIZE is per-stream: node's connection receive
|
|
1306
|
+
// window stays at 65535 (measured at 'session' on the running binary with
|
|
1307
|
+
// { initialWindowSize: 16384 }), and only new streams take the setting.
|
|
1308
|
+
effectiveLocalWindowSize: 65535,
|
|
1306
1309
|
effectiveRecvDataLength: 0,
|
|
1307
1310
|
nextStreamID: 2,
|
|
1308
|
-
localWindowSize:
|
|
1311
|
+
localWindowSize: 65535,
|
|
1309
1312
|
lastProcStreamID: 0,
|
|
1310
1313
|
remoteWindowSize: 65535,
|
|
1311
1314
|
outboundQueueSize: 0,
|
|
@@ -1568,7 +1571,16 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1568
1571
|
this._onWrittenCb = () => this._onWritten();
|
|
1569
1572
|
|
|
1570
1573
|
socket.on('error', (err) => this._onSocketError(err));
|
|
1571
|
-
socket.on('end', () =>
|
|
1574
|
+
socket.on('end', () => {
|
|
1575
|
+
// node has no socket 'end' teardown: its socketOnClose (core.js:3653)
|
|
1576
|
+
// sweeps live streams with NGHTTP2_CANCEL before closeSession destroys
|
|
1577
|
+
// the session. Here 'end' is the first half of that same close, so the
|
|
1578
|
+
// sweep has to precede the NO_ERROR destroy the next line performs.
|
|
1579
|
+
for (const stream of [...this._streams.values()]) {
|
|
1580
|
+
stream.close(CANCEL);
|
|
1581
|
+
}
|
|
1582
|
+
this.destroy(NO_ERROR);
|
|
1583
|
+
});
|
|
1572
1584
|
// 'finish' means everything we wrote reached the OS, so the GOAWAY of a close() is never
|
|
1573
1585
|
// cut off by a destroy -- but it is not the end of the session either. node keeps reading
|
|
1574
1586
|
// until the PEER closes, which is what lets it receive and report the peer's answer to that
|
|
@@ -3513,6 +3525,14 @@ class Http2ServerSession extends EventEmitter {
|
|
|
3513
3525
|
const trailers = this._share
|
|
3514
3526
|
? this._sharedObject(fields, fcount, neverNames(fields, fcount, blockFlags))
|
|
3515
3527
|
: this._toObject(fields, fcount, neverNames(fields, fcount, blockFlags));
|
|
3528
|
+
// node's inbound trailers callback delivers (trailers, flags, rawTrailers)
|
|
3529
|
+
// to compat. The compat dispatch takes one argument, so the flat raw
|
|
3530
|
+
// name/value list travels on a private event first; the merged map stays
|
|
3531
|
+
// the single argument of the public 'trailers' event. The slice owns the
|
|
3532
|
+
// pairs (the decoder reuses its backing array on the next block).
|
|
3533
|
+
if (existing._compatDispatch !== null) {
|
|
3534
|
+
existing.emit(Symbol.for('h2lean.compat.rawTrailers'), fields.slice(0, fcount));
|
|
3535
|
+
}
|
|
3516
3536
|
existing.emit('trailers', trailers);
|
|
3517
3537
|
if (end) {
|
|
3518
3538
|
existing._remoteEnded();
|
|
@@ -4584,6 +4604,14 @@ class Http2ServerSession extends EventEmitter {
|
|
|
4584
4604
|
// node cleanupSession also nulls the socket proxy so session.socket reads gone after close
|
|
4585
4605
|
this._proxySocket = undefined;
|
|
4586
4606
|
this._open = false;
|
|
4607
|
+
// node's socketOnClose (internal/http2/core.js:3653) sweeps every live and
|
|
4608
|
+
// pending stream with NGHTTP2_CANCEL *before* closeSession destroys the
|
|
4609
|
+
// session with NO_ERROR. closeStream records the code at entry and the later
|
|
4610
|
+
// destroy sees an already-closed stream, so rstCode stays 8. Do the same here;
|
|
4611
|
+
// the socket is already gone, and _destroy() skips sendRst once _open is false.
|
|
4612
|
+
for (const stream of [...this._streams.values()]) {
|
|
4613
|
+
stream.close(CANCEL);
|
|
4614
|
+
}
|
|
4587
4615
|
if (!this.destroyed) {
|
|
4588
4616
|
this.destroy(NO_ERROR);
|
|
4589
4617
|
}
|
|
@@ -4642,8 +4670,27 @@ class Http2ServerSession extends EventEmitter {
|
|
|
4642
4670
|
return this;
|
|
4643
4671
|
}
|
|
4644
4672
|
|
|
4673
|
+
// Http2Session.setNextStreamID (core.js:1441-1452) moves the handle's next id,
|
|
4674
|
+
// which is also the id nghttp2 reserves for the next PUSH_PROMISE. h2lean's
|
|
4675
|
+
// reservation counter is _nextPushSid, so both have to move together; the
|
|
4676
|
+
// state field is the read-only view of the same handle value. node validates
|
|
4677
|
+
// and returns undefined on every session, server half included.
|
|
4645
4678
|
setNextStreamID(id) {
|
|
4646
|
-
this.
|
|
4679
|
+
if (this.destroyed) {
|
|
4680
|
+
throw invalidSession();
|
|
4681
|
+
}
|
|
4682
|
+
if (typeof id !== 'number') {
|
|
4683
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
4684
|
+
`The "id" argument must be of type number. Received ${received(id)}`);
|
|
4685
|
+
}
|
|
4686
|
+
if (id <= 0 || id > 0xffffffff) {
|
|
4687
|
+
const err = new RangeError('The value of "id" is out of range. '
|
|
4688
|
+
+ `It must be > 0 and <= 4294967295. Received ${outOfRangeValue(id)}`);
|
|
4689
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
4690
|
+
throw err;
|
|
4691
|
+
}
|
|
4692
|
+
this.state.nextStreamID = id;
|
|
4693
|
+
this._nextPushSid = id;
|
|
4647
4694
|
}
|
|
4648
4695
|
|
|
4649
4696
|
// Advertise new settings to the peer. node's session.settings() returns nothing -- measured
|
|
@@ -949,13 +949,13 @@ class Http2ServerStream extends Http2Stream {
|
|
|
949
949
|
|
|
950
950
|
priority() {
|
|
951
951
|
// RFC 9218 deprecates the priority scheme; node still accepts the call but
|
|
952
|
-
// refuses a destroyed stream (internal/http2/core.js:2617)
|
|
953
|
-
//
|
|
954
|
-
// is attached below the class, byte-identical to node's
|
|
952
|
+
// refuses a destroyed stream (internal/http2/core.js:2617) and returns
|
|
953
|
+
// undefined -- the shared prototype method has no return statement. The
|
|
954
|
+
// DEP0194 warning is attached below the class, byte-identical to node's
|
|
955
|
+
// (typo included).
|
|
955
956
|
if (this._destroyed) {
|
|
956
957
|
throw errorOf('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed');
|
|
957
958
|
}
|
|
958
|
-
return true;
|
|
959
959
|
}
|
|
960
960
|
|
|
961
961
|
get pushAllowed() {
|
package/lib/server/compat.js
CHANGED
|
@@ -22,24 +22,17 @@
|
|
|
22
22
|
// reports 'finish' on both stacks.
|
|
23
23
|
// * the order of the teardown events differs: node reports the request's
|
|
24
24
|
// events (its 'end', its 'aborted', its 'close') before the response's
|
|
25
|
-
// ('finish', then 'close'), h2lean
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
//
|
|
25
|
+
// ('finish', then 'close'), h2lean can report the response's 'finish'
|
|
26
|
+
// first, at end() rather than at the stream close. Every event fires on
|
|
27
|
+
// both stacks -- measured against a live node server over the four shapes
|
|
28
|
+
// of bench/micro/probe-teardown-order.js, the sets are equal -- and only
|
|
29
|
+
// their order within the teardown turn differs. One pair is not a fixed
|
|
30
|
+
// difference at all: the request's 'end' against the 'aborted' depends on
|
|
31
|
+
// whether the peer's END_STREAM and its reset arrive in one read or in
|
|
32
|
+
// two, and the two stacks order that pair differently.
|
|
32
33
|
// * the body is pulled, not pushed: nothing is read off the wire until the
|
|
33
34
|
// application consumes it, so an unread upload applies backpressure to the
|
|
34
35
|
// sender (node does the same) instead of buffering here.
|
|
35
|
-
// * a stream 'error' reaches the compat request: onRequestError destroys req
|
|
36
|
-
// with it, so an application that listens only on res gets an unhandled
|
|
37
|
-
// 'error'. node's onStreamError (internal/http2/compat.js) is purposefully
|
|
38
|
-
// blank -- "errors in compatibility mode are not forwarded to the request
|
|
39
|
-
// and response objects". Measured with a content-length promise broken by a
|
|
40
|
-
// short body (content-length 10, two octets sent): node reports st-error
|
|
41
|
-
// then st-close:1 and the client its own error/close:1, h2lean reports
|
|
42
|
-
// st-close:1, st-error, then an uncaught ERR_HTTP2_STREAM_ERROR out of req.
|
|
43
36
|
// * with the server option shareHeaders:true, req.headers is frozen and shared
|
|
44
37
|
// between requests whose header block is identical, so req.url = ... and
|
|
45
38
|
// req.method = ... throw instead of rewriting the request.
|
|
@@ -56,6 +49,33 @@ const { Readable, Stream } = require('node:stream');
|
|
|
56
49
|
|
|
57
50
|
const { nullProto, trimName, isFieldName } = require('../headers');
|
|
58
51
|
const { kCompatSocket } = require('../Http2Stream');
|
|
52
|
+
const { NO_ERROR } = require('../frames');
|
|
53
|
+
const { Http2ServerStream } = require('./Http2ServerStream');
|
|
54
|
+
|
|
55
|
+
// A peer reset and a teardown the application starts reach the stream's
|
|
56
|
+
// _destroy with different arguments (sendRst true for the latter; session
|
|
57
|
+
// destroy sets session.destroyed before it destroys its streams). node's
|
|
58
|
+
// request sees 'close' before the queued EOF only for the local teardown;
|
|
59
|
+
// a peer reset that is not an error has its EOF delivered first. Mark the
|
|
60
|
+
// local case at the point the two shapes are still distinct. One wrapper at
|
|
61
|
+
// module load, one boolean write on a stream that is being destroyed -- the
|
|
62
|
+
// per-request dispatch path stays untouched.
|
|
63
|
+
const originalStreamDestroy = Http2ServerStream.prototype._destroy;
|
|
64
|
+
Http2ServerStream.prototype._destroy = function compatTrackedDestroy(code, sendRst, error) {
|
|
65
|
+
const req = this._request;
|
|
66
|
+
if (req !== undefined && (sendRst ||
|
|
67
|
+
(this.session !== undefined && this.session.destroyed))) {
|
|
68
|
+
req._localTeardown = true;
|
|
69
|
+
}
|
|
70
|
+
return originalStreamDestroy.call(this, code, sendRst, error);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// The inbound trailers callback on node carries (trailers, flags, rawTrailers).
|
|
74
|
+
// The session hands the flat raw field list over on this private event first,
|
|
75
|
+
// because a stream's compat dispatch forwards one argument; a symbol keeps the
|
|
76
|
+
// event off the application-visible names. Symbol.for keeps the two modules in
|
|
77
|
+
// step without an extra require on the session's hot path.
|
|
78
|
+
const kCompatRawTrailers = Symbol.for('h2lean.compat.rawTrailers');
|
|
59
79
|
|
|
60
80
|
function err(code, msg) {
|
|
61
81
|
const e = new Error(msg);
|
|
@@ -287,12 +307,11 @@ function onRequestEnd() {
|
|
|
287
307
|
req.push(null);
|
|
288
308
|
}
|
|
289
309
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
310
|
+
// node's onStreamError (internal/http2/compat.js:160, v26.12.1) is purposefully
|
|
311
|
+
// blank: errors in compatibility mode are not forwarded to the request or the
|
|
312
|
+
// response. The subscription itself stays -- a stream the application never
|
|
313
|
+
// listens to must not throw on an unhandled 'error'.
|
|
314
|
+
function onRequestError() {}
|
|
296
315
|
|
|
297
316
|
function onRequestAborted() {
|
|
298
317
|
const req = this._request;
|
|
@@ -303,12 +322,29 @@ function onRequestAborted() {
|
|
|
303
322
|
req.emit('aborted');
|
|
304
323
|
}
|
|
305
324
|
|
|
325
|
+
function onRequestRawTrailers(raw) {
|
|
326
|
+
const req = this._request;
|
|
327
|
+
if (req !== undefined) {
|
|
328
|
+
this._rawTrailerFields = raw;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
306
332
|
function onRequestTrailers(trailers) {
|
|
307
333
|
const req = this._request;
|
|
308
334
|
if (req === undefined) {
|
|
309
335
|
return;
|
|
310
336
|
}
|
|
311
|
-
|
|
337
|
+
// node's onStreamTrailers (internal/http2/compat.js:145) merges into the
|
|
338
|
+
// request's own plain object and spreads the flat raw pair list, in wire
|
|
339
|
+
// order, into its own rawTrailers array.
|
|
340
|
+
Object.assign(req._trailers, trailers);
|
|
341
|
+
const raw = this._rawTrailerFields;
|
|
342
|
+
if (raw !== undefined) {
|
|
343
|
+
for (let i = 0; i < raw.length; i += 2) {
|
|
344
|
+
req._rawTrailers.push(raw[i], raw[i + 1]);
|
|
345
|
+
}
|
|
346
|
+
this._rawTrailerFields = undefined;
|
|
347
|
+
}
|
|
312
348
|
req.emit('trailers', trailers);
|
|
313
349
|
}
|
|
314
350
|
|
|
@@ -331,20 +367,28 @@ function onRequestClose() {
|
|
|
331
367
|
if (!req._didRead) {
|
|
332
368
|
req.resume();
|
|
333
369
|
}
|
|
334
|
-
// '
|
|
335
|
-
//
|
|
336
|
-
//
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
370
|
+
// node's onStreamCloseRequest (internal/http2/compat.js:282) emits 'close'
|
|
371
|
+
// in the same turn as the stream close, before the queued EOF reaches the
|
|
372
|
+
// reader, for a local teardown or a reset carrying a code. A peer's NO_ERROR
|
|
373
|
+
// reset still delivers the owed EOF first (measured on 24 and 26), so that
|
|
374
|
+
// shape keeps the deferred close and reports readableEnded true; using the
|
|
375
|
+
// stream's own reset code rather than whether EOF happened to be emitted
|
|
376
|
+
// keeps the rule the same when the two teardown paths share a turn.
|
|
377
|
+
if (req._localTeardown || this.rstCode !== NO_ERROR) {
|
|
378
|
+
req.emit('close');
|
|
379
|
+
} else {
|
|
380
|
+
req._tick(emitCloseNT, req);
|
|
381
|
+
}
|
|
342
382
|
}
|
|
343
383
|
|
|
344
384
|
function emitFinishNT(target) {
|
|
345
385
|
target.emit('finish');
|
|
346
386
|
}
|
|
347
387
|
|
|
388
|
+
function emitCloseNT(target) {
|
|
389
|
+
target.emit('close');
|
|
390
|
+
}
|
|
391
|
+
|
|
348
392
|
function onRequestTimeout() {
|
|
349
393
|
const req = this._request;
|
|
350
394
|
if (req !== undefined && !req._closed) {
|
|
@@ -369,6 +413,9 @@ function compatDispatch(name, arg) {
|
|
|
369
413
|
// closing it here would report the 'close' without the 'finish' and
|
|
370
414
|
// before the request's own 'close'.
|
|
371
415
|
break;
|
|
416
|
+
case kCompatRawTrailers:
|
|
417
|
+
onRequestRawTrailers.call(this, arg);
|
|
418
|
+
break;
|
|
372
419
|
case 'trailers':
|
|
373
420
|
onRequestTrailers.call(this, arg);
|
|
374
421
|
break;
|
|
@@ -410,7 +457,10 @@ class Http2ServerRequest extends Readable {
|
|
|
410
457
|
this._method = stream._reqMethod;
|
|
411
458
|
this._path = stream._reqPath;
|
|
412
459
|
this._rawHeaders = undefined;
|
|
413
|
-
|
|
460
|
+
// node's request constructor uses a plain object for trailers (compat.js:323),
|
|
461
|
+
// unlike the null-prototype header maps; the incoming case variants are
|
|
462
|
+
// merged into it, not made the request's map.
|
|
463
|
+
this._trailers = {};
|
|
414
464
|
this._rawTrailers = [];
|
|
415
465
|
this._closed = false;
|
|
416
466
|
this._ended = false;
|
|
@@ -504,11 +554,20 @@ class Http2ServerRequest extends Readable {
|
|
|
504
554
|
return this._headers !== undefined ? this._headers[':method'] : this._method;
|
|
505
555
|
}
|
|
506
556
|
set method(method) {
|
|
557
|
+
// node's setter runs validateString(method, 'method') and then
|
|
558
|
+
// ERR_INVALID_ARG_VALUE('method', method) (internal/http2/compat.js:411).
|
|
559
|
+
// Both are TypeError instances; the message text is measured off the
|
|
560
|
+
// running binary, not paraphrased.
|
|
507
561
|
if (typeof method !== 'string') {
|
|
508
|
-
|
|
562
|
+
const e = new TypeError(
|
|
563
|
+
`The "method" argument must be of type string. Received ${describeType(method)}`);
|
|
564
|
+
e.code = 'ERR_INVALID_ARG_TYPE';
|
|
565
|
+
throw e;
|
|
509
566
|
}
|
|
510
567
|
if (method.trim() === '') {
|
|
511
|
-
|
|
568
|
+
const e = new TypeError(`The argument 'method' is invalid. Received ${inspect(method)}`);
|
|
569
|
+
e.code = 'ERR_INVALID_ARG_VALUE';
|
|
570
|
+
throw e;
|
|
512
571
|
}
|
|
513
572
|
assertWritable(this._materialize());
|
|
514
573
|
this._headers[':method'] = method;
|
|
@@ -520,8 +579,9 @@ class Http2ServerRequest extends Readable {
|
|
|
520
579
|
if (auth !== undefined) {
|
|
521
580
|
return auth;
|
|
522
581
|
}
|
|
523
|
-
|
|
524
|
-
|
|
582
|
+
// node's getAuthority (internal/http2/util.js:974) returns headers.host
|
|
583
|
+
// unchanged when :authority is absent; a port is part of that value.
|
|
584
|
+
return h.host;
|
|
525
585
|
}
|
|
526
586
|
|
|
527
587
|
get scheme() {
|
|
@@ -537,8 +597,27 @@ class Http2ServerRequest extends Readable {
|
|
|
537
597
|
}
|
|
538
598
|
|
|
539
599
|
setTimeout(msecs, callback) {
|
|
540
|
-
if (!this._closed) {
|
|
541
|
-
|
|
600
|
+
if (!this._closed && !this._stream.destroyed) {
|
|
601
|
+
// node's setStreamTimeout (internal/stream_base_commons.js:246) returns
|
|
602
|
+
// the stream untouched once it is destroyed; a setTimeout after finish
|
|
603
|
+
// therefore arms nothing. The rest of the method mirrors its timer:
|
|
604
|
+
// stream timer and registers the callback as a once('timeout') listener;
|
|
605
|
+
// the compat request and response subscriptions (compat.js:303, :307)
|
|
606
|
+
// re-emit 'timeout' on both objects before that callback runs. h2lean's
|
|
607
|
+
// idle timer lives on the session, so re-emit here. One closure per
|
|
608
|
+
// setTimeout call, only on the opt-in path; the timer path itself is
|
|
609
|
+
// unchanged.
|
|
610
|
+
const req = this;
|
|
611
|
+
const res = this._stream._response;
|
|
612
|
+
this._stream.setTimeout(msecs, function onCompatTimeout() {
|
|
613
|
+
req.emit('timeout');
|
|
614
|
+
if (res !== undefined) {
|
|
615
|
+
res.emit('timeout');
|
|
616
|
+
}
|
|
617
|
+
if (callback !== undefined) {
|
|
618
|
+
callback.apply(this, arguments);
|
|
619
|
+
}
|
|
620
|
+
});
|
|
542
621
|
}
|
|
543
622
|
return this;
|
|
544
623
|
}
|
|
@@ -594,6 +673,14 @@ function onResponseClose() {
|
|
|
594
673
|
if (res === undefined || res._state.closed) {
|
|
595
674
|
return;
|
|
596
675
|
}
|
|
676
|
+
// node's onStreamCloseResponse (internal/http2/compat.js:446) opens with
|
|
677
|
+
// `if (this.headRequest !== state.headRequest) return`, and state.headRequest
|
|
678
|
+
// is only set by end(). A HEAD answer closed before end() therefore reports
|
|
679
|
+
// neither 'finish' nor 'close' on node; end() calls this handler itself once
|
|
680
|
+
// the flag is flipped.
|
|
681
|
+
if (this.headRequest !== res._state.headRequest) {
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
597
684
|
res._state.closed = true;
|
|
598
685
|
res.writable = false;
|
|
599
686
|
// node emits 'finish' and 'close' together, when the stream closes
|
|
@@ -628,7 +715,10 @@ class Http2ServerResponse extends Stream {
|
|
|
628
715
|
finishEmitted: false,
|
|
629
716
|
sendDate: true,
|
|
630
717
|
statusCode: 200,
|
|
631
|
-
headRequest
|
|
718
|
+
// node's response state starts headRequest false and copies the stream's
|
|
719
|
+
// value in end() (compat.js:849); the stream itself closes HEAD answers
|
|
720
|
+
// at respond() time (Http2ServerStream.js:342-348).
|
|
721
|
+
headRequest: false
|
|
632
722
|
};
|
|
633
723
|
// A fast null-prototype map, not a { __proto__: null } literal: the
|
|
634
724
|
// response's headers are written once and read once per response, and the
|
|
@@ -868,9 +958,10 @@ class Http2ServerResponse extends Stream {
|
|
|
868
958
|
// Http2ServerResponse has no sentHeaders, so `res.sentHeaders` is undefined
|
|
869
959
|
// on both stacks.
|
|
870
960
|
stream.respond(this._headers, {
|
|
871
|
-
//
|
|
872
|
-
//
|
|
873
|
-
|
|
961
|
+
// node's compat [kBeginSend] passes only its ending flag here
|
|
962
|
+
// (internal/http2/compat.js:903): the stream closes a HEAD answer on
|
|
963
|
+
// its own, so no extra END_STREAM option is needed.
|
|
964
|
+
endStream: state.ending,
|
|
874
965
|
sendDate: state.sendDate,
|
|
875
966
|
waitForTrailers: true
|
|
876
967
|
});
|
|
@@ -970,6 +1061,7 @@ class Http2ServerResponse extends Stream {
|
|
|
970
1061
|
cb = encoding; encoding = 'utf8';
|
|
971
1062
|
}
|
|
972
1063
|
const state = this._state;
|
|
1064
|
+
const stream = this._stream;
|
|
973
1065
|
let failure;
|
|
974
1066
|
if (state.ending) {
|
|
975
1067
|
failure = err('ERR_STREAM_WRITE_AFTER_END', 'write after end');
|
|
@@ -985,16 +1077,16 @@ class Http2ServerResponse extends Stream {
|
|
|
985
1077
|
this.destroy(failure);
|
|
986
1078
|
return false;
|
|
987
1079
|
}
|
|
988
|
-
if (
|
|
1080
|
+
if (stream.headRequest) {
|
|
989
1081
|
if (typeof cb === 'function') {
|
|
990
1082
|
this._tick(cb);
|
|
991
1083
|
}
|
|
992
1084
|
return true; // a HEAD response has no body
|
|
993
1085
|
}
|
|
994
|
-
if (!
|
|
1086
|
+
if (!stream.headersSent) {
|
|
995
1087
|
this.writeHead(state.statusCode);
|
|
996
1088
|
}
|
|
997
|
-
return
|
|
1089
|
+
return stream.write(chunk, encoding, cb);
|
|
998
1090
|
}
|
|
999
1091
|
|
|
1000
1092
|
end(chunk, encoding, cb) {
|
|
@@ -1006,15 +1098,19 @@ class Http2ServerResponse extends Stream {
|
|
|
1006
1098
|
cb = encoding; encoding = 'utf8';
|
|
1007
1099
|
}
|
|
1008
1100
|
|
|
1009
|
-
|
|
1101
|
+
// node's end() guard (internal/http2/compat.js:838) also compares the two
|
|
1102
|
+
// headRequest flags: a HEAD whose stream closed before end() must still run
|
|
1103
|
+
// this call so the deferred finish/close can be reported.
|
|
1104
|
+
if ((state.closed || state.ending) && state.headRequest === stream.headRequest) {
|
|
1010
1105
|
if (typeof cb === 'function') {
|
|
1011
1106
|
this._tick(cb);
|
|
1012
1107
|
}
|
|
1013
1108
|
return this;
|
|
1014
1109
|
}
|
|
1015
|
-
if (chunk !== null && chunk !== undefined && !
|
|
1110
|
+
if (chunk !== null && chunk !== undefined && !stream.headRequest) {
|
|
1016
1111
|
this.write(chunk, encoding);
|
|
1017
1112
|
}
|
|
1113
|
+
state.headRequest = stream.headRequest;
|
|
1018
1114
|
state.ending = true;
|
|
1019
1115
|
|
|
1020
1116
|
if (!stream.headersSent) {
|
|
@@ -1056,6 +1152,13 @@ class Http2ServerResponse extends Stream {
|
|
|
1056
1152
|
// staging the frame twice. A body parked on window credit, a refused block and a
|
|
1057
1153
|
// HEAD-request close keep the deferred path -- the staged frame would land before
|
|
1058
1154
|
// the queued DATA, and the refused shape records the end without wire work.
|
|
1155
|
+
if (state.closed || stream.destroyed || stream.closed) {
|
|
1156
|
+
// node's end() calls onStreamCloseResponse itself when the stream already
|
|
1157
|
+
// closed (compat.js:862); the stream's close ran while the HEAD guard
|
|
1158
|
+
// still failed, so nothing reported finish/close until this call.
|
|
1159
|
+
onResponseClose.call(stream);
|
|
1160
|
+
return this;
|
|
1161
|
+
}
|
|
1059
1162
|
if (!recorded && stream._waitForTrailers && stream._queued === null &&
|
|
1060
1163
|
!stream._blockRefused) {
|
|
1061
1164
|
stream.endWithStagedEnd();
|
|
@@ -1096,7 +1199,7 @@ class Http2ServerResponse extends Stream {
|
|
|
1096
1199
|
}
|
|
1097
1200
|
|
|
1098
1201
|
setTimeout(msecs, callback) {
|
|
1099
|
-
if (this._state.closed) {
|
|
1202
|
+
if (this._state.closed || this._stream.destroyed) {
|
|
1100
1203
|
return this;
|
|
1101
1204
|
}
|
|
1102
1205
|
this._stream.setTimeout(msecs, callback);
|
package/package.json
CHANGED