@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 230 lines 7.59 KB
Raw
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 TypeId = "Cloudflare.DNS.ZoneTransferAcl" as const;
13 type TypeId = typeof TypeId;
14
15 export interface ZoneTransferAclProps {
16 /**
17 * Human-readable name of the ACL. If omitted, a unique name is
18 * generated from the app, stage, and logical ID.
19 *
20 * Mutable — updated in place (PUT).
21 * @default ${app}-${stage}-${id}
22 */
23 name?: string;
24 /**
25 * Allowed IPv4/IPv6 address range of primary or secondary
26 * nameservers, applied account-wide. Used to allow additional NOTIFY
27 * IPs for secondary zones and IPs Cloudflare allows AXFR/IXFR
28 * requests from for outgoing transfers. CIDRs are limited to a
29 * maximum of /24 for IPv4 and /64 for IPv6. Note that Cloudflare
30 * normalizes the range to its network address (e.g. `192.0.2.53/28`
31 * becomes `192.0.2.48/28`).
32 *
33 * Mutable — updated in place (PUT).
34 */
35 ipRange: string;
36 }
37
38 export interface ZoneTransferAclAttributes {
39 /** Identifier of the ACL. */
40 aclId: string;
41 /** The Cloudflare account the ACL belongs to. */
42 accountId: string;
43 /** Human-readable name of the ACL. */
44 name: string;
45 /** Allowed IP range, as normalized by Cloudflare. */
46 ipRange: string;
47 }
48
49 export type ZoneTransferAcl = Resource<
50 TypeId,
51 ZoneTransferAclProps,
52 ZoneTransferAclAttributes,
53 never,
54 Providers
55 >;
56
57 /**
58 * A Secondary DNS zone-transfer ACL
59 * (`/accounts/{account_id}/secondary_dns/acls`) — an account-wide
60 * IPv4/IPv6 range that may receive NOTIFYs for secondary zones and from
61 * which Cloudflare accepts AXFR/IXFR requests for outgoing transfers.
62 *
63 * Requires the Secondary DNS (zone transfer) entitlement on the
64 * account. Both `name` and `ipRange` are mutable in place.
65 * ### Creating an ACL
66 * **Example:** Allow a primary nameserver range
67 * ```typescript
68 * const acl = yield* Cloudflare.DNS.ZoneTransferAcl("PrimaryNs", {
69 * ipRange: "192.0.2.48/28",
70 * });
71 * ```
72 *
73 * **Example:** ACL with an explicit name
74 * ```typescript
75 * const acl = yield* Cloudflare.DNS.ZoneTransferAcl("PrimaryNs", {
76 * name: "primary-nameservers",
77 * ipRange: "2001:db8::/64",
78 * });
79 * ```
80 *
81 * @see https://developers.cloudflare.com/dns/zone-setups/zone-transfers/
82 *
83 * @resource
84 * @product DNS
85 * @category Domains & DNS
86 */
87 export const ZoneTransferAcl = Resource<ZoneTransferAcl>(TypeId, {
88 aliases: ["Cloudflare.Dns.ZoneTransferAcl"],
89 });
90
91 /**
92 * Returns true if the given value is a ZoneTransferAcl resource.
93 */
94 export const isZoneTransferAcl = (value: unknown): value is ZoneTransferAcl =>
95 Predicate.hasProperty(value, "Type") && value.Type === TypeId;
96
97 export const ZoneTransferAclProvider = () =>
98 Provider.succeed(ZoneTransferAcl, {
99 stables: ["aclId", "accountId"],
100
101 // Account-scoped collection: enumerate every ACL in the ambient
102 // account, paginating exhaustively, and hydrate into the exact `read`
103 // Attributes shape.
104 list: Effect.fn(function* () {
105 const { accountId } = yield* yield* CloudflareEnvironment;
106 return yield* dns.listZoneTransferAcls.pages({ accountId }).pipe(
107 Stream.runCollect,
108 Effect.map((chunk) =>
109 Array.from(chunk).flatMap((page) =>
110 (page.result ?? []).map((acl): ZoneTransferAclAttributes => ({
111 aclId: acl.id,
112 accountId,
113 name: acl.name,
114 ipRange: acl.ipRange,
115 })),
116 ),
117 ),
118 );
119 }),
120
121 diff: Effect.fn(function* ({ output }) {
122 const { accountId } = yield* yield* CloudflareEnvironment;
123 if (output !== undefined && output.accountId !== accountId) {
124 return { action: "replace" } as const;
125 }
126 return undefined;
127 }),
128
129 read: Effect.fn(function* ({ id, output, olds }) {
130 const { accountId } = yield* yield* CloudflareEnvironment;
131 const acct = output?.accountId ?? accountId;
132
133 if (output?.aclId) {
134 const observed = yield* getAcl(acct, output.aclId);
135 return observed ? toAttributes(observed, acct) : undefined;
136 }
137 // Cold read — recover from lost state by matching the
138 // deterministic physical name. ACLs carry no ownership markers,
139 // so gate takeover behind adoption.
140 const name = yield* createAclName(id, olds?.name);
141 const match = yield* findByName(acct, name);
142 return match ? Unowned(toAttributes(match, acct)) : undefined;
143 }),
144
145 reconcile: Effect.fn(function* ({ id, news, output }) {
146 const { accountId } = yield* yield* CloudflareEnvironment;
147 const name = yield* createAclName(id, news.name);
148 // Inputs are resolved to concrete values by Plan.
149 const ipRange = news.ipRange as string;
150
151 // Observe — the id cached on `output` is a hint, not a guarantee.
152 const observed = output?.aclId
153 ? yield* getAcl(output.accountId ?? accountId, output.aclId)
154 : undefined;
155
156 if (!observed) {
157 // Ensure — greenfield (or out-of-band delete). Names are not
158 // unique on Cloudflare's side, so there is no AlreadyExists
159 // race to tolerate.
160 const created = yield* dns.createZoneTransferAcl({
161 accountId,
162 name,
163 ipRange,
164 });
165 return toAttributes(created, accountId);
166 }
167
168 // Sync — the update API is a PUT with the full body; skip the
169 // call entirely when nothing differs. Cloudflare normalizes
170 // `ipRange` to the network address, so compare against the
171 // observed (normalized) value.
172 if (observed.name === name && observed.ipRange === ipRange) {
173 return toAttributes(observed, output?.accountId ?? accountId);
174 }
175 const updated = yield* dns.updateZoneTransferAcl({
176 accountId: output?.accountId ?? accountId,
177 aclId: observed.id,
178 name,
179 ipRange,
180 });
181 return toAttributes(updated, output?.accountId ?? accountId);
182 }),
183
184 delete: Effect.fn(function* ({ output }) {
185 yield* dns
186 .deleteZoneTransferAcl({
187 accountId: output.accountId,
188 aclId: output.aclId,
189 })
190 .pipe(Effect.catchTag("AclNotFound", () => Effect.void));
191 }),
192 });
193
194 type ObservedAcl =
195 | dns.GetZoneTransferAclResponse
196 | dns.CreateZoneTransferAclResponse
197 | dns.UpdateZoneTransferAclResponse;
198
199 /** Read an ACL by id, mapping "gone" (404) to `undefined`. */
200 const getAcl = (accountId: string, aclId: string) =>
201 dns
202 .getZoneTransferAcl({ accountId, aclId })
203 .pipe(Effect.catchTag("AclNotFound", () => Effect.undefined));
204
205 /**
206 * Find an ACL by exact name. Names are not unique on Cloudflare's side;
207 * pick the lexicographically-first id for determinism.
208 */
209 const findByName = (accountId: string, name: string) =>
210 dns.listZoneTransferAcls.items({ accountId }).pipe(
211 Stream.filter((a) => a.name === name),
212 Stream.runCollect,
213 Effect.map((chunk) =>
214 Array.from(chunk)
215 .sort((a, b) => a.id.localeCompare(b.id))
216 .at(0),
217 ),
218 );
219
220 const createAclName = (id: string, name: string | undefined) =>
221 Effect.gen(function* () {
222 return name ?? (yield* createPhysicalName({ id, lowercase: true }));
223 });
224
225 const toAttributes = (acl: ObservedAcl, accountId: string): ZoneTransferAclAttributes => ({
226 aclId: acl.id,
227 accountId,
228 name: acl.name,
229 ipRange: acl.ipRange,
230 });