@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 292 lines 9.03 KB
Raw
1 import { createHash } from "node:crypto";
2 import { Credentials, fromCredentials } from "@distilled.cloud/aws/Credentials";
3 import * as AwsEndpoint from "@distilled.cloud/aws/Endpoint";
4 import type { RegionName } from "@distilled.cloud/aws/Region";
5 import * as s3 from "@distilled.cloud/aws/s3";
6 import * as Provider from "alchemy/Provider";
7 import { Resource } from "alchemy/Resource";
8 import { initialCwd } from "alchemy/Util/Node";
9 import { cacheControlOf, contentTypeOf } from "alchemy/Website/assets";
10 import * as Effect from "effect/Effect";
11 import * as FileSystem from "effect/FileSystem";
12 import * as FetchHttpClient from "effect/http/FetchHttpClient";
13 import type * as HttpClient from "effect/http/HttpClient";
14 import * as Layer from "effect/Layer";
15 import * as Path from "effect/Path";
16 import * as Redacted from "effect/Redacted";
17 import * as Stream from "effect/Stream";
18 import type { Bucket } from "../Bucket.ts";
19 import { TigrisCredentialsMissing } from "../Errors.ts";
20 import type { Providers } from "../Providers.ts";
21
22 const s3Concurrency = 16;
23
24 /**
25 * Unwrap Tigris {@link Bucket} attrs for the S3 client. Credentials and
26 * the endpoint are `Redacted<string>`; names are plain strings. This is
27 * Fly Bucket plumbing, not website MIME — it stays here because this
28 * resource is the only Website helper that speaks Tigris.
29 */
30 const asPlain = (value: unknown): string | undefined => {
31 if (typeof value === "string" && value.length > 0) return value;
32 if (Redacted.isRedacted(value)) {
33 const inner = Redacted.value(value);
34 return typeof inner === "string" && inner.length > 0 ? inner : undefined;
35 }
36 return undefined;
37 };
38
39 export interface AssetDeploymentProps {
40 /**
41 * Destination Tigris bucket (a {@link Bucket} resource).
42 */
43 bucket: Bucket;
44 /**
45 * Local directory to upload (framework `clientDirectory`).
46 */
47 sourcePath: string;
48 /**
49 * Optional key prefix within the bucket.
50 */
51 prefix?: string;
52 /**
53 * Delete observed keys under the prefix that are not in this deploy.
54 * @default true
55 */
56 purge?: boolean;
57 }
58
59 export interface AssetDeployment extends Resource<
60 "Fly.Website.AssetDeployment",
61 AssetDeploymentProps,
62 {
63 bucketName: string;
64 prefix: string;
65 version: string;
66 fileCount: number;
67 files: string[];
68 },
69 never,
70 Providers
71 > {}
72
73 /**
74 * Upload a local directory into a public Tigris bucket for Fly Website
75 * statics. HTML is never cached; everything else is immutable.
76 *
77 * @resource
78 */
79 export const AssetDeployment = Resource<AssetDeployment>("Fly.Website.AssetDeployment");
80
81 const normalizePrefix = (prefix: string | undefined) =>
82 prefix ? prefix.replace(/^\/+|\/+$/g, "") : "";
83
84 const scopeOf = (bucket: Bucket) =>
85 Effect.gen(function* () {
86 const bucketName = asPlain(bucket.bucketName) ?? asPlain(bucket.name);
87 const accessKeyId = asPlain(bucket.accessKeyId);
88 const secretAccessKey = asPlain(bucket.secretAccessKey);
89 const endpoint = asPlain(bucket.endpoint);
90 const region = (asPlain(bucket.region) ?? "auto") as RegionName;
91 if (
92 bucketName === undefined ||
93 accessKeyId === undefined ||
94 secretAccessKey === undefined ||
95 endpoint === undefined
96 ) {
97 return yield* new TigrisCredentialsMissing({
98 resourceName: bucketName ?? bucket.LogicalId,
99 });
100 }
101 return { bucketName, accessKeyId, secretAccessKey, endpoint, region };
102 });
103
104 const withTigris = <A, E>(
105 scope: {
106 accessKeyId: string;
107 secretAccessKey: string;
108 endpoint: string;
109 region: RegionName;
110 },
111 operation: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>,
112 ) =>
113 operation.pipe(
114 Effect.provide(
115 Layer.mergeAll(
116 fromCredentials(
117 {
118 accessKeyId: Redacted.make(scope.accessKeyId),
119 secretAccessKey: Redacted.make(scope.secretAccessKey),
120 },
121 scope.region,
122 ),
123 AwsEndpoint.of(scope.endpoint),
124 FetchHttpClient.layer,
125 ),
126 ),
127 );
128
129 const walkFiles = Effect.fn(function* (root: string) {
130 const fs = yield* FileSystem.FileSystem;
131 const path = yield* Path.Path;
132 if (!(yield* fs.exists(root))) return [] as string[];
133 const names = yield* fs.readDirectory(root, { recursive: true });
134 const files = yield* Effect.forEach(
135 names,
136 (name) =>
137 Effect.gen(function* () {
138 const full = path.join(root, name);
139 const stat = yield* fs.stat(full);
140 return stat.type === "File" ? name.replaceAll("\\", "/") : undefined;
141 }),
142 { concurrency: "unbounded" },
143 );
144 return files
145 .filter((name): name is string => name !== undefined)
146 .sort((a, b) => a.localeCompare(b));
147 });
148
149 const listObserved = (
150 scope: {
151 bucketName: string;
152 accessKeyId: string;
153 secretAccessKey: string;
154 endpoint: string;
155 region: RegionName;
156 },
157 prefix: string,
158 ) =>
159 withTigris(
160 scope,
161 s3.listObjectsV2
162 .pages({
163 Bucket: scope.bucketName,
164 Prefix: prefix.length > 0 ? `${prefix}/` : undefined,
165 })
166 .pipe(
167 Stream.flatMap((page) => Stream.fromIterable(page.Contents ?? [])),
168 Stream.runFold(
169 () => new Map<string, string | undefined>(),
170 (observed, object) => {
171 if (object.Key !== undefined) {
172 observed.set(object.Key, object.ETag);
173 }
174 return observed;
175 },
176 ),
177 ),
178 );
179
180 const deleteObject = (
181 scope: {
182 bucketName: string;
183 accessKeyId: string;
184 secretAccessKey: string;
185 endpoint: string;
186 region: RegionName;
187 },
188 key: string,
189 ) =>
190 withTigris(
191 scope,
192 s3.deleteObject({
193 Bucket: scope.bucketName,
194 Key: key,
195 }),
196 ).pipe(Effect.catchTag("NoSuchKey", () => Effect.void));
197
198 export const AssetDeploymentProvider = () =>
199 Provider.effect(
200 AssetDeployment,
201 Effect.succeed({
202 list: () => Effect.succeed([]),
203 read: Effect.fn(function* ({ output }) {
204 return output;
205 }),
206 reconcile: Effect.fn(function* ({ news, session }) {
207 const fs = yield* FileSystem.FileSystem;
208 const path = yield* Path.Path;
209 const scope = yield* scopeOf(news.bucket);
210 const prefix = normalizePrefix(news.prefix);
211 const root = path.isAbsolute(news.sourcePath)
212 ? news.sourcePath
213 : path.resolve(initialCwd, news.sourcePath);
214 const files = yield* walkFiles(root);
215 const observed = yield* listObserved(scope, prefix);
216 const prepared = yield* Effect.forEach(
217 files,
218 (relative) =>
219 Effect.gen(function* () {
220 const body = yield* fs.readFile(path.join(root, relative));
221 const key = prefix.length > 0 ? `${prefix}/${relative}` : relative;
222 const contentType = contentTypeOf(relative);
223 const cacheControl = cacheControlOf(relative);
224 return { relative, key, body, contentType, cacheControl };
225 }),
226 { concurrency: "unbounded" },
227 );
228 const version = yield* Effect.sync(() => {
229 const hash = createHash("sha256");
230 for (const file of prepared) {
231 hash.update(file.relative);
232 hash.update(file.body);
233 hash.update(file.contentType);
234 hash.update(file.cacheControl);
235 }
236 return hash.digest("hex");
237 });
238 const desired = new Set(prepared.map((file) => file.key));
239 yield* Effect.all(
240 prepared.flatMap((file) => {
241 const expected = createHash("md5").update(file.body).digest("hex");
242 const etag = observed.get(file.key)?.replace(/^"|"$/g, "");
243 if (etag === expected) return [];
244 return [
245 withTigris(
246 scope,
247 s3.putObject({
248 Bucket: scope.bucketName,
249 Key: file.key,
250 Body: file.body,
251 ContentType: file.contentType,
252 CacheControl: file.cacheControl,
253 }),
254 ),
255 ];
256 }),
257 { concurrency: s3Concurrency },
258 );
259
260 if (news.purge ?? true) {
261 yield* Effect.forEach(
262 [...observed.keys()].filter((key) => !desired.has(key)),
263 (key) => deleteObject(scope, key),
264 { concurrency: s3Concurrency },
265 );
266 }
267
268 const output = {
269 bucketName: scope.bucketName,
270 prefix,
271 version,
272 fileCount: files.length,
273 files: prepared.map((file) => file.relative),
274 };
275 yield* session.note(
276 `Uploaded ${output.fileCount} file(s) to tigris://${output.bucketName}/${output.prefix}`,
277 );
278 return output;
279 }),
280 delete: Effect.fn(
281 function* ({ olds, output }) {
282 if (!(olds.purge ?? true)) return;
283 const scope = yield* scopeOf(olds.bucket);
284 const observed = yield* listObserved(scope, output.prefix);
285 yield* Effect.forEach([...observed.keys()], (key) => deleteObject(scope, key), {
286 concurrency: s3Concurrency,
287 });
288 },
289 Effect.catchTag("NoSuchBucket", () => Effect.void),
290 ),
291 }),
292 );