h2lean 0.11.0 → 0.11.3
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/lib/client-session.js +31 -9
- package/lib/client-stream.js +81 -16
- package/lib/compat.js +68 -22
- package/lib/fast-tick.js +139 -62
- package/lib/server.js +67 -10
- package/lib/session.js +66 -17
- package/lib/stream.js +53 -14
- package/package.json +1 -1
package/lib/client-session.js
CHANGED
|
@@ -2083,7 +2083,9 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2083
2083
|
this._frame(RST_STREAM, 0, promisedSid, body, true);
|
|
2084
2084
|
return;
|
|
2085
2085
|
}
|
|
2086
|
-
const
|
|
2086
|
+
const pushHeaders = this.listenerCount('stream') === 0
|
|
2087
|
+
? null
|
|
2088
|
+
: this._toObject(fields, fcount, neverNames(fields, fcount, flags));
|
|
2087
2089
|
// Create the pushed stream. Its writable half is pre-ended: the client never
|
|
2088
2090
|
// sends a request body for a push (node: writableEnded=true, writableFinished=true
|
|
2089
2091
|
// at the 'stream' event, measured v26.12.1).
|
|
@@ -2108,8 +2110,9 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2108
2110
|
// flags 4 = NGHTTP2_FLAG_END_HEADERS, the only flag PUSH_PROMISE carries that
|
|
2109
2111
|
// the JS layer surfaces (measured v26.12.1). Synchronous so the application's
|
|
2110
2112
|
// 'push' subscription is in place before the response HEADERS arrive in the
|
|
2111
|
-
// same read batch.
|
|
2112
|
-
|
|
2113
|
+
// same read batch. The promise headers object exists only for that argument,
|
|
2114
|
+
// so with no listener the build above skips -- unobservable either way.
|
|
2115
|
+
this.emit('stream', stream, pushHeaders, 4);
|
|
2113
2116
|
}
|
|
2114
2117
|
|
|
2115
2118
|
// The shape cache: the server's design, ported. A block whose bytes have been
|
|
@@ -2186,6 +2189,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2186
2189
|
fields = flat.items;
|
|
2187
2190
|
fcount = flat.count;
|
|
2188
2191
|
blockFlags = this._decoder.neverCount > 0 ? this._decoder.neverFlags : null;
|
|
2192
|
+
// The field list the stream holds must outlive this decode: the
|
|
2193
|
+
// scratch's backing array is cleared in place by the next one. The copy
|
|
2194
|
+
// that caches the shape serves the stream too; without a cache entry
|
|
2195
|
+
// the copy is the stream's own.
|
|
2196
|
+
let copy = null;
|
|
2189
2197
|
if (shapes !== null) {
|
|
2190
2198
|
if (++this._shapeMisses > 64 && this._shapeHits * 4 < this._shapeMisses) {
|
|
2191
2199
|
this._shapes = null;
|
|
@@ -2193,15 +2201,17 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2193
2201
|
if (shapes.size >= MAX_SHAPES) {
|
|
2194
2202
|
shapes.clear();
|
|
2195
2203
|
}
|
|
2204
|
+
copy = fields.slice(0, fcount);
|
|
2196
2205
|
shapes.set(slot, {
|
|
2197
2206
|
block: block !== null ? Buffer.from(block) : pieceConcat(pieces, from, to),
|
|
2198
|
-
flat:
|
|
2207
|
+
flat: copy,
|
|
2199
2208
|
// a snapshot copy, not the decoder's live array: the next decode
|
|
2200
2209
|
// clears the array in place, and this entry must survive it
|
|
2201
2210
|
flags: blockFlags !== null ? blockFlags.slice() : null
|
|
2202
2211
|
});
|
|
2203
2212
|
}
|
|
2204
2213
|
}
|
|
2214
|
+
fields = copy !== null ? copy : fields.slice(0, fcount);
|
|
2205
2215
|
}
|
|
2206
2216
|
} catch (err) {
|
|
2207
2217
|
if (err instanceof HPackError) {
|
|
@@ -2236,7 +2246,20 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2236
2246
|
if (this._refuseHeaders(sid, stream, fields, fcount)) {
|
|
2237
2247
|
return;
|
|
2238
2248
|
}
|
|
2239
|
-
const
|
|
2249
|
+
const sensitive = neverNames(fields, fcount, blockFlags);
|
|
2250
|
+
// The response object is not built here: the stream builds it at the first
|
|
2251
|
+
// read (lib/client-stream.js _onHeaders), so a client that only consumes
|
|
2252
|
+
// the body pays no per-response object at all -- the paired marginal is in
|
|
2253
|
+
// OPTIMIZATION.md item 48. This block still needs :status, read off the
|
|
2254
|
+
// same field list _refuseHeaders has just vouched for: exactly one on a
|
|
2255
|
+
// response, none on a trailer.
|
|
2256
|
+
let status;
|
|
2257
|
+
for (let i = 0; i < fcount; i += 2) {
|
|
2258
|
+
if (fields[i] === ':status') {
|
|
2259
|
+
status = fields[i + 1] | 0;
|
|
2260
|
+
break;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2240
2263
|
// RFC 8336 3.2 + core.js emitStreamEvent: a 421 (Misdirected Request) answer
|
|
2241
2264
|
// deletes the origin the request carried from originSet, and it does so here --
|
|
2242
2265
|
// before 'response' is scheduled, so a handler that reads `session.originSet`
|
|
@@ -2244,12 +2267,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2244
2267
|
// its lazy-seed side effect exactly as node's `session[kState].originSet =
|
|
2245
2268
|
// initOriginSet(session)` assignment does: on the first 421 of a session the set
|
|
2246
2269
|
// is created from the socket before the delete, and over h2c both stacks leave
|
|
2247
|
-
// the set unobservable (the getter is encrypted-gated).
|
|
2248
|
-
|
|
2249
|
-
if (headers[':status'] === 421) {
|
|
2270
|
+
// the set unobservable (the getter is encrypted-gated).
|
|
2271
|
+
if (status === 421) {
|
|
2250
2272
|
this._initOriginSet().delete(stream._origin);
|
|
2251
2273
|
}
|
|
2252
|
-
stream._onHeaders(
|
|
2274
|
+
stream._onHeaders(status, fields, fcount, sensitive, end, flags);
|
|
2253
2275
|
if (end) {
|
|
2254
2276
|
this._dropStream(sid);
|
|
2255
2277
|
}
|
package/lib/client-stream.js
CHANGED
|
@@ -38,10 +38,15 @@ class AbortError extends Error {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
// `sentHeaders`/`sentTrailers
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
//
|
|
41
|
+
// `sentHeaders`/`sentTrailers` and the state flags node exposes through symbols
|
|
42
|
+
// are kept as plain fields here; they are not part of the documented client
|
|
43
|
+
// surface an application is expected to touch, and pinning them to node's symbol
|
|
44
|
+
// identity is an M2 compatibility decision, not a round-trip one. The inbound
|
|
45
|
+
// response/trailer objects node does not keep on the stream at all (they pass
|
|
46
|
+
// through the events only, core.js emitStreamEvent) are prototype getters over
|
|
47
|
+
// the unbuilt field list: the object comes into being at the first read, which
|
|
48
|
+
// is what keeps a body-only client free of per-response allocations
|
|
49
|
+
// (OPTIMIZATION.md item 48).
|
|
45
50
|
class ClientHttp2Stream extends Duplex {
|
|
46
51
|
constructor(session, id, _opts) {
|
|
47
52
|
super({ allowHalfOpen: true, decodeStrings: false });
|
|
@@ -110,7 +115,17 @@ class ClientHttp2Stream extends Duplex {
|
|
|
110
115
|
// measured v26.12.1).
|
|
111
116
|
this._pushed = false;
|
|
112
117
|
this.additionalHeaders = [];
|
|
113
|
-
|
|
118
|
+
// The response and trailer objects are built at the first read, not when
|
|
119
|
+
// the block arrives -- the getters below. The slots start undefined so the
|
|
120
|
+
// object shape is stable from construction.
|
|
121
|
+
this._respFields = undefined;
|
|
122
|
+
this._respFcount = 0;
|
|
123
|
+
this._respSensitive = undefined;
|
|
124
|
+
this._responseObject = undefined;
|
|
125
|
+
this._trailerFields = undefined;
|
|
126
|
+
this._trailerFcount = 0;
|
|
127
|
+
this._trailerSensitive = undefined;
|
|
128
|
+
this._trailersObject = undefined;
|
|
114
129
|
// Set by session.request() when the application passed waitForTrailers: the last
|
|
115
130
|
// DATA on the wire does not carry END_STREAM; instead 'wantTrailers' fires and
|
|
116
131
|
// the app is expected to call sendTrailers() (or close()) to finish the message.
|
|
@@ -147,6 +162,43 @@ class ClientHttp2Stream extends Duplex {
|
|
|
147
162
|
return this._sentView;
|
|
148
163
|
}
|
|
149
164
|
|
|
165
|
+
// node's client keeps no response object on the stream at all -- core.js
|
|
166
|
+
// emitStreamEvent passes the objects through the events only -- so these two
|
|
167
|
+
// are prototype getters over the raw field list: the object comes into being
|
|
168
|
+
// at the first read (an event argument when a listener exists, or the getter),
|
|
169
|
+
// and one build serves every later read. A body-only client pays no
|
|
170
|
+
// per-response object (OPTIMIZATION.md item 48). Object.keys(stream) lists
|
|
171
|
+
// neither, which is closer to node than the plain fields this replaces were.
|
|
172
|
+
get responseHeaders() {
|
|
173
|
+
if (this._responseObject !== undefined) {
|
|
174
|
+
return this._responseObject;
|
|
175
|
+
}
|
|
176
|
+
const fields = this._respFields;
|
|
177
|
+
if (fields === undefined) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
const o = this.session._toObject(fields, this._respFcount, this._respSensitive);
|
|
181
|
+
this._respFields = undefined;
|
|
182
|
+
this._respSensitive = undefined;
|
|
183
|
+
this._responseObject = o;
|
|
184
|
+
return o;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
get trailers() {
|
|
188
|
+
if (this._trailersObject !== undefined) {
|
|
189
|
+
return this._trailersObject;
|
|
190
|
+
}
|
|
191
|
+
const fields = this._trailerFields;
|
|
192
|
+
if (fields === undefined) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
const o = this.session._toObject(fields, this._trailerFcount, this._trailerSensitive);
|
|
196
|
+
this._trailerFields = undefined;
|
|
197
|
+
this._trailerSensitive = undefined;
|
|
198
|
+
this._trailersObject = o;
|
|
199
|
+
return o;
|
|
200
|
+
}
|
|
201
|
+
|
|
150
202
|
// ---- writable side ----------------------------------------------------------
|
|
151
203
|
// The application's bytes go to the session, which frames them as DATA inside
|
|
152
204
|
// the two windows. If there is no credit, `_pump` leaves `_pending` set and the
|
|
@@ -184,15 +236,19 @@ class ClientHttp2Stream extends Duplex {
|
|
|
184
236
|
_read() {}
|
|
185
237
|
|
|
186
238
|
// The session decoded a response (or informational / trailer) block for us.
|
|
187
|
-
//
|
|
188
|
-
// `set-cookie` as an array
|
|
189
|
-
//
|
|
190
|
-
|
|
191
|
-
|
|
239
|
+
// The field list arrives unbuilt: the null-prototype object with `:status`
|
|
240
|
+
// coerced to a number and `set-cookie` as an array is what the session's
|
|
241
|
+
// _toObject builds, and it is built here at the first read -- an event
|
|
242
|
+
// argument when a listener exists, or the responseHeaders/trailers getter.
|
|
243
|
+
// A body-only client (no 'response' subscription, no getter read) pays no
|
|
244
|
+
// per-response object at all; the paired marginal is OPTIMIZATION.md item 48.
|
|
245
|
+
_onHeaders(status, fields, fcount, sensitive, end, flags) {
|
|
192
246
|
if (status !== undefined && status >= 100 && status < 200 && !this.responseEmitted) {
|
|
193
247
|
// 1xx informational (including the 100 Continue that node surfaces as
|
|
194
|
-
// 'continue'):
|
|
195
|
-
//
|
|
248
|
+
// 'continue'): built eagerly -- the blocks are rare and the one object
|
|
249
|
+
// feeds the two events and the additionalHeaders field. The 100-Continue
|
|
250
|
+
// translation node does is a 'headers' whose :status is 100.
|
|
251
|
+
const headers = this.session._toObject(fields, fcount, sensitive);
|
|
196
252
|
this.additionalHeaders.push(headers);
|
|
197
253
|
// node's inbound emit carries the frame flags as the second argument
|
|
198
254
|
// (`emit(stream, event, obj, flags, headers)`, internal/http2/core.js), which is
|
|
@@ -203,17 +259,26 @@ class ClientHttp2Stream extends Duplex {
|
|
|
203
259
|
}
|
|
204
260
|
} else if (!this.responseEmitted) {
|
|
205
261
|
this.responseEmitted = true;
|
|
206
|
-
this.responseHeaders = headers;
|
|
207
262
|
this.sentStatus = status;
|
|
263
|
+
this._respFields = fields;
|
|
264
|
+
this._respFcount = fcount;
|
|
265
|
+
this._respSensitive = sensitive;
|
|
208
266
|
// node passes the raw HEADERS frame flags as the second argument and a
|
|
209
267
|
// rawHeaders array as the third (core.js: emit(stream, event, obj, flags,
|
|
210
268
|
// headers)); the rawHeaders array is deliberately not built -- one flat
|
|
211
269
|
// array per response is a cost no h2lean consumer has asked for.
|
|
212
|
-
this.
|
|
270
|
+
const event = this._pushed ? 'push' : 'response';
|
|
271
|
+
if (this.listenerCount(event) > 0) {
|
|
272
|
+
this.emit(event, this.responseHeaders, flags);
|
|
273
|
+
}
|
|
213
274
|
} else {
|
|
214
275
|
// A second final-status block after the response is the trailer block.
|
|
215
|
-
this.
|
|
216
|
-
this.
|
|
276
|
+
this._trailerFields = fields;
|
|
277
|
+
this._trailerFcount = fcount;
|
|
278
|
+
this._trailerSensitive = sensitive;
|
|
279
|
+
if (this.listenerCount('trailers') > 0) {
|
|
280
|
+
this.emit('trailers', this.trailers);
|
|
281
|
+
}
|
|
217
282
|
}
|
|
218
283
|
if (end) {
|
|
219
284
|
this._endRead();
|
package/lib/compat.js
CHANGED
|
@@ -257,14 +257,26 @@ function compatDispatch(name, arg) {
|
|
|
257
257
|
}
|
|
258
258
|
|
|
259
259
|
class Http2ServerRequest extends Readable {
|
|
260
|
-
constructor(stream
|
|
260
|
+
constructor(stream) {
|
|
261
261
|
super({ autoDestroy: false });
|
|
262
262
|
this._stream = stream;
|
|
263
263
|
// The stream's one tick route -- node's queue or the batched pump of
|
|
264
264
|
// lib/fast-tick.js; the deferred callbacks of the compat layer go through it.
|
|
265
265
|
this._tick = stream._tick;
|
|
266
|
-
|
|
267
|
-
|
|
266
|
+
// The request object comes into being at the first read. The session builds
|
|
267
|
+
// it only for the 'stream' emit (lib/session.js accept path); when that did
|
|
268
|
+
// not happen, _headers stays undefined and _materialize() builds it from the
|
|
269
|
+
// stable field list the stream holds -- so a handler that only consumes the
|
|
270
|
+
// body and routes on req.url pays no per-request object
|
|
271
|
+
// (OPTIMIZATION.md item 49). The four scanned values below are read off the
|
|
272
|
+
// same list at accept time, one pass in lib/session.js.
|
|
273
|
+
const eager = stream._requestHeaders;
|
|
274
|
+
this._headers = eager;
|
|
275
|
+
this._reqFields = stream._reqFields;
|
|
276
|
+
this._reqSensitive = stream._reqSensitive;
|
|
277
|
+
this._method = stream._reqMethod;
|
|
278
|
+
this._path = stream._reqPath;
|
|
279
|
+
this._rawHeaders = undefined;
|
|
268
280
|
this._trailers = { __proto__: null };
|
|
269
281
|
this._rawTrailers = [];
|
|
270
282
|
this._closed = false;
|
|
@@ -288,6 +300,21 @@ class Http2ServerRequest extends Readable {
|
|
|
288
300
|
stream.on('error', onRequestError);
|
|
289
301
|
}
|
|
290
302
|
|
|
303
|
+
// Builds the request object from the stable field list, once. After it, every
|
|
304
|
+
// getter reads the object -- an application that writes into req.headers
|
|
305
|
+
// directly must see the write through the other getters, exactly as when the
|
|
306
|
+
// object was already there.
|
|
307
|
+
_materialize() {
|
|
308
|
+
let h = this._headers;
|
|
309
|
+
if (h === undefined) {
|
|
310
|
+
h = this._stream.session._toObject(this._reqFields, this._reqFields.length,
|
|
311
|
+
this._reqSensitive);
|
|
312
|
+
this._headers = h;
|
|
313
|
+
this._stream._requestHeaders = h;
|
|
314
|
+
}
|
|
315
|
+
return h;
|
|
316
|
+
}
|
|
317
|
+
|
|
291
318
|
get aborted() {
|
|
292
319
|
return this._aborted;
|
|
293
320
|
}
|
|
@@ -300,10 +327,18 @@ class Http2ServerRequest extends Readable {
|
|
|
300
327
|
return this._stream;
|
|
301
328
|
}
|
|
302
329
|
get headers() {
|
|
303
|
-
return this.
|
|
330
|
+
return this._materialize();
|
|
304
331
|
}
|
|
305
332
|
get rawHeaders() {
|
|
306
|
-
|
|
333
|
+
let r = this._rawHeaders;
|
|
334
|
+
if (r === undefined) {
|
|
335
|
+
// Own copy: the field list is also the lazy build's source, so an
|
|
336
|
+
// application mutating rawHeaders must not corrupt it. node's rawHeaders
|
|
337
|
+
// is the array its parser built for this request alone.
|
|
338
|
+
r = this._reqFields.slice();
|
|
339
|
+
this._rawHeaders = r;
|
|
340
|
+
}
|
|
341
|
+
return r;
|
|
307
342
|
}
|
|
308
343
|
get trailers() {
|
|
309
344
|
return this._trailers;
|
|
@@ -337,7 +372,7 @@ class Http2ServerRequest extends Readable {
|
|
|
337
372
|
}
|
|
338
373
|
|
|
339
374
|
get method() {
|
|
340
|
-
return this._headers[':method'];
|
|
375
|
+
return this._headers !== undefined ? this._headers[':method'] : this._method;
|
|
341
376
|
}
|
|
342
377
|
set method(method) {
|
|
343
378
|
if (typeof method !== 'string') {
|
|
@@ -346,28 +381,29 @@ class Http2ServerRequest extends Readable {
|
|
|
346
381
|
if (method.trim() === '') {
|
|
347
382
|
throw err('ERR_INVALID_ARG_VALUE', 'method must not be empty');
|
|
348
383
|
}
|
|
349
|
-
assertWritable(this.
|
|
384
|
+
assertWritable(this._materialize());
|
|
350
385
|
this._headers[':method'] = method;
|
|
351
386
|
}
|
|
352
387
|
|
|
353
388
|
get authority() {
|
|
354
|
-
const
|
|
389
|
+
const h = this._materialize();
|
|
390
|
+
const auth = h[':authority'];
|
|
355
391
|
if (auth !== undefined) {
|
|
356
392
|
return auth;
|
|
357
393
|
}
|
|
358
|
-
const host =
|
|
394
|
+
const host = h.host;
|
|
359
395
|
return host === undefined ? undefined : String(host).replace(/:[\d]+$/, '');
|
|
360
396
|
}
|
|
361
397
|
|
|
362
398
|
get scheme() {
|
|
363
|
-
return this.
|
|
399
|
+
return this._materialize()[':scheme'];
|
|
364
400
|
}
|
|
365
401
|
|
|
366
402
|
get url() {
|
|
367
|
-
return this._headers[':path'];
|
|
403
|
+
return this._headers !== undefined ? this._headers[':path'] : this._path;
|
|
368
404
|
}
|
|
369
405
|
set url(url) {
|
|
370
|
-
assertWritable(this.
|
|
406
|
+
assertWritable(this._materialize());
|
|
371
407
|
this._headers[':path'] = url;
|
|
372
408
|
}
|
|
373
409
|
|
|
@@ -463,9 +499,7 @@ class Http2ServerResponse extends Stream {
|
|
|
463
499
|
finishEmitted: false,
|
|
464
500
|
sendDate: true,
|
|
465
501
|
statusCode: 200,
|
|
466
|
-
headRequest: stream.
|
|
467
|
-
? stream._requestHeaders[':method'] === 'HEAD'
|
|
468
|
-
: false
|
|
502
|
+
headRequest: stream._reqMethod === 'HEAD'
|
|
469
503
|
};
|
|
470
504
|
// A fast null-prototype map, not a { __proto__: null } literal: the
|
|
471
505
|
// response's headers are written once and read once per response, and the
|
|
@@ -920,14 +954,16 @@ class Http2ServerResponse extends Stream {
|
|
|
920
954
|
}
|
|
921
955
|
}
|
|
922
956
|
|
|
923
|
-
// node's default 'stream' handler
|
|
924
|
-
|
|
957
|
+
// node's default 'stream' handler, reworked for the lazy request object: the
|
|
958
|
+
// session calls it directly with the stream, whose stable field list the
|
|
959
|
+
// request builds from at the first read (OPTIMIZATION.md item 49). Called with
|
|
960
|
+
// the server as `this`.
|
|
961
|
+
function onServerStream(stream) {
|
|
925
962
|
const server = this;
|
|
926
|
-
const request = new Http2ServerRequest(stream
|
|
927
|
-
stream._requestHeaders = headers;
|
|
963
|
+
const request = new Http2ServerRequest(stream);
|
|
928
964
|
const response = new Http2ServerResponse(stream);
|
|
929
965
|
|
|
930
|
-
const method =
|
|
966
|
+
const method = stream._reqMethod;
|
|
931
967
|
if (method === 'CONNECT') {
|
|
932
968
|
if (!server.emit('connect', request, response)) {
|
|
933
969
|
response.statusCode = 405;
|
|
@@ -936,8 +972,18 @@ function onServerStream(stream, headers, options, rawHeaders) {
|
|
|
936
972
|
return;
|
|
937
973
|
}
|
|
938
974
|
|
|
939
|
-
|
|
940
|
-
|
|
975
|
+
// expect is not in SINGLE_VALUE_FIELDS (lib/headers.js), so repeated values
|
|
976
|
+
// join the way _toObject joins them; the scan reads the stable list the
|
|
977
|
+
// object has not been built from.
|
|
978
|
+
const fields = stream._reqFields;
|
|
979
|
+
let expect;
|
|
980
|
+
for (let i = 0; i < fields.length; i += 2) {
|
|
981
|
+
if (fields[i] === 'expect') {
|
|
982
|
+
expect = expect === undefined ? fields[i + 1] : `${expect}, ${fields[i + 1]}`;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
if (expect !== undefined) {
|
|
986
|
+
if (expect === '100-continue') {
|
|
941
987
|
if (server.listenerCount('checkContinue') > 0) {
|
|
942
988
|
server.emit('checkContinue', request, response);
|
|
943
989
|
} else {
|
package/lib/fast-tick.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// A batched replacement for process.nextTick, built like lib/fast-timer.js is
|
|
4
|
-
// built: one module, one pump shared by every session in the
|
|
5
|
-
// callback order as the contract. The
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
4
|
+
// built: one module, one class, one pump shared by every session in the
|
|
5
|
+
// process, and the callback order as the contract. The module exports a
|
|
6
|
+
// process-wide instance plus the class itself, so the pump can be lifted into
|
|
7
|
+
// a standalone module by moving the file; sessions never construct their own,
|
|
8
|
+
// because per-instance pumps would put a tick entry back on every server --
|
|
9
|
+
// the cost this exists to remove. The application opts in per server or
|
|
10
|
+
// client with `batchNextTick: true` (default false = node's own
|
|
11
|
+
// process.nextTick); H2LEAN_BATCH_TICKS=1 turns it on wherever the option is
|
|
12
|
+
// left undefined, so the whole suite can run under the pump and pin its event
|
|
13
|
+
// orders.
|
|
9
14
|
//
|
|
10
15
|
// What it changes: instead of one tick-queue entry per callback, callbacks
|
|
11
16
|
// land in a flat array and ONE real nextTick -- the pump -- drains them, so a
|
|
@@ -14,7 +19,19 @@
|
|
|
14
19
|
// isolated while the server-measured marginal of one merged tick is
|
|
15
20
|
// 0.03-0.05 us (OPTIMIZATION.md item 46, the pump's paired numbers in item
|
|
16
21
|
// 47), which is why this removes ALL of a session's entries rather than
|
|
17
|
-
// merging two.
|
|
22
|
+
// merging two. The queues are cursor-filled preallocated stores that reset
|
|
23
|
+
// instead of right-trimming (`length = 0` makes the next fill re-grow the
|
|
24
|
+
// store from scratch on V8, item 38) -- bench/micro/tick-queue-shape.js puts
|
|
25
|
+
// the trimmed shape at 27 ns/schedule against 12 for the cursor store at the
|
|
26
|
+
// 2-entries-per-turn size a quiet connection produces, and shows the class
|
|
27
|
+
// method layer costing nothing over the flat cursor. The scheduler itself is
|
|
28
|
+
// a constructor closure rather than a bound method -- the reason is at the
|
|
29
|
+
// closure. The literal FlatList API
|
|
30
|
+
// (one push() per item) measured slower than the pair write, and its
|
|
31
|
+
// setImmediate shrink would rebuild a store that can already hold the next
|
|
32
|
+
// generation's entries -- so the technique is re-implemented here with a
|
|
33
|
+
// release-when-idle rule (an oversized store is replaced only after its
|
|
34
|
+
// generation has drained), not by requiring lib/flat-list.js.
|
|
18
35
|
//
|
|
19
36
|
// What it keeps: FIFO order exactly as node's single queue gives it --
|
|
20
37
|
// including callbacks scheduled while the pump drains, which land in the next
|
|
@@ -32,9 +49,18 @@
|
|
|
32
49
|
// when it was scheduled, so an application reading AsyncLocalStorage inside
|
|
33
50
|
// a deferred 'finish' handler needs the default mode.
|
|
34
51
|
//
|
|
52
|
+
// The callback shape is (fn, arg), the shape every h2lean site schedules
|
|
53
|
+
// through: one argument, no per-callback closure or argument array, which is
|
|
54
|
+
// the allocation the entry price is made of. A site that needs more
|
|
55
|
+
// arguments -- the pushStream callback, the error paths -- wraps them in a
|
|
56
|
+
// closure at that site, the way the rare paths always have. A variadic
|
|
57
|
+
// schedule(callback, ...args) would allocate an args array per call and give
|
|
58
|
+
// back what the pump removes.
|
|
59
|
+
//
|
|
35
60
|
// On an exception inside the pump, the throwing pair is dropped and the
|
|
36
|
-
// callbacks after it are carried into the next generation
|
|
37
|
-
//
|
|
61
|
+
// callbacks after it are carried into the next generation -- ahead of
|
|
62
|
+
// whatever the aborted drain scheduled there, the order node's tail-of-the-
|
|
63
|
+
// queue semantics give them. Measured on a live node: an exception escaping a
|
|
38
64
|
// tick aborts that turn's drain -- the ticks behind it stay in the queue and
|
|
39
65
|
// run at the next tick phase only if the event loop gets there (a pending
|
|
40
66
|
// tick queue alone does not start a turn, and without an uncaughtException
|
|
@@ -44,10 +70,6 @@
|
|
|
44
70
|
// this._tick = fastTick.resolve(opts); // schedule | process.nextTick
|
|
45
71
|
// this._tick(fn, arg); // (fn, arg) shape, like nextTick
|
|
46
72
|
|
|
47
|
-
let ready = []; // callbacks awaiting the next pump run: [fn, arg, fn, arg, ...]
|
|
48
|
-
let idle = []; // the recycled array of the generation just drained
|
|
49
|
-
let scheduled = false; // a pump entry exists in the real tick queue
|
|
50
|
-
|
|
51
73
|
// Read at resolve() time, not at module load, so a process can turn the mode
|
|
52
74
|
// on after the library is required (the suite gate runs the whole tree under
|
|
53
75
|
// the pump this way). One env getter per session construction, never per tick.
|
|
@@ -55,61 +77,116 @@ function envForced() {
|
|
|
55
77
|
return process.env.H2LEAN_BATCH_TICKS === '1';
|
|
56
78
|
}
|
|
57
79
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const rest = list.slice(i + 2);
|
|
84
|
-
for (let k = rest.length - 1; k >= 0; k--) {
|
|
85
|
-
ready.unshift(rest[k]);
|
|
80
|
+
class FastTick {
|
|
81
|
+
// 64 slots = 32 pairs per generation before the store grows; at saturation
|
|
82
|
+
// the generations reach a few dozen entries, at low load they hold one or
|
|
83
|
+
// two -- the microbench's losing size, which is exactly what the cursor
|
|
84
|
+
// store fixes.
|
|
85
|
+
constructor(capacity) {
|
|
86
|
+
this.capacity = capacity;
|
|
87
|
+
this.ready = new Array(capacity); // [fn, arg, fn, arg, ...] up to readyCount
|
|
88
|
+
this.readyCount = 0;
|
|
89
|
+
this.idle = new Array(capacity); // the drained store, empty between turns
|
|
90
|
+
this.scheduled = false; // a pump entry exists in the real tick queue
|
|
91
|
+
// The reference the sessions hold: one closure per process, never per
|
|
92
|
+
// schedule. Sessions must not mix routes -- a pump generation reordering
|
|
93
|
+
// against its own native entries is the incoherence resolve() exists to
|
|
94
|
+
// prevent -- so the resolved reference is copied through streams and the
|
|
95
|
+
// compat objects unchanged. A closure rather than a bound method: one
|
|
96
|
+
// call frame instead of two, and a raw call-shape probe prices the bound
|
|
97
|
+
// wrapper at ~7 ns against ~5 for the closure. Paired rounds of this tree
|
|
98
|
+
// against the shipped flat pump, both under the pump, put the whole
|
|
99
|
+
// conversion within noise on all three server arms -- the shape claim is
|
|
100
|
+
// the probe's, not the server's.
|
|
101
|
+
this.schedule = (fn, arg) => {
|
|
102
|
+
const c = this.readyCount;
|
|
103
|
+
if (c + 2 > this.ready.length) {
|
|
104
|
+
this._grow(c);
|
|
86
105
|
}
|
|
106
|
+
const store = this.ready;
|
|
107
|
+
store[c] = fn;
|
|
108
|
+
store[c + 1] = arg;
|
|
109
|
+
this.readyCount = c + 2;
|
|
110
|
+
if (!this.scheduled) {
|
|
111
|
+
this.scheduled = true;
|
|
112
|
+
process.nextTick(this._pumpBound);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
this._pumpBound = this.pump.bind(this);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
_grow(count) {
|
|
119
|
+
// Rare: a generation larger than the preallocation. The store is replaced
|
|
120
|
+
// under the caller with the live entries carried over; the oversized size
|
|
121
|
+
// is given back when the store finishes draining (see pump's finally).
|
|
122
|
+
const old = this.ready;
|
|
123
|
+
const bigger = new Array(old.length * 2);
|
|
124
|
+
for (let i = 0; i < count; i++) {
|
|
125
|
+
bigger[i] = old[i];
|
|
87
126
|
}
|
|
88
|
-
|
|
89
|
-
scheduled = true;
|
|
90
|
-
process.nextTick(pump);
|
|
91
|
-
}
|
|
92
|
-
throw err;
|
|
93
|
-
} finally {
|
|
94
|
-
list.length = 0;
|
|
127
|
+
this.ready = bigger;
|
|
95
128
|
}
|
|
96
|
-
}
|
|
97
129
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
130
|
+
pump() {
|
|
131
|
+
this.scheduled = false;
|
|
132
|
+
const items = this.ready;
|
|
133
|
+
const count = this.readyCount;
|
|
134
|
+
// Re-entrant schedules during the drain push into `this.ready` -- the
|
|
135
|
+
// next generation -- and queue the next pump, which is node's
|
|
136
|
+
// tail-of-the-queue semantics: a callback scheduled while tick A runs
|
|
137
|
+
// executes after the ticks that were queued before A did.
|
|
138
|
+
this.ready = this.idle;
|
|
139
|
+
this.readyCount = 0;
|
|
140
|
+
this.idle = items;
|
|
141
|
+
let i = 0;
|
|
142
|
+
try {
|
|
143
|
+
while (i < count) {
|
|
144
|
+
items[i](items[i + 1]);
|
|
145
|
+
i += 2;
|
|
146
|
+
}
|
|
147
|
+
} catch (err) {
|
|
148
|
+
// The ticks behind the throwing one stay queued on node too -- the
|
|
149
|
+
// drain aborts, the leftovers run at the next tick phase, if the loop
|
|
150
|
+
// gets there. Carry ours to the FRONT of the next generation, ahead of
|
|
151
|
+
// whatever the aborted drain scheduled there, and make sure a pump is
|
|
152
|
+
// queued. The failed pair is gone the way node's throwing tick is gone
|
|
153
|
+
// -- carrying it too re-runs the thrower at the head of every later
|
|
154
|
+
// generation and starves the survivors.
|
|
155
|
+
const rest = count - (i + 2);
|
|
156
|
+
if (rest > 0) {
|
|
157
|
+
const ahead = this.readyCount;
|
|
158
|
+
if (ahead > 0) {
|
|
159
|
+
this.ready.copyWithin(rest, 0, ahead);
|
|
160
|
+
}
|
|
161
|
+
for (let k = 0; k < rest; k++) {
|
|
162
|
+
this.ready[k] = items[i + 2 + k];
|
|
163
|
+
}
|
|
164
|
+
this.readyCount = ahead + rest;
|
|
165
|
+
}
|
|
166
|
+
if (!this.scheduled && this.readyCount !== 0) {
|
|
167
|
+
this.scheduled = true;
|
|
168
|
+
process.nextTick(this._pumpBound);
|
|
169
|
+
}
|
|
170
|
+
throw err;
|
|
171
|
+
} finally {
|
|
172
|
+
// The drained store is idle: empty in the cursor sense (readyCount of
|
|
173
|
+
// the other store is 0), and if one generation grew it past the
|
|
174
|
+
// preallocation, the size is released now -- the same
|
|
175
|
+
// one-oversized-generation-does-not-pin-it rule FlatList applies to
|
|
176
|
+
// header blocks (lib/flat-list.js), at the point where the store is
|
|
177
|
+
// provably empty.
|
|
178
|
+
if (items.length > this.capacity) {
|
|
179
|
+
this.idle = new Array(this.capacity);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
103
182
|
}
|
|
104
|
-
}
|
|
105
183
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// itself is one instance for the whole process: sessions in batch mode share
|
|
110
|
-
// it, sessions in the default mode never meet it.
|
|
111
|
-
function resolve(opts) {
|
|
112
|
-
return ((opts && opts.batchNextTick) ?? envForced()) ? schedule : process.nextTick;
|
|
184
|
+
resolve(opts) {
|
|
185
|
+
return ((opts && opts.batchNextTick) ?? envForced()) ? this.schedule : process.nextTick;
|
|
186
|
+
}
|
|
113
187
|
}
|
|
114
188
|
|
|
115
|
-
|
|
189
|
+
// One pump for the whole process; the class is exported beside it so the
|
|
190
|
+
// module can be lifted out as-is.
|
|
191
|
+
module.exports = new FastTick(64);
|
|
192
|
+
module.exports.FastTick = FastTick;
|
package/lib/server.js
CHANGED
|
@@ -127,6 +127,17 @@ function initHttp2Server(server, options) {
|
|
|
127
127
|
};
|
|
128
128
|
server._sessions = new Set();
|
|
129
129
|
server[kCompat] = null;
|
|
130
|
+
// node arms its compat layer through a newListener hook (setupCompat,
|
|
131
|
+
// internal/http2/core.js) rather than by wrapping the registration methods, so
|
|
132
|
+
// on()/once()/addListener() all count. The same hook arms the 'stream' bridge,
|
|
133
|
+
// which the direct compat delivery below depends on knowing about.
|
|
134
|
+
server.on('newListener', (event) => {
|
|
135
|
+
if (event === 'request') {
|
|
136
|
+
server._setupCompat();
|
|
137
|
+
} else if (event === 'stream') {
|
|
138
|
+
server._setupStreamBridge();
|
|
139
|
+
}
|
|
140
|
+
});
|
|
130
141
|
// node validates this as uint32 and defaults it to 10000 (initializeOptions, core.js);
|
|
131
142
|
// it bounds how long an unknown-protocol socket may sit after the 403 text.
|
|
132
143
|
if (opts.unknownProtocolTimeout !== undefined &&
|
|
@@ -169,8 +180,25 @@ function attachSession(server, socket) {
|
|
|
169
180
|
}
|
|
170
181
|
});
|
|
171
182
|
}
|
|
172
|
-
session
|
|
173
|
-
|
|
183
|
+
// The session->server bridge for the application's 'stream' listeners, and the
|
|
184
|
+
// compat delivery decision. Two shapes, exactly one per session: with 'stream'
|
|
185
|
+
// listeners the session emits the built object and compat rides the server's
|
|
186
|
+
// 'stream' event behind the bridge (the listener order node has, since node
|
|
187
|
+
// subscribes onServerStream on the server); without them the session calls the
|
|
188
|
+
// compat entry directly with the stable field list, which is what lets the
|
|
189
|
+
// request object come into being at the first req.headers read instead of at
|
|
190
|
+
// frame decode (OPTIMIZATION.md item 49).
|
|
191
|
+
if (server._streamBridgeArmed === true) {
|
|
192
|
+
session.on('stream', (stream, headers, options, rawHeaders) =>
|
|
193
|
+
server.emit('stream', stream, headers, options, rawHeaders));
|
|
194
|
+
}
|
|
195
|
+
if (server[kCompat] !== null) {
|
|
196
|
+
if (server._streamBridgeArmed === true) {
|
|
197
|
+
server._armCompatChain();
|
|
198
|
+
} else {
|
|
199
|
+
session._onStreamCompat = server[kCompat];
|
|
200
|
+
}
|
|
201
|
+
}
|
|
174
202
|
session.on('error', (err) => {
|
|
175
203
|
if (server.listenerCount('sessionError') > 0) {
|
|
176
204
|
server.emit('sessionError', err);
|
|
@@ -188,28 +216,57 @@ function applyHttp2ServerApi(proto) {
|
|
|
188
216
|
|
|
189
217
|
// node instantiates the compat req/res only when the application asks for
|
|
190
218
|
// them: a plain createServer() with a 'stream' handler never pays for two
|
|
191
|
-
// objects per request. Adding the first 'request' listener arms it
|
|
219
|
+
// objects per request. Adding the first 'request' listener arms it, via the
|
|
220
|
+
// newListener hook node itself uses (setupCompat, internal/http2/core.js).
|
|
192
221
|
proto._setupCompat = function () {
|
|
193
222
|
if (this[kCompat] !== null) {
|
|
194
223
|
return this;
|
|
195
224
|
}
|
|
196
225
|
this[kCompat] = onServerStream.bind(this);
|
|
197
226
|
this._sessionOptions.compat = true;
|
|
227
|
+
// Both delivery shapes need the raw field list: the chain hands it out as
|
|
228
|
+
// the fourth 'stream' argument, the direct call builds from it. A session
|
|
229
|
+
// that already carries the 'stream' bridge keeps compat on the server's
|
|
230
|
+
// 'stream' event; the rest take the direct call.
|
|
198
231
|
for (const session of this._sessions) {
|
|
199
232
|
session._wantRaw = true;
|
|
200
233
|
}
|
|
201
|
-
|
|
234
|
+
if (this._streamBridgeArmed === true) {
|
|
235
|
+
this._armCompatChain();
|
|
236
|
+
} else {
|
|
237
|
+
for (const session of this._sessions) {
|
|
238
|
+
session._onStreamCompat = this[kCompat];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return this;
|
|
202
242
|
};
|
|
203
243
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
244
|
+
// The bridge for the application's own 'stream' listeners, attached to the
|
|
245
|
+
// sessions that already exist (new sessions take it at accept, see
|
|
246
|
+
// attachSession). Arming it also moves the sessions that were in direct
|
|
247
|
+
// compat delivery onto the chain, so the listener order on the server's
|
|
248
|
+
// 'stream' event is the one node has however the two registrations interleave.
|
|
249
|
+
proto._setupStreamBridge = function () {
|
|
250
|
+
if (this._streamBridgeArmed === true) {
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
this._streamBridgeArmed = true;
|
|
254
|
+
for (const session of this._sessions) {
|
|
255
|
+
session.on('stream', (stream, headers, options, rawHeaders) =>
|
|
256
|
+
this.emit('stream', stream, headers, options, rawHeaders));
|
|
257
|
+
session._onStreamCompat = null;
|
|
207
258
|
}
|
|
208
|
-
|
|
259
|
+
if (this[kCompat] !== null) {
|
|
260
|
+
this._armCompatChain();
|
|
261
|
+
}
|
|
262
|
+
return this;
|
|
209
263
|
};
|
|
210
264
|
|
|
211
|
-
proto.
|
|
212
|
-
|
|
265
|
+
proto._armCompatChain = function () {
|
|
266
|
+
if (this._compatChained !== true) {
|
|
267
|
+
this._compatChained = true;
|
|
268
|
+
proto[kTransportOn].call(this, 'stream', this[kCompat]);
|
|
269
|
+
}
|
|
213
270
|
};
|
|
214
271
|
|
|
215
272
|
proto.close = function (cb) {
|
package/lib/session.js
CHANGED
|
@@ -771,6 +771,11 @@ class Http2Session extends EventEmitter {
|
|
|
771
771
|
// Copying it is a per-request allocation, so it only happens for servers
|
|
772
772
|
// that actually listen for 'request'.
|
|
773
773
|
this._wantRaw = opts.compat === true;
|
|
774
|
+
// The compat entry point the accept path calls directly with the stable
|
|
775
|
+
// field list (server.js _setupCompat); null on a session no server owns and
|
|
776
|
+
// on a server whose application listens for 'stream' only -- the object is
|
|
777
|
+
// then built for the 'stream' emit and the event chain delivers it.
|
|
778
|
+
this._onStreamCompat = null;
|
|
774
779
|
|
|
775
780
|
// Outbound frames go to the socket as one corked writev batch. A body is queued as
|
|
776
781
|
// the buffers that already hold it -- a frame header and a view of the
|
|
@@ -2698,11 +2703,44 @@ class Http2Session extends EventEmitter {
|
|
|
2698
2703
|
return;
|
|
2699
2704
|
}
|
|
2700
2705
|
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
+
// The request object is built here only when a reader is already known: a
|
|
2707
|
+
// session 'stream' listener takes it as the event argument, and the compat
|
|
2708
|
+
// request materializes it at the first req.headers access (lib/compat.js),
|
|
2709
|
+
// so a handler that only consumes the body and routes on req.url pays no
|
|
2710
|
+
// per-request object -- the paired marginal is OPTIMIZATION.md item 49. The
|
|
2711
|
+
// build still happens eagerly under shareHeaders, whose frozen shared
|
|
2712
|
+
// objects are the point of that option.
|
|
2713
|
+
const sensitive = neverNames(fields, fcount, blockFlags);
|
|
2714
|
+
const headers = shared !== null || this.listenerCount('stream') > 0 || this._share
|
|
2715
|
+
? (shared !== null ? shared
|
|
2716
|
+
: (this._share
|
|
2717
|
+
? Object.freeze(this._toObject(fields, fcount, sensitive))
|
|
2718
|
+
: this._toObject(fields, fcount, sensitive)))
|
|
2719
|
+
: undefined;
|
|
2720
|
+
// What the stack reads without the object: :method (the HEAD checks in
|
|
2721
|
+
// lib/stream.js and the compat CONNECT check), :path (req.url) and
|
|
2722
|
+
// content-length (the promise checked below). When the object was built
|
|
2723
|
+
// these are its own property reads, exactly what the eager code did; the
|
|
2724
|
+
// one-pass scan is the lazy path's only walk, and it takes the first copy
|
|
2725
|
+
// of a repeated field, the way the single-value field lands in the object.
|
|
2726
|
+
let reqMethod;
|
|
2727
|
+
let reqPath;
|
|
2728
|
+
let promised;
|
|
2729
|
+
if (headers !== undefined) {
|
|
2730
|
+
reqMethod = headers[':method'];
|
|
2731
|
+
promised = headers['content-length'];
|
|
2732
|
+
} else {
|
|
2733
|
+
for (let i = 0; i < fcount; i += 2) {
|
|
2734
|
+
const name = fields[i];
|
|
2735
|
+
if (name === ':method') {
|
|
2736
|
+
reqMethod = fields[i + 1];
|
|
2737
|
+
} else if (name === ':path') {
|
|
2738
|
+
reqPath = fields[i + 1];
|
|
2739
|
+
} else if (name === 'content-length' && promised === undefined) {
|
|
2740
|
+
promised = fields[i + 1];
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2706
2744
|
// The one shape in which the content-length promise is already broken before a single body
|
|
2707
2745
|
// octet could have arrived: the field promises a body and the HEADERS frame ends the message.
|
|
2708
2746
|
// Refusing at the accept decision -- the same place the parse failures and the size limits are
|
|
@@ -2712,7 +2750,7 @@ class Http2Session extends EventEmitter {
|
|
|
2712
2750
|
// Letting it through to Http2Stream._remoteEnded() instead resets the stream and then hands
|
|
2713
2751
|
// the handler a destroyed one, and a handler that answers it puts a response on the wire for a
|
|
2714
2752
|
// request this side has already refused.
|
|
2715
|
-
if (end &&
|
|
2753
|
+
if (end && promised !== undefined && +promised > 0) {
|
|
2716
2754
|
this.sendRst(sid, PROTOCOL_ERROR);
|
|
2717
2755
|
// The frame that carried the refusal also carried the peer's END_STREAM, so the message is
|
|
2718
2756
|
// over: the DATA that follows it (or a second HEADERS) is the error of 5.1 and not the
|
|
@@ -2726,15 +2764,21 @@ class Http2Session extends EventEmitter {
|
|
|
2726
2764
|
return;
|
|
2727
2765
|
}
|
|
2728
2766
|
const stream = new Http2Stream(this, sid);
|
|
2729
|
-
stream.
|
|
2767
|
+
stream._reqMethod = reqMethod;
|
|
2768
|
+
stream._reqPath = reqPath;
|
|
2769
|
+
stream._reqSensitive = sensitive;
|
|
2770
|
+
// node's argument order; the field list is the session's shared decode array
|
|
2771
|
+
// or a cached one, so the raw list handed out is always a copy. In the compat
|
|
2772
|
+
// path the copy is also the stable list the lazy request object builds from
|
|
2773
|
+
// (the scratch's backing array is cleared in place by the next decode), so it
|
|
2774
|
+
// travels with the stream even when no emit will carry it.
|
|
2775
|
+
const raw = this._wantRaw ? fields.slice(0, fcount) : null;
|
|
2776
|
+
stream._reqFields = raw;
|
|
2777
|
+
if (headers !== undefined) {
|
|
2778
|
+
stream._requestHeaders = headers;
|
|
2779
|
+
}
|
|
2730
2780
|
this._streams.set(sid, stream);
|
|
2731
2781
|
this._liveStreams++;
|
|
2732
|
-
// content-length is a promise about the body, and the two checks that hold the peer to it
|
|
2733
|
-
// (see _onDataFrame and Http2Stream._remoteEnded) need it as a number. The inbound
|
|
2734
|
-
// validator has already proved the value is all digits, so this is a property read and a
|
|
2735
|
-
// coercion -- and -1, the "nothing was promised" marker, costs one comparison per DATA
|
|
2736
|
-
// frame for the requests that never sent the field at all.
|
|
2737
|
-
const promised = headers['content-length'];
|
|
2738
2782
|
if (promised !== undefined) {
|
|
2739
2783
|
stream._expLen = +promised;
|
|
2740
2784
|
}
|
|
@@ -2744,10 +2788,15 @@ class Http2Session extends EventEmitter {
|
|
|
2744
2788
|
if (end) {
|
|
2745
2789
|
stream._remoteEnded();
|
|
2746
2790
|
}
|
|
2747
|
-
//
|
|
2748
|
-
//
|
|
2749
|
-
|
|
2750
|
-
|
|
2791
|
+
// Delivery turns on who is listening, not on whether an object was built:
|
|
2792
|
+
// with shareHeaders on the object exists even when no 'stream' listener
|
|
2793
|
+
// does, and emitting it into a listenerless session would drop the request
|
|
2794
|
+
// the compat callback was meant to receive.
|
|
2795
|
+
if (this.listenerCount('stream') > 0) {
|
|
2796
|
+
this.emit('stream', stream, headers, end ? OPT_END_STREAM : OPT_NO_END_STREAM, raw);
|
|
2797
|
+
} else if (this._onStreamCompat !== null) {
|
|
2798
|
+
this._onStreamCompat(stream);
|
|
2799
|
+
}
|
|
2751
2800
|
}
|
|
2752
2801
|
|
|
2753
2802
|
// node's toHeaderObject semantics: null prototype, :status coerced to a
|
package/lib/stream.js
CHANGED
|
@@ -405,8 +405,21 @@ class Http2Stream extends Stream {
|
|
|
405
405
|
this._sentStatus = 0;
|
|
406
406
|
this._sentDate = undefined;
|
|
407
407
|
this._sentView = undefined;
|
|
408
|
-
|
|
408
|
+
// sentInfoHeaders stays unset until the first informational block: node's
|
|
409
|
+
// kInfoHeaders is created by the first additionalHeaders() call
|
|
410
|
+
// (internal/http2/core.js:3285) and the getter reads undefined before it.
|
|
409
411
|
this.sentTrailers = undefined;
|
|
412
|
+
// The inbound request, as the session's accept path scanned it off the
|
|
413
|
+
// field list in one pass (lib/session.js): the :method the HEAD checks
|
|
414
|
+
// here and the compat CONNECT check read, :path for req.url, and the
|
|
415
|
+
// stable list the compat request object builds from at the first read
|
|
416
|
+
// (OPTIMIZATION.md item 49). _reqFields is null when no compat server
|
|
417
|
+
// owns the session and no raw list was asked for; _requestHeaders above
|
|
418
|
+
// stays unset until the object exists.
|
|
419
|
+
this._reqFields = null;
|
|
420
|
+
this._reqMethod = undefined;
|
|
421
|
+
this._reqPath = undefined;
|
|
422
|
+
this._reqSensitive = undefined;
|
|
410
423
|
this.headersSent = false;
|
|
411
424
|
this.pending = true;
|
|
412
425
|
this.aborted = false;
|
|
@@ -670,7 +683,7 @@ class Http2Stream extends Stream {
|
|
|
670
683
|
// request line. Same fact in h2lean, read where respond() tests it already: the flag
|
|
671
684
|
// source is the request map, so there is no state to keep out of sync.
|
|
672
685
|
get headRequest() {
|
|
673
|
-
return this.
|
|
686
|
+
return this._reqMethod === 'HEAD';
|
|
674
687
|
}
|
|
675
688
|
|
|
676
689
|
respond(headers, options) {
|
|
@@ -699,7 +712,7 @@ class Http2Stream extends Stream {
|
|
|
699
712
|
// HEADERS. Measured on the wire against live node: node puts
|
|
700
713
|
// HEADERS/ES/EH and no DATA for both shapes; h2lean used to leave a regular
|
|
701
714
|
// HEAD answer open and send the body, which nghttp2 reads as a protocol error.
|
|
702
|
-
const head = this.
|
|
715
|
+
const head = this._reqMethod === 'HEAD';
|
|
703
716
|
// node puts a `date` on every response of this API too: prepareResponseHeadersObject()
|
|
704
717
|
// (internal/http2/core.js:2702) adds one unless the application set one itself or
|
|
705
718
|
// `options.sendDate` says otherwise -- and its test is `sendDate == null || sendDate`,
|
|
@@ -781,8 +794,7 @@ class Http2Stream extends Stream {
|
|
|
781
794
|
this.ownsFd = false;
|
|
782
795
|
const sendDate = opts.sendDate == null ? true : !!opts.sendDate;
|
|
783
796
|
const { headers: prepared, statusCode } = prepareResponseHeadersObject(headers);
|
|
784
|
-
const head =
|
|
785
|
-
this._requestHeaders !== undefined && this._requestHeaders[':method'] === 'HEAD';
|
|
797
|
+
const head = this._reqMethod === 'HEAD';
|
|
786
798
|
if (statusCode === 204 || statusCode === 205 || statusCode === 304 || head) {
|
|
787
799
|
throw errorOf('ERR_HTTP2_PAYLOAD_FORBIDDEN',
|
|
788
800
|
`Responses with ${statusCode} status must not have a payload`);
|
|
@@ -834,8 +846,7 @@ class Http2Stream extends Stream {
|
|
|
834
846
|
const sendDate = opts.sendDate == null ? true : !!opts.sendDate;
|
|
835
847
|
const { headers: prepared, statusCode, isArray } =
|
|
836
848
|
prepareResponseHeadersObject(headers);
|
|
837
|
-
const head =
|
|
838
|
-
this._requestHeaders !== undefined && this._requestHeaders[':method'] === 'HEAD';
|
|
849
|
+
const head = this._reqMethod === 'HEAD';
|
|
839
850
|
if (statusCode === 204 || statusCode === 205 || statusCode === 304 || head) {
|
|
840
851
|
throw errorOf('ERR_HTTP2_PAYLOAD_FORBIDDEN',
|
|
841
852
|
`Responses with ${statusCode} status must not have a payload`);
|
|
@@ -1321,7 +1332,15 @@ class Http2Stream extends Stream {
|
|
|
1321
1332
|
// (101 has its own error), there is no default status and no generated date.
|
|
1322
1333
|
const block = hdr.block(headers, { info: true }, this.session);
|
|
1323
1334
|
const bound = hdr.sent.bound;
|
|
1324
|
-
|
|
1335
|
+
// node stores a null-proto copy of each informational block, created at the
|
|
1336
|
+
// first call (internal/http2/core.js:3265, 3285) -- the caller's object is
|
|
1337
|
+
// not retained, and the array does not exist before the first call.
|
|
1338
|
+
const copy = Object.assign({ __proto__: null }, headers);
|
|
1339
|
+
if (this.sentInfoHeaders === undefined) {
|
|
1340
|
+
this.sentInfoHeaders = [copy];
|
|
1341
|
+
} else {
|
|
1342
|
+
this.sentInfoHeaders.push(copy);
|
|
1343
|
+
}
|
|
1325
1344
|
this.session.sendHeaders(this.id, block, false, bound);
|
|
1326
1345
|
return this;
|
|
1327
1346
|
}
|
|
@@ -1397,17 +1416,36 @@ class Http2Stream extends Stream {
|
|
|
1397
1416
|
|
|
1398
1417
|
// Defaults from the parent request (node: core.js pushStream fills :method GET,
|
|
1399
1418
|
// :authority and :scheme from the request, :path '/'). The headers object is copied
|
|
1400
|
-
// into a null-proto map so the application's object is not mutated.
|
|
1401
|
-
|
|
1419
|
+
// into a null-proto map so the application's object is not mutated. The parent's
|
|
1420
|
+
// values come off the request object when it exists and off the stable field list
|
|
1421
|
+
// otherwise -- the compat request object may not have been built at all
|
|
1422
|
+
// (OPTIMIZATION.md item 49), and a pseudo-header cannot repeat, so a first match
|
|
1423
|
+
// on the list is the value the object would carry.
|
|
1402
1424
|
const h = Object.assign(Object.create(null), headers);
|
|
1403
1425
|
if (h[':method'] === undefined) {
|
|
1404
1426
|
h[':method'] = 'GET';
|
|
1405
1427
|
}
|
|
1406
|
-
|
|
1407
|
-
|
|
1428
|
+
let parentAuthority;
|
|
1429
|
+
let parentScheme;
|
|
1430
|
+
const src = this._requestHeaders;
|
|
1431
|
+
if (src !== undefined) {
|
|
1432
|
+
parentAuthority = src[':authority'];
|
|
1433
|
+
parentScheme = src[':scheme'];
|
|
1434
|
+
} else {
|
|
1435
|
+
const fields = this._reqFields;
|
|
1436
|
+
for (let i = 0; i < fields.length; i += 2) {
|
|
1437
|
+
if (fields[i] === ':authority') {
|
|
1438
|
+
parentAuthority = fields[i + 1];
|
|
1439
|
+
} else if (fields[i] === ':scheme') {
|
|
1440
|
+
parentScheme = fields[i + 1];
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
if (h[':authority'] === undefined && parentAuthority !== undefined) {
|
|
1445
|
+
h[':authority'] = parentAuthority;
|
|
1408
1446
|
}
|
|
1409
|
-
if (h[':scheme'] === undefined &&
|
|
1410
|
-
h[':scheme'] =
|
|
1447
|
+
if (h[':scheme'] === undefined && parentScheme !== undefined) {
|
|
1448
|
+
h[':scheme'] = parentScheme;
|
|
1411
1449
|
}
|
|
1412
1450
|
if (h[':path'] === undefined) {
|
|
1413
1451
|
h[':path'] = '/';
|
|
@@ -1448,6 +1486,7 @@ class Http2Stream extends Stream {
|
|
|
1448
1486
|
// the getter above hands it back unchanged.
|
|
1449
1487
|
stream._sentView = h;
|
|
1450
1488
|
stream._requestHeaders = h;
|
|
1489
|
+
stream._reqMethod = h[':method'];
|
|
1451
1490
|
this.session._streams.set(promisedId, stream);
|
|
1452
1491
|
this.session._liveStreams++;
|
|
1453
1492
|
stream._remoteEnded();
|
package/package.json
CHANGED