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