The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Predicate from "effect/Predicate"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | |
| 12 | const DnsViewTypeId = "Cloudflare.DNS.View" as const; |
| 13 | type DnsViewTypeId = typeof DnsViewTypeId; |
| 14 | |
| 15 | export interface ViewProps { |
| 16 | /** |
| 17 | * Name of the view. If omitted, a unique name is generated from the |
| 18 | * app, stage, and logical ID. |
| 19 | * |
| 20 | * Mutable — patched in place. |
| 21 | * @default ${app}-${stage}-${id} |
| 22 | */ |
| 23 | name?: string; |
| 24 | /** |
| 25 | * Zones (by id) linked to this view. Internal DNS queries resolved |
| 26 | * through the view consult these zones. |
| 27 | * |
| 28 | * Mutable — patched in place. |
| 29 | */ |
| 30 | zones: string[]; |
| 31 | } |
| 32 | |
| 33 | export interface ViewAttributes { |
| 34 | /** Identifier of the view. */ |
| 35 | viewId: string; |
| 36 | /** The Cloudflare account the view belongs to. */ |
| 37 | accountId: string; |
| 38 | /** Name of the view. */ |
| 39 | name: string; |
| 40 | /** Zone ids linked to the view. */ |
| 41 | zones: string[]; |
| 42 | /** When the view was created. */ |
| 43 | createdTime: string; |
| 44 | /** When the view was last modified. */ |
| 45 | modifiedTime: string; |
| 46 | } |
| 47 | |
| 48 | export type View = Resource<DnsViewTypeId, ViewProps, ViewAttributes, never, Providers>; |
| 49 | |
| 50 | /** |
| 51 | * An Internal DNS view (`/accounts/{account_id}/dns_settings/views`) — |
| 52 | * a named set of internal zones that DNS queries can be resolved |
| 53 | * against, for split-horizon / internal DNS setups. |
| 54 | * |
| 55 | * Requires the Enterprise Internal DNS entitlement on the account |
| 56 | * (creation fails with `InternalDnsNotAvailable` otherwise). Both |
| 57 | * `name` and `zones` are mutable in place. |
| 58 | * ### Creating a View |
| 59 | * **Example:** View over internal zones |
| 60 | * ```typescript |
| 61 | * const view = yield* Cloudflare.DNS.View("Internal", { |
| 62 | * zones: [internalZone.zoneId], |
| 63 | * }); |
| 64 | * ``` |
| 65 | * |
| 66 | * **Example:** View with an explicit name |
| 67 | * ```typescript |
| 68 | * const view = yield* Cloudflare.DNS.View("Internal", { |
| 69 | * name: "datacenter-east", |
| 70 | * zones: [zoneA.zoneId, zoneB.zoneId], |
| 71 | * }); |
| 72 | * ``` |
| 73 | * |
| 74 | * @see https://developers.cloudflare.com/dns/internal-dns/ |
| 75 | * |
| 76 | * @resource |
| 77 | * @product DNS |
| 78 | * @category Domains & DNS |
| 79 | */ |
| 80 | export const View = Resource<View>(DnsViewTypeId, { |
| 81 | aliases: ["Cloudflare.Dns.View"], |
| 82 | }); |
| 83 | |
| 84 | /** |
| 85 | * Returns true if the given value is a View resource. |
| 86 | */ |
| 87 | export const isView = (value: unknown): value is View => |
| 88 | Predicate.hasProperty(value, "Type") && value.Type === DnsViewTypeId; |
| 89 | |
| 90 | export const ViewProvider = () => |
| 91 | Provider.succeed(View, { |
| 92 | stables: ["viewId", "accountId", "createdTime"], |
| 93 | |
| 94 | // Account collection — internal DNS views are enumerated per account |
| 95 | // via the paginated list endpoint. Each item already carries the full |
| 96 | // observed shape, so map straight into the `read` Attributes. |
| 97 | list: Effect.fn(function* () { |
| 98 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 99 | return yield* dns.listSettingAccountViews.pages({ accountId }).pipe( |
| 100 | Stream.runCollect, |
| 101 | Effect.map((chunk) => |
| 102 | Array.from(chunk).flatMap((page) => |
| 103 | (page.result ?? []).map((view) => toAttributes(view, accountId)), |
| 104 | ), |
| 105 | ), |
| 106 | ); |
| 107 | }), |
| 108 | |
| 109 | diff: Effect.fn(function* ({ output }) { |
| 110 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 111 | if (output !== undefined && output.accountId !== accountId) { |
| 112 | return { action: "replace" } as const; |
| 113 | } |
| 114 | return undefined; |
| 115 | }), |
| 116 | |
| 117 | read: Effect.fn(function* ({ id, output, olds }) { |
| 118 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 119 | const acct = output?.accountId ?? accountId; |
| 120 | |
| 121 | if (output?.viewId) { |
| 122 | const observed = yield* getView(acct, output.viewId); |
| 123 | return observed ? toAttributes(observed, acct) : undefined; |
| 124 | } |
| 125 | // Cold read — recover from lost state by matching the |
| 126 | // deterministic physical name. Views carry no ownership markers, |
| 127 | // so gate takeover behind adoption. |
| 128 | const name = yield* createViewName(id, olds?.name); |
| 129 | const match = yield* findByName(acct, name); |
| 130 | return match ? Unowned(toAttributes(match, acct)) : undefined; |
| 131 | }), |
| 132 | |
| 133 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 134 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 135 | const name = yield* createViewName(id, news.name); |
| 136 | // Inputs are resolved to concrete values by Plan. |
| 137 | const zones = news.zones as string[]; |
| 138 | |
| 139 | // Observe — the id cached on `output` is a hint, not a guarantee. |
| 140 | const observed = output?.viewId |
| 141 | ? yield* getView(output.accountId ?? accountId, output.viewId) |
| 142 | : undefined; |
| 143 | |
| 144 | if (!observed) { |
| 145 | // Ensure — greenfield (or out-of-band delete). Names are not |
| 146 | // unique, so there is no AlreadyExists race to tolerate. |
| 147 | const created = yield* dns.createSettingAccountView({ |
| 148 | accountId, |
| 149 | name, |
| 150 | zones, |
| 151 | }); |
| 152 | return toAttributes(created, accountId); |
| 153 | } |
| 154 | |
| 155 | // Sync — patch only when the observed view differs. |
| 156 | if (observed.name === name && sameZones(observed.zones, zones)) { |
| 157 | return toAttributes(observed, output?.accountId ?? accountId); |
| 158 | } |
| 159 | const updated = yield* dns.patchSettingAccountView({ |
| 160 | accountId: output?.accountId ?? accountId, |
| 161 | viewId: observed.id, |
| 162 | name, |
| 163 | zones, |
| 164 | }); |
| 165 | return toAttributes(updated, output?.accountId ?? accountId); |
| 166 | }), |
| 167 | |
| 168 | delete: Effect.fn(function* ({ output }) { |
| 169 | yield* dns |
| 170 | .deleteSettingAccountView({ |
| 171 | accountId: output.accountId, |
| 172 | viewId: output.viewId, |
| 173 | }) |
| 174 | .pipe(Effect.catchTag("ViewNotFound", () => Effect.void)); |
| 175 | }), |
| 176 | }); |
| 177 | |
| 178 | type ObservedView = |
| 179 | | dns.GetSettingAccountViewResponse |
| 180 | | dns.CreateSettingAccountViewResponse |
| 181 | | dns.PatchSettingAccountViewResponse; |
| 182 | |
| 183 | /** Read a view by id, mapping "gone" (code 1015) to `undefined`. */ |
| 184 | const getView = (accountId: string, viewId: string) => |
| 185 | dns |
| 186 | .getSettingAccountView({ accountId, viewId }) |
| 187 | .pipe(Effect.catchTag("ViewNotFound", () => Effect.undefined)); |
| 188 | |
| 189 | /** |
| 190 | * Find a view by exact name. The `name.exact` filter narrows |
| 191 | * server-side; re-check exactly client-side and pick the oldest for |
| 192 | * determinism. |
| 193 | */ |
| 194 | const findByName = (accountId: string, name: string) => |
| 195 | dns.listSettingAccountViews({ accountId, name: { exact: name } }).pipe( |
| 196 | Effect.map((list) => |
| 197 | list.result |
| 198 | .filter((v) => v.name === name) |
| 199 | .sort((a, b) => a.createdTime.localeCompare(b.createdTime)) |
| 200 | .at(0), |
| 201 | ), |
| 202 | ); |
| 203 | |
| 204 | const createViewName = (id: string, name: string | undefined) => |
| 205 | Effect.gen(function* () { |
| 206 | return name ?? (yield* createPhysicalName({ id, lowercase: true })); |
| 207 | }); |
| 208 | |
| 209 | const sameZones = (observed: readonly string[], desired: readonly string[]) => |
| 210 | observed.length === desired.length && |
| 211 | [...observed].sort().join(",") === [...desired].sort().join(","); |
| 212 | |
| 213 | const toAttributes = (view: ObservedView, accountId: string): ViewAttributes => ({ |
| 214 | viewId: view.id, |
| 215 | accountId, |
| 216 | name: view.name, |
| 217 | zones: [...view.zones], |
| 218 | createdTime: view.createdTime, |
| 219 | modifiedTime: view.modifiedTime, |
| 220 | }); |