@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 153 lines 4.76 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import * as Output from "alchemy/Output";
3 import { unpackEnvValue, type RuntimeContext } from "alchemy/RuntimeContext";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Redacted from "effect/Redacted";
7 import {
8 ConnectMySQL,
9 MySQLUrlMissing,
10 connectEnvKeys,
11 type ConnectMySQLClient,
12 } from "./ConnectMySQL.ts";
13 import { isRailwayHost } from "./MountVolume.ts";
14 import { MYSQL_URL_SECRET, MYSQL_PUBLIC_URL_SECRET, type MySQL } from "./MySQL.ts";
15
16 const runtimeOutput = <A>(
17 key: string,
18 output: Output.Output<A>,
19 ): Effect.Effect<A, never, RuntimeContext> =>
20 output.bind(key).pipe(Effect.flatMap((effect) => effect));
21
22 const asRedactedUrl = (
23 value: string,
24 name: string,
25 ): Effect.Effect<Redacted.Redacted<string>, MySQLUrlMissing> =>
26 value.length > 0
27 ? Effect.succeed(Redacted.make(value))
28 : Effect.fail(new MySQLUrlMissing({ resourceName: name }));
29
30 const fromProcessEnv = (key: string): string => {
31 const unpacked = unpackEnvValue<unknown>(process.env[key]);
32 if (typeof unpacked === "string") return unpacked;
33 if (Redacted.isRedacted(unpacked)) {
34 const inner = Redacted.value(unpacked);
35 return typeof inner === "string" ? inner : "";
36 }
37 return "";
38 };
39
40 const firstUrl = (
41 values: ReadonlyArray<string>,
42 name: string,
43 ): Effect.Effect<Redacted.Redacted<string>, MySQLUrlMissing> => {
44 const found = values.find((value) => value.length > 0);
45 return found !== undefined
46 ? asRedactedUrl(found, name)
47 : Effect.fail(new MySQLUrlMissing({ resourceName: name }));
48 };
49
50 /**
51 * Implementation of {@link ConnectMySQL}. Provide it on the
52 * {@link Service} Effect.
53 *
54 * At deploy time this packs the private connection URI onto the host
55 * (`RAILWAY_MYSQL_*`, `MYSQL_URL`). At runtime the client reads
56 * `process.env`.
57 *
58 *
59 * ### Provide the layer
60 * **Example:** On a Service
61 * ```typescript
62 * Effect.gen(function* () {
63 * const conn = yield* Railway.ConnectMySQL(Db);
64 * const db = yield* Drizzle.MySQL(conn.connectionString);
65 * }).pipe(Effect.provide(Railway.ConnectMySQLHttp))
66 * ```
67 *
68 * @layer
69 * @product MySQL
70 * @provides Railway.ConnectMySQL
71 */
72 export const ConnectMySQLHttp = Layer.effect(
73 ConnectMySQL,
74 Effect.succeed(
75 Effect.fn(function* (mysql: MySQL) {
76 const keys = connectEnvKeys(mysql);
77 const name = mysql.LogicalId;
78 if (!globalThis.__ALCHEMY_RUNTIME__) {
79 const host = yield* Binding.Host;
80 if (isRailwayHost(host)) {
81 yield* host.bind`${mysql}`({
82 env: {
83 [keys.pooled]: mysql.connectionUri,
84 [keys.direct]: mysql.connectionUri,
85 [MYSQL_URL_SECRET]: mysql.connectionUri,
86 },
87 });
88 }
89 }
90
91 const fromEnv = (preferDirect: boolean) =>
92 firstUrl(
93 preferDirect
94 ? [
95 fromProcessEnv(keys.direct),
96 fromProcessEnv(MYSQL_URL_SECRET),
97 fromProcessEnv(keys.pooled),
98 fromProcessEnv(MYSQL_PUBLIC_URL_SECRET),
99 ]
100 : [
101 fromProcessEnv(keys.pooled),
102 fromProcessEnv(MYSQL_URL_SECRET),
103 fromProcessEnv(keys.direct),
104 fromProcessEnv(MYSQL_PUBLIC_URL_SECRET),
105 ],
106 name,
107 );
108
109 if (globalThis.__ALCHEMY_RUNTIME__) {
110 return {
111 connectionString: fromEnv(false),
112 directConnectionString: fromEnv(true),
113 } satisfies ConnectMySQLClient;
114 }
115
116 const pooled = runtimeOutput(keys.pooled, mysql.connectionUri);
117 const direct = runtimeOutput(keys.direct, mysql.connectionUri);
118
119 return {
120 connectionString: Effect.gen(function* () {
121 const packed = yield* pooled;
122 const unpacked = yield* direct;
123 return yield* firstUrl(
124 [
125 typeof packed === "string" ? packed : "",
126 fromProcessEnv(keys.pooled),
127 fromProcessEnv(MYSQL_URL_SECRET),
128 typeof unpacked === "string" ? unpacked : "",
129 fromProcessEnv(keys.direct),
130 fromProcessEnv(MYSQL_PUBLIC_URL_SECRET),
131 ],
132 name,
133 );
134 }),
135 directConnectionString: Effect.gen(function* () {
136 const unpacked = yield* direct;
137 const packed = yield* pooled;
138 return yield* firstUrl(
139 [
140 typeof unpacked === "string" ? unpacked : "",
141 fromProcessEnv(keys.direct),
142 fromProcessEnv(MYSQL_URL_SECRET),
143 typeof packed === "string" ? packed : "",
144 fromProcessEnv(keys.pooled),
145 fromProcessEnv(MYSQL_PUBLIC_URL_SECRET),
146 ],
147 name,
148 );
149 }),
150 } satisfies ConnectMySQLClient;
151 }),
152 ),
153 );