The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 2 | import type { MemoOptions } from "alchemy/Command/Memo"; |
| 3 | import * as DNS from "alchemy/DNS/Adapter"; |
| 4 | import * as Output from "alchemy/Output"; |
| 5 | import { ProviderModePolicy } from "alchemy/ProviderMode"; |
| 6 | import type { ResourceLike } from "alchemy/Resource"; |
| 7 | import type { WebsiteAssetsProps } from "alchemy/Website/assets"; |
| 8 | import { Server, type ServerDevProps } from "alchemy/Website/Server"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import { Compute, type ComputeProps } from "../Compute.ts"; |
| 12 | import { CustomDomain } from "../CustomDomain.ts"; |
| 13 | import { Project } from "../Project.ts"; |
| 14 | import type { Providers } from "../Providers.ts"; |
| 15 | import type { PrismaRegionId } from "../Types.ts"; |
| 16 | import { WebsiteArtifact } from "./Artifact.ts"; |
| 17 | |
| 18 | /** A Prisma Project resource, ID, or lightweight ID reference. */ |
| 19 | export type WebsiteProjectReference = |
| 20 | | Project |
| 21 | | string |
| 22 | | { |
| 23 | /** Existing Prisma project ID; no project is created or owned by the site. */ |
| 24 | readonly projectId: string | Output.Output<string>; |
| 25 | }; |
| 26 | |
| 27 | /** Deployment controls that do not replace the framework build or artifact. */ |
| 28 | export type WebsiteComputeOptions = Pick< |
| 29 | ComputeProps, |
| 30 | | "appName" |
| 31 | | "port" |
| 32 | | "envClass" |
| 33 | | "start" |
| 34 | | "skipPromote" |
| 35 | | "destroyOldDeployment" |
| 36 | | "timeoutSeconds" |
| 37 | | "pollIntervalMs" |
| 38 | | "verifyUrl" |
| 39 | | "healthCheck" |
| 40 | | "urlReadinessTimeoutSeconds" |
| 41 | >; |
| 42 | |
| 43 | /** Custom hostname of a Prisma website: the hostname, or `{ name, dns }`. */ |
| 44 | export type WebsiteDomain = |
| 45 | | string |
| 46 | | { |
| 47 | /** Hostname, e.g. `www.example.com`. */ |
| 48 | name: string; |
| 49 | /** |
| 50 | * DNS host that publishes the hostname's `CNAME` to the app's regional |
| 51 | * switchboard (`switchboard.{region}.prisma.build`) before the domain |
| 52 | * is registered — Prisma verifies it on registration (see |
| 53 | * [DNS Adapters](/infrastructure-as-code/dns-adapters)), e.g. |
| 54 | * `Cloudflare.DNS.Adapter()`. Omitted: publish it yourself first. |
| 55 | */ |
| 56 | dns?: DNS.DnsConfig; |
| 57 | }; |
| 58 | |
| 59 | /** The hostname of a {@link WebsiteDomain}. */ |
| 60 | export const websiteDomainName = (domain: WebsiteDomain | undefined): string | undefined => |
| 61 | domain === undefined || typeof domain === "string" ? domain : domain.name; |
| 62 | |
| 63 | /** |
| 64 | * The regional switchboard a custom hostname CNAMEs to, derived from the |
| 65 | * app endpoint (`https://{app}.{region}.prisma.build` → |
| 66 | * `switchboard.{region}.prisma.build`). |
| 67 | */ |
| 68 | const switchboardTarget = (endpoint: string | undefined) => { |
| 69 | const host = (endpoint ?? "").replace(/^https?:\/\//, "").replace(/\/.*$/, ""); |
| 70 | const dot = host.indexOf("."); |
| 71 | return dot < 0 ? host : `switchboard${host.slice(dot)}`; |
| 72 | }; |
| 73 | |
| 74 | /** Shared contract for Prisma's framework website composites. */ |
| 75 | export interface FrameworkSiteProps { |
| 76 | /** Existing Project or ID reference, optionally produced by an Effect. Omission creates a project without a database, only on live deployments. */ |
| 77 | project?: WebsiteProjectReference | Effect.Effect<WebsiteProjectReference, never, Providers>; |
| 78 | /** Application directory containing package.json and framework configuration. @default "." */ |
| 79 | rootDir?: string; |
| 80 | /** Build input hashing options. Set false to rebuild on every deployment. @default true */ |
| 81 | memo?: MemoOptions | boolean; |
| 82 | /** Build, development, and runtime environment. Wrap secrets in Redacted; public framework variables can be compiled into browser assets. */ |
| 83 | env?: Record<string, string | Redacted.Redacted<string> | Output.Output<string | undefined>>; |
| 84 | /** Origin static-file routing, using the same vocabulary as Fly and Railway websites. */ |
| 85 | assets?: WebsiteAssetsProps; |
| 86 | /** Native framework development server options, including external-server mode. */ |
| 87 | dev?: ServerDevProps; |
| 88 | /** |
| 89 | * Custom hostname attached using `Prisma.CustomDomain`. |
| 90 | * |
| 91 | * A string (or `{ name }`) leaves DNS to you: Prisma verifies the |
| 92 | * hostname's `CNAME` to `switchboard.{region}.prisma.build` when the |
| 93 | * domain is registered, so publish it first (`domain.dnsRecords` lists |
| 94 | * it). Set `dns` (e.g. |
| 95 | * `{ name: "www.example.com", dns: Cloudflare.DNS.Adapter() }`) to |
| 96 | * publish it through the DNS host before registration. See |
| 97 | * [DNS Adapters](/infrastructure-as-code/dns-adapters). |
| 98 | */ |
| 99 | domain?: WebsiteDomain; |
| 100 | /** Compute region. Defaults to the project's default region, then us-east-1. */ |
| 101 | regionId?: PrismaRegionId; |
| 102 | /** Existing branch ID. Mutually exclusive with branchGitName. */ |
| 103 | branchId?: string; |
| 104 | /** Existing branch git name. Defaults to the project's default branch. */ |
| 105 | branchGitName?: string; |
| 106 | /** Compute deployment, readiness, and rollout options. The website owns its build, artifact, and runtime environment. */ |
| 107 | compute?: WebsiteComputeOptions; |
| 108 | /** Accepted for website API parity; Prisma Compute does not persist resource tags. */ |
| 109 | tags?: Record<string, string>; |
| 110 | } |
| 111 | |
| 112 | /** Outputs of a Prisma framework or static website. */ |
| 113 | export interface Website { |
| 114 | /** Native dev-server URL locally; Compute URL or configured custom-domain URL on live deployments. */ |
| 115 | url: string | Output.Output<string | undefined> | undefined; |
| 116 | /** Compute app serving the artifact. Undefined during native development. */ |
| 117 | compute: Compute | undefined; |
| 118 | /** Project resource or existing project reference. Undefined during native development. */ |
| 119 | project: WebsiteProjectReference | undefined; |
| 120 | /** Custom-domain resource, including DNS records and certificate status. Undefined without domain or during native development. */ |
| 121 | domain: CustomDomain | undefined; |
| 122 | } |
| 123 | |
| 124 | /** Internal framework-to-Node-target wiring. */ |
| 125 | export interface FrameworkSiteConfig { |
| 126 | /** Framework integration module. */ |
| 127 | framework: string; |
| 128 | /** Existing Node deploy-target module. */ |
| 129 | target: string; |
| 130 | /** Serializable framework-specific overrides. */ |
| 131 | options?: Record<string, unknown>; |
| 132 | /** Artifact layout; Next.js serves from the application root. */ |
| 133 | layout?: "output" | "next"; |
| 134 | /** Default not-found behavior, overridden by assets.notFoundHandling. */ |
| 135 | notFoundHandling?: "none" | "spa" | "404-page"; |
| 136 | /** Default extensionless HTML behavior, overridden by assets.htmlHandling. */ |
| 137 | htmlHandling?: "none" | "drop-trailing-slash"; |
| 138 | } |
| 139 | |
| 140 | /** Resolve build-time secrets without changing the redacted runtime env props. */ |
| 141 | export const websiteBuildEnv = (env: FrameworkSiteProps["env"]) => |
| 142 | env === undefined |
| 143 | ? undefined |
| 144 | : Object.fromEntries( |
| 145 | Object.entries(env).map(([key, value]) => [ |
| 146 | key, |
| 147 | Redacted.isRedacted(value) ? Redacted.value(value) : value, |
| 148 | ]), |
| 149 | ); |
| 150 | |
| 151 | /** Shared live half, called only after a build resource has been declared. */ |
| 152 | export const deployWebsite = Effect.fn(function* ( |
| 153 | props: Omit<FrameworkSiteProps, "dev">, |
| 154 | artifact: WebsiteArtifact, |
| 155 | ): Effect.fn.Return<Website, never, Providers> { |
| 156 | const project = Effect.isEffect(props.project) |
| 157 | ? yield* props.project |
| 158 | : (props.project ?? |
| 159 | (yield* Project("Project", { |
| 160 | createDatabase: false, |
| 161 | region: props.regionId, |
| 162 | }))); |
| 163 | const port = props.compute?.port ?? 3000; |
| 164 | const compute = yield* Compute("Compute", { |
| 165 | ...props.compute, |
| 166 | project: typeof project === "string" ? project : project.projectId, |
| 167 | artifactPath: artifact.artifactPath, |
| 168 | isExternal: true, |
| 169 | port, |
| 170 | regionId: props.regionId, |
| 171 | branchId: props.branchId, |
| 172 | branchGitName: props.branchGitName, |
| 173 | env: { |
| 174 | ...props.env, |
| 175 | PORT: String(port), |
| 176 | HOST: "0.0.0.0", |
| 177 | NODE_ENV: "production", |
| 178 | }, |
| 179 | }); |
| 180 | const domainName = websiteDomainName(props.domain); |
| 181 | // Prisma verifies the CNAME when the domain is registered, so the record |
| 182 | // must exist first: the CustomDomain's hostname depends on it. |
| 183 | const routing = |
| 184 | domainName && typeof props.domain === "object" && props.domain.dns |
| 185 | ? yield* (yield* DNS.resolve(props.domain.dns)).alias("DomainAlias", { |
| 186 | name: domainName, |
| 187 | target: { |
| 188 | hostname: Output.map(compute.appEndpointDomain, switchboardTarget), |
| 189 | }, |
| 190 | }) |
| 191 | : undefined; |
| 192 | const domain = domainName |
| 193 | ? yield* CustomDomain("Domain", { |
| 194 | app: compute, |
| 195 | hostname: |
| 196 | routing === undefined |
| 197 | ? domainName |
| 198 | : Output.map(Output.of(routing as ResourceLike), () => domainName), |
| 199 | }) |
| 200 | : undefined; |
| 201 | return { |
| 202 | url: domain ? Output.map(domain.hostname, (hostname) => `https://${hostname}`) : compute.url, |
| 203 | compute, |
| 204 | project, |
| 205 | domain, |
| 206 | }; |
| 207 | }); |
| 208 | |
| 209 | /** Compose a real framework build/dev server with a traced Prisma artifact. */ |
| 210 | export const makeFrameworkSite = Effect.fn(function* ( |
| 211 | _id: string, |
| 212 | props: FrameworkSiteProps, |
| 213 | config: FrameworkSiteConfig, |
| 214 | ) { |
| 215 | const context = yield* AlchemyContext; |
| 216 | const remote = yield* ProviderModePolicy; |
| 217 | const handling = props.assets?.notFoundHandling; |
| 218 | const targetConfig = { |
| 219 | notFoundHandling: |
| 220 | handling === "single-page-application" ? "spa" : (handling ?? config.notFoundHandling), |
| 221 | htmlHandling: props.assets?.htmlHandling ?? config.htmlHandling, |
| 222 | }; |
| 223 | const build = yield* Server("Build", { |
| 224 | framework: config.framework, |
| 225 | target: config.target, |
| 226 | root: props.rootDir, |
| 227 | env: websiteBuildEnv(props.env), |
| 228 | options: { ...config.options, ...targetConfig, targetConfig }, |
| 229 | memo: props.memo, |
| 230 | dev: props.dev, |
| 231 | }); |
| 232 | if (context.dev && remote !== true) { |
| 233 | return { |
| 234 | url: build.url, |
| 235 | compute: undefined, |
| 236 | project: undefined, |
| 237 | domain: undefined, |
| 238 | } satisfies Website; |
| 239 | } |
| 240 | const requiredPath = (value: string | undefined) => |
| 241 | value === undefined |
| 242 | ? Effect.die( |
| 243 | new Error(`The ${config.framework} Node build produced no deployable server output.`), |
| 244 | ) |
| 245 | : Effect.succeed(value); |
| 246 | const artifact = yield* WebsiteArtifact("Artifact", { |
| 247 | root: props.rootDir ?? ".", |
| 248 | distDir: Output.mapEffect(requiredPath)(build.distDir), |
| 249 | serverEntry: Output.mapEffect(requiredPath)(build.serverEntry), |
| 250 | layout: config.layout, |
| 251 | buildHash: build.hash.output, |
| 252 | }); |
| 253 | return yield* deployWebsite(props, artifact); |
| 254 | }); |