1
1
'use strict' ;
2
2
3
- const parse = require ( './url_parser' ) ;
3
+ const parse = require ( 'mongodb-core' ) . parseConnectionString ;
4
4
const Server = require ( './topologies/server' ) ;
5
5
const Mongos = require ( './topologies/mongos' ) ;
6
6
const ReplSet = require ( './topologies/replset' ) ;
7
7
const EventEmitter = require ( 'events' ) . EventEmitter ;
8
8
const inherits = require ( 'util' ) . inherits ;
9
+ const deprecate = require ( 'util' ) . deprecate ;
9
10
const ReadPreference = require ( 'mongodb-core' ) . ReadPreference ;
10
11
const Logger = require ( 'mongodb-core' ) . Logger ;
11
12
const MongoError = require ( 'mongodb-core' ) . MongoError ;
@@ -17,6 +18,12 @@ const authenticate = require('./authenticate');
17
18
const ServerSessionPool = require ( 'mongodb-core' ) . Sessions . ServerSessionPool ;
18
19
const executeOperation = require ( './utils' ) . executeOperation ;
19
20
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
+
20
27
/**
21
28
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
22
29
*
@@ -107,9 +114,54 @@ var validOptionNames = [
107
114
'auto_reconnect' ,
108
115
'minSize' ,
109
116
'monitorCommands' ,
110
- 'retryWrites'
117
+ 'retryWrites' ,
118
+ 'useNewUrlParser'
111
119
] ;
112
120
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
+
113
165
var ignoreOptionNames = [ 'native_parser' ] ;
114
166
var legacyOptionNames = [ 'server' , 'replset' , 'replSet' , 'mongos' , 'db' ] ;
115
167
@@ -202,6 +254,7 @@ function validOptions(options) {
202
254
* @param {boolean } [options.auto_reconnect=true] Enable auto reconnecting for single server instances
203
255
* @param {boolean } [options.monitorCommands=false] Enable command monitoring for this client
204
256
* @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
205
258
* @param {MongoClient~connectCallback } [callback] The command result callback
206
259
* @return {MongoClient } a MongoClient instance
207
260
*/
@@ -921,14 +974,18 @@ var connect = function(self, url, options, callback) {
921
974
) ;
922
975
}
923
976
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 ) {
925
981
// Do not attempt to connect if parsing error
926
982
if ( err ) return callback ( err ) ;
927
983
984
+ // Flatten
985
+ const object = transform ( _object ) ;
986
+
928
987
// 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 ) ;
932
989
933
990
// Check if we have connection and socket timeout set
934
991
if ( _finalOptions . socketTimeoutMS == null ) _finalOptions . socketTimeoutMS = 360000 ;
0 commit comments