The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolve4, resolveNs, Resolver } from "node:dns/promises"; |
| 2 | /** |
| 3 | * Custom domains on a Hetzner website, deployed for real: `domain.dns` |
| 4 | * publishes the `A` record through another DNS host (Cloudflare, resolved |
| 5 | * publicly), and the default path (no `dns`) keeps the `Hetzner.RecordSet` |
| 6 | * in `zone`. Hetzner sites serve plain HTTP (no TLS). |
| 7 | */ |
| 8 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 9 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 10 | import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 11 | import * as Hetzner from "@alchemy.run/hetzner"; |
| 12 | import { expect } from "@alchemy.run/test"; |
| 13 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 14 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 15 | import * as servers from "@distilled.cloud/hetzner/servers"; |
| 16 | import * as zoneRrsets from "@distilled.cloud/hetzner/zone_rrsets"; |
| 17 | import * as zones from "@distilled.cloud/hetzner/zones"; |
| 18 | import * as Data from "effect/Data"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as Layer from "effect/Layer"; |
| 21 | import { MinimumLogLevel } from "effect/References"; |
| 22 | import * as Schedule from "effect/Schedule"; |
| 23 | import * as Stream from "effect/Stream"; |
| 24 | import * as pathe from "pathe"; |
| 25 | import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts"; |
| 26 | import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts"; |
| 27 | |
| 28 | const { test } = Test.make({ |
| 29 | providers: Layer.mergeAll(Hetzner.providers(), Cloudflare.providers()), |
| 30 | }); |
| 31 | |
| 32 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 33 | |
| 34 | const hasHetznerCreds = !!process.env.HCLOUD_TOKEN; |
| 35 | |
| 36 | const ZONE = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 37 | const CLOUDFLARE_HOST = `hz-dns.${ZONE}`; |
| 38 | /** |
| 39 | * Hetzner DNS only hosts zones for registrable domains ("unsupported tld" |
| 40 | * for any subdomain). A unique, non-delegated zone keeps this suite off the |
| 41 | * shared `alchemy-test-2.us` apex zone; its records are queried from |
| 42 | * Hetzner's authoritative name servers directly. |
| 43 | */ |
| 44 | const HETZNER_ZONE = "hz-dns-zone-alchemy-test.us"; |
| 45 | const HETZNER_RECORD_NAME = "www"; |
| 46 | const HETZNER_HOST = `${HETZNER_RECORD_NAME}.${HETZNER_ZONE}`; |
| 47 | /** Hetzner assigns three name servers to every primary zone. */ |
| 48 | const HETZNER_NAME_SERVER_COUNT = 3; |
| 49 | |
| 50 | const fixtureDir = pathe.resolve( |
| 51 | import.meta.dirname, |
| 52 | "../../Cloudflare/Website/staticsite-fixture", |
| 53 | ); |
| 54 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 55 | |
| 56 | const normalize = (name: string | undefined) => (name ?? "").replace(/\.$/, "").toLowerCase(); |
| 57 | |
| 58 | class NotPublished extends Data.TaggedError("NotPublished")<{ |
| 59 | readonly server: string; |
| 60 | readonly name: string; |
| 61 | }> {} |
| 62 | |
| 63 | const cloudflareZoneId = Effect.gen(function* () { |
| 64 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 65 | const zone = yield* findZoneByName({ accountId, name: ZONE }); |
| 66 | if (!zone) { |
| 67 | return yield* Effect.die(new Error(`zone "${ZONE}" not found in account`)); |
| 68 | } |
| 69 | return zone.id; |
| 70 | }); |
| 71 | |
| 72 | const cloudflareRecords = (zoneId: string, name: string, type: "A") => |
| 73 | dns.listRecords.items({ zoneId, name: { exact: name }, type }).pipe( |
| 74 | Stream.runCollect, |
| 75 | Effect.map((chunk) => Array.from(chunk)), |
| 76 | ); |
| 77 | |
| 78 | const waitUntilServerGone = (id: number) => |
| 79 | servers.getServer({ id }).pipe( |
| 80 | Effect.as("found" as const), |
| 81 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 82 | Effect.repeat({ |
| 83 | schedule: Schedule.spaced("1 second"), |
| 84 | until: (status) => status === "gone", |
| 85 | times: 10, |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | const waitUntilZoneGone = (zoneId: number) => |
| 90 | zones.getZone({ id_or_name: String(zoneId) }).pipe( |
| 91 | Effect.as("found" as const), |
| 92 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 93 | Effect.repeat({ |
| 94 | schedule: Schedule.spaced("1 second"), |
| 95 | until: (status) => status === "gone", |
| 96 | times: 10, |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | /** |
| 101 | * Wait until every authoritative name server answers `name` with the |
| 102 | * `expected` A records, so the first HTTP request never resolves (and |
| 103 | * negatively caches) the hostname before it exists. |
| 104 | */ |
| 105 | const waitForAuthoritativeA = ( |
| 106 | nameServers: readonly string[], |
| 107 | name: string, |
| 108 | expected: readonly string[], |
| 109 | ) => |
| 110 | Effect.gen(function* () { |
| 111 | const addresses = (yield* Effect.forEach(nameServers, (ns) => |
| 112 | Effect.tryPromise(() => resolve4(normalize(ns))).pipe(Effect.orDie), |
| 113 | )).flat(); |
| 114 | yield* Effect.forEach(addresses, (server) => |
| 115 | Effect.gen(function* () { |
| 116 | const resolver = yield* Effect.sync(() => { |
| 117 | const r = new Resolver(); |
| 118 | r.setServers([server]); |
| 119 | return r; |
| 120 | }); |
| 121 | const answers = yield* Effect.tryPromise(() => resolver.resolve4(name)).pipe( |
| 122 | Effect.orElseSucceed(() => [] as string[]), |
| 123 | ); |
| 124 | if (!expected.every((ip) => answers.includes(ip))) { |
| 125 | return yield* Effect.fail(new NotPublished({ server, name })); |
| 126 | } |
| 127 | }).pipe(Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 20 })), |
| 128 | ); |
| 129 | }); |
| 130 | |
| 131 | const cloneSite = cloneFixture(fixtureDir, { |
| 132 | prefix: "alchemy-hetzner-dns-", |
| 133 | tempRoot, |
| 134 | entries: ["src", "build.sh"], |
| 135 | }); |
| 136 | |
| 137 | const siteProps = (cwd: string) => ({ |
| 138 | cwd, |
| 139 | command: "bash build.sh", |
| 140 | outdir: "dist", |
| 141 | }); |
| 142 | |
| 143 | const tags = [ |
| 144 | "provider:hetzner", |
| 145 | "provider:hetzner:service", |
| 146 | "provider:hetzner:website", |
| 147 | "provider:cloudflare", |
| 148 | "provider:cloudflare:dns", |
| 149 | "live", |
| 150 | ]; |
| 151 | |
| 152 | test.provider.skipIf(!hasHetznerCreds)( |
| 153 | "Cloudflare DNS (no zone): A record at the Server IPv4, HTTP 200 on the hostname, removed on destroy", |
| 154 | (stack) => |
| 155 | Effect.gen(function* () { |
| 156 | yield* stack.destroy(); |
| 157 | const zoneId = yield* cloudflareZoneId; |
| 158 | const cwd = yield* cloneSite; |
| 159 | |
| 160 | yield* Effect.gen(function* () { |
| 161 | const { site } = yield* stack.deploy( |
| 162 | Effect.gen(function* () { |
| 163 | const site = yield* Hetzner.Website.StaticSite("Site", { |
| 164 | ...siteProps(cwd), |
| 165 | domain: { |
| 166 | name: CLOUDFLARE_HOST, |
| 167 | dns: Cloudflare.DNS.Adapter(), |
| 168 | }, |
| 169 | }); |
| 170 | return { site }; |
| 171 | }), |
| 172 | ); |
| 173 | const ipv4 = site.server!.ipv4!; |
| 174 | const url = site.url!; |
| 175 | expect(url).toMatch(new RegExp(`^http://${CLOUDFLARE_HOST.replaceAll(".", "\\.")}:\\d+$`)); |
| 176 | |
| 177 | const a = yield* cloudflareRecords(zoneId, CLOUDFLARE_HOST, "A"); |
| 178 | expect(a.map((record) => record.content)).toEqual([ipv4]); |
| 179 | expect(a[0]?.proxied).toBe(false); |
| 180 | |
| 181 | const nameServers = yield* Effect.tryPromise(() => resolveNs(ZONE)).pipe(Effect.orDie); |
| 182 | yield* waitForAuthoritativeA(nameServers, CLOUDFLARE_HOST, [ipv4]); |
| 183 | yield* expectUrlContains(`${url}/`, "StaticSite fixture v1", { |
| 184 | timeout: "120 seconds", |
| 185 | label: "hetzner site on cloudflare dns", |
| 186 | }); |
| 187 | |
| 188 | const serverId = site.server!.serverId; |
| 189 | yield* stack.destroy(); |
| 190 | expect(yield* cloudflareRecords(zoneId, CLOUDFLARE_HOST, "A")).toEqual([]); |
| 191 | expect(yield* waitUntilServerGone(serverId)).toEqual("gone"); |
| 192 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 193 | }).pipe(logLevel), |
| 194 | { tags, timeout: 600_000 }, |
| 195 | ); |
| 196 | |
| 197 | test.provider.skipIf(!hasHetznerCreds)( |
| 198 | "default path (no dns): the A RecordSet in a Hetzner.Zone points the hostname at the Server", |
| 199 | (stack) => |
| 200 | Effect.gen(function* () { |
| 201 | yield* stack.destroy(); |
| 202 | const cwd = yield* cloneSite; |
| 203 | |
| 204 | yield* Effect.gen(function* () { |
| 205 | const { site, zone } = yield* stack.deploy( |
| 206 | Effect.gen(function* () { |
| 207 | const zone = yield* Hetzner.Zone("Zone", { |
| 208 | name: HETZNER_ZONE, |
| 209 | ttl: 300, |
| 210 | }); |
| 211 | const site = yield* Hetzner.Website.StaticSite("Site", { |
| 212 | ...siteProps(cwd), |
| 213 | domain: HETZNER_HOST, |
| 214 | zone, |
| 215 | }); |
| 216 | return { site, zone }; |
| 217 | }), |
| 218 | ); |
| 219 | expect(zone.assignedNameservers).toHaveLength(HETZNER_NAME_SERVER_COUNT); |
| 220 | const ipv4 = site.server!.ipv4!; |
| 221 | const url = site.url!; |
| 222 | const port = new URL(url).port; |
| 223 | expect(url).toBe(`http://${HETZNER_HOST}:${port}`); |
| 224 | |
| 225 | // The unchanged default: an A RecordSet in the Hetzner zone. |
| 226 | const { rrset } = yield* zoneRrsets.getZoneRrset({ |
| 227 | id_or_name: String(zone.zoneId), |
| 228 | rr_name: HETZNER_RECORD_NAME, |
| 229 | rr_type: "A", |
| 230 | }); |
| 231 | expect(rrset.records.map((record) => record.value)).toEqual([ipv4]); |
| 232 | |
| 233 | // Hetzner's name servers serve it (the zone is not delegated). |
| 234 | yield* waitForAuthoritativeA(zone.assignedNameservers, HETZNER_HOST, [ipv4]); |
| 235 | yield* expectUrlContains(`http://${ipv4}:${port}/`, "StaticSite fixture v1", { |
| 236 | headers: { host: `${HETZNER_HOST}:${port}` }, |
| 237 | timeout: "120 seconds", |
| 238 | label: "hetzner site on hetzner zone", |
| 239 | }); |
| 240 | |
| 241 | const serverId = site.server!.serverId; |
| 242 | yield* stack.destroy(); |
| 243 | expect(yield* waitUntilZoneGone(zone.zoneId)).toEqual("gone"); |
| 244 | expect(yield* waitUntilServerGone(serverId)).toEqual("gone"); |
| 245 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 246 | }).pipe(logLevel), |
| 247 | { |
| 248 | tags: [ |
| 249 | "provider:hetzner", |
| 250 | "provider:hetzner:recordset", |
| 251 | "provider:hetzner:service", |
| 252 | "provider:hetzner:website", |
| 253 | "provider:hetzner:zone", |
| 254 | "live", |
| 255 | ], |
| 256 | timeout: 600_000, |
| 257 | }, |
| 258 | ); |