| 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 { IsolatedObject } from "./object.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Drives the container over HTTP: `GET /ping` → RPC ping, `GET /hello` → |
| 9 | * the container's own HTTP server through its TCP port. |
| 10 | */ |
| 11 | export default Cloudflare.Worker( |
| 12 | "IsolatedProjectWorker", |
| 13 | { |
| 14 | main: import.meta.filename, |
| 15 | }, |
| 16 | Effect.gen(function* () { |
| 17 | const objects = yield* IsolatedObject; |
| 18 | |
| 19 | return { |
| 20 | fetch: Effect.gen(function* () { |
| 21 | const request = yield* HttpServerRequest; |
| 22 | const url = new URL(request.url, "http://x"); |
| 23 | const object = objects.getByName("default"); |
| 24 | |
| 25 | if (url.pathname === "/ping") { |
| 26 | const pong = yield* object.ping(); |
| 27 | return HttpServerResponse.text(pong); |
| 28 | } |
| 29 | if (url.pathname === "/hello") { |
| 30 | const text = yield* object.hello(); |
| 31 | return HttpServerResponse.text(text); |
| 32 | } |
| 33 | return HttpServerResponse.text("ok"); |
| 34 | }).pipe( |
| 35 | // Surface failures as 5xx (not a thrown defect) so the test's |
| 36 | // readiness retry treats a container that is still starting as |
| 37 | // retryable rather than fatal. |
| 38 | Effect.catchCause((cause) => |
| 39 | Effect.succeed( |
| 40 | HttpServerResponse.text(String(cause), { status: 503 }), |
| 41 | ), |
| 42 | ), |
| 43 | ), |
| 44 | }; |
| 45 | }), |
| 46 | ); |