@sdxc/kit 2026.10.17 → 2026.10.21

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 (295) hide show
  1. package/README.md +397 -22
  2. package/changelog.jsonl +1171 -1092
  3. package/config.schema.json +53 -1
  4. package/dist/cli.js +12 -0
  5. package/dist/commands/deploy.d.ts +23 -0
  6. package/dist/commands/deploy.js +167 -0
  7. package/dist/commands/destroy.d.ts +22 -0
  8. package/dist/commands/destroy.js +92 -0
  9. package/dist/commands/doctor.d.ts +26 -0
  10. package/dist/commands/doctor.js +65 -0
  11. package/dist/commands/initializers.d.ts +16 -0
  12. package/dist/commands/initializers.js +23 -0
  13. package/dist/commands/make.d.ts +78 -6
  14. package/dist/commands/make.js +134 -20
  15. package/dist/commands/middleware.d.ts +16 -0
  16. package/dist/commands/middleware.js +21 -0
  17. package/dist/commands/models.d.ts +18 -0
  18. package/dist/commands/models.js +29 -0
  19. package/dist/commands/notes.d.ts +17 -0
  20. package/dist/commands/notes.js +27 -0
  21. package/dist/commands/routes.d.ts +25 -0
  22. package/dist/commands/routes.js +50 -0
  23. package/dist/commands/run.d.ts +16 -0
  24. package/dist/commands/run.js +32 -11
  25. package/dist/commands/scaffold.d.ts +30 -0
  26. package/dist/commands/scaffold.js +34 -0
  27. package/dist/commands/schedule.d.ts +16 -0
  28. package/dist/commands/schedule.js +21 -0
  29. package/dist/commands/stats.d.ts +16 -0
  30. package/dist/commands/stats.js +21 -0
  31. package/dist/commands/ui/update.d.ts +6 -3
  32. package/dist/commands/ui/update.js +24 -7
  33. package/dist/commands/upgrade.d.ts +3 -1
  34. package/dist/commands/upgrade.js +88 -23
  35. package/dist/config.d.ts +20 -0
  36. package/dist/config.js +32 -1
  37. package/dist/contract.d.ts +101 -3
  38. package/dist/contract.js +371 -26
  39. package/dist/create/templates.js +3 -2
  40. package/dist/dependencies.d.ts +5 -1
  41. package/dist/dependencies.js +16 -1
  42. package/dist/deploy/plan.d.ts +49 -0
  43. package/dist/deploy/plan.js +68 -0
  44. package/dist/destroy.d.ts +40 -0
  45. package/dist/destroy.js +317 -0
  46. package/dist/doctor/check.d.ts +71 -0
  47. package/dist/doctor/check.js +53 -0
  48. package/dist/doctor/checks.d.ts +23 -0
  49. package/dist/doctor/checks.js +70 -0
  50. package/dist/doctor/define.d.ts +59 -0
  51. package/dist/doctor/define.js +16 -0
  52. package/dist/doctor/emails.d.ts +18 -0
  53. package/dist/doctor/emails.js +243 -0
  54. package/dist/doctor/env.d.ts +26 -0
  55. package/dist/doctor/env.js +88 -0
  56. package/dist/doctor/files.d.ts +14 -0
  57. package/dist/doctor/files.js +34 -0
  58. package/dist/doctor/fixture.d.ts +23 -0
  59. package/dist/doctor/fixture.js +46 -0
  60. package/dist/doctor/install.d.ts +17 -0
  61. package/dist/doctor/install.js +37 -0
  62. package/dist/doctor/installed.d.ts +11 -0
  63. package/dist/doctor/installed.js +79 -0
  64. package/dist/doctor/jsdoc.d.ts +44 -0
  65. package/dist/doctor/jsdoc.js +201 -0
  66. package/dist/doctor/layout.d.ts +23 -0
  67. package/dist/doctor/layout.js +83 -0
  68. package/dist/doctor/migrations.d.ts +16 -0
  69. package/dist/doctor/migrations.js +96 -0
  70. package/dist/doctor/models.d.ts +13 -0
  71. package/dist/doctor/models.js +69 -0
  72. package/dist/doctor/pins.d.ts +15 -0
  73. package/dist/doctor/pins.js +79 -0
  74. package/dist/doctor/project.d.ts +58 -0
  75. package/dist/doctor/project.js +261 -0
  76. package/dist/doctor/remix.d.ts +14 -0
  77. package/dist/doctor/remix.js +44 -0
  78. package/dist/doctor/routes.d.ts +11 -0
  79. package/dist/doctor/routes.js +49 -0
  80. package/dist/doctor/typegen.d.ts +13 -0
  81. package/dist/doctor/typegen.js +57 -0
  82. package/dist/doctor/ui.d.ts +14 -0
  83. package/dist/doctor/ui.js +51 -0
  84. package/dist/initializers.d.ts +25 -0
  85. package/dist/initializers.js +89 -0
  86. package/dist/lock.d.ts +2 -2
  87. package/dist/make/actions.d.ts +23 -0
  88. package/dist/make/actions.js +82 -0
  89. package/dist/make/declare.d.ts +31 -3
  90. package/dist/make/declare.js +284 -9
  91. package/dist/make/email.d.ts +23 -0
  92. package/dist/make/email.js +111 -0
  93. package/dist/make/from-fields.d.ts +30 -0
  94. package/dist/make/from-fields.js +162 -0
  95. package/dist/make/from-model.d.ts +39 -0
  96. package/dist/make/from-model.js +161 -0
  97. package/dist/make/generate.d.ts +25 -5
  98. package/dist/make/generate.js +76 -22
  99. package/dist/make/guard.d.ts +35 -0
  100. package/dist/make/guard.js +73 -0
  101. package/dist/make/imports.d.ts +6 -0
  102. package/dist/make/imports.js +19 -0
  103. package/dist/make/index.d.ts +49 -4
  104. package/dist/make/index.js +290 -29
  105. package/dist/make/kinds.d.ts +34 -2
  106. package/dist/make/kinds.js +92 -8
  107. package/dist/make/mount.d.ts +25 -0
  108. package/dist/make/mount.js +135 -0
  109. package/dist/make/schema-diff.d.ts +58 -0
  110. package/dist/make/schema-diff.js +609 -0
  111. package/dist/make/schema.d.ts +76 -0
  112. package/dist/make/schema.js +206 -0
  113. package/dist/make/stubs.d.ts +14 -7
  114. package/dist/make/stubs.js +24 -8
  115. package/dist/make/test.d.ts +43 -0
  116. package/dist/make/test.js +150 -0
  117. package/dist/make/trigger.d.ts +31 -0
  118. package/dist/make/trigger.js +163 -0
  119. package/dist/models.d.ts +42 -0
  120. package/dist/models.js +149 -0
  121. package/dist/notes.d.ts +28 -0
  122. package/dist/notes.js +66 -0
  123. package/dist/reference.d.ts +27 -0
  124. package/dist/reference.js +103 -0
  125. package/dist/releases.d.ts +9 -0
  126. package/dist/releases.js +29 -8
  127. package/dist/routes/chains.d.ts +34 -0
  128. package/dist/routes/chains.js +95 -0
  129. package/dist/routes/filter.d.ts +53 -0
  130. package/dist/routes/filter.js +147 -0
  131. package/dist/routes/format.d.ts +23 -0
  132. package/dist/routes/format.js +123 -0
  133. package/dist/routes/helpers.d.ts +21 -0
  134. package/dist/routes/helpers.js +165 -0
  135. package/dist/routes/index.d.ts +56 -0
  136. package/dist/routes/index.js +167 -0
  137. package/dist/routes/modules.d.ts +116 -0
  138. package/dist/routes/modules.js +235 -0
  139. package/dist/routes/parse.d.ts +161 -0
  140. package/dist/routes/parse.js +621 -0
  141. package/dist/routes/routers.d.ts +69 -0
  142. package/dist/routes/routers.js +327 -0
  143. package/dist/scaffold/fields.d.ts +129 -0
  144. package/dist/scaffold/fields.js +421 -0
  145. package/dist/scaffold/index.d.ts +39 -0
  146. package/dist/scaffold/index.js +622 -0
  147. package/dist/scaffold/render.d.ts +61 -0
  148. package/dist/scaffold/render.js +417 -0
  149. package/dist/schedule.d.ts +45 -0
  150. package/dist/schedule.js +124 -0
  151. package/dist/stats.d.ts +37 -0
  152. package/dist/stats.js +126 -0
  153. package/dist/ui/commands.d.ts +10 -3
  154. package/dist/ui/commands.js +85 -15
  155. package/dist/ui/registry.d.ts +8 -8
  156. package/dist/upgrade/agents.d.ts +49 -0
  157. package/dist/upgrade/agents.js +110 -0
  158. package/dist/upgrade/changelog.d.ts +2 -2
  159. package/dist/upgrade/project.d.ts +4 -0
  160. package/dist/upgrade/project.js +11 -1
  161. package/dist/upgrade/prompt.d.ts +11 -0
  162. package/dist/upgrade/prompt.js +50 -7
  163. package/package.json +6 -4
  164. package/registry/ui/alert-dialog.json +2 -2
  165. package/registry/ui/attachment.json +2 -2
  166. package/registry/ui/calendar.json +2 -2
  167. package/registry/ui/color-picker.json +2 -2
  168. package/registry/ui/command.json +2 -2
  169. package/registry/ui/date-picker.json +2 -2
  170. package/registry/ui/date-range-picker.json +2 -2
  171. package/registry/ui/dialog.json +2 -2
  172. package/registry/ui/file-trigger.json +2 -2
  173. package/registry/ui/heading-scope.json +2 -2
  174. package/registry/ui/message-scroller.json +2 -2
  175. package/registry/ui/number-field.json +2 -2
  176. package/registry/ui/range-calendar.json +2 -2
  177. package/registry/ui/resizable.json +2 -2
  178. package/registry/ui/select.json +2 -2
  179. package/registry/ui/shared-element.json +2 -2
  180. package/registry/ui/sidebar.json +3 -3
  181. package/registry/ui/typeset.json +3 -3
  182. package/stubs/command.spec.stub +15 -0
  183. package/stubs/controller.api.resource.stub +23 -0
  184. package/stubs/controller.api.spec.stub +15 -0
  185. package/stubs/controller.form.stub +2 -2
  186. package/stubs/controller.form.test.stub +2 -2
  187. package/stubs/controller.resource.form.stub +67 -0
  188. package/stubs/controller.resource.stub +28 -0
  189. package/stubs/controller.spec.stub +15 -0
  190. package/stubs/email.stub +9 -14
  191. package/stubs/email.test.stub +9 -9
  192. package/stubs/factory.stub +1 -1
  193. package/stubs/mcp.spec.stub +16 -0
  194. package/stubs/model.stub +3 -3
  195. package/stubs/policy.test.stub +34 -8
  196. package/stubs/scaffold.api.create.stub +34 -0
  197. package/stubs/scaffold.api.create.test.stub +37 -0
  198. package/stubs/scaffold.api.destroy.stub +34 -0
  199. package/stubs/scaffold.api.destroy.test.stub +44 -0
  200. package/stubs/scaffold.api.index.stub +23 -0
  201. package/stubs/scaffold.api.index.test.stub +31 -0
  202. package/stubs/scaffold.api.show.stub +27 -0
  203. package/stubs/scaffold.api.show.test.stub +41 -0
  204. package/stubs/scaffold.api.update.stub +44 -0
  205. package/stubs/scaffold.api.update.test.stub +46 -0
  206. package/stubs/scaffold.command.create.stub +22 -0
  207. package/stubs/scaffold.command.create.test.stub +30 -0
  208. package/stubs/scaffold.command.delete.stub +20 -0
  209. package/stubs/scaffold.command.delete.test.stub +39 -0
  210. package/stubs/scaffold.command.list.stub +18 -0
  211. package/stubs/scaffold.command.list.test.stub +31 -0
  212. package/stubs/scaffold.command.show.stub +19 -0
  213. package/stubs/scaffold.command.show.test.stub +37 -0
  214. package/stubs/scaffold.controller.destroy.stub +35 -0
  215. package/stubs/scaffold.controller.destroy.test.stub +65 -0
  216. package/stubs/scaffold.controller.edit.stub +97 -0
  217. package/stubs/scaffold.controller.edit.test.stub +91 -0
  218. package/stubs/scaffold.controller.index.stub +55 -0
  219. package/stubs/scaffold.controller.index.test.stub +32 -0
  220. package/stubs/scaffold.controller.new.stub +70 -0
  221. package/stubs/scaffold.controller.new.test.stub +81 -0
  222. package/stubs/scaffold.controller.show.stub +59 -0
  223. package/stubs/scaffold.controller.show.test.stub +43 -0
  224. package/stubs/scaffold.factory.stub +16 -0
  225. package/stubs/scaffold.form.stub +75 -0
  226. package/stubs/scaffold.mcp.create.stub +24 -0
  227. package/stubs/scaffold.mcp.create.test.stub +36 -0
  228. package/stubs/scaffold.mcp.delete.stub +31 -0
  229. package/stubs/scaffold.mcp.delete.test.stub +43 -0
  230. package/stubs/scaffold.mcp.get.stub +21 -0
  231. package/stubs/scaffold.mcp.get.test.stub +31 -0
  232. package/stubs/scaffold.mcp.list.stub +23 -0
  233. package/stubs/scaffold.mcp.list.test.stub +25 -0
  234. package/stubs/scaffold.mcp.update.stub +37 -0
  235. package/stubs/scaffold.mcp.update.test.stub +40 -0
  236. package/stubs/scaffold.migration.stub +3 -0
  237. package/stubs/scaffold.model.stub +35 -0
  238. package/stubs/scaffold.model.test.stub +25 -0
  239. package/stubs/scaffold.spec.stub +7 -0
  240. package/stubs/scaffold.test-database.stub +33 -0
  241. package/stubs/scaffold.test-mcp.stub +92 -0
  242. package/stubs/scaffold.validator.stub +18 -0
  243. package/stubs/scaffold.validator.test.stub +26 -0
  244. package/templates/app/AGENTS.md +167 -0
  245. package/templates/app/CLAUDE.md +1 -0
  246. package/templates/app/README.md +33 -15
  247. package/templates/app/app/commands/console.test.ts +26 -0
  248. package/templates/app/app/commands/console.ts +19 -0
  249. package/templates/app/app/commands/db/fresh.test.ts +57 -0
  250. package/templates/app/app/commands/db/fresh.ts +37 -0
  251. package/templates/app/app/commands/db/migrate.test.ts +17 -1
  252. package/templates/app/app/commands/db/migrate.ts +51 -18
  253. package/templates/app/app/commands/db/prepare.test.ts +39 -0
  254. package/templates/app/app/commands/db/prepare.ts +35 -0
  255. package/templates/app/app/commands/db/seed.test.ts +38 -0
  256. package/templates/app/app/commands/db/seed.ts +26 -0
  257. package/templates/app/app/commands/deploy.test.ts +34 -0
  258. package/templates/app/app/commands/deploy.ts +18 -0
  259. package/templates/app/app/commands/emails/preview.test.tsx +63 -0
  260. package/templates/app/app/commands/emails/preview.ts +24 -0
  261. package/templates/app/app/commands/index.ts +57 -2
  262. package/templates/app/app/commands/typegen.test.ts +20 -1
  263. package/templates/app/app/commands/typegen.ts +16 -2
  264. package/templates/app/app/emails/index.ts +10 -0
  265. package/templates/app/app/http/middleware/access.test.ts +43 -0
  266. package/templates/app/app/http/middleware/access.ts +41 -0
  267. package/templates/app/app/http/middleware/api.ts +12 -3
  268. package/templates/app/app/http/middleware/page.test.ts +4 -1
  269. package/templates/app/app/http/middleware/page.ts +7 -5
  270. package/templates/app/app/initializers/cli.ts +35 -2
  271. package/templates/app/app/initializers/dispatcher.ts +9 -1
  272. package/templates/app/app/initializers/mail-transport.test.ts +33 -0
  273. package/templates/app/app/initializers/mail-transport.ts +26 -0
  274. package/templates/app/app/initializers/mail.test.ts +17 -17
  275. package/templates/app/app/initializers/mail.ts +15 -7
  276. package/templates/app/app/initializers/router.test.ts +7 -0
  277. package/templates/app/app/initializers/router.tsx +13 -3
  278. package/templates/app/app/jobs/email.test.tsx +70 -0
  279. package/templates/app/app/jobs/email.ts +16 -0
  280. package/templates/app/app/jobs/index.ts +6 -2
  281. package/templates/app/app/policies/abilities.ts +13 -0
  282. package/templates/app/app/policies/policy.test.ts +15 -0
  283. package/templates/app/app/policies/policy.ts +30 -0
  284. package/templates/app/app/services/commands.ts +30 -0
  285. package/templates/app/app/services/migrations.test.ts +48 -1
  286. package/templates/app/app/services/migrations.ts +66 -9
  287. package/templates/app/app/test/cloudflare-workers.ts +2 -0
  288. package/templates/app/app/test/local-database.ts +67 -0
  289. package/templates/app/app/test/session.ts +28 -0
  290. package/templates/app/bootstrap/console.ts +13 -0
  291. package/templates/app/cloudflare.config.ts +3 -3
  292. package/templates/app/database/seed.ts +12 -0
  293. package/templates/app/package.json +3 -1
  294. package/templates/app/spec/config.jsonc +16 -0
  295. package/stubs/policy.stub +0 -16
