|
7 | 7 |
|
8 | 8 | 'use strict';
|
9 | 9 |
|
10 |
| -const { URL } = require('url'); |
11 | 10 | const ClientConstants = require('./constants/client');
|
12 | 11 | const Charsets = require('./constants/charsets');
|
| 12 | +const { URL } = require('url'); |
13 | 13 | let SSLProfiles = null;
|
14 | 14 |
|
15 | 15 | const validOptions = {
|
@@ -249,13 +249,31 @@ class ConnectionConfig {
|
249 | 249 | }
|
250 | 250 |
|
251 | 251 | static parseUrl(url) {
|
252 |
| - const parsedUrl = new URL(url); |
| 252 | + // First, split the url. Take the mysql:// part. The username is after that. |
| 253 | + // Then, split at the : - usernames cannot contain a : |
| 254 | + let user = url.split("mysql://")[1]; |
| 255 | + if (user) user = user.split(":")[0]; |
| 256 | + else throw new Error("Invalid connection string"); |
| 257 | + |
| 258 | + // Now, get the password. Remove the mysql://<USER>: part from the connection string, |
| 259 | + // Continue until the last @ - that indicates the host is after it. |
| 260 | + let password = url.substring(8 + user.length + 1); |
| 261 | + if (password) { |
| 262 | + const passwordParts = password.split("@"); |
| 263 | + password = passwordParts.splice(0, passwordParts.length-1).join("@"); |
| 264 | + } else throw new Error("Invalid connection string"); |
| 265 | + |
| 266 | + // Now, let node handle the rest of the url parsing (for the host, port and database) |
| 267 | + // Also, remove the username and password and replace them with placeholders. |
| 268 | + // This way, we do not get an error when the username or password contains special characters. |
| 269 | + const parsedUrl = new URL(url.replace(user, "_PLACEHOLDER_").replace(password, "_PLACEHOLDER_")); |
| 270 | + |
253 | 271 | const options = {
|
254 | 272 | host: parsedUrl.hostname,
|
255 | 273 | port: parsedUrl.port,
|
256 | 274 | database: parsedUrl.pathname.slice(1),
|
257 |
| - user: decodeURIComponent(parsedUrl.username), |
258 |
| - password: decodeURIComponent(parsedUrl.password) |
| 275 | + user, |
| 276 | + password |
259 | 277 | };
|
260 | 278 | parsedUrl.searchParams.forEach((value, key) => {
|
261 | 279 | try {
|
|
0 commit comments