create-efforge 0.0.0-stage → 0.1.0

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 (237) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +140 -2
  3. package/dist/addon.js +115 -0
  4. package/dist/bin.js +6 -0
  5. package/dist/cli.js +9 -0
  6. package/dist/commands/add.js +440 -0
  7. package/dist/commands/create.js +186 -0
  8. package/dist/diff.js +72 -0
  9. package/dist/exec.js +20 -0
  10. package/dist/generate.js +181 -0
  11. package/dist/manifest.js +14 -0
  12. package/dist/projectName.js +41 -0
  13. package/dist/prompts/flow.js +111 -0
  14. package/dist/prompts/navigable.js +97 -0
  15. package/dist/routeTree.js +7 -0
  16. package/dist/rules.js +24 -0
  17. package/dist/slots.js +96 -0
  18. package/dist/stack.js +68 -0
  19. package/dist/transforms/addAlchemyResource.js +71 -0
  20. package/dist/transforms/addArrayItem.js +31 -0
  21. package/dist/transforms/addAuthPlugin.js +77 -0
  22. package/dist/transforms/addDevVar.js +18 -0
  23. package/dist/transforms/addExport.js +46 -0
  24. package/dist/transforms/addImport.js +11 -0
  25. package/dist/transforms/addLayer.js +31 -0
  26. package/dist/transforms/addServerFnMiddleware.js +62 -0
  27. package/dist/transforms/addWorkflowSecret.js +43 -0
  28. package/dist/transforms/addWranglerBinding.js +61 -0
  29. package/dist/transforms/appendAgentsSection.js +26 -0
  30. package/dist/transforms/errors.js +7 -0
  31. package/dist/transforms/index.js +40 -0
  32. package/dist/transforms/setWranglerValue.js +40 -0
  33. package/dist/transforms/util.js +219 -0
  34. package/dist/tree.js +1 -0
  35. package/dist/versions.js +73 -0
  36. package/dist/write.js +26 -0
  37. package/package.json +63 -4
  38. package/templates/ai/effect-ai-byok/addon.json +619 -0
  39. package/templates/ai/effect-ai-byok/files/core/llm-crypto.test.ts +47 -0
  40. package/templates/ai/effect-ai-byok/files/core/llm-crypto.ts +76 -0
  41. package/templates/ai/effect-ai-byok/files/core/llm-store.ts +21 -0
  42. package/templates/ai/effect-ai-byok/files/core/llm.ts +32 -0
  43. package/templates/ai/effect-ai-byok/files/drizzle/d1/20261018161056_llm_credential/migration.sql +9 -0
  44. package/templates/ai/effect-ai-byok/files/drizzle/d1/20261018161056_llm_credential/snapshot.json +944 -0
  45. package/templates/ai/effect-ai-byok/files/drizzle/pg/20261018161056_llm_credential/migration.sql +10 -0
  46. package/templates/ai/effect-ai-byok/files/drizzle/pg/20261018161056_llm_credential/snapshot.json +1232 -0
  47. package/templates/ai/effect-ai-byok/files/routes/ai.tsx +193 -0
  48. package/templates/ai/effect-ai-byok/files/schema/d1.ts +16 -0
  49. package/templates/ai/effect-ai-byok/files/schema/pg.ts +14 -0
  50. package/templates/ai/effect-ai-byok/files/server/ai-settings.ts +129 -0
  51. package/templates/ai/effect-ai-byok/files/server/ai.ts +25 -0
  52. package/templates/ai/effect-ai-byok/files/server/llm-env.ts +48 -0
  53. package/templates/ai/effect-ai-byok/files/server/llm-orgs.ts +146 -0
  54. package/templates/ai/effect-ai-byok/files/server/llm-provider.ts +56 -0
  55. package/templates/ai/effect-ai-byok/files/server/llm-store-drizzle.ts +48 -0
  56. package/templates/ai/effect-ai-byok/files/server/llm-store-sql-d1.ts +41 -0
  57. package/templates/ai/effect-ai-byok/files/server/llm-store-sql-pg.ts +41 -0
  58. package/templates/ai/effect-ai-byok/files/sql/d1.sql +8 -0
  59. package/templates/ai/effect-ai-byok/files/sql/pg.sql +8 -0
  60. package/templates/auth/better-auth/addon.json +424 -0
  61. package/templates/auth/better-auth/files/core/Auth.ts +18 -0
  62. package/templates/auth/better-auth/files/drizzle/d1/20261018161054_auth/migration.sql +51 -0
  63. package/templates/auth/better-auth/files/drizzle/d1/20261018161054_auth/snapshot.json +522 -0
  64. package/templates/auth/better-auth/files/drizzle/pg/20261018161053_auth/migration.sql +51 -0
  65. package/templates/auth/better-auth/files/drizzle/pg/20261018161053_auth/snapshot.json +680 -0
  66. package/templates/auth/better-auth/files/lib/auth-client.ts +4 -0
  67. package/templates/auth/better-auth/files/routes/app.tsx +54 -0
  68. package/templates/auth/better-auth/files/schema/d1/auth-schema.ts +86 -0
  69. package/templates/auth/better-auth/files/schema/pg/auth-schema.ts +73 -0
  70. package/templates/auth/better-auth/files/server/auth-live.ts +47 -0
  71. package/templates/auth/better-auth/files/server/session.ts +14 -0
  72. package/templates/auth/better-auth/files/shared/components/auth-card.tsx +32 -0
  73. package/templates/auth/better-auth/files/shared/components/user-menu.tsx +44 -0
  74. package/templates/auth/better-auth/files/shared/drizzle/d1/drizzle.config.ts +7 -0
  75. package/templates/auth/better-auth/files/shared/drizzle/pg/drizzle.config.ts +10 -0
  76. package/templates/auth/better-auth/files/shared/lib/form.ts +5 -0
  77. package/templates/auth/better-auth/files/shared/lib/redirect.ts +6 -0
  78. package/templates/auth/better-auth/files/shared/routes/app/index.tsx +54 -0
  79. package/templates/auth/better-auth/files/shared/routes/sign-in.tsx +85 -0
  80. package/templates/auth/better-auth/files/shared/routes/sign-up.tsx +91 -0
  81. package/templates/auth/better-auth/files/shared/server/auth-route.ts +15 -0
  82. package/templates/auth/better-auth/files/shared/server/auth.ts +22 -0
  83. package/templates/auth/better-auth/files/shared/server/db/d1-drizzle.ts +25 -0
  84. package/templates/auth/better-auth/files/shared/server/db/d1-effect-sql.ts +17 -0
  85. package/templates/auth/better-auth/files/shared/server/db/pg-drizzle.ts +32 -0
  86. package/templates/auth/better-auth/files/shared/server/db/pg-effect-sql.ts +29 -0
  87. package/templates/auth/better-auth/files/shared/server/require-session.ts +20 -0
  88. package/templates/auth/better-auth/files/sql/d1/0002_auth.sql +13 -0
  89. package/templates/auth/better-auth/files/sql/pg/0002_auth.sql +13 -0
  90. package/templates/auth/better-auth-orgs/addon.json +463 -0
  91. package/templates/auth/better-auth-orgs/files/components/org-switcher.tsx +65 -0
  92. package/templates/auth/better-auth-orgs/files/core/Auth.ts +20 -0
  93. package/templates/auth/better-auth-orgs/files/drizzle/d1/20261018161055_auth/migration.sql +88 -0
  94. package/templates/auth/better-auth-orgs/files/drizzle/d1/20261018161055_auth/snapshot.json +862 -0
  95. package/templates/auth/better-auth-orgs/files/drizzle/pg/20261018161055_auth/migration.sql +88 -0
  96. package/templates/auth/better-auth-orgs/files/drizzle/pg/20261018161055_auth/snapshot.json +1127 -0
  97. package/templates/auth/better-auth-orgs/files/lib/auth-client.ts +5 -0
  98. package/templates/auth/better-auth-orgs/files/routes/accept-invite.$id.tsx +68 -0
  99. package/templates/auth/better-auth-orgs/files/routes/app/settings/index.tsx +27 -0
  100. package/templates/auth/better-auth-orgs/files/routes/app/settings/members.tsx +198 -0
  101. package/templates/auth/better-auth-orgs/files/routes/app/settings.tsx +30 -0
  102. package/templates/auth/better-auth-orgs/files/routes/app.tsx +58 -0
  103. package/templates/auth/better-auth-orgs/files/routes/onboarding.tsx +66 -0
  104. package/templates/auth/better-auth-orgs/files/schema/d1/auth-schema.ts +139 -0
  105. package/templates/auth/better-auth-orgs/files/schema/pg/auth-schema.ts +124 -0
  106. package/templates/auth/better-auth-orgs/files/server/auth-live.ts +52 -0
  107. package/templates/auth/better-auth-orgs/files/server/session.ts +44 -0
  108. package/templates/auth/better-auth-orgs/files/sql/d1/0002_auth.sql +27 -0
  109. package/templates/auth/better-auth-orgs/files/sql/pg/0002_auth.sql +27 -0
  110. package/templates/base/addon.json +212 -0
  111. package/templates/base/files/AGENTS.md +70 -0
  112. package/templates/base/files/CLAUDE.md +1 -0
  113. package/templates/base/files/README.md +28 -0
  114. package/templates/base/files/core/package.json +13 -0
  115. package/templates/base/files/core/src/index.ts +2 -0
  116. package/templates/base/files/core/test/core.test.ts +4 -0
  117. package/templates/base/files/core/tsconfig.json +6 -0
  118. package/templates/base/files/gitignore +11 -0
  119. package/templates/base/files/pnpm-workspace.yaml +11 -0
  120. package/templates/base/files/root/package.json +16 -0
  121. package/templates/base/files/root/tsconfig.base.json +19 -0
  122. package/templates/base/files/root/turbo.json +9 -0
  123. package/templates/base/files/root/vite.config.ts +61 -0
  124. package/templates/base/files/web/components.json +21 -0
  125. package/templates/base/files/web/package.json +17 -0
  126. package/templates/base/files/web/src/components/theme-provider.tsx +57 -0
  127. package/templates/base/files/web/src/components/theme-toggle.tsx +34 -0
  128. package/templates/base/files/web/src/components/ui/avatar.tsx +93 -0
  129. package/templates/base/files/web/src/components/ui/button.tsx +61 -0
  130. package/templates/base/files/web/src/components/ui/card.tsx +74 -0
  131. package/templates/base/files/web/src/components/ui/dialog.tsx +141 -0
  132. package/templates/base/files/web/src/components/ui/dropdown-menu.tsx +225 -0
  133. package/templates/base/files/web/src/components/ui/input.tsx +20 -0
  134. package/templates/base/files/web/src/components/ui/label.tsx +18 -0
  135. package/templates/base/files/web/src/components/ui/sonner.tsx +45 -0
  136. package/templates/base/files/web/src/components/widgets.tsx +4 -0
  137. package/templates/base/files/web/src/lib/utils.ts +6 -0
  138. package/templates/base/files/web/src/router.tsx +12 -0
  139. package/templates/base/files/web/src/routes/__root.tsx +42 -0
  140. package/templates/base/files/web/src/routes/app/index.tsx +27 -0
  141. package/templates/base/files/web/src/routes/app/settings.tsx +29 -0
  142. package/templates/base/files/web/src/routes/app.tsx +47 -0
  143. package/templates/base/files/web/src/routes/index.tsx +51 -0
  144. package/templates/base/files/web/src/server/greeting.ts +12 -0
  145. package/templates/base/files/web/src/server/layers.ts +10 -0
  146. package/templates/base/files/web/src/server/runtime.ts +5 -0
  147. package/templates/base/files/web/src/styles.css +81 -0
  148. package/templates/base/files/web/tsconfig.json +11 -0
  149. package/templates/base/files/web/vite.config.ts +9 -0
  150. package/templates/database/d1/addon.json +118 -0
  151. package/templates/database/d1/files/package.json +12 -0
  152. package/templates/database/d1/files/tsconfig.json +5 -0
  153. package/templates/database/postgres/addon.json +142 -0
  154. package/templates/database/postgres/files/docker-compose.yml +21 -0
  155. package/templates/database/postgres/files/package.json +12 -0
  156. package/templates/database/postgres/files/runtime.ts +12 -0
  157. package/templates/database/postgres/files/tsconfig.json +5 -0
  158. package/templates/extras/browser/addon.json +173 -0
  159. package/templates/extras/browser/files/BrowserContainer.ts +14 -0
  160. package/templates/extras/browser/files/app/Dockerfile +15 -0
  161. package/templates/extras/browser/files/app/dockerignore +1 -0
  162. package/templates/extras/browser/files/app/package-lock +605 -0
  163. package/templates/extras/browser/files/app/package.json +9 -0
  164. package/templates/extras/browser/files/app/src/server.ts +78 -0
  165. package/templates/extras/browser/files/app/src/url-policy.ts +115 -0
  166. package/templates/extras/browser/files/app/tsconfig.json +6 -0
  167. package/templates/extras/browser/files/browser.ts +34 -0
  168. package/templates/extras/browser/files/core-browser.ts +21 -0
  169. package/templates/extras/browser/files/run-browser.ts +25 -0
  170. package/templates/extras/email/addon.json +182 -0
  171. package/templates/extras/email/files/core-mailer.ts +28 -0
  172. package/templates/extras/email/files/invitation-email.ts +38 -0
  173. package/templates/extras/email/files/mailer.ts +58 -0
  174. package/templates/extras/realtime/addon.json +135 -0
  175. package/templates/extras/realtime/files/Room.ts +46 -0
  176. package/templates/extras/realtime/files/core-realtime.ts +19 -0
  177. package/templates/extras/realtime/files/realtime-demo.tsx +40 -0
  178. package/templates/extras/realtime/files/realtime-limits.test.ts +32 -0
  179. package/templates/extras/realtime/files/realtime-limits.ts +35 -0
  180. package/templates/extras/realtime/files/realtime-route.ts +39 -0
  181. package/templates/extras/realtime/files/realtime.ts +39 -0
  182. package/templates/extras/realtime/files/use-room.ts +67 -0
  183. package/templates/extras/storage/addon.json +132 -0
  184. package/templates/extras/storage/files/core-storage.ts +64 -0
  185. package/templates/extras/storage/files/storage-route.ts +50 -0
  186. package/templates/extras/storage/files/storage-security.test.ts +71 -0
  187. package/templates/extras/storage/files/storage-security.ts +92 -0
  188. package/templates/extras/storage/files/storage.ts +84 -0
  189. package/templates/extras/workflows/addon.json +95 -0
  190. package/templates/extras/workflows/files/Example.ts +42 -0
  191. package/templates/extras/workflows/files/core-jobs.ts +45 -0
  192. package/templates/extras/workflows/files/example-job.ts +36 -0
  193. package/templates/extras/workflows/files/jobs.ts +34 -0
  194. package/templates/infra/alchemy/addon.json +37 -0
  195. package/templates/infra/alchemy/files/README.md +27 -0
  196. package/templates/infra/alchemy/files/alchemy.run.ts +36 -0
  197. package/templates/infra/alchemy/files/github/deploy.yml +63 -0
  198. package/templates/infra/alchemy/files/tsconfig.json +5 -0
  199. package/templates/infra/alchemy/files/vite.config.ts +30 -0
  200. package/templates/orm/drizzle/addon.json +188 -0
  201. package/templates/orm/drizzle/files/d1/Database.ts +7 -0
  202. package/templates/orm/drizzle/files/d1/drizzle/20261018154417_init/migration.sql +10 -0
  203. package/templates/orm/drizzle/files/d1/drizzle/20261018154417_init/snapshot.json +60 -0
  204. package/templates/orm/drizzle/files/d1/drizzle.config.ts +7 -0
  205. package/templates/orm/drizzle/files/d1/layer.ts +13 -0
  206. package/templates/orm/drizzle/files/d1/migrate.ts +37 -0
  207. package/templates/orm/drizzle/files/d1/schema.ts +15 -0
  208. package/templates/orm/drizzle/files/pg/Database.ts +7 -0
  209. package/templates/orm/drizzle/files/pg/drizzle/20261018154416_init/migration.sql +10 -0
  210. package/templates/orm/drizzle/files/pg/drizzle/20261018154416_init/snapshot.json +75 -0
  211. package/templates/orm/drizzle/files/pg/drizzle.config.ts +10 -0
  212. package/templates/orm/drizzle/files/pg/layer.ts +16 -0
  213. package/templates/orm/drizzle/files/pg/migrate.ts +24 -0
  214. package/templates/orm/drizzle/files/pg/schema.ts +11 -0
  215. package/templates/orm/drizzle/files/shared/index.ts +2 -0
  216. package/templates/orm/drizzle/files/shared/notes.ts +15 -0
  217. package/templates/orm/drizzle/files/shared/page.tsx +50 -0
  218. package/templates/orm/effect-sql/addon.json +124 -0
  219. package/templates/orm/effect-sql/files/d1/layer.ts +13 -0
  220. package/templates/orm/effect-sql/files/d1/migrate.ts +20 -0
  221. package/templates/orm/effect-sql/files/d1/schema.ts +7 -0
  222. package/templates/orm/effect-sql/files/d1-sql/0001_init.sql +10 -0
  223. package/templates/orm/effect-sql/files/pg/layer.ts +16 -0
  224. package/templates/orm/effect-sql/files/pg/migrate.ts +66 -0
  225. package/templates/orm/effect-sql/files/pg/schema.ts +7 -0
  226. package/templates/orm/effect-sql/files/pg-sql/0001_init.sql +10 -0
  227. package/templates/orm/effect-sql/files/shared/Database.ts +7 -0
  228. package/templates/orm/effect-sql/files/shared/index.ts +2 -0
  229. package/templates/orm/effect-sql/files/shared/notes.ts +18 -0
  230. package/templates/orm/effect-sql/files/shared/page.tsx +50 -0
  231. package/templates/runtime/cloudflare/addon.json +52 -0
  232. package/templates/runtime/cloudflare/files/cloudflare.ts +10 -0
  233. package/templates/runtime/cloudflare/files/routes.ts +5 -0
  234. package/templates/runtime/cloudflare/files/server.ts +22 -0
  235. package/templates/runtime/cloudflare/files/tsconfig.json +12 -0
  236. package/templates/runtime/cloudflare/files/vite.config.ts +20 -0
  237. package/templates/runtime/cloudflare/files/wrangler.jsonc +8 -0
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Pure helpers for signed download URLs. They use only Web Crypto, so they run in Workers, Node
3
+ * and tests. The signature is HMAC-SHA256 over `<key>\n<expires>`, hex encoded.
4
+ */
5
+
6
+ /** A shorter secret is rejected when the Storage layer is built. */
7
+ export const MIN_SECRET_LENGTH = 32
8
+
9
+ const encoder = new TextEncoder()
10
+
11
+ const importKey = (secret: string, usage: "sign" | "verify") =>
12
+ crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [
13
+ usage,
14
+ ])
15
+
16
+ const message = (key: string, expires: string) => encoder.encode(`${key}\n${expires}`)
17
+
18
+ const toHex = (buffer: ArrayBuffer) =>
19
+ [...new Uint8Array(buffer)].map((b) => b.toString(16).padStart(2, "0")).join("")
20
+
21
+ const fromHex = (hex: string): Uint8Array<ArrayBuffer> | undefined => {
22
+ if (!/^(?:[0-9a-f]{2})+$/i.test(hex)) return undefined
23
+ return Uint8Array.from(hex.match(/../g) ?? [], (byte) => Number.parseInt(byte, 16))
24
+ }
25
+
26
+ /** Unix seconds as plain digits, no sign, spaces or exponent. */
27
+ export const parseExpiry = (expires: string): number | undefined => {
28
+ if (!/^\d+$/.test(expires)) return undefined
29
+ const seconds = Number(expires)
30
+ return Number.isSafeInteger(seconds) ? seconds : undefined
31
+ }
32
+
33
+ /**
34
+ * Checks the shape of the `expires` and `signature` query parameters. `undefined` means the
35
+ * request is malformed (answer 400), which is different from a well-formed but wrong signature (403).
36
+ */
37
+ export const parseSignedParams = (
38
+ expires: string | null,
39
+ signature: string | null,
40
+ ): { readonly expires: string; readonly signature: string } | undefined =>
41
+ expires !== null &&
42
+ signature !== null &&
43
+ parseExpiry(expires) !== undefined &&
44
+ /^[0-9a-f]{64}$/i.test(signature)
45
+ ? { expires, signature }
46
+ : undefined
47
+
48
+ /** Hex HMAC for `key` valid until `expires` (unix seconds). */
49
+ export const signPayload = async (secret: string, key: string, expires: string) =>
50
+ toHex(await crypto.subtle.sign("HMAC", await importKey(secret, "sign"), message(key, expires)))
51
+
52
+ /** Constant-time check (`crypto.subtle.verify`); false for expired, malformed or wrong signatures. */
53
+ export const verifyPayload = async (
54
+ secret: string,
55
+ key: string,
56
+ expires: string,
57
+ signature: string,
58
+ now: number = Date.now(),
59
+ ): Promise<boolean> => {
60
+ const seconds = parseExpiry(expires)
61
+ const bytes = fromHex(signature)
62
+ if (seconds === undefined || bytes === undefined || seconds * 1000 < now) return false
63
+ return crypto.subtle.verify(
64
+ "HMAC",
65
+ await importKey(secret, "verify"),
66
+ bytes,
67
+ message(key, expires),
68
+ )
69
+ }
70
+
71
+ /** Only these types render in the browser; everything else (HTML, SVG, ...) downloads. */
72
+ const inlineTypes = new Set(["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif"])
73
+
74
+ /**
75
+ * Response headers for a served object. Stored content is untrusted: never sniffed, never run as
76
+ * a page, and shown inline only when it is a raster image.
77
+ */
78
+ export const downloadHeaders = (
79
+ key: string,
80
+ contentType: string | undefined,
81
+ ): Record<string, string> => {
82
+ const type = (contentType ?? "application/octet-stream").split(";")[0]?.trim().toLowerCase()
83
+ const inline = type !== undefined && inlineTypes.has(type)
84
+ const name = (key.split("/").at(-1) ?? "file").replaceAll(/[^\w.-]/g, "_") || "file"
85
+ return {
86
+ "content-type": inline ? type : (contentType ?? "application/octet-stream"),
87
+ "content-disposition": inline ? "inline" : `attachment; filename="${name}"`,
88
+ "x-content-type-options": "nosniff",
89
+ "content-security-policy": "default-src 'none'; sandbox",
90
+ "cache-control": "private, max-age=0",
91
+ }
92
+ }
@@ -0,0 +1,84 @@
1
+ import { Storage, StorageConfigError, StorageError } from "@repo/core"
2
+ import { MIN_SECRET_LENGTH, signPayload, verifyPayload } from "@repo/core/storage-security"
3
+ import { Config, ConfigProvider, Effect, Layer, Option, Redacted } from "effect"
4
+ import { R2, WorkerConfig } from "effect-cf"
5
+ import { WorkerEnvLive } from "./cloudflare"
6
+
7
+ class StorageBucket extends R2.Tag<StorageBucket>()("StorageBucket") {}
8
+
9
+ /** The signing secret, read once when the layer is built. Fails with a clear message if unusable. */
10
+ const loadSecret = (provider: ConfigProvider.ConfigProvider) =>
11
+ Config.Redacted("STORAGE_SIGNING_SECRET").pipe(
12
+ Effect.provideService(ConfigProvider.ConfigProvider, provider),
13
+ Effect.mapError(
14
+ () =>
15
+ new StorageConfigError({
16
+ message:
17
+ "STORAGE_SIGNING_SECRET is not set. Set it to a random string of at least 32 characters (wrangler secret put STORAGE_SIGNING_SECRET, or .dev.vars locally).",
18
+ }),
19
+ ),
20
+ Effect.map(Redacted.value),
21
+ Effect.filterOrFail(
22
+ (secret) => secret.length >= MIN_SECRET_LENGTH,
23
+ () =>
24
+ new StorageConfigError({
25
+ message: `STORAGE_SIGNING_SECRET is too short: use at least ${MIN_SECRET_LENGTH} characters.`,
26
+ }),
27
+ ),
28
+ )
29
+
30
+ const fail = (operation: string) => (cause: unknown) => new StorageError({ operation, cause })
31
+
32
+ const make = Effect.gen(function* () {
33
+ const bucket = yield* StorageBucket
34
+ const secret = yield* loadSecret(yield* WorkerConfig.provider)
35
+ return Storage.of({
36
+ put: (key, body, options) =>
37
+ bucket
38
+ .put(
39
+ key,
40
+ body,
41
+ options?.contentType === undefined
42
+ ? {}
43
+ : { httpMetadata: { contentType: options.contentType } },
44
+ )
45
+ .pipe(
46
+ Effect.map((o) => ({
47
+ key: o.key,
48
+ size: o.size,
49
+ contentType: o.httpMetadata?.contentType,
50
+ })),
51
+ Effect.mapError(fail("put")),
52
+ ),
53
+ get: (key) =>
54
+ Effect.gen(function* () {
55
+ const found = yield* bucket.get(key)
56
+ if (Option.isNone(found)) return Option.none()
57
+ const object = found.value
58
+ return Option.some({
59
+ key,
60
+ size: object.size,
61
+ contentType: object.httpMetadata?.contentType,
62
+ body: object.body,
63
+ })
64
+ }).pipe(Effect.mapError(fail("get"))),
65
+ delete: (key) => bucket.delete(key).pipe(Effect.mapError(fail("delete"))),
66
+ signedUrl: (key, options) =>
67
+ Effect.gen(function* () {
68
+ const expires = String(Math.floor(Date.now() / 1000) + (options?.expiresInSeconds ?? 3600))
69
+ const signature = yield* Effect.promise(() => signPayload(secret, key, expires))
70
+ return `/files/${encodeURIComponent(key)}?expires=${expires}&signature=${signature}`
71
+ }).pipe(Effect.mapError(fail("signedUrl"))),
72
+ verify: (key, expires, signature) =>
73
+ Effect.gen(function* () {
74
+ if (expires === null || signature === null) return false
75
+ return yield* Effect.promise(() => verifyPayload(secret, key, expires, signature))
76
+ }).pipe(Effect.mapError(fail("verify"))),
77
+ })
78
+ })
79
+
80
+ /** `Storage` on the `STORAGE` R2 bucket binding. */
81
+ export const StorageLive = Layer.effect(Storage, make).pipe(
82
+ Layer.provide(StorageBucket.layer({ binding: "STORAGE" })),
83
+ Layer.provide(WorkerEnvLive),
84
+ )
@@ -0,0 +1,95 @@
1
+ {
2
+ "id": "extras/workflows",
3
+ "title": "Workflows",
4
+ "description": "Durable background jobs: a Jobs service on Cloudflare Workflows, with an example workflow and server functions.",
5
+ "files": [
6
+ {
7
+ "path": "files/core-jobs.ts",
8
+ "target": "packages/core/src/jobs.ts"
9
+ },
10
+ {
11
+ "path": "files/Example.ts",
12
+ "target": "apps/web/src/workflows/Example.ts"
13
+ },
14
+ {
15
+ "path": "files/jobs.ts",
16
+ "target": "apps/web/src/server/jobs.ts"
17
+ },
18
+ {
19
+ "path": "files/example-job.ts",
20
+ "target": "apps/web/src/server/example-job.ts"
21
+ }
22
+ ],
23
+ "dependencies": {},
24
+ "devDependencies": {},
25
+ "envVars": [],
26
+ "requires": [],
27
+ "conflicts": [],
28
+ "transforms": [
29
+ {
30
+ "name": "addExport",
31
+ "args": {
32
+ "file": "packages/core/src/index.ts",
33
+ "export": "*",
34
+ "from": "./jobs"
35
+ }
36
+ },
37
+ {
38
+ "name": "addExport",
39
+ "args": {
40
+ "file": "apps/web/src/server.ts",
41
+ "export": "Example",
42
+ "from": "./workflows/Example"
43
+ }
44
+ },
45
+ {
46
+ "name": "addLayer",
47
+ "args": {
48
+ "file": "apps/web/src/server/layers.ts",
49
+ "import": "JobsLive",
50
+ "from": "./jobs",
51
+ "layer": "JobsLive"
52
+ }
53
+ },
54
+ {
55
+ "name": "addWranglerBinding",
56
+ "args": {
57
+ "key": "workflows",
58
+ "value": "{\"name\":\"example\",\"binding\":\"EXAMPLE_WORKFLOW\",\"class_name\":\"Example\"}"
59
+ },
60
+ "when": {
61
+ "infra": [
62
+ "none"
63
+ ]
64
+ }
65
+ },
66
+ {
67
+ "name": "addAlchemyResource",
68
+ "args": {
69
+ "name": "exampleWorkflow",
70
+ "code": "const exampleWorkflow = Cloudflare.Workflow(\"Example\", { className: \"Example\" })",
71
+ "binding": "EXAMPLE_WORKFLOW"
72
+ },
73
+ "when": {
74
+ "infra": [
75
+ "alchemy"
76
+ ]
77
+ }
78
+ },
79
+ {
80
+ "name": "addServerFnMiddleware",
81
+ "args": {
82
+ "file": "apps/web/src/server/example-job.ts",
83
+ "middleware": "requireSession",
84
+ "import": "requireSession",
85
+ "from": "./require-session"
86
+ },
87
+ "when": {
88
+ "auth": [
89
+ "better-auth",
90
+ "better-auth-orgs"
91
+ ]
92
+ }
93
+ }
94
+ ]
95
+ }
@@ -0,0 +1,42 @@
1
+ import { Data, Effect, Layer, Schema } from "effect"
2
+ import { Workflow } from "effect-cf"
3
+
4
+ /** A typed failure: the name is unusable, so retrying cannot help. */
5
+ export class InvalidName extends Data.TaggedError("InvalidName")<{ readonly input: string }> {}
6
+
7
+ export class ExampleWorkflow extends Workflow.Tag<ExampleWorkflow>()("ExampleWorkflow", {
8
+ payload: Schema.Struct({ name: Schema.String }),
9
+ result: Schema.Struct({ message: Schema.String }),
10
+ }) {}
11
+
12
+ /**
13
+ * A Cloudflare Workflow: each `Workflow.step` is durable, retried on failure, and not re-run once
14
+ * it has succeeded. Bound as EXAMPLE_WORKFLOW in wrangler.jsonc and exported from `src/server.ts`.
15
+ */
16
+ export class Example extends ExampleWorkflow.make(Layer.empty, {
17
+ run: ({ name }) =>
18
+ Effect.gen(function* () {
19
+ // Failing with WorkflowStepNonRetryableError ends the workflow at once; any other failure
20
+ // is retried up to `limit` times with exponential backoff (2s, 4s, 8s), then fails the run.
21
+ const greeting = yield* Workflow.step(
22
+ "greet",
23
+ name.trim() === ""
24
+ ? Effect.fail(
25
+ new Workflow.WorkflowStepNonRetryableError({
26
+ cause: new InvalidName({ input: name }),
27
+ }),
28
+ )
29
+ : Effect.succeed(`Hello, ${name.trim()}`),
30
+ {
31
+ retries: { limit: 3, delay: "2 seconds", backoff: "exponential" },
32
+ timeout: "30 seconds",
33
+ },
34
+ )
35
+ yield* Workflow.sleep("pause", "5 seconds")
36
+ const message = yield* Workflow.step(
37
+ "finish",
38
+ Effect.sync(() => `${greeting}! The workflow finished.`),
39
+ )
40
+ return { message }
41
+ }),
42
+ }) {}
@@ -0,0 +1,45 @@
1
+ import { Context, Data, type Effect } from "effect"
2
+
3
+ export class JobError extends Data.TaggedError("JobError")<{
4
+ readonly operation: string
5
+ readonly cause: unknown
6
+ }> {}
7
+
8
+ /** Payload per job name. Add an entry here when you add a workflow. */
9
+ export interface JobPayloads {
10
+ readonly example: { readonly name: string }
11
+ }
12
+
13
+ export type JobName = keyof JobPayloads
14
+
15
+ export interface JobRun {
16
+ readonly id: string
17
+ }
18
+
19
+ export interface JobStatus {
20
+ readonly status:
21
+ | "queued"
22
+ | "rollingBack"
23
+ | "running"
24
+ | "paused"
25
+ | "errored"
26
+ | "terminated"
27
+ | "complete"
28
+ | "waiting"
29
+ | "waitingForPause"
30
+ | "unknown"
31
+ readonly output: unknown
32
+ readonly error: string | undefined
33
+ }
34
+
35
+ /** Durable background jobs. The Cloudflare layer starts Cloudflare Workflows. */
36
+ export class Jobs extends Context.Service<
37
+ Jobs,
38
+ {
39
+ readonly start: <N extends JobName>(
40
+ job: N,
41
+ payload: JobPayloads[N],
42
+ ) => Effect.Effect<JobRun, JobError>
43
+ readonly status: (job: JobName, id: string) => Effect.Effect<JobStatus, JobError>
44
+ }
45
+ >()("@repo/core/Jobs") {}
@@ -0,0 +1,36 @@
1
+ import { Jobs } from "@repo/core"
2
+ import { createServerFn } from "@tanstack/react-start"
3
+ import { Effect, Schema } from "effect"
4
+ import { runtime } from "./runtime"
5
+
6
+ const StartInput = Schema.Struct({
7
+ name: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(100)),
8
+ })
9
+ const StatusInput = Schema.Struct({
10
+ id: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(100)),
11
+ })
12
+
13
+ /** Starts the example workflow; returns its instance id. */
14
+ export const startExampleJob = createServerFn({ method: "POST" })
15
+ .validator(Schema.toStandardSchemaV1(StartInput))
16
+ .handler(({ data }) =>
17
+ runtime.runPromise(
18
+ Effect.gen(function* () {
19
+ const jobs = yield* Jobs
20
+ return yield* jobs.start("example", { name: data.name })
21
+ }),
22
+ ),
23
+ )
24
+
25
+ export const getExampleJobStatus = createServerFn({ method: "GET" })
26
+ .validator(Schema.toStandardSchemaV1(StatusInput))
27
+ .handler(({ data }) =>
28
+ runtime.runPromise(
29
+ Effect.gen(function* () {
30
+ const jobs = yield* Jobs
31
+ const { status, output, error } = yield* jobs.status("example", data.id)
32
+ // server function results must be serializable, so the output travels as JSON text
33
+ return { status, error, outputJson: JSON.stringify(output ?? null) }
34
+ }),
35
+ ),
36
+ )
@@ -0,0 +1,34 @@
1
+ import { JobError, Jobs } from "@repo/core"
2
+ import { Effect, Layer, Option } from "effect"
3
+ import { ExampleWorkflow } from "../workflows/Example"
4
+ import { WorkerEnvLive } from "./cloudflare"
5
+
6
+ const fail = (operation: string) => (cause: unknown) => new JobError({ operation, cause })
7
+
8
+ const make = Effect.gen(function* () {
9
+ // one entry per workflow; keys match `JobPayloads` in @repo/core
10
+ const workflows = { example: yield* ExampleWorkflow }
11
+ return Jobs.of({
12
+ start: (job, payload) =>
13
+ workflows[job].create(payload).pipe(
14
+ Effect.map((instance) => ({ id: instance.id })),
15
+ Effect.mapError(fail("start")),
16
+ ),
17
+ status: (job, id) =>
18
+ workflows[job].get(id).pipe(
19
+ Effect.flatMap((instance) => instance.status),
20
+ Effect.map((s) => ({
21
+ status: s.status,
22
+ output: Option.getOrUndefined(s.output),
23
+ error: Option.getOrUndefined(Option.map(s.error, (e) => e.message)),
24
+ })),
25
+ Effect.mapError(fail("status")),
26
+ ),
27
+ })
28
+ })
29
+
30
+ /** `Jobs` on Cloudflare Workflows, through the EXAMPLE_WORKFLOW binding. */
31
+ export const JobsLive = Layer.effect(Jobs, make).pipe(
32
+ Layer.provide(ExampleWorkflow.layer({ binding: "EXAMPLE_WORKFLOW" })),
33
+ Layer.provide(WorkerEnvLive),
34
+ )
@@ -0,0 +1,37 @@
1
+ {
2
+ "id": "infra/alchemy",
3
+ "title": "Alchemy",
4
+ "description": "Infrastructure as Effects (Alchemy v2): infra/alchemy.run.ts declares the Worker and its bindings instead of wrangler.jsonc, with PR preview stages deployed from GitHub Actions.",
5
+ "files": [
6
+ { "path": "files/alchemy.run.ts", "target": "infra/alchemy.run.ts" },
7
+ { "path": "files/tsconfig.json", "target": "infra/tsconfig.json" },
8
+ { "path": "files/README.md", "target": "infra/README.md" },
9
+ { "path": "files/github/deploy.yml", "target": ".github/workflows/deploy.yml" },
10
+ { "path": "files/vite.config.ts", "target": "apps/web/vite.config.ts" }
11
+ ],
12
+ "dependencies": {},
13
+ "devDependencies": {
14
+ ".": ["alchemy", "effect", "@effect/platform-node", "@types/node", "typescript"]
15
+ },
16
+ "envVars": [
17
+ {
18
+ "name": "CLOUDFLARE_ACCOUNT_ID",
19
+ "example": "your-account-id",
20
+ "description": "Needed by `pnpm run deploy` (with CLOUDFLARE_API_TOKEN, or a login from `pnpm exec alchemy login`)."
21
+ }
22
+ ],
23
+ "requires": [],
24
+ "conflicts": [],
25
+ "transforms": [],
26
+ "scripts": {
27
+ ".": {
28
+ "dev": "alchemy dev infra/alchemy.run.ts",
29
+ "typecheck": "turbo run typecheck && tsc -p infra",
30
+ "deploy": "alchemy deploy infra/alchemy.run.ts",
31
+ "destroy": "alchemy destroy infra/alchemy.run.ts"
32
+ },
33
+ "apps/web": {
34
+ "deploy": "pnpm --workspace-root run deploy"
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,27 @@
1
+ # infra
2
+
3
+ `alchemy.run.ts` declares everything that runs on Cloudflare with [Alchemy v2](https://v2.alchemy.run)
4
+ ("Infrastructure as Effects"). There is no `wrangler.jsonc`: the Worker entry is
5
+ `apps/web/src/server.ts` and every binding is an entry of the `env` object in `alchemy.run.ts`.
6
+
7
+ | Command | What it does |
8
+ | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
9
+ | `pnpm dev` | `alchemy dev`: runs the Vite dev server and workerd locally with the bindings declared here, on stage `dev_$USER`. State stays in `.alchemy/`. |
10
+ | `pnpm run deploy --stage <name>` | `alchemy deploy`: builds `apps/web` and deploys it to Cloudflare. Without `--stage` it uses `$ALCHEMY_STAGE`, then `live_$USER`. |
11
+ | `pnpm run destroy --stage <name>` | `alchemy destroy`: removes every resource of that stage. |
12
+
13
+ Use `pnpm run deploy`: plain `pnpm deploy` is pnpm's own workspace command.
14
+
15
+ Copy `.env.example` to `.env` for local values. Deploying needs `CLOUDFLARE_API_TOKEN` and
16
+ `CLOUDFLARE_ACCOUNT_ID` (and `DATABASE_URL` for Postgres) in the environment.
17
+
18
+ ## CI
19
+
20
+ `.github/workflows/deploy.yml` deploys stage `prod` on every push to `main`, a stage `pr-<number>`
21
+ for each pull request, and destroys the PR stage when the PR closes. Add these repository secrets:
22
+ `CLOUDFLARE_API_TOKEN`, `CLOUDFLARE_ACCOUNT_ID`, `ALCHEMY_PASSWORD`, `DATABASE_URL` (and
23
+ `STORAGE_SIGNING_SECRET` with the storage extra, `BETTER_AUTH_SECRET` with auth; the auth add-on adds that one to the workflow itself). CI keeps Alchemy state in a Cloudflare-hosted
24
+ store that the first deploy creates.
25
+
26
+ Previews share one Postgres database (`DATABASE_URL`); run `pnpm db:migrate` against it before the
27
+ first deploy. Per-PR database branches need a database provider such as Neon.
@@ -0,0 +1,36 @@
1
+ import * as Alchemy from "alchemy"
2
+ import * as Cloudflare from "alchemy/Cloudflare"
3
+ import * as Effect from "effect/Effect"
4
+
5
+ /**
6
+ * Infrastructure as Effects (Alchemy v2). Run it with:
7
+ *
8
+ * pnpm dev local: Vite + workerd via `alchemy dev` (stage dev_$USER)
9
+ * pnpm run deploy --stage prod `alchemy deploy` to Cloudflare (or set $ALCHEMY_STAGE)
10
+ * pnpm run destroy --stage prod `alchemy destroy`
11
+ *
12
+ * Every Cloudflare binding of the Worker lives here, in the `env` object of the website below.
13
+ * `efforge add` appends resources above the marker and binds them by the name effect-cf reads
14
+ * (HYPERDRIVE, DB, STORAGE, ROOM, ...), so app code never changes between wrangler and Alchemy.
15
+ */
16
+ export default Alchemy.Stack(
17
+ "app",
18
+ {
19
+ providers: Cloudflare.providers(),
20
+ // CI shares state through a Cloudflare-hosted store; locally it is a `.alchemy/` folder.
21
+ state: process.env.CI ? Cloudflare.state() : Alchemy.localState(),
22
+ },
23
+ Effect.gen(function* () {
24
+ // efforge:resources
25
+
26
+ // TanStack Start on Workers + static assets. The Worker entry is `src/server.ts`, which also
27
+ // re-exports the Durable Object and Workflow classes that Cloudflare needs on the entry.
28
+ const web = yield* Cloudflare.Website.Vite("web", {
29
+ rootDir: "apps/web",
30
+ main: "src/server.ts",
31
+ env: {},
32
+ })
33
+
34
+ return { url: web.url.as<string>() }
35
+ }),
36
+ )
@@ -0,0 +1,63 @@
1
+ name: Deploy
2
+
3
+ # main -> stage `prod`; every pull request -> its own stage `pr-<number>`, destroyed when the PR closes.
4
+ # Live deploys are not part of efforge's tests: set the secrets below, then push.
5
+ on:
6
+ push:
7
+ branches: [main]
8
+ pull_request:
9
+ types: [opened, synchronize, reopened, closed]
10
+
11
+ concurrency:
12
+ group: deploy-${{ github.event.number || 'prod' }}
13
+ cancel-in-progress: false
14
+
15
+ env:
16
+ CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
17
+ CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
18
+ # Not read by alchemy 2.0.0-beta.83 itself (state encryption is managed by the Cloudflare state
19
+ # store); kept so a password-protecting release picks it up without a workflow change.
20
+ ALCHEMY_PASSWORD: ${{ secrets.ALCHEMY_PASSWORD }}
21
+ # Postgres only: the origin Hyperdrive connects to. Every PR preview shares this one database.
22
+ DATABASE_URL: ${{ secrets.DATABASE_URL }}
23
+ STORAGE_SIGNING_SECRET: ${{ secrets.STORAGE_SIGNING_SECRET }}
24
+ CI: "true"
25
+
26
+ jobs:
27
+ deploy:
28
+ # fork PRs get no secrets
29
+ if: >-
30
+ github.event_name == 'push' ||
31
+ (github.event.action != 'closed' &&
32
+ github.event.pull_request.head.repo.full_name == github.repository)
33
+ runs-on: ubuntu-latest
34
+ steps:
35
+ - uses: actions/checkout@v4
36
+ - uses: pnpm/action-setup@v4
37
+ - uses: actions/setup-node@v4
38
+ with:
39
+ node-version: 24
40
+ cache: pnpm
41
+ - run: pnpm install --frozen-lockfile
42
+ - name: Deploy
43
+ env:
44
+ ALCHEMY_STAGE: ${{ github.event_name == 'push' && 'prod' || format('pr-{0}', github.event.number) }}
45
+ run: pnpm exec alchemy deploy infra/alchemy.run.ts --yes
46
+
47
+ destroy:
48
+ if: >-
49
+ github.event_name == 'pull_request' && github.event.action == 'closed' &&
50
+ github.event.pull_request.head.repo.full_name == github.repository
51
+ runs-on: ubuntu-latest
52
+ steps:
53
+ - uses: actions/checkout@v4
54
+ - uses: pnpm/action-setup@v4
55
+ - uses: actions/setup-node@v4
56
+ with:
57
+ node-version: 24
58
+ cache: pnpm
59
+ - run: pnpm install --frozen-lockfile
60
+ - name: Destroy the preview stage
61
+ env:
62
+ ALCHEMY_STAGE: pr-${{ github.event.number }}
63
+ run: pnpm exec alchemy destroy infra/alchemy.run.ts --yes
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../tsconfig.base.json",
3
+ "compilerOptions": { "types": ["node"], "noEmit": true },
4
+ "include": ["alchemy.run.ts"]
5
+ }
@@ -0,0 +1,30 @@
1
+ import { cloudflare } from "@cloudflare/vite-plugin"
2
+ import tailwindcss from "@tailwindcss/vite"
3
+ import { tanstackStart } from "@tanstack/react-start/plugin/vite"
4
+ import viteReact from "@vitejs/plugin-react"
5
+ import { defineConfig } from "vite"
6
+
7
+ // `alchemy dev` / `alchemy deploy` inject their own resource-aware Cloudflare plugin (with every
8
+ // binding from infra/alchemy.run.ts) and set this variable. A plain `vite build` (pnpm build,
9
+ // CI) has no bindings to resolve, so it uses a minimal inline config instead of a wrangler.jsonc.
10
+ const injectedByAlchemy = process.env.ALCHEMY_CLOUDFLARE_VITE_INJECTED === "1"
11
+
12
+ export default defineConfig({
13
+ resolve: { tsconfigPaths: true },
14
+ plugins: [
15
+ injectedByAlchemy
16
+ ? null
17
+ : cloudflare({
18
+ config: {
19
+ name: "app",
20
+ main: "src/server.ts",
21
+ compatibility_date: "2026-10-01",
22
+ compatibility_flags: ["nodejs_compat"],
23
+ },
24
+ viteEnvironment: { name: "ssr" },
25
+ }),
26
+ tanstackStart(),
27
+ tailwindcss(),
28
+ viteReact(),
29
+ ],
30
+ })