1
- import { once } from 'events' ;
2
- import { promises as fs } from 'fs' ;
3
1
import type { TcpNetConnectOpts } from 'net' ;
4
2
import type { ConnectionOptions as TLSConnectionOptions , TLSSocketOptions } from 'tls' ;
5
3
6
4
import { type BSONSerializeOptions , type Document , resolveBSONOptions } from './bson' ;
7
5
import { ChangeStream , type ChangeStreamDocument , type ChangeStreamOptions } from './change_stream' ;
8
6
import type { AutoEncrypter , AutoEncryptionOptions } from './client-side-encryption/auto_encrypter' ;
9
- import {
10
- type AuthMechanismProperties ,
11
- DEFAULT_ALLOWED_HOSTS ,
12
- type MongoCredentials
13
- } from './cmap/auth/mongo_credentials' ;
7
+ import { type AuthMechanismProperties , type MongoCredentials } from './cmap/auth/mongo_credentials' ;
14
8
import { type TokenCache } from './cmap/auth/mongodb_oidc/token_cache' ;
15
- import { AuthMechanism } from './cmap/auth/providers' ;
9
+ import { type AuthMechanism } from './cmap/auth/providers' ;
16
10
import type { LEGAL_TCP_SOCKET_OPTIONS , LEGAL_TLS_SOCKET_OPTIONS } from './cmap/connect' ;
17
11
import type { Connection } from './cmap/connection' ;
18
12
import type { ClientMetadata } from './cmap/handshake/client_metadata' ;
19
13
import type { CompressorName } from './cmap/wire_protocol/compression' ;
20
- import { parseOptions , resolveSRVRecord } from './connection_string' ;
21
- import { MONGO_CLIENT_EVENTS } from './constants' ;
14
+ import { parseOptions } from './connection_string' ;
15
+ import { type MONGO_CLIENT_EVENTS } from './constants' ;
22
16
import { Db , type DbOptions } from './db' ;
23
17
import type { Encrypter } from './encrypter' ;
24
- import { MongoInvalidArgumentError , MongoNetworkTimeoutError } from './error' ;
18
+ import { MongoInvalidArgumentError } from './error' ;
25
19
import { MongoClientAuthProviders } from './mongo_client_auth_providers' ;
26
20
import {
27
21
type LogComponentSeveritiesClientOptions ,
@@ -35,22 +29,18 @@ import { executeOperation } from './operations/execute_operation';
35
29
import { RunAdminCommandOperation } from './operations/run_command' ;
36
30
import type { ReadConcern , ReadConcernLevel , ReadConcernLike } from './read_concern' ;
37
31
import { ReadPreference , type ReadPreferenceMode } from './read_preference' ;
38
- import { STATE_CONNECTED , STATE_CONNECTING } from './sdam/common' ;
39
32
import type { ServerMonitoringMode } from './sdam/monitor' ;
40
- import { Server } from './sdam/server' ;
41
33
import type { TagSet } from './sdam/server_description' ;
42
34
import { readPreferenceServerSelector } from './sdam/server_selection' ;
43
35
import type { SrvPoller } from './sdam/srv_polling' ;
44
36
import { Topology , type TopologyEvents } from './sdam/topology' ;
45
37
import { ClientSession , type ClientSessionOptions , ServerSessionPool } from './sessions' ;
46
- import { Timeout } from './timeout' ;
47
38
import {
48
39
COSMOS_DB_CHECK ,
49
40
COSMOS_DB_MSG ,
50
41
DOCUMENT_DB_CHECK ,
51
42
DOCUMENT_DB_MSG ,
52
43
type HostAddress ,
53
- hostMatchesWildcards ,
54
44
isHostMatch ,
55
45
type MongoDBNamespace ,
56
46
ns ,
@@ -352,7 +342,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
352
342
/** @internal */
353
343
s : MongoClientPrivate ;
354
344
/** @internal */
355
- topology ? : Topology ;
345
+ topology : Topology ;
356
346
/** @internal */
357
347
override readonly mongoLogger : MongoLogger | undefined ;
358
348
/** @internal */
@@ -406,6 +396,8 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
406
396
}
407
397
} ;
408
398
this . checkForNonGenuineHosts ( ) ;
399
+
400
+ this . topology = new Topology ( this , this [ kOptions ] ) ;
409
401
}
410
402
411
403
/** @internal */
@@ -477,16 +469,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
477
469
}
478
470
479
471
/** @internal */
480
- async _connectWithLock ( options ?: {
481
- skipPing : boolean ;
482
- readPreference ?: ReadPreference ;
483
- } ) : Promise < this> {
472
+ async _connectWithLock ( ) : Promise < this> {
484
473
if ( this . connectionLock ) {
485
474
return await this . connectionLock ;
486
475
}
487
476
488
477
try {
489
- this . connectionLock = this . _connect ( options ) ;
478
+ this . connectionLock = this . _connect ( ) ;
490
479
await this . connectionLock ;
491
480
} finally {
492
481
// release
@@ -502,21 +491,17 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
502
491
*
503
492
* @internal
504
493
*/
505
- private async _connect ( options ?: {
506
- skipPing : boolean ;
507
- readPreference ?: ReadPreference ;
508
- } ) : Promise < this> {
509
- const topology = await this . initTopology ( ) ;
510
- const readPreference = options ?. readPreference ?? this . readPreference ;
494
+ private async _connect ( ) : Promise < this> {
511
495
const { encrypter } = this [ kOptions ] ;
512
496
const topologyConnect = async ( ) => {
513
497
try {
514
498
const skipPingOnConnect = this . s . options [ Symbol . for ( '@@mdb.skipPingOnConnect' ) ] === true ;
515
- if ( ! skipPingOnConnect && ! options ?. skipPing && topology . s . credentials != null ) {
516
- await this . db ( ) . admin ( ) . ping ( { readPreference } ) ; // goes through `executeOperation` so performs server selection
499
+ const hasCredentials = this . s . options . credentials != null ;
500
+ if ( ! skipPingOnConnect && hasCredentials ) {
501
+ await this . db ( ) . admin ( ) . ping ( { readPreference : this [ kOptions ] . readPreference } ) ;
517
502
}
518
503
} catch ( error ) {
519
- topology . close ( ) ;
504
+ this . topology . close ( ) ;
520
505
throw error ;
521
506
}
522
507
} ;
@@ -532,69 +517,6 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
532
517
return this ;
533
518
}
534
519
535
- /** @internal */
536
- async initTopology ( ) {
537
- if ( this . topology && this . topology . isConnected ( ) ) {
538
- return this . topology ;
539
- }
540
-
541
- const topologyOptions = this [ kOptions ] ;
542
-
543
- if ( topologyOptions . tls ) {
544
- if ( typeof topologyOptions . tlsCAFile === 'string' ) {
545
- topologyOptions . ca ??= await fs . readFile ( topologyOptions . tlsCAFile ) ;
546
- }
547
- if ( typeof topologyOptions . tlsCRLFile === 'string' ) {
548
- topologyOptions . crl ??= await fs . readFile ( topologyOptions . tlsCRLFile ) ;
549
- }
550
- if ( typeof topologyOptions . tlsCertificateKeyFile === 'string' ) {
551
- if ( ! topologyOptions . key || ! topologyOptions . cert ) {
552
- const contents = await fs . readFile ( topologyOptions . tlsCertificateKeyFile ) ;
553
- topologyOptions . key ??= contents ;
554
- topologyOptions . cert ??= contents ;
555
- }
556
- }
557
- }
558
- if ( typeof topologyOptions . srvHost === 'string' ) {
559
- const hosts = await resolveSRVRecord ( topologyOptions ) ;
560
-
561
- for ( const [ index , host ] of hosts . entries ( ) ) {
562
- topologyOptions . hosts [ index ] = host ;
563
- }
564
- }
565
-
566
- // It is important to perform validation of hosts AFTER SRV resolution, to check the real hostname,
567
- // but BEFORE we even attempt connecting with a potentially not allowed hostname
568
- if ( topologyOptions . credentials ?. mechanism === AuthMechanism . MONGODB_OIDC ) {
569
- const allowedHosts =
570
- topologyOptions . credentials ?. mechanismProperties ?. ALLOWED_HOSTS || DEFAULT_ALLOWED_HOSTS ;
571
- const isServiceAuth = ! ! topologyOptions . credentials ?. mechanismProperties ?. ENVIRONMENT ;
572
- if ( ! isServiceAuth ) {
573
- for ( const host of topologyOptions . hosts ) {
574
- if ( ! hostMatchesWildcards ( host . toHostPort ( ) . host , allowedHosts ) ) {
575
- throw new MongoInvalidArgumentError (
576
- `Host '${ host } ' is not valid for OIDC authentication with ALLOWED_HOSTS of '${ allowedHosts . join (
577
- ','
578
- ) } '`
579
- ) ;
580
- }
581
- }
582
- }
583
- }
584
-
585
- this . topology = new Topology ( this , topologyOptions . hosts , topologyOptions ) ;
586
- // Events can be emitted before initialization is complete so we have to
587
- // save the reference to the topology on the client ASAP if the event handlers need to access it
588
-
589
- this . topology . once ( Topology . OPEN , ( ) => this . emit ( 'open' , this ) ) ;
590
-
591
- for ( const event of MONGO_CLIENT_EVENTS ) {
592
- this . topology . on ( event , ( ...args : any [ ] ) => this . emit ( event , ...( args as any ) ) ) ;
593
- }
594
-
595
- return this . topology ;
596
- }
597
-
598
520
/**
599
521
* Close the client and its underlying connections
600
522
*
@@ -643,7 +565,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
643
565
644
566
// clear out references to old topology
645
567
const topology = this . topology ;
646
- this . topology = undefined ;
568
+ // this.topology = undefined;
647
569
648
570
topology . close ( ) ;
649
571
0 commit comments