Skip to content

Commit

Permalink
fix(url parser): keep original uri options and default to ssl true
Browse files Browse the repository at this point in the history
  • Loading branch information
jlord committed Nov 22, 2017
1 parent d9f4218 commit e876a72
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/url_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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];
Expand All @@ -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);
});
});
Expand Down

0 comments on commit e876a72

Please sign in to comment.