Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getHost(name, opts) {
// encode db name if opts.prefix is a url (#5574)
if (hasUrlPrefix(opts)) {
var dbName = opts.name.substr(opts.prefix.length);
// Ensure prefix has a trailing slash
var prefix = opts.prefix.replace(/\/?$/, '/');
name = prefix + encodeURIComponent(dbName);
}
var uri = parseUri(name);
if (uri.user || uri.password) {
uri.auth = {username: uri.user, password: uri.password};
}
// Split the path part of the URI into parts using '/' as the delimiter
// after removing any leading '/' and any trailing '/'
var parts = uri.path.replace(/(^\/|\/$)/g, '').split('/');
uri.db = parts.pop();
// Prevent double encoding of URI component
if (uri.db.indexOf('%') === -1) {
uri.db = encodeURIComponent(uri.db);
}
uri.path = parts.join('/');
function hasUrlPrefix(opts) {
if (!opts.prefix) {
return false;
}
var protocol = parseUri(opts.prefix).protocol;
return protocol === 'http' || protocol === 'https';
}