11import type {
22 Auth ,
33 AuthMechanism ,
4- ClientMetadata ,
54 ReadPreferenceFromOptions ,
65 ReadPreferenceLike ,
76 OperationOptions ,
@@ -242,18 +241,6 @@ export class NodeDriverServiceProvider
242241 let state : DevtoolsConnectionState | undefined ;
243242 let lastSeenTopology : TopologyDescription | undefined ;
244243
245- class MongoshMongoClient extends MongoClientCtor {
246- constructor ( url : string , options ?: MongoClientOptions ) {
247- super ( url , options ) ;
248- this . on (
249- 'topologyDescriptionChanged' ,
250- ( evt : TopologyDescriptionChangedEvent ) => {
251- lastSeenTopology = evt . newDescription ;
252- }
253- ) ;
254- }
255- }
256-
257244 if ( cliOptions . nodb ) {
258245 const clientOptionsCopy : MongoClientOptions &
259246 Partial < DevtoolsConnectOptions > = {
@@ -266,16 +253,15 @@ export class NodeDriverServiceProvider
266253 delete clientOptionsCopy . parentState ;
267254 delete clientOptionsCopy . proxy ;
268255 delete clientOptionsCopy . applyProxyToOIDC ;
269- client = new MongoshMongoClient (
256+ client = new MongoClientCtor (
270257 connectionString . toString ( ) ,
271258 clientOptionsCopy
272259 ) ;
273260 } else {
274- ( { client, state } = await connectMongoClient (
261+ ( { client, state, lastSeenTopology } = await this . connectMongoClient (
275262 connectionString . toString ( ) ,
276263 clientOptions ,
277- bus ,
278- MongoshMongoClient
264+ bus
279265 ) ) ;
280266 }
281267 clientOptions . parentState = state ;
@@ -299,13 +285,13 @@ export class NodeDriverServiceProvider
299285 private bus : MongoshBus ;
300286
301287 /**
302- * Stores the last seen topology at the time when .connect() finishes .
288+ * Stores the last known topology for the MongoClient instance .
303289 */
304290 private _lastSeenTopology : TopologyDescription | undefined ;
305291
306292 /**
307293 * Instantiate a new NodeDriverServiceProvider with the Node driver's connected
308- * MongoClient instance.
294+ * MongoClient instance. Do not call this directly, it is only public for testing.
309295 *
310296 * @param {MongoClient } mongoClient - The Node drivers' MongoClient instance.
311297 * @param {DevtoolsConnectOptions } clientOptions
@@ -326,13 +312,20 @@ export class NodeDriverServiceProvider
326312 this . _lastSeenTopology = lastSeenTopology ;
327313 this . platform = 'CLI' ;
328314 try {
329- this . initialDb = ( mongoClient as any ) . s . options . dbName || DEFAULT_DB ;
315+ this . initialDb = mongoClient . options . dbName || DEFAULT_DB ;
330316 } catch ( err : any ) {
331317 this . initialDb = DEFAULT_DB ;
332318 }
333319 this . currentClientOptions = clientOptions ;
334320 this . baseCmdOptions = { ...DEFAULT_BASE_OPTIONS } ; // currently do not have any user-specified connection-wide command options, but I imagine we will eventually
335321 this . dbcache = new WeakMap ( ) ;
322+
323+ this . mongoClient . on ?.(
324+ 'topologyDescriptionChanged' ,
325+ ( evt : TopologyDescriptionChangedEvent ) => {
326+ this . _lastSeenTopology = evt . newDescription ;
327+ }
328+ ) ;
336329 }
337330
338331 static getVersionInformation ( ) : DependencyVersionInfo {
@@ -355,7 +348,7 @@ export class NodeDriverServiceProvider
355348 } ;
356349 }
357350
358- maybeThrowBetterMissingOptionalDependencyError (
351+ static maybeThrowBetterMissingOptionalDependencyError (
359352 err : MongoMissingDependencyError
360353 ) : never {
361354 if ( err . message . includes ( 'kerberos' ) ) {
@@ -401,17 +394,37 @@ export class NodeDriverServiceProvider
401394 throw err ;
402395 }
403396
404- async connectMongoClient (
397+ private static async connectMongoClient (
405398 connectionString : ConnectionString | string ,
406- clientOptions : DevtoolsConnectOptions
407- ) : Promise < { client : MongoClient ; state : DevtoolsConnectionState } > {
399+ clientOptions : DevtoolsConnectOptions ,
400+ bus : MongoshBus
401+ ) : Promise < {
402+ client : MongoClient ;
403+ state : DevtoolsConnectionState ;
404+ lastSeenTopology : TopologyDescription | undefined ;
405+ } > {
406+ let lastSeenTopology : TopologyDescription | undefined ;
407+
408+ class MongoshMongoClient extends MongoClientCtor {
409+ constructor ( url : string , options ?: MongoClientOptions ) {
410+ super ( url , options ) ;
411+ this . on (
412+ 'topologyDescriptionChanged' ,
413+ ( evt : TopologyDescriptionChangedEvent ) => {
414+ lastSeenTopology = evt . newDescription ;
415+ }
416+ ) ;
417+ }
418+ }
419+
408420 try {
409- return await connectMongoClient (
421+ const result = await connectMongoClient (
410422 connectionString . toString ( ) ,
411423 clientOptions ,
412- this . bus ,
413- MongoClientCtor
424+ bus ,
425+ MongoshMongoClient
414426 ) ;
427+ return { ...result , lastSeenTopology } ;
415428 } catch ( err : unknown ) {
416429 if (
417430 typeof err === 'object' &&
@@ -434,17 +447,19 @@ export class NodeDriverServiceProvider
434447 const connectionString = new ConnectionString ( uri ) ;
435448 const clientOptions = this . processDriverOptions ( connectionString , options ) ;
436449
437- const { client, state } = await this . connectMongoClient (
438- connectionString . toString ( ) ,
439- clientOptions
440- ) ;
450+ const { client, state, lastSeenTopology } =
451+ await NodeDriverServiceProvider . connectMongoClient (
452+ connectionString . toString ( ) ,
453+ clientOptions ,
454+ this . bus
455+ ) ;
441456 clientOptions . parentState = state ;
442457 return new NodeDriverServiceProvider (
443458 client ,
444459 this . bus ,
445460 clientOptions ,
446461 connectionString ,
447- this . _lastSeenTopology
462+ lastSeenTopology ?? this . _lastSeenTopology
448463 ) ;
449464 }
450465
@@ -1152,8 +1167,8 @@ export class NodeDriverServiceProvider
11521167 /**
11531168 * Get currently known topology information.
11541169 */
1155- getTopology ( ) : any | undefined {
1156- return ( this . mongoClient as any ) . topology ;
1170+ getTopologyDescription ( ) : TopologyDescription | undefined {
1171+ return this . _lastSeenTopology ;
11571172 }
11581173
11591174 /**
@@ -1365,16 +1380,19 @@ export class NodeDriverServiceProvider
13651380 this . uri as ConnectionString ,
13661381 this . currentClientOptions
13671382 ) ;
1368- const { client, state } = await this . connectMongoClient (
1369- ( this . uri as ConnectionString ) . toString ( ) ,
1370- clientOptions
1371- ) ;
1383+ const { client, state, lastSeenTopology } =
1384+ await NodeDriverServiceProvider . connectMongoClient (
1385+ ( this . uri as ConnectionString ) . toString ( ) ,
1386+ clientOptions ,
1387+ this . bus
1388+ ) ;
13721389 try {
13731390 await this . mongoClient . close ( ) ;
13741391 // eslint-disable-next-line no-empty
13751392 } catch { }
13761393 this . mongoClient = client ;
13771394 this . currentClientOptions . parentState = state ;
1395+ if ( lastSeenTopology ) this . _lastSeenTopology = lastSeenTopology ;
13781396 }
13791397
13801398 startSession ( options : ClientSessionOptions ) : ClientSession {
@@ -1389,25 +1407,17 @@ export class NodeDriverServiceProvider
13891407 coll ?: string
13901408 ) : ChangeStream < Document > {
13911409 if ( db === undefined && coll === undefined ) {
1392- // TODO: watch not exported, see NODE-2934
1393- return ( this . mongoClient as any ) . watch ( pipeline , options ) ;
1410+ return this . mongoClient . watch ( pipeline , options ) ;
13941411 } else if ( db !== undefined && coll === undefined ) {
1395- return ( this . db ( db , dbOptions ) as any ) . watch ( pipeline , options ) ;
1412+ return this . db ( db , dbOptions ) . watch ( pipeline , options ) ;
13961413 } else if ( db !== undefined && coll !== undefined ) {
1397- return ( this . db ( db , dbOptions ) . collection ( coll ) as any ) . watch (
1398- pipeline ,
1399- options
1400- ) ;
1414+ return this . db ( db , dbOptions ) . collection ( coll ) . watch ( pipeline , options ) ;
14011415 }
14021416 throw new MongoshInternalError (
14031417 'Cannot call watch with defined collection but undefined db'
14041418 ) ;
14051419 }
14061420
1407- get driverMetadata ( ) : ClientMetadata | undefined {
1408- return this . getTopology ( ) ?. clientMetadata ;
1409- }
1410-
14111421 getRawClient ( ) : MongoClient {
14121422 return this . mongoClient ;
14131423 }
0 commit comments