h2lean 0.16.0 โ 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 +2 -6
- package/lib/client-session.js +71 -25
- package/lib/compat.js +90 -1
- package/lib/hpack.js +120 -24
- 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
|
@@ -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/package.json
CHANGED