Skip to content

Commit

Permalink
fix: handle buffering with find() now that find() no longer accep…
Browse files Browse the repository at this point in the history
…ts a callback

Fix #10610
  • Loading branch information
vkarpov15 committed Aug 28, 2021
1 parent 7175e02 commit 9de60a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
19 changes: 14 additions & 5 deletions lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ NativeCollection.prototype.onClose = function(force) {
* ignore
*/

const syncCollectionMethods = { watch: true };
const syncCollectionMethods = { watch: true, find: true };

/*!
* Copy the collection methods and make them subject to queues
Expand Down Expand Up @@ -96,7 +96,7 @@ function iter(i) {
let _args = args;
let callback = null;
if (this._shouldBufferCommands() && this.buffer) {
if (syncCollectionMethods[i]) {
if (syncCollectionMethods[i] && typeof lastArg !== 'function') {
throw new Error('Collection method ' + i + ' is synchronous');
}

Expand All @@ -109,10 +109,14 @@ function iter(i) {
});

let callback;
let _args;
let _args = args;
let promise = null;
let timeout = null;
if (typeof lastArg === 'function') {
if (syncCollectionMethods[i]) {
this.addQueue(() => {
lastArg.call(this, null, this[i].apply(this, _args.slice(0, _args.length - 1)));
}, []);
} else if (typeof lastArg === 'function') {
callback = function collectionOperationCallback() {
if (timeout != null) {
clearTimeout(timeout);
Expand Down Expand Up @@ -148,7 +152,7 @@ function iter(i) {
}
}, bufferTimeoutMS);

if (typeof lastArg === 'function') {
if (!syncCollectionMethods[i] && typeof lastArg === 'function') {
this.addQueue(i, _args);
return;
}
Expand Down Expand Up @@ -188,6 +192,11 @@ function iter(i) {
'you have `bufferCommands = false`.';
throw new MongooseError(message);
}

if (syncCollectionMethods[i] && typeof lastArg === 'function') {
return lastArg.call(this, null, collection[i].apply(collection, _args.slice(0, _args.length - 1)));
}

const ret = collection[i].apply(collection, _args);
if (ret != null && typeof ret.then === 'function') {
return ret.then(
Expand Down
34 changes: 10 additions & 24 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2143,30 +2143,16 @@ Query.prototype._find = wrapThunk(function(callback) {
options.projection = this._fieldsForExec();
const filter = this._conditions;

const cursor = this._collection.collection.find(filter, options);

if (typeof cursor.then === 'function' && cursor.constructor.name !== 'FindCursor') {
cursor.then(cursor => {
if (options.explain) {
return cursor.explain(cb);
}
try {
return cursor.toArray(cb);
} catch (err) {
return cb(err);
}
});
return;
}

if (options.explain) {
return cursor.explain(cb);
}
try {
return cursor.toArray(cb);
} catch (err) {
return cb(err);
}
this._collection.collection.find(filter, options, (err, cursor) => {
if (options.explain) {
return cursor.explain(cb);
}
try {
return cursor.toArray(cb);
} catch (err) {
return cb(err);
}
});
});

/**
Expand Down
14 changes: 14 additions & 0 deletions test/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,18 @@ describe('collections:', function() {
assert.ok(thrown);
thrown = false;
});

it('buffers for sync methods (gh-10610)', function(done) {
const db = mongoose.createConnection();
const collection = db.collection('gh10610');

collection.find({}, {}, function(err, res) {
assert.ifError(err);
assert.equal(typeof res.toArray, 'function');
done();
});

const uri = 'mongodb://localhost:27017/mongoose_test';
db.openUri(process.env.MONGOOSE_TEST_URI || uri);
});
});

0 comments on commit 9de60a7

Please sign in to comment.