@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 69 lines 2.21 KB
Raw
1 import type * as runtime from "@cloudflare/workers-types";
2 import * as Binding from "alchemy/Binding";
3 import type { RuntimeContext } from "alchemy/RuntimeContext";
4 import * as Effect from "effect/Effect";
5 import type * as HttpClientError from "effect/http/HttpClientError";
6 import type * as HttpClientRequest from "effect/http/HttpClientRequest";
7 import type * as HttpClientResponse from "effect/http/HttpClientResponse";
8 import * as Layer from "effect/Layer";
9 import { fetchWithFetcher } from "./FetcherRequest.ts";
10 import { isWorker, type Worker, WorkerEnvironment } from "./Worker.ts";
11
12 /**
13 * @binding
14 * @product Workers
15 * @category Workers & Compute
16 */
17 export interface Fetch extends Binding.Service<
18 Fetch,
19 "Cloudflare.Workers.Fetch",
20 (
21 worker: Worker,
22 ) => Effect.Effect<
23 (
24 request: HttpClientRequest.HttpClientRequest,
25 ) => Effect.Effect<
26 HttpClientResponse.HttpClientResponse,
27 HttpClientError.RequestError,
28 RuntimeContext
29 >
30 >
31 > {}
32
33 export const Fetch = Binding.Service<Fetch>("Cloudflare.Workers.Fetch");
34
35 export const FetchBinding = Layer.effect(
36 Fetch,
37 Effect.gen(function* () {
38 const env = yield* WorkerEnvironment;
39
40 return Effect.fn(function* (worker: Worker) {
41 if (!globalThis.__ALCHEMY_RUNTIME__) {
42 // Deploy-time only: register the service binding for the *target*
43 // worker on the host Worker.
44 const host = yield* Binding.Host;
45 if (isWorker(host)) {
46 yield* host.bind`${worker}`({
47 bindings: [
48 {
49 type: "service",
50 name: worker.LogicalId,
51 service: worker.workerName,
52 },
53 ],
54 });
55 }
56 }
57 // Lazy — the `WorkerEnvironment` bindings are only populated at exec
58 // phase, so the fetcher must be resolved per call, not at bind time.
59 const fetcher = Effect.sync(
60 () => (env as Record<string, runtime.Fetcher>)[worker.LogicalId]!,
61 ) as Effect.Effect<runtime.Fetcher, never, RuntimeContext>;
62
63 return (request: HttpClientRequest.HttpClientRequest) =>
64 Effect.flatMap(fetcher, (f) =>
65 fetchWithFetcher(f, request, "Service binding fetch failed"),
66 );
67 });
68 }),
69 );