Skip to content

Commit 52d76e3

Browse files
authored
feat(urlParser): default useNewUrlParser to true
* feat(UrlParser): default useNewUrlParser to true * a patch that fixes the readPreference issue * fix(urlParser): defaultDatabase should be test
1 parent 0f5278d commit 52d76e3

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

lib/core/uri_parser.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ function parseConnectionString(uri, options, callback) {
534534
if (parsedOptions.auth.username) auth.username = parsedOptions.auth.username;
535535
if (parsedOptions.auth.user) auth.username = parsedOptions.auth.user;
536536
if (parsedOptions.auth.password) auth.password = parsedOptions.auth.password;
537+
} else {
538+
if (parsedOptions.username) auth.username = parsedOptions.username;
539+
if (parsedOptions.user) auth.username = parsedOptions.user;
540+
if (parsedOptions.password) auth.password = parsedOptions.password;
537541
}
538542

539543
if (cap[4].split('?')[0].indexOf('@') !== -1) {
@@ -551,8 +555,8 @@ function parseConnectionString(uri, options, callback) {
551555
return callback(new MongoParseError('Unescaped colon in authority section'));
552556
}
553557

554-
auth.username = qs.unescape(authParts[0]);
555-
auth.password = authParts[1] ? qs.unescape(authParts[1]) : null;
558+
if (!auth.username) auth.username = qs.unescape(authParts[0]);
559+
if (!auth.password) auth.password = authParts[1] ? qs.unescape(authParts[1]) : null;
556560
}
557561

558562
let hostParsingError = null;
@@ -617,6 +621,8 @@ function parseConnectionString(uri, options, callback) {
617621

618622
if (result.auth && result.auth.db) {
619623
result.defaultDatabase = result.auth.db;
624+
} else {
625+
result.defaultDatabase = 'test';
620626
}
621627

622628
try {

lib/mongo_client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const CloseOperation = require('./operations/close');
140140
* @param {boolean} [options.auto_reconnect=true] Enable auto reconnecting for single server instances
141141
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this client
142142
* @param {number} [options.minSize] If present, the connection pool will be initialized with minSize connections, and will never dip below minSize connections
143-
* @param {boolean} [options.useNewUrlParser=false] Determines whether or not to use the new url parser. Enables the new, spec-compliant, url parser shipped in the core driver. This url parser fixes a number of problems with the original parser, and aims to outright replace that parser in the near future.
143+
* @param {boolean} [options.useNewUrlParser=true] Determines whether or not to use the new url parser. Enables the new, spec-compliant, url parser shipped in the core driver. This url parser fixes a number of problems with the original parser, and aims to outright replace that parser in the near future. Defaults to true, and must be explicitly set to false to use the legacy url parser.
144144
* @param {boolean} [options.useUnifiedTopology] Enables the new unified topology layer
145145
* @param {AutoEncryptionOptions} [options.autoEncryption] Optionally enable client side auto encryption
146146
* @param {MongoClient~connectCallback} [callback] The command result callback

lib/operations/connect.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,10 @@ function connect(mongoClient, url, options, callback) {
260260
return connectWithUrl(mongoClient, url, options, connectCallback);
261261
}
262262

263-
const parseFn = options.useNewUrlParser ? parse : legacyParse;
264-
const transform = options.useNewUrlParser ? transformUrlOptions : legacyTransformUrlOptions;
263+
const useNewUrlParser = options.useNewUrlParser !== false;
264+
265+
const parseFn = useNewUrlParser ? parse : legacyParse;
266+
const transform = useNewUrlParser ? transformUrlOptions : legacyTransformUrlOptions;
265267

266268
parseFn(url, options, (err, _object) => {
267269
// Do not attempt to connect if parsing error
@@ -278,6 +280,7 @@ function connect(mongoClient, url, options, callback) {
278280
if (_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;
279281
if (_finalOptions.retryWrites == null) _finalOptions.retryWrites = true;
280282
if (_finalOptions.useRecoveryToken == null) _finalOptions.useRecoveryToken = true;
283+
if (_finalOptions.readPreference == null) _finalOptions.readPreference = 'primary';
281284

282285
if (_finalOptions.db_options && _finalOptions.db_options.auth) {
283286
delete _finalOptions.db_options.auth;
@@ -650,16 +653,16 @@ function transformUrlOptions(_object) {
650653
object.dbName = _object.defaultDatabase;
651654
}
652655

653-
if (object.maxpoolsize) {
654-
object.poolSize = object.maxpoolsize;
656+
if (object.maxPoolSize) {
657+
object.poolSize = object.maxPoolSize;
655658
}
656659

657-
if (object.readconcernlevel) {
658-
object.readConcern = new ReadConcern(object.readconcernlevel);
660+
if (object.readConcernLevel) {
661+
object.readConcern = new ReadConcern(object.readConcernLevel);
659662
}
660663

661-
if (object.wtimeoutms) {
662-
object.wtimeout = object.wtimeoutms;
664+
if (object.wTimeoutMS) {
665+
object.wtimeout = object.wTimeoutMS;
663666
}
664667

665668
if (_object.srvHost) {

test/functional/mongo_client_tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ describe('MongoClient', function() {
435435
const client = configuration.newClient('user:password@localhost:27017/test');
436436

437437
client.connect(function(err) {
438-
test.equal(err.message, 'Invalid schema, expected `mongodb` or `mongodb+srv`');
438+
expect(err).to.exist.and.to.have.property('message', 'Invalid connection string');
439439
done();
440440
});
441441
}

0 commit comments

Comments
 (0)