diff --git a/lib/ConnectionConfig.js b/lib/ConnectionConfig.js index 06f4399c5..a6f8fa78b 100644 --- a/lib/ConnectionConfig.js +++ b/lib/ConnectionConfig.js @@ -177,7 +177,7 @@ 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, @@ -185,13 +185,26 @@ ConnectionConfig.parseUrl = function(url) { database : url.pathname.substr(1) }; - if (url.auth) { + if (typeof url.username == 'string') { + options.user = url.username; + options.password = decodeUriComponent(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) { + url.searchParams.forEach(function (value, 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]; @@ -207,3 +220,13 @@ ConnectionConfig.parseUrl = function(url) { return options; }; + +function decodeUriComponent(str) { + return str.replace(/\%([a-f0-9]{2})/ig, function (_, hex) { + try { + return String.fromCharCode(parseInt(hex, 16)); + } catch (e) { + return _; + } + }); +}