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

main
ts 108 lines 3.77 KB
Raw
1 import {
2 addressRecords,
3 adapterLayer,
4 isHostnameTarget,
5 type DnsAdapter,
6 type DnsConfig,
7 type DnsRecord,
8 } from "alchemy/DNS/Adapter";
9 import type { Input } from "alchemy/Input";
10 import * as Output from "alchemy/Output";
11 import * as RemovalPolicy from "alchemy/RemovalPolicy";
12 import type { Providers } from "../Providers.ts";
13 import { RecordList } from "./RecordList.ts";
14 import { Records } from "./Records.ts";
15
16 /** The {@link DnsConfig.type} of the Cloudflare DNS adapter. */
17 export const CLOUDFLARE_DNS = "Cloudflare.DNS";
18
19 export interface AdapterOptions {
20 /**
21 * Zone that owns the records: a zone id, a zone name, or a `Zone`
22 * resource. Inferred from each hostname when omitted.
23 */
24 readonly zone?: Input<string> | { readonly zoneId: Input<string> };
25 /**
26 * Send alias records through Cloudflare's proxy (orange cloud).
27 * Validation and verification records are always DNS-only.
28 * @default false
29 */
30 readonly proxied?: boolean;
31 }
32
33 /**
34 * Publish a custom domain's DNS records in a Cloudflare zone — e.g. a
35 * domain registered with Cloudflare Registrar that is served by AWS, Fly,
36 * Railway, Neon, Prisma, or Hetzner.
37 *
38 * Pass it as `domain.dns`. The serving platform then publishes its
39 * certificate-validation / verification records (always DNS-only) and
40 * points each hostname at itself with a `CNAME` (flattened at the zone
41 * apex) or `A`/`AAAA` records. Requires `Cloudflare.providers()` in the
42 * stack. See [DNS Adapters](/infrastructure-as-code/dns-adapters).
43 *
44 * **Example:** AWS site on a Cloudflare domain
45 * ```typescript
46 * yield* AWS.Website.StaticSite("Site", {
47 * path: "./dist",
48 * domain: { name: "www.example.com", dns: Cloudflare.DNS.Adapter() },
49 * });
50 * ```
51 *
52 * **Example:** Pinned zone, proxied aliases
53 * ```typescript
54 * // Proxying CloudFront through Cloudflare: set the zone's SSL/TLS mode
55 * // to Full (strict).
56 * dns: Cloudflare.DNS.Adapter({ zone: "example.com", proxied: true })
57 * ```
58 */
59 export const Adapter = (options: AdapterOptions = {}): DnsConfig<typeof CLOUDFLARE_DNS> => {
60 const zone =
61 options.zone === undefined || typeof options.zone === "string" || Output.isOutput(options.zone)
62 ? (options.zone as Input<string> | undefined)
63 : (options.zone as { zoneId: Input<string> }).zoneId;
64 return {
65 type: CLOUDFLARE_DNS,
66 ...(zone === undefined ? {} : { zone }),
67 ...(options.proxied === undefined ? {} : { options: { proxied: options.proxied } }),
68 };
69 };
70
71 /**
72 * The Cloudflare DNS adapter, registered by `Cloudflare.providers()`:
73 *
74 * - `alias` / `aliasSet` — a `Cloudflare.DNS.Records` CNAME set for a
75 * hostname target (logical id `{id}-CNAME`), or a
76 * `Cloudflare.DNS.RecordList` of `A`/`AAAA` records for an address target
77 * (`{id}-Addresses`).
78 * - `records` — a `Cloudflare.DNS.RecordList`.
79 */
80 export const adapter = {
81 alias: (id, { zone, options, name, target }) =>
82 isHostnameTarget(target)
83 ? Records(`${id}-CNAME`, {
84 zone,
85 ...(options?.proxied === true ? { proxied: true } : {}),
86 type: "CNAME",
87 content: target.hostname as string,
88 names: [name],
89 })
90 : RecordList(`${id}-Addresses`, {
91 zone,
92 records: addressRecords(name, target.ipv4, target.ipv6) as DnsRecord[],
93 }),
94 aliasSet: (id, { zone, options, names, target }) =>
95 Records(`${id}-CNAME`, {
96 zone,
97 ...(options?.proxied === true ? { proxied: true } : {}),
98 type: "CNAME",
99 content: target.hostname as string,
100 names,
101 }),
102 records: (id, { zone, records, retain }) =>
103 RecordList(id, { zone, records: records as DnsRecord[] }).pipe(
104 retain === true ? RemovalPolicy.retain() : (effect) => effect,
105 ),
106 } satisfies DnsAdapter<Providers>;
107
108 export const AdapterLive = adapterLayer(CLOUDFLARE_DNS, adapter);