The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as route53 from "@distilled.cloud/aws/route-53"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | export class HostedZoneLookupInvalidProps extends Data.TaggedError("HostedZoneLookupInvalidProps")<{ |
| 6 | readonly message: string; |
| 7 | readonly cause?: unknown; |
| 8 | }> {} |
| 9 | |
| 10 | /** |
| 11 | * Find the most specific PUBLIC Route 53 hosted zone containing |
| 12 | * `domainName`, walking up its labels (`svc.api.example.com` → |
| 13 | * `api.example.com` → `example.com`). Wildcard labels (`*`) never name a |
| 14 | * zone, so they fall through to the parent naturally. Returns the bare |
| 15 | * zone id (no `/hostedzone/` prefix), or `undefined` when no public zone |
| 16 | * matches. |
| 17 | * |
| 18 | * Shared by every provider that infers a hosted zone from a hostname |
| 19 | * (Route 53 Record/Records, ACM Certificate validation, ECS Service DNS). |
| 20 | */ |
| 21 | export const findPublicHostedZoneId = Effect.fn("findPublicHostedZoneId")(function* ( |
| 22 | domainName: string, |
| 23 | ) { |
| 24 | const labels = domainName |
| 25 | .replace(/\.$/, "") |
| 26 | .split(".") |
| 27 | .filter((label) => label.length > 0); |
| 28 | for (let i = 0; i < labels.length - 1; i++) { |
| 29 | const candidate = `${labels.slice(i).join(".")}.`; |
| 30 | const listed = yield* route53.listHostedZonesByName({ |
| 31 | DNSName: candidate, |
| 32 | MaxItems: 1, |
| 33 | }); |
| 34 | const zone = listed.HostedZones?.[0]; |
| 35 | if (zone?.Id !== undefined && zone.Name === candidate && zone.Config?.PrivateZone !== true) { |
| 36 | return zone.Id.replace(/^\/hostedzone\//, ""); |
| 37 | } |
| 38 | } |
| 39 | return undefined; |
| 40 | }); |
| 41 | |
| 42 | /** |
| 43 | * Resolve the hosted zone for `domainName`: the explicit id when given, |
| 44 | * otherwise the most specific public zone in the account (see |
| 45 | * {@link findPublicHostedZoneId}). Fails with an actionable error when |
| 46 | * neither yields a zone. |
| 47 | */ |
| 48 | export const resolveHostedZoneId = Effect.fn("resolveHostedZoneId")(function* ( |
| 49 | hostedZoneId: string | undefined, |
| 50 | domainName: string, |
| 51 | ) { |
| 52 | if (hostedZoneId !== undefined) { |
| 53 | return hostedZoneId; |
| 54 | } |
| 55 | const inferred = yield* findPublicHostedZoneId(domainName); |
| 56 | if (inferred === undefined) { |
| 57 | return yield* new HostedZoneLookupInvalidProps({ |
| 58 | message: |
| 59 | `No public Route 53 hosted zone in this account contains "${domainName}" — ` + |
| 60 | `create the zone first or pass "hostedZoneId" explicitly.`, |
| 61 | }); |
| 62 | } |
| 63 | return inferred; |
| 64 | }); |