@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, 04:45 UTC

main
ts 233 lines 9.07 KB
Raw
1 import * as pqe from "@distilled.cloud/cloudflare/origin-post-quantum-encryption";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import * as Effect from "effect/Effect";
5 import * as Predicate from "effect/Predicate";
6 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
7 import type { Providers } from "../Providers.ts";
8 import { listAllZones } from "../Zone/lookup.ts";
9
10 const TypeId = "Cloudflare.OriginPostQuantumEncryption.OriginPostQuantumEncryption" as const;
11 type TypeId = typeof TypeId;
12
13 /**
14 * Value of the Origin Post-Quantum Encryption setting.
15 *
16 * - `"preferred"` — advertise post-quantum key agreement to the origin and
17 * use it whenever the origin supports it.
18 * - `"supported"` — accept post-quantum key agreement if the origin
19 * initiates it (Cloudflare's default).
20 * - `"off"` — never use post-quantum key agreement to the origin.
21 */
22 export type Value = "preferred" | "supported" | "off";
23
24 export type Props = {
25 /**
26 * Zone the Origin Post-Quantum Encryption setting belongs to. Stable —
27 * changing the zone triggers a replacement (the old zone's setting is
28 * restored to the value it had before Alchemy managed it).
29 */
30 zoneId: string;
31 /**
32 * Desired value of the setting. Mutable — updated in place.
33 *
34 * @default "supported"
35 */
36 value?: Value;
37 };
38
39 export type Attributes = {
40 /** Zone the setting belongs to. */
41 zoneId: string;
42 /** Resolved current value of the setting. */
43 value: Value;
44 /**
45 * Whether the setting can be modified on the zone's current plan.
46 */
47 editable: boolean;
48 /** When the setting was last modified, if Cloudflare reports it. */
49 modifiedOn: string | undefined;
50 /**
51 * The value the setting had before Alchemy first managed it. Restored
52 * on destroy, so deleting the resource puts the zone back the way it
53 * was found.
54 */
55 initialValue: Value;
56 };
57
58 export type OriginPostQuantumEncryption = Resource<TypeId, Props, Attributes, never, Providers>;
59
60 /**
61 * Origin Post-Quantum Encryption for a Cloudflare zone
62 * (`/zones/{zone_id}/cache/origin_post_quantum_encryption`).
63 *
64 * Controls whether Cloudflare uses post-quantum (PQ) key agreement on the
65 * TLS connections it makes to your origin. Despite living under the
66 * `/cache/` API path, this is an SSL/origin-connection setting, not a
67 * caching one.
68 *
69 * The setting is a singleton — it always exists on every zone with a
70 * Cloudflare default of `"supported"`, so this resource never creates or
71 * deletes anything physical. Reconcile updates the setting when the
72 * observed value differs from the desired one; destroy restores the value
73 * the setting had before Alchemy first managed it (captured as
74 * `initialValue`).
75 * ### Managing the setting
76 * **Example:** Prefer post-quantum key agreement to the origin
77 * ```typescript
78 * const zone = yield* Cloudflare.Zone.Zone("Site", { name: "example.com" });
79 *
80 * yield* Cloudflare.OriginPostQuantumEncryption.OriginPostQuantumEncryption("OriginPqe", {
81 * zoneId: zone.zoneId,
82 * value: "preferred",
83 * });
84 * ```
85 *
86 * **Example:** Disable post-quantum key agreement to the origin
87 * ```typescript
88 * yield* Cloudflare.OriginPostQuantumEncryption.OriginPostQuantumEncryption("OriginPqe", {
89 * zoneId: zone.zoneId,
90 * value: "off",
91 * });
92 * ```
93 *
94 * **Example:** Pin the Cloudflare default explicitly
95 * ```typescript
96 * yield* Cloudflare.OriginPostQuantumEncryption.OriginPostQuantumEncryption("OriginPqe", {
97 * zoneId: zone.zoneId,
98 * value: "supported",
99 * });
100 * ```
101 *
102 * @see https://developers.cloudflare.com/ssl/origin-configuration/pqc-to-origin/
103 *
104 * @resource
105 * @product Origin Post-Quantum Encryption
106 * @category SSL/TLS & Certificates
107 */
108 export const OriginPostQuantumEncryption = Resource<OriginPostQuantumEncryption>(TypeId, {
109 aliases: ["Cloudflare.OriginPostQuantumEncryption"],
110 });
111
112 /**
113 * Returns true if the given value is an OriginPostQuantumEncryption resource.
114 */
115 export const isOriginPostQuantumEncryption = (
116 value: unknown,
117 ): value is OriginPostQuantumEncryption =>
118 Predicate.hasProperty(value, "Type") && value.Type === TypeId;
119
120 export const OriginPostQuantumEncryptionProvider = () =>
121 Provider.succeed(OriginPostQuantumEncryption, {
122 nuke: { singleton: true },
123 stables: ["zoneId", "initialValue"],
124
125 list: Effect.fn(function* () {
126 const { accountId } = yield* yield* CloudflareEnvironment;
127 // No account-wide API for this zone singleton — enumerate every
128 // zone in the account and read its setting (every zone has one).
129 const allZones = yield* listAllZones(accountId);
130 const rows = yield* Effect.forEach(
131 allZones.map((zone) => zone.id),
132 (zoneId) =>
133 pqe.getOriginPostQuantumEncryption({ zoneId }).pipe(
134 Effect.map((observed) => toAttributes(zoneId, observed, toValue(observed.value))),
135 // Zone gone out-of-band / not resolvable — skip it. (Transient
136 // 403/429 "Authentication error" blips under concurrency are
137 // retried globally by the Cloudflare retry policy, so they never
138 // reach here as a real failure.)
139 Effect.catchTag("InvalidZoneIdentifier", () => Effect.undefined),
140 ),
141 { concurrency: 10 },
142 );
143 return rows.filter((row): row is Attributes => row !== undefined);
144 }),
145
146 diff: Effect.fn(function* ({ olds = {}, news, output }) {
147 const o = olds as Props;
148 const n = news as Props;
149 // zoneId is Input<string>; compare only once both sides are concrete.
150 const oldZoneId = output?.zoneId ?? (typeof o.zoneId === "string" ? o.zoneId : undefined);
151 if (oldZoneId !== undefined && typeof n.zoneId === "string" && oldZoneId !== n.zoneId) {
152 return { action: "replace" } as const;
153 }
154 return undefined;
155 }),
156
157 read: Effect.fn(function* ({ output, olds }) {
158 const zoneId = output?.zoneId ?? (olds?.zoneId as string | undefined);
159 if (!zoneId) return undefined;
160 const observed = yield* pqe.getOriginPostQuantumEncryption({ zoneId }).pipe(
161 // Zone deleted out-of-band — the setting is gone with it.
162 Effect.catchTag("InvalidZoneIdentifier", () => Effect.undefined),
163 );
164 if (observed === undefined) return undefined;
165 // The setting is a singleton that always exists with a Cloudflare
166 // default — there is nothing to "own", so a cold read adopts freely
167 // (never `Unowned`). The observed value at adoption time becomes the
168 // `initialValue` restored on destroy.
169 const initialValue = output !== undefined ? output.initialValue : toValue(observed.value);
170 return toAttributes(zoneId, observed, initialValue);
171 }),
172
173 reconcile: Effect.fn(function* ({ news, output }) {
174 // Inputs have been resolved to concrete strings by Plan.
175 const zoneId = news.zoneId as string;
176 const desired: Value = news.value ?? "supported";
177
178 // 1. Observe — the setting always exists; read its live value.
179 const observed = yield* pqe.getOriginPostQuantumEncryption({ zoneId });
180
181 // 2. Capture — the pre-management value, restored on destroy.
182 // `output` (including an adoption read) already carries it;
183 // otherwise this is our first touch and the observed value is
184 // the zone's original.
185 const initialValue = output !== undefined ? output.initialValue : toValue(observed.value);
186
187 // 3. Sync — update only when the observed value differs.
188 if (toValue(observed.value) === desired) {
189 return toAttributes(zoneId, observed, initialValue);
190 }
191 const updated = yield* pqe.putOriginPostQuantumEncryption({
192 zoneId,
193 value: desired,
194 });
195 return toAttributes(zoneId, updated, initialValue);
196 }),
197
198 delete: Effect.fn(function* ({ output }) {
199 const { zoneId, initialValue } = output;
200 // Observe — if the zone itself is gone, so is the setting.
201 const observed = yield* pqe
202 .getOriginPostQuantumEncryption({ zoneId })
203 .pipe(Effect.catchTag("InvalidZoneIdentifier", () => Effect.undefined));
204 if (observed === undefined) return;
205 // Restore the pre-management value; skip the call when it already
206 // matches (idempotent re-delete after a crashed run).
207 if (toValue(observed.value) === initialValue) return;
208 yield* pqe
209 .putOriginPostQuantumEncryption({ zoneId, value: initialValue })
210 .pipe(Effect.catchTag("InvalidZoneIdentifier", () => Effect.void));
211 }),
212 });
213
214 /**
215 * Narrow the distilled response's open
216 * `"preferred" | "supported" | "off" | (string & {})` value to the closed
217 * triple — Cloudflare only ever returns the three literals for this
218 * setting; anything else collapses to the documented default.
219 */
220 const toValue = (value: string): Value =>
221 value === "preferred" || value === "off" ? value : "supported";
222
223 const toAttributes = (
224 zoneId: string,
225 setting: pqe.GetOriginPostQuantumEncryptionResponse | pqe.PutOriginPostQuantumEncryptionResponse,
226 initialValue: Value,
227 ): Attributes => ({
228 zoneId,
229 value: toValue(setting.value),
230 editable: setting.editable,
231 modifiedOn: setting.modifiedOn ?? undefined,
232 initialValue,
233 });