The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Neon from "@alchemy.run/neon"; |
| 2 | import * as Alchemy from "alchemy"; |
| 3 | import * as Output from "alchemy/Output"; |
| 4 | import * as Config from "effect/Config"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | |
| 7 | export default Alchemy.Stack( |
| 8 | "NeonUploadPreview", |
| 9 | { |
| 10 | providers: Neon.providers(), |
| 11 | state: Alchemy.localState(), |
| 12 | }, |
| 13 | Effect.gen(function* () { |
| 14 | const projectId = yield* Config.String("PARENT_PROJECT_ID"); |
| 15 | const parentBranchId = yield* Config.String("PARENT_BRANCH_ID"); |
| 16 | const bucketName = yield* Config.String("PARENT_BUCKET_NAME"); |
| 17 | const appOrigin = yield* Effect.sync(() => process.env.UPLOAD_APP_ORIGIN ?? "*"); |
| 18 | const branch = yield* Neon.Branch("Preview", { |
| 19 | project: { projectId }, |
| 20 | parentBranch: { branchId: parentBranchId }, |
| 21 | initSource: "parent-data", |
| 22 | }); |
| 23 | // Adoption is confined to the new child; the parent Auth remains independently owned. |
| 24 | const auth = yield* Neon.Auth("PreviewAuth", { |
| 25 | branch, |
| 26 | name: "Upload journal preview", |
| 27 | allowLocalhost: true, |
| 28 | }).pipe(Alchemy.AdoptPolicy.adopt()); |
| 29 | const api = yield* Neon.Function("PreviewApi", { |
| 30 | branch, |
| 31 | main: "./src/native.ts", |
| 32 | env: { |
| 33 | APP_ORIGIN: appOrigin, |
| 34 | UPLOAD_BUCKET: bucketName, |
| 35 | AUTH_URL: auth.baseUrl, |
| 36 | AUTH_JWKS_URL: auth.jwksUrl, |
| 37 | TRIGGER_NAME: "PreviewUploads", |
| 38 | }, |
| 39 | }); |
| 40 | // Inherited triggers stay disabled. This is a new, explicitly enabled child trigger. |
| 41 | yield* Neon.FunctionTrigger("PreviewUploads", { |
| 42 | function: api, |
| 43 | name: "PreviewUploads", |
| 44 | type: "storage_object_created", |
| 45 | storageObjectCreated: { |
| 46 | bucket: { projectId, branchId: branch.branchId, bucketName }, |
| 47 | prefix: "incoming/", |
| 48 | }, |
| 49 | path: "/jobs/upload", |
| 50 | enabled: true, |
| 51 | }); |
| 52 | const web = yield* Neon.Website.Vite("PreviewWeb", { |
| 53 | branch, |
| 54 | rootDir: "./web", |
| 55 | env: { VITE_API_URL: api.url, VITE_NEON_AUTH_URL: auth.baseUrl }, |
| 56 | assets: { notFoundHandling: "single-page-application" }, |
| 57 | }); |
| 58 | if (!web.url) return yield* Effect.die(new Error("Preview website URL is unavailable")); |
| 59 | const siteUrl = typeof web.url === "string" ? Output.literal(web.url) : web.url; |
| 60 | const origin = Output.map(siteUrl, (url: string | undefined) => new URL(url ?? "").origin); |
| 61 | yield* Neon.AuthTrustedDomain("PreviewOrigin", { auth, domain: origin }); |
| 62 | // No CustomDomain is declared: parent hostnames must never route to this preview. |
| 63 | return { |
| 64 | url: web.url, |
| 65 | apiUrl: api.url, |
| 66 | authUrl: auth.baseUrl, |
| 67 | projectId, |
| 68 | parentBranchId, |
| 69 | branchId: branch.branchId, |
| 70 | bucketName, |
| 71 | }; |
| 72 | }), |
| 73 | ); |