@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 170 lines 5.89 KB
Raw
1 import { bindFunction } from "@alchemy.run/railway/Bind";
2 import { serveRailwayRpc } from "@alchemy.run/railway/rpc-server";
3 import { RPC_PATH_PREFIX, RPC_TOKEN_ENV, RPC_TOKEN_HEADER } from "@alchemy.run/railway/rpc-token";
4 import { describe, expect, it } from "@alchemy.run/test";
5 import { safeHttpEffect } from "alchemy/Http";
6 import * as Effect from "effect/Effect";
7 import * as HttpEffect from "effect/http/HttpEffect";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9
10 const TOKEN = "a".repeat(64);
11 const PRIVATE_URL = "http://greeter.railway.internal:3000";
12 const PUBLIC_URL = "https://greeter.up.railway.app";
13
14 const shape = { greet: (name: string) => Effect.succeed(`hello ${name}`) };
15
16 const fallback = Effect.succeed(HttpServerResponse.text("ok"));
17
18 const webHandler = HttpEffect.toWebHandler(safeHttpEffect(serveRailwayRpc(shape, fallback)));
19
20 const fetchImpl = ((url: any, init?: any) => {
21 const href = typeof url === "string" ? url : String(url);
22 const parsed = new URL(href);
23 const headers = new Headers(init?.headers);
24 headers.set("host", parsed.host);
25 return webHandler(new Request(href, { ...init, headers }));
26 }) as typeof globalThis.fetch;
27
28 const withEnv = <A, E>(effect: Effect.Effect<A, E>): Effect.Effect<A, E> =>
29 Effect.suspend(() => {
30 const previous = process.env[RPC_TOKEN_ENV];
31 process.env[RPC_TOKEN_ENV] = TOKEN;
32 return effect.pipe(
33 Effect.ensuring(
34 Effect.sync(() => {
35 if (previous === undefined) {
36 delete process.env[RPC_TOKEN_ENV];
37 } else {
38 process.env[RPC_TOKEN_ENV] = previous;
39 }
40 }),
41 ),
42 );
43 });
44
45 const rpcPost = (url: string, headers: Record<string, string> = {}) =>
46 webHandler(
47 new Request(url, {
48 method: "POST",
49 headers: { "content-type": "application/json", ...headers },
50 body: JSON.stringify(["sam"]),
51 }),
52 );
53
54 describe.sequential(
55 "Railway private-mesh RPC",
56 { tags: ["unit", "provider:railway", "local"] },
57 () => {
58 it("serves value methods on the private mesh with the token", () =>
59 withEnv(
60 Effect.gen(function* () {
61 const response = yield* Effect.promise(() =>
62 rpcPost(`${PRIVATE_URL}${RPC_PATH_PREFIX}greet`, {
63 host: "greeter.railway.internal:3000",
64 [RPC_TOKEN_HEADER]: TOKEN,
65 }),
66 );
67 expect(response.status).toBe(200);
68 expect(yield* Effect.promise(() => response.text())).toBe(JSON.stringify("hello sam"));
69 }),
70 ));
71
72 it("401s public Host even with the token", () =>
73 withEnv(
74 Effect.gen(function* () {
75 const response = yield* Effect.promise(() =>
76 rpcPost(`${PUBLIC_URL}${RPC_PATH_PREFIX}greet`, {
77 host: "greeter.up.railway.app",
78 [RPC_TOKEN_HEADER]: TOKEN,
79 }),
80 );
81 expect(response.status).toBe(401);
82 }),
83 ));
84
85 it("401s the private mesh without a token", () =>
86 withEnv(
87 Effect.gen(function* () {
88 const response = yield* Effect.promise(() =>
89 rpcPost(`${PRIVATE_URL}${RPC_PATH_PREFIX}greet`, {
90 host: "greeter.railway.internal:3000",
91 }),
92 );
93 expect(response.status).toBe(401);
94 }),
95 ));
96
97 it("401s a wrong token", () =>
98 withEnv(
99 Effect.gen(function* () {
100 const response = yield* Effect.promise(() =>
101 rpcPost(`${PRIVATE_URL}${RPC_PATH_PREFIX}greet`, {
102 host: "greeter.railway.internal:3000",
103 [RPC_TOKEN_HEADER]: "b".repeat(64),
104 }),
105 );
106 expect(response.status).toBe(401);
107 }),
108 ));
109
110 it("401s when X-Forwarded-Host is a public domain", () =>
111 withEnv(
112 Effect.gen(function* () {
113 const response = yield* Effect.promise(() =>
114 rpcPost(`${PRIVATE_URL}${RPC_PATH_PREFIX}greet`, {
115 host: "greeter.railway.internal:3000",
116 "x-forwarded-host": "greeter.up.railway.app",
117 [RPC_TOKEN_HEADER]: TOKEN,
118 }),
119 );
120 expect(response.status).toBe(401);
121 }),
122 ));
123
124 it("falls through to fetch outside /__rpc__/", () =>
125 withEnv(
126 Effect.gen(function* () {
127 const response = yield* Effect.promise(() => webHandler(new Request(`${PUBLIC_URL}/`)));
128 expect(response.status).toBe(200);
129 expect(yield* Effect.promise(() => response.text())).toBe("ok");
130 }),
131 ));
132
133 it(
134 "bindFunction stub round-trips through the private mesh",
135 () =>
136 withEnv(
137 Effect.sync(() => {
138 const previousFetch = globalThis.fetch;
139 globalThis.__ALCHEMY_RUNTIME__ = true;
140 globalThis.fetch = fetchImpl;
141 process.env.RAILWAY_RPC_GREETER_HOST = "greeter.railway.internal";
142 process.env.RAILWAY_RPC_GREETER_PORT = "3000";
143 process.env.RAILWAY_RPC_GREETER_TOKEN = TOKEN;
144 return previousFetch;
145 }).pipe(
146 Effect.flatMap((previousFetch) =>
147 Effect.gen(function* () {
148 const stub = yield* bindFunction<{
149 greet: (name: string) => Effect.Effect<string>;
150 }>({ Type: "Railway.Function", LogicalId: "Greeter" } as never);
151 const greeting = yield* stub.greet("sam");
152 expect(greeting).toBe("hello sam");
153 }).pipe(
154 Effect.ensuring(
155 Effect.sync(() => {
156 globalThis.fetch = previousFetch;
157 globalThis.__ALCHEMY_RUNTIME__ = false;
158 delete process.env.RAILWAY_RPC_GREETER_HOST;
159 delete process.env.RAILWAY_RPC_GREETER_PORT;
160 delete process.env.RAILWAY_RPC_GREETER_TOKEN;
161 }),
162 ),
163 ),
164 ),
165 ),
166 ),
167 { tags: ["provider:railway:bind"] },
168 );
169 },
170 );