The best way to control Claude Code, Codex and more from any computer or phone

main
ts 212 lines 7.36 KB
Raw
1 import * as emailRouting from "@distilled.cloud/cloudflare/email-routing";
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 Schedule from "effect/Schedule";
7 import * as Stream from "effect/Stream";
8 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
9 import type { Providers } from "../Providers.ts";
10
11 export type AddressProps = {
12 /**
13 * The email address to register as a verified destination on the
14 * account. Cloudflare sends a verification email to this address; the
15 * recipient must click the link before the address can receive routed
16 * mail or be used as a verified sender.
17 *
18 * Changing this property triggers a replacement.
19 */
20 email: string;
21 };
22
23 export type Address = Resource<
24 "Cloudflare.Email.Address",
25 AddressProps,
26 {
27 addressId: string;
28 email: string;
29 accountId: string;
30 verified: boolean;
31 verifiedAt: string | undefined;
32 created: string | undefined;
33 modified: string | undefined;
34 },
35 never,
36 Providers
37 >;
38
39 /**
40 * A verified destination email address on the account.
41 *
42 * Destination addresses are account-scoped (not zone-scoped). They are used
43 * as forwarding targets in `Rule` actions and can also serve as the
44 * `destinationAddress` on a `send_email` Worker binding.
45 * ### Registering an Address
46 * **Example:** Register a destination address
47 * ```typescript
48 * const ops = yield* Cloudflare.Email.Address("Ops", {
49 * email: "ops@example.com",
50 * });
51 * ```
52 *
53 * Cloudflare sends a verification email when the address is first created.
54 * The address must be verified before it can receive routed mail.
55 *
56 * @resource
57 * @product Email
58 * @category Email
59 */
60 export const Address = Resource<Address>("Cloudflare.Email.Address", {
61 aliases: ["Cloudflare.EmailAddress"],
62 });
63
64 const toAttrs = (
65 accountId: string,
66 result: {
67 id?: string | null;
68 email?: string | null;
69 verified?: string | null;
70 created?: string | null;
71 modified?: string | null;
72 },
73 ) => ({
74 addressId: result.id ?? "",
75 email: result.email ?? "",
76 accountId,
77 verified: Boolean(result.verified),
78 verifiedAt: result.verified ?? undefined,
79 created: result.created ?? undefined,
80 modified: result.modified ?? undefined,
81 });
82
83 // Authoritative account-wide lookup of a destination address by email. The
84 // per-address `getAddress` identifier is the opaque address id (not the email),
85 // so the only reliable way to find an address by its email is to enumerate the
86 // account collection (the same call `list()` exhausts).
87 const findByEmail = (accountId: string, email: string) =>
88 emailRouting.listAddresses.pages({ accountId }).pipe(
89 Stream.runCollect,
90 Effect.map((chunk) =>
91 Array.from(chunk)
92 .flatMap((page) => page.result ?? [])
93 .map((addr) => toAttrs(accountId, addr))
94 .find((a) => a.email === email),
95 ),
96 );
97
98 export const AddressProvider = () =>
99 Provider.succeed(Address, {
100 stables: ["addressId", "accountId", "email"],
101 // Account collection: destination addresses are enumerable account-wide.
102 list: Effect.fn(function* () {
103 const { accountId } = yield* yield* CloudflareEnvironment;
104 return yield* emailRouting.listAddresses.pages({ accountId }).pipe(
105 Stream.runCollect,
106 Effect.map((chunk) =>
107 Array.from(chunk).flatMap((page) =>
108 (page.result ?? []).map((addr) => toAttrs(accountId, addr)),
109 ),
110 ),
111 );
112 }),
113 diff: Effect.fn(function* ({ news, output }) {
114 const { accountId } = yield* yield* CloudflareEnvironment;
115 if (!output) return undefined;
116 if (output.accountId !== accountId) {
117 return { action: "replace" } as const;
118 }
119 if (!isResolved(news)) return undefined;
120 if (news.email !== output.email) {
121 return { action: "replace" } as const;
122 }
123 return undefined;
124 }),
125 read: Effect.fn(function* ({ output, olds }) {
126 const { accountId } = yield* yield* CloudflareEnvironment;
127 const identifier =
128 output?.addressId ?? (olds?.email ? encodeURIComponent(olds.email) : undefined);
129 if (!identifier) return undefined;
130 const acct = output?.accountId ?? accountId;
131 return yield* emailRouting
132 .getAddress({
133 accountId: acct,
134 destinationAddressIdentifier: identifier,
135 })
136 .pipe(
137 Effect.map((r) => toAttrs(acct, r)),
138 Effect.orElseSucceed(() => undefined),
139 );
140 }),
141 reconcile: Effect.fn(function* ({ news, output }) {
142 const { accountId } = yield* yield* CloudflareEnvironment;
143 const acct = output?.accountId ?? accountId;
144 const email = news.email;
145
146 // Observe — by addressId if known, else by email lookup.
147 let observed: ReturnType<typeof toAttrs> | undefined = output?.addressId
148 ? yield* emailRouting
149 .getAddress({
150 accountId: acct,
151 destinationAddressIdentifier: output.addressId,
152 })
153 .pipe(
154 Effect.map((r) => toAttrs(acct, r)),
155 Effect.orElseSucceed(() => undefined),
156 )
157 : undefined;
158
159 if (!observed) {
160 observed = yield* emailRouting
161 .getAddress({
162 accountId: acct,
163 destinationAddressIdentifier: encodeURIComponent(email),
164 })
165 .pipe(
166 Effect.map((r) => toAttrs(acct, r)),
167 Effect.orElseSucceed(() => undefined),
168 );
169 }
170
171 // Ensure — register the address if it doesn't already exist.
172 if (!observed) {
173 observed = yield* emailRouting.createAddress({ accountId: acct, email }).pipe(
174 Effect.map((created) => toAttrs(acct, created)),
175 // Cloudflare rate-limits verification emails per destination
176 // address ("Verification email has been sent too recently"). When
177 // the same address was (re)created recently the address record
178 // already exists account-wide, so adopt it instead of failing.
179 // Re-raise if the address genuinely isn't present.
180 Effect.catchTag("TooManyRequests", (error) =>
181 findByEmail(acct, email).pipe(
182 Effect.flatMap((found) => (found ? Effect.succeed(found) : Effect.fail(error))),
183 ),
184 ),
185 );
186 }
187
188 return observed;
189 }),
190 delete: Effect.fn(function* ({ output }) {
191 if (!output?.addressId) return;
192 // Idempotent on the typed not-found; transient failures are retried
193 // bounded and a persistent failure surfaces — a swallowed failure
194 // silently leaks the destination address. Cloudflare refuses to
195 // delete an address for ~15 minutes after creation
196 // (`EmailAddressCreatedTooRecently`, code 2032) — retrying is
197 // pointless within a deploy's lifetime, so that error fails fast:
198 // either destroy later or retain the address (`RemovalPolicy.retain`).
199 yield* emailRouting
200 .deleteAddress({
201 accountId: output.accountId,
202 destinationAddressIdentifier: output.addressId,
203 })
204 .pipe(
205 Effect.catchTag("EmailAddressNotFound", () => Effect.void),
206 Effect.retry({
207 while: (e) => e._tag !== "EmailAddressCreatedTooRecently",
208 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(8)]),
209 }),
210 );
211 }),
212 });