@@ -1,12 +1,15 @@
1
1
  /**
2
2
  * The page chain's security headers and language, checked through the home page, since every
3
- * page declares the same chain and so sends the same policy.
3
+ * page declares the same chain and so sends the same policy. The home controller loads with the
4
+ * file, so the cold import of the chain and its document stays outside each test's time limit.
4
5
  */
5
6
 
6
7
  import { describe, expect, test } from "vitest";
7
8
 
8
9
  import { createWebRouter } from "~/app/initializers/router";
9
10
 
11
+ import "~/app/http/controllers/home";
12
+
10
13
  describe("page", () => {
11
14
  test("sends the Content-Security-Policy with a page", async () => {
12
15
  let response = await createWebRouter().fetch("https://example.com/");
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * The middleware every page declares: the parsed form body, the cross-origin check a
3
- * cookie-backed form relies on, the visitor's session and language, the honeypot every form submission must pass, the security
4
- * headers, and the document's assets and renderer. A page controller passes it as its
5
- * `middleware`, so only requests that render a document pay for it.
3
+ * cookie-backed form relies on, the visitor's session, access and language, the honeypot every
4
+ * form submission must pass, the security headers, and the document's assets and renderer. A
5
+ * page controller passes it as its `middleware`, so only requests that render a document pay.
6
6
  */
7
7
 
8
8
  import { assets } from "@sdxc/assets-middleware";
@@ -15,6 +15,7 @@ import { render } from "remix/middleware/render";
15
15
  import { session } from "remix/middleware/session";
16
16
  import { createMiddleware } from "remix/router";
17
17
 
18
+ import access from "~/app/http/middleware/access";
18
19
  import { assetResolver } from "~/app/initializers/assets";
19
20
  import { honeypot as trap } from "~/app/initializers/honeypot";
20
21
  import { FALLBACK_LANGUAGE, RESOURCES, SUPPORTED_LANGUAGES } from "~/app/initializers/i18n";
@@ -22,14 +23,15 @@ import { sessionCookie, sessionStorage } from "~/app/initializers/session";
22
23
  import { SECURITY_POLICY } from "~/config/security-policy";
23
24
 
24
25
  /**
25
- * Publishes `ctx.formData`, `ctx.session`, `ctx.locale`, `ctx.intl`, `ctx.honeypot`, `ctx.securityHeaders`, `ctx.assets` and
26
- * `ctx.render` on a page's context. A POST without valid honeypot fields is refused with a 400
26
+ * Publishes `ctx.formData`, `ctx.session`, `ctx.access`, `ctx.locale`, `ctx.intl`, `ctx.honeypot`,
27
+ * `ctx.securityHeaders`, `ctx.assets` and `ctx.render` on a page's context. A POST without valid honeypot fields is refused with a 400
27
28
  * before the controller runs; the security headers come right before the assets and renderer.
28
29
  */
29
30
  export default createMiddleware(
30
31
  formData(),
31
32
  cop(),
32
33
  session(sessionCookie, sessionStorage),
34
+ access,
33
35
  i18n({
34
36
  detection: {
35
37
  supportedLanguages: SUPPORTED_LANGUAGES,
@@ -10,7 +10,7 @@ import { Database, database } from "@sdxc/database-middleware/cli";
10
10
  import i18n from "@sdxc/i18n/middleware/cli";
11
11
  import { createLogger } from "@sdxc/logger";
12
12
  import { log } from "@sdxc/logger/middleware/cli";
13
- import mail from "@sdxc/mail/middleware/cli";
13
+ import mailer from "@sdxc/mail/middleware/cli";
14
14
  import { PrintTransport } from "@sdxc/mail/print";
15
15
  import { trace } from "@sdxc/trace-context/middleware/cli";
16
16
 
@@ -18,6 +18,7 @@ import commandset from "~/app/commands";
18
18
  import { FALLBACK_LANGUAGE, RESOURCES, SUPPORTED_LANGUAGES } from "~/app/initializers/i18n";
19
19
  import { openLocalDatabase } from "~/app/initializers/local-database";
20
20
  import { logger } from "~/app/initializers/logger";
21
+ import { mail } from "~/app/initializers/mail";
21
22
  import registry from "~/app/models";
22
23
  import { MAIL_FROM } from "~/config/constants";
23
24
 
@@ -45,9 +46,10 @@ export const cli = createCLI({
45
46
  trace(),
46
47
  database(openLocalDatabase),
47
48
  models(registry, (ctx) => ({ db: ctx.require(Database) })),
48
- mail({
49
+ mailer({
49
50
  transport: (ctx) => new PrintTransport({ write: (text) => ctx.stderr(text) }),
50
51
  from: MAIL_FROM,
52
+ mail,
51
53
  }),
52
54
  i18n({
53
55
  resources: RESOURCES,
@@ -61,6 +63,10 @@ cli.map(
61
63
  commandset.about,
62
64
  lazy(() => import("~/app/commands/about")),
63
65
  );
66
+ cli.map(
67
+ commandset.console,
68
+ lazy(() => import("~/app/commands/console")),
69
+ );
64
70
  cli.map(
65
71
  commandset.db.console,
66
72
  lazy(() => import("~/app/commands/db/console")),
@@ -69,7 +75,34 @@ cli.map(
69
75
  commandset.db.migrate,
70
76
  lazy(() => import("~/app/commands/db/migrate")),
71
77
  );
78
+ cli.map(
79
+ commandset.db.fresh,
80
+ lazy(() => import("~/app/commands/db/fresh")),
81
+ );
82
+ cli.map(
83
+ commandset.db.prepare,
84
+ lazy(() => import("~/app/commands/db/prepare")),
85
+ );
86
+ cli.map(
87
+ commandset.db.seed,
88
+ lazy(() => import("~/app/commands/db/seed")),
89
+ );
90
+ cli.map(
91
+ commandset.deploy,
92
+ lazy(() => import("~/app/commands/deploy")),
93
+ );
94
+ cli.map(
95
+ commandset.emails.preview,
96
+ lazy(() => import("~/app/commands/emails/preview")),
97
+ );
72
98
  cli.map(
73
99
  commandset.typegen,
74
100
  lazy(() => import("~/app/commands/typegen")),
75
101
  );
102
+
103
+ declare module "@sdxc/cli" {
104
+ /** Types every command handler's `ctx` with what the middleware above publishes: `ctx.db`, `ctx.models`, `ctx.email` and `ctx.intl`. */
105
+ interface CLITypes {
106
+ cli: typeof cli;
107
+ }
108
+ }
@@ -11,11 +11,16 @@ import { Database } from "@sdxc/database-middleware";
11
11
  import { database } from "@sdxc/database-middleware/jobs";
12
12
  import { createJobDispatcher } from "@sdxc/jobs";
13
13
  import * as cloudflare from "@sdxc/jobs/cloudflare";
14
+ import mailer from "@sdxc/mail/middleware/jobs";
14
15
  import { env } from "cloudflare:workers";
15
16
 
16
17
  import { openDatabase } from "~/app/initializers/database";
17
18
  import { logger } from "~/app/initializers/logger";
19
+ import { mail } from "~/app/initializers/mail";
20
+ import { queue, transport } from "~/app/initializers/mail-transport";
21
+ import jobs from "~/app/jobs";
18
22
  import registry from "~/app/models";
23
+ import { MAIL_FROM } from "~/config/constants";
19
24
 
20
25
  /** The dispatcher `bootstrap/worker.ts` hands queue batches and cron triggers to. */
21
26
  export const dispatcher = createJobDispatcher({
@@ -24,12 +29,15 @@ export const dispatcher = createJobDispatcher({
24
29
  middleware: [
25
30
  database(openDatabase),
26
31
  models(registry, (ctx) => ({ db: ctx.require(Database) })),
32
+ mailer({ transport, from: MAIL_FROM, mail, queue }),
27
33
  ] as const,
28
34
  timeout: "5 minutes",
29
35
  });
30
36
 
37
+ dispatcher.map(jobs.email, () => import("~/app/jobs/email"));
38
+
31
39
  declare module "@sdxc/jobs" {
32
- /** Types every job handler's `ctx` with what the dispatcher's middleware publishes: `ctx.db` and `ctx.models`. */
40
+ /** Types every job handler's `ctx` with what the dispatcher's middleware publishes: `ctx.db`, `ctx.models` and `ctx.email`. */
33
41
  interface JobTypes {
34
42
  context: JobDispatcherContext<typeof dispatcher>;
35
43
  }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The app's mail transport, delivering into the in-memory `send_email` stand-in tests receive
3
+ * as the `EMAIL` binding, and its queue, handing deliveries to the `QUEUE` binding.
4
+ */
5
+
6
+ import { Mailer } from "@sdxc/mail";
7
+ import { env } from "cloudflare:workers";
8
+ import { describe, expect, test } from "vitest";
9
+
10
+ import { queue, transport } from "~/app/initializers/mail-transport";
11
+ import { MAIL_FROM } from "~/config/constants";
12
+
13
+ describe("transport", () => {
14
+ test("delivers a message through the EMAIL binding", async () => {
15
+ let mailer = new Mailer({ transport, from: MAIL_FROM });
16
+
17
+ await mailer.send({
18
+ to: { email: "ada@example.com" },
19
+ subject: "Welcome",
20
+ html: "<p>Hello.</p>",
21
+ });
22
+
23
+ expect(env.EMAIL).toMatchObject({
24
+ messages: [expect.objectContaining({ subject: "Welcome" })],
25
+ });
26
+ });
27
+
28
+ test("queues a delivery for the email job on the QUEUE binding", async () => {
29
+ await queue({ email: "welcome", to: "ada@example.com", props: {} });
30
+
31
+ expect(env.QUEUE).toMatchObject({ messages: [expect.anything()] });
32
+ });
33
+ });
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Where the Worker's email goes: Cloudflare's `send_email` binding, which `bun dev` simulates and
3
+ * a deploy delivers through Email Routing, and the `email` job `ctx.email.queue()` hands
4
+ * deliveries to. Both run only in the Worker, so the command line never imports this module.
5
+ */
6
+
7
+ import type { QueuedEmail } from "@sdxc/mail";
8
+
9
+ import { CloudflareTransport } from "@sdxc/mail/cloudflare";
10
+ import { env } from "cloudflare:workers";
11
+
12
+ import jobs from "~/app/jobs";
13
+
14
+ /** Sends through the `EMAIL` binding, from an address on a domain Email Routing verified. */
15
+ export const transport = new CloudflareTransport(env.EMAIL);
16
+
17
+ /**
18
+ * Hands a queued email to the `email` job, which delivers it with retries. The dispatcher is
19
+ * imported when the first email is queued, since its own middleware imports this module.
20
+ *
21
+ * @param delivery - The declared email's name, its recipient, locale and props.
22
+ */
23
+ export async function queue(delivery: QueuedEmail): Promise<void> {
24
+ let { dispatcher } = await import("~/app/initializers/dispatcher");
25
+ await dispatcher.enqueue(jobs.email, delivery);
26
+ }
@@ -1,27 +1,27 @@
1
1
  /**
2
- * The app's mail transport, delivering into the in-memory `send_email` stand-in tests receive
3
- * as the `EMAIL` binding.
2
+ * The email registry: every declared email has a template mapped, so a send never fails for
3
+ * an email `sdxc make email` declared but nothing maps.
4
4
  */
5
5
 
6
- import { Mailer } from "@sdxc/mail";
7
- import { env } from "cloudflare:workers";
6
+ import type { AnyEmailDeclaration } from "@sdxc/mail";
7
+
8
+ import { isEmailDeclaration } from "@sdxc/mail";
8
9
  import { describe, expect, test } from "vitest";
9
10
 
10
- import { transport } from "~/app/initializers/mail";
11
- import { MAIL_FROM } from "~/config/constants";
11
+ import emails from "~/app/emails";
12
+ import { mail } from "~/app/initializers/mail";
12
13
 
13
- describe("transport", () => {
14
- test("delivers a message through the EMAIL binding", async () => {
15
- let mailer = new Mailer({ transport, from: MAIL_FROM });
14
+ /** Every declaration in the map, walking its groups. */
15
+ function declared(tree: object): AnyEmailDeclaration[] {
16
+ return Object.values(tree).flatMap((value: object) =>
17
+ isEmailDeclaration(value) ? [value] : declared(value),
18
+ );
19
+ }
16
20
 
17
- await mailer.send({
18
- to: { email: "ada@example.com" },
19
- subject: "Welcome",
20
- html: "<p>Hello.</p>",
21
- });
21
+ describe("mail", () => {
22
+ test("maps a template for every declared email", () => {
23
+ let names = declared(emails).map((email) => email.name);
22
24
 
23
- expect(env.EMAIL).toMatchObject({
24
- messages: [expect.objectContaining({ subject: "Welcome" })],
25
- });
25
+ expect(mail.mapped.map((email) => email.name).sort()).toEqual(names.sort());
26
26
  });
27
27
  });
@@ -1,11 +1,19 @@
1
1
  /**
2
- * Where the app's email goes: Cloudflare's `send_email` binding, which `bun dev` simulates and
3
- * a deploy delivers through Email Routing. The router publishes a mailer over it as `ctx.email`;
4
- * a job builds its own with `new Mailer({ transport, from: MAIL_FROM })` and awaits `send()`.
2
+ * The email registry: every email `app/emails/index.ts` declares, mapped onto its template
3
+ * through `lazy()`, so a send imports only the template it renders. It holds no transport, so
4
+ * the Worker's mailers and the command line share it; `sdxc make email` adds the mapping.
5
5
  */
6
6
 
7
- import { CloudflareTransport } from "@sdxc/mail/cloudflare";
8
- import { env } from "cloudflare:workers";
7
+ import { createMail } from "@sdxc/mail";
9
8
 
10
- /** Sends through the `EMAIL` binding, from an address on a domain Email Routing verified. */
11
- export const transport = new CloudflareTransport(env.EMAIL);
9
+ import { translate } from "~/app/initializers/i18n";
10
+
11
+ /** The registry the router, the job dispatcher and the command line hand their mailers. */
12
+ export const mail = createMail({ translator: translate });
13
+
14
+ declare module "@sdxc/mail" {
15
+ /** Types the `t()` every template receives with the keys of the app's message bundles. */
16
+ interface MailTypes {
17
+ translate: ReturnType<typeof translate>["t"];
18
+ }
19
+ }
@@ -52,6 +52,13 @@ describe("router", () => {
52
52
  expect(SECURITY_TXT.expires.getTime() - Date.now()).toBeGreaterThan(RENEWAL_WINDOW_MS);
53
53
  });
54
54
 
55
+ test("previews the mapped emails at /_emails while developing", async () => {
56
+ let response = await createWebRouter().fetch("https://example.com/_emails");
57
+
58
+ expect(response.status).toBe(200);
59
+ expect(await response.text()).toContain("Email previews");
60
+ });
61
+
55
62
  test("redirects the www. host to the apex domain", async () => {
56
63
  let response = await createWebRouter().fetch("https://www.example.com/sitemap.xml");
57
64
 
@@ -11,7 +11,8 @@ import { database } from "@sdxc/database-middleware";
11
11
  import getClientIP from "@sdxc/get-client-ip/middleware";
12
12
  import { lazy } from "@sdxc/lazy-route";
13
13
  import { log } from "@sdxc/logger/middleware";
14
- import mail from "@sdxc/mail/middleware";
14
+ import mailer from "@sdxc/mail/middleware";
15
+ import { previews } from "@sdxc/mail/previews";
15
16
  import { noWWW } from "@sdxc/no-www-middleware";
16
17
  import { addressKey, CloudflareAdapter } from "@sdxc/rate-limit";
17
18
  import { rateLimit } from "@sdxc/rate-limit/middleware";
@@ -24,9 +25,12 @@ import { asyncContext } from "remix/middleware/async-context";
24
25
  import { createRouter } from "remix/router";
25
26
 
26
27
  import serverTiming from "~/app/http/middleware/server-timing";
28
+ import { assetResolver } from "~/app/initializers/assets";
27
29
  import { openDatabase } from "~/app/initializers/database";
30
+ import { SUPPORTED_LANGUAGES } from "~/app/initializers/i18n";
28
31
  import { logger } from "~/app/initializers/logger";
29
- import { transport } from "~/app/initializers/mail";
32
+ import { mail } from "~/app/initializers/mail";
33
+ import { queue, transport } from "~/app/initializers/mail-transport";
30
34
  import registry from "~/app/models";
31
35
  import { MAIL_FROM } from "~/config/constants";
32
36
  import problems from "~/config/problems";
@@ -48,6 +52,7 @@ declare module "remix/router" {
48
52
  * Builds the router the Worker forwards each request to. Its middleware is what pages and
49
53
  * endpoints both need, written inline so {@link AppContext} reads what each one publishes; a
50
54
  * controller adds its contract's chain, `app/http/middleware/page.ts` or `api.ts`, itself.
55
+ * While developing, `/_emails` previews every mapped email in every supported language.
51
56
  */
52
57
  export function createWebRouter() {
53
58
  let router = createRouter({
@@ -69,7 +74,12 @@ export function createWebRouter() {
69
74
  }),
70
75
  database(openDatabase()),
71
76
  models(registry, (ctx) => ({ db: ctx.db })),
72
- mail({ transport, from: MAIL_FROM }),
77
+ previews(mail, {
78
+ locales: SUPPORTED_LANGUAGES,
79
+ enabled: import.meta.env.DEV,
80
+ stylesheets: () => assetResolver.getStylesheets("resources/layouts/document.tsx"),
81
+ }),
82
+ mailer({ transport, from: MAIL_FROM, mail, queue }),
73
83
  ],
74
84
  });
75
85
 
@@ -0,0 +1,70 @@
1
+ /**
2
+ * The job that delivers queued email: it sends what `ctx.email.queue()` handed the queue, gives
3
+ * up on a delivery no retry can send, and asks for another try when the provider fails.
4
+ */
5
+
6
+ import type { NormalizedMessage, QueuedEmail, SentMessage, Transport } from "@sdxc/mail";
7
+ import type { Result } from "@sdxc/result";
8
+
9
+ import { createJobContext } from "@sdxc/jobs";
10
+ import { NonRetriable, Retry } from "@sdxc/jobs/errors";
11
+ import { createEmail, createMail, email, emails, Mailer, MailError } from "@sdxc/mail";
12
+ import { MemoryTransport } from "@sdxc/mail/memory";
13
+ import { CurrentMailer } from "@sdxc/mail/middleware/jobs";
14
+ import { failure } from "@sdxc/result";
15
+ import * as s from "remix/data-schema";
16
+ import { describe, expect, test } from "vitest";
17
+
18
+ import jobs from "~/app/jobs";
19
+ import handler from "~/app/jobs/email";
20
+ import { MAIL_FROM } from "~/config/constants";
21
+
22
+ /** An email declared for these tests alone. */
23
+ const EMAILS = emails({ greeting: email({ props: s.object({ name: s.string() }) }) });
24
+
25
+ /** A transport whose provider is down. */
26
+ class FailingTransport implements Transport {
27
+ async send(_message: NormalizedMessage): Promise<Result<SentMessage, MailError>> {
28
+ return failure(new MailError("The provider is down."));
29
+ }
30
+ }
31
+
32
+ /** Runs the job for `input` with a mailer over `transport` that sends the test email. */
33
+ async function run(input: QueuedEmail, transport: Transport) {
34
+ let mail = createMail();
35
+ mail.map(
36
+ EMAILS.greeting,
37
+ createEmail(EMAILS.greeting, {
38
+ subject: ({ name }) => `Hello, ${name}`,
39
+ body: (handle) => () => <p>Hello {handle.props.name}</p>,
40
+ }),
41
+ );
42
+ let ctx = createJobContext(jobs.email, { id: "message-1", attempts: 1, input });
43
+ ctx.set(CurrentMailer, new Mailer({ transport, from: MAIL_FROM, mail }), { property: "email" });
44
+ await handler(ctx);
45
+ }
46
+
47
+ describe("email", () => {
48
+ test("sends the queued email", async () => {
49
+ let transport = new MemoryTransport();
50
+
51
+ await run({ email: "greeting", to: "ada@example.com", props: { name: "Ada" } }, transport);
52
+
53
+ expect(transport.last?.subject).toBe("Hello, Ada");
54
+ });
55
+
56
+ test("gives up on an email nothing maps", async () => {
57
+ let sent = run({ email: "missing", to: "ada@example.com", props: {} }, new MemoryTransport());
58
+
59
+ await expect(sent).rejects.toBeInstanceOf(NonRetriable);
60
+ });
61
+
62
+ test("retries when the provider fails", async () => {
63
+ let sent = run(
64
+ { email: "greeting", to: "ada@example.com", props: { name: "Ada" } },
65
+ new FailingTransport(),
66
+ );
67
+
68
+ await expect(sent).rejects.toBeInstanceOf(Retry);
69
+ });
70
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Delivers a queued email. A delivery no retry can send (an email nothing maps, props its
3
+ * schema refuses) ends here; any other failure, such as the provider being down, is retried.
4
+ */
5
+
6
+ import { createJobHandler } from "@sdxc/jobs";
7
+ import { EmailRefusedError } from "@sdxc/mail";
8
+ import { isFailure } from "@sdxc/result";
9
+
10
+ import jobs from "~/app/jobs";
11
+
12
+ export default createJobHandler(jobs.email, async (ctx) => {
13
+ let sent = await ctx.email.deliver(ctx.input);
14
+ if (isFailure(sent) && sent.error instanceof EmailRefusedError) ctx.exit(sent.error.message);
15
+ if (isFailure(sent)) ctx.retry({ cause: sent.error });
16
+ });
@@ -4,7 +4,11 @@
4
4
  * a job without loading the handler that runs it.
5
5
  */
6
6
 
7
- import { jobs } from "@sdxc/jobs";
7
+ import { job, jobs } from "@sdxc/jobs";
8
+ import { EMAIL_DELIVERY } from "@sdxc/mail";
8
9
 
9
10
  /** The app's jobs; `sdxc make job <name>` declares one here and maps its handler. */
10
- export default jobs({});
11
+ export default jobs({
12
+ /** Delivers what `ctx.email.queue()` handed the queue, for every declared email. */
13
+ email: job({ input: EMAIL_DELIVERY }),
14
+ });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Everything a visitor can do in the app, named once so a page, an endpoint, a tool and a test
3
+ * all check the same ability. `sdxc make policy <resource>` adds a resource's abilities here, and
4
+ * importing the catalog does no work, so browser code reads it too.
5
+ */
6
+
7
+ import { abilities } from "@sdxc/authz";
8
+
9
+ /**
10
+ * Each ability by its dot-joined keys, such as `post.update`. One declared with a `context` is
11
+ * checked against a record; one without is checked with nothing.
12
+ */
13
+ export default abilities({});
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The policy compiles, so every grant names an ability the catalog declares and every condition
3
+ * reads a fact or record it is given; `sdxc make policy` writes a test per resource beside it.
4
+ */
5
+
6
+ import { isSuccess } from "@sdxc/result";
7
+ import { describe, expect, test } from "vitest";
8
+
9
+ import policy from "~/app/policies/policy";
10
+
11
+ describe("policy", () => {
12
+ test("compiles", () => {
13
+ expect(isSuccess(policy.compile())).toBe(true);
14
+ });
15
+ });
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Who may do what. `everyone` lists what any visitor may do, signed in or not, and `member` what
3
+ * a signed-in visitor adds; `sdxc make policy` grants a resource's reads to everyone and its
4
+ * writes to members. A guard under `guards` refuses whatever role is held.
5
+ */
6
+
7
+ import { definePolicy, fact } from "@sdxc/authz";
8
+
9
+ import abilities from "~/app/policies/abilities";
10
+
11
+ /** The signed-in visitor, as a condition reads it under `actor`; absent for a guest. */
12
+ export interface Actor {
13
+ id: string;
14
+ }
15
+
16
+ /** The app's policy, bound to each request by `app/http/middleware/access.ts`. */
17
+ const policy = definePolicy(abilities, {
18
+ facts: { actor: fact<Actor>({ optional: true }) },
19
+ everyone: [],
20
+ roles: { member: [] },
21
+ });
22
+
23
+ declare module "@sdxc/authz" {
24
+ /** Types `ctx.access` and every check against this policy's catalog and facts. */
25
+ interface AuthzTypes {
26
+ policy: typeof policy;
27
+ }
28
+ }
29
+
30
+ export default policy;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Runs one of the app's own commands from inside another, as typing it would, so a command
3
+ * composed of others (`db prepare`, `db fresh --seed`) behaves exactly like its parts.
4
+ */
5
+
6
+ import type { AnyCommandContext, CommandResult } from "@sdxc/cli";
7
+
8
+ import { exit, success } from "@sdxc/cli";
9
+
10
+ /**
11
+ * Runs `argv` on the app's command line with the calling run's streams, directory and
12
+ * environment, so its output lands where the caller's would. Its failure has been printed
13
+ * already, so the caller fails with the same exit code and no second message.
14
+ *
15
+ * @example let migrated = await runCommand(ctx, ["db", "migrate"]);
16
+ */
17
+ export async function runCommand(
18
+ ctx: AnyCommandContext,
19
+ argv: readonly string[],
20
+ ): Promise<CommandResult> {
21
+ let { cli } = await import("~/app/initializers/cli");
22
+ let code = await cli.run(argv, {
23
+ cwd: ctx.cwd,
24
+ env: ctx.env,
25
+ stdout: { write: ctx.stdout },
26
+ stderr: { write: ctx.stderr },
27
+ signal: ctx.signal,
28
+ });
29
+ return code === 0 ? success() : exit(code);
30
+ }
@@ -6,7 +6,12 @@ import { openDatabase } from "@sdxc/cloudflare-mocks/sqlite";
6
6
  import { isFailure, unwrap } from "@sdxc/result";
7
7
  import { describe, expect, test } from "vitest";
8
8
 
9
- import { applyMigrations } from "~/app/services/migrations";
9
+ import {
10
+ applyMigrations,
11
+ dropTables,
12
+ hasAppliedMigrations,
13
+ pendingMigrations,
14
+ } from "~/app/services/migrations";
10
15
 
11
16
  /** Two migrations, given out of order to show the runner sorts them. */
12
17
  const MIGRATIONS = [
@@ -43,3 +48,45 @@ describe("applyMigrations", () => {
43
48
  ]);
44
49
  });
45
50
  });
51
+
52
+ describe("pendingMigrations", () => {
53
+ test("lists what applyMigrations would run, and applies nothing", () => {
54
+ let database = openDatabase(":memory:");
55
+ unwrap(applyMigrations(database, MIGRATIONS.slice(1)));
56
+
57
+ let pending = pendingMigrations(database, MIGRATIONS);
58
+
59
+ expect(pending.map((migration) => migration.name)).toEqual(["0002_AddTitle.sql"]);
60
+ expect(database.query("SELECT count(*) AS applied FROM d1_migrations").all()).toEqual([
61
+ { applied: 1 },
62
+ ]);
63
+ });
64
+ });
65
+
66
+ describe("dropTables", () => {
67
+ test("drops the migrations' tables and views, and the journal, so the chain runs again", () => {
68
+ let database = openDatabase(":memory:");
69
+ unwrap(applyMigrations(database, MIGRATIONS));
70
+ database.exec(
71
+ "CREATE TABLE comments (id INTEGER PRIMARY KEY AUTOINCREMENT, post_id TEXT REFERENCES posts (id)); CREATE VIEW titles AS SELECT title FROM posts;",
72
+ );
73
+
74
+ let dropped = unwrap(dropTables(database));
75
+
76
+ expect(dropped.toSorted()).toEqual(["comments", "d1_migrations", "posts"]);
77
+ expect(
78
+ database.query("SELECT name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'").all(),
79
+ ).toEqual([]);
80
+ expect(unwrap(applyMigrations(database, MIGRATIONS))).toHaveLength(2);
81
+ });
82
+ });
83
+
84
+ describe("hasAppliedMigrations", () => {
85
+ test("tells a new database from one a migration ran on", () => {
86
+ let database = openDatabase(":memory:");
87
+ let before = hasAppliedMigrations(database);
88
+ unwrap(applyMigrations(database, MIGRATIONS));
89
+
90
+ expect([before, hasAppliedMigrations(database)]).toEqual([false, true]);
91
+ });
92
+ });