| 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 { LocalRemoteContainerObject } from "./local-object.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Local-dev twin of `worker.ts` with its own logical id — see |
| 9 | * `local-object.ts` for why the local suite owns a separate fixture identity. |
| 10 | */ |
| 11 | export default class LocalRemoteContainerWorker extends Cloudflare.Worker<LocalRemoteContainerWorker>()( |
| 12 | "LocalRemoteContainerWorker", |
| 13 | { |
| 14 | main: import.meta.url, |
| 15 | }, |
| 16 | Effect.gen(function* () { |
| 17 | const objects = yield* LocalRemoteContainerObject; |
| 18 | |
| 19 | return { |
| 20 | fetch: Effect.gen(function* () { |
| 21 | const request = yield* HttpServerRequest; |
| 22 | const url = new URL(request.url, "http://x"); |
| 23 | |
| 24 | if (url.pathname === "/hello") { |
| 25 | const text = yield* objects.getByName("default").hello(); |
| 26 | return HttpServerResponse.text(text); |
| 27 | } |
| 28 | |
| 29 | return HttpServerResponse.text("ok"); |
| 30 | }).pipe( |
| 31 | Effect.catchTag("HttpClientError", (err) => |
| 32 | Effect.succeed( |
| 33 | err.response |
| 34 | ? HttpServerResponse.fromClientResponse(err.response) |
| 35 | : HttpServerResponse.text(err.message, { |
| 36 | status: 500, |
| 37 | }), |
| 38 | ), |
| 39 | ), |
| 40 | ), |
| 41 | }; |
| 42 | }), |
| 43 | ) {} |