@geekmidas/cli 10.0.0-alpha.141 → 10.0.0-alpha.143

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 (260) hide show
  1. package/dist/{EndpointGenerator-BMRL04ic.mjs → EndpointGenerator-CusLH-9S.mjs} +2 -2
  2. package/dist/{EndpointGenerator-BMRL04ic.mjs.map → EndpointGenerator-CusLH-9S.mjs.map} +1 -1
  3. package/dist/{EndpointGenerator-ePscnbwB.cjs → EndpointGenerator-DyLMYClE.cjs} +2 -2
  4. package/dist/{EndpointGenerator-ePscnbwB.cjs.map → EndpointGenerator-DyLMYClE.cjs.map} +1 -1
  5. package/dist/{GoDaddyProvider-DxFAueU4.mjs → GoDaddyProvider-BJddaEFo.mjs} +2 -3
  6. package/dist/{GoDaddyProvider-DxFAueU4.mjs.map → GoDaddyProvider-BJddaEFo.mjs.map} +1 -1
  7. package/dist/{GoDaddyProvider-hAc0q5s-.cjs → GoDaddyProvider-D-QrclQ0.cjs} +5 -6
  8. package/dist/{GoDaddyProvider-hAc0q5s-.cjs.map → GoDaddyProvider-D-QrclQ0.cjs.map} +1 -1
  9. package/dist/{HostingerProvider-DEqpvoiD.cjs → HostingerProvider-CUtGkwa8.cjs} +2 -2
  10. package/dist/{HostingerProvider-DEqpvoiD.cjs.map → HostingerProvider-CUtGkwa8.cjs.map} +1 -1
  11. package/dist/{HostingerProvider-CgMuKgO_.mjs → HostingerProvider-DilTLFtw.mjs} +2 -2
  12. package/dist/{HostingerProvider-CgMuKgO_.mjs.map → HostingerProvider-DilTLFtw.mjs.map} +1 -1
  13. package/dist/{LegacyStateStore-BPY8Acgs.mjs → LegacyStateStore-lZn6yeNb.mjs} +3 -3
  14. package/dist/{LegacyStateStore-BPY8Acgs.mjs.map → LegacyStateStore-lZn6yeNb.mjs.map} +1 -1
  15. package/dist/{LegacyStateStore-BqSVgWi1.cjs → LegacyStateStore-ohe95jlu.cjs} +3 -3
  16. package/dist/{LegacyStateStore-BqSVgWi1.cjs.map → LegacyStateStore-ohe95jlu.cjs.map} +1 -1
  17. package/dist/{LocalStateStore-Df4RyGi2.cjs → LocalStateStore-Bs2Yl8IB.cjs} +2 -2
  18. package/dist/{LocalStateStore-Df4RyGi2.cjs.map → LocalStateStore-Bs2Yl8IB.cjs.map} +1 -1
  19. package/dist/{LocalStateStore-5pVR26KA.mjs → LocalStateStore-CkpQ3sNu.mjs} +2 -2
  20. package/dist/{LocalStateStore-5pVR26KA.mjs.map → LocalStateStore-CkpQ3sNu.mjs.map} +1 -1
  21. package/dist/{OpenApiTsGenerator-B-PQLx8X.mjs → OpenApiTsGenerator-BTMuu1WS.mjs} +2 -2
  22. package/dist/{OpenApiTsGenerator-B-PQLx8X.mjs.map → OpenApiTsGenerator-BTMuu1WS.mjs.map} +1 -1
  23. package/dist/{OpenApiTsGenerator-BvacdbiG.cjs → OpenApiTsGenerator-CI-tceXV.cjs} +2 -2
  24. package/dist/{OpenApiTsGenerator-BvacdbiG.cjs.map → OpenApiTsGenerator-CI-tceXV.cjs.map} +1 -1
  25. package/dist/{S3StateStore-Do1DwEMX.mjs → S3StateStore-BO-TXIC9.mjs} +4 -4
  26. package/dist/{S3StateStore-Do1DwEMX.mjs.map → S3StateStore-BO-TXIC9.mjs.map} +1 -1
  27. package/dist/{S3StateStore-uykstvZI.cjs → S3StateStore-CsxqGwKX.cjs} +5 -5
  28. package/dist/{S3StateStore-uykstvZI.cjs.map → S3StateStore-CsxqGwKX.cjs.map} +1 -1
  29. package/dist/{SSMStateStore-DQXFWqkE.cjs → SSMStateStore-D85DzsxJ.cjs} +2 -2
  30. package/dist/{SSMStateStore-DQXFWqkE.cjs.map → SSMStateStore-D85DzsxJ.cjs.map} +1 -1
  31. package/dist/{SSMStateStore-xQ-Ce91W.mjs → SSMStateStore-DDNpIVIc.mjs} +2 -2
  32. package/dist/{SSMStateStore-xQ-Ce91W.mjs.map → SSMStateStore-DDNpIVIc.mjs.map} +1 -1
  33. package/dist/{SubscriberGenerator-DR4Y7ihK.cjs → SubscriberGenerator-BNJyPmXJ.cjs} +3 -3
  34. package/dist/{SubscriberGenerator-DR4Y7ihK.cjs.map → SubscriberGenerator-BNJyPmXJ.cjs.map} +1 -1
  35. package/dist/{SubscriberGenerator-DLPH-ynS.mjs → SubscriberGenerator-CzPrixYu.mjs} +3 -3
  36. package/dist/{SubscriberGenerator-DLPH-ynS.mjs.map → SubscriberGenerator-CzPrixYu.mjs.map} +1 -1
  37. package/dist/{add-DLkN488n.mjs → add-CRAhswLe.mjs} +8 -7
  38. package/dist/{add-DLkN488n.mjs.map → add-CRAhswLe.mjs.map} +1 -1
  39. package/dist/{add-Y_nPkjIf.cjs → add-DiFv_IWD.cjs} +18 -17
  40. package/dist/{add-Y_nPkjIf.cjs.map → add-DiFv_IWD.cjs.map} +1 -1
  41. package/dist/apps-CA-048eZ.cjs +5008 -0
  42. package/dist/apps-CA-048eZ.cjs.map +1 -0
  43. package/dist/apps-CZ0ydmPQ.mjs +4593 -0
  44. package/dist/apps-CZ0ydmPQ.mjs.map +1 -0
  45. package/dist/{bundler-DFyrGST-.mjs → bundler-BIsNVJnB.mjs} +6 -6
  46. package/dist/{bundler-DFyrGST-.mjs.map → bundler-BIsNVJnB.mjs.map} +1 -1
  47. package/dist/{bundler-COOvv27I.cjs → bundler-D6Y2bucc.cjs} +9 -9
  48. package/dist/{bundler-COOvv27I.cjs.map → bundler-D6Y2bucc.cjs.map} +1 -1
  49. package/dist/{cli-DKsV-Myw.cjs → cli-6xibALWU.cjs} +4 -4
  50. package/dist/{cli-DKsV-Myw.cjs.map → cli-6xibALWU.cjs.map} +1 -1
  51. package/dist/{cli-iAX4BQ3t.mjs → cli-QwhW-X12.mjs} +4 -4
  52. package/dist/{cli-iAX4BQ3t.mjs.map → cli-QwhW-X12.mjs.map} +1 -1
  53. package/dist/compose-BHsW5nDE.mjs +1554 -0
  54. package/dist/compose-BHsW5nDE.mjs.map +1 -0
  55. package/dist/compose-C4kCN2tl.cjs +1895 -0
  56. package/dist/compose-C4kCN2tl.cjs.map +1 -0
  57. package/dist/{compose-ofZIpp2q.mjs → compose-E4-ZiWev.mjs} +17 -1828
  58. package/dist/compose-E4-ZiWev.mjs.map +1 -0
  59. package/dist/{compose-CGXx22Wy.cjs → compose-S_MY__b2.cjs} +102 -1967
  60. package/dist/compose-S_MY__b2.cjs.map +1 -0
  61. package/dist/config-BKpzKP6W.mjs +659 -0
  62. package/dist/config-BKpzKP6W.mjs.map +1 -0
  63. package/dist/config-QAqAEgb0.cjs +772 -0
  64. package/dist/config-QAqAEgb0.cjs.map +1 -0
  65. package/dist/config.cjs +16 -419
  66. package/dist/config.mjs +2 -405
  67. package/dist/{credentialSchemas-DZ2Nh5li.cjs → credentialSchemas-BAklWDna.cjs} +4 -5
  68. package/dist/{credentialSchemas-DZ2Nh5li.cjs.map → credentialSchemas-BAklWDna.cjs.map} +1 -1
  69. package/dist/{credentialSchemas-DUCSYl9a.mjs → credentialSchemas-mz9QZTYh.mjs} +2 -3
  70. package/dist/{credentialSchemas-DUCSYl9a.mjs.map → credentialSchemas-mz9QZTYh.mjs.map} +1 -1
  71. package/dist/credentials-BnpIwWCS.cjs +2336 -0
  72. package/dist/credentials-BnpIwWCS.cjs.map +1 -0
  73. package/dist/credentials-zlJPxBWn.mjs +1851 -0
  74. package/dist/credentials-zlJPxBWn.mjs.map +1 -0
  75. package/dist/{databases-P824RTnx.cjs → databases-DawSdqBI.cjs} +2 -2
  76. package/dist/{databases-P824RTnx.cjs.map → databases-DawSdqBI.cjs.map} +1 -1
  77. package/dist/{databases-Db48ZIXL.mjs → databases-mzSUFBTw.mjs} +2 -2
  78. package/dist/{databases-Db48ZIXL.mjs.map → databases-mzSUFBTw.mjs.map} +1 -1
  79. package/dist/deploy/public.cjs +24 -24
  80. package/dist/deploy/public.cjs.map +1 -1
  81. package/dist/deploy/public.d.cts.map +1 -1
  82. package/dist/deploy/public.d.mts.map +1 -1
  83. package/dist/deploy/public.mjs +13 -13
  84. package/dist/{deploy-BibGpDAz.cjs → deploy-0XI_bemB.cjs} +73 -72
  85. package/dist/{deploy-BibGpDAz.cjs.map → deploy-0XI_bemB.cjs.map} +1 -1
  86. package/dist/{deploy-C4QQkHK7.mjs → deploy-DJcEAscO.mjs} +16 -15
  87. package/dist/{deploy-C4QQkHK7.mjs.map → deploy-DJcEAscO.mjs.map} +1 -1
  88. package/dist/{discover-yqeJ2Bx7.cjs → discover-CRywE7yc.cjs} +157 -6
  89. package/dist/discover-CRywE7yc.cjs.map +1 -0
  90. package/dist/{discover-B2m8ytyq.mjs → discover-DbDrCQ8h.mjs} +117 -8
  91. package/dist/discover-DbDrCQ8h.mjs.map +1 -0
  92. package/dist/docker-8gbw63RW.cjs +1514 -0
  93. package/dist/docker-8gbw63RW.cjs.map +1 -0
  94. package/dist/docker-C5w9_bWP.mjs +1407 -0
  95. package/dist/docker-C5w9_bWP.mjs.map +1 -0
  96. package/dist/{dokploy-pSrsileA.mjs → dokploy-BAjJ7MPB.mjs} +2 -2
  97. package/dist/{dokploy-pSrsileA.mjs.map → dokploy-BAjJ7MPB.mjs.map} +1 -1
  98. package/dist/{dokploy-B_VX3nLM.cjs → dokploy-DOYsnbiK.cjs} +2 -2
  99. package/dist/{dokploy-B_VX3nLM.cjs.map → dokploy-DOYsnbiK.cjs.map} +1 -1
  100. package/dist/{drivers-C47nm-wB.cjs → drivers-aM87NQJi.cjs} +4 -91
  101. package/dist/drivers-aM87NQJi.cjs.map +1 -0
  102. package/dist/{drivers-CDjpjakg.mjs → drivers-uxLBq3e3.mjs} +3 -60
  103. package/dist/drivers-uxLBq3e3.mjs.map +1 -0
  104. package/dist/{eas-cSSc8Lex.cjs → eas-Aaj9nbNp.cjs} +6 -6
  105. package/dist/{eas-cSSc8Lex.cjs.map → eas-Aaj9nbNp.cjs.map} +1 -1
  106. package/dist/{eas-Bou-_M76.mjs → eas-BpyYdquY.mjs} +3 -3
  107. package/dist/{eas-Bou-_M76.mjs.map → eas-BpyYdquY.mjs.map} +1 -1
  108. package/dist/{encryption-Bps9As2D.mjs → encryption-DYFTQaZ3.mjs} +3 -3
  109. package/dist/{encryption-Bps9As2D.mjs.map → encryption-DYFTQaZ3.mjs.map} +1 -1
  110. package/dist/{encryption-CElaAwJs.cjs → encryption-K2-9Ei8q.cjs} +6 -6
  111. package/dist/{encryption-CElaAwJs.cjs.map → encryption-K2-9Ei8q.cjs.map} +1 -1
  112. package/dist/{engine-CiG7Wfyc.cjs → engine-097i9gWA.cjs} +99 -97
  113. package/dist/{engine-CiG7Wfyc.cjs.map → engine-097i9gWA.cjs.map} +1 -1
  114. package/dist/{engine-CXE4UOHj.mjs → engine-CuFkswUS.mjs} +12 -10
  115. package/dist/{engine-CXE4UOHj.mjs.map → engine-CuFkswUS.mjs.map} +1 -1
  116. package/dist/{github-CvFJlFWA.mjs → github-DGaK_ulS.mjs} +9 -8
  117. package/dist/{github-CvFJlFWA.mjs.map → github-DGaK_ulS.mjs.map} +1 -1
  118. package/dist/{github-BzppIvpw.cjs → github-ZoKihAlP.cjs} +18 -17
  119. package/dist/{github-BzppIvpw.cjs.map → github-ZoKihAlP.cjs.map} +1 -1
  120. package/dist/{googlePlay-BJnMCWoa.cjs → googlePlay-BQ-62vre.cjs} +5 -5
  121. package/dist/{googlePlay-BJnMCWoa.cjs.map → googlePlay-BQ-62vre.cjs.map} +1 -1
  122. package/dist/{googlePlay-RSe9wOfZ.mjs → googlePlay-hw7Ej5yT.mjs} +5 -5
  123. package/dist/{googlePlay-RSe9wOfZ.mjs.map → googlePlay-hw7Ej5yT.mjs.map} +1 -1
  124. package/dist/index.cjs +196 -192
  125. package/dist/index.cjs.map +1 -1
  126. package/dist/index.mjs +70 -66
  127. package/dist/index.mjs.map +1 -1
  128. package/dist/{local-B7Zv1NoA.mjs → local-CiJfYdrA.mjs} +3 -3
  129. package/dist/{local-B7Zv1NoA.mjs.map → local-CiJfYdrA.mjs.map} +1 -1
  130. package/dist/{local-OPC9BCZt.cjs → local-iC7qJkPl.cjs} +6 -6
  131. package/dist/{local-OPC9BCZt.cjs.map → local-iC7qJkPl.cjs.map} +1 -1
  132. package/dist/{migrate-B0nQra2W.mjs → migrate-CbjB-uxl.mjs} +3 -3
  133. package/dist/{migrate-B0nQra2W.mjs.map → migrate-CbjB-uxl.mjs.map} +1 -1
  134. package/dist/{migrate-BPCJBL7_.mjs → migrate-DTTn8YcN.mjs} +7 -5
  135. package/dist/{migrate-BPCJBL7_.mjs.map → migrate-DTTn8YcN.mjs.map} +1 -1
  136. package/dist/{migrate-BCBs0BMn.cjs → migrate-p1R0CSRw.cjs} +6 -6
  137. package/dist/{migrate-BCBs0BMn.cjs.map → migrate-p1R0CSRw.cjs.map} +1 -1
  138. package/dist/{migrate-Dmh5Dbcr.cjs → migrate-wH1DmtjX.cjs} +15 -13
  139. package/dist/{migrate-Dmh5Dbcr.cjs.map → migrate-wH1DmtjX.cjs.map} +1 -1
  140. package/dist/notes-CedXKWnt.mjs +399 -0
  141. package/dist/notes-CedXKWnt.mjs.map +1 -0
  142. package/dist/notes-ChbE7v2l.cjs +500 -0
  143. package/dist/notes-ChbE7v2l.cjs.map +1 -0
  144. package/dist/openapi.cjs +4 -5
  145. package/dist/openapi.cjs.map +1 -1
  146. package/dist/openapi.mjs +3 -4
  147. package/dist/openapi.mjs.map +1 -1
  148. package/dist/{projectBucket-C_DN4WzF.mjs → projectBucket-2Bw-Qy8D.mjs} +2 -2
  149. package/dist/{projectBucket-C_DN4WzF.mjs.map → projectBucket-2Bw-Qy8D.mjs.map} +1 -1
  150. package/dist/{projectBucket-BBbQ1lHF.cjs → projectBucket-CUJYUmt2.cjs} +2 -2
  151. package/dist/{projectBucket-BBbQ1lHF.cjs.map → projectBucket-CUJYUmt2.cjs.map} +1 -1
  152. package/dist/{publish-DKy3hn8a.cjs → publish-Byi0cB2N.cjs} +2 -2
  153. package/dist/{publish-DKy3hn8a.cjs.map → publish-Byi0cB2N.cjs.map} +1 -1
  154. package/dist/{publish-cthyGGQt.mjs → publish-DDsmxwl-.mjs} +2 -2
  155. package/dist/{publish-cthyGGQt.mjs.map → publish-DDsmxwl-.mjs.map} +1 -1
  156. package/dist/{ready-DuU0FXSj.cjs → ready-BNh3zsSn.cjs} +6 -6
  157. package/dist/{ready-DuU0FXSj.cjs.map → ready-BNh3zsSn.cjs.map} +1 -1
  158. package/dist/{ready-CGqJ74Kt.mjs → ready-g25dcGzK.mjs} +6 -6
  159. package/dist/{ready-CGqJ74Kt.mjs.map → ready-g25dcGzK.mjs.map} +1 -1
  160. package/dist/reconcile/public.cjs +1 -1
  161. package/dist/reconcile/public.mjs +1 -1
  162. package/dist/{resolve-AYtMIgoq.cjs → resolve-CT6P86p3.cjs} +4 -4
  163. package/dist/{resolve-AYtMIgoq.cjs.map → resolve-CT6P86p3.cjs.map} +1 -1
  164. package/dist/{resolve-BaRqJlqV.mjs → resolve-zHOTcPk4.mjs} +4 -4
  165. package/dist/{resolve-BaRqJlqV.mjs.map → resolve-zHOTcPk4.mjs.map} +1 -1
  166. package/dist/{rollback-DqV6t9oo.mjs → rollback-CFowte36.mjs} +8 -8
  167. package/dist/{rollback-DqV6t9oo.mjs.map → rollback-CFowte36.mjs.map} +1 -1
  168. package/dist/{rollback-CQ184yFu.cjs → rollback-CYnroE_3.cjs} +11 -11
  169. package/dist/{rollback-CQ184yFu.cjs.map → rollback-CYnroE_3.cjs.map} +1 -1
  170. package/dist/{s3-BKnL6YGQ.cjs → s3-82kaV_R0.cjs} +2 -2
  171. package/dist/{s3-BKnL6YGQ.cjs.map → s3-82kaV_R0.cjs.map} +1 -1
  172. package/dist/{s3-DW7SLsb8.mjs → s3-D8u9j2C5.mjs} +2 -2
  173. package/dist/{s3-DW7SLsb8.mjs.map → s3-D8u9j2C5.mjs.map} +1 -1
  174. package/dist/sandbox/credentials-worker.cjs +3 -4
  175. package/dist/sandbox/credentials-worker.cjs.map +1 -1
  176. package/dist/sandbox/credentials-worker.mjs +2 -3
  177. package/dist/sandbox/credentials-worker.mjs.map +1 -1
  178. package/dist/sandbox/discover-worker.cjs +1 -1
  179. package/dist/sandbox/discover-worker.mjs +1 -1
  180. package/dist/sandbox/migrate-worker.cjs +3 -4
  181. package/dist/sandbox/migrate-worker.cjs.map +1 -1
  182. package/dist/sandbox/migrate-worker.mjs +2 -3
  183. package/dist/sandbox/migrate-worker.mjs.map +1 -1
  184. package/dist/seeds-B3xvUAQf.cjs +363 -0
  185. package/dist/seeds-B3xvUAQf.cjs.map +1 -0
  186. package/dist/seeds-BCC8vAXR.mjs +298 -0
  187. package/dist/seeds-BCC8vAXR.mjs.map +1 -0
  188. package/dist/{serverSetup-AfG0-at8.mjs → serverSetup-B621zgYj.mjs} +7 -6
  189. package/dist/{serverSetup-AfG0-at8.mjs.map → serverSetup-B621zgYj.mjs.map} +1 -1
  190. package/dist/{serverSetup-Dnw9Kdel.cjs → serverSetup-Cip3ZWdi.cjs} +9 -8
  191. package/dist/{serverSetup-Dnw9Kdel.cjs.map → serverSetup-Cip3ZWdi.cjs.map} +1 -1
  192. package/dist/{sniffer-BHhJ8DiC.mjs → sniffer-BO7tNCDc.mjs} +8 -9
  193. package/dist/{sniffer-BHhJ8DiC.mjs.map → sniffer-BO7tNCDc.mjs.map} +1 -1
  194. package/dist/{sniffer-taO-AXSk.cjs → sniffer-Dxwe2yS0.cjs} +9 -10
  195. package/dist/{sniffer-taO-AXSk.cjs.map → sniffer-Dxwe2yS0.cjs.map} +1 -1
  196. package/dist/{snsSubscriptions-D_IBKhni.mjs → snsSubscriptions-CZlHrll5.mjs} +4 -453
  197. package/dist/snsSubscriptions-CZlHrll5.mjs.map +1 -0
  198. package/dist/{snsSubscriptions-CC-WM1zC.cjs → snsSubscriptions-CgWY_alK.cjs} +12 -559
  199. package/dist/snsSubscriptions-CgWY_alK.cjs.map +1 -0
  200. package/dist/{sst-Bz2HJlqc.mjs → sst-CnSAHj6j.mjs} +27 -5
  201. package/dist/sst-CnSAHj6j.mjs.map +1 -0
  202. package/dist/{sst-lvCHdYsY.cjs → sst-CtOI68mi.cjs} +29 -7
  203. package/dist/sst-CtOI68mi.cjs.map +1 -0
  204. package/dist/stack-BCHFuiWH.cjs +2577 -0
  205. package/dist/stack-BCHFuiWH.cjs.map +1 -0
  206. package/dist/stack-BrcNDH_E.mjs +2348 -0
  207. package/dist/stack-BrcNDH_E.mjs.map +1 -0
  208. package/dist/{stage-dd9dtLfh.mjs → stage-BhjwFQrw.mjs} +7 -7
  209. package/dist/{stage-dd9dtLfh.mjs.map → stage-BhjwFQrw.mjs.map} +1 -1
  210. package/dist/{stage-CTibxcpA.cjs → stage-DzH_iWgO.cjs} +13 -13
  211. package/dist/{stage-CTibxcpA.cjs.map → stage-DzH_iWgO.cjs.map} +1 -1
  212. package/dist/{stages--icWbhPy.cjs → stages-CFI0OiLj.cjs} +2 -2
  213. package/dist/{stages--icWbhPy.cjs.map → stages-CFI0OiLj.cjs.map} +1 -1
  214. package/dist/{stages-CUwSy39f.mjs → stages-DoYa81kN.mjs} +2 -2
  215. package/dist/{stages-CUwSy39f.mjs.map → stages-DoYa81kN.mjs.map} +1 -1
  216. package/dist/target/public.cjs +1 -1
  217. package/dist/target/public.mjs +1 -1
  218. package/dist/{terminal-DURcYlVX.cjs → terminal-455ZtUjl.cjs} +3 -3
  219. package/dist/{terminal-DURcYlVX.cjs.map → terminal-455ZtUjl.cjs.map} +1 -1
  220. package/dist/{terminal-yOZ7kKF9.mjs → terminal-iEkwo0Bq.mjs} +3 -3
  221. package/dist/{terminal-yOZ7kKF9.mjs.map → terminal-iEkwo0Bq.mjs.map} +1 -1
  222. package/dist/types-Bl_CwZC9.cjs +90 -0
  223. package/dist/types-Bl_CwZC9.cjs.map +1 -0
  224. package/dist/types-Y1NfWlyG.mjs +61 -0
  225. package/dist/types-Y1NfWlyG.mjs.map +1 -0
  226. package/dist/vitest.cjs +1 -1
  227. package/dist/vitest.mjs +1 -1
  228. package/package.json +15 -15
  229. package/dist/compose-CGXx22Wy.cjs.map +0 -1
  230. package/dist/compose-ofZIpp2q.mjs.map +0 -1
  231. package/dist/config.cjs.map +0 -1
  232. package/dist/config.mjs.map +0 -1
  233. package/dist/credentials-D7XJypNd.cjs +0 -520
  234. package/dist/credentials-D7XJypNd.cjs.map +0 -1
  235. package/dist/credentials-DpfnUvIR.mjs +0 -395
  236. package/dist/credentials-DpfnUvIR.mjs.map +0 -1
  237. package/dist/derive-Cd7Hl3r2.mjs +0 -259
  238. package/dist/derive-Cd7Hl3r2.mjs.map +0 -1
  239. package/dist/derive-CgZfoBU2.cjs +0 -282
  240. package/dist/derive-CgZfoBU2.cjs.map +0 -1
  241. package/dist/discover-B2m8ytyq.mjs.map +0 -1
  242. package/dist/discover-yqeJ2Bx7.cjs.map +0 -1
  243. package/dist/drivers-C47nm-wB.cjs.map +0 -1
  244. package/dist/drivers-CDjpjakg.mjs.map +0 -1
  245. package/dist/identity-CBXr4wNi.mjs +0 -129
  246. package/dist/identity-CBXr4wNi.mjs.map +0 -1
  247. package/dist/identity-CbLfgQNT.cjs +0 -182
  248. package/dist/identity-CbLfgQNT.cjs.map +0 -1
  249. package/dist/sandbox-CHv3OYXg.cjs +0 -165
  250. package/dist/sandbox-CHv3OYXg.cjs.map +0 -1
  251. package/dist/sandbox-CbVB7uOD.mjs +0 -118
  252. package/dist/sandbox-CbVB7uOD.mjs.map +0 -1
  253. package/dist/seeds-DndMeyzm.cjs +0 -10669
  254. package/dist/seeds-DndMeyzm.cjs.map +0 -1
  255. package/dist/seeds-o6x6BTRo.mjs +0 -9534
  256. package/dist/seeds-o6x6BTRo.mjs.map +0 -1
  257. package/dist/snsSubscriptions-CC-WM1zC.cjs.map +0 -1
  258. package/dist/snsSubscriptions-D_IBKhni.mjs.map +0 -1
  259. package/dist/sst-Bz2HJlqc.mjs.map +0 -1
  260. package/dist/sst-lvCHdYsY.cjs.map +0 -1
