@blockrun/clawrouter 0.12.298 → 0.12.300

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 +150 -75
  2. package/dist/index.js +148 -73
  3. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -43186,7 +43186,7 @@ function createPayFetchWithPreAuth(baseFetch, client, ttlMs = DEFAULT_TTL_MS, op
43186
43186
  }
43187
43187
  };
43188
43188
  const cached = !options?.skipPreAuth ? cache2.get(cacheKey3) : void 0;
43189
- const preAuthCovers = cached !== void 0 && Date.now() - cached.cachedAt < ttlMs && cached.coverMicros !== void 0 && needMicros !== void 0 && needMicros <= cached.coverMicros;
43189
+ const preAuthCovers = cached !== void 0 && Date.now() - cached.cachedAt < ttlMs && cached.coverMicros !== void 0 && needMicros !== void 0 && needMicros <= cached.coverMicros && cached.coverMicros <= needMicros * (1 + PREAUTH_OVERPAY_TOLERANCE);
43190
43190
  if (preAuthCovers) {
43191
43191
  let paymentInFlight = false;
43192
43192
  try {
@@ -43355,7 +43355,7 @@ function receiptChargedMicros(response) {
43355
43355
  }
43356
43356
  return void 0;
43357
43357
  }
43358
- var DEFAULT_TTL_MS, PaymentInDoubtError;
43358
+ var DEFAULT_TTL_MS, PREAUTH_OVERPAY_TOLERANCE, PaymentInDoubtError;
43359
43359
  var init_payment_preauth = __esm({
43360
43360
  "src/payment-preauth.ts"() {
43361
43361
  "use strict";
@@ -43363,6 +43363,7 @@ var init_payment_preauth = __esm({
43363
43363
  init_max_tokens();
43364
43364
  init_spend_control();
43365
43365
  DEFAULT_TTL_MS = 36e5;
43366
+ PREAUTH_OVERPAY_TOLERANCE = 0.03;
43366
43367
  PaymentInDoubtError = class extends Error {
43367
43368
  constructor(cause) {
43368
43369
  super(
@@ -67263,7 +67264,7 @@ var require_websocket = __commonJS({
67263
67264
  var net3 = __require("net");
67264
67265
  var tls2 = __require("tls");
67265
67266
  var { randomBytes: randomBytes9, createHash: createHash6 } = __require("crypto");
67266
- var { Duplex, Readable: Readable3 } = __require("stream");
67267
+ var { Duplex, Readable: Readable4 } = __require("stream");
67267
67268
  var { URL: URL3 } = __require("url");
67268
67269
  var PerMessageDeflate2 = require_permessage_deflate();
67269
67270
  var Receiver2 = require_receiver();
@@ -104625,6 +104626,11 @@ ${lines.join("\n")}`;
104625
104626
  });
104626
104627
 
104627
104628
  // src/local-guard.ts
104629
+ import { request as httpRequest } from "http";
104630
+ import { request as httpsRequest } from "https";
104631
+ import { lookup as dnsLookup } from "dns/promises";
104632
+ import { isIP } from "net";
104633
+ import { Readable } from "stream";
104628
104634
  import { closeSync as closeSync3, fstatSync as fstatSync2, openSync as openSync3, readSync as readSync2, realpathSync } from "fs";
104629
104635
  import { homedir as homedir11 } from "os";
104630
104636
  import { join as join15 } from "path";
@@ -104701,18 +104707,84 @@ function isBlockedSsrfHost(hostname) {
104701
104707
  }
104702
104708
  return false;
104703
104709
  }
104710
+ async function resolvePublicAddresses(hostname, resolve2 = systemResolver) {
104711
+ const h = hostname.replace(/^\[/, "").replace(/\]$/, "");
104712
+ const family = isIP(h);
104713
+ const addrs = family ? [{ address: h, family }] : await resolve2(h);
104714
+ if (addrs.length === 0) throw new Error(`could not resolve ${hostname}`);
104715
+ const blocked = addrs.find((a) => isBlockedSsrfHost(a.address));
104716
+ if (blocked) {
104717
+ throw new Error(
104718
+ `refusing to fetch ${hostname}: it resolves to a private/loopback/metadata address (${blocked.address})`
104719
+ );
104720
+ }
104721
+ return addrs;
104722
+ }
104723
+ function pinnedLookup(addrs) {
104724
+ return (_hostname, options, callback) => {
104725
+ if (options.all) {
104726
+ callback(null, addrs);
104727
+ } else {
104728
+ callback(null, addrs[0].address, addrs[0].family);
104729
+ }
104730
+ };
104731
+ }
104732
+ function pinnedFetch(url2, addrs, init2 = {}) {
104733
+ const method = (init2.method ?? "GET").toUpperCase();
104734
+ const body = init2.body;
104735
+ if (body != null && typeof body !== "string" && !(body instanceof Uint8Array)) {
104736
+ return Promise.reject(new TypeError("pinnedFetch supports only string or byte bodies"));
104737
+ }
104738
+ const headers = Object.fromEntries(new Headers(init2.headers));
104739
+ const send = url2.protocol === "https:" ? httpsRequest : httpRequest;
104740
+ return new Promise((resolve2, reject) => {
104741
+ const req = send(
104742
+ url2,
104743
+ { method, headers, lookup: pinnedLookup(addrs), signal: init2.signal ?? void 0 },
104744
+ (res) => {
104745
+ const status2 = res.statusCode ?? 0;
104746
+ if (status2 < 200 || status2 > 599) {
104747
+ res.destroy();
104748
+ reject(new Error(`unexpected HTTP status ${status2} from ${url2.host}`));
104749
+ return;
104750
+ }
104751
+ const resHeaders = new Headers();
104752
+ for (let i = 0; i + 1 < res.rawHeaders.length; i += 2) {
104753
+ resHeaders.append(res.rawHeaders[i], res.rawHeaders[i + 1]);
104754
+ }
104755
+ const noBody = method === "HEAD" || NULL_BODY_STATUSES.has(status2);
104756
+ if (noBody) res.resume();
104757
+ resolve2(
104758
+ new Response(noBody ? null : Readable.toWeb(res), {
104759
+ status: status2,
104760
+ statusText: res.statusMessage,
104761
+ headers: resHeaders
104762
+ })
104763
+ );
104764
+ }
104765
+ );
104766
+ req.on("error", reject);
104767
+ req.end(body ?? void 0);
104768
+ });
104769
+ }
104704
104770
  async function ssrfSafeFetch(url2, init2 = {}, maxHops = 5) {
104705
- const { allowPrivate, ...fetchInit } = init2;
104771
+ const { allowPrivate, viaUpstreamProxy, resolve: resolve2, ...fetchInit } = init2;
104706
104772
  let current = url2;
104707
104773
  for (let hop = 0; hop <= maxHops; hop++) {
104708
104774
  const u = new URL(current);
104709
104775
  if (u.protocol !== "http:" && u.protocol !== "https:") {
104710
104776
  throw new Error(`refusing to fetch non-http(s) URL (${u.protocol})`);
104711
104777
  }
104712
- if (!allowPrivate && isBlockedSsrfHost(u.hostname)) {
104713
- throw new Error(`refusing to fetch a private/loopback/metadata address: ${u.hostname}`);
104778
+ let res;
104779
+ if (allowPrivate) {
104780
+ res = await fetch(current, { ...fetchInit, redirect: "manual" });
104781
+ } else {
104782
+ if (isBlockedSsrfHost(u.hostname)) {
104783
+ throw new Error(`refusing to fetch a private/loopback/metadata address: ${u.hostname}`);
104784
+ }
104785
+ const addrs = await resolvePublicAddresses(u.hostname, resolve2);
104786
+ res = viaUpstreamProxy ? await fetch(current, { ...fetchInit, redirect: "manual" }) : await pinnedFetch(u, addrs, fetchInit);
104714
104787
  }
104715
- const res = await fetch(current, { ...fetchInit, redirect: "manual" });
104716
104788
  if (res.status >= 300 && res.status < 400) {
104717
104789
  const loc = res.headers.get("location");
104718
104790
  if (!loc) return res;
@@ -104751,11 +104823,13 @@ function readLocalImageAsDataUri(filePath) {
104751
104823
  closeSync3(fd);
104752
104824
  }
104753
104825
  }
104754
- var METADATA_HOSTS, MAX_LOCAL_IMAGE_BYTES, IMAGE_SIGNATURES;
104826
+ var METADATA_HOSTS, systemResolver, NULL_BODY_STATUSES, MAX_LOCAL_IMAGE_BYTES, IMAGE_SIGNATURES;
104755
104827
  var init_local_guard = __esm({
104756
104828
  "src/local-guard.ts"() {
104757
104829
  "use strict";
104758
104830
  METADATA_HOSTS = /* @__PURE__ */ new Set(["metadata", "metadata.goog", "instance-data"]);
104831
+ systemResolver = (hostname) => dnsLookup(hostname, { all: true, verbatim: true });
104832
+ NULL_BODY_STATUSES = /* @__PURE__ */ new Set([101, 103, 204, 205, 304]);
104759
104833
  MAX_LOCAL_IMAGE_BYTES = 25 * 1024 * 1024;
104760
104834
  IMAGE_SIGNATURES = [
104761
104835
  {
@@ -108278,13 +108352,13 @@ var require_util2 = __commonJS({
108278
108352
  function TAOCheck() {
108279
108353
  return "success";
108280
108354
  }
108281
- function appendFetchMetadata(httpRequest) {
108282
- if (!isURLPotentiallyTrustworthy(requestCurrentURL(httpRequest))) {
108355
+ function appendFetchMetadata(httpRequest2) {
108356
+ if (!isURLPotentiallyTrustworthy(requestCurrentURL(httpRequest2))) {
108283
108357
  return;
108284
108358
  }
108285
108359
  let header = null;
108286
- header = httpRequest.mode;
108287
- httpRequest.headersList.set("sec-fetch-mode", header, true);
108360
+ header = httpRequest2.mode;
108361
+ httpRequest2.headersList.set("sec-fetch-mode", header, true);
108288
108362
  }
108289
108363
  function appendRequestOriginHeader(request2) {
108290
108364
  let serializedOrigin = request2.origin;
@@ -120163,7 +120237,7 @@ var require_readable = __commonJS({
120163
120237
  "use strict";
120164
120238
  var assert10 = __require("assert");
120165
120239
  var { addAbortListener } = __require("events");
120166
- var { Readable: Readable3 } = __require("stream");
120240
+ var { Readable: Readable4 } = __require("stream");
120167
120241
  var { RequestAbortedError, NotSupportedError, InvalidArgumentError, AbortError } = require_errors();
120168
120242
  var util5 = require_util();
120169
120243
  var kConsume = /* @__PURE__ */ Symbol("kConsume");
@@ -120176,7 +120250,7 @@ var require_readable = __commonJS({
120176
120250
  var kBytesRead = /* @__PURE__ */ Symbol("kBytesRead");
120177
120251
  var noop2 = () => {
120178
120252
  };
120179
- var BodyReadable = class extends Readable3 {
120253
+ var BodyReadable = class extends Readable4 {
120180
120254
  /**
120181
120255
  * @param {object} opts
120182
120256
  * @param {(this: Readable, size: number) => void} opts.resume
@@ -120575,7 +120649,7 @@ var require_api_request = __commonJS({
120575
120649
  "use strict";
120576
120650
  var assert10 = __require("assert");
120577
120651
  var { AsyncResource } = __require("async_hooks");
120578
- var { Readable: Readable3 } = require_readable();
120652
+ var { Readable: Readable4 } = require_readable();
120579
120653
  var { InvalidArgumentError, RequestAbortedError } = require_errors();
120580
120654
  var util5 = require_util();
120581
120655
  function noop2() {
@@ -120661,7 +120735,7 @@ var require_api_request = __commonJS({
120661
120735
  const parsedHeaders = headers;
120662
120736
  const contentType = parsedHeaders?.["content-type"];
120663
120737
  const contentLength = parsedHeaders?.["content-length"];
120664
- const res = new Readable3({
120738
+ const res = new Readable4({
120665
120739
  resume: () => controller.resume(),
120666
120740
  abort: (reason) => controller.abort(reason),
120667
120741
  contentType,
@@ -121037,7 +121111,7 @@ var require_api_pipeline = __commonJS({
121037
121111
  "node_modules/undici/lib/api/api-pipeline.js"(exports, module) {
121038
121112
  "use strict";
121039
121113
  var {
121040
- Readable: Readable3,
121114
+ Readable: Readable4,
121041
121115
  Duplex,
121042
121116
  PassThrough
121043
121117
  } = __require("stream");
@@ -121054,7 +121128,7 @@ var require_api_pipeline = __commonJS({
121054
121128
  function noop2() {
121055
121129
  }
121056
121130
  var kResume = /* @__PURE__ */ Symbol("resume");
121057
- var PipelineRequest = class extends Readable3 {
121131
+ var PipelineRequest = class extends Readable4 {
121058
121132
  constructor() {
121059
121133
  super({ autoDestroy: true });
121060
121134
  this[kResume] = null;
@@ -121072,7 +121146,7 @@ var require_api_pipeline = __commonJS({
121072
121146
  callback(err);
121073
121147
  }
121074
121148
  };
121075
- var PipelineResponse = class extends Readable3 {
121149
+ var PipelineResponse = class extends Readable4 {
121076
121150
  constructor(resume2) {
121077
121151
  super({ autoDestroy: true });
121078
121152
  this[kResume] = resume2;
@@ -124126,7 +124200,7 @@ var require_dump = __commonJS({
124126
124200
  var require_dns = __commonJS({
124127
124201
  "node_modules/undici/lib/interceptor/dns.js"(exports, module) {
124128
124202
  "use strict";
124129
- var { isIP: isIP2 } = __require("net");
124203
+ var { isIP: isIP3 } = __require("net");
124130
124204
  var { lookup } = __require("dns");
124131
124205
  var DecoratorHandler = require_decorator_handler();
124132
124206
  var { InvalidArgumentError, InformationalError } = require_errors();
@@ -124543,7 +124617,7 @@ var require_dns = __commonJS({
124543
124617
  return dispatch(origDispatchOpts, handler);
124544
124618
  }
124545
124619
  const origin2 = origDispatchOpts.origin.constructor === URL ? origDispatchOpts.origin : new URL(origDispatchOpts.origin);
124546
- if (isIP2(origin2.hostname) !== 0) {
124620
+ if (isIP3(origin2.hostname) !== 0) {
124547
124621
  return dispatch(origDispatchOpts, handler);
124548
124622
  }
124549
124623
  instance.runLookup(origin2, origDispatchOpts, (err, newOrigin) => {
@@ -126614,7 +126688,7 @@ var require_cache2 = __commonJS({
126614
126688
  "node_modules/undici/lib/interceptor/cache.js"(exports, module) {
126615
126689
  "use strict";
126616
126690
  var assert10 = __require("assert");
126617
- var { Readable: Readable3 } = __require("stream");
126691
+ var { Readable: Readable4 } = __require("stream");
126618
126692
  var util5 = require_util();
126619
126693
  var CacheHandler = require_cache_handler();
126620
126694
  var MemoryCacheStore = require_memory_cache_store();
@@ -126810,7 +126884,7 @@ var require_cache2 = __commonJS({
126810
126884
  return dispatch(opts, new CacheHandler(globalOpts, cacheKey3, handler));
126811
126885
  }
126812
126886
  function sendCachedValue(handler, opts, result, age, context, isStale2) {
126813
- const stream4 = util5.isStream(result.body) ? result.body : Readable3.from(result.body ?? []);
126887
+ const stream4 = util5.isStream(result.body) ? result.body : Readable4.from(result.body ?? []);
126814
126888
  assert10(!stream4.destroyed, "stream should not be destroyed");
126815
126889
  assert10(!stream4.readableDidRead, "stream should not be readableDidRead");
126816
126890
  let aborted = false;
@@ -130275,7 +130349,7 @@ var require_fetch = __commonJS({
130275
130349
  subresourceSet
130276
130350
  } = require_constants3();
130277
130351
  var EE = __require("events");
130278
- var { Readable: Readable3, pipeline: pipeline2, finished: finished2, isErrored, isReadable } = __require("stream");
130352
+ var { Readable: Readable4, pipeline: pipeline2, finished: finished2, isErrored, isReadable } = __require("stream");
130279
130353
  var { addAbortListener, bufferToLowerCasedHeaderName } = require_util();
130280
130354
  var { SocketError } = require_errors();
130281
130355
  var { dataURLProcessor, serializeAMimeType, minimizeSupportedMimeType } = require_data_url();
@@ -130904,90 +130978,90 @@ var require_fetch = __commonJS({
130904
130978
  async function httpNetworkOrCacheFetch(fetchParams, isAuthenticationFetch = false, isNewConnectionFetch = false) {
130905
130979
  const request2 = fetchParams.request;
130906
130980
  let httpFetchParams = null;
130907
- let httpRequest = null;
130981
+ let httpRequest2 = null;
130908
130982
  let response = null;
130909
130983
  const httpCache = null;
130910
130984
  const revalidatingFlag = false;
130911
130985
  if (request2.window === "no-window" && request2.redirect === "error") {
130912
130986
  httpFetchParams = fetchParams;
130913
- httpRequest = request2;
130987
+ httpRequest2 = request2;
130914
130988
  } else {
130915
130989
  if (request2.body?.source != null) {
130916
- httpRequest = cloneRequest(request2);
130990
+ httpRequest2 = cloneRequest(request2);
130917
130991
  } else {
130918
- httpRequest = cloneRequest({ ...request2, body: null });
130919
- httpRequest.body = request2.body;
130992
+ httpRequest2 = cloneRequest({ ...request2, body: null });
130993
+ httpRequest2.body = request2.body;
130920
130994
  }
130921
130995
  httpFetchParams = { ...fetchParams };
130922
- httpFetchParams.request = httpRequest;
130996
+ httpFetchParams.request = httpRequest2;
130923
130997
  }
130924
130998
  const includeCredentials = request2.credentials === "include" || request2.credentials === "same-origin" && request2.responseTainting === "basic";
130925
- const contentLength = httpRequest.body ? httpRequest.body.length : null;
130999
+ const contentLength = httpRequest2.body ? httpRequest2.body.length : null;
130926
131000
  let contentLengthHeaderValue = null;
130927
- if (httpRequest.body == null && ["POST", "PUT"].includes(httpRequest.method)) {
131001
+ if (httpRequest2.body == null && ["POST", "PUT"].includes(httpRequest2.method)) {
130928
131002
  contentLengthHeaderValue = "0";
130929
131003
  }
130930
131004
  if (contentLength != null) {
130931
131005
  contentLengthHeaderValue = isomorphicEncode(`${contentLength}`);
130932
131006
  }
130933
- if (contentLengthHeaderValue != null && !httpRequest.headersList.contains("content-length", true)) {
130934
- httpRequest.headersList.append("content-length", contentLengthHeaderValue, true);
131007
+ if (contentLengthHeaderValue != null && !httpRequest2.headersList.contains("content-length", true)) {
131008
+ httpRequest2.headersList.append("content-length", contentLengthHeaderValue, true);
130935
131009
  }
130936
- if (contentLength != null && httpRequest.keepalive) {
131010
+ if (contentLength != null && httpRequest2.keepalive) {
130937
131011
  }
130938
- if (webidl.is.URL(httpRequest.referrer)) {
130939
- httpRequest.headersList.append("referer", isomorphicEncode(httpRequest.referrer.href), true);
131012
+ if (webidl.is.URL(httpRequest2.referrer)) {
131013
+ httpRequest2.headersList.append("referer", isomorphicEncode(httpRequest2.referrer.href), true);
130940
131014
  }
130941
- appendRequestOriginHeader(httpRequest);
130942
- appendFetchMetadata(httpRequest);
130943
- if (!httpRequest.headersList.contains("user-agent", true)) {
130944
- httpRequest.headersList.append("user-agent", defaultUserAgent, true);
131015
+ appendRequestOriginHeader(httpRequest2);
131016
+ appendFetchMetadata(httpRequest2);
131017
+ if (!httpRequest2.headersList.contains("user-agent", true)) {
131018
+ httpRequest2.headersList.append("user-agent", defaultUserAgent, true);
130945
131019
  }
130946
- if (httpRequest.cache === "default" && (httpRequest.headersList.contains("if-modified-since", true) || httpRequest.headersList.contains("if-none-match", true) || httpRequest.headersList.contains("if-unmodified-since", true) || httpRequest.headersList.contains("if-match", true) || httpRequest.headersList.contains("if-range", true))) {
130947
- httpRequest.cache = "no-store";
131020
+ if (httpRequest2.cache === "default" && (httpRequest2.headersList.contains("if-modified-since", true) || httpRequest2.headersList.contains("if-none-match", true) || httpRequest2.headersList.contains("if-unmodified-since", true) || httpRequest2.headersList.contains("if-match", true) || httpRequest2.headersList.contains("if-range", true))) {
131021
+ httpRequest2.cache = "no-store";
130948
131022
  }
130949
- if (httpRequest.cache === "no-cache" && !httpRequest.preventNoCacheCacheControlHeaderModification && !httpRequest.headersList.contains("cache-control", true)) {
130950
- httpRequest.headersList.append("cache-control", "max-age=0", true);
131023
+ if (httpRequest2.cache === "no-cache" && !httpRequest2.preventNoCacheCacheControlHeaderModification && !httpRequest2.headersList.contains("cache-control", true)) {
131024
+ httpRequest2.headersList.append("cache-control", "max-age=0", true);
130951
131025
  }
130952
- if (httpRequest.cache === "no-store" || httpRequest.cache === "reload") {
130953
- if (!httpRequest.headersList.contains("pragma", true)) {
130954
- httpRequest.headersList.append("pragma", "no-cache", true);
131026
+ if (httpRequest2.cache === "no-store" || httpRequest2.cache === "reload") {
131027
+ if (!httpRequest2.headersList.contains("pragma", true)) {
131028
+ httpRequest2.headersList.append("pragma", "no-cache", true);
130955
131029
  }
130956
- if (!httpRequest.headersList.contains("cache-control", true)) {
130957
- httpRequest.headersList.append("cache-control", "no-cache", true);
131030
+ if (!httpRequest2.headersList.contains("cache-control", true)) {
131031
+ httpRequest2.headersList.append("cache-control", "no-cache", true);
130958
131032
  }
130959
131033
  }
130960
- if (httpRequest.headersList.contains("range", true)) {
130961
- httpRequest.headersList.append("accept-encoding", "identity", true);
131034
+ if (httpRequest2.headersList.contains("range", true)) {
131035
+ httpRequest2.headersList.append("accept-encoding", "identity", true);
130962
131036
  }
130963
- if (!httpRequest.headersList.contains("accept-encoding", true)) {
130964
- if (urlHasHttpsScheme(requestCurrentURL(httpRequest))) {
130965
- httpRequest.headersList.append("accept-encoding", "br, gzip, deflate, zstd", true);
131037
+ if (!httpRequest2.headersList.contains("accept-encoding", true)) {
131038
+ if (urlHasHttpsScheme(requestCurrentURL(httpRequest2))) {
131039
+ httpRequest2.headersList.append("accept-encoding", "br, gzip, deflate, zstd", true);
130966
131040
  } else {
130967
- httpRequest.headersList.append("accept-encoding", "gzip, deflate", true);
131041
+ httpRequest2.headersList.append("accept-encoding", "gzip, deflate", true);
130968
131042
  }
130969
131043
  }
130970
- httpRequest.headersList.delete("host", true);
131044
+ httpRequest2.headersList.delete("host", true);
130971
131045
  if (includeCredentials) {
130972
- if (!httpRequest.headersList.contains("authorization", true)) {
131046
+ if (!httpRequest2.headersList.contains("authorization", true)) {
130973
131047
  let authorizationValue = null;
130974
- if (hasAuthenticationEntry(httpRequest) && (httpRequest.useURLCredentials === void 0 || !includesCredentials(requestCurrentURL(httpRequest)))) {
130975
- } else if (includesCredentials(requestCurrentURL(httpRequest)) && isAuthenticationFetch) {
130976
- const { username, password } = requestCurrentURL(httpRequest);
131048
+ if (hasAuthenticationEntry(httpRequest2) && (httpRequest2.useURLCredentials === void 0 || !includesCredentials(requestCurrentURL(httpRequest2)))) {
131049
+ } else if (includesCredentials(requestCurrentURL(httpRequest2)) && isAuthenticationFetch) {
131050
+ const { username, password } = requestCurrentURL(httpRequest2);
130977
131051
  authorizationValue = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
130978
131052
  }
130979
131053
  if (authorizationValue !== null) {
130980
- httpRequest.headersList.append("Authorization", authorizationValue, false);
131054
+ httpRequest2.headersList.append("Authorization", authorizationValue, false);
130981
131055
  }
130982
131056
  }
130983
131057
  }
130984
131058
  if (httpCache == null) {
130985
- httpRequest.cache = "no-store";
131059
+ httpRequest2.cache = "no-store";
130986
131060
  }
130987
- if (httpRequest.cache !== "no-store" && httpRequest.cache !== "reload") {
131061
+ if (httpRequest2.cache !== "no-store" && httpRequest2.cache !== "reload") {
130988
131062
  }
130989
131063
  if (response == null) {
130990
- if (httpRequest.cache === "only-if-cached") {
131064
+ if (httpRequest2.cache === "only-if-cached") {
130991
131065
  return makeNetworkError("only if cached");
130992
131066
  }
130993
131067
  const forwardResponse = await httpNetworkFetch(
@@ -130995,7 +131069,7 @@ var require_fetch = __commonJS({
130995
131069
  includeCredentials,
130996
131070
  isNewConnectionFetch
130997
131071
  );
130998
- if (!safeMethodsSet.has(httpRequest.method) && forwardResponse.status >= 200 && forwardResponse.status <= 399) {
131072
+ if (!safeMethodsSet.has(httpRequest2.method) && forwardResponse.status >= 200 && forwardResponse.status <= 399) {
130999
131073
  }
131000
131074
  if (revalidatingFlag && forwardResponse.status === 304) {
131001
131075
  }
@@ -131003,12 +131077,12 @@ var require_fetch = __commonJS({
131003
131077
  response = forwardResponse;
131004
131078
  }
131005
131079
  }
131006
- response.urlList = [...httpRequest.urlList];
131007
- if (httpRequest.headersList.contains("range", true)) {
131080
+ response.urlList = [...httpRequest2.urlList];
131081
+ if (httpRequest2.headersList.contains("range", true)) {
131008
131082
  response.rangeRequested = true;
131009
131083
  }
131010
131084
  response.requestIncludesCredentials = includeCredentials;
131011
- if (response.status === 401 && httpRequest.responseTainting !== "cors" && includeCredentials && (request2.useURLCredentials !== void 0 || isTraversableNavigable(request2.traversableForUserPrompts))) {
131085
+ if (response.status === 401 && httpRequest2.responseTainting !== "cors" && includeCredentials && (request2.useURLCredentials !== void 0 || isTraversableNavigable(request2.traversableForUserPrompts))) {
131012
131086
  if (request2.body != null) {
131013
131087
  if (request2.body.source == null) {
131014
131088
  return response;
@@ -131305,7 +131379,7 @@ var require_fetch = __commonJS({
131305
131379
  }
131306
131380
  }
131307
131381
  }
131308
- this.body = new Readable3({ read: () => controller.resume() });
131382
+ this.body = new Readable4({ read: () => controller.resume() });
131309
131383
  const onError = (err) => this.onResponseError(controller, err);
131310
131384
  resolve2({
131311
131385
  status: status2,
@@ -137039,7 +137113,7 @@ var init_v1 = __esm({
137039
137113
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
137040
137114
  import { createHmac } from "crypto";
137041
137115
  import { createServer } from "http";
137042
- import { finished, Readable } from "stream";
137116
+ import { finished, Readable as Readable2 } from "stream";
137043
137117
  import { pipeline } from "stream/promises";
137044
137118
  import { homedir as homedir13 } from "os";
137045
137119
  import { join as join17 } from "path";
@@ -137895,7 +137969,7 @@ async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPayment
137895
137969
  if (accountPassthrough) {
137896
137970
  if (upstream.body) {
137897
137971
  await pipeline(
137898
- Readable.fromWeb(upstream.body),
137972
+ Readable2.fromWeb(upstream.body),
137899
137973
  res,
137900
137974
  { signal: clientAbort.signal }
137901
137975
  );
@@ -138831,7 +138905,8 @@ async function startProxy(options) {
138831
138905
  } else if (val.startsWith("https://") || val.startsWith("http://")) {
138832
138906
  const imgResp = await ssrfSafeFetch(val, {
138833
138907
  signal: clientAbort.signal,
138834
- allowPrivate: process.env.CLAWROUTER_ALLOW_PRIVATE_FETCH === "1"
138908
+ allowPrivate: process.env.CLAWROUTER_ALLOW_PRIVATE_FETCH === "1",
138909
+ viaUpstreamProxy: Boolean(upstreamProxy)
138835
138910
  });
138836
138911
  if (!imgResp.ok)
138837
138912
  throw new Error(`Failed to download ${field} from ${val}: HTTP ${imgResp.status}`);
@@ -159684,7 +159759,7 @@ var init_readBlob = __esm({
159684
159759
 
159685
159760
  // node_modules/axios/lib/helpers/formDataToStream.js
159686
159761
  import util2 from "util";
159687
- import { Readable as Readable2 } from "stream";
159762
+ import { Readable as Readable3 } from "stream";
159688
159763
  var BOUNDARY_ALPHABET, textEncoder, CRLF, CRLF_BYTES, CRLF_BYTES_COUNT, FormDataPart, formDataToStream, formDataToStream_default;
159689
159764
  var init_formDataToStream = __esm({
159690
159765
  "node_modules/axios/lib/helpers/formDataToStream.js"() {
@@ -159764,7 +159839,7 @@ var init_formDataToStream = __esm({
159764
159839
  computedHeaders["Content-Length"] = contentLength;
159765
159840
  }
159766
159841
  headersHandler && headersHandler(computedHeaders);
159767
- return Readable2.from(
159842
+ return Readable3.from(
159768
159843
  (async function* () {
159769
159844
  for (const part of parts) {
159770
159845
  yield boundaryBytes;
package/dist/index.js CHANGED
@@ -37680,7 +37680,7 @@ var require_websocket = __commonJS({
37680
37680
  var net3 = __require("net");
37681
37681
  var tls2 = __require("tls");
37682
37682
  var { randomBytes: randomBytes9, createHash: createHash6 } = __require("crypto");
37683
- var { Duplex, Readable: Readable3 } = __require("stream");
37683
+ var { Duplex, Readable: Readable4 } = __require("stream");
37684
37684
  var { URL: URL3 } = __require("url");
37685
37685
  var PerMessageDeflate2 = require_permessage_deflate();
37686
37686
  var Receiver2 = require_receiver();
@@ -76444,13 +76444,13 @@ var require_util2 = __commonJS({
76444
76444
  function TAOCheck() {
76445
76445
  return "success";
76446
76446
  }
76447
- function appendFetchMetadata(httpRequest) {
76448
- if (!isURLPotentiallyTrustworthy(requestCurrentURL(httpRequest))) {
76447
+ function appendFetchMetadata(httpRequest2) {
76448
+ if (!isURLPotentiallyTrustworthy(requestCurrentURL(httpRequest2))) {
76449
76449
  return;
76450
76450
  }
76451
76451
  let header = null;
76452
- header = httpRequest.mode;
76453
- httpRequest.headersList.set("sec-fetch-mode", header, true);
76452
+ header = httpRequest2.mode;
76453
+ httpRequest2.headersList.set("sec-fetch-mode", header, true);
76454
76454
  }
76455
76455
  function appendRequestOriginHeader(request2) {
76456
76456
  let serializedOrigin = request2.origin;
@@ -88329,7 +88329,7 @@ var require_readable = __commonJS({
88329
88329
  "use strict";
88330
88330
  var assert10 = __require("assert");
88331
88331
  var { addAbortListener } = __require("events");
88332
- var { Readable: Readable3 } = __require("stream");
88332
+ var { Readable: Readable4 } = __require("stream");
88333
88333
  var { RequestAbortedError, NotSupportedError, InvalidArgumentError, AbortError } = require_errors();
88334
88334
  var util5 = require_util();
88335
88335
  var kConsume = /* @__PURE__ */ Symbol("kConsume");
@@ -88342,7 +88342,7 @@ var require_readable = __commonJS({
88342
88342
  var kBytesRead = /* @__PURE__ */ Symbol("kBytesRead");
88343
88343
  var noop2 = () => {
88344
88344
  };
88345
- var BodyReadable = class extends Readable3 {
88345
+ var BodyReadable = class extends Readable4 {
88346
88346
  /**
88347
88347
  * @param {object} opts
88348
88348
  * @param {(this: Readable, size: number) => void} opts.resume
@@ -88741,7 +88741,7 @@ var require_api_request = __commonJS({
88741
88741
  "use strict";
88742
88742
  var assert10 = __require("assert");
88743
88743
  var { AsyncResource } = __require("async_hooks");
88744
- var { Readable: Readable3 } = require_readable();
88744
+ var { Readable: Readable4 } = require_readable();
88745
88745
  var { InvalidArgumentError, RequestAbortedError } = require_errors();
88746
88746
  var util5 = require_util();
88747
88747
  function noop2() {
@@ -88827,7 +88827,7 @@ var require_api_request = __commonJS({
88827
88827
  const parsedHeaders = headers;
88828
88828
  const contentType = parsedHeaders?.["content-type"];
88829
88829
  const contentLength = parsedHeaders?.["content-length"];
88830
- const res = new Readable3({
88830
+ const res = new Readable4({
88831
88831
  resume: () => controller.resume(),
88832
88832
  abort: (reason) => controller.abort(reason),
88833
88833
  contentType,
@@ -89203,7 +89203,7 @@ var require_api_pipeline = __commonJS({
89203
89203
  "node_modules/undici/lib/api/api-pipeline.js"(exports, module) {
89204
89204
  "use strict";
89205
89205
  var {
89206
- Readable: Readable3,
89206
+ Readable: Readable4,
89207
89207
  Duplex,
89208
89208
  PassThrough
89209
89209
  } = __require("stream");
@@ -89220,7 +89220,7 @@ var require_api_pipeline = __commonJS({
89220
89220
  function noop2() {
89221
89221
  }
89222
89222
  var kResume = /* @__PURE__ */ Symbol("resume");
89223
- var PipelineRequest = class extends Readable3 {
89223
+ var PipelineRequest = class extends Readable4 {
89224
89224
  constructor() {
89225
89225
  super({ autoDestroy: true });
89226
89226
  this[kResume] = null;
@@ -89238,7 +89238,7 @@ var require_api_pipeline = __commonJS({
89238
89238
  callback(err);
89239
89239
  }
89240
89240
  };
89241
- var PipelineResponse = class extends Readable3 {
89241
+ var PipelineResponse = class extends Readable4 {
89242
89242
  constructor(resume2) {
89243
89243
  super({ autoDestroy: true });
89244
89244
  this[kResume] = resume2;
@@ -92292,7 +92292,7 @@ var require_dump = __commonJS({
92292
92292
  var require_dns = __commonJS({
92293
92293
  "node_modules/undici/lib/interceptor/dns.js"(exports, module) {
92294
92294
  "use strict";
92295
- var { isIP: isIP2 } = __require("net");
92295
+ var { isIP: isIP3 } = __require("net");
92296
92296
  var { lookup } = __require("dns");
92297
92297
  var DecoratorHandler = require_decorator_handler();
92298
92298
  var { InvalidArgumentError, InformationalError } = require_errors();
@@ -92709,7 +92709,7 @@ var require_dns = __commonJS({
92709
92709
  return dispatch(origDispatchOpts, handler);
92710
92710
  }
92711
92711
  const origin2 = origDispatchOpts.origin.constructor === URL ? origDispatchOpts.origin : new URL(origDispatchOpts.origin);
92712
- if (isIP2(origin2.hostname) !== 0) {
92712
+ if (isIP3(origin2.hostname) !== 0) {
92713
92713
  return dispatch(origDispatchOpts, handler);
92714
92714
  }
92715
92715
  instance.runLookup(origin2, origDispatchOpts, (err, newOrigin) => {
@@ -94780,7 +94780,7 @@ var require_cache2 = __commonJS({
94780
94780
  "node_modules/undici/lib/interceptor/cache.js"(exports, module) {
94781
94781
  "use strict";
94782
94782
  var assert10 = __require("assert");
94783
- var { Readable: Readable3 } = __require("stream");
94783
+ var { Readable: Readable4 } = __require("stream");
94784
94784
  var util5 = require_util();
94785
94785
  var CacheHandler = require_cache_handler();
94786
94786
  var MemoryCacheStore = require_memory_cache_store();
@@ -94976,7 +94976,7 @@ var require_cache2 = __commonJS({
94976
94976
  return dispatch(opts, new CacheHandler(globalOpts, cacheKey3, handler));
94977
94977
  }
94978
94978
  function sendCachedValue(handler, opts, result, age, context, isStale2) {
94979
- const stream4 = util5.isStream(result.body) ? result.body : Readable3.from(result.body ?? []);
94979
+ const stream4 = util5.isStream(result.body) ? result.body : Readable4.from(result.body ?? []);
94980
94980
  assert10(!stream4.destroyed, "stream should not be destroyed");
94981
94981
  assert10(!stream4.readableDidRead, "stream should not be readableDidRead");
94982
94982
  let aborted = false;
@@ -98441,7 +98441,7 @@ var require_fetch = __commonJS({
98441
98441
  subresourceSet
98442
98442
  } = require_constants3();
98443
98443
  var EE = __require("events");
98444
- var { Readable: Readable3, pipeline: pipeline2, finished: finished2, isErrored, isReadable } = __require("stream");
98444
+ var { Readable: Readable4, pipeline: pipeline2, finished: finished2, isErrored, isReadable } = __require("stream");
98445
98445
  var { addAbortListener, bufferToLowerCasedHeaderName } = require_util();
98446
98446
  var { SocketError } = require_errors();
98447
98447
  var { dataURLProcessor, serializeAMimeType, minimizeSupportedMimeType } = require_data_url();
@@ -99070,90 +99070,90 @@ var require_fetch = __commonJS({
99070
99070
  async function httpNetworkOrCacheFetch(fetchParams, isAuthenticationFetch = false, isNewConnectionFetch = false) {
99071
99071
  const request2 = fetchParams.request;
99072
99072
  let httpFetchParams = null;
99073
- let httpRequest = null;
99073
+ let httpRequest2 = null;
99074
99074
  let response = null;
99075
99075
  const httpCache = null;
99076
99076
  const revalidatingFlag = false;
99077
99077
  if (request2.window === "no-window" && request2.redirect === "error") {
99078
99078
  httpFetchParams = fetchParams;
99079
- httpRequest = request2;
99079
+ httpRequest2 = request2;
99080
99080
  } else {
99081
99081
  if (request2.body?.source != null) {
99082
- httpRequest = cloneRequest(request2);
99082
+ httpRequest2 = cloneRequest(request2);
99083
99083
  } else {
99084
- httpRequest = cloneRequest({ ...request2, body: null });
99085
- httpRequest.body = request2.body;
99084
+ httpRequest2 = cloneRequest({ ...request2, body: null });
99085
+ httpRequest2.body = request2.body;
99086
99086
  }
99087
99087
  httpFetchParams = { ...fetchParams };
99088
- httpFetchParams.request = httpRequest;
99088
+ httpFetchParams.request = httpRequest2;
99089
99089
  }
99090
99090
  const includeCredentials = request2.credentials === "include" || request2.credentials === "same-origin" && request2.responseTainting === "basic";
99091
- const contentLength = httpRequest.body ? httpRequest.body.length : null;
99091
+ const contentLength = httpRequest2.body ? httpRequest2.body.length : null;
99092
99092
  let contentLengthHeaderValue = null;
99093
- if (httpRequest.body == null && ["POST", "PUT"].includes(httpRequest.method)) {
99093
+ if (httpRequest2.body == null && ["POST", "PUT"].includes(httpRequest2.method)) {
99094
99094
  contentLengthHeaderValue = "0";
99095
99095
  }
99096
99096
  if (contentLength != null) {
99097
99097
  contentLengthHeaderValue = isomorphicEncode(`${contentLength}`);
99098
99098
  }
99099
- if (contentLengthHeaderValue != null && !httpRequest.headersList.contains("content-length", true)) {
99100
- httpRequest.headersList.append("content-length", contentLengthHeaderValue, true);
99099
+ if (contentLengthHeaderValue != null && !httpRequest2.headersList.contains("content-length", true)) {
99100
+ httpRequest2.headersList.append("content-length", contentLengthHeaderValue, true);
99101
99101
  }
99102
- if (contentLength != null && httpRequest.keepalive) {
99102
+ if (contentLength != null && httpRequest2.keepalive) {
99103
99103
  }
99104
- if (webidl.is.URL(httpRequest.referrer)) {
99105
- httpRequest.headersList.append("referer", isomorphicEncode(httpRequest.referrer.href), true);
99104
+ if (webidl.is.URL(httpRequest2.referrer)) {
99105
+ httpRequest2.headersList.append("referer", isomorphicEncode(httpRequest2.referrer.href), true);
99106
99106
  }
99107
- appendRequestOriginHeader(httpRequest);
99108
- appendFetchMetadata(httpRequest);
99109
- if (!httpRequest.headersList.contains("user-agent", true)) {
99110
- httpRequest.headersList.append("user-agent", defaultUserAgent, true);
99107
+ appendRequestOriginHeader(httpRequest2);
99108
+ appendFetchMetadata(httpRequest2);
99109
+ if (!httpRequest2.headersList.contains("user-agent", true)) {
99110
+ httpRequest2.headersList.append("user-agent", defaultUserAgent, true);
99111
99111
  }
99112
- if (httpRequest.cache === "default" && (httpRequest.headersList.contains("if-modified-since", true) || httpRequest.headersList.contains("if-none-match", true) || httpRequest.headersList.contains("if-unmodified-since", true) || httpRequest.headersList.contains("if-match", true) || httpRequest.headersList.contains("if-range", true))) {
99113
- httpRequest.cache = "no-store";
99112
+ if (httpRequest2.cache === "default" && (httpRequest2.headersList.contains("if-modified-since", true) || httpRequest2.headersList.contains("if-none-match", true) || httpRequest2.headersList.contains("if-unmodified-since", true) || httpRequest2.headersList.contains("if-match", true) || httpRequest2.headersList.contains("if-range", true))) {
99113
+ httpRequest2.cache = "no-store";
99114
99114
  }
99115
- if (httpRequest.cache === "no-cache" && !httpRequest.preventNoCacheCacheControlHeaderModification && !httpRequest.headersList.contains("cache-control", true)) {
99116
- httpRequest.headersList.append("cache-control", "max-age=0", true);
99115
+ if (httpRequest2.cache === "no-cache" && !httpRequest2.preventNoCacheCacheControlHeaderModification && !httpRequest2.headersList.contains("cache-control", true)) {
99116
+ httpRequest2.headersList.append("cache-control", "max-age=0", true);
99117
99117
  }
99118
- if (httpRequest.cache === "no-store" || httpRequest.cache === "reload") {
99119
- if (!httpRequest.headersList.contains("pragma", true)) {
99120
- httpRequest.headersList.append("pragma", "no-cache", true);
99118
+ if (httpRequest2.cache === "no-store" || httpRequest2.cache === "reload") {
99119
+ if (!httpRequest2.headersList.contains("pragma", true)) {
99120
+ httpRequest2.headersList.append("pragma", "no-cache", true);
99121
99121
  }
99122
- if (!httpRequest.headersList.contains("cache-control", true)) {
99123
- httpRequest.headersList.append("cache-control", "no-cache", true);
99122
+ if (!httpRequest2.headersList.contains("cache-control", true)) {
99123
+ httpRequest2.headersList.append("cache-control", "no-cache", true);
99124
99124
  }
99125
99125
  }
99126
- if (httpRequest.headersList.contains("range", true)) {
99127
- httpRequest.headersList.append("accept-encoding", "identity", true);
99126
+ if (httpRequest2.headersList.contains("range", true)) {
99127
+ httpRequest2.headersList.append("accept-encoding", "identity", true);
99128
99128
  }
99129
- if (!httpRequest.headersList.contains("accept-encoding", true)) {
99130
- if (urlHasHttpsScheme(requestCurrentURL(httpRequest))) {
99131
- httpRequest.headersList.append("accept-encoding", "br, gzip, deflate, zstd", true);
99129
+ if (!httpRequest2.headersList.contains("accept-encoding", true)) {
99130
+ if (urlHasHttpsScheme(requestCurrentURL(httpRequest2))) {
99131
+ httpRequest2.headersList.append("accept-encoding", "br, gzip, deflate, zstd", true);
99132
99132
  } else {
99133
- httpRequest.headersList.append("accept-encoding", "gzip, deflate", true);
99133
+ httpRequest2.headersList.append("accept-encoding", "gzip, deflate", true);
99134
99134
  }
99135
99135
  }
99136
- httpRequest.headersList.delete("host", true);
99136
+ httpRequest2.headersList.delete("host", true);
99137
99137
  if (includeCredentials) {
99138
- if (!httpRequest.headersList.contains("authorization", true)) {
99138
+ if (!httpRequest2.headersList.contains("authorization", true)) {
99139
99139
  let authorizationValue = null;
99140
- if (hasAuthenticationEntry(httpRequest) && (httpRequest.useURLCredentials === void 0 || !includesCredentials(requestCurrentURL(httpRequest)))) {
99141
- } else if (includesCredentials(requestCurrentURL(httpRequest)) && isAuthenticationFetch) {
99142
- const { username, password } = requestCurrentURL(httpRequest);
99140
+ if (hasAuthenticationEntry(httpRequest2) && (httpRequest2.useURLCredentials === void 0 || !includesCredentials(requestCurrentURL(httpRequest2)))) {
99141
+ } else if (includesCredentials(requestCurrentURL(httpRequest2)) && isAuthenticationFetch) {
99142
+ const { username, password } = requestCurrentURL(httpRequest2);
99143
99143
  authorizationValue = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
99144
99144
  }
99145
99145
  if (authorizationValue !== null) {
99146
- httpRequest.headersList.append("Authorization", authorizationValue, false);
99146
+ httpRequest2.headersList.append("Authorization", authorizationValue, false);
99147
99147
  }
99148
99148
  }
99149
99149
  }
99150
99150
  if (httpCache == null) {
99151
- httpRequest.cache = "no-store";
99151
+ httpRequest2.cache = "no-store";
99152
99152
  }
99153
- if (httpRequest.cache !== "no-store" && httpRequest.cache !== "reload") {
99153
+ if (httpRequest2.cache !== "no-store" && httpRequest2.cache !== "reload") {
99154
99154
  }
99155
99155
  if (response == null) {
99156
- if (httpRequest.cache === "only-if-cached") {
99156
+ if (httpRequest2.cache === "only-if-cached") {
99157
99157
  return makeNetworkError("only if cached");
99158
99158
  }
99159
99159
  const forwardResponse = await httpNetworkFetch(
@@ -99161,7 +99161,7 @@ var require_fetch = __commonJS({
99161
99161
  includeCredentials,
99162
99162
  isNewConnectionFetch
99163
99163
  );
99164
- if (!safeMethodsSet.has(httpRequest.method) && forwardResponse.status >= 200 && forwardResponse.status <= 399) {
99164
+ if (!safeMethodsSet.has(httpRequest2.method) && forwardResponse.status >= 200 && forwardResponse.status <= 399) {
99165
99165
  }
99166
99166
  if (revalidatingFlag && forwardResponse.status === 304) {
99167
99167
  }
@@ -99169,12 +99169,12 @@ var require_fetch = __commonJS({
99169
99169
  response = forwardResponse;
99170
99170
  }
99171
99171
  }
99172
- response.urlList = [...httpRequest.urlList];
99173
- if (httpRequest.headersList.contains("range", true)) {
99172
+ response.urlList = [...httpRequest2.urlList];
99173
+ if (httpRequest2.headersList.contains("range", true)) {
99174
99174
  response.rangeRequested = true;
99175
99175
  }
99176
99176
  response.requestIncludesCredentials = includeCredentials;
99177
- if (response.status === 401 && httpRequest.responseTainting !== "cors" && includeCredentials && (request2.useURLCredentials !== void 0 || isTraversableNavigable(request2.traversableForUserPrompts))) {
99177
+ if (response.status === 401 && httpRequest2.responseTainting !== "cors" && includeCredentials && (request2.useURLCredentials !== void 0 || isTraversableNavigable(request2.traversableForUserPrompts))) {
99178
99178
  if (request2.body != null) {
99179
99179
  if (request2.body.source == null) {
99180
99180
  return response;
@@ -99471,7 +99471,7 @@ var require_fetch = __commonJS({
99471
99471
  }
99472
99472
  }
99473
99473
  }
99474
- this.body = new Readable3({ read: () => controller.resume() });
99474
+ this.body = new Readable4({ read: () => controller.resume() });
99475
99475
  const onError = (err) => this.onResponseError(controller, err);
99476
99476
  resolve2({
99477
99477
  status: status2,
@@ -243362,7 +243362,7 @@ function fingerprint(view) {
243362
243362
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
243363
243363
  import { createHmac } from "crypto";
243364
243364
  import { createServer } from "http";
243365
- import { finished, Readable } from "stream";
243365
+ import { finished, Readable as Readable2 } from "stream";
243366
243366
  import { pipeline } from "stream/promises";
243367
243367
  import { homedir as homedir13 } from "os";
243368
243368
  import { join as join17 } from "path";
@@ -255465,6 +255465,7 @@ function formatDuration(seconds) {
255465
255465
 
255466
255466
  // src/payment-preauth.ts
255467
255467
  var DEFAULT_TTL_MS = 36e5;
255468
+ var PREAUTH_OVERPAY_TOLERANCE = 0.03;
255468
255469
  var PaymentInDoubtError = class extends Error {
255469
255470
  constructor(cause) {
255470
255471
  super(
@@ -255538,7 +255539,7 @@ function createPayFetchWithPreAuth(baseFetch, client, ttlMs = DEFAULT_TTL_MS, op
255538
255539
  }
255539
255540
  };
255540
255541
  const cached = !options?.skipPreAuth ? cache2.get(cacheKey3) : void 0;
255541
- const preAuthCovers = cached !== void 0 && Date.now() - cached.cachedAt < ttlMs && cached.coverMicros !== void 0 && needMicros !== void 0 && needMicros <= cached.coverMicros;
255542
+ const preAuthCovers = cached !== void 0 && Date.now() - cached.cachedAt < ttlMs && cached.coverMicros !== void 0 && needMicros !== void 0 && needMicros <= cached.coverMicros && cached.coverMicros <= needMicros * (1 + PREAUTH_OVERPAY_TOLERANCE);
255542
255543
  if (preAuthCovers) {
255543
255544
  let paymentInFlight = false;
255544
255545
  try {
@@ -263515,6 +263516,11 @@ ${lines.join("\n")}`;
263515
263516
  };
263516
263517
 
263517
263518
  // src/local-guard.ts
263519
+ import { request as httpRequest } from "http";
263520
+ import { request as httpsRequest } from "https";
263521
+ import { lookup as dnsLookup } from "dns/promises";
263522
+ import { isIP } from "net";
263523
+ import { Readable } from "stream";
263518
263524
  import { closeSync as closeSync3, fstatSync as fstatSync2, openSync as openSync3, readSync as readSync2, realpathSync } from "fs";
263519
263525
  import { homedir as homedir11 } from "os";
263520
263526
  import { join as join15 } from "path";
@@ -263592,18 +263598,86 @@ function isBlockedSsrfHost(hostname) {
263592
263598
  }
263593
263599
  return false;
263594
263600
  }
263601
+ var systemResolver = (hostname) => dnsLookup(hostname, { all: true, verbatim: true });
263602
+ async function resolvePublicAddresses(hostname, resolve2 = systemResolver) {
263603
+ const h = hostname.replace(/^\[/, "").replace(/\]$/, "");
263604
+ const family = isIP(h);
263605
+ const addrs = family ? [{ address: h, family }] : await resolve2(h);
263606
+ if (addrs.length === 0) throw new Error(`could not resolve ${hostname}`);
263607
+ const blocked = addrs.find((a) => isBlockedSsrfHost(a.address));
263608
+ if (blocked) {
263609
+ throw new Error(
263610
+ `refusing to fetch ${hostname}: it resolves to a private/loopback/metadata address (${blocked.address})`
263611
+ );
263612
+ }
263613
+ return addrs;
263614
+ }
263615
+ function pinnedLookup(addrs) {
263616
+ return (_hostname, options, callback) => {
263617
+ if (options.all) {
263618
+ callback(null, addrs);
263619
+ } else {
263620
+ callback(null, addrs[0].address, addrs[0].family);
263621
+ }
263622
+ };
263623
+ }
263624
+ var NULL_BODY_STATUSES = /* @__PURE__ */ new Set([101, 103, 204, 205, 304]);
263625
+ function pinnedFetch(url2, addrs, init2 = {}) {
263626
+ const method = (init2.method ?? "GET").toUpperCase();
263627
+ const body = init2.body;
263628
+ if (body != null && typeof body !== "string" && !(body instanceof Uint8Array)) {
263629
+ return Promise.reject(new TypeError("pinnedFetch supports only string or byte bodies"));
263630
+ }
263631
+ const headers = Object.fromEntries(new Headers(init2.headers));
263632
+ const send = url2.protocol === "https:" ? httpsRequest : httpRequest;
263633
+ return new Promise((resolve2, reject) => {
263634
+ const req = send(
263635
+ url2,
263636
+ { method, headers, lookup: pinnedLookup(addrs), signal: init2.signal ?? void 0 },
263637
+ (res) => {
263638
+ const status2 = res.statusCode ?? 0;
263639
+ if (status2 < 200 || status2 > 599) {
263640
+ res.destroy();
263641
+ reject(new Error(`unexpected HTTP status ${status2} from ${url2.host}`));
263642
+ return;
263643
+ }
263644
+ const resHeaders = new Headers();
263645
+ for (let i = 0; i + 1 < res.rawHeaders.length; i += 2) {
263646
+ resHeaders.append(res.rawHeaders[i], res.rawHeaders[i + 1]);
263647
+ }
263648
+ const noBody = method === "HEAD" || NULL_BODY_STATUSES.has(status2);
263649
+ if (noBody) res.resume();
263650
+ resolve2(
263651
+ new Response(noBody ? null : Readable.toWeb(res), {
263652
+ status: status2,
263653
+ statusText: res.statusMessage,
263654
+ headers: resHeaders
263655
+ })
263656
+ );
263657
+ }
263658
+ );
263659
+ req.on("error", reject);
263660
+ req.end(body ?? void 0);
263661
+ });
263662
+ }
263595
263663
  async function ssrfSafeFetch(url2, init2 = {}, maxHops = 5) {
263596
- const { allowPrivate, ...fetchInit } = init2;
263664
+ const { allowPrivate, viaUpstreamProxy, resolve: resolve2, ...fetchInit } = init2;
263597
263665
  let current = url2;
263598
263666
  for (let hop = 0; hop <= maxHops; hop++) {
263599
263667
  const u = new URL(current);
263600
263668
  if (u.protocol !== "http:" && u.protocol !== "https:") {
263601
263669
  throw new Error(`refusing to fetch non-http(s) URL (${u.protocol})`);
263602
263670
  }
263603
- if (!allowPrivate && isBlockedSsrfHost(u.hostname)) {
263604
- throw new Error(`refusing to fetch a private/loopback/metadata address: ${u.hostname}`);
263671
+ let res;
263672
+ if (allowPrivate) {
263673
+ res = await fetch(current, { ...fetchInit, redirect: "manual" });
263674
+ } else {
263675
+ if (isBlockedSsrfHost(u.hostname)) {
263676
+ throw new Error(`refusing to fetch a private/loopback/metadata address: ${u.hostname}`);
263677
+ }
263678
+ const addrs = await resolvePublicAddresses(u.hostname, resolve2);
263679
+ res = viaUpstreamProxy ? await fetch(current, { ...fetchInit, redirect: "manual" }) : await pinnedFetch(u, addrs, fetchInit);
263605
263680
  }
263606
- const res = await fetch(current, { ...fetchInit, redirect: "manual" });
263607
263681
  if (res.status >= 300 && res.status < 400) {
263608
263682
  const loc = res.headers.get("location");
263609
263683
  if (!loc) return res;
@@ -265620,7 +265694,7 @@ async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPayment
265620
265694
  if (accountPassthrough) {
265621
265695
  if (upstream.body) {
265622
265696
  await pipeline(
265623
- Readable.fromWeb(upstream.body),
265697
+ Readable2.fromWeb(upstream.body),
265624
265698
  res,
265625
265699
  { signal: clientAbort.signal }
265626
265700
  );
@@ -266556,7 +266630,8 @@ async function startProxy(options) {
266556
266630
  } else if (val.startsWith("https://") || val.startsWith("http://")) {
266557
266631
  const imgResp = await ssrfSafeFetch(val, {
266558
266632
  signal: clientAbort.signal,
266559
- allowPrivate: process.env.CLAWROUTER_ALLOW_PRIVATE_FETCH === "1"
266633
+ allowPrivate: process.env.CLAWROUTER_ALLOW_PRIVATE_FETCH === "1",
266634
+ viaUpstreamProxy: Boolean(upstreamProxy)
266560
266635
  });
266561
266636
  if (!imgResp.ok)
266562
266637
  throw new Error(`Failed to download ${field} from ${val}: HTTP ${imgResp.status}`);
@@ -274237,7 +274312,7 @@ import { EventEmitter } from "events";
274237
274312
 
274238
274313
  // node_modules/axios/lib/helpers/formDataToStream.js
274239
274314
  import util2 from "util";
274240
- import { Readable as Readable2 } from "stream";
274315
+ import { Readable as Readable3 } from "stream";
274241
274316
 
274242
274317
  // node_modules/axios/lib/helpers/readBlob.js
274243
274318
  var { asyncIterator } = Symbol;
@@ -274327,7 +274402,7 @@ var formDataToStream = (form, headersHandler, options) => {
274327
274402
  computedHeaders["Content-Length"] = contentLength;
274328
274403
  }
274329
274404
  headersHandler && headersHandler(computedHeaders);
274330
- return Readable2.from(
274405
+ return Readable3.from(
274331
274406
  (async function* () {
274332
274407
  for (const part of parts) {
274333
274408
  yield boundaryBytes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockrun/clawrouter",
3
- "version": "0.12.298",
3
+ "version": "0.12.300",
4
4
  "description": "Smart LLM router — save 84% on inference costs. 90 models (7 free). Pay by credit card (API key) or USDC (x402 wallet).",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",