h2lean 0.19.9 → 0.19.14
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/index.js +7 -7
- package/lib/client/Http2ClientSession.js +228 -27
- package/lib/client/Http2ClientStream.js +74 -40
- package/lib/client/client.js +15 -9
- package/lib/headers.js +18 -8
- package/lib/server/Http2ServerSession.js +260 -94
- package/lib/server/Http2ServerStream.js +61 -9
- package/lib/server/compat.js +164 -34
- package/lib/server/server.js +97 -16
- package/package.json +1 -1
package/lib/server/server.js
CHANGED
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
//
|
|
5
5
|
// Connection-lifetime surface, measured against node 26 rather than against its
|
|
6
6
|
// documentation:
|
|
7
|
+
// * creation-time option validation matches node's initializeOptions (internal/http2/
|
|
8
|
+
// core.js): an invalid options bag, an invalid options.settings and negative or
|
|
9
|
+
// non-integer session limits all throw synchronously at createServer/createSecureServer.
|
|
10
|
+
// node 22 put the bare option name in ERR_OUT_OF_RANGE; from 24 on it says
|
|
11
|
+
// "options.<name>". h2lean keeps the pinned 26 wording on every major (test/node-version.js).
|
|
7
12
|
// * node's Http2Server has close, setTimeout and updateSettings -- and NOTHING else: no
|
|
8
13
|
// sessionTimeout, no closeIdleSessions, no sessions getter, no settings property. node keeps
|
|
9
14
|
// the settings updateSettings merges in a private options object; h2lean keeps them in the
|
|
@@ -75,7 +80,7 @@ const net = require('node:net');
|
|
|
75
80
|
const tls = require('node:tls');
|
|
76
81
|
const { EventEmitter } = require('node:events');
|
|
77
82
|
const { Http2ServerSession, OUR_DEFAULTS, strictSingleValueFields, checkSettings,
|
|
78
|
-
validateRemoteCustomSettings } = require('./Http2ServerSession');
|
|
83
|
+
validateRemoteCustomSettings, validateConnectionWindowSize } = require('./Http2ServerSession');
|
|
79
84
|
const { onServerStream, Http2ServerRequest, Http2ServerResponse, describeType } = require('./compat');
|
|
80
85
|
|
|
81
86
|
const kCompat = Symbol('h2lean.compatListener');
|
|
@@ -92,7 +97,48 @@ function notImplemented(what) {
|
|
|
92
97
|
return err;
|
|
93
98
|
}
|
|
94
99
|
|
|
100
|
+
// node's initializeOptions (internal/http2/core.js) validates these two limits with
|
|
101
|
+
// validateUint32, synchronously, at creation time. node 22 reported the bare option name
|
|
102
|
+
// and said "argument" for a wrong-typed value; from 24 on it says "options.<name>" and
|
|
103
|
+
// "property". h2lean follows the pinned v26.12.1 wording on every major: making the
|
|
104
|
+
// message depend on the running binary would move h2lean's own expectation, which the
|
|
105
|
+
// differential suite deliberately keeps fixed (test/node-version.js).
|
|
106
|
+
function validateCreateUint32(value, name) {
|
|
107
|
+
if (value === undefined) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (typeof value !== 'number') {
|
|
111
|
+
const err = new TypeError(
|
|
112
|
+
`The "${name}" property must be of type number. Received ${describeType(value)}`);
|
|
113
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
114
|
+
throw err;
|
|
115
|
+
}
|
|
116
|
+
if (!Number.isInteger(value)) {
|
|
117
|
+
const err = new RangeError(
|
|
118
|
+
`The value of "${name}" is out of range. It must be an integer. Received ${value}`);
|
|
119
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
if (value < 0 || value > 0xffffffff) {
|
|
123
|
+
const err = new RangeError(
|
|
124
|
+
`The value of "${name}" is out of range. ` +
|
|
125
|
+
`It must be >= 0 && <= 4294967295. Received ${value}`);
|
|
126
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
95
131
|
function initHttp2Server(server, options) {
|
|
132
|
+
// node's initializeOptions opens with assertIsObject(options, 'options'), before it
|
|
133
|
+
// copies the bag. Undefined means no options at all and is valid; everything else
|
|
134
|
+
// that is not a plain object is refused here, the way performServerHandshake does it.
|
|
135
|
+
if (options !== undefined &&
|
|
136
|
+
(options === null || typeof options !== 'object' || Array.isArray(options))) {
|
|
137
|
+
const err = new TypeError(
|
|
138
|
+
`The "options" argument must be of type object. Received ${describeType(options)}`);
|
|
139
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
96
142
|
// node's initializeOptions works on its own copy of the option bag -- it never writes
|
|
97
143
|
// back to the caller's object -- and validateSessionOptions then copies
|
|
98
144
|
// remoteCustomSettings ([...remoteCustomSettings]). Both are observable: measured on a
|
|
@@ -100,12 +146,30 @@ function initHttp2Server(server, options) {
|
|
|
100
146
|
// created afterwards untouched, while handing the session the original reference made
|
|
101
147
|
// the next accepted session throw on the entry the caller pushed.
|
|
102
148
|
const opts = { ...(options || {}) };
|
|
149
|
+
// assertIsObject(options.settings, 'options.settings') is the next check in node's
|
|
150
|
+
// initializeOptions; it runs before anything reads the settings bag.
|
|
151
|
+
if (opts.settings !== undefined &&
|
|
152
|
+
(opts.settings === null || typeof opts.settings !== 'object' || Array.isArray(opts.settings))) {
|
|
153
|
+
const err = new TypeError(
|
|
154
|
+
`The "options.settings" property must be of type object. Received ${describeType(opts.settings)}`);
|
|
155
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
103
158
|
validateRemoteCustomSettings(opts);
|
|
104
159
|
server.settings = { ...OUR_DEFAULTS, ...opts.settings };
|
|
105
160
|
// One pool for every session of this server: a staging buffer completed under one
|
|
106
161
|
// connection can be taken by another, and the cap is server-wide rather than
|
|
107
162
|
// per-session. performServerHandshake carries its own single-session pool.
|
|
108
163
|
server._stagingPool = [];
|
|
164
|
+
// node's validateSessionOptions runs these two in order after remoteCustomSettings;
|
|
165
|
+
// a server with two invalid options fails on the field node reports first.
|
|
166
|
+
const strict = strictSingleValueFields(opts);
|
|
167
|
+
validateConnectionWindowSize(opts);
|
|
168
|
+
// Last two checks of node's initializeOptions, after validateSessionOptions. The
|
|
169
|
+
// numbers themselves are unchanged: they still ride to each session, where they
|
|
170
|
+
// bound the per-session work. Only the creation-time refusal is new here.
|
|
171
|
+
validateCreateUint32(opts.maxSessionInvalidFrames, 'options.maxSessionInvalidFrames');
|
|
172
|
+
validateCreateUint32(opts.maxSessionRejectedStreams, 'options.maxSessionRejectedStreams');
|
|
109
173
|
server._sessionOptions = {
|
|
110
174
|
stagingPool: server._stagingPool,
|
|
111
175
|
// Default on; `false` keeps the allocate-per-batch behaviour of the pool opt-out.
|
|
@@ -126,7 +190,7 @@ function initHttp2Server(server, options) {
|
|
|
126
190
|
shareHeaders: opts.shareHeaders === true,
|
|
127
191
|
// Validated here, at createServer time, where node's validateSessionOptions runs;
|
|
128
192
|
// the value also rides to each session, whose constructor re-checks it.
|
|
129
|
-
strictSingleValueFields:
|
|
193
|
+
strictSingleValueFields: strict,
|
|
130
194
|
// The outbound header-block bound. node does not validate this one -- updateOptionsBuffer
|
|
131
195
|
// (internal/http2/util.js) takes it on a `typeof === 'number'` test alone -- so it rides
|
|
132
196
|
// through raw and the session constructor applies the same test and the same coercion.
|
|
@@ -143,7 +207,14 @@ function initHttp2Server(server, options) {
|
|
|
143
207
|
maxOutstandingPings: opts.maxOutstandingPings,
|
|
144
208
|
maxSessionInvalidFrames: opts.maxSessionInvalidFrames,
|
|
145
209
|
maxSessionRejectedStreams: opts.maxSessionRejectedStreams,
|
|
146
|
-
//
|
|
210
|
+
// maxSettings bounds the entries in one inbound SETTINGS frame. node reads it without
|
|
211
|
+
// creation-time validation: a finite number is clamped to at least 1, anything else
|
|
212
|
+
// keeps the 32-entry default (measured on a live node server with values 0, 1, 1.5,
|
|
213
|
+
// NaN, Infinity and a string). The session applies the bound per connection.
|
|
214
|
+
maxSettings: opts.maxSettings,
|
|
215
|
+
// Connection-level receive window (separate from the per-stream SETTINGS value):
|
|
216
|
+
// validated above at createServer time, applied to the session's state and
|
|
217
|
+
// announced with one WINDOW_UPDATE(stream 0) when it exceeds the default.
|
|
147
218
|
connectionWindowSize: opts.connectionWindowSize,
|
|
148
219
|
// Per-session memory budget in decimal MB (node multiplies by 1e6 in C++) and
|
|
149
220
|
// the outbound frame-padding strategy. Both ride through raw: util.js only
|
|
@@ -159,6 +230,12 @@ function initHttp2Server(server, options) {
|
|
|
159
230
|
remoteCustomSettings: opts.remoteCustomSettings
|
|
160
231
|
};
|
|
161
232
|
server._sessions = new Set();
|
|
233
|
+
// node's setupCompat passes the caller's Http2ServerRequest/Http2ServerResponse
|
|
234
|
+
// subclasses into the compat layer, so the handler receives those objects rather
|
|
235
|
+
// than the built-in classes. Keep them on the server and let onServerStream pick
|
|
236
|
+
// them up at construction, without changing when the objects are created.
|
|
237
|
+
server._http2ServerRequest = opts.Http2ServerRequest;
|
|
238
|
+
server._http2ServerResponse = opts.Http2ServerResponse;
|
|
162
239
|
server[kCompat] = null;
|
|
163
240
|
// node arms its compat layer through a newListener hook (setupCompat,
|
|
164
241
|
// internal/http2/core.js) rather than by wrapping the registration methods, so
|
|
@@ -234,7 +311,8 @@ function attachSession(server, socket) {
|
|
|
234
311
|
}
|
|
235
312
|
session.on('error', (err) => {
|
|
236
313
|
if (server.listenerCount('sessionError') > 0) {
|
|
237
|
-
|
|
314
|
+
// node's sessionError listener receives (err, session) -- core.js:3349-3352.
|
|
315
|
+
server.emit('sessionError', err, session);
|
|
238
316
|
}
|
|
239
317
|
});
|
|
240
318
|
session.once('close', () => {
|
|
@@ -515,7 +593,9 @@ class Http2SecureServer extends tls.Server {
|
|
|
515
593
|
constructor(options) {
|
|
516
594
|
const opts = options || {};
|
|
517
595
|
super(alpnOptions(opts));
|
|
518
|
-
|
|
596
|
+
// The TLS side may default a missing/null options bag, but initHttp2Server must see
|
|
597
|
+
// the caller's raw argument: node's initializeOptions refuses null itself.
|
|
598
|
+
initHttp2Server(this, options);
|
|
519
599
|
// node's onErrorSecureServerSession (internal/http2/core.js, subscribed in the
|
|
520
600
|
// Http2SecureServer constructor): the tls.Server machinery emits 'tlsClientError'
|
|
521
601
|
// (err, tlsSocket) -- onSocketTLSError for a failed handshake, and a synthesized
|
|
@@ -581,15 +661,14 @@ function createServer(options, onRequestHandler) {
|
|
|
581
661
|
// node's createSecureServer takes the tls.Server options (key, cert, ca, ...) and
|
|
582
662
|
// hands the rest to the same machinery as the cleartext server.
|
|
583
663
|
function createSecureServer(options, onRequestHandler) {
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
if (typeof handler === 'function') {
|
|
664
|
+
// node's createSecureServer has no function-as-first-argument shorthand: only the
|
|
665
|
+
// cleartext createServer treats a lone function as the request listener. Passing one
|
|
666
|
+
// here reaches initializeOptions and throws ERR_INVALID_ARG_TYPE, so the raw argument
|
|
667
|
+
// must reach initHttp2Server unchanged.
|
|
668
|
+
const server = new Http2SecureServer(options);
|
|
669
|
+
if (typeof onRequestHandler === 'function') {
|
|
591
670
|
server._setupCompat();
|
|
592
|
-
server.on('request',
|
|
671
|
+
server.on('request', onRequestHandler);
|
|
593
672
|
}
|
|
594
673
|
return server;
|
|
595
674
|
}
|
|
@@ -606,9 +685,10 @@ function createSecureServer(options, onRequestHandler) {
|
|
|
606
685
|
// setupHandle throw escapes the call. The rest (origins) concerns a feature
|
|
607
686
|
// h2lean does implement but has no server-level ORIGIN frame to send, so it rides
|
|
608
687
|
// through raw and is applied per-session. Unbounded-work-per-peer guards
|
|
609
|
-
// (maxSessionInvalidFrames, maxSessionRejectedStreams, maxOutstandingPings,
|
|
610
|
-
//
|
|
611
|
-
//
|
|
688
|
+
// (maxSessionInvalidFrames, maxSessionRejectedStreams, maxOutstandingPings, maxSettings)
|
|
689
|
+
// also ride through raw and are resolved in the session constructor; connectionWindowSize
|
|
690
|
+
// is a session option validated here, like node's initializeOptions does.
|
|
691
|
+
// The one surface difference
|
|
612
692
|
// (node's JSStreamSocket wrap of a generic Duplex, so 'connect's second argument
|
|
613
693
|
// differs there) is written up in this file's header.
|
|
614
694
|
function performServerHandshake(socket, options) {
|
|
@@ -631,6 +711,7 @@ function performServerHandshake(socket, options) {
|
|
|
631
711
|
}
|
|
632
712
|
strictSingleValueFields(opts);
|
|
633
713
|
validateRemoteCustomSettings(opts);
|
|
714
|
+
validateConnectionWindowSize(opts);
|
|
634
715
|
// opts.settings carries only what the application asked for, and the session's
|
|
635
716
|
// _advertised defaults to Object.keys(opts.settings) -- so the first SETTINGS frame
|
|
636
717
|
// advertises exactly those, empty when nothing was asked, the way node's does.
|
package/package.json
CHANGED