@@ -386,35 +386,15 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
386
386
if ( existingSocket ) {
387
387
resolve ( socket ) ;
388
388
} else {
389
- const start = performance . now ( ) ;
390
389
const connectEvent = useTLS ? 'secureConnect' : 'connect' ;
391
390
socket
392
391
. 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' ) ) ) ;
410
395
411
396
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' ) ) ;
418
398
options . cancellationToken . once ( 'cancel' , cancellationHandler ) ;
419
399
}
420
400
}
@@ -467,11 +447,9 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
467
447
468
448
socks ??= loadSocks ( ) ;
469
449
470
- let existingSocket : Stream ;
471
-
472
450
try {
473
451
// Then, establish the Socks5 proxy connection:
474
- const connection = await socks . SocksClient . createConnection ( {
452
+ const { socket } = await socks . SocksClient . createConnection ( {
475
453
existing_socket : rawSocket ,
476
454
timeout : options . connectTimeoutMS ,
477
455
command : 'connect' ,
@@ -488,12 +466,35 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
488
466
password : options . proxyPassword || undefined
489
467
}
490
468
} ) ;
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 ) ;
494
479
}
480
+ }
495
481
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
+ }
499
500
}
0 commit comments