( connectionString: string, drivers?: ReadonlyArray<ConnectionStringDriver>, )
| 142 | * Supported protocols are inferred from the provided drivers/capabilities. |
| 143 | */ |
| 144 | export function parseConnectionString( |
| 145 | connectionString: string, |
| 146 | drivers?: ReadonlyArray<ConnectionStringDriver>, |
| 147 | ): ConnectionStringResult { |
| 148 | if (!connectionString || !connectionString.trim()) { |
| 149 | return { success: false, error: "Connection string is empty" }; |
| 150 | } |
| 151 | |
| 152 | const trimmed = connectionString.trim(); |
| 153 | |
| 154 | let url: URL; |
| 155 | try { |
| 156 | url = new URL(trimmed); |
| 157 | } catch { |
| 158 | return { success: false, error: "Invalid connection string format" }; |
| 159 | } |
| 160 | |
| 161 | const protocol = normalizeProtocol(url.protocol); |
| 162 | const registry = buildProtocolRegistry(drivers); |
| 163 | const resolved = registry.get(protocol); |
| 164 | |
| 165 | if (!resolved) { |
| 166 | const supported = getSupportedConnectionStringProtocols(drivers); |
| 167 | const suffix = |
| 168 | supported.length > 0 ? `. Supported: ${supported.join(", ")}` : ""; |
| 169 | return { |
| 170 | success: false, |
| 171 | error: `Unsupported database driver: ${protocol}${suffix}`, |
| 172 | }; |
| 173 | } |
| 174 | |
| 175 | if (resolved.local) { |
| 176 | const rawPath = url.pathname; |
| 177 | if (!rawPath) { |
| 178 | return { |
| 179 | success: false, |
| 180 | error: "Connection string must include a database path", |
| 181 | }; |
| 182 | } |
| 183 | |
| 184 | const database = decodeURIComponent(rawPath.replace(/^\//, "")); |
| 185 | if (!database) { |
| 186 | return { |
| 187 | success: false, |
| 188 | error: "Connection string must include a database path", |
| 189 | }; |
| 190 | } |
| 191 | |
| 192 | return { |
| 193 | success: true, |
| 194 | params: { |
| 195 | driver: resolved.id, |
| 196 | database, |
| 197 | }, |
| 198 | }; |
| 199 | } |
| 200 | |
| 201 | const host = url.hostname || undefined; |
no test coverage detected