@@ -341,7 +341,7 @@ export abstract class AbstractCursor<
341
341
342
342
stream ( options ?: CursorStreamOptions ) : Readable & AsyncIterable < TSchema > {
343
343
if ( options ?. transform ) {
344
- const transform = this . makeSafeTransform ( options . transform ) ;
344
+ const transform = options . transform ;
345
345
const readable = new ReadableCursorStream ( this ) ;
346
346
347
347
const transformedStream = readable . pipe (
@@ -390,12 +390,11 @@ export abstract class AbstractCursor<
390
390
throw new MongoCursorExhaustedError ( ) ;
391
391
}
392
392
393
- const transform = this [ kTransform ] ;
394
-
395
393
do {
396
394
const doc = this [ kDocuments ] . shift ( ) ;
397
395
if ( doc != null ) {
398
- return transform != null ? transform ( doc ) : doc ;
396
+ if ( this [ kTransform ] != null ) return await this . transformDocument ( doc ) ;
397
+ return doc ;
399
398
}
400
399
await this . fetchBatch ( ) ;
401
400
} while ( ! this . isDead || this [ kDocuments ] . length !== 0 ) ;
@@ -411,18 +410,18 @@ export abstract class AbstractCursor<
411
410
throw new MongoCursorExhaustedError ( ) ;
412
411
}
413
412
414
- const transform = this [ kTransform ] ;
415
-
416
413
let doc = this [ kDocuments ] . shift ( ) ;
417
414
if ( doc != null ) {
418
- return transform != null ? transform ( doc ) : doc ;
415
+ if ( this [ kTransform ] != null ) return await this . transformDocument ( doc ) ;
416
+ return doc ;
419
417
}
420
418
421
419
await this . fetchBatch ( ) ;
422
420
423
421
doc = this [ kDocuments ] . shift ( ) ;
424
422
if ( doc != null ) {
425
- return transform != null ? transform ( doc ) : doc ;
423
+ if ( this [ kTransform ] != null ) return await this . transformDocument ( doc ) ;
424
+ return doc ;
426
425
}
427
426
428
427
return null ;
@@ -533,10 +532,10 @@ export abstract class AbstractCursor<
533
532
const oldTransform = this [ kTransform ] as ( doc : TSchema ) => TSchema ; // TODO(NODE-3283): Improve transform typing
534
533
if ( oldTransform ) {
535
534
this [ kTransform ] = doc => {
536
- return this . makeSafeTransform ( transform ) ( oldTransform ( doc ) ) ;
535
+ return transform ( oldTransform ( doc ) ) ;
537
536
} ;
538
537
} else {
539
- this [ kTransform ] = this . makeSafeTransform ( transform ) ;
538
+ this [ kTransform ] = transform ;
540
539
}
541
540
542
541
return this as unknown as AbstractCursor < T > ;
@@ -812,24 +811,27 @@ export abstract class AbstractCursor<
812
811
}
813
812
814
813
/** @internal */
815
- private makeSafeTransform < TSchema > ( transform : ( doc : TSchema ) => any ) : ( doc : TSchema ) => any {
816
- return doc => {
817
- try {
818
- const result = transform ( doc ) ;
819
- // eslint-disable-next-line no-restricted-syntax
820
- if ( result === null ) {
821
- const message =
822
- 'Cursor returned a `null` document, but the cursor is not exhausted. Mapping documents to `null` is not supported in the cursor transform.' ;
823
- throw new MongoAPIError ( message ) ;
824
- }
814
+ private async transformDocument ( document : NonNullable < TSchema > ) : Promise < TSchema > {
815
+ const transform = this [ kTransform ] ;
816
+ if ( transform == null ) return document ;
825
817
826
- return result ;
827
- } catch ( error ) {
828
- // eslint-disable-next-line github/no-then
829
- this . close ( ) . then ( undefined , squashError ) ;
830
- throw error ;
818
+ try {
819
+ const transformedDocument = transform ( document ) ;
820
+ // eslint-disable-next-line no-restricted-syntax
821
+ if ( transformedDocument === null ) {
822
+ const TRANSFORM_TO_NULL_ERROR =
823
+ 'Cursor returned a `null` document, but the cursor is not exhausted. Mapping documents to `null` is not supported in the cursor transform.' ;
824
+ throw new MongoAPIError ( TRANSFORM_TO_NULL_ERROR ) ;
831
825
}
832
- } ;
826
+ return transformedDocument ;
827
+ } catch ( transformError ) {
828
+ try {
829
+ await this . close ( ) ;
830
+ } catch ( closeError ) {
831
+ squashError ( closeError ) ;
832
+ }
833
+ throw transformError ;
834
+ }
833
835
}
834
836
835
837
/** @internal */
0 commit comments