@geekmidas/cli 10.0.0-alpha.11 → 10.0.0-alpha.13

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 (242) hide show
  1. package/README.md +13 -40
  2. package/dist/{HostingerProvider-BcyFRbqV.mjs → HostingerProvider-BY26bWwB.mjs} +20 -21
  3. package/dist/HostingerProvider-BY26bWwB.mjs.map +1 -0
  4. package/dist/{HostingerProvider-BOvsgATO.cjs → HostingerProvider-BZFGYYVA.cjs} +21 -22
  5. package/dist/HostingerProvider-BZFGYYVA.cjs.map +1 -0
  6. package/dist/{LocalStateProvider-yjXZXZDM.cjs → LocalStateProvider-7HR6SS2t.cjs} +11 -6
  7. package/dist/LocalStateProvider-7HR6SS2t.cjs.map +1 -0
  8. package/dist/{LocalStateProvider-Bq-0C0sg.mjs → LocalStateProvider-D7QyJoLM.mjs} +10 -4
  9. package/dist/LocalStateProvider-D7QyJoLM.mjs.map +1 -0
  10. package/dist/{Route53Provider-C0xmer8U.mjs → Route53Provider-Cvj40A8I.mjs} +12 -12
  11. package/dist/Route53Provider-Cvj40A8I.mjs.map +1 -0
  12. package/dist/{Route53Provider-CH9XH7bz.cjs → Route53Provider-DVILBsvV.cjs} +20 -21
  13. package/dist/Route53Provider-DVILBsvV.cjs.map +1 -0
  14. package/dist/{SSMStateProvider-DOZifZfE.mjs → SSMStateProvider-B0Hu1MKZ.mjs} +13 -4
  15. package/dist/SSMStateProvider-B0Hu1MKZ.mjs.map +1 -0
  16. package/dist/{SSMStateProvider-CXtNgPfy.cjs → SSMStateProvider-VrQ-yeqd.cjs} +17 -9
  17. package/dist/SSMStateProvider-VrQ-yeqd.cjs.map +1 -0
  18. package/dist/{backup-provisioner-cE2PuCTb.cjs → backup-provisioner-BmhIO_Yr.cjs} +21 -19
  19. package/dist/backup-provisioner-BmhIO_Yr.cjs.map +1 -0
  20. package/dist/{backup-provisioner-DXXg6FIL.mjs → backup-provisioner-DIVCbfee.mjs} +10 -7
  21. package/dist/backup-provisioner-DIVCbfee.mjs.map +1 -0
  22. package/dist/{bundler-CaTGtiaO.cjs → bundler-BeNDZdHQ.cjs} +10 -28
  23. package/dist/bundler-BeNDZdHQ.cjs.map +1 -0
  24. package/dist/{bundler-S3ek7ea-.mjs → bundler-BodTL4kf.mjs} +9 -26
  25. package/dist/bundler-BodTL4kf.mjs.map +1 -0
  26. package/dist/{config-HhDpJmLO.cjs → config-Q1rvKHvw.cjs} +74 -69
  27. package/dist/config-Q1rvKHvw.cjs.map +1 -0
  28. package/dist/{config-B0L2tvvC.mjs → config-pqGD3hF9.mjs} +26 -14
  29. package/dist/config-pqGD3hF9.mjs.map +1 -0
  30. package/dist/config.cjs +4 -5
  31. package/dist/config.d.cts +14 -17
  32. package/dist/config.d.cts.map +1 -1
  33. package/dist/config.d.mts +14 -17
  34. package/dist/config.d.mts.map +1 -1
  35. package/dist/config.mjs +3 -5
  36. package/dist/{credentials-CZrys9C5.mjs → credentials-BekkKtY_.mjs} +1331 -1100
  37. package/dist/credentials-BekkKtY_.mjs.map +1 -0
  38. package/dist/{credentials-B2Pyh3bJ.cjs → credentials-Q6F0uqJq.cjs} +1468 -1232
  39. package/dist/credentials-Q6F0uqJq.cjs.map +1 -0
  40. package/dist/deploy/sniffer-envkit-patch.cjs +18 -10
  41. package/dist/deploy/sniffer-envkit-patch.cjs.map +1 -1
  42. package/dist/deploy/sniffer-envkit-patch.d.cts +0 -2
  43. package/dist/deploy/sniffer-envkit-patch.d.cts.map +1 -1
  44. package/dist/deploy/sniffer-envkit-patch.d.mts +0 -2
  45. package/dist/deploy/sniffer-envkit-patch.d.mts.map +1 -1
  46. package/dist/deploy/sniffer-envkit-patch.mjs +10 -2
  47. package/dist/deploy/sniffer-envkit-patch.mjs.map +1 -1
  48. package/dist/deploy/sniffer-hooks.cjs +13 -7
  49. package/dist/deploy/sniffer-hooks.cjs.map +1 -1
  50. package/dist/deploy/sniffer-hooks.d.cts +1 -2
  51. package/dist/deploy/sniffer-hooks.d.cts.map +1 -1
  52. package/dist/deploy/sniffer-hooks.d.mts +1 -2
  53. package/dist/deploy/sniffer-hooks.d.mts.map +1 -1
  54. package/dist/deploy/sniffer-hooks.mjs +9 -2
  55. package/dist/deploy/sniffer-hooks.mjs.map +1 -1
  56. package/dist/deploy/sniffer-loader.cjs +16 -7
  57. package/dist/deploy/sniffer-loader.cjs.map +1 -1
  58. package/dist/deploy/sniffer-loader.d.cts +1 -1
  59. package/dist/deploy/sniffer-loader.d.mts +1 -1
  60. package/dist/deploy/sniffer-loader.mjs +13 -2
  61. package/dist/deploy/sniffer-loader.mjs.map +1 -1
  62. package/dist/deploy/sniffer-routes-worker.cjs +11 -11
  63. package/dist/deploy/sniffer-routes-worker.cjs.map +1 -1
  64. package/dist/deploy/sniffer-routes-worker.d.cts +1 -1
  65. package/dist/deploy/sniffer-routes-worker.d.mts +1 -1
  66. package/dist/deploy/sniffer-routes-worker.mjs +2 -2
  67. package/dist/deploy/sniffer-routes-worker.mjs.map +1 -1
  68. package/dist/deploy/sniffer-worker.cjs +15 -6
  69. package/dist/deploy/sniffer-worker.cjs.map +1 -1
  70. package/dist/deploy/sniffer-worker.d.cts +1 -3
  71. package/dist/deploy/sniffer-worker.d.cts.map +1 -1
  72. package/dist/deploy/sniffer-worker.d.mts +1 -3
  73. package/dist/deploy/sniffer-worker.d.mts.map +1 -1
  74. package/dist/deploy/sniffer-worker.mjs +15 -4
  75. package/dist/deploy/sniffer-worker.mjs.map +1 -1
  76. package/dist/{discover-DTmFeloL.cjs → discover-BERGFg8v.cjs} +81 -64
  77. package/dist/discover-BERGFg8v.cjs.map +1 -0
  78. package/dist/{discover-CAlFgYec.mjs → discover-CK-n3w6b.mjs} +35 -20
  79. package/dist/discover-CK-n3w6b.mjs.map +1 -0
  80. package/dist/{emit-CUD1BWls.cjs → emit-BdoPFjZf.cjs} +53 -36
  81. package/dist/emit-BdoPFjZf.cjs.map +1 -0
  82. package/dist/{emit-CIlvnDvV.mjs → emit-Bzq7JsMP.mjs} +24 -6
  83. package/dist/emit-Bzq7JsMP.mjs.map +1 -0
  84. package/dist/{fullstack-secrets-METaRLS1.mjs → fullstack-secrets-BET8gqwK.mjs} +9 -15
  85. package/dist/fullstack-secrets-BET8gqwK.mjs.map +1 -0
  86. package/dist/{fullstack-secrets-D6MyHhQP.cjs → fullstack-secrets-DXsWO-5m.cjs} +19 -38
  87. package/dist/fullstack-secrets-DXsWO-5m.cjs.map +1 -0
  88. package/dist/{generator-VE2JrFlY.mjs → generator-LfZU7NlU.mjs} +8 -14
  89. package/dist/generator-LfZU7NlU.mjs.map +1 -0
  90. package/dist/{generator-CNzBw7g5.cjs → generator-Wdfp0jCS.cjs} +38 -45
  91. package/dist/generator-Wdfp0jCS.cjs.map +1 -0
  92. package/dist/{github-BrvaPkFf.cjs → github-BfflNPVe.cjs} +36 -30
  93. package/dist/{github-BrvaPkFf.cjs.map → github-BfflNPVe.cjs.map} +1 -1
  94. package/dist/{github-CrpsXSfV.mjs → github-CRwnxKmD.mjs} +27 -20
  95. package/dist/github-CRwnxKmD.mjs.map +1 -0
  96. package/dist/{index-CR5urhWX.d.cts → index-Cd-qeOam.d.mts} +3 -12
  97. package/dist/index-Cd-qeOam.d.mts.map +1 -0
  98. package/dist/{index-Ayxg_DQY.d.mts → index-D-iAVHx-.d.cts} +3 -12
  99. package/dist/index-D-iAVHx-.d.cts.map +1 -0
  100. package/dist/index.cjs +3320 -2961
  101. package/dist/index.cjs.map +1 -1
  102. package/dist/index.d.cts +1 -1
  103. package/dist/index.d.mts +1 -1
  104. package/dist/index.mjs +3200 -2858
  105. package/dist/index.mjs.map +1 -1
  106. package/dist/{keystore-TxeYM7an.mjs → keystore-B4PRlbgE.mjs} +5 -7
  107. package/dist/keystore-B4PRlbgE.mjs.map +1 -0
  108. package/dist/{keystore-C9rint2E.cjs → keystore-DgGRj2Yy.cjs} +23 -26
  109. package/dist/keystore-DgGRj2Yy.cjs.map +1 -0
  110. package/dist/{openapi-DaKlTKxz.cjs → openapi-Dcbme-Us.cjs} +134 -171
  111. package/dist/openapi-Dcbme-Us.cjs.map +1 -0
  112. package/dist/openapi-react-query.cjs +156 -2
  113. package/dist/openapi-react-query.cjs.map +1 -0
  114. package/dist/openapi-react-query.d.cts +1 -3
  115. package/dist/openapi-react-query.d.cts.map +1 -1
  116. package/dist/openapi-react-query.d.mts +1 -3
  117. package/dist/openapi-react-query.d.mts.map +1 -1
  118. package/dist/openapi-react-query.mjs +156 -2
  119. package/dist/openapi-react-query.mjs.map +1 -0
  120. package/dist/{openapi-y1d7Kalt.mjs → openapi-vy4JzhUv.mjs} +76 -106
  121. package/dist/openapi-vy4JzhUv.mjs.map +1 -0
  122. package/dist/openapi.cjs +3 -6
  123. package/dist/openapi.d.cts +8 -11
  124. package/dist/openapi.d.cts.map +1 -1
  125. package/dist/openapi.d.mts +8 -11
  126. package/dist/openapi.d.mts.map +1 -1
  127. package/dist/openapi.mjs +2 -6
  128. package/dist/reconcile/public.cjs +4 -4
  129. package/dist/reconcile/public.d.cts +8 -26
  130. package/dist/reconcile/public.d.cts.map +1 -1
  131. package/dist/reconcile/public.d.mts +8 -26
  132. package/dist/reconcile/public.d.mts.map +1 -1
  133. package/dist/reconcile/public.mjs +3 -4
  134. package/dist/{reconcile-CYMhFSYy.cjs → reconcile-Dd2iyILf.cjs} +4 -7
  135. package/dist/reconcile-Dd2iyILf.cjs.map +1 -0
  136. package/dist/{reconcile-C13ERt1U.mjs → reconcile-DhQ00DpU.mjs} +4 -7
  137. package/dist/reconcile-DhQ00DpU.mjs.map +1 -0
  138. package/dist/rolldown-runtime-BhDjJH2R.mjs +15 -0
  139. package/dist/{chunk-CUT6urMc.cjs → rolldown-runtime-C0BPl7ul.cjs} +23 -10
  140. package/dist/{storage-CSNplaJn.cjs → storage-C-30BD1e.cjs} +59 -69
  141. package/dist/storage-C-30BD1e.cjs.map +1 -0
  142. package/dist/{storage-B020InPW.mjs → storage-P_b3w7uj.mjs} +22 -13
  143. package/dist/storage-P_b3w7uj.mjs.map +1 -0
  144. package/dist/{sync-CzHXiR9c.mjs → sync-B3xxXtJT.mjs} +18 -6
  145. package/dist/sync-B3xxXtJT.mjs.map +1 -0
  146. package/dist/{sync-0ILCb-1S.cjs → sync-D7uTIIEi.cjs} +43 -25
  147. package/dist/sync-D7uTIIEi.cjs.map +1 -0
  148. package/dist/types-BGo5TaNE.d.mts +6164 -0
  149. package/dist/types-BGo5TaNE.d.mts.map +1 -0
  150. package/dist/types-C5ZNpUad.d.cts +6165 -0
  151. package/dist/types-C5ZNpUad.d.cts.map +1 -0
  152. package/dist/workspace/index.cjs +3 -3
  153. package/dist/workspace/index.d.cts +3 -3
  154. package/dist/workspace/index.d.mts +3 -3
  155. package/dist/workspace/index.mjs +2 -3
  156. package/dist/{workspace-DaEX7FEg.mjs → workspace-CgwmXEtc.mjs} +725 -871
  157. package/dist/workspace-CgwmXEtc.mjs.map +1 -0
  158. package/dist/{workspace-C-a3G_U0.cjs → workspace-mFpI_ky_.cjs} +894 -1041
  159. package/dist/workspace-mFpI_ky_.cjs.map +1 -0
  160. package/package.json +14 -14
  161. package/dist/CachedStateProvider-BEBl2wQT.cjs +0 -60
  162. package/dist/CachedStateProvider-BEBl2wQT.cjs.map +0 -1
  163. package/dist/CachedStateProvider-C-8wtlto.mjs +0 -3
  164. package/dist/CachedStateProvider-Co4n5Wvs.mjs +0 -54
  165. package/dist/CachedStateProvider-Co4n5Wvs.mjs.map +0 -1
  166. package/dist/CachedStateProvider-DqhGpzsm.cjs +0 -3
  167. package/dist/HostingerProvider-BOvsgATO.cjs.map +0 -1
  168. package/dist/HostingerProvider-BcyFRbqV.mjs.map +0 -1
  169. package/dist/LocalStateProvider-Bq-0C0sg.mjs.map +0 -1
  170. package/dist/LocalStateProvider-yjXZXZDM.cjs.map +0 -1
  171. package/dist/Route53Provider-C0xmer8U.mjs.map +0 -1
  172. package/dist/Route53Provider-CH9XH7bz.cjs.map +0 -1
  173. package/dist/SSMStateProvider-CXtNgPfy.cjs.map +0 -1
  174. package/dist/SSMStateProvider-DOZifZfE.mjs.map +0 -1
  175. package/dist/backup-provisioner-DXXg6FIL.mjs.map +0 -1
  176. package/dist/backup-provisioner-cE2PuCTb.cjs.map +0 -1
  177. package/dist/bundler-CaTGtiaO.cjs.map +0 -1
  178. package/dist/bundler-S3ek7ea-.mjs.map +0 -1
  179. package/dist/chunk-Duj1WY3L.mjs +0 -7
  180. package/dist/config-B0L2tvvC.mjs.map +0 -1
  181. package/dist/config-HhDpJmLO.cjs.map +0 -1
  182. package/dist/credentials-B2Pyh3bJ.cjs.map +0 -1
  183. package/dist/credentials-B5zRHdAl.mjs +0 -141
  184. package/dist/credentials-B5zRHdAl.mjs.map +0 -1
  185. package/dist/credentials-BapzjyV5.cjs +0 -195
  186. package/dist/credentials-BapzjyV5.cjs.map +0 -1
  187. package/dist/credentials-CZrys9C5.mjs.map +0 -1
  188. package/dist/discover-CAlFgYec.mjs.map +0 -1
  189. package/dist/discover-DTmFeloL.cjs.map +0 -1
  190. package/dist/dokploy-api-5cK2InBm.cjs +0 -3
  191. package/dist/dokploy-api-D2Ny4Jrc.mjs +0 -3
  192. package/dist/dokploy-api-pdaBryoY.mjs +0 -706
  193. package/dist/dokploy-api-pdaBryoY.mjs.map +0 -1
  194. package/dist/dokploy-api-zqKe9lHH.cjs +0 -718
  195. package/dist/dokploy-api-zqKe9lHH.cjs.map +0 -1
  196. package/dist/emit-CIlvnDvV.mjs.map +0 -1
  197. package/dist/emit-CUD1BWls.cjs.map +0 -1
  198. package/dist/encryption-D2pL3tHH.cjs +0 -54
  199. package/dist/encryption-D2pL3tHH.cjs.map +0 -1
  200. package/dist/encryption-YDsXXi6w.mjs +0 -3
  201. package/dist/encryption-_4zwjOAR.mjs +0 -42
  202. package/dist/encryption-_4zwjOAR.mjs.map +0 -1
  203. package/dist/encryption-sQ2Oxx0d.cjs +0 -4
  204. package/dist/fullstack-secrets-D6MyHhQP.cjs.map +0 -1
  205. package/dist/fullstack-secrets-METaRLS1.mjs.map +0 -1
  206. package/dist/generator-CNzBw7g5.cjs.map +0 -1
  207. package/dist/generator-VE2JrFlY.mjs.map +0 -1
  208. package/dist/github-CrpsXSfV.mjs.map +0 -1
  209. package/dist/index-Ayxg_DQY.d.mts.map +0 -1
  210. package/dist/index-CR5urhWX.d.cts.map +0 -1
  211. package/dist/keystore-C9rint2E.cjs.map +0 -1
  212. package/dist/keystore-TxeYM7an.mjs.map +0 -1
  213. package/dist/openapi-DaKlTKxz.cjs.map +0 -1
  214. package/dist/openapi-react-query-BKH334Ug.cjs +0 -173
  215. package/dist/openapi-react-query-BKH334Ug.cjs.map +0 -1
  216. package/dist/openapi-react-query-DoR5CmzQ.mjs +0 -167
  217. package/dist/openapi-react-query-DoR5CmzQ.mjs.map +0 -1
  218. package/dist/openapi-y1d7Kalt.mjs.map +0 -1
  219. package/dist/reconcile-C13ERt1U.mjs.map +0 -1
  220. package/dist/reconcile-CYMhFSYy.cjs.map +0 -1
  221. package/dist/setup-BWgMs4gg.mjs +0 -12
  222. package/dist/setup-DOv92pIu.cjs +0 -473
  223. package/dist/setup-DOv92pIu.cjs.map +0 -1
  224. package/dist/setup-DQo_h_yK.mjs +0 -443
  225. package/dist/setup-DQo_h_yK.mjs.map +0 -1
  226. package/dist/setup-lCx_DcUo.cjs +0 -12
  227. package/dist/storage-B020InPW.mjs.map +0 -1
  228. package/dist/storage-CSNplaJn.cjs.map +0 -1
  229. package/dist/storage-DGBzMLZa.mjs +0 -4
  230. package/dist/storage-DlVol_oj.cjs +0 -8
  231. package/dist/sync-0ILCb-1S.cjs.map +0 -1
  232. package/dist/sync-CzHXiR9c.mjs.map +0 -1
  233. package/dist/sync-DYENfOt4.cjs +0 -4
  234. package/dist/sync-HAau6h_R.mjs +0 -3
  235. package/dist/types-CL7jPFM2.d.mts +0 -2220
  236. package/dist/types-CL7jPFM2.d.mts.map +0 -1
  237. package/dist/types-Y3fKQSAK.d.cts +0 -2220
  238. package/dist/types-Y3fKQSAK.d.cts.map +0 -1
  239. package/dist/workspace-C-a3G_U0.cjs.map +0 -1
  240. package/dist/workspace-DQxxs7Jk.cjs +0 -10
  241. package/dist/workspace-DaEX7FEg.mjs.map +0 -1
  242. package/dist/workspace-DbI580qS.mjs +0 -9
