(connection: Connection)
| 6 | const systemDatabases = ["information_schema", "mysql", "performance_schema", "sys"]; |
| 7 | |
| 8 | const getMySQLConnection = async (connection: Connection): Promise<mysql.Connection> => { |
| 9 | const connectionOptions: ConnectionOptions = { |
| 10 | host: connection.host, |
| 11 | port: parseInt(connection.port), |
| 12 | user: connection.username, |
| 13 | password: connection.password, |
| 14 | database: connection.database, |
| 15 | }; |
| 16 | if (connection.ssl) { |
| 17 | connectionOptions.ssl = { |
| 18 | ca: connection.ssl?.ca, |
| 19 | cert: connection.ssl?.cert, |
| 20 | key: connection.ssl?.key, |
| 21 | }; |
| 22 | } else { |
| 23 | // rejectUnauthorized=false to infer sslmode=prefer since hosted MySQL venders have SSL enabled. |
| 24 | connectionOptions.ssl = { |
| 25 | rejectUnauthorized: false, |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | let conn; |
| 30 | if (connection.ssl) { |
| 31 | conn = await mysql.createConnection(connectionOptions); |
| 32 | } else { |
| 33 | try { |
| 34 | conn = await mysql.createConnection(connectionOptions); |
| 35 | } catch (error) { |
| 36 | if (error instanceof Error && error.message.includes("Server does not support secure")) { |
| 37 | connectionOptions.ssl = undefined; |
| 38 | conn = await mysql.createConnection(connectionOptions); |
| 39 | } else { |
| 40 | throw error; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return conn; |
| 45 | }; |
| 46 | |
| 47 | const testConnection = async (connection: Connection): Promise<boolean> => { |
| 48 | const conn = await getMySQLConnection(connection); |
no outgoing calls
no test coverage detected