@geekmidas/cli 10.0.0-alpha.94 → 10.0.0-alpha.96

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 (206) hide show
  1. package/dist/{GoDaddyProvider-BRZUWD8z.mjs → GoDaddyProvider-BsUKjGcg.mjs} +3 -3
  2. package/dist/{GoDaddyProvider-BRZUWD8z.mjs.map → GoDaddyProvider-BsUKjGcg.mjs.map} +1 -1
  3. package/dist/{GoDaddyProvider-DzguxU_a.cjs → GoDaddyProvider-DyHXHDHe.cjs} +3 -3
  4. package/dist/{GoDaddyProvider-DzguxU_a.cjs.map → GoDaddyProvider-DyHXHDHe.cjs.map} +1 -1
  5. package/dist/{HostingerProvider-Cjh_2Ml6.mjs → HostingerProvider-BNpS9aRJ.mjs} +2 -2
  6. package/dist/{HostingerProvider-Cjh_2Ml6.mjs.map → HostingerProvider-BNpS9aRJ.mjs.map} +1 -1
  7. package/dist/{HostingerProvider-OdQmKXE3.cjs → HostingerProvider-DR1iws1P.cjs} +2 -2
  8. package/dist/{HostingerProvider-OdQmKXE3.cjs.map → HostingerProvider-DR1iws1P.cjs.map} +1 -1
  9. package/dist/{LegacyStateStore-BQZb6-j2.mjs → LegacyStateStore-B6SOqRFi.mjs} +3 -3
  10. package/dist/{LegacyStateStore-BQZb6-j2.mjs.map → LegacyStateStore-B6SOqRFi.mjs.map} +1 -1
  11. package/dist/{LegacyStateStore-D50dgUPc.cjs → LegacyStateStore-BnMcONE0.cjs} +3 -3
  12. package/dist/{LegacyStateStore-D50dgUPc.cjs.map → LegacyStateStore-BnMcONE0.cjs.map} +1 -1
  13. package/dist/{LocalStateStore-BSaS2535.mjs → LocalStateStore-6CrsZzqI.mjs} +2 -2
  14. package/dist/{LocalStateStore-BSaS2535.mjs.map → LocalStateStore-6CrsZzqI.mjs.map} +1 -1
  15. package/dist/{LocalStateStore-EFBUjuMX.cjs → LocalStateStore-pd4bbdJJ.cjs} +2 -2
  16. package/dist/{LocalStateStore-EFBUjuMX.cjs.map → LocalStateStore-pd4bbdJJ.cjs.map} +1 -1
  17. package/dist/{S3StateStore-C5mv8To3.cjs → S3StateStore-CoGk6ZKU.cjs} +2 -2
  18. package/dist/{S3StateStore-C5mv8To3.cjs.map → S3StateStore-CoGk6ZKU.cjs.map} +1 -1
  19. package/dist/{S3StateStore-CrFq4UZU.mjs → S3StateStore-DP_7Q4yb.mjs} +2 -2
  20. package/dist/{S3StateStore-CrFq4UZU.mjs.map → S3StateStore-DP_7Q4yb.mjs.map} +1 -1
  21. package/dist/{SSMStateStore-D3Z6mZnA.mjs → SSMStateStore-Bo6_KDYY.mjs} +2 -2
  22. package/dist/{SSMStateStore-D3Z6mZnA.mjs.map → SSMStateStore-Bo6_KDYY.mjs.map} +1 -1
  23. package/dist/{SSMStateStore-DuZCH7_v.cjs → SSMStateStore-D9s4_Do3.cjs} +2 -2
  24. package/dist/{SSMStateStore-DuZCH7_v.cjs.map → SSMStateStore-D9s4_Do3.cjs.map} +1 -1
  25. package/dist/{add-C85isXFl.mjs → add-BQS2QZHp.mjs} +22 -51
  26. package/dist/add-BQS2QZHp.mjs.map +1 -0
  27. package/dist/{add-DsVDdvon.cjs → add-CjURJ-vI.cjs} +23 -52
  28. package/dist/add-CjURJ-vI.cjs.map +1 -0
  29. package/dist/{bundler-BSHog8CH.mjs → bundler-BMM3UsGm.mjs} +5 -5
  30. package/dist/{bundler-BSHog8CH.mjs.map → bundler-BMM3UsGm.mjs.map} +1 -1
  31. package/dist/{bundler-BEgUKc4j.cjs → bundler-DmLJQ7AY.cjs} +5 -5
  32. package/dist/{bundler-BEgUKc4j.cjs.map → bundler-DmLJQ7AY.cjs.map} +1 -1
  33. package/dist/{cli-BvBdv660.cjs → cli-C2c-jOVK.cjs} +4 -4
  34. package/dist/{cli-BvBdv660.cjs.map → cli-C2c-jOVK.cjs.map} +1 -1
  35. package/dist/{cli-BsFenyaf.mjs → cli-Ce6NgaQF.mjs} +4 -4
  36. package/dist/{cli-BsFenyaf.mjs.map → cli-Ce6NgaQF.mjs.map} +1 -1
  37. package/dist/{compose-D2RFYd_f.mjs → compose-BJDPFBAN.mjs} +43 -47
  38. package/dist/compose-BJDPFBAN.mjs.map +1 -0
  39. package/dist/{compose-Cd09gW-n.cjs → compose-ETFKqkTu.cjs} +60 -64
  40. package/dist/compose-ETFKqkTu.cjs.map +1 -0
  41. package/dist/{config-Ngesg4O8.d.mts → config-4yirvxS8.d.cts} +4 -4
  42. package/dist/{config-DvHNMoOp.d.cts.map → config-4yirvxS8.d.cts.map} +1 -1
  43. package/dist/{config-DvHNMoOp.d.cts → config-C8NPCn1l.d.mts} +4 -4
  44. package/dist/{config-Ngesg4O8.d.mts.map → config-C8NPCn1l.d.mts.map} +1 -1
  45. package/dist/config.cjs +1 -1
  46. package/dist/config.d.cts +3 -3
  47. package/dist/config.d.mts +3 -3
  48. package/dist/config.mjs +1 -1
  49. package/dist/{credentials-DpfnUvIR.mjs → credentials-B5N5yKk6.mjs} +4 -132
  50. package/dist/credentials-B5N5yKk6.mjs.map +1 -0
  51. package/dist/{credentials-D7XJypNd.cjs → credentials-Zqr3QQYB.cjs} +3 -173
  52. package/dist/credentials-Zqr3QQYB.cjs.map +1 -0
  53. package/dist/deploy/public.cjs +16 -15
  54. package/dist/deploy/public.cjs.map +1 -1
  55. package/dist/deploy/public.d.cts +5 -5
  56. package/dist/deploy/public.d.cts.map +1 -1
  57. package/dist/deploy/public.d.mts +5 -5
  58. package/dist/deploy/public.d.mts.map +1 -1
  59. package/dist/deploy/public.mjs +11 -10
  60. package/dist/deploy/public.mjs.map +1 -1
  61. package/dist/deploy-BdN0ym2i.cjs +2057 -0
  62. package/dist/deploy-BdN0ym2i.cjs.map +1 -0
  63. package/dist/deploy-DktQl7ti.mjs +2008 -0
  64. package/dist/deploy-DktQl7ti.mjs.map +1 -0
  65. package/dist/{discover-CTzOSGUF.d.cts → discover-BtFofyQB.d.cts} +2 -2
  66. package/dist/{discover-CTzOSGUF.d.cts.map → discover-BtFofyQB.d.cts.map} +1 -1
  67. package/dist/{discover-CihAtaoj.d.mts → discover-D-MdXZgm.d.mts} +2 -2
  68. package/dist/{discover-CihAtaoj.d.mts.map → discover-D-MdXZgm.d.mts.map} +1 -1
  69. package/dist/dns-BjHECDO7.mjs +44 -0
  70. package/dist/dns-BjHECDO7.mjs.map +1 -0
  71. package/dist/dns-C-X6PLkV.cjs +663 -0
  72. package/dist/dns-C-X6PLkV.cjs.map +1 -0
  73. package/dist/dns-C2EeR6n2.mjs +610 -0
  74. package/dist/dns-C2EeR6n2.mjs.map +1 -0
  75. package/dist/dns-ClI7-d5g.cjs +55 -0
  76. package/dist/dns-ClI7-d5g.cjs.map +1 -0
  77. package/dist/{dokploy-CV7GcobZ.cjs → dokploy-CD_Bc4EV.cjs} +2 -2
  78. package/dist/{dokploy-CV7GcobZ.cjs.map → dokploy-CD_Bc4EV.cjs.map} +1 -1
  79. package/dist/{dokploy-D_aat2Xj.mjs → dokploy-CxtkS5hl.mjs} +2 -2
  80. package/dist/{dokploy-D_aat2Xj.mjs.map → dokploy-CxtkS5hl.mjs.map} +1 -1
  81. package/dist/{encryption-_4qKqtTx.mjs → encryption-BgQd4f2S.mjs} +3 -3
  82. package/dist/{encryption-_4qKqtTx.mjs.map → encryption-BgQd4f2S.mjs.map} +1 -1
  83. package/dist/{encryption-DFcivJaO.cjs → encryption-CqVfz-m7.cjs} +3 -3
  84. package/dist/{encryption-DFcivJaO.cjs.map → encryption-CqVfz-m7.cjs.map} +1 -1
  85. package/dist/{engine-Bv-LZpmj.cjs → engine-OVVKtPmW.cjs} +18 -36
  86. package/dist/engine-OVVKtPmW.cjs.map +1 -0
  87. package/dist/{engine-DPMHThkg.mjs → engine-YlXzlTUl.mjs} +14 -32
  88. package/dist/{engine-DPMHThkg.mjs.map → engine-YlXzlTUl.mjs.map} +1 -1
  89. package/dist/{github-Br42NMrW.cjs → github-CsMloeSL.cjs} +9 -7
  90. package/dist/{github-Br42NMrW.cjs.map → github-CsMloeSL.cjs.map} +1 -1
  91. package/dist/{github-xsDT19im.mjs → github-DuuvqI3Z.mjs} +5 -3
  92. package/dist/{github-xsDT19im.mjs.map → github-DuuvqI3Z.mjs.map} +1 -1
  93. package/dist/{index-CwLvFqtH.d.cts → index-5eL8rM6H.d.mts} +2 -2
  94. package/dist/{index-BlpcClwM.d.mts.map → index-5eL8rM6H.d.mts.map} +1 -1
  95. package/dist/{index-BlpcClwM.d.mts → index-Dz-gyjm_.d.cts} +2 -2
  96. package/dist/{index-CwLvFqtH.d.cts.map → index-Dz-gyjm_.d.cts.map} +1 -1
  97. package/dist/index.cjs +54 -53
  98. package/dist/index.cjs.map +1 -1
  99. package/dist/index.mjs +52 -51
  100. package/dist/index.mjs.map +1 -1
  101. package/dist/{local-B7Zv1NoA.mjs → local-BwC2v2Y8.mjs} +2 -2
  102. package/dist/{local-B7Zv1NoA.mjs.map → local-BwC2v2Y8.mjs.map} +1 -1
  103. package/dist/{local-OPC9BCZt.cjs → local-DNhnkugT.cjs} +3 -3
  104. package/dist/{local-OPC9BCZt.cjs.map → local-DNhnkugT.cjs.map} +1 -1
  105. package/dist/{migrate-D5jUz20_.cjs → migrate-BAmJeVq1.cjs} +3 -3
  106. package/dist/{migrate-D5jUz20_.cjs.map → migrate-BAmJeVq1.cjs.map} +1 -1
  107. package/dist/{migrate-SkWJotn5.cjs → migrate-BdkoNdIu.cjs} +3 -3
  108. package/dist/{migrate-SkWJotn5.cjs.map → migrate-BdkoNdIu.cjs.map} +1 -1
  109. package/dist/{migrate-DhvFPGLi.mjs → migrate-CB8B6W9y.mjs} +3 -3
  110. package/dist/{migrate-DhvFPGLi.mjs.map → migrate-CB8B6W9y.mjs.map} +1 -1
  111. package/dist/{migrate-B7evHv7O.mjs → migrate-CsPFHgTs.mjs} +3 -3
  112. package/dist/{migrate-B7evHv7O.mjs.map → migrate-CsPFHgTs.mjs.map} +1 -1
  113. package/dist/openapi.cjs +1 -1
  114. package/dist/openapi.d.cts +1 -1
  115. package/dist/openapi.d.mts +1 -1
  116. package/dist/openapi.mjs +1 -1
  117. package/dist/reconcile/public.d.cts +2 -2
  118. package/dist/reconcile/public.d.mts +2 -2
  119. package/dist/{resolve-CFEOtRsl.cjs → resolve-BDatNOoG.cjs} +4 -4
  120. package/dist/{resolve-CFEOtRsl.cjs.map → resolve-BDatNOoG.cjs.map} +1 -1
  121. package/dist/{resolve-Dyd1S2wn.mjs → resolve-CUne0qOm.mjs} +4 -4
  122. package/dist/{resolve-Dyd1S2wn.mjs.map → resolve-CUne0qOm.mjs.map} +1 -1
  123. package/dist/{rollback-NroNbGC0.mjs → rollback-CCdEEEcF.mjs} +7 -8
  124. package/dist/{rollback-NroNbGC0.mjs.map → rollback-CCdEEEcF.mjs.map} +1 -1
  125. package/dist/{rollback-D720SFhV.cjs → rollback-CuG3DkTY.cjs} +8 -9
  126. package/dist/{rollback-D720SFhV.cjs.map → rollback-CuG3DkTY.cjs.map} +1 -1
  127. package/dist/run-BJLIerMM.mjs +133 -0
  128. package/dist/run-BJLIerMM.mjs.map +1 -0
  129. package/dist/run-fVZ-rLnc.cjs +174 -0
  130. package/dist/run-fVZ-rLnc.cjs.map +1 -0
  131. package/dist/{runtime-VktAo_8B.d.cts → runtime-DUQdM-mW.d.cts} +2 -2
  132. package/dist/{runtime-BRCq6dQ7.d.mts.map → runtime-DUQdM-mW.d.cts.map} +1 -1
  133. package/dist/{runtime-BRCq6dQ7.d.mts → runtime-Vr6mleo2.d.mts} +2 -2
  134. package/dist/{runtime-VktAo_8B.d.cts.map → runtime-Vr6mleo2.d.mts.map} +1 -1
  135. package/dist/{sandbox-D1gd0iDz.d.cts → sandbox-BbJmh2rI.d.cts} +2 -2
  136. package/dist/{sandbox-D1gd0iDz.d.cts.map → sandbox-BbJmh2rI.d.cts.map} +1 -1
  137. package/dist/{sandbox-zHnRLCj0.d.mts → sandbox-DGOsMPMf.d.mts} +2 -2
  138. package/dist/{sandbox-zHnRLCj0.d.mts.map → sandbox-DGOsMPMf.d.mts.map} +1 -1
  139. package/dist/{secrets-DAvi_O9g.cjs → secrets-C5qROlbp.cjs} +3 -3
  140. package/dist/{secrets-DAvi_O9g.cjs.map → secrets-C5qROlbp.cjs.map} +1 -1
  141. package/dist/{secrets-CwLauUB_.mjs → secrets-ZikTIxEm.mjs} +3 -3
  142. package/dist/{secrets-CwLauUB_.mjs.map → secrets-ZikTIxEm.mjs.map} +1 -1
  143. package/dist/{seeds-BCg8X36S.cjs → seeds-BjXW-Krf.cjs} +193 -946
  144. package/dist/seeds-BjXW-Krf.cjs.map +1 -0
  145. package/dist/{seeds-CTxTZa-R.mjs → seeds-CbJSne1A.mjs} +156 -825
  146. package/dist/seeds-CbJSne1A.mjs.map +1 -0
  147. package/dist/{sniffer-DCcuzHz_.mjs → sniffer-DQGBYY8i.mjs} +8 -8
  148. package/dist/{sniffer-DCcuzHz_.mjs.map → sniffer-DQGBYY8i.mjs.map} +1 -1
  149. package/dist/{sniffer-C0z3NB2A.cjs → sniffer-NPD1dyab.cjs} +8 -8
  150. package/dist/{sniffer-C0z3NB2A.cjs.map → sniffer-NPD1dyab.cjs.map} +1 -1
  151. package/dist/{sst-DRDSDwOk.mjs → sst-CGdjHn2A.mjs} +4 -3
  152. package/dist/{sst-DRDSDwOk.mjs.map → sst-CGdjHn2A.mjs.map} +1 -1
  153. package/dist/{sst-DKNH243z.cjs → sst-DGuHDxVU.cjs} +5 -4
  154. package/dist/{sst-DKNH243z.cjs.map → sst-DGuHDxVU.cjs.map} +1 -1
  155. package/dist/{stack-NrdH5ngX.mjs → stack-BFq3K-_z.mjs} +14 -52
  156. package/dist/stack-BFq3K-_z.mjs.map +1 -0
  157. package/dist/{stack-B3fPDTBg.cjs → stack-DOVvEd1T.cjs} +14 -52
  158. package/dist/stack-DOVvEd1T.cjs.map +1 -0
  159. package/dist/{stages-veC4TcQe.mjs → stages-BEn2ArJZ.mjs} +4 -3
  160. package/dist/{stages-veC4TcQe.mjs.map → stages-BEn2ArJZ.mjs.map} +1 -1
  161. package/dist/{stages-CkqP1N4S.cjs → stages-Cw6urZyJ.cjs} +5 -4
  162. package/dist/{stages-CkqP1N4S.cjs.map → stages-Cw6urZyJ.cjs.map} +1 -1
  163. package/dist/target/public.cjs +1 -1
  164. package/dist/target/public.d.cts +2 -2
  165. package/dist/target/public.d.mts +2 -2
  166. package/dist/target/public.mjs +1 -1
  167. package/dist/{terminal-CkUYuAPd.cjs → terminal-CPHuH4S1.cjs} +2 -2
  168. package/dist/{terminal-CkUYuAPd.cjs.map → terminal-CPHuH4S1.cjs.map} +1 -1
  169. package/dist/{terminal-DAHs2Zko.mjs → terminal-iNmAXXjs.mjs} +2 -2
  170. package/dist/{terminal-DAHs2Zko.mjs.map → terminal-iNmAXXjs.mjs.map} +1 -1
  171. package/dist/{types-DMoaFrjg.d.mts → types-bkqVscdN.d.mts} +12 -3
  172. package/dist/types-bkqVscdN.d.mts.map +1 -0
  173. package/dist/{types-DXyaSmqu.d.cts → types-dipJRmKV.d.cts} +12 -3
  174. package/dist/types-dipJRmKV.d.cts.map +1 -0
  175. package/dist/workspace/index.cjs +1 -1
  176. package/dist/workspace/index.d.cts +2 -2
  177. package/dist/workspace/index.d.mts +2 -2
  178. package/dist/workspace/index.mjs +1 -1
  179. package/dist/{workspace-DWK1PTXP.mjs → workspace-CqFUjObk.mjs} +54 -2
  180. package/dist/workspace-CqFUjObk.mjs.map +1 -0
  181. package/dist/{workspace-cSyi5nb1.cjs → workspace-DDkm4y6D.cjs} +71 -1
  182. package/dist/workspace-DDkm4y6D.cjs.map +1 -0
  183. package/package.json +15 -15
  184. package/dist/add-C85isXFl.mjs.map +0 -1
  185. package/dist/add-DsVDdvon.cjs.map +0 -1
  186. package/dist/compose-Cd09gW-n.cjs.map +0 -1
  187. package/dist/compose-D2RFYd_f.mjs.map +0 -1
  188. package/dist/credentials-D7XJypNd.cjs.map +0 -1
  189. package/dist/credentials-DpfnUvIR.mjs.map +0 -1
  190. package/dist/deploy-C911Akkn.cjs +0 -670
  191. package/dist/deploy-C911Akkn.cjs.map +0 -1
  192. package/dist/deploy-CeJ5D0xn.mjs +0 -641
  193. package/dist/deploy-CeJ5D0xn.mjs.map +0 -1
  194. package/dist/engine-Bv-LZpmj.cjs.map +0 -1
  195. package/dist/providers-BFeP_mYw.cjs +0 -1098
  196. package/dist/providers-BFeP_mYw.cjs.map +0 -1
  197. package/dist/providers-sTj-BG0S.mjs +0 -1079
  198. package/dist/providers-sTj-BG0S.mjs.map +0 -1
  199. package/dist/seeds-BCg8X36S.cjs.map +0 -1
  200. package/dist/seeds-CTxTZa-R.mjs.map +0 -1
  201. package/dist/stack-B3fPDTBg.cjs.map +0 -1
  202. package/dist/stack-NrdH5ngX.mjs.map +0 -1
  203. package/dist/types-DMoaFrjg.d.mts.map +0 -1
  204. package/dist/types-DXyaSmqu.d.cts.map +0 -1
  205. package/dist/workspace-DWK1PTXP.mjs.map +0 -1
  206. package/dist/workspace-cSyi5nb1.cjs.map +0 -1
