@@ -386,35 +386,15 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
386386 if ( existingSocket ) {
387387 resolve ( socket ) ;
388388 } else {
389- const start = performance . now ( ) ;
390389 const connectEvent = useTLS ? 'secureConnect' : 'connect' ;
391390 socket
392391 . once ( connectEvent , ( ) => resolve ( socket ) )
393- . once ( 'error' , cause =>
394- reject ( new MongoNetworkError ( MongoError . buildErrorMessage ( cause ) , { cause } ) )
395- )
396- . once ( 'timeout' , ( ) => {
397- reject (
398- new MongoNetworkTimeoutError (
399- `Socket '${ connectEvent } ' timed out after ${ ( performance . now ( ) - start ) | 0 } ms (connectTimeoutMS: ${ connectTimeoutMS } )`
400- )
401- ) ;
402- } )
403- . once ( 'close' , ( ) =>
404- reject (
405- new MongoNetworkError (
406- `Socket closed after ${ ( performance . now ( ) - start ) | 0 } during connection establishment`
407- )
408- )
409- ) ;
392+ . once ( 'error' , error => reject ( connectionFailureError ( 'error' , error ) ) )
393+ . once ( 'timeout' , ( ) => reject ( connectionFailureError ( 'timeout' ) ) )
394+ . once ( 'close' , ( ) => reject ( connectionFailureError ( 'close' ) ) ) ;
410395
411396 if ( options . cancellationToken != null ) {
412- cancellationHandler = ( ) =>
413- reject (
414- new MongoNetworkError (
415- `Socket connection establishment was cancelled after ${ ( performance . now ( ) - start ) | 0 } `
416- )
417- ) ;
397+ cancellationHandler = ( ) => reject ( connectionFailureError ( 'cancel' ) ) ;
418398 options . cancellationToken . once ( 'cancel' , cancellationHandler ) ;
419399 }
420400 }
@@ -467,11 +447,9 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
467447
468448 socks ??= loadSocks ( ) ;
469449
470- let existingSocket : Stream ;
471-
472450 try {
473451 // Then, establish the Socks5 proxy connection:
474- const connection = await socks . SocksClient . createConnection ( {
452+ const { socket } = await socks . SocksClient . createConnection ( {
475453 existing_socket : rawSocket ,
476454 timeout : options . connectTimeoutMS ,
477455 command : 'connect' ,
@@ -488,12 +466,35 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
488466 password : options . proxyPassword || undefined
489467 }
490468 } ) ;
491- existingSocket = connection . socket ;
492- } catch ( cause ) {
493- throw new MongoNetworkError ( MongoError . buildErrorMessage ( cause ) , { cause } ) ;
469+
470+ // Finally, now treat the resulting duplex stream as the
471+ // socket over which we send and receive wire protocol messages:
472+ return await makeSocket ( {
473+ ...options ,
474+ existingSocket : socket ,
475+ proxyHost : undefined
476+ } ) ;
477+ } catch ( error ) {
478+ throw connectionFailureError ( 'error' , error ) ;
494479 }
480+ }
495481
496- // Finally, now treat the resulting duplex stream as the
497- // socket over which we send and receive wire protocol messages:
498- return await makeSocket ( { ...options , existingSocket, proxyHost : undefined } ) ;
482+ function connectionFailureError ( type : 'error' , cause : Error ) : MongoNetworkError ;
483+ function connectionFailureError ( type : 'close' | 'timeout' | 'cancel' ) : MongoNetworkError ;
484+ function connectionFailureError (
485+ type : 'error' | 'close' | 'timeout' | 'cancel' ,
486+ cause ?: Error
487+ ) : MongoNetworkError {
488+ switch ( type ) {
489+ case 'error' :
490+ return new MongoNetworkError ( MongoError . buildErrorMessage ( cause ) , { cause } ) ;
491+ case 'timeout' :
492+ return new MongoNetworkTimeoutError ( 'connection timed out' ) ;
493+ case 'close' :
494+ return new MongoNetworkError ( 'connection closed' ) ;
495+ case 'cancel' :
496+ return new MongoNetworkError ( 'connection establishment was cancelled' ) ;
497+ default :
498+ return new MongoNetworkError ( 'unknown network error' ) ;
499+ }
499500}
0 commit comments