(options: Config)
| 62 | } |
| 63 | |
| 64 | export const resolveConfig = (options: Config): Effect.Effect<ResolvedConfig, SqlError> => |
| 65 | Effect.suspend(() => { |
| 66 | // Read each secret once so the credential and its SCRAM slot always match. |
| 67 | const { password, url: urlSecret } = options |
| 68 | const parsed: EffectResult.Result<UrlConfig, SqlError> = urlSecret !== undefined |
| 69 | ? parseUrl(Redacted.value(urlSecret)) |
| 70 | : EffectResult.succeed({}) |
| 71 | if (EffectResult.isFailure(parsed)) return Effect.fail(parsed.failure) |
| 72 | const url = parsed.success |
| 73 | const host = options.host ?? url.host ?? "localhost" |
| 74 | const port = options.port ?? url.port ?? 5432 |
| 75 | const username = options.username ?? url.username ?? process.env.USER ?? process.env.USERNAME |
| 76 | if (username === undefined) { |
| 77 | return Effect.fail(configError("No username configured")) |
| 78 | } |
| 79 | const keepAlive = options.keepAlive === undefined ? undefined : keepAliveMillis(options.keepAlive) |
| 80 | if (options.keepAlive !== undefined && keepAlive === undefined) { |
| 81 | return Effect.fail(configError("keepAlive must be between 1 and 2147483647 seconds")) |
| 82 | } |
| 83 | const named: Record<string, string> = Object.create(null) |
| 84 | for (const [name, value] of Object.entries(options.startupParameters ?? {})) { |
| 85 | const key = name.toLowerCase() |
| 86 | if (key === "user" || key === "database" || key === "replication" || key === "options") { |
| 87 | return Effect.fail(configError(`Reserved startup parameter: "${name}"`)) |
| 88 | } |
| 89 | if (key === "client_encoding") { |
| 90 | const encoding = value.toUpperCase() |
| 91 | if (encoding !== "UTF8" && encoding !== "UTF-8") { |
| 92 | return Effect.fail(configError("Startup parameter client_encoding must be UTF8 or UTF-8")) |
| 93 | } |
| 94 | named[key] = "UTF8" |
| 95 | } else { |
| 96 | named[key] = value |
| 97 | } |
| 98 | if (name === "" || name.includes("\0") || value.includes("\0")) { |
| 99 | return Effect.fail( |
| 100 | configError("Startup parameter names must be nonempty and names/values must not contain NUL") |
| 101 | ) |
| 102 | } |
| 103 | } |
| 104 | const startupParameters: PgProtocol.StartupParameters = { |
| 105 | ...named, |
| 106 | user: username, |
| 107 | database: options.database ?? url.database, |
| 108 | application_name: options.applicationName ?? named.application_name ?? url.applicationName ?? "@effect/sql-pg", |
| 109 | options: options.startupOptions ?? url.options |
| 110 | } |
| 111 | for (const [name, value] of Object.entries(startupParameters)) { |
| 112 | if (value?.includes("\0")) { |
| 113 | return Effect.fail(configError(`Startup parameter "${name}" must not contain NUL`)) |
| 114 | } |
| 115 | } |
| 116 | return Effect.succeed<ResolvedConfig>({ |
| 117 | host, |
| 118 | port, |
| 119 | path: options.path ?? (host.startsWith("/") ? `${host}/.s.PGSQL.${port}` : undefined), |
| 120 | ssl: options.ssl ?? (url.ssl === "prefer" ? true : url.ssl ?? false), |
| 121 | sslOptional: options.ssl === undefined && url.ssl === "prefer", |
no test coverage detected