h2lean 0.11.1 → 0.16.0
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 +1 -1
- package/index.js +79 -19
- package/lib/client-session.js +260 -187
- package/lib/client-stream.js +2188 -147
- package/lib/client.js +9 -1
- package/lib/compat.js +68 -22
- package/lib/frames.js +25 -1
- package/lib/headers.js +4 -3
- package/lib/pieces.js +3 -1
- package/lib/server.js +125 -28
- package/lib/session.js +1047 -136
- package/lib/stream.js +213 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -101,7 +101,7 @@ Where behavior deliberately differs from node, it is **documented, measured, and
|
|
|
101
101
|
npm install h2lean
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
Requires Node.js **≥ 20** (verified against
|
|
104
|
+
Requires Node.js **≥ 20** (verified against v22, 24 and 26). Zero runtime dependencies — `lib/` imports nothing but `node:` builtins.
|
|
105
105
|
|
|
106
106
|
## 🚀 Getting Started
|
|
107
107
|
|
package/index.js
CHANGED
|
@@ -12,38 +12,98 @@
|
|
|
12
12
|
const nodeHttp2 = require('node:http2');
|
|
13
13
|
const { createServer, createSecureServer, performServerHandshake, Http2Server, Http2SecureServer,
|
|
14
14
|
Http2ServerRequest, Http2ServerResponse } = require('./lib/server');
|
|
15
|
-
const { Http2Session, OUR_DEFAULTS, PEER_DEFAULTS, SETTINGS_BY_ID, ID_BY_SETTING
|
|
15
|
+
const { Http2Session, OUR_DEFAULTS, PEER_DEFAULTS, SETTINGS_BY_ID, ID_BY_SETTING,
|
|
16
|
+
checkSettings, buildSubmission, submissionValues, extError, received } = require('./lib/session');
|
|
16
17
|
const { DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY,
|
|
17
18
|
WINDOW_UPDATE, CONTINUATION } = require('./lib/frames');
|
|
18
19
|
const { Decoder, Encoder, HPackError } = require('./lib/hpack');
|
|
19
20
|
const { connect } = require('./lib/client');
|
|
20
21
|
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
// node's getPackedSettings (core.js): assertIsObject + validateSettings, then
|
|
23
|
+
// updateSettingsBuffer -- the same packer every submission uses -- so the blob carries
|
|
24
|
+
// the folded customSettings entries, refuses a custom id of 0 with the packer's
|
|
25
|
+
// RangeError and warns on the maxHeaderSize alias, all where node's do. The octet order
|
|
26
|
+
// is the C++ Http2Settings::Init's: named entries ascending by wire id, then the custom
|
|
27
|
+
// section in for-in order.
|
|
28
|
+
// nghttp2's own packer validates the settings it knows (nghttp2_pack_settings_payload,
|
|
29
|
+
// lib/nghttp2_session.c) and node's getPackedSettings surfaces a refusal as
|
|
30
|
+
// `undefined` rather than an error -- measured on v26.12.1: {customSettings: {1: 2}}
|
|
31
|
+
// (ENABLE_PUSH), {customSettings: {6: 2}} (ENABLE_CONNECT_PROTOCOL) and
|
|
32
|
+
// {customSettings: {3: 16383}} (MAX_FRAME_SIZE) all return undefined with no throw. The
|
|
33
|
+
// same values through session.settings() abort node natively instead; that divergence is
|
|
34
|
+
// the one lib/client-session.js writes down. The named ranges are already enforced by
|
|
35
|
+
// checkSettings, so what can reach here out of definition is a value folded out of
|
|
36
|
+
// customSettings -- and a named value in the same call replaces its fold.
|
|
37
|
+
function nghttp2Accepts(folded) {
|
|
38
|
+
for (const [name, value] of folded) {
|
|
39
|
+
switch (name) {
|
|
40
|
+
case 'enablePush':
|
|
41
|
+
case 'enableConnectProtocol':
|
|
42
|
+
if (value !== 0 && value !== 1) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
case 'initialWindowSize':
|
|
47
|
+
if (value > 0x7fffffff) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
case 'maxFrameSize':
|
|
52
|
+
if (value < 16384 || value > 0xffffff) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
31
57
|
}
|
|
32
|
-
return
|
|
58
|
+
return true;
|
|
33
59
|
}
|
|
34
60
|
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
|
|
61
|
+
function getPackedSettings(settings) {
|
|
62
|
+
checkSettings(settings);
|
|
63
|
+
const built = buildSubmission(submissionValues(settings), settings?.customSettings);
|
|
64
|
+
return nghttp2Accepts(built.folded) ? built.body : undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// node's getUnpackedSettings walks the payload once: named ids turn into their settings,
|
|
68
|
+
// the two flags as booleans (`value !== 0`), id 6 fills maxHeaderListSize AND its
|
|
69
|
+
// maxHeaderSize mirror, and everything else -- including id 0, which no submission can
|
|
70
|
+
// produce -- lands in customSettings under its numeric id. The argument is any
|
|
71
|
+
// ArrayBufferView with a `length`, which is node's test (a Buffer or a TypedArray; a
|
|
72
|
+
// DataView has no length and is refused), and options.validate re-runs validateSettings
|
|
73
|
+
// on the result.
|
|
74
|
+
function getUnpackedSettings(buf, options) {
|
|
75
|
+
if (!ArrayBuffer.isView(buf) || buf.length === undefined) {
|
|
76
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
77
|
+
'The "buf" argument must be an instance of Buffer or TypedArray. ' +
|
|
78
|
+
`Received ${received(buf)}`);
|
|
79
|
+
}
|
|
38
80
|
if (buf.length % 6 !== 0) {
|
|
39
|
-
const err = new
|
|
81
|
+
const err = new RangeError('Packed settings length must be a multiple of six');
|
|
40
82
|
err.code = 'ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH';
|
|
41
83
|
throw err;
|
|
42
84
|
}
|
|
85
|
+
// The octets are read through a Buffer over the same memory: a Uint8Array is the same
|
|
86
|
+
// bytes, and nothing is copied either way.
|
|
87
|
+
const src = Buffer.isBuffer(buf)
|
|
88
|
+
? buf
|
|
89
|
+
: Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
90
|
+
const out = {};
|
|
43
91
|
for (let i = 0; i + 6 <= buf.length; i += 6) {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
92
|
+
const id = src.readUInt16BE(i);
|
|
93
|
+
const value = src.readUInt32BE(i + 2);
|
|
94
|
+
switch (id) {
|
|
95
|
+
case 1: out.headerTableSize = value; break;
|
|
96
|
+
case 2: out.enablePush = value !== 0; break;
|
|
97
|
+
case 3: out.maxConcurrentStreams = value; break;
|
|
98
|
+
case 4: out.initialWindowSize = value; break;
|
|
99
|
+
case 5: out.maxFrameSize = value; break;
|
|
100
|
+
case 6: out.maxHeaderListSize = out.maxHeaderSize = value; break;
|
|
101
|
+
case 8: out.enableConnectProtocol = value !== 0; break;
|
|
102
|
+
default: (out.customSettings ||= {})[id] = value;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (options !== undefined && options !== null && options.validate) {
|
|
106
|
+
checkSettings(out);
|
|
47
107
|
}
|
|
48
108
|
return out;
|
|
49
109
|
}
|