@@ -0,0 +1,2057 @@
1
+ const require_rolldown_runtime = require("./rolldown-runtime-C0BPl7ul.cjs");
2
+ const require_errors = require("./errors-DMC4Umsb.cjs");
3
+ const require_run = require("./run-fVZ-rLnc.cjs");
4
+ const require_credentials = require("./credentials-Zqr3QQYB.cjs");
5
+ const require_discover = require("./discover-DPgu-ScS.cjs");
6
+ const require_sandbox = require("./sandbox-CHv3OYXg.cjs");
7
+ const require_derive = require("./derive-DAbo49sm.cjs");
8
+ const require_workspace = require("./workspace-DDkm4y6D.cjs");
9
+ const require_define = require("./define-crmMcXFS.cjs");
10
+ const require_config = require("./config.cjs");
11
+ const require_seeds = require("./seeds-BjXW-Krf.cjs");
12
+ const require_resolve = require("./resolve-BDatNOoG.cjs");
13
+ const require_local = require("./local-DNhnkugT.cjs");
14
+ const require_sniffer = require("./sniffer-NPD1dyab.cjs");
15
+ const require_dns = require("./dns-C-X6PLkV.cjs");
16
+ const require_dns$1 = require("./dns-ClI7-d5g.cjs");
17
+ const require_credentialSchemas = require("./credentialSchemas-CAPKFkrG.cjs");
18
+ let node_path = require("node:path");
19
+ let _geekmidas_manifest = require("@geekmidas/manifest");
20
+ let node_crypto = require("node:crypto");
21
+ let _geekmidas_storage_s3_url = require("@geekmidas/storage/s3-url");
22
+ _geekmidas_storage_s3_url = require_rolldown_runtime.__toESM(_geekmidas_storage_s3_url, 1);
23
+ //#region src/target/secrets.ts
24
+ /**
25
+ * A stage's secrets as a target sees them: read and written through the
26
+ * stage's own store, and masked in everything the run prints once read.
27
+ */
28
+ /**
29
+ * Shorter values are left alone: what a progress line legitimately prints —
30
+ * a port, a stage, an app name — would otherwise come out as `***` wherever
31
+ * a secret happened to share it.
32
+ */
33
+ const SHORTEST_MASKED = 6;
34
+ /** The values a run must never print, and the rewrite that keeps it so. */
35
+ var Redactor = class {
36
+ values = /* @__PURE__ */ new Set();
37
+ add(value) {
38
+ if (value && value.length >= SHORTEST_MASKED) this.values.add(value);
39
+ }
40
+ /** `text` with every masked value replaced by `***`, longest first. */
41
+ redact(text) {
42
+ if (this.values.size === 0) return text;
43
+ let out = text;
44
+ const longestFirst = [...this.values].sort((a, b) => b.length - a.length);
45
+ for (const value of longestFirst) out = out.replaceAll(value, "***");
46
+ return out;
47
+ }
48
+ };
49
+ /** Every value in `secrets` that is a secret rather than a label. */
50
+ function secretValues(secrets) {
51
+ return [
52
+ secrets.seed,
53
+ ...Object.values(secrets.custom ?? {}),
54
+ ...Object.values(secrets.urls ?? {}),
55
+ ...Object.values(secrets.services ?? {}).map((service) => service?.password)
56
+ ].filter((value) => typeof value === "string");
57
+ }
58
+ /**
59
+ * The stage's secrets through the store `secrets.store` names for it. Opening
60
+ * one builds a client and makes no request, so a target that never reads them
61
+ * costs nothing.
62
+ */
63
+ async function deploySecrets(workspace, stage, redactor, options = {}) {
64
+ const store = await require_seeds.secretsStoreFor(workspace, stage, options);
65
+ const mask = (secrets) => {
66
+ for (const value of secretValues(secrets)) redactor.add(value);
67
+ };
68
+ return {
69
+ store: store.name,
70
+ async read() {
71
+ const secrets = await store.read(stage);
72
+ if (secrets) mask(secrets);
73
+ return secrets;
74
+ },
75
+ async write(secrets) {
76
+ mask(secrets);
77
+ await store.write(stage, secrets);
78
+ },
79
+ mask: (value) => redactor.add(value)
80
+ };
81
+ }
82
+ //#endregion
83
+ //#region src/providers/aws.ts
84
+ /**
85
+ * The AWS account a provider provisions in — the stage's, found the way the
86
+ * SSM and Secrets Manager stores find it.
87
+ *
88
+ * 1. `--profile`, resolved on its own: exported keys left over from another
89
+ * account never win over a profile someone named.
90
+ * 2. `AWS_PROFILE`, else `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` — the
91
+ * deploy's own `aws` credential kind (`storedCredentials`).
92
+ * 3. The SDK's default chain: `~/.aws` default profile, SSO, a role. In CI,
93
+ * whatever `aws-actions/configure-aws-credentials` exported.
94
+ *
95
+ * None of them: the provider has no provisioning credentials, and the stage
96
+ * is treated as `external`.
97
+ */
98
+ async function awsProvisioningCredentials(lookup) {
99
+ const region = lookup.env.AWS_REGION ?? lookup.env.AWS_DEFAULT_REGION;
100
+ const where = region ? { region } : {};
101
+ if (lookup.profile) return {
102
+ profile: lookup.profile,
103
+ ...where
104
+ };
105
+ const stored = await require_credentials.storedCredentials({
106
+ env: lookup.env,
107
+ ...lookup.home ? { home: lookup.home } : {}
108
+ }).get({
109
+ kind: "aws",
110
+ stage: lookup.stage
111
+ });
112
+ if (stored) return stored;
113
+ try {
114
+ const { fromNodeProviderChain } = await import("@aws-sdk/credential-providers");
115
+ await fromNodeProviderChain({
116
+ timeout: 1e3,
117
+ maxRetries: 0
118
+ })();
119
+ return {
120
+ chain: true,
121
+ ...where
122
+ };
123
+ } catch {
124
+ return;
125
+ }
126
+ }
127
+ /**
128
+ * An S3-compatible endpoint the environment points the SDK at — the AWS
129
+ * emulator in tests. The SDK reads it itself; it is read here too, because
130
+ * an emulator takes path-style requests, and a key written for one has to
131
+ * say where it is.
132
+ */
133
+ function awsEndpoint(env, service) {
134
+ return env[`AWS_ENDPOINT_URL_${service}`] ?? env.AWS_ENDPOINT_URL;
135
+ }
136
+ /** An SDK client's config for a provisioning credential. */
137
+ async function awsClientConfig(credential, region) {
138
+ if ("profile" in credential) {
139
+ const { fromIni } = await import("@aws-sdk/credential-providers");
140
+ return {
141
+ region,
142
+ credentials: fromIni({ profile: credential.profile })
143
+ };
144
+ }
145
+ if ("accessKeyId" in credential) return {
146
+ region,
147
+ credentials: {
148
+ accessKeyId: credential.accessKeyId,
149
+ secretAccessKey: credential.secretAccessKey,
150
+ ...credential.sessionToken ? { sessionToken: credential.sessionToken } : {}
151
+ }
152
+ };
153
+ return { region };
154
+ }
155
+ const SUFFIX_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
156
+ /** A name that S3 would refuse — what the name builder never returns. */
157
+ var InvalidBucketName = class extends require_errors.GkmError {
158
+ bucket;
159
+ reason;
160
+ constructor(bucket, reason) {
161
+ super(`'${bucket}' is not a valid S3 bucket name: ${reason}. Rename the construct or set deploy.namespace in gkm.config.ts to something that leaves room for it.`);
162
+ this.bucket = bucket;
163
+ this.reason = reason;
164
+ this.name = "InvalidBucketName";
165
+ }
166
+ };
167
+ /** Lowercase `[a-z0-9-]`, runs of anything else one `-`, no edge dashes. */
168
+ function part(value) {
169
+ return (0, _geekmidas_manifest.kebabCase)(value).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
170
+ }
171
+ /**
172
+ * `parts` joined by `-`, shortened to `max` characters by trimming the longest
173
+ * part one character at a time — so a long construct id gives way before a
174
+ * short stage name does, and every part keeps its start.
175
+ */
176
+ function fitParts(parts, max) {
177
+ const kept = parts.map(part).filter(Boolean);
178
+ const length = () => kept.reduce((sum, p) => sum + p.length, 0) + kept.length - 1;
179
+ while (length() > max) {
180
+ let longest = 0;
181
+ for (let i = 1; i < kept.length; i++) if (kept[i].length > kept[longest].length) longest = i;
182
+ if (kept[longest].length <= 1) break;
183
+ kept[longest] = kept[longest].slice(0, -1).replace(/-+$/, "");
184
+ }
185
+ return kept.join("-");
186
+ }
187
+ /** Why `name` is not a valid bucket name, or undefined when it is. */
188
+ function bucketNameProblem(name) {
189
+ if (name.length < 3 || name.length > 63) return `it is ${name.length} characters, and S3 takes 3 to 63`;
190
+ if (!/^[a-z0-9-]+$/.test(name)) return "it may hold only lowercase letters, digits and -";
191
+ if (!/^[a-z0-9].*[a-z0-9]$/.test(name)) return "it must start and end with a letter or a digit";
192
+ if (name.startsWith("xn--") || name.startsWith("sthree-")) return "it starts with a prefix S3 reserves ('xn--', 'sthree-')";
193
+ if (name.endsWith("-s3alias") || name.endsWith("--ol-s3")) return "it ends with a suffix S3 reserves ('-s3alias', '--ol-s3')";
194
+ }
195
+ /**
196
+ * The good name: `<namespace>-<project>-<stage>-<bucket id>`, lowercased, no
197
+ * dots, shortened to leave room for a clash's suffix.
198
+ *
199
+ * @throws {InvalidBucketName} when nothing usable is left
200
+ */
201
+ function bucketName(input) {
202
+ const name = fitParts([
203
+ input.scope,
204
+ input.stage,
205
+ input.id
206
+ ], 56);
207
+ const problem = bucketNameProblem(name);
208
+ if (problem) throw new InvalidBucketName(name, problem);
209
+ return name;
210
+ }
211
+ /** Six random `[a-z0-9]` characters. */
212
+ function randomSuffix(random = node_crypto.randomInt) {
213
+ let suffix = "";
214
+ for (let i = 0; i < 6; i++) suffix += SUFFIX_ALPHABET[random(36)];
215
+ return suffix;
216
+ }
217
+ /** The good name with a clash's suffix. */
218
+ function suffixedBucketName(good, suffix) {
219
+ return `${good}-${suffix}`;
220
+ }
221
+ /**
222
+ * The IAM user a bucket is reached with on a stage:
223
+ * `gkm-<project>-<stage>-<bucket id>`, at most 64 characters.
224
+ */
225
+ function iamUserName(input) {
226
+ return fitParts([
227
+ "gkm",
228
+ input.scope,
229
+ input.stage,
230
+ input.id
231
+ ], 64);
232
+ }
233
+ /** The path every user gkm creates is under, so it finds its own. */
234
+ const IAM_USER_PATH = "/gkm/";
235
+ /** The inline policy each user is given. */
236
+ const IAM_POLICY_NAME = "gkm-bucket";
237
+ //#endregion
238
+ //#region src/providers/s3/origins.ts
239
+ /**
240
+ * Which browser origins may call a bucket: the stage's sites that depend on
241
+ * an API using it.
242
+ *
243
+ * The same reading of the graph an API's own CORS origins are — its callers —
244
+ * taken one step further: a site that calls an API which hands it a presigned
245
+ * upload URL is a site whose browser PUTs to the bucket. A site that reads the
246
+ * bucket (or a file server over it) itself is one too. Nothing is written
247
+ * down: add a site that calls the API and the next provision run allows it.
248
+ */
249
+ /** The keys a reader of this bucket holds: its URL, and each server's over it. */
250
+ function bucketKeys(manifest, bucket) {
251
+ return [(0, _geekmidas_manifest.provideKey)(bucket, "url"), ...Object.entries(manifest).filter(([, d]) => d.kind === "file-server" && d.of === bucket).map(([id]) => (0, _geekmidas_manifest.provideKey)(id, "url"))];
252
+ }
253
+ function bucketOrigins(input) {
254
+ const { manifest, workspace, stage } = input;
255
+ const keys = bucketKeys(manifest, input.bucket);
256
+ const readers = Object.entries(manifest).filter(([, d]) => d.kind === "rest-api" || d.kind === "site").filter(([id]) => {
257
+ const env = require_seeds.appEnvKeys(manifest, require_derive.appKey(id), input.runnables);
258
+ return env !== void 0 && keys.some((key) => env.has(key));
259
+ });
260
+ const sites = /* @__PURE__ */ new Set();
261
+ for (const [id, declaration] of readers) {
262
+ if (declaration.kind === "site") {
263
+ sites.add(id);
264
+ continue;
265
+ }
266
+ for (const caller of (0, _geekmidas_manifest.dependentsOf)(manifest, id)) if (manifest[caller]?.kind === "site") sites.add(caller);
267
+ }
268
+ const origins = /* @__PURE__ */ new Set();
269
+ const unresolved = [];
270
+ for (const id of [...sites].sort()) {
271
+ const name = require_derive.appKey(id);
272
+ const app = workspace.apps[name];
273
+ if (!app) {
274
+ unresolved.push(name);
275
+ continue;
276
+ }
277
+ try {
278
+ const host = require_seeds.resolveHost(name, app, stage, workspace.domains, require_seeds.isMainFrontendApp(name, app, workspace.apps));
279
+ origins.add(`https://${host}`);
280
+ } catch (error) {
281
+ if (!(error instanceof require_seeds.NoDomainForStage)) throw error;
282
+ unresolved.push(name);
283
+ }
284
+ }
285
+ return {
286
+ origins: [...origins].sort(),
287
+ unresolved
288
+ };
289
+ }
290
+ //#endregion
291
+ //#region src/providers/s3/policy.ts
292
+ const bucketArn = (bucket) => `arn:aws:s3:::${bucket}`;
293
+ /**
294
+ * What the app's key may do: objects in this bucket, and listing it — and
295
+ * nothing else in the account.
296
+ *
297
+ * Multipart uploads are in it because a presigned upload of a large file is
298
+ * one, and an abandoned one is cleaned up by the app. Reading old versions is
299
+ * added only when the bucket keeps them.
300
+ */
301
+ function userPolicy(bucket, options = {}) {
302
+ return {
303
+ Version: "2012-10-17",
304
+ Statement: [{
305
+ Sid: "GkmObjects",
306
+ Effect: "Allow",
307
+ Action: [
308
+ "s3:GetObject",
309
+ "s3:PutObject",
310
+ "s3:DeleteObject",
311
+ "s3:AbortMultipartUpload",
312
+ "s3:ListMultipartUploadParts",
313
+ ...options.versioning ? ["s3:GetObjectVersion"] : []
314
+ ],
315
+ Resource: `${bucketArn(bucket)}/*`
316
+ }, {
317
+ Sid: "GkmBucket",
318
+ Effect: "Allow",
319
+ Action: [
320
+ "s3:ListBucket",
321
+ "s3:GetBucketLocation",
322
+ "s3:ListBucketMultipartUploads"
323
+ ],
324
+ Resource: bucketArn(bucket)
325
+ }]
326
+ };
327
+ }
328
+ /**
329
+ * One open pattern as the resource it is public on. A `**` is S3's `*`; a
330
+ * single `*` is too, which crosses `/` where the construct's own check does
331
+ * not — the construct is the stricter of the two (see `bucketPolicy` in
332
+ * reconcile, which the local MinIO is given the same way).
333
+ */
334
+ function openResource(bucket, pattern) {
335
+ const path = pattern.replace(/^\/+/, "").replace(/\*\*/g, "*");
336
+ return `${bucketArn(bucket)}/${path}`;
337
+ }
338
+ /**
339
+ * The bucket's own policy: every request refused unless it came over TLS,
340
+ * and — only when a file server opens paths on it — anonymous `s3:GetObject`
341
+ * on exactly those prefixes.
342
+ */
343
+ function bucketPolicy(bucket, open = []) {
344
+ const statements = [{
345
+ Sid: "GkmDenyInsecureTransport",
346
+ Effect: "Deny",
347
+ Principal: "*",
348
+ Action: "s3:*",
349
+ Resource: [bucketArn(bucket), `${bucketArn(bucket)}/*`],
350
+ Condition: { Bool: { "aws:SecureTransport": "false" } }
351
+ }];
352
+ if (open.length > 0) statements.push({
353
+ Sid: "GkmOpenPaths",
354
+ Effect: "Allow",
355
+ Principal: "*",
356
+ Action: "s3:GetObject",
357
+ Resource: [...new Set(open.map((p) => openResource(bucket, p)))].sort()
358
+ });
359
+ return {
360
+ Version: "2012-10-17",
361
+ Statement: statements
362
+ };
363
+ }
364
+ /**
365
+ * Block Public Access. All four flags on, unless the bucket has open paths:
366
+ * then a *policy* may make it public (`BlockPublicPolicy`,
367
+ * `RestrictPublicBuckets` off), and ACLs still may not.
368
+ */
369
+ function publicAccessBlock(open) {
370
+ return {
371
+ BlockPublicAcls: true,
372
+ IgnorePublicAcls: true,
373
+ BlockPublicPolicy: !open,
374
+ RestrictPublicBuckets: !open
375
+ };
376
+ }
377
+ function corsRule(origins) {
378
+ return {
379
+ AllowedMethods: [
380
+ "GET",
381
+ "HEAD",
382
+ "POST",
383
+ "PUT"
384
+ ],
385
+ AllowedOrigins: [...new Set(origins)].sort(),
386
+ AllowedHeaders: ["*"],
387
+ ExposeHeaders: ["ETag"],
388
+ MaxAgeSeconds: 3e3
389
+ };
390
+ }
391
+ /**
392
+ * A policy document in one canonical form — keys sorted, a lone string a
393
+ * one-element list, lists sorted — so one AWS reformatted reads as the same
394
+ * document rather than as drift.
395
+ */
396
+ function canonicalPolicy(value) {
397
+ return JSON.stringify(canonical(value));
398
+ }
399
+ function canonical(value, key) {
400
+ if (Array.isArray(value)) {
401
+ const items = value.map((v) => canonical(v));
402
+ return key === "Statement" ? items : items.map((v) => JSON.stringify(v)).sort();
403
+ }
404
+ if (value && typeof value === "object") return Object.fromEntries(Object.keys(value).sort().map((k) => {
405
+ const v = value[k];
406
+ return [k, canonical(k === "Action" || k === "Resource" || k === "AWS" ? Array.isArray(v) ? v : [v] : v, k)];
407
+ }));
408
+ return value;
409
+ }
410
+ //#endregion
411
+ //#region src/providers/s3/index.ts
412
+ /** The tags gkm finds its own buckets and users by. */
413
+ const TAG_PROJECT = "gkm:project";
414
+ const TAG_STAGE = "gkm:stage";
415
+ const TAG_CONSTRUCT = "gkm:construct";
416
+ function bucketConstructs(manifest) {
417
+ return Object.entries(manifest).filter(([, d]) => d.kind === "objects").map(([id, d]) => ({
418
+ id,
419
+ versioned: d.versioned === true,
420
+ servers: Object.entries(manifest).filter(([, s]) => s.kind === "file-server" && s.of === id).map(([sid, s]) => ({
421
+ id: sid,
422
+ open: s.open ?? []
423
+ })).sort((a, b) => a.id.localeCompare(b.id))
424
+ })).sort((a, b) => a.id.localeCompare(b.id));
425
+ }
426
+ /**
427
+ * The region the stage's buckets are created in: the entry's, the secrets
428
+ * store's when it is in AWS, the credential's, `AWS_REGION`.
429
+ */
430
+ function s3Region(ctx, env = process.env) {
431
+ const store = ctx.workspace.secrets?.store;
432
+ const storeRegion = store && typeof store === "object" && "region" in store ? store.region : void 0;
433
+ const region = ctx.config.region ?? storeRegion ?? ctx.credential?.region ?? env.AWS_REGION ?? env.AWS_DEFAULT_REGION;
434
+ if (!region) throw new require_workspace.S3RegionRequired(ctx.stage);
435
+ return region;
436
+ }
437
+ /**
438
+ * The address a file server over `bucket` serves on: the bucket's regional
439
+ * virtual-host endpoint, which is what `FileServer.url(key)` appends a key to
440
+ * — `https://<bucket>.s3.<region>.amazonaws.com/<key>`. Against an emulator,
441
+ * its path-style address.
442
+ */
443
+ function bucketPublicUrl(bucket, region, endpoint) {
444
+ if (endpoint) return `${endpoint.replace(/\/+$/, "")}/${bucket}`;
445
+ return `https://${bucket}.s3.${region}.amazonaws.com`;
446
+ }
447
+ /** What an AWS SDK error is called, whichever field carries it. */
448
+ function errorName(error) {
449
+ const e = error;
450
+ return e?.Code ?? e?.name ?? "";
451
+ }
452
+ /** The answer an SDK call failed with. */
453
+ function s3Answer(error) {
454
+ const e = error;
455
+ const headers = Object.fromEntries(Object.entries(e?.$response?.headers ?? {}).map(([k, v]) => [k.toLowerCase(), v]));
456
+ const status = e?.$metadata?.httpStatusCode ?? e?.$response?.statusCode;
457
+ const region = headers["x-amz-bucket-region"];
458
+ const requestId = e?.$metadata?.requestId ?? headers["x-amz-request-id"];
459
+ const id2 = e?.$metadata?.extendedRequestId ?? headers["x-amz-id-2"];
460
+ return {
461
+ ...status !== void 0 ? { status } : {},
462
+ name: errorName(error) || "Error",
463
+ message: e?.message ?? String(error),
464
+ ...region ? { region } : {},
465
+ ...requestId ? { requestId } : {},
466
+ ...id2 ? { id2 } : {}
467
+ };
468
+ }
469
+ /**
470
+ * `HTTP 403 Unknown: UnknownError [x-amz-bucket-region: eu-west-1,
471
+ * x-amz-request-id: …, x-amz-id-2: …]` — everything S3 said, for a message.
472
+ */
473
+ function describeAnswer(answer) {
474
+ const said = answer.message && answer.message !== answer.name ? `${answer.name}: ${answer.message}` : answer.name;
475
+ const headers = [
476
+ answer.region && `x-amz-bucket-region: ${answer.region}`,
477
+ answer.requestId && `x-amz-request-id: ${answer.requestId}`,
478
+ answer.id2 && `x-amz-id-2: ${answer.id2}`
479
+ ].filter(Boolean);
480
+ return `${answer.status === void 0 ? "no response" : `HTTP ${answer.status}`} ${said}` + (headers.length > 0 ? ` [${headers.join(", ")}]` : "");
481
+ }
482
+ /** `HeadBucket`: the bucket's region when it answers, else what S3 said. */
483
+ async function headBucket(S3, client, bucket) {
484
+ try {
485
+ const out = await client.send(new S3.HeadBucketCommand({ Bucket: bucket }));
486
+ return {
487
+ ok: true,
488
+ ...out.BucketRegion ? { region: out.BucketRegion } : {}
489
+ };
490
+ } catch (error) {
491
+ return {
492
+ ok: false,
493
+ ...s3Answer(error)
494
+ };
495
+ }
496
+ }
497
+ /** `GetBucketLocation`'s answer as a region: none is us-east-1, `EU` eu-west-1. */
498
+ async function bucketLocation(S3, client, bucket) {
499
+ try {
500
+ const location = (await client.send(new S3.GetBucketLocationCommand({ Bucket: bucket }))).LocationConstraint;
501
+ return { region: !location ? "us-east-1" : location === "EU" ? "eu-west-1" : location };
502
+ } catch (error) {
503
+ return { failed: s3Answer(error) };
504
+ }
505
+ }
506
+ /**
507
+ * Where `bucket` is: `HeadBucket`'s `x-amz-bucket-region` — on a 200, a 301
508
+ * or a 400 alike — else `GetBucketLocation`.
509
+ *
510
+ * @throws {BucketRegionUnknown}
511
+ */
512
+ async function bucketRegion(clients, bucket) {
513
+ const { S3, s3 } = clients;
514
+ const head = await headBucket(S3, s3, bucket);
515
+ if (head.region) return head.region;
516
+ const location = await bucketLocation(S3, s3, bucket);
517
+ if ("region" in location) return location.region;
518
+ throw new require_workspace.BucketRegionUnknown(bucket, head.ok ? "HTTP 200 without x-amz-bucket-region" : describeAnswer(head), describeAnswer(location.failed));
519
+ }
520
+ /** A read whose "there is none" answer is one of `missing`. */
521
+ async function readOr(read, missing) {
522
+ try {
523
+ return await read();
524
+ } catch (error) {
525
+ if (missing.includes(errorName(error))) return void 0;
526
+ throw error;
527
+ }
528
+ }
529
+ const defaultClients = async (credential, region) => {
530
+ const S3 = await import("@aws-sdk/client-s3");
531
+ const IAM = await import("@aws-sdk/client-iam");
532
+ const config = await awsClientConfig(credential, region);
533
+ const endpoint = awsEndpoint(process.env, "S3");
534
+ return {
535
+ S3,
536
+ IAM,
537
+ s3: new S3.S3Client({
538
+ ...config,
539
+ ...endpoint ? {
540
+ endpoint,
541
+ forcePathStyle: true
542
+ } : {}
543
+ }),
544
+ iam: new IAM.IAMClient(config),
545
+ region,
546
+ ...endpoint ? { endpoint } : {}
547
+ };
548
+ };
549
+ /** For tests: what makes the clients. */
550
+ let makeClients = defaultClients;
551
+ async function ensureS3(ctx) {
552
+ const region = s3Region(ctx);
553
+ const clients = await makeClients(ctx.credential, region);
554
+ for (const construct of bucketConstructs(ctx.manifest)) await ensureBucket(ctx, clients, construct);
555
+ }
556
+ /** The keys gkm tags a bucket and a user with. */
557
+ function identityTags(ctx, id) {
558
+ return [
559
+ {
560
+ Key: TAG_PROJECT,
561
+ Value: ctx.identity.key
562
+ },
563
+ {
564
+ Key: TAG_STAGE,
565
+ Value: ctx.stage
566
+ },
567
+ {
568
+ Key: TAG_CONSTRUCT,
569
+ Value: id
570
+ }
571
+ ];
572
+ }
573
+ async function ensureBucket(ctx, stageClients, construct) {
574
+ const { id } = construct;
575
+ const nameInput = {
576
+ scope: ctx.identity.scope,
577
+ stage: ctx.stage,
578
+ id: require_derive.appKey(id)
579
+ };
580
+ const versioning = ctx.config.versioning ?? construct.versioned;
581
+ const open = [...new Set(construct.servers.flatMap((s) => s.open))].sort();
582
+ const located = await ensureBucketName(ctx, stageClients, id, bucketName(nameInput));
583
+ const { bucket, exists } = located;
584
+ let clients = stageClients;
585
+ if (located.region !== stageClients.region) {
586
+ clients = await makeClients(ctx.credential, located.region);
587
+ ctx.log(` '${bucket}' is in ${located.region}, not ${stageClients.region}: '${id}' is reached and addressed there`);
588
+ }
589
+ await ensureBucketSettings(ctx, clients, {
590
+ id,
591
+ bucket,
592
+ exists,
593
+ versioning,
594
+ open
595
+ });
596
+ const user = await ensureUser(ctx, clients, id, iamUserName(nameInput), userPolicy(bucket, { versioning }));
597
+ await ensureAccessKey(ctx, clients, {
598
+ id,
599
+ bucket,
600
+ ...user
601
+ });
602
+ const publicUrl = bucketPublicUrl(bucket, clients.region, clients.endpoint);
603
+ for (const server of construct.servers) {
604
+ const key = (0, _geekmidas_manifest.provideKey)(server.id, "url");
605
+ const entry = {
606
+ key: `file-server:${server.id}`,
607
+ type: "url"
608
+ };
609
+ const recorded = ctx.state.record(entry.key)?.id;
610
+ const current = ctx.secrets[key];
611
+ if (current === publicUrl) {
612
+ await ctx.state.ready(entry, publicUrl);
613
+ continue;
614
+ }
615
+ if (current !== void 0 && current !== recorded) {
616
+ ctx.log(` ${key} is set to ${current} by you, and kept — the bucket's own address is ${publicUrl}`);
617
+ continue;
618
+ }
619
+ await ctx.change({
620
+ construct: server.id,
621
+ resource: key,
622
+ change: `set to ${publicUrl}`
623
+ }, async () => {
624
+ await ctx.writeSecrets({ [key]: publicUrl });
625
+ await ctx.state.ready(entry, publicUrl);
626
+ });
627
+ }
628
+ }
629
+ /**
630
+ * The bucket's name: the recorded one, else the good name, else the good
631
+ * name with a random suffix — up to {@link SUFFIX_ATTEMPTS} times.
632
+ */
633
+ async function ensureBucketName(ctx, clients, id, good) {
634
+ const { S3, s3, region } = clients;
635
+ const key = `s3-bucket:${id}`;
636
+ const record = ctx.state.record(key);
637
+ const entry = (name, where) => ({
638
+ key,
639
+ type: "s3-bucket",
640
+ data: {
641
+ name,
642
+ region: where
643
+ }
644
+ });
645
+ /**
646
+ * Whether this account holds `name`: yes, no, or someone else does. Read
647
+ * by status — a `HEAD` has no body, so its error is named `Unknown` — and,
648
+ * when S3 says the bucket is in another region, asked again there.
649
+ */
650
+ const probe = async (name) => {
651
+ let head = await headBucket(S3, s3, name);
652
+ if (!head.ok && head.region && head.region !== region && (head.status === 301 || head.status === 400)) {
653
+ const there = await makeClients(ctx.credential, head.region);
654
+ head = await headBucket(there.S3, there.s3, name);
655
+ }
656
+ if (head.ok) return "ours";
657
+ if (head.status === 404) return "none";
658
+ if (head.status === 403) return "taken";
659
+ throw new require_workspace.BucketProbeFailed(name, describeAnswer(head));
660
+ };
661
+ /** Where the bucket is now — read, not assumed: it may be adopted. */
662
+ const located = async (name, made) => ctx.dryRun && made === "created" ? region : bucketRegion(clients, name);
663
+ const create = async (name) => {
664
+ if (ctx.dryRun) {
665
+ const found = await probe(name);
666
+ if (found === "none") {
667
+ await ctx.change({
668
+ construct: id,
669
+ resource: `bucket ${name}`,
670
+ change: `create in ${region}`
671
+ }, async () => {});
672
+ return "created";
673
+ }
674
+ return found;
675
+ }
676
+ await ctx.state.pending(entry(name, region));
677
+ try {
678
+ await s3.send(new S3.CreateBucketCommand({
679
+ Bucket: name,
680
+ ...region === "us-east-1" ? {} : { CreateBucketConfiguration: { LocationConstraint: region } }
681
+ }));
682
+ } catch (error) {
683
+ const code = errorName(error);
684
+ if (code === "BucketAlreadyOwnedByYou") return "ours";
685
+ if (code === "BucketAlreadyExists") return "taken";
686
+ throw error;
687
+ }
688
+ await ctx.change({
689
+ construct: id,
690
+ resource: `bucket ${name}`,
691
+ change: `create in ${region}`
692
+ }, async () => {});
693
+ return "created";
694
+ };
695
+ const adopt = async (name, where) => {
696
+ const there = where === region ? clients : await makeClients(ctx.credential, where);
697
+ const owner = (await readOr(() => there.s3.send(new there.S3.GetBucketTaggingCommand({ Bucket: name })), ["NoSuchTagSet", "NoSuchTagSetError"]))?.TagSet?.find((t) => t.Key === TAG_PROJECT)?.Value;
698
+ if (owner && owner !== ctx.identity.key) throw new require_workspace.BucketOwnedByAnotherProject(name, owner, ctx.identity.key);
699
+ };
700
+ if (record?.status === "ready" && record.id) {
701
+ const found = await probe(record.id);
702
+ if (found === "taken") throw new require_workspace.RecordedBucketUnreachable(id, record.id);
703
+ const made = found === "ours" ? "ours" : await create(record.id);
704
+ if (made === "taken") throw new require_workspace.RecordedBucketUnreachable(id, record.id);
705
+ const where = await located(record.id, made);
706
+ if (!ctx.dryRun) await ctx.state.ready(entry(record.id, where), record.id);
707
+ return {
708
+ bucket: record.id,
709
+ exists: made === "ours",
710
+ region: where
711
+ };
712
+ }
713
+ const pending = record?.data?.name;
714
+ const names = [
715
+ ...typeof pending === "string" && pending !== good ? [pending] : [],
716
+ good,
717
+ ...Array.from({ length: 5 }, () => suffixedBucketName(good, randomSuffix()))
718
+ ];
719
+ const tried = [];
720
+ for (const name of names) {
721
+ tried.push(name);
722
+ const made = await create(name);
723
+ if (made === "taken") continue;
724
+ const where = await located(name, made);
725
+ if (made === "ours") await adopt(name, where);
726
+ if (!ctx.dryRun) await ctx.state.ready(entry(name, where), name);
727
+ if (name !== good) ctx.log(` '${good}' is taken by another account; '${id}' is '${name}', recorded in the stage's state`);
728
+ return {
729
+ bucket: name,
730
+ exists: made === "ours",
731
+ region: where
732
+ };
733
+ }
734
+ throw new require_workspace.BucketNameUnavailable(id, tried.filter((n) => n !== pending));
735
+ }
736
+ /** Tags, Block Public Access, encryption, policy, versioning and CORS. */
737
+ async function ensureBucketSettings(ctx, clients, input) {
738
+ const { S3, s3 } = clients;
739
+ const { id, bucket } = input;
740
+ const Bucket = bucket;
741
+ const fresh = ctx.dryRun && !input.exists;
742
+ const read = async (fn, missing) => fresh ? void 0 : readOr(fn, missing);
743
+ const set = (change, apply) => ctx.change({
744
+ construct: id,
745
+ resource: `bucket ${bucket}`,
746
+ change
747
+ }, () => apply().then(() => void 0));
748
+ const tagged = await read(() => s3.send(new S3.GetBucketTaggingCommand({ Bucket })), ["NoSuchTagSet", "NoSuchTagSetError"]);
749
+ const ours = identityTags(ctx, id);
750
+ const others = (tagged?.TagSet ?? []).filter((t) => !ours.some((o) => o.Key === t.Key));
751
+ if (ours.some((o) => !tagged?.TagSet?.some((t) => t.Key === o.Key && t.Value === o.Value))) await set("tag", () => s3.send(new S3.PutBucketTaggingCommand({
752
+ Bucket,
753
+ Tagging: { TagSet: [...others, ...ours].map((t) => ({
754
+ Key: t.Key,
755
+ Value: t.Value
756
+ })) }
757
+ })));
758
+ const isOpen = input.open.length > 0;
759
+ const block = publicAccessBlock(isOpen);
760
+ const ensureBlock = async () => {
761
+ const now = (await read(() => s3.send(new S3.GetPublicAccessBlockCommand({ Bucket })), ["NoSuchPublicAccessBlockConfiguration"]))?.PublicAccessBlockConfiguration;
762
+ if (!(now && Object.keys(block).every((k) => (now[k] ?? false) === block[k]))) await set(isOpen ? "block public access (ACLs; the policy opens the open paths)" : "block all public access", () => s3.send(new S3.PutPublicAccessBlockCommand({
763
+ Bucket,
764
+ PublicAccessBlockConfiguration: block
765
+ })));
766
+ };
767
+ const policy = bucketPolicy(bucket, input.open);
768
+ const ensurePolicy = async () => {
769
+ const current = await read(() => s3.send(new S3.GetBucketPolicyCommand({ Bucket })), ["NoSuchBucketPolicy"]);
770
+ if (!(current?.Policy !== void 0 && canonicalPolicy(JSON.parse(current.Policy)) === canonicalPolicy(policy))) await set(isOpen ? `policy: TLS only, public read on ${input.open.join(", ")}` : "policy: TLS only", () => s3.send(new S3.PutBucketPolicyCommand({
771
+ Bucket,
772
+ Policy: JSON.stringify(policy)
773
+ })));
774
+ };
775
+ if (isOpen) {
776
+ await ensureBlock();
777
+ await ensurePolicy();
778
+ } else {
779
+ await ensurePolicy();
780
+ await ensureBlock();
781
+ }
782
+ if ((await read(() => s3.send(new S3.GetBucketEncryptionCommand({ Bucket })), ["ServerSideEncryptionConfigurationNotFoundError"]))?.ServerSideEncryptionConfiguration?.Rules?.[0]?.ApplyServerSideEncryptionByDefault?.SSEAlgorithm !== "AES256" && !fresh) await set("encrypt by default (SSE-S3)", () => s3.send(new S3.PutBucketEncryptionCommand({
783
+ Bucket,
784
+ ServerSideEncryptionConfiguration: { Rules: [{ ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "AES256" } }] }
785
+ })));
786
+ const status = (await read(() => s3.send(new S3.GetBucketVersioningCommand({ Bucket })), []))?.Status;
787
+ if (input.versioning && status !== "Enabled") await set("enable versioning", () => s3.send(new S3.PutBucketVersioningCommand({
788
+ Bucket,
789
+ VersioningConfiguration: { Status: "Enabled" }
790
+ })));
791
+ else if (!input.versioning && status === "Enabled") await set("suspend versioning", () => s3.send(new S3.PutBucketVersioningCommand({
792
+ Bucket,
793
+ VersioningConfiguration: { Status: "Suspended" }
794
+ })));
795
+ const { origins, unresolved } = bucketOrigins({
796
+ workspace: ctx.workspace,
797
+ manifest: ctx.manifest,
798
+ runnables: ctx.runnables,
799
+ stage: ctx.stage,
800
+ bucket: id
801
+ });
802
+ if (unresolved.length > 0) ctx.log(` ${unresolved.join(", ")} ${unresolved.length === 1 ? "has" : "have"} no address on '${ctx.stage}' (domains.${ctx.stage}), so ${unresolved.length === 1 ? "is" : "are"} not in '${bucket}''s CORS`);
803
+ if (origins.length === 0) return;
804
+ const rule = corsRule(origins);
805
+ const cors = await read(() => s3.send(new S3.GetBucketCorsCommand({ Bucket })), ["NoSuchCORSConfiguration"]);
806
+ if (!(cors?.CORSRules?.length === 1 && canonicalCors(cors.CORSRules[0]) === canonicalCors(rule))) await set(`CORS for ${origins.join(", ")}`, () => s3.send(new S3.PutBucketCorsCommand({
807
+ Bucket,
808
+ CORSConfiguration: { CORSRules: [rule] }
809
+ })));
810
+ }
811
+ function canonicalCors(rule) {
812
+ const sorted = (list) => [...list ?? []].sort();
813
+ return JSON.stringify({
814
+ AllowedMethods: sorted(rule.AllowedMethods),
815
+ AllowedOrigins: sorted(rule.AllowedOrigins),
816
+ AllowedHeaders: sorted(rule.AllowedHeaders),
817
+ ExposeHeaders: sorted(rule.ExposeHeaders),
818
+ MaxAgeSeconds: rule.MaxAgeSeconds ?? null
819
+ });
820
+ }
821
+ /** The bucket's IAM user and its inline policy. */
822
+ async function ensureUser(ctx, clients, id, user, policy) {
823
+ const { IAM, iam } = clients;
824
+ const entry = {
825
+ key: `iam-user:${id}`,
826
+ type: "iam-user"
827
+ };
828
+ const found = await readOr(() => iam.send(new IAM.GetUserCommand({ UserName: user })), ["NoSuchEntity", "NoSuchEntityException"]);
829
+ if (found?.User) {
830
+ const tags = await iam.send(new IAM.ListUserTagsCommand({ UserName: user }));
831
+ const tag = (key) => tags.Tags?.find((t) => t.Key === key)?.Value;
832
+ if (found.User.Path !== "/gkm/" || tag("gkm:project") !== ctx.identity.key || tag("gkm:stage") !== ctx.stage) throw new require_workspace.IamUserNotOwned(user, ctx.identity.key, ctx.stage);
833
+ await ctx.state.ready(entry, user);
834
+ } else await ctx.change({
835
+ construct: id,
836
+ resource: `IAM user ${user}`,
837
+ change: `create under ${IAM_USER_PATH}`
838
+ }, async () => {
839
+ await ctx.state.pending(entry);
840
+ await iam.send(new IAM.CreateUserCommand({
841
+ UserName: user,
842
+ Path: IAM_USER_PATH,
843
+ Tags: identityTags(ctx, id)
844
+ }));
845
+ await ctx.state.ready(entry, user);
846
+ });
847
+ const exists = Boolean(found?.User) || !ctx.dryRun;
848
+ const current = exists ? await readOr(() => iam.send(new IAM.GetUserPolicyCommand({
849
+ UserName: user,
850
+ PolicyName: IAM_POLICY_NAME
851
+ })), ["NoSuchEntity", "NoSuchEntityException"]) : void 0;
852
+ const document = current?.PolicyDocument ? JSON.parse(decodePolicy(current.PolicyDocument)) : void 0;
853
+ if (!document || canonicalPolicy(document) !== canonicalPolicy(policy)) await ctx.change({
854
+ construct: id,
855
+ resource: `IAM user ${user}`,
856
+ change: `policy ${IAM_POLICY_NAME}: this bucket only`
857
+ }, async () => {
858
+ await iam.send(new IAM.PutUserPolicyCommand({
859
+ UserName: user,
860
+ PolicyName: IAM_POLICY_NAME,
861
+ PolicyDocument: JSON.stringify(policy)
862
+ }));
863
+ });
864
+ return {
865
+ user,
866
+ exists
867
+ };
868
+ }
869
+ /** IAM returns a policy document URL-encoded; an emulator may not. */
870
+ function decodePolicy(document) {
871
+ try {
872
+ return decodeURIComponent(document);
873
+ } catch {
874
+ return document;
875
+ }
876
+ }
877
+ /**
878
+ * The user's access key, in the stage's secrets as the bucket's URL.
879
+ *
880
+ * - First run: one key, written into the stage's secrets.
881
+ * - `--rotate-keys`: a second key, written in place of the first; the first
882
+ * stays active until the stage has deployed with the second, and the next
883
+ * run after that deploy — or `--retire-old-keys` — deletes it.
884
+ * - A key the secrets no longer hold (a store emptied by hand): replaced the
885
+ * same way, since its secret cannot be read back.
886
+ */
887
+ async function ensureAccessKey(ctx, clients, input) {
888
+ const { IAM, iam } = clients;
889
+ const { id, bucket, user } = input;
890
+ const urlKey = (0, _geekmidas_manifest.provideKey)(id, "url");
891
+ const entryKey = `iam-access-key:${id}`;
892
+ const record = ctx.state.record(entryKey);
893
+ const given = ctx.secrets[urlKey];
894
+ let inUrl;
895
+ let parsed;
896
+ if (given !== void 0) {
897
+ try {
898
+ parsed = _geekmidas_storage_s3_url.parse(given);
899
+ } catch {
900
+ parsed = void 0;
901
+ }
902
+ if (parsed?.bucket !== bucket) throw new require_workspace.BucketKeySetElsewhere(urlKey, bucket, parsed?.bucket ?? given.replace(/\/\/[^@]*@/, "//"), ctx.stage);
903
+ inUrl = parsed.accessKeyId;
904
+ }
905
+ const listed = input.exists ? (await iam.send(new IAM.ListAccessKeysCommand({ UserName: user }))).AccessKeyMetadata?.map((k) => k.AccessKeyId) ?? [] : [];
906
+ let current = record?.status === "ready" ? record.id : void 0;
907
+ let previous = typeof record?.data?.previous === "string" ? record.data.previous : void 0;
908
+ const rotatedAt = typeof record?.data?.rotatedAt === "string" ? record.data.rotatedAt : void 0;
909
+ const entry = (data = {}) => ({
910
+ key: entryKey,
911
+ type: "iam-access-key",
912
+ data: {
913
+ user,
914
+ ...data
915
+ }
916
+ });
917
+ if (previous && current) {
918
+ const deployed = rotatedAt !== void 0 && ctx.state.lastDeployedAt !== void 0 && ctx.state.lastDeployedAt > rotatedAt;
919
+ if (ctx.retireOldKeys || deployed) {
920
+ const old = previous;
921
+ const keep = current;
922
+ await ctx.change({
923
+ construct: id,
924
+ resource: `IAM user ${user}`,
925
+ change: `delete the rotated-out access key ${old}`
926
+ }, async () => {
927
+ if (listed.includes(old)) await iam.send(new IAM.DeleteAccessKeyCommand({
928
+ UserName: user,
929
+ AccessKeyId: old
930
+ }));
931
+ await ctx.state.ready(entry(), keep);
932
+ });
933
+ previous = void 0;
934
+ } else ctx.log(` ${id}: the old access key ${previous} stays active until '${ctx.stage}' is deployed with the new one; the next deploy after that deletes it (or pass --retire-old-keys)`);
935
+ }
936
+ if (!current && inUrl && listed.includes(inUrl)) {
937
+ await ctx.state.ready(entry(), inUrl);
938
+ current = inUrl;
939
+ }
940
+ if (current !== void 0 && listed.includes(current) && inUrl === current && !ctx.rotateKeys) {
941
+ if (parsed && parsed.region !== clients.region) {
942
+ const address = parsed;
943
+ await ctx.change({
944
+ construct: id,
945
+ resource: urlKey,
946
+ change: `name ${clients.region}, the bucket's region (it said ${address.region ?? "none"})`
947
+ }, () => ctx.writeSecrets({ [urlKey]: _geekmidas_storage_s3_url.build({
948
+ ...address,
949
+ region: clients.region
950
+ }) }));
951
+ }
952
+ return;
953
+ }
954
+ if (ctx.rotateKeys && previous) throw new require_workspace.RotationInProgress(id, previous, ctx.stage);
955
+ const outgoing = current && listed.includes(current) ? current : void 0;
956
+ if (listed.length >= 2) throw new require_workspace.AccessKeyLimit(user, listed);
957
+ const why = !current ? "create an access key" : ctx.rotateKeys ? "rotate: issue a new access key" : "issue a new access key (the secrets no longer hold the recorded one)";
958
+ await ctx.change({
959
+ construct: id,
960
+ resource: `IAM user ${user}`,
961
+ change: why
962
+ }, async () => {
963
+ await ctx.state.pending(entry(outgoing ? { previous: outgoing } : {}));
964
+ const key = (await iam.send(new IAM.CreateAccessKeyCommand({ UserName: user }))).AccessKey;
965
+ await ctx.writeSecrets({ [urlKey]: _geekmidas_storage_s3_url.build({
966
+ bucket,
967
+ region: clients.region,
968
+ ...clients.endpoint ? {
969
+ endpoint: clients.endpoint,
970
+ forcePathStyle: true
971
+ } : {},
972
+ accessKeyId: key.AccessKeyId,
973
+ secretAccessKey: key.SecretAccessKey
974
+ }) });
975
+ const issuedAt = (/* @__PURE__ */ new Date()).toISOString();
976
+ await ctx.state.ready(entry(outgoing ? {
977
+ previous: outgoing,
978
+ rotatedAt: issuedAt,
979
+ issuedAt
980
+ } : { issuedAt }), key.AccessKeyId);
981
+ });
982
+ if (outgoing) ctx.log(` ${urlKey} holds the new key. This deploy moves '${ctx.stage}' onto it; the next deploy after it deletes the old key ${outgoing}`);
983
+ }
984
+ /** How long a key issued this recently counts as new to IAM. */
985
+ const NEW_KEY_WINDOW_MS = 6e5;
986
+ /** The waits between asks while a new key becomes usable: about a minute. */
987
+ let newKeyWaits = [
988
+ 2e3,
989
+ 4e3,
990
+ 8e3,
991
+ 15e3,
992
+ 15e3,
993
+ 15e3
994
+ ];
995
+ /** When the key in the URL was issued, if the stage's state says it was just now. */
996
+ function issuedJustNow(ctx, id, accessKeyId) {
997
+ const record = ctx.resources?.[`iam-access-key:${id}`];
998
+ const issuedAt = record?.data?.issuedAt;
999
+ if (record?.id !== accessKeyId || typeof issuedAt !== "string") return;
1000
+ const age = Date.now() - Date.parse(issuedAt);
1001
+ return age >= 0 && age < NEW_KEY_WINDOW_MS ? issuedAt : void 0;
1002
+ }
1003
+ /**
1004
+ * Each provisioned bucket answers `HeadBucket` to the key in the stage's
1005
+ * secrets — the one the apps will use. A bucket whose URL carries no key is
1006
+ * not one this provider wrote, and the deploy's own check covers a URL that
1007
+ * is missing.
1008
+ *
1009
+ * A failure is read by its status, never its name — a `HEAD` has no body,
1010
+ * so SDK v3 calls a 301, 400 or 403 `Unknown` — and the message carries
1011
+ * everything S3 said: status, error, region, request ids.
1012
+ *
1013
+ * @throws {BucketRegionMismatch} the bucket is in another region than its URL says
1014
+ * @throws {ProvisionedBucketUnreachable}
1015
+ */
1016
+ async function verifyS3(ctx) {
1017
+ const S3 = await import("@aws-sdk/client-s3");
1018
+ for (const { id } of bucketConstructs(ctx.manifest)) {
1019
+ const urlKey = (0, _geekmidas_manifest.provideKey)(id, "url");
1020
+ const url = ctx.secrets[urlKey];
1021
+ if (url === void 0) continue;
1022
+ const address = _geekmidas_storage_s3_url.parse(url);
1023
+ if (!address.accessKeyId || !address.secretAccessKey) continue;
1024
+ const asked = address.region ?? ctx.config.region ?? "us-east-1";
1025
+ const client = new S3.S3Client({
1026
+ region: asked,
1027
+ credentials: {
1028
+ accessKeyId: address.accessKeyId,
1029
+ secretAccessKey: address.secretAccessKey
1030
+ },
1031
+ ...address.endpoint ? { endpoint: address.endpoint } : {},
1032
+ ...address.forcePathStyle ? { forcePathStyle: true } : {}
1033
+ });
1034
+ try {
1035
+ let head = await headBucket(S3, client, address.bucket);
1036
+ const issuedAt = issuedJustNow(ctx, id, address.accessKeyId);
1037
+ let waited = 0;
1038
+ if (!head.ok && head.status === 403 && issuedAt) {
1039
+ ctx.log?.(` ${id}: waiting for the new key ${address.accessKeyId} to become active…`);
1040
+ for (const wait of newKeyWaits) {
1041
+ await new Promise((resolve) => setTimeout(resolve, wait));
1042
+ waited += wait;
1043
+ head = await headBucket(S3, client, address.bucket);
1044
+ if (head.ok || head.status !== 403) break;
1045
+ }
1046
+ }
1047
+ if (head.ok) continue;
1048
+ const fail = (reason) => new require_workspace.ProvisionedBucketUnreachable(id, address.bucket, ctx.stage, reason);
1049
+ const said = describeAnswer(head);
1050
+ if (head.status === 301 || head.status === 400 && head.region && head.region !== asked) {
1051
+ let where = head.region;
1052
+ let located = "";
1053
+ if (!where) {
1054
+ const location = await bucketLocation(S3, client, address.bucket);
1055
+ if ("region" in location) where = location.region;
1056
+ else located = `; GetBucketLocation answered ${describeAnswer(location.failed)}`;
1057
+ }
1058
+ if (where && where !== asked) throw new require_workspace.BucketRegionMismatch(id, address.bucket, ctx.stage, urlKey, where, address.region, asked, said);
1059
+ throw fail(`S3 redirected the request (${said}${located})`);
1060
+ }
1061
+ if (head.status === 404) throw fail(`it does not exist (${said})`);
1062
+ if (head.status === 403) throw fail(`the key is refused — AccessDenied, or an invalid or not-yet-active key (${said})` + (issuedAt ? `. The key was issued at ${issuedAt} by this deploy, and IAM keys take seconds to become usable: it was still refused after waiting ${Math.round(waited / 1e3)}s` : ""));
1063
+ throw fail(said);
1064
+ } finally {
1065
+ client.destroy();
1066
+ }
1067
+ }
1068
+ }
1069
+ //#endregion
1070
+ //#region src/providers/registry.ts
1071
+ /**
1072
+ * The providers gkm ships, by kind and name — registered the way a deploy
1073
+ * target's built-ins are (`target/builtins.ts`). A provider from a package is
1074
+ * one more entry here once there is a loading story for it.
1075
+ */
1076
+ const BUILTIN_PROVIDERS = { objects: { s3: {
1077
+ kind: "objects",
1078
+ name: "s3",
1079
+ check: require_workspace.checkS3Config,
1080
+ provisioning: require_workspace.S3_PROVISIONING,
1081
+ credentials: awsProvisioningCredentials,
1082
+ runtimeKeys(manifest) {
1083
+ return bucketConstructs(manifest).flatMap((b) => [(0, _geekmidas_manifest.provideKey)(b.id, "url"), ...b.servers.map((s) => (0, _geekmidas_manifest.provideKey)(s.id, "url"))]);
1084
+ },
1085
+ ensure: ensureS3,
1086
+ verify: verifyS3
1087
+ } } };
1088
+ /** The provider `kind` names `name`, if gkm has one. */
1089
+ function builtinProvider(kind, name) {
1090
+ return Object.hasOwn(BUILTIN_PROVIDERS[kind], name) ? BUILTIN_PROVIDERS[kind][name] : void 0;
1091
+ }
1092
+ //#endregion
1093
+ //#region src/providers/index.ts
1094
+ /** The constructs of a kind: `objects` is every bucket. */
1095
+ function constructsOf(manifest, kind) {
1096
+ return Object.entries(manifest).filter(([, d]) => d.kind === kind).map(([id]) => id).sort();
1097
+ }
1098
+ /**
1099
+ * What every deploy runs: each provider's `verify()` — the cheap check that
1100
+ * what it created is still there and the stage's key reaches it. Nothing on
1101
+ * the local stage, and nothing for a kind on `external` or `false` — a kind
1102
+ * the stage has none of is the deploy's readiness check's to refuse.
1103
+ */
1104
+ async function verifyStageProviders(input) {
1105
+ const verified = [];
1106
+ for (const kind of require_seeds.PROVIDER_KINDS) {
1107
+ if (constructsOf(input.manifest, kind).length === 0) continue;
1108
+ const choice = require_workspace.stageProvider(input.workspace, kind, input.stage);
1109
+ if (choice.mode !== "provider") continue;
1110
+ const provider = builtinProvider(kind, choice.name);
1111
+ await provider.verify({
1112
+ workspace: input.workspace,
1113
+ manifest: input.manifest,
1114
+ stage: input.stage,
1115
+ config: choice.config,
1116
+ secrets: input.secrets,
1117
+ ...input.resources ? { resources: input.resources } : {},
1118
+ ...input.log ? { log: input.log } : {}
1119
+ });
1120
+ verified.push(`${kind}: ${provider.name}`);
1121
+ }
1122
+ return verified;
1123
+ }
1124
+ /**
1125
+ * A stage whose provider creates its resources, and no credentials to create
1126
+ * them with. Raised before anything is created, built or started.
1127
+ */
1128
+ var ProviderCredentialsMissing = class extends require_errors.GkmError {
1129
+ stage;
1130
+ kind;
1131
+ provider;
1132
+ ids;
1133
+ env;
1134
+ describe;
1135
+ login;
1136
+ constructor(stage, kind, provider, ids, env, describe, login) {
1137
+ super(`deploy.${kind}.${stage} is ${provider}, so the deploy creates what ${ids.join(", ")} ${ids.length === 1 ? "is" : "are"} backed by on '${stage}' and writes ${ids.length === 1 ? "its key" : "their keys"} into the stage's secrets — and it found no ${provider} credentials to do it with. On this machine, supply ${describe}` + (login ? `, or run gkm login --provider ${login}` : "") + `. In CI, the step that runs the deploy (or gkm deploy --resources-only) needs them: the '${stage}' environment's role (AWS_ROLE_ARN, from gkm deploy:github), assumed before that step. With the keys set by hand instead, set deploy.${kind}.${stage} to 'external'.`);
1138
+ this.stage = stage;
1139
+ this.kind = kind;
1140
+ this.provider = provider;
1141
+ this.ids = ids;
1142
+ this.env = env;
1143
+ this.describe = describe;
1144
+ this.login = login;
1145
+ this.name = "ProviderCredentialsMissing";
1146
+ }
1147
+ };
1148
+ /**
1149
+ * `ensure()` for every provider the stage configures — find or create, fix
1150
+ * drift, record in the stage's state, write the runtime keys into its
1151
+ * secrets. Run by the deploy, which holds the stage's lock. A provider with
1152
+ * no provisioning credentials stops the run with
1153
+ * {@link ProviderCredentialsMissing} before anything is created.
1154
+ */
1155
+ async function provisionStage(options) {
1156
+ const { workspace, stage, manifest } = options;
1157
+ const dryRun = options.dryRun === true;
1158
+ const env = options.env ?? process.env;
1159
+ const log = options.log ?? ((line) => console.log(line));
1160
+ const runnables = options.runnables ?? {};
1161
+ const reports = [];
1162
+ const configured = [];
1163
+ for (const kind of require_seeds.PROVIDER_KINDS) {
1164
+ const choice = require_workspace.stageProvider(workspace, kind, stage);
1165
+ if (choice.mode !== "provider") {
1166
+ reports.push({
1167
+ kind,
1168
+ mode: choice.mode,
1169
+ actions: [],
1170
+ planned: []
1171
+ });
1172
+ continue;
1173
+ }
1174
+ const provider = builtinProvider(kind, choice.name);
1175
+ const report = {
1176
+ kind,
1177
+ mode: "provider",
1178
+ provider: provider.name,
1179
+ actions: [],
1180
+ planned: []
1181
+ };
1182
+ reports.push(report);
1183
+ const ids = constructsOf(manifest, kind);
1184
+ if (ids.length === 0) continue;
1185
+ const credential = await provider.credentials({
1186
+ stage,
1187
+ env,
1188
+ ...options.profile ? { profile: options.profile } : {},
1189
+ ...options.home ? { home: options.home } : {}
1190
+ });
1191
+ if (credential === void 0) throw new ProviderCredentialsMissing(stage, kind, provider.name, ids, provider.provisioning.env, provider.provisioning.describe, provider.provisioning.login);
1192
+ configured.push({
1193
+ kind,
1194
+ provider,
1195
+ config: choice.config,
1196
+ credential,
1197
+ report
1198
+ });
1199
+ }
1200
+ if (configured.length === 0) return reports;
1201
+ const identity = require_seeds.deployIdentity(workspace, stage);
1202
+ let secrets = (await options.secrets.read())?.custom ?? {};
1203
+ for (const entry of configured) entry.report.planned = entry.provider.runtimeKeys(manifest).filter((key) => secrets[key] === void 0);
1204
+ const state = dryRun ? await readOnlyState(options.state, stage) : journalState(await require_seeds.DeployJournal.open(options.state, stage, () => require_dns$1.deploysWithCompose(workspace) ? require_seeds.createComposeState(stage) : require_seeds.createEmptyState(stage, "", "")));
1205
+ const writeSecrets = async (values) => {
1206
+ if (dryRun) return;
1207
+ const stored = await options.secrets.read() ?? require_seeds.initStageSecrets(stage);
1208
+ const next = {
1209
+ ...stored,
1210
+ custom: {
1211
+ ...stored.custom,
1212
+ ...values
1213
+ },
1214
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1215
+ };
1216
+ await options.secrets.write(next);
1217
+ secrets = next.custom;
1218
+ };
1219
+ for (const entry of configured) {
1220
+ log(`☁️ ${entry.kind}: ${entry.provider.name}${dryRun ? " (dry run)" : ""}`);
1221
+ await entry.provider.ensure({
1222
+ workspace,
1223
+ manifest,
1224
+ runnables,
1225
+ stage,
1226
+ identity,
1227
+ config: entry.config,
1228
+ credential: entry.credential,
1229
+ get secrets() {
1230
+ return secrets;
1231
+ },
1232
+ state,
1233
+ dryRun,
1234
+ rotateKeys: options.rotateKeys === true,
1235
+ retireOldKeys: options.retireOldKeys === true,
1236
+ async change(action, apply) {
1237
+ if (!dryRun) await apply();
1238
+ entry.report.actions.push(action);
1239
+ log(` ${dryRun ? "would " : ""}${action.change} — ${action.resource}`);
1240
+ },
1241
+ writeSecrets,
1242
+ log
1243
+ });
1244
+ if (entry.report.actions.length === 0) log(" up to date");
1245
+ }
1246
+ return reports;
1247
+ }
1248
+ /** The journal as `ensure()` sees it. */
1249
+ function journalState(journal) {
1250
+ return {
1251
+ record: (key) => journal.record(key),
1252
+ pending: (entry) => journal.pending(entry),
1253
+ ready: (entry, id) => journal.ready(entry, id),
1254
+ lastDeployedAt: journal.existed ? journal.state.lastDeployedAt : void 0
1255
+ };
1256
+ }
1257
+ /** What a dry run reads, and the writes it does not make. */
1258
+ async function readOnlyState(store, stage) {
1259
+ const stored = await store.read(stage);
1260
+ return {
1261
+ record: (key) => stored?.resources[key],
1262
+ pending: async () => {},
1263
+ ready: async () => {},
1264
+ ...stored ? { lastDeployedAt: stored.state.lastDeployedAt } : {}
1265
+ };
1266
+ }
1267
+ //#endregion
1268
+ //#region src/secrets/stale.ts
1269
+ /**
1270
+ * Stored secrets an older gkm generated that now shadow a derived value.
1271
+ *
1272
+ * Before constructs, initialising a stage wrote an address for each app into
1273
+ * its custom secrets: `<APP>_DATABASE_URL` as a `postgresql://…@localhost`
1274
+ * URL with a password of its own, and `http://localhost:<port>` for a site or
1275
+ * an auth server. Those keys are now a construct's — `database.schema('AuthDatabase')`
1276
+ * provides `AUTH_DATABASE_URL`, `new BetterAuth('Auth')` provides `AUTH_URL`
1277
+ * — and a deploy derives them. A value the stage holds is a value set by hand,
1278
+ * and set by hand wins: the container is handed `localhost`, where nothing
1279
+ * answers, with a password no role has.
1280
+ *
1281
+ * So a deploy refuses such a stage rather than run it, naming each key and
1282
+ * the command that removes it. Nothing is deleted for the user: the value is
1283
+ * theirs, and only they can say it is stale. A URL that names a real host — a
1284
+ * managed database — is a deliberate choice and still wins.
1285
+ */
1286
+ /** The kinds whose URL an older gkm wrote into a stage's secrets. */
1287
+ const ADDRESSED = /* @__PURE__ */ new Set([
1288
+ "database",
1289
+ "database-reader",
1290
+ "database-schema",
1291
+ "rest-api",
1292
+ "site"
1293
+ ]);
1294
+ /** Hosts that are this machine — never anything, inside a container. */
1295
+ const LOOPBACK = /* @__PURE__ */ new Set([
1296
+ "localhost",
1297
+ "127.0.0.1",
1298
+ "[::1]",
1299
+ "::1"
1300
+ ]);
1301
+ /**
1302
+ * A stage holds a construct's address as a custom secret, pointing at
1303
+ * `localhost` — what an older `gkm secrets:init`, `gkm setup` or `gkm init`
1304
+ * generated. Set by hand wins over derived, so the app would be handed it.
1305
+ */
1306
+ var StaleStageSecrets = class extends require_errors.GkmError {
1307
+ stage;
1308
+ stale;
1309
+ constructor(stage, stale) {
1310
+ const one = stale.length === 1;
1311
+ super(`The stage '${stage}' holds ${one ? "a value" : "values"} an older gkm generated for ${one ? "an address a construct" : "addresses constructs"} now ${one ? "provides" : "provide"}:\n` + stale.map((s) => ` ${s.key}: ${s.construct}'s, stored pointing at ${s.host}`).join("\n") + `
1312
+ A stored value wins over the derived one, so the app would be handed an address where nothing answers inside its container. Remove ${one ? "it" : "each"}, and the construct's own value is used:\n` + stale.map((s) => ` gkm secrets:unset ${s.key} --stage ${stage}`).join("\n") + "\nA managed service set with its real host is not affected.");
1313
+ this.stage = stage;
1314
+ this.stale = stale;
1315
+ this.name = "StaleStageSecrets";
1316
+ }
1317
+ };
1318
+ /** The host a stored value names, if it is a URL. */
1319
+ function hostOf(value) {
1320
+ try {
1321
+ return new URL(value).hostname;
1322
+ } catch {
1323
+ return;
1324
+ }
1325
+ }
1326
+ /**
1327
+ * Each stored key that a construct provides an address for, holding an
1328
+ * address on this machine.
1329
+ */
1330
+ function staleStageSecrets(manifest, supplied) {
1331
+ const stale = [];
1332
+ for (const [id, declaration] of Object.entries(manifest)) {
1333
+ if (!ADDRESSED.has(declaration.kind)) continue;
1334
+ const key = (0, _geekmidas_manifest.providedKeyFor)(id, declaration.kind, "url");
1335
+ const value = supplied[key];
1336
+ if (value === void 0) continue;
1337
+ const host = hostOf(value);
1338
+ if (host === void 0 || !LOOPBACK.has(host)) continue;
1339
+ stale.push({
1340
+ key,
1341
+ construct: id,
1342
+ host
1343
+ });
1344
+ }
1345
+ return stale.sort((a, b) => a.key.localeCompare(b.key));
1346
+ }
1347
+ /**
1348
+ * Refuse a stage whose stored secrets would shadow a construct's address with
1349
+ * `localhost`.
1350
+ *
1351
+ * @throws {StaleStageSecrets} naming each key and the command removing it
1352
+ */
1353
+ function assertNoStaleSecrets(options) {
1354
+ const stale = staleStageSecrets(options.manifest, options.supplied);
1355
+ if (stale.length > 0) throw new StaleStageSecrets(options.stage, stale);
1356
+ }
1357
+ //#endregion
1358
+ //#region src/deploy/readiness.ts
1359
+ /**
1360
+ * Whether a stage is ready to be deployed: the one check, run once by every
1361
+ * deploy through a server target — after the stage's providers have created
1362
+ * what they create and written its keys, and before the target touches
1363
+ * anything.
1364
+ *
1365
+ * A build is not a deploy. Images carry no stage's keys, so a build-only run
1366
+ * (`gkm compose --build --push`) never calls this: a key the deploy itself
1367
+ * writes — a provider's bucket URL — is not there yet when CI builds, and
1368
+ * must not stop the build.
1369
+ */
1370
+ /**
1371
+ * The stage has every key it needs, read from its secrets as the providers
1372
+ * left them:
1373
+ *
1374
+ * 1. no kind it declares is one the stage set to `false`;
1375
+ * 2. its mail and storage are its own — or a dev service, where allowed,
1376
+ * reported once, loudly;
1377
+ * 3. what its providers created is still there, and its key reaches it;
1378
+ * 4. no key holds an address an older gkm stored for what a construct now
1379
+ * provides;
1380
+ * 5. each third party's credentials match their construct's schema.
1381
+ *
1382
+ * The local stage derives its mail and storage, so only 4 and 5 apply to it.
1383
+ *
1384
+ * @throws {StageProviderDisabled}
1385
+ * @throws {ExternalServicesNotConfigured} naming every missing key at once
1386
+ * @throws {StaleStageSecrets}
1387
+ * @throws {CredentialsInvalid}
1388
+ */
1389
+ async function assertStageReady(ctx) {
1390
+ const { workspace, manifest, stage } = ctx;
1391
+ const supplied = (await ctx.secrets.read())?.custom ?? {};
1392
+ if (stage !== workspace.stages.local) {
1393
+ require_seeds.assertStageProvidersEnabled(workspace, manifest, stage);
1394
+ const domain = workspace.domains?.[stage];
1395
+ const services = require_seeds.externalServices({
1396
+ stage,
1397
+ declarations: require_seeds.stageServiceDeclarations({
1398
+ manifest,
1399
+ ...ctx.runnables ? { runnables: ctx.runnables } : {}
1400
+ }),
1401
+ supplied,
1402
+ allow: ctx.allowDevServices,
1403
+ ...domain ? { domain } : {},
1404
+ providers: require_seeds.stageProviderNotes(workspace, stage)
1405
+ });
1406
+ if (services.missing.length > 0) throw new require_seeds.ExternalServicesNotConfigured(stage, services.missing);
1407
+ require_seeds.reportDevServices(ctx, require_seeds.devServicesUsed(services));
1408
+ const verified = await verifyStageProviders({
1409
+ workspace,
1410
+ manifest,
1411
+ stage,
1412
+ secrets: supplied,
1413
+ resources: (await ctx.state.read(stage))?.resources ?? {},
1414
+ log: (line) => ctx.logger.info(line)
1415
+ });
1416
+ for (const line of verified) ctx.logger.info(`✓ ${line} verified`);
1417
+ }
1418
+ assertNoStaleSecrets({
1419
+ manifest,
1420
+ stage,
1421
+ supplied
1422
+ });
1423
+ await require_credentialSchemas.assertStageCredentials({
1424
+ root: workspace.root,
1425
+ patterns: require_seeds.constructGlobs(workspace),
1426
+ manifest,
1427
+ stage,
1428
+ supplied
1429
+ });
1430
+ }
1431
+ //#endregion
1432
+ //#region src/deploy/orchestrate.ts
1433
+ /**
1434
+ * The run around a target: what every deploy does whatever it deploys
1435
+ * through.
1436
+ *
1437
+ * It loads the workspace, resolves the target, works out the apps and the
1438
+ * identity, takes the stage's lock, and calls the target's phases in order,
1439
+ * reporting each as it starts, finishes or fails. Everything that is about
1440
+ * *where* the stage goes is the target's.
1441
+ */
1442
+ /** `--resources-only` with `--skip-resources`: the one asks for what the other skips. */
1443
+ var ResourcesOnlyAndSkipped = class extends require_errors.GkmError {
1444
+ constructor() {
1445
+ super("--resources-only creates the stage's resources and nothing else, and --skip-resources deploys without creating them. Run the first where the cloud credentials are (a CI runner), then the second where the stack runs.");
1446
+ this.name = "ResourcesOnlyAndSkipped";
1447
+ }
1448
+ };
1449
+ /** Apps were asked for by name that the workspace does not have. */
1450
+ var UnknownDeployApps = class extends require_errors.GkmError {
1451
+ apps;
1452
+ available;
1453
+ constructor(apps, available) {
1454
+ super(`Unknown apps: ${apps.join(", ")}\nAvailable apps: ${available.join(", ")}`);
1455
+ this.apps = apps;
1456
+ this.available = available;
1457
+ this.name = "UnknownDeployApps";
1458
+ }
1459
+ };
1460
+ /** Every app asked for deploys through some other target. */
1461
+ var NoDeployableApps = class extends require_errors.GkmError {
1462
+ stage;
1463
+ target;
1464
+ constructor(stage, target) {
1465
+ super(`No apps to deploy through "${target}": every selected app deploys somewhere else. Pick the target with --target, or set each app's deploy in gkm.config.ts.`);
1466
+ this.stage = stage;
1467
+ this.target = target;
1468
+ this.name = "NoDeployableApps";
1469
+ }
1470
+ };
1471
+ /** A build-only run through a target with no build phase. */
1472
+ var TargetBuildsNothing = class extends require_errors.GkmError {
1473
+ target;
1474
+ constructor(target) {
1475
+ super(`"${target}" has no build phase, so a build-only run would do nothing. Deploy it in full, or build through a target that builds images (compose).`);
1476
+ this.target = target;
1477
+ this.name = "TargetBuildsNothing";
1478
+ }
1479
+ };
1480
+ /** A failed rollback, after the failure that called for it. */
1481
+ var RollbackFailed = class extends Error {
1482
+ target;
1483
+ failure;
1484
+ constructor(target, failure, cause) {
1485
+ super(`Deploying through "${target}" failed, and so did rolling it back: ${cause instanceof Error ? cause.message : String(cause)}. The stage may be part-released; deploy again, or roll it back by hand. The first failure: ${failure instanceof Error ? failure.message : String(failure)}`, { cause });
1486
+ this.target = target;
1487
+ this.failure = failure;
1488
+ this.name = "RollbackFailed";
1489
+ }
1490
+ };
1491
+ /**
1492
+ * Generate image tag from stage and timestamp
1493
+ */
1494
+ function generateTag(stage) {
1495
+ return `${stage}-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").slice(0, 19)}`;
1496
+ }
1497
+ /** Why an app that deploys through another target is left out. */
1498
+ function elsewhere(target, stage) {
1499
+ const instead = require_define.builtinTarget(target)?.instead?.(stage);
1500
+ if (instead) return `it deploys through "${target}" — ${instead}`;
1501
+ return `it deploys through "${target}" — run \`gkm deploy --target ${target} --stage ${stage}\``;
1502
+ }
1503
+ /**
1504
+ * Deploy a workspace's stage through its target.
1505
+ *
1506
+ * The engine under `deploy()` and `gkm deploy`. It never prompts and never
1507
+ * exits: credentials come from `ctx.credentials`, progress goes to
1508
+ * `ctx.emit` and to `output`, and every failure is thrown.
1509
+ *
1510
+ * @internal
1511
+ */
1512
+ async function runDeploy(source, request, ctx) {
1513
+ const { stage } = request;
1514
+ const redactor = ctx.redactor ?? new Redactor();
1515
+ const emit = (event) => ctx.emit(event.type === "log" ? {
1516
+ ...event,
1517
+ message: redactor.redact(event.message)
1518
+ } : event);
1519
+ /** Run one phase between its events. */
1520
+ const phase = async (name, body) => {
1521
+ ctx.signal?.throwIfAborted();
1522
+ emit({
1523
+ type: "phase.started",
1524
+ phase: name
1525
+ });
1526
+ try {
1527
+ const value = await body();
1528
+ emit({
1529
+ type: "phase.finished",
1530
+ phase: name
1531
+ });
1532
+ return value;
1533
+ } catch (error) {
1534
+ emit({
1535
+ type: "phase.failed",
1536
+ phase: name,
1537
+ error: require_sniffer.eventError(error)
1538
+ });
1539
+ throw error;
1540
+ }
1541
+ };
1542
+ ctx.signal?.throwIfAborted();
1543
+ emit({
1544
+ type: "phase.started",
1545
+ phase: "validate"
1546
+ });
1547
+ let prepared;
1548
+ try {
1549
+ prepared = await prepare(source, request, ctx, emit, redactor);
1550
+ } catch (error) {
1551
+ emit({
1552
+ type: "phase.failed",
1553
+ phase: "validate",
1554
+ error: require_sniffer.eventError(error)
1555
+ });
1556
+ throw error;
1557
+ }
1558
+ const { resolved, phaseCtx, store } = prepared;
1559
+ const { target } = resolved;
1560
+ const resources = !ctx.buildOnly && !request.skipResources && phaseCtx.stage !== phaseCtx.workspace.stages.local;
1561
+ const provision = async () => {
1562
+ if (!request.skipDns && resolved.target.runtime === "server") try {
1563
+ await require_dns.dnsCredentials(phaseCtx.stage, require_dns.stageDnsDomains(phaseCtx.stage, phaseCtx.workspace.domains, phaseCtx.workspace.dns), ctx.credentials);
1564
+ } catch (error) {
1565
+ emit({
1566
+ type: "phase.failed",
1567
+ phase: "validate",
1568
+ error: require_sniffer.eventError(error)
1569
+ });
1570
+ throw error;
1571
+ }
1572
+ await provisionResources(phaseCtx, request, ctx, store, emit);
1573
+ };
1574
+ if (ctx.dryRun) {
1575
+ if (resources) await provision();
1576
+ if (request.resourcesOnly) return resourcesOnly(target, phaseCtx, emit);
1577
+ const run = await validate(phaseCtx, emit, () => readyToDeploy(target, phaseCtx));
1578
+ await phase("plan", () => target.plan(phaseCtx, run));
1579
+ return target.result(phaseCtx, run);
1580
+ }
1581
+ if (ctx.buildOnly) {
1582
+ if (!target.build) throw new TargetBuildsNothing(resolved.name);
1583
+ const run = await validate(phaseCtx, emit, () => target.validate(phaseCtx));
1584
+ await phase("build", () => target.build(phaseCtx, run));
1585
+ return target.result(phaseCtx, run);
1586
+ }
1587
+ let lock;
1588
+ try {
1589
+ require_sniffer.assertStateOutlivesRun(phaseCtx.workspace, stage, "deploy");
1590
+ lock = await store.lock(stage, { operation: "deploy" });
1591
+ } catch (error) {
1592
+ emit({
1593
+ type: "phase.failed",
1594
+ phase: "validate",
1595
+ error: require_sniffer.eventError(error)
1596
+ });
1597
+ throw error;
1598
+ }
1599
+ try {
1600
+ if (resources) await provision();
1601
+ if (request.resourcesOnly) return resourcesOnly(target, phaseCtx, emit);
1602
+ const run = await validate(phaseCtx, emit, () => readyToDeploy(target, phaseCtx));
1603
+ if (target.provision) await phase("provision", () => target.provision(phaseCtx, run));
1604
+ if (target.build) await phase("build", () => target.build(phaseCtx, run));
1605
+ let releasing = "release";
1606
+ try {
1607
+ await phase("release", () => target.release(phaseCtx, run));
1608
+ releasing = "verify";
1609
+ if (target.verify) await phase("verify", () => target.verify(phaseCtx, run));
1610
+ } catch (error) {
1611
+ if (ctx.signal?.aborted || !target.capabilities.rollback) throw error;
1612
+ if (!target.rollback) throw error;
1613
+ try {
1614
+ await phase("rollback", () => target.rollback(phaseCtx, run, {
1615
+ phase: releasing,
1616
+ error
1617
+ }));
1618
+ } catch (rollbackError) {
1619
+ throw new RollbackFailed(resolved.name, error, rollbackError);
1620
+ }
1621
+ throw error;
1622
+ }
1623
+ return await target.result(phaseCtx, run);
1624
+ } finally {
1625
+ await lock.release();
1626
+ }
1627
+ }
1628
+ /**
1629
+ * The stage's providers, run inside the validate phase. A dry run plans them
1630
+ * and, so the checks after it see the stage as the run would leave it, reads
1631
+ * the keys they would write as placeholders.
1632
+ */
1633
+ async function provisionResources(phaseCtx, request, ctx, store, emit) {
1634
+ try {
1635
+ const reports = await provisionStage({
1636
+ workspace: phaseCtx.workspace,
1637
+ stage: phaseCtx.stage,
1638
+ manifest: phaseCtx.manifest,
1639
+ runnables: phaseCtx.runnables ?? {},
1640
+ secrets: phaseCtx.secrets,
1641
+ state: store,
1642
+ dryRun: ctx.dryRun,
1643
+ ...request.rotateKeys ? { rotateKeys: true } : {},
1644
+ ...request.retireOldKeys ? { retireOldKeys: true } : {},
1645
+ ...ctx.home ? { home: ctx.home } : {},
1646
+ log: (line) => phaseCtx.logger.info(line)
1647
+ });
1648
+ if (!ctx.dryRun) return;
1649
+ const planned = reports.flatMap((r) => r.planned);
1650
+ if (planned.length === 0) return;
1651
+ const secrets = phaseCtx.secrets;
1652
+ phaseCtx.secrets = {
1653
+ ...secrets,
1654
+ async read() {
1655
+ const stored = await secrets.read() ?? require_seeds.initStageSecrets(phaseCtx.stage);
1656
+ const custom = { ...stored.custom };
1657
+ for (const key of planned) custom[key] ??= PLANNED_BY_THE_DEPLOY;
1658
+ return {
1659
+ ...stored,
1660
+ custom
1661
+ };
1662
+ }
1663
+ };
1664
+ } catch (error) {
1665
+ emit({
1666
+ type: "phase.failed",
1667
+ phase: "validate",
1668
+ error: require_sniffer.eventError(error)
1669
+ });
1670
+ throw error;
1671
+ }
1672
+ }
1673
+ /** What a dry run reads for a key its providers would write. */
1674
+ const PLANNED_BY_THE_DEPLOY = "s3://planned-by-this-deploy";
1675
+ /** `--resources-only`: the target's own resources, and nothing else. */
1676
+ async function resourcesOnly(target, phaseCtx, emit) {
1677
+ try {
1678
+ if (target.resources) await target.resources(phaseCtx);
1679
+ } catch (error) {
1680
+ emit({
1681
+ type: "phase.failed",
1682
+ phase: "validate",
1683
+ error: require_sniffer.eventError(error)
1684
+ });
1685
+ throw error;
1686
+ }
1687
+ emit({
1688
+ type: "phase.finished",
1689
+ phase: "validate"
1690
+ });
1691
+ return resourcesResult(phaseCtx);
1692
+ }
1693
+ /** A `--resources-only` run: no app was deployed. */
1694
+ function resourcesResult(phaseCtx) {
1695
+ return {
1696
+ apps: [],
1697
+ projectId: "",
1698
+ successCount: 0,
1699
+ failedCount: 0,
1700
+ stage: phaseCtx.stage,
1701
+ identity: phaseCtx.identity.key,
1702
+ tag: phaseCtx.tag,
1703
+ dryRun: phaseCtx.dryRun,
1704
+ environmentId: "",
1705
+ skipped: [...phaseCtx.skipped],
1706
+ urls: {},
1707
+ changes: []
1708
+ };
1709
+ }
1710
+ /**
1711
+ * What a deploy validates: the stage ready to be deployed — every key it
1712
+ * needs, read as its providers left them — then what the target would run,
1713
+ * then whatever only the target checks. A build-only run validates the
1714
+ * target's run alone: it calls neither readiness check.
1715
+ */
1716
+ async function readyToDeploy(target, phaseCtx) {
1717
+ if (target.runtime === "server") await assertStageReady(phaseCtx);
1718
+ const run = await target.validate(phaseCtx);
1719
+ await target.ready?.(phaseCtx, run);
1720
+ return run;
1721
+ }
1722
+ /** `body`, then the run's `deploy.started`, inside the validate phase. */
1723
+ async function validate(phaseCtx, emit, body) {
1724
+ try {
1725
+ const run = await body();
1726
+ const skipped = new Set(phaseCtx.skipped.map((s) => s.app));
1727
+ emit({
1728
+ type: "deploy.started",
1729
+ stage: phaseCtx.stage,
1730
+ target: phaseCtx.target,
1731
+ identity: phaseCtx.identity.key,
1732
+ tag: phaseCtx.tag,
1733
+ apps: phaseCtx.apps.filter((app) => !skipped.has(app)),
1734
+ dryRun: phaseCtx.dryRun
1735
+ });
1736
+ emit({
1737
+ type: "phase.finished",
1738
+ phase: "validate"
1739
+ });
1740
+ return run;
1741
+ } catch (error) {
1742
+ emit({
1743
+ type: "phase.failed",
1744
+ phase: "validate",
1745
+ error: require_sniffer.eventError(error)
1746
+ });
1747
+ throw error;
1748
+ }
1749
+ }
1750
+ /** Everything the phases are handed, worked out before the first of them. */
1751
+ async function prepare(source, request, ctx, emit, redactor) {
1752
+ const { stage } = request;
1753
+ if (request.resourcesOnly && request.skipResources) throw new ResourcesOnlyAndSkipped();
1754
+ const configured = typeof source === "function" ? await source() : source;
1755
+ const local = stage === configured.stages.local;
1756
+ if (!local) require_workspace.assertDeployedStage(configured.stages, stage);
1757
+ const targetName = request.target ?? configured.deploy?.default ?? "dokploy";
1758
+ const resolved = await require_resolve.resolveTarget(targetName, {
1759
+ workspace: configured,
1760
+ stage,
1761
+ ...ctx.targets ? { host: ctx.targets } : {}
1762
+ });
1763
+ if (local && !resolved.target.capabilities.localStage) require_workspace.assertDeployedStage(configured.stages, stage);
1764
+ const options = await require_resolve.parseTargetOptions(resolved);
1765
+ const allowDevServices = request.allowDevServices === true;
1766
+ if (allowDevServices && resolved.target.runtime === "aws") throw new require_seeds.DevServicesNeedServerTarget(targetName);
1767
+ const runnables = {};
1768
+ const background = {};
1769
+ const manifest = await require_discover.discover({
1770
+ patterns: require_seeds.constructGlobs(configured),
1771
+ cwd: configured.root,
1772
+ runnables,
1773
+ background
1774
+ });
1775
+ require_workspace.resolveStageTelemetry({
1776
+ ...configured.deploy?.telemetry ? { telemetry: configured.deploy.telemetry } : {},
1777
+ stage,
1778
+ local,
1779
+ target: targetName,
1780
+ runtime: resolved.target.runtime,
1781
+ selfHosted: resolved.target.capabilities.selfHostedTelemetry === true,
1782
+ used: require_seeds.usesTelemetry(manifest)
1783
+ });
1784
+ const units = require_derive.derivedApps(manifest, configured);
1785
+ const workspace = Object.keys(units).length > 0 ? {
1786
+ ...configured,
1787
+ apps: units
1788
+ } : configured;
1789
+ const identity = require_seeds.deployIdentity(workspace, stage);
1790
+ const tag = request.tag ?? (resolved.target.tag ? await resolved.target.tag({
1791
+ cwd: workspace.root,
1792
+ stage
1793
+ }) : generateTag(stage));
1794
+ const buildOrder = require_workspace.getAppBuildOrder(workspace);
1795
+ let selected = buildOrder;
1796
+ if (request.apps && request.apps.length > 0) {
1797
+ const unknown = request.apps.filter((name) => !workspace.apps[name]);
1798
+ if (unknown.length > 0) throw new UnknownDeployApps(unknown, Object.keys(workspace.apps));
1799
+ selected = buildOrder.filter((name) => request.apps.includes(name));
1800
+ }
1801
+ const skipped = [];
1802
+ const skip = (app, reason) => {
1803
+ skipped.push({
1804
+ app,
1805
+ reason
1806
+ });
1807
+ emit({
1808
+ type: "app.skipped",
1809
+ app,
1810
+ reason
1811
+ });
1812
+ };
1813
+ const byDefault = configured.deploy?.default ?? "dokploy";
1814
+ const apps = selected.filter((name) => {
1815
+ const own = workspace.apps[name].resolvedDeployTarget;
1816
+ const deploysThrough = own === byDefault ? targetName : own;
1817
+ if (deploysThrough === targetName) return true;
1818
+ skip(name, elsewhere(deploysThrough, stage));
1819
+ return false;
1820
+ });
1821
+ if (apps.length === 0) throw new NoDeployableApps(stage, targetName);
1822
+ const store = await require_sniffer.createStateStore({
1823
+ config: workspace.state,
1824
+ workspaceRoot: workspace.root,
1825
+ workspaceName: workspace.name
1826
+ });
1827
+ const secrets = await deploySecrets(workspace, stage, redactor, { ...ctx.home ? { home: ctx.home } : {} });
1828
+ const line = (level) => (message) => emit({
1829
+ type: "log",
1830
+ level,
1831
+ message
1832
+ });
1833
+ const logger = {
1834
+ info: line("info"),
1835
+ warn: line("warn"),
1836
+ error: line("error")
1837
+ };
1838
+ return {
1839
+ resolved,
1840
+ phaseCtx: {
1841
+ target: targetName,
1842
+ cwd: workspace.root,
1843
+ stage,
1844
+ tag,
1845
+ tagGiven: request.tag !== void 0,
1846
+ apps,
1847
+ skipped,
1848
+ identity,
1849
+ workspace,
1850
+ manifest,
1851
+ runnables,
1852
+ background,
1853
+ options,
1854
+ dryRun: ctx.dryRun,
1855
+ atomic: request.atomic ?? false,
1856
+ allowDevServices,
1857
+ ...request.skipDns ? { skipDns: true } : {},
1858
+ ...request.skipResources ? { skipResources: true } : {},
1859
+ credentials: ctx.credentials,
1860
+ state: store,
1861
+ secrets,
1862
+ logger,
1863
+ signal: ctx.signal ?? new AbortController().signal,
1864
+ childOutput: ctx.childOutput ?? "inherit",
1865
+ emit,
1866
+ skip,
1867
+ name: (resource) => require_seeds.applicationName(identity, resource)
1868
+ },
1869
+ store
1870
+ };
1871
+ }
1872
+ //#endregion
1873
+ //#region src/deploy/deploy.ts
1874
+ /**
1875
+ * `deploy()`: a deploy as a library call.
1876
+ *
1877
+ * `gkm deploy` could only be driven from an interactive terminal: it prompted
1878
+ * for what it lacked and exited the process on failure, worked out of
1879
+ * `process.cwd()`, and reported progress by printing. Here the caller says
1880
+ * where the project is, hands over the credentials and a signal, and gets back
1881
+ * a run — events to iterate while it goes, and a structured result at the end.
1882
+ * Nothing below this asks a person anything or ends the process; `gkm deploy`
1883
+ * is a thin wrapper that does both.
1884
+ */
1885
+ /**
1886
+ * Every event a run emits, kept so that each iteration — however late it
1887
+ * starts — sees the run from its first event to its last.
1888
+ */
1889
+ var EventLog = class {
1890
+ events = [];
1891
+ closed = false;
1892
+ waiting = [];
1893
+ push(event) {
1894
+ this.events.push(event);
1895
+ this.wake();
1896
+ }
1897
+ close() {
1898
+ this.closed = true;
1899
+ this.wake();
1900
+ }
1901
+ wake() {
1902
+ const waiting = this.waiting;
1903
+ this.waiting = [];
1904
+ for (const resume of waiting) resume();
1905
+ }
1906
+ async *iterate() {
1907
+ let next = 0;
1908
+ while (true) if (next < this.events.length) yield this.events[next++];
1909
+ else if (this.closed) return;
1910
+ else await new Promise((resume) => this.waiting.push(resume));
1911
+ }
1912
+ };
1913
+ function forward(logger, level, message) {
1914
+ if (!logger) return;
1915
+ if (level === "info") logger.info(message);
1916
+ else if (level === "error" && logger.error) logger.error(message);
1917
+ else logger.warn(message);
1918
+ }
1919
+ /**
1920
+ * Deploy a project's stage.
1921
+ *
1922
+ * ```ts
1923
+ * import { deploy } from '@geekmidas/cli/deploy';
1924
+ *
1925
+ * const run = deploy({ cwd: '/srv/checkouts/shop', stage: 'production' });
1926
+ * for await (const event of run) {
1927
+ * if (event.type === 'app.deployed') console.log(event.app, event.url);
1928
+ * }
1929
+ * const result = await run.result;
1930
+ * ```
1931
+ *
1932
+ * The run starts at once; iterating it is optional, and an iteration started
1933
+ * late still sees every event from the first.
1934
+ */
1935
+ function deploy(input) {
1936
+ const log = new EventLog();
1937
+ const redactor = new Redactor();
1938
+ const emit = (event) => {
1939
+ const masked = event.type === "log" ? {
1940
+ ...event,
1941
+ message: redactor.redact(event.message)
1942
+ } : event;
1943
+ log.push(masked);
1944
+ if (masked.type === "log") forward(input.logger, masked.level, masked.message);
1945
+ };
1946
+ const ctx = {
1947
+ emit,
1948
+ credentials: input.credentials ?? require_credentials.storedCredentials({ home: input.home ?? require_run.gkmHome() }),
1949
+ dryRun: input.dryRun ?? false,
1950
+ ...input.buildOnly ? { buildOnly: true } : {},
1951
+ home: input.home ?? require_run.gkmHome(),
1952
+ redactor,
1953
+ ...input.signal ? { signal: input.signal } : {},
1954
+ ...input.childOutput ? { childOutput: input.childOutput } : {},
1955
+ ...input.targets ? { targets: input.targets } : {}
1956
+ };
1957
+ const result = require_discover.withOutput((level, message) => emit({
1958
+ type: "log",
1959
+ level,
1960
+ message
1961
+ }), async () => {
1962
+ try {
1963
+ input.signal?.throwIfAborted();
1964
+ const sandbox = input.sandbox ?? new require_local.LocalSandbox({ root: require_config.findWorkspaceRoot((0, node_path.resolve)(input.cwd)) });
1965
+ const deployed = await require_sandbox.withSandbox(sandbox, () => runDeploy(() => load(input, sandbox), {
1966
+ stage: input.stage,
1967
+ ...input.target ? { target: input.target } : {},
1968
+ ...input.tag ? { tag: input.tag } : {},
1969
+ ...input.apps ? { apps: input.apps } : {},
1970
+ ...input.atomic ? { atomic: true } : {},
1971
+ ...input.allowDevServices ? { allowDevServices: input.allowDevServices } : {},
1972
+ ...input.skipDns ? { skipDns: true } : {},
1973
+ ...input.resourcesOnly ? { resourcesOnly: true } : {},
1974
+ ...input.skipResources ? { skipResources: true } : {},
1975
+ ...input.rotateKeys ? { rotateKeys: true } : {},
1976
+ ...input.retireOldKeys ? { retireOldKeys: true } : {}
1977
+ }, ctx));
1978
+ emit({
1979
+ type: "deploy.finished",
1980
+ result: deployed
1981
+ });
1982
+ return deployed;
1983
+ } catch (error) {
1984
+ emit({
1985
+ type: "deploy.failed",
1986
+ error: require_sniffer.eventError(error)
1987
+ });
1988
+ throw error;
1989
+ } finally {
1990
+ log.close();
1991
+ }
1992
+ });
1993
+ result.catch(() => {});
1994
+ return {
1995
+ result,
1996
+ [Symbol.asyncIterator]: () => log.iterate()
1997
+ };
1998
+ }
1999
+ /**
2000
+ * The workspace at `input.cwd`. The stage is checked by the run, once it
2001
+ * knows the target: one that runs on this machine may take the local stage.
2002
+ */
2003
+ async function load(input, sandbox) {
2004
+ const { workspace } = await require_config.loadWorkspaceConfig((0, node_path.resolve)(input.cwd), { sandbox });
2005
+ return workspace;
2006
+ }
2007
+ //#endregion
2008
+ Object.defineProperty(exports, "IAM_USER_PATH", {
2009
+ enumerable: true,
2010
+ get: function() {
2011
+ return IAM_USER_PATH;
2012
+ }
2013
+ });
2014
+ Object.defineProperty(exports, "NoDeployableApps", {
2015
+ enumerable: true,
2016
+ get: function() {
2017
+ return NoDeployableApps;
2018
+ }
2019
+ });
2020
+ Object.defineProperty(exports, "RollbackFailed", {
2021
+ enumerable: true,
2022
+ get: function() {
2023
+ return RollbackFailed;
2024
+ }
2025
+ });
2026
+ Object.defineProperty(exports, "TargetBuildsNothing", {
2027
+ enumerable: true,
2028
+ get: function() {
2029
+ return TargetBuildsNothing;
2030
+ }
2031
+ });
2032
+ Object.defineProperty(exports, "UnknownDeployApps", {
2033
+ enumerable: true,
2034
+ get: function() {
2035
+ return UnknownDeployApps;
2036
+ }
2037
+ });
2038
+ Object.defineProperty(exports, "bucketName", {
2039
+ enumerable: true,
2040
+ get: function() {
2041
+ return bucketName;
2042
+ }
2043
+ });
2044
+ Object.defineProperty(exports, "deploy", {
2045
+ enumerable: true,
2046
+ get: function() {
2047
+ return deploy;
2048
+ }
2049
+ });
2050
+ Object.defineProperty(exports, "iamUserName", {
2051
+ enumerable: true,
2052
+ get: function() {
2053
+ return iamUserName;
2054
+ }
2055
+ });
2056
+
2057
+ //# sourceMappingURL=deploy-BdN0ym2i.cjs.map