h2lean 0.19.6 → 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 };
@@ -61,13 +61,15 @@
61
61
  // (measured, tools/probe-client-status.js; this closes the empty-block
62
62
  // divergence).
63
63
  //
64
- // KNOWN DIVERGENCE: a stream leaves _streams when its response ends (_dropStream on
65
- // END_STREAM), while node keeps it in state.streams until the Duplex is destroyed. A
66
- // RST_STREAM that arrives after END_STREAM therefore finds nothing here and is ignored,
67
- // so a request whose body was never ended -- or whose response body is still buffered
68
- // unread -- never closes: node reports aborted/finish/close, h2lean waits forever.
69
- // _wireDone already tracks these streams for the graceful close; the reset path does not
70
- // consult it. Pinned as the excluded shape in test/client/client-rst.test.js.
64
+ // A stream leaves _streams only when its Duplex closes (_trackClose), the way node
65
+ // removes it from state.streams only in the stream's destroy path (core.js:2531). END_STREAM
66
+ // ends the wire half but not the stream: a request whose writable was never ended, or
67
+ // whose response body is still buffered unread, stays reachable so a later RST_STREAM
68
+ // still reaches onStreamClose (core.js:607) -- aborted/finish/close with rstCode 0 on a
69
+ // NO_ERROR reset, or a deferred close once the buffered body is consumed. _dropStream's
70
+ // `keep` argument is that half-closed state; the bookkeeping it still does is what the
71
+ // graceful close needs. Measured with tools/probe-client-unfinished-post.js and pinned in
72
+ // test/client/client-rst.test.js.
71
73
  //
72
74
  // settings() and the initial submission take customSettings, node-shaped and measured on
73
75
  // a live node 26 client against a raw frame-decoding peer (test/custom-settings.test.js):
