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

main
ts 96 lines 3.08 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import type * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import type { Bucket } from "./Bucket.ts";
5 import { makeBucketBinding } from "./BucketBinding.ts";
6 import type { BucketCredentials } from "./BucketTypes.ts";
7 import { makeBucketAccess } from "./internal/BucketClient.ts";
8 import { readBucketOperations, type ReadBucketClient } from "./ReadBucket.ts";
9 import { writeBucketOperations, type WriteBucketClient } from "./WriteBucket.ts";
10
11 export interface ReadWriteBucket extends Binding.Service<
12 ReadWriteBucket,
13 "Prisma.ReadWriteBucket",
14 (bucket: Bucket) => Effect.Effect<ReadWriteBucketClient>
15 > {}
16
17 /**
18 * Full-access client for a Prisma Object Store bucket: every operation of
19 * {@link ReadBucketClient} and {@link WriteBucketClient}.
20 */
21 export interface ReadWriteBucketClient extends ReadBucketClient, WriteBucketClient {}
22
23 /**
24 * Bind a Prisma Object Store {@link Bucket} to a Prisma Compute app, AWS
25 * Lambda Function, or Cloudflare Worker with read and write access, and obtain
26 * the typed runtime client.
27 *
28 * Binding creates a read-write `Prisma.BucketAccessKey` for the bucket and carries
29 * its S3 credentials into the host environment, so the caller never handles a
30 * credential themselves.
31 *
32 * Provide {@link ReadWriteBucketBinding} on the host implementation.
33 *
34 * Use {@link ReadBucket} instead where read-only access is enough: Prisma
35 * bucket keys have a `read` role, so that binding's credential genuinely
36 * cannot write.
37 *
38 * ### Binding a Bucket
39 * **Example:** Read and write objects from Prisma Compute
40 * ```typescript
41 * export default Prisma.Compute(
42 * "api",
43 * { project, main: import.meta.filename },
44 * Effect.gen(function* () {
45 * const uploads = yield* Prisma.ReadWriteBucket(bucket);
46 *
47 * return {
48 * fetch: Effect.gen(function* () {
49 * yield* uploads.put("hits", "1");
50 * const object = yield* uploads.get("hits");
51 * return yield* HttpServerResponse.text(
52 * object === null ? "" : yield* object.text(),
53 * );
54 * }),
55 * };
56 * }).pipe(Effect.provide(Prisma.ReadWriteBucketBinding)),
57 * );
58 * ```
59 *
60 * @binding
61 * @product Bucket
62 */
63 export const ReadWriteBucket = Binding.Service<ReadWriteBucket>("Prisma.ReadWriteBucket");
64
65 /**
66 * Build a read-write bucket client from a bound bucket key's credentials.
67 */
68 export const makeReadWriteBucketClient = (
69 credentials: BucketCredentials,
70 ): ReadWriteBucketClient => {
71 const access = makeBucketAccess(credentials);
72 return {
73 ...readBucketOperations(access),
74 ...writeBucketOperations(access),
75 };
76 };
77
78 /**
79 * Implementation layer for {@link ReadWriteBucket}. Provide it on the host
80 * Function/Worker Effect:
81 *
82 * ```typescript
83 * Effect.gen(function* () {
84 * const uploads = yield* Prisma.ReadWriteBucket(bucket);
85 * // ...
86 * }).pipe(Effect.provide(Prisma.ReadWriteBucketBinding))
87 * ```
88 */
89 export const ReadWriteBucketBinding = Layer.effect(
90 ReadWriteBucket,
91 makeBucketBinding({
92 capability: "ReadWrite",
93 role: "read_write",
94 makeClient: makeReadWriteBucketClient,
95 }),
96 );