@xano/sdk 1.0.0

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 (336) hide show
  1. package/CHANGELOG.md +788 -0
  2. package/LICENSE +21 -0
  3. package/README.md +541 -0
  4. package/dist/.build-fingerprint +1 -0
  5. package/dist/agent-file-refresh-ZEMK33AX.js +38 -0
  6. package/dist/answer-shape-K67TAAPY.js +33 -0
  7. package/dist/backend-dir-2U53CIAL.js +39 -0
  8. package/dist/backend-slot-7QXW2QYC.js +28 -0
  9. package/dist/bin.d.ts +1 -0
  10. package/dist/bin.js +255 -0
  11. package/dist/branch-NCRNWQBQ.js +61 -0
  12. package/dist/branch-commands-ACIRCEQJ.js +79 -0
  13. package/dist/bundle.d.ts +223 -0
  14. package/dist/bundle.js +153 -0
  15. package/dist/capture-WAXPD7DR.js +41 -0
  16. package/dist/chunk-27GWNP2I.js +23 -0
  17. package/dist/chunk-2CMJZDCL.js +177 -0
  18. package/dist/chunk-2EZ3ZJZZ.js +302 -0
  19. package/dist/chunk-2GNFDZA2.js +390 -0
  20. package/dist/chunk-2MNLRIOX.js +11 -0
  21. package/dist/chunk-2TFRR6JV.js +104 -0
  22. package/dist/chunk-2ZUEE4EK.js +408 -0
  23. package/dist/chunk-36JUQZVQ.js +22 -0
  24. package/dist/chunk-3DRXWLJ5.js +83 -0
  25. package/dist/chunk-3JDU4VRL.js +137 -0
  26. package/dist/chunk-3KZCLN4Z.js +241 -0
  27. package/dist/chunk-3LDULPRE.js +168 -0
  28. package/dist/chunk-3M4LFYNQ.js +111 -0
  29. package/dist/chunk-3XI5CTUE.js +22 -0
  30. package/dist/chunk-3YW23UFH.js +50 -0
  31. package/dist/chunk-45UIT5T2.js +862 -0
  32. package/dist/chunk-4BW2SG2N.js +46 -0
  33. package/dist/chunk-4KHEMC32.js +79 -0
  34. package/dist/chunk-4POWHT2K.js +321 -0
  35. package/dist/chunk-4Q2GN7NW.js +192 -0
  36. package/dist/chunk-5A2PLKZB.js +70 -0
  37. package/dist/chunk-5FBYXK5P.js +159 -0
  38. package/dist/chunk-5HVXPTU4.js +323 -0
  39. package/dist/chunk-5QU2P5CA.js +304 -0
  40. package/dist/chunk-5V5XU32D.js +191 -0
  41. package/dist/chunk-5ZSO4T2G.js +10 -0
  42. package/dist/chunk-622HUCZT.js +10 -0
  43. package/dist/chunk-65HWOMGV.js +177 -0
  44. package/dist/chunk-6ELFRN3P.js +365 -0
  45. package/dist/chunk-6HO3NCSJ.js +1055 -0
  46. package/dist/chunk-6IUMKYWI.js +457 -0
  47. package/dist/chunk-6KEB3C3U.js +65 -0
  48. package/dist/chunk-6MR5TI5V.js +987 -0
  49. package/dist/chunk-6NNTNXN7.js +157 -0
  50. package/dist/chunk-6PBR37LN.js +97 -0
  51. package/dist/chunk-6QXLMMT3.js +155 -0
  52. package/dist/chunk-6ZGJCBZS.js +224 -0
  53. package/dist/chunk-73IVBKMA.js +250 -0
  54. package/dist/chunk-7G3CTU7I.js +286 -0
  55. package/dist/chunk-A3UDOZB6.js +222 -0
  56. package/dist/chunk-A6E77GDW.js +85 -0
  57. package/dist/chunk-AHJWCYQO.js +660 -0
  58. package/dist/chunk-AKNCFVKZ.js +36 -0
  59. package/dist/chunk-AM4FKL4J.js +237 -0
  60. package/dist/chunk-AN6BL47T.js +26 -0
  61. package/dist/chunk-AOSYVMUS.js +147 -0
  62. package/dist/chunk-APWXY5SL.js +182 -0
  63. package/dist/chunk-AWEIIR2U.js +218 -0
  64. package/dist/chunk-AWQ7BQTQ.js +73 -0
  65. package/dist/chunk-BAUZOWGX.js +393 -0
  66. package/dist/chunk-BHACRS6R.js +940 -0
  67. package/dist/chunk-BWPNT55T.js +680 -0
  68. package/dist/chunk-C6RIMRU4.js +187 -0
  69. package/dist/chunk-CGVQZXYG.js +263 -0
  70. package/dist/chunk-CIMQZRRE.js +162 -0
  71. package/dist/chunk-CJHQKJU4.js +113 -0
  72. package/dist/chunk-CTMO46PK.js +1897 -0
  73. package/dist/chunk-D53J55C5.js +106 -0
  74. package/dist/chunk-D7VGG3WS.js +90 -0
  75. package/dist/chunk-DAD3ONIS.js +245 -0
  76. package/dist/chunk-DGWHAVRG.js +162 -0
  77. package/dist/chunk-DX4QR4B2.js +483 -0
  78. package/dist/chunk-EHP3WPEG.js +21 -0
  79. package/dist/chunk-ELP2HMXC.js +432 -0
  80. package/dist/chunk-ERVIASGL.js +21 -0
  81. package/dist/chunk-ET4Y4XCB.js +865 -0
  82. package/dist/chunk-ETO6LGLD.js +4750 -0
  83. package/dist/chunk-EUUUMY7J.js +24 -0
  84. package/dist/chunk-FQJ2TTHP.js +218 -0
  85. package/dist/chunk-FQOF7E2E.js +133 -0
  86. package/dist/chunk-FWD7AL6N.js +21 -0
  87. package/dist/chunk-FXFV3KU6.js +138 -0
  88. package/dist/chunk-FXRQOWTE.js +171 -0
  89. package/dist/chunk-G554NZNI.js +210 -0
  90. package/dist/chunk-GSGDWJB5.js +13 -0
  91. package/dist/chunk-H2V3N23D.js +66 -0
  92. package/dist/chunk-H5C7MBCR.js +159 -0
  93. package/dist/chunk-HHN734BU.js +371 -0
  94. package/dist/chunk-HRVBLCVJ.js +243 -0
  95. package/dist/chunk-HVRUQXGD.js +1264 -0
  96. package/dist/chunk-I4F5TCJH.js +130 -0
  97. package/dist/chunk-IDOTCMDV.js +57 -0
  98. package/dist/chunk-IGOLHKWF.js +427 -0
  99. package/dist/chunk-JGE7CAWB.js +148 -0
  100. package/dist/chunk-JTUKLCXS.js +92 -0
  101. package/dist/chunk-JZGVVJSI.js +66 -0
  102. package/dist/chunk-K4WX6UUU.js +923 -0
  103. package/dist/chunk-K5BBGTYZ.js +217 -0
  104. package/dist/chunk-K6RXBGUB.js +427 -0
  105. package/dist/chunk-KTSDSD36.js +165 -0
  106. package/dist/chunk-L5JYN3NM.js +1038 -0
  107. package/dist/chunk-LIEOKXKD.js +354 -0
  108. package/dist/chunk-LLNZGEFK.js +51 -0
  109. package/dist/chunk-LPECSJ2J.js +55 -0
  110. package/dist/chunk-LQCMKAJ7.js +413 -0
  111. package/dist/chunk-LRN4ZJCQ.js +1145 -0
  112. package/dist/chunk-MJ4ZVHKN.js +17 -0
  113. package/dist/chunk-MXHSQOC4.js +3061 -0
  114. package/dist/chunk-N5GKCWE5.js +367 -0
  115. package/dist/chunk-NA3PXMEF.js +10 -0
  116. package/dist/chunk-NAQDMEHV.js +76 -0
  117. package/dist/chunk-NWKXM5RD.js +109 -0
  118. package/dist/chunk-NZOWWHU5.js +196 -0
  119. package/dist/chunk-OHX6MIUZ.js +184 -0
  120. package/dist/chunk-OOXWYAA4.js +15 -0
  121. package/dist/chunk-OPHB5ORL.js +1990 -0
  122. package/dist/chunk-OXA6PSZT.js +1725 -0
  123. package/dist/chunk-P2HAGF6H.js +73 -0
  124. package/dist/chunk-PTHVMXZI.js +3036 -0
  125. package/dist/chunk-PZ5AY32C.js +10 -0
  126. package/dist/chunk-Q325JKYB.js +189 -0
  127. package/dist/chunk-Q7DGTQ5G.js +255 -0
  128. package/dist/chunk-QCNYHBL7.js +601 -0
  129. package/dist/chunk-QO6BVGS5.js +145 -0
  130. package/dist/chunk-QXTKSOFI.js +76 -0
  131. package/dist/chunk-ROY4YAEQ.js +66 -0
  132. package/dist/chunk-RTJRZFFY.js +447 -0
  133. package/dist/chunk-SAVJUS2X.js +128 -0
  134. package/dist/chunk-SFLHVCKX.js +173 -0
  135. package/dist/chunk-STSFNCY4.js +127 -0
  136. package/dist/chunk-SWBF2LTK.js +170 -0
  137. package/dist/chunk-T5KELPAR.js +109 -0
  138. package/dist/chunk-TFFGOFVZ.js +1481 -0
  139. package/dist/chunk-TLHIFK4M.js +256 -0
  140. package/dist/chunk-TLQE7ZN3.js +156 -0
  141. package/dist/chunk-TPHWJ7UG.js +1981 -0
  142. package/dist/chunk-TRJCALW3.js +291 -0
  143. package/dist/chunk-TTEIYD5P.js +133 -0
  144. package/dist/chunk-U64O2MXY.js +407 -0
  145. package/dist/chunk-UCQ2VUTG.js +630 -0
  146. package/dist/chunk-UTI5G6OJ.js +44 -0
  147. package/dist/chunk-UVHDMMIP.js +188 -0
  148. package/dist/chunk-UZ6QQWJT.js +120 -0
  149. package/dist/chunk-VAX3UZOY.js +686 -0
  150. package/dist/chunk-W3HBUKFO.js +57 -0
  151. package/dist/chunk-WKQLGKCL.js +2823 -0
  152. package/dist/chunk-WQFNASK4.js +560 -0
  153. package/dist/chunk-WRBKLYSJ.js +224 -0
  154. package/dist/chunk-WTPZRR4I.js +17 -0
  155. package/dist/chunk-WZDJ2LLX.js +2385 -0
  156. package/dist/chunk-X2NL4KUO.js +14 -0
  157. package/dist/chunk-X4DVXBFY.js +38 -0
  158. package/dist/chunk-X677DMZM.js +7050 -0
  159. package/dist/chunk-XBKMWY7F.js +612 -0
  160. package/dist/chunk-XNCP4KYO.js +78 -0
  161. package/dist/chunk-XVBGLISP.js +35 -0
  162. package/dist/chunk-XW6KISBB.js +1192 -0
  163. package/dist/chunk-YGZHHAAB.js +8156 -0
  164. package/dist/chunk-YHEFQUQB.js +186 -0
  165. package/dist/chunk-YLDALTWZ.js +653 -0
  166. package/dist/chunk-YU4HSQW7.js +229 -0
  167. package/dist/chunk-ZDPP6RKB.js +27 -0
  168. package/dist/chunk-ZFDZKNGG.js +194 -0
  169. package/dist/chunk-ZIY7RNUT.js +49 -0
  170. package/dist/chunk-ZYJP6BOO.js +30 -0
  171. package/dist/cli.d.ts +1111 -0
  172. package/dist/cli.js +165 -0
  173. package/dist/codegen-command-VR2GWR6C.js +173 -0
  174. package/dist/codegen.d.ts +115 -0
  175. package/dist/codegen.js +19 -0
  176. package/dist/commands-GJMJ4XYZ.js +76 -0
  177. package/dist/completion-ZQLO6PB6.js +738 -0
  178. package/dist/config-MBYGCAFK.js +136 -0
  179. package/dist/deploy-command-JVZPWHMK.js +180 -0
  180. package/dist/deploy-source-3W45CXMP.js +81 -0
  181. package/dist/dev-env-sync-HN44K4D5.js +188 -0
  182. package/dist/diagnostics-UEAAHWVF.js +30 -0
  183. package/dist/display-name-hint-ABBPVQIR.js +46 -0
  184. package/dist/emit-CyhBDxli.d.ts +7007 -0
  185. package/dist/env-example-refresh-ET5HMMDW.js +126 -0
  186. package/dist/env-pull-command-TNMNB3CV.js +323 -0
  187. package/dist/env-target-O4HRKTGZ.js +40 -0
  188. package/dist/env-var-command-ZIRDCAZA.js +427 -0
  189. package/dist/ephemeral-P7F3PMAD.js +54 -0
  190. package/dist/ephemeral-command-CLJJKOFT.js +592 -0
  191. package/dist/ephemeral-state-PYCBSOGX.js +61 -0
  192. package/dist/file-lock-PT74GWYW.js +10 -0
  193. package/dist/generate-command-AYEOLA26.js +510 -0
  194. package/dist/gitignore-3SJ3WNRG.js +51 -0
  195. package/dist/guards-4EBNCZDC.js +162 -0
  196. package/dist/hosted-file-PSHJB3HX.js +28 -0
  197. package/dist/impersonate-command-5JAS3WSV.js +152 -0
  198. package/dist/import-EQSFU6JC.js +19 -0
  199. package/dist/index.d.ts +5470 -0
  200. package/dist/index.js +371 -0
  201. package/dist/init-command-X2FR4UAK.js +125 -0
  202. package/dist/init-modules-RY7UHAOL.js +50 -0
  203. package/dist/init-web-M4YZSZK6.js +42 -0
  204. package/dist/internal.d.ts +179 -0
  205. package/dist/internal.js +2634 -0
  206. package/dist/io-RC56QSN5.js +26 -0
  207. package/dist/keep-data-merge-7MPLVAVI.js +1119 -0
  208. package/dist/lambda-config-reconcile-OUZ4OYAB.js +116 -0
  209. package/dist/landed-753YLJ5H.js +40 -0
  210. package/dist/landing-record-SU52SKXU.js +61 -0
  211. package/dist/live-diff-YONW2VI3.js +65 -0
  212. package/dist/local-engine-choice-CH5X3W7E.js +79 -0
  213. package/dist/local-engine-command-Z3OL7VOD.js +766 -0
  214. package/dist/local-engine-config-5U3SC434.js +39 -0
  215. package/dist/local-engine-deploy-PCQ4SNPB.js +39 -0
  216. package/dist/local-engine-process-EHTE5LLU.js +63 -0
  217. package/dist/local-engine-state-A4FTRFAF.js +32 -0
  218. package/dist/local-engine-update-check-6L645VBL.js +162 -0
  219. package/dist/lock-MRRHW5GS.js +87 -0
  220. package/dist/lock-commands-2QG25HL7.js +963 -0
  221. package/dist/login-command-QPYRWOXZ.js +538 -0
  222. package/dist/logout-command-WOIQLDP6.js +56 -0
  223. package/dist/loop-ALIZLOSX.js +191 -0
  224. package/dist/marketplace-command-K5YL42NO.js +977 -0
  225. package/dist/marketplace-resolve-LY32JES5.js +40 -0
  226. package/dist/meta-client-YXGPW53C.js +244 -0
  227. package/dist/module-manifest-JFGHU2TC.js +38 -0
  228. package/dist/near-key.d.ts +13 -0
  229. package/dist/near-key.js +8 -0
  230. package/dist/node.d.ts +95 -0
  231. package/dist/node.js +969 -0
  232. package/dist/operation-outcome-TZSESNC7.js +51 -0
  233. package/dist/output-target-AL3I44GG.js +47 -0
  234. package/dist/package-manager-PRAHUSCN.js +34 -0
  235. package/dist/plan-disclosure-6G4MLZKD.js +114 -0
  236. package/dist/plugin-contributions-WRNY5ABG.js +28 -0
  237. package/dist/plugin-prompt-4VA3B4FF.js +20 -0
  238. package/dist/plugin-questions-JTSYA74Q.js +24 -0
  239. package/dist/plugin.d.ts +396 -0
  240. package/dist/plugin.js +1 -0
  241. package/dist/preflight-command-2T5Z36MI.js +321 -0
  242. package/dist/profile-command-AH3PIM4X.js +800 -0
  243. package/dist/profile-pointer-GRWJU3YD.js +31 -0
  244. package/dist/profile-select-74TRDEPD.js +23 -0
  245. package/dist/project-dependency-KSH63ODT.js +35 -0
  246. package/dist/project-sdk-REPPOIBD.js +24 -0
  247. package/dist/promote-command-PCWT5N6S.js +140 -0
  248. package/dist/prompt-II3F3LND.js +27 -0
  249. package/dist/publish-command-TTDUUJI5.js +414 -0
  250. package/dist/pull-command-VBO7C4S3.js +161 -0
  251. package/dist/pull-superseded-GJWLG7XO.js +20 -0
  252. package/dist/release-command-N4ZPKMHR.js +146 -0
  253. package/dist/release-ns-command-3KFDMZ6G.js +1745 -0
  254. package/dist/response-HlXahv4K.d.ts +925 -0
  255. package/dist/retry-command-AS5EVREX.js +34 -0
  256. package/dist/routes-manifest-SNWUVBT6.js +43 -0
  257. package/dist/runtime-XETHVF33.js +14 -0
  258. package/dist/scaffold.d.ts +1911 -0
  259. package/dist/scaffold.js +107 -0
  260. package/dist/secrets-file-LVZD6IIM.js +56 -0
  261. package/dist/secrets-fill-command-TTCPSVXN.js +218 -0
  262. package/dist/seed-VU7C3QYS.js +46 -0
  263. package/dist/sent-writes-N7X4OVXT.js +22 -0
  264. package/dist/source-resolve-S2XA3XZF.js +83 -0
  265. package/dist/static-host-B2Y6AG3W.js +352 -0
  266. package/dist/status-command-RUDXTF7I.js +554 -0
  267. package/dist/store-6VNCD4D7.js +20 -0
  268. package/dist/store-ARM6VMO5.js +98 -0
  269. package/dist/store-DuYfzKlW.d.ts +3144 -0
  270. package/dist/suggest-6FOOYKHG.js +12 -0
  271. package/dist/sveltekit.d.ts +154 -0
  272. package/dist/sveltekit.js +109 -0
  273. package/dist/sync-contributions-UCEROO26.js +415 -0
  274. package/dist/table-ENAVMITR.js +42 -0
  275. package/dist/tables-command-GUPXXSB5.js +156 -0
  276. package/dist/tenant-IIHPOHGP.js +42 -0
  277. package/dist/tenant-command-D5HGI2X4.js +965 -0
  278. package/dist/test-command-TIKZMDPZ.js +476 -0
  279. package/dist/tests-BGGIOK3L.js +26 -0
  280. package/dist/token-NICJUTMT.js +72 -0
  281. package/dist/toolchain-hooks-GHKLUJOH.js +96 -0
  282. package/dist/toolchain-modules-5EAZUONB.js +24 -0
  283. package/dist/tracked-backend-EIBUMDZA.js +80 -0
  284. package/dist/update-check-2W2NLDTC.js +136 -0
  285. package/dist/upgrade-command-QHNFWM7O.js +531 -0
  286. package/dist/verify-rollout-DRCK5TDD.js +61 -0
  287. package/dist/whoami-command-STGINTXH.js +51 -0
  288. package/dist/workspace-2CLHU2LK.js +93 -0
  289. package/dist/workspace-binding-56PIIPCP.js +54 -0
  290. package/dist/workspace-command-JEXQETJD.js +535 -0
  291. package/dist/workspace-env-F2RFMSC5.js +70 -0
  292. package/dist/workspace-export-OJSH2HIW.js +21 -0
  293. package/dist/writable-VA7OISSK.js +25 -0
  294. package/dist/xano-ICFUSPFR.js +42 -0
  295. package/dist/xanosdk-import-GN42TO2B.js +56 -0
  296. package/dist/xanosdk-project-S6DXJONQ.js +41 -0
  297. package/dist/xdo-CxDr2Q1C.d.ts +1371 -0
  298. package/guides/README.md +33 -0
  299. package/guides/authoring.md +921 -0
  300. package/guides/cli.md +401 -0
  301. package/guides/codegen.md +177 -0
  302. package/guides/codes.md +404 -0
  303. package/guides/coverage.md +74 -0
  304. package/guides/deploying.md +1525 -0
  305. package/guides/environment.md +400 -0
  306. package/guides/marketplace.md +415 -0
  307. package/guides/module-contract.md +558 -0
  308. package/guides/object-kinds.md +433 -0
  309. package/guides/project-structure.md +43 -0
  310. package/guides/scaffold.md +351 -0
  311. package/guides/typed-frontend.md +242 -0
  312. package/llms/client.md +63 -0
  313. package/llms/errors.md +56 -0
  314. package/llms/fields.md +176 -0
  315. package/llms/filters.md +183 -0
  316. package/llms/kinds-agent-mcp.md +30 -0
  317. package/llms/kinds-core.md +100 -0
  318. package/llms/kinds-knowledge.md +20 -0
  319. package/llms/kinds-realtime.md +76 -0
  320. package/llms/lambda.md +94 -0
  321. package/llms/legacy.md +44 -0
  322. package/llms/lock.md +40 -0
  323. package/llms/object-kinds.md +34 -0
  324. package/llms/statements-calls.md +62 -0
  325. package/llms/statements-catalog.md +320 -0
  326. package/llms/statements-data.md +66 -0
  327. package/llms/statements-runtime.md +68 -0
  328. package/llms/tests.md +59 -0
  329. package/llms/toolchain.md +16 -0
  330. package/llms/triggers.md +36 -0
  331. package/llms/values.md +29 -0
  332. package/llms-full.txt +2085 -0
  333. package/llms.txt +493 -0
  334. package/manifest.json +11224 -0
  335. package/package.json +162 -0
  336. package/types/lambda-globals.d.ts +129 -0
