h2lean 0.19.9 → 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.
- package/index.js +7 -7
- package/lib/client/Http2ClientSession.js +177 -23
- package/lib/client/Http2ClientStream.js +74 -40
- package/lib/client/client.js +15 -9
- package/lib/headers.js +14 -7
- package/lib/server/Http2ServerSession.js +130 -42
- package/lib/server/Http2ServerStream.js +53 -8
- package/lib/server/compat.js +147 -31
- package/lib/server/server.js +86 -16
- package/package.json +1 -1
package/lib/server/compat.js
CHANGED
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
|
|
48
48
|
const { Readable, Stream } = require('node:stream');
|
|
49
49
|
|
|
50
|
-
const { nullProto, trimName
|
|
50
|
+
const { nullProto, trimName } = require('../headers');
|
|
51
51
|
const { kCompatSocket } = require('../Http2Stream');
|
|
52
52
|
const { NO_ERROR } = require('../frames');
|
|
53
53
|
const { Http2ServerStream } = require('./Http2ServerStream');
|
|
@@ -77,8 +77,28 @@ Http2ServerStream.prototype._destroy = function compatTrackedDestroy(code, sendR
|
|
|
77
77
|
// step without an extra require on the session's hot path.
|
|
78
78
|
const kCompatRawTrailers = Symbol.for('h2lean.compat.rawTrailers');
|
|
79
79
|
|
|
80
|
+
// node declares its validation errors with a class (internal/errors.js):
|
|
81
|
+
// header and argument validation is TypeError, status codes are RangeError,
|
|
82
|
+
// stream-state errors stay plain Error. ERR_HTTP2_HEADERS_SENT stays an Error
|
|
83
|
+
// here too -- h2lean's message differs from node's and that is written down,
|
|
84
|
+
// while the class the application catches is the same.
|
|
85
|
+
const TYPE_ERROR_CODES = new Set([
|
|
86
|
+
'ERR_INVALID_ARG_TYPE',
|
|
87
|
+
'ERR_INVALID_ARG_VALUE',
|
|
88
|
+
'ERR_INVALID_HTTP_TOKEN',
|
|
89
|
+
'ERR_HTTP2_INVALID_HEADER_VALUE',
|
|
90
|
+
'ERR_HTTP2_INVALID_PSEUDOHEADER',
|
|
91
|
+
'ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'
|
|
92
|
+
]);
|
|
93
|
+
const RANGE_ERROR_CODES = new Set([
|
|
94
|
+
'ERR_HTTP2_STATUS_INVALID',
|
|
95
|
+
'ERR_HTTP2_INFO_STATUS_NOT_ALLOWED',
|
|
96
|
+
'ERR_HTTP2_INVALID_INFO_STATUS'
|
|
97
|
+
]);
|
|
80
98
|
function err(code, msg) {
|
|
81
|
-
const
|
|
99
|
+
const Ctor = RANGE_ERROR_CODES.has(code) ? RangeError
|
|
100
|
+
: TYPE_ERROR_CODES.has(code) ? TypeError : Error;
|
|
101
|
+
const e = new Ctor(msg);
|
|
82
102
|
e.code = code;
|
|
83
103
|
return e;
|
|
84
104
|
}
|
|
@@ -238,6 +258,69 @@ function checkName(name) {
|
|
|
238
258
|
}
|
|
239
259
|
}
|
|
240
260
|
|
|
261
|
+
// node's compat layer warns once per process for a status message, whether it
|
|
262
|
+
// was read, written or passed to writeHead (internal/http2/compat.js:75,112).
|
|
263
|
+
// The warning is an UnsupportedWarning with node's exact text; the round trip
|
|
264
|
+
// and the wire bytes are unaffected.
|
|
265
|
+
let statusMessageWarned = false;
|
|
266
|
+
function statusMessageWarn() {
|
|
267
|
+
if (!statusMessageWarned) {
|
|
268
|
+
process.emitWarning(
|
|
269
|
+
'Status message is not supported by HTTP/2 (RFC7540 8.1.2.4)',
|
|
270
|
+
'UnsupportedWarning');
|
|
271
|
+
statusMessageWarned = true;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// node's one header check for every name/value accessor (internal/http2/compat.js:82).
|
|
276
|
+
// The five defined pseudo headers are refused here; an unknown ':' name is
|
|
277
|
+
// refused downstream by assertValidPseudoHeader as ERR_HTTP2_INVALID_PSEUDOHEADER.
|
|
278
|
+
// A token check is deliberately not added here: node's setHeader destroys the
|
|
279
|
+
// stream for a bad token rather than throwing, which is a different path.
|
|
280
|
+
const PSEUDO_HEADERS = new Set([':status', ':method', ':path', ':authority', ':scheme']);
|
|
281
|
+
function assertValidHeader(name, value) {
|
|
282
|
+
if (typeof name !== 'string' || name === '' || name.includes(' ')) {
|
|
283
|
+
throw err('ERR_INVALID_HTTP_TOKEN', `Header name must be a valid HTTP token ["${name}"]`);
|
|
284
|
+
}
|
|
285
|
+
if (PSEUDO_HEADERS.has(name)) {
|
|
286
|
+
throw err('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', 'Cannot set HTTP/2 pseudo-headers');
|
|
287
|
+
}
|
|
288
|
+
if (name.charCodeAt(0) === 58) { // ':'
|
|
289
|
+
throw err('ERR_HTTP2_INVALID_PSEUDOHEADER',
|
|
290
|
+
`"${name}" is an invalid pseudoheader or is used incorrectly`);
|
|
291
|
+
}
|
|
292
|
+
if (value === undefined || value === null) {
|
|
293
|
+
throw err('ERR_HTTP2_INVALID_HEADER_VALUE', `Invalid value "${value}" for header "${name}"`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// node's own token grammar (_http_common.js:216), not headers.js's isFieldName:
|
|
298
|
+
// that one writes to the bounded process-wide VALID_NAMES cache on every new
|
|
299
|
+
// name, which a per-header compat path must not grow.
|
|
300
|
+
const HTTP_TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
301
|
+
|
|
302
|
+
// h2lean's timer is on the session, so arming both the request and the response
|
|
303
|
+
// leaves two once('timeout') wrappers on it; one emission calls both. node
|
|
304
|
+
// re-emits 'timeout' once on each compat object, so the first wrapper emits
|
|
305
|
+
// both events and the later ones run their callback only. The flag lives on
|
|
306
|
+
// the stream and is cleared in the next tick, the turn of one emission.
|
|
307
|
+
function clearCompatTimeoutEmitted(stream) {
|
|
308
|
+
stream._compatTimeoutEmitted = false;
|
|
309
|
+
}
|
|
310
|
+
function emitCompatTimeout(stream, req, res) {
|
|
311
|
+
if (stream._compatTimeoutEmitted === true) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
stream._compatTimeoutEmitted = true;
|
|
315
|
+
stream._tick(clearCompatTimeoutEmitted, stream);
|
|
316
|
+
if (req !== undefined) {
|
|
317
|
+
req.emit('timeout');
|
|
318
|
+
}
|
|
319
|
+
if (res !== undefined) {
|
|
320
|
+
res.emit('timeout');
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
241
324
|
// node's wording for a value of the wrong type (internal/errors.js,
|
|
242
325
|
// determineSpecificType): the message is what an application reads when it
|
|
243
326
|
// catches the error, so it says what node says.
|
|
@@ -253,6 +336,15 @@ function describeType(value) {
|
|
|
253
336
|
if (value === 0) {
|
|
254
337
|
return 1 / value === -Infinity ? 'type number (-0)' : 'type number (0)';
|
|
255
338
|
}
|
|
339
|
+
if (Number.isNaN(value)) {
|
|
340
|
+
return 'type number (NaN)';
|
|
341
|
+
}
|
|
342
|
+
if (value === Infinity) {
|
|
343
|
+
return 'type number (Infinity)';
|
|
344
|
+
}
|
|
345
|
+
if (value === -Infinity) {
|
|
346
|
+
return 'type number (-Infinity)';
|
|
347
|
+
}
|
|
256
348
|
return `type number (${value})`;
|
|
257
349
|
case 'bigint': return `type bigint (${value}n)`;
|
|
258
350
|
case 'boolean': return `type boolean (${value})`;
|
|
@@ -262,6 +354,12 @@ function describeType(value) {
|
|
|
262
354
|
const name = value.constructor?.name;
|
|
263
355
|
return typeof name === 'string' && name !== '' ? `an instance of ${name}` : 'an instance of Object';
|
|
264
356
|
}
|
|
357
|
+
case 'string': {
|
|
358
|
+
// determineSpecificType truncates long strings and quotes with the
|
|
359
|
+
// delimiter that does not occur in the value (internal/errors.js:1048).
|
|
360
|
+
const str = value.length > 28 ? `${value.slice(0, 25)}...` : value;
|
|
361
|
+
return str.includes("'") ? `type string (${JSON.stringify(str)})` : `type string ('${str}')`;
|
|
362
|
+
}
|
|
265
363
|
default: return `type ${typeof value}`;
|
|
266
364
|
}
|
|
267
365
|
}
|
|
@@ -608,12 +706,10 @@ class Http2ServerRequest extends Readable {
|
|
|
608
706
|
// setTimeout call, only on the opt-in path; the timer path itself is
|
|
609
707
|
// unchanged.
|
|
610
708
|
const req = this;
|
|
611
|
-
const
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
res.emit('timeout');
|
|
616
|
-
}
|
|
709
|
+
const stream = this._stream;
|
|
710
|
+
const res = stream._response;
|
|
711
|
+
stream.setTimeout(msecs, function onCompatTimeout() {
|
|
712
|
+
emitCompatTimeout(stream, req, res);
|
|
617
713
|
if (callback !== undefined) {
|
|
618
714
|
callback.apply(this, arguments);
|
|
619
715
|
}
|
|
@@ -789,21 +885,21 @@ class Http2ServerResponse extends Stream {
|
|
|
789
885
|
set statusCode(code) {
|
|
790
886
|
code |= 0;
|
|
791
887
|
if (code >= 100 && code < 200) {
|
|
792
|
-
throw err('ERR_HTTP2_INFO_STATUS_NOT_ALLOWED', '
|
|
888
|
+
throw err('ERR_HTTP2_INFO_STATUS_NOT_ALLOWED', 'Informational status codes cannot be used');
|
|
793
889
|
}
|
|
794
890
|
if (code < 100 || code > 599) {
|
|
795
|
-
throw err('ERR_HTTP2_STATUS_INVALID', `
|
|
891
|
+
throw err('ERR_HTTP2_STATUS_INVALID', `Invalid status code: ${code}`);
|
|
796
892
|
}
|
|
797
893
|
this._state.statusCode = code;
|
|
798
894
|
}
|
|
799
895
|
|
|
800
|
-
// HTTP/2 has no reason phrase; node warns and returns ''.
|
|
896
|
+
// HTTP/2 has no reason phrase; node warns once and returns ''.
|
|
801
897
|
get statusMessage() {
|
|
802
|
-
|
|
898
|
+
statusMessageWarn();
|
|
803
899
|
return '';
|
|
804
900
|
}
|
|
805
901
|
set statusMessage(msg) {
|
|
806
|
-
|
|
902
|
+
statusMessageWarn();
|
|
807
903
|
}
|
|
808
904
|
|
|
809
905
|
get sendDate() {
|
|
@@ -860,7 +956,9 @@ class Http2ServerResponse extends Stream {
|
|
|
860
956
|
// validate the name either (internal/http2/compat.js), so a name that is not
|
|
861
957
|
// a string fails here the way it fails there: name.trim() is not a function.
|
|
862
958
|
_setHeader(name, value) {
|
|
863
|
-
|
|
959
|
+
const key = trimName(name);
|
|
960
|
+
assertValidHeader(key, value);
|
|
961
|
+
this._headers[key] = value;
|
|
864
962
|
}
|
|
865
963
|
|
|
866
964
|
appendHeader(name, value) {
|
|
@@ -869,6 +967,7 @@ class Http2ServerResponse extends Stream {
|
|
|
869
967
|
throw err('ERR_HTTP2_HEADERS_SENT', 'response has already been sent');
|
|
870
968
|
}
|
|
871
969
|
const key = trimName(name);
|
|
970
|
+
assertValidHeader(key, value);
|
|
872
971
|
const current = this._headers[key];
|
|
873
972
|
if (current === undefined) {
|
|
874
973
|
this._headers[key] = value;
|
|
@@ -887,10 +986,12 @@ class Http2ServerResponse extends Stream {
|
|
|
887
986
|
// fast path must recall its frame so the same shape holds here (item 44); after
|
|
888
987
|
// the flush the frame is gone and the trailer is lost on node too, its tick
|
|
889
988
|
// having already answered the empty block.
|
|
989
|
+
const key = trimName(name);
|
|
990
|
+
assertValidHeader(key, value);
|
|
890
991
|
if (this._stream._esStaged) {
|
|
891
992
|
this._stream._recallStagedEnd();
|
|
892
993
|
}
|
|
893
|
-
this._trailers[
|
|
994
|
+
this._trailers[key] = value;
|
|
894
995
|
}
|
|
895
996
|
|
|
896
997
|
addTrailers(headers) {
|
|
@@ -908,23 +1009,33 @@ class Http2ServerResponse extends Stream {
|
|
|
908
1009
|
throw err('ERR_HTTP2_HEADERS_SENT', 'response has already been sent');
|
|
909
1010
|
}
|
|
910
1011
|
if (typeof statusMessage === 'string') {
|
|
911
|
-
|
|
1012
|
+
statusMessageWarn();
|
|
912
1013
|
}
|
|
913
1014
|
if (headers === undefined && typeof statusMessage === 'object') {
|
|
914
1015
|
headers = statusMessage;
|
|
915
1016
|
}
|
|
916
1017
|
|
|
917
1018
|
if (Array.isArray(headers)) {
|
|
1019
|
+
// node removes every incoming name first, then appends each pair, so a
|
|
1020
|
+
// repeated name in the array is two field lines on the wire rather than
|
|
1021
|
+
// one overwritten value (internal/http2/compat.js:735-770). The object
|
|
1022
|
+
// branch below still replaces through _setHeader().
|
|
918
1023
|
if (headers.length && Array.isArray(headers[0])) {
|
|
919
1024
|
for (let i = 0; i < headers.length; i++) {
|
|
920
|
-
this.
|
|
1025
|
+
this.removeHeader(headers[i][0]);
|
|
1026
|
+
}
|
|
1027
|
+
for (let i = 0; i < headers.length; i++) {
|
|
1028
|
+
this.appendHeader(headers[i][0], headers[i][1]);
|
|
921
1029
|
}
|
|
922
1030
|
} else {
|
|
923
1031
|
if (headers.length % 2 !== 0) {
|
|
924
1032
|
throw err('ERR_INVALID_ARG_VALUE', 'headers array must be flat key/value pairs');
|
|
925
1033
|
}
|
|
926
1034
|
for (let i = 0; i < headers.length; i += 2) {
|
|
927
|
-
this.
|
|
1035
|
+
this.removeHeader(headers[i]);
|
|
1036
|
+
}
|
|
1037
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
1038
|
+
this.appendHeader(headers[i], headers[i + 1]);
|
|
928
1039
|
}
|
|
929
1040
|
}
|
|
930
1041
|
} else if (typeof headers === 'object' && headers !== null) {
|
|
@@ -991,7 +1102,7 @@ class Http2ServerResponse extends Stream {
|
|
|
991
1102
|
// means "switch protocol", which HTTP/2 forbids outright (RFC 9113 8.5).
|
|
992
1103
|
if (typeof statusCode !== 'number' || statusCode < 100 || statusCode > 199
|
|
993
1104
|
|| statusCode === 101) {
|
|
994
|
-
throw err('ERR_HTTP2_STATUS_INVALID', `
|
|
1105
|
+
throw err('ERR_HTTP2_STATUS_INVALID', `Invalid status code: ${statusCode}`);
|
|
995
1106
|
}
|
|
996
1107
|
if (this._stream.headersSent || this._state.closed) {
|
|
997
1108
|
return false;
|
|
@@ -1033,18 +1144,11 @@ class Http2ServerResponse extends Stream {
|
|
|
1033
1144
|
continue;
|
|
1034
1145
|
}
|
|
1035
1146
|
const name = key.trim().toLowerCase();
|
|
1036
|
-
if (name === '' || name.includes(' ')) {
|
|
1037
|
-
throw err('ERR_INVALID_HTTP_TOKEN', `Header name must be a valid HTTP token ["${name}"]`);
|
|
1038
|
-
}
|
|
1039
|
-
if (name.charCodeAt(0) === 58) { // ':'
|
|
1040
|
-
throw err('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', 'Cannot set HTTP/2 pseudo-headers');
|
|
1041
|
-
}
|
|
1042
1147
|
const value = hints[key];
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
if (!isFieldName(name)) {
|
|
1148
|
+
assertValidHeader(name, value);
|
|
1149
|
+
// writeEarlyHints is the one compat path that throws on a bad token
|
|
1150
|
+
// instead of destroying the stream (internal/http2/compat.js:955).
|
|
1151
|
+
if (!HTTP_TOKEN.test(name)) {
|
|
1048
1152
|
throw err('ERR_INVALID_HTTP_TOKEN', `Header name must be a valid HTTP token ["${name}"]`);
|
|
1049
1153
|
}
|
|
1050
1154
|
headers[name] = value;
|
|
@@ -1202,7 +1306,19 @@ class Http2ServerResponse extends Stream {
|
|
|
1202
1306
|
if (this._state.closed || this._stream.destroyed) {
|
|
1203
1307
|
return this;
|
|
1204
1308
|
}
|
|
1205
|
-
|
|
1309
|
+
// node attaches the callback as a 'timeout' listener on the stream, and
|
|
1310
|
+
// the compat response re-emits the stream event as its own 'timeout'
|
|
1311
|
+
// (internal/http2/compat.js:435,878). Mirror both here; the shared helper
|
|
1312
|
+
// keeps the single event when the request armed the same timer.
|
|
1313
|
+
const res = this;
|
|
1314
|
+
const stream = this._stream;
|
|
1315
|
+
const req = this.req;
|
|
1316
|
+
stream.setTimeout(msecs, function onCompatResponseTimeout() {
|
|
1317
|
+
emitCompatTimeout(stream, req, res);
|
|
1318
|
+
if (callback !== undefined) {
|
|
1319
|
+
callback.apply(this, arguments);
|
|
1320
|
+
}
|
|
1321
|
+
});
|
|
1206
1322
|
return this;
|
|
1207
1323
|
}
|
|
1208
1324
|
|
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,9 @@ function initHttp2Server(server, options) {
|
|
|
143
207
|
maxOutstandingPings: opts.maxOutstandingPings,
|
|
144
208
|
maxSessionInvalidFrames: opts.maxSessionInvalidFrames,
|
|
145
209
|
maxSessionRejectedStreams: opts.maxSessionRejectedStreams,
|
|
146
|
-
// Connection-level
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
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',
|
|
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
|
-
//
|
|
611
|
-
//
|
|
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