@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, 04:45 UTC

main
ts 78 lines 2.92 KB
Raw
1 import * as storage from "@distilled.cloud/gcp/storage_v1";
2 import * as Effect from "effect/Effect";
3 import * as GCPBindings from "../GCPBindings.ts";
4 import type { Bucket } from "./Bucket.ts";
5 import { GrantOnBucket } from "./ObjectHttp.ts";
6 import { makeObjectMedia } from "./ObjectMedia.ts";
7 import type { ReadBucketClient } from "./ReadBucket.ts";
8 import type { WriteBucketClient } from "./WriteBucket.ts";
9
10 /**
11 * Shared HTTP scaffolding for the Storage Read/Write/ReadWrite bindings:
12 * resolves the distilled operations and media endpoints once at Layer
13 * construction and grants `role` on the bound bucket at deploy time.
14 *
15 * NOT exported from `index.ts`.
16 */
17 export const makeStorageBucketHelpers = Effect.gen(function* () {
18 const media = yield* makeObjectMedia;
19 const getObject = yield* GCPBindings.withCredentials(storage.getObjects);
20 const listObjects = yield* GCPBindings.withCredentials(storage.listObjects);
21 const deleteObject = yield* GCPBindings.withCredentials(storage.deleteObjects);
22
23 const makeRead = (bucketName: Effect.Effect<string>): ReadBucketClient => ({
24 head: (object) =>
25 bucketName.pipe(
26 Effect.flatMap((bucket) => getObject({ bucket, object })),
27 Effect.catchTag("NotFound", () => Effect.undefined),
28 ),
29 get: (object, options) =>
30 bucketName.pipe(
31 Effect.flatMap((bucket) =>
32 media.download({ bucket, object, generation: options?.generation }),
33 ),
34 Effect.catchTag("GCP.Storage.ObjectNotFound", () => Effect.undefined),
35 ),
36 list: (options) =>
37 bucketName.pipe(
38 Effect.flatMap((bucket) => listObjects({ bucket, ...options })),
39 Effect.map((page) => ({
40 objects: page.items ?? [],
41 prefixes: page.prefixes ?? [],
42 nextPageToken: page.nextPageToken,
43 })),
44 ),
45 });
46
47 const makeWrite = (bucketName: Effect.Effect<string>): WriteBucketClient => ({
48 put: (name, body, options) =>
49 bucketName.pipe(Effect.flatMap((bucket) => media.upload(bucket, { ...options, name, body }))),
50 delete: (object) =>
51 bucketName.pipe(
52 Effect.flatMap((bucket) => deleteObject({ bucket, object })),
53 Effect.asVoid,
54 Effect.catchTag("NotFound", () => Effect.void),
55 ),
56 });
57
58 return { makeRead, makeWrite };
59 });
60
61 /** Build a bucket binding that grants `role` and returns `makeClient`'s client. */
62 export const makeStorageBucketBinding = <Client>(options: {
63 tag: string;
64 role: string;
65 makeClient: (
66 helpers: Effect.Success<typeof makeStorageBucketHelpers>,
67 bucketName: Effect.Effect<string>,
68 ) => Client;
69 }) =>
70 Effect.gen(function* () {
71 const grantOnBucket = yield* GrantOnBucket;
72 const helpers = yield* makeStorageBucketHelpers;
73 return Effect.fn(function* (bucket: Bucket) {
74 yield* grantOnBucket(options.tag, bucket, options.role);
75 const bucketName = yield* bucket.bucketName;
76 return options.makeClient(helpers, bucketName);
77 });
78 });