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, 06:00 UTC
| 1 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 2 | import type { PackageInstall } from "alchemy/Bundle/InstalledPackages"; |
| 3 | import type { MemoOptions } from "alchemy/Command/Memo"; |
| 4 | import * as DNS from "alchemy/DNS/Adapter"; |
| 5 | import type { Input } from "alchemy/Input"; |
| 6 | import * as Output from "alchemy/Output"; |
| 7 | import { ProviderModePolicy } from "alchemy/ProviderMode"; |
| 8 | import { initialCwd } from "alchemy/Util/Node"; |
| 9 | import { |
| 10 | staticConfigFromAssets, |
| 11 | type WebsiteAssetsProps, |
| 12 | type WebsiteNotFoundHandling, |
| 13 | } from "alchemy/Website/assets"; |
| 14 | import { packSiteExtraFiles } from "alchemy/Website/packExtraFiles"; |
| 15 | import { Server as FrameworkServer, type ServerDevProps } from "alchemy/Website/Server"; |
| 16 | import * as Data from "effect/Data"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as FileSystem from "effect/FileSystem"; |
| 19 | import * as Path from "effect/Path"; |
| 20 | import * as Redacted from "effect/Redacted"; |
| 21 | import { CustomDomain } from "../CustomDomain.ts"; |
| 22 | import type { ExtraFile } from "../hosted.ts"; |
| 23 | import { Project, type Project as ProjectResource } from "../Project.ts"; |
| 24 | import type { Environment as EnvironmentResource } from "../ProjectEnvironment.ts"; |
| 25 | import type { Providers } from "../Providers.ts"; |
| 26 | import { Service, type Service as ServiceResource } from "../Service.ts"; |
| 27 | import { Cdn } from "./Cdn.ts"; |
| 28 | |
| 29 | /** Port the generated Node serve entry and Railway Service listen on. */ |
| 30 | export const WEBSITE_PORT = 3000; |
| 31 | |
| 32 | /** |
| 33 | * A resource-valued prop: the resource itself, or an Effect that produces |
| 34 | * it (so `yield* Project(...)` and `Project(...)` both type-check). |
| 35 | */ |
| 36 | export type Ref<T> = T | Effect.Effect<T, never, Providers>; |
| 37 | |
| 38 | export type { ServerDevProps, WebsiteAssetsProps, WebsiteNotFoundHandling }; |
| 39 | export { staticConfigFromAssets }; |
| 40 | |
| 41 | /** |
| 42 | * Custom hostname of a Railway website: the hostname itself, or |
| 43 | * `{ name, dns }` to also publish its DNS records. |
| 44 | */ |
| 45 | export type WebsiteDomain = |
| 46 | | string |
| 47 | | { |
| 48 | /** Hostname, e.g. `www.example.com`. */ |
| 49 | name: string; |
| 50 | /** |
| 51 | * DNS host that publishes the records Railway requires (the routing |
| 52 | * `CNAME` and the ownership `TXT` listed in the CustomDomain's |
| 53 | * `dnsRecords`; see |
| 54 | * [DNS Adapters](/infrastructure-as-code/dns-adapters)), e.g. |
| 55 | * `Cloudflare.DNS.Adapter()`. Omitted: publish them yourself. |
| 56 | */ |
| 57 | dns?: DNS.DnsConfig; |
| 58 | }; |
| 59 | |
| 60 | /** The hostname of a {@link WebsiteDomain}. */ |
| 61 | export const websiteDomainName = (domain: WebsiteDomain | undefined): string | undefined => |
| 62 | domain === undefined || typeof domain === "string" ? domain : domain.name; |
| 63 | |
| 64 | /** |
| 65 | * Publish the records Railway requires for a website's custom domain |
| 66 | * (`DomainRecords`) through `domain.dns`. No-op for a plain hostname or an |
| 67 | * object without `dns`. |
| 68 | */ |
| 69 | export const publishWebsiteDomainDns = Effect.fn("Railway.Website.publishDomainDns")( |
| 70 | function* (args: { |
| 71 | readonly domain: WebsiteDomain | undefined; |
| 72 | readonly customDomain: CustomDomain; |
| 73 | }) { |
| 74 | if (typeof args.domain !== "object" || args.domain.dns === undefined) { |
| 75 | return; |
| 76 | } |
| 77 | const dns = yield* DNS.resolve(args.domain.dns); |
| 78 | yield* dns.records("DomainRecords", { |
| 79 | records: Output.map(args.customDomain.dnsRecords, (records) => |
| 80 | records.map((record): DNS.DnsRecord => ({ |
| 81 | name: record.name, |
| 82 | type: record.type, |
| 83 | value: record.value, |
| 84 | })), |
| 85 | ) as unknown as Input<DNS.DnsRecord[]>, |
| 86 | }); |
| 87 | }, |
| 88 | ); |
| 89 | |
| 90 | /** |
| 91 | * Props shared by every Railway framework website composite. |
| 92 | */ |
| 93 | export interface FrameworkSiteProps { |
| 94 | /** |
| 95 | * Parent Railway Project. Accepts a `Railway.Project` or an Effect |
| 96 | * that produces one. When omitted, a `Railway.Project("Project")` is |
| 97 | * created under this site's namespace. |
| 98 | */ |
| 99 | project?: Ref<ProjectResource>; |
| 100 | /** |
| 101 | * Environment to deploy the Service into. Accepts a `Railway.Project` |
| 102 | * (primary environment), a `Railway.Environment`, or `{ environmentId }`. |
| 103 | * Defaults to the project's primary environment. |
| 104 | */ |
| 105 | environment?: Ref<EnvironmentResource | ProjectResource | { readonly environmentId: string }>; |
| 106 | /** |
| 107 | * Project root directory (the directory containing `package.json`). |
| 108 | * @default "." |
| 109 | */ |
| 110 | rootDir?: string; |
| 111 | /** |
| 112 | * Controls which files are hashed to decide whether the build re-runs. |
| 113 | * Forwarded to the framework integration when it honors memo options. |
| 114 | * @default true |
| 115 | */ |
| 116 | memo?: MemoOptions | boolean; |
| 117 | /** |
| 118 | * Process environment for the deployed Service (and, under |
| 119 | * `alchemy dev`, the framework dev server). Accepts `Output`s |
| 120 | * (e.g. `VITE_API_URL: api.url`). |
| 121 | */ |
| 122 | env?: Record<string, string | Redacted.Redacted<string> | Output.Output<string | undefined>>; |
| 123 | /** |
| 124 | * Static-asset routing (`notFoundHandling`, `htmlHandling`). Railway |
| 125 | * CDN caches hashed files by Content-Type regardless of this bag. |
| 126 | */ |
| 127 | assets?: WebsiteAssetsProps; |
| 128 | /** |
| 129 | * Options for the local dev server that runs this site under |
| 130 | * `alchemy dev`. |
| 131 | */ |
| 132 | dev?: ServerDevProps; |
| 133 | /** |
| 134 | * Optional custom hostname attached via `Railway.CustomDomain`. When |
| 135 | * set, `url` is `https://{domain}` instead of the generated |
| 136 | * `*.up.railway.app`. |
| 137 | * |
| 138 | * A string (or `{ name }`) leaves DNS to you. Set `dns` (e.g. |
| 139 | * `{ name: "www.example.com", dns: Cloudflare.DNS.Adapter() }`) to |
| 140 | * publish the records Railway requires through that DNS host. See |
| 141 | * [DNS Adapters](/infrastructure-as-code/dns-adapters). |
| 142 | */ |
| 143 | domain?: WebsiteDomain; |
| 144 | /** |
| 145 | * User-defined tags. Railway Services do not persist tags; accepted |
| 146 | * for API parity with AWS/Cloudflare Website composites. |
| 147 | */ |
| 148 | tags?: Record<string, string>; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Static-asset serving options mapped from Cloudflare `AssetsConfig` |
| 153 | * onto the generated Node serve entry. |
| 154 | */ |
| 155 | export interface WebsiteStaticConfig { |
| 156 | /** |
| 157 | * Answer misses with `index.html` (200) so client-side routes |
| 158 | * deep-link. Mutually exclusive with {@link errorPage}. |
| 159 | */ |
| 160 | spa?: boolean; |
| 161 | /** |
| 162 | * Serve this page (e.g. `404.html`) with status 404 when no file |
| 163 | * matches. Mutually exclusive with {@link spa}. |
| 164 | */ |
| 165 | errorPage?: string; |
| 166 | /** |
| 167 | * Vocs/Waku: serve `about/index.html` at `/about`. |
| 168 | * @default "none" |
| 169 | */ |
| 170 | htmlHandling?: "none" | "drop-trailing-slash"; |
| 171 | } |
| 172 | |
| 173 | /** Per-framework wiring for {@link makeFrameworkSite}. */ |
| 174 | export interface FrameworkSiteConfig { |
| 175 | /** Display name used in error messages (e.g. `"SvelteKit"`). */ |
| 176 | name: string; |
| 177 | /** Framework-integration module specifier. */ |
| 178 | framework: string; |
| 179 | /** Node deploy-target module specifier. */ |
| 180 | target: string; |
| 181 | /** |
| 182 | * Framework-specific build options forwarded to the integration (e.g. |
| 183 | * `{ kit }`, `{ nuxt }`, `{ astro }`). Must be JSON-serializable. |
| 184 | */ |
| 185 | options?: Record<string, unknown> | undefined; |
| 186 | /** |
| 187 | * Assets-only serving: used when the build produced no server modules |
| 188 | * (Vite, Foldkit, Vocs, Astro `output: "static"`). |
| 189 | */ |
| 190 | static?: WebsiteStaticConfig | undefined; |
| 191 | /** |
| 192 | * Native packages to `npm install` into the image instead of bundling |
| 193 | * (Next.js needs `next` / `react`). |
| 194 | */ |
| 195 | install?: PackageInstall | undefined; |
| 196 | /** |
| 197 | * How to bake the build into the image. |
| 198 | * - `"client"` (default): `COPY dist /app/dist` from `clientDirectory` |
| 199 | * - `"next"`: `COPY .next` + `public` (and `next.config.*`) |
| 200 | * @default "client" |
| 201 | */ |
| 202 | bake?: "client" | "next" | undefined; |
| 203 | } |
| 204 | |
| 205 | export interface Website { |
| 206 | /** |
| 207 | * Public site URL. Under `alchemy dev` this is the framework (or |
| 208 | * static) dev server (`http://localhost:<port>`). On deploy it is |
| 209 | * `https://{domain}` (`*.up.railway.app`, or the custom hostname). |
| 210 | */ |
| 211 | url: string | Output.Output<string | undefined> | undefined; |
| 212 | /** |
| 213 | * The Railway Service that serves the site. `undefined` under |
| 214 | * `alchemy dev` (no cloud resources are declared). |
| 215 | */ |
| 216 | service: ServiceResource | undefined; |
| 217 | /** |
| 218 | * The Railway Project the Service belongs to. `undefined` under |
| 219 | * `alchemy dev`. |
| 220 | */ |
| 221 | project: ProjectResource | undefined; |
| 222 | } |
| 223 | |
| 224 | export class FrameworkServerError extends Data.TaggedError("Railway.Website.FrameworkServerError")<{ |
| 225 | readonly framework: string; |
| 226 | readonly message: string; |
| 227 | readonly cause?: unknown; |
| 228 | }> {} |
| 229 | |
| 230 | const envRecord = ( |
| 231 | env: |
| 232 | | Record<string, string | Redacted.Redacted<string> | Output.Output<string | undefined>> |
| 233 | | undefined, |
| 234 | ): Record<string, string | Output.Output<string | undefined>> | undefined => { |
| 235 | if (env === undefined) return undefined; |
| 236 | return Object.fromEntries( |
| 237 | Object.entries(env).map(([key, value]) => [ |
| 238 | key, |
| 239 | Redacted.isRedacted(value) ? Redacted.value(value) : value, |
| 240 | ]), |
| 241 | ); |
| 242 | }; |
| 243 | |
| 244 | /** |
| 245 | * Shared implementation behind the Railway framework website composites: |
| 246 | * build the framework through its Node deploy target, then deploy one |
| 247 | * `Railway.Service` whose image bakes the Node serve entry plus |
| 248 | * `clientDirectory`. |
| 249 | * |
| 250 | * During `alchemy dev` the site is the framework's own dev server (native |
| 251 | * HMR) and no cloud resources are declared; `Alchemy.remote()` opts back |
| 252 | * into the live Service path. |
| 253 | * |
| 254 | * Callers pipe `Namespace.push(id)` themselves (the composites do). |
| 255 | */ |
| 256 | const runFrameworkSite = Effect.fn("Railway.Website.FrameworkSite")(function* ( |
| 257 | _id: string, |
| 258 | props: FrameworkSiteProps, |
| 259 | config: FrameworkSiteConfig, |
| 260 | ) { |
| 261 | const ctx = yield* AlchemyContext; |
| 262 | const remoted = yield* ProviderModePolicy; |
| 263 | const isLocal = ctx.dev && remoted !== true; |
| 264 | const path = yield* Path.Path; |
| 265 | const fs = yield* FileSystem.FileSystem; |
| 266 | const root = path.resolve(initialCwd, props.rootDir ?? "."); |
| 267 | const bake = config.bake ?? "client"; |
| 268 | |
| 269 | const build = yield* FrameworkServer("Build", { |
| 270 | framework: config.framework, |
| 271 | target: config.target, |
| 272 | root: props.rootDir, |
| 273 | env: envRecord(props.env), |
| 274 | options: config.options, |
| 275 | memo: props.memo, |
| 276 | dev: props.dev, |
| 277 | }); |
| 278 | |
| 279 | if (isLocal) { |
| 280 | return { |
| 281 | url: build.url, |
| 282 | service: undefined, |
| 283 | project: undefined, |
| 284 | } satisfies Website; |
| 285 | } |
| 286 | |
| 287 | // The build runs at APPLY time (`Website.Server` is a resource), so its |
| 288 | // attributes are Outputs here — derive every deploy input lazily and let |
| 289 | // the engine resolve them once the build has produced real paths. Reading |
| 290 | // `build.distDir` eagerly in the composite body would observe an Output |
| 291 | // proxy, not a string. |
| 292 | const buildOut = Output.mapEffect( |
| 293 | ([serverEntry, distDir]: [string | undefined, string | undefined]) => |
| 294 | Effect.gen(function* () { |
| 295 | if (serverEntry === undefined || distDir === undefined) { |
| 296 | return yield* Effect.die( |
| 297 | new FrameworkServerError({ |
| 298 | framework: config.framework, |
| 299 | message: `The ${config.name} build produced no Node serve entry (serverModules[0]). The Node deploy target should write serve-node.mjs.`, |
| 300 | }), |
| 301 | ); |
| 302 | } |
| 303 | const main = path.resolve(initialCwd, serverEntry); |
| 304 | if (!(yield* fs.exists(main).pipe(Effect.orElseSucceed(() => false)))) { |
| 305 | return yield* Effect.die( |
| 306 | new FrameworkServerError({ |
| 307 | framework: config.framework, |
| 308 | message: `The ${config.name} build produced no server entry at ${main}`, |
| 309 | }), |
| 310 | ); |
| 311 | } |
| 312 | return { distDir: path.resolve(initialCwd, distDir), main }; |
| 313 | }), |
| 314 | )( |
| 315 | Output.all( |
| 316 | build.serverEntry as unknown as Output.Output<string | undefined>, |
| 317 | build.distDir as unknown as Output.Output<string | undefined>, |
| 318 | ) as unknown as Output.Output<[string | undefined, string | undefined]>, |
| 319 | ); |
| 320 | const main = Output.map(buildOut, (out) => out.main); |
| 321 | |
| 322 | // `bake === "next"` ships `.next`/`public`/`next.config.*` from the |
| 323 | // project root; every other bake ships the build's dist directory. BOTH |
| 324 | // must be derived from `buildOut`: the artifacts only exist once the |
| 325 | // build has run at apply — probing the root here (pre-build) would miss |
| 326 | // a fresh project's `.next` entirely. |
| 327 | const extraFiles = Output.mapEffect((out: { distDir: string; main: string }) => |
| 328 | packSiteExtraFiles(bake === "next" ? root : out.distDir, bake), |
| 329 | )(buildOut); |
| 330 | |
| 331 | const project = Effect.isEffect(props.project) |
| 332 | ? yield* props.project |
| 333 | : (props.project ?? (yield* Project("Project"))); |
| 334 | const environment = Effect.isEffect(props.environment) |
| 335 | ? yield* props.environment |
| 336 | : (props.environment ?? project); |
| 337 | const service = yield* Service("Service", { |
| 338 | project, |
| 339 | environment, |
| 340 | main: main as unknown as string, |
| 341 | port: WEBSITE_PORT, |
| 342 | healthcheck: "/health", |
| 343 | isExternal: true, |
| 344 | env: envRecord(props.env), |
| 345 | extraFiles: extraFiles as unknown as ExtraFile[] | undefined, |
| 346 | build: config.install !== undefined ? { install: config.install } : undefined, |
| 347 | }); |
| 348 | yield* Cdn("Cdn", { |
| 349 | service, |
| 350 | environment, |
| 351 | htmlCaching: "AUTO", |
| 352 | purgeOnDeploy: "HTML", |
| 353 | }); |
| 354 | |
| 355 | const domainName = websiteDomainName(props.domain); |
| 356 | if (domainName !== undefined && domainName.length > 0) { |
| 357 | const customDomain = yield* CustomDomain("Domain", { |
| 358 | service, |
| 359 | environment, |
| 360 | domain: domainName, |
| 361 | targetPort: WEBSITE_PORT, |
| 362 | }); |
| 363 | yield* publishWebsiteDomainDns({ domain: props.domain, customDomain }); |
| 364 | return { |
| 365 | url: `https://${domainName}`, |
| 366 | service, |
| 367 | project, |
| 368 | } satisfies Website; |
| 369 | } |
| 370 | |
| 371 | return { |
| 372 | url: service.url, |
| 373 | service, |
| 374 | project, |
| 375 | } satisfies Website; |
| 376 | }); |
| 377 | |
| 378 | /** |
| 379 | * Composite-level tagged errors (`FrameworkServerError`, filesystem) |
| 380 | * are defects — `Alchemy.Stack` only admits `ConfigError` on the user |
| 381 | * effect. |
| 382 | */ |
| 383 | export const makeFrameworkSite = ( |
| 384 | id: string, |
| 385 | props: FrameworkSiteProps, |
| 386 | config: FrameworkSiteConfig, |
| 387 | ) => runFrameworkSite(id, props, config).pipe(Effect.orDie); |