Skip to content

Commit

Permalink
Merge pull request #14277 from Automattic/vkarpov15/gh-14184
Browse files Browse the repository at this point in the history
fix(collection): correctly handle buffer timeouts with `find()`
  • Loading branch information
vkarpov15 committed Jan 22, 2024
2 parents 4b6d208 + 6b67f9b commit af123cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,23 @@ function iter(i) {
let _args = args;
let promise = null;
let timeout = null;
if (syncCollectionMethods[i]) {
this.addQueue(() => {
lastArg.call(this, null, this[i].apply(this, _args.slice(0, _args.length - 1)));
}, []);
if (syncCollectionMethods[i] && typeof lastArg === 'function') {
this.addQueue(i, _args);
callback = lastArg;
} else if (syncCollectionMethods[i]) {
promise = new this.Promise((resolve, reject) => {
callback = function collectionOperationCallback(err, res) {
if (timeout != null) {
clearTimeout(timeout);
}
if (err != null) {
return reject(err);
}
resolve(res);
};
_args = args.concat([callback]);
this.addQueue(i, _args);
});
} else if (typeof lastArg === 'function') {
callback = function collectionOperationCallback() {
if (timeout != null) {
Expand Down
13 changes: 13 additions & 0 deletions test/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ describe('collections:', function() {
});
});

it('returns a promise if buffering and callback with find() (gh-14184)', function(done) {
db = mongoose.createConnection();
const collection = db.collection('gh14184');
collection.opts.bufferTimeoutMS = 100;

collection.find({ foo: 'bar' }, {}, (err, docs) => {
assert.ok(err);
assert.ok(err.message.includes('buffering timed out after 100ms'));
assert.equal(docs, undefined);
done();
});
});

it('methods should that throw (unimplemented)', function() {
const collection = new Collection('test', mongoose.connection);
let thrown = false;
Expand Down

0 comments on commit af123cb

Please sign in to comment.