The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | |
| 7 | /** Only the owning object's `interceptOutboundHttp` answers this host. */ |
| 8 | export const INTERCEPT_HOST = "intercept.internal"; |
| 9 | |
| 10 | class InterceptContainer extends Cloudflare.Container<InterceptContainer>()("InterceptContainer", { |
| 11 | // Template string, not `path.join(import.meta.dirname, …)`: this module is |
| 12 | // bundled into the Worker and `import.meta.dirname` is undefined there. |
| 13 | context: `${import.meta.dirname}/context`, |
| 14 | }) {} |
| 15 | |
| 16 | /** |
| 17 | * Durable Object that owns an {@link InterceptContainer} through |
| 18 | * `yield* InterceptContainer` and routes the container's outbound HTTP back to itself: |
| 19 | * the Fetcher it registers is a stub for its own id, so intercepted requests |
| 20 | * arrive at its `fetch`. |
| 21 | */ |
| 22 | export class InterceptContainerObject extends Cloudflare.DurableObject<InterceptContainerObject>()( |
| 23 | "InterceptContainerObject", |
| 24 | Effect.gen(function* () { |
| 25 | const container = yield* InterceptContainer; |
| 26 | const state = yield* Cloudflare.DurableObjectState; |
| 27 | const env = yield* Cloudflare.WorkerEnvironment; |
| 28 | |
| 29 | return Effect.gen(function* () { |
| 30 | const self = Cloudflare.fromCloudflareFetcher(env.InterceptContainerObject.get(state.id)); |
| 31 | // Starting is idempotent: a no-op once the container is running. |
| 32 | const start = container.start({ enableInternet: false }); |
| 33 | const { fetch } = yield* container.getTcpPort(8080); |
| 34 | |
| 35 | const probe = (url: string) => |
| 36 | Effect.gen(function* () { |
| 37 | const response = yield* fetch( |
| 38 | HttpClientRequest.get(`http://container/probe?url=${encodeURIComponent(url)}`), |
| 39 | ); |
| 40 | return yield* response.text; |
| 41 | }); |
| 42 | |
| 43 | return { |
| 44 | // The container's intercepted requests. |
| 45 | fetch: Effect.gen(function* () { |
| 46 | const request = yield* HttpServerRequest; |
| 47 | return HttpServerResponse.text(`intercepted ${request.headers.host}${request.url}`); |
| 48 | }), |
| 49 | probeHost: () => |
| 50 | start.pipe( |
| 51 | Effect.andThen(container.interceptOutboundHttp(INTERCEPT_HOST, self)), |
| 52 | Effect.andThen(probe(`http://${INTERCEPT_HOST}/hello`)), |
| 53 | ), |
| 54 | probeAll: () => |
| 55 | start.pipe( |
| 56 | Effect.andThen(container.interceptAllOutboundHttp(self)), |
| 57 | Effect.andThen(probe("http://any.example/hello")), |
| 58 | ), |
| 59 | }; |
| 60 | }); |
| 61 | }), |
| 62 | ) {} |