@gusto/baerly-storage 0.6.0 → 0.7.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 (218) hide show
  1. package/README.md +47 -124
  2. package/dist/API.md +161 -512
  3. package/dist/CHANGELOG.md +1137 -378
  4. package/dist/RECIPES.md +398 -80
  5. package/dist/THIRD-PARTY-LICENSES.txt +5 -26
  6. package/dist/{app-config-DyiFK3_k.d.ts → app-config-C6qW7m8S.d.ts} +119 -111
  7. package/dist/app-config.d.ts +31 -1
  8. package/dist/app-config.js +1 -2
  9. package/dist/app-config.js.map +1 -1
  10. package/dist/auth.d.ts +33 -28
  11. package/dist/auth.js +549 -985
  12. package/dist/auth.js.map +1 -1
  13. package/dist/baerly.js +130 -138
  14. package/dist/baerly.js.map +1 -1
  15. package/dist/baerly.spec.json +97 -22
  16. package/dist/capacity-warning-header-BlVopyiI.js +16 -0
  17. package/dist/capacity-warning-header-BlVopyiI.js.map +1 -0
  18. package/dist/{client-ypCm3zjt.d.ts → client-CHQTXpef.d.ts} +121 -36
  19. package/dist/client-react.d.ts +67 -20
  20. package/dist/client-react.js +240 -54
  21. package/dist/client-react.js.map +1 -1
  22. package/dist/client.d.ts +5 -4
  23. package/dist/client.js +51 -16
  24. package/dist/client.js.map +1 -1
  25. package/dist/{cloudflare-wrVYf4bb.js → cloudflare-CKHYXccw.js} +61 -82
  26. package/dist/cloudflare-CKHYXccw.js.map +1 -0
  27. package/dist/cloudflare.d.ts +106 -71
  28. package/dist/cloudflare.js +334 -271
  29. package/dist/cloudflare.js.map +1 -1
  30. package/dist/code-resolution-CL_7L0wS.js +22 -0
  31. package/dist/code-resolution-CL_7L0wS.js.map +1 -0
  32. package/dist/collection-api-DbyhLAY8.d.ts +1001 -0
  33. package/dist/config-DEzInG6M.js +4035 -0
  34. package/dist/config-DEzInG6M.js.map +1 -0
  35. package/dist/constants-D3GSH3tc.js +469 -0
  36. package/dist/constants-D3GSH3tc.js.map +1 -0
  37. package/dist/{context-DRZ3tZ5l.js → context-rlpSnHFA.js} +17 -17
  38. package/dist/context-rlpSnHFA.js.map +1 -0
  39. package/dist/cost-B52llVDx.js +330 -0
  40. package/dist/cost-B52llVDx.js.map +1 -0
  41. package/dist/{current-json-C6nhPOm1.js → current-json-CC6lE4Aj.js} +115 -32
  42. package/dist/current-json-CC6lE4Aj.js.map +1 -0
  43. package/dist/db-plFUNTSF.d.ts +130 -0
  44. package/dist/db-read-seams-BSs0m1z1.js +3261 -0
  45. package/dist/db-read-seams-BSs0m1z1.js.map +1 -0
  46. package/dist/{deploy-Di_Q01qx.js → deploy-Obj4nE-Z.js} +10 -14
  47. package/dist/deploy-Obj4nE-Z.js.map +1 -0
  48. package/dist/{dev-banner-CWHIk8tH.d.ts → dev-banner-B5AUHmu1.d.ts} +1 -4
  49. package/dist/dev-landing-BI5UdmOb.d.ts +32 -0
  50. package/dist/dev-landing-CD-si4dW.js +50 -0
  51. package/dist/dev-landing-CD-si4dW.js.map +1 -0
  52. package/dist/dev-vite.d.ts +9 -14
  53. package/dist/dev-vite.js +11 -10
  54. package/dist/dev-vite.js.map +1 -1
  55. package/dist/dev.d.ts +3 -2
  56. package/dist/dev.js +2 -1
  57. package/dist/doctor-BA0i-YAN.js +270 -0
  58. package/dist/doctor-BA0i-YAN.js.map +1 -0
  59. package/dist/dump-D1vX8ozg.js +133 -0
  60. package/dist/dump-D1vX8ozg.js.map +1 -0
  61. package/dist/errors-7Rk1HGCE.js +160 -0
  62. package/dist/errors-7Rk1HGCE.js.map +1 -0
  63. package/dist/{validate-DtljsCh1.js → export-BVL2EvYE.js} +409 -19
  64. package/dist/export-BVL2EvYE.js.map +1 -0
  65. package/dist/fsck-BWkPgov4.js +145 -0
  66. package/dist/fsck-BWkPgov4.js.map +1 -0
  67. package/dist/{gcs-admin-Bx6xSNJj.d.ts → gcs-admin-Dso1HWQj.d.ts} +18 -14
  68. package/dist/gcs.d.ts +19 -7
  69. package/dist/gcs.js +1 -2
  70. package/dist/{goog4-signer-Z7KnJ1su.js → goog4-signer-CeLIABoE.js} +102 -92
  71. package/dist/goog4-signer-CeLIABoE.js.map +1 -0
  72. package/dist/{http-7zT29K3P.js → http-Z5rZyYMX.js} +898 -271
  73. package/dist/http-Z5rZyYMX.js.map +1 -0
  74. package/dist/http-transport-DNUM8kU-.js +1299 -0
  75. package/dist/http-transport-DNUM8kU-.js.map +1 -0
  76. package/dist/http.d.ts +366 -294
  77. package/dist/http.js +2 -2
  78. package/dist/index-JJrIu1pO.d.ts +135 -0
  79. package/dist/index-LEgHN0qJ.d.ts +50 -0
  80. package/dist/{index-DP3ClvcA.d.ts → index-RiEYj8t0.d.ts} +291 -78
  81. package/dist/index.d.ts +27 -6
  82. package/dist/index.js +3 -5
  83. package/dist/inspect-BTsVYZKd.js +196 -0
  84. package/dist/inspect-BTsVYZKd.js.map +1 -0
  85. package/dist/{key-Di6yyxFu.js → key-C4FPZ9Io.js} +3 -4
  86. package/dist/{key-Di6yyxFu.js.map → key-C4FPZ9Io.js.map} +1 -1
  87. package/dist/ledger-Dj_bGgo0.js +192 -0
  88. package/dist/ledger-Dj_bGgo0.js.map +1 -0
  89. package/dist/legacy-names-DgZBtgOh.js +47 -0
  90. package/dist/legacy-names-DgZBtgOh.js.map +1 -0
  91. package/dist/log-C9W15Dfo.d.ts +94 -0
  92. package/dist/log-tail-DWJbneBF.js +142 -0
  93. package/dist/log-tail-DWJbneBF.js.map +1 -0
  94. package/dist/log-walk-C-vA-WGJ.js +111 -0
  95. package/dist/log-walk-C-vA-WGJ.js.map +1 -0
  96. package/dist/maintenance-Dd-Yakqj.js +1709 -0
  97. package/dist/maintenance-Dd-Yakqj.js.map +1 -0
  98. package/dist/maintenance-DevB6z4e.js +2237 -0
  99. package/dist/maintenance-DevB6z4e.js.map +1 -0
  100. package/dist/maintenance.d.ts +100 -3
  101. package/dist/maintenance.js +1 -3
  102. package/dist/node.d.ts +119 -92
  103. package/dist/node.js +3 -3
  104. package/dist/{observability-Ch-VQJyE.js → observability-C-ISCCLE.js} +1247 -326
  105. package/dist/observability-C-ISCCLE.js.map +1 -0
  106. package/dist/observability.d.ts +27 -3
  107. package/dist/observability.js +3 -3
  108. package/dist/path-segment-DBXG26PW.js +363 -0
  109. package/dist/path-segment-DBXG26PW.js.map +1 -0
  110. package/dist/{plan-C4qgiXSg.js → plan-BvQbaNMe.js} +19 -11
  111. package/dist/plan-BvQbaNMe.js.map +1 -0
  112. package/dist/request-CRWiTrza.js +272 -0
  113. package/dist/request-CRWiTrza.js.map +1 -0
  114. package/dist/restore-U6Im8V1V.js +1467 -0
  115. package/dist/restore-U6Im8V1V.js.map +1 -0
  116. package/dist/retry-OBtLB2GC.js +126 -0
  117. package/dist/retry-OBtLB2GC.js.map +1 -0
  118. package/dist/rolldown-runtime-D3KUYPsf.js +12 -0
  119. package/dist/{runtime-spec--LykAdl1.js → runtime-spec-B5mg3pHF.js} +189 -28
  120. package/dist/runtime-spec-B5mg3pHF.js.map +1 -0
  121. package/dist/{s3-http-DD5gDc7t.d.ts → s3-http-DBC_wiCL.d.ts} +14 -8
  122. package/dist/s3.d.ts +20 -7
  123. package/dist/s3.js +4 -5
  124. package/dist/s3.js.map +1 -1
  125. package/dist/{sha256-CqcKTjw9.js → sha256-4FfGQNPw.js} +9 -9
  126. package/dist/sha256-4FfGQNPw.js.map +1 -0
  127. package/dist/{shared-secret-DgEicd3x.js → shared-secret-Vkur-8r0.js} +27 -15
  128. package/dist/shared-secret-Vkur-8r0.js.map +1 -0
  129. package/dist/{aws4fetch.esm-CXE5ZbAa.js → sigv4-sign-5IUflUD_.js} +143 -96
  130. package/dist/sigv4-sign-5IUflUD_.js.map +1 -0
  131. package/dist/snapshot-CknzkDM0.js +148 -0
  132. package/dist/snapshot-CknzkDM0.js.map +1 -0
  133. package/dist/{src-BMFfDUI2.js → src-Actga-bO.js} +949 -363
  134. package/dist/src-Actga-bO.js.map +1 -0
  135. package/dist/src-D0MQfvOp.js +812 -0
  136. package/dist/src-D0MQfvOp.js.map +1 -0
  137. package/dist/src-Dr9uo3dc.js +501 -0
  138. package/dist/src-Dr9uo3dc.js.map +1 -0
  139. package/dist/{subcommand-BV68pqwJ.js → subcommand-BLoF66dG.js} +89 -16
  140. package/dist/subcommand-BLoF66dG.js.map +1 -0
  141. package/dist/{time-DUqvFMHq.js → time-DRxTP9iv.js} +15 -34
  142. package/dist/time-DRxTP9iv.js.map +1 -0
  143. package/dist/types-suVbsEWS.d.ts +116 -0
  144. package/dist/{usage-BMZR8o6U.js → usage-CPzVhrQr.js} +40 -45
  145. package/dist/usage-CPzVhrQr.js.map +1 -0
  146. package/dist/uuid-BuT_c404.js +333 -0
  147. package/dist/uuid-BuT_c404.js.map +1 -0
  148. package/dist/verifier-Bmlmndzo.d.ts +95 -0
  149. package/dist/{wire-izfGZDvr.js → wire-DndvKU-K.js} +2 -3
  150. package/dist/wire-DndvKU-K.js.map +1 -0
  151. package/package.json +90 -69
  152. package/dist/aws4fetch.esm-CXE5ZbAa.js.map +0 -1
  153. package/dist/bytes-BXdamO3L.js +0 -11
  154. package/dist/bytes-BXdamO3L.js.map +0 -1
  155. package/dist/cloudflare-wrVYf4bb.js.map +0 -1
  156. package/dist/config-B6nTy8e2.js +0 -3095
  157. package/dist/config-B6nTy8e2.js.map +0 -1
  158. package/dist/constants-CfeMWQWk.js +0 -222
  159. package/dist/constants-CfeMWQWk.js.map +0 -1
  160. package/dist/context-DRZ3tZ5l.js.map +0 -1
  161. package/dist/cost-_SE_SVgZ.js +0 -203
  162. package/dist/cost-_SE_SVgZ.js.map +0 -1
  163. package/dist/current-json-C6nhPOm1.js.map +0 -1
  164. package/dist/db-DjjthkTi.d.ts +0 -254
  165. package/dist/deploy-Di_Q01qx.js.map +0 -1
  166. package/dist/doctor-RWVRpvat.js +0 -102
  167. package/dist/doctor-RWVRpvat.js.map +0 -1
  168. package/dist/dump-m2vEYDNY.js +0 -94
  169. package/dist/dump-m2vEYDNY.js.map +0 -1
  170. package/dist/errors-Dfa3p78c.js +0 -78
  171. package/dist/errors-Dfa3p78c.js.map +0 -1
  172. package/dist/export-DenKwEQu.js +0 -401
  173. package/dist/export-DenKwEQu.js.map +0 -1
  174. package/dist/fsck-2Vq-7-e2.js +0 -279
  175. package/dist/fsck-2Vq-7-e2.js.map +0 -1
  176. package/dist/goog4-signer-Z7KnJ1su.js.map +0 -1
  177. package/dist/http-7zT29K3P.js.map +0 -1
  178. package/dist/http-transport-BTgVCY5r.js +0 -1250
  179. package/dist/http-transport-BTgVCY5r.js.map +0 -1
  180. package/dist/index-BHz92Acj.d.ts +0 -162
  181. package/dist/index-CEZKQsKt.d.ts +0 -1113
  182. package/dist/index-uE-w2dgj.d.ts +0 -115
  183. package/dist/indexes-Dn9hbGQG.d.ts +0 -22
  184. package/dist/inspect-BpYbH1DM.js +0 -133
  185. package/dist/inspect-BpYbH1DM.js.map +0 -1
  186. package/dist/log-tail-QWU4wlu-.js +0 -514
  187. package/dist/log-tail-QWU4wlu-.js.map +0 -1
  188. package/dist/maintenance-BGHGt2Yg.d.ts +0 -537
  189. package/dist/maintenance-DULFcpd1.js +0 -2144
  190. package/dist/maintenance-DULFcpd1.js.map +0 -1
  191. package/dist/maintenance-fEi-UZ2o.js +0 -1412
  192. package/dist/maintenance-fEi-UZ2o.js.map +0 -1
  193. package/dist/normalize-rjnZLwS4.js +0 -133
  194. package/dist/normalize-rjnZLwS4.js.map +0 -1
  195. package/dist/observability-Ch-VQJyE.js.map +0 -1
  196. package/dist/plan-C4qgiXSg.js.map +0 -1
  197. package/dist/query-C43r-6ib.js +0 -1773
  198. package/dist/query-C43r-6ib.js.map +0 -1
  199. package/dist/rebuild-index-BH1dc1p6.js +0 -81
  200. package/dist/rebuild-index-BH1dc1p6.js.map +0 -1
  201. package/dist/request-u0M51mhJ.js +0 -72
  202. package/dist/request-u0M51mhJ.js.map +0 -1
  203. package/dist/restore-D0VYWzfP.js +0 -778
  204. package/dist/restore-D0VYWzfP.js.map +0 -1
  205. package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
  206. package/dist/runtime-spec--LykAdl1.js.map +0 -1
  207. package/dist/sha256-CqcKTjw9.js.map +0 -1
  208. package/dist/shared-secret-DgEicd3x.js.map +0 -1
  209. package/dist/src-BMFfDUI2.js.map +0 -1
  210. package/dist/src-DYZ4H1aF.js +0 -392
  211. package/dist/src-DYZ4H1aF.js.map +0 -1
  212. package/dist/src-DvbTEya2.js +0 -536
  213. package/dist/src-DvbTEya2.js.map +0 -1
  214. package/dist/subcommand-BV68pqwJ.js.map +0 -1
  215. package/dist/time-DUqvFMHq.js.map +0 -1
  216. package/dist/usage-BMZR8o6U.js.map +0 -1
  217. package/dist/validate-DtljsCh1.js.map +0 -1
  218. package/dist/wire-izfGZDvr.js.map +0 -1
