| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import { makeLocalBucketBinding } from "./BucketLocal.ts"; |
| 4 | import { makeReadWrite } from "./ReadWriteBucketBinding.ts"; |
| 5 | import { ReadWriteBucket } from "./ReadWriteBucket.ts"; |
| 6 | import { makeReadWriteR2HttpClient } from "./ReadWriteBucketHttp.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Local implementation of the {@link ReadWriteBucket} binding — reads and |
| 10 | * writes R2 objects over the Cloudflare HTTP API using the **current |
| 11 | * credentials** instead of a native Worker binding (`ReadWriteBucketBinding`) |
| 12 | * or a scoped API token (`ReadWriteBucketHttp`). |
| 13 | * |
| 14 | * Provide it on an {@link Action} (or any deploy-time Effect) to use the same |
| 15 | * `head`/`get`/`list`/`put`/`delete` client you'd use inside a Worker. |
| 16 | * Multipart uploads are unsupported over the HTTP API (mirrors |
| 17 | * `ReadWriteBucketHttp`). |
| 18 | * |
| 19 | * @example Seeding then reading a bucket from an Action |
| 20 | * ```typescript |
| 21 | * const Seed = Alchemy.Action( |
| 22 | * "Seed", |
| 23 | * Effect.gen(function* () { |
| 24 | * const r2 = yield* Cloudflare.R2.ReadWriteBucket(bucket); |
| 25 | * return Effect.fn(function* () { |
| 26 | * yield* r2.put("hello.txt", "Hello, World!"); |
| 27 | * const object = yield* r2.get("hello.txt"); |
| 28 | * return object ? yield* object.text() : null; |
| 29 | * }); |
| 30 | * }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketLocal)), |
| 31 | * ); |
| 32 | * ``` |
| 33 | */ |
| 34 | export const ReadWriteBucketLocal = Layer.effect( |
| 35 | ReadWriteBucket, |
| 36 | Effect.suspend(() => |
| 37 | makeLocalBucketBinding({ |
| 38 | makeHttpClient: makeReadWriteR2HttpClient, |
| 39 | makeNativeClient: makeReadWrite, |
| 40 | }), |
| 41 | ), |
| 42 | ); |