| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import { makeLocalBucketBinding } from "./BucketLocal.ts"; |
| 4 | import { makeRead } from "./ReadBucketBinding.ts"; |
| 5 | import { ReadBucket } from "./ReadBucket.ts"; |
| 6 | import { makeReadR2HttpClient } from "./ReadBucketHttp.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Local implementation of the {@link ReadBucket} binding — reads R2 objects |
| 10 | * over the Cloudflare HTTP API using the **current credentials** instead of a |
| 11 | * native Worker binding (`ReadBucketBinding`) or a scoped API token |
| 12 | * (`ReadBucketHttp`). |
| 13 | * |
| 14 | * Provide it on an {@link Action} (or any deploy-time Effect) to read a bucket |
| 15 | * with the same `head`/`get`/`list` client you'd use inside a Worker. |
| 16 | * |
| 17 | * @example Reading an object from an Action |
| 18 | * ```typescript |
| 19 | * const Read = Alchemy.Action( |
| 20 | * "Read", |
| 21 | * Effect.gen(function* () { |
| 22 | * const r2 = yield* Cloudflare.R2.ReadBucket(bucket); |
| 23 | * return Effect.fn(function* () { |
| 24 | * const object = yield* r2.get("hello.txt"); |
| 25 | * return object ? yield* object.text() : null; |
| 26 | * }); |
| 27 | * }).pipe(Effect.provide(Cloudflare.R2.ReadBucketLocal)), |
| 28 | * ); |
| 29 | * ``` |
| 30 | */ |
| 31 | export const ReadBucketLocal = Layer.effect( |
| 32 | ReadBucket, |
| 33 | Effect.suspend(() => |
| 34 | makeLocalBucketBinding({ |
| 35 | makeHttpClient: makeReadR2HttpClient, |
| 36 | makeNativeClient: makeRead, |
| 37 | }), |
| 38 | ), |
| 39 | ); |