@geekmidas/cli 10.0.0-alpha.160 → 10.0.0-alpha.162

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 (245) hide show
  1. package/dist/{GoDaddyProvider-C9dDlH9I.mjs → GoDaddyProvider-COSh6bNI.mjs} +2 -2
  2. package/dist/{GoDaddyProvider-C9dDlH9I.mjs.map → GoDaddyProvider-COSh6bNI.mjs.map} +1 -1
  3. package/dist/{GoDaddyProvider-DkUMcMYy.cjs → GoDaddyProvider-DyhN8PEn.cjs} +2 -2
  4. package/dist/{GoDaddyProvider-DkUMcMYy.cjs.map → GoDaddyProvider-DyhN8PEn.cjs.map} +1 -1
  5. package/dist/{HostingerProvider-BEuKG2gz.mjs → HostingerProvider-5Nekoneq.mjs} +2 -2
  6. package/dist/{HostingerProvider-BEuKG2gz.mjs.map → HostingerProvider-5Nekoneq.mjs.map} +1 -1
  7. package/dist/{HostingerProvider-9faYOQnQ.cjs → HostingerProvider-DG08aTyh.cjs} +2 -2
  8. package/dist/{HostingerProvider-9faYOQnQ.cjs.map → HostingerProvider-DG08aTyh.cjs.map} +1 -1
  9. package/dist/{LegacyStateStore-C44jUnMH.cjs → LegacyStateStore-5W4kFia3.cjs} +3 -3
  10. package/dist/{LegacyStateStore-C44jUnMH.cjs.map → LegacyStateStore-5W4kFia3.cjs.map} +1 -1
  11. package/dist/{LegacyStateStore-DPKhjUR6.mjs → LegacyStateStore-CafGn2Yl.mjs} +3 -3
  12. package/dist/{LegacyStateStore-DPKhjUR6.mjs.map → LegacyStateStore-CafGn2Yl.mjs.map} +1 -1
  13. package/dist/{LocalStateStore-B95WE99i.cjs → LocalStateStore--CJ7ksx6.cjs} +2 -2
  14. package/dist/{LocalStateStore-B95WE99i.cjs.map → LocalStateStore--CJ7ksx6.cjs.map} +1 -1
  15. package/dist/{LocalStateStore-BU3fWkcT.mjs → LocalStateStore-DrTw_EIu.mjs} +2 -2
  16. package/dist/{LocalStateStore-BU3fWkcT.mjs.map → LocalStateStore-DrTw_EIu.mjs.map} +1 -1
  17. package/dist/{S3StateStore-Cpbeijr5.mjs → S3StateStore-DGEwSkf2.mjs} +4 -4
  18. package/dist/{S3StateStore-Cpbeijr5.mjs.map → S3StateStore-DGEwSkf2.mjs.map} +1 -1
  19. package/dist/{S3StateStore-zU5kClhd.cjs → S3StateStore-M8vVPpr7.cjs} +4 -4
  20. package/dist/{S3StateStore-zU5kClhd.cjs.map → S3StateStore-M8vVPpr7.cjs.map} +1 -1
  21. package/dist/{SSMStateStore-D9uviQ3T.cjs → SSMStateStore-B2A27u0C.cjs} +2 -2
  22. package/dist/{SSMStateStore-D9uviQ3T.cjs.map → SSMStateStore-B2A27u0C.cjs.map} +1 -1
  23. package/dist/{SSMStateStore-BxkKMBi_.mjs → SSMStateStore-VYfuykBd.mjs} +2 -2
  24. package/dist/{SSMStateStore-BxkKMBi_.mjs.map → SSMStateStore-VYfuykBd.mjs.map} +1 -1
  25. package/dist/{add-Bb2IL3Yq.cjs → add-CijlgwVr.cjs} +6 -6
  26. package/dist/{add-Bb2IL3Yq.cjs.map → add-CijlgwVr.cjs.map} +1 -1
  27. package/dist/{add-Dj-gtqTO.mjs → add-CistiUc6.mjs} +6 -6
  28. package/dist/{add-Dj-gtqTO.mjs.map → add-CistiUc6.mjs.map} +1 -1
  29. package/dist/{apps-0vc4Oz9_.cjs → apps-5iJb710M.cjs} +5 -5
  30. package/dist/{apps-0vc4Oz9_.cjs.map → apps-5iJb710M.cjs.map} +1 -1
  31. package/dist/{apps-FRNlECwi.mjs → apps-BE97n0jO.mjs} +5 -5
  32. package/dist/{apps-FRNlECwi.mjs.map → apps-BE97n0jO.mjs.map} +1 -1
  33. package/dist/{aws-8QQ8n-t6.mjs → aws-C5LAU_Ii.mjs} +10 -2
  34. package/dist/aws-C5LAU_Ii.mjs.map +1 -0
  35. package/dist/{aws-DY9NqQIT.cjs → aws-DKC2m3sT.cjs} +9 -1
  36. package/dist/aws-DKC2m3sT.cjs.map +1 -0
  37. package/dist/{bundler-9EM4ZHcu.mjs → bundler-D-SXSUZI.mjs} +6 -6
  38. package/dist/{bundler-9EM4ZHcu.mjs.map → bundler-D-SXSUZI.mjs.map} +1 -1
  39. package/dist/{bundler-Bg2eEFEh.cjs → bundler-D4E7kCYb.cjs} +6 -6
  40. package/dist/{bundler-Bg2eEFEh.cjs.map → bundler-D4E7kCYb.cjs.map} +1 -1
  41. package/dist/{cli-CmoSOzNq.mjs → cli-Brr1IVq1.mjs} +5 -5
  42. package/dist/{cli-CmoSOzNq.mjs.map → cli-Brr1IVq1.mjs.map} +1 -1
  43. package/dist/{cli-YtLjcAZT.cjs → cli-D--xSSlK.cjs} +5 -5
  44. package/dist/{cli-YtLjcAZT.cjs.map → cli-D--xSSlK.cjs.map} +1 -1
  45. package/dist/{compose-D7gOSspy.cjs → compose-BoxgqTF8.cjs} +17 -11
  46. package/dist/compose-BoxgqTF8.cjs.map +1 -0
  47. package/dist/{compose-3AtZF7ul.mjs → compose-CuoV9T9U.mjs} +6 -6
  48. package/dist/{compose-3AtZF7ul.mjs.map → compose-CuoV9T9U.mjs.map} +1 -1
  49. package/dist/{compose-CnXZAYNM.mjs → compose-Dk0Z8zdf.mjs} +17 -11
  50. package/dist/compose-Dk0Z8zdf.mjs.map +1 -0
  51. package/dist/{compose-CW1DGfYc.cjs → compose-Dpkkp-Qq.cjs} +6 -6
  52. package/dist/{compose-CW1DGfYc.cjs.map → compose-Dpkkp-Qq.cjs.map} +1 -1
  53. package/dist/{config-CkuiD9Jv.d.cts → config-BxSSAzVi.d.mts} +3 -3
  54. package/dist/{config-BNmLn0dw.d.mts.map → config-BxSSAzVi.d.mts.map} +1 -1
  55. package/dist/{config-B5Hi3mLK.cjs → config-CLXSXbcs.cjs} +2 -2
  56. package/dist/{config-B5Hi3mLK.cjs.map → config-CLXSXbcs.cjs.map} +1 -1
  57. package/dist/{config-BNmLn0dw.d.mts → config-DNoWqavN.d.cts} +3 -3
  58. package/dist/{config-CkuiD9Jv.d.cts.map → config-DNoWqavN.d.cts.map} +1 -1
  59. package/dist/{config-DWTUVGRE.mjs → config-Da9bxnkT.mjs} +2 -2
  60. package/dist/{config-DWTUVGRE.mjs.map → config-Da9bxnkT.mjs.map} +1 -1
  61. package/dist/config.cjs +2 -2
  62. package/dist/config.d.cts +3 -3
  63. package/dist/config.d.mts +3 -3
  64. package/dist/config.mjs +2 -2
  65. package/dist/{credentials-DwIRfeBL.cjs → credentials-BR3dNNtO.cjs} +12 -8
  66. package/dist/credentials-BR3dNNtO.cjs.map +1 -0
  67. package/dist/{credentials-CjFtNBVH.mjs → credentials-DhCCRQJp.mjs} +12 -8
  68. package/dist/credentials-DhCCRQJp.mjs.map +1 -0
  69. package/dist/deploy/public.cjs +13 -13
  70. package/dist/deploy/public.d.cts +4 -4
  71. package/dist/deploy/public.d.cts.map +1 -1
  72. package/dist/deploy/public.d.mts +4 -4
  73. package/dist/deploy/public.d.mts.map +1 -1
  74. package/dist/deploy/public.mjs +13 -13
  75. package/dist/{deploy-BI3fHnyb.mjs → deploy-Bb-o2KYC.mjs} +294 -531
  76. package/dist/deploy-Bb-o2KYC.mjs.map +1 -0
  77. package/dist/{deploy-3bsiWJxj.cjs → deploy-Ck0zXkM7.cjs} +330 -567
  78. package/dist/deploy-Ck0zXkM7.cjs.map +1 -0
  79. package/dist/destroy-DIV6l_94.mjs +788 -0
  80. package/dist/destroy-DIV6l_94.mjs.map +1 -0
  81. package/dist/destroy-JHapr9UF.cjs +789 -0
  82. package/dist/destroy-JHapr9UF.cjs.map +1 -0
  83. package/dist/{docker-CwayjAF2.cjs → docker-CIpOFfOw.cjs} +5 -5
  84. package/dist/{docker-CwayjAF2.cjs.map → docker-CIpOFfOw.cjs.map} +1 -1
  85. package/dist/{docker-DPdn7tYJ.mjs → docker-zaY5JoqN.mjs} +5 -5
  86. package/dist/{docker-DPdn7tYJ.mjs.map → docker-zaY5JoqN.mjs.map} +1 -1
  87. package/dist/{dokploy-tmnH0Du_.mjs → dokploy-BTzdAG5F.mjs} +2 -2
  88. package/dist/{dokploy-tmnH0Du_.mjs.map → dokploy-BTzdAG5F.mjs.map} +1 -1
  89. package/dist/{dokploy-BMBL-_Xu.cjs → dokploy-DLVSItVD.cjs} +2 -2
  90. package/dist/{dokploy-BMBL-_Xu.cjs.map → dokploy-DLVSItVD.cjs.map} +1 -1
  91. package/dist/{eas-meN1xady.cjs → eas-B6VYsGLs.cjs} +3 -3
  92. package/dist/{eas-meN1xady.cjs.map → eas-B6VYsGLs.cjs.map} +1 -1
  93. package/dist/{eas-BVGQAUDc.mjs → eas-C1WvLOi1.mjs} +3 -3
  94. package/dist/{eas-BVGQAUDc.mjs.map → eas-C1WvLOi1.mjs.map} +1 -1
  95. package/dist/{encryption-C8AMbomA.mjs → encryption-DdnL7dor.mjs} +4 -4
  96. package/dist/{encryption-C8AMbomA.mjs.map → encryption-DdnL7dor.mjs.map} +1 -1
  97. package/dist/{encryption-DvFVZdGz.cjs → encryption-oVcZSyLK.cjs} +4 -4
  98. package/dist/{encryption-DvFVZdGz.cjs.map → encryption-oVcZSyLK.cjs.map} +1 -1
  99. package/dist/{engine-BbWj0w3F.mjs → engine-Viv9p-yK.mjs} +11 -11
  100. package/dist/{engine-BbWj0w3F.mjs.map → engine-Viv9p-yK.mjs.map} +1 -1
  101. package/dist/{engine-BmiIUVWa.cjs → engine-_zA1Selg.cjs} +11 -11
  102. package/dist/{engine-BmiIUVWa.cjs.map → engine-_zA1Selg.cjs.map} +1 -1
  103. package/dist/{github-Dc1daUbH.mjs → github-BOXn5NEP.mjs} +42 -10
  104. package/dist/github-BOXn5NEP.mjs.map +1 -0
  105. package/dist/{github-BWqQr8q6.cjs → github-CRvdaD2Y.cjs} +46 -14
  106. package/dist/github-CRvdaD2Y.cjs.map +1 -0
  107. package/dist/{googlePlay-ByaFUly7.mjs → googlePlay-C4UsG1Vd.mjs} +5 -5
  108. package/dist/{googlePlay-ByaFUly7.mjs.map → googlePlay-C4UsG1Vd.mjs.map} +1 -1
  109. package/dist/{googlePlay-CdVCdfU8.cjs → googlePlay-C5T1SOGW.cjs} +5 -5
  110. package/dist/{googlePlay-CdVCdfU8.cjs.map → googlePlay-C5T1SOGW.cjs.map} +1 -1
  111. package/dist/iam-BVPpX76R.cjs +521 -0
  112. package/dist/iam-BVPpX76R.cjs.map +1 -0
  113. package/dist/iam-q9ALh-Y5.mjs +442 -0
  114. package/dist/iam-q9ALh-Y5.mjs.map +1 -0
  115. package/dist/{index-Dv1Zj86i.d.mts → index-BC1BLv9Q.d.mts} +2 -2
  116. package/dist/{index-B35vTdAW.d.cts.map → index-BC1BLv9Q.d.mts.map} +1 -1
  117. package/dist/{index-B35vTdAW.d.cts → index-Dy4ojz5P.d.cts} +2 -2
  118. package/dist/{index-Dv1Zj86i.d.mts.map → index-Dy4ojz5P.d.cts.map} +1 -1
  119. package/dist/index.cjs +101 -80
  120. package/dist/index.cjs.map +1 -1
  121. package/dist/index.mjs +101 -80
  122. package/dist/index.mjs.map +1 -1
  123. package/dist/{local-CnVFHJjS.mjs → local-Bqx5Xj3z.mjs} +2 -2
  124. package/dist/{local-CnVFHJjS.mjs.map → local-Bqx5Xj3z.mjs.map} +1 -1
  125. package/dist/{local-qevrV6dZ.cjs → local-vSeEreyp.cjs} +2 -2
  126. package/dist/{local-qevrV6dZ.cjs.map → local-vSeEreyp.cjs.map} +1 -1
  127. package/dist/{migrate-BEuCcCwN.mjs → migrate-CEhUGDFy.mjs} +6 -6
  128. package/dist/{migrate-BEuCcCwN.mjs.map → migrate-CEhUGDFy.mjs.map} +1 -1
  129. package/dist/{migrate-CU8_ZWP1.cjs → migrate-CLSY8gSY.cjs} +6 -6
  130. package/dist/{migrate-CU8_ZWP1.cjs.map → migrate-CLSY8gSY.cjs.map} +1 -1
  131. package/dist/{migrate-DgfKZB41.mjs → migrate-D8PSR_08.mjs} +6 -6
  132. package/dist/{migrate-DgfKZB41.mjs.map → migrate-D8PSR_08.mjs.map} +1 -1
  133. package/dist/{migrate-CRRps1i6.cjs → migrate-DvdpT5Y4.cjs} +6 -6
  134. package/dist/{migrate-CRRps1i6.cjs.map → migrate-DvdpT5Y4.cjs.map} +1 -1
  135. package/dist/{notes-BrBnyZz5.cjs → notes-B9ro_R3x.cjs} +24 -15
  136. package/dist/notes-B9ro_R3x.cjs.map +1 -0
  137. package/dist/{notes-CIzrdBKE.mjs → notes-CdbUjLBC.mjs} +24 -15
  138. package/dist/notes-CdbUjLBC.mjs.map +1 -0
  139. package/dist/openapi.cjs +2 -2
  140. package/dist/openapi.d.cts +1 -1
  141. package/dist/openapi.d.mts +1 -1
  142. package/dist/openapi.mjs +2 -2
  143. package/dist/{projectBucket-CDJd3prK.mjs → projectBucket-DPaUiLyY.mjs} +2 -2
  144. package/dist/{projectBucket-CDJd3prK.mjs.map → projectBucket-DPaUiLyY.mjs.map} +1 -1
  145. package/dist/{projectBucket-CKr_zObJ.cjs → projectBucket-DUtWAxWn.cjs} +2 -2
  146. package/dist/{projectBucket-CKr_zObJ.cjs.map → projectBucket-DUtWAxWn.cjs.map} +1 -1
  147. package/dist/{publish-Bq96_QnK.mjs → publish-CoCRpbPl.mjs} +2 -2
  148. package/dist/{publish-Bq96_QnK.mjs.map → publish-CoCRpbPl.mjs.map} +1 -1
  149. package/dist/{publish-DMKXh1Tb.cjs → publish-DbR3j4TS.cjs} +2 -2
  150. package/dist/{publish-DMKXh1Tb.cjs.map → publish-DbR3j4TS.cjs.map} +1 -1
  151. package/dist/reconcile/public.d.cts +1 -1
  152. package/dist/reconcile/public.d.mts +1 -1
  153. package/dist/{resolve-CSsE7frp.mjs → resolve-BjRvKEpw.mjs} +4 -4
  154. package/dist/{resolve-CSsE7frp.mjs.map → resolve-BjRvKEpw.mjs.map} +1 -1
  155. package/dist/{resolve-DF9GrosZ.cjs → resolve-DDcPuS_G.cjs} +4 -4
  156. package/dist/{resolve-DF9GrosZ.cjs.map → resolve-DDcPuS_G.cjs.map} +1 -1
  157. package/dist/{rollback-BBFGGmMa.cjs → rollback-CvlebzBY.cjs} +8 -8
  158. package/dist/{rollback-BBFGGmMa.cjs.map → rollback-CvlebzBY.cjs.map} +1 -1
  159. package/dist/{rollback-CZthwqus.mjs → rollback-xLGFZaHy.mjs} +8 -8
  160. package/dist/{rollback-CZthwqus.mjs.map → rollback-xLGFZaHy.mjs.map} +1 -1
  161. package/dist/{runtime-CFvcLEJW.d.cts → runtime-BySbkdBz.d.cts} +2 -2
  162. package/dist/{runtime-CFvcLEJW.d.cts.map → runtime-BySbkdBz.d.cts.map} +1 -1
  163. package/dist/{runtime-dwsH6ddo.d.mts → runtime-MZ5Pqgan.d.mts} +2 -2
  164. package/dist/{runtime-dwsH6ddo.d.mts.map → runtime-MZ5Pqgan.d.mts.map} +1 -1
  165. package/dist/{s3-BBRkezf7.mjs → s3-CXA6Lleo.mjs} +29 -4
  166. package/dist/s3-CXA6Lleo.mjs.map +1 -0
  167. package/dist/{s3-CI2QYgXH.cjs → s3-D9pFisyo.cjs} +28 -3
  168. package/dist/s3-D9pFisyo.cjs.map +1 -0
  169. package/dist/{secretsManager-CQORubp9.mjs → secretsManager-CJyxFcBH.mjs} +18 -2
  170. package/dist/secretsManager-CJyxFcBH.mjs.map +1 -0
  171. package/dist/{secretsManager-RAGcEDX1.cjs → secretsManager-D8on44WI.cjs} +17 -1
  172. package/dist/secretsManager-D8on44WI.cjs.map +1 -0
  173. package/dist/{seeds-B3xvUAQf.cjs → seeds-D7q1zKCJ.cjs} +7 -1
  174. package/dist/{seeds-B3xvUAQf.cjs.map → seeds-D7q1zKCJ.cjs.map} +1 -1
  175. package/dist/{seeds-BCC8vAXR.mjs → seeds-y1rzXlAc.mjs} +2 -2
  176. package/dist/{seeds-BCC8vAXR.mjs.map → seeds-y1rzXlAc.mjs.map} +1 -1
  177. package/dist/{serverSetup-Dt2DTlwa.cjs → serverSetup-BoxtHWkI.cjs} +8 -8
  178. package/dist/{serverSetup-Dt2DTlwa.cjs.map → serverSetup-BoxtHWkI.cjs.map} +1 -1
  179. package/dist/{serverSetup-C4iAABtI.mjs → serverSetup-DxcbUlwE.mjs} +8 -8
  180. package/dist/{serverSetup-C4iAABtI.mjs.map → serverSetup-DxcbUlwE.mjs.map} +1 -1
  181. package/dist/{sniffer-bTrGuu9z.cjs → sniffer-B2jgjk-F.cjs} +8 -8
  182. package/dist/{sniffer-bTrGuu9z.cjs.map → sniffer-B2jgjk-F.cjs.map} +1 -1
  183. package/dist/{sniffer-C2Ot1IbK.mjs → sniffer-Csft3UaJ.mjs} +8 -8
  184. package/dist/{sniffer-C2Ot1IbK.mjs.map → sniffer-Csft3UaJ.mjs.map} +1 -1
  185. package/dist/{snsSubscriptions-CFKb7fv0.cjs → snsSubscriptions-CSyPnoEQ.cjs} +9 -3
  186. package/dist/{snsSubscriptions-CFKb7fv0.cjs.map → snsSubscriptions-CSyPnoEQ.cjs.map} +1 -1
  187. package/dist/{snsSubscriptions-QDDDn9Qi.mjs → snsSubscriptions-DgbFGyac.mjs} +4 -4
  188. package/dist/{snsSubscriptions-QDDDn9Qi.mjs.map → snsSubscriptions-DgbFGyac.mjs.map} +1 -1
  189. package/dist/{sst-iRdMlGKd.cjs → sst-BtrgnhHy.cjs} +5 -5
  190. package/dist/{sst-iRdMlGKd.cjs.map → sst-BtrgnhHy.cjs.map} +1 -1
  191. package/dist/{sst-DDRWlXdv.mjs → sst-CmNUadAV.mjs} +5 -5
  192. package/dist/{sst-DDRWlXdv.mjs.map → sst-CmNUadAV.mjs.map} +1 -1
  193. package/dist/{stack-B9iDYMrq.cjs → stack-9qBhn2pd.cjs} +8 -8
  194. package/dist/{stack-B9iDYMrq.cjs.map → stack-9qBhn2pd.cjs.map} +1 -1
  195. package/dist/{stack-CIOY44aC.mjs → stack-dVgzDUiH.mjs} +8 -8
  196. package/dist/{stack-CIOY44aC.mjs.map → stack-dVgzDUiH.mjs.map} +1 -1
  197. package/dist/{stage-DA74aD_V.mjs → stage-CwXpkN-y.mjs} +7 -7
  198. package/dist/{stage-DA74aD_V.mjs.map → stage-CwXpkN-y.mjs.map} +1 -1
  199. package/dist/{stage-ycFS9mVn.cjs → stage-HEZZZJKr.cjs} +7 -7
  200. package/dist/{stage-ycFS9mVn.cjs.map → stage-HEZZZJKr.cjs.map} +1 -1
  201. package/dist/{stages-yA9bhStl.mjs → stages-gG-DGHLb.mjs} +3 -3
  202. package/dist/{stages-yA9bhStl.mjs.map → stages-gG-DGHLb.mjs.map} +1 -1
  203. package/dist/{stages-B0yOikgt.cjs → stages-rzyes8Ew.cjs} +3 -3
  204. package/dist/{stages-B0yOikgt.cjs.map → stages-rzyes8Ew.cjs.map} +1 -1
  205. package/dist/target/public.cjs +1 -1
  206. package/dist/target/public.d.cts +2 -2
  207. package/dist/target/public.d.mts +2 -2
  208. package/dist/target/public.mjs +1 -1
  209. package/dist/{terminal-RxfbNUwl.cjs → terminal-8AA2uK0U.cjs} +2 -2
  210. package/dist/{terminal-RxfbNUwl.cjs.map → terminal-8AA2uK0U.cjs.map} +1 -1
  211. package/dist/{terminal-DX22WLvT.mjs → terminal-BqqGsi0O.mjs} +2 -2
  212. package/dist/{terminal-DX22WLvT.mjs.map → terminal-BqqGsi0O.mjs.map} +1 -1
  213. package/dist/{types-Dk--xVOT.d.cts → types-BXboFkn9.d.mts} +56 -3
  214. package/dist/types-BXboFkn9.d.mts.map +1 -0
  215. package/dist/{types-DwQYpd_0.d.mts → types-BsfPHUFZ.d.cts} +57 -2
  216. package/dist/types-BsfPHUFZ.d.cts.map +1 -0
  217. package/dist/workspace/index.cjs +1 -1
  218. package/dist/workspace/index.d.cts +2 -2
  219. package/dist/workspace/index.d.mts +2 -2
  220. package/dist/workspace/index.mjs +1 -1
  221. package/dist/{workspace-DFGsrjGD.mjs → workspace-D5Vm2qcy.mjs} +165 -9
  222. package/dist/workspace-D5Vm2qcy.mjs.map +1 -0
  223. package/dist/{workspace-1szcahj9.cjs → workspace-VhzmoxOv.cjs} +224 -8
  224. package/dist/workspace-VhzmoxOv.cjs.map +1 -0
  225. package/package.json +15 -15
  226. package/dist/aws-8QQ8n-t6.mjs.map +0 -1
  227. package/dist/aws-DY9NqQIT.cjs.map +0 -1
  228. package/dist/compose-CnXZAYNM.mjs.map +0 -1
  229. package/dist/compose-D7gOSspy.cjs.map +0 -1
  230. package/dist/credentials-CjFtNBVH.mjs.map +0 -1
  231. package/dist/credentials-DwIRfeBL.cjs.map +0 -1
  232. package/dist/deploy-3bsiWJxj.cjs.map +0 -1
  233. package/dist/deploy-BI3fHnyb.mjs.map +0 -1
  234. package/dist/github-BWqQr8q6.cjs.map +0 -1
  235. package/dist/github-Dc1daUbH.mjs.map +0 -1
  236. package/dist/notes-BrBnyZz5.cjs.map +0 -1
  237. package/dist/notes-CIzrdBKE.mjs.map +0 -1
  238. package/dist/s3-BBRkezf7.mjs.map +0 -1
  239. package/dist/s3-CI2QYgXH.cjs.map +0 -1
  240. package/dist/secretsManager-CQORubp9.mjs.map +0 -1
  241. package/dist/secretsManager-RAGcEDX1.cjs.map +0 -1
  242. package/dist/types-Dk--xVOT.d.cts.map +0 -1
  243. package/dist/types-DwQYpd_0.d.mts.map +0 -1
  244. package/dist/workspace-1szcahj9.cjs.map +0 -1
  245. package/dist/workspace-DFGsrjGD.mjs.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deploy-Bb-o2KYC.mjs","names":["errorName","readOr","errorName","readOr","errorName","ensureIdentity"],"sources":["../src/target/secrets.ts","../src/backups/provision.ts","../src/providers/dnsWriter.ts","../src/providers/s3/cdn.ts","../src/providers/s3/events.ts","../src/providers/s3/origins.ts","../src/providers/s3/index.ts","../src/providers/ses/index.ts","../src/providers/ses/smtp.ts","../src/providers/ses/email.ts","../src/providers/registry.ts","../src/providers/index.ts","../src/secrets/stale.ts","../src/deploy/readiness.ts","../src/deploy/orchestrate.ts","../src/deploy/deploy.ts"],"sourcesContent":["/**\n * A stage's secrets as a target sees them: read and written through the\n * stage's own store, and masked in everything the run prints once read.\n */\n\nimport { isReservedStageKey } from '../compose/dnsConfig.js';\nimport { secretsStoreFor } from '../secrets/store';\nimport type { StageSecrets } from '../secrets/types';\nimport type { NormalizedWorkspace } from '../workspace/types';\nimport type { DeploySecrets } from './types';\n\n/**\n * Shorter values are left alone: what a progress line legitimately prints —\n * a port, a stage, an app name — would otherwise come out as `***` wherever\n * a secret happened to share it.\n */\nconst SHORTEST_MASKED = 6;\n\n/** The values a run must never print, and the rewrite that keeps it so. */\nexport class Redactor {\n\tprivate readonly values = new Set<string>();\n\n\tadd(value: string | undefined): void {\n\t\tif (value && value.length >= SHORTEST_MASKED) this.values.add(value);\n\t}\n\n\t/** `text` with every masked value replaced by `***`, longest first. */\n\tredact(text: string): string {\n\t\tif (this.values.size === 0) return text;\n\t\tlet out = text;\n\t\tconst longestFirst = [...this.values].sort((a, b) => b.length - a.length);\n\t\tfor (const value of longestFirst) out = out.replaceAll(value, '***');\n\t\treturn out;\n\t}\n}\n\n/** Every value in `secrets` that is a secret rather than a label. */\nfunction secretValues(secrets: StageSecrets): string[] {\n\treturn [\n\t\tsecrets.seed,\n\t\t// The server's address is not one: it is in public DNS, and the deploy\n\t\t// prints it as the place it is deploying to.\n\t\t...Object.entries(secrets.custom ?? {})\n\t\t\t.filter(([key]) => !isReservedStageKey(key))\n\t\t\t.map(([, value]) => value),\n\t\t...Object.values(secrets.urls ?? {}),\n\t\t...Object.values(secrets.services ?? {}).map(\n\t\t\t(service) => service?.password,\n\t\t),\n\t].filter((value): value is string => typeof value === 'string');\n}\n\n/**\n * The stage's secrets through the store `secrets.store` names for it. Opening\n * one builds a client and makes no request, so a target that never reads them\n * costs nothing.\n */\nexport async function deploySecrets(\n\tworkspace: NormalizedWorkspace,\n\tstage: string,\n\tredactor: Redactor,\n\toptions: { home?: string } = {},\n): Promise<DeploySecrets> {\n\tconst store = await secretsStoreFor(workspace, stage, options);\n\tconst mask = (secrets: StageSecrets) => {\n\t\tfor (const value of secretValues(secrets)) redactor.add(value);\n\t};\n\n\treturn {\n\t\tstore: store.name,\n\t\tasync read() {\n\t\t\tconst secrets = await store.read(stage);\n\t\t\tif (secrets) mask(secrets);\n\t\t\treturn secrets;\n\t\t},\n\t\tasync write(secrets) {\n\t\t\tmask(secrets);\n\t\t\tawait store.write(stage, secrets);\n\t\t},\n\t\tmask: (value) => redactor.add(value),\n\t};\n}\n","/**\n * What a deploy creates for a stage's backups, in its AWS account — before\n * its checks, beside the stage's providers, with the same credentials:\n *\n * - **the project bucket**, if it is not there yet, and its lifecycle — the\n * bucket's own rule and an expiry on every stage's `backups/` prefix,\n * rebuilt from every stage's `deploy.backups` each time, so one stage's\n * deploy never drops another's rule;\n * - **an IAM user**, `gkm-<project>-<stage>-backups` under `/gkm/`, whose\n * inline policy allows `s3:PutObject` under the stage's backups prefix and\n * nothing else — no get, no list, no delete: a server that is broken into\n * can neither read the backups nor wipe them;\n * - **its key**, written into the stage's secrets as `BACKUPS_URL`, rotated\n * by `--rotate-keys` and retired by `--retire-old-keys` like a bucket's.\n *\n * Every deploy's check puts a marker object with the key — the one thing it\n * may do — so a key that does not work stops the deploy, not the 02:00 run.\n */\n\nimport type { ConstructManifest } from '@geekmidas/manifest';\nimport type { ResourceRecord } from '../deploy/StateStore.js';\nimport { GkmError } from '../errors';\nimport {\n\ttype AwsProvisioning,\n\tawsClientConfig,\n\tawsEndpoint,\n\tawsProvisioningCredentials,\n} from '../providers/aws.js';\nimport { stageProvider } from '../providers/config.js';\nimport {\n\tensureAccessKey,\n\tensureIamUser,\n\tissuedJustNow,\n\ttype KeyContext,\n\ts3KeySecret,\n\twhileKeyIsNew,\n} from '../providers/iam.js';\nimport {\n\tcallerAccountId,\n\tensureProjectBucket,\n\tfindProjectBucket,\n\tprojectBucketName,\n\treconcileProjectBucketLifecycle,\n} from '../providers/projectBucket.js';\nimport { S3_PROVISIONING } from '../providers/s3/config.js';\nimport { iamUserName } from '../providers/s3/naming.js';\nimport { appKey } from '../workspace/derive.js';\nimport type { NormalizedWorkspace } from '../workspace/types.js';\nimport {\n\tbackupExpiries,\n\tbackupsPrefix,\n\ttype StageBackupsChoice,\n\tstageBackups,\n} from './config.js';\nimport { BACKUPS_SERVICE, BACKUPS_URL_KEY } from './service.js';\n\n/** What the backups user and its key are recorded and tagged as. */\nexport const BACKUPS_ID = 'backups';\n\n/** The backups user's inline policy's name. */\nexport const BACKUPS_POLICY_NAME = 'gkm-backups';\n\n/** Where a deploy's check puts its marker, under the backups prefix. */\nexport const VERIFY_MARKER = '.gkm-verify';\n\n/** The backups user's name: `gkm-<project>-<stage>-backups`. */\nexport function backupsUserName(scope: string, stage: string): string {\n\treturn iamUserName({ scope, stage, id: BACKUPS_ID });\n}\n\n/**\n * The backups key's policy: put objects under the stage's backups prefix.\n * Uploading in parts is `s3:PutObject` too; aborting one is not, and the\n * bucket's lifecycle does it instead.\n */\nexport function backupsPolicy(bucket: string, prefix: string) {\n\treturn {\n\t\tVersion: '2012-10-17' as const,\n\t\tStatement: [\n\t\t\t{\n\t\t\t\tSid: 'GkmBackupsPut',\n\t\t\t\tEffect: 'Allow' as const,\n\t\t\t\tAction: ['s3:PutObject'],\n\t\t\t\tResource: `arn:aws:s3:::${bucket}/${prefix}/*`,\n\t\t\t},\n\t\t],\n\t};\n}\n\n/** A stage that backs up, and no AWS credentials to create its key with. */\nexport class BackupsCredentialsMissing extends GkmError {\n\tconstructor(readonly stage: string) {\n\t\tsuper(\n\t\t\t`'${stage}' runs Postgres, so the deploy backs it up — and creates the ` +\n\t\t\t\t\"IAM user and key it backs up with in the stage's AWS account — and \" +\n\t\t\t\t`it found no AWS credentials to do it with. On this machine, supply ` +\n\t\t\t\t`${S3_PROVISIONING.describe}. In CI, the step that runs the deploy ` +\n\t\t\t\t\"needs the stage's role (AWS_ROLE_ARN, \" +\n\t\t\t\t`from gkm deploy:github). To take no backups, set deploy.backups.${stage}: false.`,\n\t\t);\n\t\tthis.name = 'BackupsCredentialsMissing';\n\t}\n}\n\n/** No region to create the project bucket in. */\nexport class BackupsRegionRequired extends GkmError {\n\tconstructor(readonly stage: string) {\n\t\tsuper(\n\t\t\t`'${stage}' is backed up to the project bucket, and there is no region ` +\n\t\t\t\t\"to find it in: set state: { provider: 's3', region } in gkm.config.ts, \" +\n\t\t\t\t'or AWS_REGION.',\n\t\t);\n\t\tthis.name = 'BackupsRegionRequired';\n\t}\n}\n\n/** A bucket construct whose IAM user would be the backups user. */\nexport class BackupsUserNameTaken extends GkmError {\n\tconstructor(\n\t\treadonly stage: string,\n\t\treadonly construct: string,\n\t) {\n\t\tsuper(\n\t\t\t`The bucket '${construct}' is reached on '${stage}' with the IAM user ` +\n\t\t\t\t\"the stage's backups are put with — both are named after 'backups'. \" +\n\t\t\t\t`Rename the construct, or set deploy.backups.${stage}: false.`,\n\t\t);\n\t\tthis.name = 'BackupsUserNameTaken';\n\t}\n}\n\n/** The backups key could not put its marker: backups would fail too. */\nexport class BackupsKeyRefused extends GkmError {\n\tconstructor(\n\t\treadonly stage: string,\n\t\treadonly bucket: string,\n\t\treadonly key: string,\n\t\treadonly said: string,\n\t) {\n\t\tsuper(\n\t\t\t`The ${BACKUPS_URL_KEY} key of '${stage}' could not put s3://${bucket}/${key} ` +\n\t\t\t\t`(${said}), so its backups would fail the same way. Run the deploy ` +\n\t\t\t\t\"with the stage's AWS credentials to put its user and policy back, or \" +\n\t\t\t\t'--rotate-keys to issue it a new key.',\n\t\t);\n\t\tthis.name = 'BackupsKeyRefused';\n\t}\n}\n\n/**\n * The region the project bucket is in: the state's, when it is kept in S3,\n * the secrets store's when that is in AWS, the credential's, `AWS_REGION`.\n */\nexport function backupsRegion(\n\tworkspace: Pick<NormalizedWorkspace, 'state' | 'secrets'>,\n\tstage: string,\n\tcredential?: { region?: string },\n\tenv: NodeJS.ProcessEnv = process.env,\n): string {\n\tconst state = workspace.state;\n\tconst store = workspace.secrets?.store;\n\tconst region =\n\t\t(state && 'region' in state ? (state.region as string) : undefined) ??\n\t\t(store && typeof store === 'object' && 'region' in store\n\t\t\t? (store as { region?: string }).region\n\t\t\t: undefined) ??\n\t\tcredential?.region ??\n\t\tenv.AWS_REGION ??\n\t\tenv.AWS_DEFAULT_REGION;\n\tif (!region) throw new BackupsRegionRequired(stage);\n\treturn region;\n}\n\nexport interface EnsureBackupsInput {\n\tworkspace: NormalizedWorkspace;\n\tmanifest: ConstructManifest;\n\tstage: string;\n\t/** Everything creating the user and key reads — the providers' own. */\n\tctx: KeyContext;\n\tprofile?: string;\n\thome?: string;\n\tenv?: NodeJS.ProcessEnv;\n}\n\n/**\n * The stage's backups bucket, lifecycle, user and key — or, for a stage set\n * to `false`, a notice and nothing else. Returns what the stage's backups\n * came to.\n *\n * @throws {BackupsCredentialsMissing} when there are no AWS credentials\n */\nexport async function ensureStageBackups(\n\tinput: EnsureBackupsInput,\n): Promise<StageBackupsChoice> {\n\tconst { workspace, manifest, stage, ctx } = input;\n\tconst env = input.env ?? process.env;\n\tconst choice = stageBackups(workspace, manifest, stage);\n\tif (choice.mode === 'disabled') {\n\t\tctx.log(\n\t\t\t`💾 backups: off — deploy.backups.${stage} is false. Nothing is backed up, and nothing already backed up is deleted.`,\n\t\t);\n\t\treturn choice;\n\t}\n\tif (choice.mode !== 'on') return choice;\n\n\tconst objects = stageProvider(workspace, 'objects', stage);\n\tif (objects.mode === 'provider') {\n\t\tconst clash = Object.entries(manifest).find(\n\t\t\t([id, d]) => d.kind === 'objects' && appKey(id) === BACKUPS_ID,\n\t\t);\n\t\tif (clash) throw new BackupsUserNameTaken(stage, clash[0]);\n\t}\n\n\tconst credential = await awsProvisioningCredentials({\n\t\tstage,\n\t\tenv,\n\t\t...(input.profile ? { profile: input.profile } : {}),\n\t\t...(input.home ? { home: input.home } : {}),\n\t});\n\tif (!credential) throw new BackupsCredentialsMissing(stage);\n\n\tconst region = backupsRegion(workspace, stage, credential, env);\n\tconst clients = await backupsClients(credential, region, env);\n\tconst bucket = projectBucketName(\n\t\tctx.identity.scope,\n\t\tawait callerAccountId(clients.sts),\n\t);\n\tconst prefix = backupsPrefix(workspace, stage);\n\tconst expiries = backupExpiries(workspace, manifest);\n\n\tctx.log(\n\t\t`💾 backups: ${choice.backups.describe}, to s3://${bucket}/${prefix}/${ctx.dryRun ? ' (dry run)' : ''}`,\n\t);\n\n\t// The bucket, and every stage's expiry on it.\n\tif ((await findProjectBucket(clients.s3, bucket)) === 'none') {\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: BACKUPS_SERVICE,\n\t\t\t\tresource: `bucket ${bucket}`,\n\t\t\t\tchange: `create in ${region}, expiring each stage's backups`,\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tawait ensureProjectBucket(clients.s3, {\n\t\t\t\t\tbucket,\n\t\t\t\t\tregion,\n\t\t\t\t\tidentity: ctx.identity,\n\t\t\t\t\texpiries,\n\t\t\t\t});\n\t\t\t},\n\t\t);\n\t} else if (\n\t\tawait reconcileProjectBucketLifecycle(clients.s3, bucket, expiries, {\n\t\t\tdryRun: true,\n\t\t})\n\t) {\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: BACKUPS_SERVICE,\n\t\t\t\tresource: `bucket ${bucket}`,\n\t\t\t\tchange: `lifecycle: ${expiries.map((e) => `${e.prefix} after ${e.days} days`).join(', ')}`,\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tawait reconcileProjectBucketLifecycle(clients.s3, bucket, expiries);\n\t\t\t},\n\t\t);\n\t}\n\n\tconst user = await ensureIamUser(ctx, clients, {\n\t\tid: BACKUPS_ID,\n\t\tuser: backupsUserName(ctx.identity.scope, stage),\n\t\tpolicyName: BACKUPS_POLICY_NAME,\n\t\tpolicy: backupsPolicy(bucket, prefix),\n\t\tdescribe: `put objects under ${prefix}/ only`,\n\t});\n\tawait ensureAccessKey(ctx, clients, {\n\t\tid: BACKUPS_ID,\n\t\tsecret: s3KeySecret(ctx, clients, { bucket, urlKey: BACKUPS_URL_KEY }),\n\t\t...user,\n\t});\n\treturn choice;\n}\n\n/** The clients a backups run creates through. */\nasync function backupsClients(\n\tcredential: AwsProvisioning,\n\tregion: string,\n\tenv: NodeJS.ProcessEnv,\n) {\n\tconst S3 = await import('@aws-sdk/client-s3');\n\tconst IAM = await import('@aws-sdk/client-iam');\n\tconst { STSClient } = await import('@aws-sdk/client-sts');\n\tconst config = await awsClientConfig(credential, region);\n\tconst endpoint = awsEndpoint(env, 'S3');\n\treturn {\n\t\tIAM,\n\t\tiam: new IAM.IAMClient(config),\n\t\ts3: new S3.S3Client({\n\t\t\t...config,\n\t\t\t...(endpoint ? { endpoint, forcePathStyle: true } : {}),\n\t\t}),\n\t\tsts: new STSClient(config),\n\t\tregion,\n\t\t...(endpoint ? { endpoint } : {}),\n\t};\n}\n\nexport interface VerifyBackupsInput {\n\tworkspace: NormalizedWorkspace;\n\tmanifest: ConstructManifest;\n\tstage: string;\n\tsecrets: Readonly<Record<string, string>>;\n\tresources?: Readonly<Record<string, ResourceRecord>>;\n\tlog?: (line: string) => void;\n}\n\n/**\n * The stage's backups key puts its marker under the prefix — the one thing\n * it may do. A key issued by this deploy is waited for, as a bucket's is.\n * Returns whether there was a key to check.\n *\n * @throws {BackupsKeyRefused}\n */\nexport async function verifyStageBackups(\n\tinput: VerifyBackupsInput,\n): Promise<boolean> {\n\tconst { stage } = input;\n\tif (stageBackups(input.workspace, input.manifest, stage).mode !== 'on') {\n\t\treturn false;\n\t}\n\tconst url = input.secrets[BACKUPS_URL_KEY];\n\tif (!url) return false;\n\tconst s3Url = await import('@geekmidas/storage/s3-url');\n\tconst address = s3Url.parse(url);\n\tif (!address.accessKeyId || !address.secretAccessKey) return false;\n\n\tconst S3 = await import('@aws-sdk/client-s3');\n\tconst client = new S3.S3Client({\n\t\tregion: address.region ?? 'us-east-1',\n\t\tcredentials: {\n\t\t\taccessKeyId: address.accessKeyId,\n\t\t\tsecretAccessKey: address.secretAccessKey,\n\t\t},\n\t\t...(address.endpoint ? { endpoint: address.endpoint } : {}),\n\t\t...(address.forcePathStyle ? { forcePathStyle: true } : {}),\n\t});\n\tconst Key = `${backupsPrefix(input.workspace, stage)}/${VERIFY_MARKER}`;\n\tconst put = async (): Promise<{\n\t\tok: boolean;\n\t\tstatus?: number;\n\t\tsaid: string;\n\t}> => {\n\t\ttry {\n\t\t\tawait client.send(\n\t\t\t\tnew S3.PutObjectCommand({\n\t\t\t\t\tBucket: address.bucket,\n\t\t\t\t\tKey,\n\t\t\t\t\tBody: new Date().toISOString(),\n\t\t\t\t\tContentType: 'text/plain',\n\t\t\t\t}),\n\t\t\t);\n\t\t\treturn { ok: true, said: '' };\n\t\t} catch (error) {\n\t\t\tconst e = error as {\n\t\t\t\tname?: string;\n\t\t\t\tmessage?: string;\n\t\t\t\t$metadata?: { httpStatusCode?: number };\n\t\t\t};\n\t\t\tconst status = e.$metadata?.httpStatusCode;\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\t...(status !== undefined ? { status } : {}),\n\t\t\t\tsaid: `HTTP ${status ?? 'no response'} ${e.name ?? 'Error'}: ${e.message ?? ''}`,\n\t\t\t};\n\t\t}\n\t};\n\ttry {\n\t\tconst { answer } = await whileKeyIsNew(await put(), {\n\t\t\tissuedAt: issuedJustNow(input.resources, BACKUPS_ID, address.accessKeyId),\n\t\t\trefused: (answer) => !answer.ok && answer.status === 403,\n\t\t\task: put,\n\t\t\tonWait: () =>\n\t\t\t\tinput.log?.(\n\t\t\t\t\t` backups: waiting for the new key ${address.accessKeyId} to become active…`,\n\t\t\t\t),\n\t\t});\n\t\tif (!answer.ok) {\n\t\t\tthrow new BackupsKeyRefused(stage, address.bucket, Key, answer.said);\n\t\t}\n\t\treturn true;\n\t} finally {\n\t\tclient.destroy();\n\t}\n}\n","/**\n * The DNS records a provider asks for — a certificate's validation record, a\n * CDN's name, a domain's MX — written through the stage's own DNS providers.\n *\n * The same machinery the deploy's DNS step uses for the stack's hosts\n * (`settleStackDns`), handed a plan of CNAMEs and MX records instead of the\n * server's addresses — an MX's value in its zone-file form,\n * `<priority> <host>`, which every DNS provider reads and writes: a record holding its value is left alone, one with another value\n * is replaced and printed, and what is written is read back. A host under no\n * domain in `dns`, or under a `manual` one, is printed for the person to\n * create and reported back, so the provider can say what it is waiting on.\n */\n\nimport {\n\ttype DomainDnsPlan,\n\tdnsCredentials,\n\ttype PlannedDnsRecord,\n\trecordTtl,\n\tsettleStackDns,\n} from '../compose/dns.js';\nimport type { CredentialProvider } from '../deploy/credentials.js';\nimport {\n\ttype DnsProvider,\n\tmxValue,\n} from '../target/dokploy/dns/DnsProvider.js';\nimport {\n\textractSubdomain,\n\tfindRootDomain,\n\tnormalizeDnsConfig,\n} from '../target/dokploy/dns/index.js';\nimport type {\n\tDnsProvider as DnsProviderConfig,\n\tNormalizedWorkspace,\n} from '../workspace/types.js';\nimport type { ProviderDnsRecord, ProviderDnsResult } from './types.js';\n\nexport interface ProviderDnsOptions {\n\tworkspace: Pick<NormalizedWorkspace, 'dns'>;\n\tstage: string;\n\tdryRun: boolean;\n\t/** Where the DNS providers' tokens come from. */\n\tcredentials: CredentialProvider;\n\t/** The provider for a domain — `createDnsProvider` by default. */\n\tproviderFor?: (config: DnsProviderConfig) => Promise<DnsProvider | null>;\n\tlog: (line: string) => void;\n}\n\nexport async function writeProviderDns(\n\trecords: readonly ProviderDnsRecord[],\n\toptions: ProviderDnsOptions,\n): Promise<ProviderDnsResult> {\n\tconst { log } = options;\n\tconst dns = options.workspace.dns\n\t\t? normalizeDnsConfig(options.workspace.dns)\n\t\t: {};\n\tconst manual: string[] = [];\n\tconst domains = new Map<string, DomainDnsPlan>();\n\n\tfor (const record of records) {\n\t\tconst host = record.host.toLowerCase().replace(/\\.$/, '');\n\t\tconst target = record.value.replace(/\\.$/, '');\n\t\tconst value =\n\t\t\trecord.type === 'MX' ? mxValue(record.priority, target) : target;\n\t\tconst domain = findRootDomain(host, dns);\n\t\tif (!domain) {\n\t\t\tlog(` create at its DNS host: ${host} ${record.type} ${value}`);\n\t\t\tmanual.push(host);\n\t\t\tcontinue;\n\t\t}\n\t\tconst config = dns[domain]!;\n\t\tconst plan = domains.get(domain) ?? {\n\t\t\tdomain,\n\t\t\tprovider:\n\t\t\t\ttypeof config.provider === 'string' ? config.provider : 'custom',\n\t\t\tconfig,\n\t\t\trecords: [],\n\t\t};\n\t\tconst planned: PlannedDnsRecord = {\n\t\t\thost,\n\t\t\tdomain,\n\t\t\tname: extractSubdomain(host, domain),\n\t\t\ttype: record.type,\n\t\t\tvalue,\n\t\t\tttl: recordTtl(config),\n\t\t};\n\t\tplan.records.push(planned);\n\t\tdomains.set(domain, plan);\n\t}\n\tif (domains.size === 0) return { manual };\n\n\tconst plans = [...domains.values()];\n\tconst tokens = await dnsCredentials(\n\t\toptions.stage,\n\t\tplans.map(({ domain, config }) => ({ domain, config })),\n\t\toptions.credentials,\n\t);\n\tconst applied = await settleStackDns(\n\t\t{\n\t\t\tstage: options.stage,\n\t\t\t// No address: these records are CNAMEs and MX, never the server's.\n\t\t\tserver: { ipv4: '' },\n\t\t\tdomains: plans,\n\t\t\tuncovered: [],\n\t\t},\n\t\t{\n\t\t\t...(options.dryRun ? { dryRun: true } : { confirm: true }),\n\t\t\tlog,\n\t\t\tcredentials: {\n\t\t\t\t...(tokens.godaddy ? { credential: tokens.godaddy } : {}),\n\t\t\t\tlog,\n\t\t\t},\n\t\t\t...(tokens.hostinger ? { hostinger: tokens.hostinger } : {}),\n\t\t\t...(options.providerFor ? { providerFor: options.providerFor } : {}),\n\t\t},\n\t);\n\n\t// A `manual` domain's records were printed, not written.\n\tfor (const change of applied.changes) {\n\t\tif (change.action !== 'manual') continue;\n\t\tconst host = plans\n\t\t\t.find((p) => p.domain === change.domain)\n\t\t\t?.records.find((r) => r.name === change.name)?.host;\n\t\tif (host) manual.push(host);\n\t}\n\treturn { manual: [...new Set(manual)].sort() };\n}\n","/**\n * A file server over an `s3` bucket, served through a CDN.\n *\n * Without one a file server answers on the bucket's regional endpoint: every\n * reader, wherever they are, fetches from one region, on an `amazonaws.com`\n * host. With one — the default — each file server gets, on the stage:\n *\n * - **a certificate** for `<fileserver>.<domain>`, from ACM in us-east-1 (the\n * one region CloudFront reads certificates from), validated by a CNAME the\n * deploy writes through the stage's DNS providers and keeps, because ACM\n * renews against the same record;\n * - **an origin access control**, which signs CloudFront's requests to the\n * bucket, and a bucket policy statement granting exactly the stage's\n * distributions `s3:GetObject` on exactly the open prefixes (see\n * `bucketPolicy`) — the bucket itself is never public;\n * - **response headers policies**: CORS for the stage's sites (the same\n * derivation the bucket's own CORS rule uses), and, for the paths the file\n * server calls `immutable`, `Cache-Control: public, max-age=31536000,\n * immutable` over whatever the object was stored with;\n * - **a distribution** on that name: HTTPS only, compressed, GET/HEAD/OPTIONS,\n * cached with AWS's managed `CachingOptimized` policy — keyed on the path\n * alone, so no query string or cookie fragments the cache;\n * - **a CNAME** from the name to the distribution, through the same DNS\n * providers.\n *\n * Open paths only: a key outside them is refused to CloudFront by the bucket\n * policy, so the edge answers it 403, and a signed read goes to the bucket's\n * own host as before (`signedUrl` is an S3 presign).\n *\n * Every resource is recorded in the stage's state and reused, found by its\n * name when a run died before recording it, and put back when it drifted.\n * Nothing is ever deleted.\n */\n\nimport type {\n\tDistributionConfig,\n\tResponseHeadersPolicyConfig,\n} from '@aws-sdk/client-cloudfront';\nimport { GkmError } from '../../errors';\nimport { appKey } from '../../workspace/derive.js';\nimport type { S3ObjectsConfig } from '../../workspace/types.js';\nimport { type AwsProvisioning, awsClientConfig, awsEndpoint } from '../aws.js';\nimport { identityTags } from '../iam.js';\nimport {\n\tnewOriginSecret,\n\tORIGIN_VERIFY_HEADER,\n\toriginSecretKey,\n\tprocessorOriginHost,\n} from '../processorOrigin.js';\nimport type { EnsureContext, StateEntry } from '../types.js';\n\ntype CloudFrontModule = typeof import('@aws-sdk/client-cloudfront');\ntype AcmModule = typeof import('@aws-sdk/client-acm');\n\ntype Ctx = EnsureContext<S3ObjectsConfig, AwsProvisioning>;\n\n/** AWS's managed `CachingOptimized` cache policy: path-keyed, gzip and brotli. */\nexport const CACHING_OPTIMIZED = '658327ea-f89d-4fab-a63d-7e88639e58f6';\n\n/** What an immutable path is served with. */\nexport const IMMUTABLE_CACHE_CONTROL = 'public, max-age=31536000, immutable';\n\n/** The only region CloudFront reads a certificate from. */\nconst CDN_REGION = 'us-east-1';\n\n/** The distribution's bucket origin. */\nconst ORIGIN_ID = 'bucket';\n\n/** Its processors' origin: the stack's processor unit, at a host of its own. */\nconst PROCESSORS_ORIGIN_ID = 'processors';\n\n/**\n * AWS's managed `CachingOptimizedForUncompressedObjects`: keyed on the path\n * alone, and nothing re-compressed — a derivative is an image, already\n * compressed, and its width is in the path, so nothing else may fragment it.\n */\nexport const CACHING_OPTIMIZED_UNCOMPRESSED =\n\t'b2884449-e4de-46a7-ac36-70bc7f1ddd6d';\n\n/** A file server, as the CDN serves it. */\nexport interface CdnServer {\n\tid: string;\n\topen: readonly string[];\n\timmutable: readonly string[];\n\t/** Its processors' prefixes — `/i/` — each sent to the processor unit. */\n\tprocessors?: readonly string[];\n}\n\n// ---------------------------------------------------------------------------\n// Errors\n// ---------------------------------------------------------------------------\n\n/** A CDN is a name on the stage's domain, and the stage has none. */\nexport class FileServerCdnNeedsDomain extends GkmError {\n\tconstructor(\n\t\treadonly server: string,\n\t\treadonly stage: string,\n\t) {\n\t\tsuper(\n\t\t\t`'${server}' is served through a CDN on '${stage}', which answers on ` +\n\t\t\t\t`<fileserver>.<domain> — and '${stage}' has no domain. Set ` +\n\t\t\t\t`domains.${stage} in gkm.config.ts, or set deploy.objects.${stage}.cdn ` +\n\t\t\t\t\"to false to serve it from the bucket's own endpoint.\",\n\t\t);\n\t\tthis.name = 'FileServerCdnNeedsDomain';\n\t}\n}\n\n/**\n * The certificate is waiting on its validation record: one the person has to\n * create, or one that has not propagated yet. Everything up to it is\n * recorded, so the next deploy carries on from here.\n */\nexport class CdnCertificatePending extends GkmError {\n\tconstructor(\n\t\treadonly server: string,\n\t\treadonly host: string,\n\t\treadonly record: { name: string; value: string },\n\t\treadonly reason: 'manual' | 'timeout',\n\t) {\n\t\tsuper(\n\t\t\t(reason === 'manual'\n\t\t\t\t? `The certificate for ${host} ('${server}') is validated by a DNS ` +\n\t\t\t\t\t'record no DNS provider in gkm.config.ts can write. Create it at ' +\n\t\t\t\t\t\"the domain's DNS host:\\n\"\n\t\t\t\t: `The certificate for ${host} ('${server}') is not issued yet: ACM ` +\n\t\t\t\t\t'has not seen its validation record. It is written; DNS can take ' +\n\t\t\t\t\t'a while to answer with it. Check it resolves:\\n') +\n\t\t\t\t` ${record.name} CNAME ${record.value}\\n` +\n\t\t\t\t'then deploy again — the certificate and everything before it are ' +\n\t\t\t\t'recorded, and the deploy carries on from there.',\n\t\t);\n\t\tthis.name = 'CdnCertificatePending';\n\t}\n}\n\n/** ACM gave up on the certificate. */\nexport class CdnCertificateFailed extends GkmError {\n\tconstructor(\n\t\treadonly server: string,\n\t\treadonly host: string,\n\t\treadonly status: string,\n\t\treadonly reason: string | undefined,\n\t) {\n\t\tsuper(\n\t\t\t`ACM did not issue the certificate for ${host} ('${server}'): ${status}` +\n\t\t\t\t(reason ? ` (${reason})` : '') +\n\t\t\t\t'. A CAA record on the domain that does not allow amazon.com, or a ' +\n\t\t\t\t'validation record removed before it was checked, are the usual ' +\n\t\t\t\t'causes. Fix it and deploy again: a new certificate is requested.',\n\t\t);\n\t\tthis.name = 'CdnCertificateFailed';\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Names and documents — pure\n// ---------------------------------------------------------------------------\n\n/** Where a file server answers: `<fileserver>.<domain>`. */\nexport function cdnHost(serverId: string, domain: string): string {\n\treturn `${appKey(serverId)}.${domain}`.toLowerCase();\n}\n\n/**\n * The name the stage's CDN resources go by, within CloudFront's limits:\n * letters, digits, `-` and `_`, and at most 64 characters (an origin access\n * control's). Kept whole where it fits; otherwise shortened with a hash of\n * the whole, so two long names never meet.\n */\nexport function cdnName(\n\tscope: string,\n\tstage: string,\n\tserverId: string,\n): string {\n\tconst whole = `gkm-${scope}-${stage}-${appKey(serverId)}`.replace(\n\t\t/[^A-Za-z0-9_-]/g,\n\t\t'-',\n\t);\n\tif (whole.length <= 56) return whole;\n\treturn `${whole.slice(0, 47)}-${hash(whole)}`;\n}\n\nfunction hash(value: string): string {\n\tlet h = 2166136261;\n\tfor (let i = 0; i < value.length; i++) {\n\t\th ^= value.charCodeAt(i);\n\t\th = Math.imul(h, 16777619);\n\t}\n\treturn (h >>> 0).toString(36).padStart(8, '0').slice(0, 8);\n}\n\n/**\n * An open or immutable glob as a CloudFront path pattern. CloudFront's `*`\n * crosses `/`, so a single star is as wide as a double one — the bucket\n * policy reads it the same way, and the construct's own check is the\n * stricter of the three.\n */\nexport function pathPattern(glob: string): string {\n\treturn glob.replace(/^\\/+/, '').replace(/\\*\\*/g, '*');\n}\n\n/** A CloudFront list: its `Quantity` and, when it has some, its `Items`. */\nfunction list<T>(items: readonly T[]): { Quantity: number; Items?: T[] } {\n\treturn items.length > 0\n\t\t? { Quantity: items.length, Items: [...items] }\n\t\t: { Quantity: 0 };\n}\n\n/** One of the stage's response headers policies. */\nexport function headersPolicyConfig(input: {\n\tname: string;\n\tcomment: string;\n\t/** The stage's sites, for CORS; none, no CORS. */\n\torigins: readonly string[];\n\timmutable: boolean;\n}): ResponseHeadersPolicyConfig {\n\treturn {\n\t\tName: input.name,\n\t\tComment: input.comment,\n\t\t...(input.origins.length > 0\n\t\t\t? {\n\t\t\t\t\tCorsConfig: {\n\t\t\t\t\t\tAccessControlAllowOrigins: list([...input.origins].sort()) as {\n\t\t\t\t\t\t\tQuantity: number;\n\t\t\t\t\t\t\tItems: string[];\n\t\t\t\t\t\t},\n\t\t\t\t\t\tAccessControlAllowHeaders: { Quantity: 1, Items: ['*'] },\n\t\t\t\t\t\tAccessControlAllowMethods: {\n\t\t\t\t\t\t\tQuantity: 3,\n\t\t\t\t\t\t\tItems: ['GET', 'HEAD', 'OPTIONS'],\n\t\t\t\t\t\t},\n\t\t\t\t\t\tAccessControlAllowCredentials: false,\n\t\t\t\t\t\tAccessControlMaxAgeSec: 86400,\n\t\t\t\t\t\tOriginOverride: false,\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: {}),\n\t\t...(input.immutable\n\t\t\t? {\n\t\t\t\t\tCustomHeadersConfig: {\n\t\t\t\t\t\tQuantity: 1,\n\t\t\t\t\t\tItems: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tHeader: 'Cache-Control',\n\t\t\t\t\t\t\t\tValue: IMMUTABLE_CACHE_CONTROL,\n\t\t\t\t\t\t\t\t// Over the origin: objects already in the bucket were\n\t\t\t\t\t\t\t\t// stored with whatever they were stored with, or nothing.\n\t\t\t\t\t\t\t\tOverride: true,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: {}),\n\t};\n}\n\n/** The parts of a distribution's config this provider owns. */\nexport function distributionConfig(input: {\n\tcomment: string;\n\tcallerReference: string;\n\thost: string;\n\tcertificateArn: string;\n\t/** The bucket's regional domain. */\n\tbucketDomain: string;\n\toriginAccessControlId: string;\n\t/** For every path: CORS. */\n\tcorsPolicyId?: string;\n\t/** For the immutable paths: CORS and the forever header. */\n\timmutablePolicyId?: string;\n\timmutable: readonly string[];\n\t/**\n\t * The file server's processors: each prefix sent to the processor unit\n\t * at `host`, with the secret header its edge checks.\n\t */\n\tprocessors?: { prefixes: readonly string[]; host: string; secret: string };\n}): DistributionConfig {\n\tconst behaviour = (policy?: string) => ({\n\t\tTargetOriginId: ORIGIN_ID,\n\t\tViewerProtocolPolicy: 'redirect-to-https' as const,\n\t\tAllowedMethods: {\n\t\t\tQuantity: 3,\n\t\t\tItems: ['GET' as const, 'HEAD' as const, 'OPTIONS' as const],\n\t\t\tCachedMethods: { Quantity: 2, Items: ['GET' as const, 'HEAD' as const] },\n\t\t},\n\t\tCompress: true,\n\t\tCachePolicyId: CACHING_OPTIMIZED,\n\t\t...(policy ? { ResponseHeadersPolicyId: policy } : {}),\n\t\t...BEHAVIOUR_DEFAULTS,\n\t});\n\tconst patterns = [...new Set(input.immutable.map(pathPattern))].sort();\n\tconst processors = input.processors;\n\tconst processorPatterns = processors\n\t\t? [...new Set(processors.prefixes.map((p) => `${pathPattern(p)}*`))].sort()\n\t\t: [];\n\treturn {\n\t\tCallerReference: input.callerReference,\n\t\tComment: input.comment,\n\t\tEnabled: true,\n\t\tAliases: { Quantity: 1, Items: [input.host] },\n\t\tOrigins: list([\n\t\t\t{\n\t\t\t\tId: ORIGIN_ID,\n\t\t\t\tDomainName: input.bucketDomain,\n\t\t\t\tOriginPath: '',\n\t\t\t\tS3OriginConfig: { OriginAccessIdentity: '' },\n\t\t\t\tOriginAccessControlId: input.originAccessControlId,\n\t\t\t\tCustomHeaders: { Quantity: 0 },\n\t\t\t\t...ORIGIN_DEFAULTS,\n\t\t\t},\n\t\t\t...(processors\n\t\t\t\t? [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tId: PROCESSORS_ORIGIN_ID,\n\t\t\t\t\t\t\tDomainName: processors.host,\n\t\t\t\t\t\t\tOriginPath: '',\n\t\t\t\t\t\t\tCustomOriginConfig: {\n\t\t\t\t\t\t\t\tHTTPPort: 80,\n\t\t\t\t\t\t\t\tHTTPSPort: 443,\n\t\t\t\t\t\t\t\tOriginProtocolPolicy: 'https-only',\n\t\t\t\t\t\t\t\tOriginSslProtocols: { Quantity: 1, Items: ['TLSv1.2'] },\n\t\t\t\t\t\t\t\tOriginReadTimeout: 30,\n\t\t\t\t\t\t\t\tOriginKeepaliveTimeout: 5,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t// What the processor unit's edge checks: without it, its\n\t\t\t\t\t\t\t// public host refuses the request.\n\t\t\t\t\t\t\t...ORIGIN_DEFAULTS,\n\t\t\t\t\t\t\tCustomHeaders: {\n\t\t\t\t\t\t\t\tQuantity: 1,\n\t\t\t\t\t\t\t\tItems: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tHeaderName: ORIGIN_VERIFY_HEADER,\n\t\t\t\t\t\t\t\t\t\tHeaderValue: processors.secret,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t]\n\t\t\t\t: []),\n\t\t]),\n\t\tDefaultCacheBehavior: behaviour(input.corsPolicyId),\n\t\t// The processors' first: CloudFront takes the first pattern that\n\t\t// matches, and an immutable `*` would otherwise answer from the bucket.\n\t\tCacheBehaviors: list([\n\t\t\t...processorPatterns.map((PathPattern) => ({\n\t\t\t\t...behaviour(input.corsPolicyId),\n\t\t\t\tTargetOriginId: PROCESSORS_ORIGIN_ID,\n\t\t\t\tCompress: false,\n\t\t\t\tCachePolicyId: CACHING_OPTIMIZED_UNCOMPRESSED,\n\t\t\t\tPathPattern,\n\t\t\t})),\n\t\t\t...patterns.map((PathPattern) => ({\n\t\t\t\t...behaviour(input.immutablePolicyId),\n\t\t\t\tPathPattern,\n\t\t\t})),\n\t\t]),\n\t\tViewerCertificate: {\n\t\t\tACMCertificateArn: input.certificateArn,\n\t\t\tSSLSupportMethod: 'sni-only',\n\t\t\tMinimumProtocolVersion: 'TLSv1.2_2021',\n\t\t\tCloudFrontDefaultCertificate: false,\n\t\t},\n\t\tHttpVersion: 'http2and3',\n\t\tIsIPV6Enabled: true,\n\t\tDefaultRootObject: '',\n\t} as DistributionConfig;\n}\n\n/**\n * What CloudFront fills in a behaviour, at its own defaults. Optional when a\n * distribution is created, required when it is updated — an update replaces\n * each behaviour and origin whole, and one without them is refused (\"The\n * parameter SmoothStreaming flag is missing\", \"The 'OriginCustomHeaders'\n * field is missing\"). At the defaults, `holds` still matches a distribution\n * CloudFront filled in itself.\n */\nconst BEHAVIOUR_DEFAULTS = {\n\tSmoothStreaming: false,\n\tFieldLevelEncryptionId: '',\n\tTrustedSigners: { Enabled: false, Quantity: 0 },\n\tTrustedKeyGroups: { Enabled: false, Quantity: 0 },\n\tLambdaFunctionAssociations: { Quantity: 0 },\n\tFunctionAssociations: { Quantity: 0 },\n} as const;\n\n/** What CloudFront fills in an origin, at its own defaults: see above. */\nconst ORIGIN_DEFAULTS = {\n\tConnectionAttempts: 3,\n\tConnectionTimeout: 10,\n} as const;\n\n/**\n * Whether `current` already holds everything in `wanted`: each key `wanted`\n * names, recursively, with lists compared as sets. What else `current` holds\n * — logging, a WAF, geo restrictions set by hand — is left alone, and a\n * `CallerReference`, fixed at creation, is never compared.\n */\nexport function holds(\n\tcurrent: unknown,\n\twanted: unknown,\n\tkey?: string,\n): boolean {\n\tif (key === 'CallerReference') return true;\n\tif (Array.isArray(wanted)) {\n\t\treturn (\n\t\t\tArray.isArray(current) &&\n\t\t\tcurrent.length === wanted.length &&\n\t\t\twanted.every((w) => current.some((c) => holds(c, w)))\n\t\t);\n\t}\n\tif (wanted && typeof wanted === 'object') {\n\t\tif (!current || typeof current !== 'object') return false;\n\t\treturn Object.entries(wanted).every(([k, v]) =>\n\t\t\tholds((current as Record<string, unknown>)[k], v, k),\n\t\t);\n\t}\n\treturn current === wanted;\n}\n\n/** Header configs a policy may hold that this provider never sets. */\nconst FOREIGN_HEADER_CONFIGS = [\n\t'CorsConfig',\n\t'CustomHeadersConfig',\n\t'SecurityHeadersConfig',\n\t'RemoveHeadersConfig',\n\t'ServerTimingHeadersConfig',\n] as const;\n\n/** Whether a policy is exactly what is wanted: nothing missing, nothing more. */\nexport function sameHeadersPolicy(\n\tcurrent: ResponseHeadersPolicyConfig | undefined,\n\twanted: ResponseHeadersPolicyConfig,\n): boolean {\n\tif (!current || !holds(current, wanted)) return false;\n\treturn FOREIGN_HEADER_CONFIGS.every((k) => {\n\t\tif (k in wanted) return true;\n\t\tconst extra = current[k] as { Quantity?: number } | undefined;\n\t\treturn !extra || extra.Quantity === 0;\n\t});\n}\n\n// ---------------------------------------------------------------------------\n// Clients\n// ---------------------------------------------------------------------------\n\nexport interface CdnClients {\n\tCF: CloudFrontModule;\n\tACM: AcmModule;\n\tcloudfront: InstanceType<CloudFrontModule['CloudFrontClient']>;\n\tacm: InstanceType<AcmModule['ACMClient']>;\n\t/** How long a certificate is waited on, and how often it is asked. */\n\twait: { timeoutMs: number; intervalMs: number };\n}\n\nexport type CdnClientFactory = (\n\tcredential: AwsProvisioning,\n) => Promise<CdnClients>;\n\nexport const defaultCdnClients: CdnClientFactory = async (credential) => {\n\tconst CF = await import('@aws-sdk/client-cloudfront');\n\tconst ACM = await import('@aws-sdk/client-acm');\n\tconst config = await awsClientConfig(credential, CDN_REGION);\n\tconst cloudfront = awsEndpoint(process.env, 'CLOUDFRONT');\n\tconst acm = awsEndpoint(process.env, 'ACM');\n\treturn {\n\t\tCF,\n\t\tACM,\n\t\tcloudfront: new CF.CloudFrontClient({\n\t\t\t...config,\n\t\t\t...(cloudfront ? { endpoint: cloudfront } : {}),\n\t\t}),\n\t\tacm: new ACM.ACMClient({ ...config, ...(acm ? { endpoint: acm } : {}) }),\n\t\t// ACM issues within minutes of seeing the record; DNS decides how many.\n\t\twait: { timeoutMs: 15 * 60_000, intervalMs: 10_000 },\n\t};\n};\n\nlet makeCdnClients: CdnClientFactory = defaultCdnClients;\n\n/** Replace the client factory — a test intercepting a call. Returns the old one. */\nexport function useCdnClients(factory: CdnClientFactory): CdnClientFactory {\n\tconst previous = makeCdnClients;\n\tmakeCdnClients = factory;\n\treturn previous;\n}\n\n/** What an AWS SDK error is called, whichever field carries it. */\nfunction errorName(error: unknown): string {\n\tconst e = error as { name?: string; Code?: string };\n\treturn e?.Code ?? e?.name ?? '';\n}\n\nasync function readOr<T>(\n\tread: () => Promise<T>,\n\tmissing: readonly string[],\n): Promise<T | undefined> {\n\ttry {\n\t\treturn await read();\n\t} catch (error) {\n\t\tif (missing.includes(errorName(error))) return undefined;\n\t\tthrow error;\n\t}\n}\n\nconst sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));\n\n/** What stands in for an id a dry run would create. */\nconst PLANNED = '(created by this deploy)';\n\n// ---------------------------------------------------------------------------\n// Ensuring\n// ---------------------------------------------------------------------------\n\nexport interface BucketCdnInput {\n\tbucket: string;\n\tregion: string;\n\tservers: readonly CdnServer[];\n\t/** The stage's sites, for CORS. */\n\torigins: readonly string[];\n}\n\nexport interface BucketCdn {\n\t/** Each distribution's ARN — what the bucket policy grants. */\n\tdistributionArns: string[];\n\t/** Each file server's public address, by its id. */\n\turls: Record<string, string>;\n}\n\n/** Each file server over a bucket, behind its own distribution. */\nexport async function ensureBucketCdn(\n\tctx: Ctx,\n\tinput: BucketCdnInput,\n): Promise<BucketCdn> {\n\tconst domain = ctx.workspace.domains?.[ctx.stage];\n\tif (!domain) {\n\t\tthrow new FileServerCdnNeedsDomain(input.servers[0]!.id, ctx.stage);\n\t}\n\tconst clients = await makeCdnClients(ctx.credential);\n\tconst out: BucketCdn = { distributionArns: [], urls: {} };\n\tfor (const server of input.servers) {\n\t\tconst { arn, url } = await ensureServerCdn(ctx, clients, {\n\t\t\t...input,\n\t\t\tserver,\n\t\t\thost: cdnHost(server.id, domain),\n\t\t\tprocessorHost: processorOriginHost(server.id, domain),\n\t\t});\n\t\tout.distributionArns.push(arn);\n\t\tout.urls[server.id] = url;\n\t}\n\treturn out;\n}\n\nasync function ensureServerCdn(\n\tctx: Ctx,\n\tclients: CdnClients,\n\tinput: BucketCdnInput & {\n\t\tserver: CdnServer;\n\t\thost: string;\n\t\tprocessorHost: string;\n\t},\n): Promise<{ arn: string; url: string }> {\n\tconst { server, host } = input;\n\tconst name = cdnName(ctx.identity.scope, ctx.stage, server.id);\n\tconst comment = `gkm ${ctx.identity.key} ${ctx.stage} ${server.id}`.slice(\n\t\t0,\n\t\t128,\n\t);\n\n\tconst certificateArn = await ensureCertificate(ctx, clients, {\n\t\tid: server.id,\n\t\thost,\n\t\tname,\n\t});\n\tconst originAccessControlId = await ensureOriginAccessControl(ctx, clients, {\n\t\tid: server.id,\n\t\tname,\n\t\tbucket: input.bucket,\n\t});\n\tconst corsPolicyId =\n\t\tinput.origins.length > 0\n\t\t\t? await ensureHeadersPolicy(ctx, clients, {\n\t\t\t\t\tid: server.id,\n\t\t\t\t\tkind: 'cors',\n\t\t\t\t\tconfig: headersPolicyConfig({\n\t\t\t\t\t\tname: `${name}-cors`,\n\t\t\t\t\t\tcomment: `${comment}: CORS`,\n\t\t\t\t\t\torigins: input.origins,\n\t\t\t\t\t\timmutable: false,\n\t\t\t\t\t}),\n\t\t\t\t})\n\t\t\t: undefined;\n\tconst immutablePolicyId =\n\t\tserver.immutable.length > 0\n\t\t\t? await ensureHeadersPolicy(ctx, clients, {\n\t\t\t\t\tid: server.id,\n\t\t\t\t\tkind: 'immutable',\n\t\t\t\t\tconfig: headersPolicyConfig({\n\t\t\t\t\t\tname: `${name}-immutable`,\n\t\t\t\t\t\tcomment: `${comment}: CORS and Cache-Control immutable`,\n\t\t\t\t\t\torigins: input.origins,\n\t\t\t\t\t\timmutable: true,\n\t\t\t\t\t}),\n\t\t\t\t})\n\t\t\t: undefined;\n\n\tconst processors = server.processors?.length\n\t\t? {\n\t\t\t\tprefixes: server.processors,\n\t\t\t\thost: input.processorHost,\n\t\t\t\tsecret: await ensureOriginSecret(ctx, server.id),\n\t\t\t}\n\t\t: undefined;\n\n\tconst distribution = await ensureDistribution(ctx, clients, {\n\t\tid: server.id,\n\t\tcomment,\n\t\tconfig: distributionConfig({\n\t\t\tcomment,\n\t\t\tcallerReference: name,\n\t\t\thost,\n\t\t\tcertificateArn,\n\t\t\tbucketDomain: `${input.bucket}.s3.${input.region}.amazonaws.com`,\n\t\t\toriginAccessControlId,\n\t\t\t...(corsPolicyId ? { corsPolicyId } : {}),\n\t\t\t...(immutablePolicyId ? { immutablePolicyId } : {}),\n\t\t\timmutable: server.immutable,\n\t\t\t...(processors ? { processors } : {}),\n\t\t}),\n\t});\n\n\tif (distribution.domainName !== PLANNED) {\n\t\tconst written = await ctx.writeDns([\n\t\t\t{ host, type: 'CNAME', value: distribution.domainName },\n\t\t]);\n\t\tif (written.manual.includes(host)) {\n\t\t\tctx.log(\n\t\t\t\t` ${host} answers once that record exists — ${server.id}'s URL is https://${host}`,\n\t\t\t);\n\t\t}\n\t}\n\treturn { arn: distribution.arn, url: `https://${host}` };\n}\n\n/**\n * The secret the CDN sends a file server's processors: the one in the stage's\n * secrets, or a new one written there — read by the stack, whose edge checks\n * it, when it composes.\n */\nasync function ensureOriginSecret(ctx: Ctx, serverId: string): Promise<string> {\n\tconst key = originSecretKey(serverId);\n\tconst kept = ctx.secrets[key];\n\tif (kept) return kept;\n\tconst secret = newOriginSecret();\n\tctx.log(` ${serverId}: a new processor origin secret, kept as ${key}`);\n\tawait ctx.writeSecrets({ [key]: secret });\n\treturn secret;\n}\n\n/** The certificate for `host`: recorded, adopted, or requested — then issued. */\nasync function ensureCertificate(\n\tctx: Ctx,\n\tclients: CdnClients,\n\tinput: { id: string; host: string; name: string },\n): Promise<string> {\n\tconst { ACM, acm } = clients;\n\tconst { id, host } = input;\n\tconst entry: StateEntry = {\n\t\tkey: `acm-certificate:${id}`,\n\t\ttype: 'acm-certificate',\n\t\tdata: { host },\n\t};\n\tconst describe = (arn: string) =>\n\t\treadOr(\n\t\t\t() =>\n\t\t\t\tacm.send(new ACM.DescribeCertificateCommand({ CertificateArn: arn })),\n\t\t\t['ResourceNotFoundException'],\n\t\t).then((out) => out?.Certificate);\n\n\tlet arn: string | undefined;\n\tconst recorded = ctx.state.record(entry.key);\n\tif (recorded?.id && recorded.data?.host === host) {\n\t\tconst found = await describe(recorded.id);\n\t\tconst usable =\n\t\t\tfound &&\n\t\t\tfound.Status !== 'FAILED' &&\n\t\t\tfound.Status !== 'VALIDATION_TIMED_OUT' &&\n\t\t\tfound.Status !== 'REVOKED' &&\n\t\t\tfound.Status !== 'EXPIRED';\n\t\tif (usable) arn = recorded.id;\n\t}\n\tif (!arn) {\n\t\tarn = await findCertificate(clients, host);\n\t\tif (arn) ctx.log(` adopting the certificate for ${host} — ${arn}`);\n\t}\n\tif (!arn) {\n\t\tif (ctx.dryRun) {\n\t\t\tawait ctx.change(\n\t\t\t\t{\n\t\t\t\t\tconstruct: id,\n\t\t\t\t\tresource: `certificate ${host}`,\n\t\t\t\t\tchange: 'request (ACM, us-east-1, DNS-validated)',\n\t\t\t\t},\n\t\t\t\tasync () => {},\n\t\t\t);\n\t\t\treturn PLANNED;\n\t\t}\n\t\tawait ctx.state.pending(entry);\n\t\tconst requested = await acm.send(\n\t\t\tnew ACM.RequestCertificateCommand({\n\t\t\t\tDomainName: host,\n\t\t\t\tValidationMethod: 'DNS',\n\t\t\t\tIdempotencyToken: input.name.replace(/[^A-Za-z0-9]/g, '').slice(0, 32),\n\t\t\t\tTags: identityTags(ctx, id).map((t) => ({\n\t\t\t\t\tKey: t.Key,\n\t\t\t\t\tValue: t.Value,\n\t\t\t\t})),\n\t\t\t}),\n\t\t);\n\t\tarn = requested.CertificateArn!;\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: id,\n\t\t\t\tresource: `certificate ${host}`,\n\t\t\t\tchange: 'request (ACM, us-east-1, DNS-validated)',\n\t\t\t},\n\t\t\tasync () => {},\n\t\t);\n\t}\n\tif (!ctx.dryRun) await ctx.state.ready(entry, arn);\n\n\t// ACM publishes the validation record moments after the request.\n\tlet certificate = await describe(arn);\n\tlet record = certificate?.DomainValidationOptions?.[0]?.ResourceRecord;\n\tfor (let i = 0; !record && i < 15; i++) {\n\t\tawait sleep(Math.min(clients.wait.intervalMs, 2000));\n\t\tcertificate = await describe(arn);\n\t\trecord = certificate?.DomainValidationOptions?.[0]?.ResourceRecord;\n\t}\n\t// Written whether or not it is issued: ACM renews against the same record.\n\tconst validation = record\n\t\t? {\n\t\t\t\tname: record.Name!.replace(/\\.$/, ''),\n\t\t\t\tvalue: record.Value!.replace(/\\.$/, ''),\n\t\t\t}\n\t\t: undefined;\n\tconst written = validation\n\t\t? await ctx.writeDns([\n\t\t\t\t{ host: validation.name, type: 'CNAME', value: validation.value },\n\t\t\t])\n\t\t: { manual: [] };\n\n\tif (certificate?.Status === 'ISSUED' || ctx.dryRun) return arn;\n\tif (validation && written.manual.includes(validation.name.toLowerCase())) {\n\t\tthrow new CdnCertificatePending(id, host, validation, 'manual');\n\t}\n\n\tctx.log(` waiting for ACM to issue the certificate for ${host}…`);\n\tconst deadline = Date.now() + clients.wait.timeoutMs;\n\twhile (\n\t\tcertificate?.Status === 'PENDING_VALIDATION' &&\n\t\tDate.now() < deadline\n\t) {\n\t\tawait sleep(clients.wait.intervalMs);\n\t\tcertificate = await describe(arn);\n\t}\n\tif (certificate?.Status === 'ISSUED') return arn;\n\tif (certificate?.Status === 'PENDING_VALIDATION' || !certificate) {\n\t\tthrow new CdnCertificatePending(\n\t\t\tid,\n\t\t\thost,\n\t\t\tvalidation ?? { name: `(not published yet) ${host}`, value: '' },\n\t\t\t'timeout',\n\t\t);\n\t}\n\tthrow new CdnCertificateFailed(\n\t\tid,\n\t\thost,\n\t\tcertificate.Status ?? 'unknown',\n\t\tcertificate.FailureReason,\n\t);\n}\n\n/** An issued or pending certificate for exactly `host`, if the account has one. */\nasync function findCertificate(\n\tclients: CdnClients,\n\thost: string,\n): Promise<string | undefined> {\n\tconst { ACM, acm } = clients;\n\tlet NextToken: string | undefined;\n\tdo {\n\t\tconst page = await acm.send(\n\t\t\tnew ACM.ListCertificatesCommand({\n\t\t\t\tCertificateStatuses: ['ISSUED', 'PENDING_VALIDATION'],\n\t\t\t\t...(NextToken ? { NextToken } : {}),\n\t\t\t}),\n\t\t);\n\t\tconst match = page.CertificateSummaryList?.find(\n\t\t\t(c) => c.DomainName?.toLowerCase() === host,\n\t\t);\n\t\tif (match?.CertificateArn) return match.CertificateArn;\n\t\tNextToken = page.NextToken;\n\t} while (NextToken);\n\treturn undefined;\n}\n\n/** The origin access control CloudFront signs its bucket requests with. */\nasync function ensureOriginAccessControl(\n\tctx: Ctx,\n\tclients: CdnClients,\n\tinput: { id: string; name: string; bucket: string },\n): Promise<string> {\n\tconst { CF, cloudfront } = clients;\n\tconst entry: StateEntry = {\n\t\tkey: `cloudfront-oac:${input.id}`,\n\t\ttype: 'cloudfront-oac',\n\t\tdata: { name: input.name },\n\t};\n\tconst recorded = ctx.state.record(entry.key)?.id;\n\tif (recorded) {\n\t\tconst found = await readOr(\n\t\t\t() =>\n\t\t\t\tcloudfront.send(new CF.GetOriginAccessControlCommand({ Id: recorded })),\n\t\t\t['NoSuchOriginAccessControl'],\n\t\t);\n\t\tif (found) return recorded;\n\t}\n\n\tlet Marker: string | undefined;\n\tdo {\n\t\tconst page = await cloudfront.send(\n\t\t\tnew CF.ListOriginAccessControlsCommand({ ...(Marker ? { Marker } : {}) }),\n\t\t);\n\t\tconst match = page.OriginAccessControlList?.Items?.find(\n\t\t\t(o) => o.Name === input.name,\n\t\t);\n\t\tif (match?.Id) {\n\t\t\tif (!ctx.dryRun) await ctx.state.ready(entry, match.Id);\n\t\t\treturn match.Id;\n\t\t}\n\t\tMarker = page.OriginAccessControlList?.NextMarker;\n\t} while (Marker);\n\n\tlet id = PLANNED;\n\tawait ctx.change(\n\t\t{\n\t\t\tconstruct: input.id,\n\t\t\tresource: `origin access control ${input.name}`,\n\t\t\tchange: `create (CloudFront signs its reads of ${input.bucket})`,\n\t\t},\n\t\tasync () => {\n\t\t\tawait ctx.state.pending(entry);\n\t\t\tconst created = await cloudfront.send(\n\t\t\t\tnew CF.CreateOriginAccessControlCommand({\n\t\t\t\t\tOriginAccessControlConfig: {\n\t\t\t\t\t\tName: input.name,\n\t\t\t\t\t\tDescription: `gkm: CloudFront's reads of ${input.bucket}`,\n\t\t\t\t\t\tOriginAccessControlOriginType: 's3',\n\t\t\t\t\t\tSigningBehavior: 'always',\n\t\t\t\t\t\tSigningProtocol: 'sigv4',\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t);\n\t\t\tid = created.OriginAccessControl!.Id!;\n\t\t\tawait ctx.state.ready(entry, id);\n\t\t},\n\t);\n\treturn id;\n}\n\n/** One response headers policy, as wanted. */\nasync function ensureHeadersPolicy(\n\tctx: Ctx,\n\tclients: CdnClients,\n\tinput: { id: string; kind: string; config: ResponseHeadersPolicyConfig },\n): Promise<string> {\n\tconst { CF, cloudfront } = clients;\n\tconst { config } = input;\n\tconst entry: StateEntry = {\n\t\tkey: `cloudfront-headers:${input.id}:${input.kind}`,\n\t\ttype: 'cloudfront-headers',\n\t\tdata: { name: config.Name },\n\t};\n\tconst read = (Id: string) =>\n\t\treadOr(\n\t\t\t() =>\n\t\t\t\tcloudfront.send(new CF.GetResponseHeadersPolicyConfigCommand({ Id })),\n\t\t\t['NoSuchResponseHeadersPolicy'],\n\t\t);\n\n\tlet id = ctx.state.record(entry.key)?.id;\n\tlet current = id ? await read(id) : undefined;\n\tif (!current) {\n\t\tid = undefined;\n\t\tlet Marker: string | undefined;\n\t\tdo {\n\t\t\tconst page = await cloudfront.send(\n\t\t\t\tnew CF.ListResponseHeadersPoliciesCommand({\n\t\t\t\t\tType: 'custom',\n\t\t\t\t\t...(Marker ? { Marker } : {}),\n\t\t\t\t}),\n\t\t\t);\n\t\t\tconst match = page.ResponseHeadersPolicyList?.Items?.find(\n\t\t\t\t(p) =>\n\t\t\t\t\tp.ResponseHeadersPolicy?.ResponseHeadersPolicyConfig?.Name ===\n\t\t\t\t\tconfig.Name,\n\t\t\t);\n\t\t\tif (match?.ResponseHeadersPolicy?.Id) {\n\t\t\t\tid = match.ResponseHeadersPolicy.Id;\n\t\t\t\tcurrent = await read(id);\n\t\t\t}\n\t\t\tMarker = page.ResponseHeadersPolicyList?.NextMarker;\n\t\t} while (!id && Marker);\n\t}\n\n\tif (id && current) {\n\t\tif (!ctx.dryRun) await ctx.state.ready(entry, id);\n\t\tif (sameHeadersPolicy(current.ResponseHeadersPolicyConfig, config)) {\n\t\t\treturn id;\n\t\t}\n\t\tconst existing = id;\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: input.id,\n\t\t\t\tresource: `response headers policy ${config.Name}`,\n\t\t\t\tchange: 'put back what drifted',\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tawait cloudfront.send(\n\t\t\t\t\tnew CF.UpdateResponseHeadersPolicyCommand({\n\t\t\t\t\t\tId: existing,\n\t\t\t\t\t\tIfMatch: current!.ETag,\n\t\t\t\t\t\tResponseHeadersPolicyConfig: config,\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t},\n\t\t);\n\t\treturn existing;\n\t}\n\n\tlet created = PLANNED;\n\tawait ctx.change(\n\t\t{\n\t\t\tconstruct: input.id,\n\t\t\tresource: `response headers policy ${config.Name}`,\n\t\t\tchange: 'create',\n\t\t},\n\t\tasync () => {\n\t\t\tawait ctx.state.pending(entry);\n\t\t\tconst out = await cloudfront.send(\n\t\t\t\tnew CF.CreateResponseHeadersPolicyCommand({\n\t\t\t\t\tResponseHeadersPolicyConfig: config,\n\t\t\t\t}),\n\t\t\t);\n\t\t\tcreated = out.ResponseHeadersPolicy!.Id!;\n\t\t\tawait ctx.state.ready(entry, created);\n\t\t},\n\t);\n\treturn created;\n}\n\n/** The distribution: recorded, found by its comment, or created — as wanted. */\nasync function ensureDistribution(\n\tctx: Ctx,\n\tclients: CdnClients,\n\tinput: { id: string; comment: string; config: DistributionConfig },\n): Promise<{ arn: string; domainName: string }> {\n\tconst { CF, cloudfront } = clients;\n\tconst entry: StateEntry = {\n\t\tkey: `cloudfront-distribution:${input.id}`,\n\t\ttype: 'cloudfront-distribution',\n\t};\n\tconst get = (Id: string) =>\n\t\treadOr(\n\t\t\t() => cloudfront.send(new CF.GetDistributionCommand({ Id })),\n\t\t\t['NoSuchDistribution'],\n\t\t);\n\n\tlet found = ctx.state.record(entry.key)?.id\n\t\t? await get(ctx.state.record(entry.key)!.id!)\n\t\t: undefined;\n\tif (!found) {\n\t\tlet Marker: string | undefined;\n\t\tdo {\n\t\t\tconst page = await cloudfront.send(\n\t\t\t\tnew CF.ListDistributionsCommand({ ...(Marker ? { Marker } : {}) }),\n\t\t\t);\n\t\t\tconst match = page.DistributionList?.Items?.find(\n\t\t\t\t(d) => d.Comment === input.comment,\n\t\t\t);\n\t\t\tif (match?.Id) found = await get(match.Id);\n\t\t\tMarker = page.DistributionList?.NextMarker;\n\t\t} while (!found && Marker);\n\t}\n\n\tif (found?.Distribution) {\n\t\tconst distribution = found.Distribution;\n\t\tconst id = distribution.Id!;\n\t\tconst located = {\n\t\t\tarn: distribution.ARN!,\n\t\t\tdomainName: distribution.DomainName!,\n\t\t};\n\t\tif (!ctx.dryRun) {\n\t\t\tawait ctx.state.ready({ ...entry, data: located }, id);\n\t\t}\n\t\tconst current = await cloudfront.send(\n\t\t\tnew CF.GetDistributionConfigCommand({ Id: id }),\n\t\t);\n\t\tif (holds(current.DistributionConfig, input.config)) return located;\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: input.id,\n\t\t\t\tresource: `distribution ${id}`,\n\t\t\t\tchange: 'put back what drifted',\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tawait cloudfront.send(\n\t\t\t\t\tnew CF.UpdateDistributionCommand({\n\t\t\t\t\t\tId: id,\n\t\t\t\t\t\tIfMatch: current.ETag,\n\t\t\t\t\t\t// What else it holds — logging, a WAF — kept.\n\t\t\t\t\t\tDistributionConfig: {\n\t\t\t\t\t\t\t...current.DistributionConfig!,\n\t\t\t\t\t\t\t...input.config,\n\t\t\t\t\t\t\tCallerReference: current.DistributionConfig!.CallerReference,\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t},\n\t\t);\n\t\treturn located;\n\t}\n\n\tlet created = {\n\t\tarn: `arn:aws:cloudfront::${PLANNED}`,\n\t\tdomainName: PLANNED,\n\t};\n\tconst host = input.config.Aliases?.Items?.[0] ?? input.id;\n\tawait ctx.change(\n\t\t{\n\t\t\tconstruct: input.id,\n\t\t\tresource: `distribution ${host}`,\n\t\t\tchange: 'create (CloudFront, takes minutes to reach every edge)',\n\t\t},\n\t\tasync () => {\n\t\t\tawait ctx.state.pending(entry);\n\t\t\tconst out = await cloudfront.send(\n\t\t\t\tnew CF.CreateDistributionWithTagsCommand({\n\t\t\t\t\tDistributionConfigWithTags: {\n\t\t\t\t\t\tDistributionConfig: input.config,\n\t\t\t\t\t\tTags: {\n\t\t\t\t\t\t\tItems: identityTags(ctx, input.id).map((t) => ({\n\t\t\t\t\t\t\t\tKey: t.Key,\n\t\t\t\t\t\t\t\tValue: t.Value,\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t);\n\t\t\tcreated = {\n\t\t\t\tarn: out.Distribution!.ARN!,\n\t\t\t\tdomainName: out.Distribution!.DomainName!,\n\t\t\t};\n\t\t\tawait ctx.state.ready({ ...entry, data: created }, out.Distribution!.Id!);\n\t\t},\n\t);\n\treturn created;\n}\n","/**\n * A bucket's changes, delivered — the `s3` provider's half of object events.\n *\n * S3 can tell only AWS services that an object changed, so for each bucket a\n * subscriber watches the deploy creates, in the stage's account:\n *\n * - **a topic**, `<bucket>-events`, that only this bucket may publish to;\n * - **a dead-letter queue**, `<bucket>-events-dlq`, for what SNS gives up\n * delivering — an outage longer than its retries would otherwise lose\n * uploads;\n * - **the bucket's notification entry**, `gkm-<id>`, sending every create and\n * delete to the topic — merged in, so an entry somebody added by hand stays;\n * - **the events key**, `<ID>_EVENTS_CONNECTION_STRING`, signed by the\n * bucket's own access key, whose policy gains `sns:ConfirmSubscription` on\n * this one topic and nothing more;\n * - **one HTTP subscription per subscriber**, to its route on its worker's\n * host. The deploy owns these — the worker cannot subscribe — and records\n * each ARN for the worker to accept deliveries under.\n *\n * Nothing is deleted except a subscription to the worker's own host that no\n * subscriber wants any more.\n */\n\nimport type {\n\tConstructManifest,\n\tObjectsDeclaration,\n} from '@geekmidas/manifest';\nimport { provideKey } from '@geekmidas/manifest';\nimport * as s3Url from '@geekmidas/storage/s3-url';\nimport {\n\tsubscriptionKey,\n\tsubscriptionResource,\n} from '../../deploy/snsSubscriptions.js';\nimport { GkmError } from '../../errors';\nimport { subscriberRoute } from '../../generators/subscriberRoute.js';\nimport { NoDomainForStage } from '../../target/dokploy/domain.js';\nimport { appKey } from '../../workspace/derive.js';\nimport type { S3ObjectsConfig } from '../../workspace/types.js';\nimport { type AwsProvisioning, awsClientConfig, awsEndpoint } from '../aws.js';\nimport { deploysWithCompose } from '../dns.js';\nimport { identityTags } from '../iam.js';\nimport type { EnsureContext } from '../types.js';\nimport { canonicalPolicy, type PolicyDocument } from './policy.js';\n\ntype Ctx = EnsureContext<S3ObjectsConfig, AwsProvisioning>;\ntype SnsModule = typeof import('@aws-sdk/client-sns');\ntype SqsModule = typeof import('@aws-sdk/client-sqs');\ntype S3Module = typeof import('@aws-sdk/client-s3');\n\n/** What S3 tells the topic about: every object written, and every removed. */\nexport const BUCKET_EVENTS = ['s3:ObjectCreated:*', 's3:ObjectRemoved:*'];\n\n/** One subscriber watching a bucket: the worker that runs it, and its export. */\nexport interface BucketWatcher {\n\tworker: string;\n\tname: string;\n}\n\n/**\n * Each bucket a subscriber watches, with the subscribers watching it: the\n * worker that runs each, and its export — what its route is named after.\n *\n * Read off the bucket's own node, where discovery nests every subscriber\n * bound to it with the worker that owns it.\n */\nexport function bucketWatchers(\n\tmanifest: ConstructManifest,\n): Map<string, BucketWatcher[]> {\n\tconst watched = new Map<string, BucketWatcher[]>();\n\tfor (const [id, declaration] of Object.entries(manifest)) {\n\t\tif (declaration.kind !== 'objects') continue;\n\t\tconst subscribers = (declaration as ObjectsDeclaration).subscribers ?? [];\n\t\tif (subscribers.length === 0) continue;\n\t\twatched.set(\n\t\t\tid,\n\t\t\tsubscribers\n\t\t\t\t.flatMap((s) => (s.owner ? [{ worker: s.owner, name: s.id }] : []))\n\t\t\t\t.sort((a, b) =>\n\t\t\t\t\t`${a.worker}/${a.name}`.localeCompare(`${b.worker}/${b.name}`),\n\t\t\t\t),\n\t\t);\n\t}\n\treturn watched;\n}\n\n/** The topic a bucket's changes are published to. */\nexport function eventsTopicName(bucket: string): string {\n\t// A bucket name may hold dots; a topic or queue name may not.\n\treturn `${bucket.replaceAll('.', '-')}-events`;\n}\n\n/** What SNS gives up delivering, kept. */\nexport function eventsDeadLetterName(bucket: string): string {\n\treturn `${eventsTopicName(bucket)}-dlq`;\n}\n\n/** Only this bucket, in this account, may publish to its topic. */\nexport function eventsTopicPolicy(\n\ttopicArn: string,\n\tbucket: string,\n\taccount: string,\n): PolicyDocument {\n\treturn {\n\t\tVersion: '2012-10-17',\n\t\tStatement: [\n\t\t\t{\n\t\t\t\tSid: 'GkmBucketPublishes',\n\t\t\t\tEffect: 'Allow',\n\t\t\t\tPrincipal: { Service: 's3.amazonaws.com' },\n\t\t\t\tAction: 'sns:Publish',\n\t\t\t\tResource: topicArn,\n\t\t\t\tCondition: {\n\t\t\t\t\tArnLike: { 'aws:SourceArn': `arn:aws:s3:::${bucket}` },\n\t\t\t\t\tStringEquals: { 'aws:SourceAccount': account },\n\t\t\t\t},\n\t\t\t},\n\t\t],\n\t};\n}\n\n/** Only the bucket's topic may send to its dead-letter queue. */\nexport function eventsDeadLetterPolicy(\n\tqueueArn: string,\n\ttopicArn: string,\n): PolicyDocument {\n\treturn {\n\t\tVersion: '2012-10-17',\n\t\tStatement: [\n\t\t\t{\n\t\t\t\tSid: 'GkmTopicDeadLetters',\n\t\t\t\tEffect: 'Allow',\n\t\t\t\tPrincipal: { Service: 'sns.amazonaws.com' },\n\t\t\t\tAction: 'sqs:SendMessage',\n\t\t\t\tResource: queueArn,\n\t\t\t\tCondition: { ArnEquals: { 'aws:SourceArn': topicArn } },\n\t\t\t},\n\t\t],\n\t};\n}\n\n/** A bucket's notification entry, as S3 spells it. */\nexport interface TopicNotification {\n\tId?: string;\n\tTopicArn: string | undefined;\n\tEvents: string[] | undefined;\n\tFilter?: unknown;\n}\n\n/** A bucket's notification configuration, as far as this provider reads it. */\nexport interface BucketNotifications {\n\tTopicConfigurations?: TopicNotification[];\n\t[other: string]: unknown;\n}\n\n/** The id this provider's notification entry goes under. */\nexport const notificationId = (id: string) => `gkm-${id}`;\n\n/**\n * The bucket's notifications with this provider's entry put back, and whether\n * that changed anything. Every other entry — a queue, a function, a topic\n * somebody added by hand — is kept exactly as it was.\n */\nexport function mergedNotifications(\n\tcurrent: BucketNotifications,\n\tid: string,\n\ttopicArn: string,\n): { config: BucketNotifications; changed: boolean } {\n\tconst own = notificationId(id);\n\tconst wanted: TopicNotification = {\n\t\tId: own,\n\t\tTopicArn: topicArn,\n\t\tEvents: [...BUCKET_EVENTS],\n\t};\n\tconst topics = current.TopicConfigurations ?? [];\n\tconst existing = topics.find((t) => t.Id === own);\n\tconst same =\n\t\texisting !== undefined &&\n\t\texisting.TopicArn === topicArn &&\n\t\tJSON.stringify([...(existing.Events ?? [])].sort()) ===\n\t\t\tJSON.stringify([...BUCKET_EVENTS].sort()) &&\n\t\texisting.Filter === undefined;\n\tconst { ResponseMetadata: _, ...rest } = current as BucketNotifications & {\n\t\tResponseMetadata?: unknown;\n\t};\n\treturn {\n\t\tconfig: {\n\t\t\t...rest,\n\t\t\tTopicConfigurations: [...topics.filter((t) => t.Id !== own), wanted],\n\t\t},\n\t\tchanged: !same,\n\t};\n}\n\n/** S3 refused the entry because another one already covers these events. */\nexport class BucketNotificationsOverlap extends GkmError {\n\tconstructor(\n\t\treadonly bucket: string,\n\t\treadonly detail: string,\n\t) {\n\t\tsuper(\n\t\t\t`'${bucket}' already sends object creates or deletes somewhere else, ` +\n\t\t\t\t`and S3 sends each event to one place only, so the deploy cannot ` +\n\t\t\t\t`add its own entry. Remove the other notification from the bucket, ` +\n\t\t\t\t`or give it a prefix, and deploy again. S3 said: ${detail}`,\n\t\t);\n\t\tthis.name = 'BucketNotificationsOverlap';\n\t}\n}\n\n/** The topic and the dead-letter queue a watched bucket publishes through. */\nexport interface BucketEvents {\n\ttopicArn: string;\n\tdeadLetterArn: string;\n}\n\nexport interface EventsClients {\n\tSNS: SnsModule;\n\tSQS: SqsModule;\n\tsns: InstanceType<SnsModule['SNSClient']>;\n\tsqs: InstanceType<SqsModule['SQSClient']>;\n\taccount: string;\n\tregion: string;\n\t/** An emulator's SNS endpoint, when the environment names one. */\n\tendpoint?: string;\n}\n\n/** Clients for a bucket's events, for tests to replace. */\nexport type EventsClientFactory = (\n\tcredential: AwsProvisioning,\n\tregion: string,\n) => Promise<EventsClients>;\n\nexport const defaultEventsClients: EventsClientFactory = async (\n\tcredential,\n\tregion,\n) => {\n\tconst SNS = await import('@aws-sdk/client-sns');\n\tconst SQS = await import('@aws-sdk/client-sqs');\n\tconst { STSClient, GetCallerIdentityCommand } = await import(\n\t\t'@aws-sdk/client-sts'\n\t);\n\tconst config = await awsClientConfig(credential, region);\n\tconst endpoint = awsEndpoint(process.env, 'SNS');\n\tconst sts = new STSClient({\n\t\t...config,\n\t\t...(awsEndpoint(process.env, 'STS')\n\t\t\t? { endpoint: awsEndpoint(process.env, 'STS') }\n\t\t\t: {}),\n\t});\n\tconst { Account } = await sts.send(new GetCallerIdentityCommand({}));\n\tsts.destroy();\n\treturn {\n\t\tSNS,\n\t\tSQS,\n\t\tsns: new SNS.SNSClient({ ...config, ...(endpoint ? { endpoint } : {}) }),\n\t\tsqs: new SQS.SQSClient({\n\t\t\t...config,\n\t\t\t...(awsEndpoint(process.env, 'SQS')\n\t\t\t\t? { endpoint: awsEndpoint(process.env, 'SQS') }\n\t\t\t\t: {}),\n\t\t}),\n\t\taccount: Account!,\n\t\tregion,\n\t\t...(endpoint ? { endpoint } : {}),\n\t};\n};\n\nlet makeEventsClients: EventsClientFactory = defaultEventsClients;\n\n/** The stage's SNS, SQS and account, through the factory a test may replace. */\nexport function eventsClients(\n\tcredential: AwsProvisioning,\n\tregion: string,\n): Promise<EventsClients> {\n\treturn makeEventsClients(credential, region);\n}\n\n/** Replace the client factory — a test intercepting a call. Returns the old one. */\nexport function useEventsClients(\n\tfactory: EventsClientFactory,\n): EventsClientFactory {\n\tconst previous = makeEventsClients;\n\tmakeEventsClients = factory;\n\treturn previous;\n}\n\nfunction errorName(error: unknown): string {\n\tconst e = error as { name?: string; Code?: string; code?: string };\n\treturn e?.name ?? e?.Code ?? e?.code ?? '';\n}\n\nasync function readOr<T>(\n\tread: () => Promise<T>,\n\tmissing: readonly string[],\n): Promise<T | undefined> {\n\ttry {\n\t\treturn await read();\n\t} catch (error) {\n\t\tif (missing.includes(errorName(error))) return undefined;\n\t\tthrow error;\n\t}\n}\n\n/**\n * An SNS topic of the stage's, created where missing, whose policy lets\n * exactly one AWS service publish to it — put back where it drifted. A\n * watched bucket's topic is published to by S3; an inbox's by SES.\n */\nexport async function ensureTopic(\n\tctx: Pick<Ctx, 'state' | 'change'> & Parameters<typeof identityTags>[0],\n\tclients: EventsClients,\n\tinput: {\n\t\tid: string;\n\t\ttopicName: string;\n\t\t/** Who may publish, as the change log says it: `'acme-uploads'`. */\n\t\tpublisher: string;\n\t\tpolicy: (topicArn: string) => PolicyDocument;\n\t},\n): Promise<string> {\n\tconst { SNS, sns, account, region } = clients;\n\tconst { id, topicName } = input;\n\tconst topicArn = `arn:aws:sns:${region}:${account}:${topicName}`;\n\tconst topicEntry = { key: `sns-topic:${id}`, type: 'sns-topic' };\n\tconst topic = await readOr(\n\t\t() => sns.send(new SNS.GetTopicAttributesCommand({ TopicArn: topicArn })),\n\t\t['NotFound', 'NotFoundException'],\n\t);\n\tif (topic) {\n\t\tawait ctx.state.ready(topicEntry, topicArn);\n\t} else {\n\t\tawait ctx.change(\n\t\t\t{ construct: id, resource: `SNS topic ${topicName}`, change: 'create' },\n\t\t\tasync () => {\n\t\t\t\tawait ctx.state.pending(topicEntry);\n\t\t\t\tawait sns.send(\n\t\t\t\t\tnew SNS.CreateTopicCommand({\n\t\t\t\t\t\tName: topicName,\n\t\t\t\t\t\tTags: identityTags(ctx, id),\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t\tawait ctx.state.ready(topicEntry, topicArn);\n\t\t\t},\n\t\t);\n\t}\n\n\tconst topicPolicy = input.policy(topicArn);\n\tconst currentTopicPolicy = topic?.Attributes?.Policy;\n\tif (\n\t\t!currentTopicPolicy ||\n\t\tcanonicalPolicy(JSON.parse(currentTopicPolicy)) !==\n\t\t\tcanonicalPolicy(topicPolicy)\n\t) {\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: id,\n\t\t\t\tresource: `SNS topic ${topicName}`,\n\t\t\t\tchange: `publishable by ${input.publisher} only`,\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tawait sns.send(\n\t\t\t\t\tnew SNS.SetTopicAttributesCommand({\n\t\t\t\t\t\tTopicArn: topicArn,\n\t\t\t\t\t\tAttributeName: 'Policy',\n\t\t\t\t\t\tAttributeValue: JSON.stringify(topicPolicy),\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t},\n\t\t);\n\t}\n\n\treturn topicArn;\n}\n\n/**\n * The topic, its policy, the dead-letter queue and the bucket's notification\n * entry for one watched bucket — created where missing, put back where they\n * drifted.\n */\nexport async function ensureBucketEvents(\n\tctx: Ctx,\n\ts3: { S3: S3Module; s3: InstanceType<S3Module['S3Client']> },\n\tinput: { id: string; bucket: string; region: string },\n): Promise<BucketEvents> {\n\tconst { id, bucket, region } = input;\n\tconst clients = await makeEventsClients(ctx.credential, region);\n\tconst { SQS, sns, sqs, account } = clients;\n\n\tconst topicName = eventsTopicName(bucket);\n\tconst topicArn = await ensureTopic(ctx, clients, {\n\t\tid,\n\t\ttopicName,\n\t\tpublisher: `'${bucket}'`,\n\t\tpolicy: (arn) => eventsTopicPolicy(arn, bucket, account),\n\t});\n\n\tconst queueName = eventsDeadLetterName(bucket);\n\tconst deadLetterArn = `arn:aws:sqs:${region}:${account}:${queueName}`;\n\tconst queueEntry = { key: `sqs-queue:${id}-events-dlq`, type: 'sqs-queue' };\n\tconst queue = await readOr(\n\t\t() => sqs.send(new SQS.GetQueueUrlCommand({ QueueName: queueName })),\n\t\t['QueueDoesNotExist', 'AWS.SimpleQueueService.NonExistentQueue'],\n\t);\n\tlet queueUrl = queue?.QueueUrl;\n\tif (queueUrl) {\n\t\tawait ctx.state.ready(queueEntry, deadLetterArn);\n\t} else {\n\t\tawait ctx.change(\n\t\t\t{ construct: id, resource: `SQS queue ${queueName}`, change: 'create' },\n\t\t\tasync () => {\n\t\t\t\tawait ctx.state.pending(queueEntry);\n\t\t\t\tconst created = await sqs.send(\n\t\t\t\t\tnew SQS.CreateQueueCommand({\n\t\t\t\t\t\tQueueName: queueName,\n\t\t\t\t\t\t// The longest SQS keeps anything: a dead letter is read by a\n\t\t\t\t\t\t// person, later.\n\t\t\t\t\t\tAttributes: { MessageRetentionPeriod: '1209600' },\n\t\t\t\t\t\ttags: Object.fromEntries(\n\t\t\t\t\t\t\tidentityTags(ctx, id).map((t) => [t.Key, t.Value]),\n\t\t\t\t\t\t),\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t\tqueueUrl = created.QueueUrl;\n\t\t\t\tawait ctx.state.ready(queueEntry, deadLetterArn);\n\t\t\t},\n\t\t);\n\t}\n\n\tconst queuePolicy = eventsDeadLetterPolicy(deadLetterArn, topicArn);\n\tconst currentQueuePolicy = queue?.QueueUrl\n\t\t? (\n\t\t\t\tawait sqs.send(\n\t\t\t\t\tnew SQS.GetQueueAttributesCommand({\n\t\t\t\t\t\tQueueUrl: queue.QueueUrl,\n\t\t\t\t\t\tAttributeNames: ['Policy'],\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t\t).Attributes?.Policy\n\t\t: undefined;\n\tif (\n\t\t!currentQueuePolicy ||\n\t\tcanonicalPolicy(JSON.parse(currentQueuePolicy)) !==\n\t\t\tcanonicalPolicy(queuePolicy)\n\t) {\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: id,\n\t\t\t\tresource: `SQS queue ${queueName}`,\n\t\t\t\tchange: `receives what ${topicName} cannot deliver`,\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tawait sqs.send(\n\t\t\t\t\tnew SQS.SetQueueAttributesCommand({\n\t\t\t\t\t\tQueueUrl: queueUrl!,\n\t\t\t\t\t\tAttributes: { Policy: JSON.stringify(queuePolicy) },\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t},\n\t\t);\n\t}\n\n\t// The bucket tells the topic last: S3 checks it may publish when the entry\n\t// is written, which the topic's policy above is what allows.\n\tconst current = (await readOr(\n\t\t() =>\n\t\t\ts3.s3.send(\n\t\t\t\tnew s3.S3.GetBucketNotificationConfigurationCommand({ Bucket: bucket }),\n\t\t\t),\n\t\t['NoSuchBucket'],\n\t)) as BucketNotifications | undefined;\n\tconst merged = mergedNotifications(current ?? {}, id, topicArn);\n\tif (merged.changed) {\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: id,\n\t\t\t\tresource: `bucket ${bucket}`,\n\t\t\t\tchange: `send object creates and deletes to ${topicName}`,\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\ttry {\n\t\t\t\t\tawait s3.s3.send(\n\t\t\t\t\t\tnew s3.S3.PutBucketNotificationConfigurationCommand({\n\t\t\t\t\t\t\tBucket: bucket,\n\t\t\t\t\t\t\tNotificationConfiguration: merged.config as never,\n\t\t\t\t\t\t}),\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = (error as Error).message ?? '';\n\t\t\t\t\tif (/overlap/i.test(message)) {\n\t\t\t\t\t\tthrow new BucketNotificationsOverlap(bucket, message);\n\t\t\t\t\t}\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t);\n\t}\n\n\tsns.destroy();\n\tsqs.destroy();\n\treturn { topicArn, deadLetterArn };\n}\n\n/**\n * A topic's connection string: its address, signed by the access key in a\n * bucket's URL — read off that URL, so a rotated key rotates this one with\n * it. A watched bucket's events key by default; an inbox's topic, signed by\n * its own bucket's key, otherwise.\n */\nexport async function ensureEventsKey(\n\tctx: Pick<Ctx, 'secrets' | 'change' | 'writeSecrets' | 'state'>,\n\tinput: {\n\t\tid: string;\n\t\ttopicArn: string;\n\t\tregion: string;\n\t\t/** The key written — `<ID>_EVENTS_CONNECTION_STRING` by default. */\n\t\tkey?: string;\n\t\t/** The URL whose access key signs it — `<ID>_URL` by default. */\n\t\turlKey?: string;\n\t},\n): Promise<void> {\n\tconst { id, topicArn, region } = input;\n\tconst key = input.key ?? provideKey(id, 'eventsConnectionString');\n\tconst url = ctx.secrets[input.urlKey ?? provideKey(id, 'url')];\n\tlet parsed: s3Url.S3Address | undefined;\n\ttry {\n\t\tparsed = url ? s3Url.parse(url) : undefined;\n\t} catch {\n\t\tparsed = undefined;\n\t}\n\tif (!parsed?.accessKeyId || !parsed.secretAccessKey) {\n\t\t// Only a dry run gets here: the key is issued above, before this.\n\t\tawait ctx.change(\n\t\t\t{ construct: id, resource: key, change: 'set to the topic’s address' },\n\t\t\tasync () => {},\n\t\t);\n\t\treturn;\n\t}\n\n\tconst { snsUrl } = await import('@geekmidas/events/sns');\n\tconst endpoint = awsEndpoint(process.env, 'SNS');\n\tconst address = new URL(\n\t\tsnsUrl.build({ topicArn, region, ...(endpoint ? { endpoint } : {}) }),\n\t);\n\taddress.searchParams.set('accessKeyId', parsed.accessKeyId);\n\taddress.searchParams.set('secretAccessKey', parsed.secretAccessKey);\n\tconst value = address.toString();\n\n\tconst entry = { key: `events-key:${id}`, type: 'url' };\n\tif (ctx.secrets[key] === value) {\n\t\tawait ctx.state.ready(entry, topicArn);\n\t\treturn;\n\t}\n\tawait ctx.change(\n\t\t{ construct: id, resource: key, change: `set to ${topicArn}` },\n\t\tasync () => {\n\t\t\tawait ctx.writeSecrets({ [key]: value });\n\t\t\tawait ctx.state.ready(entry, topicArn);\n\t\t},\n\t);\n}\n\n/** Where SNS pushes to a worker: its own host on the stage's domain. */\nexport function workerPushHost(worker: string, domain: string): string {\n\treturn `${appKey(worker)}.${domain}`;\n}\n\n/**\n * One HTTP subscription per subscriber to the bucket's topic, at its route on\n * its worker's host, with the bucket's dead-letter queue — and each ARN\n * recorded, which is what the worker accepts deliveries under.\n *\n * Unfiltered: a bucket's notifications carry no `type` attribute, and the\n * subscriber's own filter — events, prefix, suffix — is what decides. Only a\n * compose stage pushes to a worker; any other is told so and left alone.\n */\nexport async function ensurePushSubscriptions(\n\tctx: Ctx,\n\tinput: {\n\t\tid: string;\n\t\tevents: BucketEvents;\n\t\tregion: string;\n\t\twatchers: readonly BucketWatcher[];\n\t},\n): Promise<void> {\n\tconst { id, events, region, watchers } = input;\n\tif (!deploysWithCompose(ctx.workspace)) {\n\t\tctx.log(\n\t\t\t` '${id}' is watched, and only a compose stage pushes a bucket's changes to a worker: its subscribers receive nothing on '${ctx.stage}'`,\n\t\t);\n\t\treturn;\n\t}\n\tif (watchers.length === 0) return;\n\tconst domain = ctx.workspace.domains?.[ctx.stage];\n\tif (!domain) throw new NoDomainForStage(ctx.stage);\n\n\tconst { SNSConnection, reconcileHttpSubscriptions } = await import(\n\t\t'@geekmidas/events/sns'\n\t);\n\tconst { credentials } = await awsClientConfig(ctx.credential, region);\n\tconst endpoint = awsEndpoint(process.env, 'SNS');\n\n\tconst byWorker = new Map<string, BucketWatcher[]>();\n\tfor (const watcher of watchers) {\n\t\tbyWorker.set(watcher.worker, [\n\t\t\t...(byWorker.get(watcher.worker) ?? []),\n\t\t\twatcher,\n\t\t]);\n\t}\n\n\tfor (const [worker, own] of byWorker) {\n\t\tconst host = workerPushHost(worker, domain);\n\t\tconst endpoints = own.map(({ name }) => ({\n\t\t\tname,\n\t\t\tendpoint: `https://${host}${subscriberRoute(name)}`,\n\t\t\tdeadLetterQueueArn: events.deadLetterArn,\n\t\t}));\n\t\tconst unrecorded = own.filter(\n\t\t\t({ name }) =>\n\t\t\t\tctx.state.record(subscriptionKey(worker, name))?.status !== 'ready',\n\t\t);\n\t\tif (ctx.dryRun) {\n\t\t\tif (unrecorded.length > 0) {\n\t\t\t\tawait ctx.change(\n\t\t\t\t\t{\n\t\t\t\t\t\tconstruct: id,\n\t\t\t\t\t\tresource: `SNS subscriptions on ${host}`,\n\t\t\t\t\t\tchange: `subscribe ${unrecorded.map((w) => w.name).join(', ')}`,\n\t\t\t\t\t},\n\t\t\t\t\tasync () => {},\n\t\t\t\t);\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst connection = new SNSConnection({\n\t\t\ttopicArn: events.topicArn,\n\t\t\tregion,\n\t\t\t...(endpoint ? { endpoint } : {}),\n\t\t\t...(credentials ? { credentials } : {}),\n\t\t});\n\t\tconst result = await reconcileHttpSubscriptions(connection, {\n\t\t\tendpoints,\n\t\t\townedHost: host,\n\t\t});\n\t\tawait connection.close();\n\n\t\tfor (const spec of endpoints) {\n\t\t\tconst arn = result.subscriptions[spec.name]!;\n\t\t\tconst pending = result.pending.includes(spec.name);\n\t\t\tconst recorded = ctx.state.record(subscriptionKey(worker, spec.name));\n\t\t\tif (recorded?.id !== arn) {\n\t\t\t\tctx.log(\n\t\t\t\t\t` ✓ '${spec.name}' subscribed to ${events.topicArn.split(':').pop()} at ${spec.endpoint}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst { key, type, data } = subscriptionResource(\n\t\t\t\t{\n\t\t\t\t\tworker,\n\t\t\t\t\tname: spec.name,\n\t\t\t\t\ttopicArn: events.topicArn,\n\t\t\t\t\tendpoint: spec.endpoint,\n\t\t\t\t\tdeadLetterQueueArn: events.deadLetterArn,\n\t\t\t\t\tpending,\n\t\t\t\t},\n\t\t\t\tarn,\n\t\t\t);\n\t\t\tawait ctx.state.ready({ key, type, ...(data ? { data } : {}) }, arn);\n\t\t}\n\t\tfor (const arn of result.removed) {\n\t\t\tctx.log(` – unsubscribed ${arn}: no subscriber wants it any more`);\n\t\t}\n\t}\n}\n","/**\n * Which browser origins may call a bucket: the stage's sites that depend on\n * an API using it.\n *\n * The same reading of the graph an API's own CORS origins are — its callers —\n * taken one step further: a site that calls an API which hands it a presigned\n * upload URL is a site whose browser PUTs to the bucket. A site that reads the\n * bucket (or a file server over it) itself is one too. Nothing is written\n * down: add a site that calls the API and the next provision run allows it.\n */\n\nimport {\n\ttype ConstructManifest,\n\tdependentsOf,\n\tprovideKey,\n} from '@geekmidas/manifest';\nimport { appEnvKeys } from '../../reconcile/apps.js';\nimport {\n\tisMainFrontendApp,\n\tNoDomainForStage,\n\tresolveHost,\n} from '../../target/dokploy/domain.js';\nimport { appKey } from '../../workspace/derive.js';\nimport type { NormalizedWorkspace } from '../../workspace/types.js';\n\nexport interface BucketOriginsInput {\n\tworkspace: Pick<NormalizedWorkspace, 'apps' | 'deploy' | 'domains'>;\n\tmanifest: ConstructManifest;\n\tstage: string;\n\t/** The `objects` construct's id. */\n\tbucket: string;\n}\n\nexport interface BucketOrigins {\n\t/** `https://<host>` for each site, sorted. */\n\torigins: string[];\n\t/** Sites with no address on the stage — no domain for it. */\n\tunresolved: string[];\n}\n\n/** The keys a reader of this bucket holds: its URL, and each server's over it. */\nexport function bucketKeys(manifest: ConstructManifest, bucket: string) {\n\treturn [\n\t\tprovideKey(bucket, 'url'),\n\t\t...Object.entries(manifest)\n\t\t\t.filter(([, d]) => d.kind === 'file-server' && d.of === bucket)\n\t\t\t.map(([id]) => provideKey(id, 'url')),\n\t];\n}\n\nexport function bucketOrigins(input: BucketOriginsInput): BucketOrigins {\n\tconst { manifest, workspace, stage } = input;\n\tconst keys = bucketKeys(manifest, input.bucket);\n\n\tconst readers = Object.entries(manifest)\n\t\t.filter(\n\t\t\t([, d]) =>\n\t\t\t\td.kind === 'rest-api' ||\n\t\t\t\td.kind === 'websocket-api' ||\n\t\t\t\td.kind === 'site',\n\t\t)\n\t\t.filter(([id]) => {\n\t\t\tconst env = appEnvKeys(manifest, appKey(id));\n\t\t\treturn env !== undefined && keys.some((key) => env.has(key));\n\t\t});\n\n\tconst sites = new Set<string>();\n\tfor (const [id, declaration] of readers) {\n\t\tif (declaration.kind === 'site') {\n\t\t\tsites.add(id);\n\t\t\tcontinue;\n\t\t}\n\t\tfor (const caller of dependentsOf(manifest, id)) {\n\t\t\tif (manifest[caller]?.kind === 'site') sites.add(caller);\n\t\t}\n\t}\n\n\tconst origins = new Set<string>();\n\tconst unresolved: string[] = [];\n\tfor (const id of [...sites].sort()) {\n\t\tconst name = appKey(id);\n\t\tconst app = workspace.apps[name];\n\t\tif (!app) {\n\t\t\tunresolved.push(name);\n\t\t\tcontinue;\n\t\t}\n\t\ttry {\n\t\t\tconst host = resolveHost(\n\t\t\t\tname,\n\t\t\t\tapp,\n\t\t\t\tstage,\n\t\t\t\tworkspace.domains,\n\t\t\t\tisMainFrontendApp(name, app, workspace.apps),\n\t\t\t);\n\t\t\torigins.add(`https://${host}`);\n\t\t} catch (error) {\n\t\t\tif (!(error instanceof NoDomainForStage)) throw error;\n\t\t\tunresolved.push(name);\n\t\t}\n\t}\n\n\treturn { origins: [...origins].sort(), unresolved };\n}\n","/**\n * `objects: { provider: 's3' }` — each bucket construct, created in the\n * stage's AWS account with a user and key of its own.\n *\n * Per `ObjectStorage` (and each `FileServer` over it), on the stage:\n *\n * - **the bucket**, named `<namespace>-<project>-<stage>-<id>` — or with a\n * random suffix when another account holds that name — recorded in the\n * stage's state and reused forever; Block Public Access, SSE-S3, a policy\n * refusing anything but TLS, versioning when asked, and CORS for the\n * stage's sites that call an API using it;\n * - **an IAM user**, `gkm-<project>-<stage>-<id>` under `/gkm/`, tagged with\n * the deploy identity, with an inline policy for that bucket only;\n * - **its access key**, written into the stage's secrets as the bucket's URL\n * (`s3://KEY:SECRET@bucket?region=…`) — the secret is never printed;\n * - **a file server's** open paths public on exactly those prefixes, and its\n * URL the bucket's regional endpoint.\n *\n * Every run reads what is there and puts back what drifted. Nothing is ever\n * deleted, with one exception asked for by name: the old key a\n * `--rotate-keys` replaced, once the stage has deployed with the new one.\n */\n\nimport type {\n\tConstructManifest,\n\tFileServerDeclaration,\n\tObjectsDeclaration,\n} from '@geekmidas/manifest';\nimport { bucketExpiry, provideKey } from '@geekmidas/manifest';\nimport * as s3Url from '@geekmidas/storage/s3-url';\nimport { appKey } from '../../workspace/derive.js';\nimport type { S3ObjectsConfig } from '../../workspace/types.js';\nimport {\n\ttype AwsProvisioning,\n\tawsClientConfig,\n\tawsEndpoint,\n\tawsProvisioningCredentials,\n} from '../aws.js';\nimport {\n\tensureAccessKey,\n\tensureIamUser,\n\tidentityTags,\n\tissuedJustNow,\n\ts3KeySecret,\n\tTAG_PROJECT,\n\twhileKeyIsNew,\n} from '../iam.js';\nimport type {\n\tEnsureContext,\n\tProviderConfig,\n\tResourceProvider,\n\tStateEntry,\n\tVerifyContext,\n} from '../types.js';\nimport { ensureBucketCdn } from './cdn.js';\nimport { checkS3Config, S3_PROVISIONING } from './config.js';\nimport {\n\tBucketNameUnavailable,\n\tBucketOwnedByAnotherProject,\n\tBucketProbeFailed,\n\tBucketRegionMismatch,\n\tBucketRegionUnknown,\n\tProvisionedBucketUnreachable,\n\tRecordedBucketUnreachable,\n\tS3RegionRequired,\n} from './errors.js';\nimport {\n\ttype BucketWatcher,\n\tbucketWatchers,\n\tensureBucketEvents,\n\tensureEventsKey,\n\tensurePushSubscriptions,\n} from './events.js';\nimport { mergeLifecycle } from './lifecycle.js';\nimport {\n\tbucketName,\n\tIAM_POLICY_NAME,\n\tiamUserName,\n\trandomSuffix,\n\tSUFFIX_ATTEMPTS,\n\tsuffixedBucketName,\n} from './naming.js';\nimport { bucketOrigins } from './origins.js';\nimport {\n\tbucketPolicy,\n\ttype CorsRule,\n\tcanonicalPolicy,\n\tcorsRule,\n\tpublicAccessBlock,\n\tuserPolicy,\n} from './policy.js';\n\ntype S3Module = typeof import('@aws-sdk/client-s3');\ntype IamModule = typeof import('@aws-sdk/client-iam');\n\ntype Ctx = EnsureContext<S3ObjectsConfig, AwsProvisioning>;\n\n/** Each bucket construct, with the file servers over it. */\nexport interface BucketConstruct {\n\tid: string;\n\tversioned: boolean;\n\t/** Days kept under each prefix before the bucket deletes them. */\n\texpire?: Readonly<Record<string, number>>;\n\tservers: {\n\t\tid: string;\n\t\topen: readonly string[];\n\t\timmutable: readonly string[];\n\t\t/** Its processors' prefixes, each the CDN sends to the processor unit. */\n\t\tprocessors?: readonly string[];\n\t}[];\n}\n\nexport function bucketConstructs(\n\tmanifest: ConstructManifest,\n): BucketConstruct[] {\n\treturn Object.entries(manifest)\n\t\t.filter(([, d]) => d.kind === 'objects')\n\t\t.map(([id, d]) => ({\n\t\t\tid,\n\t\t\tversioned: (d as ObjectsDeclaration).versioned === true,\n\t\t\t...(bucketExpiry(manifest, id)\n\t\t\t\t? { expire: bucketExpiry(manifest, id) }\n\t\t\t\t: {}),\n\t\t\tservers: Object.entries(manifest)\n\t\t\t\t.filter(\n\t\t\t\t\t([, s]) =>\n\t\t\t\t\t\ts.kind === 'file-server' && (s as FileServerDeclaration).of === id,\n\t\t\t\t)\n\t\t\t\t.map(([sid, s]) => ({\n\t\t\t\t\tid: sid,\n\t\t\t\t\topen: (s as FileServerDeclaration).open ?? [],\n\t\t\t\t\timmutable: (s as FileServerDeclaration).immutable ?? [],\n\t\t\t\t\t...((s as FileServerDeclaration).processors?.length\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tprocessors: [\n\t\t\t\t\t\t\t\t\t...new Set(\n\t\t\t\t\t\t\t\t\t\t(s as FileServerDeclaration).processors!.map(\n\t\t\t\t\t\t\t\t\t\t\t(p) => p.prefix,\n\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t].sort(),\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {}),\n\t\t\t\t}))\n\t\t\t\t.sort((a, b) => a.id.localeCompare(b.id)),\n\t\t}))\n\t\t.sort((a, b) => a.id.localeCompare(b.id));\n}\n\n/**\n * The region the stage's buckets are created in: the entry's, the secrets\n * store's when it is in AWS, the credential's, `AWS_REGION`.\n */\nexport function s3Region(\n\tctx: Pick<Ctx, 'config' | 'workspace' | 'stage'> & {\n\t\tcredential?: AwsProvisioning;\n\t},\n\tenv: NodeJS.ProcessEnv = process.env,\n): string {\n\tconst store = ctx.workspace.secrets?.store;\n\tconst storeRegion =\n\t\tstore && typeof store === 'object' && 'region' in store\n\t\t\t? (store as { region?: string }).region\n\t\t\t: undefined;\n\tconst region =\n\t\tctx.config.region ??\n\t\tstoreRegion ??\n\t\tctx.credential?.region ??\n\t\tenv.AWS_REGION ??\n\t\tenv.AWS_DEFAULT_REGION;\n\tif (!region) throw new S3RegionRequired(ctx.stage);\n\treturn region;\n}\n\n/**\n * The address a file server over `bucket` serves on: the bucket's regional\n * virtual-host endpoint, which is what `FileServer.url(key)` appends a key to\n * — `https://<bucket>.s3.<region>.amazonaws.com/<key>`. Against an emulator,\n * its path-style address.\n */\nexport function bucketPublicUrl(\n\tbucket: string,\n\tregion: string,\n\tendpoint?: string,\n): string {\n\tif (endpoint) return `${endpoint.replace(/\\/+$/, '')}/${bucket}`;\n\treturn `https://${bucket}.s3.${region}.amazonaws.com`;\n}\n\n/** What an AWS SDK error is called, whichever field carries it. */\nfunction errorName(error: unknown): string {\n\tconst e = error as { name?: string; Code?: string };\n\treturn e?.Code ?? e?.name ?? '';\n}\n\n/**\n * What S3 answered a call, read off the response rather than the error's\n * name: a `HEAD` has no body, so SDK v3 names a 301, 400 or 403 `Unknown`\n * (only a 404 becomes `NotFound`). The status, the bucket's region and the\n * request ids are in the response either way.\n */\nexport interface S3Answer {\n\t/** The HTTP status; absent when no response came back. */\n\tstatus?: number;\n\tname: string;\n\tmessage: string;\n\t/** `x-amz-bucket-region`: where the bucket is. */\n\tregion?: string;\n\trequestId?: string;\n\tid2?: string;\n}\n\n/** The answer an SDK call failed with. */\nexport function s3Answer(error: unknown): S3Answer {\n\tconst e = error as {\n\t\tname?: string;\n\t\tCode?: string;\n\t\tmessage?: string;\n\t\t$metadata?: {\n\t\t\thttpStatusCode?: number;\n\t\t\trequestId?: string;\n\t\t\textendedRequestId?: string;\n\t\t};\n\t\t$response?: {\n\t\t\tstatusCode?: number;\n\t\t\theaders?: Record<string, string | undefined>;\n\t\t};\n\t};\n\tconst headers = Object.fromEntries(\n\t\tObject.entries(e?.$response?.headers ?? {}).map(([k, v]) => [\n\t\t\tk.toLowerCase(),\n\t\t\tv,\n\t\t]),\n\t);\n\tconst status = e?.$metadata?.httpStatusCode ?? e?.$response?.statusCode;\n\tconst region = headers['x-amz-bucket-region'];\n\tconst requestId = e?.$metadata?.requestId ?? headers['x-amz-request-id'];\n\tconst id2 = e?.$metadata?.extendedRequestId ?? headers['x-amz-id-2'];\n\treturn {\n\t\t...(status !== undefined ? { status } : {}),\n\t\tname: errorName(error) || 'Error',\n\t\tmessage: e?.message ?? String(error),\n\t\t...(region ? { region } : {}),\n\t\t...(requestId ? { requestId } : {}),\n\t\t...(id2 ? { id2 } : {}),\n\t};\n}\n\n/**\n * `HTTP 403 Unknown: UnknownError [x-amz-bucket-region: eu-west-1,\n * x-amz-request-id: …, x-amz-id-2: …]` — everything S3 said, for a message.\n */\nexport function describeAnswer(answer: S3Answer): string {\n\tconst said =\n\t\tanswer.message && answer.message !== answer.name\n\t\t\t? `${answer.name}: ${answer.message}`\n\t\t\t: answer.name;\n\tconst headers = [\n\t\tanswer.region && `x-amz-bucket-region: ${answer.region}`,\n\t\tanswer.requestId && `x-amz-request-id: ${answer.requestId}`,\n\t\tanswer.id2 && `x-amz-id-2: ${answer.id2}`,\n\t].filter(Boolean);\n\treturn (\n\t\t`${answer.status === undefined ? 'no response' : `HTTP ${answer.status}`} ${said}` +\n\t\t(headers.length > 0 ? ` [${headers.join(', ')}]` : '')\n\t);\n}\n\n/** `HeadBucket`: the bucket's region when it answers, else what S3 said. */\nasync function headBucket(\n\tS3: S3Module,\n\tclient: InstanceType<S3Module['S3Client']>,\n\tbucket: string,\n): Promise<{ ok: true; region?: string } | ({ ok: false } & S3Answer)> {\n\ttry {\n\t\tconst out = await client.send(new S3.HeadBucketCommand({ Bucket: bucket }));\n\t\treturn {\n\t\t\tok: true,\n\t\t\t...(out.BucketRegion ? { region: out.BucketRegion } : {}),\n\t\t};\n\t} catch (error) {\n\t\treturn { ok: false, ...s3Answer(error) };\n\t}\n}\n\n/** `GetBucketLocation`'s answer as a region: none is us-east-1, `EU` eu-west-1. */\nasync function bucketLocation(\n\tS3: S3Module,\n\tclient: InstanceType<S3Module['S3Client']>,\n\tbucket: string,\n): Promise<{ region: string } | { failed: S3Answer }> {\n\ttry {\n\t\tconst out = await client.send(\n\t\t\tnew S3.GetBucketLocationCommand({ Bucket: bucket }),\n\t\t);\n\t\tconst location = out.LocationConstraint as string | undefined;\n\t\treturn {\n\t\t\tregion: !location\n\t\t\t\t? 'us-east-1'\n\t\t\t\t: location === 'EU'\n\t\t\t\t\t? 'eu-west-1'\n\t\t\t\t\t: location,\n\t\t};\n\t} catch (error) {\n\t\treturn { failed: s3Answer(error) };\n\t}\n}\n\n/**\n * Where `bucket` is: `HeadBucket`'s `x-amz-bucket-region` — on a 200, a 301\n * or a 400 alike — else `GetBucketLocation`.\n *\n * @throws {BucketRegionUnknown}\n */\nasync function bucketRegion(clients: Clients, bucket: string): Promise<string> {\n\tconst { S3, s3 } = clients;\n\tconst head = await headBucket(S3, s3, bucket);\n\tif (head.region) return head.region;\n\tconst location = await bucketLocation(S3, s3, bucket);\n\tif ('region' in location) return location.region;\n\tthrow new BucketRegionUnknown(\n\t\tbucket,\n\t\thead.ok ? 'HTTP 200 without x-amz-bucket-region' : describeAnswer(head),\n\t\tdescribeAnswer(location.failed),\n\t);\n}\n\n/** A read whose \"there is none\" answer is one of `missing`. */\nasync function readOr<T>(\n\tread: () => Promise<T>,\n\tmissing: readonly string[],\n): Promise<T | undefined> {\n\ttry {\n\t\treturn await read();\n\t} catch (error) {\n\t\tif (missing.includes(errorName(error))) return undefined;\n\t\tthrow error;\n\t}\n}\n\ninterface Clients {\n\tS3: S3Module;\n\tIAM: IamModule;\n\ts3: InstanceType<S3Module['S3Client']>;\n\tiam: InstanceType<IamModule['IAMClient']>;\n\tregion: string;\n\t/** An emulator's endpoint, when the environment names one. */\n\tendpoint?: string;\n}\n\n/** Clients for a provisioning run, for tests to replace. */\nexport type S3ClientFactory = (\n\tcredential: AwsProvisioning,\n\tregion: string,\n) => Promise<Clients>;\n\nexport const defaultClients: S3ClientFactory = async (credential, region) => {\n\tconst S3 = await import('@aws-sdk/client-s3');\n\tconst IAM = await import('@aws-sdk/client-iam');\n\tconst config = await awsClientConfig(credential, region);\n\tconst endpoint = awsEndpoint(process.env, 'S3');\n\treturn {\n\t\tS3,\n\t\tIAM,\n\t\ts3: new S3.S3Client({\n\t\t\t...config,\n\t\t\t...(endpoint ? { endpoint, forcePathStyle: true } : {}),\n\t\t}),\n\t\tiam: new IAM.IAMClient(config),\n\t\tregion,\n\t\t...(endpoint ? { endpoint } : {}),\n\t};\n};\n\n/** For tests: what makes the clients. */\nlet makeClients: S3ClientFactory = defaultClients;\n\n/** Replace the client factory — a test intercepting a call. Returns the old one. */\nexport function useS3Clients(factory: S3ClientFactory): S3ClientFactory {\n\tconst previous = makeClients;\n\tmakeClients = factory;\n\treturn previous;\n}\n\nasync function ensureS3(ctx: Ctx): Promise<void> {\n\tconst region = s3Region(ctx);\n\tconst clients = await makeClients(ctx.credential, region);\n\tconst watched = bucketWatchers(ctx.manifest);\n\tfor (const construct of bucketConstructs(ctx.manifest)) {\n\t\tawait ensureBucket(ctx, clients, construct, watched.get(construct.id));\n\t}\n}\n\nasync function ensureBucket(\n\tctx: Ctx,\n\tstageClients: Clients,\n\tconstruct: BucketConstruct,\n\t/** Who watches it, when a subscriber does — see `./events.ts`. */\n\twatchers?: readonly BucketWatcher[],\n): Promise<void> {\n\tconst { id } = construct;\n\tconst nameInput = {\n\t\tscope: ctx.identity.scope,\n\t\tstage: ctx.stage,\n\t\tid: appKey(id),\n\t};\n\tconst versioning = ctx.config.versioning ?? construct.versioned;\n\tconst open = [...new Set(construct.servers.flatMap((s) => s.open))].sort();\n\n\tconst located = await ensureBucketName(\n\t\tctx,\n\t\tstageClients,\n\t\tid,\n\t\tbucketName(nameInput),\n\t);\n\tconst { bucket, exists } = located;\n\t// The bucket is reached, and its URL written, in the region it is in —\n\t// one value for both, read off the bucket: an adopted bucket, or one made\n\t// before the stage's region changed, may be elsewhere.\n\tlet clients = stageClients;\n\tif (located.region !== stageClients.region) {\n\t\tclients = await makeClients(ctx.credential, located.region);\n\t\tctx.log(\n\t\t\t` '${bucket}' is in ${located.region}, not ${stageClients.region}: '${id}' is reached and addressed there`,\n\t\t);\n\t}\n\t// Its file servers behind a CDN, unless the stage said not to — first,\n\t// because the bucket's policy grants exactly the distributions it made.\n\tconst cdn =\n\t\tctx.config.cdn !== false && construct.servers.length > 0\n\t\t\t? await ensureBucketCdn(ctx, {\n\t\t\t\t\tbucket,\n\t\t\t\t\tregion: clients.region,\n\t\t\t\t\tservers: construct.servers,\n\t\t\t\t\torigins: bucketOrigins({\n\t\t\t\t\t\tworkspace: ctx.workspace,\n\t\t\t\t\t\tmanifest: ctx.manifest,\n\t\t\t\t\t\tstage: ctx.stage,\n\t\t\t\t\t\tbucket: id,\n\t\t\t\t\t}).origins,\n\t\t\t\t})\n\t\t\t: undefined;\n\tawait ensureBucketSettings(ctx, clients, {\n\t\tid,\n\t\tbucket,\n\t\texists,\n\t\tversioning,\n\t\topen,\n\t\t...(cdn ? { cdn } : {}),\n\t\t...(construct.expire ? { expire: construct.expire } : {}),\n\t});\n\n\t// Watched: the topic it publishes its changes to comes first, because the\n\t// bucket's key is allowed to confirm subscriptions to it.\n\tconst events = watchers\n\t\t? await ensureBucketEvents(ctx, clients, {\n\t\t\t\tid,\n\t\t\t\tbucket,\n\t\t\t\tregion: clients.region,\n\t\t\t})\n\t\t: undefined;\n\n\tconst user = await ensureIamUser(ctx, clients, {\n\t\tid,\n\t\tuser: iamUserName(nameInput),\n\t\tpolicyName: IAM_POLICY_NAME,\n\t\tpolicy: userPolicy(bucket, {\n\t\t\tversioning,\n\t\t\t...(events ? { eventsTopicArn: events.topicArn } : {}),\n\t\t}),\n\t\tdescribe: 'this bucket only',\n\t});\n\tawait ensureAccessKey(ctx, clients, {\n\t\tid,\n\t\tsecret: s3KeySecret(ctx, clients, {\n\t\t\tbucket,\n\t\t\turlKey: provideKey(id, 'url'),\n\t\t}),\n\t\t...user,\n\t});\n\n\tif (events && watchers) {\n\t\tawait ensureEventsKey(ctx, {\n\t\t\tid,\n\t\t\ttopicArn: events.topicArn,\n\t\t\tregion: clients.region,\n\t\t});\n\t\tawait ensurePushSubscriptions(ctx, {\n\t\t\tid,\n\t\t\tevents,\n\t\t\tregion: clients.region,\n\t\t\twatchers,\n\t\t});\n\t}\n\n\t// Each file server over it serves on its CDN, or on the bucket's own\n\t// endpoint — unless the stage set an address of its own for it, which is\n\t// kept.\n\tconst bucketUrl = bucketPublicUrl(bucket, clients.region, clients.endpoint);\n\tfor (const server of construct.servers) {\n\t\tconst publicUrl = cdn?.urls[server.id] ?? bucketUrl;\n\t\tconst key = provideKey(server.id, 'url');\n\t\tconst entry: StateEntry = { key: `file-server:${server.id}`, type: 'url' };\n\t\tconst recorded = ctx.state.record(entry.key)?.id;\n\t\tconst current = ctx.secrets[key];\n\t\tif (current === publicUrl) {\n\t\t\tawait ctx.state.ready(entry, publicUrl);\n\t\t\tcontinue;\n\t\t}\n\t\tif (current !== undefined && current !== recorded) {\n\t\t\tctx.log(\n\t\t\t\t` ${key} is set to ${current} by you, and kept — the bucket's own address is ${publicUrl}`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\t\tawait ctx.change(\n\t\t\t{ construct: server.id, resource: key, change: `set to ${publicUrl}` },\n\t\t\tasync () => {\n\t\t\t\tawait ctx.writeSecrets({ [key]: publicUrl });\n\t\t\t\tawait ctx.state.ready(entry, publicUrl);\n\t\t\t},\n\t\t);\n\t}\n}\n\n/**\n * The bucket's name: the recorded one, else the good name, else the good\n * name with a random suffix — up to {@link SUFFIX_ATTEMPTS} times.\n */\n/**\n * A private bucket a provider makes for a construct that owns one — an\n * inbox's — by the same steps as any of the stage's buckets: found or\n * created, its settings put back where they drifted, and reached by an IAM\n * user of its own whose key is written into `urlKey`. Nothing serves it and\n * nothing watches it.\n */\nexport async function ensureOwnedBucket(\n\tctx: EnsureContext<ProviderConfig & { region?: string }, AwsProvisioning>,\n\tinput: {\n\t\t/** The construct it is for — what state and tags record it under. */\n\t\tid: string;\n\t\t/** What it is named for, after the stage: `requests-mail`. */\n\t\tname: string;\n\t\t/** The secret its URL, key included, is written to. */\n\t\turlKey: string;\n\t\t/** SES may write under a prefix of it. */\n\t\tsesWrite?: { prefix: string; account: string };\n\t\t/** A topic its key confirms subscriptions to. */\n\t\ttopicArn?: string;\n\t},\n): Promise<{ bucket: string; region: string }> {\n\t// The bucket steps read the region and nothing else of the config.\n\tconst bucketCtx = ctx as unknown as Ctx;\n\tconst stageClients = await makeClients(ctx.credential, s3Region(bucketCtx));\n\tconst nameInput = {\n\t\tscope: ctx.identity.scope,\n\t\tstage: ctx.stage,\n\t\tid: input.name,\n\t};\n\tconst located = await ensureBucketName(\n\t\tbucketCtx,\n\t\tstageClients,\n\t\tinput.id,\n\t\tbucketName(nameInput),\n\t);\n\tconst clients =\n\t\tlocated.region === stageClients.region\n\t\t\t? stageClients\n\t\t\t: await makeClients(ctx.credential, located.region);\n\tawait ensureBucketSettings(bucketCtx, clients, {\n\t\tid: input.id,\n\t\tbucket: located.bucket,\n\t\texists: located.exists,\n\t\tversioning: false,\n\t\topen: [],\n\t\t...(input.sesWrite ? { sesWrite: input.sesWrite } : {}),\n\t});\n\tconst user = await ensureIamUser(bucketCtx, clients, {\n\t\tid: input.id,\n\t\tuser: iamUserName(nameInput),\n\t\tpolicyName: IAM_POLICY_NAME,\n\t\tpolicy: userPolicy(\n\t\t\tlocated.bucket,\n\t\t\tinput.topicArn ? { eventsTopicArn: input.topicArn } : {},\n\t\t),\n\t\tdescribe: 'this bucket only',\n\t});\n\tawait ensureAccessKey(bucketCtx, clients, {\n\t\tid: input.id,\n\t\tsecret: s3KeySecret(bucketCtx, clients, {\n\t\t\tbucket: located.bucket,\n\t\t\turlKey: input.urlKey,\n\t\t}),\n\t\t...user,\n\t});\n\treturn { bucket: located.bucket, region: clients.region };\n}\n\nasync function ensureBucketName(\n\tctx: Ctx,\n\tclients: Clients,\n\tid: string,\n\tgood: string,\n): Promise<{ bucket: string; exists: boolean; region: string }> {\n\tconst { S3, s3, region } = clients;\n\tconst key = `s3-bucket:${id}`;\n\tconst record = ctx.state.record(key);\n\tconst entry = (name: string, where: string): StateEntry => ({\n\t\tkey,\n\t\ttype: 's3-bucket',\n\t\tdata: { name, region: where },\n\t});\n\n\t/**\n\t * Whether this account holds `name`: yes, no, or someone else does. Read\n\t * by status — a `HEAD` has no body, so its error is named `Unknown` — and,\n\t * when S3 says the bucket is in another region, asked again there.\n\t */\n\tconst probe = async (name: string): Promise<'ours' | 'none' | 'taken'> => {\n\t\tlet head = await headBucket(S3, s3, name);\n\t\tif (\n\t\t\t!head.ok &&\n\t\t\thead.region &&\n\t\t\thead.region !== region &&\n\t\t\t(head.status === 301 || head.status === 400)\n\t\t) {\n\t\t\tconst there = await makeClients(ctx.credential, head.region);\n\t\t\thead = await headBucket(there.S3, there.s3, name);\n\t\t}\n\t\tif (head.ok) return 'ours';\n\t\tif (head.status === 404) return 'none';\n\t\tif (head.status === 403) return 'taken';\n\t\tthrow new BucketProbeFailed(name, describeAnswer(head));\n\t};\n\n\t/** Where the bucket is now — read, not assumed: it may be adopted. */\n\tconst located = async (name: string, made: string): Promise<string> =>\n\t\tctx.dryRun && made === 'created' ? region : bucketRegion(clients, name);\n\n\tconst create = async (\n\t\tname: string,\n\t): Promise<'created' | 'ours' | 'taken'> => {\n\t\tif (ctx.dryRun) {\n\t\t\tconst found = await probe(name);\n\t\t\tif (found === 'none') {\n\t\t\t\tawait ctx.change(\n\t\t\t\t\t{\n\t\t\t\t\t\tconstruct: id,\n\t\t\t\t\t\tresource: `bucket ${name}`,\n\t\t\t\t\t\tchange: `create in ${region}`,\n\t\t\t\t\t},\n\t\t\t\t\tasync () => {},\n\t\t\t\t);\n\t\t\t\treturn 'created';\n\t\t\t}\n\t\t\treturn found;\n\t\t}\n\t\tawait ctx.state.pending(entry(name, region));\n\t\ttry {\n\t\t\tawait s3.send(\n\t\t\t\tnew S3.CreateBucketCommand({\n\t\t\t\t\tBucket: name,\n\t\t\t\t\t// us-east-1 is the one region named by leaving it out.\n\t\t\t\t\t...(region === 'us-east-1'\n\t\t\t\t\t\t? {}\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tCreateBucketConfiguration: {\n\t\t\t\t\t\t\t\t\tLocationConstraint:\n\t\t\t\t\t\t\t\t\t\tregion as import('@aws-sdk/client-s3').BucketLocationConstraint,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t}),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconst code = errorName(error);\n\t\t\tif (code === 'BucketAlreadyOwnedByYou') return 'ours';\n\t\t\tif (code === 'BucketAlreadyExists') return 'taken';\n\t\t\tthrow error;\n\t\t}\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: id,\n\t\t\t\tresource: `bucket ${name}`,\n\t\t\t\tchange: `create in ${region}`,\n\t\t\t},\n\t\t\tasync () => {},\n\t\t);\n\t\treturn 'created';\n\t};\n\n\tconst adopt = async (name: string, where: string): Promise<void> => {\n\t\tconst there =\n\t\t\twhere === region ? clients : await makeClients(ctx.credential, where);\n\t\tconst tags = await readOr(\n\t\t\t() =>\n\t\t\t\tthere.s3.send(new there.S3.GetBucketTaggingCommand({ Bucket: name })),\n\t\t\t['NoSuchTagSet', 'NoSuchTagSetError'],\n\t\t);\n\t\tconst owner = tags?.TagSet?.find((t) => t.Key === TAG_PROJECT)?.Value;\n\t\tif (owner && owner !== ctx.identity.key) {\n\t\t\tthrow new BucketOwnedByAnotherProject(name, owner, ctx.identity.key);\n\t\t}\n\t};\n\n\t// Recorded: the name is the stage's forever. Gone, it is made again under\n\t// the same name — never a new one.\n\tif (record?.status === 'ready' && record.id) {\n\t\tconst found = await probe(record.id);\n\t\tif (found === 'taken') throw new RecordedBucketUnreachable(id, record.id);\n\t\tconst made = found === 'ours' ? 'ours' : await create(record.id);\n\t\tif (made === 'taken') throw new RecordedBucketUnreachable(id, record.id);\n\t\tconst where = await located(record.id, made);\n\t\tif (!ctx.dryRun) await ctx.state.ready(entry(record.id, where), record.id);\n\t\treturn { bucket: record.id, exists: made === 'ours', region: where };\n\t}\n\n\t// A run that died between creating a suffixed bucket and recording it.\n\tconst pending = record?.data?.name;\n\tconst names = [\n\t\t...(typeof pending === 'string' && pending !== good ? [pending] : []),\n\t\tgood,\n\t\t...Array.from({ length: SUFFIX_ATTEMPTS }, () =>\n\t\t\tsuffixedBucketName(good, randomSuffix()),\n\t\t),\n\t];\n\tconst tried: string[] = [];\n\tfor (const name of names) {\n\t\ttried.push(name);\n\t\tconst made = await create(name);\n\t\tif (made === 'taken') continue;\n\t\tconst where = await located(name, made);\n\t\tif (made === 'ours') await adopt(name, where);\n\t\tif (!ctx.dryRun) await ctx.state.ready(entry(name, where), name);\n\t\tif (name !== good) {\n\t\t\tctx.log(\n\t\t\t\t` '${good}' is taken by another account; '${id}' is '${name}', recorded in the stage's state`,\n\t\t\t);\n\t\t}\n\t\treturn { bucket: name, exists: made === 'ours', region: where };\n\t}\n\tthrow new BucketNameUnavailable(\n\t\tid,\n\t\ttried.filter((n) => n !== pending),\n\t);\n}\n\n/**\n * Tags, Block Public Access, encryption, policy, versioning, lifecycle and\n * CORS.\n */\nasync function ensureBucketSettings(\n\tctx: Ctx,\n\tclients: Clients,\n\tinput: {\n\t\tid: string;\n\t\tbucket: string;\n\t\t/** False when a dry run would create it: nothing to read yet. */\n\t\texists: boolean;\n\t\tversioning: boolean;\n\t\topen: readonly string[];\n\t\t/** The distributions serving it, when a CDN does. */\n\t\tcdn?: { distributionArns: readonly string[] };\n\t\texpire?: Readonly<Record<string, number>>;\n\t\t/** SES writes received mail under a prefix — an inbox's bucket. */\n\t\tsesWrite?: { prefix: string; account: string };\n\t},\n): Promise<void> {\n\tconst { S3, s3 } = clients;\n\tconst { id, bucket } = input;\n\tconst Bucket = bucket;\n\t// A bucket a dry run would create has nothing to read: every setting\n\t// would be set.\n\tconst fresh = ctx.dryRun && !input.exists;\n\tconst read = async <T>(\n\t\tfn: () => Promise<T>,\n\t\tmissing: readonly string[],\n\t): Promise<T | undefined> => (fresh ? undefined : readOr(fn, missing));\n\tconst set = (change: string, apply: () => Promise<unknown>) =>\n\t\tctx.change({ construct: id, resource: `bucket ${bucket}`, change }, () =>\n\t\t\tapply().then(() => undefined),\n\t\t);\n\n\t// Tags — gkm's three, beside whatever else is there.\n\tconst tagged = await read(\n\t\t() => s3.send(new S3.GetBucketTaggingCommand({ Bucket })),\n\t\t['NoSuchTagSet', 'NoSuchTagSetError'],\n\t);\n\tconst ours = identityTags(ctx, id);\n\tconst others = (tagged?.TagSet ?? []).filter(\n\t\t(t) => !ours.some((o) => o.Key === t.Key),\n\t);\n\tconst missingTag = ours.some(\n\t\t(o) => !tagged?.TagSet?.some((t) => t.Key === o.Key && t.Value === o.Value),\n\t);\n\tif (missingTag) {\n\t\tawait set('tag', () =>\n\t\t\ts3.send(\n\t\t\t\tnew S3.PutBucketTaggingCommand({\n\t\t\t\t\tBucket,\n\t\t\t\t\tTagging: {\n\t\t\t\t\t\tTagSet: [...others, ...ours].map((t) => ({\n\t\t\t\t\t\t\tKey: t.Key!,\n\t\t\t\t\t\t\tValue: t.Value!,\n\t\t\t\t\t\t})),\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t),\n\t\t);\n\t}\n\n\t// Behind a CDN the bucket is never public: CloudFront alone reads it.\n\tconst isOpen = input.open.length > 0 && !input.cdn;\n\tconst block = publicAccessBlock(isOpen);\n\tconst ensureBlock = async () => {\n\t\tconst current = await read(\n\t\t\t() => s3.send(new S3.GetPublicAccessBlockCommand({ Bucket })),\n\t\t\t['NoSuchPublicAccessBlockConfiguration'],\n\t\t);\n\t\tconst now = current?.PublicAccessBlockConfiguration;\n\t\tconst same =\n\t\t\tnow &&\n\t\t\t(Object.keys(block) as (keyof typeof block)[]).every(\n\t\t\t\t(k) => (now[k] ?? false) === block[k],\n\t\t\t);\n\t\tif (!same) {\n\t\t\tawait set(\n\t\t\t\tisOpen\n\t\t\t\t\t? 'block public access (ACLs; the policy opens the open paths)'\n\t\t\t\t\t: 'block all public access',\n\t\t\t\t() =>\n\t\t\t\t\ts3.send(\n\t\t\t\t\t\tnew S3.PutPublicAccessBlockCommand({\n\t\t\t\t\t\t\tBucket,\n\t\t\t\t\t\t\tPublicAccessBlockConfiguration: block,\n\t\t\t\t\t\t}),\n\t\t\t\t\t),\n\t\t\t);\n\t\t}\n\t};\n\n\tconst policy = bucketPolicy(bucket, input.open, {\n\t\t...(input.cdn ? { cdn: input.cdn } : {}),\n\t\t...(input.sesWrite ? { sesWrite: input.sesWrite } : {}),\n\t});\n\tconst ensurePolicy = async () => {\n\t\tconst current = await read(\n\t\t\t() => s3.send(new S3.GetBucketPolicyCommand({ Bucket })),\n\t\t\t['NoSuchBucketPolicy'],\n\t\t);\n\t\tconst same =\n\t\t\tcurrent?.Policy !== undefined &&\n\t\t\tcanonicalPolicy(JSON.parse(current.Policy)) === canonicalPolicy(policy);\n\t\tif (!same) {\n\t\t\tawait set(\n\t\t\t\tisOpen\n\t\t\t\t\t? `policy: TLS only, public read on ${input.open.join(', ')}`\n\t\t\t\t\t: input.cdn && input.open.length > 0\n\t\t\t\t\t\t? `policy: TLS only, CloudFront reads ${input.open.join(', ')}`\n\t\t\t\t\t\t: 'policy: TLS only',\n\t\t\t\t() =>\n\t\t\t\t\ts3.send(\n\t\t\t\t\t\tnew S3.PutBucketPolicyCommand({\n\t\t\t\t\t\t\tBucket,\n\t\t\t\t\t\t\tPolicy: JSON.stringify(policy),\n\t\t\t\t\t\t}),\n\t\t\t\t\t),\n\t\t\t);\n\t\t}\n\t};\n\n\t// A public policy is refused while BlockPublicPolicy is on, and a block\n\t// put back while one is in place would refuse the next put: relax the\n\t// block before opening, and close the policy before blocking.\n\tif (isOpen) {\n\t\tawait ensureBlock();\n\t\tawait ensurePolicy();\n\t} else {\n\t\tawait ensurePolicy();\n\t\tawait ensureBlock();\n\t}\n\n\t// Default encryption: SSE-S3.\n\tconst encryption = await read(\n\t\t() => s3.send(new S3.GetBucketEncryptionCommand({ Bucket })),\n\t\t['ServerSideEncryptionConfigurationNotFoundError'],\n\t);\n\tconst algorithm =\n\t\tencryption?.ServerSideEncryptionConfiguration?.Rules?.[0]\n\t\t\t?.ApplyServerSideEncryptionByDefault?.SSEAlgorithm;\n\t// A new bucket is SSE-S3 already — S3 has encrypted every new bucket by\n\t// default since 2023 — so a dry run does not promise to set it.\n\tif (algorithm !== 'AES256' && !fresh) {\n\t\tawait set('encrypt by default (SSE-S3)', () =>\n\t\t\ts3.send(\n\t\t\t\tnew S3.PutBucketEncryptionCommand({\n\t\t\t\t\tBucket,\n\t\t\t\t\tServerSideEncryptionConfiguration: {\n\t\t\t\t\t\tRules: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tApplyServerSideEncryptionByDefault: { SSEAlgorithm: 'AES256' },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t),\n\t\t);\n\t}\n\n\t// Versioning: on when asked; a bucket that had it is suspended, never\n\t// stripped of the versions it kept.\n\tconst versioning = await read(\n\t\t() => s3.send(new S3.GetBucketVersioningCommand({ Bucket })),\n\t\t[],\n\t);\n\tconst status = versioning?.Status;\n\tif (input.versioning && status !== 'Enabled') {\n\t\tawait set('enable versioning', () =>\n\t\t\ts3.send(\n\t\t\t\tnew S3.PutBucketVersioningCommand({\n\t\t\t\t\tBucket,\n\t\t\t\t\tVersioningConfiguration: { Status: 'Enabled' },\n\t\t\t\t}),\n\t\t\t),\n\t\t);\n\t} else if (!input.versioning && status === 'Enabled') {\n\t\tawait set('suspend versioning', () =>\n\t\t\ts3.send(\n\t\t\t\tnew S3.PutBucketVersioningCommand({\n\t\t\t\t\tBucket,\n\t\t\t\t\tVersioningConfiguration: { Status: 'Suspended' },\n\t\t\t\t}),\n\t\t\t),\n\t\t);\n\t}\n\n\t// Lifecycle: the declared expiry, beside whatever other rules the bucket\n\t// already has — one document per bucket, so gkm rewrites only its own.\n\tconst lifecycle = await read(\n\t\t() => s3.send(new S3.GetBucketLifecycleConfigurationCommand({ Bucket })),\n\t\t['NoSuchLifecycleConfiguration'],\n\t);\n\tconst merge = mergeLifecycle(lifecycle?.Rules, input.expire);\n\tif (merge.changed) {\n\t\tawait set(merge.describe, () =>\n\t\t\tmerge.rules.length > 0\n\t\t\t\t? s3.send(\n\t\t\t\t\t\tnew S3.PutBucketLifecycleConfigurationCommand({\n\t\t\t\t\t\t\tBucket,\n\t\t\t\t\t\t\tLifecycleConfiguration: { Rules: merge.rules },\n\t\t\t\t\t\t}),\n\t\t\t\t\t)\n\t\t\t\t: // A lifecycle with no rules is refused: gkm's were the last.\n\t\t\t\t\ts3.send(new S3.DeleteBucketLifecycleCommand({ Bucket })),\n\t\t);\n\t}\n\n\t// CORS for the stage's sites.\n\tconst { origins, unresolved } = bucketOrigins({\n\t\tworkspace: ctx.workspace,\n\t\tmanifest: ctx.manifest,\n\t\tstage: ctx.stage,\n\t\tbucket: id,\n\t});\n\tif (unresolved.length > 0) {\n\t\tctx.log(\n\t\t\t` ${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`,\n\t\t);\n\t}\n\tif (origins.length === 0) return;\n\tconst rule = corsRule(origins);\n\tconst cors = await read(\n\t\t() => s3.send(new S3.GetBucketCorsCommand({ Bucket })),\n\t\t['NoSuchCORSConfiguration'],\n\t);\n\tconst sameCors =\n\t\tcors?.CORSRules?.length === 1 &&\n\t\tcanonicalCors(cors.CORSRules[0]!) === canonicalCors(rule);\n\tif (!sameCors) {\n\t\tawait set(`CORS for ${origins.join(', ')}`, () =>\n\t\t\ts3.send(\n\t\t\t\tnew S3.PutBucketCorsCommand({\n\t\t\t\t\tBucket,\n\t\t\t\t\tCORSConfiguration: { CORSRules: [rule] },\n\t\t\t\t}),\n\t\t\t),\n\t\t);\n\t}\n}\n\nfunction canonicalCors(rule: Partial<CorsRule>): string {\n\tconst sorted = (list?: string[]) => [...(list ?? [])].sort();\n\treturn JSON.stringify({\n\t\tAllowedMethods: sorted(rule.AllowedMethods),\n\t\tAllowedOrigins: sorted(rule.AllowedOrigins),\n\t\tAllowedHeaders: sorted(rule.AllowedHeaders),\n\t\tExposeHeaders: sorted(rule.ExposeHeaders),\n\t\tMaxAgeSeconds: rule.MaxAgeSeconds ?? null,\n\t});\n}\n\n/**\n * Each provisioned bucket answers `HeadBucket` to the key in the stage's\n * secrets — the one the apps will use. A bucket whose URL carries no key is\n * not one this provider wrote, and the deploy's own check covers a URL that\n * is missing.\n *\n * A failure is read by its status, never its name — a `HEAD` has no body,\n * so SDK v3 calls a 301, 400 or 403 `Unknown` — and the message carries\n * everything S3 said: status, error, region, request ids.\n *\n * @throws {BucketRegionMismatch} the bucket is in another region than its URL says\n * @throws {ProvisionedBucketUnreachable}\n */\nasync function verifyS3(ctx: VerifyContext<S3ObjectsConfig>): Promise<void> {\n\tconst S3 = await import('@aws-sdk/client-s3');\n\tfor (const { id } of bucketConstructs(ctx.manifest)) {\n\t\tconst urlKey = provideKey(id, 'url');\n\t\tconst url = ctx.secrets[urlKey];\n\t\tif (url === undefined) continue;\n\t\tconst address = s3Url.parse(url);\n\t\tif (!address.accessKeyId || !address.secretAccessKey) continue;\n\t\tconst asked = address.region ?? ctx.config.region ?? 'us-east-1';\n\t\tconst client = new S3.S3Client({\n\t\t\tregion: asked,\n\t\t\tcredentials: {\n\t\t\t\taccessKeyId: address.accessKeyId,\n\t\t\t\tsecretAccessKey: address.secretAccessKey,\n\t\t\t},\n\t\t\t...(address.endpoint ? { endpoint: address.endpoint } : {}),\n\t\t\t...(address.forcePathStyle ? { forcePathStyle: true } : {}),\n\t\t});\n\t\ttry {\n\t\t\t// A key IAM issued moments ago is refused until it propagates.\n\t\t\tconst issuedAt = issuedJustNow(ctx.resources, id, address.accessKeyId);\n\t\t\tconst { answer: head, waited } = await whileKeyIsNew(\n\t\t\t\tawait headBucket(S3, client, address.bucket),\n\t\t\t\t{\n\t\t\t\t\tissuedAt,\n\t\t\t\t\trefused: (answer) => !answer.ok && answer.status === 403,\n\t\t\t\t\task: () => headBucket(S3, client, address.bucket),\n\t\t\t\t\tonWait: () =>\n\t\t\t\t\t\tctx.log?.(\n\t\t\t\t\t\t\t` ${id}: waiting for the new key ${address.accessKeyId} to become active…`,\n\t\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t);\n\t\t\tif (head.ok) continue;\n\n\t\t\tconst fail = (reason: string) =>\n\t\t\t\tnew ProvisionedBucketUnreachable(id, address.bucket, ctx.stage, reason);\n\t\t\tconst said = describeAnswer(head);\n\n\t\t\t// In another region: S3 redirects, or refuses the signature, and\n\t\t\t// says where.\n\t\t\tif (\n\t\t\t\thead.status === 301 ||\n\t\t\t\t(head.status === 400 && head.region && head.region !== asked)\n\t\t\t) {\n\t\t\t\tlet where = head.region;\n\t\t\t\tlet located = '';\n\t\t\t\tif (!where) {\n\t\t\t\t\tconst location = await bucketLocation(S3, client, address.bucket);\n\t\t\t\t\tif ('region' in location) where = location.region;\n\t\t\t\t\telse\n\t\t\t\t\t\tlocated = `; GetBucketLocation answered ${describeAnswer(location.failed)}`;\n\t\t\t\t}\n\t\t\t\tif (where && where !== asked) {\n\t\t\t\t\tthrow new BucketRegionMismatch(\n\t\t\t\t\t\tid,\n\t\t\t\t\t\taddress.bucket,\n\t\t\t\t\t\tctx.stage,\n\t\t\t\t\t\turlKey,\n\t\t\t\t\t\twhere,\n\t\t\t\t\t\taddress.region,\n\t\t\t\t\t\tasked,\n\t\t\t\t\t\tsaid,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tthrow fail(`S3 redirected the request (${said}${located})`);\n\t\t\t}\n\t\t\tif (head.status === 404) throw fail(`it does not exist (${said})`);\n\t\t\tif (head.status === 403) {\n\t\t\t\tthrow fail(\n\t\t\t\t\t'the key is refused — AccessDenied, or an invalid or ' +\n\t\t\t\t\t\t`not-yet-active key (${said})` +\n\t\t\t\t\t\t(issuedAt\n\t\t\t\t\t\t\t? `. The key was issued at ${issuedAt} by this deploy, and IAM ` +\n\t\t\t\t\t\t\t\t'keys take seconds to become usable: it was still refused ' +\n\t\t\t\t\t\t\t\t`after waiting ${Math.round(waited / 1000)}s`\n\t\t\t\t\t\t\t: ''),\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow fail(said);\n\t\t} finally {\n\t\t\tclient.destroy();\n\t\t}\n\t}\n}\n\nexport const s3Provider: ResourceProvider<S3ObjectsConfig, AwsProvisioning> = {\n\tkind: 'objects',\n\tname: 's3',\n\tcheck: checkS3Config,\n\tprovisioning: S3_PROVISIONING,\n\tcredentials: awsProvisioningCredentials,\n\truntimeKeys(manifest) {\n\t\tconst watched = bucketWatchers(manifest);\n\t\treturn bucketConstructs(manifest).flatMap((b) => [\n\t\t\tprovideKey(b.id, 'url'),\n\t\t\t...(watched.has(b.id)\n\t\t\t\t? [provideKey(b.id, 'eventsConnectionString')]\n\t\t\t\t: []),\n\t\t\t...b.servers.map((s) => provideKey(s.id, 'url')),\n\t\t]);\n\t},\n\tensure: ensureS3,\n\tverify: verifyS3,\n};\n","/**\n * The `ses` provider: what receives an inbox's mail on a deployed stage.\n *\n * `deploy.inbox.<stage> = { provider: 'ses', region? }` has the deploy create,\n * for each `Inbox`:\n *\n * - **its own bucket** — never one the stack or `deploy.objects` runs — by\n * the steps every bucket of the stage takes (`ensureOwnedBucket`), with SES\n * allowed to write under `messages/` and an IAM user of its own whose key\n * is the inbox's `<ID>_URL`;\n * - **its topic** on SNS, publishable by SES alone, whose `sns://` address —\n * signed by the same key — is the topic's connection string. The deploy's\n * push subscriptions for a topic on SNS (`topicPushes`) then subscribe each\n * inbox subscriber at its worker's route, unfiltered;\n * - **the stage's domain as an SES identity**, verified through Easy DKIM's\n * CNAMEs, and **MX records** pointing the domain — or every subdomain of it,\n * with `subdomains: true` — at the region's inbound endpoint, both through\n * the stage's DNS;\n * - **a receipt rule** in the region's *active* rule set: the inbox's\n * recipients, stored under `messages/` and announced on its topic. Never a\n * new set made active over another — only one is active per region, and\n * switching would stop every other stage's and app's rules. A set is made\n * active only where none is.\n *\n * A removed inbox's rule is deleted on the next deploy, by its name; nothing\n * else this provider did not make is touched.\n */\n\nimport {\n\ttype ConstructManifest,\n\tINBOX_MESSAGES_PREFIX,\n\ttype InboxDeclaration,\n\tprovideKey,\n\treceiptRecipients,\n} from '@geekmidas/manifest';\nimport { NoDomainForStage } from '../../target/dokploy/domain.js';\nimport { appKey } from '../../workspace/derive.js';\nimport {\n\ttype AwsProvisioning,\n\tawsClientConfig,\n\tawsEndpoint,\n\tawsProvisioningCredentials,\n} from '../aws.js';\nimport { ensureEventsKey, ensureTopic, eventsClients } from '../s3/events.js';\nimport { ensureOwnedBucket, s3Region } from '../s3/index.js';\nimport type { PolicyDocument } from '../s3/policy.js';\nimport type {\n\tEnsureContext,\n\tResourceProvider,\n\tVerifyContext,\n} from '../types.js';\nimport {\n\tcheckSesConfig,\n\tSES_PROVISIONING,\n\ttype SesInboxConfig,\n} from './config.js';\nimport { InboxNotProvisioned, SesReceivingUnsupported } from './errors.js';\n\ntype Ctx = EnsureContext<SesInboxConfig, AwsProvisioning>;\ntype SesModule = typeof import('@aws-sdk/client-ses');\n\n/**\n * The regions SES receives mail in, each at `inbound-smtp.<region>.amazonaws.com`\n * — the AWS General Reference's \"Email Receiving endpoints\".\n */\nexport const SES_RECEIVING_REGIONS: readonly string[] = [\n\t'us-east-1',\n\t'us-east-2',\n\t'us-west-1',\n\t'us-west-2',\n\t'af-south-1',\n\t'ap-southeast-3',\n\t'ap-south-1',\n\t'ap-northeast-3',\n\t'ap-northeast-2',\n\t'ap-southeast-1',\n\t'ap-southeast-2',\n\t'ap-northeast-1',\n\t'ca-central-1',\n\t'eu-central-1',\n\t'eu-west-1',\n\t'eu-west-2',\n\t'eu-south-1',\n\t'eu-west-3',\n\t'eu-north-1',\n\t'il-central-1',\n\t'me-south-1',\n\t'sa-east-1',\n];\n\n/** Where mail for a domain is delivered in `region`: its MX target. */\nexport function inboundEndpoint(region: string): string {\n\treturn `inbound-smtp.${region}.amazonaws.com`;\n}\n\n/** The rule set made active where a region has none. */\nexport const DEFAULT_RULE_SET = 'gkm';\n\n/** Every inbox in the manifest. */\nexport function inboxConstructs(\n\tmanifest: ConstructManifest,\n): InboxDeclaration[] {\n\treturn Object.values(manifest)\n\t\t.filter((d): d is InboxDeclaration => d.kind === 'inbox')\n\t\t.sort((a, b) => a.id.localeCompare(b.id));\n}\n\n/** The name an inbox's receipt rule has: the prefix it is found by, and its id. */\nexport function receiptRuleName(\n\tscope: string,\n\tstage: string,\n\tinbox: string,\n): string {\n\treturn `${rulePrefix(scope, stage)}${appKey(inbox)}`.slice(0, 64);\n}\n\n/** What every rule this stage made is named with. */\nfunction rulePrefix(scope: string, stage: string): string {\n\treturn `gkm-${scope}-${stage}-`.replace(/[^A-Za-z0-9_.-]/g, '-');\n}\n\n/** The topic SES announces an inbox's mail on: publishable by SES, from this account. */\nexport function receiptTopicPolicy(\n\ttopicArn: string,\n\taccount: string,\n): PolicyDocument {\n\treturn {\n\t\tVersion: '2012-10-17',\n\t\tStatement: [\n\t\t\t{\n\t\t\t\tSid: 'GkmSesPublishes',\n\t\t\t\tEffect: 'Allow',\n\t\t\t\tPrincipal: { Service: 'ses.amazonaws.com' },\n\t\t\t\tAction: 'sns:Publish',\n\t\t\t\tResource: topicArn,\n\t\t\t\tCondition: { StringEquals: { 'aws:SourceAccount': account } },\n\t\t\t},\n\t\t],\n\t};\n}\n\nexport interface SesClients {\n\tSES: SesModule;\n\tses: InstanceType<SesModule['SESClient']>;\n}\n\n/** Clients for SES, for tests to replace. */\nexport type SesClientFactory = (\n\tcredential: AwsProvisioning,\n\tregion: string,\n) => Promise<SesClients>;\n\nexport const defaultSesClients: SesClientFactory = async (\n\tcredential,\n\tregion,\n) => {\n\tconst SES = await import('@aws-sdk/client-ses');\n\tconst config = await awsClientConfig(credential, region);\n\tconst endpoint = awsEndpoint(process.env, 'SES');\n\treturn {\n\t\tSES,\n\t\tses: new SES.SESClient({ ...config, ...(endpoint ? { endpoint } : {}) }),\n\t};\n};\n\nlet makeSesClients: SesClientFactory = defaultSesClients;\n\n/** The stage's SES, through the factory a test may replace. */\nexport function sesClients(\n\tcredential: AwsProvisioning,\n\tregion: string,\n): Promise<SesClients> {\n\treturn makeSesClients(credential, region);\n}\n\n/** Replace the client factory — a test intercepting a call. Returns the old one. */\nexport function useSesClients(factory: SesClientFactory): SesClientFactory {\n\tconst previous = makeSesClients;\n\tmakeSesClients = factory;\n\treturn previous;\n}\n\nfunction errorName(error: unknown): string {\n\tconst e = error as { name?: string; Code?: string; code?: string };\n\treturn e?.name ?? e?.Code ?? e?.code ?? '';\n}\n\n/** The stage's domain, which every inbox receives on. */\nfunction stageDomain(ctx: Pick<Ctx, 'workspace' | 'stage'>): string {\n\tconst domain = ctx.workspace.domains?.[ctx.stage];\n\tif (!domain) throw new NoDomainForStage(ctx.stage);\n\treturn domain;\n}\n\nasync function ensureSes(ctx: Ctx): Promise<void> {\n\tconst region = s3Region(ctx as never);\n\tif (!SES_RECEIVING_REGIONS.includes(region)) {\n\t\tthrow new SesReceivingUnsupported(ctx.stage, region, SES_RECEIVING_REGIONS);\n\t}\n\tconst inboxes = inboxConstructs(ctx.manifest);\n\tconst { SES, ses } = await makeSesClients(ctx.credential, region);\n\ttry {\n\t\tconst ruleSet = await activeRuleSet(ctx, { SES, ses }, inboxes.length > 0);\n\t\tif (inboxes.length > 0) {\n\t\t\tconst domain = stageDomain(ctx);\n\t\t\tawait ensureIdentity(ctx, { SES, ses }, domain);\n\t\t\tconst events = await eventsClients(ctx.credential, region);\n\t\t\ttry {\n\t\t\t\tfor (const inbox of inboxes) {\n\t\t\t\t\tawait ensureInbox(ctx, { SES, ses }, events, {\n\t\t\t\t\t\tinbox,\n\t\t\t\t\t\tdomain,\n\t\t\t\t\t\tregion,\n\t\t\t\t\t\truleSet: ruleSet!,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tevents.sns.destroy();\n\t\t\t\tevents.sqs.destroy();\n\t\t\t}\n\t\t\tawait ensureMx(ctx, inboxes, domain, region);\n\t\t}\n\t\tif (ruleSet) await retireRules(ctx, { SES, ses }, ruleSet, inboxes);\n\t} finally {\n\t\tses.destroy();\n\t}\n}\n\n/**\n * The region's active rule set. Made — and made active — only where none is\n * active and an inbox needs one: activating a set deactivates whichever was.\n */\nasync function activeRuleSet(\n\tctx: Ctx,\n\t{ SES, ses }: SesClients,\n\tneeded: boolean,\n): Promise<string | undefined> {\n\tconst active = await ses.send(\n\t\tnew SES.DescribeActiveReceiptRuleSetCommand({}),\n\t);\n\tconst name = active.Metadata?.Name;\n\tif (name || !needed) return name;\n\n\tawait ctx.change(\n\t\t{\n\t\t\tconstruct: 'inbox',\n\t\t\tresource: `SES receipt rule set ${DEFAULT_RULE_SET}`,\n\t\t\tchange: 'create and make active — the region has none',\n\t\t},\n\t\tasync () => {\n\t\t\ttry {\n\t\t\t\tawait ses.send(\n\t\t\t\t\tnew SES.CreateReceiptRuleSetCommand({\n\t\t\t\t\t\tRuleSetName: DEFAULT_RULE_SET,\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t} catch (error) {\n\t\t\t\tif (errorName(error) !== 'AlreadyExistsException') throw error;\n\t\t\t}\n\t\t\tawait ses.send(\n\t\t\t\tnew SES.SetActiveReceiptRuleSetCommand({\n\t\t\t\t\tRuleSetName: DEFAULT_RULE_SET,\n\t\t\t\t}),\n\t\t\t);\n\t\t},\n\t);\n\treturn DEFAULT_RULE_SET;\n}\n\n/**\n * The stage's domain as an SES identity, verified by Easy DKIM: its three\n * CNAMEs, through the stage's DNS. Receiving requires a verified domain.\n */\nasync function ensureIdentity(\n\tctx: Ctx,\n\t{ SES, ses }: SesClients,\n\tdomain: string,\n): Promise<void> {\n\tlet tokens: string[] = [];\n\tawait ctx.change(\n\t\t{\n\t\t\tconstruct: 'inbox',\n\t\t\tresource: `SES identity ${domain}`,\n\t\t\tchange: 'verify with DKIM',\n\t\t},\n\t\tasync () => {\n\t\t\tconst dkim = await ses.send(\n\t\t\t\tnew SES.VerifyDomainDkimCommand({ Domain: domain }),\n\t\t\t);\n\t\t\ttokens = dkim.DkimTokens ?? [];\n\t\t},\n\t);\n\tif (tokens.length === 0) return;\n\tawait ctx.writeDns(\n\t\ttokens.map((token) => ({\n\t\t\thost: `${token}._domainkey.${domain}`,\n\t\t\ttype: 'CNAME' as const,\n\t\t\tvalue: `${token}.dkim.amazonses.com`,\n\t\t})),\n\t);\n}\n\n/** One inbox: its bucket, its topic and their keys, and its receipt rule. */\nasync function ensureInbox(\n\tctx: Ctx,\n\t{ SES, ses }: SesClients,\n\tevents: Awaited<ReturnType<typeof eventsClients>>,\n\tinput: {\n\t\tinbox: InboxDeclaration;\n\t\tdomain: string;\n\t\tregion: string;\n\t\truleSet: string;\n\t},\n): Promise<void> {\n\tconst { inbox, domain, region, ruleSet } = input;\n\tconst name = `${appKey(inbox.id)}-mail`;\n\tconst urlKey = provideKey(inbox.id, 'url');\n\n\tconst topicArn = await ensureTopic(ctx as never, events, {\n\t\tid: inbox.id,\n\t\ttopicName: `gkm-${ctx.identity.scope}-${ctx.stage}-${name}`\n\t\t\t.replace(/[^A-Za-z0-9_-]/g, '-')\n\t\t\t.slice(0, 256),\n\t\tpublisher: 'SES',\n\t\tpolicy: (arn) => receiptTopicPolicy(arn, events.account),\n\t});\n\n\tconst { bucket } = await ensureOwnedBucket(ctx, {\n\t\tid: inbox.id,\n\t\tname,\n\t\turlKey,\n\t\tsesWrite: { prefix: INBOX_MESSAGES_PREFIX, account: events.account },\n\t\ttopicArn,\n\t});\n\n\tawait ensureEventsKey(ctx, {\n\t\tid: inbox.id,\n\t\ttopicArn,\n\t\tregion,\n\t\tkey: provideKey(inbox.topic, 'publisherConnectionString'),\n\t\turlKey,\n\t});\n\n\tconst rule = {\n\t\tName: receiptRuleName(ctx.identity.scope, ctx.stage, inbox.id),\n\t\tEnabled: true,\n\t\tTlsPolicy: 'Optional' as const,\n\t\tScanEnabled: true,\n\t\tRecipients: receiptRecipients(inbox, domain),\n\t\tActions: [\n\t\t\t{\n\t\t\t\tS3Action: {\n\t\t\t\t\tBucketName: bucket,\n\t\t\t\t\tObjectKeyPrefix: INBOX_MESSAGES_PREFIX,\n\t\t\t\t\tTopicArn: topicArn,\n\t\t\t\t},\n\t\t\t},\n\t\t],\n\t};\n\tconst current = await ses\n\t\t.send(\n\t\t\tnew SES.DescribeReceiptRuleCommand({\n\t\t\t\tRuleSetName: ruleSet,\n\t\t\t\tRuleName: rule.Name,\n\t\t\t}),\n\t\t)\n\t\t.then(\n\t\t\t(found) => found.Rule,\n\t\t\t(error) => {\n\t\t\t\tif (errorName(error) === 'RuleDoesNotExistException') return undefined;\n\t\t\t\tthrow error;\n\t\t\t},\n\t\t);\n\tconst entry = {\n\t\tkey: `ses-receipt-rule:${inbox.id}`,\n\t\ttype: 'ses-receipt-rule',\n\t};\n\tif (current && sameRule(current, rule)) {\n\t\tawait ctx.state.ready(entry, rule.Name);\n\t\treturn;\n\t}\n\tawait ctx.change(\n\t\t{\n\t\t\tconstruct: inbox.id,\n\t\t\tresource: `SES receipt rule ${rule.Name}`,\n\t\t\tchange: current\n\t\t\t\t? `receive at ${rule.Recipients.join(', ')}`\n\t\t\t\t: `create, receiving at ${rule.Recipients.join(', ')}`,\n\t\t},\n\t\tasync () => {\n\t\t\tawait ctx.state.pending(entry);\n\t\t\tawait ses.send(\n\t\t\t\tcurrent\n\t\t\t\t\t? new SES.UpdateReceiptRuleCommand({\n\t\t\t\t\t\t\tRuleSetName: ruleSet,\n\t\t\t\t\t\t\tRule: rule,\n\t\t\t\t\t\t})\n\t\t\t\t\t: new SES.CreateReceiptRuleCommand({\n\t\t\t\t\t\t\tRuleSetName: ruleSet,\n\t\t\t\t\t\t\tRule: rule,\n\t\t\t\t\t\t}),\n\t\t\t);\n\t\t\tawait ctx.state.ready(entry, rule.Name);\n\t\t},\n\t);\n}\n\nfunction sameRule(\n\tcurrent: {\n\t\tEnabled?: boolean;\n\t\tTlsPolicy?: string;\n\t\tScanEnabled?: boolean;\n\t\tRecipients?: string[];\n\t\tActions?: {\n\t\t\tS3Action?: {\n\t\t\t\tBucketName?: string;\n\t\t\t\tObjectKeyPrefix?: string;\n\t\t\t\tTopicArn?: string;\n\t\t\t};\n\t\t}[];\n\t},\n\twanted: {\n\t\tEnabled: boolean;\n\t\tTlsPolicy: string;\n\t\tScanEnabled: boolean;\n\t\tRecipients: string[];\n\t\tActions: {\n\t\t\tS3Action: {\n\t\t\t\tBucketName: string;\n\t\t\t\tObjectKeyPrefix: string;\n\t\t\t\tTopicArn: string;\n\t\t\t};\n\t\t}[];\n\t},\n): boolean {\n\tconst s3 = current.Actions?.[0]?.S3Action;\n\tconst want = wanted.Actions[0]!.S3Action;\n\treturn (\n\t\tcurrent.Enabled === wanted.Enabled &&\n\t\tcurrent.TlsPolicy === wanted.TlsPolicy &&\n\t\tcurrent.ScanEnabled === wanted.ScanEnabled &&\n\t\tcurrent.Actions?.length === 1 &&\n\t\ts3?.BucketName === want.BucketName &&\n\t\ts3?.ObjectKeyPrefix === want.ObjectKeyPrefix &&\n\t\ts3?.TopicArn === want.TopicArn &&\n\t\t[...(current.Recipients ?? [])].sort().join() ===\n\t\t\t[...wanted.Recipients].sort().join()\n\t);\n}\n\n/**\n * MX records for the stage's domain — and, for an inbox on its tenants'\n * subdomains, for every subdomain — at the region's inbound endpoint.\n */\nasync function ensureMx(\n\tctx: Ctx,\n\tinboxes: readonly InboxDeclaration[],\n\tdomain: string,\n\tregion: string,\n): Promise<void> {\n\tconst hosts = new Set<string>();\n\tfor (const inbox of inboxes) {\n\t\thosts.add(inbox.subdomains ? `*.${domain}` : domain);\n\t}\n\tawait ctx.writeDns(\n\t\t[...hosts].sort().map((host) => ({\n\t\t\thost,\n\t\t\ttype: 'MX' as const,\n\t\t\tvalue: inboundEndpoint(region),\n\t\t\tpriority: 10,\n\t\t})),\n\t);\n}\n\n/** Delete the rules this stage made for inboxes no longer declared — only those. */\nasync function retireRules(\n\tctx: Ctx,\n\t{ SES, ses }: SesClients,\n\truleSet: string,\n\tinboxes: readonly InboxDeclaration[],\n): Promise<void> {\n\tconst prefix = rulePrefix(ctx.identity.scope, ctx.stage);\n\tconst kept = new Set(\n\t\tinboxes.map((inbox) =>\n\t\t\treceiptRuleName(ctx.identity.scope, ctx.stage, inbox.id),\n\t\t),\n\t);\n\tconst set = await ses.send(\n\t\tnew SES.DescribeReceiptRuleSetCommand({ RuleSetName: ruleSet }),\n\t);\n\tfor (const rule of set.Rules ?? []) {\n\t\tif (!rule.Name?.startsWith(prefix) || kept.has(rule.Name)) continue;\n\t\tawait ctx.change(\n\t\t\t{\n\t\t\t\tconstruct: 'inbox',\n\t\t\t\tresource: `SES receipt rule ${rule.Name}`,\n\t\t\t\tchange: 'delete — its inbox is no longer declared',\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tawait ses.send(\n\t\t\t\t\tnew SES.DeleteReceiptRuleCommand({\n\t\t\t\t\t\tRuleSetName: ruleSet,\n\t\t\t\t\t\tRuleName: rule.Name!,\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t},\n\t\t);\n\t}\n}\n\n/**\n * The cheap check every deploy runs: the region receives mail, the stage has\n * a domain, and each inbox's keys are in the stage's secrets.\n */\nasync function verifySes(ctx: VerifyContext<SesInboxConfig>): Promise<void> {\n\tconst region = s3Region(ctx as never);\n\tif (!SES_RECEIVING_REGIONS.includes(region)) {\n\t\tthrow new SesReceivingUnsupported(ctx.stage, region, SES_RECEIVING_REGIONS);\n\t}\n\tconst inboxes = inboxConstructs(ctx.manifest);\n\tif (inboxes.length === 0) return;\n\tstageDomain(ctx);\n\tfor (const inbox of inboxes) {\n\t\tconst missing = [\n\t\t\tprovideKey(inbox.id, 'url'),\n\t\t\tprovideKey(inbox.topic, 'publisherConnectionString'),\n\t\t].filter((key) => ctx.secrets[key] === undefined);\n\t\tif (missing.length > 0) {\n\t\t\tthrow new InboxNotProvisioned(ctx.stage, inbox.id, missing);\n\t\t}\n\t}\n}\n\nexport const sesProvider: ResourceProvider<SesInboxConfig, AwsProvisioning> = {\n\tkind: 'inbox',\n\tname: 'ses',\n\tcheck: checkSesConfig,\n\tprovisioning: SES_PROVISIONING,\n\tcredentials: awsProvisioningCredentials,\n\truntimeKeys(manifest) {\n\t\treturn inboxConstructs(manifest).flatMap((inbox) => [\n\t\t\tprovideKey(inbox.id, 'url'),\n\t\t\tprovideKey(inbox.topic, 'publisherConnectionString'),\n\t\t]);\n\t},\n\tensure: ensureSes,\n\tverify: verifySes,\n};\n","/**\n * SES's SMTP interface: where it is, and the password an IAM access key signs\n * in with there.\n *\n * SES issues IAM credentials, not SMTP ones. The SMTP password is derived\n * from the key's secret and the region, by SigV4's key derivation over a\n * fixed date and the message `SendRawEmail` — the algorithm the SES\n * Developer Guide gives (\"Obtaining Amazon SES SMTP credentials\"). A\n * password works in that region only.\n */\n\nimport { createHmac } from 'node:crypto';\nimport type { KeySecret } from '../iam.js';\n\n/** The SMTP endpoint of `region`. */\nexport function smtpHost(region: string): string {\n\treturn `email-smtp.${region}.amazonaws.com`;\n}\n\n/** The port the URL names: STARTTLS on 587. */\nexport const SMTP_PORT = 587;\n\n/** The SMTP password for an access key's secret, in `region`. */\nexport function smtpPassword(secretAccessKey: string, region: string): string {\n\tconst sign = (key: Buffer | string, message: string) =>\n\t\tcreateHmac('sha256', key).update(message, 'utf8').digest();\n\tlet signature = sign(`AWS4${secretAccessKey}`, '11111111');\n\tfor (const part of [region, 'ses', 'aws4_request', 'SendRawEmail']) {\n\t\tsignature = sign(signature, part);\n\t}\n\treturn Buffer.concat([Buffer.from([0x04]), signature]).toString('base64');\n}\n\n/** `smtp://<key id>:<smtp password>@email-smtp.<region>.amazonaws.com:587`. */\nexport function smtpUrl(\n\taccessKeyId: string,\n\tsecretAccessKey: string,\n\tregion: string,\n): string {\n\treturn (\n\t\t`smtp://${encodeURIComponent(accessKeyId)}:` +\n\t\t`${encodeURIComponent(smtpPassword(secretAccessKey, region))}` +\n\t\t`@${smtpHost(region)}:${SMTP_PORT}`\n\t);\n}\n\n/** The user an `smtp://` URL signs in as, whatever its host. */\nexport function smtpUser(value: string): string | undefined {\n\ttry {\n\t\tconst url = new URL(value);\n\t\treturn url.username ? decodeURIComponent(url.username) : undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/**\n * A mail construct's URL as a key's secret. A URL on another region's\n * endpoint holds no key this region can use — its password was derived for\n * that region — so it counts as holding none, and a new key is issued.\n */\nexport function smtpKeySecret(urlKey: string, region: string): KeySecret {\n\treturn {\n\t\tkey: urlKey,\n\t\tkeyIdOf(value) {\n\t\t\ttry {\n\t\t\t\treturn new URL(value).hostname === smtpHost(region)\n\t\t\t\t\t? smtpUser(value)\n\t\t\t\t\t: undefined;\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t},\n\t\tvalue: (accessKeyId, secretAccessKey) =>\n\t\t\tsmtpUrl(accessKeyId, secretAccessKey, region),\n\t};\n}\n","/**\n * The `ses` provider for mail: what sends an `Email` construct's mail on a\n * deployed stage.\n *\n * `deploy.email.<stage> = { provider: 'ses', region?, from? }` has the deploy\n * create, in the stage's AWS account:\n *\n * - **the sender's domain as an SES identity**, verified through Easy DKIM's\n * CNAMEs written through the stage's DNS — the same identity an inbox on\n * that domain uses: SES has one per domain, and neither provider deletes it;\n * - for each mail construct, **an IAM user** allowed `ses:SendRawEmail` from\n * that identity and nothing else, and **its access key**, written into the\n * stage's secrets as `<ID>_URL` — `smtp://<key id>:<smtp password>@\n * email-smtp.<region>.amazonaws.com:587`, the password derived from the key;\n * - **`<ID>_FROM`**: `from`, else what the secrets hold, else `noreply@` the\n * stage's domain.\n *\n * A `<ID>_URL` the stage's secrets already hold, and this provider did not\n * write, wins: the construct sends with it and nothing is created for it — an\n * identity that already has sending credentials is not given a second set.\n * Moving an existing stack onto compose stays a `secrets:import`.\n *\n * `--rotate-keys` issues each user a successor key, and with it a new\n * password, as every other provider's keys are rotated.\n */\n\nimport {\n\ttype ConstructManifest,\n\ttype EmailDeclaration,\n\tprovideKey,\n} from '@geekmidas/manifest';\nimport {\n\ttype AwsProvisioning,\n\tawsClientConfig,\n\tawsEndpoint,\n\tawsProvisioningCredentials,\n} from '../aws.js';\nimport { ensureAccessKey, ensureIamUser, type IamClients } from '../iam.js';\nimport { callerAccountId } from '../projectBucket.js';\nimport { s3Region } from '../s3/index.js';\nimport type { PolicyDocument } from '../s3/policy.js';\nimport type {\n\tEnsureContext,\n\tResourceProvider,\n\tVerifyContext,\n} from '../types.js';\nimport {\n\tcheckSesEmailConfig,\n\temailUserName,\n\tSES_PROVISIONING,\n\ttype SesEmailConfig,\n} from './config.js';\nimport { EmailNotProvisioned, EmailSenderUnknown } from './errors.js';\nimport { type SesClients, sesClients } from './index.js';\nimport { smtpKeySecret, smtpUser } from './smtp.js';\n\ntype Ctx = EnsureContext<SesEmailConfig, AwsProvisioning>;\n\n/** The inline policy each sending user is given. */\nexport const SEND_POLICY_NAME = 'gkm-email';\n\n/**\n * The most a sandboxed account sends in a day. An account with no more is\n * taken to be in the sandbox, where SES delivers only to verified addresses.\n */\nconst SANDBOX_DAILY_QUOTA = 200;\n\n/** Every mail construct in the manifest. */\nexport function emailConstructs(\n\tmanifest: ConstructManifest,\n): EmailDeclaration[] {\n\treturn Object.values(manifest)\n\t\t.filter((d): d is EmailDeclaration => d.kind === 'email')\n\t\t.sort((a, b) => a.id.localeCompare(b.id));\n}\n\n/** What a sending user may do: send as `identityArn`, and nothing else. */\nexport function sendPolicy(identityArn: string): PolicyDocument {\n\treturn {\n\t\tVersion: '2012-10-17',\n\t\tStatement: [\n\t\t\t{\n\t\t\t\tSid: 'GkmSendEmail',\n\t\t\t\tEffect: 'Allow',\n\t\t\t\tAction: 'ses:SendRawEmail',\n\t\t\t\tResource: identityArn,\n\t\t\t},\n\t\t],\n\t};\n}\n\n/** The ARN of a domain's SES identity. */\nexport function identityArn(\n\tregion: string,\n\taccount: string,\n\tdomain: string,\n): string {\n\treturn `arn:aws:ses:${region}:${account}:identity/${domain}`;\n}\n\n/**\n * Whether the stage's secrets hold a URL for `email` that this provider did\n * not write — one set by hand, or imported — which it then leaves alone.\n */\nexport function urlSetElsewhere(\n\tctx: Pick<Ctx, 'secrets' | 'state'>,\n\temail: string,\n): boolean {\n\tconst given = ctx.secrets[provideKey(email, 'url')];\n\tif (given === undefined) return false;\n\tconst record = ctx.state.record(`iam-access-key:${email}`);\n\tif (!record) return true;\n\tconst user = smtpUser(given);\n\treturn user !== record.id && user !== record.data?.previous;\n}\n\n/**\n * The address `email` sends from: `from`, else what the stage's secrets\n * hold, else `noreply@` the stage's domain.\n *\n * @throws {EmailSenderUnknown} with none of them\n */\nexport function senderOf(\n\tctx: Pick<Ctx, 'config' | 'secrets' | 'workspace' | 'stage'>,\n\temail: string,\n): string {\n\tif (ctx.config.from) return ctx.config.from;\n\tconst held = ctx.secrets[provideKey(email, 'from')];\n\tif (held) return held;\n\tconst domain = ctx.workspace.domains?.[ctx.stage];\n\tif (!domain) throw new EmailSenderUnknown(ctx.stage);\n\treturn `noreply@${domain}`;\n}\n\n/** The domain of an address — `noreply@x.com` or `Acme <noreply@x.com>`. */\nexport function domainOf(address: string): string {\n\treturn address\n\t\t.slice(address.lastIndexOf('@') + 1)\n\t\t.replace(/>\\s*$/, '')\n\t\t.trim()\n\t\t.toLowerCase();\n}\n\nasync function iamClients(\n\tcredential: AwsProvisioning,\n\tregion: string,\n): Promise<IamClients & { sts: import('@aws-sdk/client-sts').STSClient }> {\n\tconst IAM = await import('@aws-sdk/client-iam');\n\tconst { STSClient } = await import('@aws-sdk/client-sts');\n\tconst config = await awsClientConfig(credential, region);\n\tconst stsEndpoint = awsEndpoint(process.env, 'STS');\n\treturn {\n\t\tIAM,\n\t\tiam: new IAM.IAMClient(config),\n\t\tsts: new STSClient({\n\t\t\t...config,\n\t\t\t...(stsEndpoint ? { endpoint: stsEndpoint } : {}),\n\t\t}),\n\t\tregion,\n\t};\n}\n\nasync function ensureSesEmail(ctx: Ctx): Promise<void> {\n\tconst emails = emailConstructs(ctx.manifest);\n\tconst owned = emails.filter((email) => {\n\t\tif (!urlSetElsewhere(ctx, email.id)) return true;\n\t\tctx.log(\n\t\t\t` ${email.id}: ${provideKey(email.id, 'url')} is set in the stage's secrets — it sends with that, and nothing is created for it`,\n\t\t);\n\t\treturn false;\n\t});\n\tif (owned.length === 0) return;\n\n\tconst region = s3Region(ctx as never);\n\tconst senders = new Map(owned.map((e) => [e.id, senderOf(ctx, e.id)]));\n\tconst domains = [...new Set([...senders.values()].map(domainOf))].sort();\n\n\tconst ses = await sesClients(ctx.credential, region);\n\tconst clients = await iamClients(ctx.credential, region);\n\ttry {\n\t\tconst account = await callerAccountId(clients.sts);\n\t\tfor (const domain of domains) await ensureIdentity(ctx, ses, domain);\n\t\tawait noteSandbox(ctx, ses, region);\n\n\t\tfor (const email of owned) {\n\t\t\tconst from = senders.get(email.id)!;\n\t\t\tconst domain = domainOf(from);\n\t\t\tconst user = await ensureIamUser(ctx, clients, {\n\t\t\t\tid: email.id,\n\t\t\t\tuser: emailUserName(ctx.identity.scope, ctx.stage, email.id),\n\t\t\t\tpolicyName: SEND_POLICY_NAME,\n\t\t\t\tpolicy: sendPolicy(identityArn(region, account, domain)),\n\t\t\t\tdescribe: `send as ${domain} only`,\n\t\t\t});\n\t\t\tawait ensureAccessKey(ctx, clients, {\n\t\t\t\tid: email.id,\n\t\t\t\tsecret: smtpKeySecret(provideKey(email.id, 'url'), region),\n\t\t\t\t...user,\n\t\t\t});\n\t\t\tconst fromKey = provideKey(email.id, 'from');\n\t\t\tif (ctx.secrets[fromKey] !== from) {\n\t\t\t\tawait ctx.change(\n\t\t\t\t\t{ construct: email.id, resource: fromKey, change: `set to ${from}` },\n\t\t\t\t\t() => ctx.writeSecrets({ [fromKey]: from }),\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t} finally {\n\t\tses.ses.destroy();\n\t\tclients.iam.destroy();\n\t\tclients.sts.destroy();\n\t}\n}\n\n/**\n * The domain as an SES identity, verified by Easy DKIM: its three CNAMEs,\n * through the stage's DNS. A verified one is left as it is; one that is not\n * — new, or created by another stack and never finished — is asked for its\n * tokens again, which SES answers without making a second identity.\n */\nasync function ensureIdentity(\n\tctx: Ctx,\n\t{ SES, ses }: SesClients,\n\tdomain: string,\n): Promise<void> {\n\tconst status = await ses.send(\n\t\tnew SES.GetIdentityVerificationAttributesCommand({ Identities: [domain] }),\n\t);\n\tif (\n\t\tstatus.VerificationAttributes?.[domain]?.VerificationStatus === 'Success'\n\t) {\n\t\treturn;\n\t}\n\tlet tokens: string[] = [];\n\tawait ctx.change(\n\t\t{\n\t\t\tconstruct: 'email',\n\t\t\tresource: `SES identity ${domain}`,\n\t\t\tchange: 'verify with DKIM',\n\t\t},\n\t\tasync () => {\n\t\t\tconst dkim = await ses.send(\n\t\t\t\tnew SES.VerifyDomainDkimCommand({ Domain: domain }),\n\t\t\t);\n\t\t\ttokens = dkim.DkimTokens ?? [];\n\t\t},\n\t);\n\tif (tokens.length === 0) return;\n\tawait ctx.writeDns(\n\t\ttokens.map((token) => ({\n\t\t\thost: `${token}._domainkey.${domain}`,\n\t\t\ttype: 'CNAME' as const,\n\t\t\tvalue: `${token}.dkim.amazonses.com`,\n\t\t})),\n\t);\n\tctx.log(\n\t\t` ${domain}: SES sends as it once its DKIM records resolve — usually minutes, at most 72 hours`,\n\t);\n}\n\n/** Say so when the account is in SES's sandbox, rather than fail on a send. */\nasync function noteSandbox(\n\tctx: Ctx,\n\t{ SES, ses }: SesClients,\n\tregion: string,\n): Promise<void> {\n\tconst quota = await ses.send(new SES.GetSendQuotaCommand({}));\n\tif ((quota.Max24HourSend ?? 0) > SANDBOX_DAILY_QUOTA) return;\n\tctx.log(\n\t\t` ⚠️ SES in ${region} looks to be in the sandbox (${quota.Max24HourSend ?? 0} messages a day): it delivers only to addresses verified in SES. Request production access in the SES console before sending to anyone else.`,\n\t);\n}\n\n/** The cheap check every deploy runs: each mail construct's keys are in the stage's secrets. */\nasync function verifySesEmail(\n\tctx: VerifyContext<SesEmailConfig>,\n): Promise<void> {\n\tfor (const email of emailConstructs(ctx.manifest)) {\n\t\tconst missing = [\n\t\t\tprovideKey(email.id, 'url'),\n\t\t\tprovideKey(email.id, 'from'),\n\t\t].filter((key) => ctx.secrets[key] === undefined);\n\t\tif (missing.length > 0) {\n\t\t\tthrow new EmailNotProvisioned(ctx.stage, email.id, missing);\n\t\t}\n\t}\n}\n\nexport const sesEmailProvider: ResourceProvider<\n\tSesEmailConfig,\n\tAwsProvisioning\n> = {\n\tkind: 'email',\n\tname: 'ses',\n\tcheck: checkSesEmailConfig,\n\tprovisioning: SES_PROVISIONING,\n\tcredentials: awsProvisioningCredentials,\n\truntimeKeys(manifest) {\n\t\treturn emailConstructs(manifest).flatMap((email) => [\n\t\t\tprovideKey(email.id, 'url'),\n\t\t\tprovideKey(email.id, 'from'),\n\t\t]);\n\t},\n\tensure: ensureSesEmail,\n\tverify: verifySesEmail,\n};\n","/**\n * The providers gkm ships, by kind and name — registered the way a deploy\n * target's built-ins are (`target/builtins.ts`). A provider from a package is\n * one more entry here once there is a loading story for it.\n */\n\nimport { s3Provider } from './s3/index.js';\nimport { sesEmailProvider } from './ses/email.js';\nimport { sesProvider } from './ses/index.js';\nimport type { ProviderKind, ResourceProvider } from './types.js';\n\nexport const BUILTIN_PROVIDERS: {\n\treadonly [K in ProviderKind]: Readonly<\n\t\tRecord<string, ResourceProvider<any, any>>\n\t>;\n} = {\n\tobjects: { s3: s3Provider },\n\tinbox: { ses: sesProvider },\n\temail: { ses: sesEmailProvider },\n};\n\n/** The provider `kind` names `name`, if gkm has one. */\nexport function builtinProvider(\n\tkind: ProviderKind,\n\tname: string,\n): ResourceProvider<any, any> | undefined {\n\treturn Object.hasOwn(BUILTIN_PROVIDERS[kind], name)\n\t\t? BUILTIN_PROVIDERS[kind][name]\n\t\t: undefined;\n}\n\n/** The names `deploy.<kind>.<stage>` accepts, for messages. */\nexport function providerNames(kind: ProviderKind): string[] {\n\treturn Object.keys(BUILTIN_PROVIDERS[kind]).sort();\n}\n","/**\n * Running providers on a stage: `ensure()` at the start of every deploy —\n * before its checks, so a key the deploy creates is never reported missing —\n * `verify()` from every deploy's checks, and the hint a stage missing a key\n * is given.\n *\n * See `types.ts` for what a provider is.\n */\n\nimport type { ConstructManifest } from '@geekmidas/manifest';\nimport { stageBackups } from '../backups/config.js';\nimport {\n\tensureStageBackups,\n\tverifyStageBackups,\n} from '../backups/provision.js';\nimport { BACKUPS_URL_KEY } from '../backups/service.js';\nimport {\n\ttype CredentialProvider,\n\tstoredCredentials,\n} from '../deploy/credentials.js';\nimport { deployIdentity } from '../deploy/identity.js';\nimport { DeployJournal } from '../deploy/journal.js';\nimport type { ResourceRecord, StateStore } from '../deploy/StateStore.js';\nimport { createComposeState, createEmptyState } from '../deploy/state.js';\nimport { GkmError } from '../errors';\nimport type { BoundSubscriber } from '../reconcile/discover.js';\nimport { initStageSecrets } from '../secrets/storage.js';\nimport type { StageSecrets } from '../secrets/types.js';\nimport type { DnsProvider } from '../target/dokploy/dns/DnsProvider.js';\nimport type {\n\tDnsProvider as DnsProviderConfig,\n\tNormalizedWorkspace,\n} from '../workspace/types.js';\nimport { stageProvider } from './config.js';\nimport { deploysWithCompose } from './dns.js';\nimport { writeProviderDns } from './dnsWriter.js';\nimport { builtinProvider } from './registry.js';\nimport {\n\tPROVIDER_KINDS,\n\ttype ProviderKind,\n\ttype ProvisionAction,\n\ttype ProvisionState,\n\ttype ResourceProvider,\n} from './types.js';\n\nexport {\n\tcheckStageProvider,\n\tstageProvider,\n\tUnknownStageProvider,\n} from './config.js';\nexport {\n\tassertStageProvidersEnabled,\n\tprovisionCommand,\n\tprovisionHint,\n\tStageProviderDisabled,\n\tstageProviderNotes,\n} from './notes.js';\nexport { BUILTIN_PROVIDERS, builtinProvider } from './registry.js';\nexport type * from './types.js';\n\n/** The constructs of a kind: `objects` is every bucket. */\nfunction constructsOf(manifest: ConstructManifest, kind: ProviderKind) {\n\treturn Object.entries(manifest)\n\t\t.filter(([, d]) => d.kind === kind)\n\t\t.map(([id]) => id)\n\t\t.sort();\n}\n\nexport interface VerifyStageInput {\n\tworkspace: NormalizedWorkspace;\n\tmanifest: ConstructManifest;\n\tstage: string;\n\t/** The stage's secrets, by key. */\n\tsecrets: Readonly<Record<string, string>>;\n\t/** What the stage's state records, by key. */\n\tresources?: Readonly<Record<string, ResourceRecord>>;\n\t/** Progress worth printing. */\n\tlog?: (line: string) => void;\n}\n\n/**\n * What every deploy runs: each provider's `verify()` — the cheap check that\n * what it created is still there and the stage's key reaches it. Nothing on\n * the local stage, and nothing for a kind on `external` or `false` — a kind\n * the stage has none of is the deploy's readiness check's to refuse.\n */\nexport async function verifyStageProviders(\n\tinput: VerifyStageInput,\n): Promise<string[]> {\n\tconst verified: string[] = [];\n\tfor (const kind of PROVIDER_KINDS) {\n\t\tif (constructsOf(input.manifest, kind).length === 0) continue;\n\t\tconst choice = stageProvider(input.workspace, kind, input.stage);\n\t\tif (choice.mode !== 'provider') continue;\n\t\tconst provider = builtinProvider(kind, choice.name)!;\n\t\tawait provider.verify({\n\t\t\tworkspace: input.workspace,\n\t\t\tmanifest: input.manifest,\n\t\t\tstage: input.stage,\n\t\t\tconfig: choice.config,\n\t\t\tsecrets: input.secrets,\n\t\t\t...(input.resources ? { resources: input.resources } : {}),\n\t\t\t...(input.log ? { log: input.log } : {}),\n\t\t});\n\t\tverified.push(`${kind}: ${provider.name}`);\n\t}\n\tif (await verifyStageBackups(input)) verified.push('backups: s3');\n\treturn verified;\n}\n\n/** The stage's secrets, as the deploy reads and writes them. */\nexport interface ProvisionSecrets {\n\tread(): Promise<StageSecrets | null>;\n\twrite(secrets: StageSecrets): Promise<void>;\n}\n\nexport interface ProvisionOptions {\n\tworkspace: NormalizedWorkspace;\n\tstage: string;\n\tmanifest: ConstructManifest;\n\t/** Each Worker's subscribers, from discovery. */\n\tsubscribers?: Readonly<Record<string, readonly BoundSubscriber[]>>;\n\t/** The stage's secrets — where each runtime key is written. */\n\tsecrets: ProvisionSecrets;\n\t/** The stage's deploy state. The caller holds the stage's lock. */\n\tstate: StateStore;\n\t/** Print the plan; write nothing to the account, the state or the secrets. */\n\tdryRun?: boolean;\n\t/** Issue each runtime key a successor. */\n\trotateKeys?: boolean;\n\t/** Delete a rotated-out key now rather than after the next deploy. */\n\tretireOldKeys?: boolean;\n\t/** The AWS profile for the stage's account. */\n\tprofile?: string;\n\t/** The CLI's home. */\n\thome?: string;\n\tenv?: NodeJS.ProcessEnv;\n\t/**\n\t * Where the DNS providers' tokens come from, for the records a provider\n\t * writes. Defaults to the environment and the stored logins.\n\t */\n\tcredentials?: CredentialProvider;\n\t/** The DNS provider for a domain — `createDnsProvider` by default. */\n\tdnsProviderFor?: (config: DnsProviderConfig) => Promise<DnsProvider | null>;\n\tlog?: (line: string) => void;\n}\n\n/** What one kind came to on a provisioning run. */\nexport interface KindReport {\n\t/** A provider kind, or the stage's backups. */\n\tkind: ProviderKind | 'backups';\n\tmode: 'external' | 'none' | 'provider';\n\tprovider?: string;\n\tactions: ProvisionAction[];\n\t/**\n\t * The runtime keys the stage's secrets do not hold yet, which the run\n\t * writes — or, on a dry run, would.\n\t */\n\tplanned: string[];\n}\n\n/**\n * A stage whose provider creates its resources, and no credentials to create\n * them with. Raised before anything is created, built or started.\n */\nexport class ProviderCredentialsMissing extends GkmError {\n\tconstructor(\n\t\treadonly stage: string,\n\t\treadonly kind: ProviderKind,\n\t\treadonly provider: string,\n\t\t/** The constructs it would have created resources for. */\n\t\treadonly ids: readonly string[],\n\t\t/** The environment variables it reads. */\n\t\treadonly env: readonly string[],\n\t\t/** How to supply them, in one line. */\n\t\treadonly describe: string,\n\t\t/** A `gkm login --provider` name, where it has one. */\n\t\treadonly login?: string,\n\t) {\n\t\tsuper(\n\t\t\t`deploy.${kind}.${stage} is ${provider}, so the deploy creates what ` +\n\t\t\t\t`${ids.join(', ')} ${ids.length === 1 ? 'is' : 'are'} backed by on ` +\n\t\t\t\t`'${stage}' and writes ${ids.length === 1 ? 'its key' : 'their keys'} ` +\n\t\t\t\t`into the stage's secrets — and it found no ${provider} credentials ` +\n\t\t\t\t`to do it with. On this machine, supply ${describe}` +\n\t\t\t\t(login ? `, or run gkm login --provider ${login}` : '') +\n\t\t\t\t'. In CI, the step that runs the deploy needs them: the ' +\n\t\t\t\t`'${stage}' environment's role ` +\n\t\t\t\t'(AWS_ROLE_ARN, from gkm deploy:github), assumed before that step. ' +\n\t\t\t\t`With the keys set by hand instead, set deploy.${kind}.${stage} to 'external'.`,\n\t\t);\n\t\tthis.name = 'ProviderCredentialsMissing';\n\t}\n}\n\n/**\n * `ensure()` for every provider the stage configures — find or create, fix\n * drift, record in the stage's state, write the runtime keys into its\n * secrets. Run by the deploy, which holds the stage's lock. A provider with\n * no provisioning credentials stops the run with\n * {@link ProviderCredentialsMissing} before anything is created.\n */\nexport async function provisionStage(\n\toptions: ProvisionOptions,\n): Promise<KindReport[]> {\n\tconst { workspace, stage, manifest } = options;\n\tconst dryRun = options.dryRun === true;\n\tconst env = options.env ?? process.env;\n\tconst log = options.log ?? ((line: string) => console.log(line));\n\n\tconst reports: KindReport[] = [];\n\tconst configured: {\n\t\tkind: ProviderKind;\n\t\tprovider: ResourceProvider<any, any>;\n\t\tconfig: unknown;\n\t\tcredential: unknown;\n\t\treport: KindReport;\n\t}[] = [];\n\n\tfor (const kind of PROVIDER_KINDS) {\n\t\tconst choice = stageProvider(workspace, kind, stage);\n\t\tif (choice.mode !== 'provider') {\n\t\t\treports.push({ kind, mode: choice.mode, actions: [], planned: [] });\n\t\t\tcontinue;\n\t\t}\n\t\tconst provider = builtinProvider(kind, choice.name)!;\n\t\tconst report: KindReport = {\n\t\t\tkind,\n\t\t\tmode: 'provider',\n\t\t\tprovider: provider.name,\n\t\t\tactions: [],\n\t\t\tplanned: [],\n\t\t};\n\t\treports.push(report);\n\t\tconst ids = constructsOf(manifest, kind);\n\n\t\tconst credential = await provider.credentials({\n\t\t\tstage,\n\t\t\tenv,\n\t\t\t...(options.profile ? { profile: options.profile } : {}),\n\t\t\t...(options.home ? { home: options.home } : {}),\n\t\t});\n\t\t// With nothing of the kind declared, a provider still runs where it\n\t\t// can: what it made for a construct since removed is its to retire —\n\t\t// a receipt rule left behind would keep receiving. Without credentials\n\t\t// there is nothing to retire with, and nothing is asked for.\n\t\tif (ids.length === 0 && credential === undefined) continue;\n\t\tif (credential === undefined) {\n\t\t\tthrow new ProviderCredentialsMissing(\n\t\t\t\tstage,\n\t\t\t\tkind,\n\t\t\t\tprovider.name,\n\t\t\t\tids,\n\t\t\t\tprovider.provisioning.env,\n\t\t\t\tprovider.provisioning.describe,\n\t\t\t\tprovider.provisioning.login,\n\t\t\t);\n\t\t}\n\t\tconfigured.push({\n\t\t\tkind,\n\t\t\tprovider,\n\t\t\tconfig: choice.config,\n\t\t\tcredential,\n\t\t\treport,\n\t\t});\n\t}\n\n\t// The stage's backups: whether there are any is the stack's to say, and\n\t// a stage with none needs nothing of this run.\n\tconst backups = stageBackups(workspace, manifest, stage);\n\tif (configured.length === 0 && backups.mode !== 'on') {\n\t\tif (backups.mode === 'disabled') {\n\t\t\tlog(\n\t\t\t\t`💾 backups: off — deploy.backups.${stage} is false. Nothing is backed up, and nothing already backed up is deleted.`,\n\t\t\t);\n\t\t}\n\t\treturn reports;\n\t}\n\n\tconst identity = deployIdentity(workspace, stage);\n\tlet secrets = (await options.secrets.read())?.custom ?? {};\n\tfor (const entry of configured) {\n\t\tentry.report.planned = entry.provider\n\t\t\t.runtimeKeys(manifest)\n\t\t\t.filter((key) => secrets[key] === undefined);\n\t}\n\n\tconst state: ProvisionState = dryRun\n\t\t? await readOnlyState(options.state, stage)\n\t\t: journalState(\n\t\t\t\tawait DeployJournal.open(options.state, stage, () =>\n\t\t\t\t\tdeploysWithCompose(workspace)\n\t\t\t\t\t\t? createComposeState(stage)\n\t\t\t\t\t\t: createEmptyState(stage, '', ''),\n\t\t\t\t),\n\t\t\t);\n\n\tconst writeSecrets = async (values: Record<string, string>) => {\n\t\tif (dryRun) return;\n\t\tconst stored = (await options.secrets.read()) ?? initStageSecrets(stage);\n\t\tconst next = {\n\t\t\t...stored,\n\t\t\tcustom: { ...stored.custom, ...values },\n\t\t\tupdatedAt: new Date().toISOString(),\n\t\t};\n\t\tawait options.secrets.write(next);\n\t\tsecrets = next.custom;\n\t};\n\n\tfor (const entry of configured) {\n\t\tlog(\n\t\t\t`☁️ ${entry.kind}: ${entry.provider.name}${dryRun ? ' (dry run)' : ''}`,\n\t\t);\n\t\tawait entry.provider.ensure({\n\t\t\tworkspace,\n\t\t\tmanifest,\n\t\t\t...(options.subscribers ? { subscribers: options.subscribers } : {}),\n\t\t\tstage,\n\t\t\tidentity,\n\t\t\tconfig: entry.config,\n\t\t\tcredential: entry.credential,\n\t\t\tget secrets() {\n\t\t\t\treturn secrets;\n\t\t\t},\n\t\t\tstate,\n\t\t\tdryRun,\n\t\t\trotateKeys: options.rotateKeys === true,\n\t\t\tretireOldKeys: options.retireOldKeys === true,\n\t\t\tasync change(action, apply) {\n\t\t\t\tif (!dryRun) await apply();\n\t\t\t\tentry.report.actions.push(action);\n\t\t\t\tlog(\n\t\t\t\t\t` ${dryRun ? 'would ' : ''}${action.change} — ${action.resource}`,\n\t\t\t\t);\n\t\t\t},\n\t\t\twriteSecrets,\n\t\t\twriteDns: (records) =>\n\t\t\t\twriteProviderDns(records, {\n\t\t\t\t\tworkspace,\n\t\t\t\t\tstage,\n\t\t\t\t\tdryRun,\n\t\t\t\t\tcredentials:\n\t\t\t\t\t\toptions.credentials ??\n\t\t\t\t\t\tstoredCredentials({\n\t\t\t\t\t\t\tenv,\n\t\t\t\t\t\t\t...(options.home ? { home: options.home } : {}),\n\t\t\t\t\t\t}),\n\t\t\t\t\t...(options.dnsProviderFor\n\t\t\t\t\t\t? { providerFor: options.dnsProviderFor }\n\t\t\t\t\t\t: {}),\n\t\t\t\t\tlog,\n\t\t\t\t}),\n\t\t\tlog,\n\t\t});\n\t\tif (entry.report.actions.length === 0) {\n\t\t\tlog(' up to date');\n\t\t}\n\t}\n\n\tif (backups.mode === 'on' || backups.mode === 'disabled') {\n\t\tconst report: KindReport = {\n\t\t\tkind: 'backups',\n\t\t\tmode: backups.mode === 'on' ? 'provider' : 'none',\n\t\t\tactions: [],\n\t\t\tplanned:\n\t\t\t\tbackups.mode === 'on' && secrets[BACKUPS_URL_KEY] === undefined\n\t\t\t\t\t? [BACKUPS_URL_KEY]\n\t\t\t\t\t: [],\n\t\t};\n\t\treports.push(report);\n\t\tawait ensureStageBackups({\n\t\t\tworkspace,\n\t\t\tmanifest,\n\t\t\tstage,\n\t\t\t...(options.profile ? { profile: options.profile } : {}),\n\t\t\t...(options.home ? { home: options.home } : {}),\n\t\t\tenv,\n\t\t\tctx: {\n\t\t\t\tidentity,\n\t\t\t\tstage,\n\t\t\t\tstate,\n\t\t\t\tdryRun,\n\t\t\t\tget secrets() {\n\t\t\t\t\treturn secrets;\n\t\t\t\t},\n\t\t\t\trotateKeys: options.rotateKeys === true,\n\t\t\t\tretireOldKeys: options.retireOldKeys === true,\n\t\t\t\tasync change(action, apply) {\n\t\t\t\t\tif (!dryRun) await apply();\n\t\t\t\t\treport.actions.push(action);\n\t\t\t\t\tlog(\n\t\t\t\t\t\t` ${dryRun ? 'would ' : ''}${action.change} — ${action.resource}`,\n\t\t\t\t\t);\n\t\t\t\t},\n\t\t\t\twriteSecrets,\n\t\t\t\tlog,\n\t\t\t},\n\t\t});\n\t\tif (backups.mode === 'on' && report.actions.length === 0) {\n\t\t\tlog(' up to date');\n\t\t}\n\t}\n\n\treturn reports;\n}\n\n/** The journal as `ensure()` sees it. */\nfunction journalState(journal: DeployJournal): ProvisionState {\n\treturn {\n\t\trecord: (key) => journal.record(key),\n\t\tpending: (entry) => journal.pending(entry),\n\t\tready: (entry, id) => journal.ready(entry, id),\n\t\tlastDeployedAt: journal.existed ? journal.state.lastDeployedAt : undefined,\n\t};\n}\n\n/** What a dry run reads, and the writes it does not make. */\nasync function readOnlyState(\n\tstore: StateStore,\n\tstage: string,\n): Promise<ProvisionState> {\n\tconst stored = await store.read(stage);\n\treturn {\n\t\trecord: (key) => stored?.resources[key],\n\t\tpending: async () => {},\n\t\tready: async () => {},\n\t\t...(stored ? { lastDeployedAt: stored.state.lastDeployedAt } : {}),\n\t};\n}\n","/**\n * Stored secrets an older gkm generated that now shadow a derived value.\n *\n * Before constructs, initialising a stage wrote an address for each app into\n * its custom secrets: `<APP>_DATABASE_URL` as a `postgresql://…@localhost`\n * URL with a password of its own, and `http://localhost:<port>` for a site or\n * an auth server. Those keys are now a construct's — `database.schema('AuthDatabase')`\n * provides `AUTH_DATABASE_URL`, `new BetterAuth('Auth')` provides `AUTH_URL`\n * — and a deploy derives them. A value the stage holds is a value set by hand,\n * and set by hand wins: the container is handed `localhost`, where nothing\n * answers, with a password no role has.\n *\n * So a deploy refuses such a stage rather than run it, naming each key and\n * the command that removes it. Nothing is deleted for the user: the value is\n * theirs, and only they can say it is stale. A URL that names a real host — a\n * managed database — is a deliberate choice and still wins.\n */\n\nimport {\n\ttype ConstructManifest,\n\ttype DeclarationKind,\n\tprovidedKeyFor,\n} from '@geekmidas/manifest';\nimport { GkmError } from '../errors';\n\n/** The kinds whose URL an older gkm wrote into a stage's secrets. */\nconst ADDRESSED: ReadonlySet<DeclarationKind> = new Set<DeclarationKind>([\n\t'database',\n\t'database-reader',\n\t'database-schema',\n\t'rest-api',\n\t'site',\n]);\n\n/** Hosts that are this machine — never anything, inside a container. */\nconst LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]', '::1']);\n\n/** One stored key that shadows a construct's derived value. */\nexport interface StaleSecret {\n\tkey: string;\n\t/** The construct that provides the key. */\n\tconstruct: string;\n\t/** The host the stored value names — never the value itself. */\n\thost: string;\n}\n\n/**\n * A stage holds a construct's address as a custom secret, pointing at\n * `localhost` — what an older `gkm secrets:init`, `gkm setup` or `gkm init`\n * generated. Set by hand wins over derived, so the app would be handed it.\n */\nexport class StaleStageSecrets extends GkmError {\n\tconstructor(\n\t\treadonly stage: string,\n\t\treadonly stale: readonly StaleSecret[],\n\t) {\n\t\tconst one = stale.length === 1;\n\t\tsuper(\n\t\t\t`The stage '${stage}' holds ${one ? 'a value' : 'values'} an older gkm generated ` +\n\t\t\t\t`for ${one ? 'an address a construct' : 'addresses constructs'} now ${one ? 'provides' : 'provide'}:\\n` +\n\t\t\t\tstale\n\t\t\t\t\t.map(\n\t\t\t\t\t\t(s) => ` ${s.key}: ${s.construct}'s, stored pointing at ${s.host}`,\n\t\t\t\t\t)\n\t\t\t\t\t.join('\\n') +\n\t\t\t\t`\\nA stored value wins over the derived one, so the app would be handed ` +\n\t\t\t\t`an address where nothing answers inside its container. Remove ` +\n\t\t\t\t`${one ? 'it' : 'each'}, and the construct's own value is used:\\n` +\n\t\t\t\tstale\n\t\t\t\t\t.map((s) => ` gkm secrets:unset ${s.key} --stage ${stage}`)\n\t\t\t\t\t.join('\\n') +\n\t\t\t\t'\\nA managed service set with its real host is not affected.',\n\t\t);\n\t\tthis.name = 'StaleStageSecrets';\n\t}\n}\n\n/** The host a stored value names, if it is a URL. */\nfunction hostOf(value: string): string | undefined {\n\ttry {\n\t\treturn new URL(value).hostname;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/**\n * Each stored key that a construct provides an address for, holding an\n * address on this machine.\n */\nexport function staleStageSecrets(\n\tmanifest: ConstructManifest,\n\tsupplied: Readonly<Record<string, string>>,\n): StaleSecret[] {\n\tconst stale: StaleSecret[] = [];\n\tfor (const [id, declaration] of Object.entries(manifest)) {\n\t\tif (!ADDRESSED.has(declaration.kind)) continue;\n\t\tconst key = providedKeyFor(id, declaration.kind, 'url');\n\t\tconst value = supplied[key];\n\t\tif (value === undefined) continue;\n\t\tconst host = hostOf(value);\n\t\tif (host === undefined || !LOOPBACK.has(host)) continue;\n\t\tstale.push({ key, construct: id, host });\n\t}\n\treturn stale.sort((a, b) => a.key.localeCompare(b.key));\n}\n\n/**\n * Refuse a stage whose stored secrets would shadow a construct's address with\n * `localhost`.\n *\n * @throws {StaleStageSecrets} naming each key and the command removing it\n */\nexport function assertNoStaleSecrets(options: {\n\tmanifest: ConstructManifest;\n\tstage: string;\n\tsupplied: Readonly<Record<string, string>>;\n}): void {\n\tconst stale = staleStageSecrets(options.manifest, options.supplied);\n\tif (stale.length > 0) throw new StaleStageSecrets(options.stage, stale);\n}\n","/**\n * Whether a stage is ready to be deployed: the one check, run once by every\n * deploy through a server target — after the stage's providers have created\n * what they create and written its keys, and before the target touches\n * anything.\n *\n * A build is not a deploy. Images carry no stage's keys, so a build-only run\n * (`gkm compose --build --push`) never calls this: a key the deploy itself\n * writes — a provider's bucket URL — is not there yet when CI builds, and\n * must not stop the build.\n */\n\nimport {\n\tassertStageProvidersEnabled,\n\tstageProviderNotes,\n\tverifyStageProviders,\n} from '../providers/index.js';\nimport { constructGlobs } from '../reconcile/workspace.js';\nimport { assertStageCredentials } from '../secrets/credentialSchemas.js';\nimport { assertNoStaleSecrets } from '../secrets/stale.js';\nimport type { DeployPhaseContext } from '../target/types';\nimport {\n\tdevServicesUsed,\n\tExternalServicesNotConfigured,\n\texternalServices,\n\treportDevServices,\n\tstageServiceDeclarations,\n} from './devServices';\n\n/**\n * The stage has every key it needs, read from its secrets as the providers\n * left them:\n *\n * 1. no kind it declares is one the stage set to `false`;\n * 2. its mail and storage are its own — or a dev service, where allowed,\n * reported once, loudly;\n * 3. what its providers created is still there, and its key reaches it;\n * 4. no key holds an address an older gkm stored for what a construct now\n * provides;\n * 5. each third party's credentials match their construct's schema.\n *\n * The local stage derives its mail and storage, so only 4 and 5 apply to it.\n *\n * @throws {StageProviderDisabled}\n * @throws {ExternalServicesNotConfigured} naming every missing key at once\n * @throws {StaleStageSecrets}\n * @throws {CredentialsInvalid}\n */\nexport async function assertStageReady(\n\tctx: DeployPhaseContext<unknown>,\n): Promise<void> {\n\tconst { workspace, manifest, stage } = ctx;\n\t// Read now, not before the providers ran: they just wrote keys.\n\tconst supplied = (await ctx.secrets.read())?.custom ?? {};\n\n\tif (stage !== workspace.stages.local) {\n\t\tassertStageProvidersEnabled(workspace, manifest, stage);\n\n\t\tconst domain = workspace.domains?.[stage];\n\t\tconst services = externalServices({\n\t\t\tstage,\n\t\t\tdeclarations: stageServiceDeclarations({\n\t\t\t\tmanifest,\n\t\t\t}),\n\t\t\tsupplied,\n\t\t\tallow: ctx.allowDevServices,\n\t\t\t...(domain ? { domain } : {}),\n\t\t\tproviders: stageProviderNotes(workspace, stage),\n\t\t});\n\t\tif (services.missing.length > 0) {\n\t\t\tthrow new ExternalServicesNotConfigured(stage, services.missing);\n\t\t}\n\t\t// Loud, every deploy — a dry run included: a deployed stage on Mailpit\n\t\t// delivers no mail, and one on MinIO keeps its files on one disk.\n\t\treportDevServices(ctx, devServicesUsed(services));\n\n\t\tconst verified = await verifyStageProviders({\n\t\t\tworkspace,\n\t\t\tmanifest,\n\t\t\tstage,\n\t\t\tsecrets: supplied,\n\t\t\t// What the providers just recorded: a key issued by this deploy.\n\t\t\tresources: (await ctx.state.read(stage))?.resources ?? {},\n\t\t\tlog: (line) => ctx.logger.info(line),\n\t\t});\n\t\tfor (const line of verified) ctx.logger.info(`✓ ${line} verified`);\n\t}\n\n\t// Set by hand wins, so every app would be handed `localhost`.\n\tassertNoStaleSecrets({ manifest, stage, supplied });\n\t// Before anything is built with one every app reading it would refuse.\n\tawait assertStageCredentials({\n\t\troot: workspace.root,\n\t\tpatterns: constructGlobs(workspace),\n\t\tmanifest,\n\t\tstage,\n\t\tsupplied,\n\t});\n}\n","/**\n * The run around a target: what every deploy does whatever it deploys\n * through.\n *\n * It loads the workspace, resolves the target, works out the apps and the\n * identity, takes the stage's lock, and calls the target's phases in order,\n * reporting each as it starts, finishes or fails. Everything that is about\n * *where* the stage goes is the target's.\n */\n\nimport { dnsCredentials, stageDnsDomains } from '../compose/dns.js';\nimport { assertSitePasswords } from '../compose/sitePassword.js';\nimport { GkmError } from '../errors';\nimport { provisionStage } from '../providers/index.js';\nimport { type BoundSubscriber, discover } from '../reconcile/discover.js';\nimport { constructGlobs } from '../reconcile/workspace.js';\nimport { initStageSecrets } from '../secrets/storage.js';\nimport { builtinTarget } from '../target/builtins';\nimport {\n\tparseTargetOptions,\n\ttype ResolvedTarget,\n\tresolveTarget,\n} from '../target/resolve';\nimport { deploySecrets, Redactor } from '../target/secrets';\nimport type {\n\tAnyDeployTarget,\n\tDeployPhaseContext,\n\tDeployTargetLogger,\n} from '../target/types';\nimport { resolveStageTelemetry } from '../telemetry/config';\nimport { usesTelemetry } from '../telemetry/edges';\nimport { derivedApps } from '../workspace/derive.js';\nimport { getAppBuildOrder } from '../workspace/index.js';\nimport { assertDeployedStage } from '../workspace/stages.js';\nimport type { NormalizedWorkspace } from '../workspace/types.js';\nimport type { CredentialProvider } from './credentials';\nimport { DevServicesNeedServerTarget } from './devServices';\nimport { type DeployEvent, type DeployPhase, eventError } from './events';\nimport { applicationName, deployIdentity } from './identity.js';\nimport { assertStageReady } from './readiness';\nimport {\n\tassertStateOutlivesRun,\n\tassertStateRoom,\n\tcreateStateStore,\n} from './StateStore.js';\nimport type { DeployResult } from './types';\n\n/** What a run deploys, once the workspace is loaded. */\nexport interface DeployRequest {\n\t/** Deployment stage (e.g., 'production', 'staging') */\n\tstage: string;\n\t/** Image tag (default: stage-timestamp) */\n\ttag?: string;\n\t/** Specific apps to deploy (default: all) */\n\tapps?: string[];\n\t/** The target to deploy through. Defaults to `deploy.default`. */\n\ttarget?: string;\n\t/** On a failed release, roll back every app rather than the failed ones. */\n\tatomic?: boolean;\n\t/**\n\t * `--allow-dev-services`: a deployed stage runs the dev service for every\n\t * bucket and mail it does not account for. Server targets only.\n\t */\n\tallowDevServices?: boolean;\n\t/** `--skip-dns`: a server target writes and checks no host's DNS. */\n\tskipDns?: boolean;\n\t/** `--rotate-keys`: each provider issues its runtime keys a successor. */\n\trotateKeys?: boolean;\n\t/** `--retire-old-keys`: delete a rotated-out key now. */\n\tretireOldKeys?: boolean;\n}\n\n/**\n * What a run is handed by whoever started it: where its events go, where it\n * gets credentials, and whether it may change anything. Nothing in a run\n * reads a terminal or exits the process — that is its caller's business.\n */\nexport interface DeployContext {\n\temit: (event: DeployEvent) => void;\n\tcredentials: CredentialProvider;\n\tsignal?: AbortSignal;\n\t/** Look everything up, create, build and push nothing. */\n\tdryRun: boolean;\n\t/**\n\t * Validate and build — the target's images built, and pushed where it\n\t * pushes them — and nothing else: no lock, nothing provisioned, released,\n\t * verified or recorded. What a CI job runs before a server pulls.\n\t */\n\tbuildOnly?: boolean;\n\t/** The CLI's home, for the stage's keys. Defaults to `GKM_HOME`. */\n\thome?: string;\n\t/** Where spawned processes' output goes. Defaults to the terminal. */\n\tchildOutput?: 'inherit' | 'stderr' | 'ignore';\n\t/** The host's own targets, which win over every other by the same name. */\n\ttargets?: Record<string, AnyDeployTarget>;\n\t/**\n\t * What the run masks in what it prints. `deploy()` passes the one its\n\t * progress lines go through; a run without one masks only its events.\n\t */\n\tredactor?: Redactor;\n}\n\n/** Apps were asked for by name that the workspace does not have. */\nexport class UnknownDeployApps extends GkmError {\n\tconstructor(\n\t\treadonly apps: readonly string[],\n\t\treadonly available: readonly string[],\n\t) {\n\t\tsuper(\n\t\t\t`Unknown apps: ${apps.join(', ')}\\n` +\n\t\t\t\t`Available apps: ${available.join(', ')}`,\n\t\t);\n\t\tthis.name = 'UnknownDeployApps';\n\t}\n}\n\n/** Every app asked for deploys through some other target. */\nexport class NoDeployableApps extends GkmError {\n\tconstructor(\n\t\treadonly stage: string,\n\t\treadonly target: string,\n\t) {\n\t\tsuper(\n\t\t\t`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.`,\n\t\t);\n\t\tthis.name = 'NoDeployableApps';\n\t}\n}\n\n/** A build-only run through a target with no build phase. */\nexport class TargetBuildsNothing extends GkmError {\n\tconstructor(readonly target: string) {\n\t\tsuper(\n\t\t\t`\"${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).`,\n\t\t);\n\t\tthis.name = 'TargetBuildsNothing';\n\t}\n}\n\n/** A failed rollback, after the failure that called for it. */\nexport class RollbackFailed extends Error {\n\tconstructor(\n\t\treadonly target: string,\n\t\t/** What failed first, and was being rolled back. */\n\t\treadonly failure: unknown,\n\t\tcause: unknown,\n\t) {\n\t\tsuper(\n\t\t\t`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)}`,\n\t\t\t{ cause },\n\t\t);\n\t\tthis.name = 'RollbackFailed';\n\t}\n}\n\n/**\n * Generate image tag from stage and timestamp\n */\nexport function generateTag(stage: string): string {\n\tconst timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);\n\treturn `${stage}-${timestamp}`;\n}\n\n/** Why an app that deploys through another target is left out. */\nfunction elsewhere(target: string, stage: string): string {\n\tconst instead = builtinTarget(target)?.instead?.(stage);\n\tif (instead) return `it deploys through \"${target}\" — ${instead}`;\n\treturn `it deploys through \"${target}\" — run \\`gkm deploy --target ${target} --stage ${stage}\\``;\n}\n\n/**\n * Deploy a workspace's stage through its target.\n *\n * The engine under `deploy()` and `gkm deploy`. It never prompts and never\n * exits: credentials come from `ctx.credentials`, progress goes to\n * `ctx.emit` and to `output`, and every failure is thrown.\n *\n * @internal\n */\nexport async function runDeploy(\n\tsource: NormalizedWorkspace | (() => Promise<NormalizedWorkspace>),\n\trequest: DeployRequest,\n\tctx: DeployContext,\n): Promise<DeployResult> {\n\tconst { stage } = request;\n\tconst redactor = ctx.redactor ?? new Redactor();\n\tconst emit = (event: DeployEvent) =>\n\t\tctx.emit(\n\t\t\tevent.type === 'log'\n\t\t\t\t? { ...event, message: redactor.redact(event.message) }\n\t\t\t\t: event,\n\t\t);\n\n\t/** Run one phase between its events. */\n\tconst phase = async <T>(name: DeployPhase, body: () => Promise<T>) => {\n\t\tctx.signal?.throwIfAborted();\n\t\temit({ type: 'phase.started', phase: name });\n\t\ttry {\n\t\t\tconst value = await body();\n\t\t\temit({ type: 'phase.finished', phase: name });\n\t\t\treturn value;\n\t\t} catch (error) {\n\t\t\temit({ type: 'phase.failed', phase: name, error: eventError(error) });\n\t\t\tthrow error;\n\t\t}\n\t};\n\n\tctx.signal?.throwIfAborted();\n\temit({ type: 'phase.started', phase: 'validate' });\n\n\tlet prepared: Prepared;\n\ttry {\n\t\tprepared = await prepare(source, request, ctx, emit, redactor);\n\t} catch (error) {\n\t\temit({ type: 'phase.failed', phase: 'validate', error: eventError(error) });\n\t\tthrow error;\n\t}\n\tconst { resolved, phaseCtx, store } = prepared;\n\tconst { target } = resolved;\n\n\t// The stage's resources — what its providers create, and the keys they\n\t// write into its secrets — before any check reads those secrets, so a key\n\t// the deploy creates is never reported missing. A build-only run creates\n\t// nothing, and neither does the local stage.\n\tconst resources =\n\t\t!ctx.buildOnly && phaseCtx.stage !== phaseCtx.workspace.stages.local;\n\tconst provision = async () => {\n\t\t// The token the stage's DNS records are written with, asked for first:\n\t\t// a deploy without it fails before anything is created or started.\n\t\tif (!request.skipDns && resolved.target.runtime === 'server') {\n\t\t\ttry {\n\t\t\t\tawait dnsCredentials(\n\t\t\t\t\tphaseCtx.stage,\n\t\t\t\t\tstageDnsDomains(\n\t\t\t\t\t\tphaseCtx.stage,\n\t\t\t\t\t\tphaseCtx.workspace.domains,\n\t\t\t\t\t\tphaseCtx.workspace.dns,\n\t\t\t\t\t),\n\t\t\t\t\tctx.credentials,\n\t\t\t\t);\n\t\t\t} catch (error) {\n\t\t\t\temit({\n\t\t\t\t\ttype: 'phase.failed',\n\t\t\t\t\tphase: 'validate',\n\t\t\t\t\terror: eventError(error),\n\t\t\t\t});\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t}\n\t\tawait provisionResources(phaseCtx, request, ctx, store, emit);\n\t};\n\n\t// A dry run takes no lock: it writes nothing a lock would protect, and it\n\t// should not stop a real deploy that starts while it is looking.\n\tif (ctx.dryRun) {\n\t\tif (resources) await provision();\n\t\tconst run = await validate(phaseCtx, emit, () =>\n\t\t\treadyToDeploy(target, phaseCtx),\n\t\t);\n\t\tawait phase('plan', () => target.plan(phaseCtx, run));\n\t\treturn target.result(phaseCtx, run);\n\t}\n\n\t// Building changes no stage — nothing is provisioned, released or\n\t// recorded — so it takes no lock: a CI job building the next release must\n\t// not fail because a server is deploying the last one. Nor is the stage\n\t// asked whether it is ready to be deployed: an image carries none of its\n\t// keys, and a key the deploy writes is not there until the deploy runs.\n\tif (ctx.buildOnly) {\n\t\tif (!target.build) throw new TargetBuildsNothing(resolved.name);\n\t\tconst run = await validate(phaseCtx, emit, () => target.validate(phaseCtx));\n\t\tawait phase('build', () => target.build!(phaseCtx, run));\n\t\treturn target.result(phaseCtx, run);\n\t}\n\n\t// One deploy of a stage at a time. Taken before the target generates,\n\t// provisions or records anything, and held until the run ends however it\n\t// ends. A second run of the stage — another CI job, a laptop — gets\n\t// `StateLocked` naming this one instead of racing it; a run killed with\n\t// the lock held is released with `gkm state:unlock`.\n\tlet lock: Awaited<ReturnType<typeof store.lock>>;\n\ttry {\n\t\t// A CI runner's disk is gone when the job ends; state kept on it is\n\t\t// lost with it, and the next run recreates every resource.\n\t\tassertStateOutlivesRun(phaseCtx.workspace, stage, 'deploy');\n\t\tlock = await store.lock(stage, { operation: 'deploy' });\n\t} catch (error) {\n\t\temit({ type: 'phase.failed', phase: 'validate', error: eventError(error) });\n\t\tthrow error;\n\t}\n\n\ttry {\n\t\t// The state this run will write fits where it is kept — checked before\n\t\t// anything is created or started, not found out at the last write,\n\t\t// with the stack already running and nowhere to record it.\n\t\ttry {\n\t\t\tawait assertStateRoom(store, stage);\n\t\t} catch (error) {\n\t\t\temit({\n\t\t\t\ttype: 'phase.failed',\n\t\t\t\tphase: 'validate',\n\t\t\t\terror: eventError(error),\n\t\t\t});\n\t\t\tthrow error;\n\t\t}\n\t\tif (resources) await provision();\n\t\tconst run = await validate(phaseCtx, emit, () =>\n\t\t\treadyToDeploy(target, phaseCtx),\n\t\t);\n\t\tif (target.provision) {\n\t\t\tawait phase('provision', () => target.provision!(phaseCtx, run));\n\t\t}\n\t\tif (target.build) {\n\t\t\tawait phase('build', () => target.build!(phaseCtx, run));\n\t\t}\n\n\t\tlet releasing: 'release' | 'verify' = 'release';\n\t\ttry {\n\t\t\tawait phase('release', () => target.release(phaseCtx, run));\n\t\t\treleasing = 'verify';\n\t\t\tif (target.verify) {\n\t\t\t\tawait phase('verify', () => target.verify!(phaseCtx, run));\n\t\t\t}\n\t\t} catch (error) {\n\t\t\t// The caller stopping the run is not a release to undo.\n\t\t\tif (ctx.signal?.aborted || !target.capabilities.rollback) throw error;\n\t\t\tif (!target.rollback) throw error;\n\t\t\ttry {\n\t\t\t\tawait phase('rollback', () =>\n\t\t\t\t\ttarget.rollback!(phaseCtx, run, { phase: releasing, error }),\n\t\t\t\t);\n\t\t\t} catch (rollbackError) {\n\t\t\t\tthrow new RollbackFailed(resolved.name, error, rollbackError);\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\n\t\treturn await target.result(phaseCtx, run);\n\t} finally {\n\t\tawait lock.release();\n\t}\n}\n\n/**\n * The stage's providers, run inside the validate phase. A dry run plans them\n * and, so the checks after it see the stage as the run would leave it, reads\n * the keys they would write as placeholders.\n */\nasync function provisionResources(\n\tphaseCtx: DeployPhaseContext<unknown>,\n\trequest: DeployRequest,\n\tctx: DeployContext,\n\tstore: Awaited<ReturnType<typeof createStateStore>>,\n\temit: (event: DeployEvent) => void,\n): Promise<void> {\n\ttry {\n\t\tconst reports = await provisionStage({\n\t\t\tworkspace: phaseCtx.workspace,\n\t\t\tstage: phaseCtx.stage,\n\t\t\tmanifest: phaseCtx.manifest,\n\t\t\t...(phaseCtx.subscribers ? { subscribers: phaseCtx.subscribers } : {}),\n\t\t\tsecrets: phaseCtx.secrets,\n\t\t\tstate: store,\n\t\t\tdryRun: ctx.dryRun,\n\t\t\t...(request.rotateKeys ? { rotateKeys: true } : {}),\n\t\t\t...(request.retireOldKeys ? { retireOldKeys: true } : {}),\n\t\t\t...(ctx.home ? { home: ctx.home } : {}),\n\t\t\tcredentials: ctx.credentials,\n\t\t\tlog: (line) => phaseCtx.logger.info(line),\n\t\t});\n\t\tif (!ctx.dryRun) return;\n\t\tconst planned = reports.flatMap((r) => r.planned);\n\t\tif (planned.length === 0) return;\n\t\tconst secrets = phaseCtx.secrets;\n\t\t(phaseCtx as { secrets: typeof secrets }).secrets = {\n\t\t\t...secrets,\n\t\t\tasync read() {\n\t\t\t\tconst stored =\n\t\t\t\t\t(await secrets.read()) ?? initStageSecrets(phaseCtx.stage);\n\t\t\t\tconst custom = { ...stored.custom };\n\t\t\t\tfor (const key of planned) custom[key] ??= PLANNED_BY_THE_DEPLOY;\n\t\t\t\treturn { ...stored, custom };\n\t\t\t},\n\t\t};\n\t} catch (error) {\n\t\temit({ type: 'phase.failed', phase: 'validate', error: eventError(error) });\n\t\tthrow error;\n\t}\n}\n\n/** What a dry run reads for a key its providers would write. */\nexport const PLANNED_BY_THE_DEPLOY = 's3://planned-by-this-deploy';\n\n/**\n * What a deploy validates: the stage ready to be deployed — every key it\n * needs, read as its providers left them — then what the target would run,\n * then whatever only the target checks. A build-only run validates the\n * target's run alone: it calls neither readiness check.\n */\nasync function readyToDeploy(\n\ttarget: AnyDeployTarget,\n\tphaseCtx: DeployPhaseContext<unknown>,\n): Promise<unknown> {\n\t// The rules are a stack's, built from the stage's secrets: a target that\n\t// runs none — AWS — has its stage's keys from its own tool.\n\tif (target.runtime === 'server') await assertStageReady(phaseCtx);\n\tconst run = await target.validate(phaseCtx);\n\tawait target.ready?.(phaseCtx, run);\n\treturn run;\n}\n\n/** `body`, then the run's `deploy.started`, inside the validate phase. */\nasync function validate(\n\tphaseCtx: DeployPhaseContext<unknown>,\n\temit: (event: DeployEvent) => void,\n\tbody: () => Promise<unknown>,\n): Promise<unknown> {\n\ttry {\n\t\tconst run = await body();\n\t\tconst skipped = new Set(phaseCtx.skipped.map((s) => s.app));\n\t\temit({\n\t\t\ttype: 'deploy.started',\n\t\t\tstage: phaseCtx.stage,\n\t\t\ttarget: phaseCtx.target,\n\t\t\tidentity: phaseCtx.identity.key,\n\t\t\ttag: phaseCtx.tag,\n\t\t\tapps: phaseCtx.apps.filter((app) => !skipped.has(app)),\n\t\t\tdryRun: phaseCtx.dryRun,\n\t\t});\n\t\temit({ type: 'phase.finished', phase: 'validate' });\n\t\treturn run;\n\t} catch (error) {\n\t\temit({ type: 'phase.failed', phase: 'validate', error: eventError(error) });\n\t\tthrow error;\n\t}\n}\n\ninterface Prepared {\n\tresolved: ResolvedTarget;\n\tphaseCtx: DeployPhaseContext<unknown>;\n\tstore: Awaited<ReturnType<typeof createStateStore>>;\n}\n\n/** Everything the phases are handed, worked out before the first of them. */\nasync function prepare(\n\tsource: NormalizedWorkspace | (() => Promise<NormalizedWorkspace>),\n\trequest: DeployRequest,\n\tctx: DeployContext,\n\temit: (event: DeployEvent) => void,\n\tredactor: Redactor,\n): Promise<Prepared> {\n\tconst { stage } = request;\n\tconst configured = typeof source === 'function' ? await source() : source;\n\n\t// Before anything is provisioned: a typo'd stage would otherwise create a\n\t// whole second environment under the wrong name. The local stage is left\n\t// to the target, which says whether it can run it.\n\tconst local = stage === configured.stages.local;\n\tif (!local) assertDeployedStage(configured.stages, stage);\n\n\t// Before anything is discovered or derived: a target that cannot be found\n\t// — or a package that would not load — fails a deploy that has done\n\t// nothing yet.\n\tconst targetName = request.target ?? configured.deploy?.default ?? 'dokploy';\n\tconst resolved = await resolveTarget(targetName, {\n\t\tworkspace: configured,\n\t\tstage,\n\t\t...(ctx.targets ? { host: ctx.targets } : {}),\n\t});\n\tif (local && !resolved.target.capabilities.localStage) {\n\t\tassertDeployedStage(configured.stages, stage);\n\t}\n\tconst options = await parseTargetOptions(resolved);\n\n\t// Before anything is discovered: dev services asked of a target that runs\n\t// no containers fail a deploy that has done nothing.\n\tconst allowDevServices = request.allowDevServices === true;\n\tif (allowDevServices && resolved.target.runtime === 'aws') {\n\t\tthrow new DevServicesNeedServerTarget(targetName);\n\t}\n\t// What to deploy comes from the manifest.\n\t//\n\t// Discovered here rather than by the target, because the list of things\n\t// to deploy is the *declarations* — one `rest-api` is one server, one\n\t// `site` is one site — and the config only says how to run each. A project\n\t// that declares no surfaces keeps its configured apps, so adopting\n\t// constructs stays something you do a piece at a time.\n\tconst background: Record<string, string[]> = {};\n\tconst subscribers: Record<string, BoundSubscriber[]> = {};\n\tconst manifest = await discover({\n\t\tpatterns: constructGlobs(configured),\n\t\tcwd: configured.root,\n\t\tbackground,\n\t\tsubscribers,\n\t});\n\t// Where the stage's telemetry goes, settled before anything is built: a\n\t// stage that uses a `Telemetry` node on a target that cannot choose for it\n\t// — AWS has no self-hosted provider — fails here, naming the config.\n\tresolveStageTelemetry({\n\t\t...(configured.deploy?.telemetry\n\t\t\t? { telemetry: configured.deploy.telemetry }\n\t\t\t: {}),\n\t\tstage,\n\t\tlocal,\n\t\ttarget: targetName,\n\t\truntime: resolved.target.runtime,\n\t\tselfHosted: resolved.target.capabilities.selfHostedTelemetry === true,\n\t\tused: usesTelemetry(manifest),\n\t});\n\n\tconst units = derivedApps(manifest, configured);\n\tconst workspace: NormalizedWorkspace =\n\t\tObject.keys(units).length > 0 ? { ...configured, apps: units } : configured;\n\n\t// What every resource this deploy touches is named and claimed by —\n\t// resolved first, so a namespace that cannot be a name fails before\n\t// anything is built.\n\tconst identity = deployIdentity(workspace, stage);\n\tconst tag =\n\t\trequest.tag ??\n\t\t(resolved.target.tag\n\t\t\t? await resolved.target.tag({ cwd: workspace.root, stage })\n\t\t\t: generateTag(stage));\n\n\t// The apps asked for, in dependency order.\n\tconst buildOrder = getAppBuildOrder(workspace);\n\tlet selected = buildOrder;\n\tif (request.apps && request.apps.length > 0) {\n\t\tconst unknown = request.apps.filter((name) => !workspace.apps[name]);\n\t\tif (unknown.length > 0) {\n\t\t\tthrow new UnknownDeployApps(unknown, Object.keys(workspace.apps));\n\t\t}\n\t\tselected = buildOrder.filter((name) => request.apps!.includes(name));\n\t}\n\n\tconst skipped: { app: string; reason: string }[] = [];\n\tconst skip = (app: string, reason: string) => {\n\t\tskipped.push({ app, reason });\n\t\temit({ type: 'app.skipped', app, reason });\n\t};\n\n\t// An app that names another target deploys there, in a run of its own.\n\t// One on the default follows the run, so `--target` moves the stage\n\t// without every app having to be told.\n\tconst byDefault = configured.deploy?.default ?? 'dokploy';\n\tconst apps = selected.filter((name) => {\n\t\tconst own = workspace.apps[name]!.resolvedDeployTarget;\n\t\tconst deploysThrough = own === byDefault ? targetName : own;\n\t\tif (deploysThrough === targetName) return true;\n\t\tskip(name, elsewhere(deploysThrough, stage));\n\t\treturn false;\n\t});\n\tif (apps.length === 0) throw new NoDeployableApps(stage, targetName);\n\n\t// A site meant to be private on this stage, deployed through a target that\n\t// would serve it publicly, fails before anything is deployed.\n\tassertSitePasswords(configured, manifest, stage, apps, resolved.target.name);\n\n\t// The local stage's state is this machine's, always: it runs nothing\n\t// remote, so it never reads or writes the deployed stages' store — nor\n\t// needs that store's credentials.\n\tconst store = await createStateStore({\n\t\tconfig: stage === workspace.stages.local ? undefined : workspace.state,\n\t\tworkspaceRoot: workspace.root,\n\t\tworkspaceName: workspace.name,\n\t\tnamespace: workspace.deploy?.namespace,\n\t});\n\tconst secrets = await deploySecrets(workspace, stage, redactor, {\n\t\t...(ctx.home ? { home: ctx.home } : {}),\n\t});\n\n\tconst line =\n\t\t(level: 'info' | 'warn' | 'error') =>\n\t\t(message: string): void =>\n\t\t\temit({ type: 'log', level, message });\n\tconst logger: DeployTargetLogger = {\n\t\tinfo: line('info'),\n\t\twarn: line('warn'),\n\t\terror: line('error'),\n\t};\n\n\tconst phaseCtx: DeployPhaseContext<unknown> = {\n\t\ttarget: targetName,\n\t\tcwd: workspace.root,\n\t\tstage,\n\t\ttag,\n\t\ttagGiven: request.tag !== undefined,\n\t\tapps,\n\t\tskipped,\n\t\tidentity,\n\t\tworkspace,\n\t\tmanifest,\n\t\tbackground,\n\t\tsubscribers,\n\t\toptions,\n\t\tdryRun: ctx.dryRun,\n\t\tatomic: request.atomic ?? false,\n\t\tallowDevServices,\n\t\t...(request.skipDns ? { skipDns: true } : {}),\n\t\tcredentials: ctx.credentials,\n\t\tstate: store,\n\t\tsecrets,\n\t\tlogger,\n\t\tsignal: ctx.signal ?? new AbortController().signal,\n\t\tchildOutput: ctx.childOutput ?? 'inherit',\n\t\temit,\n\t\tskip,\n\t\tname: (resource) => applicationName(identity, resource),\n\t};\n\n\treturn { resolved, phaseCtx, store };\n}\n","/**\n * `deploy()`: a deploy as a library call.\n *\n * `gkm deploy` could only be driven from an interactive terminal: it prompted\n * for what it lacked and exited the process on failure, worked out of\n * `process.cwd()`, and reported progress by printing. Here the caller says\n * where the project is, hands over the credentials and a signal, and gets back\n * a run — events to iterate while it goes, and a structured result at the end.\n * Nothing below this asks a person anything or ends the process; `gkm deploy`\n * is a thin wrapper that does both.\n */\n\nimport { resolve } from 'node:path';\nimport {\n\tfindWorkspaceRoot,\n\tloadWorkspaceConfig,\n\twithLoadedConfig,\n} from '../config';\nimport { gkmHome } from '../home';\nimport { type OutputLevel, withOutput } from '../output';\nimport { LocalSandbox } from '../sandbox/local';\nimport { type Sandbox, withSandbox } from '../sandbox/sandbox';\nimport { Redactor } from '../target/secrets';\nimport type { AnyDeployTarget } from '../target/types';\nimport type { GkmConfig } from '../types.js';\nimport type { WorkspaceConfig } from '../workspace/index.js';\nimport type { NormalizedWorkspace } from '../workspace/types.js';\nimport { type CredentialProvider, storedCredentials } from './credentials';\nimport { type DeployEvent, eventError } from './events';\nimport { type DeployContext, runDeploy } from './orchestrate';\nimport type { DeployResult } from './types';\n\n/**\n * Where a run's progress lines go, besides its events. `@geekmidas/logger`'s\n * `Logger` is one.\n */\nexport interface DeployLogger {\n\tinfo(message: string): void;\n\twarn(message: string): void;\n\terror?(message: string): void;\n}\n\nexport interface DeployInput {\n\t/**\n\t * The project to deploy: the directory holding `gkm.config.ts`, or any\n\t * directory inside it. Never assumed to be `process.cwd()`.\n\t */\n\tcwd: string;\n\t/**\n\t * The workspace's config, already loaded by the caller — `gkm deploy` and\n\t * `gkm compose` load it in their own process, with the environment they\n\t * were run in. Every step of the run then reads these settings rather than\n\t * loading the file again in the sandbox, whose environment is an\n\t * allowlist: a config reading `process.env.DOMAIN` gave the deploy no\n\t * domain. Absent, the run loads it in its sandbox, as for a checkout the\n\t * host does not trust; a host that passes one vouches for it.\n\t */\n\tconfig?: GkmConfig | WorkspaceConfig;\n\t/**\n\t * The stage to deploy — one of the config's deployed stages, or its local\n\t * stage through a target that runs one (`compose`).\n\t */\n\tstage: string;\n\t/**\n\t * The target to deploy through, by name. Defaults to `deploy.default`,\n\t * else `dokploy`.\n\t */\n\ttarget?: string;\n\t/**\n\t * The host's own targets, by name. Consulted before the built-ins and the\n\t * config's `deploy.targets`, so a host can deploy through an\n\t * implementation of its own — `{ dokploy: myDokploy }` included.\n\t */\n\ttargets?: Record<string, AnyDeployTarget>;\n\t/** The image tag. Defaults to `<stage>-<timestamp>`. */\n\ttag?: string;\n\t/** Deploy only these apps (in dependency order). Defaults to all. */\n\tapps?: string[];\n\t/**\n\t * Look everything up and report what would happen — as `resource.planned`\n\t * events — without taking the lock, writing state, generating secrets,\n\t * changing Dokploy or building and pushing images.\n\t */\n\tdryRun?: boolean;\n\t/**\n\t * Validate and build only: the target's images built — and pushed, where\n\t * it pushes them — with no lock taken and nothing provisioned, released,\n\t * verified or recorded. `gkm compose --build --push` runs this way.\n\t */\n\tbuildOnly?: boolean;\n\t/**\n\t * When a release or its verification fails, roll back every app the run\n\t * released rather than only the ones that failed — on a target that can\n\t * roll back. For apps that must move together.\n\t */\n\tatomic?: boolean;\n\t/**\n\t * Where credentials come from. Defaults to the environment, then the\n\t * logins `gkm login` stored. A credential no provider has stops the run\n\t * with `MissingCredential`; nothing prompts.\n\t */\n\tcredentials?: CredentialProvider;\n\t/**\n\t * On a server target, run a dev service for every construct a deployed\n\t * stage does not account for — no key in its secrets, and no provider\n\t * (`deploy.objects`) backing its kind: MinIO for a bucket, Mailpit for\n\t * mail. Neither is production-grade — Mailpit delivers no mail — and every\n\t * run that uses one emits `dev-service.used`. Without it, a deployed stage\n\t * missing a mail or storage key fails with `ExternalServicesNotConfigured`;\n\t * on an AWS target it fails with `DevServicesNeedServerTarget`.\n\t */\n\tallowDevServices?: boolean;\n\t/**\n\t * On a server target, neither write the public hosts' DNS records nor\n\t * check that they point at the stage's server (`GKM_SERVER_IPV4`) — for a\n\t * CDN or proxy in front of the server, or records written by another step.\n\t */\n\tskipDns?: boolean;\n\t/** Each provider issues its runtime keys a successor. */\n\trotateKeys?: boolean;\n\t/** Delete a rotated-out key now, not after the next deploy. */\n\tretireOldKeys?: boolean;\n\t/** Receives each progress line as it is written. Defaults to none. */\n\tlogger?: DeployLogger;\n\t/**\n\t * Stops the run: in-flight Dokploy requests and docker children are\n\t * cancelled, the stage's lock is released, and `result` rejects with the\n\t * signal's reason.\n\t */\n\tsignal?: AbortSignal;\n\t/**\n\t * The CLI's home: stage keys and stored logins. Defaults to `GKM_HOME`,\n\t * else `~/.gkm`.\n\t */\n\thome?: string;\n\t/**\n\t * Where docker's own output goes: the host's terminal (`inherit`, the\n\t * default), its stderr, or nowhere.\n\t */\n\tchildOutput?: 'inherit' | 'stderr' | 'ignore';\n\t/**\n\t * Where the project's own code runs: loading `gkm.config.ts`, discovering\n\t * its constructs, sniffing each app's environment. Defaults to a\n\t * `LocalSandbox` on the project — a child process here with an\n\t * allowlisted environment, for a project the host trusts.\n\t *\n\t * A host deploying repositories it does not trust passes one that\n\t * isolates — a container per build — and the config then reaches the\n\t * deploy only as data. Credentials never enter a sandbox: they go from\n\t * `credentials` to the steps that provision, push and release.\n\t */\n\tsandbox?: Sandbox;\n}\n\n/** A deploy in progress. */\nexport interface DeployRun extends AsyncIterable<DeployEvent> {\n\t/**\n\t * What the deploy did, once it is done. Rejects with the error that\n\t * stopped it — `MissingCredential`, `StateLocked`, `ProjectNotOwned`, … —\n\t * after a `deploy.failed` event carrying the same.\n\t */\n\treadonly result: Promise<DeployResult>;\n}\n\n/**\n * Every event a run emits, kept so that each iteration — however late it\n * starts — sees the run from its first event to its last.\n */\nclass EventLog {\n\tprivate readonly events: DeployEvent[] = [];\n\tprivate closed = false;\n\tprivate waiting: (() => void)[] = [];\n\n\tpush(event: DeployEvent): void {\n\t\tthis.events.push(event);\n\t\tthis.wake();\n\t}\n\n\tclose(): void {\n\t\tthis.closed = true;\n\t\tthis.wake();\n\t}\n\n\tprivate wake(): void {\n\t\tconst waiting = this.waiting;\n\t\tthis.waiting = [];\n\t\tfor (const resume of waiting) resume();\n\t}\n\n\tasync *iterate(): AsyncGenerator<DeployEvent> {\n\t\tlet next = 0;\n\t\twhile (true) {\n\t\t\tif (next < this.events.length) {\n\t\t\t\tyield this.events[next++]!;\n\t\t\t} else if (this.closed) {\n\t\t\t\treturn;\n\t\t\t} else {\n\t\t\t\tawait new Promise<void>((resume) => this.waiting.push(resume));\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunction forward(\n\tlogger: DeployLogger | undefined,\n\tlevel: OutputLevel,\n\tmessage: string,\n): void {\n\tif (!logger) return;\n\tif (level === 'info') logger.info(message);\n\telse if (level === 'error' && logger.error) logger.error(message);\n\telse logger.warn(message);\n}\n\n/**\n * Deploy a project's stage.\n *\n * ```ts\n * import { deploy } from '@geekmidas/cli/deploy';\n *\n * const run = deploy({ cwd: '/srv/checkouts/shop', stage: 'production' });\n * for await (const event of run) {\n * if (event.type === 'app.deployed') console.log(event.app, event.url);\n * }\n * const result = await run.result;\n * ```\n *\n * The run starts at once; iterating it is optional, and an iteration started\n * late still sees every event from the first.\n */\nexport function deploy(input: DeployInput): DeployRun {\n\tconst log = new EventLog();\n\t// Every secret the run reads is masked in every line it reports — in the\n\t// events and in what the logger is handed.\n\tconst redactor = new Redactor();\n\tconst emit = (event: DeployEvent) => {\n\t\tconst masked =\n\t\t\tevent.type === 'log'\n\t\t\t\t? { ...event, message: redactor.redact(event.message) }\n\t\t\t\t: event;\n\t\tlog.push(masked);\n\t\tif (masked.type === 'log') {\n\t\t\tforward(input.logger, masked.level, masked.message);\n\t\t}\n\t};\n\n\tconst ctx: DeployContext = {\n\t\temit,\n\t\tcredentials:\n\t\t\tinput.credentials ?? storedCredentials({ home: input.home ?? gkmHome() }),\n\t\tdryRun: input.dryRun ?? false,\n\t\t...(input.buildOnly ? { buildOnly: true } : {}),\n\t\thome: input.home ?? gkmHome(),\n\t\tredactor,\n\t\t...(input.signal ? { signal: input.signal } : {}),\n\t\t...(input.childOutput ? { childOutput: input.childOutput } : {}),\n\t\t...(input.targets ? { targets: input.targets } : {}),\n\t};\n\n\t// Every line anything in the run writes through `output` — the deploy's\n\t// own, the Dockerfile generator's, discovery's warnings — becomes an event\n\t// of this run, and of no other.\n\tconst result = withOutput(\n\t\t(level, message) => emit({ type: 'log', level, message }),\n\t\tasync () => {\n\t\t\ttry {\n\t\t\t\tinput.signal?.throwIfAborted();\n\t\t\t\tconst root = findWorkspaceRoot(resolve(input.cwd));\n\t\t\t\tconst sandbox = input.sandbox ?? new LocalSandbox({ root });\n\t\t\t\t// Every step of the run that executes the project's code — the\n\t\t\t\t// engine's own discoveries and sniffs included — runs in it. The\n\t\t\t\t// config, when the caller loaded it, is the one every step reads.\n\t\t\t\tconst inRun = <T>(fn: () => T): T =>\n\t\t\t\t\tinput.config ? withLoadedConfig(root, input.config, fn) : fn();\n\t\t\t\tconst deployed = await inRun(() =>\n\t\t\t\t\twithSandbox(sandbox, () =>\n\t\t\t\t\t\trunDeploy(\n\t\t\t\t\t\t\t() => load(input, sandbox),\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tstage: input.stage,\n\t\t\t\t\t\t\t\t...(input.target ? { target: input.target } : {}),\n\t\t\t\t\t\t\t\t...(input.tag ? { tag: input.tag } : {}),\n\t\t\t\t\t\t\t\t...(input.apps ? { apps: input.apps } : {}),\n\t\t\t\t\t\t\t\t...(input.atomic ? { atomic: true } : {}),\n\t\t\t\t\t\t\t\t...(input.allowDevServices\n\t\t\t\t\t\t\t\t\t? { allowDevServices: input.allowDevServices }\n\t\t\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t\t\t\t...(input.skipDns ? { skipDns: true } : {}),\n\t\t\t\t\t\t\t\t...(input.rotateKeys ? { rotateKeys: true } : {}),\n\t\t\t\t\t\t\t\t...(input.retireOldKeys ? { retireOldKeys: true } : {}),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tctx,\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\temit({ type: 'deploy.finished', result: deployed });\n\t\t\t\treturn deployed;\n\t\t\t} catch (error) {\n\t\t\t\temit({ type: 'deploy.failed', error: eventError(error) });\n\t\t\t\tthrow error;\n\t\t\t} finally {\n\t\t\t\tlog.close();\n\t\t\t}\n\t\t},\n\t);\n\n\t// A caller that only iterates still learns of the failure from the\n\t// `deploy.failed` event; the rejection must not also crash its process as\n\t// unhandled. Awaiting `result` still rejects.\n\tresult.catch(() => {});\n\n\treturn {\n\t\tresult,\n\t\t[Symbol.asyncIterator]: () => log.iterate(),\n\t};\n}\n\n/**\n * The workspace at `input.cwd`. The stage is checked by the run, once it\n * knows the target: one that runs on this machine may take the local stage.\n */\nasync function load(\n\tinput: DeployInput,\n\tsandbox: Sandbox,\n): Promise<NormalizedWorkspace> {\n\tconst { workspace } = await loadWorkspaceConfig(resolve(input.cwd), {\n\t\tsandbox,\n\t});\n\treturn workspace;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,MAAM,kBAAkB;;AAGxB,IAAa,WAAb,MAAsB;CAC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