h2lean 0.17.3 → 0.17.5
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 +6 -6
- package/lib/Http2Stream.js +1328 -0
- package/lib/{client-session.js → client/Http2ClientSession.js} +28 -28
- package/lib/{client-stream.js → client/Http2ClientStream.js} +27 -782
- package/lib/{client.js → client/client.js} +4 -4
- package/lib/fast-timer.js +1 -1
- package/lib/headers.js +131 -25
- package/lib/hpack.js +43 -7
- package/lib/{session.js → server/Http2ServerSession.js} +101 -26
- package/lib/{stream.js → server/Http2ServerStream.js} +55 -1560
- package/lib/{compat.js → server/compat.js} +9 -9
- package/lib/{server.js → server/server.js} +9 -9
- package/package.json +1 -1
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
|
|
17
17
|
const net = require('node:net');
|
|
18
18
|
const tls = require('node:tls');
|
|
19
|
-
const {
|
|
20
|
-
const { strictSingleValueFields, received, outOfRangeValue, validateRemoteCustomSettings } = require('
|
|
19
|
+
const { Http2ClientSession } = require('./Http2ClientSession');
|
|
20
|
+
const { strictSingleValueFields, received, outOfRangeValue, validateRemoteCustomSettings } = require('../server/Http2ServerSession');
|
|
21
21
|
|
|
22
22
|
function notImplemented(what) {
|
|
23
23
|
const err = new Error(`h2lean: not implemented: ${what}`);
|
|
@@ -60,7 +60,7 @@ function connect(authority, options, listener) {
|
|
|
60
60
|
// the two failure classes: a non-number is ERR_INVALID_ARG_TYPE / TypeError,
|
|
61
61
|
// a number that is NaN or negative is ERR_OUT_OF_RANGE / RangeError. Both
|
|
62
62
|
// received-value strings are node's own wording -- determineSpecificType and
|
|
63
|
-
// the ERR_OUT_OF_RANGE formatter, shared with lib/
|
|
63
|
+
// the ERR_OUT_OF_RANGE formatter, shared with lib/server/Http2ServerSession.js -- because a
|
|
64
64
|
// paraphrase is a visible difference: `Received type object` where node says
|
|
65
65
|
// `Received an instance of Array`. Measured against a live node connect() over
|
|
66
66
|
// the 16 values node's own origin-set-max-size test passes plus the huge ones.
|
|
@@ -121,7 +121,7 @@ function connect(authority, options, listener) {
|
|
|
121
121
|
|
|
122
122
|
const scheme = protocol === 'https:' ? 'https' : 'http';
|
|
123
123
|
const authorityHeader = `${host}:${port}`;
|
|
124
|
-
const session = new
|
|
124
|
+
const session = new Http2ClientSession(socket, options, scheme, authorityHeader);
|
|
125
125
|
|
|
126
126
|
if (typeof listener === 'function') {
|
|
127
127
|
session.once('connect', listener);
|
package/lib/fast-timer.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
//
|
|
10
10
|
// What h2lean uses it for: one shared 499 ms clock for all idle/keep-alive
|
|
11
11
|
// timeouts, instead of one libuv timer handle per connection. See
|
|
12
|
-
//
|
|
12
|
+
// Http2ServerSession.setTimeout() in lib/server/Http2ServerSession.js.
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* This module offers an optimized timer implementation designed for scenarios
|
package/lib/headers.js
CHANGED
|
@@ -67,8 +67,8 @@ const VALID_NAMES_MAX = 1024;
|
|
|
67
67
|
|
|
68
68
|
// The fields node's toHeaderObject (internal/http2/util.js, kSingleValueFields) reads
|
|
69
69
|
// as a single value in BOTH directions: inbound, a second copy on the wire is IGNORED
|
|
70
|
-
// and the first copy stays the value the application sees (lib/
|
|
71
|
-
// lib/client
|
|
70
|
+
// and the first copy stays the value the application sees (lib/server/Http2ServerSession.js _toObject and
|
|
71
|
+
// lib/client/Http2ClientSession.js _toObject); outbound, node refuses a repeated copy or a
|
|
72
72
|
// multi-element array outright (parse() below, and the client's request list). Cookie
|
|
73
73
|
// and set-cookie are NOT in the set, measured on v26.12.1 by putting duplicate lines on
|
|
74
74
|
// the wire and reading the header object back (test/share-headers.test.js): cookie
|
|
@@ -143,7 +143,7 @@ const NAME_CACHE_MAX = 1024;
|
|
|
143
143
|
|
|
144
144
|
// The compat layer's shape. A name that is not a string throws here, the way
|
|
145
145
|
// node's private kSetHeader does (name.trim() is not a function), and the
|
|
146
|
-
// public accessors in lib/compat.js refuse it before they get here.
|
|
146
|
+
// public accessors in lib/server/compat.js refuse it before they get here.
|
|
147
147
|
function trimName(name) {
|
|
148
148
|
let out = NAME_CACHE.get(name);
|
|
149
149
|
if (out === undefined) {
|
|
@@ -201,7 +201,7 @@ const badValue = /[\u0000-\u0008\u000a-\u001f\u007f]/;
|
|
|
201
201
|
// runs no callback, so no call can observe another's list. The shape is node's
|
|
202
202
|
// own (buildNgHeaderString checks a lazily-grown array inline in its one pass,
|
|
203
203
|
// internal/http2/util.js) and the one the client's request() carries
|
|
204
|
-
// (lib/client
|
|
204
|
+
// (lib/client/Http2ClientSession.js, OPTIMIZATION.md item 36). Measured honestly: at the
|
|
205
205
|
// granularity parse() acts on, the swap from a per-call Set is inside the noise
|
|
206
206
|
// -- item 36's win came from removing request()'s second pass over the flat
|
|
207
207
|
// list, which parse() never had; the component costs are in OPTIMIZATION.md
|
|
@@ -221,7 +221,7 @@ const CONNECTION_SPECIFIC = new Set(['connection', 'keep-alive', 'proxy-connecti
|
|
|
221
221
|
'transfer-encoding', 'upgrade']);
|
|
222
222
|
|
|
223
223
|
// Whether a field value carries a character that may not appear in one. The server's
|
|
224
|
-
// inbound path asks the same question (lib/
|
|
224
|
+
// inbound path asks the same question (lib/server/Http2ServerSession.js) and answers it the way nghttp2
|
|
225
225
|
// does: the field is ignored, the stream lives.
|
|
226
226
|
function hasBadValue(value) {
|
|
227
227
|
return badValue.test(value);
|
|
@@ -567,7 +567,7 @@ function block(headers, opts, session) {
|
|
|
567
567
|
// The announcement goes in front of the block, in the same buffer: the old form was
|
|
568
568
|
// Buffer.concat([update, out]) -- a second allocation, a copy of the whole block and a
|
|
569
569
|
// temporary array, on the one path where the peer changed the table size. The client
|
|
570
|
-
// does the same thing in its own _encodeRequest (lib/client
|
|
570
|
+
// does the same thing in its own _encodeRequest (lib/client/Http2ClientSession.js).
|
|
571
571
|
const at = update === null ? 0 : update.length;
|
|
572
572
|
const end = encodeAt(parsed, enc, at);
|
|
573
573
|
if (at !== 0) {
|
|
@@ -595,39 +595,144 @@ function encode(parsed, enc) {
|
|
|
595
595
|
// The same pass, written at `at` instead of at the front of the scratch -- the place a
|
|
596
596
|
// size update's octets took, so that the announcement and the block are one buffer and
|
|
597
597
|
// one write. Returns the offset the block ends at.
|
|
598
|
-
function encodeAt(parsed, enc, at) {
|
|
598
|
+
function encodeAt(parsed, enc, at, dst, base) {
|
|
599
|
+
dst = dst === undefined ? scratch : dst;
|
|
600
|
+
base = base === undefined ? 0 : base;
|
|
599
601
|
enc.unsettled = false;
|
|
600
602
|
const pseudo = parsed.pseudo;
|
|
601
603
|
const fields = parsed.fields;
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
604
|
+
const cap = capOf(parsed, at);
|
|
605
|
+
sent.bound = boundOf(parsed);
|
|
606
|
+
if (dst.length < base + cap) {
|
|
607
|
+
// scratch is allowed to grow; a caller-owned destination reserved its room
|
|
608
|
+
// already, so a short one is a bug in the reservation, not a size to adapt to.
|
|
609
|
+
if (dst !== scratch) {
|
|
610
|
+
throw new Error('header destination smaller than the encoded estimate');
|
|
611
|
+
}
|
|
612
|
+
scratch = Buffer.allocUnsafe(cap);
|
|
613
|
+
dst = scratch;
|
|
614
|
+
base = 0;
|
|
615
|
+
}
|
|
616
|
+
let off = at;
|
|
617
|
+
for (let i = 0; i < pseudo.length; i += 2) {
|
|
618
|
+
off = enc.write(dst, base + off, pseudo[i], pseudo[i + 1], false) - base;
|
|
619
|
+
}
|
|
620
|
+
for (let i = 0; i < fields.length; i += 3) {
|
|
621
|
+
off = enc.write(dst, base + off, fields[i], fields[i + 1], fields[i + 2]) - base;
|
|
622
|
+
}
|
|
623
|
+
return off;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// nghttp2_hd_deflate_bound (nghttp2_hd.c) is `12 + 6*2*nvlen + sum(namelen+valuelen)`:
|
|
627
|
+
// the 12 is two possible 6-octet table-size updates, the 6*2 the two literal length
|
|
628
|
+
// prefixes per field. It counts the string lengths node handed down, not the UTF-8
|
|
629
|
+
// octets the encoder writes -- measured on v26.12.1: n 'é' (2 octets) and n '€' (3)
|
|
630
|
+
// hit the same boundary as n 'X'. The caller adds the per-frame-kind `additional`.
|
|
631
|
+
function boundOf(parsed) {
|
|
610
632
|
let bound = 12;
|
|
633
|
+
const pseudo = parsed.pseudo;
|
|
634
|
+
const fields = parsed.fields;
|
|
611
635
|
for (let i = 0; i < pseudo.length; i += 2) {
|
|
612
|
-
cap += Encoder.maxBytesOf(pseudo[i], pseudo[i + 1]);
|
|
613
636
|
bound += 12 + pseudo[i].length + pseudo[i + 1].length;
|
|
614
637
|
}
|
|
615
638
|
for (let i = 0; i < fields.length; i += 3) {
|
|
616
|
-
cap += Encoder.maxBytesOf(fields[i], fields[i + 1]);
|
|
617
639
|
bound += 12 + fields[i].length + fields[i + 1].length;
|
|
618
640
|
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
641
|
+
return bound;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Worst-case octets the encoder can write for this parsed block, including the
|
|
645
|
+
// optional size update at `at`. The session uses it to reserve room in its one
|
|
646
|
+
// output scratch before any byte is written.
|
|
647
|
+
function capOf(parsed, at) {
|
|
648
|
+
let cap = 16 + at;
|
|
649
|
+
const pseudo = parsed.pseudo;
|
|
650
|
+
const fields = parsed.fields;
|
|
651
|
+
for (let i = 0; i < pseudo.length; i += 2) {
|
|
652
|
+
cap += Encoder.maxBytesOf(pseudo[i], pseudo[i + 1]);
|
|
622
653
|
}
|
|
623
|
-
let
|
|
654
|
+
for (let i = 0; i < fields.length; i += 3) {
|
|
655
|
+
cap += Encoder.maxBytesOf(fields[i], fields[i + 1]);
|
|
656
|
+
}
|
|
657
|
+
return cap;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// The cache-hit plan has no getters to read on the way to writePrepared(), so unlike
|
|
661
|
+
// the parse path it cannot be re-entered between prepare() and writePrepared(). Keep
|
|
662
|
+
// one object and fill it in place: block()'s cache hit returns hit.bytes without
|
|
663
|
+
// allocating, so the direct-write path does not pay for a plan object there either.
|
|
664
|
+
let hitPlan = null;
|
|
665
|
+
|
|
666
|
+
// Two-phase form of block() for a caller that owns the destination buffer (the
|
|
667
|
+
// session's single scratch). prepare() reads the caller's header map -- including
|
|
668
|
+
// getters -- and returns everything needed to write; writePrepared() then touches
|
|
669
|
+
// only parsed values, so no reentrant call can move the destination under it.
|
|
670
|
+
// OPTIMIZATION.md item 68.
|
|
671
|
+
function prepare(headers, opts, session) {
|
|
672
|
+
const sendDate = !!(opts && opts.sendDate);
|
|
673
|
+
const enc = session._encoder;
|
|
674
|
+
const stamp = sendDate ? touchDate() : -1;
|
|
675
|
+
const cacheable = Object.isFrozen(headers) && enc.pendingSize === null;
|
|
676
|
+
if (cacheable) {
|
|
677
|
+
const hit = session._blocks.get(headers);
|
|
678
|
+
if (hit !== undefined && hit.epoch === enc.epoch && hit.stamp === stamp) {
|
|
679
|
+
sent.status = hit.status;
|
|
680
|
+
sent.date = hit.date;
|
|
681
|
+
sent.bound = hit.bound;
|
|
682
|
+
const plan = hitPlan === null ? (hitPlan = {}) : hitPlan;
|
|
683
|
+
plan.headers = headers;
|
|
684
|
+
plan.hit = hit;
|
|
685
|
+
plan.bound = hit.bound;
|
|
686
|
+
plan.status = hit.status;
|
|
687
|
+
plan.date = hit.date;
|
|
688
|
+
return plan;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
const parsed = parse(headers,
|
|
692
|
+
{ trailer: !!(opts && opts.trailer), sendDate, info: !!(opts && opts.info),
|
|
693
|
+
strict: session._strictSingle });
|
|
694
|
+
sent.status = parsed.status;
|
|
695
|
+
sent.date = parsed.genDate;
|
|
696
|
+
// The size update is NOT consumed here: writePrepared() writes its octets into the
|
|
697
|
+
// destination itself. Reserve the maximum the 5-bit prefix can need (the same
|
|
698
|
+
// bound takeSizeUpdate() allocates) so the encoder has room before a byte is written.
|
|
699
|
+
const at = enc.pendingSize === null ? 0 : 5;
|
|
700
|
+
let bound = 12;
|
|
701
|
+
let cap = 16 + at;
|
|
702
|
+
const pseudo = parsed.pseudo;
|
|
703
|
+
const fields = parsed.fields;
|
|
624
704
|
for (let i = 0; i < pseudo.length; i += 2) {
|
|
625
|
-
|
|
705
|
+
bound += 12 + pseudo[i].length + pseudo[i + 1].length;
|
|
706
|
+
cap += Encoder.maxBytesOf(pseudo[i], pseudo[i + 1]);
|
|
626
707
|
}
|
|
627
708
|
for (let i = 0; i < fields.length; i += 3) {
|
|
628
|
-
|
|
709
|
+
bound += 12 + fields[i].length + fields[i + 1].length;
|
|
710
|
+
cap += Encoder.maxBytesOf(fields[i], fields[i + 1]);
|
|
629
711
|
}
|
|
630
|
-
|
|
712
|
+
sent.bound = bound;
|
|
713
|
+
return { headers, parsed, at, cacheable, stamp, epoch: enc.epoch,
|
|
714
|
+
cap, bound, status: parsed.status, date: parsed.genDate };
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function writePrepared(prep, session, dst, off) {
|
|
718
|
+
const enc = session._encoder;
|
|
719
|
+
// Same octets as takeSizeUpdate(), written into the caller's buffer: no 5-octet
|
|
720
|
+
// Buffer and no subarray. The encoder consumes the pending update here, so the
|
|
721
|
+
// announcement cannot be repeated by a later block.
|
|
722
|
+
const at = enc.takeSizeUpdateInto(dst, off) - off;
|
|
723
|
+
if (prep.hit !== undefined) {
|
|
724
|
+
copyInto(dst, off + at, prep.hit.bytes, 0, prep.hit.bytes.length);
|
|
725
|
+
return off + at + prep.hit.bytes.length;
|
|
726
|
+
}
|
|
727
|
+
const end = encodeAt(prep.parsed, enc, at, dst, off);
|
|
728
|
+
if (prep.cacheable && enc.epoch === prep.epoch && !enc.unsettled) {
|
|
729
|
+
const bytes = Buffer.allocUnsafe(end - at);
|
|
730
|
+
copyInto(bytes, 0, dst, off + at, off + end);
|
|
731
|
+
session._blocks.set(prep.headers,
|
|
732
|
+
{ bytes, epoch: enc.epoch, stamp: prep.stamp, status: prep.status,
|
|
733
|
+
date: prep.date, bound: prep.bound });
|
|
734
|
+
}
|
|
735
|
+
return off + end;
|
|
631
736
|
}
|
|
632
737
|
|
|
633
738
|
// The HPACK block for a PUSH_PROMISE payload: request-style pseudo-headers, no date,
|
|
@@ -782,6 +887,7 @@ function requestView(src, scheme, authority) {
|
|
|
782
887
|
return view;
|
|
783
888
|
}
|
|
784
889
|
|
|
785
|
-
module.exports = { block, pushBlock, encode, parse,
|
|
890
|
+
module.exports = { block, pushBlock, encode, parse, prepare, writePrepared, capOf, boundOf,
|
|
891
|
+
nullProto, isFieldName, trimName, lowerName,
|
|
786
892
|
nameCacheSize, hasBadValue, neverNames, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS,
|
|
787
893
|
sent, responseView, requestView };
|
package/lib/hpack.js
CHANGED
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
//
|
|
36
36
|
// The encoder therefore holds per-connection state and must not be shared between
|
|
37
37
|
// sessions: the table has to match the one the peer's decoder builds from the same
|
|
38
|
-
// stream. lib/
|
|
38
|
+
// stream. lib/server/Http2ServerSession.js owns one Encoder per session, exactly as it owns a Decoder.
|
|
39
39
|
//
|
|
40
40
|
// Where this differs from nghttp2, which is what node:http2 embeds, and why. Read from
|
|
41
41
|
// lib/nghttp2_hd.c v1.70.0 (hd_deflate_decide_indexing(), deflate_nv()); the differences
|
|
@@ -1238,6 +1238,21 @@ class Encoder {
|
|
|
1238
1238
|
return b.subarray(0, writeInt(b, 0, n, 5, 0x20));
|
|
1239
1239
|
}
|
|
1240
1240
|
|
|
1241
|
+
// The size update written straight into the caller's destination. Same octets as
|
|
1242
|
+
// takeSizeUpdate(), but the direct-write path has no intermediate 5-octet Buffer
|
|
1243
|
+
// and no subarray to hand to the staging buffer.
|
|
1244
|
+
takeSizeUpdateInto(dst, off) {
|
|
1245
|
+
const n = this.pendingSize;
|
|
1246
|
+
if (n === null) {
|
|
1247
|
+
return off;
|
|
1248
|
+
}
|
|
1249
|
+
this.pendingSize = null;
|
|
1250
|
+
if (off + 5 > dst.length) {
|
|
1251
|
+
throw new HPackError('hpack: output buffer too small');
|
|
1252
|
+
}
|
|
1253
|
+
return writeInt(dst, off, n, 5, 0x20);
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1241
1256
|
touch() {
|
|
1242
1257
|
this.epoch++;
|
|
1243
1258
|
this.cache.clear();
|
|
@@ -1253,11 +1268,15 @@ class Encoder {
|
|
|
1253
1268
|
}
|
|
1254
1269
|
|
|
1255
1270
|
// Appends one field line to `dst` at `off`, returns the new offset.
|
|
1256
|
-
// Cached
|
|
1271
|
+
// Cached values are only valid for the table state that produced them: see touch().
|
|
1272
|
+
// A cached full index is a NUMBER, not bytes: the direct-write path keeps the
|
|
1273
|
+
// same octets without rebuilding them or paying for a 4-octet Buffer each time.
|
|
1257
1274
|
write(dst, off, name, value, neverIndex) {
|
|
1258
1275
|
const bytes = this.lookup(name, value, neverIndex);
|
|
1259
1276
|
if (bytes !== undefined) {
|
|
1260
|
-
return
|
|
1277
|
+
return typeof bytes === 'number'
|
|
1278
|
+
? this.indexedInto(dst, off, bytes)
|
|
1279
|
+
: this.put(dst, off, bytes);
|
|
1261
1280
|
}
|
|
1262
1281
|
return this.emit(dst, off, name, value, neverIndex);
|
|
1263
1282
|
}
|
|
@@ -1292,9 +1311,11 @@ class Encoder {
|
|
|
1292
1311
|
}
|
|
1293
1312
|
const hit = this.fullIndex(name, value);
|
|
1294
1313
|
if (hit !== undefined) {
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1314
|
+
// Remember the index as a number in the epoch-scoped cache, so the next
|
|
1315
|
+
// sighting of the pair takes lookup() + indexedInto() instead of another
|
|
1316
|
+
// fullIndex()/findFull() walk.
|
|
1317
|
+
this.remember(name, value, neverIndex, hit);
|
|
1318
|
+
return this.indexedInto(dst, off, hit);
|
|
1298
1319
|
}
|
|
1299
1320
|
// nghttp2's entropy rule (lib/nghttp2_hd.c deflate_nv): authorization always,
|
|
1300
1321
|
// and a cookie whose value is under 20 octets with it, go out never-indexed
|
|
@@ -1415,6 +1436,17 @@ class Encoder {
|
|
|
1415
1436
|
return b.subarray(0, writeInt(b, 0, index, 7, 0x80));
|
|
1416
1437
|
}
|
|
1417
1438
|
|
|
1439
|
+
// An indexed reference is one prefixed integer and nothing else. Writing it into
|
|
1440
|
+
// the destination removes both the 4-octet Buffer and the memcpy from the cached
|
|
1441
|
+
// bytes -- and the cache entry itself, which cannot survive an epoch change while
|
|
1442
|
+
// an index written at emit time is always current. OPTIMIZATION.md item 68.
|
|
1443
|
+
indexedInto(dst, off, index) {
|
|
1444
|
+
if (off + 4 > dst.length) {
|
|
1445
|
+
throw new HPackError('hpack: output buffer too small');
|
|
1446
|
+
}
|
|
1447
|
+
return writeInt(dst, off, index, 7, 0x80);
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1418
1450
|
// prefix 0x00 = without indexing (4-bit name field), 0x10 = never indexed,
|
|
1419
1451
|
// 0x40 = with incremental indexing (6-bit name field). A never-indexed line names
|
|
1420
1452
|
// its field in full as well: nothing about the field may be left in a table.
|
|
@@ -1488,8 +1520,12 @@ class Encoder {
|
|
|
1488
1520
|
}
|
|
1489
1521
|
|
|
1490
1522
|
// Upper bound on the bytes one field line can produce, for capacity planning.
|
|
1523
|
+
// literalStrInto() picks the shorter of the raw and Huffman forms, so a string
|
|
1524
|
+
// costs its own length in octets plus a 7-bit prefix; four spare prefix octets
|
|
1525
|
+
// cover any length that fits in an outbound header block. An indexed line is one
|
|
1526
|
+
// prefixed integer, well below this.
|
|
1491
1527
|
static maxBytesOf(name, value) {
|
|
1492
|
-
return
|
|
1528
|
+
return 11 + name.length + value.length;
|
|
1493
1529
|
}
|
|
1494
1530
|
}
|
|
1495
1531
|
|