@@ -0,0 +1,4593 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-BhDjJH2R.mjs";
2
+ import { t as GkmError } from "./errors-D8Gwp5kU.mjs";
3
+ import { A as eventsBackendFor, U as externalsManifestName, j as providerOf, k as cacheBackendFor } from "./credentials-zlJPxBWn.mjs";
4
+ import { R as stageSampleRate, _ as PUBLIC_ENV_PREFIXES, a as allConstructGlobs, j as TEST_STAGE, z as telemetryEnv } from "./workspace-BbfBFtjn.mjs";
5
+ import { A as objectsWebhookToken, B as portKeys, D as NOTIFICATION_RULE_PREFIX, E as HOST_GATEWAY$1, F as emulatorQueueUrl, I as emulatorTopicArn, K as LOGS_ORG, N as EMULATOR_REGION, O as minioNotificationRule, P as emulatorEndpoint, U as primaryPortKey, V as portsOf, W as serviceAddresses, X as otlpHeaders, f as savePortState, j as watchedBuckets, l as loadPortState, m as loadLocalCredentials, n as composeFor, r as toYaml, rt as rootSite, t as CLUSTER_DATABASE, v as saveLocalCredentials, z as fakeKey } from "./compose-BHsW5nDE.mjs";
6
+ import { c as INBOX_RELAY_TOKEN_KEY, d as inboxWebhookUrl, i as readFakes, l as inboxRelayToken, o as objectsRoute, r as readCredentialFakes, u as inboxRelayWorker } from "./SubscriberGenerator-CzPrixYu.mjs";
7
+ import { g as appKey, v as protectedSubdomains } from "./config-BKpzKP6W.mjs";
8
+ import { i as holdsEveryCache, t as DEFAULT_CACHE } from "./types-Y1NfWlyG.mjs";
9
+ import { u as telemetryArgs } from "./EndpointGenerator-CusLH-9S.mjs";
10
+ import { i as discover, w as workerEdges, x as eventsEdgeBucket } from "./discover-DbDrCQ8h.mjs";
11
+ import { dirname, isAbsolute, join, parse, relative, sep } from "node:path";
12
+ import { existsSync, readFileSync } from "node:fs";
13
+ import { chmod, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
14
+ import { networkInterfaces } from "node:os";
15
+ import { execFile, spawn } from "node:child_process";
16
+ import { createServer } from "node:net";
17
+ import { createHash } from "node:crypto";
18
+ import { CLIENT_IP_HEADER, PUBLIC_PREFIX, TELEMETRY_KEYS, WILDCARD_SUBDOMAIN, bucketExpiry, cacheTable, cookieDomain, dependenciesOf, dependentsOf, drawSecret, externalApiUrl, isWildcardSite, mobileOrigins, provideKey, providedKeyFor, provisionOrder, publicEnvFor, schemeBase, wildcardDomainOf } from "@geekmidas/manifest";
19
+ import { formatKeyring } from "@geekmidas/constructs/encryption";
20
+ import { ownerRole, readerRole, roleStatements } from "@geekmidas/db/pg/roles";
21
+ import * as snsUrl from "@geekmidas/events/sns/url";
22
+ import * as sqsUrl from "@geekmidas/events/sqs/url";
23
+ import { cacheTableStatements } from "@geekmidas/cache/postgres";
24
+ import { promisify } from "node:util";
25
+ import { CreateBucketCommand, DeleteBucketLifecycleCommand, GetBucketLifecycleConfigurationCommand, GetBucketNotificationConfigurationCommand, GetBucketPolicyCommand, HeadBucketCommand, ListBucketsCommand, PutBucketLifecycleConfigurationCommand, PutBucketNotificationConfigurationCommand, PutBucketPolicyCommand, S3Client } from "@aws-sdk/client-s3";
26
+ import { CreateTopicCommand, GetTopicAttributesCommand, SNSClient } from "@aws-sdk/client-sns";
27
+ import { CreateQueueCommand, GetQueueUrlCommand, SQSClient } from "@aws-sdk/client-sqs";
28
+ import { Client } from "pg";
29
+ //#region src/generators/processorRoute.ts
30
+ /**
31
+ * Where `gkm dev` serves a file server's processors, on the dev server of the
32
+ * app holding them: named after the file server.
33
+ *
34
+ * One definition for both sides — the dev server mounts it, and the local
35
+ * edge rewrites each processor pattern on the file server's host onto it — so
36
+ * the path the edge sends and the path the server answers cannot drift.
37
+ */
38
+ const PROCESSORS_DEV_PREFIX = "/__gkm/processors/";
39
+ /** The base a file server's processors answer under, in `gkm dev`. */
40
+ function processorsDevRoute(fileServerId) {
41
+ return `${PROCESSORS_DEV_PREFIX}${fileServerId}`;
42
+ }
43
+ //#endregion
44
+ //#region src/reconcile/caddyfile.ts
45
+ /**
46
+ * The Caddy configuration a plan implies.
47
+ *
48
+ * A file server is a *domain that serves a bucket*, and locally it had neither:
49
+ * `UPLOADS_SERVER_URL` resolved to MinIO's path-style address, which works and
50
+ * is not the deployed shape. MinIO's own virtual-host mode cannot fix that —
51
+ * it reads the leading label *as the bucket name*, so it produces the right
52
+ * shape only when the server's id and the bucket's happen to agree, and never
53
+ * for a server fronting two buckets.
54
+ *
55
+ * So the mapping lives in front of MinIO instead: one host per file server,
56
+ * rewriting the bucket in as a path prefix. That is the whole of it — no
57
+ * caching, because caching is not what is missing locally, and a caching proxy
58
+ * would add an invalidation story a local stack has no use for.
59
+ *
60
+ * What it buys beyond the shape is a certificate. Caddy issues one per host
61
+ * from its own CA, so a local address is `https://` like the deployed one, and
62
+ * everything downstream of that — `Secure` cookies, `SameSite`, a cookie domain
63
+ * with more than one host under it — starts behaving the way it will in
64
+ * production rather than the way `http://localhost` allows.
65
+ *
66
+ * Pure, like `composeFor`: what should exist is data, and rendering it is one
67
+ * line at the edge.
68
+ */
69
+ /**
70
+ * The kinds the edge serves.
71
+ *
72
+ * Everything that owns a *public address*. Deployed, each of these has a real
73
+ * hostname and a certificate, so this is what makes the local shape the same
74
+ * one rather than a near-enough one — and the application is indifferent
75
+ * either way, because it reads whichever address was injected and never
76
+ * composes one.
77
+ *
78
+ * Adding a kind here is the whole of adding it to the edge.
79
+ */
80
+ const EDGE_KINDS = {
81
+ "file-server": true,
82
+ "rest-api": true,
83
+ "websocket-api": true,
84
+ site: true
85
+ };
86
+ /**
87
+ * How the edge reaches the host, from inside its container.
88
+ *
89
+ * A surface and a site are served by processes `gkm dev` starts on the host,
90
+ * not by containers — so the edge has to leave Docker's network to reach them.
91
+ * Docker Desktop resolves this name already; on Linux the compose definition
92
+ * maps it to the host gateway.
93
+ */
94
+ const HOST_GATEWAY = "host.docker.internal";
95
+ /**
96
+ * The suffix every local host shares.
97
+ *
98
+ * `.localhost` rather than a made-up TLD because it resolves to loopback
99
+ * without touching `/etc/hosts` — macOS and systemd-resolved both answer for
100
+ * subdomains of it, and Caddy issues internal certificates for it by default.
101
+ */
102
+ const LOCAL_TLD = "localhost";
103
+ /**
104
+ * The sites a plan implies, one per file server.
105
+ *
106
+ * The leading label is the *server's* stage-scoped name rather than the
107
+ * bucket's, which is the difference that matters: two servers over one bucket
108
+ * are a legitimate arrangement — two cache behaviours, one origin — and naming
109
+ * the host after the bucket would make them collide.
110
+ */
111
+ function sitesFor(plan, project, addresses = {}, processorAddresses = {}) {
112
+ const byId = new Map(plan.resources.map((r) => [r.id, r]));
113
+ const sites = [];
114
+ for (const resource of plan.resources) {
115
+ if (!EDGE_KINDS[resource.kind]) continue;
116
+ const host = hostFor$1(resource, project);
117
+ if (resource.kind === "file-server") {
118
+ const origin = resource.of ? byId.get(resource.of) : void 0;
119
+ if (!origin) continue;
120
+ const address = processorAddresses[resource.id];
121
+ const port = address ? portOf(address) : void 0;
122
+ sites.push({
123
+ host,
124
+ upstream: "http://minio:9000",
125
+ rewrite: `/${origin.name}{uri}`,
126
+ ...resource.processors?.length && port ? { processors: {
127
+ prefixes: resource.processors,
128
+ upstream: `http://${HOST_GATEWAY}:${port}`,
129
+ under: processorsDevRoute(resource.id)
130
+ } } : {}
131
+ });
132
+ continue;
133
+ }
134
+ const address = addresses[resource.id];
135
+ if (!address) continue;
136
+ const port = portOf(address);
137
+ if (!port) continue;
138
+ const upstream = `http://${HOST_GATEWAY}:${port}`;
139
+ if (resource.subdomain === WILDCARD_SUBDOMAIN) {
140
+ sites.push({
141
+ host,
142
+ upstream,
143
+ tenants: tenantsOf(resource, host, addresses)
144
+ });
145
+ continue;
146
+ }
147
+ sites.push({
148
+ host,
149
+ upstream
150
+ });
151
+ }
152
+ return sites;
153
+ }
154
+ /**
155
+ * What stands between a wildcard site and a host that is not a tenant.
156
+ *
157
+ * Caddy already prefers a named host to the `*`, so `api.shop.localhost`
158
+ * reaches the API whatever the wildcard says. What it cannot know is the
159
+ * labels nothing is running on: `www`, or an app this stage has not started.
160
+ * Those are refused here, so no slug that a deploy would protect is ever
161
+ * served locally either.
162
+ *
163
+ * The `hosts` route is asked through `forward_auth`, which is a deployed
164
+ * edge's check in miniature: a `2xx` lets the request through, and any other
165
+ * answer — the app's own `404` — is what the browser gets, so a mistyped
166
+ * slug fails here and not first in production. Without an address for the
167
+ * surface serving it there is nothing to ask, and every host is served.
168
+ */
169
+ function tenantsOf(resource, host, addresses) {
170
+ const domain = host.slice(`${WILDCARD_SUBDOMAIN}.`.length);
171
+ const surface = resource.hosts ? addresses[resource.hosts.surface] : void 0;
172
+ const port = surface ? portOf(surface) : void 0;
173
+ return {
174
+ protected: (resource.protectedSubdomains ?? []).map((label) => `${label}.${domain}`),
175
+ ...resource.hosts && port ? { hosts: {
176
+ upstream: `http://${HOST_GATEWAY}:${port}`,
177
+ uri: `${resource.hosts.path}?domain={host}`
178
+ } } : {}
179
+ };
180
+ }
181
+ /** The port a local address answers on, defaulting by scheme. */
182
+ function portOf(address) {
183
+ try {
184
+ const { port, protocol } = new URL(address);
185
+ return port || (protocol === "https:" ? "443" : "80");
186
+ } catch {
187
+ return;
188
+ }
189
+ }
190
+ /**
191
+ * Where one file server answers, including the stage its name carries.
192
+ *
193
+ * The project is a label rather than a decoration: it is what keeps two
194
+ * checkouts from answering on one address. Omitted when there is no project to
195
+ * name, because an empty label makes `uploads..localhost` — a host nothing
196
+ * resolves and no certificate can be issued for.
197
+ */
198
+ function hostFor$1(resource, project) {
199
+ const label = resource.root && project ? stageOf(resource) : labelOf(resource);
200
+ if (resource.subdomain === WILDCARD_SUBDOMAIN) return [
201
+ WILDCARD_SUBDOMAIN,
202
+ stageOf(resource),
203
+ project,
204
+ LOCAL_TLD
205
+ ].filter(Boolean).join(".");
206
+ return [
207
+ label,
208
+ project,
209
+ LOCAL_TLD
210
+ ].filter(Boolean).join(".");
211
+ }
212
+ /**
213
+ * A resource's own label: its `subdomain` where it declared one — the label a
214
+ * deploy uses too — with the stage it carries kept, so `gkm test` and `gkm dev`
215
+ * still answer on different hosts.
216
+ */
217
+ function labelOf(resource) {
218
+ if (!resource.subdomain) return resource.name;
219
+ const stage = stageOf(resource);
220
+ return stage ? `${resource.subdomain}-${stage}` : resource.subdomain;
221
+ }
222
+ /** The stage a resource's name carries, or nothing for the local stage's. */
223
+ function stageOf(resource) {
224
+ const base = resource.id.toLowerCase();
225
+ return resource.name === base ? void 0 : resource.name.slice(base.length + 1);
226
+ }
227
+ /**
228
+ * The static root config, which only ever imports.
229
+ *
230
+ * Written once and identical for every stage, because the *sites* are what
231
+ * differ and they live in their own files. That split is not tidiness: one
232
+ * container serves every stage — the same way one Postgres holds `orders` and
233
+ * `orders_test` — so a single Caddyfile would mean `gkm test` deleting the
234
+ * routes `gkm dev` is serving, and each command silently breaking the other.
235
+ */
236
+ function caddyfileRoot() {
237
+ return `${GENERATED}
238
+ {
239
+ # No ACME, no public DNS: the CA is local and so are the names.
240
+ local_certs
241
+ auto_https disable_redirects
242
+ }
243
+
244
+ # One file per stage, so two stages share this edge the way they already share
245
+ # one Postgres and one MinIO.
246
+ import /etc/caddy/sites/*.caddy
247
+ `;
248
+ }
249
+ /**
250
+ * One stage's sites, as the file imported above.
251
+ *
252
+ * `rewrite` rather than a path prefix on the address: the bucket is the
253
+ * origin's business, and the client asks for the key it stored.
254
+ *
255
+ * `header_up Host {upstream_hostport}` matters more than it looks. MinIO routes
256
+ * and signs on the Host header, so forwarding the requested hostname would make
257
+ * it look for a bucket named after the domain.
258
+ */
259
+ function toCaddyfile(sites) {
260
+ if (sites.length === 0) return `${GENERATED}
261
+ # No file server is declared in this stage, so it routes nothing.
262
+ `;
263
+ const blocks = sites.map(({ host, upstream, rewrite, processors, tenants }) => {
264
+ const proxy = `reverse_proxy ${upstream} {${rewrite ? `
265
+ rewrite ${rewrite}
266
+ # MinIO routes and signs on the Host header, so forwarding the requested
267
+ # hostname would make it look for a bucket named after the domain.
268
+ header_up Host {upstream_hostport}` : `
269
+ # The application reads whatever address was injected, so it has to see
270
+ # the one a caller used — that is what makes a redirect, a cookie domain
271
+ # and a generated link point back here rather than at the upstream.
272
+ header_up Host {host}
273
+ header_up X-Forwarded-Proto {scheme}`}
274
+ }`;
275
+ return `
276
+ https://${host} {
277
+ tls internal
278
+
279
+ # gkm's own header, which only a service of this stack may send: the auth
280
+ # server rate-limits by the client address it carries.
281
+ request_header -${CLIENT_IP_HEADER}
282
+ ${processors ? `
283
+ # Its processors' patterns, to the dev server holding them — under the
284
+ # route it mounts them on. Answered there, so the bucket below never is.
285
+ @processors path ${processors.prefixes.map((prefix) => `${prefix}*`).join(" ")}
286
+ handle @processors {
287
+ reverse_proxy ${processors.upstream} {
288
+ rewrite ${processors.under}{uri}
289
+ header_up Host {host}
290
+ }
291
+ }
292
+ ` : ""}${tenants ? tenantBlock(tenants, proxy) : ` ${proxy}`}
293
+ }`;
294
+ });
295
+ return `${GENERATED}${blocks.join("\n")}\n`;
296
+ }
297
+ /**
298
+ * A wildcard site's routes: a protected host refused, every other host asked
299
+ * about before it is proxied. `handle` blocks rather than bare directives,
300
+ * because Caddy orders directives itself and would proxy before it refused.
301
+ */
302
+ function tenantBlock(tenants, proxy) {
303
+ return `${tenants.protected.length ? `
304
+ # Labels an app answers on, or that the workspace protects: never a
305
+ # tenant's, whether or not anything is running on them.
306
+ @protected host ${tenants.protected.join(" ")}
307
+ handle @protected {
308
+ respond 404
309
+ }
310
+ ` : ""}
311
+ handle {${tenants.hosts ? `
312
+ # Which hosts are tenants is the app's to say: its \`hosts\` route
313
+ # answers 200 to serve this one, and anything else goes back as is.
314
+ forward_auth ${tenants.hosts.upstream} {
315
+ uri ${tenants.hosts.uri}
316
+ }
317
+ ` : ""}
318
+ ${proxy.replaceAll("\n", "\n ")}
319
+ }`;
320
+ }
321
+ const GENERATED = `# Generated by gkm from the construct manifest — do not edit.
322
+ #
323
+ # One host per declared file server, in front of the object store that holds its
324
+ # objects. This is the shape a file server has deployed — a domain serving a
325
+ # bucket — which MinIO alone cannot produce: its virtual-host mode reads the
326
+ # leading label as the bucket name.
327
+ #
328
+ # Certificates come from Caddy's own CA. "gkm dev" and "gkm test" export its
329
+ # root and point Node at it, so nothing has to be installed to use these URLs
330
+ # from code; a browser needs the root trusted once.
331
+ `;
332
+ //#endregion
333
+ //#region src/telemetry/edges.ts
334
+ /**
335
+ * Which processes use a `Telemetry` node — read off the manifest's edges.
336
+ *
337
+ * A process uses telemetry when its own declaration names the node: a
338
+ * surface's `telemetry`, a worker's. A surface's process runs its own
339
+ * endpoints, so a worker's edge never counts for it.
340
+ *
341
+ * A site's edge is recorded and never followed here: a site is handed none of
342
+ * the node's keys (see `PUBLIC.telemetry`).
343
+ */
344
+ /** The `OTEL_*` keys a `Telemetry` node provides. */
345
+ const TELEMETRY_ENV_KEYS = TELEMETRY_KEYS;
346
+ /** Whether `key` is one of the keys a `Telemetry` node provides. */
347
+ function isTelemetryKey(key) {
348
+ return TELEMETRY_ENV_KEYS.includes(key);
349
+ }
350
+ /** The node a declaration's `telemetry` edge names, when it names one. */
351
+ function nodeOf(manifest, id) {
352
+ if (!id) return void 0;
353
+ const node = manifest[id];
354
+ return node?.kind === "telemetry" ? node : void 0;
355
+ }
356
+ /**
357
+ * The `Telemetry` node the process for `id` — a surface, a worker, or a file
358
+ * server's processor unit, by construct id — emits through, or undefined
359
+ * when it has no edge to one.
360
+ */
361
+ function telemetryOf(manifest, id) {
362
+ const declaration = manifest[id];
363
+ if (!declaration) return void 0;
364
+ if (declaration.kind === "worker" || declaration.kind === "file-server") return nodeOf(manifest, declaration.telemetry);
365
+ if (declaration.kind !== "rest-api" && declaration.kind !== "websocket-api") return void 0;
366
+ return nodeOf(manifest, declaration.telemetry);
367
+ }
368
+ /**
369
+ * The `Telemetry` node the app `appName` emits through — its surface's, or
370
+ * undefined for a site, an app without one, or one with no edge.
371
+ */
372
+ function appTelemetry(manifest, appName) {
373
+ const id = Object.entries(manifest).find(([id, d]) => (d.kind === "rest-api" || d.kind === "websocket-api") && appKey(id) === appName)?.[0];
374
+ return id ? telemetryOf(manifest, id) : void 0;
375
+ }
376
+ /** Whether any process in the manifest emits through a `Telemetry` node. */
377
+ function usesTelemetry(manifest) {
378
+ return Object.entries(manifest).some(([id, d]) => (d.kind === "rest-api" || d.kind === "websocket-api" || d.kind === "worker" || d.kind === "file-server") && telemetryOf(manifest, id) !== void 0);
379
+ }
380
+ /**
381
+ * One process's share of the telemetry keys a stage resolved: all of them,
382
+ * named for the process, when it has an edge to the node — and none
383
+ * otherwise.
384
+ *
385
+ * `env` is everything else the process is handed; its `OTEL_*` keys are
386
+ * replaced, never merged, so a process without the edge cannot inherit them.
387
+ */
388
+ function scopeTelemetryEnv(env, options) {
389
+ const scoped = Object.fromEntries(Object.entries(env).filter(([key]) => !isTelemetryKey(key)));
390
+ if (!options.uses || !options.telemetry) return scoped;
391
+ const values = Object.fromEntries(Object.entries(options.telemetry).filter(([key]) => isTelemetryKey(key)));
392
+ if (!values.OTEL_EXPORTER_OTLP_ENDPOINT) return scoped;
393
+ return {
394
+ ...scoped,
395
+ ...values,
396
+ OTEL_SERVICE_NAME: options.serviceName
397
+ };
398
+ }
399
+ /** Whether the site `siteId` has an edge to a `Telemetry` node. */
400
+ function siteUsesTelemetry(manifest, siteId) {
401
+ const site = manifest[siteId];
402
+ return site?.kind === "site" && nodeOf(manifest, site.telemetry) !== void 0;
403
+ }
404
+ /**
405
+ * Whether the client of the surface `surfaceId` propagates trace context:
406
+ * when a site that calls it has an edge to a `Telemetry` node. One client
407
+ * serves every site that calls the surface on the host, so one with the
408
+ * edge turns it on; a site's image generates its own, from its own edge.
409
+ */
410
+ function surfaceClientTraced(manifest, surfaceId) {
411
+ return Object.entries(manifest).some(([id, d]) => d.kind === "site" && siteUsesTelemetry(manifest, id) && d.dependencies.some((edge) => edge.target === surfaceId));
412
+ }
413
+ /**
414
+ * Whether the site app `appName`'s clients propagate trace context, and at
415
+ * what rate: when the site has an edge to a `Telemetry` construct, at the
416
+ * stage's sample rate — a public value, every trace locally — and not at all
417
+ * for a stage that sends no telemetry.
418
+ */
419
+ function siteClientTelemetry(manifest, appName, workspace, stage) {
420
+ const id = Object.entries(manifest).find(([id, d]) => d.kind === "site" && appKey(id) === appName)?.[0];
421
+ if (!id || !siteUsesTelemetry(manifest, id)) return void 0;
422
+ const sampleRate = stageSampleRate(workspace.deploy?.telemetry, stage ?? workspace.stages.local, !stage || stage === workspace.stages.local);
423
+ return sampleRate === void 0 ? void 0 : { sampleRate };
424
+ }
425
+ //#endregion
426
+ //#region src/reconcile/plan.ts
427
+ /**
428
+ * The reconcile plan — what a manifest wants to exist, for one stage.
429
+ *
430
+ * Pure. Turning the plan into running containers and provisioned resources is
431
+ * the applier's job; deciding *what* should exist is this, so the decisions can
432
+ * be asserted as data rather than by starting Docker.
433
+ *
434
+ * One plan serves every stage. `gkm dev` reconciles `development` and `gkm test`
435
+ * reconciles `test`; they differ only in what the resources are called, never in
436
+ * what infrastructure exists. Roles are cluster-scoped in Postgres while grants
437
+ * are per-object, so one container and one role pair serve both.
438
+ */
439
+ /**
440
+ * Which container serves a kind.
441
+ *
442
+ * Derived kinds map to the same container as their parent — a schema tenant
443
+ * lives in its parent's Postgres — so the set is deduplicated rather than
444
+ * counted.
445
+ */
446
+ const CONTAINERS = {
447
+ database: "postgres",
448
+ "database-reader": "postgres",
449
+ "database-schema": "postgres",
450
+ objects: "minio",
451
+ "file-server": "caddy",
452
+ email: "mailpit",
453
+ inbox: "mailpit",
454
+ "websocket-api": "redis"
455
+ };
456
+ /**
457
+ * The container a cache backend needs locally.
458
+ *
459
+ * Each one matches the protocol its deployed form speaks, which is the property
460
+ * worth having: a cache that behaves differently in the two places is worse than
461
+ * a slower one.
462
+ *
463
+ * `db` is deliberately absent, the same way `pgboss` is absent from
464
+ * `EVENT_CONTAINERS`: the cache is a table in a database the manifest already
465
+ * declares, so mapping it here would start a container for a project that needs
466
+ * none.
467
+ */
468
+ const CACHE_CONTAINERS = {
469
+ upstash: "redis-http",
470
+ elasticache: "redis",
471
+ redis: "redis"
472
+ };
473
+ /**
474
+ * Kinds that resolve a value without anything running.
475
+ *
476
+ * A secret is the case: it has no address, so there is nothing to start and
477
+ * nothing to connect to — but it still appears in the plan, because the target
478
+ * still has to resolve it.
479
+ */
480
+ const CONTAINERLESS = {
481
+ secret: true,
482
+ credential: true,
483
+ encryption: true,
484
+ "rest-api": true,
485
+ site: true,
486
+ "mobile-app": true,
487
+ "external-api": true,
488
+ telemetry: true
489
+ };
490
+ /** The container that collects telemetry locally, and serves its UI. */
491
+ const TELEMETRY_CONTAINER = "openobserve";
492
+ /**
493
+ * Containers that cannot run alone.
494
+ *
495
+ * The one case is the cache: `serverless-redis-http` is a proxy and needs the
496
+ * Redis it proxies. Kept here rather than in the compose definition so the plan
497
+ * -- which is what decides what must be running -- still names everything.
498
+ */
499
+ const REQUIRES = { "redis-http": ["redis"] };
500
+ /**
501
+ * The role each kind's one key plays, where it is not a URL.
502
+ *
503
+ * The twin of `ProvidesByKind` in `@geekmidas/manifest`: that is what a
504
+ * construct declares, this is what the local target resolves, and both derive
505
+ * the key from the same `provideKey` so they cannot disagree.
506
+ */
507
+ const ROLES = {
508
+ queue: "publisherConnectionString",
509
+ topic: "publisherConnectionString",
510
+ credential: "credentials",
511
+ "mobile-app": "scheme"
512
+ };
513
+ /** The kinds whose container is the events backend's rather than their own. */
514
+ const EVENT_KINDS = {
515
+ queue: true,
516
+ topic: true
517
+ };
518
+ /**
519
+ * Which kinds provision something *inside* their container.
520
+ *
521
+ * A database creates a database, a bucket creates a bucket; mail creates
522
+ * nothing — Mailpit accepts whatever is sent to it and has no per-stage object
523
+ * to name. Both still appear in the plan, because both still resolve a URL; the
524
+ * flag is the difference between "must be running" and "must be created".
525
+ */
526
+ const PROVISIONS = {
527
+ database: true,
528
+ "database-reader": true,
529
+ "database-schema": true,
530
+ objects: true,
531
+ cache: true,
532
+ "file-server": true
533
+ };
534
+ /**
535
+ * The container each events backend needs, where it needs one of its own.
536
+ *
537
+ * `pgboss` is deliberately absent rather than mapped to `postgres`: it is a
538
+ * schema tenant in a database the manifest already declares, so mapping it here
539
+ * would start a Postgres for a project that declared no database at all.
540
+ *
541
+ * This one comes from config rather than from a declaration because there are
542
+ * no `topic` or `queue` kinds yet. When they land the mapping moves to
543
+ * {@link CONTAINERS} keyed by kind, and this constant goes with them.
544
+ */
545
+ const EVENT_CONTAINERS = {
546
+ sns: "localstack",
547
+ rabbitmq: "rabbitmq"
548
+ };
549
+ /** The backend a project gets when it declares queues or topics and says nothing. */
550
+ const DEFAULT_EVENTS = "pgboss";
551
+ /**
552
+ * How a resource of each kind separates its stage suffix.
553
+ *
554
+ * Postgres identifiers are snake_case and a hyphen would need quoting
555
+ * everywhere; buckets and queues are kebab and disallow underscores in some
556
+ * providers. The suffix is the same word either way.
557
+ */
558
+ const SEPARATORS = {
559
+ database: "_",
560
+ "database-reader": "_",
561
+ "database-schema": "_",
562
+ objects: "-"
563
+ };
564
+ /**
565
+ * The stage-scoped name for a resource.
566
+ *
567
+ * The local stage carries no suffix, so `gkm dev` keeps using `orders` and a
568
+ * developer's psql history, saved connections, and muscle memory survive a
569
+ * change of stage name. Every other stage is suffixed.
570
+ */
571
+ function resourceName(id, kind, stage, localStage) {
572
+ const base = id.toLowerCase();
573
+ if (stage === localStage) return base;
574
+ return `${base}${SEPARATORS[kind] ?? "-"}${stage}`;
575
+ }
576
+ /**
577
+ * The container a kind needs, or `undefined` if it needs none.
578
+ *
579
+ * Queues and topics have no container of their own — they live in whichever
580
+ * broker the project selected, which is why the backend is an argument here
581
+ * rather than a second pass that adds containers after the fact.
582
+ */
583
+ function containerFor(kind, events = DEFAULT_EVENTS, cache = DEFAULT_CACHE.aws, derived = false, edge = true) {
584
+ if (EVENT_KINDS[kind]) return EVENT_CONTAINERS[events] ?? CONTAINERS.database;
585
+ if (kind === "file-server") return edge ? "caddy" : CONTAINERS.objects;
586
+ if (kind === "cache") {
587
+ if (derived && !holdsEveryCache(cache)) return CONTAINERS.database;
588
+ return CACHE_CONTAINERS[cache] ?? CONTAINERS.database;
589
+ }
590
+ return CONTAINERS[kind];
591
+ }
592
+ /**
593
+ * Build the plan for a manifest and stage.
594
+ *
595
+ * `order` is the caller's provisioning order — parents before children — which
596
+ * `provisionOrder` produces from the manifest. Passing it in keeps this
597
+ * function free of the traversal and makes the ordering assertable on its own.
598
+ */
599
+ function planFor(manifest, stage, order, options = {}) {
600
+ const containers = /* @__PURE__ */ new Set();
601
+ const resources = [];
602
+ const fakes = {};
603
+ const events = options.events ?? "pgboss";
604
+ const cache = options.cache ?? DEFAULT_CACHE.aws;
605
+ for (const id of order) {
606
+ const declaration = manifest[id];
607
+ if (!declaration) continue;
608
+ const local = declaration.kind === "external-api" ? options.fakes?.[id] : void 0;
609
+ const fake = local && {
610
+ key: fakeKey(id),
611
+ ...local
612
+ };
613
+ if (fake) fakes[fake.key] = {
614
+ id,
615
+ ...fake.image ? {
616
+ image: fake.image,
617
+ port: fake.port
618
+ } : {}
619
+ };
620
+ const detached = declaration.kind === "cache" && holdsEveryCache(cache);
621
+ const container = fake?.image ? fake.key : declaration.kind === "telemetry" ? usesTelemetry(manifest) ? TELEMETRY_CONTAINER : void 0 : containerFor(declaration.kind, events, cache, "of" in declaration && typeof declaration.of === "string", options.edge !== false);
622
+ if (!container && !CONTAINERLESS[declaration.kind]) continue;
623
+ if (container) {
624
+ containers.add(container);
625
+ for (const required of REQUIRES[container] ?? []) containers.add(required);
626
+ }
627
+ resources.push({
628
+ id,
629
+ kind: declaration.kind,
630
+ ...container ? { container } : {},
631
+ name: resourceName(id, declaration.kind, stage, options.localStage),
632
+ envKey: providedKeyFor(id, declaration.kind, ROLES[declaration.kind] ?? "url"),
633
+ provisions: PROVISIONS[declaration.kind] === true,
634
+ ...declaration.kind === "rest-api" || declaration.kind === "websocket-api" || declaration.kind === "objects" || declaration.kind === "file-server" ? { callers: dependentsOf(manifest, id) } : {},
635
+ ...declaration.kind === "site" ? { publicEnv: publicEnvFor(declaration, manifest) } : {},
636
+ ...declaration.kind === "mobile-app" ? {
637
+ publicEnv: publicEnvFor(declaration, manifest),
638
+ ...declaration.scheme ? { scheme: declaration.scheme } : {}
639
+ } : {},
640
+ ...declaration.kind === "file-server" && declaration.open?.length ? { open: declaration.open } : {},
641
+ ...declaration.kind === "file-server" && declaration.processors?.length ? { processors: [...new Set(declaration.processors.map((p) => p.prefix))].sort() } : {},
642
+ ...declaration.kind === "inbox" ? { inbox: {
643
+ addresses: declaration.addresses,
644
+ subdomains: declaration.subdomains === true,
645
+ topic: declaration.topic
646
+ } } : {},
647
+ ...declaration.kind === "objects" && bucketExpiry(manifest, id) ? { expire: bucketExpiry(manifest, id) } : {},
648
+ ...(declaration.kind === "rest-api" || declaration.kind === "websocket-api" || declaration.kind === "site") && declaration.subdomain ? { subdomain: declaration.subdomain } : {},
649
+ ...isWildcardSite(declaration) ? {
650
+ protectedSubdomains: protectedSubdomains(manifest, options.protectedSubdomains),
651
+ ...declaration.hosts ? { hosts: declaration.hosts } : {}
652
+ } : {},
653
+ ...fake ? { fake: {
654
+ key: fake.key,
655
+ credentials: fake.credentials
656
+ } } : {},
657
+ ...declaration.kind === "external-api" && !fake ? { url: declaration.url } : {},
658
+ ...declaration.kind === "credential" && options.credentialFakes?.[id] !== void 0 ? { fakeCredentials: options.credentialFakes[id] } : {},
659
+ ...declaration.kind === "secret" && (declaration.length !== void 0 || declaration.symbols !== void 0) ? { format: {
660
+ ...declaration.length !== void 0 ? { length: declaration.length } : {},
661
+ ...declaration.symbols !== void 0 ? { symbols: declaration.symbols } : {}
662
+ } } : {},
663
+ ..."of" in declaration && !detached ? { of: declaration.of } : {},
664
+ ..."schema" in declaration && declaration.schema ? { schema: declaration.schema } : {},
665
+ ..."version" in declaration && declaration.version ? { version: declaration.version } : {},
666
+ ..."image" in declaration && declaration.image ? { image: declaration.image } : {},
667
+ ..."extensions" in declaration && declaration.extensions?.length ? { extensions: declaration.extensions } : {},
668
+ ..."roles" in declaration && declaration.roles === false ? { roles: false } : {},
669
+ ..."table" in declaration && declaration.table && !detached ? { table: declaration.table } : {}
670
+ });
671
+ }
672
+ if (options.edge !== false && resources.some((r) => EDGE_KINDS[r.kind] === true)) containers.add("caddy");
673
+ for (const extra of options.extraContainers ?? []) containers.add(extra);
674
+ if (events === "pgboss" && resources.some((r) => EVENT_KINDS[r.kind]) && !resources.some((r) => r.kind === "database")) throw new PgBossNeedsDatabase(resources.filter((r) => EVENT_KINDS[r.kind]).map((r) => r.id));
675
+ if (cache === "db") {
676
+ const placeless = resources.filter((r) => r.kind === "cache" && !r.of);
677
+ if (placeless.length > 0) {
678
+ const databases = resources.filter((r) => r.kind === "database");
679
+ const [only] = databases;
680
+ if (!only) throw new CacheNeedsDatabase(resources.filter((r) => r.kind === "cache").map((r) => r.id));
681
+ if (databases.length > 1) throw new CacheIsAmbiguous(placeless.map((r) => r.id), databases.map((r) => r.id));
682
+ for (const resource of placeless) resource.of = only.id;
683
+ }
684
+ }
685
+ const sites = resources.filter((r) => r.kind === "site" && r.subdomain !== WILDCARD_SUBDOMAIN);
686
+ const root = rootSite(sites.map((r) => {
687
+ const declared = manifest[r.id];
688
+ return {
689
+ name: appKey(r.id),
690
+ ...declared?.kind === "site" && declared.root ? { root: true } : {}
691
+ };
692
+ }));
693
+ for (const site of sites) if (appKey(site.id) === root) site.root = true;
694
+ const workerBroker = events === "pgboss" && Object.values(manifest).some((d) => d?.kind === "worker") && resources.some((r) => r.kind === "database");
695
+ return {
696
+ stage,
697
+ events,
698
+ cache,
699
+ containers: [...containers],
700
+ resources,
701
+ ...Object.keys(fakes).length ? { fakes } : {},
702
+ ...workerBroker ? { workerBroker: true } : {}
703
+ };
704
+ }
705
+ /**
706
+ * Queues or topics were declared with pg-boss and no database to hold them.
707
+ *
708
+ * Not a reconcile failure but a manifest one: the fix is a line of application
709
+ * code either way — declare a database, or select a backend that brings its own
710
+ * broker.
711
+ */
712
+ var CacheNeedsDatabase = class extends GkmError {
713
+ ids;
714
+ constructor(ids) {
715
+ super(`A cache backed by the database needs a declared database, and none was declared, and on this deploy target that is where a cache lives. Declare one. Caches affected: ${ids.join(", ")}.`);
716
+ this.ids = ids;
717
+ this.name = "CacheNeedsDatabase";
718
+ }
719
+ };
720
+ /**
721
+ * A `'db'` cache — a server target's default — in an app that declares more
722
+ * than one database.
723
+ *
724
+ * The target says *a* database and the graph offers several, so there is no
725
+ * answer to give — and the wrong kind of failure would be to pick one, since a
726
+ * cache silently landing in the wrong database is a bug that surfaces as
727
+ * missing rows much later. The fix is a stronger statement in application code:
728
+ * `orders.cache('Sessions')` names the database, which is a fact about the
729
+ * application rather than a deployment choice.
730
+ */
731
+ var CacheIsAmbiguous = class extends GkmError {
732
+ ids;
733
+ databases;
734
+ constructor(ids, databases) {
735
+ super(`A cache backed by the database needs to know which one, and this app declares ${databases.length}: ${databases.join(", ")}. Declare the cache from its database — e.g. ${databases[0]}.cache('Sessions') — rather than as a bare new Cache(). Caches affected: ${ids.join(", ")}.`);
736
+ this.ids = ids;
737
+ this.databases = databases;
738
+ this.name = "CacheIsAmbiguous";
739
+ }
740
+ };
741
+ var PgBossNeedsDatabase = class extends GkmError {
742
+ ids;
743
+ constructor(ids) {
744
+ super(`pg-boss keeps its queues in a declared database, and none was declared. Declare one: on this deploy target, events are carried by pg-boss. Queues and topics affected: ${ids.join(", ")}.`);
745
+ this.ids = ids;
746
+ this.name = "PgBossNeedsDatabase";
747
+ }
748
+ };
749
+ //#endregion
750
+ //#region src/reconcile/env.ts
751
+ /**
752
+ * The URLs a reconciled stage injects.
753
+ *
754
+ * One `<NAME>_URL` per construct, and the protocol picks the driver. This is the
755
+ * seam between the target that writes a URL and the client that parses it, so
756
+ * every component of it is read off the resource — never inherited from ambient
757
+ * environment. A bucket can live in a different region than the function reading
758
+ * it, and `AWS_REGION` in a Lambda is the *function's* region, so a URL that
759
+ * omits `?region=` breaks cross-region silently at runtime.
760
+ *
761
+ * Locally that self-containment is what lets the port be whatever was free: the
762
+ * app reads `ORDERS_URL` and never sees a host or a port.
763
+ */
764
+ /** The host containers are published on. */
765
+ const LOCAL_HOST = "localhost";
766
+ /** The region local buckets claim, so the client has one to sign with. */
767
+ const LOCAL_REGION = "us-east-1";
768
+ /**
769
+ * The credentials the S3 client resolves for MinIO.
770
+ *
771
+ * An `s3://` URL deliberately carries none: deployed, the SDK reads them from
772
+ * the execution role, so a URL that embedded a key would be one more thing to
773
+ * rotate and one more thing to leak into a log. Locally there is no role, and
774
+ * the same chain reads these — which is why they are injected beside the URL
775
+ * rather than written into it.
776
+ */
777
+ function storageCredentials(credentials) {
778
+ return {
779
+ AWS_ACCESS_KEY_ID: credentials.minio.user,
780
+ AWS_SECRET_ACCESS_KEY: credentials.minio.password,
781
+ AWS_REGION: LOCAL_REGION
782
+ };
783
+ }
784
+ /** The port Mailpit's inbox — its web UI and HTTP API — is published under. */
785
+ const MAILPIT_INBOX_PORT = "mailpit-web";
786
+ /** The schema pg-boss keeps its tables in, inside the declared database. */
787
+ const PGBOSS_SCHEMA = "pgboss";
788
+ /** The exchange every local topic and queue shares on RabbitMQ. */
789
+ const RABBITMQ_EXCHANGE = "gkm.events";
790
+ /** The credential `options` describe. */
791
+ function credentialOf(options) {
792
+ return {
793
+ containers: options.credentials,
794
+ ...options.seed ? { seed: options.seed } : {}
795
+ };
796
+ }
797
+ /**
798
+ * What project code run against a stage is handed — a migration, a seed, an
799
+ * app under `gkm dev`, `gkm exec`'s command, `gkm compose`'s seeds: the
800
+ * stage's stored secrets, under every value the stage resolved.
801
+ *
802
+ * One merge for every command, so a seed sees the same keys whichever one
803
+ * runs it. Each command supplies only what is its own — the ports it reaches
804
+ * the containers on, their logins — as inputs to {@link envFor}.
805
+ *
806
+ * The resolved values win: they are the statement of what exists, and a
807
+ * stored URL naming an old port is the drift resolution replaces. A command
808
+ * whose stage says a value by hand (a deployed stack's) leaves that key out
809
+ * of `resolved`.
810
+ */
811
+ function stageEnv(stored, resolved) {
812
+ return {
813
+ ...stored,
814
+ ...resolved
815
+ };
816
+ }
817
+ /**
818
+ * Every env key a reconciled plan resolves, and its value.
819
+ *
820
+ * Keys come from the declaration rather than being re-derived, so what the build
821
+ * publishes and what the app reads cannot drift.
822
+ */
823
+ function envFor(plan, options) {
824
+ const env = {};
825
+ const credential = credentialOf(options);
826
+ const resolved = {};
827
+ const devices = options.lanAddress ? deviceBuckets(plan) : /* @__PURE__ */ new Set();
828
+ for (const resource of plan.resources) {
829
+ const url = urlFor(resource, plan, options.ports, options.project ?? "", options.addresses, credential, devices.has(resource.id) ? options.lanAddress : void 0);
830
+ if (url) resolved[resource.id] = url;
831
+ }
832
+ for (const resource of plan.resources) {
833
+ const url = resolved[resource.id];
834
+ if (resource.kind === "rest-api") Object.assign(env, surfaceEnv(resource, url, resolved, {
835
+ plan,
836
+ ...options.addresses ? { addresses: options.addresses } : {},
837
+ ...options.lanAddress ? { lanAddress: options.lanAddress } : {},
838
+ ...options.metroPorts ? { metroPorts: options.metroPorts } : {}
839
+ }));
840
+ if (resource.kind === "websocket-api") {
841
+ const origins = surfaceEnv(resource, url, resolved)[provideKey(resource.id, "trustedOrigins")];
842
+ if (origins !== void 0) env[provideKey(resource.id, "trustedOrigins")] = origins;
843
+ const publisher = webSocketPublisher(resource, options.ports, options.project ?? "", credential);
844
+ if (publisher) env[provideKey(resource.id, "publisherConnectionString")] = publisher;
845
+ }
846
+ if (url) env[resource.envKey] = url;
847
+ if (resource.kind === "site" && resource.protectedSubdomains && url) {
848
+ const domain = wildcardDomainOf(new URL(url).hostname);
849
+ if (domain) {
850
+ env[provideKey(resource.id, "domain")] = domain;
851
+ env[provideKey(resource.id, "protectedSubdomains")] = resource.protectedSubdomains.join(",");
852
+ }
853
+ }
854
+ if (isDatabase(resource.kind) && resource.roles !== false) {
855
+ const owner = ownerUrl(resource, plan, options.ports, options.project ?? "", credential);
856
+ if (owner) env[provideKey(resource.id, "ownerUrl")] = owner;
857
+ }
858
+ if (resource.kind === "external-api" && url && resource.fake) env[provideKey(resource.id, "credentials")] = resource.fake.credentials;
859
+ if (resource.kind === "credential" && resource.fakeCredentials !== void 0) env[resource.envKey] = resource.fakeCredentials;
860
+ if (resource.kind === "telemetry" && url) {
861
+ const login = options.credentials.logs;
862
+ Object.assign(env, telemetryEnv({
863
+ endpoint: url,
864
+ headers: otlpHeaders(login.email, login.password)
865
+ }, 1));
866
+ }
867
+ if (resource.kind === "inbox" && url) {
868
+ const base = resource.id.toLowerCase();
869
+ const stage = resource.name === base ? void 0 : resource.name.slice(base.length + 1);
870
+ env[provideKey(resource.id, "domain")] = [
871
+ stage,
872
+ options.project,
873
+ LOCAL_HOST
874
+ ].filter(Boolean).join(".");
875
+ const smtp = options.ports[primaryPortKey("mailpit")];
876
+ if (smtp !== void 0) env[provideKey(resource.id, "smtpUrl")] = `smtp://${LOCAL_HOST}:${smtp}`;
877
+ }
878
+ if (resource.kind === "email" && url) {
879
+ env[provideKey(resource.id, "from")] = options.mailFrom ?? `noreply@${LOCAL_HOST}`;
880
+ const inbox = options.ports[MAILPIT_INBOX_PORT];
881
+ if (inbox !== void 0) env[provideKey(resource.id, "inboxUrl")] = `http://${LOCAL_HOST}:${inbox}`;
882
+ }
883
+ }
884
+ Object.assign(env, publicEnv(plan, env, options.addresses));
885
+ Object.assign(env, brokerEnv(plan, options.ports, credential));
886
+ const relay = env.EVENT_PUBLISHER_CONNECTION_STRING;
887
+ if (relay && options.relayedBuckets?.length) for (const resource of plan.resources) {
888
+ if (resource.kind !== "objects" || resource.container !== "minio") continue;
889
+ if (!options.relayedBuckets.includes(resource.id)) continue;
890
+ if (!env[resource.envKey]) continue;
891
+ env[provideKey(resource.id, "eventsConnectionString")] = relay;
892
+ env[provideKey(resource.id, "eventsToken")] = objectsWebhookToken(options.project ?? "", resource.id, credential.seed);
893
+ }
894
+ if (plan.resources.some((r) => r.kind === "inbox" && env[r.envKey])) env[INBOX_RELAY_TOKEN_KEY] = inboxRelayToken(options.project ?? "", credential.seed);
895
+ if (plan.resources.some((r) => r.kind === "objects" && env[r.envKey])) Object.assign(env, storageCredentials(options.credentials));
896
+ return env;
897
+ }
898
+ /**
899
+ * The values a site's bundler inlines, under the names it inlines them by.
900
+ *
901
+ * A rename and nothing more. `API_URL` was resolved once, by the same code that
902
+ * resolved it for the server; a site reads the same value under `VITE_API_URL`
903
+ * because that prefix is how its bundler is told to ship it. Nothing is derived
904
+ * twice, so a site and its API cannot come to disagree about where the API is.
905
+ *
906
+ * A source that resolved to nothing is skipped rather than written empty: an
907
+ * inlined empty string is a frontend that builds and then fails at runtime
908
+ * against `http:///`, where a missing variable fails at build with the name of
909
+ * the thing that is missing.
910
+ */
911
+ function publicEnv(plan, resolved, addresses = {}) {
912
+ const env = {};
913
+ const owners = new Map(plan.resources.map((r) => [r.envKey, r.id]));
914
+ for (const resource of plan.resources) for (const [key, source] of Object.entries(resource.publicEnv ?? {})) {
915
+ const value = (resource.kind === "mobile-app" ? addresses[owners.get(source) ?? ""] : void 0) ?? resolved[source];
916
+ if (value) env[key] = value;
917
+ }
918
+ return env;
919
+ }
920
+ /**
921
+ * What a surface publishes beyond its own address: who may call it, and where
922
+ * a cookie set by it is readable.
923
+ *
924
+ * Both are read off the same list — the constructs that declared an edge to
925
+ * this surface — which is why neither appears in application code. A surface
926
+ * that listed its own callers would be edited every time something new called
927
+ * it, and the thing being edited is already recorded in the graph.
928
+ *
929
+ * An empty origin list is written, and a surface with no address writes nothing
930
+ * at all. The two cases look alike and are not: "nothing depends on this yet" is
931
+ * a real state a target should publish, while "this surface has no address here"
932
+ * means it is not running in this stage, and keys belong with the address they
933
+ * describe.
934
+ */
935
+ function surfaceEnv(resource, url, resolved = {}, local = {}) {
936
+ if (!url) return {};
937
+ const origins = [...new Set((resource.callers ?? []).map((caller) => resolved[caller]).filter((address) => Boolean(address)).map(originOf).filter((origin) => Boolean(origin)))].sort();
938
+ const domain = cookieDomain([url, ...origins]);
939
+ const mobile = (resource.callers ?? []).map((caller) => local.plan?.resources.find((r) => r.id === caller && r.kind === "mobile-app")).filter((caller) => Boolean(caller));
940
+ const hosts = [...local.lanAddress ? [local.lanAddress] : [], LOCAL_HOST];
941
+ const schemes = mobile.flatMap((caller) => {
942
+ const scheme = resolved[caller.id];
943
+ const port = local.metroPorts?.[caller.id];
944
+ return scheme ? mobileOrigins(scheme, {
945
+ hosts,
946
+ ...port ? { port } : {}
947
+ }) : [];
948
+ });
949
+ const own = local.addresses?.[resource.id];
950
+ const device = mobile.length > 0 && local.lanAddress && own ? own.replace(/\/\/(localhost|127\.0\.0\.1)(?=[:/]|$)/, `//${local.lanAddress}`) : void 0;
951
+ return {
952
+ [provideKey(resource.id, "trustedOrigins")]: [...origins, ...new Set(schemes)].join(","),
953
+ ...domain ? { [provideKey(resource.id, "cookieDomain")]: domain } : {},
954
+ ...device ? { [provideKey(resource.id, "deviceUrl")]: device } : {}
955
+ };
956
+ }
957
+ /**
958
+ * The buckets whose links a phone opens: those a surface a mobile app calls
959
+ * depends on.
960
+ *
961
+ * The surface hands the app a URL it built — a file server's public link, a
962
+ * bucket's presigned one — and the phone cannot resolve `*.localhost`, or
963
+ * reach `localhost` at all. A presigned URL's signature covers its host, so
964
+ * nothing downstream can move it either: the bucket has to be resolved on the
965
+ * LAN address in the first place.
966
+ */
967
+ function deviceBuckets(plan) {
968
+ const byId = new Map(plan.resources.map((r) => [r.id, r]));
969
+ const fromApp = (id) => {
970
+ const caller = byId.get(id);
971
+ if (caller?.kind === "mobile-app") return true;
972
+ return (caller?.kind === "rest-api" || caller?.kind === "websocket-api") && (caller.callers ?? []).some((c) => byId.get(c)?.kind === "mobile-app");
973
+ };
974
+ const buckets = new Set(plan.resources.filter((r) => r.kind === "objects" || r.kind === "file-server").filter((r) => (r.callers ?? []).some(fromApp)).map((r) => r.id));
975
+ for (const r of plan.resources) if (r.kind === "file-server" && r.of && buckets.has(r.of)) buckets.add(r.id);
976
+ return buckets;
977
+ }
978
+ /**
979
+ * Where a WebSocket surface's publisher meets its sockets: the local Redis,
980
+ * on channels named for the project and the surface, so two projects sharing
981
+ * one Redis never see each other's frames.
982
+ */
983
+ function webSocketPublisher(resource, ports, project, credential) {
984
+ const port = ports[primaryPortKey("redis")];
985
+ if (port === void 0) return void 0;
986
+ const channel = encodeURIComponent(`gkm:ws:${project ? `${project}:` : ""}${resource.id}`);
987
+ return `redis://:${encodeURIComponent(credential.containers.redis.password)}@${LOCAL_HOST}:${port}?channel=${channel}`;
988
+ }
989
+ /**
990
+ * An address reduced to the origin a browser compares against.
991
+ *
992
+ * A WebSocket surface's address is `wss://`, but its host is served over
993
+ * HTTPS like any other: its origin is that one, and a non-web scheme among the
994
+ * origins means a mobile app to whoever reads the list.
995
+ */
996
+ function originOf(address) {
997
+ try {
998
+ return new URL(address.replace(/^ws(s?):/, "http$1:")).origin;
999
+ } catch {
1000
+ return;
1001
+ }
1002
+ }
1003
+ /**
1004
+ * The runtime role of whatever a derived node hangs off.
1005
+ *
1006
+ * A reader's parent may itself be a schema tenant, so this follows `of` by one
1007
+ * hop rather than assuming the cluster — the reader of a tenant reads that
1008
+ * tenant's schema, not the database's.
1009
+ */
1010
+ function parentRole(resource, plan) {
1011
+ const parent = plan.resources.find((r) => r.id === resource.of);
1012
+ return parent ? localRole(parent) : resource.name;
1013
+ }
1014
+ /** Whether a kind connects to Postgres and therefore has an owner role. */
1015
+ function isDatabase(kind) {
1016
+ return kind === "database" || kind === "database-schema";
1017
+ }
1018
+ /**
1019
+ * The URL a migrator connects with — the owner role, which can create, alter
1020
+ * and drop.
1021
+ *
1022
+ * Never in `provides`, which is the whole point. A handler that could reach this
1023
+ * could `DROP TABLE`, and the security property the role split exists for is
1024
+ * that it cannot: the value is injected by `gkm exec` for the migrate step and
1025
+ * by nothing else.
1026
+ */
1027
+ function ownerUrl(resource, plan, ports, project, credential) {
1028
+ if (!resource.container) return void 0;
1029
+ const port = ports[primaryPortKey(resource.container)];
1030
+ if (port === void 0) return void 0;
1031
+ const owner = ownerRole(localRole(resource));
1032
+ return postgres(port, rootDatabase(resource, plan), {
1033
+ user: owner,
1034
+ password: localRolePassword(project, plan, owner, credential.seed)
1035
+ });
1036
+ }
1037
+ /**
1038
+ * The shared broker keys, when anything is published or consumed.
1039
+ *
1040
+ * A queue's own key is the producer's. These two are the *connection* itself,
1041
+ * which locally is one broker for every queue and topic in the project: the
1042
+ * generated pollers open a single connection and subscribe each worker by name
1043
+ * on it. Deployed there is no such thing — a Lambda is handed its own event
1044
+ * source — so this pair exists for the local target and says so.
1045
+ */
1046
+ function brokerEnv(plan, ports, credential) {
1047
+ const carrier = plan.resources.find((r) => r.kind === "queue" || r.kind === "topic");
1048
+ const publisher = carrier ? urlFor(carrier, plan, ports, "", {}, credential) : plan.workerBroker ? workerBroker(plan, ports, credential) : void 0;
1049
+ if (!publisher) return {};
1050
+ if (plan.events === "sns") return {};
1051
+ return { EVENT_PUBLISHER_CONNECTION_STRING: publisher };
1052
+ }
1053
+ /**
1054
+ * The broker a worker schedules its crons through, when nothing else declared
1055
+ * one — pg-boss in the declared database, on that database's port.
1056
+ */
1057
+ function workerBroker(plan, ports, credential) {
1058
+ const database = plan.resources.find((r) => r.kind === "database");
1059
+ if (!database?.container) return void 0;
1060
+ const port = ports[primaryPortKey(database.container)];
1061
+ return port === void 0 ? void 0 : broker(plan, port, credential);
1062
+ }
1063
+ /**
1064
+ * The URL for one planned resource.
1065
+ *
1066
+ * Every part of it comes from the resource and the port it was published on.
1067
+ */
1068
+ function urlFor(resource, plan, ports, project, addresses, credential, lan) {
1069
+ if (resource.kind === "secret") return localSecret(project, plan, resource);
1070
+ if (resource.kind === "encryption") return localKeyring(project, plan, resource);
1071
+ if (resource.kind === "mobile-app") return schemeBase(project, resource.scheme);
1072
+ if (resource.kind === "credential") return void 0;
1073
+ if (resource.kind === "external-api") {
1074
+ if (!resource.fake) return resource.url === void 0 ? void 0 : externalApiUrl({
1075
+ id: resource.id,
1076
+ url: resource.url
1077
+ }, plan.stage);
1078
+ const port = ports[resource.fake.key];
1079
+ return port === void 0 ? void 0 : `http://${LOCAL_HOST}:${port}`;
1080
+ }
1081
+ if (resource.kind === "rest-api" || resource.kind === "site") {
1082
+ const address = addresses?.[resource.id];
1083
+ const edge = plan.containers.includes("caddy") ? ports[primaryPortKey("caddy")] : void 0;
1084
+ return address && edge !== void 0 ? `https://${hostFor$1(resource, project)}:${edge}` : address;
1085
+ }
1086
+ if (resource.kind === "websocket-api") {
1087
+ const address = addresses?.[resource.id];
1088
+ const edge = plan.containers.includes("caddy") ? ports[primaryPortKey("caddy")] : void 0;
1089
+ return address && edge !== void 0 ? `wss://${hostFor$1(resource, project)}:${edge}` : address?.replace(/^http/, "ws");
1090
+ }
1091
+ if (!resource.container) return void 0;
1092
+ const port = ports[primaryPortKey(resource.container)];
1093
+ if (port === void 0) return void 0;
1094
+ switch (resource.kind) {
1095
+ case "database":
1096
+ case "database-schema":
1097
+ case "database-reader": {
1098
+ const database = rootDatabase(resource, plan);
1099
+ if (resource.roles === false) {
1100
+ const schema = schemaOf(resource, plan);
1101
+ const master = credential.containers.postgres;
1102
+ return schema ? `${postgres(port, database, master)}?search_path=${schema}` : postgres(port, database, master);
1103
+ }
1104
+ const role = resource.kind === "database-reader" ? readerRole(parentRole(resource, plan)) : localRole(resource);
1105
+ return postgres(port, database, {
1106
+ user: role,
1107
+ password: localRolePassword(project, plan, role, credential.seed)
1108
+ });
1109
+ }
1110
+ case "telemetry": return `http://${LOCAL_HOST}:${port}/api/${LOGS_ORG}`;
1111
+ case "email": return `smtp://${LOCAL_HOST}:${port}`;
1112
+ case "inbox": {
1113
+ const api = ports[MAILPIT_INBOX_PORT];
1114
+ return api === void 0 ? void 0 : `http://${LOCAL_HOST}:${api}`;
1115
+ }
1116
+ case "objects": return `s3://${resource.name}?region=${LOCAL_REGION}&endpoint=http://${lan ?? LOCAL_HOST}:${port}&forcePathStyle=true`;
1117
+ case "file-server": {
1118
+ const origin = plan.resources.find((r) => r.id === resource.of);
1119
+ if (!origin) return void 0;
1120
+ const placed = addresses?.[resource.id];
1121
+ if (placed) return placed;
1122
+ const objects = origin.container ? ports[primaryPortKey(origin.container)] : void 0;
1123
+ if (lan && !resource.processors?.length && objects !== void 0) return `http://${lan}:${objects}/${origin.name}`;
1124
+ return plan.containers.includes("caddy") ? `https://${hostFor$1(resource, project)}:${port}` : `http://${LOCAL_HOST}:${port}/${origin.name}`;
1125
+ }
1126
+ case "cache":
1127
+ if (resource.of) {
1128
+ const parent = plan.resources.find((r) => r.id === resource.of);
1129
+ const url = parent ? urlFor(parent, plan, ports, project, addresses, credential) : void 0;
1130
+ return url ? withTable(url, resource.table ?? cacheTable(resource.id)) : void 0;
1131
+ }
1132
+ switch (plan.cache) {
1133
+ case "elasticache":
1134
+ case "redis": return `redis://:${encodeURIComponent(credential.containers.redis.password)}@${LOCAL_HOST}:${port}`;
1135
+ default: return `http://:${encodeURIComponent(credential.containers.cacheToken)}@${LOCAL_HOST}:${port}`;
1136
+ }
1137
+ case "queue":
1138
+ case "topic": return broker(plan, port, credential, resource);
1139
+ default: return;
1140
+ }
1141
+ }
1142
+ /**
1143
+ * The producer's connection string, composed from the backend the plan chose.
1144
+ *
1145
+ * The protocol is what picks the transport, so a producer never branches: the
1146
+ * same `.publish()` reaches pg-boss here and SQS deployed because the string it
1147
+ * was handed said so.
1148
+ */
1149
+ function broker(plan, port, credential, resource) {
1150
+ switch (plan.events) {
1151
+ case "pgboss": {
1152
+ const database = plan.resources.find((r) => r.kind === "database");
1153
+ if (!database) throw new PgBossNeedsDatabase([]);
1154
+ const { user, password } = credential.containers.postgres;
1155
+ return `pgboss://${user}:${encodeURIComponent(password)}@${LOCAL_HOST}:${port}/${database.name}?schema=${PGBOSS_SCHEMA}`;
1156
+ }
1157
+ case "rabbitmq": {
1158
+ const { user, password } = credential.containers.rabbitmq;
1159
+ return `rabbitmq://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${LOCAL_HOST}:${port}?exchange=${RABBITMQ_EXCHANGE}`;
1160
+ }
1161
+ case "sns": {
1162
+ if (!resource) throw new UnprovisionedEventsBackend("sns");
1163
+ const endpoint = emulatorEndpoint(port);
1164
+ const address = resource.kind === "topic" ? snsUrl.build({
1165
+ topicArn: emulatorTopicArn(resource.name),
1166
+ region: EMULATOR_REGION,
1167
+ endpoint
1168
+ }) : sqsUrl.build({
1169
+ queueUrl: emulatorQueueUrl(resource.name, port),
1170
+ region: EMULATOR_REGION,
1171
+ endpoint
1172
+ });
1173
+ const url = new URL(address);
1174
+ const { accessKeyId, secretAccessKey } = credential.containers.emulator;
1175
+ url.searchParams.set("accessKeyId", accessKeyId);
1176
+ url.searchParams.set("secretAccessKey", secretAccessKey);
1177
+ return url.toString();
1178
+ }
1179
+ }
1180
+ }
1181
+ /** A local Postgres URL, with the login it connects as. */
1182
+ function postgres(port, database, credential) {
1183
+ return `postgres://${credential.user}:${encodeURIComponent(credential.password)}@${LOCAL_HOST}:${port}/${database}`;
1184
+ }
1185
+ /**
1186
+ * The role a handler connects as, for one database or tenant.
1187
+ *
1188
+ * The construct's id, lowercased and stage-scoped exactly as its database or
1189
+ * schema is — so `Orders` in the `test` stage connects as `orders_test`, and two
1190
+ * stages sharing one cluster cannot share a credential. Roles are cluster-scoped
1191
+ * in Postgres, which is why the suffix is not optional here the way it is for a
1192
+ * schema.
1193
+ */
1194
+ function localRole(resource) {
1195
+ return resource.name;
1196
+ }
1197
+ /**
1198
+ * The password for a derived local role.
1199
+ *
1200
+ * Derived rather than random, for the same reason the local secret is: a
1201
+ * password that changed on every `gkm dev` would lock a developer out of the
1202
+ * data they had a moment ago. Seeded by the project so two checkouts do not
1203
+ * share one, and by the stage so `test` and `development` do not.
1204
+ *
1205
+ * Without a seed it is a *local* credential by construction — the cluster is
1206
+ * on loopback and everything it is derived from is in the repo. `gkm compose`
1207
+ * serving a deployed stage passes that stage's random seed, which is what
1208
+ * makes the same derivation a secret.
1209
+ */
1210
+ function localRolePassword(project, plan, role, seed) {
1211
+ return createHash("sha256").update(`${seed ? `${seed}:` : ""}${project}:${plan.stage}:role:${role}`).digest("base64url").slice(0, 32);
1212
+ }
1213
+ /**
1214
+ * A cache's address: its database's URL, carrying the table it reads.
1215
+ *
1216
+ * Appended rather than kept beside the URL as a second key, for the reason the
1217
+ * design gives everywhere else — an address and what it opens are one fact, and
1218
+ * two keys are one more thing to keep in step.
1219
+ */
1220
+ function withTable(url, table) {
1221
+ const parsed = new URL(url);
1222
+ parsed.searchParams.set("table", table);
1223
+ return parsed.toString();
1224
+ }
1225
+ /**
1226
+ * The physical database a derived resource lives in.
1227
+ *
1228
+ * Walks `of` to the top rather than reading the immediate parent, because a
1229
+ * reader's parent may itself be a schema tenant — two hops from the database
1230
+ * that actually exists.
1231
+ */
1232
+ function rootDatabase(resource, plan) {
1233
+ const byId = new Map(plan.resources.map((r) => [r.id, r]));
1234
+ let current = resource;
1235
+ while (current.of) {
1236
+ const parent = byId.get(current.of);
1237
+ if (!parent) break;
1238
+ current = parent;
1239
+ }
1240
+ return current.name;
1241
+ }
1242
+ /** The schema a derived resource puts on its search path, if it has one. */
1243
+ function schemaOf(resource, plan) {
1244
+ const byId = new Map(plan.resources.map((r) => [r.id, r]));
1245
+ let current = resource;
1246
+ while (current) {
1247
+ if (current.kind === "database-schema") return current.schema;
1248
+ current = current.of ? byId.get(current.of) : void 0;
1249
+ }
1250
+ }
1251
+ /**
1252
+ * A broker address asked for without the topic or queue it would name.
1253
+ *
1254
+ * On SNS each topic and queue is its own address, so there is no project-wide
1255
+ * one to hand out — a cron's schedule store, say, cannot live there.
1256
+ */
1257
+ var UnprovisionedEventsBackend = class extends GkmError {
1258
+ backend;
1259
+ constructor(backend) {
1260
+ super(`'${backend}' has no single broker address: each topic and queue is addressed by its own ARN. Something asked for one without naming the topic or queue.`);
1261
+ this.backend = backend;
1262
+ this.name = "UnprovisionedEventsBackend";
1263
+ }
1264
+ };
1265
+ /**
1266
+ * The keyring an `Encryption` construct resolves to locally.
1267
+ *
1268
+ * Derived like a secret — stable across restarts, distinct per project, stage
1269
+ * and construct, never written to disk — so what `gkm dev` encrypted yesterday
1270
+ * still opens today. One key, because nothing local is worth rotating: a
1271
+ * server stage's keyring is generated and rotated in its secrets instead.
1272
+ */
1273
+ function localKeyring(project, plan, resource) {
1274
+ const derive = (purpose) => createHash("sha256").update(`${project}:${plan.stage}:${resource.id}:${purpose}`).digest();
1275
+ return formatKeyring({
1276
+ keys: [{
1277
+ id: "k1",
1278
+ key: derive("k1")
1279
+ }],
1280
+ index: derive("index")
1281
+ });
1282
+ }
1283
+ /**
1284
+ * The value a secret resolves to locally.
1285
+ *
1286
+ * A hash of what identifies it, so it is stable, distinct per project, stage
1287
+ * and construct, and never written to disk as a literal. Deployed this is the
1288
+ * one kind whose value does *not* come from here — a secret manager generates
1289
+ * it once and the target reads it back. Drawn in the format the deploy draws
1290
+ * it in, so a value that passes a third party's rules here passes them there.
1291
+ */
1292
+ function localSecret(project, plan, resource) {
1293
+ return drawSecret(hashStream(`${project}:${plan.stage}:${resource.id}`), resource.format);
1294
+ }
1295
+ /**
1296
+ * Integers in `[0, n)` drawn from a hash of `seed`, the same every time —
1297
+ * what a local secret is drawn with instead of `crypto`, so `gkm dev` and
1298
+ * `gkm test` need nothing stored. Rejection-sampled, so no character is
1299
+ * likelier than another.
1300
+ */
1301
+ function hashStream(seed) {
1302
+ let block = 0;
1303
+ let bytes = Buffer.alloc(0);
1304
+ let offset = 0;
1305
+ const next = () => {
1306
+ if (offset === bytes.length) {
1307
+ bytes = createHash("sha256").update(`${seed}:${block++}`).digest();
1308
+ offset = 0;
1309
+ }
1310
+ return bytes[offset++];
1311
+ };
1312
+ return (n) => {
1313
+ const limit = 256 - 256 % n;
1314
+ for (;;) {
1315
+ const byte = next();
1316
+ if (byte < limit) return byte % n;
1317
+ }
1318
+ };
1319
+ }
1320
+ //#endregion
1321
+ //#region src/providers/s3/lifecycle.ts
1322
+ /**
1323
+ * A bucket's lifecycle, as the constructs declare it: one expiry rule per
1324
+ * prefix, merged into whatever else the bucket's lifecycle already holds.
1325
+ *
1326
+ * S3 — and MinIO, which speaks the same API — keeps one lifecycle document
1327
+ * per bucket, and a write replaces all of it. So gkm owns only the rules it
1328
+ * names (`gkm-…`): every other rule is read and put back as it was, a rule of
1329
+ * gkm's that is no longer declared is dropped, and nothing is written when
1330
+ * gkm's rules already say what the manifest does.
1331
+ *
1332
+ * Shared by the `s3` provider and the MinIO a local or compose stack runs,
1333
+ * so a prefix expires the same way on both.
1334
+ */
1335
+ /** The id every rule gkm writes starts with — and the only ones it touches. */
1336
+ const GKM_RULE_PREFIX = "gkm-";
1337
+ /**
1338
+ * The rule id for one prefix: readable, and unique however the prefix is
1339
+ * spelled — `a/b` and `a-b` read alike once sanitised, so a short hash of the
1340
+ * prefix itself follows.
1341
+ */
1342
+ function expireRuleId(prefix) {
1343
+ const readable = prefix.replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 200);
1344
+ const hash = createHash("sha256").update(prefix).digest("hex").slice(0, 8);
1345
+ return `${GKM_RULE_PREFIX}expire-${readable ? `${readable}-` : ""}${hash}`;
1346
+ }
1347
+ /** The rules a bucket's expiry asks for, in a stable order. */
1348
+ function expireRules(expire) {
1349
+ return Object.entries(expire ?? {}).map(([prefix, days]) => ({
1350
+ ID: expireRuleId(prefix),
1351
+ Status: "Enabled",
1352
+ Filter: { Prefix: prefix },
1353
+ Expiration: { Days: days }
1354
+ })).sort((a, b) => a.ID.localeCompare(b.ID));
1355
+ }
1356
+ /**
1357
+ * Merge the declared expiry into the bucket's current rules.
1358
+ *
1359
+ * @param current the bucket's rules, or nothing when it has no lifecycle
1360
+ */
1361
+ function mergeLifecycle(current, expire) {
1362
+ const wanted = expireRules(expire);
1363
+ const rules = current ?? [];
1364
+ const others = rules.filter((r) => !r.ID?.startsWith(GKM_RULE_PREFIX));
1365
+ const ours = rules.filter((r) => r.ID?.startsWith(GKM_RULE_PREFIX));
1366
+ return {
1367
+ changed: canonical(ours) !== canonical(wanted) || new Set(ours.map((r) => r.ID)).size !== ours.length,
1368
+ rules: [...others, ...wanted],
1369
+ describe: describeExpiry(expire)
1370
+ };
1371
+ }
1372
+ /** `expire quotes/ after 30 days, tmp/ after 1 day` — or what removing says. */
1373
+ function describeExpiry(expire) {
1374
+ const entries = Object.entries(expire ?? {}).sort(([a], [b]) => a.localeCompare(b));
1375
+ if (entries.length === 0) return "remove expiry rules";
1376
+ return `expire ${entries.map(([prefix, days]) => `${prefix} after ${days} day${days === 1 ? "" : "s"}`).join(", ")}`;
1377
+ }
1378
+ /** gkm's rules as the fields it sets, order-free. */
1379
+ function canonical(rules) {
1380
+ return JSON.stringify(rules.map((r) => ({
1381
+ id: r.ID,
1382
+ status: r.Status,
1383
+ prefix: r.Filter?.Prefix ?? r.Prefix ?? "",
1384
+ days: r.Expiration?.Days ?? null
1385
+ })).sort((a, b) => (a.id ?? "").localeCompare(b.id ?? "")));
1386
+ }
1387
+ //#endregion
1388
+ //#region src/reconcile/provision.ts
1389
+ /**
1390
+ * The Postgres work a plan implies.
1391
+ *
1392
+ * Databases before the schemas inside them, which `provisionOrder` already
1393
+ * guarantees by putting parents first.
1394
+ */
1395
+ function postgresStatements(plan, project = "", seed) {
1396
+ const statements = [];
1397
+ const cacheTables = [];
1398
+ for (const resource of plan.resources) {
1399
+ if (!resource.provisions || resource.container !== "postgres") continue;
1400
+ if (resource.kind === "database") {
1401
+ statements.push({
1402
+ id: resource.id,
1403
+ describe: `database ${resource.name}`,
1404
+ exists: {
1405
+ sql: "SELECT 1 FROM pg_database WHERE datname = $1",
1406
+ values: [resource.name]
1407
+ },
1408
+ create: `CREATE DATABASE ${quoteIdentifier(resource.name)}`
1409
+ });
1410
+ if (resource.schema) statements.push(...rolesFor(resource, resource.schema, plan, project, seed));
1411
+ continue;
1412
+ }
1413
+ if (resource.kind === "database-schema" && resource.schema) statements.push(...rolesFor(resource, resource.schema, plan, project, seed));
1414
+ const cacheHome = resource.kind === "cache" && resource.of ? plan.resources.find((r) => r.id === resource.of) : void 0;
1415
+ if (cacheHome) cacheTables.push(...cacheTableDdl(resource, cacheHome, plan));
1416
+ }
1417
+ return [...statements, ...cacheTables];
1418
+ }
1419
+ /**
1420
+ * A cache's table, in the schema the role that reads it resolves names in.
1421
+ *
1422
+ * Two things have to line up, and neither is the table's contents. The driver
1423
+ * names the table unqualified and lets the connection's `search_path` place it
1424
+ * — which the role carries — so creating it from the master connection, whose
1425
+ * path is `public`, leaves a table the application cannot see and a
1426
+ * `relation "cache" does not exist` on the first request. And the default
1427
+ * privileges that make a tenant's tables reachable are granted *for the owner
1428
+ * role*, so nothing covers a table the master created.
1429
+ *
1430
+ * Hence: qualified with the tenant's schema, handed to its owner, and granted
1431
+ * explicitly — `roleStatements` grants what exists at the time it runs, and
1432
+ * this table does not exist yet.
1433
+ *
1434
+ * A database that opted out of roles needs none of it: everything connects as
1435
+ * the master, whose `search_path` finds `public`, and the table it creates
1436
+ * there is the table it reads.
1437
+ */
1438
+ function cacheTableDdl(cache, home, plan) {
1439
+ const database = rootDatabase(home, plan);
1440
+ const schema = home.roles === false ? void 0 : home.schema;
1441
+ const name = cache.table ?? cacheTable(cache.id);
1442
+ const table = schema ? `${schema}.${name}` : name;
1443
+ const created = cacheTableStatements({ table }).map((statement) => ({
1444
+ id: cache.id,
1445
+ describe: statement.describe,
1446
+ database,
1447
+ ...statement.exists ? { exists: statement.exists } : {},
1448
+ create: statement.sql
1449
+ }));
1450
+ if (!schema) return created;
1451
+ const runtime = localRole(home);
1452
+ const owner = ownerRole(runtime);
1453
+ const qualified = table.split(".").map((part) => quoteIdentifier(part)).join(".");
1454
+ return [
1455
+ ...created,
1456
+ {
1457
+ id: cache.id,
1458
+ describe: `cache table ${table} is owned by ${owner}`,
1459
+ database,
1460
+ create: `ALTER TABLE ${qualified} OWNER TO ${quoteIdentifier(owner)}`
1461
+ },
1462
+ {
1463
+ id: cache.id,
1464
+ describe: `${runtime} may read and write ${table}`,
1465
+ database,
1466
+ create: `GRANT SELECT, INSERT, UPDATE, DELETE ON ${qualified} TO ${quoteIdentifier(runtime)}`
1467
+ }
1468
+ ];
1469
+ }
1470
+ /**
1471
+ * The roles and schema for one database or tenant.
1472
+ *
1473
+ * Skipped entirely when the construct declared `roles: false` — the documented
1474
+ * downgrade, where both URLs fall back to the cluster's master credential. It
1475
+ * is a choice someone made rather than a default they got.
1476
+ */
1477
+ function rolesFor(resource, schema, plan, project, seed) {
1478
+ if (resource.roles === false) return [];
1479
+ const runtime = localRole(resource);
1480
+ const owner = ownerRole(runtime);
1481
+ const reader = plan.resources.some((r) => r.kind === "database-reader" && r.of === resource.id) ? readerRole(runtime) : void 0;
1482
+ return roleStatements({
1483
+ runtime,
1484
+ owner,
1485
+ ...reader ? { reader } : {},
1486
+ schema,
1487
+ ...resource.kind === "database" ? { database: rootDatabase(resource, plan) } : {},
1488
+ ...resource.extensions ? { extensions: resource.extensions } : {},
1489
+ passwords: {
1490
+ runtime: localRolePassword(project, plan, runtime, seed),
1491
+ owner: localRolePassword(project, plan, owner, seed),
1492
+ ...reader ? { reader: localRolePassword(project, plan, reader, seed) } : {}
1493
+ }
1494
+ }).map((statement) => ({
1495
+ id: resource.id,
1496
+ describe: statement.describe,
1497
+ database: rootDatabase(resource, plan),
1498
+ ...statement.exists ? { exists: statement.exists } : {},
1499
+ create: statement.sql
1500
+ }));
1501
+ }
1502
+ /** The buckets a plan implies. */
1503
+ function bucketNames(plan) {
1504
+ return plan.resources.filter((r) => r.provisions && r.kind === "objects").map((r) => r.name);
1505
+ }
1506
+ /**
1507
+ * Quote an identifier for use in DDL.
1508
+ *
1509
+ * Names reaching here are already canonical and stage-suffixed, so this is
1510
+ * defence rather than sanitisation — but DDL cannot be parameterised, and an
1511
+ * unquoted identifier is the one place a name could ever be more than a name.
1512
+ */
1513
+ function quoteIdentifier(name) {
1514
+ return `"${name.replace(/"/g, "\"\"")}"`;
1515
+ }
1516
+ /**
1517
+ * Apply the plan's Postgres statements.
1518
+ *
1519
+ * Each is checked before it is run, so this converges rather than erroring on a
1520
+ * second pass.
1521
+ */
1522
+ async function applyPostgres(client, statements) {
1523
+ const applied = [];
1524
+ for (const statement of statements) {
1525
+ if (!statement.exists) {
1526
+ await client.query(statement.database, statement.create);
1527
+ applied.push({
1528
+ id: statement.id,
1529
+ describe: statement.describe,
1530
+ created: false
1531
+ });
1532
+ continue;
1533
+ }
1534
+ if ((await client.query(statement.database, statement.exists.sql, statement.exists.values)).length > 0) {
1535
+ applied.push({
1536
+ id: statement.id,
1537
+ describe: statement.describe,
1538
+ created: false
1539
+ });
1540
+ continue;
1541
+ }
1542
+ await client.query(statement.database, statement.create);
1543
+ applied.push({
1544
+ id: statement.id,
1545
+ describe: statement.describe,
1546
+ created: true
1547
+ });
1548
+ }
1549
+ return applied;
1550
+ }
1551
+ /**
1552
+ * What each bucket's open paths are, if anything serves it.
1553
+ *
1554
+ * Keyed by the *origin*, because that is where the policy lives: two servers
1555
+ * over one bucket contribute to one policy, so the patterns are unioned rather
1556
+ * than the last one winning.
1557
+ */
1558
+ function bucketPolicies(plan) {
1559
+ const byBucket = /* @__PURE__ */ new Map();
1560
+ for (const resource of plan.resources) {
1561
+ if (resource.kind !== "file-server" || !resource.open?.length) continue;
1562
+ const origin = plan.resources.find((r) => r.id === resource.of);
1563
+ if (!origin) continue;
1564
+ const patterns = byBucket.get(origin.name) ?? /* @__PURE__ */ new Set();
1565
+ for (const pattern of resource.open) patterns.add(pattern);
1566
+ byBucket.set(origin.name, patterns);
1567
+ }
1568
+ return [...byBucket].map(([bucket, open]) => ({
1569
+ bucket,
1570
+ open: [...open].sort()
1571
+ })).sort((a, b) => a.bucket.localeCompare(b.bucket));
1572
+ }
1573
+ /**
1574
+ * What each bucket keeps, and for how long: the buckets that declare an
1575
+ * expiry, by their stage-scoped name.
1576
+ *
1577
+ * Every bucket in the plan, not only those that declare one, is what a
1578
+ * removed expiry needs — its rules are dropped from a bucket that no longer
1579
+ * asks for them — so a bucket with none is listed with an empty expiry.
1580
+ */
1581
+ function bucketLifecycles(plan) {
1582
+ return plan.resources.filter((r) => r.kind === "objects" && r.provisions).map((r) => ({
1583
+ bucket: r.name,
1584
+ expire: r.expire ?? {}
1585
+ })).sort((a, b) => a.bucket.localeCompare(b.bucket));
1586
+ }
1587
+ /**
1588
+ * One bucket's open paths as an S3 bucket policy.
1589
+ *
1590
+ * Anonymous `s3:GetObject` on the named prefixes and nothing else — which is
1591
+ * what "open" means everywhere: the server serves that path without a
1592
+ * signature, and the bucket stays private for everything else.
1593
+ *
1594
+ * One fidelity note, stated rather than discovered: an S3 policy resource's `*`
1595
+ * crosses `/`, so a single-star pattern is *wider* here than the construct's own
1596
+ * runtime check, which stops at a segment boundary. The construct is the
1597
+ * stricter of the two, so a key it refuses to serve unsigned is never one this
1598
+ * policy was relied on to refuse — but a key fetched directly, bypassing the
1599
+ * client, can be admitted by the policy where the client would have said no.
1600
+ * Prefer `**` where crossing segments is what you meant.
1601
+ */
1602
+ function bucketPolicy(bucket, open) {
1603
+ return JSON.stringify({
1604
+ Version: "2012-10-17",
1605
+ Statement: [{
1606
+ Sid: "GkmOpenPaths",
1607
+ Effect: "Allow",
1608
+ Principal: { AWS: ["*"] },
1609
+ Action: ["s3:GetObject"],
1610
+ Resource: open.map((pattern) => `arn:aws:s3:::${bucket}/${pattern.replace(/\*\*/g, "*")}`)
1611
+ }]
1612
+ });
1613
+ }
1614
+ /**
1615
+ * The topics and queues to create on the AWS emulator.
1616
+ *
1617
+ * Only on the `sns` backend: pg-boss makes its own tables on first connect and
1618
+ * a RabbitMQ exchange is declared by the client, but an SNS topic and an SQS
1619
+ * queue must exist before anything can publish to them.
1620
+ */
1621
+ function carrierNames(plan) {
1622
+ if (plan.events !== "sns") return {
1623
+ topics: [],
1624
+ queues: []
1625
+ };
1626
+ const named = (kind) => plan.resources.filter((r) => r.kind === kind).map((r) => r.name);
1627
+ return {
1628
+ topics: named("topic"),
1629
+ queues: named("queue")
1630
+ };
1631
+ }
1632
+ /** Create each topic and queue the plan names, skipping those that exist. */
1633
+ async function applyCarriers(client, carriers) {
1634
+ const applied = [];
1635
+ for (const name of carriers.topics) {
1636
+ const created = !await client.topicExists(name);
1637
+ if (created) await client.createTopic(name);
1638
+ applied.push({
1639
+ id: name,
1640
+ describe: `topic ${name}`,
1641
+ created
1642
+ });
1643
+ }
1644
+ for (const name of carriers.queues) {
1645
+ const created = !await client.queueExists(name);
1646
+ if (created) await client.createQueue(name);
1647
+ applied.push({
1648
+ id: name,
1649
+ describe: `queue ${name}`,
1650
+ created
1651
+ });
1652
+ }
1653
+ return applied;
1654
+ }
1655
+ /** The Postgres databases the plan creates — each is a `CREATE DATABASE`. */
1656
+ function postgresDatabaseNames(plan) {
1657
+ return plan.resources.filter((resource) => resource.kind === "database" && resource.provisions && resource.container === "postgres").map((resource) => resource.name);
1658
+ }
1659
+ /**
1660
+ * Whether every one of these databases exists. A database dropped behind the
1661
+ * recorded state's back — by another checkout's test teardown, against the
1662
+ * same container — makes the state a lie, and only asking Postgres finds out.
1663
+ * Unreachable counts as missing: applying again is what finds out why.
1664
+ */
1665
+ async function databasesExist(client, names) {
1666
+ if (names.length === 0) return true;
1667
+ try {
1668
+ const rows = await client.query(void 0, "SELECT datname FROM pg_catalog.pg_database WHERE datname = ANY($1)", [names]);
1669
+ return new Set(rows.map((row) => row.datname)).size === names.length;
1670
+ } catch {
1671
+ return false;
1672
+ }
1673
+ }
1674
+ /**
1675
+ * Point each watched bucket's events at its webhook target, and stop sending
1676
+ * an unwatched one's.
1677
+ *
1678
+ * Merged rather than written whole: only the rule gkm owns on a bucket
1679
+ * (`gkm-<Bucket>`) is added or removed, and every other rule — one somebody
1680
+ * added by hand — is kept. Compared before writing, so a converged reconcile
1681
+ * changes nothing.
1682
+ *
1683
+ * @param buckets - each bucket the plan names, by construct id and name
1684
+ * @param watched - the construct ids whose events are relayed
1685
+ */
1686
+ async function applyNotifications(client, buckets, watched) {
1687
+ const applied = [];
1688
+ for (const { id, name } of buckets) {
1689
+ const current = await client.notification(name);
1690
+ const queues = current.QueueConfigurations ?? [];
1691
+ const others = queues.filter((rule) => !rule.Id?.startsWith(NOTIFICATION_RULE_PREFIX));
1692
+ const wanted = watched.has(id) ? [...others, minioNotificationRule(id)] : others;
1693
+ if (!watched.has(id) && others.length === queues.length) continue;
1694
+ const describe = `object events from ${name}`;
1695
+ const ours = queues.filter((rule) => rule.Id?.startsWith(NOTIFICATION_RULE_PREFIX));
1696
+ if (watched.has(id) ? ours.length === 1 && sameRule(ours[0], minioNotificationRule(id)) : ours.length === 0) {
1697
+ applied.push({
1698
+ id: `${name}:events`,
1699
+ describe,
1700
+ created: false
1701
+ });
1702
+ continue;
1703
+ }
1704
+ await client.setNotification(name, {
1705
+ ...current,
1706
+ QueueConfigurations: wanted
1707
+ });
1708
+ applied.push({
1709
+ id: `${name}:events`,
1710
+ describe,
1711
+ created: true
1712
+ });
1713
+ }
1714
+ return applied;
1715
+ }
1716
+ /**
1717
+ * Whether a rule read back says what gkm wrote: the same target, the same
1718
+ * events in any order, and no filter — the store may hand back an empty one.
1719
+ */
1720
+ function sameRule(current, wanted) {
1721
+ const filter = current.Filter;
1722
+ return current.Id === wanted.Id && current.QueueArn === wanted.QueueArn && JSON.stringify([...current.Events ?? []].sort()) === JSON.stringify([...wanted.Events ?? []].sort()) && (filter?.Key?.FilterRules?.length ?? 0) === 0;
1723
+ }
1724
+ /**
1725
+ * Apply each bucket's expiry, beside whatever other rules it has, skipping
1726
+ * those already correct — the same merge the `s3` provider makes deployed.
1727
+ */
1728
+ async function applyLifecycles(client, lifecycles) {
1729
+ const applied = [];
1730
+ for (const { bucket, expire } of lifecycles) {
1731
+ const merge = mergeLifecycle(await client.lifecycle(bucket), expire);
1732
+ const id = `${bucket}:lifecycle`;
1733
+ const describe = `${merge.describe} on ${bucket}`;
1734
+ if (!merge.changed && Object.keys(expire).length === 0) continue;
1735
+ if (!merge.changed) {
1736
+ applied.push({
1737
+ id,
1738
+ describe,
1739
+ created: false
1740
+ });
1741
+ continue;
1742
+ }
1743
+ await client.setLifecycle(bucket, merge.rules);
1744
+ applied.push({
1745
+ id,
1746
+ describe,
1747
+ created: true
1748
+ });
1749
+ }
1750
+ return applied;
1751
+ }
1752
+ /**
1753
+ * Apply each served bucket's open-path policy, skipping those already correct.
1754
+ *
1755
+ * Compared as parsed JSON rather than as text: the same policy re-serialised
1756
+ * with different key order is the same policy, and rewriting it every reconcile
1757
+ * would report a change on every start.
1758
+ */
1759
+ async function applyPolicies(client, policies) {
1760
+ const applied = [];
1761
+ for (const { bucket, open } of policies) {
1762
+ const wanted = bucketPolicy(bucket, open);
1763
+ const describe = `open paths on ${bucket}`;
1764
+ if (equivalent(await client.policy(bucket), wanted)) {
1765
+ applied.push({
1766
+ id: `${bucket}:policy`,
1767
+ describe,
1768
+ created: false
1769
+ });
1770
+ continue;
1771
+ }
1772
+ await client.setPolicy(bucket, wanted);
1773
+ applied.push({
1774
+ id: `${bucket}:policy`,
1775
+ describe,
1776
+ created: true
1777
+ });
1778
+ }
1779
+ return applied;
1780
+ }
1781
+ /** Whether two policy documents say the same thing. */
1782
+ function equivalent(current, wanted) {
1783
+ if (!current) return false;
1784
+ try {
1785
+ return JSON.stringify(JSON.parse(current)) === JSON.stringify(JSON.parse(wanted));
1786
+ } catch {
1787
+ return false;
1788
+ }
1789
+ }
1790
+ /** Create every bucket the plan names, skipping those that exist. */
1791
+ async function applyBuckets(client, buckets) {
1792
+ const applied = [];
1793
+ for (const bucket of buckets) {
1794
+ if (await client.exists(bucket)) {
1795
+ applied.push({
1796
+ id: bucket,
1797
+ describe: `bucket ${bucket}`,
1798
+ created: false
1799
+ });
1800
+ continue;
1801
+ }
1802
+ await client.create(bucket);
1803
+ applied.push({
1804
+ id: bucket,
1805
+ describe: `bucket ${bucket}`,
1806
+ created: true
1807
+ });
1808
+ }
1809
+ return applied;
1810
+ }
1811
+ //#endregion
1812
+ //#region src/build/workers.ts
1813
+ /** A worker with background work and no backend app to build it from. */
1814
+ var WorkerHasNoApp = class extends GkmError {
1815
+ worker;
1816
+ constructor(worker) {
1817
+ super(`The worker '${worker}' has crons, queues or subscribers, and the workspace has no backend app that builds a server to carry its entry. Declare a RestApi whose app holds the worker's code.`);
1818
+ this.worker = worker;
1819
+ this.name = "WorkerHasNoApp";
1820
+ }
1821
+ };
1822
+ /** A worker whose service name an app already has. */
1823
+ var WorkerNameTaken = class extends GkmError {
1824
+ worker;
1825
+ service;
1826
+ constructor(worker, service) {
1827
+ super(`The worker '${worker}' runs as the service '${service}', and an app is already called that. Rename the worker — new Worker('${worker}Worker') — so each container has a name of its own.`);
1828
+ this.worker = worker;
1829
+ this.service = service;
1830
+ this.name = "WorkerNameTaken";
1831
+ }
1832
+ };
1833
+ /**
1834
+ * The workers a workspace deploys — each one that has background work — and
1835
+ * the app each is built from.
1836
+ *
1837
+ * The host is the backend app whose directory holds the most of the worker's
1838
+ * files; one whose work sits outside every app (a shared `jobs/` directory)
1839
+ * is built by the first backend, by name, that generates a server. An app
1840
+ * with its own entry is never one — gkm's generators do not build it — and a
1841
+ * surface that serves itself, an auth server, only when it holds the work.
1842
+ *
1843
+ * @param background Each worker's files, from discovery's `background`.
1844
+ */
1845
+ function workerUnits(workspace, manifest, background) {
1846
+ const hosts = hostApps(workspace, manifest);
1847
+ const units = [];
1848
+ for (const [id, declaration] of Object.entries(manifest)) {
1849
+ if (declaration.kind !== "worker") continue;
1850
+ const files = background[id] ?? [];
1851
+ if (files.length === 0) continue;
1852
+ const name = appKey(id);
1853
+ if (workspace.apps[name]) throw new WorkerNameTaken(id, name);
1854
+ const host = hostFor(hosts, files);
1855
+ if (!host) throw new WorkerHasNoApp(id);
1856
+ units.push({
1857
+ id,
1858
+ name,
1859
+ app: host.name
1860
+ });
1861
+ }
1862
+ return units.sort((a, b) => a.name.localeCompare(b.name));
1863
+ }
1864
+ /** The service and image name of a file server's processors: `assets-processors`. */
1865
+ function processorUnitName(fileServerId) {
1866
+ return `${appKey(fileServerId.replace(/Server$/, ""))}-processors`;
1867
+ }
1868
+ /** A file server with processors and no backend app to build them from. */
1869
+ var ProcessorsHaveNoApp = class extends GkmError {
1870
+ fileServer;
1871
+ constructor(fileServer) {
1872
+ super(`The file server '${fileServer}' has processors, and the workspace has no backend app that builds a server to carry their entry. Declare a RestApi whose app holds the processors' code.`);
1873
+ this.fileServer = fileServer;
1874
+ this.name = "ProcessorsHaveNoApp";
1875
+ }
1876
+ };
1877
+ /**
1878
+ * The processor units a workspace deploys — each file server with processors
1879
+ * — and the app each is built from, chosen as a worker's is: the backend app
1880
+ * holding the most of its processors' files.
1881
+ */
1882
+ function processorUnits(workspace, manifest) {
1883
+ const hosts = hostApps(workspace, manifest);
1884
+ const units = [];
1885
+ for (const declaration of Object.values(manifest)) {
1886
+ if (declaration.kind !== "file-server") continue;
1887
+ const processors = declaration.processors ?? [];
1888
+ if (processors.length === 0) continue;
1889
+ const host = hostFor(hosts, processors.map(({ handler }) => {
1890
+ const file = handler.split("#")[0] ?? handler;
1891
+ return isAbsolute(file) ? file : join(workspace.root, file);
1892
+ }));
1893
+ if (!host) throw new ProcessorsHaveNoApp(declaration.id);
1894
+ const name = processorUnitName(declaration.id);
1895
+ if (workspace.apps[name]) throw new WorkerNameTaken(declaration.id, name);
1896
+ units.push({
1897
+ id: declaration.id,
1898
+ name,
1899
+ app: host.name
1900
+ });
1901
+ }
1902
+ return units.sort((a, b) => a.name.localeCompare(b.name));
1903
+ }
1904
+ /** The backend apps whose builds generate a server, each where it lives. */
1905
+ function hostApps(workspace, manifest) {
1906
+ return Object.entries(workspace.apps).filter(([, app]) => app.type === "backend" && !app.entry).map(([name, app]) => {
1907
+ const surface = Object.entries(manifest).find(([id, d]) => (d.kind === "rest-api" || d.kind === "websocket-api") && appKey(id) === name)?.[1];
1908
+ return {
1909
+ name,
1910
+ root: isAbsolute(app.path) ? app.path : join(workspace.root, app.path),
1911
+ generated: !(surface?.kind === "websocket-api" || surface?.kind === "rest-api" && surface.servesItself === true)
1912
+ };
1913
+ }).sort((a, b) => a.name.localeCompare(b.name));
1914
+ }
1915
+ /**
1916
+ * The app holding the most of these files; then one that generates a server;
1917
+ * then by name. None when no app holds any and none generates a server.
1918
+ */
1919
+ function hostFor(hosts, files) {
1920
+ const ranked = hosts.map((host) => ({
1921
+ host,
1922
+ holds: files.filter((file) => isUnder(file, host.root)).length
1923
+ })).sort((a, b) => b.holds - a.holds || Number(b.host.generated) - Number(a.host.generated));
1924
+ const host = ranked[0]?.host;
1925
+ if (!host || ranked[0].holds === 0 && !host.generated) return void 0;
1926
+ return host;
1927
+ }
1928
+ /** Whether `file` is inside `dir`. */
1929
+ function isUnder(file, dir) {
1930
+ const rel = relative(dir, file);
1931
+ return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel) ? !rel.startsWith(`..${sep}`) : false;
1932
+ }
1933
+ //#endregion
1934
+ //#region src/compose/siteBuild.ts
1935
+ /**
1936
+ * What a site's image is built with — or a mobile app's EAS build: every
1937
+ * value its build reads, resolved for the stage, and so every `ARG` a site's
1938
+ * Dockerfile declares.
1939
+ *
1940
+ * - **Its edges' public values and its own address**, under the variant's
1941
+ * public prefix (`publicEnvFor`): what the bundler inlines.
1942
+ * - **Its own address under its own name** (`WEB_URL`). Server code runs at
1943
+ * build too — `next build` prerenders, and a page's `metadataBase` is read
1944
+ * then — and `gkm exec -- next build` has it locally, so the image build
1945
+ * must, or a site that builds locally fails in CI. A mobile app has no
1946
+ * address: its own value is its scheme (`APP_SCHEME`), which its
1947
+ * `app.config.ts` reads when EAS builds it.
1948
+ * - **Every stage secret already named for the bundle**: a key carrying the
1949
+ * variant's public prefix (`NEXT_PUBLIC_APP_STORE_URL` on a `next` site) was
1950
+ * declared public by whoever named it, so it is passed. Anything else is
1951
+ * not: a secret without the prefix never reaches a site's build.
1952
+ *
1953
+ * A value the stage resolves to nothing is left out rather than passed empty.
1954
+ * A derived value wins over a stage secret of the same name: the site's
1955
+ * addresses are the construct's, not a copy someone set.
1956
+ */
1957
+ function publicBuildValues(site, manifest, resolve, secrets) {
1958
+ const args = {};
1959
+ const own = provideKey(site.id, site.kind === "site" ? "url" : "scheme");
1960
+ const ownValue = resolve(own);
1961
+ if (ownValue) args[own] = ownValue;
1962
+ for (const [key, source] of Object.entries(publicEnvFor(site, manifest))) {
1963
+ const value = resolve(source);
1964
+ if (value) args[key] = value;
1965
+ }
1966
+ const prefix = PUBLIC_PREFIX[site.variant];
1967
+ for (const [key, value] of Object.entries(secrets)) if (key.startsWith(prefix) && args[key] === void 0 && value) args[key] = value;
1968
+ return args;
1969
+ }
1970
+ //#endregion
1971
+ //#region src/exec/imageBuild.ts
1972
+ /**
1973
+ * Set by a site's Dockerfile in its builder stage: this process is building
1974
+ * an image.
1975
+ *
1976
+ * Inside an image there is no stage to load secrets for and no local
1977
+ * container to resolve an address from — the public URLs the bundle inlines
1978
+ * arrived as the Dockerfile's build args, already in the environment. So
1979
+ * `gkm exec -- next build` there injects exactly those, and never the
1980
+ * `localhost` addresses a workspace resolves on a developer's machine.
1981
+ */
1982
+ const IMAGE_BUILD_ENV = "GKM_IMAGE_BUILD";
1983
+ /** Whether this process is building an image. */
1984
+ function isImageBuild(env = process.env) {
1985
+ return env[IMAGE_BUILD_ENV] === "1";
1986
+ }
1987
+ /**
1988
+ * What `gkm exec` injects inside an image build: the public values its build
1989
+ * args set — every `NEXT_PUBLIC_*`, `VITE_*` and `EXPO_PUBLIC_*` — and
1990
+ * nothing read from a secrets store, a file or a container.
1991
+ */
1992
+ function imageBuildCredentials(env = process.env) {
1993
+ const credentials = {};
1994
+ for (const [key, value] of Object.entries(env)) {
1995
+ if (value === void 0) continue;
1996
+ if (PUBLIC_ENV_PREFIXES.some((prefix) => key.startsWith(prefix))) credentials[key] = value;
1997
+ }
1998
+ return credentials;
1999
+ }
2000
+ //#endregion
2001
+ //#region src/docker/templates.ts
2002
+ /**
2003
+ * The turbo every image prunes and builds with when the build root resolves
2004
+ * none of its own: a repository without turbo still needs `turbo prune` to
2005
+ * cut its slice of the workspace, and an unpinned `turbo` is whatever the
2006
+ * registry says on the day of the build.
2007
+ */
2008
+ const TURBO_VERSION = "2.5.4";
2009
+ /**
2010
+ * Caddy, which serves a static site's files. A 2.x tag, so a rebuild picks
2011
+ * up patch releases and never a major.
2012
+ */
2013
+ const STATIC_SITE_IMAGE = "caddy:2.10-alpine";
2014
+ /**
2015
+ * The image generated Dockerfiles build on unless one is configured. Pinned to
2016
+ * a Node security release, not the floating `node:22-alpine` tag, so a cached
2017
+ * or older image can't put a build below the patched version — and the
2018
+ * Dockerfile says which version it runs.
2019
+ */
2020
+ const DEFAULT_BASE_IMAGE = "node:22.23.4-alpine";
2021
+ const LOCKFILES = [
2022
+ ["pnpm-lock.yaml", "pnpm"],
2023
+ ["bun.lockb", "bun"],
2024
+ ["yarn.lock", "yarn"],
2025
+ ["package-lock.json", "npm"]
2026
+ ];
2027
+ /**
2028
+ * Detect package manager from lockfiles
2029
+ * Walks up the directory tree to find lockfile (for monorepos)
2030
+ */
2031
+ function detectPackageManager(cwd = process.cwd()) {
2032
+ const lockfile = findLockfilePath(cwd);
2033
+ if (!lockfile) return "pnpm";
2034
+ const name = lockfile.slice(dirname(lockfile).length + 1);
2035
+ return LOCKFILES.find(([file]) => file === name)?.[1] ?? "pnpm";
2036
+ }
2037
+ /**
2038
+ * Find the lockfile path by walking up the directory tree
2039
+ * Returns the full path to the lockfile, or null if not found
2040
+ */
2041
+ function findLockfilePath(cwd = process.cwd()) {
2042
+ for (const dir of upFrom(cwd)) for (const [lockfile] of LOCKFILES) {
2043
+ const lockfilePath = join(dir, lockfile);
2044
+ if (existsSync(lockfilePath)) return lockfilePath;
2045
+ }
2046
+ return null;
2047
+ }
2048
+ /**
2049
+ * The directory every image is built from: the nearest at or above `cwd`
2050
+ * holding a lockfile or a `pnpm-workspace.yaml` — the package manager's root,
2051
+ * which is what an install resolves the workspace's dependencies against.
2052
+ *
2053
+ * In a project of its own that is the gkm workspace's root. A gkm workspace
2054
+ * nested inside a monorepo, whose dependencies are the monorepo's
2055
+ * `workspace:*` packages, is built from the monorepo's root. `cwd` itself
2056
+ * when neither is found.
2057
+ */
2058
+ function findBuildRoot(cwd = process.cwd()) {
2059
+ for (const dir of upFrom(cwd)) {
2060
+ if (existsSync(join(dir, "pnpm-workspace.yaml"))) return dir;
2061
+ for (const [lockfile] of LOCKFILES) if (existsSync(join(dir, lockfile))) return dir;
2062
+ }
2063
+ return cwd;
2064
+ }
2065
+ /** `cwd` and every directory above it, the filesystem root last. */
2066
+ function upFrom(cwd) {
2067
+ const dirs = [];
2068
+ const root = parse(cwd).root;
2069
+ let dir = cwd;
2070
+ while (dir !== root) {
2071
+ dirs.push(dir);
2072
+ dir = dirname(dir);
2073
+ }
2074
+ dirs.push(root);
2075
+ return dirs;
2076
+ }
2077
+ /** The package manager, its version, turbo's and the shape of a build root. */
2078
+ function resolveBuildTools(buildRoot) {
2079
+ const pkg = readJson(join(buildRoot, "package.json"));
2080
+ const packageManager = detectPackageManager(buildRoot);
2081
+ const field = typeof pkg?.packageManager === "string" ? pkg.packageManager : "";
2082
+ const match = /^([a-z]+)@(\d[^+\s]*)/.exec(field);
2083
+ const packageManagerVersion = match && match[1] === packageManager ? match[2] : void 0;
2084
+ return {
2085
+ packageManager,
2086
+ ...packageManagerVersion ? { packageManagerVersion } : {},
2087
+ turboVersion: resolveTurboVersion(buildRoot) ?? "2.5.4",
2088
+ monorepo: existsSync(join(buildRoot, "pnpm-workspace.yaml")) || pkg?.workspaces !== void 0
2089
+ };
2090
+ }
2091
+ /**
2092
+ * The turbo version a build root resolves: what is installed, or what its
2093
+ * lockfile pins. Undefined when it has none.
2094
+ */
2095
+ function resolveTurboVersion(buildRoot) {
2096
+ const installed = readJson(join(buildRoot, "node_modules", "turbo", "package.json"));
2097
+ if (typeof installed?.version === "string") return installed.version;
2098
+ const read = (file) => {
2099
+ try {
2100
+ return readFileSync(join(buildRoot, file), "utf-8");
2101
+ } catch {
2102
+ return;
2103
+ }
2104
+ };
2105
+ const pnpm = read("pnpm-lock.yaml");
2106
+ const fromPnpm = pnpm && /^ {2}'?turbo@(\d[^:'(\s]*)'?:/m.exec(pnpm);
2107
+ if (fromPnpm) return fromPnpm[1];
2108
+ const npm = read("package-lock.json");
2109
+ if (npm) {
2110
+ const version = safeParse(npm)?.packages?.["node_modules/turbo"]?.version;
2111
+ if (version) return version;
2112
+ }
2113
+ const yarn = read("yarn.lock");
2114
+ const fromYarn = yarn && /^"?turbo@[^\n]*:\n\s+version:? "?(\d[^"\s]*)"?/m.exec(yarn);
2115
+ if (fromYarn) return fromYarn[1];
2116
+ }
2117
+ function readJson(path) {
2118
+ try {
2119
+ return safeParse(readFileSync(path, "utf-8"));
2120
+ } catch {
2121
+ return;
2122
+ }
2123
+ }
2124
+ function safeParse(text) {
2125
+ try {
2126
+ const value = JSON.parse(text);
2127
+ return value && typeof value === "object" ? value : void 0;
2128
+ } catch {
2129
+ return;
2130
+ }
2131
+ }
2132
+ /**
2133
+ * Check if turbo.json exists (walks up directory tree)
2134
+ */
2135
+ function hasTurboConfig(cwd = process.cwd()) {
2136
+ return upFrom(cwd).some((dir) => existsSync(join(dir, "turbo.json")));
2137
+ }
2138
+ /**
2139
+ * Package manager specific commands. `version` pins what corepack (or npm,
2140
+ * for bun) installs; without it, the latest.
2141
+ */
2142
+ function getPmConfig(pm, version) {
2143
+ return {
2144
+ pnpm: {
2145
+ install: `corepack enable && corepack prepare pnpm@${version ?? "latest"} --activate`,
2146
+ lockfile: "pnpm-lock.yaml",
2147
+ installCmd: "pnpm install",
2148
+ cacheTarget: "/root/.local/share/pnpm/store",
2149
+ cacheId: "pnpm",
2150
+ run: "pnpm run",
2151
+ dlx: "pnpm dlx"
2152
+ },
2153
+ npm: {
2154
+ install: version ? `npm install -g npm@${version}` : "",
2155
+ lockfile: "package-lock.json",
2156
+ installCmd: "npm install",
2157
+ cacheTarget: "/root/.npm",
2158
+ cacheId: "npm",
2159
+ run: "npm run",
2160
+ dlx: "npx --yes"
2161
+ },
2162
+ yarn: {
2163
+ install: `corepack enable && corepack prepare yarn@${version ?? "stable"} --activate`,
2164
+ lockfile: "yarn.lock",
2165
+ installCmd: "yarn install",
2166
+ cacheTarget: "/root/.yarn/cache",
2167
+ cacheId: "yarn",
2168
+ run: "yarn run",
2169
+ dlx: "yarn dlx"
2170
+ },
2171
+ bun: {
2172
+ install: `npm install -g bun@${version ?? "latest"}`,
2173
+ lockfile: "bun.lockb",
2174
+ installCmd: "bun install",
2175
+ cacheTarget: "/root/.bun/install/cache",
2176
+ cacheId: "bun",
2177
+ run: "bun run",
2178
+ dlx: "bunx"
2179
+ }
2180
+ }[pm];
2181
+ }
2182
+ /** What every template's first three stages need, resolved from options. */
2183
+ function layout(options) {
2184
+ const pm = getPmConfig(options.packageManager, options.packageManagerVersion);
2185
+ const turbo = `${pm.dlx} turbo@${options.turboVersion ?? "2.5.4"}`;
2186
+ const gkmRoot = options.gkmRoot ?? ".";
2187
+ return {
2188
+ pm,
2189
+ turbo,
2190
+ installPm: pm.install ? `RUN ${pm.install}` : "",
2191
+ monorepo: options.monorepo ?? true,
2192
+ gkmRoot,
2193
+ /** The app, absolute inside the image. */
2194
+ app: `/app/${options.appPath}`.replace(/\/\.$/, "")
2195
+ };
2196
+ }
2197
+ /**
2198
+ * The pruner stage: the build context cut to the app's slice, in turbo's
2199
+ * `out/` layout — `out/json` for the install, `out/full` for the build.
2200
+ *
2201
+ * `turbo prune` keeps the packages the app depends on and nothing else at
2202
+ * the root, so the root's own files (a `tsconfig.base.json` the packages
2203
+ * extend, the config their build reads) and what the gkm workspace keeps
2204
+ * outside any package are copied in beside them. A single package is copied
2205
+ * whole.
2206
+ */
2207
+ /**
2208
+ * The Node image every stage builds on, as a build argument: declared before
2209
+ * the first `FROM`, so each stage can read it, and defaulted to the configured
2210
+ * image. A compose file's `build.args`, or `docker build --build-arg`, picks
2211
+ * another version without regenerating the Dockerfile.
2212
+ */
2213
+ function nodeImageArg(options) {
2214
+ return `# The Node image; override with --build-arg NODE_IMAGE=node:<version>-alpine
2215
+ ARG NODE_IMAGE=${options.baseImage}`;
2216
+ }
2217
+ function prunerStage(options) {
2218
+ const { pm, turbo, installPm, monorepo, gkmRoot } = layout(options);
2219
+ const scopes = [options.turboPackage, ...options.prunePackages ?? []].join(" ");
2220
+ if (!monorepo) return `${nodeImageArg(options)}
2221
+
2222
+ # Stage 1: The build context, in the layout the stages below read
2223
+ FROM \${NODE_IMAGE} AS pruner
2224
+ WORKDIR /app
2225
+ COPY . .
2226
+ RUN mkdir -p /tmp/out/json /tmp/out/full && \\
2227
+ cp package.json /tmp/out/json/ && \\
2228
+ cp ${pm.lockfile} /tmp/out/ && \\
2229
+ cp -a . /tmp/out/full/ && \\
2230
+ mv /tmp/out ./out`;
2231
+ const gkmPaths = options.gkmPaths ?? [];
2232
+ const nested = gkmRoot !== ".";
2233
+ const dest = nested ? `/app/out/full/${gkmRoot}` : "/app/out/full";
2234
+ const copyGkm = gkmPaths.length ? `
2235
+
2236
+ # What the gkm workspace keeps outside any package — its config, the
2237
+ # directories its constructs are declared in — beside the slice.
2238
+ RUN ${nested ? `cd ${gkmRoot} && ` : ""}for path in ${gkmPaths.join(" ")}; do \\
2239
+ if [ -e "$path" ]; then \\
2240
+ mkdir -p "${dest}" && cp -rn "$path" "${dest}/"; \\
2241
+ fi; \\
2242
+ done` : "";
2243
+ return `${nodeImageArg(options)}
2244
+
2245
+ # Stage 1: Prune the monorepo to the app's slice
2246
+ FROM \${NODE_IMAGE} AS pruner
2247
+ WORKDIR /app
2248
+ ${installPm}
2249
+ COPY . .
2250
+ RUN ${turbo} prune ${scopes} --docker
2251
+
2252
+ # The root's own files — the tsconfig the packages extend, the config their
2253
+ # build reads — which turbo prune leaves out. What the install itself reads
2254
+ # (registry config, local tarballs) goes beside the manifests too.
2255
+ RUN find . -maxdepth 1 -type f ! -name '${pm.lockfile}' ! -name package.json \\
2256
+ -exec cp -n {} out/full/ \\; && \\
2257
+ for file in .npmrc .yarnrc.yml .pnpmfile.cjs bunfig.toml *.tgz; do \\
2258
+ if [ -f "$file" ]; then cp "$file" out/json/; fi; \\
2259
+ done${copyGkm}`;
2260
+ }
2261
+ /** The deps stage: the slice's dependencies, installed from its lockfile. */
2262
+ function depsStage(options) {
2263
+ const { pm, installPm } = layout(options);
2264
+ return `# Stage 2: Install dependencies
2265
+ FROM \${NODE_IMAGE} AS deps
2266
+ WORKDIR /app
2267
+ ${installPm}
2268
+ COPY --from=pruner /app/out/${pm.lockfile} ./
2269
+ COPY --from=pruner /app/out/json/ ./
2270
+ RUN --mount=type=cache,id=${pm.cacheId},target=${pm.cacheTarget} \\
2271
+ ${pm.installCmd}`;
2272
+ }
2273
+ /**
2274
+ * The start of the builder stage: the slice's source over its dependencies,
2275
+ * and every workspace package the app depends on built from it.
2276
+ *
2277
+ * Nothing built on the host reaches the image — `.dockerignore` keeps every
2278
+ * `dist` out — so a workspace package (a CLI that is a sibling package) is
2279
+ * built here: turbo's build of the app's dependencies (`^build`) and nothing
2280
+ * else. The root's own `build` script is never run: the slice holds one app,
2281
+ * and a root `build` that is `gkm build` (what `gkm init` scaffolds) builds
2282
+ * every app the workspace declares, most of which are not in it. The app
2283
+ * itself is built after this, by `gkm build` or its framework.
2284
+ */
2285
+ function builderStage(options, extra = {}) {
2286
+ const { turbo, monorepo } = layout(options);
2287
+ const args = extra.args ?? [];
2288
+ const argLines = args.length ? `\n# Build-time args: public values the bundler inlines\n${args.map((a) => `ARG ${a}=""`).join("\n")}\n${args.map((a) => `ENV ${a}=$${a}`).join("\n")}\n` : "";
2289
+ const scopes = [options.turboPackage, ...options.prunePackages ?? []];
2290
+ return `# Stage 3: Build
2291
+ FROM deps AS builder
2292
+ WORKDIR /app
2293
+ ${argLines}
2294
+ COPY --from=pruner /app/out/full/ ./${monorepo ? `\n# The workspace packages the app depends on, built from source.\nRUN ${turbo} run build ${scopes.map((s) => `--filter='${s}^...'`).join(" ")}` : ""}`;
2295
+ }
2296
+ /**
2297
+ * The PATH a command in the app's directory runs with: the app's binaries,
2298
+ * then the gkm workspace's, then the root's — a nested workspace's apps
2299
+ * declare nothing, and its CLI is installed where its config is.
2300
+ */
2301
+ function binPath(options) {
2302
+ const { app, gkmRoot } = layout(options);
2303
+ const dirs = [
2304
+ `${app}/node_modules/.bin`,
2305
+ gkmRoot === "." ? "" : `/app/${gkmRoot}/node_modules/.bin`,
2306
+ "/app/node_modules/.bin"
2307
+ ].filter(Boolean);
2308
+ return `PATH="${[...new Set(dirs)].join(":")}:$PATH"`;
2309
+ }
2310
+ /**
2311
+ * Run the gkm CLI the app resolves, from the app's directory: node on its
2312
+ * `bin/gkm.mjs`, found the way Node resolves a package — up from the app.
2313
+ *
2314
+ * Not through `node_modules/.bin`: a CLI that is a workspace package has no
2315
+ * bin linked, since its files were not there when the slice's dependencies
2316
+ * were installed.
2317
+ */
2318
+ function gkmCommand(args) {
2319
+ return `GKM_BIN="$(node -e '${FIND_GKM_BIN}')" && node "$GKM_BIN" ${args}`;
2320
+ }
2321
+ /** Prints the `bin/gkm.mjs` Node resolves from the cwd, or fails saying so. */
2322
+ const FIND_GKM_BIN = `let d=process.cwd();const{existsSync:e}=require("fs"),{dirname:u,join:j}=require("path");for(;;){const p=j(d,"node_modules/@geekmidas/cli/bin/gkm.mjs");if(e(p)){console.log(p);break}if(u(d)===d){console.error("@geekmidas/cli is not installed where the app can resolve it");process.exit(1)}d=u(d)}`;
2323
+ /**
2324
+ * What a site's builder does with the gkm workspace it carries, before its
2325
+ * framework builds it — all of it inside the image, nothing from the host:
2326
+ *
2327
+ * - `gkm` on the PATH: the CLI the workspace installs, so a build script that
2328
+ * is `gkm exec -- next build` runs. Resolved from the gkm root rather than
2329
+ * through `node_modules/.bin`, where a CLI that is a workspace package has
2330
+ * no bin linked.
2331
+ * - `GKM_IMAGE_BUILD`, which has `gkm exec` inject the build args above — the
2332
+ * stage's public URLs — and never load a secret or resolve a local address.
2333
+ * - each backend's typed client, generated from its endpoints at the
2334
+ * workspace root's `.gkm/client/`, where the site's tsconfig paths point.
2335
+ */
2336
+ function siteWorkspaceSteps(options) {
2337
+ const { gkmRoot } = layout(options);
2338
+ const root = `/app/${gkmRoot}`.replace(/\/\.$/, "");
2339
+ const clients = options.clients ?? [];
2340
+ const generate = clients.length ? `
2341
+
2342
+ # The typed client of each API this site calls, generated from its endpoints
2343
+ # into ${gkmRoot === "." ? "" : `${gkmRoot}/`}.gkm/client/ — offline, with no secret and no container.
2344
+ RUN ${clients.map(({ app, path, telemetry }) => [
2345
+ "cd",
2346
+ `/app/${path}`,
2347
+ "&&",
2348
+ "gkm",
2349
+ "openapi",
2350
+ "--app",
2351
+ app,
2352
+ ...telemetryArgs(telemetry)
2353
+ ].join(" ")).join(" && \\\n ")}` : "";
2354
+ return `# The gkm CLI the workspace installs, as \`gkm\`
2355
+ RUN cd ${root} && GKM_BIN="$(node -e '${FIND_GKM_BIN}')" && \\
2356
+ { echo '#!/bin/sh'; echo "exec node \\"$GKM_BIN\\" \\"\\$@\\""; } > /usr/local/bin/gkm && \\
2357
+ chmod +x /usr/local/bin/gkm
2358
+
2359
+ # \`gkm exec\` in this build injects the build args, never a stage's secrets
2360
+ ENV ${IMAGE_BUILD_ENV}=1${generate}`;
2361
+ }
2362
+ /**
2363
+ * Build the site with turbo. Loose env mode, so the task sees what the
2364
+ * Dockerfile set — the build args and `GKM_IMAGE_BUILD` — whatever the
2365
+ * project's turbo.json declares: the image's environment holds nothing else.
2366
+ */
2367
+ function siteBuild(options) {
2368
+ const { turbo } = layout(options);
2369
+ return `RUN ${turbo} run build --filter='${options.turboPackage}' --env-mode=loose`;
2370
+ }
2371
+ /**
2372
+ * The encrypted credentials, from a build secret — never an ARG, which is
2373
+ * recorded in the image history — written where the build embeds them from.
2374
+ * Two lines, ciphertext then IV; absent when the app has no secrets.
2375
+ *
2376
+ * A BuildKit secret is not part of the cache key, so `GKM_CIPHERTEXT_HASH`
2377
+ * (a hash of the ciphertext, not a secret) is: credentials encrypted under a
2378
+ * new key rebuild the layer that embeds them.
2379
+ */
2380
+ function credentialsStep(appDir, then) {
2381
+ return `ARG GKM_CIPHERTEXT_HASH=""
2382
+ RUN --mount=type=secret,id=gkm_credentials,required=false \\
2383
+ echo "credentials: \${GKM_CIPHERTEXT_HASH:-none}" && \\
2384
+ if [ -s /run/secrets/gkm_credentials ]; then \\
2385
+ mkdir -p ${appDir}/.gkm && \\
2386
+ sed -n 1p /run/secrets/gkm_credentials > ${appDir}/.gkm/credentials.enc && \\
2387
+ sed -n 2p /run/secrets/gkm_credentials > ${appDir}/.gkm/credentials.iv; \\
2388
+ fi && \\
2389
+ ${then} && \\
2390
+ rm -f ${appDir}/.gkm/credentials.enc ${appDir}/.gkm/credentials.iv`;
2391
+ }
2392
+ /**
2393
+ * The packages a bundle left out — its native ones, which load a binary built
2394
+ * for the platform they were installed on — installed for Linux beside it.
2395
+ * `gkm build` records them, at the versions it resolved, in a `package.json`
2396
+ * beside the bundle (`externalsManifestName`), so the image installs exactly
2397
+ * what the build left out: nothing, for most apps.
2398
+ */
2399
+ function externalsStages(appDir, bundle) {
2400
+ return {
2401
+ stage: `
2402
+ # The packages the bundle leaves out, installed for Linux at the versions the
2403
+ # build resolved — and nothing else.
2404
+ FROM \${NODE_IMAGE} AS externals
2405
+ WORKDIR /externals
2406
+ COPY --from=builder ${appDir}/.gkm/server/dist/${externalsManifestName(bundle)} ./package.json
2407
+ RUN --mount=type=cache,id=npm,target=/root/.npm \\
2408
+ npm install --omit=dev --no-package-lock --no-audit --no-fund && \\
2409
+ mkdir -p node_modules
2410
+ `,
2411
+ copy: `\nCOPY --from=externals --chown=hono:nodejs /externals/node_modules ./node_modules`
2412
+ };
2413
+ }
2414
+ /**
2415
+ * Generate .dockerignore file
2416
+ *
2417
+ * Written at the build root. Nothing built on the host may reach an image —
2418
+ * every image builds its packages from source — and nothing secret may reach
2419
+ * a build context.
2420
+ */
2421
+ function generateDockerignore() {
2422
+ return `# Dependencies: installed in the image, for Linux
2423
+ **/node_modules
2424
+ .pnpm-store
2425
+
2426
+ # Build output: every image builds from source, never from the host's
2427
+ **/dist
2428
+ **/.next
2429
+ **/.turbo
2430
+ **/.gkm
2431
+
2432
+ # The key gkm build --stage encrypted secrets with: runtime env only, never
2433
+ # the build context
2434
+ **/master.key
2435
+
2436
+ # gkm compose's stacks: each app's env file holds its stage's secrets
2437
+ **/.gkm/compose
2438
+
2439
+ # IDE and editor
2440
+ .idea
2441
+ .vscode
2442
+ *.swp
2443
+ *.swo
2444
+
2445
+ # Git
2446
+ .git
2447
+
2448
+ # Logs
2449
+ *.log
2450
+ npm-debug.log*
2451
+ pnpm-debug.log*
2452
+
2453
+ # Test files
2454
+ coverage
2455
+
2456
+ # Environment files (handle secrets separately)
2457
+ **/.env
2458
+ **/.env.*
2459
+ !**/.env.example
2460
+
2461
+ # Docker files (don't copy recursively)
2462
+ Dockerfile*
2463
+ docker-compose*
2464
+ .dockerignore
2465
+ `;
2466
+ }
2467
+ /**
2468
+ * Resolve Docker configuration from GkmConfig with defaults
2469
+ */
2470
+ function resolveDockerConfig(config) {
2471
+ const docker = config.docker ?? {};
2472
+ return {
2473
+ registry: docker.registry ?? "",
2474
+ imageName: docker.imageName ?? config.name ?? "api",
2475
+ baseImage: docker.baseImage ?? "node:22.23.4-alpine",
2476
+ port: docker.port ?? 3e3
2477
+ };
2478
+ }
2479
+ /** A site's root answers its health check. */
2480
+ function siteHealthcheck(port) {
2481
+ return `# A site answers at its root; Docker, and Dokploy, read the container's
2482
+ # health from it rather than from the process merely running. 127.0.0.1, not
2483
+ # localhost: the server listens on IPv4, and localhost can resolve to ::1.
2484
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \\
2485
+ CMD wget -qO- http://127.0.0.1:${port}/ > /dev/null 2>&1 || exit 1`;
2486
+ }
2487
+ /**
2488
+ * Generate a Dockerfile for Next.js frontend apps using standalone output.
2489
+ *
2490
+ * Pruned at the build root, Next traces from there, so the standalone
2491
+ * server lands at `<appPath>/server.js` under `.next/standalone`.
2492
+ * @internal Exported for testing
2493
+ */
2494
+ function generateNextjsDockerfile(options) {
2495
+ const { port, appPath, publicUrlArgs = ["NEXT_PUBLIC_API_URL", "NEXT_PUBLIC_AUTH_URL"] } = options;
2496
+ return `# syntax=docker/dockerfile:1
2497
+ # Next.js standalone Dockerfile, built from a turbo-pruned slice
2498
+
2499
+ ${prunerStage(options)}
2500
+
2501
+ ${depsStage(options)}
2502
+
2503
+ ${builderStage(options, { args: publicUrlArgs })}
2504
+
2505
+ ${siteWorkspaceSteps(options)}
2506
+
2507
+ # Ensure public directory exists (may be empty for scaffolded projects)
2508
+ RUN mkdir -p ${appPath}/public
2509
+
2510
+ ENV NEXT_TELEMETRY_DISABLED=1
2511
+
2512
+ # Build the application
2513
+ ${siteBuild(options)}
2514
+
2515
+ # The runner needs the standalone server; without it there is nothing to run.
2516
+ RUN if [ ! -d ${appPath}/.next/standalone ]; then \\
2517
+ echo "${appPath}/.next/standalone was not built: set output: 'standalone' in next.config so the image can run the app without node_modules." >&2; \\
2518
+ exit 1; \\
2519
+ fi
2520
+
2521
+ # Stage 4: Production
2522
+ FROM \${NODE_IMAGE} AS runner
2523
+
2524
+ WORKDIR /app
2525
+
2526
+ # Install tini for proper signal handling
2527
+ RUN apk add --no-cache tini
2528
+
2529
+ # Create non-root user
2530
+ RUN addgroup --system --gid 1001 nodejs && \\
2531
+ adduser --system --uid 1001 nextjs
2532
+
2533
+ # Set environment
2534
+ ENV NODE_ENV=production
2535
+ ENV NEXT_TELEMETRY_DISABLED=1
2536
+ ENV PORT=${port}
2537
+ ENV HOSTNAME="0.0.0.0"
2538
+
2539
+ # The standalone server, with its static files and public directory beside it
2540
+ COPY --from=builder --chown=nextjs:nodejs /app/${appPath}/.next/standalone ./
2541
+ COPY --from=builder --chown=nextjs:nodejs /app/${appPath}/.next/static ./${appPath}/.next/static
2542
+ COPY --from=builder --chown=nextjs:nodejs /app/${appPath}/public ./${appPath}/public
2543
+
2544
+ USER nextjs
2545
+
2546
+ EXPOSE ${port}
2547
+
2548
+ ${siteHealthcheck(port)}
2549
+
2550
+ ENTRYPOINT ["/sbin/tini", "--"]
2551
+ CMD ["node", "${appPath}/server.js"]
2552
+ `;
2553
+ }
2554
+ /**
2555
+ * Generate a Dockerfile for a gkm backend: pruned, installed, and bundled by
2556
+ * `gkm build --provider server --production` inside the image. The runner is
2557
+ * `server.mjs` — plus the packages the bundle leaves out, which is none
2558
+ * unless it uses one with native code.
2559
+ * @internal Exported for testing
2560
+ */
2561
+ function generateBackendDockerfile(options) {
2562
+ return gkmBundleDockerfile(options, {
2563
+ title: "Backend Dockerfile",
2564
+ bundle: "server.mjs",
2565
+ as: "server.mjs",
2566
+ expose: true
2567
+ });
2568
+ }
2569
+ /** The port a worker answers its health check on, inside its container. */
2570
+ const WORKER_PORT = 3e3;
2571
+ /** The port a processor unit answers on, inside its container. */
2572
+ const PROCESSOR_PORT = 3e3;
2573
+ /**
2574
+ * A Worker's image: the same build as its host app's backend — a pruned
2575
+ * slice, `gkm build --provider server --production` in the image, with the
2576
+ * encrypted credentials from the `gkm_credentials` secret — and a runner that
2577
+ * is the worker's bundle alone. It publishes nothing: its one route is the
2578
+ * health check the HEALTHCHECK asks.
2579
+ * @internal Exported for testing
2580
+ */
2581
+ function generateWorkerDockerfile(options) {
2582
+ return gkmBundleDockerfile(options, {
2583
+ title: `Worker Dockerfile (${options.worker})`,
2584
+ bundle: options.bundle,
2585
+ as: "worker.mjs",
2586
+ expose: false
2587
+ });
2588
+ }
2589
+ /**
2590
+ * A file server's processor unit: its host app's build, and the unit's bundle
2591
+ * as the runner. Its native packages — `sharp` — are installed for the image's
2592
+ * platform beside the bundle, at the versions the build resolved, as for
2593
+ * every bundle.
2594
+ */
2595
+ function generateProcessorDockerfile(options) {
2596
+ return gkmBundleDockerfile(options, {
2597
+ title: `Processors Dockerfile (${options.fileServer})`,
2598
+ bundle: options.bundle,
2599
+ as: "processors.mjs",
2600
+ expose: true
2601
+ });
2602
+ }
2603
+ function gkmBundleDockerfile(options, runner) {
2604
+ const { port, healthCheckPath = "/health" } = options;
2605
+ const { app } = layout(options);
2606
+ const externals = externalsStages(app, runner.bundle);
2607
+ return `# syntax=docker/dockerfile:1
2608
+ # ${runner.title}: a turbo-pruned slice, bundled by gkm build in the image
2609
+
2610
+ ${prunerStage(options)}
2611
+
2612
+ ${depsStage(options)}
2613
+
2614
+ ${builderStage(options)}
2615
+
2616
+ # Bundle the production server, with the encrypted credentials embedded
2617
+ ${credentialsStep(app, `cd ${app} && ${gkmCommand(`build --provider server --production${options.cache ? ` --cache ${options.cache}` : ""}`)}`)}
2618
+ ${externals.stage}
2619
+ # Production
2620
+ FROM \${NODE_IMAGE} AS runner
2621
+
2622
+ WORKDIR /app
2623
+
2624
+ RUN apk add --no-cache tini
2625
+
2626
+ RUN addgroup --system --gid 1001 nodejs && \\
2627
+ adduser --system --uid 1001 hono
2628
+
2629
+ # The bundled ${runner.as.replace(/\.mjs$/, "")}
2630
+ COPY --from=builder --chown=hono:nodejs ${app}/.gkm/server/dist/${runner.bundle} ./${runner.as === runner.bundle ? "" : runner.as}${externals.copy}
2631
+
2632
+ ENV NODE_ENV=production
2633
+ ENV PORT=${port}
2634
+
2635
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \\
2636
+ CMD wget -qO- http://localhost:${port}${healthCheckPath} > /dev/null 2>&1 || exit 1
2637
+
2638
+ USER hono
2639
+ ${runner.expose ? `\nEXPOSE ${port}\n` : ""}
2640
+ ENTRYPOINT ["/sbin/tini", "--"]
2641
+ CMD ["node", "${runner.as}"]
2642
+ `;
2643
+ }
2644
+ /**
2645
+ * Generate a Dockerfile for apps with a custom entry point.
2646
+ * Uses esbuild to bundle the entry point into dist/index.mjs with all dependencies.
2647
+ * This is used for apps that don't use gkm routes (e.g., Better Auth servers).
2648
+ * @internal Exported for testing
2649
+ */
2650
+ function generateEntryDockerfile(options) {
2651
+ const { port, entry, healthCheckPath = "/health" } = options;
2652
+ const { app } = layout(options);
2653
+ const esbuild = (defines) => `esbuild ${entry} --bundle --platform=node --target=node22 --format=esm \\
2654
+ --outfile=dist/index.mjs --packages=bundle \\
2655
+ --banner:js='import { createRequire } from "module"; const require = createRequire(import.meta.url);'${defines}`;
2656
+ return `# syntax=docker/dockerfile:1
2657
+ # Entry-based Dockerfile: a turbo-pruned slice, bundled by esbuild in the image
2658
+
2659
+ ${prunerStage(options)}
2660
+
2661
+ ${depsStage(options)}
2662
+
2663
+ ${builderStage(options)}
2664
+
2665
+ # Bundle the entry point with every dependency (dist/index.mjs), embedding the
2666
+ # encrypted credentials when there are any.
2667
+ ARG GKM_CIPHERTEXT_HASH=""
2668
+ RUN --mount=type=secret,id=gkm_credentials,required=false \\
2669
+ echo "credentials: \${GKM_CIPHERTEXT_HASH:-none}" && \\
2670
+ cd ${app} && export ${binPath(options)} && \\
2671
+ if [ -s /run/secrets/gkm_credentials ]; then \\
2672
+ CREDS=$(sed -n 1p /run/secrets/gkm_credentials) && \\
2673
+ IV=$(sed -n 2p /run/secrets/gkm_credentials) && \\
2674
+ ${esbuild(` \\
2675
+ --define:__GKM_ENCRYPTED_CREDENTIALS__="\\"$CREDS\\"" \\
2676
+ --define:__GKM_CREDENTIALS_IV__="\\"$IV\\""`)}; \\
2677
+ else \\
2678
+ ${esbuild("")}; \\
2679
+ fi
2680
+
2681
+ # Production
2682
+ FROM \${NODE_IMAGE} AS runner
2683
+
2684
+ WORKDIR /app
2685
+
2686
+ RUN apk add --no-cache tini
2687
+
2688
+ RUN addgroup --system --gid 1001 nodejs && \\
2689
+ adduser --system --uid 1001 app
2690
+
2691
+ # Copy bundled output only (no node_modules needed - fully bundled)
2692
+ COPY --from=builder --chown=app:nodejs ${app}/dist/index.mjs ./
2693
+
2694
+ ENV NODE_ENV=production
2695
+ ENV PORT=${port}
2696
+
2697
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \\
2698
+ CMD wget -qO- http://localhost:${port}${healthCheckPath} > /dev/null 2>&1 || exit 1
2699
+
2700
+ USER app
2701
+
2702
+ EXPOSE ${port}
2703
+
2704
+ ENTRYPOINT ["/sbin/tini", "--"]
2705
+ CMD ["node", "index.mjs"]
2706
+ `;
2707
+ }
2708
+ /**
2709
+ * Generate a Dockerfile for Node-based SSR web frameworks (TanStack Start,
2710
+ * Remix). Builds with turbo, then runs the framework's production start
2711
+ * script.
2712
+ *
2713
+ * Public URL build args are declared so framework bundlers (Vite, Vinxi)
2714
+ * can inline them at build time. Per-framework prefix is the caller's job.
2715
+ * @internal Exported for testing
2716
+ */
2717
+ function generateNodeWebDockerfile(options) {
2718
+ const { port, appPath } = options;
2719
+ return `# syntax=docker/dockerfile:1
2720
+ # Node SSR web Dockerfile (TanStack Start / Remix), from a turbo-pruned slice
2721
+
2722
+ ${prunerStage(options)}
2723
+
2724
+ ${depsStage(options)}
2725
+
2726
+ ${builderStage(options, { args: options.publicUrlArgs ?? [] })}
2727
+
2728
+ ${siteWorkspaceSteps(options)}
2729
+
2730
+ ${siteBuild(options)}
2731
+
2732
+ FROM \${NODE_IMAGE} AS runner
2733
+ WORKDIR /app
2734
+ RUN apk add --no-cache tini
2735
+ RUN addgroup --system --gid 1001 nodejs && \\
2736
+ adduser --system --uid 1001 app
2737
+
2738
+ # Copy the whole built workspace — frameworks differ on output paths
2739
+ COPY --from=builder --chown=app:nodejs /app/ ./
2740
+
2741
+ ENV NODE_ENV=production
2742
+ ENV PORT=${port}
2743
+ ENV HOSTNAME="0.0.0.0"
2744
+
2745
+ USER app
2746
+ EXPOSE ${port}
2747
+
2748
+ ${siteHealthcheck(port)}
2749
+
2750
+ ENTRYPOINT ["/sbin/tini", "--"]
2751
+ # npm ships with node, and runs the start script whichever package manager
2752
+ # installed it: the runner needs no other.
2753
+ CMD ["sh", "-c", "cd ${appPath} && npm start"]
2754
+ `;
2755
+ }
2756
+ /**
2757
+ * Generate a Dockerfile for Vite SPA apps. Builds the static bundle with
2758
+ * turbo, then serves it with Caddy: Vite's hashed `/assets/*` cached for a
2759
+ * year, everything else — `index.html`, and every deep link that falls back
2760
+ * to it — revalidated on each request.
2761
+ * @internal Exported for testing
2762
+ */
2763
+ function generateViteStaticDockerfile(options) {
2764
+ const { port, appPath } = options;
2765
+ return `# syntax=docker/dockerfile:1
2766
+ # Vite SPA Dockerfile — a turbo-pruned slice built to static files, served by Caddy
2767
+
2768
+ ${prunerStage(options)}
2769
+
2770
+ ${depsStage(options)}
2771
+
2772
+ ${builderStage(options, { args: options.publicUrlArgs ?? [] })}
2773
+
2774
+ ${siteWorkspaceSteps(options)}
2775
+
2776
+ ${siteBuild(options)}
2777
+
2778
+ FROM ${STATIC_SITE_IMAGE} AS runner
2779
+
2780
+ COPY <<'EOF' /etc/caddy/Caddyfile
2781
+ {
2782
+ admin off
2783
+ auto_https off
2784
+ persist_config off
2785
+ }
2786
+
2787
+ :${port} {
2788
+ root * /srv
2789
+ encode zstd gzip
2790
+
2791
+ # Vite names every built asset by its content hash: a changed file is a
2792
+ # new URL, so the old one can be cached for good.
2793
+ handle /assets/* {
2794
+ header Cache-Control "public, max-age=31536000, immutable"
2795
+ file_server
2796
+ }
2797
+
2798
+ # index.html, and every client-side route that falls back to it, is
2799
+ # asked for again each time so a deploy is seen at once.
2800
+ handle {
2801
+ header Cache-Control "no-cache"
2802
+ try_files {path} /index.html
2803
+ file_server
2804
+ }
2805
+ }
2806
+ EOF
2807
+
2808
+ COPY --from=builder /app/${appPath}/dist /srv
2809
+
2810
+ RUN addgroup -S -g 1001 site && adduser -S -u 1001 -G site site && \\
2811
+ chown -R site:site /data /config
2812
+
2813
+ USER site
2814
+
2815
+ EXPOSE ${port}
2816
+
2817
+ ${siteHealthcheck(port)}
2818
+
2819
+ CMD ["caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
2820
+ `;
2821
+ }
2822
+ //#endregion
2823
+ //#region src/docker/layout.ts
2824
+ /**
2825
+ * Where a workspace's images are built from, and what each is built with.
2826
+ *
2827
+ * Every builder of images — `gkm docker`, `gkm compose`, a Dokploy deploy —
2828
+ * builds from the same place with the same templates: the build root (the
2829
+ * package manager's root, at or above the gkm workspace), cut by `turbo
2830
+ * prune` to one app's slice. Nothing is built on the host.
2831
+ */
2832
+ /** A workspace of packages with no turbo.json, which every image prunes with. */
2833
+ var MonorepoNeedsTurbo = class extends GkmError {
2834
+ buildRoot;
2835
+ constructor(buildRoot) {
2836
+ super(`${buildRoot} is a workspace of packages, and its images are built from a turbo-pruned slice of it — but it has no turbo.json.\n\nTo fix this:
2837
+ 1. Install turbo: pnpm add -Dw turbo
2838
+ 2. Create turbo.json in ${buildRoot}\n 3. Run this command again
2839
+
2840
+ See: https://turbo.build/repo/docs/guides/tools/docker`);
2841
+ this.buildRoot = buildRoot;
2842
+ this.name = "MonorepoNeedsTurbo";
2843
+ }
2844
+ };
2845
+ /**
2846
+ * Where a workspace's images are built from.
2847
+ *
2848
+ * Refuses a workspace of packages with no turbo.json — every image prunes
2849
+ * with turbo — so only a caller about to generate or build images asks.
2850
+ */
2851
+ function imageLayout(workspace) {
2852
+ const buildRoot = findBuildRoot(workspace.root);
2853
+ const tools = resolveBuildTools(buildRoot);
2854
+ if (tools.monorepo && !hasTurboConfig(buildRoot)) throw new MonorepoNeedsTurbo(buildRoot);
2855
+ const gkmRoot = toPosix(relative(buildRoot, workspace.root)) || ".";
2856
+ const workspacePackage = gkmRoot !== "." ? packageName(workspace.root) : void 0;
2857
+ return {
2858
+ buildRoot,
2859
+ gkmRoot,
2860
+ tools,
2861
+ ...workspacePackage ? { workspacePackage } : {},
2862
+ gkmPaths: ["gkm.config.*", ...constructRoots(workspace)]
2863
+ };
2864
+ }
2865
+ /**
2866
+ * How a compose file builds an app: the build root as the context, relative
2867
+ * to the directory the compose file is in, and the Dockerfile — written under
2868
+ * the gkm root — relative to that context, which is what compose resolves
2869
+ * `dockerfile` against.
2870
+ */
2871
+ function composeBuildPaths(options) {
2872
+ const { composeDir, buildRoot, workspaceRoot, dockerfile } = options;
2873
+ return {
2874
+ context: toPosix(relative(composeDir, buildRoot)) || ".",
2875
+ dockerfile: toPosix(join(relative(buildRoot, workspaceRoot), dockerfile))
2876
+ };
2877
+ }
2878
+ /** A path under the gkm root, as the build root sees it. */
2879
+ function fromBuildRoot(layout, path) {
2880
+ if (layout.gkmRoot === ".") return toPosix(path);
2881
+ const joined = toPosix(join(layout.gkmRoot, path));
2882
+ return joined === "" ? "." : joined;
2883
+ }
2884
+ /** Everything a template needs about one app, relative to the build root. */
2885
+ function appImageOptions(layout, appName, app, workspaceRoot, apps = {}, clientTelemetry) {
2886
+ const turboPackage = packageName(isAbsolute(app.path) ? app.path : join(workspaceRoot, app.path)) ?? appName;
2887
+ return {
2888
+ imageName: appName,
2889
+ baseImage: DEFAULT_BASE_IMAGE,
2890
+ port: app.port,
2891
+ appPath: fromBuildRoot(layout, app.path),
2892
+ turboPackage,
2893
+ packageManager: layout.tools.packageManager,
2894
+ ...layout.tools.packageManagerVersion ? { packageManagerVersion: layout.tools.packageManagerVersion } : {},
2895
+ turboVersion: layout.tools.turboVersion,
2896
+ monorepo: layout.tools.monorepo,
2897
+ gkmRoot: layout.gkmRoot,
2898
+ ...app.type === "backend" ? {
2899
+ gkmPaths: layout.gkmPaths,
2900
+ ...layout.workspacePackage && layout.workspacePackage !== turboPackage ? { prunePackages: [layout.workspacePackage] } : {}
2901
+ } : app.type === "web" ? siteWorkspace(layout, app, apps, workspaceRoot, turboPackage, clientTelemetry) : {}
2902
+ };
2903
+ }
2904
+ /**
2905
+ * What a site's image carries of the gkm workspace: its config and construct
2906
+ * directories, the workspace's own package where it is nested in a monorepo
2907
+ * (it holds the CLI), and each backend the site calls — whose endpoints its
2908
+ * client is generated from, and whose package their imports resolve in.
2909
+ */
2910
+ function siteWorkspace(layout, site, apps, workspaceRoot, turboPackage, clientTelemetry) {
2911
+ const clients = siteClients(site, apps);
2912
+ const packages = /* @__PURE__ */ new Set();
2913
+ if (layout.workspacePackage) packages.add(layout.workspacePackage);
2914
+ for (const { app } of clients) {
2915
+ const backend = apps[app];
2916
+ const name = packageName(isAbsolute(backend.path) ? backend.path : join(workspaceRoot, backend.path));
2917
+ if (name) packages.add(name);
2918
+ }
2919
+ packages.delete(turboPackage);
2920
+ return {
2921
+ gkmPaths: layout.gkmPaths,
2922
+ ...packages.size > 0 ? { prunePackages: [...packages].sort() } : {},
2923
+ ...clients.length > 0 ? { clients: clients.map(({ app }) => ({
2924
+ app,
2925
+ path: fromBuildRoot(layout, apps[app].path),
2926
+ ...clientTelemetry ? { telemetry: clientTelemetry } : {}
2927
+ })) } : {}
2928
+ };
2929
+ }
2930
+ /**
2931
+ * The backends whose clients a site imports: each it depends on that gkm
2932
+ * builds from endpoints — not one with an entry of its own, and not one that
2933
+ * turned OpenAPI off.
2934
+ */
2935
+ function siteClients(site, apps) {
2936
+ return site.dependencies.filter((name) => {
2937
+ const app = apps[name];
2938
+ if (!app || app.type !== "backend" || app.entry) return false;
2939
+ if (app.openapi === false) return false;
2940
+ if (typeof app.openapi === "object" && app.openapi.enabled === false) return false;
2941
+ return true;
2942
+ }).sort().map((app) => ({ app }));
2943
+ }
2944
+ /**
2945
+ * The directories the workspace's construct globs start in, relative to its
2946
+ * root: `./constructs/**\/*.ts` reads from `constructs`.
2947
+ */
2948
+ function constructRoots(workspace) {
2949
+ const roots = /* @__PURE__ */ new Set();
2950
+ for (const glob of allConstructGlobs(workspace)) {
2951
+ const rel = toPosix(relative(workspace.root, glob));
2952
+ if (!rel || rel.startsWith("..")) continue;
2953
+ const first = rel.split("/")[0];
2954
+ if (/[*?{}[\]!]/.test(first)) continue;
2955
+ roots.add(first);
2956
+ }
2957
+ return [...roots].sort();
2958
+ }
2959
+ function packageName(dir) {
2960
+ try {
2961
+ const pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf-8"));
2962
+ return typeof pkg.name === "string" ? pkg.name : void 0;
2963
+ } catch {
2964
+ return;
2965
+ }
2966
+ }
2967
+ function toPosix(path) {
2968
+ return path.split(sep).join("/");
2969
+ }
2970
+ //#endregion
2971
+ //#region src/reconcile/clients.ts
2972
+ /**
2973
+ * The real Postgres and MinIO clients the applier runs against.
2974
+ *
2975
+ * Kept apart from `provision.ts` so the rules there — what gets created, in what
2976
+ * order, and never dropping anything — are asserted against fakes, while the
2977
+ * drivers live here where there is nothing to assert but wiring.
2978
+ *
2979
+ * Both are constructed from the same local addresses the env writer composes,
2980
+ * so the applier cannot end up talking to a different container than the app.
2981
+ */
2982
+ /**
2983
+ * A Postgres client that opens one connection per database it is asked about.
2984
+ *
2985
+ * `CREATE DATABASE` cannot run from a connection to the database being created,
2986
+ * and a schema must be created from inside its own database — so the database is
2987
+ * part of each query rather than fixed when the client is built.
2988
+ */
2989
+ function pgClient(port, login) {
2990
+ return { async query(database, sql, values) {
2991
+ const client = new Client({
2992
+ host: "localhost",
2993
+ port,
2994
+ user: login.user,
2995
+ password: login.password,
2996
+ database: database ?? "postgres"
2997
+ });
2998
+ await client.connect();
2999
+ try {
3000
+ return (await client.query(sql, values)).rows;
3001
+ } finally {
3002
+ await client.end();
3003
+ }
3004
+ } };
3005
+ }
3006
+ /** A bucket client pointed at a MinIO on this machine. */
3007
+ function bucketClient(port, credentials) {
3008
+ const s3 = new S3Client({
3009
+ region: "us-east-1",
3010
+ endpoint: `http://localhost:${port}`,
3011
+ forcePathStyle: true,
3012
+ credentials: {
3013
+ accessKeyId: credentials.user,
3014
+ secretAccessKey: credentials.password
3015
+ }
3016
+ });
3017
+ return {
3018
+ async exists(bucket) {
3019
+ try {
3020
+ await s3.send(new HeadBucketCommand({ Bucket: bucket }));
3021
+ return true;
3022
+ } catch {
3023
+ return false;
3024
+ }
3025
+ },
3026
+ async create(bucket) {
3027
+ await s3.send(new CreateBucketCommand({ Bucket: bucket }));
3028
+ },
3029
+ async policy(bucket) {
3030
+ try {
3031
+ return (await s3.send(new GetBucketPolicyCommand({ Bucket: bucket }))).Policy;
3032
+ } catch {
3033
+ return;
3034
+ }
3035
+ },
3036
+ async setPolicy(bucket, policy) {
3037
+ await s3.send(new PutBucketPolicyCommand({
3038
+ Bucket: bucket,
3039
+ Policy: policy
3040
+ }));
3041
+ },
3042
+ async lifecycle(bucket) {
3043
+ try {
3044
+ return (await s3.send(new GetBucketLifecycleConfigurationCommand({ Bucket: bucket }))).Rules;
3045
+ } catch (error) {
3046
+ if (error.name === "NoSuchLifecycleConfiguration") return;
3047
+ throw error;
3048
+ }
3049
+ },
3050
+ async setLifecycle(bucket, rules) {
3051
+ if (rules.length === 0) {
3052
+ await s3.send(new DeleteBucketLifecycleCommand({ Bucket: bucket }));
3053
+ return;
3054
+ }
3055
+ await s3.send(new PutBucketLifecycleConfigurationCommand({
3056
+ Bucket: bucket,
3057
+ LifecycleConfiguration: { Rules: rules }
3058
+ }));
3059
+ },
3060
+ async notification(bucket) {
3061
+ const { $metadata: _, ...notification } = await s3.send(new GetBucketNotificationConfigurationCommand({ Bucket: bucket }));
3062
+ return notification;
3063
+ },
3064
+ async setNotification(bucket, notification) {
3065
+ await s3.send(new PutBucketNotificationConfigurationCommand({
3066
+ Bucket: bucket,
3067
+ NotificationConfiguration: notification
3068
+ }));
3069
+ }
3070
+ };
3071
+ }
3072
+ /** A topic and queue client pointed at the local AWS emulator. */
3073
+ function carrierClient(port, credentials) {
3074
+ const config = {
3075
+ region: EMULATOR_REGION,
3076
+ endpoint: emulatorEndpoint(port),
3077
+ credentials
3078
+ };
3079
+ const sns = new SNSClient(config);
3080
+ const sqs = new SQSClient(config);
3081
+ return {
3082
+ async topicExists(name) {
3083
+ try {
3084
+ await sns.send(new GetTopicAttributesCommand({ TopicArn: emulatorTopicArn(name) }));
3085
+ return true;
3086
+ } catch {
3087
+ return false;
3088
+ }
3089
+ },
3090
+ async createTopic(name) {
3091
+ await sns.send(new CreateTopicCommand({ Name: name }));
3092
+ },
3093
+ async queueExists(name) {
3094
+ try {
3095
+ await sqs.send(new GetQueueUrlCommand({ QueueName: name }));
3096
+ return true;
3097
+ } catch {
3098
+ return false;
3099
+ }
3100
+ },
3101
+ async createQueue(name) {
3102
+ await sqs.send(new CreateQueueCommand({ QueueName: name }));
3103
+ }
3104
+ };
3105
+ }
3106
+ //#endregion
3107
+ //#region src/reconcile/docker.ts
3108
+ /**
3109
+ * The real Docker, behind the small interface reconcile needs.
3110
+ *
3111
+ * Shells out to `docker compose` rather than talking to the daemon socket: it is
3112
+ * what a developer runs by hand when something is wrong, so the two cannot
3113
+ * disagree, and it keeps Docker out of this package's dependencies.
3114
+ *
3115
+ * Every call that acts is scoped to the generated compose file. Nothing here
3116
+ * can touch a container this project did not generate; `publisher` only reads
3117
+ * who holds a port.
3118
+ */
3119
+ const run = promisify(execFile);
3120
+ /**
3121
+ * The generated file, then the project's own if it has one.
3122
+ *
3123
+ * Merged in that order so the project's wins — an `image:` there pins a
3124
+ * container this file derived, and a service there is added beside them.
3125
+ */
3126
+ function composeFiles(composePath) {
3127
+ const project = join(dirname(composePath), "docker-compose.yml");
3128
+ return [
3129
+ "-f",
3130
+ composePath,
3131
+ ...existsSync(project) ? ["-f", project] : []
3132
+ ];
3133
+ }
3134
+ /** How long to wait for containers to pass their health checks. */
3135
+ const HEALTH_TIMEOUT_MS = 12e4;
3136
+ /** `docker compose up` exited unsuccessfully; its own output said why. */
3137
+ var ComposeUpFailed = class extends Error {
3138
+ services;
3139
+ code;
3140
+ constructor(services, code) {
3141
+ super(`docker compose could not start ${services.join(", ")} (exit ${code ?? "signal"}). Its output above names the container; \`docker compose logs <service>\` shows why it stopped.`);
3142
+ this.services = services;
3143
+ this.code = code;
3144
+ this.name = "ComposeUpFailed";
3145
+ }
3146
+ };
3147
+ /** Run compose with its output on this terminal rather than captured. */
3148
+ function shown(args, services) {
3149
+ return new Promise((resolve, reject) => {
3150
+ const child = spawn("docker", args, { stdio: [
3151
+ "ignore",
3152
+ "inherit",
3153
+ "inherit"
3154
+ ] });
3155
+ child.on("error", reject);
3156
+ child.on("close", (code) => code === 0 ? resolve() : reject(new ComposeUpFailed(services, code)));
3157
+ });
3158
+ }
3159
+ const dockerCli = {
3160
+ async publishedPort(composePath, service, inside) {
3161
+ try {
3162
+ const { stdout } = await run("docker", [
3163
+ "compose",
3164
+ ...composeFiles(composePath),
3165
+ "port",
3166
+ service,
3167
+ String(inside)
3168
+ ]);
3169
+ const port = Number(stdout.trim().split(":").pop());
3170
+ return Number.isFinite(port) && port > 0 ? port : void 0;
3171
+ } catch {
3172
+ return;
3173
+ }
3174
+ },
3175
+ async publisher(port) {
3176
+ try {
3177
+ const { stdout } = await run("docker", [
3178
+ "ps",
3179
+ "--filter",
3180
+ `publish=${port}`,
3181
+ "--format",
3182
+ "{{.Label \"com.docker.compose.project\"}}"
3183
+ ]);
3184
+ return stdout.trim().split("\n")[0] || void 0;
3185
+ } catch {
3186
+ return;
3187
+ }
3188
+ },
3189
+ async up(composePath, services, options = {}) {
3190
+ const args = [
3191
+ "compose",
3192
+ ...composeFiles(composePath),
3193
+ "up",
3194
+ "-d",
3195
+ "--remove-orphans",
3196
+ "--wait",
3197
+ `--wait-timeout=${Math.floor(HEALTH_TIMEOUT_MS / 1e3)}`,
3198
+ ...services
3199
+ ];
3200
+ if (options.show) return shown(args, services);
3201
+ await run("docker", args);
3202
+ },
3203
+ async copyOut(composePath, service, from, to) {
3204
+ await mkdir(dirname(to), { recursive: true });
3205
+ await run("docker", [
3206
+ "compose",
3207
+ ...composeFiles(composePath),
3208
+ "cp",
3209
+ `${service}:${from}`,
3210
+ to
3211
+ ]);
3212
+ },
3213
+ async reload(composePath, service) {
3214
+ await run("docker", [
3215
+ "compose",
3216
+ ...composeFiles(composePath),
3217
+ "exec",
3218
+ "-T",
3219
+ service,
3220
+ "caddy",
3221
+ "reload",
3222
+ "--config",
3223
+ "/etc/caddy/Caddyfile"
3224
+ ]);
3225
+ },
3226
+ async exec(composePath, service, argv) {
3227
+ try {
3228
+ const { stdout, stderr } = await run("docker", [
3229
+ "compose",
3230
+ ...composeFiles(composePath),
3231
+ "exec",
3232
+ "-T",
3233
+ service,
3234
+ ...argv
3235
+ ]);
3236
+ return {
3237
+ ok: true,
3238
+ output: `${stdout}${stderr}`
3239
+ };
3240
+ } catch (error) {
3241
+ const { stdout = "", stderr = "" } = error;
3242
+ return {
3243
+ ok: false,
3244
+ output: `${stdout}${stderr}`
3245
+ };
3246
+ }
3247
+ },
3248
+ async healthy(composePath, services, { mounted = [] } = {}) {
3249
+ if (services.length === 0) return true;
3250
+ const checkout = dirname(composePath);
3251
+ try {
3252
+ const { stdout } = await run("docker", [
3253
+ "compose",
3254
+ ...composeFiles(composePath),
3255
+ "ps",
3256
+ "--format",
3257
+ "json",
3258
+ ...services
3259
+ ]);
3260
+ const running = /* @__PURE__ */ new Set();
3261
+ for (const line of stdout.trim().split("\n").filter(Boolean)) {
3262
+ const entry = JSON.parse(line);
3263
+ const ok = entry.State === "running" && (entry.Health === void 0 || entry.Health === "" || entry.Health === "healthy");
3264
+ const fromHere = !entry.Service || !mounted.includes(entry.Service) || label(entry.Labels, "com.docker.compose.project.working_dir") === checkout;
3265
+ if (ok && fromHere && entry.Service) running.add(entry.Service);
3266
+ }
3267
+ return services.every((service) => running.has(service));
3268
+ } catch {
3269
+ return false;
3270
+ }
3271
+ }
3272
+ };
3273
+ /** One label's value out of `docker compose ps`'s comma-joined `Labels`. */
3274
+ function label(labels, name) {
3275
+ const prefix = `${name}=`;
3276
+ return labels?.split(",").find((pair) => pair.startsWith(prefix))?.slice(prefix.length);
3277
+ }
3278
+ //#endregion
3279
+ //#region src/reconcile/lan.ts
3280
+ /**
3281
+ * This machine's address on the local network — what a phone running the app
3282
+ * reaches it on.
3283
+ */
3284
+ /**
3285
+ * The first private IPv4 address of an interface that is up and not loopback.
3286
+ *
3287
+ * Private ranges only (`10/8`, `172.16/12`, `192.168/16`): a public address
3288
+ * would mean listening for a phone on the open internet, and a link-local one
3289
+ * (`169.254/16`) is what an interface has when it found no network at all.
3290
+ * Undefined when there is none — a laptop on no network still runs, its mobile
3291
+ * app reaching only a simulator.
3292
+ */
3293
+ function lanAddress(interfaces = networkInterfaces()) {
3294
+ for (const entries of Object.values(interfaces)) for (const entry of entries ?? []) {
3295
+ if (entry.family !== "IPv4" || entry.internal) continue;
3296
+ if (isPrivate(entry.address)) return entry.address;
3297
+ }
3298
+ }
3299
+ function isPrivate(address) {
3300
+ return /^10\./.test(address) || /^192\.168\./.test(address) || /^172\.(1[6-9]|2\d|3[01])\./.test(address);
3301
+ }
3302
+ //#endregion
3303
+ //#region src/reconcile/logins.ts
3304
+ /**
3305
+ * Bringing a container that already has data in line with the workspace's
3306
+ * generated logins.
3307
+ *
3308
+ * Postgres and RabbitMQ read their login from the environment once, when the
3309
+ * volume is first initialised; a volume an older gkm made was initialised
3310
+ * with the old fixed one, and a changed environment alone would lock the
3311
+ * developer out of their own data. So after the containers are up, each one
3312
+ * is asked whether the generated login opens it, and if not:
3313
+ *
3314
+ * - **Postgres**: signed in to with the old login, the generated superuser is
3315
+ * created (or given its password), and the old one stops logging in. Where
3316
+ * no login is accepted — another checkout of the same project rotated it —
3317
+ * the password is set from inside the container, which trusts its own
3318
+ * socket.
3319
+ * - **MinIO** reads its root login on every start, so recreating the
3320
+ * container rotated it already. If the old login is still the one it takes —
3321
+ * a project's own `docker-compose.yml` pins it — that is kept, with a note.
3322
+ * - **RabbitMQ**: the user is added, or given its password, with `rabbitmqctl`
3323
+ * inside the container.
3324
+ *
3325
+ * Nothing is deleted: no volume, no database, no bucket.
3326
+ */
3327
+ /**
3328
+ * The login every local container was brought up with before logins were
3329
+ * generated. Kept only to recognise — and rotate away from — a volume made
3330
+ * then.
3331
+ */
3332
+ const LEGACY_LOCAL_LOGIN = Object.freeze({
3333
+ user: "geekmidas",
3334
+ password: "geekmidas"
3335
+ });
3336
+ /** No login this workspace knows opens its Postgres. */
3337
+ var PostgresLoginRefused = class extends GkmError {
3338
+ port;
3339
+ user;
3340
+ constructor(port, user) {
3341
+ super(`The Postgres on port ${port} accepts neither '${user}' with this workspace's generated password nor the login older gkm versions used, and could not be reset from inside its container. If it is not this project's container, stop whatever holds the port. If it is, removing its volume starts it over with the generated login — and deletes its data: docker compose -f docker-compose.constructs.yml down -v`);
3342
+ this.port = port;
3343
+ this.user = user;
3344
+ this.name = "PostgresLoginRefused";
3345
+ }
3346
+ };
3347
+ /** No login this workspace knows opens its MinIO. */
3348
+ var MinioLoginRefused = class extends GkmError {
3349
+ port;
3350
+ user;
3351
+ constructor(port, user) {
3352
+ super(`The MinIO on port ${port} accepts neither '${user}' with this workspace's generated password nor the login older gkm versions used. If your docker-compose.yml sets MINIO_ROOT_USER or MINIO_ROOT_PASSWORD, remove them so the generated login applies.`);
3353
+ this.port = port;
3354
+ this.user = user;
3355
+ this.name = "MinioLoginRefused";
3356
+ }
3357
+ };
3358
+ /** Whether `login` opens the Postgres on `port`, as a connected client. */
3359
+ async function connect$1(port, login) {
3360
+ const client = new Client({
3361
+ host: "localhost",
3362
+ port,
3363
+ user: login.user,
3364
+ password: login.password,
3365
+ database: CLUSTER_DATABASE
3366
+ });
3367
+ try {
3368
+ await client.connect();
3369
+ return client;
3370
+ } catch (error) {
3371
+ await client.end().catch(() => {});
3372
+ const code = error.code;
3373
+ if (code === "28P01" || code === "28000" || code === "3D000") return null;
3374
+ throw error;
3375
+ }
3376
+ }
3377
+ /** Quote a string literal for DDL that cannot take parameters. */
3378
+ function literal(value) {
3379
+ return `'${value.replace(/'/g, "''")}'`;
3380
+ }
3381
+ function ident(name) {
3382
+ return `"${name.replace(/"/g, "\"\"")}"`;
3383
+ }
3384
+ /** The SQL that makes `login` a superuser with its password, new or not. */
3385
+ function superuserSql(login) {
3386
+ return `DO $$ BEGIN IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = ${literal(login.user)}) THEN ALTER ROLE ${ident(login.user)} WITH SUPERUSER LOGIN PASSWORD ${literal(login.password)}; ELSE CREATE ROLE ${ident(login.user)} WITH SUPERUSER LOGIN PASSWORD ${literal(login.password)}; END IF; END \$\$`;
3387
+ }
3388
+ /**
3389
+ * Make the Postgres on `port` take `login`, rotating it from an older one
3390
+ * where that is what it takes.
3391
+ */
3392
+ async function ensurePostgresLogin(options) {
3393
+ const { port, login, legacy } = options;
3394
+ const current = await connect$1(port, login);
3395
+ if (current) {
3396
+ await current.end();
3397
+ return {
3398
+ service: "postgres",
3399
+ status: "current",
3400
+ login
3401
+ };
3402
+ }
3403
+ for (const old of legacy) {
3404
+ const client = await connect$1(port, old);
3405
+ if (!client) continue;
3406
+ try {
3407
+ await client.query(superuserSql(login));
3408
+ } catch {
3409
+ await client.end();
3410
+ return {
3411
+ service: "postgres",
3412
+ status: "kept",
3413
+ login: old,
3414
+ notice: `⚠️ Postgres still signs in as '${old.user}' with the old fixed password: it could not be rotated. To start over with a generated one, remove its volume (this deletes its data): docker compose -f docker-compose.constructs.yml down -v`
3415
+ };
3416
+ }
3417
+ await client.end();
3418
+ await retire(port, login, old);
3419
+ return {
3420
+ service: "postgres",
3421
+ status: "rotated",
3422
+ login,
3423
+ notice: `🔑 Postgres moved from the old fixed login to a generated one ('${login.user}'); its data is unchanged. See it with: gkm dev:credentials`
3424
+ };
3425
+ }
3426
+ if (options.exec) for (const user of [login.user, ...legacy.map((l) => l.user)]) {
3427
+ if (!(await options.exec("postgres", [
3428
+ "psql",
3429
+ "-v",
3430
+ "ON_ERROR_STOP=1",
3431
+ "-U",
3432
+ user,
3433
+ "-d",
3434
+ "postgres",
3435
+ "-c",
3436
+ superuserSql(login)
3437
+ ])).ok) continue;
3438
+ const client = await connect$1(port, login);
3439
+ if (!client) continue;
3440
+ await client.end();
3441
+ for (const old of legacy) await retire(port, login, old);
3442
+ return {
3443
+ service: "postgres",
3444
+ status: "rotated",
3445
+ login,
3446
+ notice: `🔑 Postgres's login was reset to this workspace's generated one ('${login.user}'); its data is unchanged.`
3447
+ };
3448
+ }
3449
+ throw new PostgresLoginRefused(port, login.user);
3450
+ }
3451
+ /** Stop an older login from opening the cluster, now another does. */
3452
+ async function retire(port, login, old) {
3453
+ if (old.user === login.user) return;
3454
+ const client = await connect$1(port, login);
3455
+ if (!client) return;
3456
+ try {
3457
+ if ((await client.query("SELECT 1 FROM pg_roles WHERE rolname = $1", [old.user])).rowCount) await client.query(`ALTER ROLE ${ident(old.user)} WITH NOLOGIN PASSWORD NULL`);
3458
+ } finally {
3459
+ await client.end();
3460
+ }
3461
+ }
3462
+ /** Whether `login` signs in to the MinIO on `port`. */
3463
+ async function minioAccepts(port, login) {
3464
+ const s3 = new S3Client({
3465
+ region: "us-east-1",
3466
+ endpoint: `http://localhost:${port}`,
3467
+ forcePathStyle: true,
3468
+ credentials: {
3469
+ accessKeyId: login.user,
3470
+ secretAccessKey: login.password
3471
+ },
3472
+ maxAttempts: 1
3473
+ });
3474
+ try {
3475
+ await s3.send(new ListBucketsCommand({}));
3476
+ return true;
3477
+ } catch (error) {
3478
+ const name = error.name ?? "";
3479
+ if (name === "InvalidAccessKeyId" || name === "SignatureDoesNotMatch" || name === "AccessDenied") return false;
3480
+ throw error;
3481
+ } finally {
3482
+ s3.destroy();
3483
+ }
3484
+ }
3485
+ /**
3486
+ * Check the MinIO on `port` takes `login`. It reads its root login on every
3487
+ * start, so a recreated container already does — unless something pins an
3488
+ * older one, which is then kept.
3489
+ */
3490
+ async function ensureMinioLogin(options) {
3491
+ const { port, login, legacy } = options;
3492
+ if (await minioAccepts(port, login)) return {
3493
+ service: "minio",
3494
+ status: "current",
3495
+ login
3496
+ };
3497
+ for (const old of legacy) if (await minioAccepts(port, old)) return {
3498
+ service: "minio",
3499
+ status: "kept",
3500
+ login: old,
3501
+ notice: `⚠️ MinIO still signs in as '${old.user}' with the old fixed password: something — usually your docker-compose.yml's MINIO_ROOT_USER/MINIO_ROOT_PASSWORD — pins it. Remove that to use the generated login.`
3502
+ };
3503
+ throw new MinioLoginRefused(port, login.user);
3504
+ }
3505
+ /**
3506
+ * Make the RabbitMQ in the project's container take `login`, with
3507
+ * `rabbitmqctl` inside it — which needs no password.
3508
+ */
3509
+ async function ensureRabbitmqLogin(options) {
3510
+ const { login, legacy, exec } = options;
3511
+ const ctl = (...args) => exec("rabbitmq", ["rabbitmqctl", ...args]);
3512
+ if ((await ctl("authenticate_user", login.user, login.password)).ok) return {
3513
+ service: "rabbitmq",
3514
+ status: "current",
3515
+ login
3516
+ };
3517
+ if (!(await ctl("add_user", login.user, login.password)).ok) await ctl("change_password", login.user, login.password);
3518
+ await ctl("set_user_tags", login.user, "administrator");
3519
+ await ctl("set_permissions", "-p", "/", login.user, ".*", ".*", ".*");
3520
+ for (const old of legacy) if (old.user !== login.user) await ctl("delete_user", old.user);
3521
+ return {
3522
+ service: "rabbitmq",
3523
+ status: "rotated",
3524
+ login,
3525
+ notice: `🔑 RabbitMQ moved from the old fixed login to a generated one ('${login.user}'); its queues are unchanged.`
3526
+ };
3527
+ }
3528
+ /**
3529
+ * Check the login of each running container that keeps one, and the
3530
+ * credentials to use from here: the generated ones, or an older login a
3531
+ * service could not be moved off.
3532
+ */
3533
+ async function ensureLogins(options) {
3534
+ const { containers, ports, exec } = options;
3535
+ let credentials = options.credentials;
3536
+ const outcomes = [];
3537
+ const legacy = [LEGACY_LOCAL_LOGIN];
3538
+ if (containers.includes("postgres") && ports.postgres !== void 0) {
3539
+ const outcome = await ensurePostgresLogin({
3540
+ port: ports.postgres,
3541
+ login: credentials.postgres,
3542
+ legacy,
3543
+ ...exec ? { exec } : {}
3544
+ });
3545
+ outcomes.push(outcome);
3546
+ credentials = {
3547
+ ...credentials,
3548
+ postgres: outcome.login
3549
+ };
3550
+ }
3551
+ if (containers.includes("minio") && ports.minio !== void 0) {
3552
+ const outcome = await ensureMinioLogin({
3553
+ port: ports.minio,
3554
+ login: credentials.minio,
3555
+ legacy
3556
+ });
3557
+ outcomes.push(outcome);
3558
+ credentials = {
3559
+ ...credentials,
3560
+ minio: outcome.login
3561
+ };
3562
+ }
3563
+ if (containers.includes("rabbitmq") && exec) outcomes.push(await ensureRabbitmqLogin({
3564
+ login: credentials.rabbitmq,
3565
+ legacy,
3566
+ exec
3567
+ }));
3568
+ return {
3569
+ credentials,
3570
+ outcomes
3571
+ };
3572
+ }
3573
+ //#endregion
3574
+ //#region src/reconcile/ports.ts
3575
+ /**
3576
+ * Port allocation — why two projects can run at once.
3577
+ *
3578
+ * Publishing `5432:5432` means the second project on your machine cannot start.
3579
+ * Nothing solves that except giving each project its own ports, and what makes
3580
+ * it *possible* is the single-`<NAME>_URL` rule: an app reads `ORDERS_URL` and
3581
+ * never sees a host or a port, so reconcile is free to bind whatever is
3582
+ * available and compose the URL from what it bound. An app reading `DB_HOST` and
3583
+ * `DB_PORT` separately could not be moved this way.
3584
+ *
3585
+ * Assignments are **persisted, not recomputed**. Recomputing means adding a
3586
+ * construct renumbers its neighbours, and every running container, saved
3587
+ * connection string, and `psql` history goes stale at once.
3588
+ *
3589
+ * Pure, with the liveness probe injected — a collision with another project's
3590
+ * stack is a fact about the machine, and keeping it a parameter is what lets the
3591
+ * rules be asserted without binding a socket.
3592
+ */
3593
+ /**
3594
+ * The window ports are drawn from.
3595
+ *
3596
+ * Above the registered range so nothing here needs privileges, and below the
3597
+ * ephemeral range both Linux (32768+) and macOS (49152+) allocate from — a port
3598
+ * the kernel might hand to an outgoing connection is one that is free when
3599
+ * checked and taken by the time it is bound.
3600
+ */
3601
+ const WINDOW = {
3602
+ min: 2e4,
3603
+ max: 32e3
3604
+ };
3605
+ /**
3606
+ * How far apart two projects start.
3607
+ *
3608
+ * Wide enough that a project can add containers without walking into its
3609
+ * neighbour's block, narrow enough that the window holds many projects.
3610
+ */
3611
+ const BLOCK = 50;
3612
+ /**
3613
+ * Where a project's ports start.
3614
+ *
3615
+ * Derived from the name rather than assigned sequentially so that a fresh clone
3616
+ * of the same project on the same machine lands on the same block — the first
3617
+ * run has nothing persisted to read, and hashing is what keeps that first run
3618
+ * from colliding with whatever else is already allocated.
3619
+ *
3620
+ * @example startingPort('toolbox') // stable for a given name
3621
+ */
3622
+ function startingPort(project) {
3623
+ const digest = createHash("sha256").update(project).digest();
3624
+ const blocks = Math.floor((WINDOW.max - WINDOW.min) / BLOCK);
3625
+ return WINDOW.min + digest.readUInt32BE(0) % blocks * BLOCK;
3626
+ }
3627
+ /**
3628
+ * Assign a port to every container that has none, keeping the ones that do.
3629
+ *
3630
+ * Existing assignments are returned untouched even when the container is no
3631
+ * longer in the plan: removing a construct must not renumber what is left, and
3632
+ * keeping the entry means re-adding it later restores the same port rather than
3633
+ * inventing a new one.
3634
+ *
3635
+ * @param probe - reports what is actually listening. A port assigned to this
3636
+ * project is skipped without probing, because on a converged reconcile our own
3637
+ * container is the thing listening on it.
3638
+ * @throws {NoPortAvailable} when the window is exhausted.
3639
+ */
3640
+ async function allocate(project, containers, existing = {}, probe = isPortFree) {
3641
+ const assignments = { ...existing };
3642
+ const taken = new Set(Object.values(assignments));
3643
+ for (const container of [...containers].sort()) {
3644
+ if (assignments[container] !== void 0) continue;
3645
+ const port = await nextFree(startingPort(project), taken, probe);
3646
+ assignments[container] = port;
3647
+ taken.add(port);
3648
+ }
3649
+ return assignments;
3650
+ }
3651
+ /**
3652
+ * The ports to keep: what running containers are observed on, then what was
3653
+ * saved — minus any saved entry whose port something else already holds.
3654
+ *
3655
+ * Spreading one over the other let an observed port override its own key and
3656
+ * leave a *different* key still saved on the same number: two services on one
3657
+ * port, and whichever answered first won. Saved keys for containers the plan no
3658
+ * longer has — a backend switched and back — were the usual source. Observed
3659
+ * wins because it is what is actually bound; a dropped entry is simply
3660
+ * allocated again.
3661
+ */
3662
+ function keptPorts(saved = {}, observed = {}) {
3663
+ const kept = { ...observed };
3664
+ const taken = new Set(Object.values(observed));
3665
+ for (const [key, port] of Object.entries(saved).sort(([a], [b]) => a < b ? -1 : 1)) {
3666
+ if (key in kept || taken.has(port)) continue;
3667
+ kept[key] = port;
3668
+ taken.add(port);
3669
+ }
3670
+ return kept;
3671
+ }
3672
+ /**
3673
+ * The kept ports, among those the plan publishes, that something other than
3674
+ * this project now holds.
3675
+ *
3676
+ * A saved port is only a memory of a free one. Another checkout's stack — the
3677
+ * same project name hashes to the same block — can bind it in the meantime,
3678
+ * and keeping it unchecked fails `docker compose up` with "port is already
3679
+ * allocated". An observed port is ours by definition and is never asked
3680
+ * about; neither is a saved one the plan no longer publishes, which is only
3681
+ * kept for when it returns.
3682
+ */
3683
+ async function heldElsewhere(kept, keys, observed, probe, ours) {
3684
+ const held = [];
3685
+ for (const key of [...keys].sort()) {
3686
+ const port = kept[key];
3687
+ if (port === void 0 || key in observed) continue;
3688
+ if (await probe(port)) continue;
3689
+ if (await ours(port)) continue;
3690
+ held.push(key);
3691
+ }
3692
+ return held;
3693
+ }
3694
+ /** The window was exhausted. */
3695
+ var NoPortAvailable = class extends GkmError {
3696
+ /** Where the search started. */
3697
+ from;
3698
+ /** The window searched, so the caller can report what was full. */
3699
+ window = WINDOW;
3700
+ constructor(from) {
3701
+ super("No free port is available in the local port range");
3702
+ this.name = "NoPortAvailable";
3703
+ this.from = from;
3704
+ }
3705
+ };
3706
+ /**
3707
+ * The first port at or after `from` that is neither ours nor in use.
3708
+ *
3709
+ * Wraps at the top of the window rather than stopping, so a project whose block
3710
+ * sits near the end is not the one that fails.
3711
+ */
3712
+ async function nextFree(from, taken, probe) {
3713
+ const span = WINDOW.max - WINDOW.min;
3714
+ for (let offset = 0; offset < span; offset++) {
3715
+ const port = WINDOW.min + (from - WINDOW.min + offset) % span;
3716
+ if (taken.has(port)) continue;
3717
+ if (await probe(port)) return port;
3718
+ }
3719
+ throw new NoPortAvailable(from);
3720
+ }
3721
+ /**
3722
+ * Whether nothing is listening on a port.
3723
+ *
3724
+ * Binds rather than connects: a refused connection says nothing about whether
3725
+ * the port can be *published*, which is the question Docker will ask. Binds on
3726
+ * `0.0.0.0` for the same reason — a port free on loopback can still be taken on
3727
+ * the address Docker publishes to — and then on `127.0.0.1`, because on macOS
3728
+ * a wildcard bind succeeds beside a listener on loopback alone.
3729
+ *
3730
+ * Inherently racy, and that is acceptable: Docker failing to bind stays the real
3731
+ * check. This exists so the common case surfaces as a message at reconcile
3732
+ * rather than as a connection failure ten minutes later.
3733
+ */
3734
+ async function isPortFree(port) {
3735
+ return await canBind(port, "0.0.0.0") && await canBind(port, "127.0.0.1");
3736
+ }
3737
+ function canBind(port, host) {
3738
+ return new Promise((resolve) => {
3739
+ const server = createServer();
3740
+ server.once("error", () => resolve(false));
3741
+ server.once("listening", () => server.close(() => resolve(true)));
3742
+ server.listen(port, host);
3743
+ });
3744
+ }
3745
+ //#endregion
3746
+ //#region src/reconcile/state.ts
3747
+ /**
3748
+ * What reconcile records, and the hash that lets it do nothing.
3749
+ *
3750
+ * Reconciling on every `gkm dev` is only acceptable if the converged case is
3751
+ * free, so the desired plan is hashed: matching hash plus healthy containers
3752
+ * means there is nothing to do. Without that, the automatic reconcile the design
3753
+ * asks for would tax every start.
3754
+ *
3755
+ * Almost nothing needs recording — roles are queried, migrations have their own
3756
+ * table, containers are remembered by Docker. Only ports and this hash have no
3757
+ * other owner.
3758
+ */
3759
+ const STATE_DIR = ".gkm/reconcile";
3760
+ /**
3761
+ * The hash of everything a reconcile would act on.
3762
+ *
3763
+ * Covers the compose document rather than the plan alone, because the plan is
3764
+ * unchanged when only an image pin moves — and a changed image is exactly a
3765
+ * container that must be recreated. `stringify` is stable here because
3766
+ * `composeFor` sorts its containers, which is what makes the hash meaningful.
3767
+ *
3768
+ * Deliberately a plain function over serialisable input: the deployed target
3769
+ * keys its provisioner invocation on the same idea, and the two must not be able
3770
+ * to disagree about what counts as a change.
3771
+ */
3772
+ function planHash(plan, compose, generated = {}) {
3773
+ return createHash("sha256").update(JSON.stringify({
3774
+ stage: plan.stage,
3775
+ plan,
3776
+ compose,
3777
+ generated
3778
+ })).digest("hex");
3779
+ }
3780
+ /** Read the recorded state, or nothing if this stage has never reconciled. */
3781
+ async function loadState(root, stage) {
3782
+ try {
3783
+ const raw = await readFile(statePath(root, stage), "utf-8");
3784
+ const state = JSON.parse(raw);
3785
+ return state.stage === stage ? state : void 0;
3786
+ } catch {
3787
+ return;
3788
+ }
3789
+ }
3790
+ /** Record what was applied, so the next run can skip it. */
3791
+ async function saveState(root, state) {
3792
+ const path = statePath(root, state.stage);
3793
+ await mkdir(dirname(path), { recursive: true });
3794
+ await writeFile(path, `${JSON.stringify(state, null, 2)}\n`);
3795
+ }
3796
+ /**
3797
+ * Forget what was applied, so the next reconcile of this stage converges from
3798
+ * nothing — creating again whatever was dropped underneath the record.
3799
+ */
3800
+ async function forgetState(root, stage) {
3801
+ await rm(statePath(root, stage), { force: true });
3802
+ }
3803
+ function statePath(root, stage) {
3804
+ return join(root, STATE_DIR, `${stage}.json`);
3805
+ }
3806
+ //#endregion
3807
+ //#region src/reconcile/index.ts
3808
+ /**
3809
+ * Reconcile — the one convergent function `gkm dev`, `gkm test`, and `gkm setup`
3810
+ * all call.
3811
+ *
3812
+ * Three phases, and only this one has side effects: declaring is pure (`gkm
3813
+ * build` and the dev watcher), running reads `<NAME>_URL` and provisions
3814
+ * nothing. Reconcile computes the desired state, compares, and applies the
3815
+ * difference — which is what makes "when does it run" answerable with
3816
+ * *whenever, repeatedly*.
3817
+ *
3818
+ * It is safe to run automatically because its blast radius is entirely local:
3819
+ * this project's containers, this project's `.gkm/`. Nothing it does can reach a
3820
+ * cloud. The line is drawn at data, not at side effects — it allocates ports,
3821
+ * writes compose, starts containers, and creates roles; it never seeds, resets,
3822
+ * or drops.
3823
+ *
3824
+ * Docker is injected rather than imported so the rules above can be asserted
3825
+ * without a daemon. The default implementation is the real one.
3826
+ */
3827
+ /**
3828
+ * Where the generated compose file is written, relative to the project root.
3829
+ *
3830
+ * At the root, beside the project's own `docker-compose.yml`, because the two
3831
+ * are one stack: this file is everything the constructs imply and is never
3832
+ * edited, that one is anything they cannot express — an image pin, an extra
3833
+ * service — and is merged over it, so it wins.
3834
+ */
3835
+ const COMPOSE_PATH = "docker-compose.constructs.yml";
3836
+ /**
3837
+ * The local edge's config, beside the compose file that mounts it.
3838
+ *
3839
+ * Relative in the compose volume, so it resolves against `.gkm/` wherever the
3840
+ * project lives.
3841
+ */
3842
+ const CADDYFILE_PATH = ".gkm/Caddyfile";
3843
+ /**
3844
+ * The containers that mount this checkout's files: Caddy reads its routes out
3845
+ * of `.gkm/`, so one started from a worktree serves that worktree's ports.
3846
+ */
3847
+ const CHECKOUT_MOUNTS = ["caddy"];
3848
+ /**
3849
+ * Where one stage's routes live, imported by the file above.
3850
+ *
3851
+ * Per stage because one edge serves every stage, the same way one Postgres
3852
+ * holds `orders` and `orders_test`. A single file would mean `gkm test`
3853
+ * deleting the routes `gkm dev` is serving.
3854
+ */
3855
+ const caddySitesPath = (stage) => `.gkm/caddy-sites/${stage}.caddy`;
3856
+ /**
3857
+ * Where the local edge's root certificate is copied to.
3858
+ *
3859
+ * Exported so a process can be pointed at it without installing anything:
3860
+ * `NODE_EXTRA_CA_CERTS` is the whole of the trust story for Node, and it needs
3861
+ * no sudo. A browser is the case that still wants the root in the system store,
3862
+ * which is a one-time `caddy trust` rather than something a reconcile should do
3863
+ * on a developer's behalf.
3864
+ */
3865
+ const LOCAL_CA_PATH = ".gkm/caddy-root.crt";
3866
+ /** Where Caddy keeps the root of the CA it generated. */
3867
+ const CADDY_ROOT_IN_CONTAINER = "/data/caddy/pki/authorities/local/root.crt";
3868
+ /**
3869
+ * Converge the local target on what the manifest declares.
3870
+ *
3871
+ * Cheap when there is nothing to do: the hash is compared before the compose
3872
+ * file is written or a container is touched, and a converged project pays one
3873
+ * hash and one health check.
3874
+ */
3875
+ async function reconcile(options) {
3876
+ const { root, project, manifest, stage, start = true, provision = true, docker = dockerCli, probe = isPortFree, sql = pgClient, buckets = bucketClient, carriers = carrierClient, logins = ensureLogins } = options;
3877
+ const { seed } = options.credentials;
3878
+ const plan = planFor(manifest, stage, provisionOrder(manifest), {
3879
+ localStage: options.localStage,
3880
+ events: options.events,
3881
+ cache: options.cache,
3882
+ extraContainers: options.extraContainers,
3883
+ ...options.fakes ? { fakes: options.fakes } : {},
3884
+ ...options.credentialFakes ? { credentialFakes: options.credentialFakes } : {},
3885
+ ...options.edge === void 0 ? {} : { edge: options.edge },
3886
+ ...options.protectedSubdomains ? { protectedSubdomains: options.protectedSubdomains } : {}
3887
+ });
3888
+ const exports = stage !== TEST_STAGE;
3889
+ const running = exports ? plan.containers : plan.containers.filter((c) => c !== TELEMETRY_CONTAINER);
3890
+ const composePath = join(root, COMPOSE_PATH);
3891
+ const observed = await observedPorts(docker, composePath, plan.containers, plan.fakes);
3892
+ const keys = portKeys(plan.containers, plan.fakes);
3893
+ const kept = keptPorts(options.saved, observed);
3894
+ const composeProject = process.env.COMPOSE_PROJECT_NAME || project;
3895
+ const held = await heldElsewhere(kept, keys, observed, probe, async (port) => await docker.publisher?.(port) === composeProject);
3896
+ const ports = await allocate(project, keys, Object.fromEntries(Object.entries(kept).filter(([key]) => !held.includes(key))), probe);
3897
+ const moved = held.map((key) => ({
3898
+ key,
3899
+ from: kept[key],
3900
+ to: ports[key]
3901
+ }));
3902
+ const compose = composeFor(plan, {
3903
+ project,
3904
+ ports,
3905
+ credentials: options.credentials,
3906
+ ...options.apps ? { apps: options.apps(plan.containers) } : {},
3907
+ ...options.objectWebhooks?.length ? { objectWebhooks: options.objectWebhooks } : {},
3908
+ ...options.inboxRelay ? { inboxRelay: options.inboxRelay } : {}
3909
+ });
3910
+ const caddyfile = toCaddyfile(sitesFor(plan, project, options.addresses, options.processorAddresses));
3911
+ const hash = planHash(plan, compose, {
3912
+ caddyfile,
3913
+ postgres: postgresStatements(plan, project, seed).map((statement) => statement.create).join(";\n")
3914
+ });
3915
+ const services = serviceAddresses(running, ports, plan.fakes);
3916
+ const lan = plan.resources.some((r) => r.kind === "mobile-app") ? options.lanAddress === void 0 ? lanAddress() : options.lanAddress ?? void 0 : void 0;
3917
+ const envWith = (credentials) => {
3918
+ const resolved = envFor(plan, {
3919
+ ports,
3920
+ project,
3921
+ credentials,
3922
+ seed,
3923
+ ...options.mailFrom ? { mailFrom: options.mailFrom } : {},
3924
+ ...options.addresses ? { addresses: options.addresses } : {},
3925
+ ...lan ? { lanAddress: lan } : {},
3926
+ ...options.metroPorts ? { metroPorts: options.metroPorts } : {},
3927
+ ...options.objectWebhooks?.length ? { relayedBuckets: options.objectWebhooks.map((hook) => hook.bucket) } : {}
3928
+ });
3929
+ const env = exports ? resolved : scopeTelemetryEnv(resolved, {
3930
+ uses: false,
3931
+ serviceName: project
3932
+ });
3933
+ if (plan.containers.includes("caddy")) env.NODE_EXTRA_CA_CERTS = join(root, LOCAL_CA_PATH);
3934
+ return env;
3935
+ };
3936
+ const result = {
3937
+ stage,
3938
+ plan,
3939
+ compose,
3940
+ ports,
3941
+ moved,
3942
+ services,
3943
+ env: envWith(options.credentials),
3944
+ credentials: options.credentials,
3945
+ logins: [],
3946
+ provisioned: [],
3947
+ fakes: options.fakes ?? {},
3948
+ hash,
3949
+ changed: false
3950
+ };
3951
+ const recorded = await loadState(root, stage);
3952
+ const postgresPort = ports[primaryPortKey("postgres")];
3953
+ if (recorded?.hash === hash && existsSync(composePath) && (!start || await docker.healthy(composePath, running, { mounted: CHECKOUT_MOUNTS })) && (!start || !provision || postgresPort === void 0 || await databasesExist(sql(postgresPort, options.credentials.postgres), postgresDatabaseNames(plan)))) return result;
3954
+ await write(composePath, toYaml(compose), 384);
3955
+ if (plan.containers.includes("caddy")) {
3956
+ await write(join(root, CADDYFILE_PATH), caddyfileRoot());
3957
+ await write(join(root, caddySitesPath(stage)), caddyfile);
3958
+ await pruneCaddySites(root, [
3959
+ stage,
3960
+ options.localStage,
3961
+ TEST_STAGE
3962
+ ]);
3963
+ }
3964
+ if (start && running.length > 0) {
3965
+ options.progress?.(`🐳 Starting ${running.join(", ")} — the first start pulls each image, which can take a few minutes`);
3966
+ await docker.up(composePath, running, options.progress ? { show: true } : {});
3967
+ }
3968
+ if (start && plan.containers.includes("caddy")) await docker.reload(composePath, "caddy").catch(() => {});
3969
+ if (start && plan.containers.includes("caddy")) await docker.copyOut(composePath, "caddy", CADDY_ROOT_IN_CONTAINER, join(root, LOCAL_CA_PATH)).catch(() => {});
3970
+ const checked = start && plan.containers.length > 0 ? await logins({
3971
+ credentials: options.credentials,
3972
+ containers: plan.containers,
3973
+ ports: {
3974
+ ...postgresPort !== void 0 ? { postgres: postgresPort } : {},
3975
+ ...ports[primaryPortKey("minio")] !== void 0 ? { minio: ports[primaryPortKey("minio")] } : {}
3976
+ },
3977
+ ...docker.exec ? { exec: (service, argv) => docker.exec(composePath, service, argv) } : {}
3978
+ }) : {
3979
+ credentials: options.credentials,
3980
+ outcomes: []
3981
+ };
3982
+ const credentials = checked.credentials;
3983
+ if (start && provision) options.progress?.("🗄️ Creating what the constructs declare — databases, roles, buckets, topics");
3984
+ const provisioned = start && provision ? await create(plan, ports, credentials, sql, buckets, carriers, project, new Set(stage === (options.localStage ?? stage) ? (options.objectWebhooks ?? []).map((hook) => hook.bucket) : [])) : [];
3985
+ await saveState(root, {
3986
+ hash,
3987
+ stage
3988
+ });
3989
+ return {
3990
+ ...result,
3991
+ ...credentials === options.credentials ? {} : { env: envWith(credentials) },
3992
+ credentials,
3993
+ logins: checked.outcomes,
3994
+ provisioned,
3995
+ changed: true
3996
+ };
3997
+ }
3998
+ /**
3999
+ * Create the databases, schemas, buckets, topics and queues the plan names.
4000
+ *
4001
+ * Idempotent throughout, so this runs on every non-converged reconcile rather
4002
+ * than being something to remember.
4003
+ */
4004
+ async function create(plan, ports, credentials, sql, buckets, emulatorCarriers, project, watched) {
4005
+ const applied = [];
4006
+ const postgresPort = ports[primaryPortKey("postgres")];
4007
+ const statements = postgresStatements(plan, project, credentials.seed);
4008
+ if (postgresPort !== void 0 && statements.length > 0) applied.push(...await applyPostgres(sql(postgresPort, credentials.postgres), statements));
4009
+ const minioPort = ports[primaryPortKey("minio")];
4010
+ const names = bucketNames(plan);
4011
+ const policies = bucketPolicies(plan);
4012
+ const lifecycles = bucketLifecycles(plan);
4013
+ if (minioPort !== void 0 && (names.length > 0 || policies.length > 0)) {
4014
+ const client = buckets(minioPort, credentials.minio);
4015
+ if (names.length > 0) applied.push(...await applyBuckets(client, names));
4016
+ if (policies.length > 0) applied.push(...await applyPolicies(client, policies));
4017
+ if (lifecycles.length > 0) applied.push(...await applyLifecycles(client, lifecycles));
4018
+ if (names.length > 0) applied.push(...await applyNotifications(client, plan.resources.filter((r) => r.provisions && r.kind === "objects").map((r) => ({
4019
+ id: r.id,
4020
+ name: r.name
4021
+ })), watched));
4022
+ }
4023
+ const emulatorPort = ports[primaryPortKey("localstack")];
4024
+ const carriers = carrierNames(plan);
4025
+ if (emulatorPort !== void 0 && carriers.topics.length + carriers.queues.length > 0) applied.push(...await applyCarriers(emulatorCarriers(emulatorPort, credentials.emulator), carriers));
4026
+ return applied;
4027
+ }
4028
+ /**
4029
+ * The host ports of containers already running.
4030
+ *
4031
+ * Keyed the same way allocation keys them, so the two merge without either
4032
+ * knowing about the other.
4033
+ */
4034
+ async function observedPorts(docker, composePath, containers, fakes) {
4035
+ const observed = {};
4036
+ for (const container of containers) for (const port of portsOf(container, fakes)) {
4037
+ const running = await docker.publishedPort(composePath, container, port.inside);
4038
+ if (running !== void 0) observed[port.key] = running;
4039
+ }
4040
+ return observed;
4041
+ }
4042
+ /** Write a file only when its content would change, so mtimes stay meaningful. */
4043
+ /**
4044
+ * Remove the routes of stages this project no longer reconciles.
4045
+ *
4046
+ * Only the local stage and `test` are ever written, so any other file is left
4047
+ * over from a stage that was renamed — `development` becoming `dev`. Caddy
4048
+ * imports every file, and the leftover declares the same hosts as its
4049
+ * replacement: it refuses the whole config as ambiguous, and every URL behind
4050
+ * the edge goes with it.
4051
+ */
4052
+ async function pruneCaddySites(root, stages) {
4053
+ const dir = dirname(join(root, caddySitesPath(TEST_STAGE)));
4054
+ const keep = new Set(stages.map((stage) => stage && `${stage}.caddy`));
4055
+ for (const file of await readdir(dir)) if (file.endsWith(".caddy") && !keep.has(file)) await rm(join(dir, file));
4056
+ }
4057
+ async function write(path, content, mode) {
4058
+ try {
4059
+ if (await readFile(path, "utf-8") === content) {
4060
+ if (mode !== void 0) await chmod(path, mode);
4061
+ return;
4062
+ }
4063
+ } catch {}
4064
+ await mkdir(dirname(path), { recursive: true });
4065
+ await writeFile(path, content, mode === void 0 ? {} : { mode });
4066
+ if (mode !== void 0) await chmod(path, mode);
4067
+ }
4068
+ //#endregion
4069
+ //#region src/reconcile/workspace.ts
4070
+ /**
4071
+ * Workspace config → reconcile.
4072
+ *
4073
+ * Nothing here is read from a `services:` block — there is none. Which
4074
+ * containers exist comes from the manifest; which backend a cache or a broker
4075
+ * resolves to comes from the deploy target; and an image pin is the project's
4076
+ * own `docker-compose.yml`, merged over the generated file.
4077
+ *
4078
+ * Shared by `gkm setup`, `gkm dev`, and `gkm test` so all three converge on the
4079
+ * same state, differing only in the stage they pass.
4080
+ */
4081
+ var workspace_exports = /* @__PURE__ */ __exportAll({
4082
+ FAKE_ENV: () => FAKE_ENV,
4083
+ backendsOf: () => backendsOf,
4084
+ constructGlobs: () => constructGlobs,
4085
+ derivedContainers: () => derivedContainers,
4086
+ fakesApply: () => fakesApply,
4087
+ localInboxWebhook: () => localInboxWebhook,
4088
+ localObjectWebhooks: () => localObjectWebhooks,
4089
+ metroPorts: () => metroPorts,
4090
+ processorAddresses: () => processorAddresses,
4091
+ reconcileWorkspace: () => reconcileWorkspace,
4092
+ surfaceAddresses: () => surfaceAddresses
4093
+ });
4094
+ /**
4095
+ * Every constructs glob in the workspace, resolved against its app.
4096
+ *
4097
+ * Absolute because discovery globs from a single cwd while apps live in
4098
+ * different directories.
4099
+ */
4100
+ function constructGlobs(workspace) {
4101
+ return allConstructGlobs(workspace);
4102
+ }
4103
+ /** The backends a workspace's target implies, as the plan takes them. */
4104
+ function backendsOf(workspace) {
4105
+ const on = providerOf(workspace);
4106
+ return {
4107
+ events: eventsBackendFor(on),
4108
+ cache: cacheBackendFor(on)
4109
+ };
4110
+ }
4111
+ /**
4112
+ * The containers a workspace's declarations imply, without reconciling.
4113
+ *
4114
+ * The same derivation `reconcileWorkspace` makes, available to callers that
4115
+ * need to know *what would run* without starting anything or writing ports —
4116
+ * `gkm setup` deciding which credentials to generate, `gkm docker` writing a
4117
+ * compose file. Those used to each read a boolean per service out of config,
4118
+ * which is how a container could exist because config asked for one rather than
4119
+ * because something declared it.
4120
+ */
4121
+ async function derivedContainers(workspace, stage, manifest) {
4122
+ const found = manifest ?? await discover({
4123
+ patterns: constructGlobs(workspace),
4124
+ cwd: workspace.root
4125
+ });
4126
+ return planFor(found, stage, provisionOrder(found), {
4127
+ localStage: workspace.stages.local,
4128
+ ...backendsOf(workspace)
4129
+ }).containers;
4130
+ }
4131
+ /**
4132
+ * Set by `gkm dev --fake` for the apps it starts. Each app's own `gkm dev`
4133
+ * reconciles too, and must answer the way the workspace did.
4134
+ */
4135
+ const FAKE_ENV = "GKM_FAKE";
4136
+ /**
4137
+ * Whether third parties are faked — external APIs, and credentials with a
4138
+ * fake: always for `gkm test`, and for `gkm dev`
4139
+ * only when asked — by `--fake`, or by the workspace that started this app
4140
+ * with it. Never for anything acting on a deployed stage.
4141
+ */
4142
+ function fakesApply(stage, fake, env = process.env) {
4143
+ return fake === true || stage === "test" || env["GKM_FAKE"] === "1";
4144
+ }
4145
+ /**
4146
+ * Reconcile a workspace for one stage.
4147
+ *
4148
+ * Ports are loaded and saved around the call rather than inside it, so the loop
4149
+ * stays a pure-ish function of its inputs and the store keeps its single owner.
4150
+ */
4151
+ async function reconcileWorkspace(workspace, options) {
4152
+ const background = {};
4153
+ const subscribers = {};
4154
+ const discovered = await discover({
4155
+ patterns: constructGlobs(workspace),
4156
+ cwd: workspace.root,
4157
+ background,
4158
+ subscribers
4159
+ });
4160
+ const manifest = options.manifest ?? discovered;
4161
+ const faked = fakesApply(options.stage, options.fake);
4162
+ const fakes = faked ? await readFakes(workspace.root, manifest) : {};
4163
+ const credentialFakes = faked ? await readCredentialFakes(workspace.root, manifest) : {};
4164
+ const { credentials } = await loadLocalCredentials(workspace);
4165
+ const objectWebhooks = localObjectWebhooks(workspace, manifest, {
4166
+ background,
4167
+ subscribers,
4168
+ seed: credentials.seed
4169
+ });
4170
+ const inboxRelay = localInboxWebhook(workspace, manifest, {
4171
+ background,
4172
+ subscribers,
4173
+ seed: credentials.seed
4174
+ });
4175
+ const result = await reconcile({
4176
+ credentials,
4177
+ root: workspace.root,
4178
+ project: workspace.name,
4179
+ manifest,
4180
+ stage: options.stage,
4181
+ localStage: workspace.stages.local,
4182
+ ...backendsOf(workspace),
4183
+ ...workspace.protectedSubdomains ? { protectedSubdomains: workspace.protectedSubdomains } : {},
4184
+ saved: await loadPortState(workspace.root),
4185
+ addresses: surfaceAddresses(workspace, manifest),
4186
+ processorAddresses: processorAddresses(workspace, manifest),
4187
+ metroPorts: metroPorts(workspace, manifest),
4188
+ fakes,
4189
+ credentialFakes,
4190
+ ...objectWebhooks.length > 0 ? { objectWebhooks } : {},
4191
+ ...inboxRelay ? { inboxRelay } : {},
4192
+ apps: (containers) => appServices(workspace, manifest, containers, credentials, fakes, credentialFakes),
4193
+ ...options.start === void 0 ? {} : { start: options.start },
4194
+ ...options.progress ? { progress: options.progress } : {}
4195
+ });
4196
+ await savePortState(workspace.root, { ...result.ports });
4197
+ for (const { key, from, to } of result.moved) console.log(`⚠️ Port ${from} (${key}) is held by another process; moved to ${to}.`);
4198
+ if (result.credentials !== credentials) await saveLocalCredentials(workspace, result.credentials);
4199
+ for (const outcome of result.logins) if (outcome.notice) console.log(outcome.notice);
4200
+ return {
4201
+ ...result,
4202
+ manifest
4203
+ };
4204
+ }
4205
+ /**
4206
+ * Where MinIO posts each watched bucket's events under `gkm dev`: the dev
4207
+ * server of the app that builds the worker running the bucket's subscribers,
4208
+ * on the host, on that app's port — the process that relays them onto the
4209
+ * broker.
4210
+ *
4211
+ * None where the broker is SNS: there a bucket's events come from a topic, and
4212
+ * nothing relays them. None for a worker no app can build — `gkm build` says
4213
+ * so; a dev loop has nothing to post to.
4214
+ */
4215
+ function localObjectWebhooks(workspace, manifest, options) {
4216
+ if (backendsOf(workspace).events === "sns") return [];
4217
+ const watched = watchedBuckets(manifest, options.subscribers);
4218
+ if (watched.size === 0) return [];
4219
+ let units;
4220
+ try {
4221
+ units = workerUnits(workspace, manifest, options.background);
4222
+ } catch (error) {
4223
+ if (error instanceof WorkerHasNoApp) return [];
4224
+ throw error;
4225
+ }
4226
+ const hooks = [];
4227
+ for (const [bucket, worker] of watched) {
4228
+ const app = units.find((unit) => unit.id === worker)?.app;
4229
+ const port = app ? workspace.apps[app]?.port : void 0;
4230
+ if (!port) continue;
4231
+ hooks.push({
4232
+ bucket,
4233
+ endpoint: `http://${HOST_GATEWAY$1}:${port}${objectsRoute(bucket)}`,
4234
+ token: objectsWebhookToken(workspace.name, bucket, options.seed)
4235
+ });
4236
+ }
4237
+ return hooks;
4238
+ }
4239
+ /**
4240
+ * Where Mailpit posts each message it receives, locally: the relay route on
4241
+ * the dev server of the app that builds the worker running the inbox
4242
+ * subscribers, through the host's gateway — as MinIO reaches a bucket's
4243
+ * subscribers. `undefined` with no inbox subscriber, or none an app builds.
4244
+ */
4245
+ function localInboxWebhook(workspace, manifest, options) {
4246
+ const worker = inboxRelayWorker(manifest, options.subscribers);
4247
+ if (!worker) return void 0;
4248
+ let units;
4249
+ try {
4250
+ units = workerUnits(workspace, manifest, options.background);
4251
+ } catch (error) {
4252
+ if (error instanceof WorkerHasNoApp) return void 0;
4253
+ throw error;
4254
+ }
4255
+ const app = units.find((unit) => unit.id === worker)?.app;
4256
+ const port = app ? workspace.apps[app]?.port : void 0;
4257
+ if (!port) return void 0;
4258
+ return inboxWebhookUrl(`http://${HOST_GATEWAY$1}:${port}`, inboxRelayToken(workspace.name, options.seed));
4259
+ }
4260
+ /**
4261
+ * Where each mobile app's Metro runs: its app's port, which `gkm exec` hands
4262
+ * Expo as `RCT_METRO_PORT`. Kept apart from the surfaces' addresses — a phone
4263
+ * reaches Metro by `exp://`, not through the edge.
4264
+ */
4265
+ function metroPorts(workspace, manifest) {
4266
+ const ports = {};
4267
+ for (const [id, declaration] of Object.entries(manifest)) {
4268
+ if (declaration.kind !== "mobile-app") continue;
4269
+ const port = workspace.apps[appKey(id)]?.port;
4270
+ if (port) ports[id] = port;
4271
+ }
4272
+ return ports;
4273
+ }
4274
+ /**
4275
+ * Where each file server's processors answer locally: the dev server of the
4276
+ * app holding them, which `gkm dev` mounts them on — the same app whose build
4277
+ * writes their unit deployed.
4278
+ */
4279
+ function processorAddresses(workspace, manifest) {
4280
+ const addresses = {};
4281
+ for (const unit of processorUnits(workspace, manifest)) {
4282
+ const port = workspace.apps[unit.app]?.port;
4283
+ if (port) addresses[unit.id] = localAddress(port);
4284
+ }
4285
+ return addresses;
4286
+ }
4287
+ /**
4288
+ * Where each declared surface and site answers locally.
4289
+ *
4290
+ * The addresses come from the ports the workspace already assigns; which
4291
+ * construct sits at which one comes from the manifest. That split is the point:
4292
+ * this used to answer "who may call whom" by listing every app the workspace
4293
+ * runs, which is a different question that happened to give the same answer in
4294
+ * a single-repo workspace and no answer at all deployed. Now it answers only
4295
+ * "where does this construct answer", and the graph answers the rest.
4296
+ *
4297
+ * A site is matched to its app by `path`, because that is the one thing a site
4298
+ * declaration and a workspace app both name. Surfaces are matched to the
4299
+ * backend app: every `rest-api` in a process answers on that process's port, so
4300
+ * an app serving both its own API and an auth server publishes one address
4301
+ * twice — which is exactly what it does at runtime.
4302
+ */
4303
+ function surfaceAddresses(workspace, manifest, at = (_app, port) => localAddress(port)) {
4304
+ const addresses = {};
4305
+ for (const [id, declaration] of Object.entries(manifest)) {
4306
+ if (declaration.kind !== "rest-api" && declaration.kind !== "websocket-api" && declaration.kind !== "site") continue;
4307
+ const host = appKey(id);
4308
+ const port = workspace.apps[host]?.port;
4309
+ if (port) addresses[id] = at(host, port);
4310
+ }
4311
+ return addresses;
4312
+ }
4313
+ /** Where a local process answers, given the port the workspace gave it. */
4314
+ function localAddress(port) {
4315
+ return `http://localhost:${port}`;
4316
+ }
4317
+ //#endregion
4318
+ //#region src/reconcile/apps.ts
4319
+ /**
4320
+ * The apps, as services beside the containers their constructs derive.
4321
+ *
4322
+ * `gkm docker` used to write these by hand: every backend got
4323
+ * `DATABASE_URL=postgresql://…@postgres:5432/app` and `REDIS_URL=redis://redis`,
4324
+ * which was right only for a project whose single database happened to be
4325
+ * called `Database` — an `Orders` database publishes `ORDERS_URL`, a schema
4326
+ * tenant has a URL of its own, and buckets and mail were not wired at all.
4327
+ *
4328
+ * Now an app's environment is exactly what reconcile hands `gkm dev`: every key
4329
+ * the declared constructs provide, from the same derivation. The only
4330
+ * difference is where the addresses point — at the containers by service name
4331
+ * on the compose network, rather than at `localhost` on the ports published for
4332
+ * the host.
4333
+ *
4334
+ * Always from the *local* stage's plan, whichever stage is being reconciled:
4335
+ * `gkm test` rewrites the same file, and running the tests must not quietly
4336
+ * point these apps at the test databases.
4337
+ */
4338
+ /** The profile app services carry, so `gkm dev` starts only the containers. */
4339
+ const APPS_PROFILE = "apps";
4340
+ /**
4341
+ * Where an app's image is built from.
4342
+ *
4343
+ * An app at the project root is a single-app project, whose `gkm docker`
4344
+ * writes `.gkm/docker/Dockerfile`; a workspace writes one per app.
4345
+ */
4346
+ function dockerfileOf(appName, path) {
4347
+ return path === "." ? ".gkm/docker/Dockerfile" : `.gkm/docker/Dockerfile.${appName}`;
4348
+ }
4349
+ /**
4350
+ * The local stage's plan, which the app services are always written from.
4351
+ *
4352
+ * Without the edge: these apps reach each other by service name and a browser
4353
+ * reaches them on the ports this file publishes. Behind the edge every
4354
+ * surface resolved to its `*.localhost` host on the edge's port, which the
4355
+ * rewrite to the compose network then turned into an address nothing answers.
4356
+ */
4357
+ function localPlan(workspace, manifest, fakes = {}, credentialFakes = {}) {
4358
+ return planFor(manifest, workspace.stages.local, provisionOrder(manifest), {
4359
+ localStage: workspace.stages.local,
4360
+ ...backendsOf(workspace),
4361
+ ...workspace.protectedSubdomains ? { protectedSubdomains: workspace.protectedSubdomains } : {},
4362
+ fakes,
4363
+ credentialFakes,
4364
+ edge: false
4365
+ });
4366
+ }
4367
+ /**
4368
+ * The values a site's bundler inlines, by the name it inlines them under —
4369
+ * `{ VITE_API_URL: 'http://localhost:3000' }`.
4370
+ *
4371
+ * Build arguments, not environment: a static bundle is finished when it is
4372
+ * built, so a value handed to its container at runtime reaches nothing. And
4373
+ * addresses a *browser* can open, since that is where the bundle runs — the
4374
+ * host's port for each app, never a name on the compose network.
4375
+ */
4376
+ function sitePublicArgs(manifest, appName, env) {
4377
+ const declaration = Object.entries(manifest).find(([id, d]) => d.kind === "site" && appKey(id) === appName)?.[1];
4378
+ if (declaration?.kind !== "site") return {};
4379
+ return publicBuildValues(declaration, manifest, (key) => env[key], {});
4380
+ }
4381
+ /**
4382
+ * Every key a plan resolves, with each container addressed by its service
4383
+ * name on the compose network — `postgres:5432` rather than the port
4384
+ * published for the host.
4385
+ *
4386
+ * The addresses of surfaces and sites are the caller's: on the compose
4387
+ * network for a service another service calls, public for what a browser is
4388
+ * handed. `gkm compose` asks twice, once for each.
4389
+ */
4390
+ function networkEnv(plan, options, hostOf = (container) => container) {
4391
+ const placeholders = {};
4392
+ const targets = /* @__PURE__ */ new Map();
4393
+ let next = 49152;
4394
+ for (const container of [...plan.containers].sort()) for (const port of portsOf(container, plan.fakes)) {
4395
+ placeholders[port.key] = next;
4396
+ targets.set(next, `${hostOf(container)}:${port.inside}`);
4397
+ next += 1;
4398
+ }
4399
+ for (const key of portKeys(plan.containers, plan.fakes)) placeholders[key] ??= next++;
4400
+ const env = envFor(plan, {
4401
+ ...options,
4402
+ ports: placeholders
4403
+ });
4404
+ const rewrite = (value) => value.replace(/(?:localhost|127\.0\.0\.1):(\d+)/g, (match, port) => {
4405
+ return targets.get(Number(port)) ?? match;
4406
+ });
4407
+ return Object.fromEntries(Object.entries(env).map(([key, value]) => [key, rewrite(value)]));
4408
+ }
4409
+ /**
4410
+ * The keys one app's environment may hold: what its own declaration provides
4411
+ * and requires, and what each construct it has an edge to provides.
4412
+ *
4413
+ * The edges, not the manifest. Every app used to be handed every key the
4414
+ * workspace resolved — a web app got the database's owner URL and the auth
4415
+ * server's signing secret, and the API got the auth server's database — which
4416
+ * is exactly what a declared edge exists to rule out. A deploy composes an
4417
+ * app's environment from its edges; so does this.
4418
+ *
4419
+ * One hop: a surface an app calls is reached over HTTP, so its URL is the
4420
+ * app's business and its database is not. A site also gets the public
4421
+ * variant of each key its bundle inlines (`NEXT_PUBLIC_API_URL`).
4422
+ *
4423
+ * A surface's process runs its own endpoints and nothing else. A Worker's
4424
+ * crons, queue consumers and subscribers run in the worker's own process
4425
+ * (`workerEnvKeys`), so no surface is handed a worker's edges — however its
4426
+ * endpoints were found.
4427
+ *
4428
+ * `undefined` for an app no declaration describes, which gets nothing but its
4429
+ * port.
4430
+ */
4431
+ function appEnvKeys(manifest, appName) {
4432
+ const entry = Object.entries(manifest).find(([id, d]) => (d.kind === "rest-api" || d.kind === "websocket-api" || d.kind === "site" || d.kind === "mobile-app") && appKey(id) === appName);
4433
+ if (!entry) return void 0;
4434
+ const [, declaration] = entry;
4435
+ const keys = /* @__PURE__ */ new Set([
4436
+ ...declaration.provides ?? [],
4437
+ ...declaration.requires ?? [],
4438
+ ...declaration.kind === "rest-api" ? declaration.endpoints.flatMap((endpoint) => endpoint.requires ?? []) : []
4439
+ ]);
4440
+ addEdgeKeys(manifest, dependenciesOf(declaration).map((edge) => edge.target), keys, declaration.kind === "site" || declaration.kind === "mobile-app");
4441
+ if (declaration.kind === "site" || declaration.kind === "mobile-app") {
4442
+ for (const key of TELEMETRY_KEYS) keys.delete(key);
4443
+ for (const key of Object.keys(publicEnvFor(declaration, manifest))) keys.add(key);
4444
+ }
4445
+ return keys;
4446
+ }
4447
+ /**
4448
+ * The keys a Worker's own process reads: what the worker and everything built
4449
+ * from it declared an edge to — the queues its consumers drain and the topics
4450
+ * its subscribers are bound to among them (see `workerEdges`) — and the
4451
+ * broker its crons schedule through.
4452
+ *
4453
+ * `undefined` when no worker has that id.
4454
+ */
4455
+ function workerEnvKeys(manifest, workerId) {
4456
+ const declaration = manifest[workerId];
4457
+ if (declaration?.kind !== "worker") return void 0;
4458
+ const keys = /* @__PURE__ */ new Set([
4459
+ ...declaration.provides ?? [],
4460
+ ...declaration.requires ?? [],
4461
+ "EVENT_PUBLISHER_CONNECTION_STRING"
4462
+ ]);
4463
+ addEdgeKeys(manifest, workerEdges(manifest, workerId), keys);
4464
+ return keys;
4465
+ }
4466
+ /** What each construct an edge reaches hands the process that reaches it. */
4467
+ /**
4468
+ * The keys a file server's processor unit reads: its bucket's, and what each
4469
+ * of its processors depends on. `undefined` for a file server with none.
4470
+ */
4471
+ function processorEnvKeys(manifest, fileServerId) {
4472
+ const server = manifest[fileServerId];
4473
+ if (server?.kind !== "file-server" || !server.processors?.length) return;
4474
+ const keys = /* @__PURE__ */ new Set();
4475
+ addEdgeKeys(manifest, [server.of, ...server.processors.flatMap((p) => p.dependencies.map((d) => d.target))], keys);
4476
+ return keys;
4477
+ }
4478
+ function addEdgeKeys(manifest, edges, keys, client = false) {
4479
+ for (const id of edges) {
4480
+ const bucket = eventsEdgeBucket(id);
4481
+ if (bucket) {
4482
+ if (manifest[bucket]?.kind === "objects") {
4483
+ keys.add(provideKey(bucket, "eventsConnectionString"));
4484
+ keys.add(provideKey(bucket, "eventsToken"));
4485
+ }
4486
+ continue;
4487
+ }
4488
+ const target = manifest[id];
4489
+ if (!target) continue;
4490
+ if (target.kind === "rest-api") {
4491
+ keys.add(provideKey(id, "url"));
4492
+ continue;
4493
+ }
4494
+ if (target.kind === "websocket-api") {
4495
+ keys.add(provideKey(id, "url"));
4496
+ if (!client) keys.add(provideKey(id, "publisherConnectionString"));
4497
+ continue;
4498
+ }
4499
+ for (const key of target.provides ?? []) keys.add(key);
4500
+ if (target.kind === "inbox") keys.add(INBOX_RELAY_TOKEN_KEY);
4501
+ if (target.kind === "queue" || target.kind === "topic") keys.add("EVENT_PUBLISHER_CONNECTION_STRING");
4502
+ if (target.kind === "objects") {
4503
+ for (const other of Object.values(manifest)) if (other.kind === "file-server" && other.of === id) for (const key of other.provides ?? []) keys.add(key);
4504
+ keys.add("AWS_ACCESS_KEY_ID");
4505
+ keys.add("AWS_SECRET_ACCESS_KEY");
4506
+ keys.add("AWS_REGION");
4507
+ }
4508
+ }
4509
+ }
4510
+ /** One compose service per app the workspace runs, behind the apps profile. */
4511
+ function appServices(workspace, manifest, containers, credentials, fakes = {}, credentialFakes = {}) {
4512
+ const plan = localPlan(workspace, manifest, fakes, credentialFakes);
4513
+ const derivation = {
4514
+ project: workspace.name,
4515
+ credentials,
4516
+ seed: credentials.seed
4517
+ };
4518
+ const env = networkEnv(plan, {
4519
+ ...derivation,
4520
+ addresses: surfaceAddresses(workspace, manifest, (app, port) => `http://${app}:${port}`)
4521
+ });
4522
+ const browser = networkEnv(plan, {
4523
+ ...derivation,
4524
+ addresses: surfaceAddresses(workspace, manifest)
4525
+ });
4526
+ const services = {};
4527
+ const buildRoot = findBuildRoot(workspace.root);
4528
+ for (const [name, app] of Object.entries(workspace.apps)) {
4529
+ if (app.type === "mobile" || !app.port) continue;
4530
+ const key = appKey(name);
4531
+ const health = app.type === "web" ? "/" : "/health";
4532
+ const build = composeBuildPaths({
4533
+ composeDir: workspace.root,
4534
+ buildRoot,
4535
+ workspaceRoot: workspace.root,
4536
+ dockerfile: dockerfileOf(name, app.path)
4537
+ });
4538
+ const ports = [`${app.port}:${app.port}`];
4539
+ if (app.type === "web") {
4540
+ const args = sitePublicArgs(manifest, name, browser);
4541
+ services[key] = {
4542
+ image: `${key}:\${TAG:-latest}`,
4543
+ build: {
4544
+ ...build,
4545
+ ...Object.keys(args).length ? { args } : {}
4546
+ },
4547
+ profiles: [APPS_PROFILE],
4548
+ ports,
4549
+ healthcheck: healthcheck(app.port, health)
4550
+ };
4551
+ continue;
4552
+ }
4553
+ const allowed = appEnvKeys(manifest, name);
4554
+ const own = scopeTelemetryEnv(Object.fromEntries(Object.entries(env).filter(([envKey]) => allowed?.has(envKey))), {
4555
+ uses: appTelemetry(manifest, name) !== void 0,
4556
+ serviceName: name,
4557
+ telemetry: env
4558
+ });
4559
+ services[key] = {
4560
+ image: `${key}:\${TAG:-latest}`,
4561
+ build,
4562
+ profiles: [APPS_PROFILE],
4563
+ ports,
4564
+ environment: {
4565
+ NODE_ENV: "production",
4566
+ PORT: String(app.port),
4567
+ ...own
4568
+ },
4569
+ ...containers.some((c) => c !== "caddy") ? { depends_on: containers.filter((c) => c !== "caddy") } : {},
4570
+ healthcheck: healthcheck(app.port, health)
4571
+ };
4572
+ }
4573
+ return services;
4574
+ }
4575
+ /** An app's health check: its own server answering on `path`. */
4576
+ function healthcheck(port, path) {
4577
+ return {
4578
+ test: [
4579
+ "CMD",
4580
+ "wget",
4581
+ "-q",
4582
+ "--spider",
4583
+ `http://localhost:${port}${path}`
4584
+ ],
4585
+ interval: "10s",
4586
+ timeout: "5s",
4587
+ retries: 5
4588
+ };
4589
+ }
4590
+ //#endregion
4591
+ export { mergeLifecycle as $, generateNextjsDockerfile as A, processorUnitName as B, TURBO_VERSION as C, generateBackendDockerfile as D, findBuildRoot as E, resolveDockerConfig as F, applyNotifications as G, workerUnits as H, imageBuildCredentials as I, bucketLifecycles as J, applyPolicies as K, isImageBuild as L, generateProcessorDockerfile as M, generateViteStaticDockerfile as N, generateDockerignore as O, generateWorkerDockerfile as P, quoteIdentifier as Q, publicBuildValues as R, PROCESSOR_PORT as S, detectPackageManager as T, applyBuckets as U, processorUnits as V, applyLifecycles as W, postgresDatabaseNames as X, bucketPolicies as Y, postgresStatements as Z, bucketClient as _, workerEnvKeys as a, resourceName as at, composeBuildPaths as b, constructGlobs as c, scopeTelemetryEnv as ct, workspace_exports as d, telemetryOf as dt, envFor as et, COMPOSE_PATH as f, usesTelemetry as ft, ensurePostgresLogin as g, LEGACY_LOCAL_LOGIN as h, PROCESSORS_DEV_PREFIX as ht, processorEnvKeys as i, planFor as it, generateNodeWebDockerfile as j, generateEntryDockerfile as k, derivedContainers as l, siteClientTelemetry as lt, forgetState as m, hostFor$1 as mt, dockerfileOf as n, stageEnv as nt, FAKE_ENV as o, appTelemetry as ot, LOCAL_CA_PATH as p, LOCAL_TLD as pt, applyPostgres as q, networkEnv as r, TELEMETRY_CONTAINER as rt, backendsOf as s, isTelemetryKey as st, appEnvKeys as t, localRolePassword as tt, reconcileWorkspace as u, surfaceClientTraced as ut, pgClient as v, WORKER_PORT as w, imageLayout as x, appImageOptions as y, isUnder as z };
4592
+
4593
+ //# sourceMappingURL=apps-CZ0ydmPQ.mjs.map