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
| 1 | import * as S3 from "@distilled.cloud/aws/s3"; |
| 2 | import * as Binding from "alchemy/Binding"; |
| 3 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import type { Bucket } from "./Bucket.ts"; |
| 7 | import { makeBucketBinding } from "./BucketBinding.ts"; |
| 8 | import type { |
| 9 | BucketCredentials, |
| 10 | BucketError, |
| 11 | BucketObject, |
| 12 | BucketObjectBody, |
| 13 | GetOptions, |
| 14 | ListOptions, |
| 15 | ListResult, |
| 16 | PresignGetOptions, |
| 17 | } from "./BucketTypes.ts"; |
| 18 | import { |
| 19 | makeBucketAccess, |
| 20 | objectBodyFrom, |
| 21 | objectFrom, |
| 22 | objectFromListEntry, |
| 23 | rangeHeader, |
| 24 | toBucketError, |
| 25 | type BucketAccess, |
| 26 | } from "./internal/BucketClient.ts"; |
| 27 | |
| 28 | export interface ReadBucket extends Binding.Service< |
| 29 | ReadBucket, |
| 30 | "Prisma.ReadBucket", |
| 31 | (bucket: Bucket) => Effect.Effect<ReadBucketClient> |
| 32 | > {} |
| 33 | |
| 34 | /** |
| 35 | * Read-only client for a Prisma Object Store bucket. It deliberately exposes |
| 36 | * no write operations — see the role caveat on {@link ReadBucketBinding}. |
| 37 | */ |
| 38 | export interface ReadBucketClient { |
| 39 | /** |
| 40 | * Read an object's metadata without downloading it. Resolves `null` when the |
| 41 | * key does not exist. |
| 42 | */ |
| 43 | head(key: string): Effect.Effect<BucketObject | null, BucketError, RuntimeContext>; |
| 44 | /** |
| 45 | * Read an object and its body. Resolves `null` when the key does not exist. |
| 46 | */ |
| 47 | get( |
| 48 | key: string, |
| 49 | options?: GetOptions, |
| 50 | ): Effect.Effect<BucketObjectBody | null, BucketError, RuntimeContext>; |
| 51 | /** |
| 52 | * List one page of objects in the bucket. |
| 53 | */ |
| 54 | list(options?: ListOptions): Effect.Effect<ListResult, BucketError, RuntimeContext>; |
| 55 | /** |
| 56 | * Mint a presigned download URL, so a browser can read the object without |
| 57 | * credentials. Pure client-side SigV4 — no request is made to the store. |
| 58 | */ |
| 59 | presignGet( |
| 60 | key: string, |
| 61 | options?: PresignGetOptions, |
| 62 | ): Effect.Effect<string, BucketError, RuntimeContext>; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Bind a Prisma Object Store {@link Bucket} to a Prisma Compute app, AWS |
| 67 | * Lambda Function, or Cloudflare Worker with read-only access, and obtain the |
| 68 | * typed runtime client. |
| 69 | * |
| 70 | * Binding creates a read-scoped `Prisma.BucketAccessKey` for the bucket and carries |
| 71 | * its S3 credentials into the host environment, so the caller never handles a |
| 72 | * credential themselves. |
| 73 | * |
| 74 | * Provide {@link ReadBucketBinding} on the host implementation. |
| 75 | * |
| 76 | * Prisma bucket keys carry one of two coarse roles, `read` and `read_write`. |
| 77 | * This binding mints the `read` one, so the credential it puts in the host |
| 78 | * environment cannot write, and {@link ReadBucketClient} exposes no write |
| 79 | * operations either. |
| 80 | * |
| 81 | * ### Binding a Bucket |
| 82 | * **Example:** Read objects from Prisma Compute |
| 83 | * ```typescript |
| 84 | * export default Prisma.Compute( |
| 85 | * "api", |
| 86 | * { project, main: import.meta.filename }, |
| 87 | * Effect.gen(function* () { |
| 88 | * const uploads = yield* Prisma.ReadBucket(bucket); |
| 89 | * |
| 90 | * return { |
| 91 | * fetch: Effect.gen(function* () { |
| 92 | * const object = yield* uploads.get("reports/2026.json"); |
| 93 | * return yield* HttpServerResponse.json( |
| 94 | * object === null ? null : yield* object.json(), |
| 95 | * ); |
| 96 | * }), |
| 97 | * }; |
| 98 | * }).pipe(Effect.provide(Prisma.ReadBucketBinding)), |
| 99 | * ); |
| 100 | * ``` |
| 101 | * |
| 102 | * @binding |
| 103 | * @product Bucket |
| 104 | */ |
| 105 | export const ReadBucket = Binding.Service<ReadBucket>("Prisma.ReadBucket"); |
| 106 | |
| 107 | /** |
| 108 | * Build the read operations over an already-resolved transport. Shared with |
| 109 | * {@link ReadWriteBucket} so both levels run the same code. |
| 110 | */ |
| 111 | export const readBucketOperations = (access: BucketAccess): ReadBucketClient => ({ |
| 112 | head: (key: string) => |
| 113 | access.bucketName.pipe( |
| 114 | Effect.flatMap((Bucket) => access.authorize(S3.headObject({ Bucket, Key: key }))), |
| 115 | Effect.map((response) => objectFrom(key, response)), |
| 116 | // A missing key is absence, not a failure — mirror the native bucket |
| 117 | // clients that resolve `null`. |
| 118 | Effect.catchTag("NotFound", () => Effect.succeed(null)), |
| 119 | Effect.mapError(toBucketError), |
| 120 | ), |
| 121 | get: (key: string, options?: GetOptions) => |
| 122 | access.bucketName.pipe( |
| 123 | Effect.flatMap((Bucket) => |
| 124 | access.authorize( |
| 125 | S3.getObject({ |
| 126 | Bucket, |
| 127 | Key: key, |
| 128 | Range: rangeHeader(options?.range), |
| 129 | }), |
| 130 | ), |
| 131 | ), |
| 132 | Effect.map((response) => objectBodyFrom(key, response)), |
| 133 | Effect.catchTag("NoSuchKey", () => Effect.succeed(null)), |
| 134 | Effect.mapError(toBucketError), |
| 135 | ), |
| 136 | list: (options?: ListOptions) => |
| 137 | access.bucketName.pipe( |
| 138 | Effect.flatMap((Bucket) => |
| 139 | access.authorize( |
| 140 | S3.listObjectsV2({ |
| 141 | Bucket, |
| 142 | Prefix: options?.prefix, |
| 143 | Delimiter: options?.delimiter, |
| 144 | ContinuationToken: options?.cursor, |
| 145 | MaxKeys: options?.limit, |
| 146 | StartAfter: options?.startAfter, |
| 147 | }), |
| 148 | ), |
| 149 | ), |
| 150 | Effect.map((response): ListResult => { |
| 151 | const objects = (response.Contents ?? []).map(objectFromListEntry); |
| 152 | const delimitedPrefixes = (response.CommonPrefixes ?? []).flatMap((prefix) => |
| 153 | prefix.Prefix === undefined ? [] : [prefix.Prefix], |
| 154 | ); |
| 155 | return response.IsTruncated && response.NextContinuationToken |
| 156 | ? { |
| 157 | objects, |
| 158 | delimitedPrefixes, |
| 159 | truncated: true, |
| 160 | cursor: response.NextContinuationToken, |
| 161 | } |
| 162 | : { objects, delimitedPrefixes, truncated: false }; |
| 163 | }), |
| 164 | Effect.mapError(toBucketError), |
| 165 | ), |
| 166 | presignGet: (key: string, options?: PresignGetOptions) => |
| 167 | access.presign({ |
| 168 | method: "GET", |
| 169 | key, |
| 170 | expiresIn: options?.expiresIn, |
| 171 | responseContentType: options?.contentType, |
| 172 | }), |
| 173 | }); |
| 174 | |
| 175 | /** |
| 176 | * Build a read-only bucket client from a bound bucket key's credentials. |
| 177 | */ |
| 178 | export const makeReadBucketClient = (credentials: BucketCredentials): ReadBucketClient => |
| 179 | readBucketOperations(makeBucketAccess(credentials)); |
| 180 | |
| 181 | /** |
| 182 | * Implementation layer for {@link ReadBucket}. Provide it on the host |
| 183 | * Function/Worker Effect: |
| 184 | * |
| 185 | * ```typescript |
| 186 | * Effect.gen(function* () { |
| 187 | * const uploads = yield* Prisma.ReadBucket(bucket); |
| 188 | * // ... |
| 189 | * }).pipe(Effect.provide(Prisma.ReadBucketBinding)) |
| 190 | * ``` |
| 191 | * |
| 192 | * Prisma bucket keys have only `read` and `read_write` roles, and this binding |
| 193 | * mints the `read` one, so the credential it carries into the host cannot |
| 194 | * write. |
| 195 | */ |
| 196 | export const ReadBucketBinding = Layer.effect( |
| 197 | ReadBucket, |
| 198 | makeBucketBinding({ |
| 199 | capability: "Read", |
| 200 | role: "read", |
| 201 | makeClient: makeReadBucketClient, |
| 202 | }), |
| 203 | ); |