11import { clearTimeout , setTimeout } from 'timers' ;
22
33import { type Document , Long } from '../bson' ;
4- import { connect } from '../cmap/connect' ;
4+ import { connect , makeConnection , makeSocket , performInitialHandshake } from '../cmap/connect' ;
55import type { Connection , ConnectionOptions } from '../cmap/connection' ;
66import { getFAASEnv } from '../cmap/handshake/client_metadata' ;
77import { LEGACY_HELLO_COMMAND } from '../constants' ;
@@ -235,7 +235,7 @@ function useStreamingProtocol(monitor: Monitor, topologyVersion: TopologyVersion
235235}
236236
237237function checkServer ( monitor : Monitor , callback : Callback < Document | null > ) {
238- let start = now ( ) ;
238+ let start : number ;
239239 let awaited : boolean ;
240240 const topologyVersion = monitor [ kServer ] . description . topologyVersion ;
241241 const isAwaitable = useStreamingProtocol ( monitor , topologyVersion ) ;
@@ -335,6 +335,8 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
335335 ) ;
336336 }
337337
338+ start = now ( ) ;
339+
338340 if ( isAwaitable ) {
339341 awaited = true ;
340342 return connection . exhaustCommand ( ns ( 'admin.$cmd' ) , cmd , options , ( error , hello ) => {
@@ -352,7 +354,13 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
352354 }
353355
354356 // connecting does an implicit `hello`
355- connect ( monitor . connectOptions ) . then (
357+ ( async ( ) => {
358+ const socket = await makeSocket ( monitor . connectOptions ) ;
359+ const connection = makeConnection ( monitor . connectOptions , socket ) ;
360+ start = now ( ) ;
361+ await performInitialHandshake ( connection , monitor . connectOptions ) ;
362+ return connection ;
363+ } ) ( ) . then (
356364 connection => {
357365 if ( isInCloseState ( monitor ) ) {
358366 connection . destroy ( { force : true } ) ;
0 commit comments