Skip to content

Commit c1c5d8d

Browse files
committed
feat(urlParser): use core URL parser
Adds the ability to opt-in to using the new spec-compliant url parser. Fixes NODE-1304
1 parent 3c31e31 commit c1c5d8d

File tree

1 file changed

+63
-6
lines changed

1 file changed

+63
-6
lines changed

lib/mongo_client.js

+63-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

3-
const parse = require('./url_parser');
3+
const parse = require('mongodb-core').parseConnectionString;
44
const Server = require('./topologies/server');
55
const Mongos = require('./topologies/mongos');
66
const ReplSet = require('./topologies/replset');
77
const EventEmitter = require('events').EventEmitter;
88
const inherits = require('util').inherits;
9+
const deprecate = require('util').deprecate;
910
const ReadPreference = require('mongodb-core').ReadPreference;
1011
const Logger = require('mongodb-core').Logger;
1112
const MongoError = require('mongodb-core').MongoError;
@@ -17,6 +18,12 @@ const authenticate = require('./authenticate');
1718
const ServerSessionPool = require('mongodb-core').Sessions.ServerSessionPool;
1819
const executeOperation = require('./utils').executeOperation;
1920

21+
const legacyParse = deprecate(
22+
require('./url_parser'),
23+
'current URL string parser is deprecated, and will be removed in a future version. ' +
24+
'To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.'
25+
);
26+
2027
/**
2128
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
2229
*
@@ -107,9 +114,54 @@ var validOptionNames = [
107114
'auto_reconnect',
108115
'minSize',
109116
'monitorCommands',
110-
'retryWrites'
117+
'retryWrites',
118+
'useNewUrlParser'
111119
];
112120

121+
const validOptionsLowerCaseToCamelCase = validOptionNames.reduce((obj, name) => {
122+
obj[name.toLowerCase()] = name;
123+
return obj;
124+
}, {});
125+
126+
function transformUrlOptions(_object) {
127+
let object = Object.assign({ servers: _object.hosts }, _object.options);
128+
for (let name in object) {
129+
const camelCaseName = validOptionsLowerCaseToCamelCase[name];
130+
if (camelCaseName) {
131+
object[camelCaseName] = object[name];
132+
}
133+
}
134+
if (_object.auth) {
135+
const auth = _object.auth;
136+
for (let i in auth) {
137+
if (auth[i]) {
138+
object[i] = auth[i];
139+
}
140+
}
141+
if (auth.username) {
142+
object.auth = auth;
143+
object.user = auth.username;
144+
}
145+
if (auth.db) {
146+
object.dbName = auth.db;
147+
}
148+
}
149+
if (object.maxpoolsize) {
150+
object.poolSize = object.maxpoolsize;
151+
}
152+
if (object.readconcernlevel) {
153+
object.readConcern = { level: object.readconcernlevel };
154+
}
155+
if (object.wtimeoutms) {
156+
object.wtimeout = object.wtimeoutms;
157+
}
158+
return object;
159+
}
160+
161+
function legacyTransformUrlOptions(object) {
162+
return mergeOptions(createUnifiedOptions({}, object), object, false);
163+
}
164+
113165
var ignoreOptionNames = ['native_parser'];
114166
var legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db'];
115167

@@ -202,6 +254,7 @@ function validOptions(options) {
202254
* @param {boolean} [options.auto_reconnect=true] Enable auto reconnecting for single server instances
203255
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this client
204256
* @param {number} [options.minSize] If present, the connection pool will be initialized with minSize connections, and will never dip below minSize connections
257+
* @param {boolean} [options.useNewUrlParser=false] Determines whether or not to use the new url parser
205258
* @param {MongoClient~connectCallback} [callback] The command result callback
206259
* @return {MongoClient} a MongoClient instance
207260
*/
@@ -921,14 +974,18 @@ var connect = function(self, url, options, callback) {
921974
);
922975
}
923976

924-
parse(url, options, function(err, object) {
977+
const parseFn = options.useNewUrlParser ? parse : legacyParse;
978+
const transform = options.useNewUrlParser ? transformUrlOptions : legacyTransformUrlOptions;
979+
980+
parseFn(url, options, function(err, _object) {
925981
// Do not attempt to connect if parsing error
926982
if (err) return callback(err);
927983

984+
// Flatten
985+
const object = transform(_object);
986+
928987
// Parse the string
929-
var _finalOptions = createUnifiedOptions({}, object);
930-
_finalOptions = mergeOptions(_finalOptions, object, false);
931-
_finalOptions = createUnifiedOptions(_finalOptions, options);
988+
const _finalOptions = createUnifiedOptions(object, options);
932989

933990
// Check if we have connection and socket timeout set
934991
if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;

0 commit comments

Comments
 (0)