h2lean 0.19.8 → 0.19.13

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.
@@ -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: strictSingleValueFields(opts),
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,9 @@ function initHttp2Server(server, options) {
143
207
  maxOutstandingPings: opts.maxOutstandingPings,
144
208
  maxSessionInvalidFrames: opts.maxSessionInvalidFrames,
145
209
  maxSessionRejectedStreams: opts.maxSessionRejectedStreams,
146
- // Connection-level send window override (separate from per-stream initialWindowSize).
210
+ // Connection-level receive window (separate from the per-stream SETTINGS value):
211
+ // validated above at createServer time, applied to the session's state and
212
+ // announced with one WINDOW_UPDATE(stream 0) when it exceeds the default.
147
213
  connectionWindowSize: opts.connectionWindowSize,
148
214
  // Per-session memory budget in decimal MB (node multiplies by 1e6 in C++) and
149
215
  // the outbound frame-padding strategy. Both ride through raw: util.js only
@@ -234,7 +300,8 @@ function attachSession(server, socket) {
234
300
  }
235
301
  session.on('error', (err) => {
236
302
  if (server.listenerCount('sessionError') > 0) {
237
- server.emit('sessionError', err);
303
+ // node's sessionError listener receives (err, session) -- core.js:3349-3352.
304
+ server.emit('sessionError', err, session);
238
305
  }
239
306
  });
240
307
  session.once('close', () => {
@@ -515,7 +582,9 @@ class Http2SecureServer extends tls.Server {
515
582
  constructor(options) {
516
583
  const opts = options || {};
517
584
  super(alpnOptions(opts));
518
- initHttp2Server(this, opts);
585
+ // The TLS side may default a missing/null options bag, but initHttp2Server must see
586
+ // the caller's raw argument: node's initializeOptions refuses null itself.
587
+ initHttp2Server(this, options);
519
588
  // node's onErrorSecureServerSession (internal/http2/core.js, subscribed in the
520
589
  // Http2SecureServer constructor): the tls.Server machinery emits 'tlsClientError'
521
590
  // (err, tlsSocket) -- onSocketTLSError for a failed handshake, and a synthesized
@@ -581,15 +650,14 @@ function createServer(options, onRequestHandler) {
581
650
  // node's createSecureServer takes the tls.Server options (key, cert, ca, ...) and
582
651
  // hands the rest to the same machinery as the cleartext server.
583
652
  function createSecureServer(options, onRequestHandler) {
584
- let opts = options;
585
- let handler = onRequestHandler;
586
- if (typeof opts === 'function') {
587
- handler = opts; opts = undefined;
588
- }
589
- const server = new Http2SecureServer(opts);
590
- if (typeof handler === 'function') {
653
+ // node's createSecureServer has no function-as-first-argument shorthand: only the
654
+ // cleartext createServer treats a lone function as the request listener. Passing one
655
+ // here reaches initializeOptions and throws ERR_INVALID_ARG_TYPE, so the raw argument
656
+ // must reach initHttp2Server unchanged.
657
+ const server = new Http2SecureServer(options);
658
+ if (typeof onRequestHandler === 'function') {
591
659
  server._setupCompat();
592
- server.on('request', handler);
660
+ server.on('request', onRequestHandler);
593
661
  }
594
662
  return server;
595
663
  }
@@ -606,9 +674,10 @@ function createSecureServer(options, onRequestHandler) {
606
674
  // setupHandle throw escapes the call. The rest (origins) concerns a feature
607
675
  // h2lean does implement but has no server-level ORIGIN frame to send, so it rides
608
676
  // through raw and is applied per-session. Unbounded-work-per-peer guards
609
- // (maxSessionInvalidFrames, maxSessionRejectedStreams, maxOutstandingPings,
610
- // connectionWindowSize) also ride through raw and are resolved in the session
611
- // constructor. The one surface difference
677
+ // (maxSessionInvalidFrames, maxSessionRejectedStreams, maxOutstandingPings) also
678
+ // ride through raw and are resolved in the session constructor; connectionWindowSize
679
+ // is a session option validated here, like node's initializeOptions does.
680
+ // The one surface difference
612
681
  // (node's JSStreamSocket wrap of a generic Duplex, so 'connect's second argument
613
682
  // differs there) is written up in this file's header.
614
683
  function performServerHandshake(socket, options) {
@@ -631,6 +700,7 @@ function performServerHandshake(socket, options) {
631
700
  }
632
701
  strictSingleValueFields(opts);
633
702
  validateRemoteCustomSettings(opts);
703
+ validateConnectionWindowSize(opts);
634
704
  // opts.settings carries only what the application asked for, and the session's
635
705
  // _advertised defaults to Object.keys(opts.settings) -- so the first SETTINGS frame
636
706
  // advertises exactly those, empty when nothing was asked, the way node's does.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.19.8",
3
+ "version": "0.19.13",
4
4
  "description": "Pure-JavaScript HTTP/2 for Node.js — a drop-in node:http2 replacement (server and client, h2c and TLS) with zero dependencies",
5
5
  "main": "index.js",
6
6
  "files": [