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/client-stream.js
CHANGED
|
@@ -15,6 +15,28 @@
|
|
|
15
15
|
'use strict';
|
|
16
16
|
|
|
17
17
|
const { Duplex } = require('node:stream');
|
|
18
|
+
const { NO_ERROR, CANCEL, INTERNAL_ERROR } = require('./frames');
|
|
19
|
+
const { requestView } = require('./headers');
|
|
20
|
+
const { streamErrorOf } = require('./stream');
|
|
21
|
+
|
|
22
|
+
// node's request() destroys an aborted stream with an instance of the internal
|
|
23
|
+
// AbortError class (core.js:1920 `stream.destroy(new AbortError(undefined, { cause:
|
|
24
|
+
// signal.reason }))`), and _destroy tests `err instanceof AbortError` (core.js:2516) --
|
|
25
|
+
// not err.name. The distinction is observable: measured on a live node client against a
|
|
26
|
+
// raw peer, the signal path frames RST_STREAM(8) and reports rstCode 8, while a plain
|
|
27
|
+
// Error an application merely *names* 'AbortError' frames RST_STREAM(2) and reports
|
|
28
|
+
// rstCode 2. A class is what keeps the two apart here; the message, name, code and the
|
|
29
|
+
// cause-only-when-defined rule are internal/errors AbortError's.
|
|
30
|
+
class AbortError extends Error {
|
|
31
|
+
constructor(reason) {
|
|
32
|
+
super('The operation was aborted');
|
|
33
|
+
this.name = 'AbortError';
|
|
34
|
+
this.code = 'ABORT_ERR';
|
|
35
|
+
if (reason !== undefined) {
|
|
36
|
+
this.cause = reason;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
18
40
|
|
|
19
41
|
// `sentHeaders`/`sentTrailers`/`responseHeaders` and the state flags node exposes
|
|
20
42
|
// through symbols are kept as plain fields here; they are not part of the
|
|
@@ -54,14 +76,30 @@ class ClientHttp2Stream extends Duplex {
|
|
|
54
76
|
this.endAfterHeaders = false;
|
|
55
77
|
// The readable half has seen END_STREAM (server finished the response body).
|
|
56
78
|
this._remoteEnded = false;
|
|
79
|
+
// node's STREAM_FLAGS_ABORTED, exposed through the `aborted` getter
|
|
80
|
+
// (internal/http2/core.js:2289). Set once, by the reset that found the writable
|
|
81
|
+
// half still open; closeStream guards the event on it.
|
|
82
|
+
this.aborted = false;
|
|
83
|
+
// The peer reset this stream. nghttp2 refuses DATA and HEADERS on a closed
|
|
84
|
+
// stream, so _pump must settle the Duplex's writable half without framing.
|
|
85
|
+
this._reset = false;
|
|
57
86
|
// node keeps rstCode in the stream's C-side state, initialised with
|
|
58
87
|
// NGHTTP2_NO_ERROR (internal/http2/core.js:2152 `rstCode: NGHTTP2_NO_ERROR`), so a
|
|
59
88
|
// stream that closed without a reset reports 0 -- the upstream
|
|
60
89
|
// max-reserved-streams test reads exactly that off the parent request.
|
|
61
|
-
//
|
|
90
|
+
// closeStream overwrites it with the code the stream closes with, whether that came
|
|
91
|
+
// from the peer, from a refusal or from the destroy derivation below.
|
|
62
92
|
this.rstCode = 0;
|
|
93
|
+
// node's STREAM_FLAGS_CLOSED (core.js:2074, set by closeStream). It is what lets
|
|
94
|
+
// _destroy tell "the wire life already ended with a code" from "this destroy is the
|
|
95
|
+
// first thing to end it": the former keeps rstCode, the latter derives a code from
|
|
96
|
+
// the error and the session (core.js:2512).
|
|
97
|
+
this._closed = false;
|
|
63
98
|
this.headersSent = false; // node flips this once HEADERS are sent
|
|
64
|
-
|
|
99
|
+
// What request() was given, for the sentHeaders getter below. node keeps the caller's
|
|
100
|
+
// map as kRawHeaders and folds it on the first read (internal/http2/core.js:2225).
|
|
101
|
+
this._sentSrc = undefined;
|
|
102
|
+
this._sentView = undefined;
|
|
65
103
|
this.sentStatus = null;
|
|
66
104
|
this.responseEmitted = false;
|
|
67
105
|
// Set by the session for a stream created from PUSH_PROMISE: the first final
|
|
@@ -70,6 +108,40 @@ class ClientHttp2Stream extends Duplex {
|
|
|
70
108
|
this._pushed = false;
|
|
71
109
|
this.additionalHeaders = [];
|
|
72
110
|
this.trailers = null;
|
|
111
|
+
// Set by session.request() when the application passed waitForTrailers: the last
|
|
112
|
+
// DATA on the wire does not carry END_STREAM; instead 'wantTrailers' fires and
|
|
113
|
+
// the app is expected to call sendTrailers() (or close()) to finish the message.
|
|
114
|
+
// Matches node's STREAM_FLAGS_HAS_TRAILERS on a client stream.
|
|
115
|
+
this._waitForTrailers = false;
|
|
116
|
+
// node's `state.trailersReady`, and the same rule as on the server: the permission
|
|
117
|
+
// to call sendTrailers() comes from the 'wantTrailers' callback, not from the
|
|
118
|
+
// option. sendTrailers() is defined on the shared Http2Stream base in node
|
|
119
|
+
// (internal/http2/core.js), so a client stream answers with the same three errors.
|
|
120
|
+
this._trailersReady = false;
|
|
121
|
+
// undefined until a trailer block is sent, the way node's kSentTrailers reads before
|
|
122
|
+
// sendTrailers() (measured on a live node client); the server stream's field is the same.
|
|
123
|
+
this.sentTrailers = undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// node's client sentHeaders is the request map with the pseudo-headers request() resolved
|
|
127
|
+
// -- ':method' GET, ':authority' from the connect authority, ':scheme' from the transport,
|
|
128
|
+
// ':path' '/' -- folded out of what the application passed (prepareRequestHeadersObject /
|
|
129
|
+
// ...Array, internal/http2/util.js:717/:627, read through the getter at core.js:2225).
|
|
130
|
+
// Resolved against the session as it stood when the request was made, and built on the
|
|
131
|
+
// first read so a request nobody inspects pays no copy.
|
|
132
|
+
get sentHeaders() {
|
|
133
|
+
// A stream created from PUSH_PROMISE has neither kSentHeaders nor kRawHeaders,
|
|
134
|
+
// so node's getter answers undefined (core.js:2226 `if (this[kSentHeaders] ||
|
|
135
|
+
// !this[kRawHeaders]) return this[kSentHeaders]`; measured on a live node client).
|
|
136
|
+
// A request() with no headers argument is not that case: node prepares `{}` and
|
|
137
|
+
// resolves the four pseudo-headers out of it (core.js:1871-1878).
|
|
138
|
+
if (this._pushed) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
if (this._sentView === undefined) {
|
|
142
|
+
this._sentView = requestView(this._sentSrc, this.session._scheme, this.session._authority);
|
|
143
|
+
}
|
|
144
|
+
return this._sentView;
|
|
73
145
|
}
|
|
74
146
|
|
|
75
147
|
// ---- writable side ----------------------------------------------------------
|
|
@@ -119,7 +191,10 @@ class ClientHttp2Stream extends Duplex {
|
|
|
119
191
|
// 'continue'): emit 'headers', keep waiting for the final response. The
|
|
120
192
|
// 100-Continue translation node does is a 'headers' whose :status is 100.
|
|
121
193
|
this.additionalHeaders.push(headers);
|
|
122
|
-
|
|
194
|
+
// node's inbound emit carries the frame flags as the second argument
|
|
195
|
+
// (`emit(stream, event, obj, flags, headers)`, internal/http2/core.js), which is
|
|
196
|
+
// what the upstream sent-headers test asserts is a number.
|
|
197
|
+
this.emit('headers', headers, flags);
|
|
123
198
|
if (status === 100) {
|
|
124
199
|
this.emit('continue', headers);
|
|
125
200
|
}
|
|
@@ -167,24 +242,125 @@ class ClientHttp2Stream extends Duplex {
|
|
|
167
242
|
this.read(0);
|
|
168
243
|
}
|
|
169
244
|
|
|
170
|
-
//
|
|
171
|
-
|
|
245
|
+
// node's closeStream (core.js:2071) minus the RST submission, which each caller
|
|
246
|
+
// decides for itself: _destroy forces one, the session's outbound refusal submits the
|
|
247
|
+
// refusal's own code, and an inbound reset submits none (kNoRstStream). What is shared
|
|
248
|
+
// is the record and the writable half -- 'aborted' turns on whether end() was *called*,
|
|
249
|
+
// not whether the octets went out, so a request body still open is 'aborted' and then
|
|
250
|
+
// ended, and the stream reports 'finish' and writableEnded like node's (measured on
|
|
251
|
+
// v26.12.1 for codes 0, 1 and 8 against a raw peer, with and without END_STREAM).
|
|
252
|
+
_closeStream(code) {
|
|
253
|
+
this._closed = true;
|
|
172
254
|
this.rstCode = code;
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
255
|
+
if (!this.writableEnded) {
|
|
256
|
+
if (!this.aborted) {
|
|
257
|
+
this.aborted = true;
|
|
258
|
+
this.emit('aborted');
|
|
259
|
+
}
|
|
260
|
+
this.end();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// The peer reset this stream. node runs two steps in this order -- closeStream, then
|
|
265
|
+
// the readable decision in onStreamClose (internal/http2/core.js:607) -- and both are
|
|
266
|
+
// observable: the writable half is closed first, and a clean reset waits for the
|
|
267
|
+
// buffered body instead of discarding it. onStreamClose then calls `stream.destroy()`
|
|
268
|
+
// with NO error (core.js:624): the error the application sees is the one _destroy
|
|
269
|
+
// synthesizes from rstCode, which is why the code has to be recorded before it.
|
|
270
|
+
_onRst(code) {
|
|
271
|
+
this._reset = true;
|
|
272
|
+
this._closeStream(code);
|
|
273
|
+
// NO_ERROR is a clean end, not a reset that discards: node pushes null and destroys
|
|
274
|
+
// on 'end' ("Preserve buffered reads after a clean reset"), so 'end' fires once the
|
|
275
|
+
// body is consumed. A body nobody consumes keeps the stream open on node too --
|
|
276
|
+
// measured, no 'close' until a read -- so the wait is parity, not a leak we invent.
|
|
277
|
+
if (code === NO_ERROR && this.readable) {
|
|
278
|
+
this._endRead();
|
|
279
|
+
this.once('end', () => this.destroy());
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
this.destroy();
|
|
178
283
|
}
|
|
179
284
|
|
|
180
285
|
// Destroying the Duplex is how the application abandons a request (`stream.destroy()`,
|
|
181
|
-
// or `close()`).
|
|
182
|
-
//
|
|
183
|
-
// is
|
|
286
|
+
// or `close()`). node derives the code here (core.js:2503-2522) rather than fixing it:
|
|
287
|
+
// a stream whose wire life already ended keeps the code it closed with, otherwise the
|
|
288
|
+
// session's goaway/destroy code is the starting point, and an error overrides it --
|
|
289
|
+
// the session's code first, then CANCEL for node's own AbortError, then INTERNAL_ERROR.
|
|
290
|
+
// Measured on a live node client against a raw peer: destroy() frames RST_STREAM(0) and
|
|
291
|
+
// reports rstCode 0, destroy(new Error()) frames RST_STREAM(2) and reports rstCode 2,
|
|
292
|
+
// and an aborted options.signal frames RST_STREAM(8) and reports rstCode 8. The old
|
|
293
|
+
// unconditional rstStream(id) framed CANCEL for all three and left rstCode at 0.
|
|
184
294
|
_destroy(err, cb) {
|
|
185
|
-
this.session
|
|
295
|
+
const session = this.session;
|
|
296
|
+
const sessionCode = session.goawayCode || session._destroyCode;
|
|
297
|
+
let code = this._closed ? this.rstCode : sessionCode;
|
|
298
|
+
if (err != null) {
|
|
299
|
+
if (sessionCode) {
|
|
300
|
+
code = sessionCode;
|
|
301
|
+
} else if (err instanceof AbortError) {
|
|
302
|
+
code = CANCEL;
|
|
303
|
+
} else {
|
|
304
|
+
code = INTERNAL_ERROR;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (!this._closed) {
|
|
308
|
+
this._closeStream(code);
|
|
309
|
+
// A stream the session never opened has no id on the wire: rstStream finds nothing
|
|
310
|
+
// and submits nothing, which is node's kNoRstStream for a pending stream.
|
|
311
|
+
session.rstStream(this.id, code);
|
|
312
|
+
}
|
|
313
|
+
// node's _destroy pushes null unconditionally, after closeStream and before the
|
|
314
|
+
// handle goes: a destroy that carries no error still ends the readable half, so a
|
|
315
|
+
// CANCEL reset -- and an application destroy() with the body already consumed --
|
|
316
|
+
// reports 'end' (measured on v26.12.1). onEofChunk is idempotent, so a readable half
|
|
317
|
+
// that already saw END_STREAM is unaffected.
|
|
318
|
+
this.push(null);
|
|
319
|
+
// RST code 8 is not an error: it is how a client signifies abort, already covered by
|
|
320
|
+
// 'aborted' (core.js:2540-2544). streamErrorOf is that rule, and the same one the
|
|
321
|
+
// server uses; an error the caller passed is never replaced.
|
|
322
|
+
if (err == null && code !== NO_ERROR && code !== CANCEL) {
|
|
323
|
+
err = streamErrorOf(code);
|
|
324
|
+
}
|
|
186
325
|
cb(err);
|
|
187
326
|
}
|
|
327
|
+
|
|
328
|
+
// node's Http2Stream.sendTrailers(headers) -- the same base method the server stream
|
|
329
|
+
// exposes, so the same three guards in the same order, all synchronous throws:
|
|
330
|
+
// destroyed (node also refuses when the stream is closed; this Duplex has no separate
|
|
331
|
+
// closed flag, its 'close' follows destroy) -> ERR_HTTP2_INVALID_STREAM, already sent
|
|
332
|
+
// -> ERR_HTTP2_TRAILERS_ALREADY_SENT, and 'wantTrailers' not yet emitted
|
|
333
|
+
// -> ERR_HTTP2_TRAILERS_NOT_READY.
|
|
334
|
+
sendTrailers(trailers) {
|
|
335
|
+
if (this.destroyed) {
|
|
336
|
+
const err = new Error('The stream has been destroyed');
|
|
337
|
+
err.code = 'ERR_HTTP2_INVALID_STREAM';
|
|
338
|
+
throw err;
|
|
339
|
+
}
|
|
340
|
+
if (this.sentTrailers) {
|
|
341
|
+
const err = new Error('Trailing headers have already been sent');
|
|
342
|
+
err.code = 'ERR_HTTP2_TRAILERS_ALREADY_SENT';
|
|
343
|
+
throw err;
|
|
344
|
+
}
|
|
345
|
+
if (!this._trailersReady) {
|
|
346
|
+
const err = new Error(
|
|
347
|
+
'Trailing headers cannot be sent until after the wantTrailers event is emitted');
|
|
348
|
+
err.code = 'ERR_HTTP2_TRAILERS_NOT_READY';
|
|
349
|
+
throw err;
|
|
350
|
+
}
|
|
351
|
+
// node drops the permission flag before touching the wire so a second
|
|
352
|
+
// sendTrailers() hits the ALREADY_SENT guard. The bytes are built inside the
|
|
353
|
+
// session, which also puts HEADERS+END_STREAM on the wire for this stream id.
|
|
354
|
+
this._waitForTrailers = false;
|
|
355
|
+
this._trailersReady = false;
|
|
356
|
+
this.session._sendTrailers(this, trailers);
|
|
357
|
+
// node records a null-prototype copy, not the caller's map
|
|
358
|
+
// (`headers = ObjectAssign({ __proto__: null }, headers)` then
|
|
359
|
+
// `this[kSentTrailers] = headers`, internal/http2/core.js sendTrailers), and only
|
|
360
|
+
// once the block has been built -- a trailer map the encoder refuses leaves
|
|
361
|
+
// sentTrailers unset on both stacks.
|
|
362
|
+
this.sentTrailers = Object.assign({ __proto__: null }, trailers);
|
|
363
|
+
}
|
|
188
364
|
}
|
|
189
365
|
|
|
190
|
-
module.exports = { ClientHttp2Stream };
|
|
366
|
+
module.exports = { ClientHttp2Stream, AbortError };
|
package/lib/client.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
const net = require('node:net');
|
|
18
18
|
const tls = require('node:tls');
|
|
19
19
|
const { ClientHttp2Session } = require('./client-session');
|
|
20
|
-
const { strictSingleValueFields } = require('./session');
|
|
20
|
+
const { strictSingleValueFields, received, outOfRangeValue } = require('./session');
|
|
21
21
|
|
|
22
22
|
function notImplemented(what) {
|
|
23
23
|
const err = new Error(`h2lean: not implemented: ${what}`);
|
|
@@ -44,6 +44,38 @@ function connect(authority, options, listener) {
|
|
|
44
44
|
// receive its errors. The constructor re-checks, which costs one typeof.
|
|
45
45
|
strictSingleValueFields(options);
|
|
46
46
|
|
|
47
|
+
// core.js connect(): maxOriginSetSize is client-only and defaults to 128 (min 0).
|
|
48
|
+
// It bounds how many ORIGIN frames the session accumulates into originSet before
|
|
49
|
+
// destroying itself with ERR_HTTP2_TOO_MANY_ORIGINS. Validated here (before the
|
|
50
|
+
// socket exists, as every other option is) and passed to the session.
|
|
51
|
+
// Node runs this value through validateNumber(name, 0), which distinguishes
|
|
52
|
+
// the two failure classes: a non-number is ERR_INVALID_ARG_TYPE / TypeError,
|
|
53
|
+
// a number that is NaN or negative is ERR_OUT_OF_RANGE / RangeError. Both
|
|
54
|
+
// received-value strings are node's own wording -- determineSpecificType and
|
|
55
|
+
// the ERR_OUT_OF_RANGE formatter, shared with lib/session.js -- because a
|
|
56
|
+
// paraphrase is a visible difference: `Received type object` where node says
|
|
57
|
+
// `Received an instance of Array`. Measured against a live node connect() over
|
|
58
|
+
// the 16 values node's own origin-set-max-size test passes plus the huge ones.
|
|
59
|
+
if (options.maxOriginSetSize != null) {
|
|
60
|
+
const v = options.maxOriginSetSize;
|
|
61
|
+
if (typeof v !== 'number') {
|
|
62
|
+
const err = new TypeError(
|
|
63
|
+
'The "options.maxOriginSetSize" property must be of type number. '
|
|
64
|
+
+ `Received ${received(v)}`);
|
|
65
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
68
|
+
if (Number.isNaN(v) || v < 0) {
|
|
69
|
+
const err = new RangeError(
|
|
70
|
+
'The value of "options.maxOriginSetSize" is out of range. It must be >= 0. '
|
|
71
|
+
+ `Received ${outOfRangeValue(v)}`);
|
|
72
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
options.maxOriginSetSize = 128;
|
|
77
|
+
}
|
|
78
|
+
|
|
47
79
|
const protocol = authority.protocol || options.protocol || 'https:';
|
|
48
80
|
const port = '' + (authority.port !== ''
|
|
49
81
|
? authority.port : (authority.protocol === 'http:' ? 80 : 443));
|
package/lib/compat.js
CHANGED
|
@@ -8,8 +8,12 @@
|
|
|
8
8
|
// is still replenished, because h2lean releases stream credit as the buffered
|
|
9
9
|
// body is consumed and connection credit in half-window steps, neither of them
|
|
10
10
|
// per read(). A paused request buffers in memory instead of slowing the peer down.
|
|
11
|
-
// * res.cork()/uncork()
|
|
12
|
-
//
|
|
11
|
+
// * res.cork()/uncork() (and writableCorked/writableHighWaterMark) delegate to the wrapped
|
|
12
|
+
// stream, exactly as node's compat does (`cork(){ this[kStream].cork(); }`). That stream's
|
|
13
|
+
// cork is a counting stub, not Writable's buffering cork (lib/stream.js): h2lean coalesces a
|
|
14
|
+
// synchronous burst into one write() but sends immediately while corked, where node defers
|
|
15
|
+
// the DATA until the count reaches 0. Measured on a live node server, so the response
|
|
16
|
+
// inherits the same cross-tick deferral divergence (test/cork-parity.test.js).
|
|
13
17
|
// * res.write() returning false is honest backpressure, and 'drain' is emitted
|
|
14
18
|
// when the send window reopens.
|
|
15
19
|
// * res 'finish' means the local half of the stream was closed, which for
|
|
@@ -28,6 +32,14 @@
|
|
|
28
32
|
// * the body is pulled, not pushed: nothing is read off the wire until the
|
|
29
33
|
// application consumes it, so an unread upload applies backpressure to the
|
|
30
34
|
// sender (node does the same) instead of buffering here.
|
|
35
|
+
// * a stream 'error' reaches the compat request: onRequestError destroys req
|
|
36
|
+
// with it, so an application that listens only on res gets an unhandled
|
|
37
|
+
// 'error'. node's onStreamError (internal/http2/compat.js) is purposefully
|
|
38
|
+
// blank -- "errors in compatibility mode are not forwarded to the request
|
|
39
|
+
// and response objects". Measured with a content-length promise broken by a
|
|
40
|
+
// short body (content-length 10, two octets sent): node reports st-error
|
|
41
|
+
// then st-close:1 and the client its own error/close:1, h2lean reports
|
|
42
|
+
// st-close:1, st-error, then an uncaught ERR_HTTP2_STREAM_ERROR out of req.
|
|
31
43
|
// * with the server option shareHeaders:true, req.headers is frozen and shared
|
|
32
44
|
// between requests whose header block is identical, so req.url = ... and
|
|
33
45
|
// req.method = ... throw instead of rewriting the request.
|
|
@@ -223,6 +235,9 @@ function compatDispatch(name, arg) {
|
|
|
223
235
|
case 'trailers':
|
|
224
236
|
onRequestTrailers.call(this, arg);
|
|
225
237
|
break;
|
|
238
|
+
case 'wantTrailers':
|
|
239
|
+
onResponseTrailersReady.call(this);
|
|
240
|
+
break;
|
|
226
241
|
case 'close':
|
|
227
242
|
onRequestClose.call(this, arg);
|
|
228
243
|
onResponseClose.call(this, arg);
|
|
@@ -300,13 +315,15 @@ class Http2ServerRequest extends Readable {
|
|
|
300
315
|
return '2.0';
|
|
301
316
|
}
|
|
302
317
|
|
|
303
|
-
// node hands
|
|
304
|
-
// socket
|
|
305
|
-
//
|
|
306
|
-
//
|
|
318
|
+
// node hands `req.socket` a Proxy over the *stream* (compat.js proxySocketHandler, distinct
|
|
319
|
+
// from session.socket's core handler): write/read/pause/resume throw, end/emit/destroy route
|
|
320
|
+
// to the stream. h2lean instead hands out the real socket -- it is smaller and answers the
|
|
321
|
+
// same questions (remoteAddress), but a `.write()` that node refuses succeeds here. KNOWN
|
|
322
|
+
// DIVERGENCE, and separate from the session surface. The session reference is cleared when the
|
|
323
|
+
// stream dies (lib/stream.js _destroy, node core.js:2546), so the socket goes with it.
|
|
307
324
|
get socket() {
|
|
308
325
|
const session = this._stream.session;
|
|
309
|
-
return session === undefined ? undefined : session.
|
|
326
|
+
return session === undefined ? undefined : session._socket;
|
|
310
327
|
}
|
|
311
328
|
get connection() {
|
|
312
329
|
return this.socket;
|
|
@@ -388,6 +405,18 @@ function onResponseDrain() {
|
|
|
388
405
|
}
|
|
389
406
|
}
|
|
390
407
|
|
|
408
|
+
function onResponseTrailersReady() {
|
|
409
|
+
// node's onStreamTrailersReady (internal/http2/compat): a compat response always asks
|
|
410
|
+
// for trailers (`waitForTrailers: true` in [kBeginSend]) and always answers the
|
|
411
|
+
// callback, with the trailer map as it stands -- empty for every response that set no
|
|
412
|
+
// trailer. A compat response that ended at respond() never reaches this: END_STREAM was
|
|
413
|
+
// already on the HEADERS, so the flag was cleared and the callback was not asked for.
|
|
414
|
+
const res = this._response;
|
|
415
|
+
if (res !== undefined && !res._state.closed) {
|
|
416
|
+
this.sendTrailers(res._trailers);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
391
420
|
function onResponseClose() {
|
|
392
421
|
const res = this._response;
|
|
393
422
|
if (res === undefined || res._state.closed) {
|
|
@@ -436,7 +465,6 @@ class Http2ServerResponse extends Stream {
|
|
|
436
465
|
// make than the literal (OPTIMIZATION.md item 4).
|
|
437
466
|
this._headers = nullProto();
|
|
438
467
|
this._trailers = { __proto__: null };
|
|
439
|
-
this._trailerNames = 0;
|
|
440
468
|
this._stream = stream;
|
|
441
469
|
stream._response = this;
|
|
442
470
|
this.writable = true;
|
|
@@ -463,7 +491,7 @@ class Http2ServerResponse extends Stream {
|
|
|
463
491
|
return undefined;
|
|
464
492
|
}
|
|
465
493
|
const session = this._stream.session;
|
|
466
|
-
return session === undefined ? undefined : session.
|
|
494
|
+
return session === undefined ? undefined : session._socket;
|
|
467
495
|
}
|
|
468
496
|
get connection() {
|
|
469
497
|
return this.socket;
|
|
@@ -591,11 +619,7 @@ class Http2ServerResponse extends Stream {
|
|
|
591
619
|
|
|
592
620
|
setTrailer(name, value) {
|
|
593
621
|
checkName(name);
|
|
594
|
-
|
|
595
|
-
if (this._trailers[key] === undefined) {
|
|
596
|
-
this._trailerNames++;
|
|
597
|
-
}
|
|
598
|
-
this._trailers[key] = value;
|
|
622
|
+
this._trailers[trimName(name)] = value;
|
|
599
623
|
}
|
|
600
624
|
|
|
601
625
|
addTrailers(headers) {
|
|
@@ -656,9 +680,12 @@ class Http2ServerResponse extends Stream {
|
|
|
656
680
|
// either. A merged copy was built here until the header path was measured:
|
|
657
681
|
// it bought nothing (OPTIMIZATION.md item 25) -- the map is never frozen
|
|
658
682
|
// and the HPACK block cache -- which is keyed by the identity of a frozen
|
|
659
|
-
// object -- cannot use it.
|
|
660
|
-
// node's
|
|
661
|
-
//
|
|
683
|
+
// object -- cannot use it. The stream's sentHeaders getter folds a view out
|
|
684
|
+
// of this map on demand, and node's compat path publishes the same three
|
|
685
|
+
// kinds of key (measured on a live node server: the stream reports
|
|
686
|
+
// abc,:status,date). It is the stream that has the getter -- node's
|
|
687
|
+
// Http2ServerResponse has no sentHeaders, so `res.sentHeaders` is undefined
|
|
688
|
+
// on both stacks.
|
|
662
689
|
stream.respond(this._headers, {
|
|
663
690
|
// a HEAD response must close the stream after the headers, and an
|
|
664
691
|
// application that called end() before flushing also does
|
|
@@ -774,21 +801,41 @@ class Http2ServerResponse extends Stream {
|
|
|
774
801
|
process.nextTick(emitNT, this, 'finish');
|
|
775
802
|
}
|
|
776
803
|
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
}
|
|
804
|
+
// node's compat end() ends the stream and does nothing else
|
|
805
|
+
// (internal/http2/compat: `else stream.end()`). The trailer block is answered from
|
|
806
|
+
// the 'wantTrailers' callback -- onResponseTrailersReady above -- which the stream
|
|
807
|
+
// only reaches when the local side ends while the flag is still set, and which sends
|
|
808
|
+
// whatever the response recorded by then: nothing at all for a response whose HEADERS
|
|
809
|
+
// already carried END_STREAM, an empty block for a response that added no trailer
|
|
810
|
+
// (measured on the wire: HEADERS, DATA, then a zero-length DATA that carries
|
|
811
|
+
// END_STREAM), the recorded fields when it added some. Calling sendTrailers() here
|
|
812
|
+
// instead put the block in front of that machinery -- and threw NOT_READY as soon as
|
|
813
|
+
// sendTrailers() grew node's own trailersReady guard.
|
|
814
|
+
stream.end();
|
|
789
815
|
return this;
|
|
790
816
|
}
|
|
791
817
|
|
|
818
|
+
// node's compat response delegates corking to the stream it wraps (internal/http2/compat.js:
|
|
819
|
+
// `cork(){ this[kStream].cork(); }`, and writableCorked/writableHighWaterMark read the stream's
|
|
820
|
+
// Writable side). h2lean's stream cork is a counting stub (lib/stream.js), so res.cork()/uncork()
|
|
821
|
+
// inherit its cross-tick deferral divergence -- measured against a live node server and pinned in
|
|
822
|
+
// test/cork-parity.test.js.
|
|
823
|
+
cork() {
|
|
824
|
+
this._stream.cork();
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
uncork() {
|
|
828
|
+
this._stream.uncork();
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
get writableCorked() {
|
|
832
|
+
return this._stream.writableCorked;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
get writableHighWaterMark() {
|
|
836
|
+
return this._stream.writableHighWaterMark;
|
|
837
|
+
}
|
|
838
|
+
|
|
792
839
|
destroy(error) {
|
|
793
840
|
const state = this._state;
|
|
794
841
|
if (state.destroyed) {
|
package/lib/frames.js
CHANGED
|
@@ -8,13 +8,15 @@ const HEADER_LEN = 9;
|
|
|
8
8
|
|
|
9
9
|
// Frame types, RFC 9113 §6 and the IANA HTTP/2 frame registry. The numbers are the
|
|
10
10
|
// wire format, not an internal enumeration: a peer sends 9 when it splits a header
|
|
11
|
-
// block, and 10 is ALTSVC (RFC 7807
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
11
|
+
// block, and 10 is ALTSVC (RFC 7807 3.1) and 12 is ORIGIN (RFC 8336 3). ALTSVC and
|
|
12
|
+
// ORIGIN are the two extension frames h2lean parses rather than discards; every other
|
|
13
|
+
// unknown type is discarded per RFC 9113 §5.5. Getting one of these wrong is invisible
|
|
14
|
+
// to our own tests -- both ends would use the same wrong number -- and looks to every
|
|
15
|
+
// other implementation like a connection error, so each value here was checked against a
|
|
15
16
|
// live node:http2 endpoint rather than recalled.
|
|
16
17
|
const DATA = 0, HEADERS = 1, PRIORITY = 2, RST_STREAM = 3, SETTINGS = 4;
|
|
17
18
|
const PUSH_PROMISE = 5, PING = 6, GOAWAY = 7, WINDOW_UPDATE = 8, CONTINUATION = 9;
|
|
19
|
+
const ALTSVC = 10, ORIGIN = 12;
|
|
18
20
|
|
|
19
21
|
const F_END_STREAM = 0x1;
|
|
20
22
|
const F_ACK = 0x1;
|
|
@@ -29,6 +31,21 @@ const SETTINGS_TIMEOUT = 4, STREAM_CLOSED = 5, FRAME_SIZE_ERROR = 6;
|
|
|
29
31
|
const REFUSED_STREAM = 7, CANCEL = 8, COMPRESSION_ERROR = 9, CONNECT_ERROR = 10;
|
|
30
32
|
const ENHANCE_YOUR_CALM = 11, INADEQUATE_SECURITY = 12, HTTP_1_1_REQUIRED = 13;
|
|
31
33
|
|
|
34
|
+
// The nghttp2 spellings of those codes, indexed by code. node builds the stream error
|
|
35
|
+
// message from its C++ `nameForErrorCode` table (`const { constants, nameForErrorCode } =
|
|
36
|
+
// binding`, internal/http2/core.js:249, used at :2544), so the names are not reachable
|
|
37
|
+
// from node's JS: all 14 were measured off a live node client and a live node server,
|
|
38
|
+
// one RST_STREAM per code, and the enum in nghttp2.h ends at NGHTTP2_HTTP_1_1_REQUIRED
|
|
39
|
+
// = 0x0D. A code outside the table falls back to the number itself -- measured with 14,
|
|
40
|
+
// 100 and 4294967295: 'Stream closed with error code 14'.
|
|
41
|
+
const ERROR_NAMES = [
|
|
42
|
+
'NGHTTP2_NO_ERROR', 'NGHTTP2_PROTOCOL_ERROR', 'NGHTTP2_INTERNAL_ERROR',
|
|
43
|
+
'NGHTTP2_FLOW_CONTROL_ERROR', 'NGHTTP2_SETTINGS_TIMEOUT', 'NGHTTP2_STREAM_CLOSED',
|
|
44
|
+
'NGHTTP2_FRAME_SIZE_ERROR', 'NGHTTP2_REFUSED_STREAM', 'NGHTTP2_CANCEL',
|
|
45
|
+
'NGHTTP2_COMPRESSION_ERROR', 'NGHTTP2_CONNECT_ERROR', 'NGHTTP2_ENHANCE_YOUR_CALM',
|
|
46
|
+
'NGHTTP2_INADEQUATE_SECURITY', 'NGHTTP2_HTTP_1_1_REQUIRED'
|
|
47
|
+
];
|
|
48
|
+
|
|
32
49
|
// Stream state numbers as node reports them in stream.state.state.
|
|
33
50
|
const ST_IDLE = 1, ST_OPEN = 2, ST_RESERVED_LOCAL = 3, ST_RESERVED_REMOTE = 4;
|
|
34
51
|
const ST_HALF_CLOSED_LOCAL = 5, ST_HALF_CLOSED_REMOTE = 6, ST_CLOSED = 7;
|
|
@@ -104,11 +121,12 @@ function trimPadding(pieces) {
|
|
|
104
121
|
module.exports = {
|
|
105
122
|
PREFACE, PREFACE_LEN, HEADER_LEN,
|
|
106
123
|
DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY,
|
|
107
|
-
WINDOW_UPDATE, CONTINUATION,
|
|
124
|
+
WINDOW_UPDATE, CONTINUATION, ALTSVC, ORIGIN,
|
|
108
125
|
F_END_STREAM, F_ACK, F_END_HEADERS, F_PADDED, F_PRIORITY,
|
|
109
126
|
NO_ERROR, PROTOCOL_ERROR, INTERNAL_ERROR, FLOW_CONTROL_ERROR, SETTINGS_TIMEOUT,
|
|
110
127
|
STREAM_CLOSED, FRAME_SIZE_ERROR, REFUSED_STREAM, CANCEL, COMPRESSION_ERROR,
|
|
111
128
|
CONNECT_ERROR, ENHANCE_YOUR_CALM, INADEQUATE_SECURITY, HTTP_1_1_REQUIRED,
|
|
129
|
+
ERROR_NAMES,
|
|
112
130
|
ST_IDLE, ST_OPEN, ST_RESERVED_LOCAL, ST_RESERVED_REMOTE,
|
|
113
131
|
ST_HALF_CLOSED_LOCAL, ST_HALF_CLOSED_REMOTE, ST_CLOSED,
|
|
114
132
|
readHeader, writeHeader, trimPadding,
|