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

main
ts 299 lines 10.2 KB
Raw
1 import { createHash } from "node:crypto";
2 import * as storage from "@distilled.cloud/gcp/storage_v1";
3 import { Unowned } from "alchemy/AdoptPolicy";
4 import { isResolved } from "alchemy/Diff";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { tagRecord } from "alchemy/Tags";
8 import { initialCwd } from "alchemy/Util/Node";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as FileSystem from "effect/FileSystem";
12 import * as Path from "effect/Path";
13 import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
14 import type { Providers } from "../Providers.ts";
15 import { makeObjectMedia } from "./ObjectMedia.ts";
16
17 export type ObjectProps = {
18 /**
19 * Name of the bucket that holds the object. Changing it replaces the
20 * object (the new one is written before the old one is deleted).
21 */
22 bucketName: string;
23 /**
24 * Object name (key), e.g. `index.html` or `assets/app.js`. Changing it
25 * replaces the object.
26 */
27 key: string;
28 /**
29 * Inline object content. Strings are encoded as UTF-8. Exactly one of
30 * `content` or `file` must be set.
31 */
32 content?: string | Uint8Array;
33 /**
34 * Path of a local file to upload, resolved against the working directory
35 * the deploy started in. The file is re-hashed on every plan, so edits
36 * re-upload without any prop change.
37 */
38 file?: string;
39 /**
40 * `Content-Type` served with the object. Inferred from the key's
41 * extension when omitted (`.html` → `text/html; charset=utf-8`, …).
42 * @default inferred from `key`, else `text/plain; charset=utf-8` for string content and `application/octet-stream` otherwise
43 */
44 contentType?: string;
45 /**
46 * `Cache-Control` served with the object. When omitted, Cloud Storage
47 * serves publicly readable objects with `public, max-age=3600`.
48 */
49 cacheControl?: string;
50 };
51
52 export type Object = Resource<
53 "GCP.Storage.Object",
54 ObjectProps,
55 {
56 /** Name of the bucket that holds the object. */
57 bucketName: string;
58 /** Object name (key). */
59 key: string;
60 /** Content generation of the live object. */
61 generation: string | undefined;
62 /** Base64 MD5 of the object bytes, as reported by Cloud Storage. */
63 md5Hash: string | undefined;
64 /** Size of the object in bytes. */
65 size: number;
66 /** `Content-Type` the object is served with. */
67 contentType: string | undefined;
68 /** `Cache-Control` the object is served with. */
69 cacheControl: string | undefined;
70 /** Authenticated media download link. */
71 mediaLink: string | undefined;
72 /**
73 * Path-style public URL (`https://storage.googleapis.com/{bucket}/{key}`).
74 * Only readable anonymously when the bucket grants `allUsers` read.
75 */
76 url: string;
77 },
78 never,
79 Providers
80 >;
81
82 /**
83 * A single object in a Cloud Storage bucket whose content is declared as
84 * infrastructure — inline `content` or a local `file`.
85 *
86 * Reconcile observes the live object and uploads only when its MD5,
87 * `Content-Type`, `Cache-Control`, or ownership metadata differ from the
88 * desired state. Use {@link Files} to upload a whole directory.
89 *
90 * ### Uploading content
91 * **Example:** Inline content
92 * ```typescript
93 * const bucket = yield* GCP.Storage.Bucket("assets", { forceDestroy: true });
94 * yield* GCP.Storage.Object("Robots", {
95 * bucketName: bucket.bucketName,
96 * key: "robots.txt",
97 * content: "User-agent: *\nAllow: /\n",
98 * });
99 * ```
100 *
101 * **Example:** A local file with caching
102 * ```typescript
103 * yield* GCP.Storage.Object("Logo", {
104 * bucketName: bucket.bucketName,
105 * key: "logo.svg",
106 * file: "./public/logo.svg",
107 * cacheControl: "public, max-age=31536000, immutable",
108 * });
109 * ```
110 *
111 * @resource
112 * @category Storage
113 */
114 export const Object = Resource<Object>("GCP.Storage.Object");
115
116 export class ObjectContentInvalid extends Data.TaggedError("GCP.Storage.ObjectContentInvalid")<{
117 key: string;
118 message: string;
119 }> {}
120
121 const TEXT_TYPES: Record<string, string> = {
122 ".html": "text/html",
123 ".htm": "text/html",
124 ".css": "text/css",
125 ".js": "text/javascript",
126 ".mjs": "text/javascript",
127 ".json": "application/json",
128 ".map": "application/json",
129 ".txt": "text/plain",
130 ".md": "text/markdown",
131 ".csv": "text/csv",
132 ".xml": "application/xml",
133 ".webmanifest": "application/manifest+json",
134 };
135
136 const BINARY_TYPES: Record<string, string> = {
137 ".svg": "image/svg+xml",
138 ".png": "image/png",
139 ".jpg": "image/jpeg",
140 ".jpeg": "image/jpeg",
141 ".gif": "image/gif",
142 ".webp": "image/webp",
143 ".avif": "image/avif",
144 ".ico": "image/x-icon",
145 ".woff": "font/woff",
146 ".woff2": "font/woff2",
147 ".ttf": "font/ttf",
148 ".otf": "font/otf",
149 ".pdf": "application/pdf",
150 ".wasm": "application/wasm",
151 ".mp4": "video/mp4",
152 ".webm": "video/webm",
153 ".mp3": "audio/mpeg",
154 };
155
156 /**
157 * `Content-Type` for an object name, by extension. Text types carry
158 * `charset=utf-8`. Returns `undefined` for unknown extensions.
159 */
160 export const inferContentType = (key: string): string | undefined => {
161 const base = key.split("/").pop() ?? key;
162 const dot = base.lastIndexOf(".");
163 if (dot <= 0) return undefined;
164 const ext = base.slice(dot).toLowerCase();
165 const text = TEXT_TYPES[ext];
166 if (text !== undefined) return `${text}; charset=utf-8`;
167 return BINARY_TYPES[ext];
168 };
169
170 const desiredContentType = (props: ObjectProps) =>
171 props.contentType ??
172 inferContentType(props.key) ??
173 (typeof props.content === "string" ? "text/plain; charset=utf-8" : "application/octet-stream");
174
175 const desiredBytes = Effect.fn(function* (props: ObjectProps) {
176 if ((props.content === undefined) === (props.file === undefined)) {
177 return yield* new ObjectContentInvalid({
178 key: props.key,
179 message: "exactly one of `content` or `file` must be set",
180 });
181 }
182 if (props.file !== undefined) {
183 const fs = yield* FileSystem.FileSystem;
184 const path = yield* Path.Path;
185 return yield* fs.readFile(path.resolve(initialCwd, props.file));
186 }
187 const content = props.content!;
188 return typeof content === "string"
189 ? yield* Effect.sync(() => new TextEncoder().encode(content))
190 : content;
191 });
192
193 const md5Of = (bytes: Uint8Array) =>
194 Effect.sync(() => createHash("md5").update(bytes).digest("base64"));
195
196 const publicUrl = (bucketName: string, key: string) =>
197 `https://storage.googleapis.com/${bucketName}/${key
198 .split("/")
199 .map(encodeURIComponent)
200 .join("/")}`;
201
202 const toAttrs = (bucketName: string, key: string, object: storage.Storage_Object) => ({
203 bucketName,
204 key,
205 generation: object.generation,
206 md5Hash: object.md5Hash,
207 size: Number(object.size ?? 0),
208 contentType: object.contentType,
209 cacheControl: object.cacheControl,
210 mediaLink: object.mediaLink,
211 url: publicUrl(bucketName, key),
212 });
213
214 const observe = (bucketName: string, key: string) =>
215 storage
216 .getObjects({ bucket: bucketName, object: key })
217 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
218
219 const sameMetadata = (observed: Record<string, string>, desired: Record<string, string>) =>
220 globalThis.Object.entries(desired).every(([key, value]) => observed[key] === value);
221
222 export const ObjectProvider = () =>
223 Provider.effect(
224 Object,
225 Effect.gen(function* () {
226 const media = yield* makeObjectMedia;
227
228 return {
229 stables: ["bucketName", "key", "url"],
230
231 diff: Effect.fn(function* ({ news, output }) {
232 if (!isResolved(news) || output === undefined) return undefined;
233 if (news.bucketName !== output.bucketName || news.key !== output.key) {
234 return { action: "replace" as const, deleteFirst: false };
235 }
236 // The observed MD5 + served headers fully describe the object, so
237 // a `file` edit is caught by hashing it and a re-serialized
238 // `content` with identical bytes stays a no-op.
239 const bytes = yield* desiredBytes(news);
240 const md5 = yield* md5Of(bytes);
241 const unchanged =
242 md5 === output.md5Hash &&
243 desiredContentType(news) === output.contentType &&
244 news.cacheControl === output.cacheControl;
245 return unchanged
246 ? { action: "noop" as const }
247 : { action: "update" as const, stables: ["bucketName", "key"] };
248 }),
249
250 read: Effect.fn(function* ({ fqn, olds, output }) {
251 const bucketName = output?.bucketName ?? olds?.bucketName;
252 const key = output?.key ?? olds?.key;
253 if (bucketName === undefined || key === undefined) return undefined;
254 const object = yield* observe(bucketName, key);
255 if (object === undefined) return undefined;
256 const attrs = toAttrs(bucketName, key, object);
257 return (yield* hasAlchemyLabels(fqn, tagRecord(object.metadata)))
258 ? attrs
259 : Unowned(attrs);
260 }),
261
262 reconcile: Effect.fn(function* ({ fqn, news }) {
263 const bytes = yield* desiredBytes(news);
264 const md5 = yield* md5Of(bytes);
265 const contentType = desiredContentType(news);
266 const ownership = yield* createInternalLabels(fqn);
267
268 // Observe — the live object is the baseline, not olds/output.
269 const current = yield* observe(news.bucketName, news.key);
270 const inSync =
271 current !== undefined &&
272 current.md5Hash === md5 &&
273 current.contentType === contentType &&
274 current.cacheControl === news.cacheControl &&
275 sameMetadata(tagRecord(current.metadata), ownership);
276 if (current !== undefined && inSync) {
277 return toAttrs(news.bucketName, news.key, current);
278 }
279
280 // Ensure — a multipart upload replaces content and metadata in
281 // one write, so a stale object converges in a single call.
282 const uploaded = yield* media.upload(news.bucketName, {
283 name: news.key,
284 body: bytes,
285 contentType,
286 cacheControl: news.cacheControl,
287 metadata: ownership,
288 });
289 return toAttrs(news.bucketName, news.key, uploaded);
290 }),
291
292 delete: Effect.fn(function* ({ output }) {
293 yield* storage
294 .deleteObjects({ bucket: output.bucketName, object: output.key })
295 .pipe(Effect.catchTag("NotFound", () => Effect.void));
296 }),
297 };
298 }),
299 );