Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

'use strict';

const { URL } = require('url');
const ClientConstants = require('./constants/client');
const Charsets = require('./constants/charsets');
const { URL } = require('url');
let SSLProfiles = null;

const validOptions = {
Expand Down Expand Up @@ -249,13 +249,31 @@ class ConnectionConfig {
}

static parseUrl(url) {
const parsedUrl = new URL(url);
// First, split the url. Take the mysql:// part. The username is after that.
// Then, split at the : - usernames cannot contain a :
let user = url.split("mysql://")[1];
if (user) user = user.split(":")[0];
else throw new Error("Invalid connection string");

// Now, get the password. Remove the mysql://<USER>: part from the connection string,
// Continue until the last @ - that indicates the host is after it.
let password = url.substring(8 + user.length + 1);
if (password) {
const passwordParts = password.split("@");
password = passwordParts.splice(0, passwordParts.length-1).join("@");
} else throw new Error("Invalid connection string");

// Now, let node handle the rest of the url parsing (for the host, port and database)
// Also, remove the username and password and replace them with placeholders.
// This way, we do not get an error when the username or password contains special characters.
const parsedUrl = new URL(url.replace(user, "_PLACEHOLDER_").replace(password, "_PLACEHOLDER_"));

const options = {
host: parsedUrl.hostname,
port: parsedUrl.port,
database: parsedUrl.pathname.slice(1),
user: parsedUrl.username,
password: parsedUrl.password
user,
password
};
parsedUrl.searchParams.forEach((value, key) => {
try {
Expand Down