@@ -54,7 +54,6 @@ import type { Transaction } from '../transactions';
54
54
import type { CloseOptions } from '../cmap/connection_pool' ;
55
55
import type { LoggerOptions } from '../logger' ;
56
56
import { DestroyOptions , Connection } from '../cmap/connection' ;
57
- import type { CommandOptions } from '../cmap/wire_protocol/command' ;
58
57
import { RunCommandOperation } from '../operations/run_command' ;
59
58
import type { CursorOptions } from '../cursor/cursor' ;
60
59
import type { MongoClientOptions } from '../mongo_client' ;
@@ -356,15 +355,28 @@ export class Topology extends EventEmitter {
356
355
357
356
ReadPreference . translate ( options ) ;
358
357
const readPreference = options . readPreference || ReadPreference . primary ;
359
- const connectHandler = ( err : Error | MongoError | undefined ) => {
358
+ this . selectServer ( readPreferenceServerSelector ( readPreference ) , options , ( err , server ) => {
360
359
if ( err ) {
361
360
this . close ( ) ;
362
361
363
- if ( typeof callback === 'function' ) {
364
- callback ( err ) ;
365
- } else {
366
- this . emit ( Topology . ERROR , err ) ;
367
- }
362
+ typeof callback === 'function' ? callback ( err ) : this . emit ( Topology . ERROR , err ) ;
363
+ return ;
364
+ }
365
+
366
+ // TODO: NODE-2471
367
+ if ( server && this . s . credentials ) {
368
+ server . command ( 'admin.$cmd' , { ping : 1 } , err => {
369
+ if ( err ) {
370
+ typeof callback === 'function' ? callback ( err ) : this . emit ( Topology . ERROR , err ) ;
371
+ return ;
372
+ }
373
+
374
+ stateTransition ( this , STATE_CONNECTED ) ;
375
+ this . emit ( Topology . OPEN , err , this ) ;
376
+ this . emit ( Topology . CONNECT , this ) ;
377
+
378
+ if ( typeof callback === 'function' ) callback ( undefined , this ) ;
379
+ } ) ;
368
380
369
381
return ;
370
382
}
@@ -373,16 +385,8 @@ export class Topology extends EventEmitter {
373
385
this . emit ( Topology . OPEN , err , this ) ;
374
386
this . emit ( Topology . CONNECT , this ) ;
375
387
376
- if ( typeof callback === 'function' ) callback ( err , this ) ;
377
- } ;
378
-
379
- // TODO: NODE-2471
380
- if ( this . s . credentials ) {
381
- this . command ( 'admin.$cmd' , { ping : 1 } , { readPreference } , connectHandler ) ;
382
- return ;
383
- }
384
-
385
- this . selectServer ( readPreferenceServerSelector ( readPreference ) , options , connectHandler ) ;
388
+ if ( typeof callback === 'function' ) callback ( undefined , this ) ;
389
+ } ) ;
386
390
}
387
391
388
392
/** Close this topology */
@@ -567,18 +571,27 @@ export class Topology extends EventEmitter {
567
571
}
568
572
569
573
/** Send endSessions command(s) with the given session ids */
570
- endSessions ( sessions : ServerSessionId [ ] , callback ?: Callback ) : void {
574
+ endSessions ( sessions : ServerSessionId [ ] , callback ?: Callback < Document > ) : void {
571
575
if ( ! Array . isArray ( sessions ) ) {
572
576
sessions = [ sessions ] ;
573
577
}
574
578
575
- this . command (
576
- 'admin.$cmd' ,
577
- { endSessions : sessions } ,
578
- { readPreference : ReadPreference . primaryPreferred , noResponse : true } ,
579
- ( ) => {
580
- // intentionally ignored, per spec
581
- if ( typeof callback === 'function' ) callback ( ) ;
579
+ this . selectServer (
580
+ readPreferenceServerSelector ( ReadPreference . primaryPreferred ) ,
581
+ ( err , server ) => {
582
+ if ( err || ! server ) {
583
+ if ( typeof callback === 'function' ) callback ( err ) ;
584
+ return ;
585
+ }
586
+
587
+ server . command (
588
+ 'admin.$cmd' ,
589
+ { endSessions : sessions } ,
590
+ { noResponse : true } ,
591
+ ( err , result ) => {
592
+ if ( typeof callback === 'function' ) callback ( err , result ) ;
593
+ }
594
+ ) ;
582
595
}
583
596
) ;
584
597
}
@@ -669,56 +682,6 @@ export class Topology extends EventEmitter {
669
682
if ( typeof callback === 'function' ) callback ( undefined , true ) ;
670
683
}
671
684
672
- /**
673
- * Execute a command
674
- *
675
- * @param ns - The MongoDB fully qualified namespace (ex: db1.collection1)
676
- * @param cmd - The command
677
- */
678
- command ( ns : string , cmd : Document , options : CommandOptions , callback : Callback ) : void {
679
- if ( typeof options === 'function' ) {
680
- ( callback = options ) , ( options = { } ) , ( options = options || { } ) ;
681
- }
682
-
683
- ReadPreference . translate ( options ) ;
684
- const readPreference = ( options . readPreference as ReadPreference ) || ReadPreference . primary ;
685
-
686
- this . selectServer ( readPreferenceServerSelector ( readPreference ) , options , ( err , server ) => {
687
- if ( err || ! server ) {
688
- callback ( err ) ;
689
- return ;
690
- }
691
-
692
- const willRetryWrite =
693
- ! options . retrying &&
694
- ! ! options . retryWrites &&
695
- options . session &&
696
- isRetryableWritesSupported ( this ) &&
697
- ! options . session . inTransaction ( ) &&
698
- isWriteCommand ( cmd ) ;
699
-
700
- // increment and assign txnNumber
701
- if ( willRetryWrite ) {
702
- options . session ?. incrementTransactionNumber ( ) ;
703
- options . willRetryWrite = willRetryWrite ;
704
- }
705
-
706
- server . command ( ns , cmd , options , ( err , result ) => {
707
- if ( ! err ) return callback ( undefined , result ) ;
708
- if ( ! shouldRetryOperation ( err ) ) {
709
- return callback ( err ) ;
710
- }
711
-
712
- if ( willRetryWrite ) {
713
- const newOptions = Object . assign ( { } , options , { retrying : true } ) ;
714
- return this . command ( ns , cmd , newOptions , callback ) ;
715
- }
716
-
717
- return callback ( err ) ;
718
- } ) ;
719
- } ) ;
720
- }
721
-
722
685
/**
723
686
* Create a new cursor
724
687
*
@@ -788,11 +751,6 @@ export class Topology extends EventEmitter {
788
751
) ;
789
752
}
790
753
791
- const RETRYABLE_WRITE_OPERATIONS = [ 'findAndModify' , 'insert' , 'update' , 'delete' ] ;
792
- function isWriteCommand ( command : Document ) {
793
- return RETRYABLE_WRITE_OPERATIONS . some ( ( op : string ) => command [ op ] ) ;
794
- }
795
-
796
754
/** Destroys a server, and removes all event listeners from the instance */
797
755
function destroyServer (
798
756
server : Server ,
@@ -941,10 +899,6 @@ function updateServers(topology: Topology, incomingServerDescription?: ServerDes
941
899
}
942
900
}
943
901
944
- function shouldRetryOperation ( err : AnyError ) {
945
- return err instanceof MongoError && err . hasErrorLabel ( 'RetryableWriteError' ) ;
946
- }
947
-
948
902
function srvPollingHandler ( topology : Topology ) {
949
903
return function handleSrvPolling ( ev : SrvPollingEvent ) {
950
904
const previousTopologyDescription = topology . s . description ;
@@ -1058,26 +1012,6 @@ function makeCompressionInfo(options: TopologyOptions) {
1058
1012
return options . compression . compressors ;
1059
1013
}
1060
1014
1061
- const RETRYABLE_WIRE_VERSION = 6 ;
1062
-
1063
- /** Determines whether the provided topology supports retryable writes */
1064
- function isRetryableWritesSupported ( topology : Topology ) {
1065
- const maxWireVersion = topology . lastIsMaster ( ) . maxWireVersion ;
1066
- if ( maxWireVersion < RETRYABLE_WIRE_VERSION ) {
1067
- return false ;
1068
- }
1069
-
1070
- if ( ! topology . logicalSessionTimeoutMinutes ) {
1071
- return false ;
1072
- }
1073
-
1074
- if ( topology . description . type === TopologyType . Single ) {
1075
- return false ;
1076
- }
1077
-
1078
- return true ;
1079
- }
1080
-
1081
1015
/** @public */
1082
1016
export class ServerCapabilities {
1083
1017
maxWireVersion : number ;
0 commit comments