@@ -63,7 +63,7 @@ export type CursorFlag = (typeof CURSOR_FLAGS)[number];
63
63
/** @internal */
64
64
export const CursorTimeoutMode = Object . freeze ( {
65
65
ITERATION : 'iteration' ,
66
- LIFETIME : 'lifetime '
66
+ LIFETIME : 'cursorLifetime '
67
67
} as const ) ;
68
68
69
69
/** @internal
@@ -206,20 +206,20 @@ export abstract class AbstractCursor<
206
206
this . cursorOptions . timeoutMS = options . timeoutMS ;
207
207
if ( this . cursorOptions . timeoutMS != null ) {
208
208
if ( options . timeoutMode == null ) {
209
- if ( this . cursorOptions . tailable ) {
209
+ if ( options . tailable ) {
210
210
this . cursorOptions . timeoutMode = CursorTimeoutMode . ITERATION ;
211
211
} else {
212
212
this . cursorOptions . timeoutMode = CursorTimeoutMode . LIFETIME ;
213
213
}
214
214
} else {
215
- if (
216
- this . cursorOptions . tailable &&
217
- this . cursorOptions . timeoutMode === CursorTimeoutMode . LIFETIME
218
- ) {
215
+ if ( options . tailable && this . cursorOptions . timeoutMode === CursorTimeoutMode . LIFETIME ) {
219
216
throw new MongoAPIError ( "Cannot set tailable cursor's timeoutMode to LIFETIME" ) ;
220
217
}
221
218
this . cursorOptions . timeoutMode = options . timeoutMode ;
222
219
}
220
+ } else {
221
+ if ( options . timeoutMode != null )
222
+ throw new MongoAPIError ( 'Cannot set timeoutMode without setting timeoutMS' ) ;
223
223
}
224
224
225
225
const readConcern = ReadConcern . fromOptions ( options ) ;
@@ -444,19 +444,22 @@ export abstract class AbstractCursor<
444
444
throw new MongoCursorExhaustedError ( ) ;
445
445
}
446
446
447
- do {
448
- const doc = this . documents ?. shift ( this . cursorOptions ) ;
449
- if ( doc != null ) {
450
- if ( this . transform != null ) return await this . transformDocument ( doc ) ;
451
- return doc ;
452
- }
453
- await this . fetchBatch ( ) ;
454
- } while ( ! this . isDead || ( this . documents ?. length ?? 0 ) !== 0 ) ;
447
+ try {
448
+ do {
449
+ const doc = this . documents ?. shift ( this . cursorOptions ) ;
450
+ if ( doc != null ) {
451
+ if ( this . transform != null ) return await this . transformDocument ( doc ) ;
452
+ return doc ;
453
+ }
454
+ await this . fetchBatch ( ) ;
455
+ } while ( ! this . isDead || ( this . documents ?. length ?? 0 ) !== 0 ) ;
455
456
456
- if ( this . cursorOptions . timeoutMode === 'iteration' ) {
457
- this . timeoutContext ?. refresh ( ) ;
457
+ return null ;
458
+ } finally {
459
+ if ( this . cursorOptions . timeoutMode === CursorTimeoutMode . ITERATION ) {
460
+ this . timeoutContext ?. refresh ( ) ;
461
+ }
458
462
}
459
- return null ;
460
463
}
461
464
462
465
/**
@@ -689,6 +692,8 @@ export abstract class AbstractCursor<
689
692
690
693
this . cursorId = null ;
691
694
this . documents ?. clear ( ) ;
695
+ this . timeoutContext ?. clear ( ) ;
696
+ this . timeoutContext = undefined ;
692
697
this . isClosed = false ;
693
698
this . isKilled = false ;
694
699
this . initialized = false ;
@@ -761,7 +766,8 @@ export abstract class AbstractCursor<
761
766
}
762
767
const omitMaxTimeMS =
763
768
this . cursorOptions . timeoutMS != null &&
764
- ( ( this . cursorOptions . timeoutMode === 'iteration' && ! this . cursorOptions . tailable ) ||
769
+ ( ( this . cursorOptions . timeoutMode === CursorTimeoutMode . ITERATION &&
770
+ ! this . cursorOptions . tailable ) ||
765
771
( this . cursorOptions . tailable && ! this . cursorOptions . awaitData ) ) ;
766
772
try {
767
773
const state = await this . _initialize ( this . cursorSession , {
0 commit comments