Skip to content

Commit 4ea5d69

Browse files
fix lint and refactor
1 parent 34424ed commit 4ea5d69

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Readable, Transform } from 'stream';
2-
import { callbackify, promisify } from 'util';
2+
import { promisify } from 'util';
33

44
import { type BSONSerializeOptions, type Document, Long, pluckBSONSerializeOptions } from '../bson';
55
import {
@@ -708,7 +708,10 @@ async function next<T>(
708708
try {
709709
return cursor[kTransform](doc);
710710
} catch (error) {
711-
await cleanupCursorAsync(cursor, { error, needsToEmitClosed: true });
711+
await cleanupCursorAsync(cursor, { error, needsToEmitClosed: true }).catch(() => {
712+
// `cleanupCursorAsync` should never throw, but if it does we want to throw the original
713+
// error instead.
714+
});
712715
throw error;
713716
}
714717
}
@@ -725,7 +728,9 @@ async function next<T>(
725728

726729
if (cursorIsDead(cursor)) {
727730
// if the cursor is dead, we clean it up
728-
await cleanupCursorAsync(cursor);
731+
// cleanupCursorAsync should never throw, but if it does it indicates a bug in the driver
732+
// and we should surface the error
733+
await cleanupCursorAsync(cursor, {});
729734
return null;
730735
}
731736

@@ -740,7 +745,10 @@ async function next<T>(
740745
response = await getMore(batchSize);
741746
} catch (error) {
742747
if (error) {
743-
await cleanupCursorAsync(cursor, { error });
748+
await cleanupCursorAsync(cursor, { error }).catch(() => {
749+
// `cleanupCursorAsync` should never throw, but if it does we want to throw the original
750+
// error instead.
751+
});
744752
throw error;
745753
}
746754
}
@@ -762,7 +770,10 @@ async function next<T>(
762770
// we intentionally clean up the cursor to release its session back into the pool before the cursor
763771
// is iterated. This prevents a cursor that is exhausted on the server from holding
764772
// onto a session indefinitely until the AbstractCursor is iterated.
765-
await cleanupCursorAsync(cursor);
773+
//
774+
// cleanupCursorAsync should never throw, but if it does it indicates a bug in the driver
775+
// and we should surface the error
776+
await cleanupCursorAsync(cursor, {});
766777
}
767778

768779
if (cursor[kDocuments].length === 0 && blocking === false) {
@@ -777,20 +788,7 @@ function cursorIsDead(cursor: AbstractCursor): boolean {
777788
return !!cursorId && cursorId.isZero();
778789
}
779790

780-
const cleanupCursorAsyncInternal = promisify(cleanupCursor);
781-
782-
async function cleanupCursorAsync<T>(
783-
cursor: AbstractCursor<T>,
784-
options: { needsToEmitClosed?: boolean; error?: AnyError } = {}
785-
): Promise<void> {
786-
try {
787-
await cleanupCursorAsyncInternal(cursor, options);
788-
} catch {
789-
// `cleanupCursor` never throws but we can't really test that.
790-
// so this is a hack to ensure that any upstream consumers
791-
// can safely guarantee on this wrapper never throwing.
792-
}
793-
}
791+
const cleanupCursorAsync = promisify(cleanupCursor);
794792

795793
function cleanupCursor(
796794
cursor: AbstractCursor,
@@ -802,6 +800,8 @@ function cleanupCursor(
802800
const server = cursor[kServer];
803801
const session = cursor[kSession];
804802
const error = options?.error;
803+
804+
// Cursors only emit closed events
805805
const needsToEmitClosed = options?.needsToEmitClosed ?? cursor[kDocuments].length === 0;
806806

807807
if (error) {

test/integration/node-specific/cursor_stream.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const { expect } = require('chai');
33
const { Binary } = require('../../mongodb');
4-
const { setTimeout, setImmediate } = require('timers');
4+
const { setTimeout } = require('timers');
55

66
describe('Cursor Streams', function () {
77
let client;

0 commit comments

Comments
 (0)