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

main
ts 305 lines 10 KB
Raw
1 import * as siteVerification from "@distilled.cloud/gcp/siteVerification_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 DEFAULT_SITE_TYPE,
12 defaultVerificationMethod,
13 deleteWebResource,
14 findWebResource,
15 getWebResource,
16 getWebResourceToken,
17 hasOwnershipMarker,
18 listWebResources,
19 sameOwners,
20 sameText,
21 sortedOwners,
22 toGeneratedIdentifier,
23 toPathId,
24 type SiteType,
25 type VerificationMethod,
26 } from "./internal.ts";
27
28 export type { SiteType, VerificationMethod };
29 export { getWebResourceToken };
30
31 export type WebResourceProps = {
32 /**
33 * Site or domain identifier. For `SITE`, a URL such as
34 * `https://www.example.com/`. For `INET_DOMAIN`, a domain name such
35 * as `example.com`. If omitted, a unique hostname under
36 * `alchemy-site-verification.test` is generated from the stack,
37 * stage, and logical id so `list` / nuke can find it. Immutable —
38 * changing it replaces the web resource.
39 */
40 identifier?: string;
41 /**
42 * Resource type. `SITE` is a URL; `INET_DOMAIN` is a domain.
43 * Immutable — changing it replaces the web resource.
44 * @default "SITE"
45 */
46 siteType?: SiteType;
47 /**
48 * Verification method Google should check when inserting the
49 * resource. Must match the token previously placed on the site
50 * (from {@link getWebResourceToken}). Sites use `FILE`, `META`,
51 * `ANALYTICS`, or `TAG_MANAGER`. Domains use `DNS_TXT` or
52 * `DNS_CNAME`. Create-only — Google does not store it after
53 * verification.
54 * @default "FILE" for SITE, "DNS_TXT" for INET_DOMAIN
55 */
56 verificationMethod?: VerificationMethod;
57 /**
58 * Email addresses of verified owners. The authenticated user is
59 * always an owner after a successful insert. Omitted means leave
60 * the owner list unchanged on update. Replacing the list in place
61 * notifies every owner by email.
62 */
63 owners?: string[];
64 };
65
66 export type WebResource = Resource<
67 "GCP.SiteVerification.WebResource",
68 WebResourceProps,
69 {
70 /**
71 * API id for get / update / delete. Google returns this
72 * percent-encoded (`http%3A%2F%2F…`).
73 */
74 webResourceId: string;
75 /** Project id used when the resource was reconciled. */
76 project: string;
77 /** Site or domain identifier as stored by Google. */
78 identifier: string;
79 /** `SITE` or `INET_DOMAIN`. */
80 siteType: string;
81 /**
82 * Verification method used on insert, if known. The API does not
83 * return this after create.
84 */
85 verificationMethod: string | undefined;
86 /** Email addresses of verified owners. */
87 owners: string[];
88 },
89 never,
90 Providers
91 >;
92
93 /**
94 * A Google Site Verification web resource (verified site or domain).
95 *
96 * Site Verification has no labels or description field, so Alchemy
97 * stamps ownership into generated identifiers under
98 * `alchemy-site-verification.test` for `list` / nuke. User-provided
99 * identifiers are identity — they are not rewritten — and are not
100 * listed for nuke unless they contain that host. `identifier` and
101 * `siteType` are identity (changing either replaces the resource).
102 * `owners` update in place via `webResource.update`.
103 *
104 * Place the verification token from {@link getWebResourceToken} on
105 * the site or domain before the first reconcile. Insert checks that
106 * token; it fails with `BadRequest` if Google cannot find it. Creating
107 * web resources as a service account requires a user OAuth token with
108 * the `https://www.googleapis.com/auth/siteverification` scope (or
109 * domain-wide delegation).
110 *
111 * ### Creating a Web Resource
112 * **Example:** Verify a site with a FILE token
113 * ```typescript
114 * const token = yield* GCP.SiteVerification.getWebResourceToken({
115 * identifier: "https://www.example.com/",
116 * siteType: "SITE",
117 * verificationMethod: "FILE",
118 * });
119 * // Place token.token as a file at the site root, then:
120 * const site = yield* GCP.SiteVerification.WebResource("Docs", {
121 * identifier: "https://www.example.com/",
122 * siteType: "SITE",
123 * verificationMethod: "FILE",
124 * });
125 * ```
126 *
127 * **Example:** Verify a domain with DNS TXT
128 * ```typescript
129 * const token = yield* GCP.SiteVerification.getWebResourceToken({
130 * identifier: "example.com",
131 * siteType: "INET_DOMAIN",
132 * verificationMethod: "DNS_TXT",
133 * });
134 * const domain = yield* GCP.SiteVerification.WebResource("Apex", {
135 * identifier: "example.com",
136 * siteType: "INET_DOMAIN",
137 * verificationMethod: "DNS_TXT",
138 * });
139 * ```
140 *
141 * ### Updating Owners
142 * Re-declare the same logical id with changed props; the engine keeps the
143 * physical resource and updates it in place.
144 *
145 * **Example:** Delegate a co-owner
146 * ```typescript
147 * const site = yield* GCP.SiteVerification.WebResource("Docs", {
148 * identifier: "https://www.example.com/",
149 * siteType: "SITE",
150 * owners: ["admin@example.com", "teammate@example.com"],
151 * });
152 * ```
153 *
154 * @resource
155 * @category SiteVerification
156 */
157 export const WebResource = Resource<WebResource>("GCP.SiteVerification.WebResource");
158
159 export class WebResourceNotResolved extends Data.TaggedError(
160 "GCP.SiteVerification.WebResourceNotResolved",
161 )<{
162 identifier: string;
163 siteType: string;
164 }> {}
165
166 const toAttrs = (
167 resource: siteVerification.SiteVerificationWebResourceResource,
168 project: string,
169 verificationMethod: string | undefined,
170 ) => ({
171 webResourceId: resource.id ?? toPathId(resource.site?.identifier),
172 project,
173 identifier: resource.site?.identifier ?? "",
174 siteType: resource.site?.type ?? DEFAULT_SITE_TYPE,
175 verificationMethod,
176 owners: sortedOwners(resource.owners),
177 });
178
179 const desiredSiteType = (
180 news: WebResourceProps,
181 current: siteVerification.SiteVerificationWebResourceResource | undefined,
182 outputType: string | undefined,
183 ) => news.siteType ?? current?.site?.type ?? outputType ?? DEFAULT_SITE_TYPE;
184
185 const desiredMethod = (
186 news: WebResourceProps,
187 siteType: string,
188 outputMethod: string | undefined,
189 ) => news.verificationMethod ?? outputMethod ?? defaultVerificationMethod(siteType);
190
191 export const WebResourceProvider = () =>
192 Provider.succeed(WebResource, {
193 stables: ["webResourceId", "project", "identifier", "siteType"],
194
195 diff: Effect.fn(function* ({ news, olds, output }) {
196 if (!isResolved(news)) return undefined;
197 const previousType = olds?.siteType ?? output?.siteType;
198 if (
199 previousType !== undefined &&
200 news.siteType !== undefined &&
201 news.siteType !== previousType
202 ) {
203 return { action: "replace" as const, deleteFirst: false };
204 }
205 const previousId = olds?.identifier ?? output?.identifier;
206 if (
207 previousId !== undefined &&
208 news.identifier !== undefined &&
209 !sameText(news.identifier, previousId) &&
210 !sameText(
211 news.identifier.endsWith("/") ? news.identifier : `${news.identifier}/`,
212 previousId,
213 )
214 ) {
215 return { action: "replace" as const, deleteFirst: false };
216 }
217 return undefined;
218 }),
219
220 read: Effect.fn(function* ({ id, olds, output }) {
221 const env = yield* GcpEnvironment.current;
222 const siteType = olds?.siteType ?? output?.siteType ?? DEFAULT_SITE_TYPE;
223 const identifier = yield* toGeneratedIdentifier(
224 id,
225 siteType,
226 olds?.identifier,
227 output?.identifier,
228 );
229 let existing = yield* getWebResource(output?.webResourceId);
230 const foundById = existing !== undefined;
231 if (existing === undefined) {
232 existing = yield* findWebResource(identifier, siteType);
233 }
234 if (existing === undefined) return undefined;
235 const attrs = toAttrs(
236 existing,
237 env.project,
238 olds?.verificationMethod ?? output?.verificationMethod,
239 );
240 return foundById || hasOwnershipMarker(existing.site?.identifier) ? attrs : Unowned(attrs);
241 }),
242
243 list: () =>
244 Effect.gen(function* () {
245 const env = yield* GcpEnvironment.current;
246 const items = yield* listWebResources();
247 return items
248 .filter((item) => hasOwnershipMarker(item.site?.identifier))
249 .map((item) => toAttrs(item, env.project, undefined));
250 }),
251
252 reconcile: Effect.fn(function* ({ id, news, output }) {
253 const env = yield* GcpEnvironment.current;
254 const siteType = desiredSiteType(news, undefined, output?.siteType);
255 const identifier = yield* toGeneratedIdentifier(
256 id,
257 siteType,
258 news.identifier,
259 output?.identifier,
260 );
261 const verificationMethod = desiredMethod(news, siteType, output?.verificationMethod);
262
263 let current = yield* getWebResource(output?.webResourceId);
264 if (current === undefined) {
265 current = yield* findWebResource(identifier, siteType);
266 }
267
268 if (current === undefined) {
269 const created = yield* siteVerification
270 .insertWebResource({
271 verificationMethod,
272 body: {
273 site: { identifier, type: siteType },
274 owners: news.owners,
275 },
276 })
277 .pipe(Effect.catchTag("Conflict", () => findWebResource(identifier, siteType)));
278 current = created ?? undefined;
279 }
280
281 if (current === undefined) {
282 return yield* new WebResourceNotResolved({ identifier, siteType });
283 }
284
285 if (news.owners !== undefined && !sameOwners(current.owners, news.owners)) {
286 const pathId = toPathId(current.id ?? output?.webResourceId);
287 current = yield* siteVerification.updateWebResource({
288 id: pathId,
289 body: {
290 site: {
291 identifier: current.site?.identifier ?? identifier,
292 type: current.site?.type ?? siteType,
293 },
294 owners: sortedOwners(news.owners),
295 },
296 });
297 }
298
299 return toAttrs(current, env.project, verificationMethod);
300 }),
301
302 delete: Effect.fn(function* ({ output }) {
303 yield* deleteWebResource(output.webResourceId);
304 }),
305 });