h2lean 0.11.3 → 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 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 v26.12.1). Zero runtime dependencies — `lib/` imports nothing but `node:` builtins.
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 } = require('./lib/session');
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
- // The packed SETTINGS blob node's getPackedSettings produces: id/value pairs.
22
- function getPackedSettings(settings) {
23
- const names = Object.keys(settings).filter((k) => ID_BY_SETTING[k] !== undefined);
24
- const body = Buffer.alloc(names.length * 6);
25
- let n = 0;
26
- for (const name of names) {
27
- const value = typeof settings[name] === 'boolean' ? (settings[name] ? 1 : 0) : settings[name];
28
- body.writeUInt16BE(ID_BY_SETTING[name], n);
29
- body.writeUInt32BE(value >>> 0, n + 2);
30
- n += 6;
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 body;
58
+ return true;
33
59
  }
34
60
 
35
- function getUnpackedSettings(buf) {
36
- const out = {};
37
- if (!Buffer.isBuffer(buf)) throw new TypeError('a Buffer is required');
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 Error('buffer length must be a multiple of 6');
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 name = SETTINGS_BY_ID[buf.readUInt16BE(i)];
45
- if (name === undefined) continue;
46
- out[name] = buf.readUInt32BE(i + 2);
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
  }