@@ -0,0 +1,653 @@
1
+ import {
2
+ profileSourceLabel
3
+ } from "./chunk-QXTKSOFI.js";
4
+ import {
5
+ readEnvVar
6
+ } from "./chunk-X2NL4KUO.js";
7
+ import {
8
+ UsageError
9
+ } from "./chunk-PTHVMXZI.js";
10
+
11
+ // src/util/last-credential.ts
12
+ import { AsyncLocalStorage } from "async_hooks";
13
+ var runScope = new AsyncLocalStorage();
14
+ function inCredentialScope(fn) {
15
+ return runScope.run({}, fn);
16
+ }
17
+ function noteResolvedAuth(auth, args) {
18
+ const state = runScope.getStore();
19
+ if (state !== void 0) state.last = { auth, args };
20
+ }
21
+ function lastResolvedAuth() {
22
+ return runScope.getStore()?.last;
23
+ }
24
+ async function noteMetaAnswer(url, res) {
25
+ if (res.status !== 404 && res.status !== 500) return;
26
+ const state = runScope.getStore();
27
+ const auth = state?.last?.auth;
28
+ if (state === void 0 || auth === void 0 || state.workspaceMiss === true) return;
29
+ try {
30
+ const at = new URL(url);
31
+ if (at.origin !== new URL(auth.instance).origin) return;
32
+ const id = /^\/api:meta\/workspace\/(\d+)(?:\/|$)/.exec(at.pathname)?.[1];
33
+ if (id === void 0 || Number(id) !== auth.workspaceId) return;
34
+ const text = await res.clone().text();
35
+ let said = text.trim();
36
+ try {
37
+ const message = JSON.parse(said).message;
38
+ said = typeof message === "string" ? message.trim() : "";
39
+ } catch {
40
+ }
41
+ if (said === "" || /^invalid workspace\.?$/i.test(said)) state.workspaceMiss = true;
42
+ } catch {
43
+ }
44
+ }
45
+ function pinnedWorkspaceMissed() {
46
+ return runScope.getStore()?.workspaceMiss === true;
47
+ }
48
+
49
+ // src/util/http.ts
50
+ import { isIP } from "net";
51
+ var MAX_DETAIL_CHARS = 500;
52
+ function debugEnabled() {
53
+ const v = readEnvVar("XANOSDK_DEBUG")?.trim();
54
+ return v !== void 0 && v !== "0" && v.toLowerCase() !== "false";
55
+ }
56
+ function serverMessage(text) {
57
+ let parsed;
58
+ let isJson = false;
59
+ try {
60
+ parsed = JSON.parse(text);
61
+ isJson = true;
62
+ } catch {
63
+ }
64
+ if (isJson) {
65
+ const message = parsed !== null && typeof parsed === "object" && !Array.isArray(parsed) ? parsed.message : void 0;
66
+ if (typeof message !== "string") return void 0;
67
+ const flat2 = message.replace(/\s+/g, " ").trim();
68
+ if (flat2 === "") return void 0;
69
+ return flat2.length > MAX_DETAIL_CHARS ? `${flat2.slice(0, MAX_DETAIL_CHARS)}\u2026` : flat2;
70
+ }
71
+ const page = markupSummary(text);
72
+ if (page !== void 0) return page;
73
+ const first = text.split(/\r?\n/).map((l) => l.trim()).find((l) => l !== "");
74
+ if (first === void 0) return void 0;
75
+ const flat = first.replace(/\s+/g, " ");
76
+ return flat.length > MAX_LINE_CHARS ? `${flat.slice(0, MAX_LINE_CHARS)}\u2026` : flat;
77
+ }
78
+ var MAX_LINE_CHARS = 200;
79
+ function markupSummary(text) {
80
+ const head = text.trimStart().slice(0, 512).toLowerCase();
81
+ const isMarkup = head.startsWith("<!doctype html") || head.startsWith("<html") || /<(html|head|body|title)[\s>]/.test(head);
82
+ if (!isMarkup) return void 0;
83
+ const title = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(text)?.[1]?.replace(/\s+/g, " ").trim();
84
+ const bare = text.replace(/<(script|style)[\s\S]*?<\/\1>/gi, " ").replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
85
+ const shown = title !== void 0 && title !== "" ? title.slice(0, 120) : bare !== "" && bare.length <= 120 ? bare : void 0;
86
+ return shown !== void 0 ? `an HTML page ("${shown}"), not an API response` : "an HTML page, not an API response";
87
+ }
88
+ function notJsonError(action, text, url, opts = {}) {
89
+ const where = url === void 0 || url === "" ? "the instance" : transportTarget(url);
90
+ const isPage = markupSummary(text) !== void 0;
91
+ const titled = isPage && opts.safe !== true && /<title[^>]*>\s*\S[\s\S]*?<\/title>/i.test(text);
92
+ const what = isPage ? `${titled ? markupSummary(text) : "an HTML page, not an API response"} \u2014 a proxy or captive portal between you and the instance?` : text.trim() === "" ? "an empty body, not an API response." : "something that is not JSON, not an API response.";
93
+ const head = `${action}: ${where} answered with ${what}${opts.aftermath !== void 0 ? `
94
+ ${opts.aftermath}` : ""}`;
95
+ return new Error(opts.safe !== true && debugEnabled() && text.trim() !== "" ? `${head}
96
+ ${text}` : head);
97
+ }
98
+ function unexpectedAnswerError(action, text, url, opts = {}) {
99
+ const where = url === void 0 || url === "" ? "the instance" : transportTarget(url);
100
+ const head = `${action}: ${where} answered with something that is not the answer this request expects.${opts.aftermath !== void 0 ? `
101
+ ${opts.aftermath}` : ""}`;
102
+ return new Error(opts.safe !== true && debugEnabled() && text.trim() !== "" ? `${head}
103
+ ${text}` : head);
104
+ }
105
+ function parseJsonAnswer(text, action, url, opts = {}) {
106
+ try {
107
+ return JSON.parse(text);
108
+ } catch {
109
+ throw notJsonError(action, text, url, opts);
110
+ }
111
+ }
112
+ var TransportError = class extends Error {
113
+ constructor(message, timeout, options) {
114
+ super(message, options);
115
+ this.timeout = timeout;
116
+ }
117
+ timeout;
118
+ name = "TransportError";
119
+ };
120
+ function isBodyTransportFailure(err) {
121
+ if (isTimeoutError(err)) return true;
122
+ const name = err?.name;
123
+ if (name === "AbortError") return true;
124
+ if (typeof err?.cause?.code === "string") return true;
125
+ return err instanceof TypeError && /terminated|fetch failed|socket|network|ECONN/i.test(err.message);
126
+ }
127
+ function explainBodyFailure(err, opts) {
128
+ const where = opts.display ?? transportTarget(opts.url);
129
+ const redirected = redirectFailure(err, opts.url);
130
+ if (redirected !== void 0) {
131
+ const head2 = `${opts.what}: ${where} ${redirected}`;
132
+ return new TransportError(opts.aftermath === void 0 ? head2 : `${head2}
133
+ ${opts.aftermath}`, false, { cause: err });
134
+ }
135
+ const timeout = isTimeoutError(err);
136
+ const head = timeout ? `${opts.what}: ${where} did not finish answering` + (opts.timeoutMs !== void 0 ? ` within ${seconds(opts.timeoutMs)}` : "") + ` \u2014 the read timed out.` : `${opts.what}: the connection to ${where} dropped before the answer was fully read (${describeTransportFailure(err)}).`;
137
+ return new TransportError(`${head}
138
+ ${opts.aftermath ?? READ_AFTERMATH}`, timeout, { cause: err });
139
+ }
140
+ async function readBodyText(res, what, opts = {}) {
141
+ try {
142
+ return await res.text();
143
+ } catch (err) {
144
+ if (err instanceof TransportError || !isBodyTransportFailure(err)) throw err;
145
+ throw explainBodyFailure(err, { url: opts.url ?? res.url, what, timeoutMs: opts.timeoutMs, aftermath: opts.aftermath });
146
+ }
147
+ }
148
+ async function readBodyBytes(res, what, opts = {}) {
149
+ try {
150
+ return new Uint8Array(await res.arrayBuffer());
151
+ } catch (err) {
152
+ if (err instanceof TransportError || !isBodyTransportFailure(err)) throw err;
153
+ throw explainBodyFailure(err, { url: opts.url ?? res.url, what, timeoutMs: opts.timeoutMs, aftermath: opts.aftermath });
154
+ }
155
+ }
156
+ function explainingBody(res, opts) {
157
+ for (const method of ["text", "arrayBuffer", "json", "blob", "bytes", "formData"]) {
158
+ const original = res[method];
159
+ if (typeof original !== "function") continue;
160
+ Object.defineProperty(res, method, {
161
+ configurable: true,
162
+ value: async (...a) => {
163
+ try {
164
+ return await original.apply(res, a);
165
+ } catch (err) {
166
+ if (err instanceof TransportError || !isBodyTransportFailure(err)) throw err;
167
+ throw explainBodyFailure(err, opts);
168
+ }
169
+ }
170
+ });
171
+ }
172
+ return res;
173
+ }
174
+ var SENT_AFTERMATH = "The request was sent, so it may or may not have taken effect \u2014 check before retrying.";
175
+ function writeRefusedBy(status, opts = {}) {
176
+ if (opts.atomic === true) return !GATEWAY_STATUSES.has(status);
177
+ return status >= 400 && status < 500 || status === 501;
178
+ }
179
+ var GATEWAY_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
180
+ function writeStatusAftermath(status, aftermath = SENT_AFTERMATH, opts = {}) {
181
+ return writeRefusedBy(status, opts) ? void 0 : aftermath;
182
+ }
183
+ var OPENED_THEN_LOST = /* @__PURE__ */ new Set(["UND_ERR_SOCKET"]);
184
+ function answerWasLost(err) {
185
+ let next = err;
186
+ for (let depth = 0; depth < 8 && next !== null && typeof next === "object"; depth++) {
187
+ const code = next.code;
188
+ if (typeof code === "string" && OPENED_THEN_LOST.has(code)) return true;
189
+ next = next.cause;
190
+ }
191
+ return false;
192
+ }
193
+ function lostAnswerHead(head, err) {
194
+ if (!answerWasLost(err)) return head;
195
+ const m = /^(.*?) could not reach (.+?): (.*)$/s.exec(head);
196
+ return m === null ? head : `${m[1]}: the answer from ${m[2]} was lost \u2014 ${m[3]}`;
197
+ }
198
+ function withWriteStatusAftermath(head, status, aftermath, opts = {}) {
199
+ const line = writeStatusAftermath(status, aftermath, opts);
200
+ return line === void 0 ? head : `${head}
201
+ ${line}`;
202
+ }
203
+ function isReadMethod(init) {
204
+ const method = (init.method ?? "GET").toUpperCase();
205
+ return method === "GET" || method === "HEAD";
206
+ }
207
+ function labelledMessage(text) {
208
+ let parsed;
209
+ try {
210
+ parsed = JSON.parse(text);
211
+ } catch {
212
+ return void 0;
213
+ }
214
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return void 0;
215
+ const message = parsed.message;
216
+ if (typeof message !== "string" || message === "") return void 0;
217
+ const flat = message.replace(/\s+/g, " ").trim();
218
+ if (flat === "") return void 0;
219
+ return flat.length > MAX_DETAIL_CHARS ? `${flat.slice(0, MAX_DETAIL_CHARS)}\u2026` : flat;
220
+ }
221
+ function statusLabel(res) {
222
+ return res.statusText === "" ? String(res.status) : `${res.status} ${res.statusText}`;
223
+ }
224
+ function safeHttpFailure(action, res, body, binding) {
225
+ const detail = labelledMessage(body);
226
+ const head = `${action} failed (${statusLabel(res)})${detail !== void 0 ? `: ${detail}` : "."}`;
227
+ if (res.status !== 403 || binding === void 0) return head;
228
+ const why = explainBindingRefusal(binding);
229
+ return why === void 0 ? head : `${head}
230
+ ${why}`;
231
+ }
232
+ function bindingFor(auth, workspaceId, tenantHost) {
233
+ return {
234
+ boundWorkspaceId: auth.workspaceId,
235
+ instance: auth.instance,
236
+ profile: auth.profile,
237
+ addressedWorkspaceId: workspaceId,
238
+ credentialType: auth.credentialType,
239
+ tenantHost
240
+ };
241
+ }
242
+ function explainBindingRefusal(ctx, flag = ctx.profile?.flag ?? "--profile") {
243
+ if (ctx.tenantHost) return void 0;
244
+ if (ctx.credentialType === "token") return void 0;
245
+ if (ctx.boundWorkspaceId !== void 0 && ctx.boundWorkspaceId === ctx.addressedWorkspaceId) {
246
+ return void 0;
247
+ }
248
+ const arm = ctx.credentialType === "oauth-refresh" ? `This credential came from \`XANO_REFRESH_TOKEN\`, which resolves its workspace at run time \u2014 check that the refresh token was minted for workspace ${ctx.addressedWorkspaceId}.` : profileRemedy(ctx.profile, flag);
249
+ const whose = ctx.profile === void 0 ? "This credential" : `Profile "${ctx.profile.name}"`;
250
+ return `${whose} is bound to ${ctx.boundWorkspaceId === void 0 ? "a different workspace" : `workspace ${ctx.boundWorkspaceId}`} on ${ctx.instance ?? "this instance"}, and this call addressed workspace ${ctx.addressedWorkspaceId}. A token can only act on the workspace it was issued for.
251
+ ` + arm;
252
+ }
253
+ function profileRemedy(profile, flag) {
254
+ if (profile !== void 0 && profile.source === "flag" && flag !== "--profile") {
255
+ return `Run \`xanosdk profile list\` to see which profile addresses the workspace you meant, then name it with \`${flag} <name>\`.`;
256
+ }
257
+ if (profile === void 0 || profile.source === "flag" || profile.source === "env") {
258
+ return `Run \`xanosdk profile list\` to see which profile addresses the workspace you meant, or \`xanosdk whoami\` for this one's binding.`;
259
+ }
260
+ return `Nothing on the command line selected this profile \u2014 it came from ${profileSourceLabel(profile.source, profile.defaultIn)}. Run \`xanosdk profile list\` to see the others, then \`--profile <name>\` for one run or \`xanosdk profile use <name>\` to pin this project to it.`;
261
+ }
262
+ function httpFailure(action, res, body) {
263
+ const detail = serverMessage(body);
264
+ const head = `${action} failed (${statusLabel(res)})${detail !== void 0 ? `: ${detail}` : "."}`;
265
+ return debugEnabled() && body.trim() !== "" ? `${head}
266
+ ${body}` : head;
267
+ }
268
+ function closeSentence(text) {
269
+ const t = text.trimEnd();
270
+ return t === "" || /[.!?…]["')\]]*$/.test(t) ? t : `${t}.`;
271
+ }
272
+ function appendSentence(message, sentence) {
273
+ const [first = "", ...rest] = message.split("\n");
274
+ return [`${closeSentence(first)} ${sentence}`.trimStart(), ...rest].join("\n");
275
+ }
276
+ function httpFailureError(action, res, body) {
277
+ return new Error(httpFailure(action, res, body));
278
+ }
279
+ function describeTransportFailure(err) {
280
+ const cause = err?.cause;
281
+ if (isInvalidRequestHeader(err)) return INVALID_HEADER;
282
+ const certificate = certificateFailureCode(err);
283
+ if (certificate !== void 0) return describeCertificateFailure(certificate);
284
+ if (isBlockedPort(err)) {
285
+ return `its port is one fetch refuses to connect to (a blocked port, e.g. 1 or 25) \u2014 nothing was sent. Check the port in the URL.`;
286
+ }
287
+ const code = cause?.code;
288
+ const outer = err instanceof Error ? err.message : String(err);
289
+ if (typeof code === "string") return `${outer} (${code})`;
290
+ if (cause instanceof Error && cause.message !== "") return `${outer} (${cause.message})`;
291
+ if (cause !== void 0 && cause !== null) return `${outer} (${String(cause)})`;
292
+ return outer;
293
+ }
294
+ var INVALID_HEADER = "a request header holds a character no request can carry (a line break, a control or a non-ASCII character), so nothing was sent. Check the credential's token for stray characters.";
295
+ function isInvalidRequestHeader(err) {
296
+ const message = err instanceof Error ? err.message : "";
297
+ if (/is an invalid header value|Cannot convert argument to a ByteString/.test(message)) return true;
298
+ const cause = err?.cause;
299
+ return cause?.code === "UND_ERR_INVALID_ARG" && /header/i.test(String(cause.message ?? ""));
300
+ }
301
+ var TLS_CERTIFICATE_CODES = /* @__PURE__ */ new Set([
302
+ "DEPTH_ZERO_SELF_SIGNED_CERT",
303
+ "SELF_SIGNED_CERT_IN_CHAIN",
304
+ "UNABLE_TO_VERIFY_LEAF_SIGNATURE",
305
+ "UNABLE_TO_GET_ISSUER_CERT",
306
+ "UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
307
+ "UNABLE_TO_DECRYPT_CERT_SIGNATURE",
308
+ "UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY",
309
+ "CERT_SIGNATURE_FAILURE",
310
+ "CERT_NOT_YET_VALID",
311
+ "CERT_HAS_EXPIRED",
312
+ "CERT_REVOKED",
313
+ "CERT_UNTRUSTED",
314
+ "CERT_REJECTED",
315
+ "CERT_CHAIN_TOO_LONG",
316
+ "INVALID_CA",
317
+ "INVALID_PURPOSE",
318
+ "HOSTNAME_MISMATCH",
319
+ "ERR_TLS_CERT_ALTNAME_INVALID",
320
+ "ERR_TLS_CERT_ALTNAME_FORMAT"
321
+ ]);
322
+ var PROXY_CERTIFICATE_CODES = /* @__PURE__ */ new Set(["SELF_SIGNED_CERT_IN_CHAIN", "UNABLE_TO_GET_ISSUER_CERT_LOCALLY"]);
323
+ function certificateFailureCode(err) {
324
+ for (let e = err, depth = 0; e !== void 0 && e !== null && depth < 8; depth++) {
325
+ const code = e.code;
326
+ if (typeof code === "string" && TLS_CERTIFICATE_CODES.has(code)) return code;
327
+ e = e.cause;
328
+ }
329
+ return void 0;
330
+ }
331
+ function certificateFailureHost(err) {
332
+ for (let e = err, depth = 0; e !== void 0 && e !== null && depth < 8; depth++) {
333
+ const m = e instanceof Error ? /could not reach (https?:\/\/[^\s/]+?)(?=: |\/| |$)/.exec(e.message) : null;
334
+ if (m !== null) {
335
+ try {
336
+ return new URL(m[1]).host;
337
+ } catch {
338
+ break;
339
+ }
340
+ }
341
+ e = e.cause;
342
+ }
343
+ return "the server";
344
+ }
345
+ var UNTRUSTED_CERTIFICATE = "the server's TLS certificate is not trusted";
346
+ function describeCertificateFailure(code) {
347
+ const proxy = PROXY_CERTIFICATE_CODES.has(code) ? " If a corporate proxy re-signs HTTPS traffic, point NODE_EXTRA_CA_CERTS at its CA certificate file." : "";
348
+ return `${UNTRUSTED_CERTIFICATE} (${code}), so nothing was sent \u2014 a certificate or proxy configuration problem, not a network blip, and retrying will not help until it is fixed.${proxy}`;
349
+ }
350
+ function isBlockedPort(err) {
351
+ const cause = err?.cause;
352
+ return cause instanceof Error && cause.message === "bad port";
353
+ }
354
+ function isTimeoutError(err) {
355
+ return typeof err?.name === "string" && err.name === "TimeoutError";
356
+ }
357
+ function seconds(ms) {
358
+ return `${Number((ms / 1e3).toFixed(1))}s`;
359
+ }
360
+ function transportTarget(url) {
361
+ let parsed;
362
+ try {
363
+ parsed = new URL(url);
364
+ } catch {
365
+ return "the instance";
366
+ }
367
+ const cut = parsed.pathname.search(/\/api:/);
368
+ const path = (cut === -1 ? parsed.pathname : parsed.pathname.slice(0, cut)).replace(/\/$/, "");
369
+ return `${parsed.origin}${path}`;
370
+ }
371
+ function explainFetchFailure(err, opts) {
372
+ const where = opts.display ?? transportTarget(opts.url);
373
+ const redirected = redirectFailure(err, opts.url);
374
+ if (redirected !== void 0) {
375
+ const head = `${opts.what}: ${where} ${redirected}`;
376
+ return new TransportError(opts.aftermath === void 0 ? head : `${head}
377
+ ${opts.aftermath}`, false, { cause: err });
378
+ }
379
+ const timeout = isTimeoutError(err);
380
+ const within = opts.timeoutMs !== void 0 ? ` within ${seconds(opts.timeoutMs)}` : "";
381
+ if (opts.aftermath !== void 0 && !isBlockedPort(err) && certificateFailureCode(err) === void 0) {
382
+ const head = timeout ? `${opts.what}: ${where} did not answer${within}. The instance may still be starting up or under load.` : closeSentence(`${opts.what} could not reach ${where}: ${describeTransportFailure(err)}`);
383
+ return new TransportError(`${head}
384
+ ${opts.aftermath}`, timeout, { cause: err });
385
+ }
386
+ const message = timeout ? `${opts.what}: ${where} did not answer${within}. The instance may still be starting up or under load \u2014 retry, or raise the wait.` : `${opts.what} could not reach ${where}: ${describeTransportFailure(err)}`;
387
+ return new TransportError(message, timeout, { cause: err });
388
+ }
389
+ function redirectedUnresolvedHost(err, url) {
390
+ let own;
391
+ try {
392
+ own = new URL(url).hostname;
393
+ } catch {
394
+ return void 0;
395
+ }
396
+ for (let e = err, depth = 0; e !== void 0 && e !== null && depth < 5; depth++) {
397
+ const { code, hostname } = e;
398
+ if (code === "ENOTFOUND" && typeof hostname === "string" && hostname !== "") {
399
+ return hostname.replace(/^\[|\]$/g, "") === own.replace(/^\[|\]$/g, "") ? void 0 : hostname;
400
+ }
401
+ e = e.cause;
402
+ }
403
+ return void 0;
404
+ }
405
+ var BLOCKED_PORTS = /* @__PURE__ */ new Set([
406
+ 1,
407
+ 7,
408
+ 9,
409
+ 11,
410
+ 13,
411
+ 15,
412
+ 17,
413
+ 19,
414
+ 20,
415
+ 21,
416
+ 22,
417
+ 23,
418
+ 25,
419
+ 37,
420
+ 42,
421
+ 43,
422
+ 53,
423
+ 69,
424
+ 77,
425
+ 79,
426
+ 87,
427
+ 95,
428
+ 101,
429
+ 102,
430
+ 103,
431
+ 104,
432
+ 109,
433
+ 110,
434
+ 111,
435
+ 113,
436
+ 115,
437
+ 117,
438
+ 119,
439
+ 123,
440
+ 135,
441
+ 137,
442
+ 139,
443
+ 143,
444
+ 161,
445
+ 179,
446
+ 389,
447
+ 427,
448
+ 465,
449
+ 512,
450
+ 513,
451
+ 514,
452
+ 515,
453
+ 526,
454
+ 530,
455
+ 531,
456
+ 532,
457
+ 540,
458
+ 548,
459
+ 554,
460
+ 556,
461
+ 563,
462
+ 587,
463
+ 601,
464
+ 636,
465
+ 989,
466
+ 990,
467
+ 993,
468
+ 995,
469
+ 1719,
470
+ 1720,
471
+ 1723,
472
+ 2049,
473
+ 3659,
474
+ 4045,
475
+ 4190,
476
+ 5060,
477
+ 5061,
478
+ 6e3,
479
+ 6566,
480
+ 6665,
481
+ 6666,
482
+ 6667,
483
+ 6668,
484
+ 6669,
485
+ 6679,
486
+ 6697,
487
+ 10080
488
+ ]);
489
+ function redirectFailure(err, url) {
490
+ let own;
491
+ try {
492
+ own = new URL(url);
493
+ } catch {
494
+ return void 0;
495
+ }
496
+ const unresolved = redirectedUnresolvedHost(err, url);
497
+ if (unresolved !== void 0) return `redirected to ${unresolved}, which does not resolve (ENOTFOUND). Check where that host points.`;
498
+ const ownHost = own.hostname.replace(/^\[|\]$/g, "");
499
+ const ownPort = own.port === "" ? own.protocol === "https:" ? "443" : "80" : own.port;
500
+ if (isBlockedPort(err)) {
501
+ return BLOCKED_PORTS.has(Number(ownPort)) ? void 0 : `redirected to a port fetch refuses to connect to (a blocked port, e.g. 1 or 25), so nothing was sent there. Check where that redirect points.`;
502
+ }
503
+ for (let e = err, depth = 0; e !== void 0 && e !== null && depth < 5; depth++) {
504
+ const { code, address, port } = e;
505
+ if (typeof code === "string" && typeof address === "string" && (typeof port === "number" || typeof port === "string")) {
506
+ const otherPort = String(port) !== ownPort;
507
+ const otherAddress = isIP(ownHost) !== 0 && address !== ownHost;
508
+ if (!otherPort && !otherAddress) return void 0;
509
+ const target = `${address.includes(":") ? `[${address}]` : address}:${port}`;
510
+ const what = code === "ECONNREFUSED" ? "which refused the connection" : "which could not be reached";
511
+ return `redirected to ${target}, ${what} (${code}). Check where that redirect points.`;
512
+ }
513
+ e = e.cause;
514
+ }
515
+ return void 0;
516
+ }
517
+ var READ_AFTERMATH = "Nothing was changed \u2014 retry.";
518
+ function withoutReadAftermath(message) {
519
+ return message.split("\n").filter((line) => line !== READ_AFTERMATH).join("\n");
520
+ }
521
+ async function fetchOrExplain(url, init, what, timeoutMs, display) {
522
+ let res;
523
+ try {
524
+ res = await fetchPastRateLimit(url, init, isReadMethod(init));
525
+ await noteMetaAnswer(url, res);
526
+ } catch (err) {
527
+ if (isInvalidRequestHeader(err)) throw new UsageError(`${what}: ${INVALID_HEADER}`);
528
+ throw explainFetchFailure(err, {
529
+ url,
530
+ what,
531
+ timeoutMs,
532
+ display,
533
+ ...isReadMethod(init) ? { aftermath: READ_AFTERMATH } : {}
534
+ });
535
+ }
536
+ return explainingBody(res, {
537
+ url,
538
+ what,
539
+ ...timeoutMs !== void 0 ? { timeoutMs } : {},
540
+ ...display !== void 0 ? { display } : {},
541
+ ...isReadMethod(init) ? {} : { aftermath: SENT_AFTERMATH }
542
+ });
543
+ }
544
+ var READ_ATTEMPTS = 3;
545
+ var READ_BACKOFF_MS = 400;
546
+ var RATE_LIMIT_MAX_WAIT_S = 5;
547
+ var RATE_LIMIT_RETRIES = 2;
548
+ var lastRetryAfterS;
549
+ function retryAfterSeconds(header, now = Date.now()) {
550
+ if (header === null || header.trim() === "") return void 0;
551
+ const t = header.trim();
552
+ if (/^\d+$/.test(t)) return Number(t);
553
+ const at = Date.parse(t);
554
+ return Number.isNaN(at) ? void 0 : Math.max(0, Math.ceil((at - now) / 1e3));
555
+ }
556
+ function noteRateLimit(res) {
557
+ if (res.status === 429) lastRetryAfterS = retryAfterSeconds(res.headers?.get?.("retry-after") ?? null);
558
+ }
559
+ function lastRateLimitRetryAfter() {
560
+ return lastRetryAfterS;
561
+ }
562
+ async function fetchPastRateLimit(url, init, read) {
563
+ for (let attempt = 0; ; attempt++) {
564
+ const res = await fetch(url, init);
565
+ if (res.status !== 429) return res;
566
+ noteRateLimit(res);
567
+ const after = lastRetryAfterS;
568
+ if (!read || attempt >= RATE_LIMIT_RETRIES) return res;
569
+ const wait = after ?? 2 ** attempt;
570
+ if (wait > RATE_LIMIT_MAX_WAIT_S) return res;
571
+ await res.body?.cancel().catch(() => void 0);
572
+ await new Promise((r) => setTimeout(r, wait * 1e3));
573
+ }
574
+ }
575
+ function isRetryableTransportFailure(err) {
576
+ if (isTimeoutError(err)) return false;
577
+ const code = err?.cause?.code;
578
+ return typeof code === "string" && ["ECONNREFUSED", "ECONNRESET", "ENOTFOUND", "EAI_AGAIN", "EPIPE", "ETIMEDOUT", "ENETUNREACH"].includes(code);
579
+ }
580
+ async function fetchReadOrExplain(url, init, what, timeoutMs) {
581
+ let res;
582
+ try {
583
+ res = await retryingFetch(url, init);
584
+ } catch (err) {
585
+ throw explainFetchFailure(err, { url, what, timeoutMs, aftermath: READ_AFTERMATH });
586
+ }
587
+ return explainingBody(res, { url, what, ...timeoutMs !== void 0 ? { timeoutMs } : {} });
588
+ }
589
+ async function retryingFetch(url, init) {
590
+ let last;
591
+ for (let attempt = 0; attempt < READ_ATTEMPTS; attempt++) {
592
+ try {
593
+ const res = await fetchPastRateLimit(url, init, true);
594
+ await noteMetaAnswer(url, res);
595
+ return res;
596
+ } catch (err) {
597
+ last = err;
598
+ if (!isRetryableTransportFailure(err) || attempt === READ_ATTEMPTS - 1) break;
599
+ await new Promise((r) => setTimeout(r, READ_BACKOFF_MS * 2 ** attempt));
600
+ }
601
+ }
602
+ throw last;
603
+ }
604
+
605
+ export {
606
+ inCredentialScope,
607
+ noteResolvedAuth,
608
+ lastResolvedAuth,
609
+ noteMetaAnswer,
610
+ pinnedWorkspaceMissed,
611
+ debugEnabled,
612
+ serverMessage,
613
+ markupSummary,
614
+ notJsonError,
615
+ unexpectedAnswerError,
616
+ parseJsonAnswer,
617
+ TransportError,
618
+ explainBodyFailure,
619
+ readBodyText,
620
+ readBodyBytes,
621
+ SENT_AFTERMATH,
622
+ writeRefusedBy,
623
+ writeStatusAftermath,
624
+ lostAnswerHead,
625
+ withWriteStatusAftermath,
626
+ statusLabel,
627
+ safeHttpFailure,
628
+ bindingFor,
629
+ explainBindingRefusal,
630
+ httpFailure,
631
+ closeSentence,
632
+ appendSentence,
633
+ httpFailureError,
634
+ describeTransportFailure,
635
+ TLS_CERTIFICATE_CODES,
636
+ certificateFailureCode,
637
+ certificateFailureHost,
638
+ UNTRUSTED_CERTIFICATE,
639
+ isTimeoutError,
640
+ transportTarget,
641
+ explainFetchFailure,
642
+ redirectedUnresolvedHost,
643
+ READ_AFTERMATH,
644
+ withoutReadAftermath,
645
+ fetchOrExplain,
646
+ RATE_LIMIT_MAX_WAIT_S,
647
+ retryAfterSeconds,
648
+ noteRateLimit,
649
+ lastRateLimitRetryAfter,
650
+ fetchReadOrExplain,
651
+ retryingFetch
652
+ };
653
+ //# sourceMappingURL=chunk-YLDALTWZ.js.map