@sdxc/kit 0.0.0-stage → 2026.10.17

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 (298) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +93 -2
  3. package/changelog.jsonl +1097 -0
  4. package/config.schema.json +89 -0
  5. package/dist/author.d.ts +36 -0
  6. package/dist/author.js +107 -0
  7. package/dist/bin.d.ts +9 -0
  8. package/dist/bin.js +10 -0
  9. package/dist/cli.d.ts +33 -0
  10. package/dist/cli.js +58 -0
  11. package/dist/commands/create.d.ts +20 -0
  12. package/dist/commands/create.js +169 -0
  13. package/dist/commands/init.d.ts +16 -0
  14. package/dist/commands/init.js +27 -0
  15. package/dist/commands/make.d.ts +335 -0
  16. package/dist/commands/make.js +152 -0
  17. package/dist/commands/run.d.ts +23 -0
  18. package/dist/commands/run.js +53 -0
  19. package/dist/commands/ui/add.d.ts +23 -0
  20. package/dist/commands/ui/add.js +50 -0
  21. package/dist/commands/ui/diff.d.ts +20 -0
  22. package/dist/commands/ui/diff.js +28 -0
  23. package/dist/commands/ui/list.d.ts +24 -0
  24. package/dist/commands/ui/list.js +45 -0
  25. package/dist/commands/ui/remove.d.ts +22 -0
  26. package/dist/commands/ui/remove.js +48 -0
  27. package/dist/commands/ui/update.d.ts +23 -0
  28. package/dist/commands/ui/update.js +37 -0
  29. package/dist/commands/upgrade.d.ts +30 -0
  30. package/dist/commands/upgrade.js +184 -0
  31. package/dist/config.d.ts +81 -0
  32. package/dist/config.js +157 -0
  33. package/dist/contract.d.ts +161 -0
  34. package/dist/contract.js +308 -0
  35. package/dist/create/github.d.ts +63 -0
  36. package/dist/create/github.js +172 -0
  37. package/dist/create/target.d.ts +31 -0
  38. package/dist/create/target.js +56 -0
  39. package/dist/create/templates.d.ts +69 -0
  40. package/dist/create/templates.js +251 -0
  41. package/dist/dependencies.d.ts +34 -0
  42. package/dist/dependencies.js +76 -0
  43. package/dist/diff.d.ts +14 -0
  44. package/dist/diff.js +103 -0
  45. package/dist/format.d.ts +21 -0
  46. package/dist/format.js +40 -0
  47. package/dist/init.d.ts +40 -0
  48. package/dist/init.js +100 -0
  49. package/dist/lock.d.ts +74 -0
  50. package/dist/lock.js +214 -0
  51. package/dist/make/declare.d.ts +54 -0
  52. package/dist/make/declare.js +598 -0
  53. package/dist/make/generate.d.ts +62 -0
  54. package/dist/make/generate.js +160 -0
  55. package/dist/make/imports.d.ts +43 -0
  56. package/dist/make/imports.js +105 -0
  57. package/dist/make/index.d.ts +43 -0
  58. package/dist/make/index.js +140 -0
  59. package/dist/make/kinds.d.ts +40 -0
  60. package/dist/make/kinds.js +96 -0
  61. package/dist/make/migrations.d.ts +41 -0
  62. package/dist/make/migrations.js +116 -0
  63. package/dist/make/names.d.ts +31 -0
  64. package/dist/make/names.js +47 -0
  65. package/dist/make/stubs.d.ts +44 -0
  66. package/dist/make/stubs.js +89 -0
  67. package/dist/middleware.d.ts +44 -0
  68. package/dist/middleware.js +49 -0
  69. package/dist/package-manager.d.ts +41 -0
  70. package/dist/package-manager.js +80 -0
  71. package/dist/project.d.ts +19 -0
  72. package/dist/project.js +42 -0
  73. package/dist/releases.d.ts +40 -0
  74. package/dist/releases.js +110 -0
  75. package/dist/testing.d.ts +60 -0
  76. package/dist/testing.js +148 -0
  77. package/dist/ui/commands.d.ts +114 -0
  78. package/dist/ui/commands.js +432 -0
  79. package/dist/ui/imports.d.ts +46 -0
  80. package/dist/ui/imports.js +160 -0
  81. package/dist/ui/layout.d.ts +48 -0
  82. package/dist/ui/layout.js +138 -0
  83. package/dist/ui/registry.d.ts +58 -0
  84. package/dist/ui/registry.js +82 -0
  85. package/dist/upgrade/changelog.d.ts +50 -0
  86. package/dist/upgrade/changelog.js +69 -0
  87. package/dist/upgrade/project.d.ts +62 -0
  88. package/dist/upgrade/project.js +132 -0
  89. package/dist/upgrade/prompt.d.ts +51 -0
  90. package/dist/upgrade/prompt.js +227 -0
  91. package/dist/upgrade/registry.d.ts +33 -0
  92. package/dist/upgrade/registry.js +50 -0
  93. package/lock.schema.json +60 -0
  94. package/package.json +44 -5
  95. package/registry/ui/accordion.json +15 -0
  96. package/registry/ui/alert-dialog.json +20 -0
  97. package/registry/ui/alert.json +15 -0
  98. package/registry/ui/aspect-ratio.json +13 -0
  99. package/registry/ui/attachment.json +15 -0
  100. package/registry/ui/avatar.json +15 -0
  101. package/registry/ui/badge.json +13 -0
  102. package/registry/ui/breadcrumbs.json +15 -0
  103. package/registry/ui/bubble.json +13 -0
  104. package/registry/ui/button.json +16 -0
  105. package/registry/ui/calendar.json +16 -0
  106. package/registry/ui/card.json +18 -0
  107. package/registry/ui/carousel.json +15 -0
  108. package/registry/ui/chart.json +13 -0
  109. package/registry/ui/checkbox-group.json +13 -0
  110. package/registry/ui/checkbox.json +16 -0
  111. package/registry/ui/color-area.json +13 -0
  112. package/registry/ui/color-field.json +20 -0
  113. package/registry/ui/color-picker.json +17 -0
  114. package/registry/ui/color-slider.json +13 -0
  115. package/registry/ui/color-swatch-picker.json +15 -0
  116. package/registry/ui/color-swatch.json +13 -0
  117. package/registry/ui/color-wheel.json +13 -0
  118. package/registry/ui/combobox.json +15 -0
  119. package/registry/ui/command.json +13 -0
  120. package/registry/ui/confirm.json +19 -0
  121. package/registry/ui/context-menu.json +16 -0
  122. package/registry/ui/date-field.json +19 -0
  123. package/registry/ui/date-picker.json +15 -0
  124. package/registry/ui/date-range-picker.json +16 -0
  125. package/registry/ui/description.json +13 -0
  126. package/registry/ui/dialog.json +19 -0
  127. package/registry/ui/disclosure.json +18 -0
  128. package/registry/ui/drawer.json +18 -0
  129. package/registry/ui/drop-indicator.json +13 -0
  130. package/registry/ui/drop-zone.json +13 -0
  131. package/registry/ui/empty.json +15 -0
  132. package/registry/ui/field-error.json +13 -0
  133. package/registry/ui/file-trigger.json +13 -0
  134. package/registry/ui/form.json +21 -0
  135. package/registry/ui/grid-list.json +18 -0
  136. package/registry/ui/group.json +13 -0
  137. package/registry/ui/header.json +13 -0
  138. package/registry/ui/heading-scope.json +20 -0
  139. package/registry/ui/heading.json +15 -0
  140. package/registry/ui/highlight.json +16 -0
  141. package/registry/ui/hover-card.json +13 -0
  142. package/registry/ui/image-placeholder.json +16 -0
  143. package/registry/ui/input.json +13 -0
  144. package/registry/ui/item.json +13 -0
  145. package/registry/ui/keyboard.json +13 -0
  146. package/registry/ui/label.json +13 -0
  147. package/registry/ui/link-button.json +16 -0
  148. package/registry/ui/link.json +13 -0
  149. package/registry/ui/listbox.json +15 -0
  150. package/registry/ui/logo.json +15 -0
  151. package/registry/ui/marker.json +13 -0
  152. package/registry/ui/menu.json +19 -0
  153. package/registry/ui/menubar.json +13 -0
  154. package/registry/ui/message-scroller.json +16 -0
  155. package/registry/ui/message.json +13 -0
  156. package/registry/ui/meter.json +13 -0
  157. package/registry/ui/modal.json +15 -0
  158. package/registry/ui/nav-link.json +13 -0
  159. package/registry/ui/navigation-menu.json +15 -0
  160. package/registry/ui/number-field.json +15 -0
  161. package/registry/ui/otp-field.json +15 -0
  162. package/registry/ui/overlay-arrow.json +13 -0
  163. package/registry/ui/pagination.json +16 -0
  164. package/registry/ui/popover.json +13 -0
  165. package/registry/ui/progress-bar.json +13 -0
  166. package/registry/ui/radio-group.json +13 -0
  167. package/registry/ui/range-calendar.json +16 -0
  168. package/registry/ui/resizable.json +13 -0
  169. package/registry/ui/scroll-area.json +13 -0
  170. package/registry/ui/search-field.json +16 -0
  171. package/registry/ui/section.json +13 -0
  172. package/registry/ui/select.json +13 -0
  173. package/registry/ui/selection-indicator.json +13 -0
  174. package/registry/ui/sentinel-row.json +16 -0
  175. package/registry/ui/separator.json +13 -0
  176. package/registry/ui/shared-element.json +13 -0
  177. package/registry/ui/sheet.json +15 -0
  178. package/registry/ui/shortcut-list.json +18 -0
  179. package/registry/ui/sidebar.json +20 -0
  180. package/registry/ui/skeleton.json +13 -0
  181. package/registry/ui/slider.json +16 -0
  182. package/registry/ui/spinner.json +13 -0
  183. package/registry/ui/switch.json +16 -0
  184. package/registry/ui/table.json +16 -0
  185. package/registry/ui/tabs.json +17 -0
  186. package/registry/ui/tag-group.json +18 -0
  187. package/registry/ui/text-field.json +19 -0
  188. package/registry/ui/text.json +13 -0
  189. package/registry/ui/textarea.json +13 -0
  190. package/registry/ui/time-field.json +19 -0
  191. package/registry/ui/toast.json +16 -0
  192. package/registry/ui/toggle-button.json +17 -0
  193. package/registry/ui/toolbar.json +13 -0
  194. package/registry/ui/tooltip.json +16 -0
  195. package/registry/ui/tree.json +18 -0
  196. package/registry/ui/typeset.json +16 -0
  197. package/registry/ui/youtube-embed.json +16 -0
  198. package/stubs/command.stub +16 -0
  199. package/stubs/command.test.stub +21 -0
  200. package/stubs/controller.api.stub +21 -0
  201. package/stubs/controller.api.test.stub +18 -0
  202. package/stubs/controller.form.stub +63 -0
  203. package/stubs/controller.form.test.stub +61 -0
  204. package/stubs/controller.stub +24 -0
  205. package/stubs/controller.test.stub +18 -0
  206. package/stubs/email.stub +26 -0
  207. package/stubs/email.test.stub +21 -0
  208. package/stubs/factory.stub +13 -0
  209. package/stubs/initializer.stub +9 -0
  210. package/stubs/initializer.test.stub +16 -0
  211. package/stubs/job.cron.stub +16 -0
  212. package/stubs/job.stub +15 -0
  213. package/stubs/job.test.stub +18 -0
  214. package/stubs/mcp.resource.stub +15 -0
  215. package/stubs/mcp.stub +13 -0
  216. package/stubs/mcp.test.stub +18 -0
  217. package/stubs/middleware.stub +16 -0
  218. package/stubs/middleware.test.stub +16 -0
  219. package/stubs/migration.stub +1 -0
  220. package/stubs/model.stub +33 -0
  221. package/stubs/model.test.stub +18 -0
  222. package/stubs/policy.stub +16 -0
  223. package/stubs/policy.test.stub +20 -0
  224. package/stubs/service.stub +17 -0
  225. package/stubs/service.test.stub +17 -0
  226. package/stubs/validator.stub +17 -0
  227. package/stubs/validator.test.stub +25 -0
  228. package/templates/app/.env.example +16 -0
  229. package/templates/app/README.md +84 -0
  230. package/templates/app/app/commands/about.test.ts +29 -0
  231. package/templates/app/app/commands/about.ts +34 -0
  232. package/templates/app/app/commands/db/console.ts +6 -0
  233. package/templates/app/app/commands/db/migrate.test.ts +30 -0
  234. package/templates/app/app/commands/db/migrate.ts +55 -0
  235. package/templates/app/app/commands/index.ts +31 -0
  236. package/templates/app/app/commands/typegen.test.ts +17 -0
  237. package/templates/app/app/commands/typegen.ts +16 -0
  238. package/templates/app/app/http/controllers/api/health.tsx +15 -0
  239. package/templates/app/app/http/controllers/api/not-found.tsx +16 -0
  240. package/templates/app/app/http/controllers/api/openapi.tsx +17 -0
  241. package/templates/app/app/http/controllers/home.tsx +30 -0
  242. package/templates/app/app/http/controllers/mcp.tsx +16 -0
  243. package/templates/app/app/http/controllers/not-found.tsx +30 -0
  244. package/templates/app/app/http/controllers/robots.test.ts +18 -0
  245. package/templates/app/app/http/controllers/robots.ts +24 -0
  246. package/templates/app/app/http/controllers/sitemap.test.ts +28 -0
  247. package/templates/app/app/http/controllers/sitemap.ts +25 -0
  248. package/templates/app/app/http/middleware/api.ts +10 -0
  249. package/templates/app/app/http/middleware/page.test.ts +40 -0
  250. package/templates/app/app/http/middleware/page.ts +49 -0
  251. package/templates/app/app/http/middleware/server-timing.test.ts +15 -0
  252. package/templates/app/app/http/middleware/server-timing.ts +42 -0
  253. package/templates/app/app/initializers/assets.ts +11 -0
  254. package/templates/app/app/initializers/captcha.ts +11 -0
  255. package/templates/app/app/initializers/cli.ts +75 -0
  256. package/templates/app/app/initializers/database.test.ts +18 -0
  257. package/templates/app/app/initializers/database.ts +18 -0
  258. package/templates/app/app/initializers/dispatcher.ts +36 -0
  259. package/templates/app/app/initializers/honeypot.ts +11 -0
  260. package/templates/app/app/initializers/i18n.test.ts +16 -0
  261. package/templates/app/app/initializers/i18n.ts +28 -0
  262. package/templates/app/app/initializers/local-database.test.ts +33 -0
  263. package/templates/app/app/initializers/local-database.ts +53 -0
  264. package/templates/app/app/initializers/logger.ts +10 -0
  265. package/templates/app/app/initializers/mail.test.ts +27 -0
  266. package/templates/app/app/initializers/mail.ts +11 -0
  267. package/templates/app/app/initializers/mcp.ts +16 -0
  268. package/templates/app/app/initializers/openapi.ts +21 -0
  269. package/templates/app/app/initializers/router.test.ts +61 -0
  270. package/templates/app/app/initializers/router.tsx +111 -0
  271. package/templates/app/app/initializers/seo.ts +15 -0
  272. package/templates/app/app/initializers/session.test.ts +34 -0
  273. package/templates/app/app/initializers/session.ts +29 -0
  274. package/templates/app/app/initializers/spam.ts +10 -0
  275. package/templates/app/app/jobs/index.ts +10 -0
  276. package/templates/app/app/mcp/resources/index.ts +9 -0
  277. package/templates/app/app/mcp/tools/index.ts +10 -0
  278. package/templates/app/app/models/index.ts +10 -0
  279. package/templates/app/app/services/migrations.test.ts +45 -0
  280. package/templates/app/app/services/migrations.ts +60 -0
  281. package/templates/app/app/test/cloudflare-workers.ts +25 -0
  282. package/templates/app/bootstrap/browser.ts +27 -0
  283. package/templates/app/bootstrap/cli.ts +9 -0
  284. package/templates/app/bootstrap/worker.ts +31 -0
  285. package/templates/app/cloudflare.config.ts +56 -0
  286. package/templates/app/config/constants.ts +19 -0
  287. package/templates/app/config/problems.ts +24 -0
  288. package/templates/app/config/security-policy.ts +34 -0
  289. package/templates/app/config/security-txt.ts +28 -0
  290. package/templates/app/database/migrations/.gitkeep +0 -0
  291. package/templates/app/gitignore +9 -0
  292. package/templates/app/package.json +68 -0
  293. package/templates/app/resources/layouts/document.tsx +79 -0
  294. package/templates/app/resources/locales/en.json +10 -0
  295. package/templates/app/routes/api.ts +29 -0
  296. package/templates/app/routes/web.ts +14 -0
  297. package/templates/app/tsconfig.json +40 -0
  298. package/templates/app/vite.config.ts +44 -0
