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

main
ts 108 lines 3.1 KB
Raw
1 import * as s3 from "@distilled.cloud/aws/s3";
2 import * as Effect from "effect/Effect";
3 import * as Option from "effect/Option";
4 import * as Schedule from "effect/Schedule";
5 import {
6 createAssetsBucket,
7 ensureAssetsBucketTags,
8 lookupAssetsBucket,
9 lookupAssetsBuckets,
10 } from "./Assets.ts";
11
12 /**
13 * Bootstrap the AWS environment by creating the assets bucket.
14 *
15 * This is idempotent - running it multiple times is safe.
16 * The bucket is tagged and later discovered by tag lookup instead of by name.
17 */
18 export const bootstrap = Effect.fn(function* () {
19 const existingBucket = yield* lookupAssetsBucket;
20
21 if (Option.isSome(existingBucket)) {
22 yield* ensureAssetsBucketTags(existingBucket.value);
23 yield* Effect.logInfo(`Assets bucket already exists: ${existingBucket.value}`);
24 return { bucketName: existingBucket.value, created: false };
25 }
26
27 const bucketName = yield* createAssetsBucket;
28 return { bucketName, created: true };
29 });
30
31 export const destroyBootstrap = Effect.fn(function* () {
32 const bucketNames = yield* lookupAssetsBuckets;
33
34 for (const bucketName of bucketNames) {
35 yield* Effect.logInfo(`Destroying assets bucket: ${bucketName}`);
36 yield* deleteAllObjects(bucketName);
37 yield* s3.deleteBucket({ Bucket: bucketName }).pipe(
38 Effect.retry({
39 while: (e) => e._tag === "OperationAborted" || e._tag === "ServiceUnavailable",
40 schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(10)]),
41 }),
42 );
43 }
44
45 return {
46 bucketNames,
47 destroyed: bucketNames.length,
48 };
49 });
50
51 /** Delete every object, version and delete marker in a bucket. */
52 export const deleteAllObjects = Effect.fn(function* (bucketName: string) {
53 let continuationToken: string | undefined;
54 do {
55 const listResponse = yield* s3.listObjectsV2({
56 Bucket: bucketName,
57 ContinuationToken: continuationToken,
58 });
59
60 if (listResponse.Contents && listResponse.Contents.length > 0) {
61 yield* s3.deleteObjects({
62 Bucket: bucketName,
63 Delete: {
64 Objects: listResponse.Contents.map((obj) => ({
65 Key: obj.Key!,
66 })),
67 Quiet: true,
68 },
69 });
70 }
71
72 continuationToken = listResponse.NextContinuationToken;
73 } while (continuationToken);
74
75 let keyMarker: string | undefined;
76 let versionIdMarker: string | undefined;
77 do {
78 const listVersionsResponse = yield* s3.listObjectVersions({
79 Bucket: bucketName,
80 KeyMarker: keyMarker,
81 VersionIdMarker: versionIdMarker,
82 });
83
84 const objectsToDelete = [
85 ...(listVersionsResponse.Versions ?? []).map((v) => ({
86 Key: v.Key!,
87 VersionId: v.VersionId,
88 })),
89 ...(listVersionsResponse.DeleteMarkers ?? []).map((dm) => ({
90 Key: dm.Key!,
91 VersionId: dm.VersionId,
92 })),
93 ];
94
95 if (objectsToDelete.length > 0) {
96 yield* s3.deleteObjects({
97 Bucket: bucketName,
98 Delete: {
99 Objects: objectsToDelete,
100 Quiet: true,
101 },
102 });
103 }
104
105 keyMarker = listVersionsResponse.NextKeyMarker;
106 versionIdMarker = listVersionsResponse.NextVersionIdMarker;
107 } while (keyMarker);
108 });