Skip to content

Commit c23b57b

Browse files
authored
Merge pull request #5892 from Countly/ar2rsawseen/master
Fixes for gridfs
2 parents 4918a7e + c7a8349 commit c23b57b

File tree

3 files changed

+126
-43
lines changed

3 files changed

+126
-43
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Version xx.xx.xx
22

3+
Fixes:
4+
- [gridfs] fixes for moving to Promises
5+
36
Dependencies:
47
- Bump sass from 1.81.0 to 1.83.1
58
- Bump countly-sdk-nodejs from 24.10.0 to 24.10.1

api/utils/countlyFs.js

+71-42
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ countlyFs.gridfs = {};
6868
**/
6969
function beforeSave(category, filename, options, callback, done) {
7070
log.d("checking file", filename);
71-
ob.getId(category, filename, function(err, res) {
71+
ob.getId(category, filename, async function(err, res) {
7272
log.d("file state", filename, err, res);
7373
if (options.forceClean) {
7474
ob.clearFile(category, filename, done);
@@ -80,15 +80,20 @@ countlyFs.gridfs = {};
8080
else if (options.writeMode === "overwrite") {
8181
var bucket = new GridFSBucket(db, { bucketName: category });
8282
log.d("deleting file", filename);
83-
bucket.delete(res, function(error) {
84-
log.d("deleted", filename, error);
85-
if (!error) {
86-
setTimeout(done, 1);
87-
}
88-
else if (callback) {
89-
callback(error);
90-
}
91-
});
83+
let errHandle = null;
84+
try {
85+
await bucket.delete(res);
86+
}
87+
catch (error) {
88+
errHandle = error;
89+
}
90+
log.d("deleted", filename, errHandle);
91+
if (!errHandle) {
92+
setTimeout(done, 1);
93+
}
94+
else if (callback) {
95+
callback(errHandle);
96+
}
9297
}
9398
else {
9499
if (callback) {
@@ -116,6 +121,7 @@ countlyFs.gridfs = {};
116121
* });
117122
*/
118123
ob.getId = function(category, filename, callback) {
124+
log.d("getId", category, filename);
119125
db.collection(category + ".files").findOne({ filename: filename }, {_id: 1}, function(err, res) {
120126
if (callback) {
121127
callback(err, (res && res._id) ? res._id : false);
@@ -144,6 +150,7 @@ countlyFs.gridfs = {};
144150
if (!options) {
145151
options = {};
146152
}
153+
log.d("exists", category, dest, options);
147154
var query = {};
148155
if (options.id) {
149156
query._id = options.id;
@@ -184,7 +191,7 @@ countlyFs.gridfs = {};
184191
if (!options) {
185192
options = {};
186193
}
187-
194+
log.d("saveFile", category, dest, source, options);
188195
var filename = dest.split(path.sep).pop();
189196
beforeSave(category, filename, options, callback, function() {
190197
save(category, filename, fs.createReadStream(source), options, callback);
@@ -218,6 +225,7 @@ countlyFs.gridfs = {};
218225
if (!options) {
219226
options = {};
220227
}
228+
log.d("saveData", category, dest, typeof data, options);
221229
beforeSave(category, filename, options, callback, function() {
222230
var readStream = new Readable;
223231
readStream.push(data);
@@ -253,6 +261,7 @@ countlyFs.gridfs = {};
253261
if (!options) {
254262
options = {};
255263
}
264+
log.d("saveStream", category, dest, typeof readStream, options);
256265
beforeSave(category, filename, options, callback, function() {
257266
save(category, filename, readStream, options, callback);
258267
});
@@ -271,7 +280,7 @@ countlyFs.gridfs = {};
271280
* console.log("Finished", err);
272281
* });
273282
*/
274-
ob.rename = function(category, dest, source, options, callback) {
283+
ob.rename = async function(category, dest, source, options, callback) {
275284
var newname = dest.split(path.sep).pop();
276285
var oldname = source.split(path.sep).pop();
277286
if (typeof options === "function") {
@@ -281,25 +290,35 @@ countlyFs.gridfs = {};
281290
if (!options) {
282291
options = {};
283292
}
284-
293+
log.d("rename", category, dest, source, options);
285294
if (options.id) {
286295
let bucket = new GridFSBucket(db, { bucketName: category });
287-
bucket.rename(options.id, newname, function(error) {
288-
if (callback) {
289-
callback(error);
290-
}
291-
});
296+
let errHandle = null;
297+
try {
298+
await bucket.rename(options.id, newname);
299+
}
300+
catch (error) {
301+
errHandle = error;
302+
}
303+
if (callback) {
304+
callback(errHandle);
305+
}
292306
}
293307
else {
294-
db.collection(category + ".files").findOne({ filename: oldname }, {_id: 1}, function(err, res) {
308+
db.collection(category + ".files").findOne({ filename: oldname }, {_id: 1}, async function(err, res) {
295309
if (!err) {
296310
if (res && res._id) {
297311
let bucket = new GridFSBucket(db, { bucketName: category });
298-
bucket.rename(res._id, newname, function(error) {
299-
if (callback) {
300-
callback(error);
301-
}
302-
});
312+
let errHandle = null;
313+
try {
314+
await bucket.rename(res._id, newname);
315+
}
316+
catch (error) {
317+
errHandle = error;
318+
}
319+
if (callback) {
320+
callback(errHandle);
321+
}
303322
}
304323
else {
305324
if (callback) {
@@ -337,7 +356,7 @@ countlyFs.gridfs = {};
337356
if (!options) {
338357
options = {};
339358
}
340-
359+
log.d("deleteFile", category, dest, options);
341360
if (options.id) {
342361
ob.deleteFileById(category, options.id, callback);
343362
}
@@ -372,13 +391,19 @@ countlyFs.gridfs = {};
372391
* console.log("Finished", err);
373392
* });
374393
*/
375-
ob.deleteAll = function(category, dest, callback) {
394+
ob.deleteAll = async function(category, dest, callback) {
395+
log.d("deleteAll", category, dest);
376396
var bucket = new GridFSBucket(db, { bucketName: category });
377-
bucket.drop(function(error) {
378-
if (callback) {
379-
callback(error);
380-
}
381-
});
397+
let errHandle = null;
398+
try {
399+
await bucket.drop();
400+
}
401+
catch (error) {
402+
errHandle = error;
403+
}
404+
if (callback) {
405+
callback(errHandle);
406+
}
382407
};
383408

384409
/**
@@ -403,7 +428,7 @@ countlyFs.gridfs = {};
403428
if (!options) {
404429
options = {};
405430
}
406-
431+
log.d("getStream", category, dest, options);
407432
if (callback) {
408433
if (options.id) {
409434
ob.getStreamById(category, options.id, callback);
@@ -436,7 +461,7 @@ countlyFs.gridfs = {};
436461
if (!options) {
437462
options = {};
438463
}
439-
464+
log.d("getData", category, dest, options);
440465
if (options.id) {
441466
ob.getDataById(category, options.id, callback);
442467
}
@@ -482,7 +507,7 @@ countlyFs.gridfs = {};
482507
if (!options) {
483508
options = {};
484509
}
485-
510+
log.d("getSize", category, dest, options);
486511
var query = {};
487512
if (options.id) {
488513
query._id = options.id;
@@ -517,7 +542,7 @@ countlyFs.gridfs = {};
517542
if (!options) {
518543
options = {};
519544
}
520-
545+
log.d("getStats", category, dest, options);
521546
var query = {};
522547
if (options.id) {
523548
query._id = options.id;
@@ -554,6 +579,7 @@ countlyFs.gridfs = {};
554579
* });
555580
*/
556581
ob.getDataById = function(category, id, callback) {
582+
log.d("getDataById", category, id);
557583
var bucket = new GridFSBucket(db, { bucketName: category });
558584
var downloadStream = bucket.openDownloadStream(id);
559585
downloadStream.on('error', function(error) {
@@ -585,6 +611,7 @@ countlyFs.gridfs = {};
585611
* });
586612
*/
587613
ob.getStreamById = function(category, id, callback) {
614+
log.d("getStreamById", category, id);
588615
if (callback) {
589616
var bucket = new GridFSBucket(db, { bucketName: category });
590617
callback(null, bucket.openDownloadStream(id));
@@ -602,17 +629,17 @@ countlyFs.gridfs = {};
602629
* });
603630
*/
604631
ob.deleteFileById = async function(category, id, callback) {
632+
log.d("deleteFileById", category, id);
605633
var bucket = new GridFSBucket(db, { bucketName: category });
634+
let errHandle = null;
606635
try {
607636
await bucket.delete(id);
608-
if (callback) {
609-
callback(null);
610-
}
611637
}
612-
catch (ee) {
613-
if (callback) {
614-
callback(ee);
615-
}
638+
catch (error) {
639+
errHandle = error;
640+
}
641+
if (callback) {
642+
callback(errHandle);
616643
}
617644
};
618645

@@ -627,6 +654,7 @@ countlyFs.gridfs = {};
627654
* });
628655
*/
629656
ob.clearFile = function(category, filename, callback) {
657+
log.d("clearFile", category, filename);
630658
db.collection(category + ".files").deleteMany({ filename: filename }, function(err1, res1) {
631659
log.d("deleting files", category, { filename: filename }, err1, res1 && res1.result);
632660
db.collection(category + ".chunks").deleteMany({ files_id: filename }, function(err2, res2) {
@@ -643,6 +671,7 @@ countlyFs.gridfs = {};
643671
* @param {function} callback - function called when files found or query errored, providing error object as first param and a list of filename, creation date and size as secondas second
644672
*/
645673
ob.listFiles = function(category, callback) {
674+
log.d("listFiles", category);
646675
const bucket = new GridFSBucket(db, { bucketName: category });
647676
bucket.find().toArray()
648677
.then((records) => callback(

plugins/pluginManager.js

+52-1
Original file line numberDiff line numberDiff line change
@@ -2226,7 +2226,58 @@ var pluginManager = function pluginManager() {
22262226
}
22272227
});
22282228

2229-
var findOptions = ["limit", "sort", "projection", "skip", "hint", "explain", "snapshot", "timeout", "tailable", "batchSize", "returnKey", "maxScan", "min", "max", "showDiskLoc", "comment", "raw", "promoteLongs", "promoteValues", "promoteBuffers", "readPreference", "partial", "maxTimeMS", "collation", "session", "omitReadPreference"];
2229+
var findOptions = [
2230+
"allowDiskUse",
2231+
"allowPartialResults",
2232+
"authdb",
2233+
"awaitData",
2234+
"batchSize",
2235+
"bsonRegExp",
2236+
"checkKeys",
2237+
"collation",
2238+
"comment",
2239+
"dbName",
2240+
"enableUtf8Validation",
2241+
"explain",
2242+
"fieldsAsRaw",
2243+
"hint",
2244+
"ignoreUndefined",
2245+
"let",
2246+
"limit",
2247+
"max",
2248+
"maxAwaitTimeMS",
2249+
"maxScan",
2250+
"maxTimeMS",
2251+
"min",
2252+
"noCursorTimeout",
2253+
"noResponse",
2254+
"omitReadPreference",
2255+
"oplogReplay",
2256+
"partial",
2257+
"projection",
2258+
"promoteBuffers",
2259+
"promoteLongs",
2260+
"promoteValues",
2261+
"raw",
2262+
"readConcern",
2263+
"readPreference",
2264+
"retryWrites",
2265+
"returnKey",
2266+
"serializeFunctions",
2267+
"session",
2268+
"showDiskLoc",
2269+
"showRecordId",
2270+
"singleBatch",
2271+
"skip",
2272+
"snapshot",
2273+
"sort",
2274+
"tailable",
2275+
"timeout",
2276+
"timeoutMode",
2277+
"timeoutMS",
2278+
"useBigInt64",
2279+
"willRetryWrite"
2280+
];
22302281

22312282
countlyDb._collection_cache = {};
22322283
//overwrite some methods

0 commit comments

Comments
 (0)