h2lean 0.16.0 โ 0.17.3
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 +2 -6
- package/lib/client-session.js +71 -25
- package/lib/compat.js +90 -1
- package/lib/hpack.js +145 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -283,16 +283,12 @@ None silently returns a wrong answer: a missing method is `undefined`, so callin
|
|
|
283
283
|
|
|
284
284
|
**Missing methods / properties** (the compat `(req, res)` request side is complete):
|
|
285
285
|
|
|
286
|
-
- ๐ **`ServerHttp2Stream` is not a `stream.Duplex`** โ
|
|
287
|
-
- ๐ **`stream.respondWithFD()` / `respondWithFile()`** and the newer **`stream.bufferSize`** โ not implemented (nghttp2 `sendfile`-backed in node). Use `createReadStream()` + `stream.write()`; send-side backpressure already gates it.
|
|
288
|
-
- ๐ **`session.altsvc()` / `session.origin()`** โ not implemented (ALTSVC / ORIGIN frames, RFC 7838 / 8336).
|
|
289
|
-
- ๐งฐ **`ClientHttp2Stream`** inherits the Duplex surface but lacks the base-`Http2Stream` members: `close()`, `priority()`, `setTimeout()`, `sendTrailers()` and the getters `aborted`, `pending`, `state`, `sentTrailers`, `sentInfoHeaders`, `bufferSize`.
|
|
290
|
-
- ๐ค **`res.writeEarlyHints()`** (103 Early Hints) is not implemented (`res.writeInformation()` / `createPushResponse()` are); `Http2ServerResponse` is not an `http.ServerResponse`, so `res.cork()/uncork()` are absent (h2lean coalesces per turn already).
|
|
286
|
+
- ๐ **`ServerHttp2Stream` is not a `stream.Duplex`** โ its base is `stream.Stream`, but `pipe()`, `read()`, `cork()/uncork()`, `setEncoding()` and the `readable*`/`writable*` getters are carried as methods/getters over the hand-rolled core. Deliberate: a Duplex base costs ~1.4 ยตs/req of intrinsic machinery ([OPTIMIZATION.md](./OPTIMIZATION.md) item 41). `on('data'/'end')`, `pause/resume`, `write/end`, `setTimeout`, `priority`, `sendTrailers` all work.
|
|
291
287
|
- โฑ๏ธ **`server.timeout`** getter is absent (`setTimeout()` is present).
|
|
292
288
|
|
|
293
289
|
**Events not emitted:**
|
|
294
290
|
|
|
295
|
-
- ๐ชค **`'
|
|
291
|
+
- ๐ชค **`'ready'`** on a stream (node emits it when the stream id is assigned).
|
|
296
292
|
|
|
297
293
|
**Behavioural differences** (full reasoning in the header comment of the affected file and [AGENTS.md](./AGENTS.md) ยง10):
|
|
298
294
|
|
package/lib/client-session.js
CHANGED
|
@@ -1127,8 +1127,8 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1127
1127
|
list.push(':authority', this._authority,
|
|
1128
1128
|
neverList !== null && neverList.has(':authority'));
|
|
1129
1129
|
}
|
|
1130
|
-
|
|
1131
|
-
block = this._encodeRequest(
|
|
1130
|
+
orderPseudo(list); // in place: `list` is built by _addField for this request only
|
|
1131
|
+
block = this._encodeRequest(list);
|
|
1132
1132
|
bound = sentBound;
|
|
1133
1133
|
if (hp !== undefined && hp !== null && typeof hp === 'object') {
|
|
1134
1134
|
this._blocks.set(hp, { epoch: this._encoder.epoch, block, bound });
|
|
@@ -3313,38 +3313,84 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
3313
3313
|
|
|
3314
3314
|
// Pseudo-headers must precede regular fields (RFC 9113 8.1); the application may
|
|
3315
3315
|
// have interleaved them, so re-order :method, :path, :scheme, :authority, :protocol
|
|
3316
|
-
// to the front and leave the rest in place.
|
|
3316
|
+
// to the front and leave the rest in place. The flat list is this request's own
|
|
3317
|
+
// (built by _addField), so the three passes below reorder it in place: the old
|
|
3318
|
+
// form allocated a pseudo list, a fields list, an output list, a closure and a
|
|
3319
|
+
// priority-name array on every block-cache miss (bench/micro/order-pseudo-inplace.js).
|
|
3320
|
+
const PSEUDO_ORDER = [':method', ':path', ':scheme', ':authority', ':protocol'];
|
|
3317
3321
|
function orderPseudo(list) {
|
|
3318
|
-
|
|
3319
|
-
const fields = [];
|
|
3322
|
+
let w = 0;
|
|
3320
3323
|
for (let i = 0; i < list.length; i += 3) {
|
|
3321
|
-
(list[i].charCodeAt(0) === 58
|
|
3322
|
-
|
|
3324
|
+
if (list[i].charCodeAt(0) === 58) {
|
|
3325
|
+
if (i !== w) {
|
|
3326
|
+
const a = list[i];
|
|
3327
|
+
const b = list[i + 1];
|
|
3328
|
+
const c = list[i + 2];
|
|
3329
|
+
for (let k = i; k > w; k -= 3) {
|
|
3330
|
+
list[k] = list[k - 3];
|
|
3331
|
+
list[k + 1] = list[k - 2];
|
|
3332
|
+
list[k + 2] = list[k - 1];
|
|
3333
|
+
}
|
|
3334
|
+
list[w] = a;
|
|
3335
|
+
list[w + 1] = b;
|
|
3336
|
+
list[w + 2] = c;
|
|
3337
|
+
}
|
|
3338
|
+
w += 3;
|
|
3339
|
+
}
|
|
3340
|
+
}
|
|
3341
|
+
const n = w / 3;
|
|
3342
|
+
let pos = 0;
|
|
3343
|
+
for (let p = 0; p < PSEUDO_ORDER.length; p++) {
|
|
3344
|
+
for (let j = pos; j < n; j++) {
|
|
3345
|
+
if (list[j * 3] === PSEUDO_ORDER[p]) {
|
|
3346
|
+
const a = list[j * 3];
|
|
3347
|
+
const b = list[j * 3 + 1];
|
|
3348
|
+
const c = list[j * 3 + 2];
|
|
3349
|
+
for (let k = j; k > pos; k--) {
|
|
3350
|
+
list[k * 3] = list[(k - 1) * 3];
|
|
3351
|
+
list[k * 3 + 1] = list[(k - 1) * 3 + 1];
|
|
3352
|
+
list[k * 3 + 2] = list[(k - 1) * 3 + 2];
|
|
3353
|
+
}
|
|
3354
|
+
list[pos * 3] = a;
|
|
3355
|
+
list[pos * 3 + 1] = b;
|
|
3356
|
+
list[pos * 3 + 2] = c;
|
|
3357
|
+
pos++;
|
|
3358
|
+
break;
|
|
3359
|
+
}
|
|
3360
|
+
}
|
|
3323
3361
|
}
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3362
|
+
// The old form's out.indexOf() dropped every later pseudo line whose name had
|
|
3363
|
+
// already been emitted, duplicates included; keep that exact behaviour here.
|
|
3364
|
+
let kept = 0;
|
|
3365
|
+
for (let j = 0; j < n; j++) {
|
|
3366
|
+
const name = list[j * 3];
|
|
3367
|
+
let dup = false;
|
|
3368
|
+
for (let q = 0; q < kept; q++) {
|
|
3369
|
+
if (list[q * 3] === name) {
|
|
3370
|
+
dup = true;
|
|
3371
|
+
break;
|
|
3328
3372
|
}
|
|
3329
3373
|
}
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
const out = [];
|
|
3333
|
-
for (const n of [':method', ':path', ':scheme', ':authority', ':protocol']) {
|
|
3334
|
-
const idx = byIdx(n);
|
|
3335
|
-
if (idx !== -1) {
|
|
3336
|
-
out.push(pseudo[idx], pseudo[idx + 1], pseudo[idx + 2]);
|
|
3374
|
+
if (dup) {
|
|
3375
|
+
continue;
|
|
3337
3376
|
}
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3377
|
+
if (kept !== j) {
|
|
3378
|
+
list[kept * 3] = list[j * 3];
|
|
3379
|
+
list[kept * 3 + 1] = list[j * 3 + 1];
|
|
3380
|
+
list[kept * 3 + 2] = list[j * 3 + 2];
|
|
3342
3381
|
}
|
|
3382
|
+
kept++;
|
|
3343
3383
|
}
|
|
3344
|
-
|
|
3345
|
-
|
|
3384
|
+
if (kept !== n) {
|
|
3385
|
+
let d = kept * 3;
|
|
3386
|
+
for (let s = n * 3; s < list.length; s += 3) {
|
|
3387
|
+
list[d] = list[s];
|
|
3388
|
+
list[d + 1] = list[s + 1];
|
|
3389
|
+
list[d + 2] = list[s + 2];
|
|
3390
|
+
d += 3;
|
|
3391
|
+
}
|
|
3392
|
+
list.length = d;
|
|
3346
3393
|
}
|
|
3347
|
-
return out;
|
|
3348
3394
|
}
|
|
3349
3395
|
|
|
3350
3396
|
function httpErr(code, msg) {
|
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/hpack.js
CHANGED
|
@@ -250,25 +250,43 @@ function huffEncode(src, start, end, dst, off) {
|
|
|
250
250
|
// so the byte of a character is its code unit & 0xff -- the byte the buffer version
|
|
251
251
|
// above would have been handed. Coding straight from the string saves the copy into
|
|
252
252
|
// a scratch buffer and the view of it that the copy needed (OPTIMIZATION.md item 12).
|
|
253
|
+
// The accumulator is kept below 2^31 so emitting an octet is two bitwise ops
|
|
254
|
+
// instead of a floating-point division by POW2[nbits]. A code that does not fit
|
|
255
|
+
// in the remaining room is split: its high bits fill the accumulator, the whole
|
|
256
|
+
// octets are flushed, then its low bits are appended. Only the rare 19-30 bit
|
|
257
|
+
// codes take that path; digits and letters are 5-8 bits and never split.
|
|
253
258
|
function huffEncodeStr(s, dst, off) {
|
|
254
259
|
let acc = 0;
|
|
255
260
|
let nbits = 0;
|
|
256
261
|
for (let i = 0; i < s.length; i++) {
|
|
257
262
|
const sym = s.charCodeAt(i) & 0xff;
|
|
258
263
|
const b = HUFF_BITS[sym];
|
|
259
|
-
|
|
260
|
-
|
|
264
|
+
const code = HUFF_CODES[sym];
|
|
265
|
+
const room = 31 - nbits;
|
|
266
|
+
if (b <= room) {
|
|
267
|
+
acc = (acc << b) | code;
|
|
268
|
+
nbits += b;
|
|
269
|
+
} else {
|
|
270
|
+
acc = (acc << room) | (code >>> (b - room));
|
|
271
|
+
nbits = 31;
|
|
272
|
+
while (nbits >= 8) {
|
|
273
|
+
nbits -= 8;
|
|
274
|
+
dst[off++] = (acc >>> nbits) & 0xff;
|
|
275
|
+
acc &= (1 << nbits) - 1;
|
|
276
|
+
}
|
|
277
|
+
const rest = b - room;
|
|
278
|
+
acc = (acc << rest) | (code & (2 ** rest - 1));
|
|
279
|
+
nbits += rest;
|
|
280
|
+
}
|
|
261
281
|
while (nbits >= 8) {
|
|
262
282
|
nbits -= 8;
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
acc -= byte * POW2[nbits];
|
|
283
|
+
dst[off++] = (acc >>> nbits) & 0xff;
|
|
284
|
+
acc &= (1 << nbits) - 1;
|
|
266
285
|
}
|
|
267
286
|
}
|
|
268
287
|
// EOS is the all-ones code, so padding the last byte with ones *is* EOS
|
|
269
288
|
if (nbits > 0) {
|
|
270
|
-
|
|
271
|
-
dst[off++] = acc * POW2[8 - nbits] + pad;
|
|
289
|
+
dst[off++] = (acc << (8 - nbits)) | (2 ** (8 - nbits) - 1);
|
|
272
290
|
}
|
|
273
291
|
return off;
|
|
274
292
|
}
|
|
@@ -289,34 +307,91 @@ function latin1Into(dst, off, s) {
|
|
|
289
307
|
return off + len;
|
|
290
308
|
}
|
|
291
309
|
|
|
310
|
+
// Eight-bit prefix table: FAST[byte] = (symbol << 8) | length for the 74 codes
|
|
311
|
+
// of at most eight bits. Longer codes, EOS and the padding tail use the trie.
|
|
312
|
+
const FAST = new Int32Array(256).fill(-1);
|
|
313
|
+
for (let s = 0; s < EOS; s++) {
|
|
314
|
+
const bits = HUFF_BITS[s];
|
|
315
|
+
if (bits === 0 || bits > 8) {
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const code = HUFF_CODES[s];
|
|
319
|
+
const count = 1 << (8 - bits);
|
|
320
|
+
for (let k = 0; k < count; k++) {
|
|
321
|
+
FAST[(code << (8 - bits)) | k] = (s << 8) | bits;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
292
325
|
// Returns the decoded byte count, or -1 if the input is not a valid sequence of
|
|
293
326
|
// complete codes (an explicit EOS, or a tail that is neither empty nor all ones).
|
|
294
327
|
function huffDecode(src, start, end, dst) {
|
|
295
|
-
let node = 0;
|
|
296
328
|
let out = 0;
|
|
297
|
-
let
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
329
|
+
let pos = start;
|
|
330
|
+
let shift = 0;
|
|
331
|
+
while (pos < end) {
|
|
332
|
+
const avail = 8 - shift;
|
|
333
|
+
// The next eight bits from the current code boundary. Bits past the end are
|
|
334
|
+
// zero-filled; a table entry is used only if its whole code is within the
|
|
335
|
+
// real bits left, so a fill bit can never decide a symbol.
|
|
336
|
+
const prefix = shift === 0 ? src[pos]
|
|
337
|
+
: (((src[pos] & ((1 << avail) - 1)) << shift)
|
|
338
|
+
| (pos + 1 < end ? src[pos + 1] >>> avail : 0));
|
|
339
|
+
const entry = FAST[prefix];
|
|
340
|
+
if (entry !== -1) {
|
|
341
|
+
const len = entry & 15;
|
|
342
|
+
if (len <= (end - pos) * 8 - shift) {
|
|
343
|
+
dst[out++] = entry >>> 8;
|
|
344
|
+
shift += len;
|
|
345
|
+
while (shift >= 8) {
|
|
346
|
+
shift -= 8;
|
|
347
|
+
pos++;
|
|
348
|
+
}
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
// A code longer than eight bits, or the padding tail: walk the trie from
|
|
353
|
+
// the current bit and advance by the bits a symbol actually consumed.
|
|
354
|
+
let node = 0;
|
|
355
|
+
let p = pos;
|
|
356
|
+
let s = shift;
|
|
357
|
+
let pending = 0;
|
|
358
|
+
let found = false;
|
|
359
|
+
while (p < end) {
|
|
360
|
+
node = ((src[p] >>> (7 - s)) & 1) ? TR_RIGHT[node] : TR_LEFT[node];
|
|
302
361
|
const sym = TR_LEAF[node];
|
|
303
362
|
if (sym !== -1) {
|
|
304
363
|
if (sym === EOS) {
|
|
305
364
|
return -1;
|
|
306
365
|
}
|
|
307
366
|
dst[out++] = sym;
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
367
|
+
s++;
|
|
368
|
+
if (s === 8) {
|
|
369
|
+
s = 0;
|
|
370
|
+
p++;
|
|
371
|
+
}
|
|
372
|
+
pos = p;
|
|
373
|
+
shift = s;
|
|
374
|
+
found = true;
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
pending++;
|
|
378
|
+
s++;
|
|
379
|
+
if (s === 8) {
|
|
380
|
+
s = 0;
|
|
381
|
+
p++;
|
|
312
382
|
}
|
|
313
383
|
}
|
|
384
|
+
if (!found) {
|
|
385
|
+
// `pending` counts every bit since the last completed symbol; those bits
|
|
386
|
+
// are the padding and must be at most seven ones.
|
|
387
|
+
if (pending > 7 || (src[end - 1] & ((1 << pending) - 1)) !== (1 << pending) - 1) {
|
|
388
|
+
return -1;
|
|
389
|
+
}
|
|
390
|
+
return out;
|
|
391
|
+
}
|
|
314
392
|
}
|
|
315
|
-
//
|
|
316
|
-
|
|
317
|
-
if (tail > 7) {
|
|
318
|
-
return -1;
|
|
319
|
-
}
|
|
393
|
+
// a short code in the final octet can leave bits over; they are padding too
|
|
394
|
+
const tail = shift === 0 ? 0 : 8 - shift;
|
|
320
395
|
if (tail > 0 && (src[end - 1] & ((1 << tail) - 1)) !== (1 << tail) - 1) {
|
|
321
396
|
return -1;
|
|
322
397
|
}
|
|
@@ -948,6 +1023,22 @@ let L_NAME_BYTES = null;
|
|
|
948
1023
|
let L_VALUE_BITS = 0;
|
|
949
1024
|
let L_NEED = 0;
|
|
950
1025
|
|
|
1026
|
+
// Drop one evicted position from the name index. A pair can be present more than
|
|
1027
|
+
// once; only the position that left the table is removed.
|
|
1028
|
+
function byNameDrop(byName, name, k) {
|
|
1029
|
+
const ks = byName.get(name);
|
|
1030
|
+
if (ks === undefined) {
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
const i = ks.indexOf(k);
|
|
1034
|
+
if (i !== -1) {
|
|
1035
|
+
ks.splice(i, 1);
|
|
1036
|
+
}
|
|
1037
|
+
if (ks.length === 0) {
|
|
1038
|
+
byName.delete(name);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
951
1042
|
class Encoder {
|
|
952
1043
|
// `peerSize` is the peer's SETTINGS_HEADER_TABLE_SIZE -- the size it assumes the table
|
|
953
1044
|
// we build for it has -- and `cap` is the ceiling we are willing to spend on it. The
|
|
@@ -960,6 +1051,10 @@ class Encoder {
|
|
|
960
1051
|
this.size = 0;
|
|
961
1052
|
this.dyn = []; // oldest first; everything below `base` is evicted
|
|
962
1053
|
this.base = 0;
|
|
1054
|
+
// name -> ascending live positions in `dyn`. The scan below walks a few
|
|
1055
|
+
// positions under one name instead of the whole table; maintained on insert,
|
|
1056
|
+
// eviction, compaction and size changes, so the HPACK index math is unchanged.
|
|
1057
|
+
this.byName = new Map();
|
|
963
1058
|
this.epoch = 0; // bumped on every change the peer's table mirrors
|
|
964
1059
|
this.pendingSize = this.maxSize === peerSize ? null : this.maxSize;
|
|
965
1060
|
// The encoded bytes of a field line, in two levels: the name, then the value.
|
|
@@ -981,22 +1076,25 @@ class Encoder {
|
|
|
981
1076
|
}
|
|
982
1077
|
|
|
983
1078
|
// Newest dynamic index holding exactly this pair, 0 when absent. Only reached on a
|
|
984
|
-
// byte-cache miss
|
|
1079
|
+
// byte-cache miss; the byName index holds the live positions for one name, so the
|
|
1080
|
+
// walk is over that name's entries rather than the whole table.
|
|
985
1081
|
findFull(name, value) {
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1082
|
+
const ks = this.byName.get(name);
|
|
1083
|
+
if (ks !== undefined) {
|
|
1084
|
+
for (let i = ks.length - 1; i >= 0; i--) {
|
|
1085
|
+
const k = ks[i];
|
|
1086
|
+
if (this.dyn[k][1] === value) {
|
|
1087
|
+
return STATIC_LEN + this.dyn.length - k;
|
|
1088
|
+
}
|
|
990
1089
|
}
|
|
991
1090
|
}
|
|
992
1091
|
return 0;
|
|
993
1092
|
}
|
|
994
1093
|
|
|
995
1094
|
findName(name) {
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
}
|
|
1095
|
+
const ks = this.byName.get(name);
|
|
1096
|
+
if (ks !== undefined && ks.length !== 0) {
|
|
1097
|
+
return STATIC_LEN + this.dyn.length - ks[ks.length - 1];
|
|
1000
1098
|
}
|
|
1001
1099
|
return 0;
|
|
1002
1100
|
}
|
|
@@ -1010,23 +1108,38 @@ class Encoder {
|
|
|
1010
1108
|
if (sz > this.maxSize) { // RFC 7541 4.4: the table ends up empty
|
|
1011
1109
|
this.rememberEvicted(this.dyn, this.base, this.dyn.length);
|
|
1012
1110
|
this.dyn.length = this.base = 0;
|
|
1111
|
+
this.byName.clear();
|
|
1013
1112
|
this.size = 0;
|
|
1014
1113
|
} else {
|
|
1114
|
+
const k = this.dyn.length;
|
|
1015
1115
|
this.dyn.push([name, value]);
|
|
1116
|
+
let ks = this.byName.get(name);
|
|
1117
|
+
if (ks === undefined) {
|
|
1118
|
+
ks = [];
|
|
1119
|
+
this.byName.set(name, ks);
|
|
1120
|
+
}
|
|
1121
|
+
ks.push(k);
|
|
1016
1122
|
this.size += sz;
|
|
1017
1123
|
const from = this.base;
|
|
1018
1124
|
while (this.size > this.maxSize && this.dyn.length > this.base + 1) {
|
|
1019
1125
|
const old = this.dyn[this.base++];
|
|
1020
1126
|
this.size -= old[0].length + old[1].length + 32;
|
|
1127
|
+
byNameDrop(this.byName, old[0], this.base - 1);
|
|
1021
1128
|
dropped++;
|
|
1022
1129
|
}
|
|
1023
1130
|
if (dropped > 0) {
|
|
1024
1131
|
this.rememberEvicted(this.dyn, from, this.base);
|
|
1025
1132
|
}
|
|
1026
1133
|
if (this.base > 64) { // amortised compaction
|
|
1134
|
+
const shift = this.base;
|
|
1027
1135
|
this.dyn.copyWithin(0, this.base);
|
|
1028
1136
|
this.dyn.length -= this.base;
|
|
1029
1137
|
this.base = 0;
|
|
1138
|
+
for (const live of this.byName.values()) {
|
|
1139
|
+
for (let i = 0; i < live.length; i++) {
|
|
1140
|
+
live[i] -= shift;
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1030
1143
|
this.size = 0;
|
|
1031
1144
|
for (const e of this.dyn) {
|
|
1032
1145
|
this.size += e[0].length + e[1].length + 32;
|
|
@@ -1105,6 +1218,7 @@ class Encoder {
|
|
|
1105
1218
|
while (this.size > want && this.dyn.length > this.base) {
|
|
1106
1219
|
const old = this.dyn[this.base++];
|
|
1107
1220
|
this.size -= old[0].length + old[1].length + 32;
|
|
1221
|
+
byNameDrop(this.byName, old[0], this.base - 1);
|
|
1108
1222
|
moved = true;
|
|
1109
1223
|
}
|
|
1110
1224
|
if (moved) {
|
package/package.json
CHANGED