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

main
ts 190 lines 6.21 KB
Raw
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 BucketBody,
11 BucketError,
12 BucketObject,
13 PresignPutOptions,
14 PutOptions,
15 } from "./BucketTypes.ts";
16 import {
17 makeBucketAccess,
18 objectFrom,
19 toBucketError,
20 type BucketAccess,
21 } from "./internal/BucketClient.ts";
22
23 export interface WriteBucket extends Binding.Service<
24 WriteBucket,
25 "Prisma.WriteBucket",
26 (bucket: Bucket) => Effect.Effect<WriteBucketClient>
27 > {}
28
29 /**
30 * Write-only client for a Prisma Object Store bucket. It deliberately exposes
31 * no read operations — see the role caveat on {@link WriteBucketBinding}.
32 */
33 export interface WriteBucketClient {
34 /**
35 * Store an object, replacing any object already under the key.
36 */
37 put(
38 key: string,
39 value: BucketBody,
40 options?: PutOptions,
41 ): Effect.Effect<BucketObject, BucketError, RuntimeContext>;
42 /**
43 * Delete one key or a list of keys. Deleting a key that does not exist
44 * succeeds.
45 */
46 delete(keys: string | string[]): Effect.Effect<void, BucketError, RuntimeContext>;
47 /**
48 * Mint a presigned upload URL, so a browser can write the object without
49 * credentials. Pure client-side SigV4 — no request is made to the store.
50 *
51 * When `contentType` is set the uploader must send exactly that
52 * `Content-Type`, because it is signed into the URL.
53 */
54 presignPut(
55 key: string,
56 options?: PresignPutOptions,
57 ): Effect.Effect<string, BucketError, RuntimeContext>;
58 }
59
60 /**
61 * Bind a Prisma Object Store {@link Bucket} to a Prisma Compute app, AWS
62 * Lambda Function, or Cloudflare Worker with write access, and obtain the
63 * typed runtime client.
64 *
65 * Binding creates a `Prisma.BucketAccessKey` for the bucket and carries its S3
66 * credentials into the host environment, so the caller never handles a
67 * credential themselves.
68 *
69 * Provide {@link WriteBucketBinding} on the host implementation.
70 *
71 * **Role caveat.** Prisma bucket keys carry one of two coarse roles, `read`
72 * and `read_write`; there is no write-only role. This binding therefore mints
73 * a `read_write` key, and the credential it puts in the host environment can
74 * also read. The write-only contract is enforced client-side —
75 * {@link WriteBucketClient} exposes no read operations — and becomes a
76 * server-side boundary if Prisma grows a write-only role.
77 *
78 * ### Binding a Bucket
79 * **Example:** Write objects from Prisma Compute
80 * ```typescript
81 * export default Prisma.Compute(
82 * "api",
83 * { project, main: import.meta.filename },
84 * Effect.gen(function* () {
85 * const uploads = yield* Prisma.WriteBucket(bucket);
86 *
87 * return {
88 * fetch: Effect.gen(function* () {
89 * yield* uploads.put("reports/2026.json", JSON.stringify({ ok: true }), {
90 * contentType: "application/json",
91 * });
92 * return yield* HttpServerResponse.empty({ status: 204 });
93 * }),
94 * };
95 * }).pipe(Effect.provide(Prisma.WriteBucketBinding)),
96 * );
97 * ```
98 *
99 * @binding
100 * @product Bucket
101 */
102 export const WriteBucket = Binding.Service<WriteBucket>("Prisma.WriteBucket");
103
104 /**
105 * Build the write operations over an already-resolved transport. Shared with
106 * {@link ReadWriteBucket} so both levels run the same code.
107 */
108 export const writeBucketOperations = (access: BucketAccess): WriteBucketClient => ({
109 put: (key: string, value: BucketBody, options?: PutOptions) =>
110 access.bucketName.pipe(
111 Effect.flatMap((Bucket) =>
112 access.authorize(
113 S3.putObject({
114 Bucket,
115 Key: key,
116 Body: value,
117 ContentType: options?.contentType,
118 ContentLength: options?.contentLength,
119 CacheControl: options?.cacheControl,
120 ContentDisposition: options?.contentDisposition,
121 ContentEncoding: options?.contentEncoding,
122 Metadata: options?.metadata,
123 }),
124 ),
125 ),
126 Effect.map((response) =>
127 objectFrom(key, {
128 ETag: response.ETag,
129 ContentLength: options?.contentLength,
130 ContentType: options?.contentType,
131 Metadata: options?.metadata,
132 }),
133 ),
134 Effect.mapError(toBucketError),
135 ),
136 // Deletes are issued one key at a time rather than through the batch
137 // `DeleteObjects` operation, whose signed-payload digest requirement is not
138 // uniformly supported by S3-compatible stores.
139 delete: (keys: string | string[]) =>
140 access.bucketName.pipe(
141 Effect.flatMap((Bucket) =>
142 Effect.forEach(
143 typeof keys === "string" ? [keys] : keys,
144 (key) => access.authorize(S3.deleteObject({ Bucket, Key: key })),
145 { concurrency: 16, discard: true },
146 ),
147 ),
148 Effect.mapError(toBucketError),
149 ),
150 presignPut: (key: string, options?: PresignPutOptions) =>
151 access.presign({
152 method: "PUT",
153 key,
154 expiresIn: options?.expiresIn,
155 contentType: options?.contentType,
156 }),
157 });
158
159 /**
160 * Build a write-only bucket client from a bound bucket key's credentials.
161 */
162 export const makeWriteBucketClient = (credentials: BucketCredentials): WriteBucketClient =>
163 writeBucketOperations(makeBucketAccess(credentials));
164
165 /**
166 * Implementation layer for {@link WriteBucket}. Provide it on the host
167 * Function/Worker Effect:
168 *
169 * ```typescript
170 * Effect.gen(function* () {
171 * const uploads = yield* Prisma.WriteBucket(bucket);
172 * // ...
173 * }).pipe(Effect.provide(Prisma.WriteBucketBinding))
174 * ```
175 *
176 * Prisma bucket keys carry one of two coarse roles, `read` and `read_write`;
177 * there is no write-only role, so this binding mints a `read_write` key and
178 * the credential it carries into the host can also read. The write-only
179 * contract is enforced client-side by {@link WriteBucketClient}, which exposes
180 * no read operations, and becomes a server-side boundary if Prisma grows a
181 * write-only role.
182 */
183 export const WriteBucketBinding = Layer.effect(
184 WriteBucket,
185 makeBucketBinding({
186 capability: "Write",
187 role: "read_write",
188 makeClient: makeWriteBucketClient,
189 }),
190 );