@@ -29,6 +29,8 @@ import { Logger, LoggerLevel } from './logger';
2929import { PromiseProvider } from './promise_provider' ;
3030import { createAutoEncrypter } from './operations/connect' ;
3131
32+ const VALID_TXT_RECORDS = [ 'authSource' , 'replicaSet' , 'loadBalanced' ] ;
33+
3234/**
3335 * Determines whether a provided address matches the provided parent domain in order
3436 * to avoid certain attack vectors.
@@ -94,9 +96,9 @@ export function resolveSRVRecord(options: MongoOptions, callback: Callback<HostA
9496
9597 const txtRecordOptions = new URLSearchParams ( record [ 0 ] . join ( '' ) ) ;
9698 const txtRecordOptionKeys = [ ...txtRecordOptions . keys ( ) ] ;
97- if ( txtRecordOptionKeys . some ( key => key !== 'authSource' && key !== 'replicaSet' ) ) {
99+ if ( txtRecordOptionKeys . some ( key => ! VALID_TXT_RECORDS . includes ( key ) ) ) {
98100 return callback (
99- new MongoParseError ( ' Text record must only set `authSource` or `replicaSet`' )
101+ new MongoParseError ( ` Text record must only set one of: ${ VALID_TXT_RECORDS . join ( ', ' ) } ` )
100102 ) ;
101103 }
102104
@@ -116,6 +118,8 @@ export function resolveSRVRecord(options: MongoOptions, callback: Callback<HostA
116118 }
117119 }
118120
121+ validateLoadBalancedOptions ( hostAddresses , options ) ;
122+
119123 callback ( undefined , hostAddresses ) ;
120124 } ) ;
121125 } ) ;
@@ -426,6 +430,8 @@ export function parseOptions(
426430 throw new MongoParseError ( 'directConnection not supported with SRV URI' ) ;
427431 }
428432
433+ validateLoadBalancedOptions ( hosts , mongoOptions ) ;
434+
429435 // Potential SRV Overrides
430436 mongoOptions . userSpecifiedAuthSource =
431437 objectOptions . has ( 'authSource' ) || urlOptions . has ( 'authSource' ) ;
@@ -435,6 +441,20 @@ export function parseOptions(
435441 return mongoOptions ;
436442}
437443
444+ function validateLoadBalancedOptions ( hosts : HostAddress [ ] | string [ ] , mongoOptions : MongoOptions ) {
445+ if ( mongoOptions . loadBalanced ) {
446+ if ( hosts . length > 1 ) {
447+ throw new MongoParseError ( 'loadBalanced option only supported with a single host in the URI' ) ;
448+ }
449+ if ( mongoOptions . replicaSet ) {
450+ throw new MongoParseError ( 'loadBalanced option not supported with a replicaSet option' ) ;
451+ }
452+ if ( mongoOptions . directConnection ) {
453+ throw new MongoParseError ( 'loadBalanced option not supported when directConnection is true' ) ;
454+ }
455+ }
456+ }
457+
438458function setOption (
439459 mongoOptions : any ,
440460 key : string ,
@@ -696,6 +716,10 @@ export const OPTIONS = {
696716 default : 120000 ,
697717 type : 'uint'
698718 } ,
719+ loadBalanced : {
720+ default : false ,
721+ type : 'boolean'
722+ } ,
699723 localThresholdMS : {
700724 default : 15 ,
701725 type : 'uint'
0 commit comments