@@ -1,63 +1,38 @@
1
- import { DEFAULT_CACHE, DEFAULT_EMAIL, DEFAULT_STORAGE, discover } from "./discover-CAlFgYec.mjs";
2
- import { appKey, getAppNameFromCwd, hostOf, loadWorkspaceAppInfo } from "./config-B0L2tvvC.mjs";
3
- import { allConstructGlobs, getDependencyEnvVars } from "./workspace-DaEX7FEg.mjs";
4
- import { generatePgBossUrl } from "./generator-VE2JrFlY.mjs";
5
- import { readStageSecrets, secretsExist, toEmbeddableSecrets } from "./storage-B020InPW.mjs";
1
+ import { t as __exportAll } from "./rolldown-runtime-BhDjJH2R.mjs";
2
+ import { a as DEFAULT_EVENTS$1, i as DEFAULT_CACHE, o as DEFAULT_STORAGE, t as discover } from "./discover-CK-n3w6b.mjs";
3
+ import { a as loadWorkspaceAppInfo, c as appKey, n as getAppNameFromCwd } from "./config-pqGD3hF9.mjs";
4
+ import { s as getDependencyEnvVars, t as allConstructGlobs } from "./workspace-CgwmXEtc.mjs";
5
+ import { r as generatePgBossUrl } from "./generator-LfZU7NlU.mjs";
6
+ import { n as readStageSecrets, o as toEmbeddableSecrets, r as secretsExist } from "./storage-P_b3w7uj.mjs";
6
7
  import { existsSync, readFileSync } from "node:fs";
7
- import { dirname, join, resolve } from "node:path";
8
- import { DEFAULT_POSTGRES_VERSION, cacheTable, cookieDomain, dependentsOf, provideKey, providedKeyFor, provisionOrder, publicEnvFor } from "@geekmidas/manifest";
9
8
  import { mkdir, readFile, writeFile } from "node:fs/promises";
9
+ import { dirname, join, resolve } from "node:path";
10
10
  import { execFile, execSync } from "node:child_process";
11
+ import { DEFAULT_POSTGRES_VERSION, cacheTable, cookieDomain, dependentsOf, provideKey, providedKeyFor, provisionOrder, publicEnvFor } from "@geekmidas/manifest";
11
12
  import { createServer } from "node:net";
12
13
  import { config } from "dotenv";
13
14
  import { parse as parse$1, stringify } from "yaml";
15
+ import { createHash } from "node:crypto";
16
+ import { ownerRole, readerRole, roleStatements } from "@geekmidas/db/pg/roles";
14
17
  import { CreateBucketCommand, GetBucketPolicyCommand, HeadBucketCommand, PutBucketPolicyCommand, S3Client } from "@aws-sdk/client-s3";
15
18
  import { Client } from "pg";
16
19
  import { promisify } from "node:util";
17
- import { createHash } from "node:crypto";
18
- import { ownerRole, readerRole, roleStatements } from "@geekmidas/db/pg/roles";
19
20
  import { cacheTableStatements } from "@geekmidas/cache/postgres";
20
-
21
21
  //#region src/workspace/backends.ts
22
- const CACHE_BACKENDS = [
23
- "upstash",
24
- "elasticache",
25
- "db"
26
- ];
27
- const EMAIL_BACKENDS = [
28
- "resend",
29
- "ses",
30
- "smtp"
31
- ];
32
- const STORAGE_BACKENDS = [
33
- "minio",
34
- "s3",
35
- "r2"
36
- ];
37
- /**
38
- * The cache backend a workspace selected, or the one its target implies.
39
- *
40
- * Defaulted here rather than by every caller, so "what happens when nobody
41
- * said" is answered once and the answer is greppable — and now answered by
42
- * *where it deploys*, because there is no single right answer for both a
43
- * Lambda and a box running its own Postgres.
44
- *
45
- * `on` is the deploy target, not where this code happens to be running. A
46
- * local `gkm dev` for an AWS app still defaults to Upstash, because a backend
47
- * that differed between local and deployed would be worse than a slower one —
48
- * which is the property the type's own documentation asks for.
49
- */
50
- function cacheBackendOf(value, on = "aws") {
51
- return isBackend(value, CACHE_BACKENDS) ? value : DEFAULT_CACHE[on];
52
- }
53
- /** The email backend a workspace selected, defaulting to SES on every target. */
54
- function emailBackendOf(value) {
55
- return isBackend(value, EMAIL_BACKENDS) ? value : DEFAULT_EMAIL;
56
- }
57
- /** The storage backend a workspace selected, or the one its target implies. */
58
- function storageBackendOf(value, on = "aws") {
59
- return isBackend(value, STORAGE_BACKENDS) ? value : DEFAULT_STORAGE[on];
60
- }
22
+ /**
23
+ * Which backend each declared kind resolves to — decided by where the project
24
+ * deploys, and by nothing else.
25
+ *
26
+ * These used to be read out of a `services:` block, where one key answered two
27
+ * questions: *does this exist* and *what backs it*. The first is the manifest's
28
+ * — a declared `Cache` is why there is a cache — and the second is the deploy
29
+ * target's, because the right cache for a Lambda is not the right cache for a
30
+ * box already running Postgres. With both answered elsewhere there was nothing
31
+ * left for the block to say, so it is gone and these read the target.
32
+ *
33
+ * Mail has no answer here at all: every provider speaks SMTP, so the provider
34
+ * is whichever one the stage's mail URL points at.
35
+ */
61
36
  /**
62
37
  * Which family of defaults a workspace's deploy target belongs to.
63
38
  *
@@ -76,17 +51,162 @@ function providerOf(workspace) {
76
51
  return target === "dokploy" || target === "server" ? "server" : "aws";
77
52
  }
78
53
  /**
79
- * Whether a string names a backend, as opposed to being one of the other things
80
- * a `services:` value can be.
54
+ * The cache backend a target implies.
81
55
  *
82
- * A misspelling falls through to the default rather than throwing, which is the
83
- * wrong trade for something with a cost attached — so callers that can report it
84
- * should use {@link unknownBackend} to say so.
56
+ * The same answer locally and deployed: a local `gkm dev` for an AWS app still
57
+ * uses the Upstash protocol, because a backend that differed between the two
58
+ * would be worse than a slower one.
85
59
  */
