| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import { Object } from "./object.ts"; |
| 6 | |
| 7 | export default Cloudflare.Worker( |
| 8 | "Worker", |
| 9 | { |
| 10 | main: import.meta.url, |
| 11 | }, |
| 12 | Effect.gen(function* () { |
| 13 | const objects = yield* Object; |
| 14 | |
| 15 | return { |
| 16 | fetch: Effect.gen(function* () { |
| 17 | const request = yield* HttpServerRequest; |
| 18 | const url = new URL(request.url, "http://x"); |
| 19 | |
| 20 | const object = objects.getByName("default"); |
| 21 | |
| 22 | // Plain RPC into the container (no bucket). |
| 23 | if (url.pathname === "/ping") { |
| 24 | const pong = yield* object.ping(); |
| 25 | return HttpServerResponse.text(pong); |
| 26 | } |
| 27 | |
| 28 | // The env var a `Binding.Service` bound onto the container. |
| 29 | if (url.pathname === "/bound-env") { |
| 30 | const value = yield* object.boundEnv(); |
| 31 | return yield* HttpServerResponse.json({ value: value ?? null }); |
| 32 | } |
| 33 | |
| 34 | // Seed R2 through the DO's native binding. |
| 35 | if (request.method === "PUT" && url.pathname === "/seed") { |
| 36 | const key = url.searchParams.get("key") ?? ""; |
| 37 | const value = yield* request.text; |
| 38 | yield* object.put(key, value).pipe(Effect.orDie); |
| 39 | return yield* HttpServerResponse.json({ ok: true }); |
| 40 | } |
| 41 | |
| 42 | // Bucket read end-to-end through the container over RPC. |
| 43 | if (url.pathname === "/rpc") { |
| 44 | const key = url.searchParams.get("key") ?? ""; |
| 45 | const value = yield* object.readObjectRpc(key).pipe(Effect.orDie); |
| 46 | return yield* HttpServerResponse.json({ value }); |
| 47 | } |
| 48 | |
| 49 | // Bucket read end-to-end through the container over fetch (TCP port). |
| 50 | if (url.pathname === "/fetch") { |
| 51 | const key = url.searchParams.get("key") ?? ""; |
| 52 | const result = yield* object.readObjectFetch(key); |
| 53 | return yield* HttpServerResponse.json(result); |
| 54 | } |
| 55 | |
| 56 | if (url.pathname === "/hello") { |
| 57 | const text = yield* object.hello().pipe(Effect.orDie); |
| 58 | return HttpServerResponse.text(text); |
| 59 | } |
| 60 | |
| 61 | return HttpServerResponse.text("ok"); |
| 62 | }), |
| 63 | }; |
| 64 | }), |
| 65 | ); |