@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 153 lines 4.75 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 ConnectMongo,
9 MongoUrlMissing,
10 connectEnvKeys,
11 type ConnectMongoClient,
12 } from "./ConnectMongo.ts";
13 import { MONGO_URL_SECRET, MONGO_PUBLIC_URL_SECRET, type Mongo } from "./Mongo.ts";
14 import { isRailwayHost } from "./MountVolume.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>, MongoUrlMissing> =>
26 value.length > 0
27 ? Effect.succeed(Redacted.make(value))
28 : Effect.fail(new MongoUrlMissing({ 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>, MongoUrlMissing> => {
44 const found = values.find((value) => value.length > 0);
45 return found !== undefined
46 ? asRedactedUrl(found, name)
47 : Effect.fail(new MongoUrlMissing({ resourceName: name }));
48 };
49
50 /**
51 * Implementation of {@link ConnectMongo}. Provide it on the
52 * {@link Service} Effect.
53 *
54 * At deploy time this packs the private connection URI onto the host
55 * (`RAILWAY_MONGO_*`, `MONGO_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.ConnectMongo(Db);
64 * const url = yield* conn.connectionString;
65 * }).pipe(Effect.provide(Railway.ConnectMongoHttp))
66 * ```
67 *
68 * @layer
69 * @product MongoDB
70 * @provides Railway.ConnectMongo
71 */
72 export const ConnectMongoHttp = Layer.effect(
73 ConnectMongo,
74 Effect.succeed(
75 Effect.fn(function* (mongo: Mongo) {
76 const keys = connectEnvKeys(mongo);
77 const name = mongo.LogicalId;
78 if (!globalThis.__ALCHEMY_RUNTIME__) {
79 const host = yield* Binding.Host;
80 if (isRailwayHost(host)) {
81 yield* host.bind`${mongo}`({
82 env: {
83 [keys.pooled]: mongo.connectionUri,
84 [keys.direct]: mongo.connectionUri,
85 [MONGO_URL_SECRET]: mongo.connectionUri,
86 },
87 });
88 }
89 }
90
91 const fromEnv = (preferDirect: boolean) =>
92 firstUrl(
93 preferDirect
94 ? [
95 fromProcessEnv(keys.direct),
96 fromProcessEnv(MONGO_URL_SECRET),
97 fromProcessEnv(keys.pooled),
98 fromProcessEnv(MONGO_PUBLIC_URL_SECRET),
99 ]
100 : [
101 fromProcessEnv(keys.pooled),
102 fromProcessEnv(MONGO_URL_SECRET),
103 fromProcessEnv(keys.direct),
104 fromProcessEnv(MONGO_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 ConnectMongoClient;
114 }
115
116 const pooled = runtimeOutput(keys.pooled, mongo.connectionUri);
117 const direct = runtimeOutput(keys.direct, mongo.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(MONGO_URL_SECRET),
128 typeof unpacked === "string" ? unpacked : "",
129 fromProcessEnv(keys.direct),
130 fromProcessEnv(MONGO_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(MONGO_URL_SECRET),
143 typeof packed === "string" ? packed : "",
144 fromProcessEnv(keys.pooled),
145 fromProcessEnv(MONGO_PUBLIC_URL_SECRET),
146 ],
147 name,
148 );
149 }),
150 } satisfies ConnectMongoClient;
151 }),
152 ),
153 );