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

main
ts 35 lines 1.45 KB
Raw
1 import type { ReadBucketClient } from "@alchemy.run/cloudflare/R2/ReadBucket";
2 import * as Effect from "effect/Effect";
3 import * as HttpServerResponse from "effect/http/HttpServerResponse";
4
5 /**
6 * Shared read-side routes exercised by both the binding and HTTP fixtures so
7 * every method of {@link ReadBucketClient} (`get`, `head`, `list`) is driven
8 * over `fetch`. Returns `undefined` when the path is not a read route so the
9 * caller can fall through (used by the read-write fixtures).
10 */
11 export const readRoutes = (r2: ReadBucketClient, url: URL) =>
12 Effect.gen(function* () {
13 if (url.pathname === "/get") {
14 const key = url.searchParams.get("key") ?? "";
15 const object = yield* r2.get(key).pipe(Effect.orDie);
16 const value = object ? yield* object.text().pipe(Effect.orDie) : null;
17 return yield* HttpServerResponse.json({ value });
18 }
19 if (url.pathname === "/head") {
20 const key = url.searchParams.get("key") ?? "";
21 const object = yield* r2.head(key).pipe(Effect.orDie);
22 return yield* HttpServerResponse.json({
23 exists: object !== null,
24 size: object?.size ?? null,
25 });
26 }
27 if (url.pathname === "/list") {
28 const prefix = url.searchParams.get("prefix") ?? undefined;
29 const result = yield* r2.list(prefix ? { prefix } : undefined).pipe(Effect.orDie);
30 return yield* HttpServerResponse.json({
31 keys: result.objects.map((o) => o.key),
32 });
33 }
34 return undefined;
35 });