Skip to content

Commit

Permalink
Use WHATWG URL parsing, if available, instead of url.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed Nov 21, 2023
1 parent dc9c152 commit 4bda23e
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/ConnectionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,36 @@ ConnectionConfig.parseFlagList = function parseFlagList(flagList) {
};

ConnectionConfig.parseUrl = function(url) {
url = urlParse(url, true);
url = (typeof URL == 'function' && typeof URL.prototype == 'object' ? new URL(url) : urlParse(url, true));

var options = {
host : url.hostname,
port : url.port,
database : url.pathname.substr(1)
};

if (url.auth) {
if (typeof url.username == 'string') {
options.user = url.username;
options.password = url.password;
} else if (url.auth) {
var auth = url.auth.split(':');
options.user = auth.shift();
options.password = auth.join(':');
}

if (url.query) {
if (url.searchParams) {
for (var key in url.searchParams) {
var value = url.searchParams[key];

try {
// Try to parse this as a JSON expression first
options[key] = JSON.parse(value);
} catch (err) {
// Otherwise assume it is a plain string
options[key] = value;
}
}
} else if (url.query) {
for (var key in url.query) {
var value = url.query[key];

Expand Down

0 comments on commit 4bda23e

Please sign in to comment.