@@ -61,33 +61,32 @@ export function executeOperation<
61
61
throw new TypeError ( 'This method requires a valid operation instance' ) ;
62
62
}
63
63
64
- if ( topology . shouldCheckForSessionSupport ( ) ) {
65
- return maybePromise ( callback , cb => {
66
- topology . selectServer ( ReadPreference . primaryPreferred , err => {
67
- if ( err ) {
68
- cb ( err ) ;
69
- return ;
70
- }
64
+ return maybePromise ( callback , cb => {
65
+ if ( topology . shouldCheckForSessionSupport ( ) ) {
66
+ return topology . selectServer ( ReadPreference . primaryPreferred , err => {
67
+ if ( err ) return cb ( err ) ;
71
68
72
69
executeOperation < T , TResult > ( topology , operation , cb ) ;
73
70
} ) ;
74
- } ) ;
75
- }
71
+ }
76
72
77
- // The driver sessions spec mandates that we implicitly create sessions for operations
78
- // that are not explicitly provided with a session.
79
- let session = operation . session ;
80
- let owner : symbol ;
81
- if ( topology . hasSessionSupport ( ) ) {
82
- if ( session == null ) {
83
- owner = Symbol ( ) ;
84
- session = topology . startSession ( { owner, explicit : false } ) ;
85
- } else if ( operation . session . hasEnded ) {
86
- throw new MongoError ( 'Use of expired sessions is not permitted' ) ;
73
+ // The driver sessions spec mandates that we implicitly create sessions for operations
74
+ // that are not explicitly provided with a session.
75
+ let session : ClientSession | undefined = operation . session ;
76
+ let owner : symbol | undefined ;
77
+ if ( topology . hasSessionSupport ( ) ) {
78
+ if ( session == null ) {
79
+ owner = Symbol ( ) ;
80
+ session = topology . startSession ( { owner, explicit : false } ) ;
81
+ } else if ( session . hasEnded ) {
82
+ return cb ( new MongoError ( 'Use of expired sessions is not permitted' ) ) ;
83
+ }
84
+ } else if ( session ) {
85
+ // If the user passed an explicit session and we are still, after server selection,
86
+ // trying to run against a topology that doesn't support sessions we error out.
87
+ return cb ( new MongoError ( 'Current topology does not support sessions' ) ) ;
87
88
}
88
- }
89
89
90
- return maybePromise ( callback , cb => {
91
90
try {
92
91
executeWithServerSelection ( topology , session , operation , ( err , result ) => {
93
92
if ( session && session . owner && session . owner === owner ) {
@@ -113,7 +112,7 @@ function supportsRetryableReads(server: Server) {
113
112
function executeWithServerSelection (
114
113
topology : Topology ,
115
114
session : ClientSession ,
116
- operation : any ,
115
+ operation : AbstractOperation ,
117
116
callback : Callback
118
117
) {
119
118
const readPreference = operation . readPreference || ReadPreference . primary ;
0 commit comments