From 0519f8473492cc05ed18ad983cb8b2fa7f581dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 31 Aug 2018 11:04:42 -0400 Subject: [PATCH] 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 --- install/package.json | 1 + src/categories.js | 2 ++ src/database/mongo.js | 4 +++- src/database/postgres.js | 2 ++ src/database/redis.js | 2 ++ src/groups.js | 2 ++ src/messaging.js | 2 ++ src/notifications.js | 2 ++ src/posts.js | 2 ++ src/privileges.js | 2 ++ src/promisify.js | 37 +++++++++++++++++++++++++++++++++++++ src/topics.js | 2 ++ src/user.js | 1 + 13 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/promisify.js diff --git a/install/package.json b/install/package.json index 48a6784957..b07bef8fbd 100644 --- a/install/package.json +++ b/install/package.json @@ -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", diff --git a/src/categories.js b/src/categories.js index 1bbc80e599..93d2c87e54 100644 --- a/src/categories.js +++ b/src/categories.js @@ -386,3 +386,5 @@ Categories.filterIgnoringUids = function (cid, uids, callback) { }, ], callback); }; + +Categories.async = require('./promisify')(Categories); diff --git a/src/database/mongo.js b/src/database/mongo.js index 8c9cd8e0fa..99e045d9f4 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -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(); }); }; diff --git a/src/database/postgres.js b/src/database/postgres.js index 8cd4f97d42..b8f4e4e90a 100644 --- a/src/database/postgres.js +++ b/src/database/postgres.js @@ -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(); }); }); diff --git a/src/database/redis.js b/src/database/redis.js index 8186a950e2..6d780d0853 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -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(); }); }; diff --git a/src/groups.js b/src/groups.js index 780eda46c4..9ac5cc8b85 100644 --- a/src/groups.js +++ b/src/groups.js @@ -301,3 +301,5 @@ Groups.existsBySlug = function (slug, callback) { db.isObjectField('groupslug:groupname', slug, callback); } }; + +Groups.async = require('./promisify')(Groups); diff --git a/src/messaging.js b/src/messaging.js index ba9e794b30..543d6d03a7 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -383,3 +383,5 @@ Messaging.hasPrivateChat = function (uid, withUid, callback) { }, ], callback); }; + +Messaging.async = require('./promisify')(Messaging); diff --git a/src/notifications.js b/src/notifications.js index 32adb67fe3..e45625e6e4 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -608,3 +608,5 @@ Notifications.merge = function (notifications, callback) { callback(err, data.notifications); }); }; + +Notifications.async = require('./promisify')(Notifications); diff --git a/src/posts.js b/src/posts.js index cd58d81527..080a143f94 100644 --- a/src/posts.js +++ b/src/posts.js @@ -321,3 +321,5 @@ Posts.modifyPostByPrivilege = function (post, privileges) { } } }; + +Posts.async = require('./promisify')(Posts); diff --git a/src/privileges.js b/src/privileges.js index 9ed1664ced..add204c029 100644 --- a/src/privileges.js +++ b/src/privileges.js @@ -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); diff --git a/src/promisify.js b/src/promisify.js new file mode 100644 index 0000000000..9882cf2753 --- /dev/null +++ b/src/promisify.js @@ -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; +}; diff --git a/src/topics.js b/src/topics.js index 5214b6a716..a67ad80097 100644 --- a/src/topics.js +++ b/src/topics.js @@ -368,3 +368,5 @@ Topics.search = function (tid, term, callback) { callback(err, Array.isArray(pids) ? pids : []); }); }; + +Topics.async = require('./promisify')(Topics); diff --git a/src/user.js b/src/user.js index 3b005d5572..72a0d1d74c 100644 --- a/src/user.js +++ b/src/user.js @@ -435,3 +435,4 @@ User.addInterstitials = function (callback) { callback(); }; +User.async = require('./promisify')(User);