86
- function isBackend(value, backends) {
87
- return typeof value === "string" && backends.includes(value);
60
+ function cacheBackendFor(on) {
61
+ return DEFAULT_CACHE[on];
62
+ }
63
+ /** Where a declared bucket lives on a target. */
64
+ function storageBackendFor(on) {
65
+ return DEFAULT_STORAGE[on];
66
+ }
67
+ /**
68
+ * Which broker carries declared queues and topics on a target.
69
+ *
70
+ * Only consulted when something declares one; a project with no `Topic` or
71
+ * `Queue` has no events at all, whatever the target.
72
+ */
73
+ function eventsBackendFor(on) {
74
+ return DEFAULT_EVENTS$1[on];
75
+ }
76
+ //#endregion
77
+ //#region src/reconcile/containers.ts
78
+ /**
79
+ * What gkm knows about each container it can derive.
80
+ *
81
+ * Everything provider-specific about the local target lives here: the image, the
82
+ * ports the image listens on, and whether its data is worth keeping. The plan
83
+ * says *which* containers exist and never says any of this, which is the split
84
+ * the design asks for — neutral facts from the declaration, provider detail
85
+ * from here and from config.
86
+ *
87
+ * Shared by allocation and by the compose writer so the two cannot disagree
88
+ * about how many ports a container needs.
89
+ */
90
+ /**
91
+ * The Postgres image for a major version.
92
+ *
93
+ * One place that knows the tag shape, so a declared version reaches the
94
+ * container without anyone restating `-alpine` beside it.
95
+ */
96
+ function postgresImage(version) {
97
+ return `postgres:${version}-alpine`;
98
+ }
99
+ /**
100
+ * The default image for each container.
101
+ *
102
+ * Pinned to a major rather than `latest` where the image publishes one, so a
103
+ * rebuild does not silently move a developer's Postgres a version. Overridable
104
+ * per project: a project needing `postgis/postgis` says so in config.
105
+ */
106
+ const DEFAULT_IMAGES = {
107
+ postgres: postgresImage(DEFAULT_POSTGRES_VERSION),
108
+ minio: "pgsty/minio:RELEASE.2026-08-04T00-00-00Z",
109
+ mailpit: "axllent/mailpit:latest",
110
+ redis: "redis:8-alpine",
111
+ "redis-http": "hiett/serverless-redis-http:latest",
112
+ rabbitmq: "rabbitmq:4-management-alpine",
113
+ localstack: "floci/floci:latest",
114
+ caddy: "caddy:2-alpine"
115
+ };
116
+ /**
117
+ * The ports each container listens on.
118
+ *
119
+ * The first is always the one an app connects to; the rest are consoles and
120
+ * management UIs, published because a bucket or an inbox you can look at is most
121
+ * of why these are real containers rather than stubs.
122
+ */
123
+ const PORTS = {
124
+ postgres: [{
125
+ key: "postgres",
126
+ inside: 5432,
127
+ label: "postgres"
128
+ }],
129
+ minio: [{
130
+ key: "minio",
131
+ inside: 9e3,
132
+ label: "minio api"
133
+ }, {
134
+ key: "minio-console",
135
+ inside: 9001,
136
+ label: "minio console"
137
+ }],
138
+ mailpit: [{
139
+ key: "mailpit",
140
+ inside: 1025,
141
+ label: "smtp"
142
+ }, {
143
+ key: "mailpit-web",
144
+ inside: 8025,
145
+ label: "mailpit inbox"
146
+ }],
147
+ redis: [{
148
+ key: "redis",
149
+ inside: 6379,
150
+ label: "redis"
151
+ }],
152
+ "redis-http": [{
153
+ key: "redis-http",
154
+ inside: 80,
155
+ label: "cache"
156
+ }],
157
+ rabbitmq: [{
158
+ key: "rabbitmq",
159
+ inside: 5672,
160
+ label: "amqp"
161
+ }, {
162
+ key: "rabbitmq-management",
163
+ inside: 15672,
164
+ label: "rabbitmq console"
165
+ }],
166
+ localstack: [{
167
+ key: "localstack",
168
+ inside: 4566,
169
+ label: "localstack"
170
+ }],
171
+ caddy: [{
172
+ key: "caddy",
173
+ inside: 443,
174
+ label: "https edge"
175
+ }]
176
+ };
177
+ /** The ports a container needs published. Empty for one gkm does not know. */
178
+ function portsOf(container) {
179
+ return PORTS[container] ?? [];
180
+ }
181
+ /** The key a container's primary port is allocated under. */
182
+ function primaryPortKey(container) {
183
+ return portsOf(container)[0]?.key ?? container;
184
+ }
185
+ /**
186
+ * Every port key a set of containers needs, in a stable order.
187
+ *
188
+ * What allocation is handed: it assigns ports to names and knows nothing about
189
+ * what listens on them.
190
+ */
191
+ function portKeys(containers) {
192
+ return [...containers].sort().flatMap((container) => portsOf(container).map((port) => port.key));
193
+ }
194
+ /**
195
+ * The named volume a container's data lives in.
196
+ *
197
+ * Mailpit deliberately has none: local mail is disposable, and an inbox that
198
+ * survives days of dev is noise rather than state.
199
+ */
200
+ function volumeOf(container) {
201
+ return {
202
+ postgres: "postgres-data",
203
+ minio: "minio-data",
204
+ redis: "redis-data",
205
+ rabbitmq: "rabbitmq-data",
206
+ localstack: "localstack-data",
207
+ caddy: "caddy-data"
208
+ }[container];
88
209
  }
89
-
90
210
  //#endregion
91
211
  //#region src/reconcile/caddyfile.ts
92
212
  /**
@@ -163,7 +283,7 @@ function portOf(address) {
163
283
  const { port, protocol } = new URL(address);
164
284
  return port || (protocol === "https:" ? "443" : "80");
165
285
  } catch {
166
- return void 0;
286
+ return;
167
287
  }
168
288
  }
169
289
  /**
@@ -246,1137 +366,1194 @@ const GENERATED = `# Generated by gkm from the construct manifest — do not edi
246
366
  # root and point Node at it, so nothing has to be installed to use these URLs
247
367
  # from code; a browser needs the root trusted once.
248
368
  `;
249
-
250
369
  //#endregion
251
- //#region src/reconcile/clients.ts
252
- /** The credential local containers are brought up with. */
253
- const LOCAL_USER$2 = "geekmidas";
370
+ //#region src/reconcile/plan.ts
254
371
  /**
255
- * A Postgres client that opens one connection per database it is asked about.
372
+ * The reconcile plan — what a manifest wants to exist, for one stage.
256
373
  *
257
- * `CREATE DATABASE` cannot run from a connection to the database being created,
258
- * and a schema must be created from inside its own database — so the database is
259
- * part of each query rather than fixed when the client is built.
374
+ * Pure. Turning the plan into running containers and provisioned resources is
375
+ * the applier's job; deciding *what* should exist is this, so the decisions can
376
+ * be asserted as data rather than by starting Docker.
377
+ *
378
+ * One plan serves every stage. `gkm dev` reconciles `development` and `gkm test`
379
+ * reconciles `test`; they differ only in what the resources are called, never in
380
+ * what infrastructure exists. Roles are cluster-scoped in Postgres while grants
381
+ * are per-object, so one container and one role pair serve both.
260
382
  */
261
- function pgClient(port) {
262
- return { async query(database, sql, values) {
263
- const client = new Client({
264
- host: "localhost",
265
- port,
266
- user: LOCAL_USER$2,
267
- password: LOCAL_USER$2,
268
- database: database ?? LOCAL_USER$2
269
- });
270
- await client.connect();
271
- try {
272
- const result = await client.query(sql, values);
273
- return result.rows;
274
- } finally {
275
- await client.end();
276
- }
277
- } };
278
- }
279
- /** A bucket client pointed at the local MinIO. */
280
- function bucketClient(port) {
281
- const s3 = new S3Client({
282
- region: "us-east-1",
283
- endpoint: `http://localhost:${port}`,
284
- forcePathStyle: true,
285
- credentials: {
286
- accessKeyId: LOCAL_USER$2,
287
- secretAccessKey: LOCAL_USER$2
288
- }
289
- });
290
- return {
291
- async exists(bucket) {
292
- try {
293
- await s3.send(new HeadBucketCommand({ Bucket: bucket }));
294
- return true;
295
- } catch {
296
- return false;
297
- }
298
- },
299
- async create(bucket) {
300
- await s3.send(new CreateBucketCommand({ Bucket: bucket }));
301
- },
302
- async policy(bucket) {
303
- try {
304
- const result = await s3.send(new GetBucketPolicyCommand({ Bucket: bucket }));
305
- return result.Policy;
306
- } catch {
307
- return void 0;
308
- }
309
- },
310
- async setPolicy(bucket, policy) {
311
- await s3.send(new PutBucketPolicyCommand({
312
- Bucket: bucket,
313
- Policy: policy
314
- }));
315
- }
316
- };
317
- }
318
-
319
- //#endregion
320
- //#region src/reconcile/containers.ts
321
383
  /**
322
- * What gkm knows about each container it can derive.
323
- *
324
- * Everything provider-specific about the local target lives here: the image, the
325
- * ports the image listens on, and whether its data is worth keeping. The plan
326
- * says *which* containers exist and never says any of this, which is the split
327
- * the design asks for — neutral facts from the declaration, provider detail
328
- * from here and from config.
384
+ * Which container serves a kind.
329
385
  *
330
- * Shared by allocation and by the compose writer so the two cannot disagree
331
- * about how many ports a container needs.
386
+ * Derived kinds map to the same container as their parent — a schema tenant
387
+ * lives in its parent's Postgres — so the set is deduplicated rather than
388
+ * counted.
332
389
  */
390
+ const CONTAINERS = {
391
+ database: "postgres",
392
+ "database-reader": "postgres",
393
+ "database-schema": "postgres",
394
+ objects: "minio",
395
+ "file-server": "caddy",
396
+ email: "mailpit"
397
+ };
333
398
  /**
334
- * The Postgres image for a major version.
399
+ * The container a cache backend needs locally.
335
400
  *
336
- * One place that knows the tag shape, so a declared version reaches the
337
- * container without anyone restating `-alpine` beside it.
401
+ * Each one matches the protocol its deployed form speaks, which is the property
402
+ * worth having: a cache that behaves differently in the two places is worse than
403
+ * a slower one.
404
+ *
405
+ * `db` is deliberately absent, the same way `pgboss` is absent from
406
+ * `EVENT_CONTAINERS`: the cache is a table in a database the manifest already
407
+ * declares, so mapping it here would start a container for a project that needs
408
+ * none.
338
409
  */
339
- function postgresImage(version) {
340
- return `postgres:${version}-alpine`;
341
- }
410
+ const CACHE_CONTAINERS = {
411
+ upstash: "redis-http",
412
+ elasticache: "redis"
413
+ };
342
414
  /**
343
- * The default image for each container.
415
+ * Kinds that resolve a value without anything running.
344
416
  *
345
- * Pinned to a major rather than `latest` where the image publishes one, so a
346
- * rebuild does not silently move a developer's Postgres a version. Overridable
347
- * per project: a project needing `postgis/postgis` says so in config.
417
+ * A secret is the case: it has no address, so there is nothing to start and
418
+ * nothing to connect to — but it still appears in the plan, because the target
419
+ * still has to resolve it.
348
420
  */
349
- const DEFAULT_IMAGES = {
350
- postgres: postgresImage(DEFAULT_POSTGRES_VERSION),
351
- minio: "pgsty/minio:RELEASE.2026-08-04T00-00-00Z",
352
- mailpit: "axllent/mailpit:latest",
353
- redis: "redis:8-alpine",
354
- "redis-http": "hiett/serverless-redis-http:latest",
355
- rabbitmq: "rabbitmq:4-management-alpine",
356
- localstack: "floci/floci:latest",
357
- caddy: "caddy:2-alpine"
421
+ const CONTAINERLESS = {
422
+ secret: true,
423
+ credential: true,
424
+ "rest-api": true,
425
+ site: true
358
426
  };
359
427
  /**
360
- * The ports each container listens on.
428
+ * Containers that cannot run alone.
361
429
  *
362
- * The first is always the one an app connects to; the rest are consoles and
363
- * management UIs, published because a bucket or an inbox you can look at is most
364
- * of why these are real containers rather than stubs.
430
+ * The one case is the cache: `serverless-redis-http` is a proxy and needs the
431
+ * Redis it proxies. Kept here rather than in the compose definition so the plan
432
+ * -- which is what decides what must be running -- still names everything.
365
433
  */
366
- const PORTS = {
367
- postgres: [{
368
- key: "postgres",
369
- inside: 5432,
370
- label: "postgres"
371
- }],
372
- minio: [{
373
- key: "minio",
374
- inside: 9e3,
375
- label: "minio api"
376
- }, {
377
- key: "minio-console",
378
- inside: 9001,
379
- label: "minio console"
380
- }],
381
- mailpit: [{
382
- key: "mailpit",
383
- inside: 1025,
384
- label: "smtp"
385
- }, {
386
- key: "mailpit-web",
387
- inside: 8025,
388
- label: "mailpit inbox"
389
- }],
390
- redis: [{
391
- key: "redis",
392
- inside: 6379,
393
- label: "redis"
394
- }],
395
- "redis-http": [{
396
- key: "redis-http",
397
- inside: 80,
398
- label: "cache"
399
- }],
400
- rabbitmq: [{
401
- key: "rabbitmq",
402
- inside: 5672,
403
- label: "amqp"
404
- }, {
405
- key: "rabbitmq-management",
406
- inside: 15672,
407
- label: "rabbitmq console"
408
- }],
409
- localstack: [{
410
- key: "localstack",
411
- inside: 4566,
412
- label: "localstack"
413
- }],
414
- caddy: [{
415
- key: "caddy",
416
- inside: 443,
417
- label: "https edge"
418
- }]
434
+ const REQUIRES = { "redis-http": ["redis"] };
435
+ /**
436
+ * The role each kind's one key plays, where it is not a URL.
437
+ *
438
+ * The twin of `ProvidesByKind` in `@geekmidas/manifest`: that is what a
439
+ * construct declares, this is what the local target resolves, and both derive
440
+ * the key from the same `provideKey` so they cannot disagree.
441
+ */
442
+ const ROLES = {
443
+ queue: "publisherConnectionString",
444
+ topic: "publisherConnectionString",
445
+ credential: "credential"
446
+ };
447
+ /** The kinds whose container is the events backend's rather than their own. */
448
+ const EVENT_KINDS = {
449
+ queue: true,
450
+ topic: true
419
451
  };
420
- /** The ports a container needs published. Empty for one gkm does not know. */
421
- function portsOf(container) {
422
- return PORTS[container] ?? [];
423
- }
424
- /** The key a container's primary port is allocated under. */
425
- function primaryPortKey(container) {
426
- return portsOf(container)[0]?.key ?? container;
427
- }
428
452
  /**
429
- * Every port key a set of containers needs, in a stable order.
453
+ * Which kinds provision something *inside* their container.
430
454
  *
431
- * What allocation is handed: it assigns ports to names and knows nothing about
432
- * what listens on them.
455
+ * A database creates a database, a bucket creates a bucket; mail creates
456
+ * nothing — Mailpit accepts whatever is sent to it and has no per-stage object
457
+ * to name. Both still appear in the plan, because both still resolve a URL; the
458
+ * flag is the difference between "must be running" and "must be created".
433
459
  */
434
- function portKeys(containers) {
435
- return [...containers].sort().flatMap((container) => portsOf(container).map((port) => port.key));
436
- }
460
+ const PROVISIONS = {
461
+ database: true,
462
+ "database-reader": true,
463
+ "database-schema": true,
464
+ objects: true,
465
+ cache: true,
466
+ "file-server": true
467
+ };
437
468
  /**
438
- * The named volume a container's data lives in.
469
+ * The container each events backend needs, where it needs one of its own.
439
470
  *
440
- * Mailpit deliberately has none: local mail is disposable, and an inbox that
441
- * survives days of dev is noise rather than state.
471
+ * `pgboss` is deliberately absent rather than mapped to `postgres`: it is a
472
+ * schema tenant in a database the manifest already declares, so mapping it here
473
+ * would start a Postgres for a project that declared no database at all.
474
+ *
475
+ * This one comes from config rather than from a declaration because there are
476
+ * no `topic` or `queue` kinds yet. When they land the mapping moves to
477
+ * {@link CONTAINERS} keyed by kind, and this constant goes with them.
442
478
  */
443
- function volumeOf(container) {
444
- const volumes = {
445
- postgres: "postgres-data",
446
- minio: "minio-data",
447
- redis: "redis-data",
448
- rabbitmq: "rabbitmq-data",
449
- localstack: "localstack-data",
450
- caddy: "caddy-data"
451
- };
452
- return volumes[container];
479
+ const EVENT_CONTAINERS = {
480
+ sns: "localstack",
481
+ rabbitmq: "rabbitmq"
482
+ };
483
+ /** The backend a project gets when it declares queues or topics and says nothing. */
484
+ const DEFAULT_EVENTS = "pgboss";
485
+ /**
486
+ * How a resource of each kind separates its stage suffix.
487
+ *
488
+ * Postgres identifiers are snake_case and a hyphen would need quoting
489
+ * everywhere; buckets and queues are kebab and disallow underscores in some
490
+ * providers. The suffix is the same word either way.
491
+ */
492
+ const SEPARATORS = {
493
+ database: "_",
494
+ "database-reader": "_",
495
+ "database-schema": "_",
496
+ objects: "-"
497
+ };
498
+ /**
499
+ * The stage-scoped name for a resource.
500
+ *
501
+ * The local stage carries no suffix, so `gkm dev` keeps using `orders` and a
502
+ * developer's psql history, saved connections, and muscle memory survive a
503
+ * change of stage name. Every other stage is suffixed.
504
+ */
505
+ function resourceName(id, kind, stage, localStage) {
506
+ const base = id.toLowerCase();
507
+ if (stage === localStage) return base;
508
+ return `${base}${SEPARATORS[kind] ?? "-"}${stage}`;
453
509
  }
454
-
455
- //#endregion
456
- //#region src/reconcile/compose.ts
457
- /** The credential every derived container is brought up with locally. */
458
- const LOCAL_USER$1 = "geekmidas";
459
510
  /**
460
- * The token the local cache accepts.
511
+ * The container a kind needs, or `undefined` if it needs none.
461
512
  *
462
- * Fixed rather than generated: it is only ever reachable on a port this project
463
- * published, and the URL that carries it is derived, so nothing is made safer
464
- * by making it unguessable and a stable one keeps `redis-cli` sessions working.
513
+ * Queues and topics have no container of their own — they live in whichever
514
+ * broker the project selected, which is why the backend is an argument here
515
+ * rather than a second pass that adds containers after the fact.
465
516
  */
466
- const LOCAL_TOKEN$1 = "geekmidas";
517
+ function containerFor(kind, events = DEFAULT_EVENTS, cache = DEFAULT_CACHE.aws, derived = false, edge = true) {
518
+ if (EVENT_KINDS[kind]) return EVENT_CONTAINERS[events] ?? CONTAINERS.database;
519
+ if (kind === "file-server") return edge ? "caddy" : CONTAINERS.objects;
520
+ if (kind === "cache") {
521
+ if (derived) return CONTAINERS.database;
522
+ return CACHE_CONTAINERS[cache] ?? CONTAINERS.database;
523
+ }
524
+ return CONTAINERS[kind];
525
+ }
467
526
  /**
468
- * Build the compose document for a plan.
527
+ * Build the plan for a manifest and stage.
469
528
  *
470
- * Every container is published on its assigned port, never on the image's
471
- * default: the point of allocation is that two projects can run at once, and
472
- * one container publishing 5432 puts that back.
529
+ * `order` is the caller's provisioning order — parents before children — which
530
+ * `provisionOrder` produces from the manifest. Passing it in keeps this
531
+ * function free of the traversal and makes the ordering assertable on its own.
473
532
  */
474
- function composeFor(plan, options) {
475
- const services = {};
476
- const volumes = {};
477
- for (const container of [...plan.containers].sort()) {
478
- const declared = container === "postgres" ? plan.resources.find((r) => r.version)?.version : void 0;
479
- const image = options.images?.[container] ?? (declared ? postgresImage(declared) : DEFAULT_IMAGES[container]);
480
- if (!image) throw new UnknownContainer(container);
481
- services[container] = define(container, image, publish(container, options.ports));
482
- const volume = volumeOf(container);
483
- if (volume) volumes[volume] = {};
533
+ function planFor(manifest, stage, order, options = {}) {
534
+ const containers = /* @__PURE__ */ new Set();
535
+ const resources = [];
536
+ const events = options.events ?? "pgboss";
537
+ const cache = options.cache ?? DEFAULT_CACHE.aws;
538
+ for (const id of order) {
539
+ const declaration = manifest[id];
540
+ if (!declaration) continue;
541
+ const container = containerFor(declaration.kind, events, cache, "of" in declaration && typeof declaration.of === "string", options.edge !== false);
542
+ if (!container && !CONTAINERLESS[declaration.kind]) continue;
543
+ if (container) {
544
+ containers.add(container);
545
+ for (const required of REQUIRES[container] ?? []) containers.add(required);
546
+ }
547
+ resources.push({
548
+ id,
549
+ kind: declaration.kind,
550
+ ...container ? { container } : {},
551
+ name: resourceName(id, declaration.kind, stage, options.localStage),
552
+ envKey: providedKeyFor(id, declaration.kind, ROLES[declaration.kind] ?? "url"),
553
+ provisions: PROVISIONS[declaration.kind] === true,
554
+ ...declaration.kind === "rest-api" ? { callers: dependentsOf(manifest, id) } : {},
555
+ ...declaration.kind === "site" ? { publicEnv: publicEnvFor(declaration, manifest) } : {},
556
+ ...declaration.kind === "file-server" && declaration.open?.length ? { open: declaration.open } : {},
557
+ ..."of" in declaration ? { of: declaration.of } : {},
558
+ ..."schema" in declaration && declaration.schema ? { schema: declaration.schema } : {},
559
+ ..."version" in declaration && declaration.version ? { version: declaration.version } : {},
560
+ ..."roles" in declaration && declaration.roles === false ? { roles: false } : {},
561
+ ..."table" in declaration && declaration.table ? { table: declaration.table } : {}
562
+ });
563
+ }
564
+ if (options.edge !== false && resources.some((r) => EDGE_KINDS[r.kind] === true)) containers.add("caddy");
565
+ for (const extra of options.extraContainers ?? []) containers.add(extra);
566
+ 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));
567
+ if (cache === "db") {
568
+ const placeless = resources.filter((r) => r.kind === "cache" && !r.of);
569
+ if (placeless.length > 0) {
570
+ const databases = resources.filter((r) => r.kind === "database");
571
+ const [only] = databases;
572
+ if (!only) throw new CacheNeedsDatabase(resources.filter((r) => r.kind === "cache").map((r) => r.id));
573
+ if (databases.length > 1) throw new CacheIsAmbiguous(placeless.map((r) => r.id), databases.map((r) => r.id));
574
+ for (const resource of placeless) resource.of = only.id;
575
+ }
484
576
  }
485
577
  return {
486
- name: options.project,
487
- services,
488
- ...Object.keys(volumes).length ? { volumes } : {}
578
+ stage,
579
+ events,
580
+ cache,
581
+ containers: [...containers],
582
+ resources
489
583
  };
490
584
  }
491
- /** Render a compose document as the file it becomes. */
492
- function toYaml(file) {
493
- return `# Generated by gkm from the construct manifest — do not edit.
494
- # Containers are derived from what the app declares; images and anything no
495
- # construct implies are yours, in gkm.config.ts.
496
- #
497
- # Use "gkm dev" or "gkm test" to start these. Running "docker compose up"
498
- # directly will not inject secrets.
499
- ${stringify(file, { lineWidth: 0 })}`;
500
- }
501
- /** A container with no image, from config or from the defaults. */
502
- var UnknownContainer = class extends Error {
503
- /** The container that could not be resolved. */
504
- container;
505
- /** The containers that have a default, so the caller can see the set. */
506
- known = Object.keys(DEFAULT_IMAGES).sort();
507
- constructor(container) {
508
- super("No image is known for this container; pin one in config");
509
- this.name = "UnknownContainer";
510
- this.container = container;
585
+ /**
586
+ * Queues or topics were declared with pg-boss and no database to hold them.
587
+ *
588
+ * Not a reconcile failure but a manifest one: the fix is a line of application
589
+ * code either way — declare a database, or select a backend that brings its own
590
+ * broker.
591
+ */
592
+ var CacheNeedsDatabase = class extends Error {
593
+ ids;
594
+ constructor(ids) {
595
+ 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(", ")}.`);
596
+ this.ids = ids;
597
+ this.name = "CacheNeedsDatabase";
511
598
  }
512
599
  };
513
- /** One container's definition, published on the port it was assigned. */
514
- function define(container, image, published) {
515
- switch (container) {
516
- case "postgres": return {
517
- image,
518
- restart: "unless-stopped",
519
- ports: published,
520
- environment: {
521
- POSTGRES_USER: LOCAL_USER$1,
522
- POSTGRES_PASSWORD: LOCAL_USER$1,
523
- POSTGRES_DB: LOCAL_USER$1
524
- },
525
- volumes: ["postgres-data:/var/lib/postgresql"],
526
- healthcheck: {
527
- test: ["CMD-SHELL", `pg_isready -U ${LOCAL_USER$1}`],
528
- interval: "5s",
529
- timeout: "3s",
530
- retries: 10,
531
- start_period: "10s"
532
- }
533
- };
534
- case "minio": return {
535
- image,
536
- restart: "unless-stopped",
537
- ports: published,
538
- environment: {
539
- MINIO_ROOT_USER: LOCAL_USER$1,
540
- MINIO_ROOT_PASSWORD: LOCAL_USER$1
541
- },
542
- command: "server --console-address \":9001\" /data",
543
- volumes: ["minio-data:/data"],
544
- healthcheck: {
545
- test: [
546
- "CMD",
547
- "mc",
548
- "ready",
549
- "local"
550
- ],
551
- interval: "10s",
552
- timeout: "5s",
553
- retries: 5,
554
- start_period: "10s"
555
- }
556
- };
557
- case "caddy": return {
558
- image,
559
- restart: "unless-stopped",
560
- ports: published,
561
- volumes: [
562
- "./Caddyfile:/etc/caddy/Caddyfile:ro",
563
- "./caddy-sites:/etc/caddy/sites:ro",
564
- "caddy-data:/data"
565
- ],
566
- extra_hosts: ["host.docker.internal:host-gateway"],
567
- healthcheck: {
568
- test: [
569
- "CMD",
570
- "caddy",
571
- "version"
572
- ],
573
- interval: "10s",
574
- timeout: "5s",
575
- retries: 5
576
- }
577
- };
578
- case "mailpit": return {
579
- image,
580
- restart: "unless-stopped",
581
- ports: published,
582
- environment: {
583
- MP_SMTP_AUTH_ACCEPT_ANY: "1",
584
- MP_SMTP_AUTH_ALLOW_INSECURE: "1"
585
- },
586
- healthcheck: {
587
- test: [
588
- "CMD",
589
- "wget",
590
- "-q",
591
- "--spider",
592
- "http://localhost:8025"
593
- ],
594
- interval: "10s",
595
- timeout: "5s",
596
- retries: 5
597
- }
598
- };
599
- case "redis": return {
600
- image,
601
- restart: "unless-stopped",
602
- ports: published,
603
- volumes: ["redis-data:/data"],
604
- healthcheck: {
605
- test: [
606
- "CMD",
607
- "redis-cli",
608
- "ping"
609
- ],
610
- interval: "5s",
611
- timeout: "3s",
612
- retries: 5
613
- }
614
- };
615
- case "redis-http": return {
616
- image,
617
- restart: "unless-stopped",
618
- ports: published,
619
- environment: {
620
- SRH_MODE: "env",
621
- SRH_TOKEN: LOCAL_TOKEN$1,
622
- SRH_CONNECTION_STRING: "redis://redis:6379"
623
- },
624
- depends_on: ["redis"],
625
- healthcheck: {
626
- test: [
627
- "CMD",
628
- "wget",
629
- "-q",
630
- "-O",
631
- "/dev/null",
632
- "http://127.0.0.1:80/"
633
- ],
634
- interval: "5s",
635
- timeout: "3s",
636
- retries: 10
637
- }
638
- };
639
- case "rabbitmq": return {
640
- image,
641
- restart: "unless-stopped",
642
- ports: published,
643
- environment: {
644
- RABBITMQ_DEFAULT_USER: LOCAL_USER$1,
645
- RABBITMQ_DEFAULT_PASS: LOCAL_USER$1
646
- },
647
- volumes: ["rabbitmq-data:/var/lib/rabbitmq"],
648
- healthcheck: {
649
- test: [
650
- "CMD",
651
- "rabbitmq-diagnostics",
652
- "-q",
653
- "ping"
654
- ],
655
- interval: "10s",
656
- timeout: "5s",
657
- retries: 5
658
- }
659
- };
660
- case "localstack": return {
661
- image,
662
- restart: "unless-stopped",
663
- ports: published,
664
- environment: {
665
- AWS_DEFAULT_REGION: "us-east-1",
666
- AWS_ACCESS_KEY_ID: "LSIAQAAAAAAVNCBMPNSG",
667
- AWS_SECRET_ACCESS_KEY: LOCAL_USER$1
668
- },
669
- volumes: ["localstack-data:/var/lib/localstack"],
670
- healthcheck: {
671
- test: [
672
- "CMD",
673
- "curl",
674
- "-f",
675
- "http://localhost:4566/_localstack/health"
676
- ],
677
- interval: "10s",
678
- timeout: "5s",
679
- retries: 5,
680
- start_period: "10s"
681
- }
682
- };
683
- default: throw new UnknownContainer(container);
684
- }
685
- }
686
600
  /**
687
- * The published port mappings for a container.
601
+ * A `'db'` cache — a server target's default — in an app that declares more
602
+ * than one database.
688
603
  *
689
- * Every port is looked up by its own key rather than being derived as an offset
690
- * from the container's first, so MinIO's console cannot land on whatever was
691
- * allocated to the next container.
604
+ * The target says *a* database and the graph offers several, so there is no
605
+ * answer to give — and the wrong kind of failure would be to pick one, since a
606
+ * cache silently landing in the wrong database is a bug that surfaces as
607
+ * missing rows much later. The fix is a stronger statement in application code:
608
+ * `orders.cache('Sessions')` names the database, which is a fact about the
609
+ * application rather than a deployment choice.
692
610
  */
693
- function publish(container, ports) {
694
- return portsOf(container).map((port) => {
695
- const assigned = ports[port.key];
696
- if (assigned === void 0) throw new UnassignedPort(port.key);
697
- return `${assigned}:${port.inside}`;
698
- });
699
- }
700
- /** A planned port that allocation never assigned. */
701
- var UnassignedPort = class extends Error {
702
- /** The port key that went unassigned, e.g. `minio-console`. */
703
- key;
704
- constructor(key) {
705
- super("No port was assigned for this container port");
706
- this.name = "UnassignedPort";
707
- this.key = key;
611
+ var CacheIsAmbiguous = class extends Error {
612
+ ids;
613
+ databases;
614
+ constructor(ids, databases) {
615
+ 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(", ")}.`);
616
+ this.ids = ids;
617
+ this.databases = databases;
618
+ this.name = "CacheIsAmbiguous";
708
619
  }
709
620
  };
710
-
711
- //#endregion
712
- //#region src/reconcile/docker.ts
713
- const run = promisify(execFile);
714
- /** How long to wait for containers to pass their health checks. */
715
- const HEALTH_TIMEOUT_MS = 12e4;
716
- const dockerCli = {
717
- async publishedPort(composePath, service, inside) {
718
- try {
719
- const { stdout } = await run("docker", [
720
- "compose",
721
- "-f",
722
- composePath,
723
- "port",
724
- service,
725
- String(inside)
726
- ]);
727
- const port = Number(stdout.trim().split(":").pop());
728
- return Number.isFinite(port) && port > 0 ? port : void 0;
729
- } catch {
730
- return void 0;
731
- }
732
- },
733
- async up(composePath, services) {
734
- await run("docker", [
735
- "compose",
736
- "-f",
737
- composePath,
738
- "up",
739
- "-d",
740
- "--remove-orphans",
741
- "--wait",
742
- `--wait-timeout=${Math.floor(HEALTH_TIMEOUT_MS / 1e3)}`,
743
- ...services
744
- ]);
745
- },
746
- async copyOut(composePath, service, from, to) {
747
- await mkdir(dirname(to), { recursive: true });
748
- await run("docker", [
749
- "compose",
750
- "-f",
751
- composePath,
752
- "cp",
753
- `${service}:${from}`,
754
- to
755
- ]);
756
- },
757
- async reload(composePath, service) {
758
- await run("docker", [
759
- "compose",
760
- "-f",
761
- composePath,
762
- "exec",
763
- "-T",
764
- service,
765
- "caddy",
766
- "reload",
767
- "--config",
768
- "/etc/caddy/Caddyfile"
769
- ]);
770
- },
771
- async healthy(composePath, services) {
772
- if (services.length === 0) return true;
773
- try {
774
- const { stdout } = await run("docker", [
775
- "compose",
776
- "-f",
777
- composePath,
778
- "ps",
779
- "--format",
780
- "json",
781
- ...services
782
- ]);
783
- const running = /* @__PURE__ */ new Set();
784
- for (const line of stdout.trim().split("\n").filter(Boolean)) {
785
- const entry = JSON.parse(line);
786
- const ok = entry.State === "running" && (entry.Health === void 0 || entry.Health === "" || entry.Health === "healthy");
787
- if (ok && entry.Service) running.add(entry.Service);
788
- }
789
- return services.every((service) => running.has(service));
790
- } catch {
791
- return false;
792
- }
621
+ var PgBossNeedsDatabase = class extends Error {
622
+ ids;
623
+ constructor(ids) {
624
+ 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(", ")}.`);
625
+ this.ids = ids;
626
+ this.name = "PgBossNeedsDatabase";
793
627
  }
794
628
  };
795
-
796
629
  //#endregion
797
- //#region src/reconcile/plan.ts
630
+ //#region src/reconcile/env.ts
798
631
  /**
799
- * Which container serves a kind.
632
+ * The URLs a reconciled stage injects.
800
633
  *
801
- * Derived kinds map to the same container as their parent — a schema tenant
802
- * lives in its parent's Postgres — so the set is deduplicated rather than
803
- * counted.
634
+ * One `<NAME>_URL` per construct, and the protocol picks the driver. This is the
635
+ * seam between the target that writes a URL and the client that parses it, so
636
+ * every component of it is read off the resource — never inherited from ambient
637
+ * environment. A bucket can live in a different region than the function reading
638
+ * it, and `AWS_REGION` in a Lambda is the *function's* region, so a URL that
639
+ * omits `?region=` breaks cross-region silently at runtime.
640
+ *
641
+ * Locally that self-containment is what lets the port be whatever was free: the
642
+ * app reads `ORDERS_URL` and never sees a host or a port.
804
643
  */
805
- const CONTAINERS = {
806
- database: "postgres",
807
- "database-reader": "postgres",
808
- "database-schema": "postgres",
809
- objects: "minio",
810
- "file-server": "caddy",
811
- email: "mailpit"
812
- };
813
644
  /**
814
- * The container a cache backend needs locally.
645
+ * The credential local containers are brought up with.
815
646
  *
816
- * Each one matches the protocol its deployed form speaks, which is the property
817
- * worth having: a cache that behaves differently in the two places is worse than
818
- * a slower one.
647
+ * The design's `roles: false` case — both URLs fall back to the cluster's master
648
+ * credential. A deliberate downgrade, not a default: it holds until the role DDL
649
+ * lands in `@geekmidas/db`, at which point the runtime role's URL replaces this
650
+ * and the owner URL stops being reachable from any manifest edge.
651
+ */
652
+ const LOCAL_USER$2 = "geekmidas";
653
+ /** The host containers are published on. */
654
+ const LOCAL_HOST = "localhost";
655
+ /** The region local buckets claim, so the client has one to sign with. */
656
+ const LOCAL_REGION = "us-east-1";
657
+ /**
658
+ * The credentials the S3 client resolves for MinIO.
819
659
  *
820
- * `db` is deliberately absent, the same way `pgboss` is absent from
821
- * `EVENT_CONTAINERS`: the cache is a table in a database the manifest already
822
- * declares, so mapping it here would start a container for a project that needs
823
- * none.
660
+ * An `s3://` URL deliberately carries none: deployed, the SDK reads them from
661
+ * the execution role, so a URL that embedded a key would be one more thing to
662
+ * rotate and one more thing to leak into a log. Locally there is no role, and
663
+ * the same chain reads these — which is why they are injected beside the URL
664
+ * rather than written into it.
824
665
  */
825
- const CACHE_CONTAINERS = {
826
- upstash: "redis-http",
827
- elasticache: "redis"
666
+ const STORAGE_CREDENTIALS = {
667
+ AWS_ACCESS_KEY_ID: LOCAL_USER$2,
668
+ AWS_SECRET_ACCESS_KEY: LOCAL_USER$2,
669
+ AWS_REGION: LOCAL_REGION
828
670
  };
671
+ /** The token the local cache proxy accepts. Matches the compose definition. */
672
+ const LOCAL_TOKEN$1 = "geekmidas";
673
+ /** The schema pg-boss keeps its tables in, inside the declared database. */
674
+ const PGBOSS_SCHEMA = "pgboss";
675
+ /** The exchange every local topic and queue shares on RabbitMQ. */
676
+ const RABBITMQ_EXCHANGE = "geekmidas.events";
829
677
  /**
830
- * Kinds that resolve a value without anything running.
678
+ * Every env key a reconciled plan resolves, and its value.
831
679
  *
832
- * A secret is the case: it has no address, so there is nothing to start and
833
- * nothing to connect to — but it still appears in the plan, because the target
834
- * still has to resolve it.
680
+ * Keys come from the declaration rather than being re-derived, so what the build
681
+ * publishes and what the app reads cannot drift.
835
682
  */
836
- const CONTAINERLESS = {
837
- secret: true,
838
- credential: true,
839
- "rest-api": true,
840
- site: true
841
- };
683
+ function envFor(plan, options) {
684
+ const env = {};
685
+ const resolved = {};
686
+ for (const resource of plan.resources) {
687
+ const url = urlFor(resource, plan, options.ports, options.project ?? "", options.addresses);
688
+ if (url) resolved[resource.id] = url;
689
+ }
690
+ for (const resource of plan.resources) {
691
+ const url = resolved[resource.id];
692
+ if (resource.kind === "rest-api") Object.assign(env, surfaceEnv(resource, url, resolved));
693
+ if (url) env[resource.envKey] = url;
694
+ if (isDatabase(resource.kind) && resource.roles !== false) {
695
+ const owner = ownerUrl(resource, plan, options.ports, options.project ?? "");
696
+ if (owner) env[provideKey(resource.id, "ownerUrl")] = owner;
697
+ }
698
+ if (resource.kind === "email" && url) env[provideKey(resource.id, "from")] = options.mailFrom ?? `noreply@${LOCAL_HOST}`;
699
+ }
700
+ Object.assign(env, publicEnv(plan, env));
701
+ Object.assign(env, brokerEnv(plan, options.ports));
702
+ if (plan.resources.some((r) => r.kind === "objects" && env[r.envKey])) Object.assign(env, STORAGE_CREDENTIALS);
703
+ return env;
704
+ }
705
+ /**
706
+ * The values a site's bundler inlines, under the names it inlines them by.
707
+ *
708
+ * A rename and nothing more. `API_URL` was resolved once, by the same code that
709
+ * resolved it for the server; a site reads the same value under `VITE_API_URL`
710
+ * because that prefix is how its bundler is told to ship it. Nothing is derived
711
+ * twice, so a site and its API cannot come to disagree about where the API is.
712
+ *
713
+ * A source that resolved to nothing is skipped rather than written empty: an
714
+ * inlined empty string is a frontend that builds and then fails at runtime
715
+ * against `http:///`, where a missing variable fails at build with the name of
716
+ * the thing that is missing.
717
+ */
718
+ function publicEnv(plan, resolved) {
719
+ const env = {};
720
+ for (const resource of plan.resources) for (const [key, source] of Object.entries(resource.publicEnv ?? {})) {
721
+ const value = resolved[source];
722
+ if (value) env[key] = value;
723
+ }
724
+ return env;
725
+ }
726
+ /**
727
+ * What a surface publishes beyond its own address: who may call it, and where
728
+ * a cookie set by it is readable.
729
+ *
730
+ * Both are read off the same list — the constructs that declared an edge to
731
+ * this surface — which is why neither appears in application code. A surface
732
+ * that listed its own callers would be edited every time something new called
733
+ * it, and the thing being edited is already recorded in the graph.
734
+ *
735
+ * An empty origin list is written, and a surface with no address writes nothing
736
+ * at all. The two cases look alike and are not: "nothing depends on this yet" is
737
+ * a real state a target should publish, while "this surface has no address here"
738
+ * means it is not running in this stage, and keys belong with the address they
739
+ * describe.
740
+ */
741
+ function surfaceEnv(resource, url, resolved = {}) {
742
+ if (!url) return {};
743
+ const origins = [...new Set((resource.callers ?? []).map((caller) => resolved[caller]).filter((address) => Boolean(address)).map(originOf).filter((origin) => Boolean(origin)))].sort();
744
+ const domain = cookieDomain([url, ...origins]);
745
+ return {
746
+ [provideKey(resource.id, "trustedOrigins")]: origins.join(","),
747
+ ...domain ? { [provideKey(resource.id, "cookieDomain")]: domain } : {}
748
+ };
749
+ }
750
+ /** An address reduced to the origin a browser compares against. */
751
+ function originOf(address) {
752
+ try {
753
+ return new URL(address).origin;
754
+ } catch {
755
+ return;
756
+ }
757
+ }
842
758
  /**
843
- * Containers that cannot run alone.
759
+ * The runtime role of whatever a derived node hangs off.
844
760
  *
845
- * The one case is the cache: `serverless-redis-http` is a proxy and needs the
846
- * Redis it proxies. Kept here rather than in the compose definition so the plan
847
- * -- which is what decides what must be running -- still names everything.
761
+ * A reader's parent may itself be a schema tenant, so this follows `of` by one
762
+ * hop rather than assuming the cluster — the reader of a tenant reads that
763
+ * tenant's schema, not the database's.
848
764
  */
849
- const REQUIRES = { "redis-http": ["redis"] };
765
+ function parentRole(resource, plan) {
766
+ const parent = plan.resources.find((r) => r.id === resource.of);
767
+ return parent ? localRole(parent) : resource.name;
768
+ }
769
+ /** Whether a kind connects to Postgres and therefore has an owner role. */
770
+ function isDatabase(kind) {
771
+ return kind === "database" || kind === "database-schema";
772
+ }
850
773
  /**
851
- * The role each kind's one key plays, where it is not a URL.
774
+ * The URL a migrator connects with — the owner role, which can create, alter
775
+ * and drop.
852
776
  *
853
- * The twin of `ProvidesByKind` in `@geekmidas/manifest`: that is what a
854
- * construct declares, this is what the local target resolves, and both derive
855
- * the key from the same `provideKey` so they cannot disagree.
777
+ * Never in `provides`, which is the whole point. A handler that could reach this
778
+ * could `DROP TABLE`, and the security property the role split exists for is
779
+ * that it cannot: the value is injected by `gkm exec` for the migrate step and
780
+ * by nothing else.
856
781
  */
857
- const ROLES = {
858
- queue: "publisherConnectionString",
859
- topic: "publisherConnectionString",
860
- credential: "credential"
861
- };
862
- /** The kinds whose container is the events backend's rather than their own. */
863
- const EVENT_KINDS = {
864
- queue: true,
865
- topic: true
866
- };
782
+ function ownerUrl(resource, plan, ports, project) {
783
+ if (!resource.container) return void 0;
784
+ const port = ports[primaryPortKey(resource.container)];
785
+ if (port === void 0) return void 0;
786
+ const owner = ownerRole(localRole(resource));
787
+ return postgres(port, rootDatabase(resource, plan), {
788
+ user: owner,
789
+ password: localRolePassword(project, plan, owner)
790
+ });
791
+ }
867
792
  /**
868
- * Which kinds provision something *inside* their container.
793
+ * The shared broker keys, when anything is published or consumed.
869
794
  *
870
- * A database creates a database, a bucket creates a bucket; mail creates
871
- * nothing — Mailpit accepts whatever is sent to it and has no per-stage object
872
- * to name. Both still appear in the plan, because both still resolve a URL; the
873
- * flag is the difference between "must be running" and "must be created".
795
+ * A queue's own key is the producer's. These two are the *connection* itself,
796
+ * which locally is one broker for every queue and topic in the project: the
797
+ * generated pollers open a single connection and subscribe each worker by name
798
+ * on it. Deployed there is no such thing — a Lambda is handed its own event
799
+ * source — so this pair exists for the local target and says so.
874
800
  */
875
- const PROVISIONS = {
876
- database: true,
877
- "database-reader": true,
878
- "database-schema": true,
879
- objects: true,
880
- cache: true,
881
- "file-server": true
882
- };
801
+ function brokerEnv(plan, ports) {
802
+ const carrier = plan.resources.find((r) => r.kind === "queue" || r.kind === "topic");
803
+ if (!carrier) return {};
804
+ const publisher = urlFor(carrier, plan, ports, "");
805
+ if (!publisher) return {};
806
+ return {
807
+ EVENT_PUBLISHER_CONNECTION_STRING: publisher,
808
+ EVENT_SUBSCRIBER_CONNECTION_STRING: plan.events === "sns" ? publisher.replace(/^sns:/, "sqs:") : publisher
809
+ };
810
+ }
883
811
  /**
884
- * The container each events backend needs, where it needs one of its own.
885
- *
886
- * `pgboss` is deliberately absent rather than mapped to `postgres`: it is a
887
- * schema tenant in a database the manifest already declares, so mapping it here
888
- * would start a Postgres for a project that declared no database at all.
812
+ * The URL for one planned resource.
889
813
  *
890
- * This one comes from config rather than from a declaration because there are
891
- * no `topic` or `queue` kinds yet. When they land the mapping moves to
892
- * {@link CONTAINERS} keyed by kind, and this constant goes with them.
814
+ * Every part of it comes from the resource and the port it was published on.
893
815
  */
894
- const EVENT_CONTAINERS = {
895
- sns: "localstack",
896
- rabbitmq: "rabbitmq"
897
- };
898
- /** The backend a project gets when it declares queues or topics and says nothing. */
899
- const DEFAULT_EVENTS = "pgboss";
816
+ function urlFor(resource, plan, ports, project, addresses = {}) {
817
+ if (resource.kind === "secret") return localSecret(project, plan, resource);
818
+ if (resource.kind === "credential") return void 0;
819
+ if (resource.kind === "rest-api" || resource.kind === "site") {
820
+ const address = addresses[resource.id];
821
+ const edge = plan.containers.includes("caddy") ? ports[primaryPortKey("caddy")] : void 0;
822
+ return address && edge !== void 0 ? `https://${hostFor(resource, project)}:${edge}` : address;
823
+ }
824
+ if (!resource.container) return void 0;
825
+ const port = ports[primaryPortKey(resource.container)];
826
+ if (port === void 0) return void 0;
827
+ switch (resource.kind) {
828
+ case "database":
829
+ case "database-schema":
830
+ case "database-reader": {
831
+ const database = rootDatabase(resource, plan);
832
+ if (resource.roles === false) {
833
+ const schema = schemaOf(resource, plan);
834
+ return schema ? `${postgres(port, database)}?search_path=${schema}` : postgres(port, database);
835
+ }
836
+ const role = resource.kind === "database-reader" ? readerRole(parentRole(resource, plan)) : localRole(resource);
837
+ return postgres(port, database, {
838
+ user: role,
839
+ password: localRolePassword(project, plan, role)
840
+ });
841
+ }
842
+ case "email": return `smtp://${LOCAL_HOST}:${port}`;
843
+ case "objects": return `s3://${resource.name}?region=${LOCAL_REGION}&endpoint=http://${LOCAL_HOST}:${port}&forcePathStyle=true`;
844
+ case "file-server": {
845
+ const origin = plan.resources.find((r) => r.id === resource.of);
846
+ if (!origin) return void 0;
847
+ return plan.containers.includes("caddy") ? `https://${hostFor(resource, project)}:${port}` : `http://${LOCAL_HOST}:${port}/${origin.name}`;
848
+ }
849
+ case "cache":
850
+ if (resource.of) {
851
+ const parent = plan.resources.find((r) => r.id === resource.of);
852
+ const url = parent ? urlFor(parent, plan, ports, project, addresses) : void 0;
853
+ return url ? withTable(url, resource.table ?? cacheTable(resource.id)) : void 0;
854
+ }
855
+ switch (plan.cache) {
856
+ case "elasticache": return `redis://${LOCAL_HOST}:${port}`;
857
+ default: return `http://:${LOCAL_TOKEN$1}@${LOCAL_HOST}:${port}`;
858
+ }
859
+ case "queue":
860
+ case "topic": return broker(plan, port);
861
+ default: return;
862
+ }
863
+ }
900
864
  /**
901
- * How a resource of each kind separates its stage suffix.
865
+ * The producer's connection string, composed from the backend the plan chose.
902
866
  *
903
- * Postgres identifiers are snake_case and a hyphen would need quoting
904
- * everywhere; buckets and queues are kebab and disallow underscores in some
905
- * providers. The suffix is the same word either way.
867
+ * The protocol is what picks the transport, so a producer never branches: the
868
+ * same `.publish()` reaches pg-boss here and SQS deployed because the string it
869
+ * was handed said so.
906
870
  */
907
- const SEPARATORS = {
908
- database: "_",
909
- "database-reader": "_",
910
- "database-schema": "_",
911
- objects: "-"
912
- };
871
+ function broker(plan, port) {
872
+ switch (plan.events) {
873
+ case "pgboss": {
874
+ const database = plan.resources.find((r) => r.kind === "database");
875
+ if (!database) throw new PgBossNeedsDatabase([]);
876
+ return `pgboss://${LOCAL_USER$2}:${LOCAL_USER$2}@${LOCAL_HOST}:${port}/${database.name}?schema=${PGBOSS_SCHEMA}`;
877
+ }
878
+ case "rabbitmq": return `rabbitmq://${LOCAL_USER$2}:${LOCAL_USER$2}@${LOCAL_HOST}:${port}?exchange=${RABBITMQ_EXCHANGE}`;
879
+ case "sns": throw new UnprovisionedEventsBackend("sns");
880
+ }
881
+ }
882
+ /** A local Postgres URL, master credential and all. */
883
+ function postgres(port, database, credential = {
884
+ user: LOCAL_USER$2,
885
+ password: LOCAL_USER$2
886
+ }) {
887
+ return `postgres://${credential.user}:${credential.password}@${LOCAL_HOST}:${port}/${database}`;
888
+ }
913
889
  /**
914
- * The stage-scoped name for a resource.
890
+ * The role a handler connects as, for one database or tenant.
915
891
  *
916
- * The local stage carries no suffix, so `gkm dev` keeps using `orders` and a
917
- * developer's psql history, saved connections, and muscle memory survive a
918
- * change of stage name. Every other stage is suffixed.
892
+ * The construct's id, lowercased and stage-scoped exactly as its database or
893
+ * schema is — so `Orders` in the `test` stage connects as `orders_test`, and two
894
+ * stages sharing one cluster cannot share a credential. Roles are cluster-scoped
895
+ * in Postgres, which is why the suffix is not optional here the way it is for a
896
+ * schema.
919
897
  */
920
- function resourceName(id, kind, stage, localStage) {
921
- const base = id.toLowerCase();
922
- if (stage === localStage) return base;
923
- return `${base}${SEPARATORS[kind] ?? "-"}${stage}`;
898
+ function localRole(resource) {
899
+ return resource.name;
924
900
  }
925
901
  /**
926
- * The container a kind needs, or `undefined` if it needs none.
902
+ * The password for a derived local role.
927
903
  *
928
- * Queues and topics have no container of their own — they live in whichever
929
- * broker the project selected, which is why the backend is an argument here
930
- * rather than a second pass that adds containers after the fact.
904
+ * Derived rather than random, for the same reason the local secret is: a
905
+ * password that changed on every `gkm dev` would lock a developer out of the
906
+ * data they had a moment ago. Seeded by the project so two checkouts do not
907
+ * share one, and by the stage so `test` and `development` do not.
908
+ *
909
+ * It is a *local* credential by construction — the cluster is on loopback and
910
+ * the seed is a checkout path — and no deployed target uses this function.
931
911
  */
932
- function containerFor(kind, events = DEFAULT_EVENTS, cache = DEFAULT_CACHE.aws, derived = false, edge = true) {
933
- if (EVENT_KINDS[kind]) return EVENT_CONTAINERS[events] ?? CONTAINERS.database;
934
- if (kind === "file-server") return edge ? "caddy" : CONTAINERS.objects;
935
- if (kind === "cache") {
936
- if (derived) return CONTAINERS.database;
937
- return CACHE_CONTAINERS[cache] ?? CONTAINERS.database;
938
- }
939
- return CONTAINERS[kind];
912
+ function localRolePassword(project, plan, role) {
913
+ return createHash("sha256").update(`${project}:${plan.stage}:role:${role}`).digest("base64url").slice(0, 32);
940
914
  }
941
915
  /**
942
- * Build the plan for a manifest and stage.
916
+ * A cache's address: its database's URL, carrying the table it reads.
943
917
  *
944
- * `order` is the caller's provisioning order — parents before children — which
945
- * `provisionOrder` produces from the manifest. Passing it in keeps this
946
- * function free of the traversal and makes the ordering assertable on its own.
918
+ * Appended rather than kept beside the URL as a second key, for the reason the
919
+ * design gives everywhere else — an address and what it opens are one fact, and
920
+ * two keys are one more thing to keep in step.
947
921
  */
948
- function planFor(manifest, stage, order, options = {}) {
949
- const containers = /* @__PURE__ */ new Set();
950
- const resources = [];
951
- const events = options.events ?? DEFAULT_EVENTS;
952
- const cache = options.cache ?? DEFAULT_CACHE.aws;
953
- for (const id of order) {
954
- const declaration = manifest[id];
955
- if (!declaration) continue;
956
- const container = containerFor(declaration.kind, events, cache, "of" in declaration && typeof declaration.of === "string", options.edge !== false);
957
- if (!container && !CONTAINERLESS[declaration.kind]) continue;
958
- if (container) {
959
- containers.add(container);
960
- for (const required of REQUIRES[container] ?? []) containers.add(required);
961
- }
962
- resources.push({
963
- id,
964
- kind: declaration.kind,
965
- ...container ? { container } : {},
966
- name: resourceName(id, declaration.kind, stage, options.localStage),
967
- envKey: providedKeyFor(id, declaration.kind, ROLES[declaration.kind] ?? "url"),
968
- provisions: PROVISIONS[declaration.kind] === true,
969
- ...declaration.kind === "rest-api" ? { callers: dependentsOf(manifest, id) } : {},
970
- ...declaration.kind === "site" ? { publicEnv: publicEnvFor(declaration, manifest) } : {},
971
- ...declaration.kind === "file-server" && declaration.open?.length ? { open: declaration.open } : {},
972
- ..."of" in declaration ? { of: declaration.of } : {},
973
- ..."schema" in declaration && declaration.schema ? { schema: declaration.schema } : {},
974
- ..."version" in declaration && declaration.version ? { version: declaration.version } : {},
975
- ..."roles" in declaration && declaration.roles === false ? { roles: false } : {},
976
- ..."table" in declaration && declaration.table ? { table: declaration.table } : {}
977
- });
922
+ function withTable(url, table) {
923
+ const parsed = new URL(url);
924
+ parsed.searchParams.set("table", table);
925
+ return parsed.toString();
926
+ }
927
+ /**
928
+ * The physical database a derived resource lives in.
929
+ *
930
+ * Walks `of` to the top rather than reading the immediate parent, because a
931
+ * reader's parent may itself be a schema tenant — two hops from the database
932
+ * that actually exists.
933
+ */
934
+ function rootDatabase(resource, plan) {
935
+ const byId = new Map(plan.resources.map((r) => [r.id, r]));
936
+ let current = resource;
937
+ while (current.of) {
938
+ const parent = byId.get(current.of);
939
+ if (!parent) break;
940
+ current = parent;
978
941
  }
979
- if (options.edge !== false && resources.some((r) => EDGE_KINDS[r.kind] === true)) containers.add("caddy");
980
- for (const extra of options.extraContainers ?? []) containers.add(extra);
981
- 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));
982
- if (cache === "db") {
983
- const placeless = resources.filter((r) => r.kind === "cache" && !r.of);
984
- if (placeless.length > 0) {
985
- const databases = resources.filter((r) => r.kind === "database");
986
- const [only] = databases;
987
- if (!only) throw new CacheNeedsDatabase(resources.filter((r) => r.kind === "cache").map((r) => r.id));
988
- if (databases.length > 1) throw new CacheIsAmbiguous(placeless.map((r) => r.id), databases.map((r) => r.id));
989
- for (const resource of placeless) resource.of = only.id;
990
- }
942
+ return current.name;
943
+ }
944
+ /** The schema a derived resource puts on its search path, if it has one. */
945
+ function schemaOf(resource, plan) {
946
+ const byId = new Map(plan.resources.map((r) => [r.id, r]));
947
+ let current = resource;
948
+ while (current) {
949
+ if (current.kind === "database-schema") return current.schema;
950
+ current = current.of ? byId.get(current.of) : void 0;
991
951
  }
992
- return {
993
- stage,
994
- events,
995
- cache,
996
- containers: [...containers],
997
- resources
998
- };
999
952
  }
1000
953
  /**
1001
- * Queues or topics were declared with pg-boss and no database to hold them.
954
+ * A backend whose local addresses cannot be derived yet.
1002
955
  *
1003
- * Not a reconcile failure but a manifest one: the fix is a line of application
1004
- * code either way — declare a database, or select a backend that brings its own
1005
- * broker.
956
+ * SNS and SQS are addressed by ARN, which means the topic and queue have to be
957
+ * created in LocalStack before a URL can name them. Until that provisioning
958
+ * lands, saying so is better than handing out a string that fails on first use.
1006
959
  */
1007
- var CacheNeedsDatabase = class extends Error {
1008
- constructor(ids) {
1009
- super(`A cache backed by the database needs a declared database, and none was declared. Declare one, or set services.cache to 'upstash' or 'elasticache'. Caches affected: ${ids.join(", ")}.`);
1010
- this.ids = ids;
1011
- this.name = "CacheNeedsDatabase";
960
+ var UnprovisionedEventsBackend = class extends Error {
961
+ backend;
962
+ constructor(backend) {
963
+ super(`The local target cannot derive addresses for '${backend}' yet — its topics and queues are named by ARN, which nothing creates locally. Use 'pgboss' (the default) or 'rabbitmq' for local development.`);
964
+ this.backend = backend;
965
+ this.name = "UnprovisionedEventsBackend";
1012
966
  }
1013
967
  };
1014
968
  /**
1015
- * `services.cache: 'db'` in an app that declares more than one database.
969
+ * The value a secret resolves to locally.
1016
970
  *
1017
- * The config says *a* database and the graph offers several, so there is no
1018
- * answer to give — and the wrong kind of failure would be to pick one, since a
1019
- * cache silently landing in the wrong database is a bug that surfaces as
1020
- * missing rows much later. The fix is a stronger statement in application code:
1021
- * `orders.cache('Sessions')` names the database, which is a fact about the
1022
- * application rather than a deployment choice.
971
+ * A hash of what identifies it, so it is stable, distinct per project, stage
972
+ * and construct, and never written to disk as a literal. Deployed this is the
973
+ * one kind whose value does *not* come from here — a secret manager generates
974
+ * it once and the target reads it back.
1023
975
  */
1024
- var CacheIsAmbiguous = class extends Error {
1025
- constructor(ids, databases) {
1026
- 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 with services.cache. Caches affected: ${ids.join(", ")}.`);
1027
- this.ids = ids;
1028
- this.databases = databases;
1029
- this.name = "CacheIsAmbiguous";
1030
- }
1031
- };
1032
- var PgBossNeedsDatabase = class extends Error {
1033
- constructor(ids) {
1034
- super(`pg-boss keeps its queues in a declared database, and none was declared. Declare one, or set services.events to 'rabbitmq' or 'sns'. Queues and topics affected: ${ids.join(", ")}.`);
1035
- this.ids = ids;
1036
- this.name = "PgBossNeedsDatabase";
1037
- }
1038
- };
1039
-
976
+ function localSecret(project, plan, resource) {
977
+ return createHash("sha256").update(`${project}:${plan.stage}:${resource.id}`).digest("base64url").slice(0, 43);
978
+ }
1040
979
  //#endregion
1041
- //#region src/reconcile/env.ts
980
+ //#region src/reconcile/apps.ts
1042
981
  /**
1043
- * The credential local containers are brought up with.
982
+ * The apps, as services beside the containers their constructs derive.
1044
983
  *
1045
- * The design's `roles: false` case — both URLs fall back to the cluster's master
1046
- * credential. A deliberate downgrade, not a default: it holds until the role DDL
1047
- * lands in `@geekmidas/db`, at which point the runtime role's URL replaces this
1048
- * and the owner URL stops being reachable from any manifest edge.
984
+ * `gkm docker` used to write these by hand: every backend got
985
+ * `DATABASE_URL=postgresql://…@postgres:5432/app` and `REDIS_URL=redis://redis`,
986
+ * which was right only for a project whose single database happened to be
987
+ * called `Database` — an `Orders` database publishes `ORDERS_URL`, a schema
988
+ * tenant has a URL of its own, and buckets and mail were not wired at all.
989
+ *
990
+ * Now an app's environment is exactly what reconcile hands `gkm dev`: every key
991
+ * the declared constructs provide, from the same derivation. The only
992
+ * difference is where the addresses point — at the containers by service name
993
+ * on the compose network, rather than at `localhost` on the ports published for
994
+ * the host.
995
+ *
996
+ * Always from the *local* stage's plan, whichever stage is being reconciled:
997
+ * `gkm test` rewrites the same file, and running the tests must not quietly
998
+ * point these apps at the test databases.
1049
999
  */
1050
- const LOCAL_USER = "geekmidas";
1051
- /** The host containers are published on. */
1052
- const LOCAL_HOST = "localhost";
1053
- /** The region local buckets claim, so the client has one to sign with. */
1054
- const LOCAL_REGION = "us-east-1";
1000
+ /** The profile app services carry, so `gkm dev` starts only the containers. */
1001
+ const APPS_PROFILE = "apps";
1055
1002
  /**
1056
- * The credentials the S3 client resolves for MinIO.
1003
+ * Where an app's image is built from.
1057
1004
  *
1058
- * An `s3://` URL deliberately carries none: deployed, the SDK reads them from
1059
- * the execution role, so a URL that embedded a key would be one more thing to
1060
- * rotate and one more thing to leak into a log. Locally there is no role, and
1061
- * the same chain reads these — which is why they are injected beside the URL
1062
- * rather than written into it.
1005
+ * An app at the project root is a single-app project, whose `gkm docker`
1006
+ * writes `.gkm/docker/Dockerfile`; a workspace writes one per app.
1063
1007
  */
1064
- const STORAGE_CREDENTIALS = {
1065
- AWS_ACCESS_KEY_ID: LOCAL_USER,
1066
- AWS_SECRET_ACCESS_KEY: LOCAL_USER,
1067
- AWS_REGION: LOCAL_REGION
1068
- };
1069
- /** The token the local cache proxy accepts. Matches the compose definition. */
1070
- const LOCAL_TOKEN = "geekmidas";
1071
- /** The schema pg-boss keeps its tables in, inside the declared database. */
1072
- const PGBOSS_SCHEMA = "pgboss";
1073
- /** The exchange every local topic and queue shares on RabbitMQ. */
1074
- const RABBITMQ_EXCHANGE = "geekmidas.events";
1008
+ function dockerfileOf(appName, path) {
1009
+ return path === "." ? ".gkm/docker/Dockerfile" : `.gkm/docker/Dockerfile.${appName}`;
1010
+ }
1075
1011
  /**
1076
- * Every env key a reconciled plan resolves, and its value.
1012
+ * Every construct key, addressed across the compose network.
1077
1013
  *
1078
- * Keys come from the declaration rather than being re-derived, so what the build
1079
- * publishes and what the app reads cannot drift.
1014
+ * `envFor` is asked with a placeholder port for every port key, so each
1015
+ * `localhost:<placeholder>` in its answer names exactly one container port —
1016
+ * two containers listening on the same port inside cannot be confused — and is
1017
+ * rewritten to `<container>:<inside>`.
1080
1018
  */
1081
- function envFor(plan, options) {
1082
- const env = {};
1083
- const resolved = {};
1084
- for (const resource of plan.resources) {
1085
- const url = urlFor(resource, plan, options.ports, options.project ?? "", options.addresses);
1086
- if (url) resolved[resource.id] = url;
1019
+ function inNetworkEnv(workspace, manifest) {
1020
+ const plan = planFor(manifest, workspace.stages.local, provisionOrder(manifest), {
1021
+ localStage: workspace.stages.local,
1022
+ ...backendsOf(workspace)
1023
+ });
1024
+ const placeholders = {};
1025
+ const targets = /* @__PURE__ */ new Map();
1026
+ let next = 49152;
1027
+ for (const container of [...plan.containers].sort()) for (const port of portsOf(container)) {
1028
+ placeholders[port.key] = next;
1029
+ targets.set(next, `${container}:${port.inside}`);
1030
+ next += 1;
1087
1031
  }
1088
- for (const resource of plan.resources) {
1089
- const url = resolved[resource.id];
1090
- if (resource.kind === "rest-api") Object.assign(env, surfaceEnv(resource, url, resolved));
1091
- if (url) env[resource.envKey] = url;
1092
- if (isDatabase(resource.kind) && resource.roles !== false) {
1093
- const owner = ownerUrl(resource, plan, options.ports, options.project ?? "");
1094
- if (owner) env[provideKey(resource.id, "ownerUrl")] = owner;
1095
- }
1096
- if (resource.kind === "email" && url) env[provideKey(resource.id, "from")] = options.mailFrom ?? `noreply@${LOCAL_HOST}`;
1032
+ for (const key of portKeys(plan.containers)) placeholders[key] ??= next++;
1033
+ const env = envFor(plan, {
1034
+ ports: placeholders,
1035
+ project: workspace.name,
1036
+ addresses: surfaceAddresses(workspace, manifest, (app, port) => `http://${app}:${port}`)
1037
+ });
1038
+ const rewrite = (value) => value.replace(/(?:localhost|127\.0\.0\.1):(\d+)/g, (match, port) => {
1039
+ return targets.get(Number(port)) ?? match;
1040
+ });
1041
+ return Object.fromEntries(Object.entries(env).map(([key, value]) => [key, rewrite(value)]));
1042
+ }
1043
+ /** One compose service per app the workspace runs, behind the apps profile. */
1044
+ function appServices(workspace, manifest, containers) {
1045
+ const env = inNetworkEnv(workspace, manifest);
1046
+ const services = {};
1047
+ for (const [name, app] of Object.entries(workspace.apps)) {
1048
+ if (app.type === "mobile" || !app.port) continue;
1049
+ const key = appKey(name);
1050
+ const health = app.type === "web" ? "/" : "/health";
1051
+ services[key] = {
1052
+ image: `${key}:\${TAG:-latest}`,
1053
+ build: {
1054
+ context: ".",
1055
+ dockerfile: dockerfileOf(name, app.path)
1056
+ },
1057
+ profiles: [APPS_PROFILE],
1058
+ ports: [`${app.port}:${app.port}`],
1059
+ environment: {
1060
+ NODE_ENV: "production",
1061
+ PORT: String(app.port),
1062
+ ...env
1063
+ },
1064
+ ...containers.some((c) => c !== "caddy") ? { depends_on: containers.filter((c) => c !== "caddy") } : {},
1065
+ healthcheck: {
1066
+ test: [
1067
+ "CMD",
1068
+ "wget",
1069
+ "-q",
1070
+ "--spider",
1071
+ `http://localhost:${app.port}${health}`
1072
+ ],
1073
+ interval: "10s",
1074
+ timeout: "5s",
1075
+ retries: 5
1076
+ }
1077
+ };
1097
1078
  }
1098
- Object.assign(env, publicEnv(plan, env));
1099
- Object.assign(env, brokerEnv(plan, options.ports));
1100
- if (plan.resources.some((r) => r.kind === "objects" && env[r.envKey])) Object.assign(env, STORAGE_CREDENTIALS);
1101
- return env;
1079
+ return services;
1102
1080
  }
1081
+ //#endregion
1082
+ //#region src/reconcile/clients.ts
1103
1083
  /**
1104
- * The values a site's bundler inlines, under the names it inlines them by.
1084
+ * The real Postgres and MinIO clients the applier runs against.
1105
1085
  *
1106
- * A rename and nothing more. `API_URL` was resolved once, by the same code that
1107
- * resolved it for the server; a site reads the same value under `VITE_API_URL`
1108
- * because that prefix is how its bundler is told to ship it. Nothing is derived
1109
- * twice, so a site and its API cannot come to disagree about where the API is.
1086
+ * Kept apart from `provision.ts` so the rules there — what gets created, in what
1087
+ * order, and never dropping anything — are asserted against fakes, while the
1088
+ * drivers live here where there is nothing to assert but wiring.
1110
1089
  *
1111
- * A source that resolved to nothing is skipped rather than written empty: an
1112
- * inlined empty string is a frontend that builds and then fails at runtime
1113
- * against `http:///`, where a missing variable fails at build with the name of
1114
- * the thing that is missing.
1090
+ * Both are constructed from the same local addresses the env writer composes,
1091
+ * so the applier cannot end up talking to a different container than the app.
1115
1092
  */
1116
- function publicEnv(plan, resolved) {
1117
- const env = {};
1118
- for (const resource of plan.resources) for (const [key, source] of Object.entries(resource.publicEnv ?? {})) {
1119
- const value = resolved[source];
1120
- if (value) env[key] = value;
1121
- }
1122
- return env;
1123
- }
1093
+ /** The credential local containers are brought up with. */
1094
+ const LOCAL_USER$1 = "geekmidas";
1124
1095
  /**
1125
- * What a surface publishes beyond its own address: who may call it, and where
1126
- * a cookie set by it is readable.
1127
- *
1128
- * Both are read off the same list — the constructs that declared an edge to
1129
- * this surface — which is why neither appears in application code. A surface
1130
- * that listed its own callers would be edited every time something new called
1131
- * it, and the thing being edited is already recorded in the graph.
1096
+ * A Postgres client that opens one connection per database it is asked about.
1132
1097
  *
1133
- * An empty origin list is written, and a surface with no address writes nothing
1134
- * at all. The two cases look alike and are not: "nothing depends on this yet" is
1135
- * a real state a target should publish, while "this surface has no address here"
1136
- * means it is not running in this stage, and keys belong with the address they
1137
- * describe.
1098
+ * `CREATE DATABASE` cannot run from a connection to the database being created,
1099
+ * and a schema must be created from inside its own database — so the database is
1100
+ * part of each query rather than fixed when the client is built.
1138
1101
  */
1139
- function surfaceEnv(resource, url, resolved = {}) {
1140
- if (!url) return {};
1141
- const origins = [...new Set((resource.callers ?? []).map((caller) => resolved[caller]).filter((address) => Boolean(address)).map(originOf).filter((origin) => Boolean(origin)))].sort();
1142
- const domain = cookieDomain([url, ...origins]);
1102
+ function pgClient(port) {
1103
+ return { async query(database, sql, values) {
1104
+ const client = new Client({
1105
+ host: "localhost",
1106
+ port,
1107
+ user: LOCAL_USER$1,
1108
+ password: LOCAL_USER$1,
1109
+ database: database ?? LOCAL_USER$1
1110
+ });
1111
+ await client.connect();
1112
+ try {
1113
+ return (await client.query(sql, values)).rows;
1114
+ } finally {
1115
+ await client.end();
1116
+ }
1117
+ } };
1118
+ }
1119
+ /** A bucket client pointed at the local MinIO. */
1120
+ function bucketClient(port) {
1121
+ const s3 = new S3Client({
1122
+ region: "us-east-1",
1123
+ endpoint: `http://localhost:${port}`,
1124
+ forcePathStyle: true,
1125
+ credentials: {
1126
+ accessKeyId: LOCAL_USER$1,
1127
+ secretAccessKey: LOCAL_USER$1
1128
+ }
1129
+ });
1143
1130
  return {
1144
- [provideKey(resource.id, "trustedOrigins")]: origins.join(","),
1145
- ...domain ? { [provideKey(resource.id, "cookieDomain")]: domain } : {}
1131
+ async exists(bucket) {
1132
+ try {
1133
+ await s3.send(new HeadBucketCommand({ Bucket: bucket }));
1134
+ return true;
1135
+ } catch {
1136
+ return false;
1137
+ }
1138
+ },
1139
+ async create(bucket) {
1140
+ await s3.send(new CreateBucketCommand({ Bucket: bucket }));
1141
+ },
1142
+ async policy(bucket) {
1143
+ try {
1144
+ return (await s3.send(new GetBucketPolicyCommand({ Bucket: bucket }))).Policy;
1145
+ } catch {
1146
+ return;
1147
+ }
1148
+ },
1149
+ async setPolicy(bucket, policy) {
1150
+ await s3.send(new PutBucketPolicyCommand({
1151
+ Bucket: bucket,
1152
+ Policy: policy
1153
+ }));
1154
+ }
1146
1155
  };
1147
1156
  }
1148
- /** An address reduced to the origin a browser compares against. */
1149
- function originOf(address) {
1150
- try {
1151
- return new URL(address).origin;
1152
- } catch {
1153
- return void 0;
1154
- }
1155
- }
1157
+ //#endregion
1158
+ //#region src/reconcile/compose.ts
1156
1159
  /**
1157
- * The runtime role of whatever a derived node hangs off.
1160
+ * The compose document a plan implies.
1158
1161
  *
1159
- * A reader's parent may itself be a schema tenant, so this follows `of` by one
1160
- * hop rather than assuming the cluster — the reader of a tenant reads that
1161
- * tenant's schema, not the database's.
1162
+ * The third declaration of the same containers disappears here: today a
1163
+ * `services: ['postgres','minio']` block is kept in step with the code by hand,
1164
+ * which is the triplication this design exists to remove, in the one place you
1165
+ * touch every day. A `database` implies Postgres; the list is derived.
1166
+ *
1167
+ * Returns an object rather than a string for the same reason `plan` is pure —
1168
+ * what should exist is asserted as data, and turning it into YAML is one line at
1169
+ * the edge.
1170
+ *
1171
+ * What stays config is unchanged: images and versions are pinned explicitly, and
1172
+ * a container no construct implies is still declared by hand. Neutral facts come
1173
+ * from the declaration, provider detail from config.
1162
1174
  */
1163
- function parentRole(resource, plan) {
1164
- const parent = plan.resources.find((r) => r.id === resource.of);
1165
- return parent ? localRole(parent) : resource.name;
1166
- }
1167
- /** Whether a kind connects to Postgres and therefore has an owner role. */
1168
- function isDatabase(kind) {
1169
- return kind === "database" || kind === "database-schema";
1170
- }
1175
+ /** The credential every derived container is brought up with locally. */
1176
+ const LOCAL_USER = "geekmidas";
1171
1177
  /**
1172
- * The URL a migrator connects with — the owner role, which can create, alter
1173
- * and drop.
1178
+ * The token the local cache accepts.
1174
1179
  *
1175
- * Never in `provides`, which is the whole point. A handler that could reach this
1176
- * could `DROP TABLE`, and the security property the role split exists for is
1177
- * that it cannot: the value is injected by `gkm exec` for the migrate step and
1178
- * by nothing else.
1180
+ * Fixed rather than generated: it is only ever reachable on a port this project
1181
+ * published, and the URL that carries it is derived, so nothing is made safer
1182
+ * by making it unguessable and a stable one keeps `redis-cli` sessions working.
1179
1183
  */
1180
- function ownerUrl(resource, plan, ports, project) {
1181
- if (!resource.container) return void 0;
1182
- const port = ports[primaryPortKey(resource.container)];
1183
- if (port === void 0) return void 0;
1184
- const owner = ownerRole(localRole(resource));
1185
- return postgres(port, rootDatabase(resource, plan), {
1186
- user: owner,
1187
- password: localRolePassword(project, plan, owner)
1188
- });
1189
- }
1184
+ const LOCAL_TOKEN = "geekmidas";
1190
1185
  /**
1191
- * The shared broker keys, when anything is published or consumed.
1186
+ * Build the compose document for a plan.
1192
1187
  *
1193
- * A queue's own key is the producer's. These two are the *connection* itself,
1194
- * which locally is one broker for every queue and topic in the project: the
1195
- * generated pollers open a single connection and subscribe each worker by name
1196
- * on it. Deployed there is no such thing — a Lambda is handed its own event
1197
- * source — so this pair exists for the local target and says so.
1188
+ * Every container is published on its assigned port, never on the image's
1189
+ * default: the point of allocation is that two projects can run at once, and
1190
+ * one container publishing 5432 puts that back.
1198
1191
  */
1199
- function brokerEnv(plan, ports) {
1200
- const carrier = plan.resources.find((r) => r.kind === "queue" || r.kind === "topic");
1201
- if (!carrier) return {};
1202
- const publisher = urlFor(carrier, plan, ports, "");
1203
- if (!publisher) return {};
1192
+ function composeFor(plan, options) {
1193
+ const services = {};
1194
+ const volumes = {};
1195
+ for (const container of [...plan.containers].sort()) {
1196
+ const declared = container === "postgres" ? plan.resources.find((r) => r.version)?.version : void 0;
1197
+ const image = declared ? postgresImage(declared) : DEFAULT_IMAGES[container];
1198
+ if (!image) throw new UnknownContainer(container);
1199
+ services[container] = define(container, image, publish(container, options.ports));
1200
+ const volume = volumeOf(container);
1201
+ if (volume) volumes[volume] = {};
1202
+ }
1204
1203
  return {
1205
- EVENT_PUBLISHER_CONNECTION_STRING: publisher,
1206
- EVENT_SUBSCRIBER_CONNECTION_STRING: plan.events === "sns" ? publisher.replace(/^sns:/, "sqs:") : publisher
1204
+ name: options.project,
1205
+ services: {
1206
+ ...services,
1207
+ ...options.apps
1208
+ },
1209
+ ...Object.keys(volumes).length ? { volumes } : {}
1207
1210
  };
1208
1211
  }
1209
- /**
1210
- * The URL for one planned resource.
1211
- *
1212
- * Every part of it comes from the resource and the port it was published on.
1213
- */
1214
- function urlFor(resource, plan, ports, project, addresses = {}) {
1215
- if (resource.kind === "secret") return localSecret(project, plan, resource);
1216
- if (resource.kind === "credential") return void 0;
1217
- if (resource.kind === "rest-api" || resource.kind === "site") {
1218
- const address = addresses[resource.id];
1219
- const edge = plan.containers.includes("caddy") ? ports[primaryPortKey("caddy")] : void 0;
1220
- return address && edge !== void 0 ? `https://${hostFor(resource, project)}:${edge}` : address;
1212
+ /** Render a compose document as the file it becomes. */
1213
+ function toYaml(file) {
1214
+ return `# Generated by gkm from the construct manifest — do not edit.
1215
+ # Containers are derived from what the app declares. Image pins and anything
1216
+ # no construct implies go in your own docker-compose.yml, which is merged over
1217
+ # this file and wins:
1218
+ #
1219
+ # docker compose -f docker-compose.constructs.yml -f docker-compose.yml …
1220
+ #
1221
+ # "gkm dev" and "gkm test" start the containers. The apps sit behind the
1222
+ # "apps" profile: run "gkm setup", then
1223
+ # docker compose -f docker-compose.constructs.yml --profile apps up --build
1224
+ ${stringify(file, { lineWidth: 0 })}`;
1225
+ }
1226
+ /** A container with no image, from config or from the defaults. */
1227
+ var UnknownContainer = class extends Error {
1228
+ /** The container that could not be resolved. */
1229
+ container;
1230
+ /** The containers that have a default, so the caller can see the set. */
1231
+ known = Object.keys(DEFAULT_IMAGES).sort();
1232
+ constructor(container) {
1233
+ super("No image is known for this container; pin one in config");
1234
+ this.name = "UnknownContainer";
1235
+ this.container = container;
1221
1236
  }
1222
- if (!resource.container) return void 0;
1223
- const port = ports[primaryPortKey(resource.container)];
1224
- if (port === void 0) return void 0;
1225
- switch (resource.kind) {
1226
- case "database":
1227
- case "database-schema":
1228
- case "database-reader": {
1229
- const database = rootDatabase(resource, plan);
1230
- if (resource.roles === false) {
1231
- const schema = schemaOf(resource, plan);
1232
- return schema ? `${postgres(port, database)}?search_path=${schema}` : postgres(port, database);
1237
+ };
1238
+ /** One container's definition, published on the port it was assigned. */
1239
+ function define(container, image, published) {
1240
+ switch (container) {
1241
+ case "postgres": return {
1242
+ image,
1243
+ restart: "unless-stopped",
1244
+ ports: published,
1245
+ environment: {
1246
+ POSTGRES_USER: LOCAL_USER,
1247
+ POSTGRES_PASSWORD: LOCAL_USER,
1248
+ POSTGRES_DB: LOCAL_USER
1249
+ },
1250
+ volumes: ["postgres-data:/var/lib/postgresql"],
1251
+ healthcheck: {
1252
+ test: ["CMD-SHELL", `pg_isready -U ${LOCAL_USER}`],
1253
+ interval: "5s",
1254
+ timeout: "3s",
1255
+ retries: 10,
1256
+ start_period: "10s"
1233
1257
  }
1234
- const role = resource.kind === "database-reader" ? readerRole(parentRole(resource, plan)) : localRole(resource);
1235
- return postgres(port, database, {
1236
- user: role,
1237
- password: localRolePassword(project, plan, role)
1238
- });
1239
- }
1240
- case "email": return `smtp://${LOCAL_HOST}:${port}`;
1241
- case "objects": return `s3://${resource.name}?region=${LOCAL_REGION}&endpoint=http://${LOCAL_HOST}:${port}&forcePathStyle=true`;
1242
- case "file-server": {
1243
- const origin = plan.resources.find((r) => r.id === resource.of);
1244
- if (!origin) return void 0;
1245
- return plan.containers.includes("caddy") ? `https://${hostFor(resource, project)}:${port}` : `http://${LOCAL_HOST}:${port}/${origin.name}`;
1246
- }
1247
- case "cache":
1248
- if (resource.of) {
1249
- const parent = plan.resources.find((r) => r.id === resource.of);
1250
- const url = parent ? urlFor(parent, plan, ports, project, addresses) : void 0;
1251
- return url ? withTable(url, resource.table ?? cacheTable(resource.id)) : void 0;
1258
+ };
1259
+ case "minio": return {
1260
+ image,
1261
+ restart: "unless-stopped",
1262
+ ports: published,
1263
+ environment: {
1264
+ MINIO_ROOT_USER: LOCAL_USER,
1265
+ MINIO_ROOT_PASSWORD: LOCAL_USER
1266
+ },
1267
+ command: "server --console-address \":9001\" /data",
1268
+ volumes: ["minio-data:/data"],
1269
+ healthcheck: {
1270
+ test: [
1271
+ "CMD",
1272
+ "mc",
1273
+ "ready",
1274
+ "local"
1275
+ ],
1276
+ interval: "10s",
1277
+ timeout: "5s",
1278
+ retries: 5,
1279
+ start_period: "10s"
1280
+ }
1281
+ };
1282
+ case "caddy": return {
1283
+ image,
1284
+ restart: "unless-stopped",
1285
+ ports: published,
1286
+ volumes: [
1287
+ "./.gkm/Caddyfile:/etc/caddy/Caddyfile:ro",
1288
+ "./.gkm/caddy-sites:/etc/caddy/sites:ro",
1289
+ "caddy-data:/data"
1290
+ ],
1291
+ extra_hosts: ["host.docker.internal:host-gateway"],
1292
+ healthcheck: {
1293
+ test: [
1294
+ "CMD",
1295
+ "caddy",
1296
+ "version"
1297
+ ],
1298
+ interval: "10s",
1299
+ timeout: "5s",
1300
+ retries: 5
1301
+ }
1302
+ };
1303
+ case "mailpit": return {
1304
+ image,
1305
+ restart: "unless-stopped",
1306
+ ports: published,
1307
+ environment: {
1308
+ MP_SMTP_AUTH_ACCEPT_ANY: "1",
1309
+ MP_SMTP_AUTH_ALLOW_INSECURE: "1"
1310
+ },
1311
+ healthcheck: {
1312
+ test: [
1313
+ "CMD",
1314
+ "wget",
1315
+ "-q",
1316
+ "--spider",
1317
+ "http://localhost:8025"
1318
+ ],
1319
+ interval: "10s",
1320
+ timeout: "5s",
1321
+ retries: 5
1322
+ }
1323
+ };
1324
+ case "redis": return {
1325
+ image,
1326
+ restart: "unless-stopped",
1327
+ ports: published,
1328
+ volumes: ["redis-data:/data"],
1329
+ healthcheck: {
1330
+ test: [
1331
+ "CMD",
1332
+ "redis-cli",
1333
+ "ping"
1334
+ ],
1335
+ interval: "5s",
1336
+ timeout: "3s",
1337
+ retries: 5
1338
+ }
1339
+ };
1340
+ case "redis-http": return {
1341
+ image,
1342
+ restart: "unless-stopped",
1343
+ ports: published,
1344
+ environment: {
1345
+ SRH_MODE: "env",
1346
+ SRH_TOKEN: LOCAL_TOKEN,
1347
+ SRH_CONNECTION_STRING: "redis://redis:6379"
1348
+ },
1349
+ depends_on: ["redis"],
1350
+ healthcheck: {
1351
+ test: [
1352
+ "CMD",
1353
+ "wget",
1354
+ "-q",
1355
+ "-O",
1356
+ "/dev/null",
1357
+ "http://127.0.0.1:80/"
1358
+ ],
1359
+ interval: "5s",
1360
+ timeout: "3s",
1361
+ retries: 10
1362
+ }
1363
+ };
1364
+ case "rabbitmq": return {
1365
+ image,
1366
+ restart: "unless-stopped",
1367
+ ports: published,
1368
+ environment: {
1369
+ RABBITMQ_DEFAULT_USER: LOCAL_USER,
1370
+ RABBITMQ_DEFAULT_PASS: LOCAL_USER
1371
+ },
1372
+ volumes: ["rabbitmq-data:/var/lib/rabbitmq"],
1373
+ healthcheck: {
1374
+ test: [
1375
+ "CMD",
1376
+ "rabbitmq-diagnostics",
1377
+ "-q",
1378
+ "ping"
1379
+ ],
1380
+ interval: "10s",
1381
+ timeout: "5s",
1382
+ retries: 5
1252
1383
  }
1253
- switch (plan.cache) {
1254
- case "elasticache": return `redis://${LOCAL_HOST}:${port}`;
1255
- default: return `http://:${LOCAL_TOKEN}@${LOCAL_HOST}:${port}`;
1384
+ };
1385
+ case "localstack": return {
1386
+ image,
1387
+ restart: "unless-stopped",
1388
+ ports: published,
1389
+ environment: {
1390
+ AWS_DEFAULT_REGION: "us-east-1",
1391
+ AWS_ACCESS_KEY_ID: "LSIAQAAAAAAVNCBMPNSG",
1392
+ AWS_SECRET_ACCESS_KEY: LOCAL_USER
1393
+ },
1394
+ volumes: ["localstack-data:/var/lib/localstack"],
1395
+ healthcheck: {
1396
+ test: [
1397
+ "CMD",
1398
+ "curl",
1399
+ "-f",
1400
+ "http://localhost:4566/_localstack/health"
1401
+ ],
1402
+ interval: "10s",
1403
+ timeout: "5s",
1404
+ retries: 5,
1405
+ start_period: "10s"
1256
1406
  }
1257
- case "queue":
1258
- case "topic": return broker(plan, port);
1259
- default: return void 0;
1260
- }
1261
- }
1262
- /**
1263
- * The producer's connection string, composed from the backend the plan chose.
1264
- *
1265
- * The protocol is what picks the transport, so a producer never branches: the
1266
- * same `.publish()` reaches pg-boss here and SQS deployed because the string it
1267
- * was handed said so.
1268
- */
1269
- function broker(plan, port) {
1270
- switch (plan.events) {
1271
- case "pgboss": {
1272
- const database = plan.resources.find((r) => r.kind === "database");
1273
- if (!database) throw new PgBossNeedsDatabase([]);
1274
- return `pgboss://${LOCAL_USER}:${LOCAL_USER}@${LOCAL_HOST}:${port}/${database.name}?schema=${PGBOSS_SCHEMA}`;
1275
- }
1276
- case "rabbitmq": return `rabbitmq://${LOCAL_USER}:${LOCAL_USER}@${LOCAL_HOST}:${port}?exchange=${RABBITMQ_EXCHANGE}`;
1277
- case "sns": throw new UnprovisionedEventsBackend("sns");
1407
+ };
1408
+ default: throw new UnknownContainer(container);
1278
1409
  }
1279
1410
  }
1280
- /** A local Postgres URL, master credential and all. */
1281
- function postgres(port, database, credential = {
1282
- user: LOCAL_USER,
1283
- password: LOCAL_USER
1284
- }) {
1285
- return `postgres://${credential.user}:${credential.password}@${LOCAL_HOST}:${port}/${database}`;
1286
- }
1287
1411
  /**
1288
- * The role a handler connects as, for one database or tenant.
1412
+ * The published port mappings for a container.
1289
1413
  *
1290
- * The construct's id, lowercased and stage-scoped exactly as its database or
1291
- * schema is — so `Orders` in the `test` stage connects as `orders_test`, and two
1292
- * stages sharing one cluster cannot share a credential. Roles are cluster-scoped
1293
- * in Postgres, which is why the suffix is not optional here the way it is for a
1294
- * schema.
1414
+ * Every port is looked up by its own key rather than being derived as an offset
1415
+ * from the container's first, so MinIO's console cannot land on whatever was
1416
+ * allocated to the next container.
1295
1417
  */
1296
- function localRole(resource) {
1297
- return resource.name;
1418
+ function publish(container, ports) {
1419
+ return portsOf(container).map((port) => {
1420
+ const assigned = ports[port.key];
1421
+ if (assigned === void 0) throw new UnassignedPort(port.key);
1422
+ return `${assigned}:${port.inside}`;
1423
+ });
1298
1424
  }
1425
+ /** A planned port that allocation never assigned. */
1426
+ var UnassignedPort = class extends Error {
1427
+ /** The port key that went unassigned, e.g. `minio-console`. */
1428
+ key;
1429
+ constructor(key) {
1430
+ super("No port was assigned for this container port");
1431
+ this.name = "UnassignedPort";
1432
+ this.key = key;
1433
+ }
1434
+ };
1435
+ //#endregion
1436
+ //#region src/reconcile/docker.ts
1299
1437
  /**
1300
- * The password for a derived local role.
1301
- *
1302
- * Derived rather than random, for the same reason the local secret is: a
1303
- * password that changed on every `gkm dev` would lock a developer out of the
1304
- * data they had a moment ago. Seeded by the project so two checkouts do not
1305
- * share one, and by the stage so `test` and `development` do not.
1438
+ * The real Docker, behind the small interface reconcile needs.
1306
1439
  *
1307
- * It is a *local* credential by construction — the cluster is on loopback and
1308
- * the seed is a checkout path — and no deployed target uses this function.
1309
- */
1310
- function localRolePassword(project, plan, role) {
1311
- return createHash("sha256").update(`${project}:${plan.stage}:role:${role}`).digest("base64url").slice(0, 32);
1312
- }
1313
- /**
1314
- * A cache's address: its database's URL, carrying the table it reads.
1440
+ * Shells out to `docker compose` rather than talking to the daemon socket: it is
1441
+ * what a developer runs by hand when something is wrong, so the two cannot
1442
+ * disagree, and it keeps Docker out of this package's dependencies.
1315
1443
  *
1316
- * Appended rather than kept beside the URL as a second key, for the reason the
1317
- * design gives everywhere else — an address and what it opens are one fact, and
1318
- * two keys are one more thing to keep in step.
1444
+ * Every call is scoped to the generated compose file. Nothing here can touch a
1445
+ * container this project did not generate.
1319
1446
  */
1320
- function withTable(url, table) {
1321
- const parsed = new URL(url);
1322
- parsed.searchParams.set("table", table);
1323
- return parsed.toString();
1324
- }
1447
+ const run = promisify(execFile);
1325
1448
  /**
1326
- * The physical database a derived resource lives in.
1449
+ * The generated file, then the project's own if it has one.
1327
1450
  *
1328
- * Walks `of` to the top rather than reading the immediate parent, because a
1329
- * reader's parent may itself be a schema tenant — two hops from the database
1330
- * that actually exists.
1451
+ * Merged in that order so the project's wins — an `image:` there pins a
1452
+ * container this file derived, and a service there is added beside them.
1331
1453
  */
1332
- function rootDatabase(resource, plan) {
1333
- const byId = new Map(plan.resources.map((r) => [r.id, r]));
1334
- let current = resource;
1335
- while (current.of) {
1336
- const parent = byId.get(current.of);
1337
- if (!parent) break;
1338
- current = parent;
1339
- }
1340
- return current.name;
1341
- }
1342
- /** The schema a derived resource puts on its search path, if it has one. */
1343
- function schemaOf(resource, plan) {
1344
- const byId = new Map(plan.resources.map((r) => [r.id, r]));
1345
- let current = resource;
1346
- while (current) {
1347
- if (current.kind === "database-schema") return current.schema;
1348
- current = current.of ? byId.get(current.of) : void 0;
1349
- }
1350
- return void 0;
1454
+ function composeFiles(composePath) {
1455
+ const project = join(dirname(composePath), "docker-compose.yml");
1456
+ return [
1457
+ "-f",
1458
+ composePath,
1459
+ ...existsSync(project) ? ["-f", project] : []
1460
+ ];
1351
1461
  }
1352
- /**
1353
- * A backend whose local addresses cannot be derived yet.
1354
- *
1355
- * SNS and SQS are addressed by ARN, which means the topic and queue have to be
1356
- * created in LocalStack before a URL can name them. Until that provisioning
1357
- * lands, saying so is better than handing out a string that fails on first use.
1358
- */
1359
- var UnprovisionedEventsBackend = class extends Error {
1360
- constructor(backend) {
1361
- super(`The local target cannot derive addresses for '${backend}' yet — its topics and queues are named by ARN, which nothing creates locally. Use 'pgboss' (the default) or 'rabbitmq' for local development.`);
1362
- this.backend = backend;
1363
- this.name = "UnprovisionedEventsBackend";
1462
+ /** How long to wait for containers to pass their health checks. */
1463
+ const HEALTH_TIMEOUT_MS = 12e4;
1464
+ const dockerCli = {
1465
+ async publishedPort(composePath, service, inside) {
1466
+ try {
1467
+ const { stdout } = await run("docker", [
1468
+ "compose",
1469
+ ...composeFiles(composePath),
1470
+ "port",
1471
+ service,
1472
+ String(inside)
1473
+ ]);
1474
+ const port = Number(stdout.trim().split(":").pop());
1475
+ return Number.isFinite(port) && port > 0 ? port : void 0;
1476
+ } catch {
1477
+ return;
1478
+ }
1479
+ },
1480
+ async up(composePath, services) {
1481
+ await run("docker", [
1482
+ "compose",
1483
+ ...composeFiles(composePath),
1484
+ "up",
1485
+ "-d",
1486
+ "--remove-orphans",
1487
+ "--wait",
1488
+ `--wait-timeout=${Math.floor(HEALTH_TIMEOUT_MS / 1e3)}`,
1489
+ ...services
1490
+ ]);
1491
+ },
1492
+ async copyOut(composePath, service, from, to) {
1493
+ await mkdir(dirname(to), { recursive: true });
1494
+ await run("docker", [
1495
+ "compose",
1496
+ ...composeFiles(composePath),
1497
+ "cp",
1498
+ `${service}:${from}`,
1499
+ to
1500
+ ]);
1501
+ },
1502
+ async reload(composePath, service) {
1503
+ await run("docker", [
1504
+ "compose",
1505
+ ...composeFiles(composePath),
1506
+ "exec",
1507
+ "-T",
1508
+ service,
1509
+ "caddy",
1510
+ "reload",
1511
+ "--config",
1512
+ "/etc/caddy/Caddyfile"
1513
+ ]);
1514
+ },
1515
+ async healthy(composePath, services) {
1516
+ if (services.length === 0) return true;
1517
+ try {
1518
+ const { stdout } = await run("docker", [
1519
+ "compose",
1520
+ ...composeFiles(composePath),
1521
+ "ps",
1522
+ "--format",
1523
+ "json",
1524
+ ...services
1525
+ ]);
1526
+ const running = /* @__PURE__ */ new Set();
1527
+ for (const line of stdout.trim().split("\n").filter(Boolean)) {
1528
+ const entry = JSON.parse(line);
1529
+ if (entry.State === "running" && (entry.Health === void 0 || entry.Health === "" || entry.Health === "healthy") && entry.Service) running.add(entry.Service);
1530
+ }
1531
+ return services.every((service) => running.has(service));
1532
+ } catch {
1533
+ return false;
1534
+ }
1364
1535
  }
1365
1536
  };
1537
+ //#endregion
1538
+ //#region src/reconcile/ports.ts
1366
1539
  /**
1367
- * The value a secret resolves to locally.
1540
+ * Port allocation — why two projects can run at once.
1368
1541
  *
1369
- * A hash of what identifies it, so it is stable, distinct per project, stage
1370
- * and construct, and never written to disk as a literal. Deployed this is the
1371
- * one kind whose value does *not* come from here — a secret manager generates
1372
- * it once and the target reads it back.
1542
+ * Publishing `5432:5432` means the second project on your machine cannot start.
1543
+ * Nothing solves that except giving each project its own ports, and what makes
1544
+ * it *possible* is the single-`<NAME>_URL` rule: an app reads `ORDERS_URL` and
1545
+ * never sees a host or a port, so reconcile is free to bind whatever is
1546
+ * available and compose the URL from what it bound. An app reading `DB_HOST` and
1547
+ * `DB_PORT` separately could not be moved this way.
1548
+ *
1549
+ * Assignments are **persisted, not recomputed**. Recomputing means adding a
1550
+ * construct renumbers its neighbours, and every running container, saved
1551
+ * connection string, and `psql` history goes stale at once.
1552
+ *
1553
+ * Pure, with the liveness probe injected — a collision with another project's
1554
+ * stack is a fact about the machine, and keeping it a parameter is what lets the
1555
+ * rules be asserted without binding a socket.
1373
1556
  */
1374
- function localSecret(project, plan, resource) {
1375
- return createHash("sha256").update(`${project}:${plan.stage}:${resource.id}`).digest("base64url").slice(0, 43);
1376
- }
1377
-
1378
- //#endregion
1379
- //#region src/reconcile/ports.ts
1380
1557
  /**
1381
1558
  * The window ports are drawn from.
1382
1559
  *
@@ -1475,17 +1652,33 @@ async function nextFree(from, taken, probe) {
1475
1652
  * rather than as a connection failure ten minutes later.
1476
1653
  */
1477
1654
  function isPortFree(port) {
1478
- return new Promise((resolve$1) => {
1655
+ return new Promise((resolve) => {
1479
1656
  const server = createServer();
1480
- server.once("error", () => resolve$1(false));
1481
- server.once("listening", () => server.close(() => resolve$1(true)));
1657
+ server.once("error", () => resolve(false));
1658
+ server.once("listening", () => server.close(() => resolve(true)));
1482
1659
  server.listen(port, "0.0.0.0");
1483
1660
  });
1484
1661
  }
1485
-
1486
1662
  //#endregion
1487
1663
  //#region src/reconcile/provision.ts
1488
1664
  /**
1665
+ * Creating what the URLs name.
1666
+ *
1667
+ * Convergent and idempotent: every statement asks whether the thing exists
1668
+ * first, so reconciling repeatedly is free and a half-applied run recovers by
1669
+ * being run again. That property is what lets `gkm dev` do this on every start.
1670
+ *
1671
+ * The line the design draws is at data, not at side effects — this creates
1672
+ * databases, schemas, and buckets, and it never seeds, resets, or drops. A
1673
+ * developer who wants their data gone asks for it explicitly.
1674
+ *
1675
+ * Statement generation is separated from application for the reason the design
1676
+ * gives: the same DDL has to run in-process locally and from a provisioner in a
1677
+ * VPC, and one implementation is what stops "works locally, fails deployed".
1678
+ * When the role DDL lands in `@geekmidas/db` this delegates to it rather than
1679
+ * growing its own.
1680
+ */
1681
+ /**
1489
1682
  * The Postgres work a plan implies.
1490
1683
  *
1491
1684
  * Databases before the schemas inside them, which `provisionOrder` already
@@ -1627,8 +1820,7 @@ async function applyPostgres(client, statements) {
1627
1820
  });
1628
1821
  continue;
1629
1822
  }
1630
- const rows = await client.query(statement.database, statement.exists.sql, statement.exists.values);
1631
- if (rows.length > 0) {
1823
+ if ((await client.query(statement.database, statement.exists.sql, statement.exists.values)).length > 0) {
1632
1824
  applied.push({
1633
1825
  id: statement.id,
1634
1826
  describe: statement.describe,
@@ -1753,9 +1945,20 @@ async function applyBuckets(client, buckets) {
1753
1945
  }
1754
1946
  return applied;
1755
1947
  }
1756
-
1757
1948
  //#endregion
1758
1949
  //#region src/reconcile/state.ts
1950
+ /**
1951
+ * What reconcile records, and the hash that lets it do nothing.
1952
+ *
1953
+ * Reconciling on every `gkm dev` is only acceptable if the converged case is
1954
+ * free, so the desired plan is hashed: matching hash plus healthy containers
1955
+ * means there is nothing to do. Without that, the automatic reconcile the design
1956
+ * asks for would tax every start.
1957
+ *
1958
+ * Almost nothing needs recording — roles are queried, migrations have their own
1959
+ * table, containers are remembered by Docker. Only ports and this hash have no
1960
+ * other owner.
1961
+ */
1759
1962
  const STATE_DIR = ".gkm/reconcile";
1760
1963
  /**
1761
1964
  * The hash of everything a reconcile would act on.
@@ -1784,7 +1987,7 @@ async function loadState(root, stage) {
1784
1987
  const state = JSON.parse(raw);
1785
1988
  return state.stage === stage ? state : void 0;
1786
1989
  } catch {
1787
- return void 0;
1990
+ return;
1788
1991
  }
1789
1992
  }
1790
1993
  /** Record what was applied, so the next run can skip it. */
@@ -1796,11 +1999,36 @@ async function saveState(root, state) {
1796
1999
  function statePath(root, stage) {
1797
2000
  return join(root, STATE_DIR, `${stage}.json`);
1798
2001
  }
1799
-
1800
2002
  //#endregion
1801
2003
  //#region src/reconcile/index.ts
1802
- /** Where the generated compose file is written, relative to the project root. */
1803
- const COMPOSE_PATH = ".gkm/docker-compose.yml";
2004
+ /**
2005
+ * Reconcile — the one convergent function `gkm dev`, `gkm test`, and `gkm setup`
2006
+ * all call.
2007
+ *
2008
+ * Three phases, and only this one has side effects: declaring is pure (`gkm
2009
+ * build` and the dev watcher), running reads `<NAME>_URL` and provisions
2010
+ * nothing. Reconcile computes the desired state, compares, and applies the
2011
+ * difference — which is what makes "when does it run" answerable with
2012
+ * *whenever, repeatedly*.
2013
+ *
2014
+ * It is safe to run automatically because its blast radius is entirely local:
2015
+ * this project's containers, this project's `.gkm/`. Nothing it does can reach a
2016
+ * cloud. The line is drawn at data, not at side effects — it allocates ports,
2017
+ * writes compose, starts containers, and creates roles; it never seeds, resets,
2018
+ * or drops.
2019
+ *
2020
+ * Docker is injected rather than imported so the rules above can be asserted
2021
+ * without a daemon. The default implementation is the real one.
2022
+ */
2023
+ /**
2024
+ * Where the generated compose file is written, relative to the project root.
2025
+ *
2026
+ * At the root, beside the project's own `docker-compose.yml`, because the two
2027
+ * are one stack: this file is everything the constructs imply and is never
2028
+ * edited, that one is anything they cannot express — an image pin, an extra
2029
+ * service — and is merged over it, so it wins.
2030
+ */
2031
+ const COMPOSE_PATH = "docker-compose.constructs.yml";
1804
2032
  /**
1805
2033
  * The local edge's config, beside the compose file that mounts it.
1806
2034
  *
@@ -1853,7 +2081,7 @@ async function reconcile(options) {
1853
2081
  const compose = composeFor(plan, {
1854
2082
  project,
1855
2083
  ports,
1856
- ...options.images ? { images: options.images } : {}
2084
+ ...options.apps ? { apps: options.apps(plan.containers) } : {}
1857
2085
  });
1858
2086
  const caddyfile = toCaddyfile(sitesFor(plan, project, options.addresses));
1859
2087
  const hash = planHash(plan, compose, { caddyfile });
@@ -1876,9 +2104,7 @@ async function reconcile(options) {
1876
2104
  hash,
1877
2105
  changed: false
1878
2106
  };
1879
- const recorded = await loadState(root, stage);
1880
- const converged = recorded?.hash === hash && (!start || await docker.healthy(composePath, plan.containers));
1881
- if (converged) return result;
2107
+ if ((await loadState(root, stage))?.hash === hash && existsSync(composePath) && (!start || await docker.healthy(composePath, plan.containers))) return result;
1882
2108
  await write(composePath, toYaml(compose));
1883
2109
  if (plan.containers.includes("caddy")) {
1884
2110
  await write(join(root, CADDYFILE_PATH), caddyfileRoot());
@@ -1955,10 +2181,28 @@ async function write(path, content) {
1955
2181
  await mkdir(dirname(path), { recursive: true });
1956
2182
  await writeFile(path, content);
1957
2183
  }
1958
-
1959
2184
  //#endregion
1960
2185
  //#region src/reconcile/workspace.ts
1961
2186
  /**
2187
+ * Workspace config → reconcile.
2188
+ *
2189
+ * Nothing here is read from a `services:` block — there is none. Which
2190
+ * containers exist comes from the manifest; which backend a cache or a broker
2191
+ * resolves to comes from the deploy target; and an image pin is the project's
2192
+ * own `docker-compose.yml`, merged over the generated file.
2193
+ *
2194
+ * Shared by `gkm setup`, `gkm dev`, and `gkm test` so all three converge on the
2195
+ * same state, differing only in the stage they pass.
2196
+ */
2197
+ var workspace_exports = /* @__PURE__ */ __exportAll({
2198
+ backendsOf: () => backendsOf,
2199
+ constructGlobs: () => constructGlobs,
2200
+ derivedContainers: () => derivedContainers,
2201
+ reconcileWorkspace: () => reconcileWorkspace,
2202
+ surfaceAddresses: () => surfaceAddresses,
2203
+ usesConstructs: () => usesConstructs
2204
+ });
2205
+ /**
1962
2206
  * Every constructs glob in the workspace, resolved against its app.
1963
2207
  *
1964
2208
  * Absolute because discovery globs from a single cwd while apps live in
@@ -1971,16 +2215,13 @@ function constructGlobs(workspace) {
1971
2215
  function usesConstructs(workspace) {
1972
2216
  return constructGlobs(workspace).length > 0;
1973
2217
  }
1974
- /**
1975
- * Image pins, by container. The config half of the derived/config split.
1976
- *
1977
- * Read from `services.images` rather than from the backend keys beside it.
1978
- * They used to share one key, which is how `cache: true` came to mean "start a
1979
- * Redis" — the last way a container could exist because config asked for one
1980
- * instead of because something declared it.
1981
- */
1982
- function imagePins(workspace) {
1983
- return { ...workspace.services.images };
2218
+ /** The backends a workspace's target implies, as the plan takes them. */
2219
+ function backendsOf(workspace) {
2220
+ const on = providerOf(workspace);
2221
+ return {
2222
+ events: eventsBackendFor(on),
2223
+ cache: cacheBackendFor(on)
2224
+ };
1984
2225
  }
1985
2226
  /**
1986
2227
  * The containers a workspace's declarations imply, without reconciling.
@@ -1999,8 +2240,7 @@ async function derivedContainers(workspace, stage, manifest) {
1999
2240
  });
2000
2241
  return planFor(found, stage, provisionOrder(found), {
2001
2242
  localStage: workspace.stages.local,
2002
- events: workspace.services.events,
2003
- cache: cacheBackendOf(workspace.services.cache, providerOf(workspace))
2243
+ ...backendsOf(workspace)
2004
2244
  }).containers;
2005
2245
  }
2006
2246
  /**
@@ -2020,11 +2260,10 @@ async function reconcileWorkspace(workspace, options) {
2020
2260
  manifest,
2021
2261
  stage: options.stage,
2022
2262
  localStage: workspace.stages.local,
2023
- events: workspace.services.events,
2024
- cache: cacheBackendOf(workspace.services.cache, providerOf(workspace)),
2025
- images: imagePins(workspace),
2263
+ ...backendsOf(workspace),
2026
2264
  saved: await loadPortState(workspace.root),
2027
2265
  addresses: surfaceAddresses(workspace, manifest),
2266
+ apps: (containers) => appServices(workspace, manifest, containers),
2028
2267
  ...options.start === void 0 ? {} : { start: options.start }
2029
2268
  });
2030
2269
  await savePortState(workspace.root, { ...result.ports });
@@ -2046,26 +2285,20 @@ async function reconcileWorkspace(workspace, options) {
2046
2285
  * an app serving both its own API and an auth server publishes one address
2047
2286
  * twice — which is exactly what it does at runtime.
2048
2287
  */
2049
- function surfaceAddresses(workspace, manifest) {
2288
+ function surfaceAddresses(workspace, manifest, at = (_app, port) => localAddress(port)) {
2050
2289
  const addresses = {};
2051
2290
  for (const [id, declaration] of Object.entries(manifest)) {
2052
2291
  if (declaration.kind !== "rest-api" && declaration.kind !== "site") continue;
2053
- const host = declaration.kind === "site" ? appKey(id) : appKeyOfHost(manifest, id);
2054
- const app = host ? workspace.apps[host] : void 0;
2055
- if (app?.port) addresses[id] = localAddress(app.port);
2292
+ const host = appKey(id);
2293
+ const port = workspace.apps[host]?.port;
2294
+ if (port) addresses[id] = at(host, port);
2056
2295
  }
2057
2296
  return addresses;
2058
2297
  }
2059
- /** The app key serving a surface, following a mount to its host. */
2060
- function appKeyOfHost(manifest, id) {
2061
- const host = hostOf(manifest, id);
2062
- return host ? appKey(host) : void 0;
2063
- }
2064
2298
  /** Where a local process answers, given the port the workspace gave it. */
2065
2299
  function localAddress(port) {
2066
2300
  return `http://localhost:${port}`;
2067
2301
  }
2068
-
2069
2302
  //#endregion
2070
2303
  //#region src/credentials/index.ts
2071
2304
  const logger = console;
@@ -2098,15 +2331,15 @@ function loadEnvFiles(envConfig, cwd = process.cwd()) {
2098
2331
  * @internal Exported for testing
2099
2332
  */
2100
2333
  async function isPortAvailable(port) {
2101
- return new Promise((resolve$1) => {
2334
+ return new Promise((resolve) => {
2102
2335
  const server = createServer();
2103
2336
  server.once("error", (err) => {
2104
- if (err.code === "EADDRINUSE") resolve$1(false);
2105
- else resolve$1(false);
2337
+ if (err.code === "EADDRINUSE") resolve(false);
2338
+ else resolve(false);
2106
2339
  });
2107
2340
  server.once("listening", () => {
2108
2341
  server.close();
2109
- resolve$1(true);
2342
+ resolve(true);
2110
2343
  });
2111
2344
  server.listen(port);
2112
2345
  });
@@ -2175,11 +2408,10 @@ async function savePortState(workspaceRoot, ports) {
2175
2408
  */
2176
2409
  function getContainerHostPort(workspaceRoot, service, containerPort) {
2177
2410
  try {
2178
- const result = execSync(`docker compose port ${service} ${containerPort}`, {
2411
+ const match = execSync(`docker compose port ${service} ${containerPort}`, {
2179
2412
  cwd: workspaceRoot,
2180
2413
  stdio: "pipe"
2181
- }).toString().trim();
2182
- const match = result.match(/:(\d+)$/);
2414
+ }).toString().trim().match(/:(\d+)$/);
2183
2415
  return match ? Number(match[1]) : null;
2184
2416
  } catch {
2185
2417
  return null;
@@ -2192,8 +2424,7 @@ function getContainerHostPort(workspaceRoot, service, containerPort) {
2192
2424
  * @internal Exported for testing
2193
2425
  */
2194
2426
  async function resolveServicePorts(workspaceRoot) {
2195
- const composePath = join(workspaceRoot, "docker-compose.yml");
2196
- const mappings = parseComposePortMappings(composePath);
2427
+ const mappings = parseComposePortMappings(join(workspaceRoot, "docker-compose.yml"));
2197
2428
  if (mappings.length === 0) return {
2198
2429
  dockerEnv: {},
2199
2430
  ports: {},
@@ -2427,13 +2658,12 @@ ${generateCredentialsInjection(secretsJsonPath)}`;
2427
2658
  * @internal Exported for testing
2428
2659
  */
2429
2660
  async function createEntryWrapper(wrapperPath, entryPath, secretsJsonPath) {
2430
- const credentialsInjection = secretsJsonPath ? `${generateCredentialsInjection(secretsJsonPath)}
2431
- ` : "";
2432
2661
  const content = `#!/usr/bin/env node
2433
2662
  /**
2434
2663
  * Entry wrapper generated by 'gkm dev --entry'
2435
2664
  */
2436
- ${credentialsInjection}// Import and run the user's entry file (dynamic import ensures secrets load first)
2665
+ ${secretsJsonPath ? `${generateCredentialsInjection(secretsJsonPath)}
2666
+ ` : ""}// Import and run the user's entry file (dynamic import ensures secrets load first)
2437
2667
  await import('${entryPath}');
2438
2668
  `;
2439
2669
  await writeFile(wrapperPath, content);
@@ -2473,8 +2703,8 @@ async function prepareEntryCredentials(options) {
2473
2703
  credentials.PORT = String(resolvedPort);
2474
2704
  const declaredKeys = [];
2475
2705
  if (appInfo && usesConstructs(appInfo.workspace)) {
2476
- const { reconcileWorkspace: reconcileWorkspace$1 } = await import("./workspace-DbI580qS.mjs");
2477
- const reconciled = await reconcileWorkspace$1(appInfo.workspace, {
2706
+ const { reconcileWorkspace } = await Promise.resolve().then(() => workspace_exports);
2707
+ const reconciled = await reconcileWorkspace(appInfo.workspace, {
2478
2708
  stage: options.reconcileStage ?? appInfo.workspace.stages.local,
2479
2709
  start: options.startDocker ?? false
2480
2710
  });
@@ -2482,8 +2712,7 @@ async function prepareEntryCredentials(options) {
2482
2712
  declaredKeys.push(...Object.keys(reconciled.env));
2483
2713
  if (Object.keys(reconciled.env).length > 0) logger.log(`🔌 Resolved ${Object.keys(reconciled.env).length} declared URL(s)`);
2484
2714
  } else {
2485
- const composePath = join(secretsRoot, "docker-compose.yml");
2486
- const mappings = parseComposePortMappings(composePath);
2715
+ const mappings = parseComposePortMappings(join(secretsRoot, "docker-compose.yml"));
2487
2716
  if (mappings.length > 0) {
2488
2717
  let resolvedPorts;
2489
2718
  if (portMode === "full") resolvedPorts = await resolveServicePorts(secretsRoot);
@@ -2504,8 +2733,10 @@ async function prepareEntryCredentials(options) {
2504
2733
  mappings
2505
2734
  };
2506
2735
  }
2507
- if (options.startDocker) if (appInfo) await startWorkspaceServices(appInfo.workspace, resolvedPorts.dockerEnv, credentials);
2508
- else await startComposeServices(secretsRoot, resolvedPorts.dockerEnv, credentials);
2736
+ if (options.startDocker) {
2737
+ if (appInfo) await startWorkspaceServices(appInfo.workspace, resolvedPorts.dockerEnv, credentials);
2738
+ else await startComposeServices(secretsRoot, resolvedPorts.dockerEnv, credentials);
2739
+ }
2509
2740
  if (Object.keys(resolvedPorts.ports).length > 0) {
2510
2741
  const rewritten = rewriteUrlsWithPorts(credentials, resolvedPorts);
2511
2742
  Object.assign(credentials, rewritten);
@@ -2543,7 +2774,7 @@ async function prepareEntryCredentials(options) {
2543
2774
  declaredKeys
2544
2775
  };
2545
2776
  }
2546
-
2547
2777
  //#endregion
2548
- export { LOCAL_CA_PATH, applyPostgres, cacheBackendOf, constructGlobs, createCredentialsPreload, createEntryWrapper, derivedContainers, emailBackendOf, findAvailablePort, imagePins, isPortAvailable, loadEnvFiles, loadSecretsForApp, prepareEntryCredentials, providerOf, reconcileWorkspace, resolveServicePorts, resourceName, rewriteUrlsWithPorts, startWorkspaceServices, storageBackendOf, usesConstructs };
2549
- //# sourceMappingURL=credentials-CZrys9C5.mjs.map
2778
+ export { storageBackendFor as C, providerOf as S, LOCAL_CA_PATH as _, loadEnvFiles as a, cacheBackendFor as b, resolveServicePorts as c, constructGlobs as d, derivedContainers as f, COMPOSE_PATH as g, workspace_exports as h, isPortAvailable as i, rewriteUrlsWithPorts as l, usesConstructs as m, createEntryWrapper as n, loadSecretsForApp as o, reconcileWorkspace as p, findAvailablePort as r, prepareEntryCredentials as s, createCredentialsPreload as t, startWorkspaceServices as u, applyPostgres as v, eventsBackendFor as x, resourceName as y };
2779
+
2780
+ //# sourceMappingURL=credentials-BekkKtY_.mjs.map