@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, 06:00 UTC

main
ts 60 lines 2.24 KB
Raw
1 import type { Bucket } from "@alchemy.run/cloudflare/R2/Bucket";
2 import * as Binding from "alchemy/Binding";
3 import type { Resource, ResourceLike } from "alchemy/Resource";
4 import type { RuntimeContext } from "alchemy/RuntimeContext";
5 import * as Effect from "effect/Effect";
6 import * as Layer from "effect/Layer";
7
8 /**
9 * The env var the binding injects into whichever host it is provided on.
10 */
11 export const PROBE_ENV_KEY = "ALCHEMY_TEST_BOUND_ENV";
12
13 /**
14 * A minimal `Binding.Service` whose entire deploy-time contribution is an
15 * environment variable — the shape `Prisma.Connect` uses for a Prisma
16 * Compute app, a Lambda Function, and (as of this fixture's coverage) a
17 * Cloudflare Container.
18 *
19 * A container is a real process, so it has no workerd bindings: `env` is the
20 * only channel a capability has to reach it. This proves the container
21 * provider actually folds the binding contract's `env` into the deployed
22 * application's environment variables — it used to declare the contract and
23 * silently drop the values.
24 */
25 export interface ProbeEnv extends Binding.Service<
26 ProbeEnv,
27 "Test.ProbeEnv",
28 (bucket: Bucket) => Effect.Effect<Effect.Effect<string | undefined, never, RuntimeContext>>
29 > {}
30
31 export const ProbeEnv = Binding.Service<ProbeEnv>("Test.ProbeEnv");
32
33 type EnvBindingHost = Resource<string, object | undefined, object, { env?: Record<string, any> }>;
34
35 const acceptsEnvBinding = (host: ResourceLike | undefined): host is EnvBindingHost =>
36 host?.Type === "Cloudflare.Container";
37
38 export const ProbeEnvBinding = Layer.effect(
39 ProbeEnv,
40 Effect.succeed(
41 Effect.fn(function* (bucket: Bucket) {
42 if (!globalThis.__ALCHEMY_RUNTIME__) {
43 const host = yield* Binding.Host;
44 if (!acceptsEnvBinding(host)) {
45 return yield* Effect.die(
46 new Error(
47 `Test.ProbeEnv expects a Cloudflare.Container host, got '${host?.Type ?? "no host"}'`,
48 ),
49 );
50 }
51 // The value is an Output of a sibling resource's attribute, so this
52 // also pins that bound env survives Output resolution.
53 yield* host.bind`${bucket}`({
54 env: { [PROBE_ENV_KEY]: bucket.bucketName },
55 });
56 }
57 return Effect.sync(() => process.env[PROBE_ENV_KEY]);
58 }),
59 ),
60 );