@@ -1,3095 +0,0 @@
1
- import { l as __commonJSMin, o as BaerlyError } from "./baerly.js";
2
- import { createHash } from "node:crypto";
3
- import { link, mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
4
- import { tmpdir } from "node:os";
5
- import { dirname, join, posix, relative, resolve, sep } from "node:path";
6
- import { existsSync } from "node:fs";
7
- import { fileURLToPath, pathToFileURL } from "node:url";
8
- //#region ../protocol/src/constants.ts
9
- /**
10
- * Max total object-key length S3 and R2 accept (UTF-8 bytes). A key
11
- * over this fails the PUT with an opaque provider 400; baerly surfaces
12
- * it early as InvalidConfig. See docs/spec/storage-compatibility.md.
13
- */
14
- const MAX_KEY_BYTES = 1024;
15
- /**
16
- * Backoff (in milliseconds) inserted between LIST attempts when S3
17
- * returns 429 (rate-limited).
18
- */
19
- const RATE_LIMIT_BACKOFF_MILLIS = 1e3;
20
- /**
21
- * Runaway guard on the tolerant forward-probe walk (`probeTailFrom`):
22
- * max `log/<seq>` GETs past `tail_hint` before it THROWS `Internal`
23
- * (rather than silently truncating reads). A runaway alarm only — the
24
- * maintenance tick keeps `tail_hint` near the tail, so a >cap gap means
25
- * maintenance is off or the collection is far past graduation.
26
- *
27
- * @see packages/server/src/log-tail.ts (`probeTailFrom`)
28
- */
29
- const LOG_FORWARD_PROBE_CAP = 1e5;
30
- /**
31
- * MIME type written for `current.json` PUTs. S3 round-trips this on
32
- * subsequent GETs; useful for diagnostics when staring at a bucket
33
- * via the AWS console.
34
- *
35
- * @see packages/protocol/src/coordination/current-json.ts
36
- */
37
- const CURRENT_JSON_CONTENT_TYPE = "application/json";
38
- /**
39
- * MIME type written for `gc/pending.json` PUTs.
40
- *
41
- * @see packages/protocol/src/coordination/gc-pending.ts
42
- */
43
- const GC_PENDING_CONTENT_TYPE = "application/json";
44
- /**
45
- * Default grace period between "marking" a key for GC and "sweeping"
46
- * (deleting) it. 7 days, chosen to span the worst plausible writer-
47
- * retry window (a paused-process writer that resumes hours later
48
- * should still find its idempotency anchor on the bucket). The
49
- * `runGc()` function accepts an override for tests.
50
- *
51
- * Why 7 days specifically:
52
- * - **1 day is too aggressive for batch workloads.** Worker isolate
53
- * scheduling pauses, cross-region replication lag, and
54
- * {@link RATE_LIMIT_BACKOFF_MILLIS} retry cascades can plausibly
55
- * exceed an hour under pathological conditions.
56
- * - **30 days is conservative beyond the worst plausible pause.**
57
- * Doubles `gc/pending.json` size at steady state and slows
58
- * visibility into "did GC actually run?" by 4×.
59
- * - **7 days spans the realistic upper bound** — long-running batch
60
- * jobs, multi-region propagation delays, queue backlogs, and
61
- * downstream outages the writer is retrying through. The protocol
62
- * is unaffected by the choice of value within the [hours, weeks]
63
- * range; this constant is the operator-tunable knob.
64
- *
65
- * Production code MUST NOT call `runGc` with `graceMillis` below the
66
- * default outside maintenance windows — going below the longest
67
- * plausible writer-retry latency risks deleting an anchor a writer
68
- * is about to find on retry. Test code that sets `graceMillis: 0` is
69
- * exercising the sweep path deliberately, not modelling production.
70
- *
71
- * @see packages/server/src/gc.ts
72
- */
73
- const GC_GRACE_PERIOD_MILLIS = 10080 * 60 * 1e3;
74
- /**
75
- * Cap on candidates kept in `gc/pending.json` to bound the size of
76
- * the file. The compactor marks at most this many candidates per
77
- * pass; subsequent passes pick up the rest. Larger collections will
78
- * lag GC by one pass per `GC_MAX_PENDING_CANDIDATES` orphans, which
79
- * is acceptable.
80
- *
81
- * @see packages/server/src/gc.ts
82
- */
83
- const GC_MAX_PENDING_CANDIDATES = 1e3;
84
- /**
85
- * Floor for the ratio denominator — avoid div-by-tiny on a fresh collection. Also
86
- * sets the first-fold threshold: until a snapshot exists, fold fires at tail ≈ this.
87
- *
88
- * @see packages/server/src/maintenance.ts
89
- */
90
- const MAINTENANCE_MIN_LIVE_BYTES = 64 * 1024;
91
- /**
92
- * SNAPSHOT-rebuild ceiling `C` (the unsliceable axis — Decision 3a), memory. Default sized
93
- * ~5.5 ms under CF-free ~10 ms. Raise via BAERLY_MAINTENANCE_MAX_FOLD_BYTES on capable
94
- * hosts. Auto-maintained snapshot ceiling S_max = C. NOT snapshot+tail (tail is sliced).
95
- *
96
- * @see docs/about/graduation.md
97
- * @see packages/server/src/maintenance.ts
98
- */
99
- const MAINTENANCE_MAX_FOLD_BYTES_DEFAULT = 512 * 1024;
100
- /**
101
- * SNAPSHOT-rebuild ceiling `E`, per-entry CPU axis: gates `snapshot_rows` (per-entry
102
- * parse/merge/serialize is ~half of fold CPU and scales with ROW COUNT not bytes, VLDB 2021
103
- * Sarkar — a tiny-doc snapshot can blow CPU under C). PROVISIONAL — bench
104
- * landed; paid recalibration deferred.
105
- *
106
- * @see docs/about/graduation.md
107
- * @see packages/server/src/maintenance.ts
108
- */
109
- const MAINTENANCE_MAX_FOLD_ROWS = 2048;
110
- /** CF-free profile; the absent-context default. */
111
- const MAINTENANCE_PROFILE_CF_FREE = {
112
- gcInterval: 4,
113
- gcMaxMarks: 20,
114
- gcMaxSweeps: 10,
115
- maxFoldEntriesPerPass: 20,
116
- maxFoldBytes: MAINTENANCE_MAX_FOLD_BYTES_DEFAULT,
117
- maxFoldRows: MAINTENANCE_MAX_FOLD_ROWS
118
- };
119
- /**
120
- * CF-paid profile — opt-in via `BAERLY_MAINTENANCE_PROFILE=cf-paid`. A paid
121
- * isolate keeps the CPU-killable single-phase shape but has the 10,000-
122
- * subrequest budget (vs free's 50), so it reuses the `NODE_MAINTENANCE_*`
123
- * per-pass caps. CRITICAL: snapshot ceilings `maxFoldBytes` (`C`) /
124
- * `maxFoldRows` (`E`) are UNCHANGED — a profile moves rate, never correctness
125
- * (the equivalence invariant); the memory wall is raised via
126
- * `BAERLY_MAINTENANCE_MAX_FOLD_BYTES`.
127
- *
128
- * @see packages/adapter-cloudflare/src/worker.ts
129
- * @see docs/about/graduation.md
130
- */
131
- const MAINTENANCE_PROFILE_CF_PAID = {
132
- gcInterval: 2,
133
- gcMaxMarks: 200,
134
- gcMaxSweeps: 100,
135
- maxFoldEntriesPerPass: 200,
136
- maxFoldBytes: MAINTENANCE_MAX_FOLD_BYTES_DEFAULT,
137
- maxFoldRows: MAINTENANCE_MAX_FOLD_ROWS
138
- };
139
- /**
140
- * Rate-limit the defer-warn off SHARED current.json.last_warned_seq (not per-isolate
141
- * memory — CF recycles isolates). ~once per this many writes.
142
- *
143
- * @see packages/protocol/src/coordination/current-json.ts
144
- * @see packages/server/src/maintenance.ts
145
- */
146
- const MAINTENANCE_WARN_INTERVAL_WRITES = 1e3;
147
- /**
148
- * Canonical auth posture identifiers consumed by `BaerlyAppConfig.auth`,
149
- * the adapter resolution path (`packages/adapter-cloudflare/src/worker.ts`,
150
- * `packages/adapter-node/src/baerly-node.ts`), and `baerly doctor`.
151
- *
152
- * - `"none"` — no header check; pin every request to `config.tenant`.
153
- * For local dev, intranet, CLI tools — contexts where the network
154
- * seam itself is the trust boundary.
155
- * - `"shared-secret"` — bearer-token check; reads `SHARED_SECRET` from
156
- * the runtime env and pins every request to `config.tenant`.
157
- *
158
- * Custom verifiers (`cloudflareAccess`, `bearerJwt`, SigV4, …) bypass
159
- * this enum — pass a `Verifier` to the adapter factory's `verifier:`
160
- * option instead.
161
- */
162
- const AUTH_CONFIG_VALUES = ["none", "shared-secret"];
163
- /**
164
- * Locked error wording for `auth: "shared-secret"` + missing env var.
165
- * Same pinning rationale as {@link NO_AUTH_CONFIGURED_MESSAGE}.
166
- */
167
- const SHARED_SECRET_MISSING_MESSAGE = "baerly: auth=\"shared-secret\" but SHARED_SECRET env is empty/unset. Cloudflare: `wrangler secret put SHARED_SECRET`, or add to .dev.vars for local dev. Node: set in process env.";
168
- //#endregion
169
- //#region ../protocol/src/sha256.ts
170
- /**
171
- * SHA-256 of `bytes` as a 64-char lowercase hex string.
172
- *
173
- * Zero-import leaf module on purpose: co-locating this with heavier
174
- * runtime (snapshot-hash.ts / hashing.ts) would drag that module into
175
- * every consumer's bundle closure. Callers that want a truncated form
176
- * (e.g. {@link versionFromContent}'s content keys) slice the return.
177
- */
178
- const sha256Hex = async (bytes) => {
179
- const view = new Uint8Array(bytes.byteLength);
180
- view.set(bytes);
181
- const digest = await crypto.subtle.digest("SHA-256", view);
182
- const out = new Uint8Array(digest);
183
- let hex = "";
184
- for (let i = 0; i < out.length; i++) hex += out[i].toString(16).padStart(2, "0");
185
- return hex;
186
- };
187
- //#endregion
188
- //#region ../protocol/src/env.ts
189
- const PAAS_MARKERS = [
190
- "RAILWAY_ENVIRONMENT",
191
- "RENDER",
192
- "FLY_APP_NAME",
193
- "K_SERVICE",
194
- "DYNO",
195
- "KUBERNETES_SERVICE_HOST",
196
- "ECS_CONTAINER_METADATA_URI_V4"
197
- ];
198
- /**
199
- * True when `env` looks like a deployed environment: `NODE_ENV=production`
200
- * or any {@link PAAS_MARKERS} set to a non-empty value. Decides whether a
201
- * non-durable store (in-memory, local-fs) should fail loud instead of
202
- * silently serving a production workload. A CI runner (`CI` set to a
203
- * non-empty, non-`false` value) is never treated as deployed, so PaaS
204
- * markers from an in-cluster CI agent do not trip it.
205
- */
206
- const isDeployedEnv = (env) => {
207
- const inCi = (env["CI"] ?? "") !== "" && env["CI"] !== "false";
208
- return env["NODE_ENV"] === "production" || !inCi && PAAS_MARKERS.some((m) => (env[m] ?? "") !== "");
209
- };
210
- //#endregion
211
- //#region ../protocol/src/storage/key.ts
212
- /**
213
- * Validate a raw {@link Storage} object key against the portable baerly key
214
- * grammar before it is used to address an object. A key MUST be non-empty
215
- * and `/`-delimited, with **no path segment equal to `.` or `..`**.
216
- *
217
- * The dot-segment rule is not a vendor quirk — it is a *client-side*
218
- * universal: RFC 3986 §5.2.4 "remove dot segments" is mandatory for every
219
- * conformant URL parser, so `<endpoint>/<bucket>/.` normalizes to
220
- * `<endpoint>/<bucket>/` and `<endpoint>/<bucket>/..` escapes the bucket
221
- * entirely — *before* the request is signed or sent, in TypeScript, Go,
222
- * Rust, Python, or any language a `Storage` port is written in. Such a key
223
- * cannot be addressed over the S3/R2 HTTP API regardless of backend; a bare
224
- * `.` PUT surfaces as a confusing bucket-root 403 rather than a clear
225
- * rejection. The kernel never emits such keys (caller-controlled segments
226
- * are already screened by `assertPathSegment` one layer up), so this is the
227
- * boundary guard that makes every adapter reject them *identically* and
228
- * gives ports a single, testable contract.
229
- *
230
- * Thrown as `BaerlyError{code:"InvalidConfig"}` to match the sibling
231
- * key/segment guards (`assertKeyWithinLimit`, `assertPathSegment`). The
232
- * full-key 1024-byte ceiling is enforced separately on the write path
233
- * (`assertKeyWithinLimit`), where multi-segment keys are assembled.
234
- *
235
- * @see docs/spec/storage-compatibility.md — "Key namespace"
236
- */
237
- function assertValidStorageKey(key) {
238
- if (key.length === 0) throw new BaerlyError("InvalidConfig", "storage key must be a non-empty string");
239
- for (const segment of key.split("/")) if (segment === "." || segment === "..") throw new BaerlyError("InvalidConfig", `storage key may not contain a "." or ".." path segment — RFC 3986 dot-segment removal makes it unaddressable over the S3/R2 HTTP API: ${JSON.stringify(key)}`);
240
- }
241
- //#endregion
242
- //#region ../protocol/src/storage/memory.ts
243
- const utf8Encoder$1 = new TextEncoder();
244
- const EPHEMERAL_ENV_OPT_IN = "BAERLY_ALLOW_EPHEMERAL_STORAGE";
245
- const readProcessEnv = () => globalThis.process?.env ?? {};
246
- let warnedEphemeralInDeployment = false;
247
- const assertEphemeralAllowed = (explicitOptIn) => {
248
- const env = readProcessEnv();
249
- if (!isDeployedEnv(env)) return;
250
- if (!explicitOptIn && env[EPHEMERAL_ENV_OPT_IN] !== "true") throw new BaerlyError("InvalidConfig", `Refusing to use in-memory storage in a deployed environment — all data is lost on restart. MemoryStorage is for tests/local dev only; configure a durable bucket (AWS S3 / Cloudflare R2). To intentionally opt in: new MemoryStorage({ ephemeral: true }) or ${EPHEMERAL_ENV_OPT_IN}=true.`);
251
- if (!warnedEphemeralInDeployment) {
252
- warnedEphemeralInDeployment = true;
253
- console.warn("[baerly] in-memory storage in a deployed environment — ALL DATA IS LOST ON RESTART.");
254
- }
255
- };
256
- /**
257
- * Compare two keys by their UTF-8 byte sequences — the order S3 and
258
- * R2 use for `list`. JavaScript's default string sort compares UTF-16
259
- * code units, which diverges from UTF-8 byte order for supplementary-
260
- * plane characters (e.g. emoji sort before high-BMP characters under
261
- * UTF-16 but after them in UTF-8). Using this keeps the in-memory
262
- * reference backend faithful to the real adapters. (All kernel keys
263
- * are ASCII base-32, where the two orders coincide — this only
264
- * matters for adversarial / non-ASCII keys.)
265
- */
266
- const compareKeysUtf8$1 = (a, b) => {
267
- const ba = utf8Encoder$1.encode(a);
268
- const bb = utf8Encoder$1.encode(b);
269
- const n = Math.min(ba.length, bb.length);
270
- for (let i = 0; i < n; i++) if (ba[i] !== bb[i]) return ba[i] - bb[i];
271
- return ba.length - bb.length;
272
- };
273
- /**
274
- * In-memory `Storage`. The randomized property test runs against
275
- * this; it must be deterministic — no clocks beyond the caller's,
276
- * no randomness, no I/O.
277
- *
278
- * ETags are a monotonically increasing hex counter formatted in the
279
- * `"<hex>"` shape S3 returns (the surrounding double-quotes are part
280
- * of the ETag header value). Keys are stored verbatim.
281
- *
282
- * **Test-confidence caveat:** every method resolves on the microtask
283
- * queue with no real concurrency, so `put` reads `existing` and writes
284
- * back with no interleaving. MemoryStorage therefore CANNOT exercise the
285
- * compare-and-swap race the writer-fence model depends on — two writers
286
- * contending on one `current.json` always serialize here, so a green run
287
- * is necessary but not sufficient. That race is covered only by the
288
- * randomized integration tests against real S3 / Minio
289
- * (`tests/integration/randomized.test.ts`, `node-minio` variant).
290
- * Relatedly, `serverDate` returns the local clock, not an independent
291
- * server clock (see the `put` return below).
292
- */
293
- var MemoryStorage = class {
294
- #objects = /* @__PURE__ */ new Map();
295
- #etagCounter = 0;
296
- constructor(opts) {
297
- assertEphemeralAllowed(opts?.ephemeral === true);
298
- }
299
- #nextEtag() {
300
- this.#etagCounter += 1;
301
- return `"${this.#etagCounter.toString(16)}"`;
302
- }
303
- async get(key, opts) {
304
- assertValidStorageKey(key);
305
- opts?.signal?.throwIfAborted();
306
- const stored = this.#objects.get(key);
307
- if (stored === void 0) return null;
308
- if (opts?.ifNoneMatch !== void 0 && opts.ifNoneMatch === stored.etag) return null;
309
- return {
310
- body: stored.body,
311
- etag: stored.etag
312
- };
313
- }
314
- async put(key, body, opts) {
315
- assertValidStorageKey(key);
316
- opts?.signal?.throwIfAborted();
317
- const existing = this.#objects.get(key);
318
- if (opts?.ifNoneMatch === "*" && existing !== void 0) throw new BaerlyError("Conflict", `PUT ${key}: precondition failed (ifNoneMatch="*" but key exists)`);
319
- if (opts?.ifMatch !== void 0) {
320
- if (existing === void 0) throw new BaerlyError("Conflict", `PUT ${key}: precondition failed (ifMatch=${opts.ifMatch} but key does not exist)`);
321
- if (existing.etag !== opts.ifMatch) throw new BaerlyError("Conflict", `PUT ${key}: precondition failed (ifMatch=${opts.ifMatch} but current ETag is ${existing.etag})`);
322
- }
323
- const etag = this.#nextEtag();
324
- this.#objects.set(key, {
325
- body,
326
- etag,
327
- ...opts?.contentType !== void 0 && { contentType: opts.contentType }
328
- });
329
- return {
330
- etag,
331
- serverDate: /* @__PURE__ */ new Date()
332
- };
333
- }
334
- async delete(key, opts) {
335
- assertValidStorageKey(key);
336
- opts?.signal?.throwIfAborted();
337
- this.#objects.delete(key);
338
- }
339
- async *list(prefix, opts) {
340
- opts?.signal?.throwIfAborted();
341
- const startAfter = opts?.startAfter ?? "";
342
- const maxKeys = opts?.maxKeys ?? Infinity;
343
- const sorted = [...this.#objects.keys()].filter((k) => k.startsWith(prefix) && compareKeysUtf8$1(k, startAfter) > 0).toSorted(compareKeysUtf8$1);
344
- let yielded = 0;
345
- for (const key of sorted) {
346
- if (yielded >= maxKeys) return;
347
- opts?.signal?.throwIfAborted();
348
- const stored = this.#objects.get(key);
349
- if (stored === void 0) continue;
350
- yield {
351
- key,
352
- etag: stored.etag
353
- };
354
- yielded += 1;
355
- }
356
- }
357
- /**
358
- * Test-only: drop all objects.
359
- * @internal
360
- */
361
- _clear() {
362
- this.#objects.clear();
363
- this.#etagCounter = 0;
364
- }
365
- };
366
- const sharedPerBucket = /* @__PURE__ */ new Map();
367
- /**
368
- * Get the process-singleton {@link MemoryStorage} for the named
369
- * bucket, creating one on first access. Use this when constructing
370
- * a `Storage` directly so multiple consumers in the same process
371
- * see each other's writes for the same bucket name.
372
- */
373
- const getOrCreateMemoryStorageForBucket = (bucket) => {
374
- let s = sharedPerBucket.get(bucket);
375
- if (s === void 0) {
376
- s = new MemoryStorage();
377
- sharedPerBucket.set(bucket, s);
378
- }
379
- return s;
380
- };
381
- //#endregion
382
- //#region ../protocol/src/types.ts
383
- /**
384
- * Mint a fresh {@link UUID}. The cast lives here so callers don't sprinkle
385
- * `<UUID>crypto.randomUUID()` throughout the codebase.
386
- */
387
- const uuid = () => crypto.randomUUID();
388
- /**
389
- * Mint the trailing seq segment of an LSN (`<base32-time>_<sess>_<seq>`).
390
- * Uses `COUNT_BIT_WIDTH` (see `packages/protocol/src/constants.ts`) to
391
- * produce an 11-char descending base-32 string with a domain of
392
- * 0 .. `Number.MAX_SAFE_INTEGER` (2^53 − 1).
393
- *
394
- * Used together with {@link timestamp} (the descending base-32
395
- * encoding of millis-since-epoch). See {@link uint2strDesc} for the
396
- * lex-reversal property both rely on.
397
- */
398
- const countKey = (number) => uint2strDesc(number, 53);
399
- const uint2str = (num, bits) => {
400
- const maxBase32Length = Math.ceil(bits / 5);
401
- return num.toString(32).padStart(maxBase32Length, "0");
402
- };
403
- /**
404
- * Lex-reversed base-32 encoder for an unsigned integer in `[0, 2^bits)`.
405
- * Pads to `Math.ceil(bits/5)` chars so all values in the domain compare
406
- * lex against each other.
407
- *
408
- * **Order-reversal property:** for any pair of integers `a < b` (with
409
- * `a, b ∈ [0, 2^bits)`), `uint2strDesc(a, bits) > uint2strDesc(b, bits)`
410
- * lexicographically. Composed into an LSN of shape
411
- * `<base32-time>_<session>_<seq>` (where both `<base32-time>` and
412
- * `<seq>` use this encoder), a forward `Storage.list(prefix)`
413
- * returns log entries in reverse-causal order — newer entries first.
414
- *
415
- * This is the structural property the read path leans on:
416
- * `ListObjectsV2` is forward-only on S3, but the descending encoding
417
- * makes "fetch the K newest entries" a single bounded LIST with
418
- * `maxKeys=K` instead of a full-population LIST + in-memory reverse.
419
- *
420
- * Behaviourally verified across randomized populations in
421
- * `packages/protocol/src/lsn-reverse-list.test.ts`. Quantified
422
- * bytes-listed reduction is in
423
- * `docs/spec/attachments/lsn-reverse-walk-baseline.json`
424
- * (run `pnpm bench:lsn-reverse-walk` to reproduce). Spec rationale:
425
- * `docs/spec/sync-protocol.md` §"LSNs, wall clocks, and downstream
426
- * consumers".
427
- */
428
- const uint2strDesc = (num, bits) => {
429
- return uint2str(Math.pow(2, bits) - 1 - num, bits);
430
- };
431
- //#endregion
432
- //#region ../protocol/src/time.ts
433
- /**
434
- * Encode an epoch-millis instant as a lexicographically-DEScending
435
- * base-32 key (newer sorts first). The load-bearing LSN ordering
436
- * primitive — see {@link uint2strDesc}.
437
- *
438
- * `epoch` is REQUIRED and validated. There is no default: an argless
439
- * call used to encode `0`, which descending-sorts as the most-ancient
440
- * instant and would silently invert ordering. A non-finite, negative,
441
- * or out-of-range `epoch` (e.g. a broken `Date.now()` returning `NaN`)
442
- * would corrupt the key silently rather than fail loud, so it throws
443
- * `Internal` instead — this is an invariant violation, not caller config
444
- * (the only legitimate argument is `Date.now()`).
445
- */
446
- const timestamp = (epoch) => {
447
- if (!Number.isInteger(epoch) || epoch < 0 || epoch >= 2 ** 42) throw new BaerlyError("Internal", `timestamp(): epoch must be a non-negative integer < 2^42 (got ${epoch})`);
448
- return uint2strDesc(epoch, 42);
449
- };
450
- /**
451
- * Delay for `ms` milliseconds, with optional cancellation. If
452
- * `signal` aborts before the delay elapses, the returned promise
453
- * rejects with `signal.reason`. Used by `S3HttpStorage`'s retry
454
- * loop so shutdown can interrupt an in-flight backoff instead of
455
- * waiting for it to complete.
456
- */
457
- const delay = (ms, signal) => new Promise((resolve, reject) => {
458
- if (signal?.aborted) {
459
- reject(signal.reason);
460
- return;
461
- }
462
- const timer = setTimeout(resolve, ms);
463
- signal?.addEventListener("abort", () => {
464
- clearTimeout(timer);
465
- reject(signal.reason);
466
- }, { once: true });
467
- });
468
- //#endregion
469
- //#region ../dev/src/local-fs.ts
470
- /**
471
- * Reserved prefix for the in-directory create-if-absent temp (see `put`).
472
- * `walk`/`list` skip it; it is internal/transient and never a real key.
473
- */
474
- const TEMP_PREFIX = ".baerly-tmp-";
475
- const utf8Encoder = new TextEncoder();
476
- /**
477
- * Compare two keys by their UTF-8 byte sequences — the order S3 and R2
478
- * use for `list`. JS's default string sort compares UTF-16 code units,
479
- * which diverges from UTF-8 byte order for supplementary-plane
480
- * characters; using this keeps `list` ordering faithful to the real
481
- * adapters. (All kernel keys are ASCII base-32, where both orders
482
- * coincide.)
483
- */
484
- const compareKeysUtf8 = (a, b) => {
485
- const ba = utf8Encoder.encode(a);
486
- const bb = utf8Encoder.encode(b);
487
- const n = Math.min(ba.length, bb.length);
488
- for (let i = 0; i < n; i++) if (ba[i] !== bb[i]) return ba[i] - bb[i];
489
- return ba.length - bb.length;
490
- };
491
- /**
492
- * `Storage` backed by a directory tree. Keys may contain `/` and map
493
- * to nested directories on disk so `ls`/`cat` work as expected.
494
- *
495
- * ETags are content-addressed: `"<sha-256-hex>"` (the surrounding
496
- * double-quotes match S3's wire format for non-multipart objects).
497
- * Identical bodies share an ETag across runs — the property that
498
- * makes this adapter useful for fixture-based tests.
499
- *
500
- * Writes are atomic via `writeFile(temp) + rename(final)`; readers
501
- * never see a partially-written file.
502
- *
503
- * `ifNoneMatch:"*"` uses a same-dir temp + `link(2)` (atomic exclusive
504
- * create; `EEXIST` ⇒ key exists) so concurrent creates have exactly one
505
- * winner. Same-dir avoids `EXDEV`; temp+`link` over `open(…,"wx")` keeps
506
- * a partially-written new key invisible to a concurrent reader. `ifMatch`
507
- * keeps the `rename` path (in-process TOCTOU only; cross-process
508
- * contention uses the S3 / Minio `If-Match` path).
509
- *
510
- * Node-only — imports `node:fs`, `node:path`, `node:crypto`. Lives in
511
- * `@baerly/dev` because the protocol kernel is pure-modules / no I/O
512
- * and must remain Worker-bundleable.
513
- */
514
- var LocalFsStorage = class {
515
- #root;
516
- constructor(opts) {
517
- this.#root = resolve(opts.root);
518
- }
519
- async get(key, opts) {
520
- opts?.signal?.throwIfAborted();
521
- const path = this.#pathFor(key);
522
- let body;
523
- try {
524
- body = await readFile(path);
525
- } catch (error) {
526
- if (isErrnoException(error) && error.code === "ENOENT") return null;
527
- throw new BaerlyError("InvalidResponse", `LocalFsStorage.get(${key}): ${error.message}`, error);
528
- }
529
- const etag = etagOf(body);
530
- if (opts?.ifNoneMatch !== void 0 && opts.ifNoneMatch === etag) return null;
531
- return {
532
- body: toUint8Array(body),
533
- etag
534
- };
535
- }
536
- async put(key, body, opts) {
537
- opts?.signal?.throwIfAborted();
538
- const path = this.#pathFor(key);
539
- const newEtag = etagOf(body);
540
- await mkdir(dirname(path), { recursive: true });
541
- if (opts?.ifNoneMatch === "*") {
542
- const tmp = join(dirname(path), `${TEMP_PREFIX}${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`);
543
- try {
544
- await writeFile(tmp, body);
545
- await link(tmp, path);
546
- } catch (error) {
547
- if (isErrnoException(error) && error.code === "EEXIST") throw new BaerlyError("Conflict", `PUT ${key}: precondition failed (ifNoneMatch="*" but key exists)`);
548
- throw new BaerlyError("InvalidResponse", `LocalFsStorage.put(${key}): ${error.message}`, error);
549
- } finally {
550
- await rm(tmp, { force: true }).catch(() => {});
551
- }
552
- return {
553
- etag: newEtag,
554
- serverDate: /* @__PURE__ */ new Date()
555
- };
556
- }
557
- if (opts?.ifMatch !== void 0) {
558
- const existing = await readExisting(path);
559
- if (existing === null) throw new BaerlyError("Conflict", `PUT ${key}: precondition failed (ifMatch=${opts.ifMatch} but key does not exist)`);
560
- const currentEtag = etagOf(existing);
561
- if (currentEtag !== opts.ifMatch) throw new BaerlyError("Conflict", `PUT ${key}: precondition failed (ifMatch=${opts.ifMatch} but current ETag is ${currentEtag})`);
562
- }
563
- const tmp = join(tmpdir(), `baerly-localfs-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`);
564
- await writeFile(tmp, body);
565
- try {
566
- await rename(tmp, path);
567
- } catch (error) {
568
- await rm(tmp, { force: true }).catch(() => {});
569
- throw new BaerlyError("InvalidResponse", `LocalFsStorage.put(${key}): ${error.message}`, error);
570
- }
571
- return {
572
- etag: newEtag,
573
- serverDate: /* @__PURE__ */ new Date()
574
- };
575
- }
576
- async delete(key, opts) {
577
- opts?.signal?.throwIfAborted();
578
- const path = this.#pathFor(key);
579
- try {
580
- await rm(path);
581
- } catch (error) {
582
- if (isErrnoException(error) && error.code === "ENOENT") return;
583
- throw new BaerlyError("InvalidResponse", `LocalFsStorage.delete(${key}): ${error.message}`, error);
584
- }
585
- }
586
- async *list(prefix, opts) {
587
- opts?.signal?.throwIfAborted();
588
- const startAfter = opts?.startAfter ?? "";
589
- const maxKeys = opts?.maxKeys ?? Infinity;
590
- const keys = [];
591
- await walk(this.#root, keys);
592
- keys.sort(compareKeysUtf8);
593
- let yielded = 0;
594
- for (const key of keys) {
595
- if (!key.startsWith(prefix)) continue;
596
- if (compareKeysUtf8(key, startAfter) <= 0) continue;
597
- if (yielded >= maxKeys) return;
598
- opts?.signal?.throwIfAborted();
599
- yield {
600
- key,
601
- etag: etagOf(await readFile(this.#pathFor(key)))
602
- };
603
- yielded += 1;
604
- }
605
- }
606
- /**
607
- * Map an S3-style key to a filesystem path under `#root`, rejecting
608
- * anything that could escape the root (path traversal, absolute
609
- * paths, empty segments, backslashes). All rejections throw
610
- * `BaerlyError("InvalidConfig", …)`.
611
- */
612
- #pathFor(key) {
613
- assertValidStorageKey(key);
614
- if (key.startsWith("/")) throw new BaerlyError("InvalidConfig", `LocalFsStorage: leading "/" in key: ${key}`);
615
- if (key.includes("\\")) throw new BaerlyError("InvalidConfig", `LocalFsStorage: backslash in key: ${key}`);
616
- const segments = key.split("/");
617
- if (segments.some((s) => s === "" || s === "." || s === "..")) throw new BaerlyError("InvalidConfig", `LocalFsStorage: illegal segment in key: ${key}`);
618
- const path = join(this.#root, ...segments);
619
- if (path !== this.#root && !path.startsWith(this.#root + sep)) throw new BaerlyError("InvalidConfig", `LocalFsStorage: resolved path escapes root: ${key}`);
620
- return path;
621
- }
622
- };
623
- const etagOf = (body) => {
624
- return `"${createHash("sha256").update(body).digest("hex")}"`;
625
- };
626
- const toUint8Array = (buf) => new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
627
- const isErrnoException = (e) => typeof e === "object" && e !== null && "code" in e;
628
- const readExisting = async (path) => {
629
- try {
630
- return await readFile(path);
631
- } catch (error) {
632
- if (isErrnoException(error) && error.code === "ENOENT") return null;
633
- throw error;
634
- }
635
- };
636
- /**
637
- * Recursive directory walk. Yields filesystem-relative paths in
638
- * forward-slash form so callers can use them as S3-style keys
639
- * regardless of the host's path separator.
640
- */
641
- const walk = async (root, out) => {
642
- const stack = [root];
643
- while (stack.length > 0) {
644
- const dir = stack.pop();
645
- let entries;
646
- try {
647
- entries = await readdir(dir, { withFileTypes: true });
648
- } catch (error) {
649
- if (isErrnoException(error) && error.code === "ENOENT") continue;
650
- throw error;
651
- }
652
- for (const entry of entries) {
653
- const full = join(dir, entry.name);
654
- if (entry.isDirectory()) stack.push(full);
655
- else if (entry.isFile() && !entry.name.startsWith(TEMP_PREFIX)) out.push(relative(root, full).split(sep).join(posix.sep));
656
- }
657
- }
658
- };
659
- /*! @rgrove/parse-xml v4.2.1 | ISC License | Copyright Ryan Grove */
660
- //#endregion
661
- //#region ../adapter-node/src/xml.ts
662
- var import_browser = (/* @__PURE__ */ __commonJSMin(((exports, module) => {
663
- var __defProp = Object.defineProperty;
664
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
665
- var __getOwnPropNames = Object.getOwnPropertyNames;
666
- var __hasOwnProp = Object.prototype.hasOwnProperty;
667
- var __export = (target, all) => {
668
- for (var name in all) __defProp(target, name, {
669
- get: all[name],
670
- enumerable: true
671
- });
672
- };
673
- var __copyProps = (to, from, except, desc) => {
674
- if (from && typeof from === "object" || typeof from === "function") {
675
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
676
- get: () => from[key],
677
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
678
- });
679
- }
680
- return to;
681
- };
682
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
683
- var src_exports = {};
684
- __export(src_exports, {
685
- XmlCdata: () => XmlCdata,
686
- XmlComment: () => XmlComment,
687
- XmlDeclaration: () => XmlDeclaration,
688
- XmlDocument: () => XmlDocument,
689
- XmlDocumentType: () => XmlDocumentType,
690
- XmlElement: () => XmlElement,
691
- XmlError: () => XmlError,
692
- XmlNode: () => XmlNode,
693
- XmlProcessingInstruction: () => XmlProcessingInstruction,
694
- XmlText: () => XmlText,
695
- parseXml: () => parseXml
696
- });
697
- module.exports = __toCommonJS(src_exports);
698
- var emptyString = "";
699
- var surrogatePair = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
700
- var StringScanner = class {
701
- constructor(string) {
702
- this.k = this.u(string, true);
703
- this.d = 0;
704
- this.length = string.length;
705
- this.l = this.k !== this.length;
706
- this.h = string;
707
- if (this.l) {
708
- let charsToBytes = [];
709
- for (let byteIndex = 0, charIndex = 0; charIndex < this.k; ++charIndex) {
710
- charsToBytes[charIndex] = byteIndex;
711
- byteIndex += string.codePointAt(byteIndex) > 65535 ? 2 : 1;
712
- }
713
- this.A = charsToBytes;
714
- }
715
- }
716
- /**
717
- * Whether the current character index is at the end of the input string.
718
- */
719
- get B() {
720
- return this.d >= this.k;
721
- }
722
- /**
723
- * Returns the number of characters in the given string, which may differ from
724
- * the byte length if the string contains multibyte characters.
725
- */
726
- u(string, multiByteSafe = this.l) {
727
- return multiByteSafe ? string.replace(surrogatePair, "_").length : string.length;
728
- }
729
- /**
730
- * Advances the scanner by the given number of characters, stopping if the end
731
- * of the string is reached.
732
- */
733
- p(count = 1) {
734
- this.d = Math.min(this.k, this.d + count);
735
- }
736
- /**
737
- * Returns the byte index of the given character index in the string. The two
738
- * may differ in strings that contain multibyte characters.
739
- */
740
- f(charIndex = this.d) {
741
- var _a;
742
- return this.l ? (_a = this.A[charIndex]) != null ? _a : Infinity : charIndex;
743
- }
744
- /**
745
- * Consumes and returns the given number of characters if possible, advancing
746
- * the scanner and stopping if the end of the string is reached.
747
- *
748
- * If no characters could be consumed, an empty string will be returned.
749
- */
750
- G(charCount = 1) {
751
- let chars = this.m(charCount);
752
- this.p(charCount);
753
- return chars;
754
- }
755
- /**
756
- * Consumes and returns the given number of bytes if possible, advancing the
757
- * scanner and stopping if the end of the string is reached.
758
- *
759
- * It's up to the caller to ensure that the given byte count doesn't split a
760
- * multibyte character.
761
- *
762
- * If no bytes could be consumed, an empty string will be returned.
763
- */
764
- v(byteCount) {
765
- let byteIndex = this.f();
766
- let result = this.h.slice(byteIndex, byteIndex + byteCount);
767
- this.p(this.u(result));
768
- return result;
769
- }
770
- /**
771
- * Consumes and returns all characters for which the given function returns
772
- * `true`, stopping when `false` is returned or the end of the input is
773
- * reached.
774
- */
775
- w(fn) {
776
- let { length, l: multiByteMode, h: string } = this;
777
- let startByteIndex = this.f();
778
- let endByteIndex = startByteIndex;
779
- if (multiByteMode) while (endByteIndex < length) {
780
- let char = string[endByteIndex];
781
- let isSurrogatePair = char >= "\ud800" && char <= "\udbff";
782
- if (isSurrogatePair) char += string[endByteIndex + 1];
783
- if (!fn(char)) break;
784
- endByteIndex += isSurrogatePair ? 2 : 1;
785
- }
786
- else while (endByteIndex < length && fn(string[endByteIndex])) ++endByteIndex;
787
- return this.v(endByteIndex - startByteIndex);
788
- }
789
- /**
790
- * Consumes the given string if it exists at the current character index, and
791
- * advances the scanner.
792
- *
793
- * If the given string doesn't exist at the current character index, an empty
794
- * string will be returned and the scanner will not be advanced.
795
- */
796
- b(stringToConsume) {
797
- let { length } = stringToConsume;
798
- let byteIndex = this.f();
799
- if (stringToConsume === this.h.slice(byteIndex, byteIndex + length)) {
800
- this.p(length === 1 ? 1 : this.u(stringToConsume));
801
- return stringToConsume;
802
- }
803
- return emptyString;
804
- }
805
- /**
806
- * Consumes characters until the given global regex is matched, advancing the
807
- * scanner up to (but not beyond) the beginning of the match. If the regex
808
- * doesn't match, nothing will be consumed.
809
- *
810
- * Returns the consumed string, or an empty string if nothing was consumed.
811
- */
812
- x(regex) {
813
- let matchByteIndex = this.h.slice(this.f()).search(regex);
814
- return matchByteIndex > 0 ? this.v(matchByteIndex) : emptyString;
815
- }
816
- /**
817
- * Consumes characters until the given string is found, advancing the scanner
818
- * up to (but not beyond) that point. If the string is never found, nothing
819
- * will be consumed.
820
- *
821
- * Returns the consumed string, or an empty string if nothing was consumed.
822
- */
823
- s(searchString) {
824
- let byteIndex = this.f();
825
- let matchByteIndex = this.h.indexOf(searchString, byteIndex);
826
- return matchByteIndex > 0 ? this.v(matchByteIndex - byteIndex) : emptyString;
827
- }
828
- /**
829
- * Returns the given number of characters starting at the current character
830
- * index, without advancing the scanner and without exceeding the end of the
831
- * input string.
832
- */
833
- m(count = 1) {
834
- let { d: charIndex, h: string } = this;
835
- return this.l ? string.slice(this.f(charIndex), this.f(charIndex + count)) : string.slice(charIndex, charIndex + count);
836
- }
837
- /**
838
- * Resets the scanner position to the given character _index_, or to the start
839
- * of the input string if no index is given.
840
- *
841
- * If _index_ is negative, the scanner position will be moved backward by that
842
- * many characters, stopping if the beginning of the string is reached.
843
- */
844
- n(index = 0) {
845
- this.d = index >= 0 ? Math.min(this.k, index) : Math.max(0, this.d + index);
846
- }
847
- };
848
- var attValueCharDoubleQuote = /["&<]/;
849
- var attValueCharSingleQuote = /['&<]/;
850
- var attValueNormalizedWhitespace = /\r\n|[\n\r\t]/g;
851
- var endCharData = /<|&|]]>/;
852
- var predefinedEntities = Object.freeze(Object.assign(/* @__PURE__ */ Object.create(null), {
853
- amp: "&",
854
- apos: "'",
855
- gt: ">",
856
- lt: "<",
857
- quot: "\""
858
- }));
859
- function isNameChar(char) {
860
- let cp = char.codePointAt(0);
861
- return cp >= 97 && cp <= 122 || cp >= 65 && cp <= 90 || cp >= 48 && cp <= 57 || cp === 45 || cp === 46 || cp === 183 || cp >= 768 && cp <= 879 || cp === 8255 || cp === 8256 || isNameStartChar(char, cp);
862
- }
863
- function isNameStartChar(char, cp = char.codePointAt(0)) {
864
- return cp >= 97 && cp <= 122 || cp >= 65 && cp <= 90 || cp === 58 || cp === 95 || cp >= 192 && cp <= 214 || cp >= 216 && cp <= 246 || cp >= 248 && cp <= 767 || cp >= 880 && cp <= 893 || cp >= 895 && cp <= 8191 || cp === 8204 || cp === 8205 || cp >= 8304 && cp <= 8591 || cp >= 11264 && cp <= 12271 || cp >= 12289 && cp <= 55295 || cp >= 63744 && cp <= 64975 || cp >= 65008 && cp <= 65533 || cp >= 65536 && cp <= 983039;
865
- }
866
- function isReferenceChar(char) {
867
- return char === "#" || isNameChar(char);
868
- }
869
- function isWhitespace(char) {
870
- let cp = char.codePointAt(0);
871
- return cp === 32 || cp === 9 || cp === 10 || cp === 13;
872
- }
873
- function isXmlCodePoint(cp) {
874
- return cp >= 32 && cp <= 55295 || cp === 10 || cp === 9 || cp === 13 || cp >= 57344 && cp <= 65533 || cp >= 65536 && cp <= 1114111;
875
- }
876
- var _XmlNode = class _XmlNode {
877
- constructor() {
878
- /**
879
- * Parent node of this node, or `null` if this node has no parent.
880
- */
881
- this.parent = null;
882
- /**
883
- * Starting byte offset of this node in the original XML string, or `-1` if
884
- * the offset is unknown.
885
- */
886
- this.start = -1;
887
- /**
888
- * Ending byte offset of this node in the original XML string, or `-1` if the
889
- * offset is unknown.
890
- */
891
- this.end = -1;
892
- }
893
- /**
894
- * Document that contains this node, or `null` if this node is not associated
895
- * with a document.
896
- */
897
- get document() {
898
- var _a, _b;
899
- return (_b = (_a = this.parent) == null ? void 0 : _a.document) != null ? _b : null;
900
- }
901
- /**
902
- * Whether this node is the root node of the document (also known as the
903
- * document element).
904
- */
905
- get isRootNode() {
906
- return this.parent !== null && this.parent === this.document && this.type === _XmlNode.TYPE_ELEMENT;
907
- }
908
- /**
909
- * Whether whitespace should be preserved in the content of this element and
910
- * its children.
911
- *
912
- * This is influenced by the value of the special `xml:space` attribute, and
913
- * will be `true` for any node whose `xml:space` attribute is set to
914
- * "preserve". If a node has no such attribute, it will inherit the value of
915
- * the nearest ancestor that does (if any).
916
- *
917
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space
918
- */
919
- get preserveWhitespace() {
920
- var _a;
921
- return !!((_a = this.parent) == null ? void 0 : _a.preserveWhitespace);
922
- }
923
- /**
924
- * Type of this node.
925
- *
926
- * The value of this property is a string that matches one of the static
927
- * `TYPE_*` properties on the `XmlNode` class (e.g. `TYPE_ELEMENT`,
928
- * `TYPE_TEXT`, etc.).
929
- *
930
- * The `XmlNode` class itself is a base class and doesn't have its own type
931
- * name.
932
- */
933
- get type() {
934
- return "";
935
- }
936
- /**
937
- * Returns a JSON-serializable object representing this node, minus properties
938
- * that could result in circular references.
939
- */
940
- toJSON() {
941
- let json = { type: this.type };
942
- if (this.isRootNode) json.isRootNode = true;
943
- if (this.preserveWhitespace) json.preserveWhitespace = true;
944
- if (this.start !== -1) {
945
- json.start = this.start;
946
- json.end = this.end;
947
- }
948
- return json;
949
- }
950
- };
951
- /**
952
- * Type value for an `XmlCdata` node.
953
- */
954
- _XmlNode.TYPE_CDATA = "cdata";
955
- /**
956
- * Type value for an `XmlComment` node.
957
- */
958
- _XmlNode.TYPE_COMMENT = "comment";
959
- /**
960
- * Type value for an `XmlDocument` node.
961
- */
962
- _XmlNode.TYPE_DOCUMENT = "document";
963
- /**
964
- * Type value for an `XmlDocumentType` node.
965
- */
966
- _XmlNode.TYPE_DOCUMENT_TYPE = "doctype";
967
- /**
968
- * Type value for an `XmlElement` node.
969
- */
970
- _XmlNode.TYPE_ELEMENT = "element";
971
- /**
972
- * Type value for an `XmlProcessingInstruction` node.
973
- */
974
- _XmlNode.TYPE_PROCESSING_INSTRUCTION = "pi";
975
- /**
976
- * Type value for an `XmlText` node.
977
- */
978
- _XmlNode.TYPE_TEXT = "text";
979
- /**
980
- * Type value for an `XmlDeclaration` node.
981
- */
982
- _XmlNode.TYPE_XML_DECLARATION = "xmldecl";
983
- var XmlNode = _XmlNode;
984
- var XmlText = class extends XmlNode {
985
- constructor(text = "") {
986
- super();
987
- this.text = text;
988
- }
989
- get type() {
990
- return XmlNode.TYPE_TEXT;
991
- }
992
- toJSON() {
993
- return Object.assign(XmlNode.prototype.toJSON.call(this), { text: this.text });
994
- }
995
- };
996
- var XmlCdata = class extends XmlText {
997
- get type() {
998
- return XmlNode.TYPE_CDATA;
999
- }
1000
- };
1001
- var XmlComment = class extends XmlNode {
1002
- constructor(content = "") {
1003
- super();
1004
- this.content = content;
1005
- }
1006
- get type() {
1007
- return XmlNode.TYPE_COMMENT;
1008
- }
1009
- toJSON() {
1010
- return Object.assign(XmlNode.prototype.toJSON.call(this), { content: this.content });
1011
- }
1012
- };
1013
- var XmlDeclaration = class extends XmlNode {
1014
- constructor(version, encoding, standalone) {
1015
- super();
1016
- this.version = version;
1017
- this.encoding = encoding != null ? encoding : null;
1018
- this.standalone = standalone != null ? standalone : null;
1019
- }
1020
- get type() {
1021
- return XmlNode.TYPE_XML_DECLARATION;
1022
- }
1023
- toJSON() {
1024
- let json = XmlNode.prototype.toJSON.call(this);
1025
- json.version = this.version;
1026
- for (let key of ["encoding", "standalone"]) if (this[key] !== null) json[key] = this[key];
1027
- return json;
1028
- }
1029
- };
1030
- var XmlElement = class _XmlElement extends XmlNode {
1031
- constructor(name, attributes = /* @__PURE__ */ Object.create(null), children = []) {
1032
- super();
1033
- this.name = name;
1034
- this.attributes = attributes;
1035
- this.children = children;
1036
- }
1037
- /**
1038
- * Whether this element is empty (meaning it has no children).
1039
- */
1040
- get isEmpty() {
1041
- return this.children.length === 0;
1042
- }
1043
- get preserveWhitespace() {
1044
- let node = this;
1045
- while (node instanceof _XmlElement) {
1046
- if ("xml:space" in node.attributes) return node.attributes["xml:space"] === "preserve";
1047
- node = node.parent;
1048
- }
1049
- return false;
1050
- }
1051
- /**
1052
- * Text content of this element and all its descendants.
1053
- */
1054
- get text() {
1055
- return this.children.map((child) => "text" in child ? child.text : "").join("");
1056
- }
1057
- get type() {
1058
- return XmlNode.TYPE_ELEMENT;
1059
- }
1060
- toJSON() {
1061
- return Object.assign(XmlNode.prototype.toJSON.call(this), {
1062
- name: this.name,
1063
- attributes: this.attributes,
1064
- children: this.children.map((child) => child.toJSON())
1065
- });
1066
- }
1067
- };
1068
- var XmlDocument = class extends XmlNode {
1069
- constructor(children = []) {
1070
- super();
1071
- this.children = children;
1072
- }
1073
- get document() {
1074
- return this;
1075
- }
1076
- /**
1077
- * Root element of this document, or `null` if this document is empty.
1078
- */
1079
- get root() {
1080
- for (let child of this.children) if (child instanceof XmlElement) return child;
1081
- return null;
1082
- }
1083
- /**
1084
- * Text content of this document and all its descendants.
1085
- */
1086
- get text() {
1087
- return this.children.map((child) => "text" in child ? child.text : "").join("");
1088
- }
1089
- get type() {
1090
- return XmlNode.TYPE_DOCUMENT;
1091
- }
1092
- toJSON() {
1093
- return Object.assign(XmlNode.prototype.toJSON.call(this), { children: this.children.map((child) => child.toJSON()) });
1094
- }
1095
- };
1096
- var XmlDocumentType = class extends XmlNode {
1097
- constructor(name, publicId, systemId, internalSubset) {
1098
- super();
1099
- this.name = name;
1100
- this.publicId = publicId != null ? publicId : null;
1101
- this.systemId = systemId != null ? systemId : null;
1102
- this.internalSubset = internalSubset != null ? internalSubset : null;
1103
- }
1104
- get type() {
1105
- return XmlNode.TYPE_DOCUMENT_TYPE;
1106
- }
1107
- toJSON() {
1108
- let json = XmlNode.prototype.toJSON.call(this);
1109
- json.name = this.name;
1110
- for (let key of [
1111
- "publicId",
1112
- "systemId",
1113
- "internalSubset"
1114
- ]) if (this[key] !== null) json[key] = this[key];
1115
- return json;
1116
- }
1117
- };
1118
- var XmlError = class extends Error {
1119
- constructor(message, charIndex, xml) {
1120
- let column = 1;
1121
- let excerpt = "";
1122
- let line = 1;
1123
- for (let i = 0; i < charIndex; ++i) {
1124
- let char = xml[i];
1125
- if (char === "\n") {
1126
- column = 1;
1127
- excerpt = "";
1128
- line += 1;
1129
- } else {
1130
- column += 1;
1131
- excerpt += char;
1132
- }
1133
- }
1134
- let eol = xml.indexOf("\n", charIndex);
1135
- excerpt += eol === -1 ? xml.slice(charIndex) : xml.slice(charIndex, eol);
1136
- let excerptStart = 0;
1137
- if (excerpt.length > 50) if (column < 40) excerpt = excerpt.slice(0, 50);
1138
- else {
1139
- excerptStart = column - 20;
1140
- excerpt = excerpt.slice(excerptStart, column + 30);
1141
- }
1142
- super(`${message} (line ${line}, column ${column})
1143
- ${excerpt}
1144
- ` + " ".repeat(column - excerptStart + 1) + "^\n");
1145
- this.column = column;
1146
- this.excerpt = excerpt;
1147
- this.line = line;
1148
- this.name = "XmlError";
1149
- this.pos = charIndex;
1150
- }
1151
- };
1152
- var XmlProcessingInstruction = class extends XmlNode {
1153
- constructor(name, content = "") {
1154
- super();
1155
- this.name = name;
1156
- this.content = content;
1157
- }
1158
- get type() {
1159
- return XmlNode.TYPE_PROCESSING_INSTRUCTION;
1160
- }
1161
- toJSON() {
1162
- return Object.assign(XmlNode.prototype.toJSON.call(this), {
1163
- name: this.name,
1164
- content: this.content
1165
- });
1166
- }
1167
- };
1168
- var emptyString2 = "";
1169
- var Parser = class {
1170
- /**
1171
- * @param xml XML string to parse.
1172
- * @param options Parser options.
1173
- */
1174
- constructor(xml, options = {}) {
1175
- let doc = this.document = new XmlDocument();
1176
- this.j = doc;
1177
- this.g = options;
1178
- this.c = new StringScanner(xml);
1179
- if (this.g.includeOffsets) {
1180
- doc.start = 0;
1181
- doc.end = xml.length;
1182
- }
1183
- this.parse();
1184
- }
1185
- /**
1186
- * Adds the given `XmlNode` as a child of `this.currentNode`.
1187
- */
1188
- i(node, charIndex) {
1189
- node.parent = this.j;
1190
- if (this.g.includeOffsets) {
1191
- node.start = this.c.f(charIndex);
1192
- node.end = this.c.f();
1193
- }
1194
- this.j.children.push(node);
1195
- return true;
1196
- }
1197
- /**
1198
- * Adds the given _text_ to the document, either by appending it to a
1199
- * preceding `XmlText` node (if possible) or by creating a new `XmlText` node.
1200
- *
1201
- * When _normalize_ is `true` (the default), line breaks in _text_ are
1202
- * normalized per section 2.11 of the XML spec. This must be `false` for text
1203
- * that comes from a character or entity reference, since references aren't
1204
- * subject to line break normalization.
1205
- */
1206
- y(text, charIndex, normalize = true) {
1207
- let { children } = this.j;
1208
- let { length } = children;
1209
- if (normalize) text = normalizeLineBreaks(text);
1210
- if (length > 0) {
1211
- let prevNode = children[length - 1];
1212
- if ((prevNode == null ? void 0 : prevNode.type) === XmlNode.TYPE_TEXT) {
1213
- let textNode = prevNode;
1214
- textNode.text += text;
1215
- if (this.g.includeOffsets) textNode.end = this.c.f();
1216
- return true;
1217
- }
1218
- }
1219
- return this.i(new XmlText(text), charIndex);
1220
- }
1221
- /**
1222
- * Consumes element attributes.
1223
- *
1224
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-starttags
1225
- */
1226
- H() {
1227
- let attributes = /* @__PURE__ */ Object.create(null);
1228
- while (this.e()) {
1229
- let attrName = this.q();
1230
- if (!attrName) break;
1231
- let attrValue = this.t() && this.I();
1232
- if (attrValue === false) throw this.a("Attribute value expected");
1233
- if (attrName in attributes) throw this.a(`Duplicate attribute: ${attrName}`);
1234
- if (attrName === "xml:space" && attrValue !== "default" && attrValue !== "preserve") throw this.a("Value of the `xml:space` attribute must be \"default\" or \"preserve\"");
1235
- attributes[attrName] = attrValue;
1236
- }
1237
- if (this.g.sortAttributes) {
1238
- let attrNames = Object.keys(attributes).sort();
1239
- let sortedAttributes = /* @__PURE__ */ Object.create(null);
1240
- for (let i = 0; i < attrNames.length; ++i) {
1241
- let attrName = attrNames[i];
1242
- sortedAttributes[attrName] = attributes[attrName];
1243
- }
1244
- attributes = sortedAttributes;
1245
- }
1246
- return attributes;
1247
- }
1248
- /**
1249
- * Consumes an `AttValue` (attribute value) if possible.
1250
- *
1251
- * @returns
1252
- * Contents of the `AttValue` minus quotes, or `false` if nothing was
1253
- * consumed. An empty string indicates that an `AttValue` was consumed but
1254
- * was empty.
1255
- *
1256
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-AttValue
1257
- */
1258
- I() {
1259
- let { c: scanner } = this;
1260
- let quote = scanner.m();
1261
- if (quote !== "\"" && quote !== "'") return false;
1262
- scanner.p();
1263
- let chars;
1264
- let isClosed = false;
1265
- let value = emptyString2;
1266
- let regex = quote === "\"" ? attValueCharDoubleQuote : attValueCharSingleQuote;
1267
- matchLoop: while (!scanner.B) {
1268
- chars = scanner.x(regex);
1269
- if (chars) {
1270
- this.o(chars);
1271
- value += chars.replace(attValueNormalizedWhitespace, " ");
1272
- }
1273
- switch (scanner.m()) {
1274
- case quote:
1275
- isClosed = true;
1276
- break matchLoop;
1277
- case "&":
1278
- value += this.C();
1279
- continue;
1280
- case "<": throw this.a("Unescaped `<` is not allowed in an attribute value");
1281
- default: break matchLoop;
1282
- }
1283
- }
1284
- if (!isClosed) throw this.a("Unclosed attribute");
1285
- scanner.p();
1286
- return value;
1287
- }
1288
- /**
1289
- * Consumes a CDATA section if possible.
1290
- *
1291
- * @returns Whether a CDATA section was consumed.
1292
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-cdata-sect
1293
- */
1294
- J() {
1295
- let { c: scanner } = this;
1296
- let startIndex = scanner.d;
1297
- if (!scanner.b("<![CDATA[")) return false;
1298
- let text = scanner.s("]]>");
1299
- this.o(text);
1300
- if (!scanner.b("]]>")) throw this.a("Unclosed CDATA section");
1301
- return this.g.preserveCdata ? this.i(new XmlCdata(normalizeLineBreaks(text)), startIndex) : this.y(text, startIndex);
1302
- }
1303
- /**
1304
- * Consumes character data if possible.
1305
- *
1306
- * @returns Whether character data was consumed.
1307
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#dt-chardata
1308
- */
1309
- K() {
1310
- let { c: scanner } = this;
1311
- let startIndex = scanner.d;
1312
- let charData = scanner.x(endCharData);
1313
- if (!charData) return false;
1314
- this.o(charData);
1315
- if (scanner.m(3) === "]]>") throw this.a("Element content may not contain the CDATA section close delimiter `]]>`");
1316
- return this.y(charData, startIndex);
1317
- }
1318
- /**
1319
- * Consumes a comment if possible.
1320
- *
1321
- * @returns Whether a comment was consumed.
1322
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Comment
1323
- */
1324
- D() {
1325
- let { c: scanner } = this;
1326
- let startIndex = scanner.d;
1327
- if (!scanner.b("<!--")) return false;
1328
- let content = scanner.s("--");
1329
- this.o(content);
1330
- if (!scanner.b("-->")) {
1331
- if (scanner.m(2) === "--") throw this.a("The string `--` isn't allowed inside a comment");
1332
- throw this.a("Unclosed comment");
1333
- }
1334
- return this.g.preserveComments ? this.i(new XmlComment(normalizeLineBreaks(content)), startIndex) : true;
1335
- }
1336
- /**
1337
- * Consumes a reference in a content context if possible.
1338
- *
1339
- * This differs from `consumeReference()` in that a consumed reference will be
1340
- * added to the document as a text node instead of returned.
1341
- *
1342
- * @returns Whether a reference was consumed.
1343
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#entproc
1344
- */
1345
- L() {
1346
- let startIndex = this.c.d;
1347
- let ref = this.C();
1348
- return ref ? this.y(ref, startIndex, false) : false;
1349
- }
1350
- /**
1351
- * Consumes a doctype declaration if possible.
1352
- *
1353
- * This is a loose implementation since doctype declarations are currently
1354
- * discarded without further parsing.
1355
- *
1356
- * @returns Whether a doctype declaration was consumed.
1357
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#dtd
1358
- */
1359
- M() {
1360
- let { c: scanner } = this;
1361
- let startIndex = scanner.d;
1362
- if (!scanner.b("<!DOCTYPE")) return false;
1363
- let name = this.e() && this.q();
1364
- if (!name) throw this.a("Expected a name");
1365
- let publicId;
1366
- let systemId;
1367
- if (this.e()) {
1368
- if (scanner.b("PUBLIC")) {
1369
- publicId = this.e() && this.N();
1370
- if (publicId === false) throw this.a("Expected a public identifier");
1371
- this.e();
1372
- }
1373
- if (publicId !== void 0 || scanner.b("SYSTEM")) {
1374
- this.e();
1375
- systemId = this.r();
1376
- if (systemId === false) throw this.a("Expected a system identifier");
1377
- this.e();
1378
- }
1379
- }
1380
- let internalSubset;
1381
- if (scanner.b("[")) {
1382
- internalSubset = scanner.x(/\][\x20\t\r\n]*>/);
1383
- if (!scanner.b("]")) throw this.a("Unclosed internal subset");
1384
- this.e();
1385
- }
1386
- if (!scanner.b(">")) throw this.a("Unclosed doctype declaration");
1387
- return this.g.preserveDocumentType ? this.i(new XmlDocumentType(name, publicId, systemId, internalSubset), startIndex) : true;
1388
- }
1389
- /**
1390
- * Consumes an element if possible.
1391
- *
1392
- * @returns Whether an element was consumed.
1393
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-element
1394
- */
1395
- E() {
1396
- let { c: scanner } = this;
1397
- let startIndex = scanner.d;
1398
- if (!scanner.b("<")) return false;
1399
- let name = this.q();
1400
- if (!name) {
1401
- scanner.n(startIndex);
1402
- return false;
1403
- }
1404
- let attributes = this.H();
1405
- let isEmpty = !!scanner.b("/>");
1406
- let element = new XmlElement(name, attributes);
1407
- element.parent = this.j;
1408
- if (!isEmpty) {
1409
- if (!scanner.b(">")) throw this.a(`Unclosed start tag for element \`${name}\``);
1410
- this.j = element;
1411
- do
1412
- this.K();
1413
- while (this.E() || this.L() || this.J() || this.F() || this.D());
1414
- let endTagMark = scanner.d;
1415
- let endTagName;
1416
- if (!scanner.b("</") || !(endTagName = this.q()) || endTagName !== name) {
1417
- scanner.n(endTagMark);
1418
- throw this.a(`Missing end tag for element ${name}`);
1419
- }
1420
- this.e();
1421
- if (!scanner.b(">")) throw this.a(`Unclosed end tag for element ${name}`);
1422
- this.j = element.parent;
1423
- }
1424
- return this.i(element, startIndex);
1425
- }
1426
- /**
1427
- * Consumes an `Eq` production if possible.
1428
- *
1429
- * @returns Whether an `Eq` production was consumed.
1430
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Eq
1431
- */
1432
- t() {
1433
- this.e();
1434
- if (this.c.b("=")) {
1435
- this.e();
1436
- return true;
1437
- }
1438
- return false;
1439
- }
1440
- /**
1441
- * Consumes `Misc` content if possible.
1442
- *
1443
- * @returns Whether anything was consumed.
1444
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Misc
1445
- */
1446
- z() {
1447
- return this.D() || this.F() || this.e();
1448
- }
1449
- /**
1450
- * Consumes one or more `Name` characters if possible.
1451
- *
1452
- * @returns `Name` characters, or an empty string if none were consumed.
1453
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name
1454
- */
1455
- q() {
1456
- return isNameStartChar(this.c.m()) ? this.c.w(isNameChar) : emptyString2;
1457
- }
1458
- /**
1459
- * Consumes a processing instruction if possible.
1460
- *
1461
- * @returns Whether a processing instruction was consumed.
1462
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-pi
1463
- */
1464
- F() {
1465
- let { c: scanner } = this;
1466
- let startIndex = scanner.d;
1467
- if (!scanner.b("<?")) return false;
1468
- let name = this.q();
1469
- if (name) {
1470
- if (name.toLowerCase() === "xml") {
1471
- scanner.n(startIndex);
1472
- throw this.a("XML declaration isn't allowed here");
1473
- }
1474
- } else throw this.a("Invalid processing instruction");
1475
- if (!this.e()) {
1476
- if (scanner.b("?>")) return this.i(new XmlProcessingInstruction(name), startIndex);
1477
- throw this.a("Whitespace is required after a processing instruction name");
1478
- }
1479
- let content = scanner.s("?>");
1480
- this.o(content);
1481
- if (!scanner.b("?>")) throw this.a("Unterminated processing instruction");
1482
- return this.i(new XmlProcessingInstruction(name, normalizeLineBreaks(content)), startIndex);
1483
- }
1484
- /**
1485
- * Consumes a prolog if possible.
1486
- *
1487
- * @returns Whether a prolog was consumed.
1488
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-prolog-dtd
1489
- */
1490
- O() {
1491
- let { c: scanner } = this;
1492
- let startIndex = scanner.d;
1493
- this.P();
1494
- while (this.z());
1495
- if (this.M()) while (this.z());
1496
- return startIndex < scanner.d;
1497
- }
1498
- /**
1499
- * Consumes a public identifier literal if possible.
1500
- *
1501
- * @returns
1502
- * Value of the public identifier literal minus quotes, or `false` if
1503
- * nothing was consumed. An empty string indicates that a public id literal
1504
- * was consumed but was empty.
1505
- *
1506
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-PubidLiteral
1507
- */
1508
- N() {
1509
- let startIndex = this.c.d;
1510
- let value = this.r();
1511
- if (value !== false && !/^[-\x20\r\na-zA-Z0-9'()+,./:=?;!*#@$_%]*$/.test(value)) {
1512
- this.c.n(startIndex);
1513
- throw this.a("Invalid character in public identifier");
1514
- }
1515
- return value;
1516
- }
1517
- /**
1518
- * Consumes a reference if possible.
1519
- *
1520
- * This differs from `consumeContentReference()` in that a consumed reference
1521
- * will be returned rather than added to the document.
1522
- *
1523
- * @returns
1524
- * Parsed reference value, or `false` if nothing was consumed (to
1525
- * distinguish from a reference that resolves to an empty string).
1526
- *
1527
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Reference
1528
- */
1529
- C() {
1530
- let { c: scanner } = this;
1531
- if (!scanner.b("&")) return false;
1532
- let ref = scanner.w(isReferenceChar);
1533
- if (scanner.G() !== ";") throw this.a("Unterminated reference (a reference must end with `;`)");
1534
- let parsedValue;
1535
- if (ref[0] === "#") {
1536
- let codePoint = ref[1] === "x" ? parseInt(ref.slice(2), 16) : parseInt(ref.slice(1), 10);
1537
- if (isNaN(codePoint)) throw this.a("Invalid character reference");
1538
- if (!isXmlCodePoint(codePoint)) throw this.a("Character reference resolves to an invalid character");
1539
- parsedValue = String.fromCodePoint(codePoint);
1540
- } else {
1541
- parsedValue = predefinedEntities[ref];
1542
- if (parsedValue === void 0) {
1543
- let { ignoreUndefinedEntities, resolveUndefinedEntity } = this.g;
1544
- let wrappedRef = `&${ref};`;
1545
- if (resolveUndefinedEntity) {
1546
- let resolvedValue = resolveUndefinedEntity(wrappedRef);
1547
- if (resolvedValue !== null && resolvedValue !== void 0) {
1548
- let type = typeof resolvedValue;
1549
- if (type !== "string") throw new TypeError(`\`resolveUndefinedEntity()\` must return a string, \`null\`, or \`undefined\`, but returned a value of type ${type}`);
1550
- return resolvedValue;
1551
- }
1552
- }
1553
- if (ignoreUndefinedEntities) return wrappedRef;
1554
- scanner.n(-wrappedRef.length);
1555
- throw this.a(`Named entity isn't defined: ${wrappedRef}`);
1556
- }
1557
- }
1558
- return parsedValue;
1559
- }
1560
- /**
1561
- * Consumes a `SystemLiteral` if possible.
1562
- *
1563
- * A `SystemLiteral` is similar to an attribute value, but allows the
1564
- * characters `<` and `&` and doesn't replace references.
1565
- *
1566
- * @returns
1567
- * Value of the `SystemLiteral` minus quotes, or `false` if nothing was
1568
- * consumed. An empty string indicates that a `SystemLiteral` was consumed
1569
- * but was empty.
1570
- *
1571
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-SystemLiteral
1572
- */
1573
- r() {
1574
- let { c: scanner } = this;
1575
- let quote = scanner.b("\"") || scanner.b("'");
1576
- if (!quote) return false;
1577
- let value = scanner.s(quote);
1578
- this.o(value);
1579
- if (!scanner.b(quote)) throw this.a("Missing end quote");
1580
- return value;
1581
- }
1582
- /**
1583
- * Consumes one or more whitespace characters if possible.
1584
- *
1585
- * @returns Whether any whitespace characters were consumed.
1586
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#white
1587
- */
1588
- e() {
1589
- return !!this.c.w(isWhitespace);
1590
- }
1591
- /**
1592
- * Consumes an XML declaration if possible.
1593
- *
1594
- * @returns Whether an XML declaration was consumed.
1595
- * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-XMLDecl
1596
- */
1597
- P() {
1598
- let { c: scanner } = this;
1599
- let startIndex = scanner.d;
1600
- if (!scanner.b("<?xml")) return false;
1601
- if (!this.e()) throw this.a("Invalid XML declaration");
1602
- let version = !!scanner.b("version") && this.t() && this.r();
1603
- if (version === false) throw this.a("XML version is missing or invalid");
1604
- else if (!/^1\.[0-9]+$/.test(version)) throw this.a("Invalid character in version number");
1605
- let encoding;
1606
- let standalone;
1607
- if (this.e()) {
1608
- encoding = !!scanner.b("encoding") && this.t() && this.r();
1609
- if (encoding) {
1610
- if (!/^[A-Za-z][\w.-]*$/.test(encoding)) throw this.a("Invalid character in encoding name");
1611
- this.e();
1612
- }
1613
- standalone = !!scanner.b("standalone") && this.t() && this.r();
1614
- if (standalone) {
1615
- if (standalone !== "yes" && standalone !== "no") throw this.a("Only \"yes\" and \"no\" are permitted as values of `standalone`");
1616
- this.e();
1617
- }
1618
- }
1619
- if (!scanner.b("?>")) throw this.a("Invalid or unclosed XML declaration");
1620
- return this.g.preserveXmlDeclaration ? this.i(new XmlDeclaration(version, encoding || void 0, standalone || void 0), startIndex) : true;
1621
- }
1622
- /**
1623
- * Returns an `XmlError` for the current scanner position.
1624
- */
1625
- a(message) {
1626
- let { c: scanner } = this;
1627
- return new XmlError(message, scanner.d, scanner.h);
1628
- }
1629
- /**
1630
- * Parses the XML input.
1631
- */
1632
- parse() {
1633
- this.c.b("");
1634
- this.O();
1635
- if (!this.E()) throw this.a("Root element is missing or invalid");
1636
- while (this.z());
1637
- if (!this.c.B) throw this.a("Extra content at the end of the document");
1638
- }
1639
- /**
1640
- * Throws an invalid character error if any character in the given _string_
1641
- * isn't a valid XML character.
1642
- */
1643
- o(string) {
1644
- let { length } = string;
1645
- for (let i = 0; i < length; ++i) {
1646
- let cp = string.codePointAt(i);
1647
- if (!isXmlCodePoint(cp)) {
1648
- this.c.n(-([...string].length - i));
1649
- throw this.a("Invalid character");
1650
- }
1651
- if (cp > 65535) i += 1;
1652
- }
1653
- }
1654
- };
1655
- function normalizeLineBreaks(text) {
1656
- let i = 0;
1657
- while ((i = text.indexOf("\r", i)) !== -1) text = text[i + 1] === "\n" ? text.slice(0, i) + text.slice(i + 1) : text.slice(0, i) + "\n" + text.slice(i + 1);
1658
- return text;
1659
- }
1660
- function parseXml(xml, options) {
1661
- return new Parser(xml, options).document;
1662
- }
1663
- })))();
1664
- const localName = (name) => name.includes(":") ? name.split(":")[1] : name;
1665
- const isElement = (node) => node.type === "element";
1666
- const isText = (node) => node.type === "text";
1667
- const child = (el, name) => {
1668
- for (const node of el.children) if (isElement(node) && localName(node.name) === name) return node;
1669
- };
1670
- const textOf = (el) => {
1671
- if (el === void 0) return;
1672
- let result = "";
1673
- for (const node of el.children) if (isText(node)) result += node.text;
1674
- return result === "" ? void 0 : result;
1675
- };
1676
- const docRoot = (doc) => doc.root ?? void 0;
1677
- const xmlVal = (el, name) => {
1678
- const v = el !== void 0 ? textOf(child(el, name)) : void 0;
1679
- if (v === void 0 || v === "") return;
1680
- try {
1681
- return decodeURIComponent(v.replace(/\+/g, " "));
1682
- } catch {
1683
- throw new BaerlyError("InvalidResponse", `Malformed percent-escape in S3 <${name}>: ${v}`);
1684
- }
1685
- };
1686
- const rawXmlVal = (el, name) => {
1687
- return el !== void 0 ? textOf(child(el, name)) : void 0;
1688
- };
1689
- /**
1690
- * Build a {@link ParsedS3Error} from the element that directly carries
1691
- * the `<Code>` / `<Message>` children — `<Error>` for S3, the inner
1692
- * `<Error>` for STS. Returns `undefined` when neither child is present
1693
- * so callers can fall back to the raw status. Shared by
1694
- * {@link parseS3Error} and {@link parseStsError}, which differ only in
1695
- * how they locate that element.
1696
- */
1697
- const codeMessageFrom = (el) => {
1698
- const code = rawXmlVal(el, "Code");
1699
- const message = rawXmlVal(el, "Message");
1700
- if (code === void 0 && message === void 0) return;
1701
- return {
1702
- ...code !== void 0 && { Code: code },
1703
- ...message !== void 0 && { Message: message }
1704
- };
1705
- };
1706
- /**
1707
- * Parse an S3 `<Error><Code>…</Code><Message>…</Message></Error>`
1708
- * body. Returns `undefined` when the body is not a recognizable S3
1709
- * error document (empty, HTML, a success payload, or a DTD), so
1710
- * callers can fall back to the raw response text. This never throws:
1711
- * it runs on a path that is already reporting an error.
1712
- */
1713
- const parseS3Error = (xml) => {
1714
- if (!/<Error\b/i.test(xml) || /<!DOCTYPE\b/i.test(xml)) return;
1715
- let root;
1716
- try {
1717
- root = docRoot((0, import_browser.parseXml)(xml));
1718
- } catch {
1719
- return;
1720
- }
1721
- if (root === void 0) return;
1722
- if (localName(root.name) !== "Error") return;
1723
- return codeMessageFrom(root);
1724
- };
1725
- const parseLastModified = (lm) => {
1726
- if (lm === void 0) return;
1727
- const d = new Date(lm);
1728
- return Number.isNaN(d.getTime()) ? void 0 : d;
1729
- };
1730
- const parseListObjectsV2CommandOutput = (xml) => {
1731
- if (/<!DOCTYPE\b/i.test(xml)) throw new BaerlyError("InvalidResponse", "DTD not allowed in S3 XML responses");
1732
- let root;
1733
- try {
1734
- root = docRoot((0, import_browser.parseXml)(xml));
1735
- } catch {
1736
- throw new BaerlyError("InvalidResponse", `Invalid XML: ${xml}`);
1737
- }
1738
- if (root === void 0) return {};
1739
- if (localName(root.name) !== "ListBucketResult") return {};
1740
- return {
1741
- Contents: root.children.filter(isElement).filter((c) => localName(c.name) === "Contents").map((contentEl) => {
1742
- return {
1743
- ETag: rawXmlVal(contentEl, "ETag"),
1744
- Key: xmlVal(contentEl, "Key"),
1745
- LastModified: parseLastModified(rawXmlVal(contentEl, "LastModified")),
1746
- Generation: rawXmlVal(contentEl, "Generation")
1747
- };
1748
- }),
1749
- NextContinuationToken: rawXmlVal(root, "NextContinuationToken")
1750
- };
1751
- };
1752
- //#endregion
1753
- //#region ../adapter-node/src/http-transport.ts
1754
- /**
1755
- * Permanent {@link BaerlyError} codes that must short-circuit `retry`.
1756
- * These represent caller- or environment-level faults where retrying
1757
- * cannot succeed: `AccessDenied` (403 — credentials/policy),
1758
- * `InvalidConfig` (bad bucket / unsupported credential type),
1759
- * `InvalidResponse` (server returned unparseable data), and
1760
- * `Conflict` (CAS guard lost — retrying would just re-lose against
1761
- * the same state). `NetworkError` is intentionally absent — it covers
1762
- * transient transport faults and stays retryable.
1763
- *
1764
- * Transport-layer set, distinct from `RETRIABLE_CODES` in `@baerly/protocol`'s
1765
- * `errors.ts` (the default wire `retriable` hint). `Conflict` is "permanent"
1766
- * here (a blind re-PUT re-loses the CAS race) yet retryable for logical CAS
1767
- * callers that can fresh-read + re-apply. Same code, two different questions.
1768
- */
1769
- const PERMANENT_ERROR_CODES = /* @__PURE__ */ new Set([
1770
- "AccessDenied",
1771
- "InvalidConfig",
1772
- "InvalidResponse",
1773
- "Conflict"
1774
- ]);
1775
- /**
1776
- * Parse an HTTP `Retry-After` header into a non-negative seconds value,
1777
- * clamped to {@link RETRY_AFTER_MAX_SECONDS}. Returns `undefined` if
1778
- * the header is absent, malformed, or whitespace.
1779
- *
1780
- * RFC 7231 §7.1.3 admits two forms: delta-seconds (non-negative
1781
- * integer) and HTTP-date. Both are accepted; dates in the past return
1782
- * `0`. Fractional seconds, negatives, and arbitrary strings reject.
1783
- *
1784
- * The `now` injection point exists for the unit test; production
1785
- * callers should pass nothing.
1786
- */
1787
- function parseRetryAfter(header, now = Date.now) {
1788
- if (header === null) return;
1789
- const trimmed = header.trim();
1790
- if (trimmed === "") return;
1791
- if (/^\d+$/.test(trimmed)) {
1792
- const n = Number.parseInt(trimmed, 10);
1793
- return Number.isFinite(n) ? Math.min(n, 60) : void 0;
1794
- }
1795
- if (!/[a-z]/i.test(trimmed)) return;
1796
- const t = Date.parse(trimmed);
1797
- if (Number.isNaN(t)) return;
1798
- const seconds = Math.max(0, Math.ceil((t - now()) / 1e3));
1799
- return Math.min(seconds, 60);
1800
- }
1801
- const retryAfterCause = (res) => {
1802
- const hint = parseRetryAfter(res.headers.get("Retry-After"));
1803
- return hint !== void 0 ? { retryAfterSeconds: hint } : {};
1804
- };
1805
- const xmlErrorDetail = (status, body) => {
1806
- const parsed = parseS3Error(body);
1807
- if (parsed !== void 0) {
1808
- const code = parsed.Code ?? "UnknownError";
1809
- return parsed.Message !== void 0 ? `${status} ${code}: ${parsed.Message}` : `${status} ${code}`;
1810
- }
1811
- return `${status} ${body}`;
1812
- };
1813
- /**
1814
- * Shared status→{@link BaerlyError} mapping for the S3/GCS XML adapters, so
1815
- * the retryable set (`429`/`≥500`) can't drift between them. Store-specific
1816
- * statuses (GCS `412`; S3 `409`/`404`-with-`If-Match`) are handled inline
1817
- * first; the rest route here: `403`→AccessDenied (permanent), `429`/`≥500`→
1818
- * retryable NetworkError carrying `{status, retryAfterSeconds?}`, else
1819
- * InvalidResponse. Callers `throw` the result; consumes `res.text()` on the
1820
- * non-`403` paths.
1821
- */
1822
- const mapStorageError = async (res, verb, key) => {
1823
- if (res.status === 403) return new BaerlyError("AccessDenied", `${verb} ${key}: 403`);
1824
- if (res.status === 429 || res.status >= 500) return new BaerlyError("NetworkError", `${verb} ${key}: ${res.status} ${await res.text()}`, {
1825
- status: res.status,
1826
- ...retryAfterCause(res)
1827
- });
1828
- return new BaerlyError("InvalidResponse", `${verb} ${key}: ${xmlErrorDetail(res.status, await res.text())}`);
1829
- };
1830
- const retry = async (fn, { retries = 8, backoffMs = 100, maxDelayMs = 1e4 } = {}) => {
1831
- let wait = backoffMs;
1832
- for (let attempt = 0; attempt <= retries; attempt++) try {
1833
- return await fn();
1834
- } catch (error) {
1835
- if (error instanceof BaerlyError && PERMANENT_ERROR_CODES.has(error.code)) throw error;
1836
- if (attempt === retries) throw error;
1837
- const hintMs = retryAfterHintMs(error, maxDelayMs);
1838
- await delay(hintMs ?? wait);
1839
- if (hintMs === void 0) wait = Math.min(wait * 1.5, maxDelayMs);
1840
- else wait = backoffMs;
1841
- }
1842
- throw new BaerlyError("Internal", "http-transport retry loop exited without returning or throwing");
1843
- };
1844
- const retryAfterHintMs = (e, maxDelayMs) => {
1845
- if (!(e instanceof BaerlyError)) return;
1846
- const cause = e.cause;
1847
- if (cause?.retryAfterSeconds === void 0) return;
1848
- return Math.min(cause.retryAfterSeconds * 1e3, maxDelayMs);
1849
- };
1850
- //#endregion
1851
- //#region ../adapter-node/src/gcs-http.ts
1852
- /** Default GCS native XML API endpoint. */
1853
- const DEFAULT_GCS_ENDPOINT = "https://storage.googleapis.com";
1854
- /**
1855
- * `Storage` impl that speaks the GCS native XML API and drives GCS's own
1856
- * `x-goog-if-generation-match` conditional writes. Unlike GCS's S3-interop
1857
- * endpoint (which treats `If-Match`/`If-None-Match` as read-scoped and can
1858
- * never linearize the log), the native API returns 412 on any precondition
1859
- * miss and the object `generation` in `x-goog-generation`. That generation
1860
- * is carried verbatim in the opaque `Storage` etag.
1861
- *
1862
- * Authentication is plugged in via `sign` (GOOG4-HMAC-SHA256); this class
1863
- * does no signing itself.
1864
- */
1865
- var GcsHttpStorage = class {
1866
- #endpoint;
1867
- #bucket;
1868
- #fetch;
1869
- #sign;
1870
- #retries;
1871
- #backoffMs;
1872
- constructor(options) {
1873
- this.#endpoint = (options.endpoint ?? "https://storage.googleapis.com").replace(/\/+$/, "");
1874
- this.#bucket = options.bucket;
1875
- this.#fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
1876
- this.#sign = options.sign;
1877
- this.#retries = options.retries ?? 8;
1878
- this.#backoffMs = options.backoffMs ?? 100;
1879
- }
1880
- #objectUrl(key, generation) {
1881
- assertValidStorageKey(key);
1882
- const base = `${this.#endpoint}/${this.#bucket}/${encodeURIComponent(key)}`;
1883
- return generation !== void 0 ? `${base}?generation=${encodeURIComponent(generation)}` : base;
1884
- }
1885
- async #dispatch(req) {
1886
- const signed = this.#sign ? await this.#sign(req) : req;
1887
- try {
1888
- return await this.#fetch(signed);
1889
- } catch (error) {
1890
- if (error instanceof BaerlyError) throw error;
1891
- throw new BaerlyError("NetworkError", error instanceof Error ? error.message : String(error), error);
1892
- }
1893
- }
1894
- #retry(fn) {
1895
- return retry(fn, {
1896
- retries: this.#retries,
1897
- backoffMs: this.#backoffMs
1898
- });
1899
- }
1900
- async get(key, opts) {
1901
- opts?.signal?.throwIfAborted();
1902
- const url = this.#objectUrl(key, opts?.versionId);
1903
- return this.#retry(async () => {
1904
- const res = await this.#dispatch(new Request(url, {
1905
- method: "GET",
1906
- signal: opts?.signal ?? null
1907
- }));
1908
- switch (res.status) {
1909
- case 200: {
1910
- const etag = res.headers.get("x-goog-generation");
1911
- if (etag === null) throw new BaerlyError("InvalidResponse", `GET ${key}: missing x-goog-generation`);
1912
- return {
1913
- body: new Uint8Array(await res.arrayBuffer()),
1914
- etag
1915
- };
1916
- }
1917
- case 304:
1918
- case 404: return null;
1919
- default: throw await mapStorageError(res, "GET", key);
1920
- }
1921
- });
1922
- }
1923
- async put(key, body, opts) {
1924
- opts?.signal?.throwIfAborted();
1925
- const url = this.#objectUrl(key);
1926
- const headers = new Headers();
1927
- headers.set("Content-Type", opts?.contentType ?? "application/octet-stream");
1928
- if (opts?.ifNoneMatch === "*") headers.set("x-goog-if-generation-match", "0");
1929
- else if (opts?.ifMatch !== void 0) headers.set("x-goog-if-generation-match", opts.ifMatch);
1930
- return this.#retry(async () => {
1931
- const res = await this.#dispatch(new Request(url, {
1932
- method: "PUT",
1933
- headers,
1934
- body,
1935
- signal: opts?.signal ?? null
1936
- }));
1937
- if (res.status === 412) throw new BaerlyError("Conflict", `PUT ${key}: precondition failed`);
1938
- if (res.status !== 200 && res.status !== 204) throw await mapStorageError(res, "PUT", key);
1939
- const etag = res.headers.get("x-goog-generation");
1940
- if (etag === null) throw new BaerlyError("InvalidResponse", `PUT ${key}: missing x-goog-generation`);
1941
- const dateStr = res.headers.get("Date");
1942
- const result = { etag };
1943
- if (dateStr !== null) result.serverDate = new Date(dateStr);
1944
- return result;
1945
- });
1946
- }
1947
- async delete(key, opts) {
1948
- opts?.signal?.throwIfAborted();
1949
- const url = this.#objectUrl(key);
1950
- await this.#retry(async () => {
1951
- const res = await this.#dispatch(new Request(url, {
1952
- method: "DELETE",
1953
- signal: opts?.signal ?? null
1954
- }));
1955
- if (res.status !== 200 && res.status !== 204 && res.status !== 404) throw await mapStorageError(res, "DELETE", key);
1956
- });
1957
- }
1958
- async *list(prefix, opts) {
1959
- opts?.signal?.throwIfAborted();
1960
- let yielded = 0;
1961
- let continuationToken;
1962
- const startAfter = opts?.startAfter ?? "";
1963
- while (true) {
1964
- opts?.signal?.throwIfAborted();
1965
- const params = new URLSearchParams();
1966
- params.set("list-type", "2");
1967
- params.set("prefix", prefix);
1968
- params.set("encoding-type", "url");
1969
- if (continuationToken !== void 0) params.set("continuation-token", continuationToken);
1970
- else if (startAfter !== "") params.set("start-after", startAfter);
1971
- if (opts?.maxKeys !== void 0) {
1972
- const remaining = opts.maxKeys - yielded;
1973
- params.set("max-keys", String(Math.min(remaining, 1e3)));
1974
- }
1975
- const url = `${this.#endpoint}/${this.#bucket}/?${params.toString()}`;
1976
- let parsed;
1977
- let lastHint;
1978
- for (let attempt = 0; attempt < 10; attempt++) {
1979
- const outcome = await this.#retry(async () => {
1980
- const res = await this.#dispatch(new Request(url, {
1981
- method: "GET",
1982
- signal: opts?.signal ?? null
1983
- }));
1984
- if (res.status === 200) return {
1985
- kind: "ok",
1986
- body: await res.text()
1987
- };
1988
- if (res.status === 429) return {
1989
- kind: "ratelimited",
1990
- retryAfterSeconds: parseRetryAfter(res.headers.get("Retry-After"))
1991
- };
1992
- throw await mapStorageError(res, "LIST", prefix);
1993
- });
1994
- if (outcome.kind === "ok") {
1995
- parsed = parseListObjectsV2CommandOutput(outcome.body);
1996
- break;
1997
- }
1998
- lastHint = outcome.retryAfterSeconds;
1999
- const hintMs = outcome.retryAfterSeconds !== void 0 ? outcome.retryAfterSeconds * 1e3 : 0;
2000
- await delay(Math.max(hintMs, RATE_LIMIT_BACKOFF_MILLIS));
2001
- }
2002
- if (parsed === void 0) throw new BaerlyError("NetworkError", `LIST ${prefix}: rate-limited`, {
2003
- status: 429,
2004
- ...lastHint !== void 0 ? { retryAfterSeconds: lastHint } : {}
2005
- });
2006
- for (const entry of parsed.Contents ?? []) {
2007
- if (entry.Key === void 0) continue;
2008
- yield {
2009
- key: entry.Key,
2010
- etag: entry.Generation ?? "",
2011
- ...entry.LastModified !== void 0 && { lastModified: entry.LastModified }
2012
- };
2013
- yielded += 1;
2014
- if (opts?.maxKeys !== void 0 && yielded >= opts.maxKeys) return;
2015
- }
2016
- continuationToken = parsed.NextContinuationToken;
2017
- if (continuationToken === void 0 || continuationToken === "") return;
2018
- }
2019
- }
2020
- };
2021
- //#endregion
2022
- //#region ../adapter-node/src/credentials/goog4-signer.ts
2023
- /** GOOG4 signing-key prefix (vs AWS SigV4's "AWS4"). @see https://docs.cloud.google.com/storage/docs/authentication/signatures */
2024
- const GOOG4_KEY_PREFIX = "GOOG4";
2025
- /** Credential-scope terminator (vs SigV4's "aws4_request"). @see https://docs.cloud.google.com/storage/docs/authentication/signatures */
2026
- const GOOG4_REQUEST = "goog4_request";
2027
- /** Signing algorithm string. @see https://docs.cloud.google.com/storage/docs/authentication/signatures */
2028
- const GOOG4_ALGORITHM = "GOOG4-HMAC-SHA256";
2029
- /** GCS storage service name in the credential scope (vs SigV4's "s3"). @see https://docs.cloud.google.com/storage/docs/authentication/signatures */
2030
- const GOOG4_SERVICE = "storage";
2031
- /** GCS accepts "auto" as the signing region. @see https://docs.cloud.google.com/storage/docs/authentication/signatures */
2032
- const GOOG4_REGION = "auto";
2033
- const encoder$1 = new TextEncoder();
2034
- function toHex(bytes) {
2035
- let hex = "";
2036
- for (let i = 0; i < bytes.length; i++) hex += bytes[i].toString(16).padStart(2, "0");
2037
- return hex;
2038
- }
2039
- /** RFC-3986-strict percent-encoding for canonical query components: encodes
2040
- * everything except the unreserved set A-Za-z0-9-._~. encodeURIComponent
2041
- * already handles that set but leaves !'()* unescaped, so escape those too.
2042
- * @see https://docs.cloud.google.com/storage/docs/authentication/canonical-requests */
2043
- function uriEncode(s) {
2044
- return encodeURIComponent(s).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
2045
- }
2046
- /** Canonical-URI encoding for the request path. `URL.pathname` already
2047
- * percent-encodes spaces / non-ASCII / control chars (and `^` `` ` `` `{` `}`),
2048
- * but leaves GCS's enumerated reserved set raw — the sub-delims, `:@`, and
2049
- * `[]`. GCS canonicalizes the path it receives by percent-encoding exactly
2050
- * that set (`?=!#$&'()*+,:;@[]"`; `?#"` can't survive in `pathname`), so we
2051
- * must reproduce the same delta or a `[`/`]`-bearing key signs differently
2052
- * than GCS computes it (403 SignatureDoesNotMatch). Target only the delta so
2053
- * already-encoded `%XX` and the unreserved set stay intact (no double-encode).
2054
- * This is a denylist, not an allow-list, on purpose: over-encoding chars GCS
2055
- * leaves raw (`|` `` ` `` `{}` …) would itself cause a mismatch.
2056
- * @see https://docs.cloud.google.com/storage/docs/authentication/canonical-requests */
2057
- function canonicalUri(pathname) {
2058
- return pathname.replace(/[!$&'()*+,;=:@[\]]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
2059
- }
2060
- async function hmac$1(key, data) {
2061
- const view = new Uint8Array(key.byteLength);
2062
- view.set(key);
2063
- const cryptoKey = await crypto.subtle.importKey("raw", view, {
2064
- name: "HMAC",
2065
- hash: "SHA-256"
2066
- }, false, ["sign"]);
2067
- const sig = await crypto.subtle.sign("HMAC", cryptoKey, encoder$1.encode(data));
2068
- return new Uint8Array(sig);
2069
- }
2070
- /** Pure: build the GOOG4 canonical request string. Exported for the intermediates test. */
2071
- function goog4CanonicalRequest(input) {
2072
- const u = new URL(input.url);
2073
- const normalized = {};
2074
- for (const [name, value] of Object.entries(input.signedHeaders)) normalized[name.toLowerCase()] = value;
2075
- const names = Object.keys(normalized).toSorted();
2076
- const canonicalHeaders = names.map((n) => `${n}:${normalized[n].trim()}\n`).join("");
2077
- const signedHeaderList = names.join(";");
2078
- const query = [...u.searchParams].map(([k, v]) => [uriEncode(k), uriEncode(v)]).toSorted((a, b) => {
2079
- const ka = `${a[0]}\0${a[1]}`;
2080
- const kb = `${b[0]}\0${b[1]}`;
2081
- if (ka < kb) return -1;
2082
- return ka > kb ? 1 : 0;
2083
- }).map(([k, v]) => `${k}=${v}`).join("&");
2084
- return [
2085
- input.method,
2086
- canonicalUri(u.pathname),
2087
- query,
2088
- canonicalHeaders,
2089
- signedHeaderList,
2090
- input.hashedPayload
2091
- ].join("\n");
2092
- }
2093
- /** Pure: build the SigV4-family string-to-sign. Async because it hashes the canonical request. */
2094
- async function goog4StringToSign(input) {
2095
- const hash = await sha256Hex(encoder$1.encode(input.canonicalRequest));
2096
- return [
2097
- input.algorithm,
2098
- input.amzDate,
2099
- input.scope,
2100
- hash
2101
- ].join("\n");
2102
- }
2103
- /**
2104
- * The SigV4-family signing machinery: four-stage HMAC key chain + final HMAC.
2105
- * Identical between GOOG4 and AWS SigV4 modulo the {keyPrefix, terminator,
2106
- * service, region} constants — which is exactly what the AWS-vector oracle
2107
- * exploits to prove this in CI. Exported for oracle 1.
2108
- */
2109
- async function sigV4Signature(input) {
2110
- return toHex(await hmac$1(await hmac$1(await hmac$1(await hmac$1(await hmac$1(encoder$1.encode(input.keyPrefix + input.secretAccessKey), input.yyyymmdd), input.region), input.service), input.terminator), input.stringToSign));
2111
- }
2112
- function amzDateFrom(ms) {
2113
- const amzDate = new Date(ms).toISOString().replace(/[:-]/g, "").replace(/\.\d{3}Z$/, "Z");
2114
- return {
2115
- amzDate,
2116
- yyyymmdd: amzDate.slice(0, 8)
2117
- };
2118
- }
2119
- function goog4Signer(opts) {
2120
- const now = opts.now ?? (() => Date.now());
2121
- const resolve = async () => typeof opts.credentials === "function" ? opts.credentials() : opts.credentials;
2122
- return async (req) => {
2123
- const creds = await resolve();
2124
- const { amzDate, yyyymmdd } = amzDateFrom(now());
2125
- const scope = `${yyyymmdd}/${GOOG4_REGION}/${GOOG4_SERVICE}/${GOOG4_REQUEST}`;
2126
- const payloadHash = await sha256Hex(new Uint8Array(await req.clone().arrayBuffer()));
2127
- const url = new URL(req.url);
2128
- const headers = new Headers(req.headers);
2129
- headers.set("host", url.host);
2130
- headers.set("x-goog-date", amzDate);
2131
- headers.set("x-goog-content-sha256", payloadHash);
2132
- const signedHeaders = {};
2133
- for (const [name, value] of headers) {
2134
- const lower = name.toLowerCase();
2135
- if (lower === "host" || lower === "content-type" || lower.startsWith("x-goog-")) signedHeaders[lower] = value;
2136
- }
2137
- const stringToSign = await goog4StringToSign({
2138
- algorithm: GOOG4_ALGORITHM,
2139
- amzDate,
2140
- scope,
2141
- canonicalRequest: goog4CanonicalRequest({
2142
- method: req.method,
2143
- url: req.url,
2144
- signedHeaders,
2145
- hashedPayload: payloadHash
2146
- })
2147
- });
2148
- const signature = await sigV4Signature({
2149
- keyPrefix: GOOG4_KEY_PREFIX,
2150
- terminator: GOOG4_REQUEST,
2151
- service: GOOG4_SERVICE,
2152
- region: GOOG4_REGION,
2153
- secretAccessKey: creds.secretAccessKey,
2154
- yyyymmdd,
2155
- stringToSign
2156
- });
2157
- const signedHeaderList = Object.keys(signedHeaders).map((n) => n.toLowerCase()).toSorted().join(";");
2158
- headers.set("Authorization", `${GOOG4_ALGORITHM} Credential=${creds.accessKeyId}/${scope}, SignedHeaders=${signedHeaderList}, Signature=${signature}`);
2159
- return new Request(req, { headers });
2160
- };
2161
- }
2162
- //#endregion
2163
- //#region ../adapter-node/src/s3-http.ts
2164
- /**
2165
- * `Storage` impl that speaks the S3 REST API directly. Works against
2166
- * any S3-compatible endpoint — AWS S3, R2 (S3-compat), Minio. Does not
2167
- * depend on any AWS SDK. GCS is NOT supported over this S3-interop path
2168
- * (its conditional-write semantics can't linearize the commit log); use
2169
- * the native `GcsHttpStorage` adapter instead.
2170
- *
2171
- * Authentication is plugged in via the `sign` callback; this class
2172
- * itself does no SigV4. For Cloudflare Workers, the R2 binding fast
2173
- * path lives in `@baerly/adapter-cloudflare`.
2174
- */
2175
- var S3HttpStorage = class {
2176
- #endpoint;
2177
- #bucket;
2178
- #fetch;
2179
- #sign;
2180
- #retries;
2181
- #backoffMs;
2182
- constructor(options) {
2183
- this.#endpoint = options.endpoint.replace(/\/+$/, "");
2184
- this.#bucket = options.bucket;
2185
- this.#fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
2186
- this.#sign = options.sign;
2187
- this.#retries = options.retries ?? 8;
2188
- this.#backoffMs = options.backoffMs ?? 100;
2189
- }
2190
- #objectUrl(key, versionId) {
2191
- assertValidStorageKey(key);
2192
- const base = `${this.#endpoint}/${this.#bucket}/${encodeURIComponent(key)}`;
2193
- return versionId !== void 0 ? `${base}?versionId=${encodeURIComponent(versionId)}` : base;
2194
- }
2195
- async #dispatch(req) {
2196
- const signed = this.#sign ? await this.#sign(req) : req;
2197
- try {
2198
- return await this.#fetch(signed);
2199
- } catch (error) {
2200
- if (error instanceof BaerlyError) throw error;
2201
- throw new BaerlyError("NetworkError", error instanceof Error ? error.message : String(error), error);
2202
- }
2203
- }
2204
- #retry(fn) {
2205
- return retry(fn, {
2206
- retries: this.#retries,
2207
- backoffMs: this.#backoffMs
2208
- });
2209
- }
2210
- async get(key, opts) {
2211
- opts?.signal?.throwIfAborted();
2212
- const url = this.#objectUrl(key, opts?.versionId);
2213
- const headers = new Headers();
2214
- if (opts?.ifNoneMatch !== void 0) headers.set("If-None-Match", opts.ifNoneMatch);
2215
- return this.#retry(async () => {
2216
- const res = await this.#dispatch(new Request(url, {
2217
- method: "GET",
2218
- headers,
2219
- signal: opts?.signal ?? null
2220
- }));
2221
- switch (res.status) {
2222
- case 200: {
2223
- const etag = res.headers.get("ETag");
2224
- if (etag === null) throw new BaerlyError("InvalidResponse", `GET ${key}: missing ETag`);
2225
- const body = new Uint8Array(await res.arrayBuffer());
2226
- const versionId = res.headers.get("x-amz-version-id") ?? void 0;
2227
- return versionId !== void 0 ? {
2228
- body,
2229
- etag,
2230
- versionId
2231
- } : {
2232
- body,
2233
- etag
2234
- };
2235
- }
2236
- case 304:
2237
- case 404: return null;
2238
- default: throw await mapStorageError(res, "GET", key);
2239
- }
2240
- });
2241
- }
2242
- async put(key, body, opts) {
2243
- opts?.signal?.throwIfAborted();
2244
- const url = this.#objectUrl(key);
2245
- const headers = new Headers();
2246
- headers.set("Content-Type", opts?.contentType ?? "application/octet-stream");
2247
- if (opts?.ifMatch !== void 0) headers.set("If-Match", opts.ifMatch);
2248
- if (opts?.ifNoneMatch === "*") headers.set("If-None-Match", "*");
2249
- return this.#retry(async () => {
2250
- const res = await this.#dispatch(new Request(url, {
2251
- method: "PUT",
2252
- headers,
2253
- body,
2254
- signal: opts?.signal ?? null
2255
- }));
2256
- if (res.status === 412) throw new BaerlyError("Conflict", `PUT ${key}: precondition failed`);
2257
- if (res.status === 409 && opts?.ifNoneMatch === "*") throw new BaerlyError("NetworkError", `PUT ${key}: 409 ConditionalRequestConflict (contended conditional create; retryable)`, {
2258
- status: res.status,
2259
- ...retryAfterCause(res)
2260
- });
2261
- if (res.status === 404 && opts?.ifMatch !== void 0) throw new BaerlyError("Conflict", `PUT ${key}: precondition failed (ifMatch=${opts.ifMatch} but key does not exist)`);
2262
- if (res.status !== 200 && res.status !== 204) throw await mapStorageError(res, "PUT", key);
2263
- const etag = res.headers.get("ETag");
2264
- if (etag === null) throw new BaerlyError("InvalidResponse", `PUT ${key}: missing ETag`);
2265
- const dateStr = res.headers.get("Date");
2266
- const versionId = res.headers.get("x-amz-version-id") ?? void 0;
2267
- const result = { etag };
2268
- if (dateStr !== null) result.serverDate = new Date(dateStr);
2269
- if (versionId !== void 0) result.versionId = versionId;
2270
- return result;
2271
- });
2272
- }
2273
- async delete(key, opts) {
2274
- opts?.signal?.throwIfAborted();
2275
- const url = this.#objectUrl(key);
2276
- await this.#retry(async () => {
2277
- const res = await this.#dispatch(new Request(url, {
2278
- method: "DELETE",
2279
- signal: opts?.signal ?? null
2280
- }));
2281
- if (res.status !== 200 && res.status !== 204 && res.status !== 404) throw await mapStorageError(res, "DELETE", key);
2282
- });
2283
- }
2284
- async *list(prefix, opts) {
2285
- opts?.signal?.throwIfAborted();
2286
- let yielded = 0;
2287
- let continuationToken;
2288
- const startAfter = opts?.startAfter ?? "";
2289
- while (true) {
2290
- opts?.signal?.throwIfAborted();
2291
- const params = new URLSearchParams();
2292
- params.set("list-type", "2");
2293
- params.set("prefix", prefix);
2294
- params.set("encoding-type", "url");
2295
- if (continuationToken !== void 0) params.set("continuation-token", continuationToken);
2296
- else if (startAfter !== "") params.set("start-after", startAfter);
2297
- if (opts?.maxKeys !== void 0) {
2298
- const remaining = opts.maxKeys - yielded;
2299
- params.set("max-keys", String(Math.min(remaining, 1e3)));
2300
- }
2301
- const url = `${this.#endpoint}/${this.#bucket}/?${params.toString()}`;
2302
- let parsed;
2303
- let lastHint;
2304
- for (let attempt = 0; attempt < 10; attempt++) {
2305
- const outcome = await this.#retry(async () => {
2306
- const res = await this.#dispatch(new Request(url, {
2307
- method: "GET",
2308
- signal: opts?.signal ?? null
2309
- }));
2310
- if (res.status === 200) return {
2311
- kind: "ok",
2312
- body: await res.text()
2313
- };
2314
- if (res.status === 429) return {
2315
- kind: "ratelimited",
2316
- retryAfterSeconds: parseRetryAfter(res.headers.get("Retry-After"))
2317
- };
2318
- throw await mapStorageError(res, "LIST", prefix);
2319
- });
2320
- if (outcome.kind === "ok") {
2321
- parsed = parseListObjectsV2CommandOutput(outcome.body);
2322
- break;
2323
- }
2324
- lastHint = outcome.retryAfterSeconds;
2325
- const hintMs = outcome.retryAfterSeconds !== void 0 ? outcome.retryAfterSeconds * 1e3 : 0;
2326
- await delay(Math.max(hintMs, RATE_LIMIT_BACKOFF_MILLIS));
2327
- }
2328
- if (parsed === void 0) throw new BaerlyError("NetworkError", `LIST ${prefix}: rate-limited`, {
2329
- status: 429,
2330
- ...lastHint !== void 0 ? { retryAfterSeconds: lastHint } : {}
2331
- });
2332
- for (const entry of parsed.Contents ?? []) {
2333
- if (entry.Key === void 0) continue;
2334
- yield {
2335
- key: entry.Key,
2336
- etag: entry.ETag ?? "",
2337
- ...entry.LastModified !== void 0 && { lastModified: entry.LastModified }
2338
- };
2339
- yielded += 1;
2340
- if (opts?.maxKeys !== void 0 && yielded >= opts.maxKeys) return;
2341
- }
2342
- continuationToken = parsed.NextContinuationToken;
2343
- if (continuationToken === void 0 || continuationToken === "") return;
2344
- }
2345
- }
2346
- };
2347
- //#endregion
2348
- //#region ../../node_modules/.pnpm/aws4fetch@1.0.20/node_modules/aws4fetch/dist/aws4fetch.esm.mjs
2349
- /**
2350
- * @license MIT <https://opensource.org/licenses/MIT>
2351
- * @copyright Michael Hart 2024
2352
- */
2353
- const encoder = new TextEncoder();
2354
- const HOST_SERVICES = {
2355
- appstream2: "appstream",
2356
- cloudhsmv2: "cloudhsm",
2357
- email: "ses",
2358
- marketplace: "aws-marketplace",
2359
- mobile: "AWSMobileHubService",
2360
- pinpoint: "mobiletargeting",
2361
- queue: "sqs",
2362
- "git-codecommit": "codecommit",
2363
- "mturk-requester-sandbox": "mturk-requester",
2364
- "personalize-runtime": "personalize"
2365
- };
2366
- const UNSIGNABLE_HEADERS = /* @__PURE__ */ new Set([
2367
- "authorization",
2368
- "content-type",
2369
- "content-length",
2370
- "user-agent",
2371
- "presigned-expires",
2372
- "expect",
2373
- "x-amzn-trace-id",
2374
- "range",
2375
- "connection"
2376
- ]);
2377
- var AwsClient = class {
2378
- constructor({ accessKeyId, secretAccessKey, sessionToken, service, region, cache, retries, initRetryMs }) {
2379
- if (accessKeyId == null) throw new TypeError("accessKeyId is a required option");
2380
- if (secretAccessKey == null) throw new TypeError("secretAccessKey is a required option");
2381
- this.accessKeyId = accessKeyId;
2382
- this.secretAccessKey = secretAccessKey;
2383
- this.sessionToken = sessionToken;
2384
- this.service = service;
2385
- this.region = region;
2386
- this.cache = cache || /* @__PURE__ */ new Map();
2387
- this.retries = retries != null ? retries : 10;
2388
- this.initRetryMs = initRetryMs || 50;
2389
- }
2390
- async sign(input, init) {
2391
- if (input instanceof Request) {
2392
- const { method, url, headers, body } = input;
2393
- init = Object.assign({
2394
- method,
2395
- url,
2396
- headers
2397
- }, init);
2398
- if (init.body == null && headers.has("Content-Type")) init.body = body != null && headers.has("X-Amz-Content-Sha256") ? body : await input.clone().arrayBuffer();
2399
- input = url;
2400
- }
2401
- const signer = new AwsV4Signer(Object.assign({ url: input.toString() }, init, this, init && init.aws));
2402
- const signed = Object.assign({}, init, await signer.sign());
2403
- delete signed.aws;
2404
- try {
2405
- return new Request(signed.url.toString(), signed);
2406
- } catch (e) {
2407
- if (e instanceof TypeError) return new Request(signed.url.toString(), Object.assign({ duplex: "half" }, signed));
2408
- throw e;
2409
- }
2410
- }
2411
- async fetch(input, init) {
2412
- for (let i = 0; i <= this.retries; i++) {
2413
- const fetched = fetch(await this.sign(input, init));
2414
- if (i === this.retries) return fetched;
2415
- const res = await fetched;
2416
- if (res.status < 500 && res.status !== 429) return res;
2417
- await new Promise((resolve) => setTimeout(resolve, Math.random() * this.initRetryMs * Math.pow(2, i)));
2418
- }
2419
- throw new Error("An unknown error occurred, ensure retries is not negative");
2420
- }
2421
- };
2422
- var AwsV4Signer = class {
2423
- constructor({ method, url, headers, body, accessKeyId, secretAccessKey, sessionToken, service, region, cache, datetime, signQuery, appendSessionToken, allHeaders, singleEncode }) {
2424
- if (url == null) throw new TypeError("url is a required option");
2425
- if (accessKeyId == null) throw new TypeError("accessKeyId is a required option");
2426
- if (secretAccessKey == null) throw new TypeError("secretAccessKey is a required option");
2427
- this.method = method || (body ? "POST" : "GET");
2428
- this.url = new URL(url);
2429
- this.headers = new Headers(headers || {});
2430
- this.body = body;
2431
- this.accessKeyId = accessKeyId;
2432
- this.secretAccessKey = secretAccessKey;
2433
- this.sessionToken = sessionToken;
2434
- let guessedService, guessedRegion;
2435
- if (!service || !region) [guessedService, guessedRegion] = guessServiceRegion(this.url, this.headers);
2436
- this.service = service || guessedService || "";
2437
- this.region = region || guessedRegion || "us-east-1";
2438
- this.cache = cache || /* @__PURE__ */ new Map();
2439
- this.datetime = datetime || (/* @__PURE__ */ new Date()).toISOString().replace(/[:-]|\.\d{3}/g, "");
2440
- this.signQuery = signQuery;
2441
- this.appendSessionToken = appendSessionToken || this.service === "iotdevicegateway";
2442
- this.headers.delete("Host");
2443
- if (this.service === "s3" && !this.signQuery && !this.headers.has("X-Amz-Content-Sha256")) this.headers.set("X-Amz-Content-Sha256", "UNSIGNED-PAYLOAD");
2444
- const params = this.signQuery ? this.url.searchParams : this.headers;
2445
- params.set("X-Amz-Date", this.datetime);
2446
- if (this.sessionToken && !this.appendSessionToken) params.set("X-Amz-Security-Token", this.sessionToken);
2447
- this.signableHeaders = ["host", ...this.headers.keys()].filter((header) => allHeaders || !UNSIGNABLE_HEADERS.has(header)).sort();
2448
- this.signedHeaders = this.signableHeaders.join(";");
2449
- this.canonicalHeaders = this.signableHeaders.map((header) => header + ":" + (header === "host" ? this.url.host : (this.headers.get(header) || "").replace(/\s+/g, " "))).join("\n");
2450
- this.credentialString = [
2451
- this.datetime.slice(0, 8),
2452
- this.region,
2453
- this.service,
2454
- "aws4_request"
2455
- ].join("/");
2456
- if (this.signQuery) {
2457
- if (this.service === "s3" && !params.has("X-Amz-Expires")) params.set("X-Amz-Expires", "86400");
2458
- params.set("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
2459
- params.set("X-Amz-Credential", this.accessKeyId + "/" + this.credentialString);
2460
- params.set("X-Amz-SignedHeaders", this.signedHeaders);
2461
- }
2462
- if (this.service === "s3") try {
2463
- this.encodedPath = decodeURIComponent(this.url.pathname.replace(/\+/g, " "));
2464
- } catch (e) {
2465
- this.encodedPath = this.url.pathname;
2466
- }
2467
- else this.encodedPath = this.url.pathname.replace(/\/+/g, "/");
2468
- if (!singleEncode) this.encodedPath = encodeURIComponent(this.encodedPath).replace(/%2F/g, "/");
2469
- this.encodedPath = encodeRfc3986(this.encodedPath);
2470
- const seenKeys = /* @__PURE__ */ new Set();
2471
- this.encodedSearch = [...this.url.searchParams].filter(([k]) => {
2472
- if (!k) return false;
2473
- if (this.service === "s3") {
2474
- if (seenKeys.has(k)) return false;
2475
- seenKeys.add(k);
2476
- }
2477
- return true;
2478
- }).map((pair) => pair.map((p) => encodeRfc3986(encodeURIComponent(p)))).sort(([k1, v1], [k2, v2]) => k1 < k2 ? -1 : k1 > k2 ? 1 : v1 < v2 ? -1 : v1 > v2 ? 1 : 0).map((pair) => pair.join("=")).join("&");
2479
- }
2480
- async sign() {
2481
- if (this.signQuery) {
2482
- this.url.searchParams.set("X-Amz-Signature", await this.signature());
2483
- if (this.sessionToken && this.appendSessionToken) this.url.searchParams.set("X-Amz-Security-Token", this.sessionToken);
2484
- } else this.headers.set("Authorization", await this.authHeader());
2485
- return {
2486
- method: this.method,
2487
- url: this.url,
2488
- headers: this.headers,
2489
- body: this.body
2490
- };
2491
- }
2492
- async authHeader() {
2493
- return [
2494
- "AWS4-HMAC-SHA256 Credential=" + this.accessKeyId + "/" + this.credentialString,
2495
- "SignedHeaders=" + this.signedHeaders,
2496
- "Signature=" + await this.signature()
2497
- ].join(", ");
2498
- }
2499
- async signature() {
2500
- const date = this.datetime.slice(0, 8);
2501
- const cacheKey = [
2502
- this.secretAccessKey,
2503
- date,
2504
- this.region,
2505
- this.service
2506
- ].join();
2507
- let kCredentials = this.cache.get(cacheKey);
2508
- if (!kCredentials) {
2509
- kCredentials = await hmac(await hmac(await hmac(await hmac("AWS4" + this.secretAccessKey, date), this.region), this.service), "aws4_request");
2510
- this.cache.set(cacheKey, kCredentials);
2511
- }
2512
- return buf2hex(await hmac(kCredentials, await this.stringToSign()));
2513
- }
2514
- async stringToSign() {
2515
- return [
2516
- "AWS4-HMAC-SHA256",
2517
- this.datetime,
2518
- this.credentialString,
2519
- buf2hex(await hash(await this.canonicalString()))
2520
- ].join("\n");
2521
- }
2522
- async canonicalString() {
2523
- return [
2524
- this.method.toUpperCase(),
2525
- this.encodedPath,
2526
- this.encodedSearch,
2527
- this.canonicalHeaders + "\n",
2528
- this.signedHeaders,
2529
- await this.hexBodyHash()
2530
- ].join("\n");
2531
- }
2532
- async hexBodyHash() {
2533
- let hashHeader = this.headers.get("X-Amz-Content-Sha256") || (this.service === "s3" && this.signQuery ? "UNSIGNED-PAYLOAD" : null);
2534
- if (hashHeader == null) {
2535
- if (this.body && typeof this.body !== "string" && !("byteLength" in this.body)) throw new Error("body must be a string, ArrayBuffer or ArrayBufferView, unless you include the X-Amz-Content-Sha256 header");
2536
- hashHeader = buf2hex(await hash(this.body || ""));
2537
- }
2538
- return hashHeader;
2539
- }
2540
- };
2541
- async function hmac(key, string) {
2542
- const cryptoKey = await crypto.subtle.importKey("raw", typeof key === "string" ? encoder.encode(key) : key, {
2543
- name: "HMAC",
2544
- hash: { name: "SHA-256" }
2545
- }, false, ["sign"]);
2546
- return crypto.subtle.sign("HMAC", cryptoKey, encoder.encode(string));
2547
- }
2548
- async function hash(content) {
2549
- return crypto.subtle.digest("SHA-256", typeof content === "string" ? encoder.encode(content) : content);
2550
- }
2551
- const HEX_CHARS = [
2552
- "0",
2553
- "1",
2554
- "2",
2555
- "3",
2556
- "4",
2557
- "5",
2558
- "6",
2559
- "7",
2560
- "8",
2561
- "9",
2562
- "a",
2563
- "b",
2564
- "c",
2565
- "d",
2566
- "e",
2567
- "f"
2568
- ];
2569
- function buf2hex(arrayBuffer) {
2570
- const buffer = new Uint8Array(arrayBuffer);
2571
- let out = "";
2572
- for (let idx = 0; idx < buffer.length; idx++) {
2573
- const n = buffer[idx];
2574
- out += HEX_CHARS[n >>> 4 & 15];
2575
- out += HEX_CHARS[n & 15];
2576
- }
2577
- return out;
2578
- }
2579
- function encodeRfc3986(urlEncodedStr) {
2580
- return urlEncodedStr.replace(/[!'()*]/g, (c) => "%" + c.charCodeAt(0).toString(16).toUpperCase());
2581
- }
2582
- function guessServiceRegion(url, headers) {
2583
- const { hostname, pathname } = url;
2584
- if (hostname.endsWith(".on.aws")) {
2585
- const match = hostname.match(/^[^.]{1,63}\.lambda-url\.([^.]{1,63})\.on\.aws$/);
2586
- return match != null ? ["lambda", match[1] || ""] : ["", ""];
2587
- }
2588
- if (hostname.endsWith(".r2.cloudflarestorage.com")) return ["s3", "auto"];
2589
- if (hostname.endsWith(".backblazeb2.com")) {
2590
- const match = hostname.match(/^(?:[^.]{1,63}\.)?s3\.([^.]{1,63})\.backblazeb2\.com$/);
2591
- return match != null ? ["s3", match[1] || ""] : ["", ""];
2592
- }
2593
- const match = hostname.replace("dualstack.", "").match(/([^.]{1,63})\.(?:([^.]{0,63})\.)?amazonaws\.com(?:\.cn)?$/);
2594
- let service = match && match[1] || "";
2595
- let region = match && match[2];
2596
- if (region === "us-gov") region = "us-gov-west-1";
2597
- else if (region === "s3" || region === "s3-accelerate") {
2598
- region = "us-east-1";
2599
- service = "s3";
2600
- } else if (service === "iot") if (hostname.startsWith("iot.")) service = "execute-api";
2601
- else if (hostname.startsWith("data.jobs.iot.")) service = "iot-jobs-data";
2602
- else service = pathname === "/mqtt" ? "iotdevicegateway" : "iotdata";
2603
- else if (service === "autoscaling") {
2604
- const targetPrefix = (headers.get("X-Amz-Target") || "").split(".")[0];
2605
- if (targetPrefix === "AnyScaleFrontendService") service = "application-autoscaling";
2606
- else if (targetPrefix === "AnyScaleScalingPlannerFrontendService") service = "autoscaling-plans";
2607
- } else if (region == null && service.startsWith("s3-")) {
2608
- region = service.slice(3).replace(/^fips-|^external-1/, "");
2609
- service = "s3";
2610
- } else if (service.endsWith("-fips")) service = service.slice(0, -5);
2611
- else if (region && /-\d$/.test(service) && !/-\d$/.test(region)) [service, region] = [region, service];
2612
- return [HOST_SERVICES[service] || service, region || ""];
2613
- }
2614
- //#endregion
2615
- //#region ../adapter-node/src/credentials/signer.ts
2616
- const REFRESH_BUFFER_MS = 300 * 1e3;
2617
- function buildAwsClient(creds, region) {
2618
- return new AwsClient({
2619
- accessKeyId: creds.accessKeyId,
2620
- secretAccessKey: creds.secretAccessKey,
2621
- sessionToken: creds.sessionToken,
2622
- region,
2623
- service: "s3"
2624
- });
2625
- }
2626
- function refreshingSigner(opts) {
2627
- const region = opts.region;
2628
- const now = opts.now ?? (() => Date.now());
2629
- if (typeof opts.credentials !== "function") {
2630
- const aws = buildAwsClient(opts.credentials, region);
2631
- return (req) => aws.sign(req);
2632
- }
2633
- const provider = opts.credentials;
2634
- let cached = null;
2635
- let inFlight = null;
2636
- const resolve = async () => {
2637
- if (cached !== null && cached.expiresAt - REFRESH_BUFFER_MS > now()) return cached.aws;
2638
- if (inFlight !== null) return inFlight;
2639
- inFlight = (async () => {
2640
- try {
2641
- const creds = await provider();
2642
- const aws = buildAwsClient(creds, region);
2643
- cached = {
2644
- aws,
2645
- expiresAt: creds.expiration?.getTime() ?? Number.POSITIVE_INFINITY
2646
- };
2647
- return aws;
2648
- } finally {
2649
- inFlight = null;
2650
- }
2651
- })();
2652
- return inFlight;
2653
- };
2654
- return async (req) => {
2655
- return (await resolve()).sign(req);
2656
- };
2657
- }
2658
- //#endregion
2659
- //#region ../adapter-node/src/storage-factories.ts
2660
- function buildS3Storage(opts) {
2661
- const sign = refreshingSigner({
2662
- region: opts.region,
2663
- credentials: opts.credentials
2664
- });
2665
- return new S3HttpStorage({
2666
- endpoint: opts.endpoint,
2667
- bucket: opts.bucket,
2668
- sign
2669
- });
2670
- }
2671
- /**
2672
- * AWS S3 `Storage` factory. Wraps `S3HttpStorage` with the standard
2673
- * `aws4fetch` SigV4 signer. The endpoint is derived from the region as
2674
- * `https://s3.<region>.amazonaws.com`.
2675
- *
2676
- * @example Static credentials
2677
- * ```ts
2678
- * import { s3Storage } from "@gusto/baerly-storage/node";
2679
- *
2680
- * const storage = s3Storage({
2681
- * region: process.env["AWS_REGION"] ?? "us-east-1",
2682
- * bucket: process.env["BUCKET"]!,
2683
- * credentials: {
2684
- * accessKeyId: process.env["AWS_ACCESS_KEY_ID"]!,
2685
- * secretAccessKey: process.env["AWS_SECRET_ACCESS_KEY"]!,
2686
- * },
2687
- * });
2688
- * ```
2689
- *
2690
- * @example EKS (refreshing) — auto-detects Pod Identity or IRSA
2691
- * ```ts
2692
- * import { s3Storage, fromEks } from "@gusto/baerly-storage/node";
2693
- *
2694
- * const storage = s3Storage({
2695
- * region: process.env["AWS_REGION"] ?? "us-east-1",
2696
- * bucket: process.env["BUCKET"]!,
2697
- * credentials: fromEks(),
2698
- * });
2699
- * ```
2700
- */
2701
- function s3Storage(opts) {
2702
- return buildS3Storage({
2703
- endpoint: `https://s3.${opts.region}.amazonaws.com`,
2704
- region: opts.region,
2705
- bucket: opts.bucket,
2706
- credentials: opts.credentials
2707
- });
2708
- }
2709
- /**
2710
- * Cloudflare R2 `Storage` factory (S3-compat endpoint). Derives the
2711
- * endpoint from `accountId` as
2712
- * `https://<accountId>.r2.cloudflarestorage.com` and pins region
2713
- * `"auto"` — R2 ignores region but `aws4fetch`'s signer requires
2714
- * the field to be set.
2715
- *
2716
- * For Workers that have a native R2 binding (no HTTP hop), see
2717
- * `r2BindingStorage` in `@baerly/adapter-cloudflare`. This factory
2718
- * is for Node hosts that talk to R2 over its public S3-compat URL.
2719
- *
2720
- * @example
2721
- * ```ts
2722
- * import { r2Storage } from "@gusto/baerly-storage/node";
2723
- *
2724
- * const storage = r2Storage({
2725
- * accountId: process.env["R2_ACCOUNT_ID"]!,
2726
- * bucket: process.env["BUCKET"]!,
2727
- * credentials: {
2728
- * accessKeyId: process.env["AWS_ACCESS_KEY_ID"]!,
2729
- * secretAccessKey: process.env["AWS_SECRET_ACCESS_KEY"]!,
2730
- * },
2731
- * });
2732
- * ```
2733
- */
2734
- function r2Storage(opts) {
2735
- return buildS3Storage({
2736
- endpoint: `https://${opts.accountId}.r2.cloudflarestorage.com`,
2737
- region: "auto",
2738
- bucket: opts.bucket,
2739
- credentials: opts.credentials
2740
- });
2741
- }
2742
- /**
2743
- * Minio `Storage` factory — for local-dev / self-hosted S3-compat.
2744
- * Endpoint is fully caller-supplied (e.g. `http://localhost:9102`)
2745
- * because Minio deployments don't follow a URL template. Region is
2746
- * pinned to `"us-east-1"`; Minio ignores it but `aws4fetch` requires
2747
- * a value.
2748
- *
2749
- * @example
2750
- * ```ts
2751
- * import { minioStorage } from "@gusto/baerly-storage/node";
2752
- *
2753
- * const storage = minioStorage({
2754
- * endpoint: process.env["BAERLY_S3_ENDPOINT"] ?? "http://localhost:9102",
2755
- * bucket: "baerly",
2756
- * credentials: {
2757
- * accessKeyId: process.env["MINIO_ACCESS_KEY"]!,
2758
- * secretAccessKey: process.env["MINIO_SECRET_KEY"]!,
2759
- * },
2760
- * });
2761
- * ```
2762
- */
2763
- function minioStorage(opts) {
2764
- return buildS3Storage({
2765
- endpoint: opts.endpoint,
2766
- region: "us-east-1",
2767
- bucket: opts.bucket,
2768
- credentials: opts.credentials
2769
- });
2770
- }
2771
- /**
2772
- * Google Cloud Storage `Storage` factory over the native GCS XML API
2773
- * at `https://storage.googleapis.com`. Wraps {@link GcsHttpStorage} with
2774
- * a GOOG4-HMAC-SHA256 signer, driving GCS's own generation-precondition
2775
- * conditional writes (`x-goog-if-generation-match: 0` for create-if-absent,
2776
- * `x-goog-if-generation-match: <generation>` for compare-and-swap), which
2777
- * return `412` on a lost write. The object `generation` is carried verbatim
2778
- * as the opaque `Storage` etag. This is the sanctioned GCS path — the
2779
- * S3-interop endpoint treats `If-Match`/`If-None-Match` as read-scoped only
2780
- * and cannot linearize the log, so it is not supported.
2781
- *
2782
- * Authentication uses GCS HMAC keys (Console → Settings → Interoperability)
2783
- * — mint an interop key, put the HMAC access key in `credentials.accessKeyId`
2784
- * and the HMAC secret in `credentials.secretAccessKey`.
2785
- *
2786
- * @example
2787
- * ```ts
2788
- * import { gcsStorage } from "@gusto/baerly-storage/node";
2789
- *
2790
- * const storage = gcsStorage({
2791
- * bucket: process.env["BUCKET"]!,
2792
- * credentials: {
2793
- * accessKeyId: process.env["GCS_HMAC_ACCESS_KEY_ID"]!,
2794
- * secretAccessKey: process.env["GCS_HMAC_SECRET"]!,
2795
- * },
2796
- * });
2797
- * ```
2798
- */
2799
- function gcsStorage(opts) {
2800
- const sign = goog4Signer({ credentials: opts.credentials });
2801
- return new GcsHttpStorage({
2802
- bucket: opts.bucket,
2803
- sign,
2804
- ...opts.endpoint !== void 0 && { endpoint: opts.endpoint }
2805
- });
2806
- }
2807
- //#endregion
2808
- //#region src/bucket-uri.ts
2809
- /**
2810
- * Bucket URI parsing for the CLI surface. Consumed by every
2811
- * `baerly admin <cmd>`, `baerly inspect`, and `baerly export`.
2812
- *
2813
- * Grammar:
2814
- * - `s3://<bucket>[/<prefix>]` — S3-compatible HTTP. Creds via env
2815
- * vars (`BAERLY_S3_ENDPOINT`, `BAERLY_S3_ACCESS_KEY_ID`,
2816
- * `BAERLY_S3_SECRET_ACCESS_KEY`, `BAERLY_S3_REGION` — region
2817
- * defaults to `us-east-1`).
2818
- * - `gcs://<bucket>[/<prefix>]` — native GCS XML API. Creds via
2819
- * env vars `BAERLY_GCS_HMAC_ACCESS_KEY_ID` + `BAERLY_GCS_HMAC_SECRET`
2820
- * (GCS HMAC interop keys: Console → Cloud Storage → Settings →
2821
- * Interoperability). Drives GCS's native generation-precondition
2822
- * path (`x-goog-if-generation-match`); the S3-interop endpoint is
2823
- * NOT supported (it treats If-Match/If-None-Match as read-only).
2824
- * - `file:///<absolute-path>` — `LocalFsStorage` rooted at the path.
2825
- * Relative paths are rejected by the three-slash prefix.
2826
- * - `memory://<bucket>` — `MemoryStorage` keyed via
2827
- * `getOrCreateMemoryStorageForBucket`. Test-only.
2828
- */
2829
- /**
2830
- * Parse a bucket URI into a constructed `Storage` handle + optional
2831
- * key prefix.
2832
- *
2833
- * @throws BaerlyError code="InvalidConfig" — unsupported scheme, or an
2834
- * `s3://` / `gcs://` URI with a missing env var.
2835
- */
2836
- const parseBucketUri = async (uri) => {
2837
- if (uri.startsWith("s3://")) {
2838
- const rest = uri.slice(5);
2839
- const slash = rest.indexOf("/");
2840
- const bucket = slash === -1 ? rest : rest.slice(0, slash);
2841
- const prefix = slash === -1 ? "" : rest.slice(slash + 1);
2842
- if (bucket.length === 0) throw new BaerlyError("InvalidConfig", `bucket URI: s3:// URI requires a bucket name (got ${JSON.stringify(uri)})`);
2843
- const accessKeyId = requireEnv("BAERLY_S3_ACCESS_KEY_ID");
2844
- const secretAccessKey = requireEnv("BAERLY_S3_SECRET_ACCESS_KEY");
2845
- const region = process.env["BAERLY_S3_REGION"] ?? "us-east-1";
2846
- const endpoint = requireEnv("BAERLY_S3_ENDPOINT");
2847
- return {
2848
- storage: pickS3Storage({
2849
- r2Host: endpoint.match(/^https?:\/\/([^./]+)\.r2\.cloudflarestorage\.com\b/i),
2850
- isAws: /^https?:\/\/s3(\.[^.]+)?\.amazonaws\.com\b/i.test(endpoint),
2851
- endpoint,
2852
- bucket,
2853
- accessKeyId,
2854
- secretAccessKey,
2855
- region
2856
- }),
2857
- keyPrefix: normalizeKeyPrefix(prefix)
2858
- };
2859
- }
2860
- if (uri.startsWith("gcs://")) {
2861
- const rest = uri.slice(6);
2862
- const slash = rest.indexOf("/");
2863
- const bucket = slash === -1 ? rest : rest.slice(0, slash);
2864
- const prefix = slash === -1 ? "" : rest.slice(slash + 1);
2865
- if (bucket.length === 0) throw new BaerlyError("InvalidConfig", `bucket URI: gcs:// URI requires a bucket name (got ${JSON.stringify(uri)})`);
2866
- const accessKeyId = requireEnv("BAERLY_GCS_HMAC_ACCESS_KEY_ID");
2867
- const secretAccessKey = requireEnv("BAERLY_GCS_HMAC_SECRET");
2868
- const endpoint = DEFAULT_GCS_ENDPOINT;
2869
- const credentials = {
2870
- accessKeyId,
2871
- secretAccessKey
2872
- };
2873
- return {
2874
- storage: gcsStorage({
2875
- endpoint,
2876
- bucket,
2877
- credentials
2878
- }),
2879
- keyPrefix: normalizeKeyPrefix(prefix),
2880
- gcs: {
2881
- endpoint,
2882
- bucket,
2883
- credentials
2884
- }
2885
- };
2886
- }
2887
- if (uri.startsWith("file:///")) return {
2888
- storage: new LocalFsStorage({ root: uri.slice(7) }),
2889
- keyPrefix: ""
2890
- };
2891
- if (uri.startsWith("memory://")) {
2892
- const bucket = uri.slice(9);
2893
- if (bucket.length === 0) throw new BaerlyError("InvalidConfig", `bucket URI: memory:// URI requires a bucket name (got ${JSON.stringify(uri)})`);
2894
- return {
2895
- storage: getOrCreateMemoryStorageForBucket(bucket),
2896
- keyPrefix: ""
2897
- };
2898
- }
2899
- throw new BaerlyError("InvalidConfig", `bucket URI: unsupported URI ${JSON.stringify(uri)}`);
2900
- };
2901
- const requireEnv = (name) => {
2902
- const v = process.env[name];
2903
- if (v === void 0 || v === "") throw new BaerlyError("InvalidConfig", `bucket URI: env var ${name} unset`);
2904
- return v;
2905
- };
2906
- const pickS3Storage = (opts) => {
2907
- const { r2Host, isAws, endpoint, bucket, accessKeyId, secretAccessKey, region } = opts;
2908
- if (r2Host !== null) return r2Storage({
2909
- accountId: r2Host[1],
2910
- bucket,
2911
- credentials: {
2912
- accessKeyId,
2913
- secretAccessKey
2914
- }
2915
- });
2916
- if (isAws) return s3Storage({
2917
- region,
2918
- bucket,
2919
- credentials: {
2920
- accessKeyId,
2921
- secretAccessKey
2922
- }
2923
- });
2924
- return minioStorage({
2925
- endpoint,
2926
- bucket,
2927
- credentials: {
2928
- accessKeyId,
2929
- secretAccessKey
2930
- }
2931
- });
2932
- };
2933
- const normalizeKeyPrefix = (prefix) => {
2934
- if (prefix === "") return "";
2935
- if (prefix.endsWith("/")) return prefix;
2936
- return `${prefix}/`;
2937
- };
2938
- //#endregion
2939
- //#region src/config.ts
2940
- /**
2941
- * `baerly.config.ts` loader for `baerly deploy` / `baerly doctor`.
2942
- *
2943
- * Mirrors the shape of `BaerlyAppConfig` from `@gusto/baerly-storage/config`
2944
- * but lives in `@baerly/cli` so the validator/loader doesn't depend on
2945
- * the rolldown'd umbrella bundle. The scaffolder emits the config file;
2946
- * the CLI reads it. Both sides agree on the wire shape (informally —
2947
- * the duplication is small and intentional).
2948
- *
2949
- * Resolution order:
2950
- * 1. `<cwd>/baerly.config.ts`
2951
- * 2. `<cwd>/baerly.config.js`
2952
- * 3. `<cwd>/baerly.config.mjs`
2953
- * 4. `<cwd>/baerly.config.json`
2954
- *
2955
- * The `.ts` form requires the user to have a TS-aware Node entry
2956
- * point (`tsx`, `--experimental-strip-types`, or a build step that
2957
- * produced the `.js` alongside). If `import()` throws on the `.ts`
2958
- * file, we re-wrap as `InvalidConfig` with a hint pointing at the
2959
- * `.js` / `.json` fallbacks.
2960
- */
2961
- const CONFIG_BASENAMES = [
2962
- "baerly.config.ts",
2963
- "baerly.config.js",
2964
- "baerly.config.mjs",
2965
- "baerly.config.json"
2966
- ];
2967
- /**
2968
- * Locate the first `baerly.config.*` file under `cwd`. Returns the
2969
- * absolute path or `null` if none exists.
2970
- *
2971
- * Exported so the collection-aware loader (and future consumers) can
2972
- * reuse the same resolution order without re-implementing it.
2973
- */
2974
- const locateConfig = (cwd) => {
2975
- for (const base of CONFIG_BASENAMES) {
2976
- const abs = resolve(cwd, base);
2977
- if (existsSync(abs)) return abs;
2978
- }
2979
- return null;
2980
- };
2981
- /**
2982
- * Load `baerly.config.{ts,js,mjs,json}` from `cwd` (defaults to
2983
- * `process.cwd()`). Validates the required fields and the
2984
- * locked-shape optionals; returns an `AppConfig` decorated with the
2985
- * `repoRoot` it was loaded from.
2986
- *
2987
- * @throws BaerlyError code="InvalidConfig" — file missing, default
2988
- * export missing, or any field has the wrong shape / type.
2989
- */
2990
- const loadAppConfig = async (cwd = process.cwd()) => {
2991
- const cfgPath = locateConfig(cwd);
2992
- if (cfgPath === null) throw new BaerlyError("InvalidConfig", `baerly: no baerly.config.{ts,js,mjs,json} at ${cwd}. Are you in a baerly app directory?`);
2993
- let raw;
2994
- if (cfgPath.endsWith(".json")) {
2995
- let text;
2996
- try {
2997
- text = await readFile(cfgPath, "utf8");
2998
- } catch (error) {
2999
- throw new BaerlyError("InvalidConfig", `baerly: failed to read ${cfgPath}: ${error.message}`, error);
3000
- }
3001
- try {
3002
- raw = JSON.parse(text);
3003
- } catch (error) {
3004
- throw new BaerlyError("InvalidConfig", `baerly: failed to parse ${cfgPath}: ${error.message}`, error);
3005
- }
3006
- } else {
3007
- let mod;
3008
- try {
3009
- mod = await import(pathToFileURL(cfgPath).href);
3010
- } catch (error) {
3011
- const hint = cfgPath.endsWith(".ts") ? " (Node cannot import .ts directly without a TS loader — point at the compiled .js or use a .json config)" : "";
3012
- throw new BaerlyError("InvalidConfig", `baerly: failed to load ${cfgPath}: ${error.message}${hint}`, error);
3013
- }
3014
- raw = mod.default;
3015
- }
3016
- if (raw === void 0 || typeof raw !== "object" || raw === null) throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath} must default-export a BaerlyAppConfig object`);
3017
- const obj = raw;
3018
- const app = obj["app"];
3019
- const tenant = obj["tenant"];
3020
- const target = obj["target"];
3021
- if (typeof app !== "string" || app.length === 0) throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`app\` must be a non-empty string`);
3022
- if (typeof tenant !== "string" || tenant.length === 0) throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`tenant\` must be a non-empty string`);
3023
- if (target !== "cloudflare" && target !== "node") throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`target\` must be "cloudflare" or "node" (got ${JSON.stringify(target)})`);
3024
- const authRaw = obj["auth"];
3025
- if (typeof authRaw !== "string" || !AUTH_CONFIG_VALUES.includes(authRaw)) throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`auth\` is required and must be one of ${JSON.stringify(AUTH_CONFIG_VALUES)} (got ${JSON.stringify(authRaw)})`);
3026
- const auth = authRaw;
3027
- const domainRaw = obj["domain"];
3028
- if (domainRaw !== void 0 && typeof domainRaw !== "string") throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`domain\` must be a string when set`);
3029
- const secretsRaw = obj["requiredSecrets"];
3030
- let requiredSecrets;
3031
- if (secretsRaw !== void 0) {
3032
- if (!Array.isArray(secretsRaw) || !secretsRaw.every((s) => typeof s === "string")) throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`requiredSecrets\` must be an array of strings`);
3033
- requiredSecrets = secretsRaw;
3034
- }
3035
- const cfaRaw = obj["cloudflareAccess"];
3036
- let cloudflareAccess;
3037
- if (cfaRaw !== void 0) {
3038
- if (typeof cfaRaw !== "object" || cfaRaw === null) throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`cloudflareAccess\` must be an object when set`);
3039
- const cfa = cfaRaw;
3040
- const td = cfa["teamDomain"];
3041
- const aud = cfa["audienceTag"];
3042
- if (typeof td !== "string" || td.length === 0) throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`cloudflareAccess.teamDomain\` must be a non-empty string`);
3043
- if (typeof aud !== "string" || aud.length === 0) throw new BaerlyError("InvalidConfig", `baerly: ${cfgPath}: \`cloudflareAccess.audienceTag\` must be a non-empty string`);
3044
- cloudflareAccess = {
3045
- teamDomain: td,
3046
- audienceTag: aud
3047
- };
3048
- }
3049
- return {
3050
- app,
3051
- tenant,
3052
- target,
3053
- auth,
3054
- ...domainRaw !== void 0 && { domain: domainRaw },
3055
- ...requiredSecrets !== void 0 && { requiredSecrets },
3056
- ...cloudflareAccess !== void 0 && { cloudflareAccess },
3057
- repoRoot: cwd
3058
- };
3059
- };
3060
- /**
3061
- * Load the declared `IndexDefinition[]` for a single collection from a
3062
- * `baerly.config.{js,mjs,json}`. Used by `baerly inspect` (key-count
3063
- * probes) and `baerly admin rebuild-index` (resolving the `on` field).
3064
- *
3065
- * `.ts` configs are rejected — Node can't `import` them without a TS
3066
- * loader; point at the compiled `.js` output instead.
3067
- *
3068
- * `commandName` is woven into thrown error messages so the operator
3069
- * sees the calling command name in the prefix.
3070
- *
3071
- * @throws BaerlyError code="InvalidConfig" — `.ts` extension, JSON
3072
- * parse error, missing default export, or missing
3073
- * `collections[collection]`.
3074
- */
3075
- const loadCollectionIndexes = async (configPath, collection, commandName) => {
3076
- if (configPath.endsWith(".ts")) throw new BaerlyError("InvalidConfig", `${commandName}: --config must point at compiled JS / JSON (got .ts: ${JSON.stringify(configPath)})`);
3077
- let cfg;
3078
- if (configPath.endsWith(".json")) {
3079
- const text = await readFile(configPath, "utf8");
3080
- try {
3081
- cfg = JSON.parse(text);
3082
- } catch (error) {
3083
- throw new BaerlyError("InvalidConfig", `${commandName}: --config JSON parse error in ${JSON.stringify(configPath)}: ${error.message}`);
3084
- }
3085
- } else {
3086
- const mod = await import(pathToFileURL(configPath.startsWith("file://") ? fileURLToPath(configPath) : resolve(configPath)).href);
3087
- if (mod.default === void 0) throw new BaerlyError("InvalidConfig", `${commandName}: --config ${JSON.stringify(configPath)} has no default export`);
3088
- cfg = mod.default;
3089
- }
3090
- return cfg.collections?.[collection]?.indexes ?? [];
3091
- };
3092
- //#endregion
3093
- export { MAINTENANCE_PROFILE_CF_PAID as _, DEFAULT_GCS_ENDPOINT as a, SHARED_SECRET_MISSING_MESSAGE as b, uuid as c, GC_GRACE_PERIOD_MILLIS as d, GC_MAX_PENDING_CANDIDATES as f, MAINTENANCE_PROFILE_CF_FREE as g, MAINTENANCE_MIN_LIVE_BYTES as h, goog4Signer as i, sha256Hex as l, LOG_FORWARD_PROBE_CAP as m, loadCollectionIndexes as n, timestamp as o, GC_PENDING_CONTENT_TYPE as p, parseBucketUri as r, countKey as s, loadAppConfig as t, CURRENT_JSON_CONTENT_TYPE as u, MAINTENANCE_WARN_INTERVAL_WRITES as v, MAX_KEY_BYTES as y };
3094
-
3095
- //# sourceMappingURL=config-B6nTy8e2.js.map