1import * as storage from "@distilled.cloud/gcp/storage_v1";
2import * as Data from "effect/Data";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import { createHash } from "node:crypto";
7import { Unowned } from "../../AdoptPolicy.ts";
8import { isResolved } from "../../Diff.ts";
9import * as Provider from "../../Provider.ts";
10import { Resource } from "../../Resource.ts";
11import { tagRecord } from "../../Tags.ts";
12import { initialCwd } from "../../Util/Node.ts";
13import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
14import type { Providers } from "../Providers.ts";
15import { makeObjectMedia } from "./ObjectMedia.ts";
16
17export 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
52export 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 */
114export const Object = Resource<Object>("GCP.Storage.Object");
115
116export class ObjectContentInvalid extends Data.TaggedError(
117 "GCP.Storage.ObjectContentInvalid",
118)<{
119 key: string;
120 message: string;
121}> {}
122
123const TEXT_TYPES: Record<string, string> = {
124 ".html": "text/html",
125 ".htm": "text/html",
126 ".css": "text/css",
127 ".js": "text/javascript",
128 ".mjs": "text/javascript",
129 ".json": "application/json",
130 ".map": "application/json",
131 ".txt": "text/plain",
132 ".md": "text/markdown",
133 ".csv": "text/csv",
134 ".xml": "application/xml",
135 ".webmanifest": "application/manifest+json",
136};
137
138const BINARY_TYPES: Record<string, string> = {
139 ".svg": "image/svg+xml",
140 ".png": "image/png",
141 ".jpg": "image/jpeg",
142 ".jpeg": "image/jpeg",
143 ".gif": "image/gif",
144 ".webp": "image/webp",
145 ".avif": "image/avif",
146 ".ico": "image/x-icon",
147 ".woff": "font/woff",
148 ".woff2": "font/woff2",
149 ".ttf": "font/ttf",
150 ".otf": "font/otf",
151 ".pdf": "application/pdf",
152 ".wasm": "application/wasm",
153 ".mp4": "video/mp4",
154 ".webm": "video/webm",
155 ".mp3": "audio/mpeg",
156};
157
158/**
159 * `Content-Type` for an object name, by extension. Text types carry
160 * `charset=utf-8`. Returns `undefined` for unknown extensions.
161 */
162export const inferContentType = (key: string): string | undefined => {
163 const base = key.split("/").pop() ?? key;
164 const dot = base.lastIndexOf(".");
165 if (dot <= 0) return undefined;
166 const ext = base.slice(dot).toLowerCase();
167 const text = TEXT_TYPES[ext];
168 if (text !== undefined) return `${text}; charset=utf-8`;
169 return BINARY_TYPES[ext];
170};
171
172const desiredContentType = (props: ObjectProps) =>
173 props.contentType ??
174 inferContentType(props.key) ??
175 (typeof props.content === "string"
176 ? "text/plain; charset=utf-8"
177 : "application/octet-stream");
178
179const desiredBytes = Effect.fn(function* (props: ObjectProps) {
180 if ((props.content === undefined) === (props.file === undefined)) {
181 return yield* new ObjectContentInvalid({
182 key: props.key,
183 message: "exactly one of `content` or `file` must be set",
184 });
185 }
186 if (props.file !== undefined) {
187 const fs = yield* FileSystem.FileSystem;
188 const path = yield* Path.Path;
189 return yield* fs.readFile(path.resolve(initialCwd, props.file));
190 }
191 const content = props.content!;
192 return typeof content === "string"
193 ? yield* Effect.sync(() => new TextEncoder().encode(content))
194 : content;
195});
196
197const md5Of = (bytes: Uint8Array) =>
198 Effect.sync(() => createHash("md5").update(bytes).digest("base64"));
199
200const publicUrl = (bucketName: string, key: string) =>
201 `https://storage.googleapis.com/${bucketName}/${key
202 .split("/")
203 .map(encodeURIComponent)
204 .join("/")}`;
205
206const toAttrs = (
207 bucketName: string,
208 key: string,
209 object: storage.Storage_Object,
210) => ({
211 bucketName,
212 key,
213 generation: object.generation,
214 md5Hash: object.md5Hash,
215 size: Number(object.size ?? 0),
216 contentType: object.contentType,
217 cacheControl: object.cacheControl,
218 mediaLink: object.mediaLink,
219 url: publicUrl(bucketName, key),
220});
221
222const observe = (bucketName: string, key: string) =>
223 storage
224 .getObjects({ bucket: bucketName, object: key })
225 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
226
227const sameMetadata = (
228 observed: Record<string, string>,
229 desired: Record<string, string>,
230) =>
231 globalThis.Object.entries(desired).every(
232 ([key, value]) => observed[key] === value,
233 );
234
235export const ObjectProvider = () =>
236 Provider.effect(
237 Object,
238 Effect.gen(function* () {
239 const media = yield* makeObjectMedia;
240
241 return {
242 stables: ["bucketName", "key", "url"],
243
244 diff: Effect.fn(function* ({ news, output }) {
245 if (!isResolved(news) || output === undefined) return undefined;
246 if (
247 news.bucketName !== output.bucketName ||
248 news.key !== output.key
249 ) {
250 return { action: "replace" as const, deleteFirst: false };
251 }
252 // The observed MD5 + served headers fully describe the object, so
253 // a `file` edit is caught by hashing it and a re-serialized
254 // `content` with identical bytes stays a no-op.
255 const bytes = yield* desiredBytes(news);
256 const md5 = yield* md5Of(bytes);
257 const unchanged =
258 md5 === output.md5Hash &&
259 desiredContentType(news) === output.contentType &&
260 news.cacheControl === output.cacheControl;
261 return unchanged
262 ? { action: "noop" as const }
263 : { action: "update" as const, stables: ["bucketName", "key"] };
264 }),
265
266 read: Effect.fn(function* ({ fqn, olds, output }) {
267 const bucketName = output?.bucketName ?? olds?.bucketName;
268 const key = output?.key ?? olds?.key;
269 if (bucketName === undefined || key === undefined) return undefined;
270 const object = yield* observe(bucketName, key);
271 if (object === undefined) return undefined;
272 const attrs = toAttrs(bucketName, key, object);
273 return (yield* hasAlchemyLabels(fqn, tagRecord(object.metadata)))
274 ? attrs
275 : Unowned(attrs);
276 }),
277
278 reconcile: Effect.fn(function* ({ fqn, news }) {
279 const bytes = yield* desiredBytes(news);
280 const md5 = yield* md5Of(bytes);
281 const contentType = desiredContentType(news);
282 const ownership = yield* createInternalLabels(fqn);
283
284 // Observe — the live object is the baseline, not olds/output.
285 const current = yield* observe(news.bucketName, news.key);
286 const inSync =
287 current !== undefined &&
288 current.md5Hash === md5 &&
289 current.contentType === contentType &&
290 current.cacheControl === news.cacheControl &&
291 sameMetadata(tagRecord(current.metadata), ownership);
292 if (current !== undefined && inSync) {
293 return toAttrs(news.bucketName, news.key, current);
294 }
295
296 // Ensure — a multipart upload replaces content and metadata in
297 // one write, so a stale object converges in a single call.
298 const uploaded = yield* media.upload(news.bucketName, {
299 name: news.key,
300 body: bytes,
301 contentType,
302 cacheControl: news.cacheControl,
303 metadata: ownership,
304 });
305 return toAttrs(news.bucketName, news.key, uploaded);
306 }),
307
308 delete: Effect.fn(function* ({ output }) {
309 yield* storage
310 .deleteObjects({ bucket: output.bucketName, object: output.key })
311 .pipe(Effect.catchTag("NotFound", () => Effect.void));
312 }),
313 };
314 }),
315 );