The best way to control Claude Code, Codex and more from any computer or phone

main
ts 116 lines 3.97 KB
Raw
1 import {
2 addressRecords,
3 DnsAdapterError,
4 adapterLayer,
5 isHostnameTarget,
6 normalizeDnsName,
7 type DnsAdapter,
8 type DnsConfig,
9 type DnsRecord,
10 } from "alchemy/DNS/Adapter";
11 import type { Input } from "alchemy/Input";
12 import * as Output from "alchemy/Output";
13 import * as RemovalPolicy from "alchemy/RemovalPolicy";
14 import * as Effect from "effect/Effect";
15 import { RecordList } from "./DnsRecordList.ts";
16 import type { Providers } from "./Providers.ts";
17
18 /** The {@link DnsConfig.type} of the Hetzner DNS adapter. */
19 export const HETZNER_DNS = "Hetzner.DNS";
20
21 export interface AdapterOptions {
22 /**
23 * Zone that owns the records: a zone id, a zone name, or a
24 * `Hetzner.Zone` resource. Inferred from each hostname when omitted.
25 */
26 readonly zone?: Input<string> | { readonly zoneId: Input<number>; readonly name?: Input<string> };
27 }
28
29 /**
30 * Publish a custom domain's DNS records in a Hetzner DNS zone — the default
31 * for `Hetzner.Website.*`, and usable from any other platform (an AWS site,
32 * a Fly or Railway site, a Cloudflare Worker through Cloudflare for SaaS).
33 *
34 * Hostnames get a `CNAME` (or `A`/`AAAA` records for an address target).
35 * Hetzner zones cannot flatten a `CNAME` at the zone apex, so an apex
36 * hostname needs an address target. Requires `Hetzner.providers()` in the
37 * stack. See [DNS Adapters](/infrastructure-as-code/dns-adapters).
38 *
39 * **Example:** AWS site on a Hetzner domain
40 * ```typescript
41 * yield* AWS.Website.StaticSite("Site", {
42 * path: "./dist",
43 * domain: {
44 * name: "www.example.com",
45 * dns: Hetzner.DNS.Adapter({ zone: "example.com" }),
46 * },
47 * });
48 * ```
49 */
50 export const Adapter = (options: AdapterOptions = {}): DnsConfig<typeof HETZNER_DNS> => {
51 const zone =
52 options.zone === undefined || typeof options.zone === "string" || Output.isOutput(options.zone)
53 ? (options.zone as Input<string> | undefined)
54 : (Output.map(
55 (options.zone as { zoneId: Input<number> }).zoneId as Output.Output<number>,
56 (id) => String(id),
57 ) as unknown as Input<string>);
58 return {
59 type: HETZNER_DNS,
60 ...(zone === undefined ? {} : { zone }),
61 };
62 };
63
64 /**
65 * A Hetzner zone can't hold a CNAME at its apex. Only a zone pinned by name
66 * is checkable here — a `Hetzner.Zone` reference is an unresolved Output.
67 */
68 const rejectApexCname = (zone: Input<string> | undefined, name: string) =>
69 typeof zone === "string" && normalizeDnsName(name) === normalizeDnsName(zone)
70 ? Effect.die(
71 new DnsAdapterError({
72 message:
73 `Hetzner DNS cannot publish a CNAME at the zone apex "${name}". ` +
74 "Point the apex at an address target, or use a DNS host that flattens CNAMEs (Cloudflare).",
75 }),
76 )
77 : Effect.void;
78
79 /**
80 * The Hetzner DNS adapter, registered by `Hetzner.providers()`. Every
81 * method declares a `Hetzner.DNS.RecordList`: `alias` → `{id}-CNAME` or
82 * `{id}-Addresses`, `aliasSet` → `{id}-CNAME`, `records` → `id`.
83 */
84 export const adapter = {
85 alias: (id, { zone, name, target }) =>
86 isHostnameTarget(target)
87 ? rejectApexCname(zone, name).pipe(
88 Effect.andThen(
89 RecordList(`${id}-CNAME`, {
90 zone,
91 names: [name],
92 target: target.hostname as string,
93 }),
94 ),
95 )
96 : RecordList(`${id}-Addresses`, {
97 zone,
98 records: addressRecords(name, target.ipv4, target.ipv6) as DnsRecord[],
99 }),
100 aliasSet: (id, { zone, names, target }) =>
101 Effect.forEach(names ?? [], (name) => rejectApexCname(zone, name)).pipe(
102 Effect.andThen(
103 RecordList(`${id}-CNAME`, {
104 zone,
105 names,
106 target: target.hostname as string,
107 }),
108 ),
109 ),
110 records: (id, { zone, records, retain }) =>
111 RecordList(id, { zone, records: records as DnsRecord[] }).pipe(
112 retain === true ? RemovalPolicy.retain() : (effect) => effect,
113 ),
114 } satisfies DnsAdapter<Providers>;
115
116 export const AdapterLive = adapterLayer(HETZNER_DNS, adapter);