h2lean 0.11.3 → 0.16.0
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 +1 -1
- package/index.js +79 -19
- package/lib/client-session.js +260 -187
- package/lib/client-stream.js +2188 -147
- package/lib/client.js +9 -1
- package/lib/frames.js +25 -1
- package/lib/headers.js +4 -3
- package/lib/pieces.js +3 -1
- package/lib/server.js +58 -18
- package/lib/session.js +981 -119
- package/lib/stream.js +160 -25
- package/package.json +1 -1
package/lib/client.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, received, outOfRangeValue } = require('./session');
|
|
20
|
+
const { strictSingleValueFields, received, outOfRangeValue, validateRemoteCustomSettings } = require('./session');
|
|
21
21
|
|
|
22
22
|
function notImplemented(what) {
|
|
23
23
|
const err = new Error(`h2lean: not implemented: ${what}`);
|
|
@@ -44,6 +44,14 @@ 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
|
+
// remoteCustomSettings rides validateSessionOptions too: a non-array is
|
|
48
|
+
// ERR_INVALID_ARG_TYPE ('...must be an instance of Array'), more than
|
|
49
|
+
// MAX_ADDITIONAL_SETTINGS entries the plain ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS, and
|
|
50
|
+
// the array is copied, node's [...remoteCustomSettings]. The per-entry checks wait
|
|
51
|
+
// for the setup call -- the session 'error' path for a connecting socket, an
|
|
52
|
+
// uncaught throw out of connect() for an already-connected one (both measured).
|
|
53
|
+
validateRemoteCustomSettings(options);
|
|
54
|
+
|
|
47
55
|
// core.js connect(): maxOriginSetSize is client-only and defaults to 128 (min 0).
|
|
48
56
|
// It bounds how many ORIGIN frames the session accumulates into originSet before
|
|
49
57
|
// destroying itself with ERR_HTTP2_TOO_MANY_ORIGINS. Validated here (before the
|
package/lib/frames.js
CHANGED
|
@@ -118,6 +118,30 @@ function trimPadding(pieces) {
|
|
|
118
118
|
return total;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
// Move src[srcOff, srcEnd) into dst at dstOff, choosing by length. buffer.copy is not
|
|
122
|
+
// a bare memmove: copyImpl runs two ArrayBufferIsView tests, three integer
|
|
123
|
+
// normalizations and the range checks in the JS layer first, and below a few tens of
|
|
124
|
+
// octets that floor is the whole cost. Measured (OPTIMIZATION.md item 56, node
|
|
125
|
+
// v26.12.1, unaligned sources).
|
|
126
|
+
//
|
|
127
|
+
// Bounds are exact and the caller owns them: unlike copy, the stores do not clamp to
|
|
128
|
+
// src.length, so passing a range that runs off the end writes zeros rather than
|
|
129
|
+
// truncating. Every call site here has its range checked (a frame length validated
|
|
130
|
+
// before the payload is touched, or a length the caller computed itself). No return
|
|
131
|
+
// value: the two branches have different ones and no caller needs either.
|
|
132
|
+
const COPY_LOOP_MAX = 32;
|
|
133
|
+
|
|
134
|
+
function copyInto(dst, dstOff, src, srcOff, srcEnd) {
|
|
135
|
+
const n = srcEnd - srcOff;
|
|
136
|
+
if (n < COPY_LOOP_MAX) {
|
|
137
|
+
for (let i = 0; i < n; i++) {
|
|
138
|
+
dst[dstOff + i] = src[srcOff + i];
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
src.copy(dst, dstOff, srcOff, srcEnd);
|
|
143
|
+
}
|
|
144
|
+
|
|
121
145
|
module.exports = {
|
|
122
146
|
PREFACE, PREFACE_LEN, HEADER_LEN,
|
|
123
147
|
DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY,
|
|
@@ -129,7 +153,7 @@ module.exports = {
|
|
|
129
153
|
ERROR_NAMES,
|
|
130
154
|
ST_IDLE, ST_OPEN, ST_RESERVED_LOCAL, ST_RESERVED_REMOTE,
|
|
131
155
|
ST_HALF_CLOSED_LOCAL, ST_HALF_CLOSED_REMOTE, ST_CLOSED,
|
|
132
|
-
readHeader, writeHeader, trimPadding,
|
|
156
|
+
readHeader, writeHeader, trimPadding, copyInto,
|
|
133
157
|
get FH_LEN() {
|
|
134
158
|
return FH_LEN;
|
|
135
159
|
},
|
package/lib/headers.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
'use strict';
|
|
15
15
|
|
|
16
16
|
const { Encoder } = require('./hpack');
|
|
17
|
+
const { copyInto } = require('./frames');
|
|
17
18
|
const { sensitiveHeaders: KSensitiveHeaders } = require('node:http2');
|
|
18
19
|
|
|
19
20
|
// A header map with no prototype.
|
|
@@ -570,14 +571,14 @@ function block(headers, opts, session) {
|
|
|
570
571
|
const at = update === null ? 0 : update.length;
|
|
571
572
|
const end = encodeAt(parsed, enc, at);
|
|
572
573
|
if (at !== 0) {
|
|
573
|
-
|
|
574
|
+
copyInto(scratch, 0, update, 0, update.length);
|
|
574
575
|
}
|
|
575
576
|
if (cacheable && enc.epoch === epoch && !enc.unsettled) {
|
|
576
577
|
// a cached block has to outlive the scratch, so only the cached path copies --
|
|
577
578
|
// allocUnsafe, not Buffer.from(subarray): at the sizes a response block has the
|
|
578
579
|
// pool copy is the cheaper allocation (OPTIMIZATION.md item 18)
|
|
579
580
|
const bytes = Buffer.allocUnsafe(end - at);
|
|
580
|
-
|
|
581
|
+
copyInto(bytes, 0, scratch, at, end);
|
|
581
582
|
session._blocks.set(headers,
|
|
582
583
|
{ bytes, epoch: enc.epoch, stamp, status: parsed.status, date: parsed.genDate,
|
|
583
584
|
bound: sent.bound });
|
|
@@ -641,7 +642,7 @@ function pushBlock(headers, session) {
|
|
|
641
642
|
const at = update === null ? 0 : update.length;
|
|
642
643
|
const end = encodeAt(parsed, enc, at);
|
|
643
644
|
if (at !== 0) {
|
|
644
|
-
|
|
645
|
+
copyInto(scratch, 0, update, 0, update.length);
|
|
645
646
|
}
|
|
646
647
|
return scratch.subarray(0, end);
|
|
647
648
|
}
|
package/lib/pieces.js
CHANGED
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
// the padding off a frame passes the trimmed offsets.
|
|
21
21
|
'use strict';
|
|
22
22
|
|
|
23
|
+
const { copyInto } = require('./frames');
|
|
24
|
+
|
|
23
25
|
// How many octets the views hold together.
|
|
24
26
|
function piecesLength(pieces) {
|
|
25
27
|
let n = 0;
|
|
@@ -84,7 +86,7 @@ function pieceCopyTo(dst, dstOff, pieces, from, to) {
|
|
|
84
86
|
if (end > from && off < to) {
|
|
85
87
|
const a = from > off ? from - off : 0;
|
|
86
88
|
const b = to < end ? to - off : p.length;
|
|
87
|
-
|
|
89
|
+
copyInto(dst, dstOff + written, p, a, b);
|
|
88
90
|
written += b - a;
|
|
89
91
|
}
|
|
90
92
|
off = end;
|
package/lib/server.js
CHANGED
|
@@ -74,7 +74,8 @@
|
|
|
74
74
|
const net = require('node:net');
|
|
75
75
|
const tls = require('node:tls');
|
|
76
76
|
const { EventEmitter } = require('node:events');
|
|
77
|
-
const { Http2Session, OUR_DEFAULTS, strictSingleValueFields
|
|
77
|
+
const { Http2Session, OUR_DEFAULTS, strictSingleValueFields, checkSettings,
|
|
78
|
+
validateRemoteCustomSettings } = require('./session');
|
|
78
79
|
const { onServerStream, Http2ServerRequest, Http2ServerResponse, describeType } = require('./compat');
|
|
79
80
|
|
|
80
81
|
const kCompat = Symbol('h2lean.compatListener');
|
|
@@ -92,7 +93,14 @@ function notImplemented(what) {
|
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
function initHttp2Server(server, options) {
|
|
95
|
-
|
|
96
|
+
// node's initializeOptions works on its own copy of the option bag -- it never writes
|
|
97
|
+
// back to the caller's object -- and validateSessionOptions then copies
|
|
98
|
+
// remoteCustomSettings ([...remoteCustomSettings]). Both are observable: measured on a
|
|
99
|
+
// live node server, mutating the caller's array after createServer leaves every session
|
|
100
|
+
// created afterwards untouched, while handing the session the original reference made
|
|
101
|
+
// the next accepted session throw on the entry the caller pushed.
|
|
102
|
+
const opts = { ...(options || {}) };
|
|
103
|
+
validateRemoteCustomSettings(opts);
|
|
96
104
|
server.settings = { ...OUR_DEFAULTS, ...opts.settings };
|
|
97
105
|
server._sessionOptions = {
|
|
98
106
|
settings: server.settings,
|
|
@@ -123,7 +131,18 @@ function initHttp2Server(server, options) {
|
|
|
123
131
|
// The server `origins` option: each session sends one ORIGIN frame with these origins at
|
|
124
132
|
// connect. node only does it when the value is an array (core.js setupHandle), so it rides
|
|
125
133
|
// through unvalidated and Http2Session._start applies the same Array.isArray test.
|
|
126
|
-
origins: opts.origins
|
|
134
|
+
origins: opts.origins,
|
|
135
|
+
// Unbounded-work-per-peer guards: ride through raw, resolved in the session constructor.
|
|
136
|
+
maxOutstandingPings: opts.maxOutstandingPings,
|
|
137
|
+
maxSessionInvalidFrames: opts.maxSessionInvalidFrames,
|
|
138
|
+
maxSessionRejectedStreams: opts.maxSessionRejectedStreams,
|
|
139
|
+
// Connection-level send window override (separate from per-stream initialWindowSize).
|
|
140
|
+
connectionWindowSize: opts.connectionWindowSize,
|
|
141
|
+
// Validated and copied above, where node's initializeOptions runs
|
|
142
|
+
// validateSessionOptions -- synchronously, at createServer. The per-entry checks
|
|
143
|
+
// wait for the session constructor (setupHandle), where node runs them per
|
|
144
|
+
// connection, and a refused value throws uncaught at accept.
|
|
145
|
+
remoteCustomSettings: opts.remoteCustomSettings
|
|
127
146
|
};
|
|
128
147
|
server._sessions = new Set();
|
|
129
148
|
server[kCompat] = null;
|
|
@@ -324,20 +343,21 @@ function applyHttp2ServerApi(proto) {
|
|
|
324
343
|
return this;
|
|
325
344
|
};
|
|
326
345
|
|
|
327
|
-
// node's Http2Server.updateSettings() (internal/http2/core.js, next to its setTimeout)
|
|
328
|
-
// merges into the server's own
|
|
329
|
-
//
|
|
330
|
-
//
|
|
331
|
-
//
|
|
332
|
-
//
|
|
346
|
+
// node's Http2Server.updateSettings() (internal/http2/core.js, next to its setTimeout)
|
|
347
|
+
// runs assertIsObject + validateSettings and merges shallowly into the server's own
|
|
348
|
+
// settings object -- it does not walk the live sessions. That is exactly what the wire
|
|
349
|
+
// shows: after updateSettings({maxConcurrentStreams: 7}) the next connection is sent
|
|
350
|
+
// SETTINGS(3)=7 while an established session keeps the values it was opened with.
|
|
351
|
+
// checkSettings carries the customSettings checks too; the merged bag's customs reach
|
|
352
|
+
// the next session's initial submission through its constructor, where the packer
|
|
353
|
+
// checks throw uncaught at accept, the way node's setupHandle throws.
|
|
333
354
|
proto.updateSettings = function (settings) {
|
|
334
|
-
|
|
335
|
-
throw new TypeError('settings must be an object');
|
|
336
|
-
}
|
|
355
|
+
checkSettings(settings);
|
|
337
356
|
this.settings = { ...this.settings, ...settings };
|
|
338
357
|
this._sessionOptions.settings = this.settings;
|
|
339
358
|
// the values merged here become promises to the next peer, so they join the advertisement;
|
|
340
|
-
//
|
|
359
|
+
// the set is only the membership of the next session's initial frame, whose entries go out
|
|
360
|
+
// in ascending setting-id order (measured on node, see submissionValues in lib/session.js)
|
|
341
361
|
this._sessionOptions.advertised = [...new Set(
|
|
342
362
|
[...this._sessionOptions.advertised, ...Object.keys(settings)])];
|
|
343
363
|
return this;
|
|
@@ -482,6 +502,19 @@ class Http2SecureServer extends tls.Server {
|
|
|
482
502
|
const opts = options || {};
|
|
483
503
|
super(alpnOptions(opts));
|
|
484
504
|
initHttp2Server(this, opts);
|
|
505
|
+
// node's onErrorSecureServerSession (internal/http2/core.js, subscribed in the
|
|
506
|
+
// Http2SecureServer constructor): the tls.Server machinery emits 'tlsClientError'
|
|
507
|
+
// (err, tlsSocket) -- onSocketTLSError for a failed handshake, and a synthesized
|
|
508
|
+
// ConnResetException('socket hang up') from onSocketClose when the client
|
|
509
|
+
// disconnects before the handshake completes -- and the secure server re-emits it
|
|
510
|
+
// as 'clientError' (err, socket), destroying the socket with the error when
|
|
511
|
+
// nothing listens for 'clientError'. h2lean inherits the 'tlsClientError'
|
|
512
|
+
// emission from tls.Server; this bridge is the piece that was missing.
|
|
513
|
+
this.on('tlsClientError', (err, socket) => {
|
|
514
|
+
if (!this.emit('clientError', err, socket)) {
|
|
515
|
+
socket.destroy(err);
|
|
516
|
+
}
|
|
517
|
+
});
|
|
485
518
|
if (opts.allowHTTP1 === true) {
|
|
486
519
|
initHttp1Fallback(this, opts);
|
|
487
520
|
}
|
|
@@ -548,11 +581,17 @@ function createSecureServer(options, onRequestHandler) {
|
|
|
548
581
|
// net.Socket for h2c. node does NOT check alpnProtocol here (the ALPN verdict is
|
|
549
582
|
// the caller's, not the handshake's), and neither do we; a still-connecting socket
|
|
550
583
|
// is waited for by the session constructor. The options h2lean implements are
|
|
551
|
-
// validated the way initializeOptions validates them
|
|
552
|
-
//
|
|
553
|
-
//
|
|
554
|
-
//
|
|
555
|
-
//
|
|
584
|
+
// validated the way initializeOptions validates them -- remoteCustomSettings here,
|
|
585
|
+
// synchronously, like every initializeOptions caller; the session constructor then
|
|
586
|
+
// runs the per-entry checks and the settings packer unguarded, where node's
|
|
587
|
+
// setupHandle throw escapes the call. The rest (origins) concerns a feature
|
|
588
|
+
// h2lean does implement but has no server-level ORIGIN frame to send, so it rides
|
|
589
|
+
// through raw and is applied per-session. Unbounded-work-per-peer guards
|
|
590
|
+
// (maxSessionInvalidFrames, maxSessionRejectedStreams, maxOutstandingPings,
|
|
591
|
+
// connectionWindowSize) also ride through raw and are resolved in the session
|
|
592
|
+
// constructor. The one surface difference
|
|
593
|
+
// (node's JSStreamSocket wrap of a generic Duplex, so 'connect's second argument
|
|
594
|
+
// differs there) is written up in this file's header.
|
|
556
595
|
function performServerHandshake(socket, options) {
|
|
557
596
|
if (options !== undefined &&
|
|
558
597
|
(options === null || typeof options !== 'object' || Array.isArray(options))) {
|
|
@@ -570,6 +609,7 @@ function performServerHandshake(socket, options) {
|
|
|
570
609
|
throw err;
|
|
571
610
|
}
|
|
572
611
|
strictSingleValueFields(opts);
|
|
612
|
+
validateRemoteCustomSettings(opts);
|
|
573
613
|
// opts.settings carries only what the application asked for, and the session's
|
|
574
614
|
// _advertised defaults to Object.keys(opts.settings) -- so the first SETTINGS frame
|
|
575
615
|
// advertises exactly those, empty when nothing was asked, the way node's does.
|