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

main
ts 82 lines 3.55 KB
Raw
1 /**
2 * Node-side path into the local workerd R2 simulator, built on the
3 * runtime's platform proxy (`PlatformProxy.open` — our `getPlatformProxy`).
4 *
5 * For a `dev:` bucket there is no cloud bucket to speak REST to. Instead,
6 * each operation opens a scoped platform proxy hosting the native R2
7 * binding and runs against `proxy.env.R2` — the proxy protocol rehydrates
8 * `R2Object`/`R2ObjectBody` on the Node side, matching the surface the
9 * Worker-binding client builders (`makeRead` / `makeWrite`) consume, so the
10 * `*Local` layers reuse those builders verbatim:
11 *
12 * Node ── proxy.env.R2.get(...) ──▶ r2 service ──▶ R2BucketObject
13 *
14 * One proxy boot per operation — slow but correct; the `*Local` layers run
15 * in deploy-time Actions, not on a request path. Data lands in the same
16 * `{storage}/r2` directory every local worker binding reads.
17 *
18 * NOT exported from `index.ts` — capability-internal scaffolding.
19 */
20 import { R2Bucket } from "@alchemy.run/cloudflare-runtime/core/bindings";
21 import { open } from "@alchemy.run/cloudflare-runtime/core/platform-proxy";
22 import type * as runtime from "@cloudflare/workers-types";
23 import type * as Context from "effect/Context";
24 import * as Effect from "effect/Effect";
25 import { gatewayName, localGatewayRuntime } from "../LocalGateway.ts";
26 import { makeR2ObjectWrappers, type makeHelpers } from "./BucketBinding.ts";
27 import { R2Error } from "./BucketTypes.ts";
28
29 const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, R2Error> =>
30 Effect.tryPromise({
31 try: fn,
32 catch: (error: any) =>
33 new R2Error({
34 message: error?.message ?? "Unknown error",
35 cause: error,
36 }),
37 });
38
39 /**
40 * Binding-client helpers backed by a per-operation platform proxy instead
41 * of a Worker's `env`. `raw` cannot be satisfied — a native bucket only
42 * lives as long as its proxy's scope — so it dies with guidance.
43 */
44 export const makeProxyBucketHelpers = (
45 bucketName: string,
46 /**
47 * The FULL ambient stack-eval context: platform services for booting
48 * workerd plus `CloudflareEnvironment`/`AlchemyContext` for the runtime
49 * layer.
50 */
51 ambient: Context.Context<never>,
52 ): ReturnType<typeof makeHelpers> => {
53 const use = <T>(fn: (raw: runtime.R2Bucket) => Promise<T>): Effect.Effect<T, R2Error> =>
54 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- remaining requirements are satisfied by the captured ambient context
55 Effect.scoped(
56 Effect.gen(function* () {
57 const proxy = yield* open({
58 name: gatewayName("alchemy-r2-gateway", bucketName),
59 bindings: [R2Bucket.local({ binding: "R2", id: bucketName })],
60 });
61 const bucket = (proxy.env as Record<string, unknown>).R2 as runtime.R2Bucket;
62 return yield* tryPromise(() => fn(bucket));
63 }),
64 ).pipe(
65 Effect.provide(localGatewayRuntime),
66 // The gateway layer's platform requirements are satisfied by the
67 // ambient stack-eval context; `Context<never>` can't prove that
68 // statically, so erase the leftover R (and the proxy's own infra
69 // error channel) with a cast — mirroring D1's gateway.
70 Effect.provideContext(ambient),
71 ) as Effect.Effect<T, R2Error>;
72
73 const raw = Effect.die(
74 new R2Error({
75 message:
76 "A locally-emulated R2 bucket has no long-lived native binding outside a Worker; use put/get/head/list/delete (each runs through a scoped local gateway).",
77 cause: new Error("unsupported"),
78 }),
79 ) as Effect.Effect<runtime.R2Bucket>;
80
81 return { raw, use, tryPromise, ...makeR2ObjectWrappers(tryPromise) };
82 };