@@ -263,7 +263,7 @@ export function parseOptions(
263
263
264
264
mongoOptions . hosts = isSRV ? [ ] : hosts . map ( HostAddress . fromString ) ;
265
265
266
- const urlOptions = new CaseInsensitiveMap < any [ ] > ( ) ;
266
+ const urlOptions = new CaseInsensitiveMap < unknown [ ] > ( ) ;
267
267
268
268
if ( url . pathname !== '/' && url . pathname !== '' ) {
269
269
const dbName = decodeURIComponent (
@@ -298,7 +298,7 @@ export function parseOptions(
298
298
}
299
299
}
300
300
301
- const objectOptions = new CaseInsensitiveMap (
301
+ const objectOptions = new CaseInsensitiveMap < unknown > (
302
302
Object . entries ( options ) . filter ( ( [ , v ] ) => v != null )
303
303
) ;
304
304
@@ -316,48 +316,42 @@ export function parseOptions(
316
316
317
317
// All option collection
318
318
319
- const allOptions = new CaseInsensitiveMap ( ) ;
319
+ const allProvidedOptions = new CaseInsensitiveMap < unknown [ ] > ( ) ;
320
320
321
- const allKeys = new Set < string > ( [
322
- ...urlOptions . keys ( ) ,
323
- ...objectOptions . keys ( ) ,
324
- ...DEFAULT_OPTIONS . keys ( )
325
- ] ) ;
321
+ const allProvidedKeys = new Set < string > ( [ ...urlOptions . keys ( ) , ...objectOptions . keys ( ) ] ) ;
326
322
327
- for ( const key of allKeys ) {
323
+ for ( const key of allProvidedKeys ) {
328
324
const values = [ ] ;
329
325
const objectOptionValue = objectOptions . get ( key ) ;
330
326
if ( objectOptionValue != null ) {
331
327
values . push ( objectOptionValue ) ;
332
328
}
333
- const urlValue = urlOptions . get ( key ) ;
334
- if ( urlValue != null ) {
335
- values . push ( ...urlValue ) ;
336
- }
337
- const defaultOptionsValue = DEFAULT_OPTIONS . get ( key ) ;
338
- if ( defaultOptionsValue != null ) {
339
- values . push ( defaultOptionsValue ) ;
340
- }
341
- allOptions . set ( key , values ) ;
329
+
330
+ const urlValues = urlOptions . get ( key ) ?? [ ] ;
331
+ values . push ( ...urlValues ) ;
332
+ allProvidedOptions . set ( key , values ) ;
342
333
}
343
334
344
- if ( allOptions . has ( 'tlsCertificateKeyFile' ) && ! allOptions . has ( 'tlsCertificateFile' ) ) {
345
- allOptions . set ( 'tlsCertificateFile' , allOptions . get ( 'tlsCertificateKeyFile' ) ) ;
335
+ if (
336
+ allProvidedOptions . has ( 'tlsCertificateKeyFile' ) &&
337
+ ! allProvidedOptions . has ( 'tlsCertificateFile' )
338
+ ) {
339
+ allProvidedOptions . set ( 'tlsCertificateFile' , allProvidedOptions . get ( 'tlsCertificateKeyFile' ) ) ;
346
340
}
347
341
348
- if ( allOptions . has ( 'tls' ) || allOptions . has ( 'ssl' ) ) {
349
- const tlsAndSslOpts = ( allOptions . get ( 'tls' ) || [ ] )
350
- . concat ( allOptions . get ( 'ssl' ) || [ ] )
342
+ if ( allProvidedOptions . has ( 'tls' ) || allProvidedOptions . has ( 'ssl' ) ) {
343
+ const tlsAndSslOpts = ( allProvidedOptions . get ( 'tls' ) || [ ] )
344
+ . concat ( allProvidedOptions . get ( 'ssl' ) || [ ] )
351
345
. map ( getBoolean . bind ( null , 'tls/ssl' ) ) ;
352
346
if ( new Set ( tlsAndSslOpts ) . size !== 1 ) {
353
347
throw new MongoParseError ( 'All values of tls/ssl must be the same.' ) ;
354
348
}
355
349
}
356
350
357
- checkTLSOptions ( allOptions ) ;
351
+ checkTLSOptions ( allProvidedOptions ) ;
358
352
359
353
const unsupportedOptions = setDifference (
360
- allKeys ,
354
+ allProvidedKeys ,
361
355
Array . from ( Object . keys ( OPTIONS ) ) . map ( s => s . toLowerCase ( ) )
362
356
) ;
363
357
if ( unsupportedOptions . size !== 0 ) {
@@ -371,9 +365,20 @@ export function parseOptions(
371
365
// Option parsing and setting
372
366
373
367
for ( const [ key , descriptor ] of Object . entries ( OPTIONS ) ) {
374
- const values = allOptions . get ( key ) ;
375
- if ( ! values || values . length === 0 ) continue ;
376
- setOption ( mongoOptions , key , descriptor , values ) ;
368
+ const values = allProvidedOptions . get ( key ) ;
369
+ if ( ! values || values . length === 0 ) {
370
+ if ( DEFAULT_OPTIONS . has ( key ) ) {
371
+ setOption ( mongoOptions , key , descriptor , [ DEFAULT_OPTIONS . get ( key ) ] ) ;
372
+ }
373
+ } else {
374
+ const { deprecated } = descriptor ;
375
+ if ( deprecated ) {
376
+ const deprecatedMsg = typeof deprecated === 'string' ? `: ${ deprecated } ` : '' ;
377
+ emitWarning ( `${ key } is a deprecated option${ deprecatedMsg } ` ) ;
378
+ }
379
+
380
+ setOption ( mongoOptions , key , descriptor , values ) ;
381
+ }
377
382
}
378
383
379
384
if ( mongoOptions . credentials ) {
@@ -383,7 +388,7 @@ export function parseOptions(
383
388
const isOidc = mongoOptions . credentials . mechanism === AuthMechanism . MONGODB_OIDC ;
384
389
if (
385
390
( isGssapi || isX509 ) &&
386
- allOptions . has ( 'authSource' ) &&
391
+ allProvidedOptions . has ( 'authSource' ) &&
387
392
mongoOptions . credentials . source !== '$external'
388
393
) {
389
394
// If authSource was explicitly given and its incorrect, we error
@@ -395,7 +400,7 @@ export function parseOptions(
395
400
if (
396
401
! ( isGssapi || isX509 || isAws || isOidc ) &&
397
402
mongoOptions . dbName &&
398
- ! allOptions . has ( 'authSource' )
403
+ ! allProvidedOptions . has ( 'authSource' )
399
404
) {
400
405
// inherit the dbName unless GSSAPI or X509, then silently ignore dbName
401
406
// and there was no specific authSource given
@@ -571,14 +576,9 @@ function setOption(
571
576
descriptor : OptionDescriptor ,
572
577
values : unknown [ ]
573
578
) {
574
- const { target, type, transform, deprecated } = descriptor ;
579
+ const { target, type, transform } = descriptor ;
575
580
const name = target ?? key ;
576
581
577
- if ( deprecated ) {
578
- const deprecatedMsg = typeof deprecated === 'string' ? `: ${ deprecated } ` : '' ;
579
- emitWarning ( `${ key } is a deprecated option${ deprecatedMsg } ` ) ;
580
- }
581
-
582
582
switch ( type ) {
583
583
case 'boolean' :
584
584
mongoOptions [ name ] = getBoolean ( name , values [ 0 ] ) ;
0 commit comments