@@ -343,6 +343,7 @@ export interface InternalConnectionOptions {
343343 columnEncryptionSetting : boolean ;
344344 columnNameReplacer : undefined | ( ( colName : string , index : number , metadata : Metadata ) => string ) ;
345345 connectionRetryInterval : number ;
346+ connector : undefined | ( ( ) => Promise < Socket > ) ;
346347 connectTimeout : number ;
347348 connectionIsolationLevel : typeof ISOLATION_LEVEL [ keyof typeof ISOLATION_LEVEL ] ;
348349 cryptoCredentialsDetails : SecureContextOptions ;
@@ -535,6 +536,13 @@ export interface ConnectionOptions {
535536 */
536537 connectionRetryInterval ?: number ;
537538
539+ /**
540+ * Custom connector factory method.
541+ *
542+ * (default: `undefined`)
543+ */
544+ connector ?: ( ) => Promise < Socket > ;
545+
538546 /**
539547 * The number of milliseconds before the attempt to connect is considered failed
540548 *
@@ -1222,6 +1230,7 @@ class Connection extends EventEmitter {
12221230 columnNameReplacer : undefined ,
12231231 connectionRetryInterval : DEFAULT_CONNECT_RETRY_INTERVAL ,
12241232 connectTimeout : DEFAULT_CONNECT_TIMEOUT ,
1233+ connector : undefined ,
12251234 connectionIsolationLevel : ISOLATION_LEVEL . READ_COMMITTED ,
12261235 cryptoCredentialsDetails : { } ,
12271236 database : undefined ,
@@ -1330,6 +1339,14 @@ class Connection extends EventEmitter {
13301339 this . config . options . connectTimeout = config . options . connectTimeout ;
13311340 }
13321341
1342+ if ( config . options . connector !== undefined ) {
1343+ if ( typeof config . options . connector !== 'function' ) {
1344+ throw new TypeError ( 'The "config.options.connector" property must be a function.' ) ;
1345+ }
1346+
1347+ this . config . options . connector = config . options . connector ;
1348+ }
1349+
13331350 if ( config . options . cryptoCredentialsDetails !== undefined ) {
13341351 if ( typeof config . options . cryptoCredentialsDetails !== 'object' || config . options . cryptoCredentialsDetails === null ) {
13351352 throw new TypeError ( 'The "config.options.cryptoCredentialsDetails" property must be of type Object.' ) ;
@@ -1884,7 +1901,7 @@ class Connection extends EventEmitter {
18841901 const signal = this . createConnectTimer ( ) ;
18851902
18861903 if ( this . config . options . port ) {
1887- return this . connectOnPort ( this . config . options . port , this . config . options . multiSubnetFailover , signal ) ;
1904+ return this . connectOnPort ( this . config . options . port , this . config . options . multiSubnetFailover , signal , this . config . options . connector ) ;
18881905 } else {
18891906 return instanceLookup ( {
18901907 server : this . config . server ,
@@ -1893,7 +1910,7 @@ class Connection extends EventEmitter {
18931910 signal : signal
18941911 } ) . then ( ( port ) => {
18951912 process . nextTick ( ( ) => {
1896- this . connectOnPort ( port , this . config . options . multiSubnetFailover , signal ) ;
1913+ this . connectOnPort ( port , this . config . options . multiSubnetFailover , signal , this . config . options . connector ) ;
18971914 } ) ;
18981915 } , ( err ) => {
18991916 this . clearConnectTimer ( ) ;
@@ -1956,14 +1973,14 @@ class Connection extends EventEmitter {
19561973 return new TokenStreamParser ( message , this . debug , handler , this . config . options ) ;
19571974 }
19581975
1959- connectOnPort ( port : number , multiSubnetFailover : boolean , signal : AbortSignal ) {
1976+ connectOnPort ( port : number , multiSubnetFailover : boolean , signal : AbortSignal , customConnector ?: ( ) => Promise < Socket > ) {
19601977 const connectOpts = {
19611978 host : this . routingData ? this . routingData . server : this . config . server ,
19621979 port : this . routingData ? this . routingData . port : port ,
19631980 localAddress : this . config . options . localAddress
19641981 } ;
19651982
1966- const connect = multiSubnetFailover ? connectInParallel : connectInSequence ;
1983+ const connect = customConnector || ( multiSubnetFailover ? connectInParallel : connectInSequence ) ;
19671984
19681985 connect ( connectOpts , dns . lookup , signal ) . then ( ( socket ) => {
19691986 process . nextTick ( ( ) => {
0 commit comments