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, 04:45 UTC
| 1 | import * as addressing from "@distilled.cloud/cloudflare/addressing"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Predicate from "effect/Predicate"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | const TypeId = "Cloudflare.Addressing.ServiceBinding" as const; |
| 12 | type TypeId = typeof TypeId; |
| 13 | |
| 14 | export interface ServiceBindingProps { |
| 15 | /** |
| 16 | * Identifier of the parent BYOIP prefix. Changing it forces a |
| 17 | * replacement. |
| 18 | */ |
| 19 | prefixId: string; |
| 20 | /** |
| 21 | * IP Prefix in Classless Inter-Domain Routing format to bind. Must be |
| 22 | * contained in the parent prefix. Changing it forces a replacement. |
| 23 | */ |
| 24 | cidr: string; |
| 25 | /** |
| 26 | * Identifier of the Cloudflare service (CDN, Spectrum, Magic Transit) to |
| 27 | * bind the CIDR to. Service IDs are discoverable via the |
| 28 | * `addressing.listServices` catalog. Changing it forces a replacement. |
| 29 | */ |
| 30 | serviceId: string; |
| 31 | } |
| 32 | |
| 33 | export interface ServiceBindingAttributes { |
| 34 | /** Cloudflare-assigned identifier of the service binding. */ |
| 35 | bindingId: string; |
| 36 | /** Identifier of the parent BYOIP prefix. */ |
| 37 | prefixId: string; |
| 38 | /** The Cloudflare account the prefix belongs to. */ |
| 39 | accountId: string; |
| 40 | /** Bound IP Prefix in CIDR format. */ |
| 41 | cidr: string; |
| 42 | /** Identifier of the bound Cloudflare service. */ |
| 43 | serviceId: string; |
| 44 | /** Name of the bound Cloudflare service. */ |
| 45 | serviceName: string | undefined; |
| 46 | /** |
| 47 | * Deployment status of the binding on the Cloudflare network. |
| 48 | * Provisioning is asynchronous — `state` flips from `provisioning` to |
| 49 | * `active` after a few minutes. |
| 50 | */ |
| 51 | provisioning: { state: string | undefined }; |
| 52 | } |
| 53 | |
| 54 | export type ServiceBinding = Resource< |
| 55 | TypeId, |
| 56 | ServiceBindingProps, |
| 57 | ServiceBindingAttributes, |
| 58 | never, |
| 59 | Providers |
| 60 | >; |
| 61 | |
| 62 | /** |
| 63 | * Binds part of a BYOIP prefix to a Cloudflare service (CDN, Spectrum, or |
| 64 | * Magic Transit), routing traffic for the bound CIDR to that service. |
| 65 | * |
| 66 | * Bindings are create/delete only — every prop change forces a |
| 67 | * replacement. Provisioning to the edge is asynchronous: the binding is |
| 68 | * returned immediately with `provisioning.state: "provisioning"` and flips |
| 69 | * to `"active"` on Cloudflare's side; the resource does not wait for it. |
| 70 | * ### Binding a Prefix to a Service |
| 71 | * **Example:** Bind a /24 to the CDN |
| 72 | * ```typescript |
| 73 | * const binding = yield* Cloudflare.Addressing.ServiceBinding("cdn", { |
| 74 | * prefixId: prefix.prefixId, |
| 75 | * cidr: "192.0.2.0/24", |
| 76 | * serviceId: cdnServiceId, |
| 77 | * }); |
| 78 | * ``` |
| 79 | * |
| 80 | * @see https://developers.cloudflare.com/byoip/concepts/service-bindings/ |
| 81 | * |
| 82 | * @resource |
| 83 | * @product Addressing |
| 84 | * @category Network |
| 85 | */ |
| 86 | export const ServiceBinding = Resource<ServiceBinding>(TypeId); |
| 87 | |
| 88 | /** |
| 89 | * Returns true if the given value is an ServiceBinding resource. |
| 90 | */ |
| 91 | export const isServiceBinding = (value: unknown): value is ServiceBinding => |
| 92 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 93 | |
| 94 | export const ServiceBindingProvider = () => |
| 95 | Provider.succeed(ServiceBinding, { |
| 96 | stables: ["bindingId", "prefixId", "accountId", "cidr", "serviceId"], |
| 97 | |
| 98 | diff: Effect.fn(function* ({ olds, news, output }) { |
| 99 | if (olds === undefined) return undefined; |
| 100 | if (!isResolved(news) || !isResolved(olds)) return undefined; |
| 101 | // Create/delete only — any change forces a replacement. |
| 102 | const oldPrefixId = output?.prefixId ?? olds.prefixId; |
| 103 | if ( |
| 104 | typeof oldPrefixId === "string" && |
| 105 | typeof news.prefixId === "string" && |
| 106 | news.prefixId !== oldPrefixId |
| 107 | ) { |
| 108 | return { action: "replace" } as const; |
| 109 | } |
| 110 | if (news.cidr !== (output?.cidr ?? olds.cidr)) { |
| 111 | return { action: "replace" } as const; |
| 112 | } |
| 113 | const oldServiceId = output?.serviceId ?? olds.serviceId; |
| 114 | if ( |
| 115 | typeof oldServiceId === "string" && |
| 116 | typeof news.serviceId === "string" && |
| 117 | news.serviceId !== oldServiceId |
| 118 | ) { |
| 119 | return { action: "replace" } as const; |
| 120 | } |
| 121 | return undefined; |
| 122 | }), |
| 123 | |
| 124 | read: Effect.fn(function* ({ output, olds }) { |
| 125 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 126 | const acct = output?.accountId ?? accountId; |
| 127 | const prefixId = |
| 128 | output?.prefixId ?? (typeof olds?.prefixId === "string" ? olds.prefixId : undefined); |
| 129 | if (!prefixId) return undefined; |
| 130 | |
| 131 | if (output?.bindingId) { |
| 132 | const observed = yield* getBinding(acct, prefixId, output.bindingId); |
| 133 | return observed ? toAttributes(observed, prefixId, acct) : undefined; |
| 134 | } |
| 135 | // Cold read — (cidr, serviceId) identify a binding uniquely. |
| 136 | const cidr = output?.cidr ?? olds?.cidr; |
| 137 | if (typeof cidr !== "string") return undefined; |
| 138 | const match = yield* findByCidr(acct, prefixId, cidr); |
| 139 | return match ? toAttributes(match, prefixId, acct) : undefined; |
| 140 | }), |
| 141 | |
| 142 | // Service bindings are children of a BYOIP prefix with no account-wide |
| 143 | // enumeration API — fan out across every account prefix and list each |
| 144 | // prefix's bindings, hydrating into the exact `read` shape. |
| 145 | list: Effect.fn(function* () { |
| 146 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 147 | const prefixIds = yield* addressing.listPrefixes.items({ accountId }).pipe( |
| 148 | Stream.runCollect, |
| 149 | Effect.map((chunk) => |
| 150 | Array.from(chunk) |
| 151 | .map((p) => p.id) |
| 152 | .filter((id): id is string => typeof id === "string"), |
| 153 | ), |
| 154 | ); |
| 155 | const perPrefix = yield* Effect.forEach( |
| 156 | prefixIds, |
| 157 | (prefixId) => |
| 158 | addressing.listPrefixServiceBindings.items({ accountId, prefixId }).pipe( |
| 159 | Stream.runCollect, |
| 160 | Effect.map((chunk) => |
| 161 | Array.from(chunk).map((b) => toAttributes(b, prefixId, accountId)), |
| 162 | ), |
| 163 | // The parent prefix vanished between enumeration and listing. |
| 164 | Effect.catchTag("PrefixNotFound", () => Effect.succeed<ServiceBindingAttributes[]>([])), |
| 165 | ), |
| 166 | { concurrency: 10 }, |
| 167 | ); |
| 168 | return perPrefix.flat(); |
| 169 | }), |
| 170 | |
| 171 | reconcile: Effect.fn(function* ({ news, output }) { |
| 172 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 173 | const acct = output?.accountId ?? accountId; |
| 174 | const prefixId = news.prefixId as string; |
| 175 | const serviceId = news.serviceId as string; |
| 176 | |
| 177 | // 1. Observe — by cached id, then by CIDR. |
| 178 | let observed = output?.bindingId |
| 179 | ? yield* getBinding(acct, prefixId, output.bindingId) |
| 180 | : undefined; |
| 181 | if (!observed) { |
| 182 | observed = yield* findByCidr(acct, prefixId, news.cidr); |
| 183 | } |
| 184 | |
| 185 | // 2. Ensure — nothing is mutable, so an observed binding is already |
| 186 | // converged (a serviceId mismatch is a replacement, handled by |
| 187 | // diff). Provisioning is async; return immediately. |
| 188 | if (observed) { |
| 189 | return toAttributes(observed, prefixId, acct); |
| 190 | } |
| 191 | const created = yield* addressing.createPrefixServiceBinding({ |
| 192 | accountId: acct, |
| 193 | prefixId, |
| 194 | cidr: news.cidr, |
| 195 | serviceId, |
| 196 | }); |
| 197 | return toAttributes(created, prefixId, acct); |
| 198 | }), |
| 199 | |
| 200 | delete: Effect.fn(function* ({ output }) { |
| 201 | yield* addressing |
| 202 | .deletePrefixServiceBinding({ |
| 203 | accountId: output.accountId, |
| 204 | prefixId: output.prefixId, |
| 205 | bindingId: output.bindingId, |
| 206 | }) |
| 207 | .pipe(Effect.catchTag(["BindingNotFound", "PrefixNotFound"], () => Effect.void)); |
| 208 | }), |
| 209 | }); |
| 210 | |
| 211 | type ObservedBinding = addressing.GetPrefixServiceBindingResponse; |
| 212 | |
| 213 | /** |
| 214 | * Read a service binding by id, mapping "gone" (`BindingNotFound` / |
| 215 | * `PrefixNotFound`) to `undefined`. |
| 216 | */ |
| 217 | const getBinding = (accountId: string, prefixId: string, bindingId: string) => |
| 218 | addressing |
| 219 | .getPrefixServiceBinding({ accountId, prefixId, bindingId }) |
| 220 | .pipe(Effect.catchTag(["BindingNotFound", "PrefixNotFound"], () => Effect.undefined)); |
| 221 | |
| 222 | /** |
| 223 | * Find a service binding by exact CIDR — unique within a parent prefix. |
| 224 | * The parent prefix being gone reads as "no match". |
| 225 | */ |
| 226 | const findByCidr = (accountId: string, prefixId: string, cidr: string) => |
| 227 | addressing.listPrefixServiceBindings.items({ accountId, prefixId }).pipe( |
| 228 | Stream.runCollect, |
| 229 | Effect.map((chunk) => Array.from(chunk).find((b) => b.cidr === cidr)), |
| 230 | Effect.catchTag("PrefixNotFound", () => Effect.undefined), |
| 231 | ); |
| 232 | |
| 233 | const toAttributes = ( |
| 234 | binding: ObservedBinding, |
| 235 | prefixId: string, |
| 236 | accountId: string, |
| 237 | ): ServiceBindingAttributes => ({ |
| 238 | bindingId: binding.id ?? "", |
| 239 | prefixId, |
| 240 | accountId, |
| 241 | cidr: binding.cidr ?? "", |
| 242 | serviceId: binding.serviceId ?? "", |
| 243 | serviceName: binding.serviceName ?? undefined, |
| 244 | provisioning: { state: binding.provisioning?.state ?? undefined }, |
| 245 | }); |