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

main
ts 237 lines 8.19 KB
Raw
1 import * as Credentials from "@distilled.cloud/aws/Credentials";
2 import * as Endpoint from "@distilled.cloud/aws/Endpoint";
3 import { presignS3Url } from "@distilled.cloud/aws/Presign";
4 import * as Region from "@distilled.cloud/aws/Region";
5 import * as S3 from "@distilled.cloud/aws/s3";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Layer from "effect/Layer";
10 import * as Redacted from "effect/Redacted";
11
12 export interface StorageConfig {
13 /** Branch-specific, path-style S3 endpoint. */
14 endpoint: string;
15 /** Signing region returned by Neon. */
16 region: string;
17 /** Branch credential token ID, not an AWS account key. */
18 accessKeyId: string;
19 /** Branch credential S3 secret. */
20 secretAccessKey: Redacted.Redacted<string>;
21 }
22
23 export class StoragePaginationError extends Data.TaggedError("StoragePaginationError")<{
24 message: string;
25 }> {}
26 export class StorageDeleteError extends Data.TaggedError("StorageDeleteError")<{
27 message: string;
28 }> {}
29
30 /** Internal S3 transport. Endpoint overrides retain the bucket in the request path. */
31 export const storageLayer = (config: StorageConfig) =>
32 Layer.mergeAll(
33 Endpoint.of(config.endpoint),
34 Region.of(config.region as Region.RegionName),
35 Layer.succeed(
36 Credentials.Credentials,
37 Effect.succeed({
38 accessKeyId: Redacted.make(config.accessKeyId),
39 secretAccessKey: config.secretAccessKey,
40 sessionToken: undefined,
41 region: config.region as Region.RegionName,
42 }),
43 ),
44 );
45
46 export type StoragePutOptions = Pick<
47 S3.PutObjectRequest,
48 | "ContentType"
49 | "CacheControl"
50 | "ContentDisposition"
51 | "ContentEncoding"
52 | "ContentLanguage"
53 | "Metadata"
54 >;
55
56 /** Construct clients once; no connections or disposable resources are retained. */
57 export const makeStorageClient = Effect.fn(function* (config: StorageConfig, bucket: string) {
58 const http = yield* HttpClient.HttpClient;
59 const layer = Layer.mergeAll(storageLayer(config), Layer.succeed(HttpClient.HttpClient, http));
60 const provide = <A, E, R>(effect: Effect.Effect<A, E, R>) => effect.pipe(Effect.provide(layer));
61 return {
62 get: (key: string) =>
63 provide(S3.getObject({ Bucket: bucket, Key: key })).pipe(
64 Effect.catchTag("NoSuchKey", () => Effect.undefined),
65 ),
66 head: (key: string) =>
67 provide(S3.headObject({ Bucket: bucket, Key: key })).pipe(
68 Effect.catchTag("NotFound", () => Effect.undefined),
69 ),
70 list: (
71 options: {
72 prefix?: string;
73 delimiter?: string;
74 cursor?: string;
75 limit?: number;
76 } = {},
77 ) =>
78 provide(
79 S3.listObjectsV2({
80 Bucket: bucket,
81 Prefix: options.prefix,
82 Delimiter: options.delimiter,
83 ContinuationToken: options.cursor,
84 MaxKeys: options.limit,
85 }),
86 ),
87 put: (key: string, body: string | Uint8Array, options: StoragePutOptions = {}) =>
88 provide(S3.putObject({ Bucket: bucket, Key: key, Body: body, ...options })),
89 delete: (key: string) => provide(S3.deleteObject({ Bucket: bucket, Key: key })),
90 deleteMany: (keys: string[]) =>
91 Effect.gen(function* () {
92 const deleted: S3.DeletedObject[] = [];
93 // Large Neon batches can exceed the gateway's 60-second request limit.
94 for (let offset = 0; offset < keys.length; offset += 100) {
95 const result = yield* provide(
96 S3.deleteObjects({
97 Bucket: bucket,
98 Delete: {
99 Objects: keys.slice(offset, offset + 100).map((Key) => ({ Key })),
100 },
101 }),
102 );
103 if (result.Errors?.length)
104 return yield* new StorageDeleteError({
105 message: `S3 refused ${result.Errors.length} object deletions`,
106 });
107 deleted.push(...(result.Deleted ?? []));
108 }
109 return { Deleted: deleted } satisfies S3.DeleteObjectsOutput;
110 }),
111 createMultipartUpload: (key: string, options: StoragePutOptions = {}) =>
112 provide(S3.createMultipartUpload({ Bucket: bucket, Key: key, ...options })),
113 uploadPart: (key: string, uploadId: string, partNumber: number, body: Uint8Array) =>
114 provide(
115 S3.uploadPart({
116 Bucket: bucket,
117 Key: key,
118 UploadId: uploadId,
119 PartNumber: partNumber,
120 Body: body,
121 }),
122 ),
123 completeMultipartUpload: (key: string, uploadId: string, parts: S3.CompletedPart[]) =>
124 provide(
125 S3.completeMultipartUpload({
126 Bucket: bucket,
127 Key: key,
128 UploadId: uploadId,
129 MultipartUpload: { Parts: parts },
130 }),
131 ),
132 abortMultipartUpload: (key: string, uploadId: string) =>
133 provide(
134 S3.abortMultipartUpload({
135 Bucket: bucket,
136 Key: key,
137 UploadId: uploadId,
138 }),
139 ).pipe(Effect.catchTag("NoSuchUpload", () => Effect.void)),
140 listMultipartUploads: (keyMarker?: string, uploadIdMarker?: string) =>
141 provide(
142 S3.listMultipartUploads({
143 Bucket: bucket,
144 KeyMarker: keyMarker,
145 UploadIdMarker: uploadIdMarker,
146 }),
147 ),
148 listParts: (key: string, uploadId: string, partNumberMarker?: string) =>
149 provide(
150 S3.listParts({
151 Bucket: bucket,
152 Key: key,
153 UploadId: uploadId,
154 PartNumberMarker: partNumberMarker,
155 }),
156 ),
157 presign: (
158 key: string,
159 method: "GET" | "PUT",
160 options: { expiresIn?: number; contentType?: string } = {},
161 ) => provide(presignS3Url({ bucket, key, method, ...options })),
162 getTags: () =>
163 provide(S3.getBucketTagging({ Bucket: bucket })).pipe(
164 Effect.catchTag("NoSuchTagSet", () => Effect.succeed({ TagSet: [] })),
165 ),
166 putTags: (tags: Record<string, string>) =>
167 provide(
168 S3.putBucketTagging({
169 Bucket: bucket,
170 Tagging: {
171 TagSet: Object.entries(tags).map(([Key, Value]) => ({
172 Key,
173 Value,
174 })),
175 },
176 }),
177 ),
178 getCors: () =>
179 provide(S3.getBucketCors({ Bucket: bucket })).pipe(
180 Effect.catchTag("NoSuchCORSConfiguration", () => Effect.succeed({ CORSRules: [] })),
181 ),
182 putCors: (rules: S3.CORSRule[]) =>
183 provide(
184 S3.putBucketCors({
185 Bucket: bucket,
186 CORSConfiguration: { CORSRules: rules },
187 }),
188 ),
189 deleteCors: () => provide(S3.deleteBucketCors({ Bucket: bucket })),
190 deleteBucket: () =>
191 provide(S3.deleteBucket({ Bucket: bucket })).pipe(
192 Effect.catchTag("NoSuchBucket", () => Effect.void),
193 ),
194 };
195 });
196
197 export type StorageClient = Effect.Success<ReturnType<typeof makeStorageClient>>;
198
199 /** Empty only this bucket and branch, checking pagination and partial failures. */
200 export const emptyStorageBucket = Effect.fn(function* (client: StorageClient) {
201 let cursor: string | undefined;
202 const seen = new Set<string>();
203 do {
204 const page = yield* client.list({ cursor, limit: 1000 });
205 const keys = (page.Contents ?? []).flatMap((object) =>
206 object.Key === undefined ? [] : [object.Key],
207 );
208 if (keys.length) yield* client.deleteMany(keys);
209 cursor = page.IsTruncated ? page.NextContinuationToken : undefined;
210 if (page.IsTruncated && (!cursor || seen.has(cursor))) {
211 return yield* new StoragePaginationError({
212 message: "Object listing did not advance",
213 });
214 }
215 if (cursor) seen.add(cursor);
216 } while (cursor);
217 let keyMarker: string | undefined;
218 let uploadMarker: string | undefined;
219 const uploadsSeen = new Set<string>();
220 for (;;) {
221 const page = yield* client.listMultipartUploads(keyMarker, uploadMarker);
222 for (const upload of page.Uploads ?? []) {
223 if (upload.Key && upload.UploadId)
224 yield* client.abortMultipartUpload(upload.Key, upload.UploadId);
225 }
226 if (!page.IsTruncated) break;
227 keyMarker = page.NextKeyMarker;
228 uploadMarker = page.NextUploadIdMarker;
229 const marker = `${keyMarker ?? ""}/${uploadMarker ?? ""}`;
230 if (!keyMarker || uploadsSeen.has(marker)) {
231 return yield* new StoragePaginationError({
232 message: "Multipart listing did not advance",
233 });
234 }
235 uploadsSeen.add(marker);
236 }
237 });