h2lean 0.11.3 → 0.17.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/README.md +3 -7
- package/index.js +79 -19
- package/lib/client-session.js +331 -212
- package/lib/client-stream.js +2188 -147
- package/lib/client.js +9 -1
- package/lib/compat.js +90 -1
- package/lib/frames.js +25 -1
- package/lib/headers.js +4 -3
- package/lib/hpack.js +120 -24
- package/lib/pieces.js +3 -1
- package/lib/server.js +58 -18
- package/lib/session.js +981 -119
- package/lib/stream.js +160 -25
- package/package.json +1 -1
package/lib/client-stream.js
CHANGED
|
@@ -1,23 +1,102 @@
|
|
|
1
|
-
// ClientHttp2Stream: the
|
|
1
|
+
// ClientHttp2Stream: the stream node hands back from `session.request()`.
|
|
2
2
|
//
|
|
3
|
-
// This mirrors `
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
3
|
+
// This mirrors `ClientHttp2Stream` in node's bundled `internal/http2/core.js`.
|
|
4
|
+
// It is a hand-written stream over `stream.Stream` -- the server's option-A
|
|
5
|
+
// shape (lib/stream.js) -- not a Duplex: OPTIMIZATION.md item 41 measured the
|
|
6
|
+
// Duplex machinery end-to-end on the server (+1.19 us/req at 0 B, x1.54) and
|
|
7
|
+
// item 42 measured the same construction on the client with a full
|
|
8
|
+
// hand-written replacement that passed the whole client suite (the 0 B arm
|
|
9
|
+
// x0.868 of the Duplex, the 16 KiB/256 KiB/upload arms x0.94-0.98 -- a real
|
|
10
|
+
// but bounded win that does not buy out the surface the contract needs, which
|
|
11
|
+
// is why the surface is ported below rather than dropped). Everything the
|
|
12
|
+
// session drives keeps the name and semantics node's session expects:
|
|
13
|
+
// * it extends `stream.Stream` with `allowHalfOpen` observable true, so
|
|
14
|
+
// ending the request body (writable) does not close the response
|
|
15
|
+
// (readable), and the common `req.pipe(...)` / `for await` idioms work;
|
|
11
16
|
// * the protocol work (DATA framing, flow control, HPACK) belongs to the
|
|
12
|
-
// session, not here. This file only moves bytes between the
|
|
13
|
-
//
|
|
14
|
-
// ('response', 'trailers', 'close', 'error', informational 'headers').
|
|
17
|
+
// session, not here. This file only moves bytes between the two hand-built
|
|
18
|
+
// halves and the session, and emits the events the application subscribes
|
|
19
|
+
// to ('response', 'trailers', 'close', 'error', informational 'headers').
|
|
20
|
+
//
|
|
21
|
+
// Deliberate divergences from node's Duplex, each carried on purpose:
|
|
22
|
+
// * `read()` follows node's howMuchToRead/fromList (readable.js): no argument
|
|
23
|
+
// drains the whole queue as one merged chunk, a finite ask gathers across
|
|
24
|
+
// entries and splits the overshooting remainder by slicing (a view of the
|
|
25
|
+
// buffer the session read off the socket, never a copy). What is NOT
|
|
26
|
+
// reproduced is the watermark prefetch machinery -- no maybeReadMore runs
|
|
27
|
+
// ahead of the ask, the queue only grows from the wire. The decoder merge
|
|
28
|
+
// for a split multibyte char is reproduced (decode after the gather,
|
|
29
|
+
// fromList's order), and the decoder tail at a mid-char EOF is queued for
|
|
30
|
+
// pull-mode consumers the way node's onEofChunk pushes it -- with the
|
|
31
|
+
// 'readable' wake deferred one tick (readable.js emitReadable), because
|
|
32
|
+
// this is the one path that queues from inside read(), before the
|
|
33
|
+
// consumer's waiter has registered.
|
|
34
|
+
// * Writable backpressure is the queue length, not node's highWaterMark
|
|
35
|
+
// arithmetic: write() returns false while a chunk is parked or queued and
|
|
36
|
+
// 'drain' fires once when the queue empties. `writableLength`,
|
|
37
|
+
// `writableBuffer` and `bufferSize` report the octets still queued in the
|
|
38
|
+
// writable queue; the one chunk in flight lives in the session's `_pending`
|
|
39
|
+
// slot (write-through), which is where node's nghttp2 queue would hold it.
|
|
40
|
+
// * `cork()`/`uncork()` are counting stubs like the server's; the cross-tick
|
|
41
|
+
// deferral node's Writable.cork does is a known divergence (pinned
|
|
42
|
+
// server-side by test/cork-parity.test.js).
|
|
43
|
+
// * `once('data')` does not flip flowing -- the same gap the server's on()
|
|
44
|
+
// override carries; `on('data')` does.
|
|
45
|
+
// * destroy() defers the teardown one tick by hand; 'error' then 'close' fire
|
|
46
|
+
// from that tick instead of the machinery's destroy chain. The forced EOF on
|
|
47
|
+
// a flowing consumer is delivered in that same tick, before 'close', the
|
|
48
|
+
// order the machinery produces (push(null) inside _destroy, endReadable
|
|
49
|
+
// ahead of the destroy chain).
|
|
50
|
+
// * setEncoding decodes at the delivery sites instead of at push() time; the
|
|
51
|
+
// observable contract -- every 'data' and every read() past the call yields
|
|
52
|
+
// decoded strings, a split multibyte char never emits '' and its tail comes
|
|
53
|
+
// out of the EOF as U+FFFD -- is the same shape (measured against node).
|
|
54
|
+
// * `endAfterHeaders` stays false on requests: node's client sets the flag
|
|
55
|
+
// only on an inbound stream whose HEADERS carried END_STREAM (core.js
|
|
56
|
+
// onSessionHeaders), and a PUSH_PROMISE cannot carry that flag (RFC 9113
|
|
57
|
+
// 6.6), so it never reads true here. h2lean's own "HEADERS already carried
|
|
58
|
+
// END_STREAM" knowledge lives in `_endOnHeaders`, which the request pump
|
|
59
|
+
// reads to suppress an empty trailing DATA.
|
|
60
|
+
// * `state`, `headRequest` and `close(code, callback)` are node's Http2Stream
|
|
61
|
+
// surface, provided here too. Three divergences, all on shapes node can
|
|
62
|
+
// observe and h2lean cannot produce: node spends one loop turn reporting
|
|
63
|
+
// state:1/weight:0/localWindowSize:0 between request() and the session's
|
|
64
|
+
// first flush -- the HEADERS are framed inside the call here, so the getter
|
|
65
|
+
// reports the post-HEADERS state immediately; a close() on a request queued
|
|
66
|
+
// behind a still-connecting session destroys it without anything going out,
|
|
67
|
+
// where node sends the HEADERS once the session is ready and resets the
|
|
68
|
+
// stream right after; and `state.localWindowSize` shrinks when DATA ARRIVES
|
|
69
|
+
// rather than when the application consumes it (node consumes inside
|
|
70
|
+
// nghttp2_session_consume, called at delivery), so an unread body already
|
|
71
|
+
// counts against the reported number -- a reading that also stands behind
|
|
72
|
+
// the client's no-partial-credit accounting, the same deliberate shape the
|
|
73
|
+
// server documents in lib/compat.js.
|
|
74
|
+
//
|
|
75
|
+
// The ported surface below -- pipe()/unpipe() (the Readable.prototype.pipe
|
|
76
|
+
// port, not the legacy base pipe), push(), compose(), wrap(),
|
|
77
|
+
// [Symbol.asyncIterator] with the fourteen helpers, setTimeout() and
|
|
78
|
+
// priority() -- is member-by-member from lib/stream.js, where the same shapes
|
|
79
|
+
// were measured against live node servers across majors
|
|
80
|
+
// (test/duplex-surface.test.js, test/pipe-parity.test.js).
|
|
15
81
|
'use strict';
|
|
16
82
|
|
|
17
|
-
const {
|
|
18
|
-
|
|
83
|
+
const {
|
|
84
|
+
Stream, Writable, Readable, Duplex, finished, addAbortSignal,
|
|
85
|
+
destroy: destroyStream
|
|
86
|
+
} = require('node:stream');
|
|
87
|
+
const { StringDecoder } = require('node:string_decoder');
|
|
88
|
+
const { inspect, deprecate } = require('node:util');
|
|
89
|
+
const { NO_ERROR, CANCEL, INTERNAL_ERROR, ST_OPEN, ST_HALF_CLOSED_LOCAL } = require('./frames');
|
|
19
90
|
const { requestView } = require('./headers');
|
|
20
|
-
const { streamErrorOf } = require('./stream');
|
|
91
|
+
const { errorOf, streamErrorOf, validateRstCode } = require('./stream');
|
|
92
|
+
|
|
93
|
+
// node's Http2Stream sets no highWaterMark on either side, so the values an
|
|
94
|
+
// application reads are the machinery defaults -- 16384 on node 20, 65536 from
|
|
95
|
+
// 22 on, measured on live streams of all four majors and equal to
|
|
96
|
+
// `new Readable()/.writable().<side>HighWaterMark` on each (lib/stream.js
|
|
97
|
+
// carries the same constants for the server).
|
|
98
|
+
const WRITABLE_HWM = new Writable().writableHighWaterMark;
|
|
99
|
+
const READABLE_HWM = new Readable().readableHighWaterMark;
|
|
21
100
|
|
|
22
101
|
// node's request() destroys an aborted stream with an instance of the internal
|
|
23
102
|
// AbortError class (core.js:1920 `stream.destroy(new AbortError(undefined, { cause:
|
|
@@ -38,18 +117,639 @@ class AbortError extends Error {
|
|
|
38
117
|
}
|
|
39
118
|
}
|
|
40
119
|
|
|
120
|
+
// The parked end() callback when the application passed none: the session's pump
|
|
121
|
+
// branches on `_finalCb !== null` and _writableDone calls it, so the slot needs a
|
|
122
|
+
// callable even when there is nothing to answer. One shared closure, never per-stream.
|
|
123
|
+
const EMPTY_CB = () => {};
|
|
124
|
+
|
|
125
|
+
// --- the async-iteration machinery ---------------------------------------------
|
|
126
|
+
// Ported from lib/stream.js, which reproduces node's
|
|
127
|
+
// internal/streams/readable.js:1552-1805 (streamToAsyncIterator/createAsyncIterator)
|
|
128
|
+
// and internal/streams/operators, read from the binary in the same session as the
|
|
129
|
+
// measurements cited at the methods. The one adaptation is in the iterator's
|
|
130
|
+
// finalize(): the server stream's internal destroy entry is called directly with
|
|
131
|
+
// the derived pair, while this stream's destroy(err) is the measured client
|
|
132
|
+
// teardown route -- the AbortError instance reaches _destroyBody and derives
|
|
133
|
+
// RST_STREAM(CANCEL) there (client-destroy-code.test.js pins the transcript).
|
|
134
|
+
|
|
135
|
+
const kEmpty = Symbol('kEmpty');
|
|
136
|
+
const kEof = Symbol('kEof');
|
|
137
|
+
// %AsyncIteratorPrototype% in V8: the function's .prototype is an INSTANCE of
|
|
138
|
+
// the (shared) async-generator prototype, so the intrinsic with
|
|
139
|
+
// [Symbol.asyncIterator]/[Symbol.asyncDispose] and no own methods sits TWO
|
|
140
|
+
// levels up. Verified identity with the proto of a live node v26 iterator
|
|
141
|
+
// object; the single getPrototypeOf lands on the generator prototype instead.
|
|
142
|
+
// Node's own iterator is NOT this shape on every major: 22 and 24 hand back a
|
|
143
|
+
// real async-generator instance, whose proto has no own keys at all. The
|
|
144
|
+
// intrinsic is the only anchor stable across majors, and the behaviour that
|
|
145
|
+
// depends on it ([Symbol.asyncIterator] resolving through the chain,
|
|
146
|
+
// self-identical) is verified per major; exact proto identity is unportable.
|
|
147
|
+
const ASYNC_ITERATOR_PROTO =
|
|
148
|
+
Object.getPrototypeOf(Object.getPrototypeOf(async function* () {}.prototype));
|
|
149
|
+
|
|
150
|
+
function noop() {}
|
|
151
|
+
|
|
152
|
+
function validateObjectArg(value, name) {
|
|
153
|
+
if (value === null || typeof value !== 'object') {
|
|
154
|
+
throw errorOf('ERR_INVALID_ARG_TYPE',
|
|
155
|
+
`The "${name}" argument must be of type object. Received ${inspect(value)}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function validateFunctionArg(value, name) {
|
|
160
|
+
if (typeof value !== 'function') {
|
|
161
|
+
throw errorOf('ERR_INVALID_ARG_TYPE',
|
|
162
|
+
`The "${name}" argument must be of type function. Received ${inspect(value)}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// node's pair in every operator: validateObject(options) then
|
|
167
|
+
// validateAbortSignal(options.signal).
|
|
168
|
+
function validateSignalArg(options) {
|
|
169
|
+
if (options != null && options.signal != null && !(options.signal instanceof AbortSignal)) {
|
|
170
|
+
throw errorOf('ERR_INVALID_ARG_TYPE',
|
|
171
|
+
'The "options.signal" argument must be an instance of type AbortSignal.');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function validateOperatorOptions(options) {
|
|
176
|
+
if (options != null) {
|
|
177
|
+
validateObjectArg(options, 'options');
|
|
178
|
+
}
|
|
179
|
+
validateSignalArg(options);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// node's toIntegerOrInfinity (operators:298): Number coercion per the
|
|
183
|
+
// iterator-helpers proposal, then the range refusal.
|
|
184
|
+
function toIntegerOrInfinity(number) {
|
|
185
|
+
number = Number(number);
|
|
186
|
+
if (Number.isNaN(number)) {
|
|
187
|
+
return 0;
|
|
188
|
+
}
|
|
189
|
+
if (number < 0) {
|
|
190
|
+
throw errorOf('ERR_OUT_OF_RANGE',
|
|
191
|
+
`The value of "number" is out of range. It must be >= 0. Received ${number}`);
|
|
192
|
+
}
|
|
193
|
+
return number;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// node's createAsyncIterator (readable.js:1576). The request protocol -- next
|
|
197
|
+
// fast path, queue behind an inFlight request, return/throw draining through
|
|
198
|
+
// the same pump -- is reproduced member by member; the wake is the 'readable'
|
|
199
|
+
// event (node's emitReadable, same coalescing) and the end verdict comes from
|
|
200
|
+
// the error/end/close trio standing in for eos(stream, { writable: false }).
|
|
201
|
+
function createStreamIterator(stream, options) {
|
|
202
|
+
let callback = noop;
|
|
203
|
+
let error; // undefined: active, null: ended cleanly, else: the Error
|
|
204
|
+
let started = false;
|
|
205
|
+
let completed = false;
|
|
206
|
+
let inFlight = false; // an asynchronous request is outstanding
|
|
207
|
+
let queue = null; // requests received while inFlight
|
|
208
|
+
let draining = false;
|
|
209
|
+
let detach = noop;
|
|
210
|
+
|
|
211
|
+
function fire() {
|
|
212
|
+
const cb = callback;
|
|
213
|
+
callback = noop;
|
|
214
|
+
cb();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function wait() {
|
|
218
|
+
return new Promise((resolve) => {
|
|
219
|
+
callback = resolve;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function start() {
|
|
224
|
+
started = true;
|
|
225
|
+
stream.on('readable', fire);
|
|
226
|
+
// node's eos(stream, { writable: false }, cb), adapted: the verdict
|
|
227
|
+
// handlers record the FIRST verdict and wake the pump (node's once-wrapped
|
|
228
|
+
// callback suppresses the rest -- an 'end' followed by a destroy(err) is
|
|
229
|
+
// swallowed on node, measured off a live client), and the LISTENERS stay
|
|
230
|
+
// attached until finalize's detach branch -- the part that matters here:
|
|
231
|
+
// node's eos keeps its 'error' handler attached through the teardown, so a
|
|
232
|
+
// destroy that races a natural end does not turn into an unhandled
|
|
233
|
+
// 'error'. A close with neither end nor error is node's eos verdict
|
|
234
|
+
// ERR_STREAM_PREMATURE_CLOSE ('Premature close', read out of
|
|
235
|
+
// internal/errors with --expose-internals).
|
|
236
|
+
const settle = (err) => {
|
|
237
|
+
if (error !== undefined) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
error = err;
|
|
241
|
+
fire();
|
|
242
|
+
};
|
|
243
|
+
const onEnd = () => {
|
|
244
|
+
settle(null);
|
|
245
|
+
};
|
|
246
|
+
const onClose = () => {
|
|
247
|
+
settle(errorOf('ERR_STREAM_PREMATURE_CLOSE', 'Premature close'));
|
|
248
|
+
};
|
|
249
|
+
stream.on('error', settle);
|
|
250
|
+
stream.on('end', onEnd);
|
|
251
|
+
stream.on('close', onClose);
|
|
252
|
+
detach = () => {
|
|
253
|
+
stream.removeListener('readable', fire);
|
|
254
|
+
stream.removeListener('error', settle);
|
|
255
|
+
stream.removeListener('end', onEnd);
|
|
256
|
+
stream.removeListener('close', onClose);
|
|
257
|
+
};
|
|
258
|
+
// node's eos settles immediately when the stream has ALREADY reached its
|
|
259
|
+
// verdict at start() (end-of-stream.js:158-166): on a closed stream the
|
|
260
|
+
// verdict is getEosOnCloseError -- the tracked error FIRST, then
|
|
261
|
+
// ERR_STREAM_PREMATURE_CLOSE for an unfinished readable (the eos was
|
|
262
|
+
// created with writable:false, so the writable side is never premature
|
|
263
|
+
// here), else a clean verdict. A stream that ended without closing yet
|
|
264
|
+
// does not settle early: the 'end' listener only records
|
|
265
|
+
// readableFinished and the verdict waits for 'close' (eos.js onend holds
|
|
266
|
+
// a GET whose writable already ended). Measured on live node v26.12.1: a
|
|
267
|
+
// second iterator over an ended-and-closed GET answers {done:true}, and
|
|
268
|
+
// every() after some() on the same stream rejects the ABORT_ERR the
|
|
269
|
+
// short-circuit's destroyer handed to destroy().
|
|
270
|
+
if (stream._destroyFlag === true || stream._closed === true) {
|
|
271
|
+
if (stream._errored !== undefined) {
|
|
272
|
+
stream._tick(() => settle(stream._errored));
|
|
273
|
+
} else if (stream.readableEnded !== true) {
|
|
274
|
+
stream._tick(() => settle(errorOf('ERR_STREAM_PREMATURE_CLOSE', 'Premature close')));
|
|
275
|
+
} else {
|
|
276
|
+
stream._tick(() => settle(null));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function finalize() {
|
|
282
|
+
completed = true;
|
|
283
|
+
// node's finalize (readable.js:1609) reads autoDestroy off the state
|
|
284
|
+
// object; Http2Stream fixes it false (core.js:2132), so the second
|
|
285
|
+
// clause reduces to `error === undefined`: only a still-active
|
|
286
|
+
// return()/break destroys -- node's destroyer(stream, null) on a
|
|
287
|
+
// not-finished stream is destroy(new AbortError()), which core.js:2516
|
|
288
|
+
// turns into RST CANCEL. The client destroy(err) derives exactly that
|
|
289
|
+
// from an AbortError instance; a natural end, an errored end and
|
|
290
|
+
// destroyOnReturn:false only DETACH. Measured on the server stream:
|
|
291
|
+
// break -- destroyed true, rstCode 8, aborted, error:ABORT_ERR, close;
|
|
292
|
+
// natural end -- destroyed stays false.
|
|
293
|
+
if ((error || options?.destroyOnReturn !== false) && error === undefined) {
|
|
294
|
+
stream.destroy(new AbortError());
|
|
295
|
+
} else {
|
|
296
|
+
stream.removeListener('readable', fire);
|
|
297
|
+
detach();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function drain() {
|
|
302
|
+
// node's guard: requests settled synchronously call back into drain(),
|
|
303
|
+
// and one loop runs instead of one recursion per request.
|
|
304
|
+
if (draining) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
draining = true;
|
|
308
|
+
try {
|
|
309
|
+
while (!inFlight && queue !== null && queue.length > 0) {
|
|
310
|
+
const req = queue.shift();
|
|
311
|
+
if (req.type === 'next') {
|
|
312
|
+
processNext(req.resolve, req.reject);
|
|
313
|
+
} else if (req.type === 'return') {
|
|
314
|
+
processReturn(req.value, req.resolve);
|
|
315
|
+
} else {
|
|
316
|
+
processThrow(req.value, req.reject);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
} finally {
|
|
320
|
+
draining = false;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function pump(resolve, reject) {
|
|
325
|
+
const chunk = stream.destroyed ? null : stream.read();
|
|
326
|
+
if (chunk !== null) {
|
|
327
|
+
inFlight = false;
|
|
328
|
+
resolve({ done: false, value: chunk });
|
|
329
|
+
if (queue !== null) {
|
|
330
|
+
drain();
|
|
331
|
+
}
|
|
332
|
+
} else if (error) {
|
|
333
|
+
inFlight = false;
|
|
334
|
+
finalize();
|
|
335
|
+
reject(error);
|
|
336
|
+
if (queue !== null) {
|
|
337
|
+
drain();
|
|
338
|
+
}
|
|
339
|
+
} else if (error === null) {
|
|
340
|
+
inFlight = false;
|
|
341
|
+
finalize();
|
|
342
|
+
resolve({ done: true, value: undefined });
|
|
343
|
+
if (queue !== null) {
|
|
344
|
+
drain();
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
// nothing buffered yet: wait for the next 'readable' or end verdict
|
|
348
|
+
// and retry, node's wakeup-promise tail.
|
|
349
|
+
wait().then(() => pump(resolve, reject));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function processNext(resolve, reject) {
|
|
354
|
+
if (completed) {
|
|
355
|
+
resolve({ done: true, value: undefined });
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (!started) {
|
|
359
|
+
start();
|
|
360
|
+
}
|
|
361
|
+
inFlight = true;
|
|
362
|
+
pump(resolve, reject);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function processReturn(value, resolve) {
|
|
366
|
+
if (!completed) {
|
|
367
|
+
if (started) {
|
|
368
|
+
finalize();
|
|
369
|
+
} else {
|
|
370
|
+
// never started: complete without touching the stream.
|
|
371
|
+
completed = true;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
resolve({ done: true, value });
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function processThrow(err, reject) {
|
|
378
|
+
if (completed || !started) {
|
|
379
|
+
completed = true;
|
|
380
|
+
reject(err);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
error = err;
|
|
384
|
+
finalize();
|
|
385
|
+
reject(err);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const iter = {
|
|
389
|
+
__proto__: ASYNC_ITERATOR_PROTO,
|
|
390
|
+
next() {
|
|
391
|
+
if (!inFlight && !completed) {
|
|
392
|
+
if (!started) {
|
|
393
|
+
start();
|
|
394
|
+
}
|
|
395
|
+
const chunk = stream.destroyed ? null : stream.read();
|
|
396
|
+
if (chunk !== null) {
|
|
397
|
+
return Promise.resolve({ done: false, value: chunk });
|
|
398
|
+
}
|
|
399
|
+
if (error) {
|
|
400
|
+
finalize();
|
|
401
|
+
return Promise.reject(error);
|
|
402
|
+
}
|
|
403
|
+
if (error === null) {
|
|
404
|
+
finalize();
|
|
405
|
+
return Promise.resolve({ done: true, value: undefined });
|
|
406
|
+
}
|
|
407
|
+
inFlight = true;
|
|
408
|
+
return new Promise((resolve, reject) => {
|
|
409
|
+
wait().then(() => pump(resolve, reject));
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
return new Promise((resolve, reject) => {
|
|
413
|
+
if (inFlight) {
|
|
414
|
+
(queue ??= []).push({ __proto__: null, type: 'next', value: undefined, resolve, reject });
|
|
415
|
+
} else {
|
|
416
|
+
resolve({ done: true, value: undefined });
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
},
|
|
420
|
+
return(value) {
|
|
421
|
+
return new Promise((resolve) => {
|
|
422
|
+
if (inFlight) {
|
|
423
|
+
(queue ??= []).push({ __proto__: null, type: 'return', value, resolve, reject: null });
|
|
424
|
+
} else {
|
|
425
|
+
processReturn(value, resolve);
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
},
|
|
429
|
+
throw(err) {
|
|
430
|
+
return new Promise((resolve, reject) => {
|
|
431
|
+
if (inFlight) {
|
|
432
|
+
(queue ??= []).push({ __proto__: null, type: 'throw', value: err, resolve, reject });
|
|
433
|
+
} else {
|
|
434
|
+
processThrow(err, reject);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
// node's streamToAsyncIterator hangs the source on the iterator object
|
|
440
|
+
// (readable.js:1563-1565); measured raw.stream === rd on a live node.
|
|
441
|
+
iter.stream = stream;
|
|
442
|
+
return iter;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// node's operators (internal/streams/operators). Each is a plain function or
|
|
446
|
+
// async function mirroring the original's argument shape, so the synchronous
|
|
447
|
+
// validation the transform family performs (measured: map('nope') throws
|
|
448
|
+
// ERR_INVALID_ARG_TYPE out of the call) and the rejection the promise family
|
|
449
|
+
// produces land exactly where node puts them. The abort error is the client's
|
|
450
|
+
// own AbortError class -- the one destroy() maps onto RST_STREAM(CANCEL).
|
|
451
|
+
|
|
452
|
+
function opMap(stream, fn, options) {
|
|
453
|
+
validateFunctionArg(fn, 'fn');
|
|
454
|
+
validateOperatorOptions(options);
|
|
455
|
+
let concurrency = 1;
|
|
456
|
+
if (options?.concurrency != null) {
|
|
457
|
+
concurrency = Math.floor(options.concurrency);
|
|
458
|
+
}
|
|
459
|
+
let highWaterMark = concurrency - 1;
|
|
460
|
+
if (options?.highWaterMark != null) {
|
|
461
|
+
highWaterMark = Math.floor(options.highWaterMark);
|
|
462
|
+
}
|
|
463
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
464
|
+
throw errorOf('ERR_OUT_OF_RANGE',
|
|
465
|
+
`The value of "options.concurrency" is out of range. It must be >= 1. Received ${concurrency}`);
|
|
466
|
+
}
|
|
467
|
+
if (!Number.isInteger(highWaterMark) || highWaterMark < 0) {
|
|
468
|
+
throw errorOf('ERR_OUT_OF_RANGE',
|
|
469
|
+
`The value of "options.highWaterMark" is out of range. It must be >= 0. Received ${highWaterMark}`);
|
|
470
|
+
}
|
|
471
|
+
highWaterMark += concurrency;
|
|
472
|
+
// node builds its always-present signal with AbortSignal.any([...]) --
|
|
473
|
+
// any([]) is a fresh never-aborted signal, which a never-aborted
|
|
474
|
+
// controller's signal is byte-equivalent to for everything the operator
|
|
475
|
+
// reads off it.
|
|
476
|
+
const signal = options?.signal ?? new AbortController().signal;
|
|
477
|
+
|
|
478
|
+
return (async function* map() {
|
|
479
|
+
const queue = [];
|
|
480
|
+
const signalOpt = { signal };
|
|
481
|
+
let next = null;
|
|
482
|
+
let resume = null;
|
|
483
|
+
let done = false;
|
|
484
|
+
let cnt = 0;
|
|
485
|
+
|
|
486
|
+
function onCatch() {
|
|
487
|
+
done = true;
|
|
488
|
+
afterItemProcessed();
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function afterItemProcessed() {
|
|
492
|
+
cnt -= 1;
|
|
493
|
+
maybeResume();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function maybeResume() {
|
|
497
|
+
if (resume && !done && cnt < concurrency && queue.length < highWaterMark) {
|
|
498
|
+
const r = resume;
|
|
499
|
+
resume = null;
|
|
500
|
+
r();
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
async function pump() {
|
|
505
|
+
try {
|
|
506
|
+
for await (let val of stream) {
|
|
507
|
+
if (done) {
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (signal.aborted) {
|
|
511
|
+
throw new AbortError();
|
|
512
|
+
}
|
|
513
|
+
try {
|
|
514
|
+
val = fn(val, signalOpt);
|
|
515
|
+
if (val === kEmpty) {
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
val = Promise.resolve(val);
|
|
519
|
+
} catch (err) {
|
|
520
|
+
val = Promise.reject(err);
|
|
521
|
+
}
|
|
522
|
+
cnt += 1;
|
|
523
|
+
val.then(afterItemProcessed, onCatch);
|
|
524
|
+
queue.push(val);
|
|
525
|
+
if (next) {
|
|
526
|
+
const n = next;
|
|
527
|
+
next = null;
|
|
528
|
+
n();
|
|
529
|
+
}
|
|
530
|
+
if (!done && (queue.length >= highWaterMark || cnt >= concurrency)) {
|
|
531
|
+
await new Promise((resolve) => {
|
|
532
|
+
resume = resolve;
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
queue.push(kEof);
|
|
537
|
+
} catch (err) {
|
|
538
|
+
const val = Promise.reject(err);
|
|
539
|
+
val.then(afterItemProcessed, onCatch);
|
|
540
|
+
queue.push(val);
|
|
541
|
+
} finally {
|
|
542
|
+
done = true;
|
|
543
|
+
if (next) {
|
|
544
|
+
const n = next;
|
|
545
|
+
next = null;
|
|
546
|
+
n();
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
pump();
|
|
552
|
+
|
|
553
|
+
try {
|
|
554
|
+
while (true) {
|
|
555
|
+
while (queue.length > 0) {
|
|
556
|
+
const val = await queue[0];
|
|
557
|
+
if (val === kEof) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (signal.aborted) {
|
|
561
|
+
throw new AbortError();
|
|
562
|
+
}
|
|
563
|
+
if (val !== kEmpty) {
|
|
564
|
+
yield val;
|
|
565
|
+
}
|
|
566
|
+
queue.shift();
|
|
567
|
+
maybeResume();
|
|
568
|
+
}
|
|
569
|
+
await new Promise((resolve) => {
|
|
570
|
+
next = resolve;
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
} finally {
|
|
574
|
+
done = true;
|
|
575
|
+
if (resume) {
|
|
576
|
+
const r = resume;
|
|
577
|
+
resume = null;
|
|
578
|
+
r();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
})();
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function opFilter(stream, fn, options) {
|
|
585
|
+
validateFunctionArg(fn, 'fn');
|
|
586
|
+
async function filterFn(value, opt) {
|
|
587
|
+
if (await fn(value, opt)) {
|
|
588
|
+
return value;
|
|
589
|
+
}
|
|
590
|
+
return kEmpty;
|
|
591
|
+
}
|
|
592
|
+
return opMap(stream, filterFn, options);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
async function opForEach(stream, fn, options) {
|
|
596
|
+
validateFunctionArg(fn, 'fn');
|
|
597
|
+
async function forEachFn(value, opt) {
|
|
598
|
+
await fn(value, opt);
|
|
599
|
+
return kEmpty;
|
|
600
|
+
}
|
|
601
|
+
// node drains the mapped generator with an empty for-await body. The same
|
|
602
|
+
// descent is spelled out here -- an fn error propagates out of the mapped
|
|
603
|
+
// generator's own body, so its cleanup runs at the throw point and no
|
|
604
|
+
// abrupt-close return() call from this loop would add anything.
|
|
605
|
+
const it = opMap(stream, forEachFn, options)[Symbol.asyncIterator]();
|
|
606
|
+
let res = await it.next();
|
|
607
|
+
while (!res.done) {
|
|
608
|
+
res = await it.next();
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
function opFlatMap(stream, fn, options) {
|
|
613
|
+
const values = opMap(stream, fn, options);
|
|
614
|
+
return (async function* flatMap() {
|
|
615
|
+
for await (const val of values) {
|
|
616
|
+
yield* val;
|
|
617
|
+
}
|
|
618
|
+
})();
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function opDrop(stream, number, options) {
|
|
622
|
+
validateOperatorOptions(options);
|
|
623
|
+
const limit = toIntegerOrInfinity(number);
|
|
624
|
+
return (async function* drop() {
|
|
625
|
+
if (options?.signal?.aborted) {
|
|
626
|
+
throw new AbortError();
|
|
627
|
+
}
|
|
628
|
+
let numberLeft = limit;
|
|
629
|
+
for await (const val of stream) {
|
|
630
|
+
if (options?.signal?.aborted) {
|
|
631
|
+
throw new AbortError();
|
|
632
|
+
}
|
|
633
|
+
if (numberLeft-- <= 0) {
|
|
634
|
+
yield val;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
})();
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function opTake(stream, number, options) {
|
|
641
|
+
validateOperatorOptions(options);
|
|
642
|
+
const limit = toIntegerOrInfinity(number);
|
|
643
|
+
return (async function* take() {
|
|
644
|
+
if (options?.signal?.aborted) {
|
|
645
|
+
throw new AbortError();
|
|
646
|
+
}
|
|
647
|
+
let numberLeft = limit;
|
|
648
|
+
for await (const val of stream) {
|
|
649
|
+
if (options?.signal?.aborted) {
|
|
650
|
+
throw new AbortError();
|
|
651
|
+
}
|
|
652
|
+
if (numberLeft-- > 0) {
|
|
653
|
+
yield val;
|
|
654
|
+
}
|
|
655
|
+
// Don't get another item from iterator in case we reached the end --
|
|
656
|
+
// node's comment; the return() this early exit triggers on the source
|
|
657
|
+
// iterator is what destroys a stream taken short of its EOF.
|
|
658
|
+
if (numberLeft <= 0) {
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
})();
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
async function opSome(stream, fn, options) {
|
|
666
|
+
for await (const unused of opFilter(stream, fn, options)) {
|
|
667
|
+
return true;
|
|
668
|
+
}
|
|
669
|
+
return false;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
async function opEvery(stream, fn, options) {
|
|
673
|
+
validateFunctionArg(fn, 'fn');
|
|
674
|
+
// https://en.wikipedia.org/wiki/De_Morgan%27s_laws -- node's comment.
|
|
675
|
+
return !(await opSome(stream, async (...args) => !(await fn(...args)), options));
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
async function opFind(stream, fn, options) {
|
|
679
|
+
for await (const result of opFilter(stream, fn, options)) {
|
|
680
|
+
return result;
|
|
681
|
+
}
|
|
682
|
+
return undefined;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
async function opReduce(stream, hasInitial, reducer, initialValue, options) {
|
|
686
|
+
validateFunctionArg(reducer, 'reducer');
|
|
687
|
+
validateOperatorOptions(options);
|
|
688
|
+
if (options?.signal?.aborted) {
|
|
689
|
+
const err = new AbortError(options.signal.reason);
|
|
690
|
+
stream.once('error', () => {}); // the error is already propagated
|
|
691
|
+
// node awaits its internal eos, which RESOLVES on the errored teardown;
|
|
692
|
+
// the callback form wrapped here is that promise, since the public
|
|
693
|
+
// callback-less `finished` refuses instead.
|
|
694
|
+
await new Promise((resolve) => {
|
|
695
|
+
// the stream's destroy() returns nothing, unlike node's, so the stream
|
|
696
|
+
// is handed to eos on its own line.
|
|
697
|
+
stream.destroy(err);
|
|
698
|
+
finished(stream, resolve);
|
|
699
|
+
});
|
|
700
|
+
throw err;
|
|
701
|
+
}
|
|
702
|
+
const ac = new AbortController();
|
|
703
|
+
const signal = ac.signal;
|
|
704
|
+
if (options?.signal) {
|
|
705
|
+
options.signal.addEventListener('abort', () => ac.abort(), { once: true });
|
|
706
|
+
}
|
|
707
|
+
let gotAnyItemFromStream = false;
|
|
708
|
+
try {
|
|
709
|
+
for await (const value of stream) {
|
|
710
|
+
gotAnyItemFromStream = true;
|
|
711
|
+
if (options?.signal?.aborted) {
|
|
712
|
+
throw new AbortError();
|
|
713
|
+
}
|
|
714
|
+
if (!hasInitial) {
|
|
715
|
+
initialValue = value;
|
|
716
|
+
hasInitial = true;
|
|
717
|
+
} else {
|
|
718
|
+
initialValue = await reducer(initialValue, value, { signal });
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
if (!gotAnyItemFromStream && !hasInitial) {
|
|
722
|
+
// node's ReduceAwareErrMissingArgs: same code, same custom message.
|
|
723
|
+
throw errorOf('ERR_MISSING_ARGS', 'Reduce of an empty stream requires an initial value');
|
|
724
|
+
}
|
|
725
|
+
} finally {
|
|
726
|
+
ac.abort();
|
|
727
|
+
}
|
|
728
|
+
return initialValue;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
async function opToArray(stream, options) {
|
|
732
|
+
validateOperatorOptions(options);
|
|
733
|
+
const result = [];
|
|
734
|
+
for await (const val of stream) {
|
|
735
|
+
if (options?.signal?.aborted) {
|
|
736
|
+
throw new AbortError(options.signal.reason);
|
|
737
|
+
}
|
|
738
|
+
result.push(val);
|
|
739
|
+
}
|
|
740
|
+
return result;
|
|
741
|
+
}
|
|
742
|
+
|
|
41
743
|
// `sentHeaders`/`sentTrailers` and the state flags node exposes through symbols
|
|
42
744
|
// are kept as plain fields here; they are not part of the documented client
|
|
43
|
-
// surface an application is expected to touch
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
// (OPTIMIZATION.md item 48).
|
|
50
|
-
class ClientHttp2Stream extends Duplex {
|
|
745
|
+
// surface an application is expected to touch. The inbound response/trailer
|
|
746
|
+
// objects node does not keep on the stream at all (they pass through the events
|
|
747
|
+
// only, core.js emitStreamEvent) are prototype getters over the unbuilt field
|
|
748
|
+
// list: the object comes into being at the first read, which is what keeps a
|
|
749
|
+
// body-only client free of per-response allocations (OPTIMIZATION.md item 48).
|
|
750
|
+
class ClientHttp2Stream extends Stream {
|
|
51
751
|
constructor(session, id, _opts) {
|
|
52
|
-
super(
|
|
752
|
+
super();
|
|
53
753
|
this.id = id;
|
|
54
754
|
this.session = session;
|
|
55
755
|
// The session's one tick route -- node's queue or the batched pump of
|
|
@@ -59,10 +759,17 @@ class ClientHttp2Stream extends Duplex {
|
|
|
59
759
|
// Both start at the peer's advertised initial window (65535 until its SETTINGS
|
|
60
760
|
// land) exactly as the server side does. The value written here is the
|
|
61
761
|
// default one: the session overwrites it with the peer's number when the
|
|
62
|
-
// stream is actually opened (RFC 9113 6.9.2
|
|
63
|
-
//
|
|
762
|
+
// stream is actually opened (RFC 9113 6.9.2), which may be later than this
|
|
763
|
+
// construction.
|
|
64
764
|
this._sendWin = session.remoteSettings.initialWindowSize;
|
|
65
765
|
this._recvUsed = 0;
|
|
766
|
+
// The stream is not on the session's map until the request actually opens --
|
|
767
|
+
// a request queued behind a still-connecting session or a concurrency slot
|
|
768
|
+
// reads {} from the state getter and true from the pending getter below
|
|
769
|
+
// (node's queued shape: pending true, id undefined; the id itself is
|
|
770
|
+
// reserved here at request() time, which is the one queued-shape
|
|
771
|
+
// divergence and it is carried on the id, not on pending).
|
|
772
|
+
this._opened = false;
|
|
66
773
|
// Whether this stream is already in the session's _debt list, i.e. whether the
|
|
67
774
|
// next credit pass has to visit it. Declared here so every stream shares one
|
|
68
775
|
// V8 object shape; only lib/client-session.js reads or writes it.
|
|
@@ -73,35 +780,77 @@ class ClientHttp2Stream extends Duplex {
|
|
|
73
780
|
this._pendingOff = 0;
|
|
74
781
|
this._writeCb = null;
|
|
75
782
|
this._finalCb = null;
|
|
783
|
+
// The hand-written writable half's queue: chunks waiting behind the one parked
|
|
784
|
+
// in `_pending`, plus the end() bookkeeping node keeps in _writableState.
|
|
785
|
+
this._wq = null;
|
|
786
|
+
this._endQueued = false; // node's _writableState.ending; `aborted` reads it
|
|
787
|
+
this._endCb = undefined;
|
|
788
|
+
this._needDrain = false;
|
|
789
|
+
this._finishScheduled = false;
|
|
790
|
+
// The two stubs the session pokes: `_writableState.finished` settled
|
|
791
|
+
// synchronously on the push path (client-session.js, node settles it at the
|
|
792
|
+
// 'stream' event) and `errorEmitted` read by the captureRejection routing.
|
|
793
|
+
// Two small objects instead of the machinery's two large ones -- that share
|
|
794
|
+
// of the remaining cost is what this hand-written construction still saves.
|
|
795
|
+
this._writableState = { finished: false, errorEmitted: false };
|
|
796
|
+
this._readableState = { errorEmitted: false };
|
|
76
797
|
// Whether the request HEADERS frame has been committed to the outbound queue.
|
|
77
798
|
// _pump() returns early until this is true, so a write() that lands before the
|
|
78
799
|
// 'connect' event (or before the queued request is framed) cannot put DATA on
|
|
79
800
|
// the wire ahead of its stream's HEADERS -- nghttp2 reads that as INTERNAL_ERROR.
|
|
80
801
|
this._headersSent = false;
|
|
81
802
|
// Whether the request message is over. `endAfterHeaders` is the GET case:
|
|
82
|
-
// HEADERS already carried END_STREAM, so `
|
|
803
|
+
// HEADERS already carried END_STREAM, so the parked `_finalCb` must not
|
|
804
|
+
// produce an empty DATA.
|
|
83
805
|
this._endSent = false;
|
|
84
806
|
this.endAfterHeaders = false;
|
|
807
|
+
// The request-side record that the request HEADERS carried END_STREAM. The
|
|
808
|
+
// public `endAfterHeaders` above is node's flag, which only inbound streams
|
|
809
|
+
// get (header above); the pump reads THIS one (client-session.js).
|
|
810
|
+
this._endOnHeaders = false;
|
|
85
811
|
// The readable half has seen END_STREAM (server finished the response body).
|
|
86
812
|
this._remoteEnded = false;
|
|
813
|
+
// The hand-written readable half: flowing emits synchronously, paused buffers.
|
|
814
|
+
// undefined until anything touches the readable side -- node's tri-state
|
|
815
|
+
// flowing (readable.js:1901), read back by the readableFlowing getter.
|
|
816
|
+
this._flowing = undefined;
|
|
817
|
+
this._rbuf = null;
|
|
818
|
+
// the deferred 'readable' wake guard (node's kEmittedReadable coalescing)
|
|
819
|
+
this._readableScheduled = false;
|
|
820
|
+
// setEncoding() state: null (octets) or the StringDecoder the chunks run
|
|
821
|
+
// through at delivery. Constructing it is the validation -- an unknown name
|
|
822
|
+
// throws ERR_UNKNOWN_ENCODING with StringDecoder's message, the same error
|
|
823
|
+
// node's readable.setEncoding surfaces (measured on live node).
|
|
824
|
+
this._decoder = null;
|
|
825
|
+
this._encoding = null;
|
|
826
|
+
// EOF known to the readable half, and whether 'end' itself has been
|
|
827
|
+
// delivered. node's endEmitted bit flips at the emission, not at the
|
|
828
|
+
// scheduling, so `readable`/`readableEnded` read the delivered flag while
|
|
829
|
+
// the internal end machinery reads the scheduled one.
|
|
830
|
+
this._rEof = false;
|
|
831
|
+
this._endEmitted = false;
|
|
832
|
+
this._endDelivered = false;
|
|
833
|
+
// node's readableDidRead is the dataEmitted bit (readable.js:1857): true
|
|
834
|
+
// once any delivery of octets happened.
|
|
835
|
+
this._dataRead = false;
|
|
87
836
|
// node's STREAM_FLAGS_ABORTED, exposed through the `aborted` getter
|
|
88
837
|
// (internal/http2/core.js:2289). Set once, by the reset that found the writable
|
|
89
838
|
// half still open; closeStream guards the event on it.
|
|
90
839
|
this.aborted = false;
|
|
91
840
|
// The peer reset this stream. nghttp2 refuses DATA and HEADERS on a closed
|
|
92
|
-
// stream, so _pump must settle the
|
|
841
|
+
// stream, so _pump must settle the writable half without framing.
|
|
93
842
|
this._reset = false;
|
|
94
843
|
// node keeps rstCode in the stream's C-side state, initialised with
|
|
95
|
-
// NGHTTP2_NO_ERROR (internal/http2/core.js:2152
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
// from the
|
|
844
|
+
// NGHTTP2_NO_ERROR (internal/http2/core.js:2152), so a stream that closed
|
|
845
|
+
// without a reset reports 0 -- the upstream max-reserved-streams test reads
|
|
846
|
+
// exactly that off the parent request. closeStream overwrites it with the code
|
|
847
|
+
// the stream closes with, whether that came from the peer, from a refusal or
|
|
848
|
+
// from the destroy derivation below.
|
|
100
849
|
this.rstCode = 0;
|
|
101
850
|
// node's STREAM_FLAGS_CLOSED (core.js:2074, set by closeStream). It is what lets
|
|
102
851
|
// _destroy tell "the wire life already ended with a code" from "this destroy is the
|
|
103
852
|
// first thing to end it": the former keeps rstCode, the latter derives a code from
|
|
104
|
-
// the error and the session (core.js:2512).
|
|
853
|
+
// the error and the session (core.js:2512). The public `closed` getter reads it.
|
|
105
854
|
this._closed = false;
|
|
106
855
|
this.headersSent = false; // node flips this once HEADERS are sent
|
|
107
856
|
// What request() was given, for the sentHeaders getter below. node keeps the caller's
|
|
@@ -139,6 +888,26 @@ class ClientHttp2Stream extends Duplex {
|
|
|
139
888
|
// undefined until a trailer block is sent, the way node's kSentTrailers reads before
|
|
140
889
|
// sendTrailers() (measured on a live node client); the server stream's field is the same.
|
|
141
890
|
this.sentTrailers = undefined;
|
|
891
|
+
// The hand-written halves' own destroy bookkeeping. allowHalfOpen stays
|
|
892
|
+
// observable: ending the request body never closes the response half, and the
|
|
893
|
+
// auto-close (node's autoDestroy) waits for both halves like the machinery's
|
|
894
|
+
// does.
|
|
895
|
+
this.allowHalfOpen = true;
|
|
896
|
+
this._destroyFlag = false;
|
|
897
|
+
this._defaultEnc = 'utf8';
|
|
898
|
+
this._corked = 0;
|
|
899
|
+
// The errored slot the public `errored` getter reads: the Error the
|
|
900
|
+
// teardown carries, set when destroy runs with one (destroy.js checkError),
|
|
901
|
+
// not when the event fires.
|
|
902
|
+
this._errored = undefined;
|
|
903
|
+
// The pipe surface's bookkeeping (node's Readable.prototype.pipe port): the
|
|
904
|
+
// destinations and the await-drain record, both read by pipe/unpipe below.
|
|
905
|
+
this._pipes = null;
|
|
906
|
+
this._awaitDrain = null;
|
|
907
|
+
// Assigned by the session right after construction: the origin the request
|
|
908
|
+
// goes to (421 answers prune it from session.originSet) and node's kSID.
|
|
909
|
+
this._origin = undefined;
|
|
910
|
+
this._sid = 0;
|
|
142
911
|
}
|
|
143
912
|
|
|
144
913
|
// node's client sentHeaders is the request map with the pseudo-headers request() resolved
|
|
@@ -149,8 +918,7 @@ class ClientHttp2Stream extends Duplex {
|
|
|
149
918
|
// first read so a request nobody inspects pays no copy.
|
|
150
919
|
get sentHeaders() {
|
|
151
920
|
// A stream created from PUSH_PROMISE has neither kSentHeaders nor kRawHeaders,
|
|
152
|
-
// so node's getter answers undefined (core.js:2226
|
|
153
|
-
// !this[kRawHeaders]) return this[kSentHeaders]`; measured on a live node client).
|
|
921
|
+
// so node's getter answers undefined (core.js:2226; measured on a live node client).
|
|
154
922
|
// A request() with no headers argument is not that case: node prepares `{}` and
|
|
155
923
|
// resolves the four pseudo-headers out of it (core.js:1871-1878).
|
|
156
924
|
if (this._pushed) {
|
|
@@ -167,8 +935,7 @@ class ClientHttp2Stream extends Duplex {
|
|
|
167
935
|
// are prototype getters over the raw field list: the object comes into being
|
|
168
936
|
// at the first read (an event argument when a listener exists, or the getter),
|
|
169
937
|
// and one build serves every later read. A body-only client pays no
|
|
170
|
-
// per-response object (OPTIMIZATION.md item 48).
|
|
171
|
-
// neither, which is closer to node than the plain fields this replaces were.
|
|
938
|
+
// per-response object (OPTIMIZATION.md item 48).
|
|
172
939
|
get responseHeaders() {
|
|
173
940
|
if (this._responseObject !== undefined) {
|
|
174
941
|
return this._responseObject;
|
|
@@ -199,115 +966,559 @@ class ClientHttp2Stream extends Duplex {
|
|
|
199
966
|
return o;
|
|
200
967
|
}
|
|
201
968
|
|
|
202
|
-
// ---- writable side
|
|
203
|
-
//
|
|
204
|
-
//
|
|
205
|
-
// session
|
|
206
|
-
//
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
969
|
+
// ---- writable side -----------------------------------------------------------
|
|
970
|
+
// node's Writable.write, reduced to the shapes the client surface uses: the
|
|
971
|
+
// chunk is queued, _driveW parks it in the session's `_pending` slot and the
|
|
972
|
+
// session's pump frames it when the windows allow; the callback answers when
|
|
973
|
+
// the octets left (the moment the machinery's _write cb answered before).
|
|
974
|
+
// Backpressure is the queue itself: false while anything is parked or queued,
|
|
975
|
+
// 'drain' once when it empties -- node's highWaterMark arithmetic is not
|
|
976
|
+
// reproduced (documented above).
|
|
977
|
+
write(chunk, encoding, cb) {
|
|
978
|
+
if (typeof encoding === 'function') {
|
|
979
|
+
cb = encoding; encoding = undefined;
|
|
980
|
+
}
|
|
981
|
+
if (this._endQueued || this._endSent || this._destroyFlag) {
|
|
982
|
+
// node: write after end answers ERR_STREAM_WRITE_AFTER_END through the
|
|
983
|
+
// callback and errorOrDestroy -- which destroys a LIVE stream and is a
|
|
984
|
+
// no-op on one already destroyed (internal/streams/writable.js); after
|
|
985
|
+
// destroy the callback carries ERR_STREAM_DESTROYED and nothing is emitted.
|
|
986
|
+
const afterEnd = this._endQueued || this._endSent;
|
|
987
|
+
const err = afterEnd
|
|
988
|
+
? errorOf('ERR_STREAM_WRITE_AFTER_END', 'write after end')
|
|
989
|
+
: errorOf('ERR_STREAM_DESTROYED', 'Cannot call write after a stream was destroyed');
|
|
990
|
+
if (cb !== undefined) {
|
|
991
|
+
this._tick(cb, err);
|
|
992
|
+
}
|
|
993
|
+
if (afterEnd && !this._destroyFlag) {
|
|
994
|
+
this.destroy(err);
|
|
995
|
+
}
|
|
996
|
+
return false;
|
|
997
|
+
}
|
|
998
|
+
const buf = typeof chunk === 'string'
|
|
999
|
+
? Buffer.from(chunk, encoding || this._defaultEnc) : chunk;
|
|
1000
|
+
if (this._wq === null) {
|
|
1001
|
+
this._wq = [];
|
|
1002
|
+
}
|
|
1003
|
+
this._wq.push([buf, cb]);
|
|
1004
|
+
this._driveW();
|
|
1005
|
+
if (this._pending !== null || this._wq.length > 0) {
|
|
1006
|
+
this._needDrain = true;
|
|
1007
|
+
return false;
|
|
1008
|
+
}
|
|
1009
|
+
return true;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
end(chunk, encoding, cb) {
|
|
1013
|
+
if (typeof chunk === 'function') {
|
|
1014
|
+
cb = chunk; chunk = null;
|
|
1015
|
+
} else if (typeof encoding === 'function') {
|
|
1016
|
+
cb = encoding; encoding = null;
|
|
1017
|
+
}
|
|
1018
|
+
if (this._destroyFlag) {
|
|
1019
|
+
// node: end() after destroy() answers the callback with ERR_STREAM_DESTROYED
|
|
1020
|
+
// (internal/streams/writable.js) and changes nothing on the wire.
|
|
1021
|
+
if (cb !== undefined) {
|
|
1022
|
+
this._tick(cb, errorOf('ERR_STREAM_DESTROYED',
|
|
1023
|
+
'Cannot call end after a stream was destroyed'));
|
|
1024
|
+
}
|
|
1025
|
+
return this;
|
|
1026
|
+
}
|
|
1027
|
+
if (this._endQueued) {
|
|
1028
|
+
// node: end() on an ended writable is a no-op that still answers the
|
|
1029
|
+
// callback (internal/streams/writable.js end() -> writableEnded checks).
|
|
1030
|
+
if (cb !== undefined) {
|
|
1031
|
+
this._tick(cb);
|
|
1032
|
+
}
|
|
1033
|
+
return this;
|
|
1034
|
+
}
|
|
1035
|
+
// node's Writable.end flips ending here, before anything is sent -- even when
|
|
1036
|
+
// the body is still parked on window credit. 'aborted' reads it.
|
|
1037
|
+
this._endQueued = true;
|
|
1038
|
+
if (chunk != null) {
|
|
1039
|
+
const buf = typeof chunk === 'string'
|
|
1040
|
+
? Buffer.from(chunk, encoding || this._defaultEnc) : chunk;
|
|
1041
|
+
if (this._wq === null) {
|
|
1042
|
+
this._wq = [];
|
|
1043
|
+
}
|
|
1044
|
+
this._wq.push([buf, undefined]);
|
|
1045
|
+
}
|
|
1046
|
+
this._endCb = cb === undefined ? EMPTY_CB : cb;
|
|
1047
|
+
this._driveW();
|
|
1048
|
+
return this;
|
|
213
1049
|
}
|
|
214
1050
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
1051
|
+
// The pump contract (lib/client-session.js _pump): exactly one chunk parked in
|
|
1052
|
+
// `_pending` with its `_writeCb`, settled by the session when the windows let it
|
|
1053
|
+
// frame; `_finalCb` parked for the END_STREAM behind the body. The hand-written
|
|
1054
|
+
// half keeps that shape and adds the serialization the Writable machinery used to
|
|
1055
|
+
// do: one chunk in flight, the rest of the queue waiting, end() parking `_finalCb`
|
|
1056
|
+
// behind them. When the pump settles a chunk it calls the wrapper, which answers
|
|
1057
|
+
// the application's callback and drives the next queue entry -- re-entrant with
|
|
1058
|
+
// the pump's own loop, which re-checks `_pending` after the call and so never
|
|
1059
|
+
// frames a chunk twice.
|
|
1060
|
+
_driveW() {
|
|
1061
|
+
if (this._pending !== null) {
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
if (this._wq !== null && this._wq.length > 0) {
|
|
1065
|
+
const [buf, cb] = this._wq.shift();
|
|
1066
|
+
this._pending = buf;
|
|
1067
|
+
this._pendingOff = 0;
|
|
1068
|
+
this._writeCb = () => {
|
|
1069
|
+
if (cb !== undefined) {
|
|
1070
|
+
cb();
|
|
1071
|
+
}
|
|
1072
|
+
this._driveW();
|
|
1073
|
+
if ((this._wq === null || this._wq.length === 0) && this._pending === null &&
|
|
1074
|
+
this._needDrain) {
|
|
1075
|
+
this._needDrain = false;
|
|
1076
|
+
this._tick(() => {
|
|
1077
|
+
if (!this._destroyFlag) {
|
|
1078
|
+
this.emit('drain');
|
|
1079
|
+
}
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
};
|
|
1083
|
+
this.session._pump(this);
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
if (this._endQueued && this._finalCb === null && !this._endSent) {
|
|
1087
|
+
this._finalCb = this._endCb;
|
|
1088
|
+
this._endCb = undefined;
|
|
1089
|
+
this.session._pump(this);
|
|
1090
|
+
}
|
|
218
1091
|
}
|
|
219
1092
|
|
|
220
1093
|
// Called by the session once the writable half has been fully framed and
|
|
221
|
-
// END_STREAM put on the wire.
|
|
1094
|
+
// END_STREAM put on the wire. 'finish' fires one tick later, in node's order:
|
|
1095
|
+
// writableFinished true, then the event (the machinery's prefinish/finish pair
|
|
1096
|
+
// collapsed into one tick -- a documented simplification).
|
|
222
1097
|
_writableDone() {
|
|
223
1098
|
this._endSent = true;
|
|
224
1099
|
const cb = this._finalCb;
|
|
225
1100
|
if (cb !== null) {
|
|
226
1101
|
this._finalCb = null; cb();
|
|
227
1102
|
}
|
|
1103
|
+
// node fires 'finish' only for an end() accepted on a live writable. The
|
|
1104
|
+
// teardown routes end a destroyed stream: an application destroy whose
|
|
1105
|
+
// closeStream runs inside the destroy chain reports 'close' and no 'finish'
|
|
1106
|
+
// (client-destroy-code.test.js 'an open request body'), while an inbound
|
|
1107
|
+
// reset closes the writable before destroy() is called and 'finish' fires
|
|
1108
|
+
// for every reset code (client-rst.test.js). The two routes still differ by
|
|
1109
|
+
// the flag at this point -- the framing is synchronous inside _closeStream
|
|
1110
|
+
// -- so the decision is made here, not in the tick.
|
|
1111
|
+
if (!this._finishScheduled && !this._destroyFlag) {
|
|
1112
|
+
this._finishScheduled = true;
|
|
1113
|
+
this._tick(() => {
|
|
1114
|
+
this._writableState.finished = true;
|
|
1115
|
+
this.emit('finish');
|
|
1116
|
+
this._maybeAutoClose();
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
228
1119
|
}
|
|
229
1120
|
|
|
230
|
-
//
|
|
231
|
-
//
|
|
232
|
-
//
|
|
233
|
-
//
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
1121
|
+
// Writable-side corking, as a counting stub -- the server's shape (lib/stream.js
|
|
1122
|
+
// cork()): node's Writable.cork buffers write()/end() and defers the DATA to the
|
|
1123
|
+
// wire until corked reaches 0; the wire here sends straight through. Only the count
|
|
1124
|
+
// is observable.
|
|
1125
|
+
cork() {
|
|
1126
|
+
this._corked++;
|
|
1127
|
+
}
|
|
237
1128
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
//
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
1129
|
+
uncork() {
|
|
1130
|
+
if (this._corked > 0) {
|
|
1131
|
+
this._corked--;
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
// node's Writable.setDefaultEncoding: the WRITABLE half's default -- what an
|
|
1136
|
+
// encoding-less write(string) encodes with. The lowercase fold and the
|
|
1137
|
+
// ERR_UNKNOWN_ENCODING refusal are node's (lib/stream.js, measured).
|
|
1138
|
+
setDefaultEncoding(encoding) {
|
|
1139
|
+
if (typeof encoding === 'string') {
|
|
1140
|
+
encoding = encoding.toLowerCase();
|
|
1141
|
+
}
|
|
1142
|
+
if (!Buffer.isEncoding(encoding)) {
|
|
1143
|
+
const err = new TypeError(`Unknown encoding: ${encoding}`);
|
|
1144
|
+
err.code = 'ERR_UNKNOWN_ENCODING';
|
|
1145
|
+
throw err;
|
|
1146
|
+
}
|
|
1147
|
+
this._defaultEnc = encoding;
|
|
1148
|
+
return this;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
// node's readable.setEncoding: the decoder runs at delivery, so chunks buffered
|
|
1152
|
+
// before the call decode like chunks after it (node decodes at push() time and
|
|
1153
|
+
// re-decodes on encoding change; the observable contract -- every 'data' and
|
|
1154
|
+
// every read() past the call yields decoded strings -- is the same shape here,
|
|
1155
|
+
// decoded at the delivery site instead). The delivery sites are the flowing
|
|
1156
|
+
// emit in _onData, read(), _flushBuffered, push() and the decoder.end() tail in
|
|
1157
|
+
// _scheduleEnd; a decoder holding a partial multibyte char returns '' and
|
|
1158
|
+
// nothing is delivered for it (measured: node pushes one 'data' for a char
|
|
1159
|
+
// split across two chunks, never an empty string).
|
|
1160
|
+
setEncoding(encoding) {
|
|
1161
|
+
this._decoder = new StringDecoder(encoding);
|
|
1162
|
+
this._encoding = encoding;
|
|
1163
|
+
return this;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
// ---- readable half -----------------------------------------------------------
|
|
1167
|
+
// Flowing mode is the synchronous emit('data') the server's _recvData uses -- the
|
|
1168
|
+
// fast path item 42 gated, with no readableState and no nextTick flow loop. Paused
|
|
1169
|
+
// mode buffers and serves read(); 'end' is scheduled one tick after the queue
|
|
1170
|
+
// drains at a known EOF, the one nextTick the delivery pays (node's endReadable).
|
|
1171
|
+
// The 'end'-waits-for-the-drain rule and the node-kick (an unconsumed response
|
|
1172
|
+
// still reaches 'end') both carry over from _endRead.
|
|
1173
|
+
_onData(buf) {
|
|
1174
|
+
this._remoteEnded = false;
|
|
1175
|
+
// The session can hand over an empty DATA payload (_onDataFrame delivers
|
|
1176
|
+
// buf.subarray(p, p + l) even when l === 0); node's push() is a no-op for an
|
|
1177
|
+
// empty buffer, so no 'data' reaches the application -- the hand-written half
|
|
1178
|
+
// has to drop it explicitly.
|
|
1179
|
+
if (this._destroyFlag || buf.length === 0) {
|
|
1180
|
+
return;
|
|
1181
|
+
}
|
|
1182
|
+
if (this._flowing === true) {
|
|
1183
|
+
const d = this._decoder;
|
|
1184
|
+
if (d !== null) {
|
|
1185
|
+
const s = d.write(buf);
|
|
1186
|
+
if (s.length !== 0) {
|
|
1187
|
+
this._dataRead = true;
|
|
1188
|
+
this.emit('data', s);
|
|
1189
|
+
}
|
|
1190
|
+
return;
|
|
259
1191
|
}
|
|
260
|
-
|
|
261
|
-
this.
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
this.
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
1192
|
+
this._dataRead = true;
|
|
1193
|
+
this.emit('data', buf);
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
if (this._rbuf === null) {
|
|
1197
|
+
this._rbuf = [];
|
|
1198
|
+
}
|
|
1199
|
+
this._rbuf.push(buf);
|
|
1200
|
+
this.emit('readable');
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
read(n) {
|
|
1204
|
+
if (n === undefined) {
|
|
1205
|
+
n = NaN;
|
|
1206
|
+
} else if (!Number.isInteger(n)) {
|
|
1207
|
+
n = Number.parseInt(n, 10);
|
|
1208
|
+
}
|
|
1209
|
+
if (this._destroyFlag) {
|
|
1210
|
+
return null;
|
|
1211
|
+
}
|
|
1212
|
+
const rbuf = this._rbuf;
|
|
1213
|
+
let ret = null;
|
|
1214
|
+
if (rbuf !== null && rbuf.length > 0 && (Number.isNaN(n) || n > 0)) {
|
|
1215
|
+
const first = rbuf.shift();
|
|
1216
|
+
if (typeof first === 'string') {
|
|
1217
|
+
// the decoder tail _scheduleEnd queued: already decoded, never merged
|
|
1218
|
+
ret = first;
|
|
1219
|
+
} else if (Number.isNaN(n)) {
|
|
1220
|
+
// node's no-argument read() drains the whole queue as one chunk:
|
|
1221
|
+
// howMuchToRead(undefined) answers state.length and fromList concats
|
|
1222
|
+
if (rbuf.length === 0) {
|
|
1223
|
+
ret = first;
|
|
1224
|
+
} else {
|
|
1225
|
+
const parts = [first];
|
|
1226
|
+
while (rbuf.length > 0) {
|
|
1227
|
+
parts.push(rbuf.shift());
|
|
1228
|
+
}
|
|
1229
|
+
ret = parts.length === 1 ? first : Buffer.concat(parts);
|
|
1230
|
+
}
|
|
1231
|
+
} else if (first.length > n) {
|
|
1232
|
+
// the whole-first-chunk slice: a view of the buffer the session read
|
|
1233
|
+
// off the socket, never a copy (AGENTS.md 5: an issued buffer stays
|
|
1234
|
+
// valid under the application).
|
|
1235
|
+
rbuf.unshift(first.subarray(n));
|
|
1236
|
+
ret = first.subarray(0, n);
|
|
1237
|
+
} else if (first.length === n || rbuf.length === 0 ||
|
|
1238
|
+
typeof rbuf[0] === 'string') {
|
|
1239
|
+
ret = first;
|
|
1240
|
+
} else {
|
|
1241
|
+
// node's fromList gathers n octets across queue entries
|
|
1242
|
+
const parts = [first];
|
|
1243
|
+
let got = first.length;
|
|
1244
|
+
while (got < n && rbuf.length > 0 && typeof rbuf[0] !== 'string') {
|
|
1245
|
+
const next = rbuf.shift();
|
|
1246
|
+
if (got + next.length <= n) {
|
|
1247
|
+
parts.push(next);
|
|
1248
|
+
got += next.length;
|
|
1249
|
+
} else {
|
|
1250
|
+
parts.push(next.subarray(0, n - got));
|
|
1251
|
+
rbuf.unshift(next.subarray(n - got));
|
|
1252
|
+
got = n;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
ret = parts.length === 1 ? first : Buffer.concat(parts);
|
|
273
1256
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
this
|
|
1257
|
+
const d = this._decoder;
|
|
1258
|
+
if (d !== null && typeof ret !== 'string') {
|
|
1259
|
+
// the decoder tail queued by _scheduleEnd arrives as a string -- the
|
|
1260
|
+
// only string in the queue -- and must not be decoded again
|
|
1261
|
+
ret = d.write(ret);
|
|
1262
|
+
// a partial multibyte char at the queue head: node's fromList keeps
|
|
1263
|
+
// pulling until it has a string, so this read() cannot answer '' --
|
|
1264
|
+
// the queue drains through the decoder (the decode happens after the
|
|
1265
|
+
// gather, the way fromList decodes the concatenated buffer).
|
|
1266
|
+
while (ret.length === 0 && rbuf.length > 0) {
|
|
1267
|
+
ret = d.write(rbuf.shift());
|
|
1268
|
+
}
|
|
1269
|
+
if (ret.length === 0) {
|
|
1270
|
+
ret = null;
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
if (ret !== null) {
|
|
1274
|
+
this._dataRead = true;
|
|
1275
|
+
this.emit('data', ret);
|
|
281
1276
|
}
|
|
282
1277
|
}
|
|
283
|
-
if (
|
|
284
|
-
|
|
1278
|
+
if ((this._rbuf === null || this._rbuf.length === 0) && this._rEof === true) {
|
|
1279
|
+
// the tail of node's read(): the queue drained at a known EOF -- end-of-stream
|
|
1280
|
+
this._scheduleEnd();
|
|
285
1281
|
}
|
|
1282
|
+
return ret;
|
|
286
1283
|
}
|
|
287
1284
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
1285
|
+
_scheduleEnd(force) {
|
|
1286
|
+
if (this._endEmitted || (this._destroyFlag && force !== true)) {
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
// The decoder tail node's onEofChunk delivers before 'end': the octets a
|
|
1290
|
+
// partial multibyte char held come out as the replacement character
|
|
1291
|
+
// (measured: StringDecoder.end() at a mid-char EOF answers '\uFFFD').
|
|
1292
|
+
// node pushes the tail into the queue -- so a pull-mode consumer reads it
|
|
1293
|
+
// as a chunk and 'end' waits behind it -- while a flowing consumer gets it
|
|
1294
|
+
// as one more 'data'.
|
|
1295
|
+
if (this._decoder !== null) {
|
|
1296
|
+
const tail = this._decoder.end();
|
|
1297
|
+
if (tail.length !== 0) {
|
|
1298
|
+
if (this._flowing === true) {
|
|
1299
|
+
this._dataRead = true;
|
|
1300
|
+
this.emit('data', tail);
|
|
1301
|
+
} else {
|
|
1302
|
+
if (this._rbuf === null) {
|
|
1303
|
+
this._rbuf = [];
|
|
1304
|
+
}
|
|
1305
|
+
// already decoded: read() must not run it through the decoder again
|
|
1306
|
+
this._rbuf.push(tail);
|
|
1307
|
+
// node's onEofChunk push lands the tail in the queue and the
|
|
1308
|
+
// 'readable' wake fires on the NEXT tick (readable.js emitReadable,
|
|
1309
|
+
// kEmittedReadable coalescing). The wake must not be synchronous
|
|
1310
|
+
// here: this path queues the tail from inside read(), before the
|
|
1311
|
+
// consumer's own waiter (the iterator's promise executor) has
|
|
1312
|
+
// registered, so a synchronous emit would be lost.
|
|
1313
|
+
if (this._readableScheduled !== true) {
|
|
1314
|
+
this._readableScheduled = true;
|
|
1315
|
+
this._tick(() => {
|
|
1316
|
+
this._readableScheduled = false;
|
|
1317
|
+
if (this._destroyFlag || this._flowing === true ||
|
|
1318
|
+
this.listenerCount('readable') === 0) {
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1321
|
+
if (this._rbuf === null || this._rbuf.length === 0) {
|
|
1322
|
+
return;
|
|
1323
|
+
}
|
|
1324
|
+
this.emit('readable');
|
|
1325
|
+
});
|
|
1326
|
+
}
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
this._endEmitted = true;
|
|
1332
|
+
this._tick(() => {
|
|
1333
|
+
// node's endReadableNT emits 'end' only while the readable half never
|
|
1334
|
+
// errored (internal/streams/readable.js:2145, kErrored in the state
|
|
1335
|
+
// mask): a destroy(err) landing between the schedule and the tick
|
|
1336
|
+
// suppresses the queued 'end'. Without the delivery-time check an
|
|
1337
|
+
// iterator's settle(null) outruns the destroy's 'error' pair and a
|
|
1338
|
+
// helper started mid-teardown answers a clean done (the some/every
|
|
1339
|
+
// shape, measured on node v26.12.1: every() rejects ABORT_ERR).
|
|
1340
|
+
if (this._errored !== undefined) {
|
|
1341
|
+
return;
|
|
1342
|
+
}
|
|
1343
|
+
this._endDelivered = true;
|
|
1344
|
+
this.emit('end');
|
|
1345
|
+
this._maybeAutoClose();
|
|
1346
|
+
});
|
|
292
1347
|
}
|
|
293
1348
|
|
|
1349
|
+
// The session decoded END_STREAM for this stream (or a destroy forces the EOF).
|
|
294
1350
|
_endRead() {
|
|
295
1351
|
if (this._remoteEnded) {
|
|
296
1352
|
return;
|
|
297
1353
|
}
|
|
298
1354
|
this._remoteEnded = true;
|
|
299
|
-
this.
|
|
300
|
-
|
|
301
|
-
|
|
1355
|
+
this._rEof = true;
|
|
1356
|
+
if (this._rbuf !== null && this._rbuf.length > 0) {
|
|
1357
|
+
// buffered octets a paused consumer has not pulled yet: 'end' waits for the
|
|
1358
|
+
// drain, the way node's push(null) parks the EOF behind the buffer.
|
|
1359
|
+
return;
|
|
1360
|
+
}
|
|
1361
|
+
// Empty queue at a known EOF: node kicks the readable half here -- its
|
|
302
1362
|
// onStreamClose calls stream.read(0) on the client branch
|
|
303
|
-
// (internal/http2/core.js:641)
|
|
304
|
-
//
|
|
305
|
-
// -- the upstream capture-rejection shape, an empty
|
|
306
|
-
// (measured on live node
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
1363
|
+
// (internal/http2/core.js:641), so an unconsumed response still reaches 'end'
|
|
1364
|
+
// (and, with the writable finished, auto-destroy's 'close'): a request the
|
|
1365
|
+
// application never resumed -- the upstream capture-rejection shape, an empty
|
|
1366
|
+
// 500 body -- never closed without it (measured on live node).
|
|
1367
|
+
this._scheduleEnd();
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
// node's Readable.push, at this stream's delivery fidelity: flowing delivers
|
|
1371
|
+
// synchronously through the decoder, paused queues and wakes 'readable'. The
|
|
1372
|
+
// decision order is node's readableAddChunkPushByteMode: convert, then the
|
|
1373
|
+
// empty-chunk branch, and only after it the after-EOF error and the destroyed
|
|
1374
|
+
// silence (lib/stream.js push(), measured against node). An empty chunk queues
|
|
1375
|
+
// nothing and there is no watermark, so the canPushMore verdict is always true.
|
|
1376
|
+
push(chunk, encoding) {
|
|
1377
|
+
if (chunk === null) {
|
|
1378
|
+
// push(null) is the EOF into the readable queue: silent when the EOF is
|
|
1379
|
+
// already known (node's onEofChunk early return), parked behind the buffer
|
|
1380
|
+
// like the wire's END_STREAM (_endRead).
|
|
1381
|
+
if (this._rEof !== true && this._endEmitted !== true) {
|
|
1382
|
+
this._rEof = true;
|
|
1383
|
+
if (this._rbuf === null || this._rbuf.length === 0) {
|
|
1384
|
+
this._scheduleEnd();
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
return false;
|
|
1388
|
+
}
|
|
1389
|
+
if (chunk === undefined) {
|
|
1390
|
+
// node's byte-mode chain lets undefined fall past the type error into the
|
|
1391
|
+
// empty-chunk branch: no queueing, no event.
|
|
1392
|
+
return true;
|
|
1393
|
+
}
|
|
1394
|
+
let buf = chunk;
|
|
1395
|
+
if (typeof buf === 'string') {
|
|
1396
|
+
buf = Buffer.from(buf, encoding || 'utf8'); // the readable side keeps its own 'utf8'
|
|
1397
|
+
} else if (buf instanceof Buffer) {
|
|
1398
|
+
// keep
|
|
1399
|
+
} else if (ArrayBuffer.isView(buf)) {
|
|
1400
|
+
buf = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
1401
|
+
} else {
|
|
1402
|
+
this.destroy(errorOf('ERR_INVALID_ARG_TYPE',
|
|
1403
|
+
'The "chunk" argument must be of type string or an instance of Buffer, ' +
|
|
1404
|
+
'ArrayBufferView, or DataView'));
|
|
1405
|
+
return false;
|
|
1406
|
+
}
|
|
1407
|
+
if (buf.length === 0) {
|
|
1408
|
+
return true;
|
|
1409
|
+
}
|
|
1410
|
+
if (this._rEof === true || this._endDelivered === true) {
|
|
1411
|
+
const err = errorOf('ERR_STREAM_PUSH_AFTER_EOF', 'push() after EOF');
|
|
1412
|
+
if (!this._destroyFlag) {
|
|
1413
|
+
// node's errorOrDestroy destroys the stream through its autoDestroy, so
|
|
1414
|
+
// this arrives as 'error' then 'close' on the next ticks -- the same
|
|
1415
|
+
// route the write-after-end shape takes in write() above.
|
|
1416
|
+
this.destroy(err);
|
|
1417
|
+
}
|
|
1418
|
+
return false;
|
|
1419
|
+
}
|
|
1420
|
+
if (this._destroyFlag || this._readableState.errorEmitted === true) {
|
|
1421
|
+
return false; // node: kDestroyed|kErrored is the silent false
|
|
1422
|
+
}
|
|
1423
|
+
if (this._flowing === true) {
|
|
1424
|
+
const d = this._decoder;
|
|
1425
|
+
if (d !== null) {
|
|
1426
|
+
const s = d.write(buf);
|
|
1427
|
+
if (s.length !== 0) {
|
|
1428
|
+
this._dataRead = true;
|
|
1429
|
+
this.emit('data', s);
|
|
1430
|
+
}
|
|
1431
|
+
return true;
|
|
1432
|
+
}
|
|
1433
|
+
this._dataRead = true;
|
|
1434
|
+
this.emit('data', buf);
|
|
1435
|
+
return true;
|
|
1436
|
+
}
|
|
1437
|
+
if (this._rbuf === null) {
|
|
1438
|
+
this._rbuf = [];
|
|
1439
|
+
}
|
|
1440
|
+
this._rbuf.push(buf);
|
|
1441
|
+
this.emit('readable');
|
|
1442
|
+
return true;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
pause() {
|
|
1446
|
+
this._flowing = false;
|
|
1447
|
+
return this;
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
resume() {
|
|
1451
|
+
if (this._flowing !== true) {
|
|
1452
|
+
this._flowing = true;
|
|
1453
|
+
const rbuf = this._rbuf;
|
|
1454
|
+
if (rbuf !== null && rbuf.length > 0) {
|
|
1455
|
+
// node's resume() flips the flowing bit synchronously but the delivery
|
|
1456
|
+
// itself runs on the kResumeScheduled next tick (readable.js).
|
|
1457
|
+
this._tick(() => {
|
|
1458
|
+
if (this._flowing === true && !this._destroyFlag) {
|
|
1459
|
+
this._flushBuffered();
|
|
1460
|
+
}
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
return this;
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
// node's isPaused reads flowing === false (measured: false on a fresh stream,
|
|
1468
|
+
// true after pause(), false again after resume()).
|
|
1469
|
+
isPaused() {
|
|
1470
|
+
return this._flowing === false;
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
_flushBuffered() {
|
|
1474
|
+
const rbuf = this._rbuf;
|
|
1475
|
+
const d = this._decoder;
|
|
1476
|
+
while (this._flowing === true && !this._destroyFlag && rbuf !== null &&
|
|
1477
|
+
rbuf.length > 0) {
|
|
1478
|
+
let s = rbuf.shift();
|
|
1479
|
+
if (d !== null && typeof s !== 'string') {
|
|
1480
|
+
s = d.write(s);
|
|
1481
|
+
if (s.length === 0) {
|
|
1482
|
+
continue;
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
this._dataRead = true;
|
|
1486
|
+
this.emit('data', s);
|
|
1487
|
+
}
|
|
1488
|
+
if (this._flowing === true && !this._destroyFlag &&
|
|
1489
|
+
(this._rbuf === null || this._rbuf.length === 0) && this._rEof === true) {
|
|
1490
|
+
this._scheduleEnd();
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
// node: attaching 'data' flips the stream flowing and flushes whatever buffered
|
|
1495
|
+
// on the resume tick (readable.js data-listener resume). once('data') misses
|
|
1496
|
+
// this -- the same gap the server's on() override carries.
|
|
1497
|
+
on(name, fn) {
|
|
1498
|
+
if (name === 'data' && this._flowing !== true && !this._destroyFlag) {
|
|
1499
|
+
this._flowing = true;
|
|
1500
|
+
const rbuf = this._rbuf;
|
|
1501
|
+
if (rbuf !== null && rbuf.length > 0) {
|
|
1502
|
+
this._tick(() => {
|
|
1503
|
+
if (this._flowing === true && !this._destroyFlag) {
|
|
1504
|
+
this._flushBuffered();
|
|
1505
|
+
}
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
return super.on(name, fn);
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
// node's autoDestroy: the stream closes itself when both halves are done. The
|
|
1513
|
+
// gate reads the two flags the machinery would have -- the readable side's
|
|
1514
|
+
// scheduled 'end' and the writable side's END_STREAM on the wire. A GET whose
|
|
1515
|
+
// request writable the application never ended never auto-closes (node's
|
|
1516
|
+
// machinery does not close it either; the session dropped the id already).
|
|
1517
|
+
_maybeAutoClose() {
|
|
1518
|
+
if (this._destroyFlag || this._endEmitted !== true || this._endSent !== true) {
|
|
1519
|
+
return;
|
|
1520
|
+
}
|
|
1521
|
+
this.destroy();
|
|
311
1522
|
}
|
|
312
1523
|
|
|
313
1524
|
// node's closeStream (core.js:2071) minus the RST submission, which each caller
|
|
@@ -317,7 +1528,11 @@ class ClientHttp2Stream extends Duplex {
|
|
|
317
1528
|
// not whether the octets went out, so a request body still open is 'aborted' and then
|
|
318
1529
|
// ended, and the stream reports 'finish' and writableEnded like node's (measured on
|
|
319
1530
|
// v26.12.1 for codes 0, 1 and 8 against a raw peer, with and without END_STREAM).
|
|
320
|
-
|
|
1531
|
+
// The ending flip is done directly instead of through public end(): _destroy's
|
|
1532
|
+
// closeStream runs after destroy() already flagged the stream, and node's
|
|
1533
|
+
// closeStream ends the writable during teardown without the application-facing
|
|
1534
|
+
// destroyed guard.
|
|
1535
|
+
_closeStream(code, errored) {
|
|
321
1536
|
this._closed = true;
|
|
322
1537
|
this.rstCode = code;
|
|
323
1538
|
if (!this.writableEnded) {
|
|
@@ -325,7 +1540,22 @@ class ClientHttp2Stream extends Duplex {
|
|
|
325
1540
|
this.aborted = true;
|
|
326
1541
|
this.emit('aborted');
|
|
327
1542
|
}
|
|
328
|
-
|
|
1543
|
+
// node's closeStream calls end() in both routes, but whether it takes
|
|
1544
|
+
// effect is the machinery's decision: a clean destroy accepts it --
|
|
1545
|
+
// writableEnded true afterwards (client-destroy-code.test.js 'an open
|
|
1546
|
+
// request body') -- while an errored destroy refuses it the way
|
|
1547
|
+
// end() on an errored stream answers ERR_STREAM_DESTROYED without setting
|
|
1548
|
+
// ending, and writableEnded stays false ('an open request body and an
|
|
1549
|
+
// error'). 'aborted' fires in both.
|
|
1550
|
+
if (errored !== true) {
|
|
1551
|
+
this._endQueued = true;
|
|
1552
|
+
// node's closeStream ends the writable with no application callback:
|
|
1553
|
+
// the machinery's end(undefined) parks a nop, and _writableDone settles
|
|
1554
|
+
// it -- park the same EMPTY_CB here, or _driveW promotes undefined into
|
|
1555
|
+
// _finalCb and the pump crashes.
|
|
1556
|
+
this._endCb = EMPTY_CB;
|
|
1557
|
+
this._driveW();
|
|
1558
|
+
}
|
|
329
1559
|
}
|
|
330
1560
|
}
|
|
331
1561
|
|
|
@@ -350,16 +1580,36 @@ class ClientHttp2Stream extends Duplex {
|
|
|
350
1580
|
this.destroy();
|
|
351
1581
|
}
|
|
352
1582
|
|
|
353
|
-
// Destroying the
|
|
354
|
-
// or `close()`). node derives the code here
|
|
355
|
-
// a stream whose wire life already
|
|
356
|
-
//
|
|
357
|
-
// the
|
|
358
|
-
//
|
|
359
|
-
//
|
|
360
|
-
//
|
|
361
|
-
//
|
|
362
|
-
|
|
1583
|
+
// Destroying the stream is how the application abandons a request
|
|
1584
|
+
// (`stream.destroy()`, or `close()`). node derives the code here
|
|
1585
|
+
// (core.js:2503-2522) rather than fixing it: a stream whose wire life already
|
|
1586
|
+
// ended keeps the code it closed with, otherwise the session's goaway/destroy
|
|
1587
|
+
// code is the starting point, and an error overrides it -- the session's code
|
|
1588
|
+
// first, then CANCEL for node's own AbortError, then INTERNAL_ERROR. Measured on
|
|
1589
|
+
// a live node client against a raw peer: destroy() frames RST_STREAM(0) and
|
|
1590
|
+
// reports rstCode 0, destroy(new Error()) frames RST_STREAM(2) and reports
|
|
1591
|
+
// rstCode 2, and an aborted options.signal frames RST_STREAM(8) and reports
|
|
1592
|
+
// rstCode 8. The old unconditional rstStream(id) framed CANCEL for all three
|
|
1593
|
+
// and left rstCode at 0. The teardown runs one tick after the call (node's
|
|
1594
|
+
// destroy chain defers _destroy the same way); 'error' then 'close' fire from
|
|
1595
|
+
// that tick, one tick earlier than the machinery's emitErrorNT pair.
|
|
1596
|
+
destroy(err) {
|
|
1597
|
+
if (this._destroyFlag) {
|
|
1598
|
+
// streams.destroy() silently drops an error handed to an already-destroyed
|
|
1599
|
+
// stream (internal/streams/destroy.js)
|
|
1600
|
+
return;
|
|
1601
|
+
}
|
|
1602
|
+
this._destroyFlag = true;
|
|
1603
|
+
// node's destroy() runs _destroy inline -- the constructed branch calls
|
|
1604
|
+
// _destroy synchronously (internal/streams/destroy.js:49), so the destroyed
|
|
1605
|
+
// flag, the RST and the forced EOF are all settled before the caller's next
|
|
1606
|
+
// await; deferring the body here made a return() that resumes in a
|
|
1607
|
+
// microtask read rstCode before the teardown reached the wire, a shape node
|
|
1608
|
+
// does not have.
|
|
1609
|
+
this._destroyBody(err);
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
_destroyBody(err) {
|
|
363
1613
|
const session = this.session;
|
|
364
1614
|
const sessionCode = session.goawayCode || session._destroyCode;
|
|
365
1615
|
let code = this._closed ? this.rstCode : sessionCode;
|
|
@@ -372,35 +1622,128 @@ class ClientHttp2Stream extends Duplex {
|
|
|
372
1622
|
code = INTERNAL_ERROR;
|
|
373
1623
|
}
|
|
374
1624
|
}
|
|
1625
|
+
// The error the teardown carries, whether the caller's own or the one
|
|
1626
|
+
// synthesized from the code. Derived ahead of closeStream on purpose: the
|
|
1627
|
+
// machinery decides at closeStream time whether end() takes effect, so
|
|
1628
|
+
// closeStream needs the verdict, not just the code.
|
|
1629
|
+
const verdict = err != null ? err
|
|
1630
|
+
: (code !== NO_ERROR && code !== CANCEL ? streamErrorOf(code) : null);
|
|
375
1631
|
if (!this._closed) {
|
|
376
|
-
this._closeStream(code);
|
|
1632
|
+
this._closeStream(code, verdict != null);
|
|
377
1633
|
// A stream the session never opened has no id on the wire: rstStream finds nothing
|
|
378
1634
|
// and submits nothing, which is node's kNoRstStream for a pending stream.
|
|
379
1635
|
session.rstStream(this.id, code);
|
|
380
1636
|
}
|
|
381
1637
|
// node's _destroy pushes null unconditionally, after closeStream and before the
|
|
382
|
-
// handle goes
|
|
383
|
-
//
|
|
384
|
-
//
|
|
385
|
-
//
|
|
386
|
-
|
|
387
|
-
//
|
|
388
|
-
//
|
|
389
|
-
//
|
|
390
|
-
|
|
391
|
-
|
|
1638
|
+
// handle goes -- but its readable machinery only delivers that EOF as 'end' when
|
|
1639
|
+
// the stream did not error: an errored destroy errors the readable half and
|
|
1640
|
+
// endReadableNT suppresses 'end' for it. The transcript rule (client-rst.test.js,
|
|
1641
|
+
// client-fatal.test.js): NO_ERROR and CANCEL reach 'end', an error code does not --
|
|
1642
|
+
// so a CANCEL reset -- and an application destroy() with the body already consumed
|
|
1643
|
+
// -- reports 'end' (measured on v26.12.1), while the fatal-frame teardown and an
|
|
1644
|
+
// application destroy(err) end with the error/close pair only. The machinery
|
|
1645
|
+
// delivers its 'end' before 'close' (push(null) inside _destroy, endReadable
|
|
1646
|
+
// ahead of the destroy chain), so the forced EOF is emitted in the same tick
|
|
1647
|
+
// instead of scheduled behind. node's unread DATA lives in nghttp2's handle
|
|
1648
|
+
// buffer, invisible to the JS readable until the first _read() pulls it
|
|
1649
|
+
// (core.js:2417 flags didRead), so a stream nobody read has an EMPTY JS buffer
|
|
1650
|
+
// at the push(null) and 'end' fires (measured: close(0) at the response tick
|
|
1651
|
+
// reports aborted, finish, end, close); only a stream that read and then paused
|
|
1652
|
+
// -- with delivered bytes parked in the JS buffer -- keeps 'end' waiting for a
|
|
1653
|
+
// consumer that never comes. _dataRead is that didRead here. The extension is
|
|
1654
|
+
// bound to NO_ERROR alone: node reaches onStreamClose's push(null) only through
|
|
1655
|
+
// the NO_ERROR else-branch (core.js:615-631) -- every other code, including
|
|
1656
|
+
// CANCEL, destroys directly and _destroy's push(null) is a no-op behind the
|
|
1657
|
+
// destroyed flag, so a close(8) on an unread body stops at aborted+finish
|
|
1658
|
+
// (measured both stacks), and a never-read inbound CANCEL keeps the stream open
|
|
1659
|
+
// for a consumer (measured: finish only, no close, on both stacks).
|
|
1660
|
+
if (verdict == null && this._rEof !== true && this._endEmitted !== true &&
|
|
1661
|
+
(this._flowing === true || (code === NO_ERROR &&
|
|
1662
|
+
(this._dataRead !== true || this._rbuf === null || this._rbuf.length === 0)))) {
|
|
1663
|
+
this._rEof = true;
|
|
1664
|
+
this._endEmitted = true;
|
|
1665
|
+
let tail = null;
|
|
1666
|
+
if (this._decoder !== null) {
|
|
1667
|
+
tail = this._decoder.end();
|
|
1668
|
+
if (tail.length !== 0) {
|
|
1669
|
+
this._dataRead = true;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
this._endDelivered = true;
|
|
1673
|
+
// The delivery rides _tick so it lands behind the 'finish' the teardown's
|
|
1674
|
+
// writable end scheduled ahead of it -- node's transcript for an inbound
|
|
1675
|
+
// reset is aborted, finish, then the readable verdict (client-rst.test.js),
|
|
1676
|
+
// and a synchronous 'end' would invert that pair now that destroy() runs
|
|
1677
|
+
// its body inline.
|
|
1678
|
+
this._tick(() => {
|
|
1679
|
+
if (tail !== null && tail.length !== 0) {
|
|
1680
|
+
this.emit('data', tail);
|
|
1681
|
+
}
|
|
1682
|
+
this.emit('end');
|
|
1683
|
+
});
|
|
1684
|
+
}
|
|
1685
|
+
// The error/close pair is one nextTick behind the teardown body the way
|
|
1686
|
+
// node's is: onDestroy schedules emitErrorCloseNT/emitCloseNT
|
|
1687
|
+
// (internal/streams/destroy.js _destroy), so a destroy(err) arrives as
|
|
1688
|
+
// 'error' then 'close' on the next ticks while the destroyed flag and the
|
|
1689
|
+
// wire rst are already synchronous. The pair stays one tick -- a surface
|
|
1690
|
+
// test that settles on the destroyed flag reads its report before the
|
|
1691
|
+
// events, exactly as the same shape reads on node.
|
|
1692
|
+
if (verdict != null) {
|
|
1693
|
+
this._errored = verdict;
|
|
1694
|
+
this._readableState.errorEmitted = true;
|
|
1695
|
+
this._writableState.errorEmitted = true;
|
|
1696
|
+
process.nextTick(() => {
|
|
1697
|
+
this.emit('error', verdict);
|
|
1698
|
+
this.emit('close');
|
|
1699
|
+
});
|
|
1700
|
+
} else {
|
|
1701
|
+
process.nextTick(() => {
|
|
1702
|
+
this.emit('close');
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
// node: Http2Stream.close (internal/http2/core.js:2474) -- validateInteger 0..2^32-1,
|
|
1708
|
+
// validateFunction; a second call on a closed stream is a silent no-op; the callback
|
|
1709
|
+
// rides 'close' and receives no arguments (measured: cb(0)). closeStream's RST default
|
|
1710
|
+
// is kSubmitRstStream (core.js:2071): it goes out at once unless the writable was
|
|
1711
|
+
// already ending, not finished, and the code is NO_ERROR -- that shape rides 'finish'
|
|
1712
|
+
// (core.js:2095-2101; the `ending` there is destructured BEFORE closeStream's own
|
|
1713
|
+
// end() flips it, which is why a POST with an open body RSTs at once). Measured on a
|
|
1714
|
+
// live node v26.12.1 client: a GET (auto-ended and finished) and that open-body POST
|
|
1715
|
+
// -- which 'aborted's, its writable never having ended -- both RST at once, and the
|
|
1716
|
+
// JS teardown ('error' for a code past 0 and 8, then 'close') lands a turn later,
|
|
1717
|
+
// with the full state object still readable in the same tick. destroy() itself runs
|
|
1718
|
+
// its body inline here; close() defers the destroy one tick to keep that same-tick
|
|
1719
|
+
// state read.
|
|
1720
|
+
close(code = NO_ERROR, callback) {
|
|
1721
|
+
validateRstCode(code, callback);
|
|
1722
|
+
if (this.closed) {
|
|
1723
|
+
return;
|
|
1724
|
+
}
|
|
1725
|
+
if (callback !== undefined) {
|
|
1726
|
+
this.once('close', callback);
|
|
1727
|
+
}
|
|
1728
|
+
const ending = this._endQueued === true;
|
|
1729
|
+
this._closeStream(code);
|
|
1730
|
+
if (ending && this.writableFinished !== true && code === NO_ERROR) {
|
|
1731
|
+
this.once('finish', () => {
|
|
1732
|
+
this.session.rstStream(this.id, code);
|
|
1733
|
+
this._tick(() => this.destroy());
|
|
1734
|
+
});
|
|
1735
|
+
return;
|
|
392
1736
|
}
|
|
393
|
-
|
|
1737
|
+
this.session.rstStream(this.id, code);
|
|
1738
|
+
this._tick(() => this.destroy());
|
|
394
1739
|
}
|
|
395
1740
|
|
|
396
1741
|
// node's Http2Stream.sendTrailers(headers) -- the same base method the server stream
|
|
397
1742
|
// exposes, so the same three guards in the same order, all synchronous throws:
|
|
398
|
-
// destroyed
|
|
399
|
-
//
|
|
400
|
-
// -> ERR_HTTP2_TRAILERS_ALREADY_SENT, and 'wantTrailers' not yet emitted
|
|
401
|
-
// -> ERR_HTTP2_TRAILERS_NOT_READY.
|
|
1743
|
+
// destroyed -> ERR_HTTP2_INVALID_STREAM, already sent -> ERR_HTTP2_TRAILERS_ALREADY_SENT,
|
|
1744
|
+
// and 'wantTrailers' not yet emitted -> ERR_HTTP2_TRAILERS_NOT_READY.
|
|
402
1745
|
sendTrailers(trailers) {
|
|
403
|
-
if (this.
|
|
1746
|
+
if (this._destroyFlag) {
|
|
404
1747
|
const err = new Error('The stream has been destroyed');
|
|
405
1748
|
err.code = 'ERR_HTTP2_INVALID_STREAM';
|
|
406
1749
|
throw err;
|
|
@@ -429,6 +1772,704 @@ class ClientHttp2Stream extends Duplex {
|
|
|
429
1772
|
// sentTrailers unset on both stacks.
|
|
430
1773
|
this.sentTrailers = Object.assign({ __proto__: null }, trailers);
|
|
431
1774
|
}
|
|
1775
|
+
|
|
1776
|
+
// node's Readable.prototype.pipe (internal/streams/readable.js:978), not the
|
|
1777
|
+
// legacy Stream.prototype.pipe the bare base would supply: the tracked
|
|
1778
|
+
// destination list, the unpipe() surface, and the teardown that makes a
|
|
1779
|
+
// destroyed or finished source end and unpipe its destinations -- measured
|
|
1780
|
+
// on a live node the sink of a stream destroyed mid-pipe reports
|
|
1781
|
+
// pipe,finish,unpipe,close where the legacy base stops at pipe,close
|
|
1782
|
+
// (test/pipe-parity.test.js). The await-drain bookkeeping is node's
|
|
1783
|
+
// single-dest fast value promoted to a Set for the second destination.
|
|
1784
|
+
pipe(dest, options) {
|
|
1785
|
+
const src = this;
|
|
1786
|
+
if (src._pipes === null) {
|
|
1787
|
+
src._pipes = [];
|
|
1788
|
+
}
|
|
1789
|
+
src._pipes.push(dest);
|
|
1790
|
+
|
|
1791
|
+
const doEnd = (!options || options.end !== false) &&
|
|
1792
|
+
dest !== process.stdout && dest !== process.stderr;
|
|
1793
|
+
const endFn = doEnd ? onend : unpipe;
|
|
1794
|
+
if (src._endDelivered === true) {
|
|
1795
|
+
src._tick(endFn);
|
|
1796
|
+
} else {
|
|
1797
|
+
src.once('end', endFn);
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
dest.on('unpipe', onunpipe);
|
|
1801
|
+
function onunpipe(readable, unpipeInfo) {
|
|
1802
|
+
if (readable === src) {
|
|
1803
|
+
if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
|
|
1804
|
+
unpipeInfo.hasUnpiped = true;
|
|
1805
|
+
cleanup();
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
function onend() {
|
|
1811
|
+
dest.end();
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
let ondrain;
|
|
1815
|
+
let cleanedUp = false;
|
|
1816
|
+
function cleanup() {
|
|
1817
|
+
dest.removeListener('close', onclose);
|
|
1818
|
+
dest.removeListener('finish', onfinish);
|
|
1819
|
+
if (ondrain) {
|
|
1820
|
+
dest.removeListener('drain', ondrain);
|
|
1821
|
+
}
|
|
1822
|
+
dest.removeListener('error', onerror);
|
|
1823
|
+
dest.removeListener('unpipe', onunpipe);
|
|
1824
|
+
src.removeListener('end', onend);
|
|
1825
|
+
src.removeListener('end', unpipe);
|
|
1826
|
+
src.removeListener('data', ondata);
|
|
1827
|
+
cleanedUp = true;
|
|
1828
|
+
// a dest that errored or closed while the source waited on its drain
|
|
1829
|
+
// must not leave the flow parked for a writer that is gone
|
|
1830
|
+
if (ondrain && src._awaitDrain !== null) {
|
|
1831
|
+
ondrain();
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
function pause() {
|
|
1836
|
+
if (!cleanedUp) {
|
|
1837
|
+
if (src._pipes.length === 1 && src._pipes[0] === dest) {
|
|
1838
|
+
src._awaitDrain = dest;
|
|
1839
|
+
} else if (src._pipes.includes(dest)) {
|
|
1840
|
+
if (!(src._awaitDrain instanceof Set)) {
|
|
1841
|
+
src._awaitDrain = new Set(src._awaitDrain === null ? [] : [src._awaitDrain]);
|
|
1842
|
+
}
|
|
1843
|
+
src._awaitDrain.add(dest);
|
|
1844
|
+
}
|
|
1845
|
+
src.pause();
|
|
1846
|
+
}
|
|
1847
|
+
if (!ondrain) {
|
|
1848
|
+
ondrain = pipeOnDrain;
|
|
1849
|
+
dest.on('drain', ondrain);
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
function pipeOnDrain() {
|
|
1854
|
+
const a = src._awaitDrain;
|
|
1855
|
+
if (a === dest) {
|
|
1856
|
+
src._awaitDrain = null;
|
|
1857
|
+
} else if (a instanceof Set) {
|
|
1858
|
+
a.delete(dest);
|
|
1859
|
+
}
|
|
1860
|
+
const still = src._awaitDrain;
|
|
1861
|
+
if ((still === null || still.size === 0) && src.listenerCount('data') > 0) {
|
|
1862
|
+
src.resume();
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
src.on('data', ondata);
|
|
1867
|
+
function ondata(chunk) {
|
|
1868
|
+
try {
|
|
1869
|
+
if (dest.write(chunk) === false) {
|
|
1870
|
+
pause();
|
|
1871
|
+
}
|
|
1872
|
+
} catch (error) {
|
|
1873
|
+
dest.destroy(error);
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
// If the dest has an error, then stop piping into it. However, don't
|
|
1878
|
+
// suppress the throwing behavior for this (node's comment, verbatim rule).
|
|
1879
|
+
function onerror(er) {
|
|
1880
|
+
unpipe();
|
|
1881
|
+
dest.removeListener('error', onerror);
|
|
1882
|
+
if (dest.listenerCount('error') === 0) {
|
|
1883
|
+
const s = dest._writableState || dest._readableState;
|
|
1884
|
+
if (s && !s.errorEmitted) {
|
|
1885
|
+
dest.destroy(er);
|
|
1886
|
+
} else {
|
|
1887
|
+
dest.emit('error', er);
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
dest.prependListener('error', onerror);
|
|
1892
|
+
|
|
1893
|
+
// Both close and finish should trigger unpipe, but only once.
|
|
1894
|
+
function onclose() {
|
|
1895
|
+
dest.removeListener('finish', onfinish);
|
|
1896
|
+
unpipe();
|
|
1897
|
+
}
|
|
1898
|
+
dest.once('close', onclose);
|
|
1899
|
+
function onfinish() {
|
|
1900
|
+
dest.removeListener('close', onclose);
|
|
1901
|
+
unpipe();
|
|
1902
|
+
}
|
|
1903
|
+
dest.once('finish', onfinish);
|
|
1904
|
+
|
|
1905
|
+
function unpipe() {
|
|
1906
|
+
src.unpipe(dest);
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
dest.emit('pipe', src);
|
|
1910
|
+
|
|
1911
|
+
if (dest.writableNeedDrain === true) {
|
|
1912
|
+
pause();
|
|
1913
|
+
} else if (src._flowing !== true) {
|
|
1914
|
+
src.resume();
|
|
1915
|
+
}
|
|
1916
|
+
return dest;
|
|
1917
|
+
}
|
|
1918
|
+
|
|
1919
|
+
// node's Readable.prototype.unpipe (readable.js:1180): no argument removes
|
|
1920
|
+
// every destination and stops the flow; one argument removes that
|
|
1921
|
+
// destination if it is piped to, and the call is its own 'unpipe' event
|
|
1922
|
+
// onto the dest with the hasUnpiped record the pipe closure cleans up on.
|
|
1923
|
+
unpipe(dest) {
|
|
1924
|
+
const pipes = this._pipes;
|
|
1925
|
+
if (pipes === null || pipes.length === 0) {
|
|
1926
|
+
return this;
|
|
1927
|
+
}
|
|
1928
|
+
if (!dest) {
|
|
1929
|
+
const dests = pipes;
|
|
1930
|
+
this._pipes = [];
|
|
1931
|
+
this.pause();
|
|
1932
|
+
for (let i = 0; i < dests.length; i++) {
|
|
1933
|
+
dests[i].emit('unpipe', this, { hasUnpiped: false });
|
|
1934
|
+
}
|
|
1935
|
+
return this;
|
|
1936
|
+
}
|
|
1937
|
+
const index = pipes.indexOf(dest);
|
|
1938
|
+
if (index === -1) {
|
|
1939
|
+
return this;
|
|
1940
|
+
}
|
|
1941
|
+
pipes.splice(index, 1);
|
|
1942
|
+
if (pipes.length === 0) {
|
|
1943
|
+
this.pause();
|
|
1944
|
+
}
|
|
1945
|
+
dest.emit('unpipe', this, { hasUnpiped: false });
|
|
1946
|
+
return this;
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
// node's Readable.prototype.compose (readable.js:1810 plus
|
|
1950
|
+
// internal/streams/compose.js, the Node-stream branch): the composed Duplex
|
|
1951
|
+
// writes into this and reads out of the chain behind it. h2lean drives the
|
|
1952
|
+
// two-stream chain with its own pipe() above -- the port of
|
|
1953
|
+
// Readable.prototype.pipe -- where node uses internal/streams/pipeline; for
|
|
1954
|
+
// the prototype method's single argument those are the same chain (src
|
|
1955
|
+
// 'end' -> dest.end(), src error -> dest teardown), and the rest is node's
|
|
1956
|
+
// wiring verbatim: d._write into head with the drain callback, d._final =
|
|
1957
|
+
// head.end(), tail 'data'/'end' into d.push, teardown destroying the tail.
|
|
1958
|
+
// Web streams (TransformStream and friends) are refused here where node
|
|
1959
|
+
// composes them -- KNOWN DIVERGENCE, stated in the header.
|
|
1960
|
+
compose(stream, options) {
|
|
1961
|
+
if (options != null) {
|
|
1962
|
+
validateObjectArg(options, 'options');
|
|
1963
|
+
}
|
|
1964
|
+
validateSignalArg(options);
|
|
1965
|
+
const tail = typeof stream === 'function' ? Duplex.from(stream) : stream;
|
|
1966
|
+
if (tail === null || typeof tail !== 'object' ||
|
|
1967
|
+
typeof tail.pipe !== 'function' || typeof tail.write !== 'function') {
|
|
1968
|
+
// node's internal compose SKIPS non-streams in its own loop (the
|
|
1969
|
+
// "TODO: add checks for non streams" branch) and the refusal actually
|
|
1970
|
+
// comes from pipeline's `body` validation -- measured code:
|
|
1971
|
+
// ERR_INVALID_ARG_TYPE, not ERR_INVALID_ARG_VALUE.
|
|
1972
|
+
throw errorOf('ERR_INVALID_ARG_TYPE',
|
|
1973
|
+
'The "body" argument must be of type function or an instance of Stream. ' +
|
|
1974
|
+
`Received type ${typeof stream} (${inspect(stream)}).`);
|
|
1975
|
+
}
|
|
1976
|
+
let ondrain = null;
|
|
1977
|
+
let onfinish = null;
|
|
1978
|
+
let onclose = undefined;
|
|
1979
|
+
const src = this;
|
|
1980
|
+
const d = new Duplex({
|
|
1981
|
+
writableObjectMode: !!src.writableObjectMode,
|
|
1982
|
+
readableObjectMode: !!tail.readableObjectMode,
|
|
1983
|
+
writable: true,
|
|
1984
|
+
readable: true
|
|
1985
|
+
});
|
|
1986
|
+
|
|
1987
|
+
function onfinished(err) {
|
|
1988
|
+
const cb = onclose;
|
|
1989
|
+
onclose = null;
|
|
1990
|
+
if (cb) {
|
|
1991
|
+
cb(err);
|
|
1992
|
+
} else if (err) {
|
|
1993
|
+
d.destroy(err);
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
|
|
1997
|
+
// the writable half, node's d._write/d._final over head = this.
|
|
1998
|
+
d._write = function (chunk, encoding, callback) {
|
|
1999
|
+
if (src.write(chunk, encoding)) {
|
|
2000
|
+
callback();
|
|
2001
|
+
} else {
|
|
2002
|
+
ondrain = callback;
|
|
2003
|
+
}
|
|
2004
|
+
};
|
|
2005
|
+
d._final = function (callback) {
|
|
2006
|
+
src.end();
|
|
2007
|
+
onfinish = callback;
|
|
2008
|
+
};
|
|
2009
|
+
src.on('drain', () => {
|
|
2010
|
+
if (ondrain) {
|
|
2011
|
+
const cb = ondrain;
|
|
2012
|
+
ondrain = null;
|
|
2013
|
+
cb();
|
|
2014
|
+
}
|
|
2015
|
+
});
|
|
2016
|
+
|
|
2017
|
+
// node's pipeline([head, tail], onfinished): the chain is this pipe, and
|
|
2018
|
+
// any chain error reaches onfinished the way the pipeline callback takes
|
|
2019
|
+
// it (the pipe's own dest-side teardown is already node's behaviour).
|
|
2020
|
+
src.pipe(tail);
|
|
2021
|
+
src.on('error', onfinished);
|
|
2022
|
+
tail.on('error', onfinished);
|
|
2023
|
+
|
|
2024
|
+
// node's eos(toRead, cb) that releases d._final's callback.
|
|
2025
|
+
finished(tail, () => {
|
|
2026
|
+
if (onfinish) {
|
|
2027
|
+
const cb = onfinish;
|
|
2028
|
+
onfinish = null;
|
|
2029
|
+
cb();
|
|
2030
|
+
}
|
|
2031
|
+
});
|
|
2032
|
+
|
|
2033
|
+
// the readable half, node's tail 'data'/'end' push.
|
|
2034
|
+
d._read = function () {
|
|
2035
|
+
tail.resume();
|
|
2036
|
+
};
|
|
2037
|
+
tail.on('data', (chunk) => {
|
|
2038
|
+
if (!d.push(chunk)) {
|
|
2039
|
+
tail.pause();
|
|
2040
|
+
}
|
|
2041
|
+
});
|
|
2042
|
+
tail.on('end', () => {
|
|
2043
|
+
d.push(null);
|
|
2044
|
+
});
|
|
2045
|
+
|
|
2046
|
+
d._destroy = function (err, callback) {
|
|
2047
|
+
if (!err && onclose !== null) {
|
|
2048
|
+
err = new AbortError();
|
|
2049
|
+
}
|
|
2050
|
+
ondrain = null;
|
|
2051
|
+
onfinish = null;
|
|
2052
|
+
destroyStream(tail, err);
|
|
2053
|
+
if (onclose === null) {
|
|
2054
|
+
callback(err);
|
|
2055
|
+
} else {
|
|
2056
|
+
onclose = callback;
|
|
2057
|
+
}
|
|
2058
|
+
};
|
|
2059
|
+
|
|
2060
|
+
if (options && options.signal) {
|
|
2061
|
+
addAbortSignal(options.signal, d);
|
|
2062
|
+
}
|
|
2063
|
+
return d;
|
|
2064
|
+
}
|
|
2065
|
+
|
|
2066
|
+
// node's legacy Readable.prototype.wrap (readable.js:1503) -- the "ugly
|
|
2067
|
+
// unfortunate mess of history" that turns any event source into readable
|
|
2068
|
+
// data on this stream. It is honest machinery here because push() is node's
|
|
2069
|
+
// and the pull-side resume hook fires where node's maybeReadMore would call
|
|
2070
|
+
// _read.
|
|
2071
|
+
wrap(stream) {
|
|
2072
|
+
let paused = false;
|
|
2073
|
+
stream.on('data', (chunk) => {
|
|
2074
|
+
if (!this.push(chunk) && stream.pause) {
|
|
2075
|
+
paused = true;
|
|
2076
|
+
stream.pause();
|
|
2077
|
+
}
|
|
2078
|
+
});
|
|
2079
|
+
stream.on('end', () => {
|
|
2080
|
+
this.push(null);
|
|
2081
|
+
});
|
|
2082
|
+
stream.on('error', (err) => {
|
|
2083
|
+
if (!this.destroyed) {
|
|
2084
|
+
this.destroy(err);
|
|
2085
|
+
}
|
|
2086
|
+
});
|
|
2087
|
+
stream.on('close', () => {
|
|
2088
|
+
if (!this.destroyed) {
|
|
2089
|
+
this.destroy();
|
|
2090
|
+
}
|
|
2091
|
+
});
|
|
2092
|
+
stream.on('destroy', () => {
|
|
2093
|
+
if (!this.destroyed) {
|
|
2094
|
+
this.destroy();
|
|
2095
|
+
}
|
|
2096
|
+
});
|
|
2097
|
+
this._read = () => {
|
|
2098
|
+
if (paused && stream.resume) {
|
|
2099
|
+
paused = false;
|
|
2100
|
+
stream.resume();
|
|
2101
|
+
}
|
|
2102
|
+
};
|
|
2103
|
+
const streamKeys = Object.keys(stream);
|
|
2104
|
+
for (let j = 0; j < streamKeys.length; j++) {
|
|
2105
|
+
const i = streamKeys[j];
|
|
2106
|
+
if (this[i] === undefined && typeof stream[i] === 'function') {
|
|
2107
|
+
this[i] = stream[i].bind(stream);
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
return this;
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2113
|
+
// --- the async-iteration surface ---------------------------------------------
|
|
2114
|
+
// node's readable-iteration protocol and the fourteen helpers the public
|
|
2115
|
+
// `stream` module bolts onto the same prototype (lib/stream.js carries the
|
|
2116
|
+
// server-side port and the measurements): the transform family returns
|
|
2117
|
+
// `Readable.from(generator)` -- measured on a live node stream: `map(fn)`
|
|
2118
|
+
// yields an objectMode Readable -- and the promise family returns the
|
|
2119
|
+
// operation's promise. The iterator itself is a pull machine over read();
|
|
2120
|
+
// break over for-await destroys through the iterator's return() (the
|
|
2121
|
+
// AbortError into destroy(), RST CANCEL(8), events aborted, error:ABORT_ERR,
|
|
2122
|
+
// close), while a natural end leaves the stream alive -- node reaches
|
|
2123
|
+
// finalize() in both cases, but Http2Stream sets autoDestroy false
|
|
2124
|
+
// (core.js:2132), so only the still-active return() branch satisfies
|
|
2125
|
+
// finalize's second clause.
|
|
2126
|
+
[Symbol.asyncIterator]() {
|
|
2127
|
+
return createStreamIterator(this, undefined);
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
iterator(options) {
|
|
2131
|
+
if (options !== undefined) {
|
|
2132
|
+
validateObjectArg(options, 'options');
|
|
2133
|
+
}
|
|
2134
|
+
return createStreamIterator(this, options);
|
|
2135
|
+
}
|
|
2136
|
+
|
|
2137
|
+
map(fn, options) {
|
|
2138
|
+
return Readable.from(opMap(this, fn, options));
|
|
2139
|
+
}
|
|
2140
|
+
|
|
2141
|
+
filter(fn, options) {
|
|
2142
|
+
return Readable.from(opFilter(this, fn, options));
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
flatMap(fn, options) {
|
|
2146
|
+
return Readable.from(opFlatMap(this, fn, options));
|
|
2147
|
+
}
|
|
2148
|
+
|
|
2149
|
+
drop(number, options) {
|
|
2150
|
+
return Readable.from(opDrop(this, number, options));
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
take(number, options) {
|
|
2154
|
+
return Readable.from(opTake(this, number, options));
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
reduce(reducer, initialValue, options) {
|
|
2158
|
+
// node's reduce tests `arguments.length > 1` inside the operator to tell
|
|
2159
|
+
// "no initial value" from "initial value undefined"; the arity has to be
|
|
2160
|
+
// read at the call, which is here.
|
|
2161
|
+
return opReduce(this, arguments.length > 1, reducer, initialValue, options);
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
forEach(fn, options) {
|
|
2165
|
+
return opForEach(this, fn, options);
|
|
2166
|
+
}
|
|
2167
|
+
|
|
2168
|
+
toArray(options) {
|
|
2169
|
+
return opToArray(this, options);
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
some(fn, options) {
|
|
2173
|
+
return opSome(this, fn, options);
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
every(fn, options) {
|
|
2177
|
+
return opEvery(this, fn, options);
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
find(fn, options) {
|
|
2181
|
+
return opFind(this, fn, options);
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
// node maps a per-stream timeout onto the connection's idle timeout.
|
|
2185
|
+
setTimeout(ms, cb) {
|
|
2186
|
+
this.session.setTimeout(ms, cb);
|
|
2187
|
+
return this;
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
// RFC 9218 deprecates the priority scheme; node still accepts the call but
|
|
2191
|
+
// refuses a destroyed stream (internal/http2/core.js:2617). Returning true
|
|
2192
|
+
// without reordering anything is the honest v1 answer. The DEP0194 warning
|
|
2193
|
+
// is attached below the class, byte-identical to node's (typo included).
|
|
2194
|
+
priority() {
|
|
2195
|
+
if (this._destroyFlag) {
|
|
2196
|
+
throw errorOf('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed');
|
|
2197
|
+
}
|
|
2198
|
+
return true;
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
// --- the state node exposes through its machinery ----------------------------
|
|
2202
|
+
// Measured member-by-member off a live node client stream (v26.12.1: a fresh
|
|
2203
|
+
// GET request reads readableFlowing null, isPaused false, writable false with
|
|
2204
|
+
// writableEnded true because the request HEADERS carried END_STREAM, both
|
|
2205
|
+
// high-water marks at the machinery defaults; after 'close': destroyed true,
|
|
2206
|
+
// closed true, readableEnded true, readableDidRead true). Every getter is
|
|
2207
|
+
// computed from state this file already keeps, plus the five lazily-written
|
|
2208
|
+
// flags (_rEof, _endEmitted, _endDelivered, _dataRead, _flowing) written at
|
|
2209
|
+
// the delivery/decision points above -- no per-request allocation beyond the
|
|
2210
|
+
// two stub state objects the session pokes.
|
|
2211
|
+
|
|
2212
|
+
get destroyed() {
|
|
2213
|
+
return this._destroyFlag;
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
// node's STREAM_FLAGS_CLOSED through Http2Stream.closed (core.js), set by
|
|
2217
|
+
// closeStream: false until the wire life ended with a code.
|
|
2218
|
+
get closed() {
|
|
2219
|
+
return this._closed;
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
// node: Http2Stream.pending (core.js:2264) reads `this[kID] === undefined` --
|
|
2223
|
+
// the id does not exist until requestOnConnect submits the request. h2lean
|
|
2224
|
+
// reserves the id in request() and queues the whole request behind the
|
|
2225
|
+
// connecting session or a concurrency slot, so the same question is answered
|
|
2226
|
+
// from _opened: true until _openStream() frames the HEADERS, false afterwards,
|
|
2227
|
+
// and false stays through close the way node's assigned id stays assigned
|
|
2228
|
+
// (a queued stream destroyed before it opens reads true on both stacks).
|
|
2229
|
+
get pending() {
|
|
2230
|
+
return !this._opened;
|
|
2231
|
+
}
|
|
2232
|
+
|
|
2233
|
+
// node: Http2Stream.sentInfoHeaders (core.js:2260) reads kInfoHeaders, which
|
|
2234
|
+
// only the SERVER additionalHeaders() populates (core.js:3284); the client
|
|
2235
|
+
// receive path for 1xx blocks writes nothing there, so a client stream reads
|
|
2236
|
+
// undefined for its whole life -- measured on a live node v26.12.1 client
|
|
2237
|
+
// after a received 103 and a 100. The getter exists so
|
|
2238
|
+
// `'sentInfoHeaders' in req` sees the same surface; the received blocks
|
|
2239
|
+
// themselves surface as the 'headers'/'continue' events and the
|
|
2240
|
+
// additionalHeaders field.
|
|
2241
|
+
get sentInfoHeaders() {
|
|
2242
|
+
return undefined;
|
|
2243
|
+
}
|
|
2244
|
+
|
|
2245
|
+
// node: Http2Stream.state (core.js:2304) answers {} when destroyed or before the
|
|
2246
|
+
// stream has an id, else the six getStreamState fields (internal util). Measured on
|
|
2247
|
+
// a live node v26.12.1 client: weight 16, sumDependencyWeight 0, remoteClose 0 in
|
|
2248
|
+
// every phase reachable before teardown (an inbound RST destroys first), localClose
|
|
2249
|
+
// 1 once the request ended, and localWindowSize = the peer's initial window minus
|
|
2250
|
+
// consumed-but-not-yet-credited octets (16 KiB consumed reads 49151; node credits
|
|
2251
|
+
// at its own thresholds, so only the subtraction is pinned, not a post-credit
|
|
2252
|
+
// value). The subtraction reads _recvUsed, which books octets at DATA ARRIVAL,
|
|
2253
|
+
// not at consumption -- node consumes at delivery -- so an unread body already
|
|
2254
|
+
// counts here; the header list carries the divergence and its reason.
|
|
2255
|
+
// Divergences in the header list: node's one-turn IDLE shape (state 1, weight 0,
|
|
2256
|
+
// window 0) between request() and the first flush does not exist here -- the
|
|
2257
|
+
// HEADERS frame is built in the call; a request queued behind a connecting
|
|
2258
|
+
// session reads {} here like node's pending shape, because _opened is still
|
|
2259
|
+
// false (the id itself is reserved at request() time -- node's queued stream
|
|
2260
|
+
// has no id yet, the one divergence the pending getter below cannot cover).
|
|
2261
|
+
get state() {
|
|
2262
|
+
if (this._destroyFlag || this._opened !== true) {
|
|
2263
|
+
return {};
|
|
2264
|
+
}
|
|
2265
|
+
return {
|
|
2266
|
+
state: this._endQueued ? ST_HALF_CLOSED_LOCAL : ST_OPEN,
|
|
2267
|
+
weight: 16,
|
|
2268
|
+
sumDependencyWeight: 0,
|
|
2269
|
+
localClose: this._endQueued ? 1 : 0,
|
|
2270
|
+
remoteClose: 0,
|
|
2271
|
+
localWindowSize: this.session.localSettings.initialWindowSize - this._recvUsed
|
|
2272
|
+
};
|
|
2273
|
+
}
|
|
2274
|
+
|
|
2275
|
+
// The flag that names a HEAD request is set only on the server side of node's
|
|
2276
|
+
// stream construction (core.js:416/3046); a client stream never carries it, and a
|
|
2277
|
+
// HEAD request through request() reads false (measured v26.12.1).
|
|
2278
|
+
get headRequest() {
|
|
2279
|
+
return false;
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
// node: `!r.destroyed && !r.errorEmitted && !r.endEmitted` -- readable-side,
|
|
2283
|
+
// not half-open-ness: a stream that has emitted 'end' reads false with both
|
|
2284
|
+
// halves alive.
|
|
2285
|
+
get readable() {
|
|
2286
|
+
return this._destroyFlag !== true && this._readableState.errorEmitted !== true &&
|
|
2287
|
+
this._endDelivered !== true;
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
// node's readableDidRead is the dataEmitted bit (readable.js:1857): true once any
|
|
2291
|
+
// delivery of octets happened -- through a 'data' event OR a successful read(),
|
|
2292
|
+
// because node's read() itself emits 'data' on every chunk it hands back.
|
|
2293
|
+
get readableDidRead() {
|
|
2294
|
+
return this._dataRead === true;
|
|
2295
|
+
}
|
|
2296
|
+
|
|
2297
|
+
// The 'end' event has been emitted -- node's endEmitted bit flips at the
|
|
2298
|
+
// emission, not at the scheduling, hence the delivered flag.
|
|
2299
|
+
get readableEnded() {
|
|
2300
|
+
return this._endDelivered === true;
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
// node's tri-state (readable.js:1901): null until anything ever touches the
|
|
2304
|
+
// readable side, true while it flows, false while paused.
|
|
2305
|
+
get readableFlowing() {
|
|
2306
|
+
return this._flowing === undefined ? null : this._flowing;
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
// node: `readable !== false && (destroyed || errored) && !endEmitted`.
|
|
2310
|
+
get readableAborted() {
|
|
2311
|
+
return (this._destroyFlag || this._errored !== undefined) &&
|
|
2312
|
+
this._endDelivered !== true;
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
get readableEncoding() {
|
|
2316
|
+
return this._encoding;
|
|
2317
|
+
}
|
|
2318
|
+
|
|
2319
|
+
get readableHighWaterMark() {
|
|
2320
|
+
return READABLE_HWM;
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2323
|
+
get readableObjectMode() {
|
|
2324
|
+
return false;
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
get readableLength() {
|
|
2328
|
+
const rbuf = this._rbuf;
|
|
2329
|
+
if (rbuf === null) {
|
|
2330
|
+
return 0;
|
|
2331
|
+
}
|
|
2332
|
+
let n = 0;
|
|
2333
|
+
for (let i = 0; i < rbuf.length; i++) {
|
|
2334
|
+
n += rbuf[i].length;
|
|
2335
|
+
}
|
|
2336
|
+
return n;
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
// node returns the buffer list itself; h2lean hands out a copy -- the live
|
|
2340
|
+
// array would let the caller splice chunks out from under the delivery queue.
|
|
2341
|
+
get readableBuffer() {
|
|
2342
|
+
return this._rbuf === null ? [] : this._rbuf.slice();
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
// node: `(kEnding | kEnded | kDestroyed | kErrored) === 0`.
|
|
2346
|
+
get writable() {
|
|
2347
|
+
return this._destroyFlag !== true && this._endQueued !== true &&
|
|
2348
|
+
this._errored === undefined;
|
|
2349
|
+
}
|
|
2350
|
+
|
|
2351
|
+
get writableEnded() {
|
|
2352
|
+
return this._endQueued;
|
|
2353
|
+
}
|
|
2354
|
+
|
|
2355
|
+
get writableObjectMode() {
|
|
2356
|
+
return false;
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
// The octets still queued behind the one chunk in flight; the in-flight chunk
|
|
2360
|
+
// lives in the session's `_pending` slot (write-through, header above).
|
|
2361
|
+
get writableLength() {
|
|
2362
|
+
const wq = this._wq;
|
|
2363
|
+
if (wq === null) {
|
|
2364
|
+
return 0;
|
|
2365
|
+
}
|
|
2366
|
+
let n = 0;
|
|
2367
|
+
for (let i = 0; i < wq.length; i++) {
|
|
2368
|
+
n += wq[i][0].length;
|
|
2369
|
+
}
|
|
2370
|
+
return n;
|
|
2371
|
+
}
|
|
2372
|
+
|
|
2373
|
+
get writableBuffer() {
|
|
2374
|
+
if (this._wq === null) {
|
|
2375
|
+
return [];
|
|
2376
|
+
}
|
|
2377
|
+
const out = new Array(this._wq.length);
|
|
2378
|
+
for (let i = 0; i < this._wq.length; i++) {
|
|
2379
|
+
out[i] = this._wq[i][0];
|
|
2380
|
+
}
|
|
2381
|
+
return out;
|
|
2382
|
+
}
|
|
2383
|
+
|
|
2384
|
+
// node: `kNeedDrain` set and neither destroyed nor ending.
|
|
2385
|
+
get writableNeedDrain() {
|
|
2386
|
+
return this._needDrain === true && this._destroyFlag !== true &&
|
|
2387
|
+
this._endQueued !== true;
|
|
2388
|
+
}
|
|
2389
|
+
|
|
2390
|
+
// core.js:2215: `writeQueueSize + writableLength`. The nghttp2 queue this
|
|
2391
|
+
// file has no equivalent for (write-through, header above), so the second
|
|
2392
|
+
// term is the whole number.
|
|
2393
|
+
get bufferSize() {
|
|
2394
|
+
return this.writableLength;
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
get writableCorked() {
|
|
2398
|
+
return this._corked;
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
get writableHighWaterMark() {
|
|
2402
|
+
return WRITABLE_HWM;
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
get writableFinished() {
|
|
2406
|
+
return this._writableState.finished;
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
// node keeps one `errored` per half; the public getter is the READABLE one
|
|
2410
|
+
// (readable.js:1939). It holds the Error an 'error' is delivering, set when
|
|
2411
|
+
// destroy runs with an error (destroy.js checkError), not when the event fires.
|
|
2412
|
+
get errored() {
|
|
2413
|
+
return this._errored === undefined ? null : this._errored;
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
// The session decoded a response (or informational / trailer) block for us.
|
|
2417
|
+
// The field list arrives unbuilt: the null-prototype object with `:status`
|
|
2418
|
+
// coerced to a number and `set-cookie` as an array is what the session's
|
|
2419
|
+
// _toObject builds, and it is built here at the first read -- an event
|
|
2420
|
+
// argument when a listener exists, or the responseHeaders/trailers getter.
|
|
2421
|
+
// A body-only client (no 'response' subscription, no getter read) pays no
|
|
2422
|
+
// per-response object at all; the paired marginal is OPTIMIZATION.md item 48.
|
|
2423
|
+
_onHeaders(status, fields, fcount, sensitive, end, flags) {
|
|
2424
|
+
if (status !== undefined && status >= 100 && status < 200 && !this.responseEmitted) {
|
|
2425
|
+
// 1xx informational (including the 100 Continue that node surfaces as
|
|
2426
|
+
// 'continue'): built eagerly -- the blocks are rare and the one object
|
|
2427
|
+
// feeds the two events and the additionalHeaders field. The 100-Continue
|
|
2428
|
+
// translation node does is a 'headers' whose :status is 100.
|
|
2429
|
+
const headers = this.session._toObject(fields, fcount, sensitive);
|
|
2430
|
+
this.additionalHeaders.push(headers);
|
|
2431
|
+
// node's inbound emit carries the frame flags as the second argument
|
|
2432
|
+
// (`emit(stream, event, obj, flags, headers)`, internal/http2/core.js), which is
|
|
2433
|
+
// what the upstream sent-headers test asserts is a number.
|
|
2434
|
+
this.emit('headers', headers, flags);
|
|
2435
|
+
if (status === 100) {
|
|
2436
|
+
this.emit('continue', headers);
|
|
2437
|
+
}
|
|
2438
|
+
} else if (!this.responseEmitted) {
|
|
2439
|
+
this.responseEmitted = true;
|
|
2440
|
+
this.sentStatus = status;
|
|
2441
|
+
this._respFields = fields;
|
|
2442
|
+
this._respFcount = fcount;
|
|
2443
|
+
this._respSensitive = sensitive;
|
|
2444
|
+
// node passes the raw HEADERS frame flags as the second argument and a
|
|
2445
|
+
// rawHeaders array as the third (core.js: emit(stream, event, obj, flags,
|
|
2446
|
+
// headers)); the rawHeaders array is deliberately not built -- one flat
|
|
2447
|
+
// array per response is a cost no h2lean consumer has asked for.
|
|
2448
|
+
const event = this._pushed ? 'push' : 'response';
|
|
2449
|
+
if (this.listenerCount(event) > 0) {
|
|
2450
|
+
this.emit(event, this.responseHeaders, flags);
|
|
2451
|
+
}
|
|
2452
|
+
} else {
|
|
2453
|
+
// A second final-status block after the response is the trailer block.
|
|
2454
|
+
this._trailerFields = fields;
|
|
2455
|
+
this._trailerFcount = fcount;
|
|
2456
|
+
this._trailerSensitive = sensitive;
|
|
2457
|
+
if (this.listenerCount('trailers') > 0) {
|
|
2458
|
+
this.emit('trailers', this.trailers);
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
if (end) {
|
|
2462
|
+
this._endRead();
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
432
2465
|
}
|
|
433
2466
|
|
|
2467
|
+
// node wraps the whole method in util.deprecate, so the warning fires once per
|
|
2468
|
+
// process with this exact message -- typo included, byte-identical to
|
|
2469
|
+
// internal/http2/core.js:2620 (DEP0194).
|
|
2470
|
+
ClientHttp2Stream.prototype.priority = deprecate(
|
|
2471
|
+
ClientHttp2Stream.prototype.priority,
|
|
2472
|
+
'http2Stream.priority is longer supported after priority signalling was deprecated in RFC 9113',
|
|
2473
|
+
'DEP0194');
|
|
2474
|
+
|
|
434
2475
|
module.exports = { ClientHttp2Stream, AbortError };
|