@@ -427,10 +427,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
427427 ...options
428428 } ;
429429
430- if ( ! options . omitMaxTimeMS ) {
431- const maxTimeMS = options . timeoutContext ?. maxTimeMS ;
432- if ( maxTimeMS && maxTimeMS > 0 && Number . isFinite ( maxTimeMS ) ) cmd . maxTimeMS = maxTimeMS ;
433- }
430+ options . timeoutContext ?. addMaxTimeMSToCommand ( cmd , options ) ;
434431
435432 const message = this . supportsOpMsg
436433 ? new OpMsgRequest ( db , cmd , commandOptions )
@@ -446,13 +443,11 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
446443 ) : AsyncGenerator < MongoDBResponse > {
447444 this . throwIfAborted ( ) ;
448445
449- if ( options . timeoutContext ?. csotEnabled ( ) ) {
450- this . socket . setTimeout ( 0 ) ;
451- } else if ( typeof options . socketTimeoutMS === 'number' ) {
452- this . socket . setTimeout ( options . socketTimeoutMS ) ;
453- } else if ( this . socketTimeoutMS !== 0 ) {
454- this . socket . setTimeout ( this . socketTimeoutMS ) ;
455- }
446+ const timeout =
447+ options . socketTimeoutMS ??
448+ options ?. timeoutContext ?. getSocketTimeoutMS ( ) ??
449+ this . socketTimeoutMS ;
450+ this . socket . setTimeout ( timeout ) ;
456451
457452 try {
458453 await this . writeCommand ( message , {
@@ -487,11 +482,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
487482 yield document ;
488483 this . throwIfAborted ( ) ;
489484
490- if ( typeof options . socketTimeoutMS === 'number' ) {
491- this . socket . setTimeout ( options . socketTimeoutMS ) ;
492- } else if ( this . socketTimeoutMS !== 0 ) {
493- this . socket . setTimeout ( this . socketTimeoutMS ) ;
494- }
485+ this . socket . setTimeout ( timeout ) ;
495486 }
496487 } finally {
497488 this . socket . setTimeout ( 0 ) ;
0 commit comments