The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as PgClient from "@effect/sql-pg/PgClient"; |
| 2 | import * as Redacted from "effect/Redacted"; |
| 3 | |
| 4 | type PgSsl = PgClient.PgPoolConfig["ssl"]; |
| 5 | |
| 6 | /** |
| 7 | * `sslmode` values `@effect/sql-pg` (≥ 4.0.0-rc.113, still as of rc.115) |
| 8 | * refuses unless `ssl` is set explicitly: `sslmode "prefer" is not |
| 9 | * supported: set ssl explicitly to true or false`. node-postgres treated both |
| 10 | * as "TLS on" (aliases of `verify-full`), and URLs carry them by default in |
| 11 | * places — Hyperdrive's local `dev` origin passthrough hands the worker |
| 12 | * `?sslmode=prefer`, and `prefer` is libpq's own default. |
| 13 | */ |
| 14 | const OPPORTUNISTIC_SSL_MODES: ReadonlySet<string> = new Set(["prefer", "allow"]); |
| 15 | |
| 16 | /** |
| 17 | * Resolve the `ssl` option to hand `@effect/sql-pg` for a connection URL. |
| 18 | * |
| 19 | * When the caller left `ssl` implicit and the URL's `sslmode` is `prefer` or |
| 20 | * `allow`, returns `ssl: true` so the connection is TLS-on exactly as it was |
| 21 | * under node-postgres. Railway's `no-verify` mode defaults to TLS without |
| 22 | * certificate verification. Explicit caller settings take precedence, and |
| 23 | * other URL modes keep driving the decision inside `@effect/sql-pg`. |
| 24 | * |
| 25 | * (`@effect/sql-pg` < rc.115 also sent no TLS SNI; that was fixed upstream in |
| 26 | * Effect-TS/effect#8174, so this helper no longer sets `servername`.) |
| 27 | */ |
| 28 | export const resolveSsl = (url: Redacted.Redacted<string>, ssl: PgSsl): PgSsl => { |
| 29 | if (ssl !== undefined) return ssl; |
| 30 | let parsed: URL; |
| 31 | try { |
| 32 | parsed = new URL(Redacted.value(url)); |
| 33 | } catch { |
| 34 | // Let `@effect/sql-pg` report the malformed URL. |
| 35 | return ssl; |
| 36 | } |
| 37 | const sslmode = parsed.searchParams.get("sslmode"); |
| 38 | if (sslmode === "no-verify") return { rejectUnauthorized: false }; |
| 39 | return sslmode !== null && OPPORTUNISTIC_SSL_MODES.has(sslmode) ? true : ssl; |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Normalize the URL and TLS settings together for Effect's Postgres driver. |
| 44 | * Translate node-postgres's `sslmode=no-verify` to `require` with explicit |
| 45 | * TLS options so it also works with stricter URL parsers. Retain its verification |
| 46 | * choice in the TLS options. Other URL parameters and explicit TLS settings |
| 47 | * remain intact; credentials stay redacted. |
| 48 | */ |
| 49 | export const resolveConnectionOptions = ( |
| 50 | url: Redacted.Redacted<string>, |
| 51 | ssl?: PgSsl, |
| 52 | ): { url: Redacted.Redacted<string>; ssl: PgSsl } => { |
| 53 | const resolvedSsl = resolveSsl(url, ssl); |
| 54 | try { |
| 55 | const parsed = new URL(Redacted.value(url)); |
| 56 | if (parsed.searchParams.get("sslmode") === "no-verify") { |
| 57 | parsed.searchParams.set("sslmode", "require"); |
| 58 | return { url: Redacted.make(parsed.toString()), ssl: resolvedSsl }; |
| 59 | } |
| 60 | } catch { |
| 61 | // The driver owns malformed URL diagnostics. |
| 62 | } |
| 63 | return { url, ssl: resolvedSsl }; |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * The `@effect/sql-pg` pool config for a connection URL plus caller options. |
| 68 | * Options such as `prepare` or pool limits pass through; the URL and TLS come |
| 69 | * from {@link resolveConnectionOptions}, honoring an explicit `ssl` option. |
| 70 | */ |
| 71 | export const resolvePoolConfig = ( |
| 72 | url: Redacted.Redacted<string>, |
| 73 | options?: Omit<PgClient.PgPoolConfig, "url">, |
| 74 | ): PgClient.PgPoolConfig => ({ |
| 75 | ...options, |
| 76 | ...resolveConnectionOptions(url, options?.ssl), |
| 77 | }); |