h2lean 0.19.7 → 0.19.8

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.
@@ -103,6 +103,12 @@ const hdr = require('./headers');
103
103
 
104
104
  const EMPTY = Buffer.alloc(0);
105
105
 
106
+ // The compat layer's cached req.socket/res.socket Proxy, one per stream. node
107
+ // reserves this slot in its Http2Stream constructor (core.js:1343,2166) so the
108
+ // first `.socket` read only fills the slot instead of adding a property and
109
+ // changing the stream's hidden class. Server streams initialise it to null.
110
+ const kCompatSocket = Symbol('compatSocket');
111
+
106
112
  // node's Http2Stream sets no highWaterMark (internal/http2/core.js constructor passes only
107
113
  // allowHalfOpen/decodeStrings/autoDestroy), so its writableHighWaterMark is the default
108
114
  // Writable one -- 16384 on node 20, 65536 from 22 on. Read it off node itself once rather
@@ -1325,4 +1331,4 @@ function streamErrorOf(code) {
1325
1331
  }
1326
1332
 
1327
1333
 
1328
- module.exports = { Http2Stream, fs, frames, hdr, EMPTY, WRITABLE_HWM, READABLE_HWM, emitNT, emitCloseNT, emitDrainNT, emitErrorCloseNT, emitErrorOnlyNT, emitEndNT, wantTrailersNT, finishNT, emitEndCloseNT, emitCloseDeliveredNT, autoCloseNT, noopClose, FILE_CHUNK, isFileHandle, receivedArgType, argTypeError, propertyValueError, fdOptions, prepareResponseHeadersObject, errorOf, specType, validateRstCode, kEmpty, kEof, ASYNC_ITERATOR_PROTO, noop, abortError, validateObjectArg, validateFunctionArg, validateSignalArg, validateOperatorOptions, toIntegerOrInfinity, createStreamIterator, opMap, opFilter, opFlatMap, opDrop, opTake, streamErrorOf };
1334
+ module.exports = { Http2Stream, fs, frames, hdr, EMPTY, kCompatSocket, WRITABLE_HWM, READABLE_HWM, emitNT, emitCloseNT, emitDrainNT, emitErrorCloseNT, emitErrorOnlyNT, emitEndNT, wantTrailersNT, finishNT, emitEndCloseNT, emitCloseDeliveredNT, autoCloseNT, noopClose, FILE_CHUNK, isFileHandle, receivedArgType, argTypeError, propertyValueError, fdOptions, prepareResponseHeadersObject, errorOf, specType, validateRstCode, kEmpty, kEof, ASYNC_ITERATOR_PROTO, noop, abortError, validateObjectArg, validateFunctionArg, validateSignalArg, validateOperatorOptions, toIntegerOrInfinity, createStreamIterator, opMap, opFilter, opFlatMap, opDrop, opTake, streamErrorOf };
@@ -33,10 +33,11 @@
33
33
  // consumer's waiter has registered.
34
34
  // * Writable backpressure is the queue length, not node's highWaterMark
35
35
  // arithmetic: write() returns false while a chunk is parked or queued and
36
- // 'drain' fires once when the queue empties. `writableLength`,
37
- // `writableBuffer` and `bufferSize` report the octets still queued in the
38
- // writable queue; the one chunk in flight lives in the session's `_pending`
39
- // slot (write-through), which is where node's nghttp2 queue would hold it.
36
+ // 'drain' fires once when the queue empties. `writableLength` and
37
+ // `writableBuffer` report the octets still queued in the writable queue;
38
+ // `bufferSize` adds the unwritten remainder of the one chunk in flight in
39
+ // the session's `_pending` slot (write-through), which is where node's
40
+ // nghttp2 queue holds it (core.js:2215, writeQueueSize + writableLength).
40
41
  // * `cork()`/`uncork()` are counting stubs like the server's; the cross-tick
41
42
  // deferral node's Writable.cork does is a known divergence (pinned
42
43
  // server-side by test/cork-parity.test.js).
@@ -1657,6 +1658,16 @@ class Http2ClientStream extends Http2Stream {
1657
1658
  return out;
1658
1659
  }
1659
1660
 
