1
1
import type {
2
2
Auth ,
3
3
AuthMechanism ,
4
- ClientMetadata ,
5
4
ReadPreferenceFromOptions ,
6
5
ReadPreferenceLike ,
7
6
OperationOptions ,
@@ -242,18 +241,6 @@ export class NodeDriverServiceProvider
242
241
let state : DevtoolsConnectionState | undefined ;
243
242
let lastSeenTopology : TopologyDescription | undefined ;
244
243
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
-
257
244
if ( cliOptions . nodb ) {
258
245
const clientOptionsCopy : MongoClientOptions &
259
246
Partial < DevtoolsConnectOptions > = {
@@ -266,16 +253,15 @@ export class NodeDriverServiceProvider
266
253
delete clientOptionsCopy . parentState ;
267
254
delete clientOptionsCopy . proxy ;
268
255
delete clientOptionsCopy . applyProxyToOIDC ;
269
- client = new MongoshMongoClient (
256
+ client = new MongoClientCtor (
270
257
connectionString . toString ( ) ,
271
258
clientOptionsCopy
272
259
) ;
273
260
} else {
274
- ( { client, state } = await connectMongoClient (
261
+ ( { client, state, lastSeenTopology } = await this . connectMongoClient (
275
262
connectionString . toString ( ) ,
276
263
clientOptions ,
277
- bus ,
278
- MongoshMongoClient
264
+ bus
279
265
) ) ;
280
266
}
281
267
clientOptions . parentState = state ;
@@ -299,13 +285,13 @@ export class NodeDriverServiceProvider
299
285
private bus : MongoshBus ;
300
286
301
287
/**
302
- * Stores the last seen topology at the time when .connect() finishes .
288
+ * Stores the last known topology for the MongoClient instance .
303
289
*/
304
290
private _lastSeenTopology : TopologyDescription | undefined ;
305
291
306
292
/**
307
293
* 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.
309
295
*
310
296
* @param {MongoClient } mongoClient - The Node drivers' MongoClient instance.
311
297
* @param {DevtoolsConnectOptions } clientOptions
@@ -326,13 +312,20 @@ export class NodeDriverServiceProvider
326
312
this . _lastSeenTopology = lastSeenTopology ;
327
313
this . platform = 'CLI' ;
328
314
try {
329
- this . initialDb = ( mongoClient as any ) . s . options . dbName || DEFAULT_DB ;
315
+ this . initialDb = mongoClient . options . dbName || DEFAULT_DB ;
330
316
} catch ( err : any ) {
331
317
this . initialDb = DEFAULT_DB ;
332
318
}
333
319
this . currentClientOptions = clientOptions ;
334
320
this . baseCmdOptions = { ...DEFAULT_BASE_OPTIONS } ; // currently do not have any user-specified connection-wide command options, but I imagine we will eventually
335
321
this . dbcache = new WeakMap ( ) ;
322
+
323
+ this . mongoClient . on ?.(
324
+ 'topologyDescriptionChanged' ,
325
+ ( evt : TopologyDescriptionChangedEvent ) => {
326
+ this . _lastSeenTopology = evt . newDescription ;
327
+ }
328
+ ) ;
336
329
}
337
330
338
331
static getVersionInformation ( ) : DependencyVersionInfo {
@@ -355,7 +348,7 @@ export class NodeDriverServiceProvider
355
348
} ;
356
349
}
357
350
358
- maybeThrowBetterMissingOptionalDependencyError (
351
+ static maybeThrowBetterMissingOptionalDependencyError (
359
352
err : MongoMissingDependencyError
360
353
) : never {
361
354
if ( err . message . includes ( 'kerberos' ) ) {
@@ -401,17 +394,37 @@ export class NodeDriverServiceProvider
401
394
throw err ;
402
395
}
403
396
404
- async connectMongoClient (
397
+ private static async connectMongoClient (
405
398
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
+
408
420
try {
409
- return await connectMongoClient (
421
+ const result = await connectMongoClient (
410
422
connectionString . toString ( ) ,
411
423
clientOptions ,
412
- this . bus ,
413
- MongoClientCtor
424
+ bus ,
425
+ MongoshMongoClient
414
426
) ;
427
+ return { ...result , lastSeenTopology } ;
415
428
} catch ( err : unknown ) {
416
429
if (
417
430
typeof err === 'object' &&
@@ -434,17 +447,19 @@ export class NodeDriverServiceProvider
434
447
const connectionString = new ConnectionString ( uri ) ;
435
448
const clientOptions = this . processDriverOptions ( connectionString , options ) ;
436
449
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
+ ) ;
441
456
clientOptions . parentState = state ;
442
457
return new NodeDriverServiceProvider (
443
458
client ,
444
459
this . bus ,
445
460
clientOptions ,
446
461
connectionString ,
447
- this . _lastSeenTopology
462
+ lastSeenTopology ?? this . _lastSeenTopology
448
463
) ;
449
464
}
450
465
@@ -1152,8 +1167,8 @@ export class NodeDriverServiceProvider
1152
1167
/**
1153
1168
* Get currently known topology information.
1154
1169
*/
1155
- getTopology ( ) : any | undefined {
1156
- return ( this . mongoClient as any ) . topology ;
1170
+ getTopologyDescription ( ) : TopologyDescription | undefined {
1171
+ return this . _lastSeenTopology ;
1157
1172
}
1158
1173
1159
1174
/**
@@ -1365,16 +1380,19 @@ export class NodeDriverServiceProvider
1365
1380
this . uri as ConnectionString ,
1366
1381
this . currentClientOptions
1367
1382
) ;
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
+ ) ;
1372
1389
try {
1373
1390
await this . mongoClient . close ( ) ;
1374
1391
// eslint-disable-next-line no-empty
1375
1392
} catch { }
1376
1393
this . mongoClient = client ;
1377
1394
this . currentClientOptions . parentState = state ;
1395
+ if ( lastSeenTopology ) this . _lastSeenTopology = lastSeenTopology ;
1378
1396
}
1379
1397
1380
1398
startSession ( options : ClientSessionOptions ) : ClientSession {
@@ -1389,25 +1407,17 @@ export class NodeDriverServiceProvider
1389
1407
coll ?: string
1390
1408
) : ChangeStream < Document > {
1391
1409
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 ) ;
1394
1411
} 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 ) ;
1396
1413
} 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 ) ;
1401
1415
}
1402
1416
throw new MongoshInternalError (
1403
1417
'Cannot call watch with defined collection but undefined db'
1404
1418
) ;
1405
1419
}
1406
1420
1407
- get driverMetadata ( ) : ClientMetadata | undefined {
1408
- return this . getTopology ( ) ?. clientMetadata ;
1409
- }
1410
-
1411
1421
getRawClient ( ) : MongoClient {
1412
1422
return this . mongoClient ;
1413
1423
}
0 commit comments