diff --git a/lib/url_parser.js b/lib/url_parser.js index 331988de177..90946a77154 100644 --- a/lib/url_parser.js +++ b/lib/url_parser.js @@ -17,12 +17,7 @@ module.exports = function(url, options, callback) { } if (result.protocol === 'mongodb+srv:') { - if (options) { - // TODO let the options passed in override - if (options.ssl && options.ssl !== true) { - options.ssl = true; - } - } + if (result.hostname.split('.').length < 3) { return callback(new Error('uri does not have hostname, domainname and tld')); } @@ -56,15 +51,28 @@ module.exports = function(url, options, callback) { else return `${address.name}:${address.port}`; }); - let connectionString = connectionStrings.join(',') + '/?'; + let connectionString = connectionStrings.join(',') + '/'; + + if (!options.ssl && !result.search.match('ssl')) { + // Default to SSL true + connectionString += '?ssl=true'; + } else if (!result.search) connectionString += '?' + // Keep original uri options + if (result.search && !result.search.match('ssl')) { + connectionString += result.search.replace('?', '&'); + } else if (result.search && result.search.match('ssl')) { + connectionString += result.search; + } dns.resolveTxt(result.host, function(err, record) { if (err && err.code !== 'ENODATA') return callback(err); if (err && err.code === 'ENODATA') record = null; + if (record) { if (record.length > 1) { return callback(new Error('multiple text records not allowed')); } + record = record[0]; if (record.length > 1) record = record.join(''); else record = record[0]; @@ -73,9 +81,8 @@ module.exports = function(url, options, callback) { return callback(new Error('text record must only set `authSource` or `replicaSet`')); } - connectionString += record; + connectionString += '&' + record; } - parseHandler(connectionString, options, callback); }); });