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,115 @@
1
+ /**
2
+ * Which URLs the browser may load. This container fetches whatever URL it is given, so without a
3
+ * policy it is a server-side request forgery (SSRF) proxy into every network it can reach.
4
+ *
5
+ * Blocked unless `allowPrivate` is set: non-http(s) schemes, URLs with credentials, `localhost`,
6
+ * `.local`/`.internal` hosts, and IP literals in private, loopback, link-local (cloud metadata
7
+ * at 169.254.169.254), carrier-grade NAT, multicast and reserved ranges, IPv4 and IPv6.
8
+ *
9
+ * Limit: hostnames are checked by name. A public-looking DNS name that resolves to a private
10
+ * address is not caught here, so also restrict egress at the network level if the URLs are untrusted.
11
+ */
12
+
13
+ export type UrlCheck =
14
+ | { readonly ok: true; readonly url: URL }
15
+ | { readonly ok: false; readonly reason: string }
16
+
17
+ const blockedSuffixes = [".localhost", ".local", ".internal", ".localdomain", ".home.arpa"]
18
+
19
+ const ipv4Octets = (host: string): ReadonlyArray<number> | undefined => {
20
+ const parts = host.split(".")
21
+ if (parts.length !== 4 || !parts.every((p) => /^\d{1,3}$/.test(p))) return undefined
22
+ const octets = parts.map(Number)
23
+ return octets.every((n) => n <= 255) ? octets : undefined
24
+ }
25
+
26
+ const isPrivateIpv4 = ([a = 0, b = 0, c = 0]: ReadonlyArray<number>): boolean =>
27
+ a === 0 ||
28
+ a === 10 ||
29
+ a === 127 ||
30
+ (a === 100 && b >= 64 && b <= 127) ||
31
+ (a === 169 && b === 254) ||
32
+ (a === 172 && b >= 16 && b <= 31) ||
33
+ (a === 192 && b === 0 && c === 0) ||
34
+ (a === 192 && b === 168) ||
35
+ (a === 198 && (b === 18 || b === 19)) ||
36
+ a >= 224
37
+
38
+ const side = (s: string) => (s === "" ? [] : s.split(":"))
39
+
40
+ /** Eight 16-bit groups of an IPv6 literal (without brackets), or undefined if it isn't one. */
41
+ const ipv6Groups = (host: string): ReadonlyArray<number> | undefined => {
42
+ if (!/^[0-9a-f:.]+$/i.test(host) || !host.includes(":")) return undefined
43
+ let text = host
44
+ const tail = text.slice(text.lastIndexOf(":") + 1)
45
+ if (tail.includes(".")) {
46
+ const v4 = ipv4Octets(tail)
47
+ if (v4 === undefined) return undefined
48
+ const [a = 0, b = 0, c = 0, d = 0] = v4
49
+ text = `${text.slice(0, text.lastIndexOf(":") + 1)}${((a << 8) | b).toString(16)}:${((c << 8) | d).toString(16)}`
50
+ }
51
+ const halves = text.split("::")
52
+ if (halves.length > 2) return undefined
53
+ const head = side(halves[0] ?? "")
54
+ const rest = side(halves[1] ?? "")
55
+ const missing = 8 - head.length - rest.length
56
+ if (halves.length === 1 ? missing !== 0 : missing < 1) return undefined
57
+ const groups = [
58
+ ...head,
59
+ ...Array.from({ length: halves.length === 2 ? missing : 0 }, () => "0"),
60
+ ...rest,
61
+ ]
62
+ if (!groups.every((g) => /^[0-9a-f]{1,4}$/i.test(g))) return undefined
63
+ return groups.map((g) => Number.parseInt(g, 16))
64
+ }
65
+
66
+ const isPrivateIpv6 = (g: ReadonlyArray<number>): boolean => {
67
+ const [g0 = 0, g1 = 0, g2 = 0, g3 = 0, g4 = 0, g5 = 0, g6 = 0, g7 = 0] = g
68
+ const embedded = [g6 >> 8, g6 & 255, g7 >> 8, g7 & 255]
69
+ // ::, ::1 and the deprecated IPv4-compatible ::a.b.c.d
70
+ if (g0 === 0 && g1 === 0 && g2 === 0 && g3 === 0 && g4 === 0 && g5 === 0) return true
71
+ // IPv4-mapped ::ffff:a.b.c.d
72
+ if (g0 === 0 && g1 === 0 && g2 === 0 && g3 === 0 && g4 === 0 && g5 === 0xffff)
73
+ return isPrivateIpv4(embedded)
74
+ // NAT64 64:ff9b::/96 carries an IPv4 address
75
+ if (g0 === 0x64 && g1 === 0xff9b && g2 === 0 && g3 === 0 && g4 === 0 && g5 === 0)
76
+ return isPrivateIpv4(embedded)
77
+ // 6to4 2002:AABB:CCDD::/48 carries an IPv4 address
78
+ if (g0 === 0x2002) return isPrivateIpv4([g1 >> 8, g1 & 255, g2 >> 8, g2 & 255])
79
+ return (
80
+ (g0 & 0xfe00) === 0xfc00 || // unique local fc00::/7
81
+ (g0 & 0xffc0) === 0xfe80 || // link-local fe80::/10
82
+ (g0 & 0xff00) === 0xff00 // multicast
83
+ )
84
+ }
85
+
86
+ /** Parses `raw` with `new URL` and applies the policy. `allowPrivate` turns the network checks off. */
87
+ export const checkUrl = (raw: string, allowPrivate = false): UrlCheck => {
88
+ let url: URL
89
+ try {
90
+ url = new URL(raw)
91
+ } catch {
92
+ return { ok: false, reason: "not a valid URL" }
93
+ }
94
+ if (url.protocol !== "http:" && url.protocol !== "https:")
95
+ return { ok: false, reason: "only http and https URLs are allowed" }
96
+ if (url.username !== "" || url.password !== "")
97
+ return { ok: false, reason: "URLs with credentials are not allowed" }
98
+ if (allowPrivate) return { ok: true, url }
99
+
100
+ // `new URL` already lowercases, and turns decimal/hex/octal IPv4 forms into dotted quads
101
+ const host = url.hostname.replace(/^\[|\]$/g, "").replace(/\.$/, "")
102
+ const blocked = {
103
+ ok: false,
104
+ reason: "this host is not allowed (private or local network)",
105
+ } as const
106
+ if (host === "localhost" || blockedSuffixes.some((suffix) => host.endsWith(suffix)))
107
+ return blocked
108
+ const v4 = ipv4Octets(host)
109
+ if (v4 !== undefined) return isPrivateIpv4(v4) ? blocked : { ok: true, url }
110
+ if (url.hostname.startsWith("[")) {
111
+ const v6 = ipv6Groups(host)
112
+ return v6 === undefined || isPrivateIpv6(v6) ? blocked : { ok: true, url }
113
+ }
114
+ return { ok: true, url }
115
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "$schema": "../../node_modules/@effect/tsgo/schema.json",
3
+ "extends": "../../tsconfig.base.json",
4
+ "compilerOptions": { "types": ["node"], "noEmit": true },
5
+ "include": ["src"]
6
+ }
@@ -0,0 +1,34 @@
1
+ import { Browser, BrowserError } from "@repo/core"
2
+ import { Effect, Layer, Schema } from "effect"
3
+ import { ContainerNamespace } from "effect-cf"
4
+ import { WorkerEnvLive } from "./cloudflare"
5
+
6
+ class BrowserNamespace extends ContainerNamespace.Tag<BrowserNamespace>()("BrowserNamespace") {}
7
+
8
+ const RunResult = Schema.Struct({ title: Schema.String, requests: Schema.Finite })
9
+ const decode = Schema.decodeUnknownEffect(RunResult)
10
+
11
+ const fail = (operation: string) => (cause: unknown) => new BrowserError({ operation, cause })
12
+
13
+ const make = Effect.gen(function* () {
14
+ const containers = yield* BrowserNamespace
15
+ return Browser.of({
16
+ run: (url) =>
17
+ Effect.gen(function* () {
18
+ // one shared instance; use a different name per tenant or job to run containers in parallel
19
+ const response = yield* containers.byName("default").fetch("http://container/run", {
20
+ method: "POST",
21
+ headers: { "content-type": "application/json" },
22
+ body: JSON.stringify({ url }),
23
+ })
24
+ if (!response.ok) return yield* Effect.fail(`container responded ${response.status}`)
25
+ return yield* decode(yield* Effect.promise(() => response.json()))
26
+ }).pipe(Effect.mapError(fail("run"))),
27
+ })
28
+ })
29
+
30
+ /** `Browser` on the BROWSER container binding. */
31
+ export const BrowserLive = Layer.effect(Browser, make).pipe(
32
+ Layer.provide(BrowserNamespace.layer({ binding: "BROWSER" })),
33
+ Layer.provide(WorkerEnvLive),
34
+ )
@@ -0,0 +1,21 @@
1
+ import { Context, Data, type Effect } from "effect"
2
+
3
+ export class BrowserError extends Data.TaggedError("BrowserError")<{
4
+ readonly operation: string
5
+ readonly cause: unknown
6
+ }> {}
7
+
8
+ export interface BrowserRun {
9
+ readonly title: string
10
+ /** Network requests the page made while loading. */
11
+ readonly requests: number
12
+ }
13
+
14
+ /** A headless browser. The Cloudflare layer runs Playwright in the `apps/browser` container. */
15
+ export class Browser extends Context.Service<
16
+ Browser,
17
+ {
18
+ /** Loads `url` and reports what the page did. */
19
+ readonly run: (url: string) => Effect.Effect<BrowserRun, BrowserError>
20
+ }
21
+ >()("@repo/core/Browser") {}
@@ -0,0 +1,25 @@
1
+ import { Browser } from "@repo/core"
2
+ import { createServerFn } from "@tanstack/react-start"
3
+ import { Effect, Schema } from "effect"
4
+ import { runtime } from "./runtime"
5
+
6
+ const RunInput = Schema.Struct({
7
+ url: Schema.String.check(Schema.isPattern(/^https?:\/\//i), Schema.isMaxLength(2048)),
8
+ })
9
+
10
+ /**
11
+ * Loads `url` in the browser container and returns the page title and request count. The
12
+ * container refuses private and local addresses (see apps/browser/src/server.ts). With an auth
13
+ * add-on this function is gated by `requireSession`; without one, add your own check before
14
+ * exposing it to anonymous users.
15
+ */
16
+ export const runBrowser = createServerFn({ method: "POST" })
17
+ .validator(Schema.toStandardSchemaV1(RunInput))
18
+ .handler(({ data }) =>
19
+ runtime.runPromise(
20
+ Effect.gen(function* () {
21
+ const browser = yield* Browser
22
+ return yield* browser.run(data.url)
23
+ }),
24
+ ),
25
+ )
@@ -0,0 +1,182 @@
1
+ {
2
+ "id": "extras/email",
3
+ "title": "Email",
4
+ "description": "Transactional email: a Mailer service on Cloudflare Email Service, printed to the console in development, plus invitation emails with organizations.",
5
+ "files": [
6
+ {
7
+ "path": "files/core-mailer.ts",
8
+ "target": "packages/core/src/mailer.ts"
9
+ },
10
+ {
11
+ "path": "files/mailer.ts",
12
+ "target": "apps/web/src/server/mailer.ts"
13
+ },
14
+ {
15
+ "path": "files/invitation-email.ts",
16
+ "target": "apps/web/src/server/invitation-email.ts",
17
+ "when": {
18
+ "auth": [
19
+ "better-auth-orgs"
20
+ ]
21
+ }
22
+ }
23
+ ],
24
+ "dependencies": {},
25
+ "devDependencies": {},
26
+ "envVars": [
27
+ {
28
+ "name": "MAIL_FROM",
29
+ "example": "noreply@example.com",
30
+ "description": "Sender address; its domain must be enabled for Cloudflare Email Sending (wrangler email sending enable <domain>)"
31
+ },
32
+ {
33
+ "name": "MAIL_TRANSPORT",
34
+ "example": "cloudflare",
35
+ "description": "cloudflare (default) sends through the EMAIL binding; console prints emails instead (used by .dev.vars)"
36
+ }
37
+ ],
38
+ "requires": [],
39
+ "conflicts": [],
40
+ "transforms": [
41
+ {
42
+ "name": "addExport",
43
+ "args": {
44
+ "file": "packages/core/src/index.ts",
45
+ "export": "*",
46
+ "from": "./mailer"
47
+ }
48
+ },
49
+ {
50
+ "name": "addLayer",
51
+ "args": {
52
+ "file": "apps/web/src/server/layers.ts",
53
+ "import": "MailerLive",
54
+ "from": "./mailer",
55
+ "layer": "MailerLive"
56
+ }
57
+ },
58
+ {
59
+ "name": "addDevVar",
60
+ "args": {
61
+ "name": "MAIL_TRANSPORT",
62
+ "value": "console"
63
+ }
64
+ },
65
+ {
66
+ "name": "addDevVar",
67
+ "args": {
68
+ "name": "MAIL_FROM",
69
+ "value": "noreply@localhost"
70
+ }
71
+ },
72
+ {
73
+ "name": "addAuthPlugin",
74
+ "args": {
75
+ "mergeInto": "organization",
76
+ "property": "sendInvitationEmail",
77
+ "value": "sendInvitationEmail",
78
+ "import": "sendInvitationEmail",
79
+ "from": "./invitation-email"
80
+ },
81
+ "when": {
82
+ "auth": [
83
+ "better-auth-orgs"
84
+ ]
85
+ }
86
+ },
87
+ {
88
+ "name": "addWranglerBinding",
89
+ "args": {
90
+ "key": "send_email",
91
+ "value": "{\"name\":\"EMAIL\"}"
92
+ },
93
+ "when": {
94
+ "infra": [
95
+ "none"
96
+ ]
97
+ }
98
+ },
99
+ {
100
+ "name": "addImport",
101
+ "args": {
102
+ "file": "infra/alchemy.run.ts",
103
+ "import": "* as Config",
104
+ "from": "effect/Config"
105
+ },
106
+ "when": {
107
+ "infra": [
108
+ "alchemy"
109
+ ]
110
+ }
111
+ },
112
+ {
113
+ "name": "addAlchemyResource",
114
+ "args": {
115
+ "name": "email",
116
+ "code": "const email = Cloudflare.Email.SendEmail(\"Email\")",
117
+ "binding": "EMAIL"
118
+ },
119
+ "when": {
120
+ "infra": [
121
+ "alchemy"
122
+ ]
123
+ }
124
+ },
125
+ {
126
+ "name": "addAlchemyResource",
127
+ "args": {
128
+ "name": "mailFrom",
129
+ "code": "const mailFrom = yield* Config.String(\"MAIL_FROM\")",
130
+ "binding": "MAIL_FROM"
131
+ },
132
+ "when": {
133
+ "infra": [
134
+ "alchemy"
135
+ ]
136
+ }
137
+ },
138
+ {
139
+ "name": "addAlchemyResource",
140
+ "args": {
141
+ "name": "mailTransport",
142
+ "code": "// local stages print emails to the console; every other stage sends them\nconst mailTransport = yield* Config.String(\"MAIL_TRANSPORT\").pipe(\n Config.withDefault((yield* Alchemy.Stack).stage.startsWith(\"dev\") ? \"console\" : \"cloudflare\"),\n)",
143
+ "binding": "MAIL_TRANSPORT"
144
+ },
145
+ "when": {
146
+ "infra": [
147
+ "alchemy"
148
+ ]
149
+ }
150
+ },
151
+ {
152
+ "name": "addWorkflowSecret",
153
+ "args": {
154
+ "name": "MAIL_FROM"
155
+ },
156
+ "when": {
157
+ "infra": [
158
+ "alchemy"
159
+ ]
160
+ }
161
+ },
162
+ {
163
+ "name": "appendAgentsSection",
164
+ "args": {
165
+ "id": "email",
166
+ "content": "## Email\n\n- Send mail through the `Mailer` service from `@repo/core` (`send({ to, subject, text, html? })`); do not use the `EMAIL` binding directly. Always send a `text` body.\n- `apps/web/src/server/mailer.ts` picks the transport from `MAIL_TRANSPORT`: `console` (set in `.dev.vars`) prints the message to the dev server log, anything else sends through Cloudflare Email Service with the `EMAIL` `send_email` binding. `MAIL_FROM` must be an address on a domain enabled for sending: `wrangler email sending enable <domain>`.\n- Transactional mail only (invitations, receipts, password resets); use a dedicated platform for marketing or bulk sends."
167
+ }
168
+ },
169
+ {
170
+ "name": "appendAgentsSection",
171
+ "args": {
172
+ "id": "email-invitations",
173
+ "content": "### Invitation emails\n\n- `organization({ sendInvitationEmail })` in `auth.ts` calls `invitation-email.ts`, which emails the `/accept-invite/<id>` link through the `Mailer`. A failed send is logged and does not fail the invitation; the link is still shown to the inviter."
174
+ },
175
+ "when": {
176
+ "auth": [
177
+ "better-auth-orgs"
178
+ ]
179
+ }
180
+ }
181
+ ]
182
+ }
@@ -0,0 +1,28 @@
1
+ import { Context, Data, type Effect } from "effect"
2
+
3
+ export class MailerError extends Data.TaggedError("MailerError")<{
4
+ readonly operation: string
5
+ readonly cause: unknown
6
+ }> {}
7
+
8
+ /** The Mailer layer could not be built: a required setting (for example MAIL_FROM) is missing. */
9
+ export class MailerConfigError extends Data.TaggedError("MailerConfigError")<{
10
+ readonly message: string
11
+ }> {}
12
+
13
+ export interface MailMessage {
14
+ readonly to: string
15
+ readonly subject: string
16
+ readonly text: string
17
+ /** Optional HTML version; always send `text` too. */
18
+ readonly html?: string
19
+ }
20
+
21
+ /**
22
+ * Transactional email. In production the layer sends through Cloudflare Email Service (the
23
+ * `EMAIL` `send_email` binding); locally it prints the message to the console instead.
24
+ */
25
+ export class Mailer extends Context.Service<
26
+ Mailer,
27
+ { readonly send: (message: MailMessage) => Effect.Effect<void, MailerError> }
28
+ >()("@repo/core/Mailer") {}
@@ -0,0 +1,38 @@
1
+ import { Mailer } from "@repo/core/mailer"
2
+ import { Effect, ManagedRuntime } from "effect"
3
+ import { MailerLive } from "./mailer"
4
+
5
+ // Better Auth calls this from inside `makeAuth`, which the app layers import, so it can't reach
6
+ // back into `runtime` (an import cycle). The Mailer needs nothing but the Worker env: own runtime.
7
+ const mailRuntime = ManagedRuntime.make(MailerLive)
8
+
9
+ const escapeHtml = (text: string) => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`)
10
+
11
+ interface Invitation {
12
+ readonly id: string
13
+ readonly email: string
14
+ readonly organization: { readonly name: string }
15
+ readonly inviter: { readonly user: { readonly name: string } }
16
+ }
17
+
18
+ /**
19
+ * `organization({ sendInvitationEmail })`: emails the invite link. A failed send is logged, not
20
+ * thrown, so the invitation still exists and the link shown to the inviter keeps working.
21
+ */
22
+ export const sendInvitationEmail = (data: Invitation, request?: Request): Promise<void> => {
23
+ if (request === undefined) return Promise.resolve()
24
+ const link = `${new URL(request.url).origin}/accept-invite/${data.id}`
25
+ const { name } = data.organization
26
+ const inviter = data.inviter.user.name
27
+ return mailRuntime.runPromise(
28
+ Effect.gen(function* () {
29
+ const mailer = yield* Mailer
30
+ yield* mailer.send({
31
+ to: data.email,
32
+ subject: `${inviter} invited you to ${name}`,
33
+ text: `${inviter} invited you to join ${name}.\n\nAccept the invitation: ${link}\n`,
34
+ html: `<p>${escapeHtml(inviter)} invited you to join <strong>${escapeHtml(name)}</strong>.</p><p><a href="${escapeHtml(link)}">Accept the invitation</a></p>`,
35
+ })
36
+ }).pipe(Effect.catch((error) => Effect.logError("Could not send the invitation email", error))),
37
+ )
38
+ }
@@ -0,0 +1,58 @@
1
+ import { Mailer, MailerConfigError, MailerError } from "@repo/core/mailer"
2
+ import { Config, Effect, Layer, Option } from "effect"
3
+ import { Email, WorkerConfig } from "effect-cf"
4
+ import { WorkerEnvLive } from "./cloudflare"
5
+
6
+ /** The `EMAIL` `send_email` binding (see wrangler.jsonc or infra/alchemy.run.ts). */
7
+ class EmailBinding extends Email.Tag<EmailBinding>()("EmailBinding") {}
8
+
9
+ /** Prints each message instead of sending it, so invitations and receipts are visible in `pnpm dev`. */
10
+ const ConsoleMailer = (from: string) =>
11
+ Layer.succeed(
12
+ Mailer,
13
+ Mailer.of({
14
+ send: (message) =>
15
+ Effect.log(
16
+ `[mail] from ${from} to ${message.to}\nSubject: ${message.subject}\n\n${message.text}`,
17
+ ),
18
+ }),
19
+ )
20
+
21
+ /** Sends through Cloudflare Email Service. `from` must be an address on a domain onboarded to Email Sending. */
22
+ const CloudflareMailer = (from: string) =>
23
+ Layer.effect(
24
+ Mailer,
25
+ Effect.gen(function* () {
26
+ const email = yield* EmailBinding
27
+ return Mailer.of({
28
+ send: ({ to, subject, text, html }) =>
29
+ email.send({ from, to, subject, text, ...(html === undefined ? {} : { html }) }).pipe(
30
+ Effect.asVoid,
31
+ Effect.mapError((cause) => new MailerError({ operation: "send", cause })),
32
+ ),
33
+ })
34
+ }),
35
+ ).pipe(Layer.provide(EmailBinding.layer({ binding: "EMAIL" })))
36
+
37
+ /**
38
+ * `Mailer`, chosen by MAIL_TRANSPORT: `console` (local development, prints the message) or
39
+ * `cloudflare` (the default). The Cloudflare transport needs MAIL_FROM, checked when the layer is built.
40
+ */
41
+ export const MailerLive = Layer.unwrap(
42
+ Effect.gen(function* () {
43
+ const provider = yield* WorkerConfig.provider
44
+ const transport = Option.getOrElse(
45
+ yield* Config.option(Config.String("MAIL_TRANSPORT")).parse(provider),
46
+ () => "cloudflare",
47
+ )
48
+ const from = yield* Config.option(Config.String("MAIL_FROM")).parse(provider)
49
+ if (transport === "console")
50
+ return ConsoleMailer(Option.getOrElse(from, () => "noreply@localhost"))
51
+ if (Option.isNone(from) || from.value === "")
52
+ return yield* new MailerConfigError({
53
+ message:
54
+ "MAIL_FROM is not set. Set it to an address on a domain enabled for Cloudflare Email Sending (wrangler email sending enable <domain>), or set MAIL_TRANSPORT=console to print emails instead.",
55
+ })
56
+ return CloudflareMailer(from.value)
57
+ }),
58
+ ).pipe(Layer.provide(WorkerEnvLive))
@@ -0,0 +1,135 @@
1
+ {
2
+ "id": "extras/realtime",
3
+ "title": "Realtime",
4
+ "description": "WebSockets: a Realtime service, a hibernating Room Durable Object, a useRoom hook and a demo on /app.",
5
+ "files": [
6
+ {
7
+ "path": "files/core-realtime.ts",
8
+ "target": "packages/core/src/realtime.ts"
9
+ },
10
+ {
11
+ "path": "files/Room.ts",
12
+ "target": "apps/web/src/realtime/Room.ts"
13
+ },
14
+ {
15
+ "path": "files/realtime.ts",
16
+ "target": "apps/web/src/server/realtime.ts"
17
+ },
18
+ {
19
+ "path": "files/realtime-route.ts",
20
+ "target": "apps/web/src/server/realtime-route.ts"
21
+ },
22
+ {
23
+ "path": "files/use-room.ts",
24
+ "target": "apps/web/src/lib/use-room.ts"
25
+ },
26
+ {
27
+ "path": "files/realtime-demo.tsx",
28
+ "target": "apps/web/src/components/realtime-demo.tsx"
29
+ },
30
+ {
31
+ "path": "files/realtime-limits.ts",
32
+ "target": "packages/core/src/realtime-limits.ts"
33
+ },
34
+ {
35
+ "path": "files/realtime-limits.test.ts",
36
+ "target": "packages/core/test/realtime-limits.test.ts"
37
+ }
38
+ ],
39
+ "dependencies": {},
40
+ "devDependencies": {},
41
+ "envVars": [],
42
+ "requires": [],
43
+ "conflicts": [],
44
+ "transforms": [
45
+ {
46
+ "name": "addExport",
47
+ "args": {
48
+ "file": "packages/core/src/index.ts",
49
+ "export": "*",
50
+ "from": "./realtime"
51
+ }
52
+ },
53
+ {
54
+ "name": "addExport",
55
+ "args": {
56
+ "file": "apps/web/src/server.ts",
57
+ "export": "Room",
58
+ "from": "./realtime/Room"
59
+ }
60
+ },
61
+ {
62
+ "name": "addLayer",
63
+ "args": {
64
+ "file": "apps/web/src/server/layers.ts",
65
+ "import": "RealtimeLive",
66
+ "from": "./realtime",
67
+ "layer": "RealtimeLive"
68
+ }
69
+ },
70
+ {
71
+ "name": "addArrayItem",
72
+ "args": {
73
+ "file": "apps/web/src/server/routes.ts",
74
+ "name": "workerRoutes",
75
+ "item": "realtimeRoute",
76
+ "import": "realtimeRoute",
77
+ "from": "./realtime-route"
78
+ }
79
+ },
80
+ {
81
+ "name": "addArrayItem",
82
+ "args": {
83
+ "file": "apps/web/src/components/widgets.tsx",
84
+ "name": "widgets",
85
+ "item": "<RealtimeDemo key=\"realtime\" />",
86
+ "import": "RealtimeDemo",
87
+ "from": "./realtime-demo"
88
+ }
89
+ },
90
+ {
91
+ "name": "addWranglerBinding",
92
+ "args": {
93
+ "key": "durable_objects.bindings",
94
+ "value": "{\"name\":\"ROOM\",\"class_name\":\"Room\"}"
95
+ },
96
+ "when": {
97
+ "infra": [
98
+ "none"
99
+ ]
100
+ }
101
+ },
102
+ {
103
+ "name": "addWranglerBinding",
104
+ "args": {
105
+ "key": "migrations",
106
+ "value": "{\"tag\":\"realtime-v1\",\"new_sqlite_classes\":[\"Room\"]}"
107
+ },
108
+ "when": {
109
+ "infra": [
110
+ "none"
111
+ ]
112
+ }
113
+ },
114
+ {
115
+ "name": "addAlchemyResource",
116
+ "args": {
117
+ "name": "room",
118
+ "code": "const room = Cloudflare.DurableObject(\"Room\")",
119
+ "binding": "ROOM"
120
+ },
121
+ "when": {
122
+ "infra": [
123
+ "alchemy"
124
+ ]
125
+ }
126
+ },
127
+ {
128
+ "name": "appendAgentsSection",
129
+ "args": {
130
+ "id": "realtime",
131
+ "content": "## Realtime\n\n- Rooms are a Durable Object (`Room`, exported from `apps/web/src/server.ts`); WebSocket upgrades are handled by a Worker route in `server/routes.ts`.\n- Use the `Realtime` service from `@repo/core` on the server and the `useRoom` hook in `apps/web/src/lib/use-room.ts` in the UI.\n- Keep message payloads small and validate incoming messages with `Schema` before using them."
132
+ }
133
+ }
134
+ ]
135
+ }