| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 4 | import { RELOAD_CONTAINER_PORT, ReloadContainer } from "./container.ts"; |
| 5 | |
| 6 | /** Durable Object fronting {@link ReloadContainer}; proxies `path` into it. */ |
| 7 | export class ReloadContainerObject extends Cloudflare.DurableObject<ReloadContainerObject>()( |
| 8 | "ReloadContainerObject", |
| 9 | Effect.gen(function* () { |
| 10 | const container = yield* ReloadContainer; |
| 11 | |
| 12 | return Effect.gen(function* () { |
| 13 | const { fetch } = yield* container.getTcpPort(RELOAD_CONTAINER_PORT); |
| 14 | |
| 15 | return { |
| 16 | read: (path: string) => |
| 17 | Effect.gen(function* () { |
| 18 | const response = yield* fetch( |
| 19 | HttpClientRequest.get(`http://container${path}`), |
| 20 | ); |
| 21 | return yield* response.text; |
| 22 | }), |
| 23 | }; |
| 24 | }); |
| 25 | }).pipe( |
| 26 | Effect.provide( |
| 27 | Cloudflare.Containers.layer(ReloadContainer, { enableInternet: false }), |
| 28 | ), |
| 29 | ), |
| 30 | ) {} |