Promisify modules (#6723)

* WIP promisify

* promisify psql

* ability to skip some keys

* dont promisify client object

* remove async

* clone entire module so it has all properties

* add shim for node 6

* ignore sessionStore as well

* ignore pool on psql
v1.18.x
Barış Soner Uşaklı 6 years ago committed by GitHub
parent e882a091a1
commit 0519f84734
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -109,6 +109,7 @@
"spider-detector": "1.0.18",
"toobusy-js": "^0.5.1",
"uglify-es": "^3.3.9",
"util.promisify": "1.0.0",
"validator": "10.7.0",
"winston": "^2.4.0",
"xml": "^1.0.1",

@ -386,3 +386,5 @@ Categories.filterIgnoringUids = function (cid, uids, callback) {
},
], callback);
};
Categories.async = require('./promisify')(Categories);

@ -118,7 +118,6 @@ mongoModule.init = function (callback) {
}
client = _client;
db = client.db();
mongoModule.client = db;
require('./mongo/main')(db, mongoModule);
@ -127,6 +126,9 @@ mongoModule.init = function (callback) {
require('./mongo/sorted')(db, mongoModule);
require('./mongo/list')(db, mongoModule);
require('./mongo/transaction')(db, mongoModule);
mongoModule.async = require('../promisify')(mongoModule, ['client', 'sessionStore']);
callback();
});
};

@ -123,6 +123,8 @@ postgresModule.init = function (callback) {
require('./postgres/list')(wrappedDB, postgresModule);
require('./postgres/transaction')(db, dbNamespace, postgresModule);
postgresModule.async = require('../promisify')(postgresModule, ['client', 'sessionStore', 'pool']);
callback();
});
});

@ -52,6 +52,8 @@ redisModule.init = function (callback) {
require('./redis/list')(redisClient, redisModule);
require('./redis/transaction')(redisClient, redisModule);
redisModule.async = require('../promisify')(redisModule, ['client', 'sessionStore']);
callback();
});
};

@ -301,3 +301,5 @@ Groups.existsBySlug = function (slug, callback) {
db.isObjectField('groupslug:groupname', slug, callback);
}
};
Groups.async = require('./promisify')(Groups);

@ -383,3 +383,5 @@ Messaging.hasPrivateChat = function (uid, withUid, callback) {
},
], callback);
};
Messaging.async = require('./promisify')(Messaging);

@ -608,3 +608,5 @@ Notifications.merge = function (notifications, callback) {
callback(err, data.notifications);
});
};
Notifications.async = require('./promisify')(Notifications);

@ -321,3 +321,5 @@ Posts.modifyPostByPrivilege = function (post, privileges) {
}
}
};
Posts.async = require('./promisify')(Posts);

@ -49,3 +49,5 @@ require('./privileges/categories')(privileges);
require('./privileges/topics')(privileges);
require('./privileges/posts')(privileges);
require('./privileges/users')(privileges);
privileges.async = require('./promisify')(privileges);

@ -0,0 +1,37 @@
'use strict';
// remove once node 6 support is removed
require('util.promisify/shim')();
var util = require('util');
var _ = require('lodash');
module.exports = function (theModule, ignoreKeys) {
ignoreKeys = ignoreKeys || [];
function isCallbackedFunction(func) {
if (typeof func !== 'function') {
return false;
}
var str = func.toString().split('\n')[0];
return str.includes('callback)');
}
function promisifyRecursive(module) {
if (!module) {
return;
}
var keys = Object.keys(module);
keys.forEach(function (key) {
if (ignoreKeys.includes(key)) {
return;
}
if (isCallbackedFunction(module[key])) {
module[key] = util.promisify(module[key]);
} else if (typeof module[key] === 'object') {
promisifyRecursive(module[key]);
}
});
}
const asyncModule = _.cloneDeep(theModule);
promisifyRecursive(asyncModule);
return asyncModule;
};

@ -368,3 +368,5 @@ Topics.search = function (tid, term, callback) {
callback(err, Array.isArray(pids) ? pids : []);
});
};
Topics.async = require('./promisify')(Topics);

@ -435,3 +435,4 @@ User.addInterstitials = function (callback) {
callback();
};
User.async = require('./promisify')(User);

Loading…
Cancel
Save