Skip to content

Commit af123cb

Browse files
authoredJan 22, 2024
Merge pull request #14277 from Automattic/vkarpov15/gh-14184
fix(collection): correctly handle buffer timeouts with `find()`
2 parents 4b6d208 + 6b67f9b commit af123cb

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed
 

‎lib/drivers/node-mongodb-native/collection.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,23 @@ function iter(i) {
138138
let _args = args;
139139
let promise = null;
140140
let timeout = null;
141-
if (syncCollectionMethods[i]) {
142-
this.addQueue(() => {
143-
lastArg.call(this, null, this[i].apply(this, _args.slice(0, _args.length - 1)));
144-
}, []);
141+
if (syncCollectionMethods[i] && typeof lastArg === 'function') {
142+
this.addQueue(i, _args);
143+
callback = lastArg;
144+
} else if (syncCollectionMethods[i]) {
145+
promise = new this.Promise((resolve, reject) => {
146+
callback = function collectionOperationCallback(err, res) {
147+
if (timeout != null) {
148+
clearTimeout(timeout);
149+
}
150+
if (err != null) {
151+
return reject(err);
152+
}
153+
resolve(res);
154+
};
155+
_args = args.concat([callback]);
156+
this.addQueue(i, _args);
157+
});
145158
} else if (typeof lastArg === 'function') {
146159
callback = function collectionOperationCallback() {
147160
if (timeout != null) {

‎test/collection.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ describe('collections:', function() {
6868
});
6969
});
7070

71+
it('returns a promise if buffering and callback with find() (gh-14184)', function(done) {
72+
db = mongoose.createConnection();
73+
const collection = db.collection('gh14184');
74+
collection.opts.bufferTimeoutMS = 100;
75+
76+
collection.find({ foo: 'bar' }, {}, (err, docs) => {
77+
assert.ok(err);
78+
assert.ok(err.message.includes('buffering timed out after 100ms'));
79+
assert.equal(docs, undefined);
80+
done();
81+
});
82+
});
83+
7184
it('methods should that throw (unimplemented)', function() {
7285
const collection = new Collection('test', mongoose.connection);
7386
let thrown = false;

0 commit comments

Comments
 (0)
Please sign in to comment.