(dsn: string, config?: ConnectorConfig)
| 39 | */ |
| 40 | class SQLiteDSNParser implements DSNParser { |
| 41 | async parse(dsn: string, config?: ConnectorConfig): Promise<{ dbPath: string }> { |
| 42 | // Basic validation |
| 43 | if (!this.isValidDSN(dsn)) { |
| 44 | const obfuscatedDSN = obfuscateDSNPassword(dsn); |
| 45 | const expectedFormat = this.getSampleDSN(); |
| 46 | throw new Error( |
| 47 | `Invalid SQLite DSN format.\nProvided: ${obfuscatedDSN}\nExpected: ${expectedFormat}` |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | // Use SafeURL helper to handle special characters properly |
| 53 | const url = new SafeURL(dsn); |
| 54 | let dbPath: string; |
| 55 | |
| 56 | // Handle in-memory database |
| 57 | if (url.hostname === "" && url.pathname === "/:memory:") { |
| 58 | dbPath = ":memory:"; |
| 59 | } |
| 60 | // Handle file paths |
| 61 | else { |
| 62 | // Get the path part, handling both relative and absolute paths |
| 63 | if (url.pathname.startsWith("//")) { |
| 64 | // Unix absolute path: sqlite:///path/to/db.sqlite |
| 65 | dbPath = url.pathname.substring(2); // Remove leading // |
| 66 | } else if (url.pathname.match(/^\/[A-Za-z]:\//)) { |
| 67 | // Windows absolute path: sqlite:///C:/path/to/db.sqlite |
| 68 | // URL parser adds leading slash to drive letter paths, so strip it |
| 69 | dbPath = url.pathname.substring(1); |
| 70 | } else { |
| 71 | // Relative path: sqlite://./path/to/db.sqlite |
| 72 | dbPath = url.pathname; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return { dbPath }; |
| 77 | } catch (error) { |
| 78 | throw new Error( |
| 79 | `Failed to parse SQLite DSN: ${error instanceof Error ? error.message : String(error)}` |
| 80 | ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | getSampleDSN(): string { |
| 85 | return "sqlite:///path/to/database.db"; |
nothing calls this directly
no test coverage detected