Skip to content

Commit 811fc30

Browse files
committed
chore: await close when transform errors
1 parent feba106 commit 811fc30

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ export abstract class AbstractCursor<
341341

342342
stream(options?: CursorStreamOptions): Readable & AsyncIterable<TSchema> {
343343
if (options?.transform) {
344-
const transform = this.makeSafeTransform(options.transform);
344+
const transform = options.transform;
345345
const readable = new ReadableCursorStream(this);
346346

347347
const transformedStream = readable.pipe(
@@ -390,12 +390,11 @@ export abstract class AbstractCursor<
390390
throw new MongoCursorExhaustedError();
391391
}
392392

393-
const transform = this[kTransform];
394-
395393
do {
396394
const doc = this[kDocuments].shift();
397395
if (doc != null) {
398-
return transform != null ? transform(doc) : doc;
396+
if (this[kTransform] != null) return await this.transformDocument(doc);
397+
return doc;
399398
}
400399
await this.fetchBatch();
401400
} while (!this.isDead || this[kDocuments].length !== 0);
@@ -411,18 +410,18 @@ export abstract class AbstractCursor<
411410
throw new MongoCursorExhaustedError();
412411
}
413412

414-
const transform = this[kTransform];
415-
416413
let doc = this[kDocuments].shift();
417414
if (doc != null) {
418-
return transform != null ? transform(doc) : doc;
415+
if (this[kTransform] != null) return await this.transformDocument(doc);
416+
return doc;
419417
}
420418

421419
await this.fetchBatch();
422420

423421
doc = this[kDocuments].shift();
424422
if (doc != null) {
425-
return transform != null ? transform(doc) : doc;
423+
if (this[kTransform] != null) return await this.transformDocument(doc);
424+
return doc;
426425
}
427426

428427
return null;
@@ -533,10 +532,10 @@ export abstract class AbstractCursor<
533532
const oldTransform = this[kTransform] as (doc: TSchema) => TSchema; // TODO(NODE-3283): Improve transform typing
534533
if (oldTransform) {
535534
this[kTransform] = doc => {
536-
return this.makeSafeTransform(transform)(oldTransform(doc));
535+
return transform(oldTransform(doc));
537536
};
538537
} else {
539-
this[kTransform] = this.makeSafeTransform(transform);
538+
this[kTransform] = transform;
540539
}
541540

542541
return this as unknown as AbstractCursor<T>;
@@ -812,24 +811,27 @@ export abstract class AbstractCursor<
812811
}
813812

814813
/** @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;
825817

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);
831825
}
832-
};
826+
return transformedDocument;
827+
} catch (transformError) {
828+
try {
829+
await this.close();
830+
} catch (closeError) {
831+
squashError(closeError);
832+
}
833+
throw transformError;
834+
}
833835
}
834836

835837
/** @internal */

0 commit comments

Comments
 (0)