The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ssl from "@distilled.cloud/cloudflare/ssl"; |
| 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 Schedule from "effect/Schedule"; |
| 8 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { listAllZones } from "../Zone/lookup.ts"; |
| 11 | |
| 12 | const TypeId = "Cloudflare.Ssl.UniversalSsl" as const; |
| 13 | type TypeId = typeof TypeId; |
| 14 | |
| 15 | export type UniversalSslProps = { |
| 16 | /** |
| 17 | * Zone whose Universal SSL setting is managed. Stable — changing the |
| 18 | * zone triggers a replacement (the old zone's setting is restored to |
| 19 | * the value it had before Alchemy managed it). |
| 20 | */ |
| 21 | zoneId: string; |
| 22 | /** |
| 23 | * Whether Universal SSL certificates are issued for the zone. |
| 24 | * |
| 25 | * Disabling removes any currently active Universal SSL certificates |
| 26 | * for the zone from the edge and prevents future Universal SSL |
| 27 | * certificates from being ordered — visitors will see TLS errors |
| 28 | * unless the zone has advanced/custom certificates covering its |
| 29 | * hostnames. |
| 30 | * |
| 31 | * Mutable — patched in place. |
| 32 | */ |
| 33 | enabled: boolean; |
| 34 | }; |
| 35 | |
| 36 | export type UniversalSslAttributes = { |
| 37 | /** Zone the setting belongs to. */ |
| 38 | zoneId: string; |
| 39 | /** Whether Universal SSL is currently enabled for the zone. */ |
| 40 | enabled: boolean; |
| 41 | /** |
| 42 | * The value the setting had before Alchemy first patched it. Restored |
| 43 | * on destroy, so deleting the resource puts the zone back the way it |
| 44 | * was found. |
| 45 | */ |
| 46 | initialEnabled: boolean; |
| 47 | }; |
| 48 | |
| 49 | export type UniversalSsl = Resource< |
| 50 | TypeId, |
| 51 | UniversalSslProps, |
| 52 | UniversalSslAttributes, |
| 53 | never, |
| 54 | Providers |
| 55 | >; |
| 56 | |
| 57 | /** |
| 58 | * The Universal SSL setting of a Cloudflare zone |
| 59 | * (`/zones/{zone_id}/ssl/universal/settings`). |
| 60 | * |
| 61 | * Universal SSL is a zone singleton — the setting always exists (Cloudflare |
| 62 | * defaults it to enabled), so this resource never creates or deletes |
| 63 | * anything physical. Reconcile patches the setting when the observed value |
| 64 | * differs from the desired one; destroy restores the value the setting had |
| 65 | * before Alchemy first managed it (captured as `initialEnabled`). |
| 66 | * |
| 67 | * **Warning:** disabling Universal SSL removes any active Universal SSL |
| 68 | * certificates for the zone from the edge. Visitors will see TLS errors |
| 69 | * unless advanced/custom certificates cover the zone's hostnames. |
| 70 | * ### Managing Universal SSL |
| 71 | * **Example:** Disable Universal SSL for a zone |
| 72 | * ```typescript |
| 73 | * yield* Cloudflare.Ssl.UniversalSsl("UniversalSsl", { |
| 74 | * zoneId: zone.zoneId, |
| 75 | * enabled: false, |
| 76 | * }); |
| 77 | * ``` |
| 78 | * |
| 79 | * **Example:** Pin Universal SSL enabled |
| 80 | * ```typescript |
| 81 | * yield* Cloudflare.Ssl.UniversalSsl("UniversalSsl", { |
| 82 | * zoneId: zone.zoneId, |
| 83 | * enabled: true, |
| 84 | * }); |
| 85 | * ``` |
| 86 | * |
| 87 | * @see https://developers.cloudflare.com/api/resources/ssl/subresources/universal/subresources/settings/ |
| 88 | * |
| 89 | * @resource |
| 90 | * @product SSL/TLS |
| 91 | * @category SSL/TLS & Certificates |
| 92 | */ |
| 93 | export const UniversalSsl = Resource<UniversalSsl>(TypeId); |
| 94 | |
| 95 | /** |
| 96 | * Returns true if the given value is a UniversalSsl resource. |
| 97 | */ |
| 98 | export const isUniversalSsl = (value: unknown): value is UniversalSsl => |
| 99 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 100 | |
| 101 | /** |
| 102 | * The Universal SSL "edit setting" endpoint enforces its OWN per-zone, |
| 103 | * per-operation rate limit ("Rate limit reached for the update operation. |
| 104 | * Please try again in a minute") — much longer than the engine's blanket |
| 105 | * retry budget (~40s). Ride it out patiently on the typed `TooManyRequests` |
| 106 | * within a bounded number of attempts (~120s total) so a concurrent or |
| 107 | * recent patch on the same zone's singleton doesn't fail the operation. |
| 108 | */ |
| 109 | const patchUniversal = (input: { zoneId: string; enabled: boolean }) => |
| 110 | ssl.patchUniversalSetting(input).pipe( |
| 111 | Effect.retry({ |
| 112 | while: (e) => e._tag === "TooManyRequests", |
| 113 | schedule: Schedule.spaced("20 seconds"), |
| 114 | times: 6, |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | export const UniversalSslProvider = () => |
| 119 | Provider.succeed(UniversalSsl, { |
| 120 | nuke: { singleton: true }, |
| 121 | stables: ["zoneId", "initialEnabled"], |
| 122 | |
| 123 | list: Effect.fn(function* () { |
| 124 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 125 | // No account-wide API for this zone singleton — enumerate every |
| 126 | // zone in the account and read its setting (every zone has one). |
| 127 | const allZones = yield* listAllZones(accountId); |
| 128 | const rows = yield* Effect.forEach( |
| 129 | allZones.map((zone) => zone.id), |
| 130 | (zoneId) => |
| 131 | ssl.getUniversalSetting({ zoneId }).pipe( |
| 132 | Effect.map((observed) => { |
| 133 | const enabled = observedEnabled(observed); |
| 134 | // Observed value is the pre-management value at adoption. |
| 135 | return { zoneId, enabled, initialEnabled: enabled }; |
| 136 | }), |
| 137 | // Plan-gated or partial zones reject the route; skip them. |
| 138 | Effect.catchTag("InvalidRoute", () => Effect.undefined), |
| 139 | ), |
| 140 | { concurrency: 10 }, |
| 141 | ); |
| 142 | return rows.filter((row): row is UniversalSslAttributes => row !== undefined); |
| 143 | }), |
| 144 | |
| 145 | diff: Effect.fn(function* ({ olds, news, output }) { |
| 146 | // news is Input<Props> during plan — only compare once resolved. |
| 147 | if (!isResolved(news)) return undefined; |
| 148 | // zoneId is the resource's identity; it is Input<string>, so |
| 149 | // compare only once both sides are concrete. |
| 150 | const oldZoneId = |
| 151 | output?.zoneId ?? (typeof olds?.zoneId === "string" ? olds.zoneId : undefined); |
| 152 | if (oldZoneId !== undefined && typeof news.zoneId === "string" && oldZoneId !== news.zoneId) { |
| 153 | return { action: "replace" } as const; |
| 154 | } |
| 155 | return undefined; |
| 156 | }), |
| 157 | |
| 158 | read: Effect.fn(function* ({ output, olds }) { |
| 159 | const zoneId = output?.zoneId ?? (olds?.zoneId as string | undefined); |
| 160 | if (!zoneId) return undefined; |
| 161 | const observed = yield* ssl.getUniversalSetting({ zoneId }).pipe( |
| 162 | // Zone deleted out-of-band — the setting is gone with it. |
| 163 | Effect.catchTag("InvalidRoute", () => Effect.undefined), |
| 164 | ); |
| 165 | if (observed === undefined) return undefined; |
| 166 | const enabled = observedEnabled(observed); |
| 167 | // Universal SSL is a singleton that always exists with a |
| 168 | // Cloudflare default — there is nothing to "own", so a cold read |
| 169 | // adopts freely (never `Unowned`). The observed value at adoption |
| 170 | // time becomes the `initialEnabled` restored on destroy. |
| 171 | const initialEnabled = output !== undefined ? output.initialEnabled : enabled; |
| 172 | return { zoneId, enabled, initialEnabled }; |
| 173 | }), |
| 174 | |
| 175 | reconcile: Effect.fn(function* ({ news, output }) { |
| 176 | // Inputs have been resolved to concrete strings by Plan. |
| 177 | const zoneId = news.zoneId as string; |
| 178 | |
| 179 | // 1. Observe — the setting always exists; read its live value. |
| 180 | const observed = yield* ssl.getUniversalSetting({ zoneId }); |
| 181 | const enabled = observedEnabled(observed); |
| 182 | |
| 183 | // 2. Capture — the pre-management value, restored on destroy. |
| 184 | // `output` (including an adoption read) already carries it; |
| 185 | // otherwise this is our first touch and the observed value is |
| 186 | // the zone's original. |
| 187 | const initialEnabled = output !== undefined ? output.initialEnabled : enabled; |
| 188 | |
| 189 | // 3. Sync — patch only when the observed value differs. |
| 190 | if (enabled === news.enabled) { |
| 191 | return { zoneId, enabled, initialEnabled }; |
| 192 | } |
| 193 | const patched = yield* patchUniversal({ |
| 194 | zoneId, |
| 195 | enabled: news.enabled, |
| 196 | }); |
| 197 | return { zoneId, enabled: observedEnabled(patched), initialEnabled }; |
| 198 | }), |
| 199 | |
| 200 | delete: Effect.fn(function* ({ output }) { |
| 201 | const { zoneId, initialEnabled } = output; |
| 202 | // Observe — if the zone itself is gone, so is the setting. |
| 203 | const observed = yield* ssl |
| 204 | .getUniversalSetting({ zoneId }) |
| 205 | .pipe(Effect.catchTag("InvalidRoute", () => Effect.undefined)); |
| 206 | if (observed === undefined) return; |
| 207 | // Restore the pre-management value; skip the call when it already |
| 208 | // matches (idempotent re-delete after a crashed run). |
| 209 | if (observedEnabled(observed) === initialEnabled) return; |
| 210 | yield* patchUniversal({ zoneId, enabled: initialEnabled }).pipe( |
| 211 | Effect.catchTag("InvalidRoute", () => Effect.void), |
| 212 | ); |
| 213 | }), |
| 214 | }); |
| 215 | |
| 216 | /** |
| 217 | * Normalize the distilled response's `enabled?: boolean | null` to a |
| 218 | * concrete boolean — Cloudflare's default for Universal SSL is enabled. |
| 219 | */ |
| 220 | const observedEnabled = ( |
| 221 | setting: ssl.GetUniversalSettingResponse | ssl.PatchUniversalSettingResponse, |
| 222 | ): boolean => setting.enabled ?? true; |