1661
+ // node's bufferSize is writeQueueSize + writableLength (core.js:2215). The
1662
+ // chunk parked in _pending is in our write queue, not an already-written one,
1663
+ // so leaving it out undercounted by exactly one chunk (test/upstream/
1664
+ // buffersize.test.js). _pendingOff is how much of it the session has framed.
1665
+ get bufferSize() {
1666
+ const pending = this._pending;
1667
+ return this.writableLength +
1668
+ (pending === null ? 0 : pending.length - this._pendingOff);
1669
+ }
1670
+
1660
1671
  get writableNeedDrain() {
1661
1672
  return this._needDrain === true && this._destroyFlag !== true &&
1662
1673
  this._endQueued !== true;
@@ -702,7 +702,7 @@ function parseOrigins(buf, start, len) {
702
702
  // getDefaultSettings() so the numbers the guards compare against are the ones node's guards compare
703
703
  // against. They are NOT advertised by default: what goes into a SETTINGS frame is decided by
704
704
  // _advertised, because an advertised value is a promise to the peer rather than a local choice.
705
- const OUR_DEFAULTS = { ...require('node:http2').getDefaultSettings(), enablePush: false };
705
+ const OUR_DEFAULTS = { ...require('node:http2').getDefaultSettings() };
706
706
 
707
707
  // What node actually has in effect before the peer's SETTINGS arrives. Read off
708
708
  // a live session: doc/api/http2.md quotes different numbers for
@@ -96,7 +96,7 @@ const {
96
96
  PROTOCOL_ERROR, INTERNAL_ERROR, HEADERS
97
97
  } = frames;
98
98
  const hdr = require('../headers');
99
- const { Http2Stream, EMPTY, READABLE_HWM, emitCloseNT, emitErrorCloseNT, emitErrorOnlyNT, emitEndNT, wantTrailersNT, finishNT, emitEndCloseNT, emitCloseDeliveredNT, autoCloseNT, noopClose, FILE_CHUNK, isFileHandle, argTypeError, fdOptions, prepareResponseHeadersObject, errorOf, validateRstCode, abortError, validateObjectArg, validateSignalArg, streamErrorOf, createStreamIterator } = require('../Http2Stream');
99
+ const { Http2Stream, EMPTY, kCompatSocket, READABLE_HWM, emitCloseNT, emitErrorCloseNT, emitErrorOnlyNT, emitEndNT, wantTrailersNT, finishNT, emitEndCloseNT, emitCloseDeliveredNT, autoCloseNT, noopClose, FILE_CHUNK, isFileHandle, argTypeError, fdOptions, prepareResponseHeadersObject, errorOf, validateRstCode, abortError, validateObjectArg, validateSignalArg, streamErrorOf, createStreamIterator } = require('../Http2Stream');
100
100
 
101
101
  class Http2ServerStream extends Http2Stream {
102
102
  constructor(session, id) {
@@ -205,6 +205,10 @@ class Http2ServerStream extends Http2Stream {
205
205
  // The finish work carried into a close-delivery tick scheduled by the same call
206
206
  // of _endLocalSide (see the fuse there) instead of a nextTick of its own.
207
207
  this._finishFuse = false;
208
+ // Reserved here, as node reserves kProxySocket in its stream constructor, so
209
+ // the compat layer's first `.socket` read fills a slot instead of adding one
210
+ // to an already-live object. Only filled when the application reads it.
211
+ this[kCompatSocket] = null;
208
212
  }
209
213
 
210
214
  get destroyed() {
@@ -55,6 +55,7 @@
55
55
  const { Readable, Stream } = require('node:stream');
56
56
 
57
57
  const { nullProto, trimName, isFieldName } = require('../headers');
58
+ const { kCompatSocket } = require('../Http2Stream');
58
59
 
59
60
  function err(code, msg) {
60
61
  const e = new Error(msg);
@@ -62,6 +63,99 @@ function err(code, msg) {
62
63
  return e;
63
64
  }
64
65
 
66
+ // node's compat `req.socket`/`res.socket` is a Proxy over the STREAM (a different
67
+ // handler from session.socket's core.js one): write/read/pause/resume throw,
68
+ // end/emit/destroy route to the stream, everything else falls through to the
69
+ // transport socket. The stream reserves the slot at construction (Http2Stream.js's
70
+ // kCompatSocket), so the first read only creates the Proxy and fills it; req and
71
+ // res share one object and a handler that never reads either pays no allocation.
72
+ // Ported from node's proxySocketHandler (internal/http2/compat.js).
73
+ const NO_SOCKET_MANIPULATION = 'HTTP/2 sockets should not be directly manipulated (e.g. read and written)';
74
+ const compatSocketHandler = {
75
+ has(stream, prop) {
76
+ const ref = stream.session !== undefined ? stream.session._socket : stream;
77
+ return (prop in stream) || (prop in ref);
78
+ },
79
+ get(stream, prop) {
80
+ switch (prop) {
81
+ case 'on':
82
+ case 'once':
83
+ case 'end':
84
+ case 'emit':
85
+ case 'destroy':
86
+ return stream[prop].bind(stream);
87
+ case 'writable':
88
+ case 'destroyed':
89
+ return stream[prop];
90
+ case 'readable':
91
+ return stream.destroyed ? false : stream.readable;
92
+ case 'setTimeout': {
93
+ const session = stream.session;
94
+ return session !== undefined
95
+ ? session.setTimeout.bind(session) : stream.setTimeout.bind(stream);
96
+ }
97
+ case 'write':
98
+ case 'read':
99
+ case 'pause':
100
+ case 'resume':
101
+ throw err('ERR_HTTP2_NO_SOCKET_MANIPULATION', NO_SOCKET_MANIPULATION);
102
+ default: {
103
+ const session = stream.session;
104
+ const ref = session !== undefined ? session._socket : stream;
105
+ const value = ref[prop];
106
+ return typeof value === 'function' ? value.bind(ref) : value;
107
+ }
108
+ }
109
+ },
110
+ getPrototypeOf(stream) {
111
+ const session = stream.session;
112
+ return Reflect.getPrototypeOf(session !== undefined ? session._socket : stream);
113
+ },
114
+ set(stream, prop, value) {
115
+ switch (prop) {
116
+ case 'writable':
117
+ case 'readable':
118
+ case 'destroyed':
119
+ case 'on':
120
+ case 'once':
121
+ case 'end':
122
+ case 'emit':
123
+ case 'destroy':
124
+ stream[prop] = value;
125
+ return true;
126
+ case 'setTimeout': {
127
+ const session = stream.session;
128
+ if (session !== undefined) {
129
+ session.setTimeout = value;
130
+ } else {
131
+ stream.setTimeout = value;
132
+ }
133
+ return true;
134
+ }
135
+ case 'write':
136
+ case 'read':
137
+ case 'pause':
138
+ case 'resume':
139
+ throw err('ERR_HTTP2_NO_SOCKET_MANIPULATION', NO_SOCKET_MANIPULATION);
140
+ default: {
141
+ const session = stream.session;
142
+ const ref = session !== undefined ? session._socket : stream;
143
+ ref[prop] = value;
144
+ return true;
145
+ }
146
+ }
147
+ }
148
+ };
149
+
150
+ function compatSocket(stream) {
151
+ let proxy = stream[kCompatSocket];
152
+ if (proxy === null) {
153
+ proxy = new Proxy(stream, compatSocketHandler);
154
+ stream[kCompatSocket] = proxy;
155
+ }
156
+ return proxy;
157
+ }
158
+
65
159
  // node's Link validation for writeEarlyHints (internal/validators.js:528-583,
66
160
  // v26.12.1): a string, or an array of strings joined with ', ', each matching the
67
161
  // link-value grammar `</...>; params`. The regex is node's own linkValueRegExp.
@@ -396,15 +490,11 @@ class Http2ServerRequest extends Readable {
396
490
  return '2.0';
397
491
  }
398
492
 
399
- // node hands `req.socket` a Proxy over the *stream* (compat.js proxySocketHandler, distinct
400
- // from session.socket's core handler): write/read/pause/resume throw, end/emit/destroy route
401
- // to the stream. h2lean instead hands out the real socket -- it is smaller and answers the
402
- // same questions (remoteAddress), but a `.write()` that node refuses succeeds here. KNOWN
403
- // DIVERGENCE, and separate from the session surface. The session reference is cleared when the
404
- // stream dies (lib/Http2Stream.js _destroy, node core.js:2546), so the socket goes with it.
493
+ // node's IncomingMessage keeps its socket after the stream dies (only its
494
+ // ServerResponse drops it), so the proxy is returned unconditionally and is
495
+ // cached on the stream from the first read -- lazy, and one object per stream.
405
496
  get socket() {
406
- const session = this._stream.session;
407
- return session === undefined ? undefined : session._socket;
497
+ return compatSocket(this._stream);
408
498
  }
409
499
  get connection() {
410
500
  return this.socket;
@@ -573,8 +663,7 @@ class Http2ServerResponse extends Stream {
573
663
  if (this._state.closed) {
574
664
  return undefined;
575
665
  }
576
- const session = this._stream.session;
577
- return session === undefined ? undefined : session._socket;
666
+ return compatSocket(this._stream);
578
667
  }
579
668
  get connection() {
580
669
  return this.socket;
@@ -550,7 +550,12 @@ class Http2SecureServer extends tls.Server {
550
550
  unknownProtocol(this, socket);
551
551
  }
552
552
  } else {
553
- socket.destroy();
553
+ // node's connectionListener (core.js) only diverts ALPN false and
554
+ // 'http/1.1'; every other negotiated protocol still becomes a session,
555
+ // and the session is announced with the server's 'session' event before
556
+ // the application's own 'secureConnection' listener runs. Destroying
557
+ // here used to drop that event for an ALPNCallback that picks 'a'.
558
+ attachSession(this, socket);
554
559
  }
555
560
  });
556
561
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.19.7",
3
+ "version": "0.19.8",
4
4
  "description": "Pure-JavaScript HTTP/2 for Node.js — a drop-in node:http2 replacement (server and client, h2c and TLS) with zero dependencies",
5
5
  "main": "index.js",
6
6
  "files": [