h2lean 0.4.2 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/lib/client-session.js +859 -73
- package/lib/client-stream.js +191 -15
- package/lib/client.js +33 -1
- package/lib/compat.js +76 -29
- package/lib/frames.js +23 -5
- package/lib/headers.js +190 -4
- package/lib/hpack.js +49 -2
- package/lib/server.js +9 -1
- package/lib/session.js +704 -49
- package/lib/stream.js +245 -20
- package/package.json +4 -3
package/lib/stream.js
CHANGED
|
@@ -2,6 +2,23 @@
|
|
|
2
2
|
//
|
|
3
3
|
// v1 deliberately implements the parts of the contract that can be honoured
|
|
4
4
|
// honestly, and omits the ones that cannot:
|
|
5
|
+
// * the base is `stream.Stream`, not Duplex and not a bare EventEmitter. node's Http2Stream
|
|
6
|
+
// extends Duplex (internal/http2/core.js:2128), but the part of that surface real code reaches
|
|
7
|
+
// for is pipe() on the readable half, and the Duplex machinery behind it -- two state objects
|
|
8
|
+
// plus the nextTick read loop -- is what OPTIMIZATION.md item 41 measured as ~+1.4 us/req
|
|
9
|
+
// against the EventEmitter arm. Stream.prototype.pipe supplies that one method without the
|
|
10
|
+
// cost. So `stream.pipe(dest)` works (test/pipe-parity.test.js). cork()/uncork() and the
|
|
11
|
+
// writableCorked/writableHighWaterMark getters are present too, but as a counting stub:
|
|
12
|
+
// node's Writable cork buffers write()/end() and defers the DATA until corked reaches 0
|
|
13
|
+
// (measured on v26.12.1: write('AAA') while corked leaves writableLength 3 and the peer
|
|
14
|
+
// receives nothing until the second uncork), while h2lean writes straight through -- its
|
|
15
|
+
// per-read socket batch already coalesces a synchronous burst, so only the cross-tick
|
|
16
|
+
// deferral differs. KNOWN DIVERGENCE, pinned in test/cork-parity.test.js. The rest of the
|
|
17
|
+
// Duplex surface -- unpipe(), read(), writev() and the readable/writable/writableLength
|
|
18
|
+
// state getters -- is still absent (AGENTS.md section 10). The one behavioural difference the
|
|
19
|
+
// bare base leaves is pipe teardown: legacy Stream.prototype.pipe ends the dest only on the
|
|
20
|
+
// source 'end', so a stream destroyed mid-pipe leaves the dest without the 'finish'/'unpipe'
|
|
21
|
+
// node's Duplex pipe emits. Measured and pinned in test/pipe-parity.test.js.
|
|
5
22
|
// * the read side releases flow-control credit as the application takes the
|
|
6
23
|
// bytes. With no reader the peer's window stays shut, so an unread body
|
|
7
24
|
// costs the sender instead of our heap; data that arrives before anyone is
|
|
@@ -22,15 +39,40 @@
|
|
|
22
39
|
// sends HEADERS{:status 200} before the DATA, as node does. An end() with no body at
|
|
23
40
|
// all is the one shape where h2lean answers (200, empty DATA/ES) and node says nothing
|
|
24
41
|
// -- measured, and recorded in AGENTS.md section 10.
|
|
42
|
+
// * a response STARTED and then destroyed in the same turn of the loop is the other:
|
|
43
|
+
// node submits the HEADERS to nghttp2 and nothing sends them before the destroy, so
|
|
44
|
+
// the teardown discards them and the peer sees only RST_STREAM. respond() here frames
|
|
45
|
+
// them into the session's staging buffer immediately and the batch leaves at the end
|
|
46
|
+
// of the read, so they reach the peer. The trigger is the turn, not the empty write:
|
|
47
|
+
// a non-empty write() flushes in the same turn on both stacks, and a destroy a
|
|
48
|
+
// setImmediate later agrees too. headersSent and rstCode are the same either way.
|
|
49
|
+
// Measured by tools/probe-zero-write.js, pinned in test/response-start.test.js, and
|
|
50
|
+
// the same outbound-batch decision as the refusal case in AGENTS.md section 10 --
|
|
51
|
+
// the staging buffer holds flat bytes with no per-stream handle to discard.
|
|
52
|
+
// * an outbound header block over the session's maxSendHeaderBlockLength is refused the
|
|
53
|
+
// way nghttp2 refuses it: 'frameError' on the stream, then a deferred close carrying
|
|
54
|
+
// FRAME_SIZE_ERROR and a session close behind it -- not a throw out of the send call.
|
|
55
|
+
// With no reader on the request half, node's own unread-request auto-close reaches the
|
|
56
|
+
// stream first and reports rstCode 0 where h2lean reports 6: the two stacks schedule
|
|
57
|
+
// the same pair in opposite orders, node's writable 'finish' before the frame pump
|
|
58
|
+
// refuses the block and h2lean's half-close after it
|
|
59
|
+
// (test/upstream/exceeds-server-trailer-size.test.js).
|
|
25
60
|
'use strict';
|
|
26
61
|
|
|
27
|
-
const {
|
|
62
|
+
const { Stream, Writable } = require('node:stream');
|
|
28
63
|
const { deprecate } = require('node:util');
|
|
29
64
|
const frames = require('./frames');
|
|
30
65
|
const hdr = require('./headers');
|
|
31
66
|
|
|
32
67
|
const EMPTY = Buffer.alloc(0);
|
|
33
68
|
|
|
69
|
+
// node's Http2Stream sets no highWaterMark (internal/http2/core.js constructor passes only
|
|
70
|
+
// allowHalfOpen/decodeStrings/autoDestroy), so its writableHighWaterMark is the default
|
|
71
|
+
// Writable one -- 16384 on node 20, 65536 from 22 on. Read it off node itself once rather
|
|
72
|
+
// than hardcode a per-major constant that could drift; measured equal to a live node
|
|
73
|
+
// stream's writableHighWaterMark on v26.12.1.
|
|
74
|
+
const WRITABLE_HWM = new Writable().writableHighWaterMark;
|
|
75
|
+
|
|
34
76
|
// process.nextTick(fn, args) instead of an arrow closure per event: the arrow
|
|
35
77
|
// allocated a new function object for every request, and in the compat layer
|
|
36
78
|
// that showed up as GC time.
|
|
@@ -47,6 +89,30 @@ function emitErrorCloseNT(target, err) {
|
|
|
47
89
|
target.emit('close');
|
|
48
90
|
}
|
|
49
91
|
|
|
92
|
+
// node's onStreamTrailers (internal/http2/core.js), the callback nghttp2 runs when the
|
|
93
|
+
// writable half of a stream that asked for trailers finishes. Two parts of it are easy
|
|
94
|
+
// to lose: `trailersReady` is set before the destroyed/closed check (so a stream that is
|
|
95
|
+
// already gone is still marked ready), and when nothing is listening for the event node
|
|
96
|
+
// does not leave the deferred END_STREAM outstanding -- it sends an empty trailer block
|
|
97
|
+
// itself and closes. An empty block is not a header section on the wire: it is the withheld
|
|
98
|
+
// END_STREAM on a zero-length DATA, see sendTrailers() below. Without that last part a
|
|
99
|
+
// `respond({waitForTrailers:true})` followed by `end()` with no 'wantTrailers' handler hangs
|
|
100
|
+
// the stream forever, which is exactly what upstream's
|
|
101
|
+
// test-http2-no-wanttrailers-listener.js tests.
|
|
102
|
+
//
|
|
103
|
+
// h2lean's compat layer is not a subscriber (see emit() below), so `false` from emit()
|
|
104
|
+
// does not by itself mean nobody answered: the compat handler calls sendTrailers() from
|
|
105
|
+
// inside the dispatch, and the sentTrailers check is what sees that it did.
|
|
106
|
+
function wantTrailersNT(stream) {
|
|
107
|
+
stream._trailersReady = true;
|
|
108
|
+
if (stream._destroyed || stream.closed) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (!stream.emit('wantTrailers') && !stream.sentTrailers) {
|
|
112
|
+
stream.sendTrailers({});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
50
116
|
// 'end' and 'close' for a stream whose two halves finished in the same turn.
|
|
51
117
|
// They were already going to run in the same tick drain, so folding them into
|
|
52
118
|
// one callback only removes one TickObject per request.
|
|
@@ -73,13 +139,19 @@ const {
|
|
|
73
139
|
INTERNAL_ERROR
|
|
74
140
|
} = frames;
|
|
75
141
|
|
|
76
|
-
class Http2Stream extends
|
|
142
|
+
class Http2Stream extends Stream {
|
|
77
143
|
constructor(session, id) {
|
|
78
144
|
super();
|
|
79
145
|
this.id = id;
|
|
80
146
|
this.session = session;
|
|
81
147
|
this.rstCode = 0;
|
|
82
|
-
|
|
148
|
+
// What respond() put on the wire, for the `sentHeaders` getter below: the caller's map
|
|
149
|
+
// plus the two values the encoder resolved. node keeps the same three pieces
|
|
150
|
+
// (kRawHeaders / kSentHeaders, internal/http2/core.js:2225).
|
|
151
|
+
this._sentSrc = undefined;
|
|
152
|
+
this._sentStatus = 0;
|
|
153
|
+
this._sentDate = undefined;
|
|
154
|
+
this._sentView = undefined;
|
|
83
155
|
this.sentInfoHeaders = [];
|
|
84
156
|
this.sentTrailers = undefined;
|
|
85
157
|
this.headersSent = false;
|
|
@@ -105,9 +177,24 @@ class Http2Stream extends EventEmitter {
|
|
|
105
177
|
this._endPending = false; // END_STREAM arrived with no reader
|
|
106
178
|
this._consumeState = 0; // 0 no reader, 1 delivering, 2 reader is full
|
|
107
179
|
this._endQueued = false;
|
|
180
|
+
this._corked = 0; // Writable cork count -- a stub, not a write buffer (see cork()/uncork())
|
|
108
181
|
this._gracefulClose = false; // both halves closed on their own terms (_closeNow)
|
|
109
182
|
this._waitForTrailers = false;
|
|
183
|
+
// node's `state.trailersReady` (internal/http2/core.js onStreamTrailers): the
|
|
184
|
+
// permission to call sendTrailers(), granted by the 'wantTrailers' callback and
|
|
185
|
+
// NOT by the waitForTrailers option. node keeps the two separate -- calling
|
|
186
|
+
// sendTrailers() after respond({waitForTrailers:true}) but before 'wantTrailers'
|
|
187
|
+
// has fired throws ERR_HTTP2_TRAILERS_NOT_READY, measured live.
|
|
188
|
+
this._trailersReady = false;
|
|
110
189
|
this._pendingTrailers = null; // trailer block waiting behind a queued body
|
|
190
|
+
this._pendingTrailersBound = 0; // ...and its outbound estimate, checked when it drains
|
|
191
|
+
// A refused outbound HEADERS (session._refuseBlock) left the wire a step behind the
|
|
192
|
+
// application: no response HEADERS means no DATA may follow, and no END_STREAM means
|
|
193
|
+
// the stream must not close gracefully. An informational block sets neither.
|
|
194
|
+
// A refused PUSH_PROMISE sets it on the PROMISED stream at construction, before the
|
|
195
|
+
// pushStream callback runs: nghttp2 has already closed that stream, so the respond()
|
|
196
|
+
// and end() the callback makes submit into nothing (session._refusePush).
|
|
197
|
+
this._blockRefused = false;
|
|
111
198
|
this._destroyed = false;
|
|
112
199
|
// The compat layer's handler for this stream's events, or null when the
|
|
113
200
|
// application uses the stream API. See emit() below.
|
|
@@ -127,6 +214,23 @@ class Http2Stream extends EventEmitter {
|
|
|
127
214
|
return this._destroyed ? {} : this._state;
|
|
128
215
|
}
|
|
129
216
|
|
|
217
|
+
// node's sentHeaders is the message that went out, not the object respond() was given:
|
|
218
|
+
// a null-prototype map carrying the coerced ':status' number and the `date` the stack
|
|
219
|
+
// generated (prepareResponseHeadersObject, internal/http2/core.js:2677-2708). Built on
|
|
220
|
+
// the first read and kept, so repeated reads give one object the way node's memoized
|
|
221
|
+
// kSentHeaders does, and a response nobody inspects pays no copy. A pushed stream's map
|
|
222
|
+
// is a request, not a response, and pushStream() fills it directly.
|
|
223
|
+
get sentHeaders() {
|
|
224
|
+
if (this._sentView !== undefined) {
|
|
225
|
+
return this._sentView;
|
|
226
|
+
}
|
|
227
|
+
if (this._sentSrc === undefined) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
this._sentView = hdr.responseView(this._sentSrc, this._sentStatus, this._sentDate);
|
|
231
|
+
return this._sentView;
|
|
232
|
+
}
|
|
233
|
+
|
|
130
234
|
respond(headers, options) {
|
|
131
235
|
// node guards the stream's life first (internal/http2/core.js respond():
|
|
132
236
|
// `if (this.destroyed || this.closed) throw new ERR_HTTP2_INVALID_STREAM()`),
|
|
@@ -163,7 +267,12 @@ class Http2Stream extends EventEmitter {
|
|
|
163
267
|
const sendDate = opts.sendDate == null ? true : !!opts.sendDate;
|
|
164
268
|
const block = hdr.block(headers, opts.sensitiveHeaders,
|
|
165
269
|
{ sendDate }, this.session);
|
|
166
|
-
|
|
270
|
+
// Read the encoder's result slots before anything else can build a block: they are
|
|
271
|
+
// what `sentHeaders` reports, so they have to be this response's, not a later one's.
|
|
272
|
+
this._sentSrc = headers;
|
|
273
|
+
this._sentStatus = hdr.sent.status;
|
|
274
|
+
this._sentDate = hdr.sent.date;
|
|
275
|
+
const bound = hdr.sent.bound;
|
|
167
276
|
this.headersSent = true;
|
|
168
277
|
this.pending = false;
|
|
169
278
|
this._waitForTrailers = !!opts.waitForTrailers;
|
|
@@ -171,7 +280,14 @@ class Http2Stream extends EventEmitter {
|
|
|
171
280
|
// side only closes when the application says so -- except for the two shapes
|
|
172
281
|
// above, where node's own end() has already run or runs inside respond().
|
|
173
282
|
const empty = opts.endStream === true || head || this._endQueued;
|
|
174
|
-
this.session.sendHeaders(this.id, block, empty)
|
|
283
|
+
if (this._blockRefused || this.session.sendHeaders(this.id, block, empty, bound)) {
|
|
284
|
+
// The response HEADERS never reached the wire, so the stream never opened for a
|
|
285
|
+
// body: nghttp2's response-DATA predicate requires NGHTTP2_STREAM_OPENED
|
|
286
|
+
// (nghttp2_session_predicate_data_send, nghttp2_session.c), which only a sent
|
|
287
|
+
// response HEADERS reaches. Measured on v26.12.1: the wire carries RST_STREAM(6)
|
|
288
|
+
// and the GOAWAYs -- no HEADERS and no DATA, even for end('hello').
|
|
289
|
+
this._blockRefused = true;
|
|
290
|
+
}
|
|
175
291
|
if (empty) {
|
|
176
292
|
// This response has already carried END_STREAM, and nothing may follow it on this
|
|
177
293
|
// stream: RFC 9113 6.1 puts a trailing HEADERS *before* the close, and node shows
|
|
@@ -283,26 +399,88 @@ class Http2Stream extends EventEmitter {
|
|
|
283
399
|
return this;
|
|
284
400
|
}
|
|
285
401
|
|
|
402
|
+
// Writable-side corking, as a counting stub. node's Http2Stream inherits Writable.cork,
|
|
403
|
+
// which buffers write()/end() and defers the DATA to the wire until corked reaches 0;
|
|
404
|
+
// h2lean's write() sends straight through (the per-read socket batch already coalesces a
|
|
405
|
+
// synchronous burst). Only the count is observable here -- the cross-tick deferral is a
|
|
406
|
+
// KNOWN DIVERGENCE written up in the header and pinned in test/cork-parity.test.js.
|
|
407
|
+
// cork()/uncork() return undefined like Writable's, and uncork() floors at 0 the way
|
|
408
|
+
// Writable's does (internal/streams/writable.js: `if (state.corked) state.corked--`).
|
|
409
|
+
cork() {
|
|
410
|
+
this._corked++;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
uncork() {
|
|
414
|
+
if (this._corked > 0) {
|
|
415
|
+
this._corked--;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
get writableCorked() {
|
|
420
|
+
return this._corked;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
get writableHighWaterMark() {
|
|
424
|
+
return WRITABLE_HWM;
|
|
425
|
+
}
|
|
426
|
+
|
|
286
427
|
sendTrailers(trailers) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
428
|
+
// node's three guards, in node's order, and all three as synchronous throws --
|
|
429
|
+
// not emitted errors (internal/http2/core.js Http2Stream.sendTrailers). Measured
|
|
430
|
+
// live: each of the three messages below is what a node stream raises in the
|
|
431
|
+
// matching case, and the case that decides most is the third -- the permission is
|
|
432
|
+
// `state.trailersReady`, granted by the 'wantTrailers' callback, so a call that
|
|
433
|
+
// arrives after respond({waitForTrailers:true}) but before that callback throws
|
|
434
|
+
// rather than quietly putting a trailer block on the wire.
|
|
435
|
+
if (this._destroyed || this.closed) {
|
|
436
|
+
throw errorOf('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed');
|
|
437
|
+
}
|
|
438
|
+
if (this.sentTrailers) {
|
|
439
|
+
throw errorOf('ERR_HTTP2_TRAILERS_ALREADY_SENT',
|
|
440
|
+
'Trailing headers have already been sent');
|
|
441
|
+
}
|
|
442
|
+
if (!this._trailersReady) {
|
|
443
|
+
throw errorOf('ERR_HTTP2_TRAILERS_NOT_READY',
|
|
444
|
+
'Trailing headers cannot be sent until after the wantTrailers event is emitted');
|
|
291
445
|
}
|
|
292
446
|
const block = hdr.block(trailers, undefined, { trailer: true }, this.session);
|
|
293
|
-
|
|
447
|
+
const bound = hdr.sent.bound;
|
|
448
|
+
// node records a null-prototype copy, not the caller's map (`ObjectAssign({
|
|
449
|
+
// __proto__: null }, headers)`, internal/http2/core.js sendTrailers -- the shared
|
|
450
|
+
// base method, so the client stream records the same shape).
|
|
451
|
+
this.sentTrailers = Object.assign({ __proto__: null }, trailers);
|
|
294
452
|
// the trailers are out: the flag that permitted this call must not keep asking for
|
|
295
453
|
// wantTrailers, whatever happens to the bytes below
|
|
296
454
|
this._waitForTrailers = false;
|
|
455
|
+
// An empty trailer block does not become a HEADERS frame. nghttp2 has nothing to
|
|
456
|
+
// put in a fieldless trailing header section, so node lets the END_STREAM the body
|
|
457
|
+
// withheld land on a zero-length DATA instead -- measured: HEADERS/EH, DATA len4
|
|
458
|
+
// flags 0x0, DATA len0 flags 0x1. This is the shape of *every* compat response,
|
|
459
|
+
// because node's (req,res) layer always asks for trailers (internal/http2/compat.js
|
|
460
|
+
// kBeginSend passes waitForTrailers: true) and answers with the empty trailer map
|
|
461
|
+
// when the application added none.
|
|
462
|
+
const empty = block.length === 0;
|
|
297
463
|
if (this._queued !== null) {
|
|
298
464
|
// The body is still waiting for credit or for the socket, and this block has to
|
|
299
465
|
// follow it: END_STREAM rides on the trailers, so a HEADERS sent now would outrun
|
|
300
|
-
// DATA that has not gone out. Stash a copy (the block is a view into shared scratch
|
|
301
|
-
// and let the session put it on the wire when the
|
|
302
|
-
|
|
466
|
+
// DATA that has not gone out. Stash a copy (the block is a view into shared scratch;
|
|
467
|
+
// the zero-length marker needs none) and let the session put it on the wire when the
|
|
468
|
+
// queue drains.
|
|
469
|
+
this._pendingTrailers = empty ? EMPTY : Buffer.from(block);
|
|
470
|
+
this._pendingTrailersBound = bound;
|
|
303
471
|
return this;
|
|
304
472
|
}
|
|
305
|
-
|
|
473
|
+
if (empty) {
|
|
474
|
+
// sendData() frames the zero-length DATA and ends the local half with it.
|
|
475
|
+
this.session.sendData(this, EMPTY, true);
|
|
476
|
+
return this;
|
|
477
|
+
}
|
|
478
|
+
if (this.session.sendHeaders(this.id, block, true, bound)) {
|
|
479
|
+
// The END_STREAM this block carried never went out, so the stream has not finished
|
|
480
|
+
// on the wire and must not close on its own terms ahead of the deferred
|
|
481
|
+
// FRAME_SIZE_ERROR close (session._refuseBlock).
|
|
482
|
+
this._blockRefused = true;
|
|
483
|
+
}
|
|
306
484
|
// the local half closes for good now: the bytes that owed it are on the wire
|
|
307
485
|
this._endLocalSide();
|
|
308
486
|
}
|
|
@@ -331,8 +509,9 @@ class Http2Stream extends EventEmitter {
|
|
|
331
509
|
// `{ info: true }` is node's separate rule for these blocks: :status must be 100..199
|
|
332
510
|
// (101 has its own error), there is no default status and no generated date.
|
|
333
511
|
const block = hdr.block(headers, undefined, { info: true }, this.session);
|
|
512
|
+
const bound = hdr.sent.bound;
|
|
334
513
|
this.sentInfoHeaders.push(headers);
|
|
335
|
-
this.session.sendHeaders(this.id, block, false);
|
|
514
|
+
this.session.sendHeaders(this.id, block, false, bound);
|
|
336
515
|
return this;
|
|
337
516
|
}
|
|
338
517
|
|
|
@@ -426,13 +605,24 @@ class Http2Stream extends EventEmitter {
|
|
|
426
605
|
// Validation errors are synchronous in node (buildNgHeaderString throws before
|
|
427
606
|
// the frame is submitted). The id was already reserved; it stays consumed.
|
|
428
607
|
const block = hdr.pushBlock(h, options.sensitiveHeaders, this.session);
|
|
429
|
-
this.session.sendPushPromise(this.id, promisedId, block);
|
|
608
|
+
const refused = this.session.sendPushPromise(this.id, promisedId, block, hdr.sent.bound);
|
|
430
609
|
|
|
431
610
|
// Create the pushed stream and register it. The readable side is pre-ended:
|
|
432
611
|
// remoteClose=1 means _endLocalSide() takes the graceful close path (no RST),
|
|
433
612
|
// and no inbound credit is ever charged for a request body that does not exist.
|
|
434
613
|
const stream = new Http2Stream(this.session, promisedId);
|
|
435
|
-
|
|
614
|
+
if (refused) {
|
|
615
|
+
// nghttp2 closed the promised stream at prep time, before the callback below runs,
|
|
616
|
+
// so the respond()/end() the application makes there submit into a closed stream
|
|
617
|
+
// and reach no wire. The stream object is still handed over with no error -- node
|
|
618
|
+
// creates it before the send loop too -- and its INTERNAL_ERROR close is the
|
|
619
|
+
// deferred one session._refusePush schedules.
|
|
620
|
+
stream._blockRefused = true;
|
|
621
|
+
}
|
|
622
|
+
// The pushed stream's map is a request: node fills kSentHeaders with the resolved
|
|
623
|
+
// request map at pushStream() time (core.js:3038), so it is already final here and
|
|
624
|
+
// the getter above hands it back unchanged.
|
|
625
|
+
stream._sentView = h;
|
|
436
626
|
stream._requestHeaders = h;
|
|
437
627
|
this.session._streams.set(promisedId, stream);
|
|
438
628
|
this.session._liveStreams++;
|
|
@@ -498,7 +688,19 @@ class Http2Stream extends EventEmitter {
|
|
|
498
688
|
// the readable side is done: 'end' after the last 'data', like a Readable.
|
|
499
689
|
// No auto-destroy: the application still has to answer. node keeps the
|
|
500
690
|
// writable side of an unanswered request open as well.
|
|
501
|
-
if (this._state.localClose === 1) {
|
|
691
|
+
if (this._state.localClose === 1 && !this._waitForTrailers) {
|
|
692
|
+
// The local half is only *finished* once END_STREAM left on the wire. A stream that
|
|
693
|
+
// still owes a trailer block owes that END_STREAM too -- the body DATA was framed
|
|
694
|
+
// without it (sendTrailers() puts the close on the trailer block, or on an empty DATA
|
|
695
|
+
// when the block has no fields) -- so closing here would leave the response
|
|
696
|
+
// unanswered for the peer and drop the owed frame. node's rule is the same and lives
|
|
697
|
+
// in the writable shutdown, not in the read path: a stream with
|
|
698
|
+
// STREAM_FLAGS_HAS_TRAILERS set is not shut down early
|
|
699
|
+
// (internal/http2/core.js, `Trailers are pending, so the writable side cannot be
|
|
700
|
+
// shut down early`). Measured: a compat response that ends with no body against an
|
|
701
|
+
// unread request put HEADERS then a zero-length DATA/END_STREAM on node's wire, with
|
|
702
|
+
// the stream close after both; ending here sent the HEADERS only and the client
|
|
703
|
+
// waited for an answer that never came.
|
|
502
704
|
this._closeNow();
|
|
503
705
|
process.nextTick(emitEndCloseNT, this);
|
|
504
706
|
return;
|
|
@@ -519,6 +721,14 @@ class Http2Stream extends EventEmitter {
|
|
|
519
721
|
const first = this._state.localClose !== 1;
|
|
520
722
|
this._state.localClose = 1;
|
|
521
723
|
this._endQueued = false;
|
|
724
|
+
if (this._blockRefused) {
|
|
725
|
+
// The half-close is the application's, and node records it whether or not the
|
|
726
|
+
// octets went out (`_writableState.ending` is what decides 'aborted'). The close
|
|
727
|
+
// machinery below must not run: it would finish the stream with NO_ERROR here,
|
|
728
|
+
// synchronously, ahead of the deferred FRAME_SIZE_ERROR close node's onFrameError
|
|
729
|
+
// schedules -- measured, node reports rstCode 6 for this shape and h2lean reported 0.
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
522
732
|
if (this._state.state === ST_OPEN) {
|
|
523
733
|
this._state.state = ST_HALF_CLOSED_LOCAL;
|
|
524
734
|
}
|
|
@@ -526,7 +736,7 @@ class Http2Stream extends EventEmitter {
|
|
|
526
736
|
// one shot: a later end() must not ask for trailers a second time. The flag
|
|
527
737
|
// itself stays set, because sendTrailers() is only allowed while it is set.
|
|
528
738
|
if (first) {
|
|
529
|
-
process.nextTick(
|
|
739
|
+
process.nextTick(wantTrailersNT, this);
|
|
530
740
|
}
|
|
531
741
|
return;
|
|
532
742
|
}
|
|
@@ -762,4 +972,19 @@ function errorOf(code, msg) {
|
|
|
762
972
|
return err;
|
|
763
973
|
}
|
|
764
974
|
|
|
765
|
-
|
|
975
|
+
// The error a stream reports when an RST_STREAM ends it, on either side. node's rule is
|
|
976
|
+
// one line (internal/http2/core.js:2543): `if (err == null && code !== NGHTTP2_NO_ERROR &&
|
|
977
|
+
// code !== NGHTTP2_CANCEL) err = new ERR_HTTP2_STREAM_ERROR(nameForErrorCode[code] || code)`.
|
|
978
|
+
// So a graceful reset (0) and a client abort (8) are NOT errors -- measured on a live node
|
|
979
|
+
// client and a live node server, both report only 'aborted'/'close' for those two codes --
|
|
980
|
+
// and every other code names itself in the message. h2lean used to answer every code with
|
|
981
|
+
// 'HTTP/2 stream error' on the client and with no error at all on the server.
|
|
982
|
+
function streamErrorOf(code) {
|
|
983
|
+
if (code === frames.NO_ERROR || code === frames.CANCEL) {
|
|
984
|
+
return null;
|
|
985
|
+
}
|
|
986
|
+
return errorOf('ERR_HTTP2_STREAM_ERROR',
|
|
987
|
+
`Stream closed with error code ${frames.ERROR_NAMES[code] || code}`);
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
module.exports = { Http2Stream, errorOf, streamErrorOf, emitNT };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "h2lean",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Pure-JavaScript HTTP/2 for Node.js — a drop-in node:http2 replacement (server and client, h2c and TLS) with zero dependencies",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"grpc"
|
|
28
28
|
],
|
|
29
29
|
"scripts": {
|
|
30
|
-
"test": "node --test --test-timeout=120000 test/*.test.js && node --test --test-timeout=120000 test/client/*.test.js",
|
|
31
|
-
"test:server": "node --test --test-timeout=120000 test/*.test.js",
|
|
30
|
+
"test": "node --test --test-timeout=120000 test/*.test.js test/upstream/*.test.js && node --test --test-timeout=120000 test/client/*.test.js",
|
|
31
|
+
"test:server": "node --test --test-timeout=120000 test/*.test.js test/upstream/*.test.js",
|
|
32
32
|
"test:client": "node --test --test-timeout=120000 test/client/*.test.js",
|
|
33
33
|
"lint": "eslint .",
|
|
34
34
|
"spec": "node tools/check-h2spec.js",
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
"eslint": "^10.14.0",
|
|
51
51
|
"fast-xml-parser": "^5.12.0",
|
|
52
52
|
"fastify": "^5.12.8",
|
|
53
|
+
"undici": "^8.13.0",
|
|
53
54
|
"uWebSockets.js": "github:uNetworking/UWebSockets.js#v20.70.0"
|
|
54
55
|
}
|
|
55
56
|
}
|