@@ -334,6 +336,12 @@ class Http2ClientSession extends EventEmitter {
334
336
  this._maxReservedRemoteStreams = opts.maxReservedRemoteStreams === undefined
335
337
  ? 200 : opts.maxReservedRemoteStreams;
336
338
  this._streams = new Map();
339
+ // Streams whose wire life is still open. A stream that took END_STREAM stops
340
+ // counting against the peer's max_concurrent_streams as soon as nghttp2 closes
341
+ // its C-side handle, even while its Duplex is finishing on the JS side. The id
342
+ // stays in _streams until 'close' (so a late RST can find it), so concurrency
343
+ // has to use this count rather than the map's size.
344
+ this._liveWire = 0;
337
345
  // Streams whose wire life is over (_dropStream) but whose Duplex has not
338
346
  // emitted 'close' yet -- an unread response body keeps the readable half
339
347
  // open. node's session counts a stream until it is destroyed, not until
@@ -1062,7 +1070,7 @@ class Http2ClientSession extends EventEmitter {
1062
1070
  _enqueue(req) {
1063
1071
  const max = this.remoteSettings.maxConcurrentStreams;
1064
1072
  if (this._outboundQueue.length === 0
1065
- && (max === undefined || this._streams.size < max)) {
1073
+ && (max === undefined || this._liveWire < max)) {
1066
1074
  this._openStream(req);
1067
1075
  } else {
1068
1076
  this._outboundQueue.push(req);
@@ -1078,6 +1086,7 @@ class Http2ClientSession extends EventEmitter {
1078
1086
  // the peer's number and not the 65535 default the peer may never have chosen.
1079
1087
  req.stream._sendWin = this.remoteSettings.initialWindowSize;
1080
1088
  req.stream._opened = true;
1089
+ this._liveWire++;
1081
1090
  this._streams.set(req.stream.id, req.stream);
1082
1091
  this._sendRequest(req.stream, req.list, req.nl, req.end, req.hp);
1083
1092
  }
@@ -1089,7 +1098,7 @@ class Http2ClientSession extends EventEmitter {
1089
1098
  return;
1090
1099
  }
1091
1100
  const max = this.remoteSettings.maxConcurrentStreams;
1092
- while (this._outboundQueue.length > 0 && (max === undefined || this._streams.size < max)) {
1101
+ while (this._outboundQueue.length > 0 && (max === undefined || this._liveWire < max)) {
1093
1102
  const req = this._outboundQueue.shift();
1094
1103
  if (this.closed && req.stream.id > (this._goawayLastStreamID || 0)) {
1095
1104
  // past the peer's GOAWAY mark: this request will not be carried; surface it.
@@ -2504,6 +2513,13 @@ class Http2ClientSession extends EventEmitter {
2504
2513
  }
2505
2514
  return this._fatal(PROTOCOL_ERROR, 'response HEADERS on an unopened stream id');
2506
2515
  }
2516
+ // nghttp2 silently drops a block for a stream both of whose wire halves are
2517
+ // done (client-fatal.test.js: a second HEADERS/ES on a completed id). The id
2518
+ // stays in _streams until the Duplex closes, so the old "no stream" branch no
2519
+ // longer catches this shape; without this guard it would read as trailers.
2520
+ if (stream._rEof && stream._endSent) {
2521
+ return;
2522
+ }
2507
2523
  if (this._refuseHeaders(sid, stream, fields, fcount)) {
2508
2524
  return;
2509
2525
  }
@@ -2534,7 +2550,7 @@ class Http2ClientSession extends EventEmitter {
2534
2550
  }
2535
2551
  stream._onHeaders(status, fields, fcount, sensitive, end, flags);
2536
2552
  if (end) {
2537
- this._dropStream(sid);
2553
+ this._dropStream(sid, true);
2538
2554
  }
2539
2555
  }
2540
2556
 
@@ -2660,7 +2676,7 @@ class Http2ClientSession extends EventEmitter {
2660
2676
  // shared buffer it reused -- that buffer is gone.
2661
2677
  stream._onData(buf.subarray(p, p + l));
2662
2678
  if (flags & F_END_STREAM) {
2663
- stream._endRead(); this._dropStream(sid);
2679
+ stream._endRead(); this._dropStream(sid, true);
2664
2680
  }
2665
2681
  }
2666
2682
 
@@ -2689,7 +2705,7 @@ class Http2ClientSession extends EventEmitter {
2689
2705
  }
2690
2706
  }
2691
2707
  if (flags & F_END_STREAM) {
2692
- stream._endRead(); this._dropStream(sid);
2708
+ stream._endRead(); this._dropStream(sid, true);
2693
2709
  }
2694
2710
  }
2695
2711
 
@@ -2727,9 +2743,17 @@ class Http2ClientSession extends EventEmitter {
2727
2743
 
2728
2744
  _onRst(sid, code) {
2729
2745
  const stream = this._streams.get(sid);
2730
- if (stream === undefined) {
2746
+ // node's onStreamClose guard (core.js:609): a stream already destroyed -- including
2747
+ // one whose END_STREAM and finished writable just auto-destroyed it on a tick that
2748
+ // has not reached its 'close' yet -- is gone at the C layer; its reset is ignored.
2749
+ // So is one whose two wire halves are both done: nghttp2 has closed that stream and
2750
+ // hands no further close callback to JS, which is why the reset after a completed
2751
+ // request keeps rstCode 0 (test/client/client-rst.test.js).
2752
+ if (stream === undefined || stream._destroyFlag ||
2753
+ (stream._rEof && stream._endSent)) {
2731
2754
  return;
2732
2755
  }
2756
+ stream._flushWireEnd();
2733
2757
  this._dropStream(sid);
2734
2758
  stream._onRst(code);
2735
2759
  }
@@ -2739,7 +2763,12 @@ class Http2ClientSession extends EventEmitter {
2739
2763
  // is gone -- including the inbound-RST path, where _onRst has already dropped it,
2740
2764
  // so we never answer a reset with a reset of our own.
2741
2765
  rstStream(sid, code) {
2742
- if (this.destroyed || !this._open || !this._streams.has(sid)) {
2766
+ const stream = this._streams.get(sid);
2767
+ // A stream whose two wire halves are done is closed in nghttp2 even while its
2768
+ // Duplex is finishing: an application destroy then finds no handle to submit a
2769
+ // reset through (client-destroy-code.test.js, the closed-stream cases).
2770
+ if (this.destroyed || !this._open || stream === undefined ||
2771
+ (stream._rEof && stream._endSent)) {
2743
2772
  return;
2744
2773
  }
2745
2774
  const body = Buffer.allocUnsafe(4);
@@ -3303,11 +3332,18 @@ class Http2ClientSession extends EventEmitter {
3303
3332
 
3304
3333
  // The 'close' subscription is installed at construction, before any event can
3305
3334
  // fire, so the wire-done bookkeeping never races the Duplex's own teardown.
3306
- // It is the one subscription per stream the session keeps: the mark it clears
3307
- // is what lets a graceful close outlive an unread response body (see _wireDone).
3335
+ // It is the one subscription per stream the session keeps: the marks it clears are
3336
+ // what let a graceful close outlive an unread response body (see _wireDone) and what
3337
+ // finally retire a half-closed stream from the id map (see _dropStream's `keep`).
3308
3338
  _trackClose(stream) {
3309
3339
  stream.on('close', () => {
3310
3340
  this._wireDone.delete(stream);
3341
+ if (this._streams.get(stream.id) === stream) {
3342
+ this._streams.delete(stream.id);
3343
+ // A queued request was waiting for this stream's concurrency slot. The old
3344
+ // _dropStream did the draining at END_STREAM; now the id is retired here.
3345
+ this._drainQueue();
3346
+ }
3311
3347
  if (this.destroyed && !this._closeEmitted && !this._destroyScheduled
3312
3348
  && this._wireDone.size === 0) {
3313
3349
  // The socket is already gone (_onSocketClose deferred to this moment):
@@ -3344,16 +3380,28 @@ class Http2ClientSession extends EventEmitter {
3344
3380
  }
3345
3381
  }
3346
3382
 
3347
- _dropStream(sid) {
3383
+ // `keep` is the inbound END_STREAM shape: the remote half is done but the Duplex may
3384
+ // still owe its own 'finish'/'close', and a RST_STREAM can arrive in that window. node
3385
+ // keeps the id in state.streams until the stream's destroy (core.js:2531), so the id
3386
+ // stays here too; _trackClose retires it. Every other caller has finished the stream's
3387
+ // wire life for good and drops the id at once.
3388
+ _dropStream(sid, keep) {
3348
3389
  const stream = this._streams.get(sid);
3349
3390
  if (stream !== undefined && stream._debtMarked) {
3350
3391
  this._removeDebt(stream);
3351
3392
  }
3352
- this._streams.delete(sid);
3393
+ if (!keep) {
3394
+ this._streams.delete(sid);
3395
+ }
3353
3396
  if (stream !== undefined) {
3354
- // The wire life is over; the Duplex may still owe 'end'/'close' (an unread
3355
- // body). The session's graceful close waits for that 'close' (_wireDone).
3356
- this._wireDone.add(stream);
3397
+ if (!this._wireDone.has(stream)) {
3398
+ // Counted once: a second _dropStream for the same half-closed stream must
3399
+ // not free the concurrency slot twice.
3400
+ this._liveWire--;
3401
+ // The wire life is over; the Duplex may still owe 'end'/'close' (an unread
3402
+ // body). The session's graceful close waits for that 'close' (_wireDone).
3403
+ this._wireDone.add(stream);
3404
+ }
3357
3405
  }
3358
3406
  this._drainQueue();
3359
3407
  this._maybeClose();
@@ -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).
@@ -924,7 +925,7 @@ class Http2ClientStream extends Http2Stream {
924
925
  // iterator's settle(null) outruns the destroy's 'error' pair and a
925
926
  // helper started mid-teardown answers a clean done (the some/every
926
927
  // shape, measured on node v26.12.1: every() rejects ABORT_ERR).
927
- if (this._errored !== undefined) {
928
+ if (this._errored !== undefined || this._endDelivered === true) {
928
929
  return;
929
930
  }
930
931
  this._endDelivered = true;
@@ -933,6 +934,19 @@ class Http2ClientStream extends Http2Stream {
933
934
  });
934
935
  }
935
936
 
937
+ // The wire's END_STREAM is delivered inside node's socket read (onStreamClose calls
938
+ // stream.read(0) there), so a RST_STREAM in the same read batch is reported after 'end'.
939
+ // h2lean queues that 'end' on a tick; flush it first when the reset arrives, or the
940
+ // reset's 'aborted' would invert the pair. The scheduled tick then sees _endDelivered
941
+ // and delivers nothing twice.
942
+ _flushWireEnd() {
943
+ if (this._endEmitted === true && this._endDelivered !== true) {
944
+ this._endDelivered = true;
945
+ this.emit('end');
946
+ this._maybeAutoClose();
947
+ }
948
+ }
949
+
936
950
  _endRead() {
937
951
  if (this._remoteEnded) {
938
952
  return;
@@ -1644,6 +1658,16 @@ class Http2ClientStream extends Http2Stream {
1644
1658
  return out;
1645
1659
  }
1646
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
+
1647
1671
  get writableNeedDrain() {
1648
1672
  return this._needDrain === true && this._destroyFlag !== true &&
1649
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.6",
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": [