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

main
ts 120 lines 4.11 KB
Raw
1 import {
2 addressRecords,
3 adapterLayer,
4 isHostnameTarget,
5 type DnsAdapter,
6 type DnsConfig,
7 type DnsHostnameTarget,
8 type DnsRecord,
9 } from "alchemy/DNS/Adapter";
10 import type { Input } from "alchemy/Input";
11 import * as RemovalPolicy from "alchemy/RemovalPolicy";
12 import * as Effect from "effect/Effect";
13 import type { Providers } from "../Providers.ts";
14 import { Record } from "./Record.ts";
15 import { RecordList } from "./RecordList.ts";
16 import { Records } from "./Records.ts";
17
18 /** The {@link DnsConfig.type} of the Route 53 DNS adapter. */
19 export const ROUTE53_DNS = "AWS.Route53";
20
21 /** TTL of CNAMEs to non-AWS targets. */
22 const CNAME_TTL = "300 seconds";
23
24 export interface AdapterOptions {
25 /**
26 * Hosted zone that owns the records. Inferred per hostname (the most
27 * specific public hosted zone in the account) when omitted.
28 */
29 readonly hostedZoneId?: Input<string>;
30 }
31
32 /**
33 * Publish a custom domain's DNS records in Route 53 — the default for AWS
34 * composites when `domain.dns` is omitted, and usable from any other
35 * platform (a Cloudflare Worker through Cloudflare for SaaS, a Fly or
36 * Railway site, …).
37 *
38 * AWS targets get `A` alias records (plus `AAAA` where the target serves
39 * IPv6); other hostname targets get a `CNAME`; address targets get
40 * `A`/`AAAA` records. Requires `AWS.providers()` in the stack. See
41 * [DNS Adapters](/infrastructure-as-code/dns-adapters).
42 *
43 * **Example:** Fly site on a Route 53 domain
44 * ```typescript
45 * yield* Fly.Website.Vite("Web", {
46 * domain: {
47 * name: "app.example.com",
48 * dns: AWS.Route53.Adapter({ hostedZoneId: "Z1234567890" }),
49 * },
50 * });
51 * ```
52 */
53 export const Adapter = (options: AdapterOptions = {}): DnsConfig<typeof ROUTE53_DNS> => ({
54 type: ROUTE53_DNS,
55 ...(options.hostedZoneId === undefined ? {} : { zone: options.hostedZoneId }),
56 });
57
58 const aliasTargetOf = (target: DnsHostnameTarget) => ({
59 hostedZoneId: target.route53Alias!.hostedZoneId,
60 dnsName: target.hostname,
61 ...(target.route53Alias!.evaluateTargetHealth === undefined
62 ? {}
63 : { evaluateTargetHealth: target.route53Alias!.evaluateTargetHealth }),
64 });
65
66 /**
67 * The Route 53 DNS adapter, registered by `AWS.providers()`. Logical ids and
68 * props match the records AWS composites declared before DNS adapters
69 * existed, so upgrading plans no changes:
70 *
71 * - `alias` — `AWS.Route53.Record` `id` (a lone `A` alias or `CNAME`), or
72 * `id-A` / `id-AAAA` for a dual-stack alias; `id-Addresses`
73 * (`AWS.Route53.RecordList`) for an address target.
74 * - `aliasSet` — `AWS.Route53.Records` `id`.
75 * - `records` — `AWS.Route53.RecordList` `id`.
76 *
77 * `hostedZoneId` is always passed (even `undefined`), exactly as before.
78 */
79 export const adapter = {
80 alias: (id, { zone: hostedZoneId, name, target, ipv6 }) =>
81 !isHostnameTarget(target)
82 ? RecordList(`${id}-Addresses`, {
83 hostedZoneId,
84 records: addressRecords(name, target.ipv4, target.ipv6) as DnsRecord[],
85 })
86 : target.route53Alias === undefined
87 ? Record(id, {
88 hostedZoneId,
89 name,
90 type: "CNAME",
91 ttl: CNAME_TTL,
92 records: [target.hostname as string],
93 })
94 : Effect.forEach(ipv6 ? (["A", "AAAA"] as const) : (["A"] as const), (type) =>
95 Record(ipv6 ? `${id}-${type}` : id, {
96 hostedZoneId,
97 name,
98 type,
99 aliasTarget: aliasTargetOf(target),
100 }),
101 ),
102 aliasSet: (id, { zone: hostedZoneId, names, target }) =>
103 Records(id, {
104 hostedZoneId,
105 ...(names === undefined ? {} : { names }),
106 ...(target.route53Alias === undefined
107 ? {
108 type: "CNAME" as const,
109 ttl: CNAME_TTL,
110 records: [target.hostname as string],
111 }
112 : { type: "A" as const, aliasTarget: aliasTargetOf(target) }),
113 }),
114 records: (id, { zone: hostedZoneId, records, retain }) =>
115 RecordList(id, { hostedZoneId, records: records as DnsRecord[] }).pipe(
116 retain === true ? RemovalPolicy.retain() : (effect) => effect,
117 ),
118 } satisfies DnsAdapter<Providers>;
119
120 export const AdapterLive = adapterLayer(ROUTE53_DNS, adapter);