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 | /** |
| 2 | * Runtime half of the Prisma Object Store bindings: an S3-compatible client |
| 3 | * pointed at a bucket key's endpoint. Internal — the capability modules |
| 4 | * (`ReadBucket.ts`, `WriteBucket.ts`, `ReadWriteBucket.ts`) build their public |
| 5 | * clients on top of these primitives. |
| 6 | */ |
| 7 | import { Credentials, Endpoint, Presign, Region } from "@distilled.cloud/aws"; |
| 8 | import type * as S3 from "@distilled.cloud/aws/s3"; |
| 9 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 12 | import type { HttpClient } from "effect/http/HttpClient"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Redacted from "effect/Redacted"; |
| 15 | import * as Stream from "effect/Stream"; |
| 16 | import { |
| 17 | BucketError, |
| 18 | type BucketCredentials, |
| 19 | type BucketObject, |
| 20 | type BucketObjectBody, |
| 21 | type BucketRange, |
| 22 | } from "../BucketTypes.ts"; |
| 23 | |
| 24 | /** |
| 25 | * SigV4 credential scope used for Prisma Object Store requests. Prisma |
| 26 | * addresses buckets through a single regionless endpoint and does not report |
| 27 | * a region on a bucket key, so requests are signed under the `auto` scope |
| 28 | * that S3-compatible stores conventionally accept. |
| 29 | */ |
| 30 | export const BUCKET_SIGNING_REGION = "auto"; |
| 31 | |
| 32 | export type PresignRequest = |
| 33 | | { |
| 34 | method: "GET"; |
| 35 | key: string; |
| 36 | expiresIn?: number | undefined; |
| 37 | /** Signed `response-content-type` override for the download. */ |
| 38 | responseContentType?: string | undefined; |
| 39 | } |
| 40 | | { |
| 41 | method: "PUT"; |
| 42 | key: string; |
| 43 | expiresIn?: number | undefined; |
| 44 | /** `Content-Type` header the uploader must send, signed into the URL. */ |
| 45 | contentType?: string | undefined; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * The transport a capability client is built against: the resolved bucket |
| 50 | * name, a way to run a distilled S3 operation with the bucket key's |
| 51 | * credentials, and SigV4 query-string presigning against the same endpoint. |
| 52 | */ |
| 53 | export interface BucketAccess { |
| 54 | bucketName: Effect.Effect<string, never, RuntimeContext>; |
| 55 | authorize: <A, E>( |
| 56 | effect: Effect.Effect<A, E, Credentials.Credentials | Region.Region | HttpClient>, |
| 57 | ) => Effect.Effect<A, E, RuntimeContext>; |
| 58 | presign: (request: PresignRequest) => Effect.Effect<string, BucketError, RuntimeContext>; |
| 59 | } |
| 60 | |
| 61 | export const toBucketError = (error: unknown): BucketError => |
| 62 | new BucketError({ |
| 63 | message: |
| 64 | typeof error === "object" && error !== null && "message" in error |
| 65 | ? String((error as { message: unknown }).message) |
| 66 | : "Unknown Prisma Object Store error", |
| 67 | cause: error instanceof Error ? error : new Error(String(error)), |
| 68 | }); |
| 69 | |
| 70 | const signingContext = (credentials: BucketCredentials) => |
| 71 | Effect.all([credentials.endpoint, credentials.accessKeyId, credentials.secretAccessKey]).pipe( |
| 72 | Effect.map(([endpoint, accessKeyId, secretAccessKey]) => |
| 73 | Layer.mergeAll( |
| 74 | Layer.succeed( |
| 75 | Credentials.Credentials, |
| 76 | Effect.succeed({ |
| 77 | accessKeyId: Redacted.make(accessKeyId), |
| 78 | secretAccessKey, |
| 79 | sessionToken: undefined, |
| 80 | region: BUCKET_SIGNING_REGION, |
| 81 | }), |
| 82 | ), |
| 83 | Layer.succeed(Region.Region, Effect.succeed(BUCKET_SIGNING_REGION)), |
| 84 | Layer.succeed(Endpoint.Endpoint, Effect.succeed(endpoint)), |
| 85 | FetchHttpClient.layer, |
| 86 | ), |
| 87 | ), |
| 88 | ); |
| 89 | |
| 90 | /** |
| 91 | * Build the transport for a bound bucket key. Requests are signed with the |
| 92 | * key's own credentials against its endpoint; because a custom endpoint is |
| 93 | * set, the S3 client addresses the bucket path-style rather than through the |
| 94 | * AWS virtual-host rules. |
| 95 | */ |
| 96 | export const makeBucketAccess = (credentials: BucketCredentials): BucketAccess => { |
| 97 | const context = signingContext(credentials); |
| 98 | return { |
| 99 | bucketName: credentials.bucketName, |
| 100 | authorize: (effect) => context.pipe(Effect.flatMap((layer) => Effect.provide(effect, layer))), |
| 101 | presign: (request) => |
| 102 | Effect.all([context, credentials.bucketName]).pipe( |
| 103 | Effect.flatMap(([layer, bucket]) => |
| 104 | Presign.presignS3Url({ |
| 105 | method: request.method, |
| 106 | bucket, |
| 107 | key: request.key, |
| 108 | region: BUCKET_SIGNING_REGION, |
| 109 | expiresIn: request.expiresIn, |
| 110 | contentType: request.method === "PUT" ? request.contentType : undefined, |
| 111 | responseContentType: request.method === "GET" ? request.responseContentType : undefined, |
| 112 | }).pipe(Effect.provide(layer)), |
| 113 | ), |
| 114 | Effect.mapError(toBucketError), |
| 115 | ), |
| 116 | }; |
| 117 | }; |
| 118 | |
| 119 | /** Render a {@link BucketRange} as an HTTP `Range` header value. */ |
| 120 | export const rangeHeader = (range: BucketRange | undefined) => { |
| 121 | if (range === undefined) return undefined; |
| 122 | const end = range.length === undefined ? "" : String(range.offset + range.length - 1); |
| 123 | return `bytes=${range.offset}-${end}`; |
| 124 | }; |
| 125 | |
| 126 | const stripQuotes = (etag: string | undefined) => |
| 127 | etag === undefined ? "" : etag.replaceAll('"', ""); |
| 128 | |
| 129 | const definedMetadata = ( |
| 130 | metadata: { [key: string]: string | undefined } | undefined, |
| 131 | ): Record<string, string> => { |
| 132 | const result: Record<string, string> = {}; |
| 133 | for (const [key, value] of Object.entries(metadata ?? {})) { |
| 134 | if (value !== undefined) result[key] = value; |
| 135 | } |
| 136 | return result; |
| 137 | }; |
| 138 | |
| 139 | export const objectFrom = ( |
| 140 | key: string, |
| 141 | attributes: { |
| 142 | ContentLength?: number | undefined; |
| 143 | ETag?: string | undefined; |
| 144 | LastModified?: Date | undefined; |
| 145 | ContentType?: string | undefined; |
| 146 | Metadata?: { [key: string]: string | undefined } | undefined; |
| 147 | }, |
| 148 | ): BucketObject => ({ |
| 149 | key, |
| 150 | size: attributes.ContentLength ?? 0, |
| 151 | etag: stripQuotes(attributes.ETag), |
| 152 | lastModified: attributes.LastModified, |
| 153 | contentType: attributes.ContentType, |
| 154 | metadata: definedMetadata(attributes.Metadata), |
| 155 | }); |
| 156 | |
| 157 | export const objectFromListEntry = (entry: S3.Object): BucketObject => ({ |
| 158 | key: entry.Key ?? "", |
| 159 | size: entry.Size ?? 0, |
| 160 | etag: stripQuotes(entry.ETag), |
| 161 | lastModified: entry.LastModified, |
| 162 | contentType: undefined, |
| 163 | metadata: {}, |
| 164 | }); |
| 165 | |
| 166 | const concatBytes = (chunks: readonly Uint8Array[]): Uint8Array => { |
| 167 | const total = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0); |
| 168 | const result = new Uint8Array(total); |
| 169 | let offset = 0; |
| 170 | for (const chunk of chunks) { |
| 171 | result.set(chunk, offset); |
| 172 | offset += chunk.byteLength; |
| 173 | } |
| 174 | return result; |
| 175 | }; |
| 176 | |
| 177 | /** |
| 178 | * Wrap a `GetObject` response as a {@link BucketObjectBody}. The body is a |
| 179 | * single-consumption stream, so `body` and the buffering accessors are three |
| 180 | * views of the same bytes and only one of them may be read. |
| 181 | */ |
| 182 | export const objectBodyFrom = (key: string, response: S3.GetObjectOutput): BucketObjectBody => { |
| 183 | const body = (response.Body ?? Stream.empty).pipe(Stream.mapError(toBucketError)); |
| 184 | const bytes = () => Stream.runCollect(body).pipe(Effect.map(concatBytes)); |
| 185 | const text = () => body.pipe(Stream.decodeText, Stream.mkString); |
| 186 | return { |
| 187 | ...objectFrom(key, response), |
| 188 | body, |
| 189 | bytes, |
| 190 | arrayBuffer: () => |
| 191 | bytes().pipe( |
| 192 | Effect.map((collected) => { |
| 193 | const buffer = new ArrayBuffer(collected.byteLength); |
| 194 | new Uint8Array(buffer).set(collected); |
| 195 | return buffer; |
| 196 | }), |
| 197 | ), |
| 198 | text, |
| 199 | json: <T>() => |
| 200 | text().pipe( |
| 201 | Effect.flatMap((decoded) => |
| 202 | Effect.try({ |
| 203 | try: () => JSON.parse(decoded) as T, |
| 204 | catch: toBucketError, |
| 205 | }), |
| 206 | ), |
| 207 | ), |
| 208 | }; |
| 209 | }; |