@@ -0,0 +1,24 @@
1
+ /**
2
+ * `/robots.txt`: every crawler may read every page, and the sitemap lists them. Add a rule here
3
+ * before a path should stay out of search results.
4
+ */
5
+
6
+ import { text } from "@sdxc/http/response";
7
+ import { stringify } from "@sdxc/robots";
8
+ import { createAction } from "remix/router";
9
+
10
+ import { seo } from "~/app/initializers/seo";
11
+ import routes from "~/routes/web";
12
+
13
+ export default createAction(routes.robots, {
14
+ /** GET /robots.txt, cached for a day since it changes only with a deploy. */
15
+ handler: () =>
16
+ text(
17
+ stringify({
18
+ groups: [{ userAgents: ["*"], rules: [{ allow: true, pattern: "/" }] }],
19
+ sitemaps: [seo.absolute(routes.sitemap.href())],
20
+ records: [],
21
+ }),
22
+ { headers: { "Cache-Control": "public, max-age=86400" } },
23
+ ),
24
+ });
@@ -0,0 +1,28 @@
1
+ /**
2
+ * `/sitemap.xml`, fetched through the app's router.
3
+ */
4
+
5
+ import { describe, expect, test } from "vitest";
6
+
7
+ import { createWebRouter } from "~/app/initializers/router";
8
+
9
+ describe("sitemap", () => {
10
+ test("lists the home page at its canonical URL, cached for an hour", async () => {
11
+ let response = await createWebRouter().fetch("https://preview.example.net/sitemap.xml");
12
+
13
+ expect(response.headers.get("content-type")).toBe("text/xml; charset=utf-8");
14
+ expect(response.headers.get("cache-control")).toBe("public, max-age=3600");
15
+ let body = await response.text();
16
+ expect(body).toContain("<loc>https://example.com/</loc>");
17
+ expect(body).toContain("<priority>1</priority>");
18
+ });
19
+
20
+ test("leaves out robots.txt and the sitemap itself", async () => {
21
+ let response = await createWebRouter().fetch("https://preview.example.net/sitemap.xml");
22
+ let body = await response.text();
23
+
24
+ expect(body).not.toContain("robots.txt");
25
+ expect(body).not.toContain("sitemap.xml");
26
+ expect(body).not.toContain("preview.example.net");
27
+ });
28
+ });
@@ -0,0 +1,25 @@
1
+ /**
2
+ * `/sitemap.xml`: every public page, at its canonical URL, for search engines to find. Every
3
+ * GET route in `routes/web.ts` needs a decision here, so a new route fails to typecheck until
4
+ * it is listed or excluded.
5
+ */
6
+
7
+ import { xml } from "@sdxc/http/response";
8
+ import { fromRoutes } from "@sdxc/sitemap/remix";
9
+ import { createAction } from "remix/router";
10
+
11
+ import { seo } from "~/app/initializers/seo";
12
+ import routes from "~/routes/web";
13
+
14
+ export default createAction(routes.sitemap, {
15
+ /** GET /sitemap.xml, cached for an hour, with URLs on `SITE_URL` whichever host served it. */
16
+ handler: () => {
17
+ let sitemap = fromRoutes(routes, seo.baseUrl, {
18
+ home: { priority: 1 },
19
+ robots: false,
20
+ sitemap: false,
21
+ notFound: false,
22
+ });
23
+ return xml(sitemap.toString(), { headers: { "Cache-Control": "public, max-age=3600" } });
24
+ },
25
+ });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The middleware every API endpoint declares: CORS, so a browser on another origin can call
3
+ * it. An endpoint reads its own body, as JSON or a form, so it is left unparsed here.
4
+ */
5
+
6
+ import { cors } from "remix/middleware/cors";
7
+ import { createMiddleware } from "remix/router";
8
+
9
+ /** Answers CORS preflights and adds the CORS headers to every endpoint's response. */
10
+ export default createMiddleware(cors());
@@ -0,0 +1,40 @@
1
+ /**
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.
4
+ */
5
+
6
+ import { describe, expect, test } from "vitest";
7
+
8
+ import { createWebRouter } from "~/app/initializers/router";
9
+
10
+ describe("page", () => {
11
+ test("sends the Content-Security-Policy with a page", async () => {
12
+ let response = await createWebRouter().fetch("https://example.com/");
13
+
14
+ expect(response.headers.get("content-security-policy")).toContain("default-src 'self'");
15
+ expect(response.headers.get("x-frame-options")).toBe("DENY");
16
+ });
17
+
18
+ test("advertises no nonce when the page renders no inline script", async () => {
19
+ let response = await createWebRouter().fetch("https://example.com/");
20
+
21
+ expect(response.headers.get("content-security-policy")).not.toContain("'nonce-");
22
+ });
23
+
24
+ test("renders in the language the visitor asks for, or the fallback", async () => {
25
+ let response = await createWebRouter().fetch(
26
+ new Request("https://example.com/", { headers: { "accept-language": "fr-FR, en;q=0.5" } }),
27
+ );
28
+
29
+ let html = await response.text();
30
+ expect(html).toContain('<html lang="en"');
31
+ expect(html).toContain("<h1>Welcome</h1>");
32
+ expect(html).toContain("<code>app/http/controllers/home.tsx</code>");
33
+ });
34
+
35
+ test("links the canonical URL on SITE_URL, whichever host served the page", async () => {
36
+ let response = await createWebRouter().fetch("https://preview.example.net/?utm_source=mail");
37
+
38
+ expect(await response.text()).toContain('<link rel="canonical" href="https://example.com/"');
39
+ });
40
+ });
@@ -0,0 +1,49 @@
1
+ /**
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.
6
+ */
7
+
8
+ import { assets } from "@sdxc/assets-middleware";
9
+ import { honeypot } from "@sdxc/honeypot/middleware";
10
+ import i18n from "@sdxc/i18n/middleware";
11
+ import { securityHeaders } from "@sdxc/security-headers/middleware";
12
+ import { cop } from "remix/middleware/cop";
13
+ import { formData } from "remix/middleware/form-data";
14
+ import { render } from "remix/middleware/render";
15
+ import { session } from "remix/middleware/session";
16
+ import { createMiddleware } from "remix/router";
17
+
18
+ import { assetResolver } from "~/app/initializers/assets";
19
+ import { honeypot as trap } from "~/app/initializers/honeypot";
20
+ import { FALLBACK_LANGUAGE, RESOURCES, SUPPORTED_LANGUAGES } from "~/app/initializers/i18n";
21
+ import { sessionCookie, sessionStorage } from "~/app/initializers/session";
22
+ import { SECURITY_POLICY } from "~/config/security-policy";
23
+
24
+ /**
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
27
+ * before the controller runs; the security headers come right before the assets and renderer.
28
+ */
29
+ export default createMiddleware(
30
+ formData(),
31
+ cop(),
32
+ session(sessionCookie, sessionStorage),
33
+ i18n({
34
+ detection: {
35
+ supportedLanguages: SUPPORTED_LANGUAGES,
36
+ fallbackLanguage: FALLBACK_LANGUAGE,
37
+ sessionKey: "lng",
38
+ },
39
+ resources: RESOURCES,
40
+ }),
41
+ honeypot(trap),
42
+ securityHeaders(SECURITY_POLICY),
43
+ assets(assetResolver, {
44
+ stylesheets: "resources/layouts/document.tsx",
45
+ script: "bootstrap/browser.ts",
46
+ nonce: (ctx) => ctx.securityHeaders.nonce,
47
+ }),
48
+ render({ assets: assetResolver }),
49
+ );
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The `Server-Timing` header every response carries, checked through the app's router.
3
+ */
4
+
5
+ import { describe, expect, test } from "vitest";
6
+
7
+ import { createWebRouter } from "~/app/initializers/router";
8
+
9
+ describe("serverTiming", () => {
10
+ test("reports how long the request took", async () => {
11
+ let response = await createWebRouter().fetch("https://example.com/api/health");
12
+
13
+ expect(response.headers.get("server-timing")).toMatch(/^app(;dur=[\d.]+)?$/);
14
+ });
15
+ });
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Publishes a `Server-Timing` collector on every request as `ctx.timing` and writes what it
3
+ * measured onto the response, so browser devtools show where a request spent its time:
4
+ * `await ctx.timing.measure("db", "listPosts", () => listPosts())`.
5
+ */
6
+
7
+ import type { Middleware } from "remix/router";
8
+
9
+ import { TimingCollector } from "@sdxc/server-timing";
10
+ import { createContextKey } from "remix/router";
11
+
12
+ declare module "remix/router" {
13
+ interface RequestContext {
14
+ /** The request's timings, sent as its `Server-Timing` header. */
15
+ timing: TimingCollector;
16
+ }
17
+ }
18
+
19
+ /** Reads the request's collector by key, for code holding a context whose chain is unknown. */
20
+ export const Timing: { defaultValue?: TimingCollector } = createContextKey<TimingCollector>();
21
+
22
+ /**
23
+ * Creates the middleware. The whole request is measured as `app`, recorded after every entry a
24
+ * handler adds; the header goes on a copy of the response, so a redirect or a proxied
25
+ * response with immutable headers carries it too.
26
+ */
27
+ export default function serverTiming(): Middleware<{
28
+ key: typeof Timing;
29
+ value: TimingCollector;
30
+ property: "timing";
31
+ }> {
32
+ return async (ctx, next) => {
33
+ let timing = new TimingCollector();
34
+ ctx.set(Timing, timing, { property: "timing" });
35
+ let response = await timing.measure("app", "", () => next());
36
+ return new Response(response.body, {
37
+ status: response.status,
38
+ statusText: response.statusText,
39
+ headers: timing.toHeaders(new Headers(response.headers)),
40
+ });
41
+ };
42
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The asset resolver, read from the manifest the Vite plugin writes: the source files
3
+ * `vp dev` serves while editing, and the hashed files `vp build` emits. The document
4
+ * links what it returns, so no tag names a file the build renamed.
5
+ */
6
+
7
+ import { createAssetResolver } from "@pitlane/assets";
8
+ import manifest from "@pitlane/assets/manifest";
9
+
10
+ /** Resolves the document's stylesheets, its client entry and every island by source path. */
11
+ export const assetResolver = createAssetResolver(manifest);
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The CAPTCHA a form asks a visitor to pass before its action runs: Cloudflare Turnstile,
3
+ * verified on the server with `TURNSTILE_SECRET_KEY`. A form renders the widget with
4
+ * `TURNSTILE_SITE_KEY` and its action declares `captcha(turnstile)`.
5
+ */
6
+
7
+ import { Turnstile } from "@sdxc/captcha/turnstile";
8
+ import { env } from "cloudflare:workers";
9
+
10
+ /** Verifies the token the widget writes into the form, once per submission. */
11
+ export const turnstile = new Turnstile({ secretKey: env.TURNSTILE_SECRET_KEY });
@@ -0,0 +1,75 @@
1
+ /**
2
+ * The app's command line. `app/commands/index.ts` declares its commands and each handler is
3
+ * mapped here through `lazy()`, so a run imports only the command it reaches; `sdxc make
4
+ * command` adds the mapping. It runs on Bun, outside the Worker, started by `bootstrap/cli.ts`.
5
+ */
6
+
7
+ import { createCLI, lazy } from "@sdxc/cli";
8
+ import { models } from "@sdxc/data-model/cli";
9
+ import { Database, database } from "@sdxc/database-middleware/cli";
10
+ import i18n from "@sdxc/i18n/middleware/cli";
11
+ import { createLogger } from "@sdxc/logger";
12
+ import { log } from "@sdxc/logger/middleware/cli";
13
+ import mail from "@sdxc/mail/middleware/cli";
14
+ import { PrintTransport } from "@sdxc/mail/print";
15
+ import { trace } from "@sdxc/trace-context/middleware/cli";
16
+
17
+ import commandset from "~/app/commands";
18
+ import { FALLBACK_LANGUAGE, RESOURCES, SUPPORTED_LANGUAGES } from "~/app/initializers/i18n";
19
+ import { openLocalDatabase } from "~/app/initializers/local-database";
20
+ import { logger } from "~/app/initializers/logger";
21
+ import registry from "~/app/models";
22
+ import { MAIL_FROM } from "~/config/constants";
23
+
24
+ import manifest from "../../package.json" with { type: "json" };
25
+
26
+ /**
27
+ * The app's logger with each command's record written to stderr as one JSON line, so stdout
28
+ * holds only what the command prints and can be piped.
29
+ */
30
+ const COMMAND_LOGGER = createLogger({
31
+ ...logger.options,
32
+ sink: (record) => process.stderr.write(`${JSON.stringify(record)}\n`),
33
+ });
34
+
35
+ /**
36
+ * The command line `bootstrap/cli.ts` runs and a command's test invokes. A command reads the
37
+ * local database as `ctx.db` and its models as `ctx.models`, prints the email it sends to stderr
38
+ * through `ctx.email`, and writes its output in the language `LANG` names through `ctx.intl`.
39
+ */
40
+ export const cli = createCLI({
41
+ name: manifest.name,
42
+ commands: commandset,
43
+ middleware: [
44
+ log(COMMAND_LOGGER),
45
+ trace(),
46
+ database(openLocalDatabase),
47
+ models(registry, (ctx) => ({ db: ctx.require(Database) })),
48
+ mail({
49
+ transport: (ctx) => new PrintTransport({ write: (text) => ctx.stderr(text) }),
50
+ from: MAIL_FROM,
51
+ }),
52
+ i18n({
53
+ resources: RESOURCES,
54
+ supportedLanguages: SUPPORTED_LANGUAGES,
55
+ fallbackLanguage: FALLBACK_LANGUAGE,
56
+ }),
57
+ ],
58
+ });
59
+
60
+ cli.map(
61
+ commandset.about,
62
+ lazy(() => import("~/app/commands/about")),
63
+ );
64
+ cli.map(
65
+ commandset.db.console,
66
+ lazy(() => import("~/app/commands/db/console")),
67
+ );
68
+ cli.map(
69
+ commandset.db.migrate,
70
+ lazy(() => import("~/app/commands/db/migrate")),
71
+ );
72
+ cli.map(
73
+ commandset.typegen,
74
+ lazy(() => import("~/app/commands/typegen")),
75
+ );
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The database the router and the job dispatcher publish as `ctx.db`, run against the
3
+ * in-memory D1 stand-in tests receive as the `DB` binding.
4
+ */
5
+
6
+ import { describe, expect, test } from "vitest";
7
+
8
+ import { openDatabase } from "~/app/initializers/database";
9
+
10
+ describe("openDatabase", () => {
11
+ test("runs SQL through the DB binding", async () => {
12
+ let db = openDatabase();
13
+
14
+ let result = await db.exec("SELECT 41 + 1 AS answer");
15
+
16
+ expect(result.rows).toEqual([{ answer: 42 }]);
17
+ });
18
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The app's database: `remix/data-table` over the `DB` D1 binding. The router and the job
3
+ * dispatcher publish it as `ctx.db`, so a controller, a job and a model bound with
4
+ * `Model.bind({ db: ctx.db })` all read and write the same tables.
5
+ */
6
+
7
+ import { createD1DatabaseAdapter } from "@sdxc/data-table-d1";
8
+ import { env } from "cloudflare:workers";
9
+ import { Database } from "remix/data-table";
10
+
11
+ /**
12
+ * Opens the database over the `DB` binding. Timestamps are written as epoch milliseconds,
13
+ * since `sdxc make model` types `created_at` and `updated_at` as integer columns and D1 binds
14
+ * no `Date`.
15
+ */
16
+ export function openDatabase(): Database {
17
+ return new Database(createD1DatabaseAdapter(env.DB), { now: () => Date.now() });
18
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The job dispatcher: every background job is enqueued through it and runs inside it, over
3
+ * the Cloudflare queue the Worker consumes. `sdxc make job` maps each new handler here, as a
4
+ * loader, so a module is imported only once a message for its job arrives.
5
+ */
6
+
7
+ import type { JobDispatcherContext } from "@sdxc/jobs";
8
+
9
+ import { models } from "@sdxc/data-model/jobs";
10
+ import { Database } from "@sdxc/database-middleware";
11
+ import { database } from "@sdxc/database-middleware/jobs";
12
+ import { createJobDispatcher } from "@sdxc/jobs";
13
+ import * as cloudflare from "@sdxc/jobs/cloudflare";
14
+ import { env } from "cloudflare:workers";
15
+
16
+ import { openDatabase } from "~/app/initializers/database";
17
+ import { logger } from "~/app/initializers/logger";
18
+ import registry from "~/app/models";
19
+
20
+ /** The dispatcher `bootstrap/worker.ts` hands queue batches and cron triggers to. */
21
+ export const dispatcher = createJobDispatcher({
22
+ logger,
23
+ queue: cloudflare.queue(() => env.QUEUE),
24
+ middleware: [
25
+ database(openDatabase),
26
+ models(registry, (ctx) => ({ db: ctx.require(Database) })),
27
+ ] as const,
28
+ timeout: "5 minutes",
29
+ });
30
+
31
+ declare module "@sdxc/jobs" {
32
+ /** Types every job handler's `ctx` with what the dispatcher's middleware publishes: `ctx.db` and `ctx.models`. */
33
+ interface JobTypes {
34
+ context: JobDispatcherContext<typeof dispatcher>;
35
+ }
36
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The honeypot every page form carries: a hidden trap field a person never fills, and a token
3
+ * signing when the form was rendered. The page middleware refuses a submission that fails
4
+ * either, so a form needs only to render the fields.
5
+ */
6
+
7
+ import { Honeypot } from "@sdxc/honeypot";
8
+ import { env } from "cloudflare:workers";
9
+
10
+ /** Signs with `HONEYPOT_SECRET`; set it with `cf workers secrets update` before deploying. */
11
+ export const honeypot = new Honeypot({ secret: env.HONEYPOT_SECRET });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The translator a job or an email uses, which reads the same bundles a page does.
3
+ */
4
+
5
+ import { describe, expect, test } from "vitest";
6
+
7
+ import { translate } from "~/app/initializers/i18n";
8
+
9
+ describe("translate", () => {
10
+ test("binds the fallback language for an unsupported one", () => {
11
+ let { locale, t } = translate("fr");
12
+
13
+ expect(locale).toBe("en");
14
+ expect(t("notFound.title")).toBe("Not found");
15
+ });
16
+ });
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The app's languages and message bundles, in one place so the page middleware, a job and an
3
+ * email reach the same copy. Add a language by adding its bundle under `resources/locales/` and
4
+ * listing it in {@link SUPPORTED_LANGUAGES}.
5
+ */
6
+
7
+ import { createTranslator } from "@sdxc/i18n";
8
+
9
+ import en from "~/resources/locales/en.json";
10
+
11
+ /** The languages a page can render in; a request asking for any other gets the fallback. */
12
+ export const SUPPORTED_LANGUAGES = ["en"];
13
+
14
+ /** The language that answers a request asking for none the app speaks, and any key a bundle lacks. */
15
+ export const FALLBACK_LANGUAGE = "en";
16
+
17
+ /** The message bundles, one per supported language, as MessageFormat 2 strings. */
18
+ export const RESOURCES = { en };
19
+
20
+ /**
21
+ * Translates outside a request, such as in a job or an email, into the recipient's language:
22
+ * `translate(user.language).t("home.title")`. An unsupported language binds the fallback.
23
+ */
24
+ export const translate = createTranslator({
25
+ resources: RESOURCES,
26
+ supportedLanguages: SUPPORTED_LANGUAGES,
27
+ fallbackLanguage: FALLBACK_LANGUAGE,
28
+ });
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The local database a command reads as `ctx.db`, opened in memory so the test leaves the
3
+ * project's own copy untouched.
4
+ */
5
+
6
+ import { createModel } from "@sdxc/data-model";
7
+ import { unwrap } from "@sdxc/result";
8
+ import { column as c, table } from "remix/data-table";
9
+ import { describe, expect, test } from "vitest";
10
+
11
+ import { openLocalDatabase } from "~/app/initializers/local-database";
12
+
13
+ /** A table with the timestamp columns `sdxc make model` writes. */
14
+ const POSTS = table({
15
+ name: "posts",
16
+ columns: { id: c.text().primaryKey(), created_at: c.integer(), updated_at: c.integer() },
17
+ timestamps: true,
18
+ });
19
+
20
+ describe("openLocalDatabase", () => {
21
+ test("writes timestamps as epoch milliseconds, as the Worker's database does", async () => {
22
+ let db = openLocalDatabase(":memory:");
23
+ await db.exec(
24
+ "CREATE TABLE posts (id TEXT PRIMARY KEY, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL)",
25
+ );
26
+
27
+ let posts = createModel(POSTS, {}).bind({ db });
28
+
29
+ let row = unwrap(await posts.create({ id: "p1" }));
30
+
31
+ expect(typeof row.created_at).toBe("number");
32
+ });
33
+ });
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The local copy of the app's D1 database, opened as the SQLite file `bun dev` keeps it in, so a
3
+ * command reads and writes the rows the dev server serves. Miniflare names that file after the
4
+ * database id in `cloudflare.config.ts`, so it is found without starting the Worker runtime.
5
+ */
6
+
7
+ import { createHmac, createHash } from "node:crypto";
8
+ import { mkdirSync } from "node:fs";
9
+ import { dirname, join } from "node:path";
10
+
11
+ import type { Database } from "remix/data-table";
12
+
13
+ import { createSqliteDatabase } from "remix/data-table/sqlite";
14
+
15
+ import config from "../../cloudflare.config";
16
+ import manifest from "../../package.json" with { type: "json" };
17
+
18
+ /** The Durable Object namespace Miniflare stores every local D1 database under. */
19
+ const NAMESPACE = "miniflare-D1DatabaseObject";
20
+
21
+ /**
22
+ * Miniflare's object id for `name`: an HMAC-SHA256 of the name and an HMAC of that, both keyed by
23
+ * the namespace's SHA-256 and cut to 16 bytes, so one database id always maps to one file.
24
+ */
25
+ function objectId(namespace: string, name: string): string {
26
+ let key = createHash("sha256").update(namespace).digest();
27
+ let id = createHmac("sha256", key).update(name).digest().subarray(0, 16);
28
+ let mac = createHmac("sha256", key).update(id).digest().subarray(0, 16);
29
+ return Buffer.concat([id, mac]).toString("hex");
30
+ }
31
+
32
+ /** The project's root, which every path a command reads is resolved against. */
33
+ export const PROJECT_ROOT = join(import.meta.dirname, "../..");
34
+
35
+ /** The `DB` binding's id, as `cloudflare.config.ts` declares it, or its name until it has one. */
36
+ export const DATABASE_ID = config.worker.env.DB.id ?? config.worker.env.DB.name ?? manifest.name;
37
+
38
+ /** The SQLite file `bun dev` and `sdxc run db migrate` use for the `DB` binding. */
39
+ export const LOCAL_DATABASE_FILE = join(
40
+ PROJECT_ROOT,
41
+ ".cloudflare/state/v3/d1",
42
+ NAMESPACE,
43
+ `${objectId(NAMESPACE, DATABASE_ID)}.sqlite`,
44
+ );
45
+
46
+ /**
47
+ * Opens the local database, creating the file where `bun dev` will find it when none exists yet.
48
+ * Timestamps are written as epoch milliseconds, as the Worker's database writes them.
49
+ */
50
+ export function openLocalDatabase(filename = LOCAL_DATABASE_FILE): Database {
51
+ if (filename !== ":memory:") mkdirSync(dirname(filename), { recursive: true });
52
+ return createSqliteDatabase({ filename }, { now: () => Date.now() });
53
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The logger every request and job writes through. The router and the job dispatcher both
3
+ * attach it, so their records carry the same `service` and one query finds both halves of
4
+ * the work a request started.
5
+ */
6
+
7
+ import { createLogger } from "@sdxc/logger";
8
+
9
+ /** The Worker's one logger; read the invocation's log through `ctx.log`. */
10
+ export const logger = createLogger({ service: "worker" });
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The app's mail transport, delivering into the in-memory `send_email` stand-in tests receive
3
+ * as the `EMAIL` 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 { transport } from "~/app/initializers/mail";
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
+ });
@@ -0,0 +1,11 @@
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()`.
5
+ */
6
+
7
+ import { CloudflareTransport } from "@sdxc/mail/cloudflare";
8
+ import { env } from "cloudflare:workers";
9
+
10
+ /** Sends through the `EMAIL` binding, from an address on a domain Email Routing verified. */
11
+ export const transport = new CloudflareTransport(env.EMAIL);
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The app's MCP server. `app/mcp/tools/` and `app/mcp/resources/` declare what it offers and
3
+ * answer it, one file per handler; `sdxc make mcp` maps each new handler here through
4
+ * `lazy()`, so a request imports only the handlers it reaches.
5
+ */
6
+
7
+ import { createHandler } from "@sdxc/mcp";
8
+
9
+ /** The server `app/http/controllers/mcp.ts` forwards each `/mcp` request to. */
10
+ const mcp = createHandler({
11
+ name: "app",
12
+ version: "1.0.0",
13
+ cacheScope: "public",
14
+ });
15
+
16
+ export default mcp;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * The API's OpenAPI document. Add each operation `routes/api.ts` declares, so
3
+ * `/api/openapi.json` lists every documented endpoint.
4
+ */
5
+
6
+ import { createDocument } from "@sdxc/openapi";
7
+
8
+ import problems from "~/config/problems";
9
+ import { HEALTH } from "~/routes/api";
10
+
11
+ /**
12
+ * Starts the document for the origin the API is served from, which OpenAPI requires as an
13
+ * absolute server URL; `build()` assembles it, failing on a contract it cannot express.
14
+ */
15
+ export function createApiDocument(origin: string) {
16
+ return createDocument({
17
+ info: { title: "App API", version: "1.0.0" },
18
+ servers: [{ url: origin }],
19
+ problems,
20
+ }).add(HEALTH);
21
+ }