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 { MemoOptions } from "alchemy/Command/Memo"; |
| 3 | import * as DNS from "alchemy/DNS/Adapter"; |
| 4 | import type { Input } from "alchemy/Input"; |
| 5 | import * as Output from "alchemy/Output"; |
| 6 | import { ProviderModePolicy } from "alchemy/ProviderMode"; |
| 7 | import { initialCwd } from "alchemy/Util/Node"; |
| 8 | import { |
| 9 | staticConfigFromAssets, |
| 10 | type WebsiteAssetsProps, |
| 11 | type WebsiteNotFoundHandling, |
| 12 | } from "alchemy/Website/assets"; |
| 13 | import { packSiteExtraFiles } from "alchemy/Website/packExtraFiles"; |
| 14 | import { Server as FrameworkServer, type ServerDevProps } from "alchemy/Website/Server"; |
| 15 | import * as Data from "effect/Data"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as FileSystem from "effect/FileSystem"; |
| 18 | import * as Path from "effect/Path"; |
| 19 | import * as Redacted from "effect/Redacted"; |
| 20 | import type { Providers } from "../Providers.ts"; |
| 21 | import { RecordSet } from "../RecordSet.ts"; |
| 22 | import { Server } from "../Server.ts"; |
| 23 | import { Service } from "../Service.ts"; |
| 24 | import type { Zone } from "../Zone.ts"; |
| 25 | |
| 26 | /** |
| 27 | * A resource-valued prop: the resource itself, or an Effect that produces |
| 28 | * it (so `yield* Server(...)` and `Server(...)` both type-check). |
| 29 | */ |
| 30 | export type Ref<T> = T | Effect.Effect<T, never, Providers>; |
| 31 | |
| 32 | const resolveRef = <T>(ref: Ref<T>): Effect.Effect<T, never, Providers> => |
| 33 | Effect.isEffect(ref) ? ref : Effect.succeed(ref); |
| 34 | |
| 35 | /** Default listen port. Hetzner `deployUnit` curls `/health` whenever `PORT` is set. */ |
| 36 | export const DEFAULT_WEBSITE_PORT = 3000; |
| 37 | |
| 38 | export type { ServerDevProps, WebsiteAssetsProps, WebsiteNotFoundHandling }; |
| 39 | export { staticConfigFromAssets }; |
| 40 | |
| 41 | /** |
| 42 | * Custom domain of a Hetzner website: the hostname itself (an `A` |
| 43 | * {@link RecordSet} in {@link FrameworkSiteProps.zone}), or `{ name, dns }` |
| 44 | * to publish the record through a DNS adapter instead. |
| 45 | */ |
| 46 | export type WebsiteDomain = |
| 47 | | string |
| 48 | | { |
| 49 | /** Hostname, e.g. `app.example.com`. */ |
| 50 | name: string; |
| 51 | /** |
| 52 | * DNS host that publishes the `A` record at the Server's public |
| 53 | * IPv4 (see [DNS Adapters](/infrastructure-as-code/dns-adapters)), |
| 54 | * e.g. `Hetzner.DNS.Adapter()` or `Cloudflare.DNS.Adapter()`. When |
| 55 | * set, `zone` is not required (and is ignored). Omitted: the `A` |
| 56 | * {@link RecordSet} in `zone`. |
| 57 | */ |
| 58 | dns?: DNS.DnsConfig; |
| 59 | }; |
| 60 | |
| 61 | /** The hostname of a {@link WebsiteDomain}. */ |
| 62 | export const websiteDomainName = (domain: WebsiteDomain | undefined): string | undefined => |
| 63 | domain === undefined || typeof domain === "string" ? domain : domain.name; |
| 64 | |
| 65 | /** The DNS adapter config of a {@link WebsiteDomain}, when it names one. */ |
| 66 | const websiteDomainDns = (domain: WebsiteDomain | undefined): DNS.DnsConfig | undefined => |
| 67 | typeof domain === "object" ? domain.dns : undefined; |
| 68 | |
| 69 | /** |
| 70 | * Dies unless a custom domain can be published: `domain.dns` names a DNS |
| 71 | * host, or `zone` is set for the `A` {@link RecordSet}. |
| 72 | */ |
| 73 | export const requireWebsiteDomainZone = ( |
| 74 | label: string, |
| 75 | props: { |
| 76 | readonly domain?: WebsiteDomain | undefined; |
| 77 | readonly zone?: Ref<Zone> | undefined; |
| 78 | }, |
| 79 | ): Effect.Effect<void> => |
| 80 | props.domain !== undefined && |
| 81 | websiteDomainDns(props.domain) === undefined && |
| 82 | props.zone === undefined |
| 83 | ? Effect.die( |
| 84 | `${label}: "domain" requires "zone" (an existing Hetzner.Zone) or "domain.dns" (a DNS adapter).`, |
| 85 | ) |
| 86 | : Effect.void; |
| 87 | |
| 88 | /** |
| 89 | * Point a website's custom domain at its Server: through `domain.dns` when |
| 90 | * set (`Domain` alias, an address target), otherwise an `A` |
| 91 | * {@link RecordSet} named `Domain` in `zone` (unchanged from before DNS |
| 92 | * adapters existed). |
| 93 | */ |
| 94 | export const publishWebsiteDomain = Effect.fn("Hetzner.Website.publishDomain")(function* (props: { |
| 95 | readonly domain?: WebsiteDomain | undefined; |
| 96 | readonly zone?: Ref<Zone> | undefined; |
| 97 | readonly server: Server; |
| 98 | readonly tags?: Record<string, string> | undefined; |
| 99 | }) { |
| 100 | const name = websiteDomainName(props.domain); |
| 101 | if (name === undefined) return; |
| 102 | const dnsConfig = websiteDomainDns(props.domain); |
| 103 | if (dnsConfig !== undefined) { |
| 104 | const dns = yield* DNS.resolve(dnsConfig); |
| 105 | yield* dns.alias("Domain", { |
| 106 | name, |
| 107 | target: { |
| 108 | ipv4: Output.map(props.server.ipv4, (ip) => |
| 109 | ip === undefined ? [] : [ip], |
| 110 | ) as unknown as Input<string[]>, |
| 111 | }, |
| 112 | }); |
| 113 | return; |
| 114 | } |
| 115 | if (props.zone !== undefined) { |
| 116 | yield* bindWebsiteDomain({ |
| 117 | domain: name, |
| 118 | zone: props.zone, |
| 119 | server: props.server, |
| 120 | tags: props.tags, |
| 121 | }); |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | /** |
| 126 | * Props shared by every Hetzner framework website composite. |
| 127 | */ |
| 128 | export interface FrameworkSiteProps { |
| 129 | /** |
| 130 | * Project root directory (the directory containing `package.json`). |
| 131 | * @default "." |
| 132 | */ |
| 133 | rootDir?: string; |
| 134 | /** |
| 135 | * Controls which files are hashed to decide whether the build re-runs. |
| 136 | * @default true |
| 137 | */ |
| 138 | memo?: MemoOptions | boolean; |
| 139 | /** |
| 140 | * Options for the local dev server that runs this site under |
| 141 | * `alchemy dev`. |
| 142 | */ |
| 143 | dev?: ServerDevProps; |
| 144 | /** |
| 145 | * Process environment for the hosted unit (and the local framework |
| 146 | * dev server). Not Cloudflare Worker bindings. Accepts `Output`s |
| 147 | * (e.g. `VITE_API_URL: api.url`). |
| 148 | */ |
| 149 | env?: Record<string, string | Redacted.Redacted<string> | Output.Output<string | undefined>>; |
| 150 | /** |
| 151 | * Static-asset routing (`notFoundHandling`, `htmlHandling`). |
| 152 | */ |
| 153 | assets?: WebsiteAssetsProps; |
| 154 | /** |
| 155 | * Optional custom domain pointed at the Server's public IPv4. The site |
| 156 | * `url` becomes `http://{domain}:{port}` (no TLS on Service). |
| 157 | * |
| 158 | * A string (or `{ name }`) creates an `A` {@link RecordSet} in |
| 159 | * {@link zone}. Set `dns` (e.g. |
| 160 | * `{ name: "app.example.com", dns: Cloudflare.DNS.Adapter() }`) to |
| 161 | * publish the `A` record through that DNS host instead — any adapter |
| 162 | * works, including `Hetzner.DNS.Adapter()` and `AWS.Route53.Adapter()`. |
| 163 | * See [DNS Adapters](/infrastructure-as-code/dns-adapters). |
| 164 | */ |
| 165 | domain?: WebsiteDomain; |
| 166 | /** |
| 167 | * Existing Hetzner DNS Zone `domain` is created in. Required when |
| 168 | * {@link domain} is set without `dns` — v1 does not provision a Zone. |
| 169 | * Ignored when `domain.dns` is set. |
| 170 | */ |
| 171 | zone?: Ref<Zone>; |
| 172 | /** |
| 173 | * Server the site's Service runs on. Accepts a `Hetzner.Server` or an |
| 174 | * Effect that produces one. When omitted, a `cpx12` / `ubuntu-24.04` |
| 175 | * Server is created in `fsn1` (public IPv4 is the Server default). |
| 176 | */ |
| 177 | server?: Ref<Server>; |
| 178 | /** |
| 179 | * User-defined labels applied to auto-created Server / RecordSet. |
| 180 | */ |
| 181 | tags?: Record<string, string>; |
| 182 | } |
| 183 | |
| 184 | /** Per-framework wiring for {@link makeFrameworkSite}. */ |
| 185 | export interface FrameworkSiteConfig { |
| 186 | /** Display name used in error messages (e.g. `"SvelteKit"`). */ |
| 187 | name: string; |
| 188 | /** Framework-integration module specifier. */ |
| 189 | framework: string; |
| 190 | /** Node container deploy-target module specifier. */ |
| 191 | target: string; |
| 192 | /** |
| 193 | * Framework-specific build options forwarded to the integration (e.g. |
| 194 | * `{ kit }`, `{ nuxt }`, `{ astro }`). Must be JSON-serializable. |
| 195 | */ |
| 196 | options?: Record<string, unknown> | undefined; |
| 197 | /** |
| 198 | * Assets-only mode: no server modules (or every page prerendered). The |
| 199 | * composite generates a static-file server as `main`. |
| 200 | */ |
| 201 | static?: |
| 202 | | { |
| 203 | spa?: boolean | undefined; |
| 204 | errorPage?: string | undefined; |
| 205 | htmlHandling?: "none" | "drop-trailing-slash" | undefined; |
| 206 | } |
| 207 | | undefined; |
| 208 | /** |
| 209 | * Vocs/Waku: serve `about/index.html` at `/about`. |
| 210 | * @default "none" |
| 211 | */ |
| 212 | htmlHandling?: "none" | "drop-trailing-slash"; |
| 213 | /** |
| 214 | * Skip baking `clientDirectory` at `dist/`. Next.js serves `.next` |
| 215 | * from the unit root instead. |
| 216 | */ |
| 217 | skipClientAssets?: boolean | undefined; |
| 218 | /** |
| 219 | * Native packages to `npm install` into the unit instead of bundling |
| 220 | * (Next.js needs `next` / `react` / `react-dom`). |
| 221 | */ |
| 222 | install?: string[] | undefined; |
| 223 | } |
| 224 | |
| 225 | export interface Website { |
| 226 | /** |
| 227 | * Local framework URL under `alchemy dev`, or the live Service URL |
| 228 | * (`http://{ipv4}:{port}` / `http://{domain}:{port}`). |
| 229 | */ |
| 230 | readonly url: string | Output.Output<string | undefined> | undefined; |
| 231 | /** Server the unit runs on. `undefined` during `alchemy dev`. */ |
| 232 | readonly server: Server | undefined; |
| 233 | /** Hosted systemd unit. `undefined` during `alchemy dev`. */ |
| 234 | readonly service: Service | undefined; |
| 235 | } |
| 236 | |
| 237 | export class FrameworkSiteError extends Data.TaggedError("Hetzner.Website.FrameworkSiteError")<{ |
| 238 | readonly framework: string; |
| 239 | readonly message: string; |
| 240 | readonly cause?: unknown; |
| 241 | }> {} |
| 242 | |
| 243 | export const unwrapEnv = ( |
| 244 | env: |
| 245 | | Record<string, string | Redacted.Redacted<string> | Output.Output<string | undefined>> |
| 246 | | undefined, |
| 247 | ): Record<string, string | Output.Output<string | undefined>> | undefined => { |
| 248 | if (env === undefined) return undefined; |
| 249 | return Object.fromEntries( |
| 250 | Object.entries(env).map(([key, value]) => [ |
| 251 | key, |
| 252 | Redacted.isRedacted(value) ? Redacted.value(value) : value, |
| 253 | ]), |
| 254 | ); |
| 255 | }; |
| 256 | |
| 257 | export const resolveWebsiteServer = Effect.fn(function* (props: { |
| 258 | readonly server?: Ref<Server> | undefined; |
| 259 | readonly tags?: Record<string, string> | undefined; |
| 260 | }) { |
| 261 | if (props.server !== undefined) { |
| 262 | return yield* resolveRef(props.server); |
| 263 | } |
| 264 | return yield* Server("Server", { |
| 265 | serverType: "cpx12", |
| 266 | image: "ubuntu-24.04", |
| 267 | location: "fsn1", |
| 268 | labels: props.tags, |
| 269 | }); |
| 270 | }); |
| 271 | |
| 272 | export const bindWebsiteDomain = Effect.fn(function* (props: { |
| 273 | readonly domain: string; |
| 274 | readonly zone: Ref<Zone>; |
| 275 | readonly server: Server; |
| 276 | readonly tags?: Record<string, string> | undefined; |
| 277 | }) { |
| 278 | const zone = yield* resolveRef(props.zone); |
| 279 | const name = Output.map((apex: string | undefined) => { |
| 280 | const domain = props.domain; |
| 281 | if (apex === undefined || domain === apex) return "@"; |
| 282 | const suffix = `.${apex}`; |
| 283 | if (domain.endsWith(suffix)) return domain.slice(0, -suffix.length); |
| 284 | throw new Error(`Hetzner.Website domain "${domain}" is not inside zone "${apex}"`); |
| 285 | })(zone.name as never); |
| 286 | yield* RecordSet("Domain", { |
| 287 | zone, |
| 288 | name: name as never, |
| 289 | type: "A", |
| 290 | records: [{ value: props.server.ipv4 as never }], |
| 291 | labels: props.tags, |
| 292 | }); |
| 293 | }); |
| 294 | |
| 295 | export const websiteUrl = (args: { |
| 296 | readonly domain?: string | undefined; |
| 297 | readonly service: Service; |
| 298 | readonly port: number; |
| 299 | }) => (args.domain !== undefined ? `http://${args.domain}:${String(args.port)}` : args.service.url); |
| 300 | |
| 301 | /** |
| 302 | * Shared implementation behind the Hetzner framework website composites: |
| 303 | * build through the Node deploy target, then host one Service on a |
| 304 | * Hetzner Server (auto-created `cpx12` in `fsn1` when `server` is omitted). |
| 305 | * |
| 306 | * During `alchemy dev` the site is the framework's own dev server and no |
| 307 | * cloud resources are declared; `Alchemy.remote()` opts back into the |
| 308 | * live Service path. |
| 309 | */ |
| 310 | const runFrameworkSite = Effect.fn("Hetzner.Website.FrameworkSite")(function* ( |
| 311 | id: string, |
| 312 | props: FrameworkSiteProps, |
| 313 | config: FrameworkSiteConfig, |
| 314 | ) { |
| 315 | const ctx = yield* AlchemyContext; |
| 316 | const remoted = yield* ProviderModePolicy; |
| 317 | const isLocal = ctx.dev && remoted !== true; |
| 318 | const path = yield* Path.Path; |
| 319 | const fs = yield* FileSystem.FileSystem; |
| 320 | const port = DEFAULT_WEBSITE_PORT; |
| 321 | |
| 322 | if (config.static?.spa && config.static.errorPage) { |
| 323 | return yield* Effect.die( |
| 324 | `Cannot provide both "spa" and "errorPage". A SPA answers misses with the index page (200); "errorPage" answers them with a real 404.`, |
| 325 | ); |
| 326 | } |
| 327 | yield* requireWebsiteDomainZone(`Hetzner.Website "${config.name}"`, props); |
| 328 | |
| 329 | const build = yield* FrameworkServer("Build", { |
| 330 | framework: config.framework, |
| 331 | target: config.target, |
| 332 | root: props.rootDir, |
| 333 | env: unwrapEnv(props.env), |
| 334 | options: config.options, |
| 335 | memo: props.memo, |
| 336 | dev: props.dev, |
| 337 | }); |
| 338 | |
| 339 | if (isLocal) { |
| 340 | return { |
| 341 | url: build.url, |
| 342 | server: undefined, |
| 343 | service: undefined, |
| 344 | }; |
| 345 | } |
| 346 | |
| 347 | // The build runs at APPLY time (`Website.Server` is a resource), so its |
| 348 | // attributes are Outputs here — derive every deploy input lazily and let |
| 349 | // the engine resolve them once the build has produced real paths. Reading |
| 350 | // `build.distDir` eagerly in the composite body would observe an Output |
| 351 | // proxy, not a string. |
| 352 | const buildOut = Output.mapEffect( |
| 353 | ([serverEntry, distDir]: [string | undefined, string | undefined]) => |
| 354 | Effect.gen(function* () { |
| 355 | if (serverEntry === undefined || distDir === undefined) { |
| 356 | return yield* Effect.die( |
| 357 | new FrameworkSiteError({ |
| 358 | framework: config.framework, |
| 359 | message: `The ${config.name} build produced no Node serve entry (serverModules[0]). The Node deploy target should write serve-node.mjs.`, |
| 360 | }), |
| 361 | ); |
| 362 | } |
| 363 | const main = path.resolve(initialCwd, serverEntry); |
| 364 | if (!(yield* fs.exists(main).pipe(Effect.orElseSucceed(() => false)))) { |
| 365 | return yield* Effect.die( |
| 366 | new FrameworkSiteError({ |
| 367 | framework: config.framework, |
| 368 | message: `The ${config.name} build produced no server entry at ${main}`, |
| 369 | }), |
| 370 | ); |
| 371 | } |
| 372 | return { distDir: path.resolve(initialCwd, distDir), main }; |
| 373 | }), |
| 374 | )( |
| 375 | Output.all( |
| 376 | build.serverEntry as unknown as Output.Output<string | undefined>, |
| 377 | build.distDir as unknown as Output.Output<string | undefined>, |
| 378 | ) as unknown as Output.Output<[string | undefined, string | undefined]>, |
| 379 | ); |
| 380 | const main = Output.map(buildOut, (out) => out.main); |
| 381 | |
| 382 | const server = yield* resolveWebsiteServer(props); |
| 383 | const env = { |
| 384 | ...unwrapEnv(props.env), |
| 385 | PORT: String(port), |
| 386 | }; |
| 387 | |
| 388 | const extraFiles = Output.mapEffect((out: { distDir: string; main: string }) => |
| 389 | packSiteExtraFiles(out.distDir, config.skipClientAssets === true ? "next" : "client").pipe( |
| 390 | Effect.map((files) => |
| 391 | files?.map((file) => ({ |
| 392 | source: file.source, |
| 393 | destination: file.dest, |
| 394 | })), |
| 395 | ), |
| 396 | ), |
| 397 | )(buildOut); |
| 398 | |
| 399 | const service = yield* Service("Service", { |
| 400 | server, |
| 401 | main: main as unknown as string, |
| 402 | extraFiles: extraFiles as unknown as Array<{ |
| 403 | source: string; |
| 404 | destination: string; |
| 405 | }>, |
| 406 | port, |
| 407 | env, |
| 408 | isExternal: true, |
| 409 | build: |
| 410 | config.install !== undefined && config.install.length > 0 |
| 411 | ? { install: config.install } |
| 412 | : undefined, |
| 413 | }); |
| 414 | |
| 415 | yield* publishWebsiteDomain({ |
| 416 | domain: props.domain, |
| 417 | zone: props.zone, |
| 418 | server, |
| 419 | tags: props.tags, |
| 420 | }); |
| 421 | |
| 422 | return { |
| 423 | url: websiteUrl({ |
| 424 | domain: websiteDomainName(props.domain), |
| 425 | service, |
| 426 | port, |
| 427 | }), |
| 428 | server, |
| 429 | service, |
| 430 | }; |
| 431 | }); |
| 432 | |
| 433 | /** |
| 434 | * Composite-level tagged errors (`FrameworkSiteError`, filesystem) are |
| 435 | * defects — `Alchemy.Stack` only admits `ConfigError` on the user effect. |
| 436 | */ |
| 437 | export const makeFrameworkSite = ( |
| 438 | id: string, |
| 439 | props: FrameworkSiteProps, |
| 440 | config: FrameworkSiteConfig, |
| 441 | ) => runFrameworkSite(id, props, config).pipe(Effect.orDie); |