h2lean 0.11.3 → 0.17.2
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 +3 -7
- package/index.js +79 -19
- package/lib/client-session.js +331 -212
- package/lib/client-stream.js +2188 -147
- package/lib/client.js +9 -1
- package/lib/compat.js +90 -1
- package/lib/frames.js +25 -1
- package/lib/headers.js +4 -3
- package/lib/hpack.js +120 -24
- 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/compat.js
CHANGED
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
|
|
55
55
|
const { Readable, Stream } = require('node:stream');
|
|
56
56
|
|
|
57
|
-
const { nullProto, trimName } = require('./headers');
|
|
57
|
+
const { nullProto, trimName, isFieldName } = require('./headers');
|
|
58
58
|
|
|
59
59
|
function err(code, msg) {
|
|
60
60
|
const e = new Error(msg);
|
|
@@ -62,6 +62,45 @@ function err(code, msg) {
|
|
|
62
62
|
return e;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
// node's Link validation for writeEarlyHints (internal/validators.js:528-583,
|
|
66
|
+
// v26.12.1): a string, or an array of strings joined with ', ', each matching the
|
|
67
|
+
// link-value grammar `</...>; params`. The regex is node's own linkValueRegExp.
|
|
68
|
+
// Anything else -- including `undefined`, i.e. a hints object without `link` --
|
|
69
|
+
// is ERR_INVALID_ARG_VALUE; an empty array yields '' and the caller sends nothing.
|
|
70
|
+
const linkValueRegExp = /^(?:<[^>\r\n]*>)(?:\s*;\s*[^;"\s]+(?:=(")?[^;"\s]*\1)?)*$/;
|
|
71
|
+
// node's ERR_INVALID_ARG_VALUE formats the offending value with util.inspect
|
|
72
|
+
// (internal/errors.js:1485), not with the determineSpecificType wording that
|
|
73
|
+
// ERR_INVALID_ARG_TYPE uses -- `Received 5`, `Received 'x'`, `Received undefined`.
|
|
74
|
+
const { inspect } = require('node:util');
|
|
75
|
+
function invalidLinkValue(value) {
|
|
76
|
+
return err('ERR_INVALID_ARG_VALUE',
|
|
77
|
+
'The argument \'hints\' must be an array or string of format ' +
|
|
78
|
+
'"</styles.css>; rel=preload; as=style". Received ' + inspect(value));
|
|
79
|
+
}
|
|
80
|
+
function validateLinkHeaderValue(hints) {
|
|
81
|
+
if (typeof hints === 'string') {
|
|
82
|
+
if (!linkValueRegExp.test(hints)) {
|
|
83
|
+
throw invalidLinkValue(hints);
|
|
84
|
+
}
|
|
85
|
+
return hints;
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(hints)) {
|
|
88
|
+
let result = '';
|
|
89
|
+
for (let i = 0; i < hints.length; i++) {
|
|
90
|
+
const link = hints[i];
|
|
91
|
+
if (typeof link !== 'string' || !linkValueRegExp.test(link)) {
|
|
92
|
+
throw invalidLinkValue(link);
|
|
93
|
+
}
|
|
94
|
+
result += link;
|
|
95
|
+
if (i !== hints.length - 1) {
|
|
96
|
+
result += ', ';
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
throw invalidLinkValue(hints);
|
|
102
|
+
}
|
|
103
|
+
|
|
65
104
|
// node validates a header name before it normalises it. Every accessor that
|
|
66
105
|
// takes a name calls validateString(name, 'name') first (internal/http2/compat.js:
|
|
67
106
|
// setTrailer 581, getHeader 596, hasHeader 618, removeHeader 623, setHeader 639,
|
|
@@ -787,6 +826,56 @@ class Http2ServerResponse extends Stream {
|
|
|
787
826
|
return true;
|
|
788
827
|
}
|
|
789
828
|
|
|
829
|
+
writeEarlyHints(hints) {
|
|
830
|
+
// node's shape (internal/http2/compat.js:944, v26.12.1): validateObject on the
|
|
831
|
+
// whole argument, then a dedicated Link validation that runs before any other
|
|
832
|
+
// header is touched -- `link` is not optional, an empty array means "nothing to
|
|
833
|
+
// send" and returns false without a frame, and a string or array of strings must
|
|
834
|
+
// match node's link-value grammar (internal/validators.js:528). The remaining
|
|
835
|
+
// keys are trimmed and lowercased, `link` itself is skipped, and each pair goes
|
|
836
|
+
// through assertValidHeader + checkIsHttpToken before writeInformation(103, ...)
|
|
837
|
+
// carries it out. Measured against a live node server: a name with a space
|
|
838
|
+
// reports ERR_INVALID_HTTP_TOKEN, a pseudo-header
|
|
839
|
+
// ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED, an undefined or null value
|
|
840
|
+
// ERR_HTTP2_INVALID_HEADER_VALUE, a connection-specific field
|
|
841
|
+
// ERR_HTTP2_INVALID_CONNECTION_HEADERS (that one from additionalHeaders'
|
|
842
|
+
// own validation, which this path reaches through writeInformation).
|
|
843
|
+
if (typeof hints !== 'object' || hints === null) {
|
|
844
|
+
const e = new TypeError(
|
|
845
|
+
`The "hints" argument must be of type object. Received ${describeType(hints)}`);
|
|
846
|
+
e.code = 'ERR_INVALID_ARG_TYPE';
|
|
847
|
+
throw e;
|
|
848
|
+
}
|
|
849
|
+
const link = validateLinkHeaderValue(hints.link);
|
|
850
|
+
const headers = { __proto__: null };
|
|
851
|
+
for (const key of Object.keys(hints)) {
|
|
852
|
+
if (key === 'link') {
|
|
853
|
+
continue;
|
|
854
|
+
}
|
|
855
|
+
const name = key.trim().toLowerCase();
|
|
856
|
+
if (name === '' || name.includes(' ')) {
|
|
857
|
+
throw err('ERR_INVALID_HTTP_TOKEN', `Header name must be a valid HTTP token ["${name}"]`);
|
|
858
|
+
}
|
|
859
|
+
if (name.charCodeAt(0) === 58) { // ':'
|
|
860
|
+
throw err('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', 'Cannot set HTTP/2 pseudo-headers');
|
|
861
|
+
}
|
|
862
|
+
const value = hints[key];
|
|
863
|
+
if (value === undefined || value === null) {
|
|
864
|
+
throw err('ERR_HTTP2_INVALID_HEADER_VALUE',
|
|
865
|
+
`Invalid value "${value}" for header "${name}"`);
|
|
866
|
+
}
|
|
867
|
+
if (!isFieldName(name)) {
|
|
868
|
+
throw err('ERR_INVALID_HTTP_TOKEN', `Header name must be a valid HTTP token ["${name}"]`);
|
|
869
|
+
}
|
|
870
|
+
headers[name] = value;
|
|
871
|
+
}
|
|
872
|
+
if (link.length === 0) {
|
|
873
|
+
return false;
|
|
874
|
+
}
|
|
875
|
+
headers.Link = link;
|
|
876
|
+
return this.writeInformation(103, headers);
|
|
877
|
+
}
|
|
878
|
+
|
|
790
879
|
write(chunk, encoding, cb) {
|
|
791
880
|
if (typeof encoding === 'function') {
|
|
792
881
|
cb = encoding; encoding = 'utf8';
|
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/hpack.js
CHANGED
|
@@ -289,34 +289,91 @@ function latin1Into(dst, off, s) {
|
|
|
289
289
|
return off + len;
|
|
290
290
|
}
|
|
291
291
|
|
|
292
|
+
// Eight-bit prefix table: FAST[byte] = (symbol << 8) | length for the 74 codes
|
|
293
|
+
// of at most eight bits. Longer codes, EOS and the padding tail use the trie.
|
|
294
|
+
const FAST = new Int32Array(256).fill(-1);
|
|
295
|
+
for (let s = 0; s < EOS; s++) {
|
|
296
|
+
const bits = HUFF_BITS[s];
|
|
297
|
+
if (bits === 0 || bits > 8) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
const code = HUFF_CODES[s];
|
|
301
|
+
const count = 1 << (8 - bits);
|
|
302
|
+
for (let k = 0; k < count; k++) {
|
|
303
|
+
FAST[(code << (8 - bits)) | k] = (s << 8) | bits;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
292
307
|
// Returns the decoded byte count, or -1 if the input is not a valid sequence of
|
|
293
308
|
// complete codes (an explicit EOS, or a tail that is neither empty nor all ones).
|
|
294
309
|
function huffDecode(src, start, end, dst) {
|
|
295
|
-
let node = 0;
|
|
296
310
|
let out = 0;
|
|
297
|
-
let
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
311
|
+
let pos = start;
|
|
312
|
+
let shift = 0;
|
|
313
|
+
while (pos < end) {
|
|
314
|
+
const avail = 8 - shift;
|
|
315
|
+
// The next eight bits from the current code boundary. Bits past the end are
|
|
316
|
+
// zero-filled; a table entry is used only if its whole code is within the
|
|
317
|
+
// real bits left, so a fill bit can never decide a symbol.
|
|
318
|
+
const prefix = shift === 0 ? src[pos]
|
|
319
|
+
: (((src[pos] & ((1 << avail) - 1)) << shift)
|
|
320
|
+
| (pos + 1 < end ? src[pos + 1] >>> avail : 0));
|
|
321
|
+
const entry = FAST[prefix];
|
|
322
|
+
if (entry !== -1) {
|
|
323
|
+
const len = entry & 15;
|
|
324
|
+
if (len <= (end - pos) * 8 - shift) {
|
|
325
|
+
dst[out++] = entry >>> 8;
|
|
326
|
+
shift += len;
|
|
327
|
+
while (shift >= 8) {
|
|
328
|
+
shift -= 8;
|
|
329
|
+
pos++;
|
|
330
|
+
}
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// A code longer than eight bits, or the padding tail: walk the trie from
|
|
335
|
+
// the current bit and advance by the bits a symbol actually consumed.
|
|
336
|
+
let node = 0;
|
|
337
|
+
let p = pos;
|
|
338
|
+
let s = shift;
|
|
339
|
+
let pending = 0;
|
|
340
|
+
let found = false;
|
|
341
|
+
while (p < end) {
|
|
342
|
+
node = ((src[p] >>> (7 - s)) & 1) ? TR_RIGHT[node] : TR_LEFT[node];
|
|
302
343
|
const sym = TR_LEAF[node];
|
|
303
344
|
if (sym !== -1) {
|
|
304
345
|
if (sym === EOS) {
|
|
305
346
|
return -1;
|
|
306
347
|
}
|
|
307
348
|
dst[out++] = sym;
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
349
|
+
s++;
|
|
350
|
+
if (s === 8) {
|
|
351
|
+
s = 0;
|
|
352
|
+
p++;
|
|
353
|
+
}
|
|
354
|
+
pos = p;
|
|
355
|
+
shift = s;
|
|
356
|
+
found = true;
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
pending++;
|
|
360
|
+
s++;
|
|
361
|
+
if (s === 8) {
|
|
362
|
+
s = 0;
|
|
363
|
+
p++;
|
|
312
364
|
}
|
|
313
365
|
}
|
|
366
|
+
if (!found) {
|
|
367
|
+
// `pending` counts every bit since the last completed symbol; those bits
|
|
368
|
+
// are the padding and must be at most seven ones.
|
|
369
|
+
if (pending > 7 || (src[end - 1] & ((1 << pending) - 1)) !== (1 << pending) - 1) {
|
|
370
|
+
return -1;
|
|
371
|
+
}
|
|
372
|
+
return out;
|
|
373
|
+
}
|
|
314
374
|
}
|
|
315
|
-
//
|
|
316
|
-
|
|
317
|
-
if (tail > 7) {
|
|
318
|
-
return -1;
|
|
319
|
-
}
|
|
375
|
+
// a short code in the final octet can leave bits over; they are padding too
|
|
376
|
+
const tail = shift === 0 ? 0 : 8 - shift;
|
|
320
377
|
if (tail > 0 && (src[end - 1] & ((1 << tail) - 1)) !== (1 << tail) - 1) {
|
|
321
378
|
return -1;
|
|
322
379
|
}
|
|
@@ -948,6 +1005,22 @@ let L_NAME_BYTES = null;
|
|
|
948
1005
|
let L_VALUE_BITS = 0;
|
|
949
1006
|
let L_NEED = 0;
|
|
950
1007
|
|
|
1008
|
+
// Drop one evicted position from the name index. A pair can be present more than
|
|
1009
|
+
// once; only the position that left the table is removed.
|
|
1010
|
+
function byNameDrop(byName, name, k) {
|
|
1011
|
+
const ks = byName.get(name);
|
|
1012
|
+
if (ks === undefined) {
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
const i = ks.indexOf(k);
|
|
1016
|
+
if (i !== -1) {
|
|
1017
|
+
ks.splice(i, 1);
|
|
1018
|
+
}
|
|
1019
|
+
if (ks.length === 0) {
|
|
1020
|
+
byName.delete(name);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
|
|
951
1024
|
class Encoder {
|
|
952
1025
|
// `peerSize` is the peer's SETTINGS_HEADER_TABLE_SIZE -- the size it assumes the table
|
|
953
1026
|
// we build for it has -- and `cap` is the ceiling we are willing to spend on it. The
|
|
@@ -960,6 +1033,10 @@ class Encoder {
|
|
|
960
1033
|
this.size = 0;
|
|
961
1034
|
this.dyn = []; // oldest first; everything below `base` is evicted
|
|
962
1035
|
this.base = 0;
|
|
1036
|
+
// name -> ascending live positions in `dyn`. The scan below walks a few
|
|
1037
|
+
// positions under one name instead of the whole table; maintained on insert,
|
|
1038
|
+
// eviction, compaction and size changes, so the HPACK index math is unchanged.
|
|
1039
|
+
this.byName = new Map();
|
|
963
1040
|
this.epoch = 0; // bumped on every change the peer's table mirrors
|
|
964
1041
|
this.pendingSize = this.maxSize === peerSize ? null : this.maxSize;
|
|
965
1042
|
// The encoded bytes of a field line, in two levels: the name, then the value.
|
|
@@ -981,22 +1058,25 @@ class Encoder {
|
|
|
981
1058
|
}
|
|
982
1059
|
|
|
983
1060
|
// Newest dynamic index holding exactly this pair, 0 when absent. Only reached on a
|
|
984
|
-
// byte-cache miss
|
|
1061
|
+
// byte-cache miss; the byName index holds the live positions for one name, so the
|
|
1062
|
+
// walk is over that name's entries rather than the whole table.
|
|
985
1063
|
findFull(name, value) {
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1064
|
+
const ks = this.byName.get(name);
|
|
1065
|
+
if (ks !== undefined) {
|
|
1066
|
+
for (let i = ks.length - 1; i >= 0; i--) {
|
|
1067
|
+
const k = ks[i];
|
|
1068
|
+
if (this.dyn[k][1] === value) {
|
|
1069
|
+
return STATIC_LEN + this.dyn.length - k;
|
|
1070
|
+
}
|
|
990
1071
|
}
|
|
991
1072
|
}
|
|
992
1073
|
return 0;
|
|
993
1074
|
}
|
|
994
1075
|
|
|
995
1076
|
findName(name) {
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
}
|
|
1077
|
+
const ks = this.byName.get(name);
|
|
1078
|
+
if (ks !== undefined && ks.length !== 0) {
|
|
1079
|
+
return STATIC_LEN + this.dyn.length - ks[ks.length - 1];
|
|
1000
1080
|
}
|
|
1001
1081
|
return 0;
|
|
1002
1082
|
}
|
|
@@ -1010,23 +1090,38 @@ class Encoder {
|
|
|
1010
1090
|
if (sz > this.maxSize) { // RFC 7541 4.4: the table ends up empty
|
|
1011
1091
|
this.rememberEvicted(this.dyn, this.base, this.dyn.length);
|
|
1012
1092
|
this.dyn.length = this.base = 0;
|
|
1093
|
+
this.byName.clear();
|
|
1013
1094
|
this.size = 0;
|
|
1014
1095
|
} else {
|
|
1096
|
+
const k = this.dyn.length;
|
|
1015
1097
|
this.dyn.push([name, value]);
|
|
1098
|
+
let ks = this.byName.get(name);
|
|
1099
|
+
if (ks === undefined) {
|
|
1100
|
+
ks = [];
|
|
1101
|
+
this.byName.set(name, ks);
|
|
1102
|
+
}
|
|
1103
|
+
ks.push(k);
|
|
1016
1104
|
this.size += sz;
|
|
1017
1105
|
const from = this.base;
|
|
1018
1106
|
while (this.size > this.maxSize && this.dyn.length > this.base + 1) {
|
|
1019
1107
|
const old = this.dyn[this.base++];
|
|
1020
1108
|
this.size -= old[0].length + old[1].length + 32;
|
|
1109
|
+
byNameDrop(this.byName, old[0], this.base - 1);
|
|
1021
1110
|
dropped++;
|
|
1022
1111
|
}
|
|
1023
1112
|
if (dropped > 0) {
|
|
1024
1113
|
this.rememberEvicted(this.dyn, from, this.base);
|
|
1025
1114
|
}
|
|
1026
1115
|
if (this.base > 64) { // amortised compaction
|
|
1116
|
+
const shift = this.base;
|
|
1027
1117
|
this.dyn.copyWithin(0, this.base);
|
|
1028
1118
|
this.dyn.length -= this.base;
|
|
1029
1119
|
this.base = 0;
|
|
1120
|
+
for (const live of this.byName.values()) {
|
|
1121
|
+
for (let i = 0; i < live.length; i++) {
|
|
1122
|
+
live[i] -= shift;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1030
1125
|
this.size = 0;
|
|
1031
1126
|
for (const e of this.dyn) {
|
|
1032
1127
|
this.size += e[0].length + e[1].length + 32;
|
|
@@ -1105,6 +1200,7 @@ class Encoder {
|
|
|
1105
1200
|
while (this.size > want && this.dyn.length > this.base) {
|
|
1106
1201
|
const old = this.dyn[this.base++];
|
|
1107
1202
|
this.size -= old[0].length + old[1].length + 32;
|
|
1203
|
+
byNameDrop(this.byName, old[0], this.base - 1);
|
|
1108
1204
|
moved = true;
|
|
1109
1205
|
}
|
|
1110
1206
|
if (moved) {
|
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.
|