@blockrun/clawrouter 0.12.239 → 0.12.240

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.
Files changed (3) hide show
  1. package/dist/cli.js +423 -99
  2. package/dist/index.js +423 -99
  3. package/package.json +12 -9
package/dist/index.js CHANGED
@@ -56858,6 +56858,7 @@ var require_symbols = __commonJS({
56858
56858
  kCounter: /* @__PURE__ */ Symbol("socket request counter"),
56859
56859
  kMaxResponseSize: /* @__PURE__ */ Symbol("max response size"),
56860
56860
  kHTTP2Session: /* @__PURE__ */ Symbol("http2Session"),
56861
+ kHTTP2Options: /* @__PURE__ */ Symbol("http2 options"),
56861
56862
  kHTTP2SessionState: /* @__PURE__ */ Symbol("http2Session state"),
56862
56863
  kRetryHandlerDefaultRetry: /* @__PURE__ */ Symbol("retry agent default retry"),
56863
56864
  kConstruct: /* @__PURE__ */ Symbol("constructable"),
@@ -59626,14 +59627,26 @@ var require_connect = __commonJS({
59626
59627
  } else {
59627
59628
  assert10(!httpSocket, "httpSocket can only be sent on TLS update");
59628
59629
  port = port || 80;
59629
- socket = net3.connect({
59630
+ const connectOptions = {
59630
59631
  highWaterMark: 64 * 1024,
59631
59632
  // Same as nodejs fs streams.
59632
59633
  ...options,
59633
59634
  localAddress,
59634
59635
  port,
59635
59636
  host: hostname
59636
- });
59637
+ };
59638
+ const family = net3.isIP(hostname);
59639
+ if (family !== 0 && servername && servername !== hostname) {
59640
+ connectOptions.host = servername;
59641
+ connectOptions.lookup = (_hostname, lookupOptions, cb) => {
59642
+ if (lookupOptions.all) {
59643
+ cb(null, [{ address: hostname, family }]);
59644
+ } else {
59645
+ cb(null, hostname, family);
59646
+ }
59647
+ };
59648
+ }
59649
+ socket = net3.connect(connectOptions);
59637
59650
  if (useH2c === true) {
59638
59651
  socket.alpnProtocol = "h2";
59639
59652
  }
@@ -64073,20 +64086,20 @@ var require_client_h1 = __commonJS({
64073
64086
  }
64074
64087
  function clearIdleSocketValidation(socket) {
64075
64088
  if (socket[kIdleSocketValidationTimeout]) {
64076
- clearImmediate(socket[kIdleSocketValidationTimeout]);
64089
+ clearTimeout(socket[kIdleSocketValidationTimeout]);
64077
64090
  socket[kIdleSocketValidationTimeout] = null;
64078
64091
  }
64079
64092
  socket[kIdleSocketValidation] = 0;
64080
64093
  }
64081
64094
  function scheduleIdleSocketValidation(client, socket) {
64082
64095
  socket[kIdleSocketValidation] = 1;
64083
- socket[kIdleSocketValidationTimeout] = setImmediate(() => {
64096
+ socket[kIdleSocketValidationTimeout] = setTimeout(() => {
64084
64097
  socket[kIdleSocketValidationTimeout] = null;
64085
64098
  socket[kIdleSocketValidation] = 2;
64086
64099
  if (client[kSocket] === socket && !socket.destroyed) {
64087
64100
  client[kResume]();
64088
64101
  }
64089
- });
64102
+ }, 0);
64090
64103
  socket[kIdleSocketValidationTimeout].unref?.();
64091
64104
  }
64092
64105
  function resumeH1(client) {
@@ -64610,10 +64623,7 @@ var require_client_h2 = __commonJS({
64610
64623
  kStrictContentLength,
64611
64624
  kOnError,
64612
64625
  kMaxConcurrentStreams,
64613
- kPingInterval,
64614
64626
  kHTTP2Session,
64615
- kHTTP2InitialWindowSize,
64616
- kHTTP2ConnectionWindowSize,
64617
64627
  kHostAuthority,
64618
64628
  kResume,
64619
64629
  kSize,
@@ -64625,7 +64635,8 @@ var require_client_h2 = __commonJS({
64625
64635
  kEnableConnectProtocol,
64626
64636
  kRemoteSettings,
64627
64637
  kHTTP2Stream,
64628
- kHTTP2SessionState
64638
+ kHTTP2SessionState,
64639
+ kHTTP2Options
64629
64640
  } = require_symbols();
64630
64641
  var { channels } = require_diagnostics();
64631
64642
  var kOpenStreams = /* @__PURE__ */ Symbol("open streams");
@@ -64634,6 +64645,9 @@ var require_client_h2 = __commonJS({
64634
64645
  var kRequestStreamCleanup = /* @__PURE__ */ Symbol("request stream cleanup");
64635
64646
  var kRequestStreamState = /* @__PURE__ */ Symbol("request stream state");
64636
64647
  var kReceivedGoAway = /* @__PURE__ */ Symbol("received goaway");
64648
+ var kGoAwayReplayAttempts = /* @__PURE__ */ Symbol("goaway replay attempts");
64649
+ var kRefusedStreamRetry = /* @__PURE__ */ Symbol("refused stream retry");
64650
+ var MAX_GOAWAY_REPLAY_ATTEMPTS = 1;
64637
64651
  var extractBody;
64638
64652
  var http23;
64639
64653
  try {
@@ -64731,10 +64745,15 @@ var require_client_h2 = __commonJS({
64731
64745
  client[kPendingIdx] = client[kRunningIdx];
64732
64746
  }
64733
64747
  }
64734
- function canRetryRequestAfterGoAway(request2) {
64748
+ function canReplayRequest(request2) {
64735
64749
  const { body } = request2;
64736
64750
  return body == null || util5.isBuffer(body) || util5.isBlobLike(body);
64737
64751
  }
64752
+ function registerGoAwayRefusal(request2) {
64753
+ const attempts = (request2[kGoAwayReplayAttempts] ?? 0) + 1;
64754
+ request2[kGoAwayReplayAttempts] = attempts;
64755
+ return attempts <= MAX_GOAWAY_REPLAY_ATTEMPTS;
64756
+ }
64738
64757
  function closeStream(stream4, code = NGHTTP2_REFUSED_STREAM) {
64739
64758
  if (stream4 != null && !stream4.destroyed && !stream4.closed) {
64740
64759
  try {
@@ -64746,15 +64765,30 @@ var require_client_h2 = __commonJS({
64746
64765
  function detachRequestStreamForClose(request2) {
64747
64766
  const stream4 = request2[kRequestStream];
64748
64767
  clearRequestStream(request2);
64768
+ severRequestStream(stream4);
64749
64769
  return stream4;
64750
64770
  }
64771
+ function severRequestStream(stream4) {
64772
+ if (stream4 == null || stream4[kRequestStreamState] == null) {
64773
+ return;
64774
+ }
64775
+ stream4[kRequestStreamState] = null;
64776
+ stream4.off("close", completeRequestStream);
64777
+ stream4.off("close", onUpgradeStreamClose);
64778
+ if (stream4[kHTTP2Session] != null) {
64779
+ closeStreamSession(stream4);
64780
+ }
64781
+ if (!stream4.destroyed && !stream4.closed) {
64782
+ stream4.once("error", noop2);
64783
+ }
64784
+ }
64751
64785
  function connectH2(client, socket) {
64752
64786
  client[kSocket] = socket;
64753
- const http2InitialWindowSize = client[kHTTP2InitialWindowSize];
64754
- const http2ConnectionWindowSize = client[kHTTP2ConnectionWindowSize];
64787
+ const http2InitialWindowSize = client[kHTTP2Options].sessionOptions?.initialWindowSize;
64788
+ const http2ConnectionWindowSize = client[kHTTP2Options].connectionWindowSize;
64755
64789
  const session = http23.connect(client[kUrl], {
64756
64790
  createConnection: () => socket,
64757
- peerMaxConcurrentStreams: client[kMaxConcurrentStreams],
64791
+ peerMaxConcurrentStreams: client[kHTTP2Options].maxConcurrentStreams,
64758
64792
  settings: {
64759
64793
  // TODO(metcoder95): add support for PUSH
64760
64794
  enablePush: false,
@@ -64767,13 +64801,16 @@ var require_client_h2 = __commonJS({
64767
64801
  session[kSocket] = socket;
64768
64802
  session[kHTTP2SessionState] = {
64769
64803
  idleTimeout: null,
64804
+ // Armed while the peer advertises MAX_CONCURRENT_STREAMS = 0 and we have
64805
+ // work that cannot start. See setNoStreamsTimeout.
64806
+ noStreamsTimeout: null,
64770
64807
  // Sockets start out ref'd. Session ref/unref proxies to the socket, so a
64771
64808
  // single cached flag lets us skip redundant uv ref/unref calls, provided
64772
64809
  // every ref/unref of the session or its socket goes through
64773
64810
  // refH2Session/unrefH2Session.
64774
64811
  refed: true,
64775
64812
  ping: {
64776
- interval: client[kPingInterval] === 0 ? null : setInterval(onHttp2SendPing, client[kPingInterval], session).unref()
64813
+ interval: client[kHTTP2Options].pingInterval === 0 ? null : setInterval(onHttp2SendPing, client[kHTTP2Options].pingInterval, session).unref()
64777
64814
  }
64778
64815
  };
64779
64816
  session[kReceivedGoAway] = false;
@@ -64878,8 +64915,48 @@ var require_client_h2 = __commonJS({
64878
64915
  } else {
64879
64916
  clearHttp2IdleTimeout(session);
64880
64917
  }
64918
+ if (client[kMaxConcurrentStreams] === 0 && client[kRunning] === 0 && client[kPending] > 0) {
64919
+ setNoStreamsTimeout(session);
64920
+ } else {
64921
+ clearNoStreamsTimeout(session);
64922
+ }
64881
64923
  }
64882
64924
  }
64925
+ function clearNoStreamsTimeout(session) {
64926
+ const state = session[kHTTP2SessionState];
64927
+ if (state?.noStreamsTimeout != null) {
64928
+ clearTimeout(state.noStreamsTimeout);
64929
+ state.noStreamsTimeout = null;
64930
+ }
64931
+ }
64932
+ function setNoStreamsTimeout(session) {
64933
+ const client = session[kClient];
64934
+ const state = session[kHTTP2SessionState];
64935
+ const timeout = client[kHeadersTimeout];
64936
+ if (!timeout || state.noStreamsTimeout != null) {
64937
+ return;
64938
+ }
64939
+ state.noStreamsTimeout = setTimeout(onNoStreamsTimeout, timeout, session).unref();
64940
+ }
64941
+ function onNoStreamsTimeout(session) {
64942
+ const client = session[kClient];
64943
+ const state = session[kHTTP2SessionState];
64944
+ state.noStreamsTimeout = null;
64945
+ if (client[kHTTP2Session] !== session || client[kMaxConcurrentStreams] !== 0 || client[kRunning] !== 0 || client[kPending] === 0) {
64946
+ return;
64947
+ }
64948
+ const err = new HeadersTimeoutError(
64949
+ `HTTP/2: server did not accept a new stream within ${client[kHeadersTimeout]}`
64950
+ );
64951
+ const requests = client[kQueue].splice(client[kPendingIdx]);
64952
+ for (let i = 0; i < requests.length; i++) {
64953
+ if (requests[i] != null) {
64954
+ util5.errorRequest(client, requests[i], err);
64955
+ }
64956
+ }
64957
+ session[kError] = err;
64958
+ resetHttp2Session(session, err);
64959
+ }
64883
64960
  function clearHttp2IdleTimeout(session) {
64884
64961
  const state = session[kHTTP2SessionState];
64885
64962
  if (state?.idleTimeout != null) {
@@ -64986,7 +65063,7 @@ var require_client_h2 = __commonJS({
64986
65063
  const request2 = client[kQueue][i];
64987
65064
  if (request2 != null) {
64988
65065
  streamsToClose.push(detachRequestStreamForClose(request2));
64989
- if (canRetryRequestAfterGoAway(request2)) {
65066
+ if (canReplayRequest(request2) && registerGoAwayRefusal(request2)) {
64990
65067
  retriableRequests.push(request2);
64991
65068
  } else {
64992
65069
  util5.errorRequest(client, request2, err);
@@ -65007,6 +65084,7 @@ var require_client_h2 = __commonJS({
65007
65084
  client[kHTTP2Session] = null;
65008
65085
  }
65009
65086
  clearHttp2IdleTimeout(this);
65087
+ clearNoStreamsTimeout(this);
65010
65088
  if (!this.closed && !this.destroyed) {
65011
65089
  this.close();
65012
65090
  }
@@ -65023,6 +65101,7 @@ var require_client_h2 = __commonJS({
65023
65101
  client[kHTTP2Session] = null;
65024
65102
  }
65025
65103
  clearHttp2IdleTimeout(this);
65104
+ clearNoStreamsTimeout(this);
65026
65105
  if (state.ping.interval != null) {
65027
65106
  clearInterval(state.ping.interval);
65028
65107
  state.ping.interval = null;
@@ -65098,6 +65177,12 @@ var require_client_h2 = __commonJS({
65098
65177
  releaseRequestStream(this);
65099
65178
  if (state.pendingEnd && !state.request.aborted && !state.request.completed) {
65100
65179
  state.request.onResponseEnd(state.trailers || {});
65180
+ } else if (!state.request.aborted && !state.request.completed) {
65181
+ util5.errorRequest(
65182
+ state.client,
65183
+ state.request,
65184
+ new InformationalError("HTTP/2: stream closed before the response was complete")
65185
+ );
65101
65186
  }
65102
65187
  finalizeRequest(state);
65103
65188
  closeStreamSession(this);
@@ -65497,6 +65582,20 @@ var require_client_h2 = __commonJS({
65497
65582
  state.abort(new InformationalError("HTTP/2: stream half-closed (remote)"), true);
65498
65583
  }
65499
65584
  }
65585
+ function retryRefusedStream(stream4, state) {
65586
+ const { client, request: request2 } = state;
65587
+ if (state.responseReceived || request2.aborted || request2.completed || request2[kRefusedStreamRetry] || !canReplayRequest(request2)) {
65588
+ return false;
65589
+ }
65590
+ request2[kRefusedStreamRetry] = true;
65591
+ detachRequestStreamForClose(request2);
65592
+ state.stream = null;
65593
+ state.requestFinalized = true;
65594
+ completeRequest(client, request2);
65595
+ client[kQueue].splice(client[kPendingIdx], 0, request2);
65596
+ client[kResume]();
65597
+ return true;
65598
+ }
65500
65599
  function onError(err) {
65501
65600
  const stream4 = this;
65502
65601
  const state = stream4[kRequestStreamState];
@@ -65504,6 +65603,12 @@ var require_client_h2 = __commonJS({
65504
65603
  return;
65505
65604
  }
65506
65605
  stream4.off("error", onError);
65606
+ if (typeof stream4.rstCode === "number" && stream4.rstCode !== NGHTTP2_NO_ERROR) {
65607
+ err.http2ErrorCode = stream4.rstCode;
65608
+ }
65609
+ if (stream4.rstCode === NGHTTP2_REFUSED_STREAM && retryRefusedStream(stream4, state)) {
65610
+ return;
65611
+ }
65507
65612
  state.abort(err);
65508
65613
  }
65509
65614
  function onFrameError(type, code) {
@@ -65785,10 +65890,8 @@ var require_client = __commonJS({
65785
65890
  kHTTPContext,
65786
65891
  kMaxConcurrentStreams,
65787
65892
  kHostAuthority,
65788
- kHTTP2InitialWindowSize,
65789
- kHTTP2ConnectionWindowSize,
65790
65893
  kResume,
65791
- kPingInterval
65894
+ kHTTP2Options
65792
65895
  } = require_symbols();
65793
65896
  var connectH1 = require_client_h1();
65794
65897
  var connectH2 = require_client_h2();
@@ -65801,6 +65904,14 @@ var require_client = __commonJS({
65801
65904
  function getPipelining(client) {
65802
65905
  return client[kPipelining] ?? client[kHTTPContext]?.defaultPipelining ?? 1;
65803
65906
  }
65907
+ var h2NamespaceOptsWarning = false;
65908
+ function emitH2OptionsNamespaceWarning(optName) {
65909
+ if (h2NamespaceOptsWarning === true) return;
65910
+ process.emitWarning(`Use h2Options.${optName} instead. ${optName} for H2 will be deprecated in future major.`, {
65911
+ code: "UNDICI-H2-OPTIONS"
65912
+ });
65913
+ h2NamespaceOptsWarning = true;
65914
+ }
65804
65915
  function getMaxConcurrent(client) {
65805
65916
  if (client[kHTTPContext]?.version === "h2") {
65806
65917
  return client[kMaxConcurrentStreams];
@@ -65844,7 +65955,8 @@ var require_client = __commonJS({
65844
65955
  initialWindowSize,
65845
65956
  connectionWindowSize,
65846
65957
  pingInterval,
65847
- webSocket
65958
+ webSocket,
65959
+ h2Options
65848
65960
  } = {}) {
65849
65961
  if (keepAlive !== void 0) {
65850
65962
  throw new InvalidArgumentError("unsupported keepAlive, use pipelining=0 instead");
@@ -65907,20 +66019,45 @@ var require_client = __commonJS({
65907
66019
  if (allowH2 != null && typeof allowH2 !== "boolean") {
65908
66020
  throw new InvalidArgumentError("allowH2 must be a valid boolean value");
65909
66021
  }
65910
- if (maxConcurrentStreams != null && (typeof maxConcurrentStreams !== "number" || maxConcurrentStreams < 1)) {
65911
- throw new InvalidArgumentError("maxConcurrentStreams must be a positive integer, greater than 0");
65912
- }
65913
- if (useH2c != null && typeof useH2c !== "boolean") {
65914
- throw new InvalidArgumentError("useH2c must be a valid boolean value");
65915
- }
65916
- if (initialWindowSize != null && (!Number.isInteger(initialWindowSize) || initialWindowSize < 1)) {
65917
- throw new InvalidArgumentError("initialWindowSize must be a positive integer, greater than 0");
65918
- }
65919
- if (connectionWindowSize != null && (!Number.isInteger(connectionWindowSize) || connectionWindowSize < 1)) {
65920
- throw new InvalidArgumentError("connectionWindowSize must be a positive integer, greater than 0");
65921
- }
65922
- if (pingInterval != null && (typeof pingInterval !== "number" || !Number.isInteger(pingInterval) || pingInterval < 0)) {
65923
- throw new InvalidArgumentError("pingInterval must be a positive integer, greater or equal to 0");
66022
+ if (allowH2 !== false) {
66023
+ if (h2Options != null) {
66024
+ if (h2Options.useH2c != null && typeof h2Options.useH2c !== "boolean") {
66025
+ throw new InvalidArgumentError("h2Options.useH2c must be a valid boolean value");
66026
+ }
66027
+ if (h2Options.settings?.initialWindowSize != null && (!Number.isInteger(h2Options.settings.initialWindowSize) || h2Options.settings.initialWindowSize < 1)) {
66028
+ throw new InvalidArgumentError("h2Options.settings.initialWindowSize must be a positive integer, greater than 0");
66029
+ }
66030
+ if (h2Options.maxConcurrentStreams != null && (!Number.isInteger(h2Options.connectionWindowSize) || h2Options.maxConcurrentStreams < 1)) {
66031
+ throw new InvalidArgumentError("h2Options.maxConcurrentStreams must be a positive integer, greater than 0");
66032
+ }
66033
+ if (h2Options.connectionWindowSize != null && (!Number.isInteger(h2Options.connectionWindowSize) || h2Options.connectionWindowSize < 1)) {
66034
+ throw new InvalidArgumentError("h2Options.connectionWindowSize must be a positive integer, greater than 0");
66035
+ }
66036
+ if (h2Options.pingInterval != null && (typeof h2Options.pingInterval !== "number" || !Number.isInteger(h2Options.pingInterval) || h2Options.pingInterval < 0)) {
66037
+ throw new InvalidArgumentError("h2Options.pingInterval must be a positive integer, greater or equal to 0");
66038
+ }
66039
+ } else {
66040
+ if (useH2c != null && typeof useH2c !== "boolean") {
66041
+ emitH2OptionsNamespaceWarning("useH2c");
66042
+ throw new InvalidArgumentError("useH2c must be a valid boolean value");
66043
+ }
66044
+ if (maxConcurrentStreams != null && (typeof maxConcurrentStreams !== "number" || maxConcurrentStreams < 1)) {
66045
+ emitH2OptionsNamespaceWarning("maxConcurrentStreams");
66046
+ throw new InvalidArgumentError("maxConcurrentStreams must be a positive integer, greater than 0");
66047
+ }
66048
+ if (initialWindowSize != null && (!Number.isInteger(initialWindowSize) || initialWindowSize < 1)) {
66049
+ emitH2OptionsNamespaceWarning("initialWindowSize");
66050
+ throw new InvalidArgumentError("initialWindowSize must be a positive integer, greater than 0");
66051
+ }
66052
+ if (connectionWindowSize != null && (!Number.isInteger(connectionWindowSize) || connectionWindowSize < 1)) {
66053
+ emitH2OptionsNamespaceWarning("connectionWindowSize");
66054
+ throw new InvalidArgumentError("connectionWindowSize must be a positive integer, greater than 0");
66055
+ }
66056
+ if (pingInterval != null && (typeof pingInterval !== "number" || !Number.isInteger(pingInterval) || pingInterval < 0)) {
66057
+ emitH2OptionsNamespaceWarning("pingInterval");
66058
+ throw new InvalidArgumentError("pingInterval must be a positive integer, greater or equal to 0");
66059
+ }
66060
+ }
65924
66061
  }
65925
66062
  super({ webSocket });
65926
66063
  if (typeof connect4 !== "function") {
@@ -65928,8 +66065,8 @@ var require_client = __commonJS({
65928
66065
  ...tls2,
65929
66066
  maxCachedSessions,
65930
66067
  allowH2,
65931
- useH2c,
65932
66068
  socketPath,
66069
+ useH2c: h2Options?.useH2c ?? useH2c,
65933
66070
  timeout: connectTimeout,
65934
66071
  ...typeof autoSelectFamily === "boolean" ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : void 0,
65935
66072
  ...connect4
@@ -65964,10 +66101,21 @@ var require_client = __commonJS({
65964
66101
  this[kClosedResolve] = null;
65965
66102
  this[kMaxResponseSize] = maxResponseSize > -1 ? maxResponseSize : -1;
65966
66103
  this[kHTTPContext] = null;
65967
- this[kMaxConcurrentStreams] = maxConcurrentStreams != null ? maxConcurrentStreams : 100;
65968
- this[kHTTP2InitialWindowSize] = initialWindowSize != null ? initialWindowSize : 262144;
65969
- this[kHTTP2ConnectionWindowSize] = connectionWindowSize != null ? connectionWindowSize : 524288;
65970
- this[kPingInterval] = pingInterval != null ? pingInterval : 6e4;
66104
+ this[kHTTP2Options] = {
66105
+ pingInterval: h2Options?.pingInterval ?? pingInterval ?? 6e4,
66106
+ connectionWindowSize: h2Options?.connectionWindowSize ?? connectionWindowSize ?? 524288,
66107
+ maxConcurrentStreams: h2Options?.maxConcurrentStreams ?? maxConcurrentStreams ?? 100,
66108
+ // Max peerConcurrentStreams for a Node h2 server
66109
+ sessionOptions: {
66110
+ // HTTP/2 window sizes are set to higher defaults than Node.js core for better performance:
66111
+ // - initialWindowSize: 262144 (256KB) vs Node.js default 65535 (64KB - 1)
66112
+ // Allows more data to be sent before requiring acknowledgment, improving throughput
66113
+ // especially on high-latency networks. This matches common production HTTP/2 servers.
66114
+ // - connectionWindowSize: 524288 (512KB) vs Node.js default (none set)
66115
+ // Provides better flow control for the entire connection across multiple streams.
66116
+ initialWindowSize: h2Options?.initialWindowSize ?? initialWindowSize ?? 262144
66117
+ }
66118
+ };
65971
66119
  this[kQueue] = [];
65972
66120
  this[kRunningIdx] = 0;
65973
66121
  this[kPendingIdx] = 0;
@@ -66252,6 +66400,7 @@ var require_client = __commonJS({
66252
66400
  return;
66253
66401
  }
66254
66402
  if (!client[kHTTPContext]) {
66403
+ client[kServerName] = request2.servername;
66255
66404
  connect3(client);
66256
66405
  return;
66257
66406
  }
@@ -67662,7 +67811,7 @@ var require_socks5_proxy_agent = __commonJS({
67662
67811
  var DispatcherBase = require_dispatcher_base();
67663
67812
  var { InvalidArgumentError } = require_errors();
67664
67813
  var { Socks5Client, STATES } = require_socks5_client();
67665
- var { kDispatch, kClose, kDestroy } = require_symbols();
67814
+ var { kBusy, kConnected, kDispatch, kClose, kDestroy } = require_symbols();
67666
67815
  var Pool = require_pool();
67667
67816
  var buildConnector = require_connect();
67668
67817
  var { debuglog } = __require("util");
@@ -67820,6 +67969,17 @@ var require_socks5_proxy_agent = __commonJS({
67820
67969
  }
67821
67970
  });
67822
67971
  this[kPools].set(originKey, pool);
67972
+ const closePoolIfUnused = () => {
67973
+ if (this[kPools].get(originKey) !== pool || pool[kConnected] > 0 || pool[kBusy]) {
67974
+ return;
67975
+ }
67976
+ this[kPools].delete(originKey);
67977
+ if (!pool.destroyed) {
67978
+ pool.close();
67979
+ }
67980
+ };
67981
+ pool.on("disconnect", closePoolIfUnused);
67982
+ pool.on("connectionError", closePoolIfUnused);
67823
67983
  }
67824
67984
  return pool[kDispatch](opts, handler);
67825
67985
  } catch (err) {
@@ -68204,7 +68364,7 @@ var require_env_http_proxy_agent = __commonJS({
68204
68364
  }
68205
68365
  #getProxyAgentForUrl(url2) {
68206
68366
  let { protocol, host: hostname, port } = url2;
68207
- hostname = hostname.replace(/:\d*$/, "").toLowerCase();
68367
+ hostname = hostname.replace(/:\d*$/, "").replace(/^\[(.+)\]$/, "$1").toLowerCase();
68208
68368
  port = Number.parseInt(port, 10) || DEFAULT_PORTS3[protocol] || 0;
68209
68369
  if (!this.#shouldProxy(hostname, port)) {
68210
68370
  return this[kNoProxyAgent];
@@ -68247,11 +68407,22 @@ var require_env_http_proxy_agent = __commonJS({
68247
68407
  if (!entry) {
68248
68408
  continue;
68249
68409
  }
68250
- const parsed = entry.match(/^(.+):(\d+)$/);
68410
+ let hostname, port;
68411
+ const ipv6WithPort = entry.match(/^\[(.+)\]:(\d+)$/);
68412
+ if (ipv6WithPort) {
68413
+ hostname = ipv6WithPort[1];
68414
+ port = Number.parseInt(ipv6WithPort[2], 10);
68415
+ } else {
68416
+ const unbracketed = entry.replace(/^\[(.+)\]$/, "$1");
68417
+ const colonCount = (unbracketed.match(/:/g) || []).length;
68418
+ const parsed = colonCount === 1 && unbracketed.match(/^(.+):(\d+)$/);
68419
+ hostname = parsed ? parsed[1] : unbracketed;
68420
+ port = parsed ? Number.parseInt(parsed[2], 10) : 0;
68421
+ }
68251
68422
  noProxyEntries.push({
68252
68423
  // strip leading dot or asterisk with dot
68253
- hostname: (parsed ? parsed[1] : entry).replace(/^\*?\./, "").toLowerCase(),
68254
- port: parsed ? Number.parseInt(parsed[2], 10) : 0
68424
+ hostname: hostname.replace(/^\*?\./, "").toLowerCase(),
68425
+ port
68255
68426
  });
68256
68427
  }
68257
68428
  this.#noProxyValue = noProxyValue;
@@ -68473,6 +68644,10 @@ var require_retry_handler = __commonJS({
68473
68644
  setTimeout(() => cb(null), retryTimeout);
68474
68645
  }
68475
68646
  onResponseStart(controller, statusCode, headers, statusMessage) {
68647
+ if (statusCode < 200) {
68648
+ this.handler.onResponseStart?.(this.controllerProxy, statusCode, headers, statusMessage);
68649
+ return;
68650
+ }
68476
68651
  this.error = null;
68477
68652
  this.retryCount += 1;
68478
68653
  this.statusCode = statusCode;
@@ -68516,7 +68691,7 @@ var require_retry_handler = __commonJS({
68516
68691
  if (this.end == null) {
68517
68692
  if (statusCode === 206) {
68518
68693
  const range = parseRangeHeader(headers["content-range"]);
68519
- if (range == null) {
68694
+ if (range == null || range.end == null) {
68520
68695
  this.headersSent = true;
68521
68696
  this.handler.onResponseStart?.(
68522
68697
  this.controllerProxy,
@@ -68536,7 +68711,7 @@ var require_retry_handler = __commonJS({
68536
68711
  this.start = start;
68537
68712
  this.end = end;
68538
68713
  }
68539
- if (this.end == null) {
68714
+ if (this.end == null && this.opts.method !== "HEAD") {
68540
68715
  const contentLength = headers["content-length"];
68541
68716
  this.end = contentLength != null ? Number(contentLength) - 1 : null;
68542
68717
  }
@@ -68737,7 +68912,6 @@ var require_readable = __commonJS({
68737
68912
  var kContentLength = /* @__PURE__ */ Symbol("kContentLength");
68738
68913
  var kUsed = /* @__PURE__ */ Symbol("kUsed");
68739
68914
  var kBytesRead = /* @__PURE__ */ Symbol("kBytesRead");
68740
- var kPreservedBuffer = /* @__PURE__ */ Symbol("kPreservedBuffer");
68741
68915
  var noop2 = () => {
68742
68916
  };
68743
68917
  var BodyReadable = class extends Readable2 {
@@ -68976,21 +69150,6 @@ var require_readable = __commonJS({
68976
69150
  */
68977
69151
  setEncoding(encoding) {
68978
69152
  if (Buffer.isEncoding(encoding)) {
68979
- const state = this._readableState;
68980
- const buffer2 = state.buffer;
68981
- if (buffer2 && state.length > 0) {
68982
- const bufferIndex = state.bufferIndex ?? 0;
68983
- const preserved = [];
68984
- const source = typeof buffer2.slice === "function" ? buffer2.slice(bufferIndex) : buffer2;
68985
- for (const data of source) {
68986
- if (Buffer.isBuffer(data)) {
68987
- preserved.push(data);
68988
- }
68989
- }
68990
- if (preserved.length > 0) {
68991
- this[kPreservedBuffer] = (this[kPreservedBuffer] || []).concat(preserved);
68992
- }
68993
- }
68994
69153
  super.setEncoding(encoding);
68995
69154
  }
68996
69155
  return this;
@@ -69041,13 +69200,7 @@ var require_readable = __commonJS({
69041
69200
  return;
69042
69201
  }
69043
69202
  const { _readableState: state } = consume2.stream;
69044
- const preserved = consume2.stream[kPreservedBuffer];
69045
- if (preserved && preserved.length > 0) {
69046
- for (const chunk of preserved) {
69047
- consumePush(consume2, chunk);
69048
- }
69049
- consume2.stream[kPreservedBuffer] = null;
69050
- } else if (state.bufferIndex) {
69203
+ if (state.bufferIndex) {
69051
69204
  const start = state.bufferIndex;
69052
69205
  const end = state.buffer.length;
69053
69206
  for (let n = start; n < end; n++) {
@@ -69058,13 +69211,17 @@ var require_readable = __commonJS({
69058
69211
  consumePush(consume2, chunk);
69059
69212
  }
69060
69213
  }
69214
+ const decoder2 = state.decoder;
69215
+ if (decoder2 != null && decoder2.lastNeed > 0) {
69216
+ consumePush(consume2, Buffer.from(decoder2.lastChar.subarray(0, decoder2.lastTotal - decoder2.lastNeed)));
69217
+ }
69061
69218
  if (state.endEmitted) {
69062
- consumeEnd(this[kConsume], this._readableState.encoding);
69063
- } else {
69064
- consume2.stream.on("end", function() {
69065
- consumeEnd(this[kConsume], this._readableState.encoding);
69066
- });
69219
+ consumeEnd(consume2, state.encoding);
69220
+ return;
69067
69221
  }
69222
+ consume2.stream.on("end", function() {
69223
+ consumeEnd(this[kConsume], this._readableState.encoding);
69224
+ });
69068
69225
  consume2.stream.resume();
69069
69226
  while (consume2.stream.read() != null) {
69070
69227
  }
@@ -69121,6 +69278,9 @@ var require_readable = __commonJS({
69121
69278
  if (consume2.body === null) {
69122
69279
  return;
69123
69280
  }
69281
+ if (typeof chunk === "string") {
69282
+ chunk = Buffer.from(chunk, consume2.stream._readableState.encoding);
69283
+ }
69124
69284
  consume2.length += chunk.length;
69125
69285
  consume2.body.push(chunk);
69126
69286
  }
@@ -70105,6 +70265,7 @@ var require_mock_utils = __commonJS({
70105
70265
  }
70106
70266
  } = __require("util");
70107
70267
  var { InvalidArgumentError } = require_errors();
70268
+ var requestAborted = /* @__PURE__ */ Symbol("request aborted");
70108
70269
  function matchValue(match, value) {
70109
70270
  if (typeof match === "string") {
70110
70271
  return match === value;
@@ -70217,6 +70378,8 @@ var require_mock_utils = __commonJS({
70217
70378
  return data;
70218
70379
  } else if (data instanceof ArrayBuffer) {
70219
70380
  return data;
70381
+ } else if (ArrayBuffer.isView(data)) {
70382
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
70220
70383
  } else if (typeof data === "object") {
70221
70384
  return JSON.stringify(data);
70222
70385
  } else if (data) {
@@ -70270,6 +70433,9 @@ var require_mock_utils = __commonJS({
70270
70433
  }
70271
70434
  }
70272
70435
  function removeTrailingSlash(path6) {
70436
+ if (typeof path6 !== "string") {
70437
+ return path6;
70438
+ }
70273
70439
  while (path6.endsWith("/")) {
70274
70440
  path6 = path6.slice(0, -1);
70275
70441
  }
@@ -70323,30 +70489,38 @@ var require_mock_utils = __commonJS({
70323
70489
  const { timesInvoked, times } = mockDispatch2;
70324
70490
  mockDispatch2.consumed = !mockDispatch2.persist && timesInvoked >= times;
70325
70491
  mockDispatch2.pending = timesInvoked < times;
70326
- if (mockDispatch2.data.callback) {
70327
- const callbackResult = mockDispatch2.data.callback(opts);
70492
+ const hasBodyHooks = typeof handler.onBodySent === "function" || typeof handler.onRequestSent === "function";
70493
+ if (mockDispatch2.data.callback && (!hasBodyHooks || opts.body == null)) {
70494
+ const { callback, ...responseDefaults } = mockDispatch2.data;
70495
+ const callbackResult = callback(opts);
70328
70496
  if (isPromise(callbackResult)) {
70329
70497
  callbackResult.then(
70330
70498
  (resolvedData) => {
70331
- mockDispatch2.data = { ...mockDispatch2.data, ...resolvedData };
70499
+ if (resolvedData == null || typeof resolvedData !== "object") {
70500
+ handler.onResponseError(null, new InvalidArgumentError("reply options callback must return an object"));
70501
+ return;
70502
+ }
70503
+ mockDispatch2.data = { ...responseDefaults, ...resolvedData };
70332
70504
  dispatchMockReply(mockDispatches, mockDispatch2, key2, opts, handler);
70333
70505
  },
70334
70506
  (error) => {
70335
- deleteMockDispatch(mockDispatches, key2);
70336
70507
  handler.onResponseError(null, error);
70337
70508
  }
70338
70509
  );
70339
70510
  return true;
70340
70511
  }
70341
- mockDispatch2.data = { ...mockDispatch2.data, ...callbackResult };
70512
+ if (callbackResult == null || typeof callbackResult !== "object") {
70513
+ throw new InvalidArgumentError("reply options callback must return an object");
70514
+ }
70515
+ mockDispatch2.data = { ...responseDefaults, ...callbackResult };
70342
70516
  }
70343
70517
  return dispatchMockReply(mockDispatches, mockDispatch2, key2, opts, handler);
70344
70518
  }
70345
70519
  function dispatchMockReply(mockDispatches, mockDispatch2, key2, opts, handler) {
70346
- const { data: { statusCode, data, headers, trailers, error }, delay } = mockDispatch2;
70347
- if (error !== null) {
70520
+ const { data: response, delay } = mockDispatch2;
70521
+ if (response.error !== null) {
70348
70522
  deleteMockDispatch(mockDispatches, key2);
70349
- handler.onResponseError(null, error);
70523
+ handler.onResponseError(null, response.error);
70350
70524
  return true;
70351
70525
  }
70352
70526
  let aborted = false;
@@ -70373,30 +70547,91 @@ var require_mock_utils = __commonJS({
70373
70547
  handler.onResponseError?.(controller, reason);
70374
70548
  }
70375
70549
  };
70550
+ let replyOpts = opts;
70551
+ const dispatches = mockDispatches;
70376
70552
  handler.onRequestStart?.(controller, null);
70377
- if (typeof delay === "number" && delay > 0) {
70378
- timer2 = setTimeout(() => {
70379
- timer2 = null;
70380
- handleReply(mockDispatches);
70381
- }, delay);
70382
- } else {
70383
- handleReply(mockDispatches);
70553
+ if (aborted) {
70554
+ return true;
70555
+ }
70556
+ const requestBody = dispatchRequestBody(opts.body, handler, controller, () => aborted);
70557
+ if (isPromise(requestBody)) {
70558
+ requestBody.then((body) => {
70559
+ if (body === requestAborted) {
70560
+ return;
70561
+ }
70562
+ if (body !== opts.body) {
70563
+ replyOpts = { ...opts, body };
70564
+ }
70565
+ sendReply();
70566
+ }, (error) => controller.abort(error));
70567
+ return true;
70568
+ }
70569
+ if (requestBody === requestAborted) {
70570
+ return true;
70571
+ }
70572
+ if (requestBody !== opts.body) {
70573
+ replyOpts = { ...opts, body: requestBody };
70574
+ }
70575
+ sendReply();
70576
+ function sendReply() {
70577
+ if (response.callback) {
70578
+ const { callback, ...responseDefaults } = response;
70579
+ let callbackResult;
70580
+ try {
70581
+ callbackResult = callback(replyOpts);
70582
+ } catch (err) {
70583
+ deleteMockDispatch(mockDispatches, key2);
70584
+ handler.onResponseError(null, err);
70585
+ return;
70586
+ }
70587
+ if (isPromise(callbackResult)) {
70588
+ callbackResult.then(
70589
+ (resolvedData) => {
70590
+ if (resolvedData == null || typeof resolvedData !== "object") {
70591
+ handler.onResponseError(null, new InvalidArgumentError("reply options callback must return an object"));
70592
+ return;
70593
+ }
70594
+ mockDispatch2.data = { ...responseDefaults, ...resolvedData };
70595
+ handleReply(dispatches, mockDispatch2.data);
70596
+ },
70597
+ (err) => {
70598
+ handler.onResponseError(null, err);
70599
+ }
70600
+ );
70601
+ return;
70602
+ }
70603
+ if (callbackResult == null || typeof callbackResult !== "object") {
70604
+ throw new InvalidArgumentError("reply options callback must return an object");
70605
+ }
70606
+ mockDispatch2.data = { ...responseDefaults, ...callbackResult };
70607
+ handleReply(dispatches, mockDispatch2.data);
70608
+ return;
70609
+ }
70610
+ if (typeof delay === "number" && delay > 0) {
70611
+ timer2 = setTimeout(() => {
70612
+ timer2 = null;
70613
+ handleReply(dispatches);
70614
+ }, delay);
70615
+ } else {
70616
+ handleReply(dispatches);
70617
+ }
70384
70618
  }
70385
- function handleReply(mockDispatches2, _data = data) {
70619
+ function handleReply(mockDispatches2, _response = response) {
70386
70620
  if (aborted) {
70387
70621
  return;
70388
70622
  }
70623
+ const { statusCode, data, headers, trailers } = _response;
70389
70624
  const optsHeaders = Array.isArray(opts.headers) ? buildHeadersFromArray(opts.headers) : opts.headers;
70390
- const body = typeof _data === "function" ? _data({ ...opts, headers: optsHeaders }) : _data;
70625
+ const body = typeof data === "function" ? data({ ...replyOpts, headers: optsHeaders }) : data;
70391
70626
  if (isPromise(body)) {
70392
- return body.then((newData) => handleReply(mockDispatches2, newData));
70627
+ return body.then((newData) => handleReply(mockDispatches2, { ..._response, data: newData }));
70393
70628
  }
70394
70629
  if (aborted) {
70395
70630
  return;
70396
70631
  }
70397
70632
  const responseData = getResponseData(body);
70398
- const responseHeaders = generateKeyValues(headers);
70399
- const responseTrailers = generateKeyValues(trailers);
70633
+ const responseHeaders = generateKeyValues(headers ?? {});
70634
+ const responseTrailers = generateKeyValues(trailers ?? {});
70400
70635
  controller.rawHeaders = responseHeaders;
70401
70636
  controller.rawTrailers = responseTrailers;
70402
70637
  handler.onResponseStart?.(controller, statusCode, parseHeaders(responseHeaders), getStatusText(statusCode));
@@ -70406,6 +70641,78 @@ var require_mock_utils = __commonJS({
70406
70641
  }
70407
70642
  return true;
70408
70643
  }
70644
+ function dispatchRequestBody(body, handler, controller, isAborted2) {
70645
+ if (typeof handler.onBodySent !== "function" && typeof handler.onRequestSent !== "function") {
70646
+ return body;
70647
+ }
70648
+ if (body == null) {
70649
+ return callOnRequestSent(handler, controller, isAborted2) ? body : requestAborted;
70650
+ }
70651
+ if (body && typeof body[Symbol.asyncIterator] === "function") {
70652
+ return dispatchAsyncIterableBody(body, handler, controller, isAborted2);
70653
+ }
70654
+ if (isIterableBody(body)) {
70655
+ const chunks = [];
70656
+ for (const chunk of body) {
70657
+ if (isAborted2()) {
70658
+ return requestAborted;
70659
+ }
70660
+ chunks.push(chunk);
70661
+ if (!callOnBodySent(handler, controller, chunk) || isAborted2()) {
70662
+ return requestAborted;
70663
+ }
70664
+ }
70665
+ return callOnRequestSent(handler, controller, isAborted2) ? chunks : requestAborted;
70666
+ }
70667
+ if (isAborted2()) {
70668
+ return requestAborted;
70669
+ }
70670
+ if (!callOnBodySent(handler, controller, body)) {
70671
+ return requestAborted;
70672
+ }
70673
+ return callOnRequestSent(handler, controller, isAborted2) ? body : requestAborted;
70674
+ }
70675
+ async function dispatchAsyncIterableBody(body, handler, controller, isAborted2) {
70676
+ const chunks = [];
70677
+ for await (const chunk of body) {
70678
+ if (isAborted2()) {
70679
+ return requestAborted;
70680
+ }
70681
+ chunks.push(chunk);
70682
+ if (!callOnBodySent(handler, controller, chunk) || isAborted2()) {
70683
+ return requestAborted;
70684
+ }
70685
+ }
70686
+ if (!callOnRequestSent(handler, controller, isAborted2)) {
70687
+ return requestAborted;
70688
+ }
70689
+ return {
70690
+ async *[Symbol.asyncIterator]() {
70691
+ yield* chunks;
70692
+ }
70693
+ };
70694
+ }
70695
+ function callOnBodySent(handler, controller, chunk) {
70696
+ try {
70697
+ handler.onBodySent?.(chunk);
70698
+ return true;
70699
+ } catch (error) {
70700
+ controller.abort(error);
70701
+ return false;
70702
+ }
70703
+ }
70704
+ function callOnRequestSent(handler, controller, isAborted2) {
70705
+ try {
70706
+ handler.onRequestSent?.();
70707
+ return !isAborted2();
70708
+ } catch (error) {
70709
+ controller.abort(error);
70710
+ return false;
70711
+ }
70712
+ }
70713
+ function isIterableBody(body) {
70714
+ return typeof body !== "string" && !Buffer.isBuffer(body) && !ArrayBuffer.isView(body) && typeof body[Symbol.iterator] === "function";
70715
+ }
70409
70716
  function buildMockDispatch() {
70410
70717
  const agent = this[kMockAgent];
70411
70718
  const origin2 = this[kOrigin];
@@ -73049,15 +73356,13 @@ var require_cache = __commonJS({
73049
73356
  }
73050
73357
  }
73051
73358
  function makeCacheKey(opts) {
73052
- if (!opts.origin) {
73053
- throw new Error("opts.origin is undefined");
73054
- }
73359
+ const origin2 = opts.origin ? opts.origin.toString() : "";
73055
73360
  let fullPath = opts.path || "/";
73056
73361
  if (opts.query && !pathHasQueryOrFragment(fullPath)) {
73057
73362
  fullPath = serializePathWithQuery(fullPath, opts.query);
73058
73363
  }
73059
73364
  return {
73060
- origin: opts.origin.toString(),
73365
+ origin: origin2,
73061
73366
  method: opts.method,
73062
73367
  path: fullPath,
73063
73368
  headers: opts.headers
@@ -75140,10 +75445,13 @@ var require_cache2 = __commonJS({
75140
75445
  }
75141
75446
  return (dispatch) => {
75142
75447
  return (opts2, handler) => {
75143
- if (!opts2.origin || arrayIncludes(safeMethodsToNotCache, opts2.method)) {
75448
+ if (arrayIncludes(safeMethodsToNotCache, opts2.method)) {
75144
75449
  return dispatch(opts2, handler);
75145
75450
  }
75146
75451
  if (origins !== void 0) {
75452
+ if (!opts2.origin) {
75453
+ return dispatch(opts2, handler);
75454
+ }
75147
75455
  const requestOrigin = opts2.origin.toString().toLowerCase();
75148
75456
  let isAllowed = false;
75149
75457
  for (let i = 0; i < origins.length; i++) {
@@ -75841,7 +76149,7 @@ var require_deduplicate = __commonJS({
75841
76149
  const pendingRequests = /* @__PURE__ */ new Map();
75842
76150
  return (dispatch) => {
75843
76151
  return (opts2, handler) => {
75844
- if (!opts2.origin || methods.includes(opts2.method) === false) {
76152
+ if (opts2.upgrade || methods.includes(opts2.method) === false) {
75845
76153
  return dispatch(opts2, handler);
75846
76154
  }
75847
76155
  opts2 = {
@@ -81691,6 +81999,8 @@ var require_websocket2 = __commonJS({
81691
81999
  var { SendQueue } = require_sender2();
81692
82000
  var { WebsocketFrameSend } = require_frame();
81693
82001
  var { channels } = require_diagnostics();
82002
+ var kRef = /* @__PURE__ */ Symbol.for("nodejs.ref");
82003
+ var kUnref = /* @__PURE__ */ Symbol.for("nodejs.unref");
81694
82004
  function getSocketAddress(socket) {
81695
82005
  if (typeof socket?.address === "function") {
81696
82006
  return socket.address();
@@ -81710,6 +82020,7 @@ var require_websocket2 = __commonJS({
81710
82020
  #bufferedAmount = 0;
81711
82021
  #protocol = "";
81712
82022
  #extensions = "";
82023
+ #refed = true;
81713
82024
  /** @type {SendQueue} */
81714
82025
  #sendQueue;
81715
82026
  /** @type {Handler} */
@@ -81792,6 +82103,16 @@ var require_websocket2 = __commonJS({
81792
82103
  this.#handler.readyState = _WebSocket.CONNECTING;
81793
82104
  this.#binaryType = "blob";
81794
82105
  }
82106
+ [kRef]() {
82107
+ webidl.brandCheck(this, _WebSocket);
82108
+ this.#refed = true;
82109
+ this.#handler.socket?.ref?.();
82110
+ }
82111
+ [kUnref]() {
82112
+ webidl.brandCheck(this, _WebSocket);
82113
+ this.#refed = false;
82114
+ this.#handler.socket?.unref?.();
82115
+ }
81795
82116
  /**
81796
82117
  * @see https://websockets.spec.whatwg.org/#dom-websocket-close
81797
82118
  * @param {number|undefined} code
@@ -81953,6 +82274,9 @@ var require_websocket2 = __commonJS({
81953
82274
  */
81954
82275
  #onConnectionEstablished(response, parsedExtensions) {
81955
82276
  this.#handler.socket = response.socket;
82277
+ if (!this.#refed) {
82278
+ this.#handler.socket.unref?.();
82279
+ }
81956
82280
  const maxFragments = this.#handler.controller.dispatcher?.webSocketOptions?.maxFragments;
81957
82281
  const maxPayloadSize = this.#handler.controller.dispatcher?.webSocketOptions?.maxPayloadSize;
81958
82282
  const parser = new ByteParser(this.#handler, parsedExtensions, {