@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 393 lines 13.3 KB
Raw
1 import type { Server, ServerOptions } from "@prisma/dev";
2 import * as Data from "effect/Data";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as Option from "effect/Option";
6 import * as Path from "effect/Path";
7 import * as ChildProcess from "effect/process/ChildProcess";
8 import * as Redacted from "effect/Redacted";
9 import * as Result from "effect/Result";
10 import * as Semaphore from "effect/Semaphore";
11 import * as Stream from "effect/Stream";
12 import type { DatabaseDev } from "../Database.ts";
13
14 export class PrismaDevDatabaseInvalidProps extends Data.TaggedError(
15 "PrismaDevDatabaseInvalidProps",
16 )<{
17 readonly message: string;
18 readonly cause?: unknown;
19 }> {}
20
21 export interface PrismaDevDatabaseAttrs {
22 directConnectionString: Redacted.Redacted<string>;
23 pooledConnectionString: Redacted.Redacted<string>;
24 accelerateConnectionString: Redacted.Redacted<string>;
25 host: string | null;
26 user: string | null;
27 password: Redacted.Redacted<string> | undefined;
28 }
29
30 interface PrismaDevDatabaseEntry {
31 optionsKey: string;
32 migrationKey: string | undefined;
33 server: Server;
34 attrs: PrismaDevDatabaseAttrs | undefined;
35 }
36
37 const servers = new Map<string, PrismaDevDatabaseEntry>();
38 const startMutex = Semaphore.makeUnsafe(1);
39 const MIGRATION_OUTPUT_LIMIT_BYTES = 1024 * 1024;
40
41 export class PrismaDevDatabaseError extends Data.TaggedError("PrismaDevDatabaseError")<{
42 readonly message: string;
43 readonly cause?: unknown;
44 }> {}
45
46 const toError = (message: string) => (cause: unknown) =>
47 new PrismaDevDatabaseError({
48 message: cause instanceof Error ? cause.message : `${message}: ${String(cause)}`,
49 cause,
50 });
51
52 // A literal specifier makes bundlers follow `@prisma/dev` (and its runtime
53 // tarballs) into every Worker that imports a Prisma Database. It is only ever
54 // loaded at deploy time, by the local provider.
55 const PRISMA_DEV = "@prisma/dev";
56
57 const importPrismaDev = Effect.tryPromise({
58 try: (): Promise<typeof import("@prisma/dev")> => import(PRISMA_DEV),
59 catch: toError("Failed to load @prisma/dev"),
60 });
61
62 const stableJson = (value: unknown) => Effect.sync(() => JSON.stringify(value) ?? "");
63
64 const sanitizeName = (value: string) =>
65 value.replace(/[^a-zA-Z0-9_.-]+/g, "-").replace(/^-+|-+$/g, "") || "alchemy";
66
67 const optionsFrom = (databaseId: string, dev: DatabaseDev): ServerOptions => ({
68 name: dev.name ?? `alchemy-${sanitizeName(databaseId)}`,
69 persistenceMode: dev.persistenceMode ?? "stateful",
70 port: dev.port,
71 databasePort: dev.databasePort,
72 shadowDatabasePort: dev.shadowDatabasePort,
73 debug: dev.debug,
74 databaseConnectTimeoutMillis: dev.databaseConnectTimeoutMillis,
75 databaseIdleTimeoutMillis: dev.databaseIdleTimeoutMillis,
76 shadowDatabaseConnectTimeoutMillis: dev.shadowDatabaseConnectTimeoutMillis,
77 shadowDatabaseIdleTimeoutMillis: dev.shadowDatabaseIdleTimeoutMillis,
78 });
79
80 const parseUrl = (label: string, value: string) =>
81 Effect.try({
82 try: () => new URL(value),
83 catch: toError(`Invalid ${label} connection string`),
84 });
85
86 const normalizeConnectionString = Effect.fn(function* (value: string) {
87 const url = yield* parseUrl("local Prisma", value);
88 if (url.hostname === "localhost" || url.hostname === "::1" || url.hostname === "[::1]") {
89 url.hostname = "127.0.0.1";
90 }
91 return url.toString();
92 });
93
94 const detailsFrom = Effect.fn(function* (value: string) {
95 const url = yield* parseUrl("direct Prisma", value);
96 return yield* Effect.try({
97 try: () => ({
98 host: url.hostname || null,
99 user: url.username ? decodeURIComponent(url.username) : null,
100 password: url.password ? Redacted.make(decodeURIComponent(url.password)) : undefined,
101 }),
102 catch: toError("Invalid direct Prisma credentials"),
103 });
104 });
105
106 export const prismaDevDatabaseAttrsFromServer = Effect.fn(function* (server: Server) {
107 const direct = yield* normalizeConnectionString(
108 server.database.prismaORMConnectionString ?? server.database.connectionString,
109 );
110 const pooled = server.ppg.url;
111 const details = yield* detailsFrom(direct);
112 return {
113 directConnectionString: Redacted.make(direct),
114 pooledConnectionString: Redacted.make(pooled),
115 accelerateConnectionString: Redacted.make(pooled),
116 host: details.host,
117 user: details.user,
118 password: details.password,
119 };
120 });
121
122 const startServer = Effect.fn(function* (databaseId: string, options: ServerOptions) {
123 const prismaDev = yield* importPrismaDev;
124 return yield* Semaphore.withPermits(
125 startMutex,
126 1,
127 )(
128 Effect.tryPromise({
129 try: () => prismaDev.startPrismaDevServer(options),
130 catch: toError(`Failed to start local Prisma database ${databaseId}`),
131 }),
132 );
133 });
134
135 const closeEntry = Effect.fn(function* (entry: PrismaDevDatabaseEntry) {
136 yield* Effect.tryPromise({
137 try: () => entry.server.close(),
138 catch: toError("Failed to stop local Prisma database"),
139 });
140 });
141
142 const collectMigrationOutput = <E>(
143 stream: Stream.Stream<Uint8Array, E>,
144 label: "stdout" | "stderr",
145 ) =>
146 Stream.runFoldEffect(
147 stream,
148 () => ({ chunks: [] as Uint8Array[], bytes: 0 }),
149 (state, chunk) => {
150 const bytes = state.bytes + chunk.byteLength;
151 return bytes > MIGRATION_OUTPUT_LIMIT_BYTES
152 ? Effect.fail(
153 new PrismaDevDatabaseError({
154 message: `Local Prisma migration ${label} exceeded the ${MIGRATION_OUTPUT_LIMIT_BYTES} byte safety limit. Reduce migration verbosity.`,
155 }),
156 )
157 : Effect.succeed({ chunks: [...state.chunks, chunk], bytes });
158 },
159 ).pipe(
160 Effect.map(({ chunks, bytes }) => {
161 const output = new Uint8Array(bytes);
162 let offset = 0;
163 for (const chunk of chunks) {
164 output.set(chunk, offset);
165 offset += chunk.byteLength;
166 }
167 return new TextDecoder().decode(output);
168 }),
169 );
170
171 const sensitiveValuesFromUrl = (value: string) => {
172 try {
173 const url = new URL(value);
174 const values = new Set<string>();
175 const add = (secret: string | undefined) => {
176 if (!secret) return;
177 values.add(secret);
178 try {
179 values.add(decodeURIComponent(secret));
180 } catch {
181 // Keep the original value when it is not valid percent-encoding.
182 }
183 values.add(encodeURIComponent(secret));
184 };
185 add(url.username);
186 add(url.password);
187 for (const [key, secret] of url.searchParams) {
188 if (/(?:api[_-]?key|token|secret|password|credential)/i.test(key)) {
189 add(secret);
190 }
191 }
192 return values;
193 } catch {
194 return new Set<string>();
195 }
196 };
197
198 const redactMigrationOutput = (output: string, attrs: PrismaDevDatabaseAttrs) => {
199 const password = attrs.password ? Redacted.value(attrs.password) : undefined;
200 const connectionStrings = [
201 Redacted.value(attrs.directConnectionString),
202 Redacted.value(attrs.pooledConnectionString),
203 Redacted.value(attrs.accelerateConnectionString),
204 ];
205 const knownSecrets = [
206 ...connectionStrings,
207 ...connectionStrings.flatMap((value) => [...sensitiveValuesFromUrl(value)]),
208 password,
209 password === undefined ? undefined : encodeURIComponent(password),
210 ]
211 .filter((value): value is string => value !== undefined && value !== "")
212 .sort((left, right) => right.length - left.length);
213 return knownSecrets.reduce(
214 (redacted, secret) => redacted.split(secret).join("[REDACTED]"),
215 output,
216 );
217 };
218
219 const runMigration = Effect.fn(function* (
220 dev: DatabaseDev,
221 attrs: PrismaDevDatabaseAttrs,
222 timeoutSeconds: number,
223 ) {
224 const path = yield* Path.Path;
225 const command = dev.migrate;
226 if (command === undefined || command.trim() === "") return;
227
228 const cwd = dev.migrateCwd ? path.resolve(dev.migrateCwd) : path.resolve(".");
229 const resultOption = yield* Effect.scoped(
230 Effect.gen(function* () {
231 const handle = yield* ChildProcess.make(command, [], {
232 shell: true,
233 cwd,
234 env: {
235 DATABASE_URL: Redacted.value(attrs.pooledConnectionString),
236 DIRECT_URL: Redacted.value(attrs.directConnectionString),
237 POOLED_DATABASE_URL: Redacted.value(attrs.pooledConnectionString),
238 },
239 extendEnv: true,
240 stdin: "ignore",
241 stdout: "pipe",
242 stderr: "pipe",
243 // Scoped interruption must terminate the entire POSIX process group
244 // immediately. The Effect process runtime defaults to a detached group
245 // on POSIX, so descendants cannot outlive a timed-out migration.
246 killSignal: "SIGKILL",
247 });
248 return yield* Effect.all(
249 [
250 handle.exitCode,
251 collectMigrationOutput(handle.stdout, "stdout"),
252 collectMigrationOutput(handle.stderr, "stderr"),
253 ] as const,
254 { concurrency: 3 },
255 );
256 }).pipe(Effect.timeoutOption(Duration.seconds(timeoutSeconds))),
257 );
258 if (Option.isNone(resultOption)) {
259 return yield* new PrismaDevDatabaseError({
260 message: `Local Prisma migration command timed out after ${timeoutSeconds} seconds and was terminated.`,
261 });
262 }
263 const [exitCode, rawStdout, rawStderr] = resultOption.value;
264 const stdout = redactMigrationOutput(rawStdout, attrs);
265 const stderr = redactMigrationOutput(rawStderr, attrs);
266 if (exitCode !== 0) {
267 return yield* new PrismaDevDatabaseError({
268 message: `Local Prisma migration command failed with exit code ${exitCode}${stderr ? `\n${stderr}` : ""}`,
269 });
270 }
271 if (stdout) yield* Effect.logDebug("Local Prisma migration output", stdout);
272 if (stderr) yield* Effect.logDebug("Local Prisma migration stderr", stderr);
273 });
274
275 export const ensurePrismaDevDatabase = Effect.fn(function* (
276 databaseId: string,
277 dev: false | DatabaseDev | undefined,
278 ) {
279 if (dev === false) {
280 const cached = servers.get(databaseId);
281 if (cached !== undefined) {
282 yield* closeEntry(cached);
283 servers.delete(databaseId);
284 }
285 return undefined;
286 }
287 const config: DatabaseDev = dev ?? {};
288 if (config.provider !== undefined && config.provider !== "@prisma/dev") {
289 return yield* new PrismaDevDatabaseInvalidProps({
290 message: `Unsupported Prisma local database provider ${String(config.provider)}`,
291 });
292 }
293 const migrateTimeoutSeconds = config.migrateTimeoutSeconds ?? 900;
294 if (!Number.isFinite(migrateTimeoutSeconds) || migrateTimeoutSeconds <= 0) {
295 return yield* new PrismaDevDatabaseInvalidProps({
296 message: "migrateTimeoutSeconds must be a positive finite number.",
297 });
298 }
299
300 const options = optionsFrom(databaseId, config);
301 const optionsKey = yield* stableJson(options);
302 const cached = servers.get(databaseId);
303 let entry = cached;
304 if (entry === undefined || entry.optionsKey !== optionsKey || entry.attrs === undefined) {
305 if (entry !== undefined) {
306 // Keep the handle tracked when close fails so a later cleanup can retry
307 // instead of leaking an unreachable local database process.
308 yield* closeEntry(entry);
309 servers.delete(databaseId);
310 }
311 const server = yield* startServer(databaseId, options);
312 // `@prisma/dev` binds Postgres/HTTP to 127.0.0.1. Native Linux Docker
313 // must not SYN the bridge IP (UFW INPUT). The workerd sidecar proxy
314 // unix-socket-tunnels these ports into the container netns.
315 const attrsResult = yield* Effect.result(prismaDevDatabaseAttrsFromServer(server));
316 if (Result.isFailure(attrsResult)) {
317 const closeResult = yield* Effect.result(
318 Effect.tryPromise({
319 try: () => server.close(),
320 catch: toError("Failed to stop invalid local Prisma database"),
321 }),
322 );
323 if (Result.isFailure(closeResult)) {
324 servers.set(databaseId, {
325 optionsKey,
326 migrationKey: undefined,
327 server,
328 attrs: undefined,
329 });
330 return yield* Effect.fail(
331 new AggregateError(
332 [attrsResult.failure, closeResult.failure],
333 `Failed to initialize and stop local Prisma database ${databaseId}`,
334 ),
335 );
336 }
337 return yield* attrsResult.failure;
338 }
339 const attrs = attrsResult.success;
340 entry = {
341 optionsKey,
342 migrationKey: undefined,
343 server,
344 attrs,
345 };
346 servers.set(databaseId, entry);
347 }
348
349 if (entry.attrs === undefined) {
350 return yield* new PrismaDevDatabaseError({
351 message: `Local Prisma database ${databaseId} has no usable endpoints.`,
352 });
353 }
354 const attrs = entry.attrs;
355
356 if (config.migrate !== undefined && config.migrate.trim() !== "") {
357 const migrationKey = yield* stableJson({
358 command: config.migrate,
359 cwd: config.migrateCwd,
360 optionsKey,
361 });
362 if (entry.migrationKey !== migrationKey) {
363 yield* runMigration(config, attrs, migrateTimeoutSeconds);
364 entry.migrationKey = migrationKey;
365 }
366 }
367
368 return attrs;
369 });
370
371 export const closePrismaDevDatabase = Effect.fn(function* (databaseId: string) {
372 const entry = servers.get(databaseId);
373 if (entry === undefined) return;
374 yield* closeEntry(entry);
375 servers.delete(databaseId);
376 });
377
378 export const closePrismaDevDatabases = Effect.fn(function* () {
379 const ids = Array.from(servers.keys());
380 const failures: Error[] = [];
381 for (const id of ids) {
382 const result = yield* closePrismaDevDatabase(id).pipe(Effect.result);
383 if (Result.isFailure(result)) failures.push(result.failure);
384 }
385 if (failures.length === 1) {
386 return yield* Effect.fail(failures[0]!);
387 }
388 if (failures.length > 1) {
389 return yield* Effect.fail(
390 new AggregateError(failures, `Failed to stop ${failures.length} local Prisma databases`),
391 );
392 }
393 });