| 23 | * Expected format: mssql://username:password@host:port/database |
| 24 | */ |
| 25 | export class SQLServerDSNParser implements DSNParser { |
| 26 | async parse(dsn: string, config?: ConnectorConfig): Promise<sql.config> { |
| 27 | const connectionTimeoutSeconds = config?.connectionTimeoutSeconds; |
| 28 | const queryTimeoutSeconds = config?.queryTimeoutSeconds; |
| 29 | // Basic validation |
| 30 | if (!this.isValidDSN(dsn)) { |
| 31 | const obfuscatedDSN = obfuscateDSNPassword(dsn); |
| 32 | const expectedFormat = this.getSampleDSN(); |
| 33 | throw new Error( |
| 34 | `Invalid SQL Server DSN format.\nProvided: ${obfuscatedDSN}\nExpected: ${expectedFormat}` |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | try { |
| 39 | // Use the SafeURL helper to parse DSNs with special characters |
| 40 | const url = new SafeURL(dsn); |
| 41 | |
| 42 | // Parse additional options from query parameters |
| 43 | const options: Record<string, any> = {}; |
| 44 | |
| 45 | // Process query parameters |
| 46 | url.forEachSearchParam((value, key) => { |
| 47 | if (key === "authentication") { |
| 48 | options.authentication = value; |
| 49 | } else if (key === "sslmode") { |
| 50 | options.sslmode = value; |
| 51 | } else if (key === "instanceName") { |
| 52 | options.instanceName = value; |
| 53 | } else if (key === "domain") { |
| 54 | options.domain = value; |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | // Validate NTLM parameter consistency |
| 59 | if (options.authentication === "ntlm" && !options.domain) { |
| 60 | throw new Error("NTLM authentication requires 'domain' parameter"); |
| 61 | } |
| 62 | if (options.domain && options.authentication !== "ntlm") { |
| 63 | throw new Error("Parameter 'domain' requires 'authentication=ntlm'"); |
| 64 | } |
| 65 | |
| 66 | // Handle sslmode parameter similar to PostgreSQL and MySQL |
| 67 | if (options.sslmode) { |
| 68 | if (options.sslmode === "disable") { |
| 69 | options.encrypt = false; |
| 70 | options.trustServerCertificate = false; |
| 71 | } else if (options.sslmode === "require") { |
| 72 | options.encrypt = true; |
| 73 | options.trustServerCertificate = true; |
| 74 | } |
| 75 | // Default behavior (certificate verification) is handled by the default values below |
| 76 | } |
| 77 | |
| 78 | // Base configuration |
| 79 | const config: sql.config = { |
| 80 | server: url.hostname, |
| 81 | port: url.port ? parseInt(url.port) : 1433, // Default SQL Server port |
| 82 | database: url.pathname ? url.pathname.substring(1) : '', // Remove leading slash |
nothing calls this directly
no outgoing calls
no test coverage detected