h2lean 0.11.3 → 0.17.2

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/lib/stream.js CHANGED
@@ -249,7 +249,7 @@ function autoCloseNT(stream) {
249
249
  return;
250
250
  }
251
251
  stream._tick(emitEndNT, stream);
252
- stream._destroy(NO_ERROR, undefined, true);
252
+ stream._destroy(NO_ERROR, true);
253
253
  }
254
254
 
255
255
  // node's onFileUnpipe closes an owned descriptor without caring about the error; the
@@ -398,6 +398,7 @@ class Http2Stream extends Stream {
398
398
  // lib/fast-tick.js; every deferred callback of this stream goes through it.
399
399
  this._tick = session._tick;
400
400
  this.rstCode = 0;
401
+ this._closedFlag = false;
401
402
  // What respond() put on the wire, for the `sentHeaders` getter below: the caller's map
402
403
  // plus the two values the encoder resolved. node keeps the same three pieces
403
404
  // (kRawHeaders / kSentHeaders, internal/http2/core.js:2225).
@@ -502,7 +503,10 @@ class Http2Stream extends Stream {
502
503
  return this._destroyed;
503
504
  }
504
505
  get closed() {
505
- return this._state.state === ST_CLOSED;
506
+ // node's STREAM_FLAGS_CLOSED (core.js:2458) -- set by close() at entry, so the
507
+ // second-call guard holds even while the teardown waits on 'finish'; the state
508
+ // check keeps the machinery paths (peer RST, destroy) answering as they did.
509
+ return this._closedFlag === true || this._state.state === ST_CLOSED;
506
510
  }
507
511
  // node's public state getter answers {} once the stream is destroyed
508
512
  // (internal/http2/core.js:2304, `if (this.destroyed || id === undefined)`);
@@ -1307,17 +1311,48 @@ class Http2Stream extends Stream {
1307
1311
  this._endLocalSide();
1308
1312
  }
1309
1313
 
1310
- close(code = NO_ERROR, reason) {
1311
- this._destroy(code, reason, true);
1314
+ close(code = NO_ERROR, callback) {
1315
+ // node: Http2Stream.close (internal/http2/core.js:2474) -- validateInteger 0..2^32-1,
1316
+ // validateFunction, a second call on a closed stream is a silent no-op, and the
1317
+ // callback rides 'close' with no arguments (measured: cb(0)). The old (code, reason)
1318
+ // shape was h2lean's invention: RST_STREAM carries no reason text (RFC 9113 6.4) and
1319
+ // node's second argument is this callback.
1320
+ validateRstCode(code, callback);
1321
+ if (this.closed) {
1322
+ return;
1323
+ }
1324
+ if (callback !== undefined) {
1325
+ this.once('close', callback);
1326
+ }
1327
+ // closeStream (core.js:2071-2101) is the base method both sides share. It sets
1328
+ // STREAM_FLAGS_CLOSED and rstCode at ENTRY, before the abort and the RST decision
1329
+ // -- a second close() is a no-op even while the teardown still waits on 'finish'.
1330
+ // h2lean sets the flag after the end() below instead: this end() is the one place
1331
+ // the auto-respond runs, and respond() refuses a closed stream -- setting the flag
1332
+ // first would throw out of close() on the unresponded shape node's plain end()
1333
+ // never reaches (AGENTS.md section 10).
1334
+ const ending = this._ending;
1335
+ if (!ending) {
1336
+ this.aborted = true;
1337
+ this.emit('aborted');
1338
+ this.end();
1339
+ }
1340
+ this._closedFlag = true;
1341
+ this.rstCode = code;
1342
+ if (ending && this._writableFinished !== true && code === NO_ERROR) {
1343
+ this.once('finish', () => this._destroy(code, true));
1344
+ } else {
1345
+ this._destroy(code, true);
1346
+ }
1312
1347
  }
1313
1348
 
1314
- destroy(codeOrError, reason) {
1349
+ destroy(codeOrError) {
1315
1350
  // node: an Error argument means NGHTTP2_INTERNAL_ERROR on the wire
1316
1351
  // (Http2Stream._destroy, internal/http2/core.js:2518); a number is the RST
1317
1352
  // code itself, and no argument at all is a graceful NO_ERROR close.
1318
1353
  const code = typeof codeOrError === 'number' ? codeOrError
1319
1354
  : (codeOrError != null ? INTERNAL_ERROR : NO_ERROR);
1320
- this._destroy(code, reason, true, typeof codeOrError === 'object' ? codeOrError : null);
1355
+ this._destroy(code, true, typeof codeOrError === 'object' ? codeOrError : null);
1321
1356
  }
1322
1357
 
1323
1358
  // An informational (1xx) HEADERS block, sent before the final response.
@@ -1722,7 +1757,7 @@ class Http2Stream extends Stream {
1722
1757
  const c = pending[i];
1723
1758
  const buf = c.length === undefined ? c.data : c;
1724
1759
  const take = buf.length <= n - off ? buf.length : n - off;
1725
- buf.copy(out, off, 0, take);
1760
+ frames.copyInto(out, off, buf, 0, take);
1726
1761
  off += take;
1727
1762
  if (c.length !== undefined) {
1728
1763
  wire += take;
@@ -2595,7 +2630,7 @@ class Http2Stream extends Stream {
2595
2630
  // inside the read batch.
2596
2631
  }
2597
2632
 
2598
- _destroy(code, reason, sendRst, err) {
2633
+ _destroy(code, sendRst, err) {
2599
2634
  if (this._destroyed) {
2600
2635
  return;
2601
2636
  }
@@ -2634,7 +2669,7 @@ class Http2Stream extends Stream {
2634
2669
  this._state.localClose = 1;
2635
2670
  this._state.state = ST_CLOSED;
2636
2671
  if (sendRst && this.session._open) {
2637
- this.session.sendRst(this.id, code, reason);
2672
+ this.session.sendRst(this.id, code);
2638
2673
  }
2639
2674
  this._pending = null;
2640
2675
  this.session._dropStream(this, true);
@@ -2887,6 +2922,104 @@ function errorOf(code, msg) {
2887
2922
  return err;
2888
2923
  }
2889
2924
 
2925
+ // node's Http2Stream.close validates its code with validateInteger(code, 'code', 0,
2926
+ // kMaxInt) and the callback with validateFunction (internal/http2/core.js:2474,
2927
+ // kMaxInt = 2^32 - 1 at core.js:239). The messages are internal/validators', read
2928
+ // out of it in the same session: a non-number is ERR_INVALID_ARG_TYPE, a
2929
+ // non-integer or an out-of-range integer is ERR_OUT_OF_RANGE.
2930
+ // The Received-tail is determineSpecificType (internal/errors.js), ported from the
2931
+ // bundled source and measured through Http2Stream.close on a live v26.12.1 client:
2932
+ // strings quote with '' and flip to JSON.stringify when the value itself carries a
2933
+ // quote, both truncated at 28 octets to 25 plus '...'; -0/NaN/Infinity are named;
2934
+ // an object reports `an instance of ${constructor.name}` only when that name is a
2935
+ // non-empty string, else the inspect fallback at depth -1; `function ${name}` keeps
2936
+ // the trailing space for an anonymous function; the generic tail inspects, it does
2937
+ // not String().
2938
+ function specType(v) {
2939
+ if (v === null) {
2940
+ return 'null';
2941
+ }
2942
+ if (v === undefined) {
2943
+ return 'undefined';
2944
+ }
2945
+ const t = typeof v;
2946
+ if (t === 'string') {
2947
+ if (v.length > 28) {
2948
+ v = `${v.slice(0, 25)}...`;
2949
+ }
2950
+ return v.indexOf("'") === -1 ? `type string ('${v}')`
2951
+ : `type string (${JSON.stringify(v)})`;
2952
+ }
2953
+ if (t === 'number') {
2954
+ if (v === 0) {
2955
+ return 1 / v === -Infinity ? 'type number (-0)' : 'type number (0)';
2956
+ }
2957
+ if (v !== v) {
2958
+ return 'type number (NaN)';
2959
+ }
2960
+ if (v === Infinity) {
2961
+ return 'type number (Infinity)';
2962
+ }
2963
+ if (v === -Infinity) {
2964
+ return 'type number (-Infinity)';
2965
+ }
2966
+ return `type number (${v})`;
2967
+ }
2968
+ if (t === 'boolean') {
2969
+ return v ? 'type boolean (true)' : 'type boolean (false)';
2970
+ }
2971
+ if (t === 'bigint') {
2972
+ return `type bigint (${v}n)`;
2973
+ }
2974
+ if (t === 'symbol') {
2975
+ return `type symbol (${String(v)})`;
2976
+ }
2977
+ if (t === 'function') {
2978
+ return `function ${v.name}`;
2979
+ }
2980
+ if (t === 'object') {
2981
+ // `constructor` may be user-controlled: node reads it once and only trusts a
2982
+ // non-empty string name (internal/errors.js).
2983
+ const name = v.constructor?.name;
2984
+ if (typeof name === 'string' && name !== '') {
2985
+ return `an instance of ${name}`;
2986
+ }
2987
+ return inspect(v, { depth: -1 });
2988
+ }
2989
+ let shown = inspect(v, { colors: false });
2990
+ if (shown.length > 28) {
2991
+ shown = `${shown.slice(0, 25)}...`;
2992
+ }
2993
+ return `type ${t} (${shown})`;
2994
+ }
2995
+
2996
+ // node's Http2Stream.close (core.js:2474) validates its code through
2997
+ // validateInteger(code, 'code', 0, kMaxInt) -- kMaxInt is 2^32-1 (core.js:239) -- and
2998
+ // its callback through validateFunction; the three verdicts and their exact texts are
2999
+ // measured on a live v26.12.1 client: a non-number code is ERR_INVALID_ARG_TYPE
3000
+ // ("must be of type number"), a non-integer number is ERR_OUT_OF_RANGE ("It must be an
3001
+ // integer"), and a code outside [0, 2^32-1] is ERR_OUT_OF_RANGE ("It must be >= 0 &&
3002
+ // <= 4294967295"). The callback's own message names "callback" and follows the same
3003
+ // determineSpecificType shapes (measured: string, null, number).
3004
+ function validateRstCode(code, callback) {
3005
+ if (typeof code !== 'number') {
3006
+ throw errorOf('ERR_INVALID_ARG_TYPE',
3007
+ `The "code" argument must be of type number. Received ${specType(code)}`);
3008
+ }
3009
+ if (!Number.isInteger(code)) {
3010
+ throw errorOf('ERR_OUT_OF_RANGE',
3011
+ `The value of "code" is out of range. It must be an integer. Received ${code}`);
3012
+ }
3013
+ if (code < 0 || code > 4294967295) {
3014
+ throw errorOf('ERR_OUT_OF_RANGE',
3015
+ `The value of "code" is out of range. It must be >= 0 && <= 4294967295. Received ${code}`);
3016
+ }
3017
+ if (callback !== undefined && typeof callback !== 'function') {
3018
+ throw errorOf('ERR_INVALID_ARG_TYPE',
3019
+ `The "callback" argument must be of type function. Received ${specType(callback)}`);
3020
+ }
3021
+ }
3022
+
2890
3023
  // --- the async-iteration machinery ----------------------------------------------
2891
3024
  // Everything below reproduces node's internal/streams/readable.js:1552-1805
2892
3025
  // (streamToAsyncIterator/createAsyncIterator) and internal/streams/operators,
@@ -3002,22 +3135,26 @@ function createStreamIterator(stream, options) {
3002
3135
  function start() {
3003
3136
  started = true;
3004
3137
  stream.on('readable', fire);
3005
- let settled = false;
3138
+ // node's eos(stream, { writable: false }, cb), adapted: the verdict
3139
+ // handlers record the FIRST verdict and wake the pump (node's once-wrapped
3140
+ // callback suppresses the rest -- an 'end' followed by a destroy(err) is
3141
+ // swallowed on node, measured off a live client), and the LISTENERS stay
3142
+ // attached until finalize's detach branch -- the part that matters here:
3143
+ // node's eos keeps its 'error' handler attached through the teardown, so a
3144
+ // destroy that races a natural end does not turn into an unhandled
3145
+ // 'error'. A close with neither end nor error is node's eos verdict
3146
+ // ERR_STREAM_PREMATURE_CLOSE ('Premature close', read out of
3147
+ // internal/errors with --expose-internals).
3006
3148
  const settle = (err) => {
3007
- if (settled) {
3149
+ if (error !== undefined) {
3008
3150
  return;
3009
3151
  }
3010
- settled = true;
3011
- detach();
3012
3152
  error = err;
3013
3153
  fire();
3014
3154
  };
3015
3155
  const onEnd = () => {
3016
3156
  settle(null);
3017
3157
  };
3018
- // a close with neither end nor error is node's eos verdict
3019
- // ERR_STREAM_PREMATURE_CLOSE ('Premature close', read out of
3020
- // internal/errors with --expose-internals).
3021
3158
  const onClose = () => {
3022
3159
  settle(errorOf('ERR_STREAM_PREMATURE_CLOSE', 'Premature close'));
3023
3160
  };
@@ -3030,13 +3167,11 @@ function createStreamIterator(stream, options) {
3030
3167
  stream.removeListener('end', onEnd);
3031
3168
  stream.removeListener('close', onClose);
3032
3169
  };
3033
- // node's start() hooks eos, not raw 'end'/'close', and an eos on a
3034
- // stream that has ALREADY settled calls back on the next tick: a second
3035
- // iterator over an ended stream gets `{done:true}` (measured on live
3036
- // node: every() after some() on the same stream answers true without
3037
- // hanging), and over a destroyed one gets the premature-close error.
3038
- // The one-shot trio below can never fire again, so this is where that
3039
- // verdict is reproduced.
3170
+ // node's eos settles immediately (on the next tick) when the stream has
3171
+ // ALREADY reached its verdict at start(): a second iterator over an ended
3172
+ // stream gets `{done:true}` (measured on live node: every() after some()
3173
+ // on the same stream answers true without hanging), and over a destroyed
3174
+ // one gets the premature-close error.
3040
3175
  if (stream.readableEnded === true) {
3041
3176
  stream._tick(() => settle(null));
3042
3177
  } else if (stream.destroyed === true) {
@@ -3058,7 +3193,7 @@ function createStreamIterator(stream, options) {
3058
3193
  // error:ABORT_ERR, close; natural end -- destroyed stays false.
3059
3194
  if ((error || options?.destroyOnReturn !== false) && error === undefined) {
3060
3195
  if (!stream.destroyed) {
3061
- stream._destroy(frames.CANCEL, undefined, true, abortError());
3196
+ stream._destroy(frames.CANCEL, true, abortError());
3062
3197
  }
3063
3198
  } else {
3064
3199
  stream.removeListener('readable', fire);
@@ -3522,4 +3657,4 @@ function streamErrorOf(code) {
3522
3657
  `Stream closed with error code ${frames.ERROR_NAMES[code] || code}`);
3523
3658
  }
3524
3659
 
3525
- module.exports = { Http2Stream, errorOf, streamErrorOf, emitNT, emitCloseNT, emitDrainNT };
3660
+ module.exports = { Http2Stream, errorOf, streamErrorOf, validateRstCode, emitNT, emitCloseNT, emitDrainNT };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.11.3",
3
+ "version": "0.17.2",
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": [