@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 185 lines 7.02 KB
Raw
1 import * as workers from "@distilled.cloud/cloudflare/workers";
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
9 const TypeId = "Cloudflare.Workers.Subdomain" as const;
10 type TypeId = typeof TypeId;
11
12 export type SubdomainProps = {
13 /**
14 * The account's `workers.dev` subdomain name (the `<subdomain>` in
15 * `https://<script>.<subdomain>.workers.dev`). Must contain only
16 * lowercase ASCII letters, digits, and hyphens.
17 *
18 * Mutable — renamed in place via PUT. **Renaming changes the URL of
19 * every deployed Worker on the account that uses a `workers.dev`
20 * subdomain**, so treat changes with extreme care.
21 */
22 subdomain: string;
23 };
24
25 export type SubdomainAttributes = {
26 /** The Cloudflare account the subdomain belongs to. */
27 accountId: string;
28 /** The account's current `workers.dev` subdomain name. */
29 subdomain: string;
30 /**
31 * The subdomain name the account had before Alchemy first managed this
32 * singleton, or `undefined` if the account had no subdomain registered.
33 * Restored on destroy (or the subdomain is removed entirely when there
34 * was none).
35 */
36 initialSubdomain: string | undefined;
37 };
38
39 export type Subdomain = Resource<TypeId, SubdomainProps, SubdomainAttributes, never, Providers>;
40
41 /**
42 * The account-wide `workers.dev` subdomain singleton
43 * (`/accounts/{account_id}/workers/subdomain`). Every Worker with
44 * `workers.dev` enabled is served at
45 * `https://<script>.<subdomain>.workers.dev`.
46 *
47 * Each account has at most one subdomain — "creating" this resource claims
48 * (or renames to) the requested name. Subdomain names are globally unique
49 * across all Cloudflare accounts; claiming a taken name fails with the
50 * typed `SubdomainAlreadyExists` error.
51 *
52 * Destroy is capture-and-restore: the subdomain is renamed back to the
53 * value it had before Alchemy first managed it. If the account had no
54 * subdomain at first touch, destroy removes it entirely.
55 *
56 * **Warning:** renaming or removing the subdomain immediately changes the
57 * URL of every deployed Worker on the account that relies on
58 * `workers.dev`. Only manage this resource on accounts where that is
59 * acceptable.
60 * ### Managing the subdomain
61 * **Example:** Pin the account's workers.dev subdomain
62 * ```typescript
63 * const sub = yield* Cloudflare.Workers.Subdomain("Subdomain", {
64 * subdomain: "my-team",
65 * });
66 * // Workers are now served from https://<script>.my-team.workers.dev
67 * ```
68 *
69 * @see https://developers.cloudflare.com/workers/configuration/routing/workers-dev/
70 *
71 * @resource
72 * @product Workers
73 * @category Workers & Compute
74 */
75 export const Subdomain = Resource<Subdomain>(TypeId);
76
77 /**
78 * Returns true if the given value is a Subdomain resource.
79 */
80 export const isSubdomain = (value: unknown): value is Subdomain =>
81 Predicate.hasProperty(value, "Type") && value.Type === TypeId;
82
83 export const SubdomainProvider = () =>
84 Provider.succeed(Subdomain, {
85 nuke: { singleton: true },
86 stables: ["accountId", "initialSubdomain"],
87
88 diff: Effect.fn(function* ({ output }) {
89 const { accountId } = yield* yield* CloudflareEnvironment;
90 // The singleton's identity is the account it lives on; the name
91 // itself is renamed in place via PUT.
92 if (output !== undefined && output.accountId !== accountId) {
93 return { action: "replace" } as const;
94 }
95 return undefined;
96 }),
97
98 read: Effect.fn(function* ({ output }) {
99 const { accountId } = yield* yield* CloudflareEnvironment;
100 const acct = output?.accountId ?? accountId;
101 const observed = yield* getSubdomain(acct);
102 if (observed === undefined) return undefined;
103 // The subdomain is an account singleton — there is nothing to
104 // "own", so a cold read adopts freely (never `Unowned`). The
105 // observed name at adoption time becomes the `initialSubdomain`
106 // restored on destroy.
107 return {
108 accountId: acct,
109 subdomain: observed,
110 initialSubdomain: output !== undefined ? output.initialSubdomain : observed,
111 };
112 }),
113
114 list: Effect.fn(function* () {
115 const { accountId } = yield* yield* CloudflareEnvironment;
116 // Account singleton — at most one workers.dev subdomain per account.
117 // Mirror `read` with no prior output: the observed name is also the
118 // `initialSubdomain`. Return a one-element array when registered, `[]`
119 // when the account has never claimed a subdomain.
120 const observed = yield* getSubdomain(accountId);
121 if (observed === undefined) return [];
122 return [
123 {
124 accountId,
125 subdomain: observed,
126 initialSubdomain: observed,
127 },
128 ];
129 }),
130
131 reconcile: Effect.fn(function* ({ news, output }) {
132 const { accountId } = yield* yield* CloudflareEnvironment;
133
134 // 1. Observe — read the account's live subdomain (undefined when
135 // the account has never registered one).
136 const observed = yield* getSubdomain(accountId);
137
138 // 2. Capture — the pre-management name, restored on destroy.
139 // `output` (including an adoption read) already carries it;
140 // otherwise this is our first touch and the observed name is
141 // the account's original (undefined = none existed).
142 const initialSubdomain = output !== undefined ? output.initialSubdomain : observed;
143
144 // 3. Sync — claim/rename only when the observed name differs.
145 if (observed === news.subdomain) {
146 return { accountId, subdomain: observed, initialSubdomain };
147 }
148 const updated = yield* workers.putSubdomain({
149 accountId,
150 subdomain: news.subdomain,
151 });
152 return { accountId, subdomain: updated.subdomain, initialSubdomain };
153 }),
154
155 delete: Effect.fn(function* ({ output }) {
156 const { accountId, initialSubdomain } = output;
157 // Observe — already gone (or never existed) means nothing to do.
158 const observed = yield* getSubdomain(accountId);
159 // Restore the pre-management name; skip the call when it already
160 // matches (idempotent re-delete after a crashed run).
161 if (observed === initialSubdomain) return;
162 if (initialSubdomain === undefined) {
163 // The account had no subdomain before we claimed one — remove it.
164 yield* workers
165 .deleteSubdomain({ accountId })
166 .pipe(Effect.catchTag("SubdomainNotFound", () => Effect.void));
167 return;
168 }
169 if (observed === undefined) return;
170 yield* workers.putSubdomain({
171 accountId,
172 subdomain: initialSubdomain,
173 });
174 }),
175 });
176
177 /**
178 * Read the account's workers.dev subdomain, mapping "no subdomain
179 * registered" (`SubdomainNotFound`, HTTP 404) to `undefined`.
180 */
181 const getSubdomain = (accountId: string) =>
182 workers.getSubdomain({ accountId }).pipe(
183 Effect.map((r) => r.subdomain),
184 Effect.catchTag("SubdomainNotFound", () => Effect.undefined),
185 );