@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 60 lines 2.22 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Redacted from "effect/Redacted";
3 import type * as Scope from "effect/Scope";
4 import type { Pool, PoolConfig } from "pg";
5
6 /**
7 * Drop `sslmode` and `channel_binding` from a Postgres connection URI. Hosted
8 * Postgres URIs (Neon, Fly, PlanetScale) carry `sslmode=require`, which
9 * `pg-connection-string` warns about and treats as `verify-full`; set TLS on
10 * the client (`ssl: { rejectUnauthorized }`) instead. A string that isn't a
11 * URL is returned unchanged.
12 */
13 export const stripSslQueryParams = (uri: string): string => {
14 if (!URL.canParse(uri)) return uri;
15 const url = new URL(uri);
16 url.searchParams.delete("sslmode");
17 url.searchParams.delete("channel_binding");
18 return url.toString();
19 };
20
21 /**
22 * Lazily load the raw `pg` driver (an optional peer dependency of
23 * alchemy) with a descriptive failure when it isn't installed. CJS/ESM
24 * interop is normalized — callers always get the module's named surface.
25 */
26 export const importPg = (): Promise<typeof import("pg")> =>
27 import("pg")
28 .then((mod) =>
29 (mod as { default?: { Pool?: unknown } }).default?.Pool !== undefined
30 ? (mod as unknown as { default: typeof import("pg") }).default
31 : mod,
32 )
33 .catch((cause) => {
34 throw new Error(
35 "Failed to load the 'pg' driver. Install the optional peer dependency 'pg' to connect to Postgres.",
36 { cause },
37 );
38 });
39
40 /**
41 * Open a raw `pg.Pool` on the current `Scope` — `pool.end()` runs when the
42 * scope closes. Pair with `makeExecutionMemo` for the one-pool-per-event
43 * shape workerd and Lambda require (see `SQL/Postgres.ts` for the
44 * effect-sql equivalent).
45 *
46 * `max` defaults to 1: per-execution pools never need more than one
47 * connection.
48 */
49 export const openPostgresPool = (
50 url: Effect.Effect<Redacted.Redacted<string>>,
51 config?: Omit<PoolConfig, "connectionString">,
52 ): Effect.Effect<Pool, never, Scope.Scope> =>
53 Effect.gen(function* () {
54 const pg = yield* Effect.promise(importPg);
55 const connectionString = Redacted.value(yield* url);
56 return yield* Effect.acquireRelease(
57 Effect.sync(() => new pg.Pool({ connectionString, max: 1, ...config })),
58 (pool) => Effect.promise(() => pool.end()),
59 );
60 });