From 0c2d6625410b81dbd299ed92c5e97735580166de Mon Sep 17 00:00:00 2001 From: Ray Bellis Date: Wed, 23 Jan 2019 12:24:53 +0000 Subject: [PATCH] plugins download and search: converted to Promises Also fixed a bug where the system would make a request to the central server for the plugin list for every search even if the list was already cached. --- src/node/hooks/express/adminplugins.js | 49 +++++++++++++------------- src/static/js/pluginfw/installer.js | 40 +++++++++++---------- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/node/hooks/express/adminplugins.js b/src/node/hooks/express/adminplugins.js index 4c94c5780..7cfb160b9 100644 --- a/src/node/hooks/express/adminplugins.js +++ b/src/node/hooks/express/adminplugins.js @@ -42,14 +42,10 @@ exports.socketio = function(hook_name, args, cb) { socket.emit("results:installed", {installed: installed}); }); - socket.on("checkUpdates", function() { + socket.on("checkUpdates", async function() { // Check plugins for updates - installer.getAvailablePlugins(/*maxCacheAge:*/ 60 * 10, function(er, results) { - if (er) { - console.warn(er); - socket.emit("results:updatable", {updatable: {}}); - return; - } + try { + let results = await installer.getAvailablePlugins(/*maxCacheAge:*/ 60 * 10); var updatable = _(plugins.plugins).keys().filter(function(plugin) { if (!results[plugin]) return false; @@ -61,27 +57,26 @@ exports.socketio = function(hook_name, args, cb) { }); socket.emit("results:updatable", {updatable: updatable}); - }); + } catch (er) { + console.warn(er); + + socket.emit("results:updatable", {updatable: {}}); + } }); - socket.on("getAvailable", function(query) { - installer.getAvailablePlugins(/*maxCacheAge:*/ false, function(er, results) { - if (er) { - console.error(er); - results = {}; - } - - socket.emit("results:available", results); - }); + socket.on("getAvailable", async function(query) { + try { + let results = await installer.getAvailablePlugins(/*maxCacheAge:*/ false); + socket.emit("results:available", results); + } catch (er) { + console.error(er); + socket.emit("results:available", {}); + } }); - socket.on("search", function(query) { - installer.search(query.searchTerm, /*maxCacheAge:*/ 60 * 10, function(er, results) { - if (er) { - console.error(er); - results = {}; - } - + socket.on("search", async function(query) { + try { + let results = await installer.search(query.searchTerm, /*maxCacheAge:*/ 60 * 10); var res = Object.keys(results) .map(function(pluginName) { return results[pluginName]; @@ -92,7 +87,11 @@ exports.socketio = function(hook_name, args, cb) { res = sortPluginList(res, query.sortBy, query.sortDir) .slice(query.offset, query.offset+query.limit); socket.emit("results:search", {results: res, query: query}); - }); + } catch (er) { + console.error(er); + + socket.emit("results:search", {results: {}, query: query}); + } }); socket.on("install", function(plugin_name) { diff --git a/src/static/js/pluginfw/installer.js b/src/static/js/pluginfw/installer.js index dbf8696ed..934d5f036 100644 --- a/src/static/js/pluginfw/installer.js +++ b/src/static/js/pluginfw/installer.js @@ -77,33 +77,35 @@ exports.install = function(plugin_name, cb) { exports.availablePlugins = null; var cacheTimestamp = 0; -exports.getAvailablePlugins = function(maxCacheAge, cb) { - request("https://static.etherpad.org/plugins.json", function(er, response, plugins){ - if (er) return cb && cb(er); +exports.getAvailablePlugins = function(maxCacheAge) { + var nowTimestamp = Math.round(Date.now() / 1000); - if (exports.availablePlugins && maxCacheAge && Math.round(+ new Date / 1000) - cacheTimestamp <= maxCacheAge) { - return cb && cb(null, exports.availablePlugins); + return new Promise(function(resolve, reject) { + // check cache age before making any request + if (exports.availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) { + return resolve(exports.availablePlugins); } - try { - plugins = JSON.parse(plugins); - } catch (err) { - console.error('error parsing plugins.json:', err); - plugins = []; - } + request("https://static.etherpad.org/plugins.json", function(er, response, plugins) { + if (er) return reject(er); - exports.availablePlugins = plugins; - cacheTimestamp = Math.round(+ new Date / 1000); + try { + plugins = JSON.parse(plugins); + } catch (err) { + console.error('error parsing plugins.json:', err); + plugins = []; + } - cb && cb(null, plugins); + exports.availablePlugins = plugins; + cacheTimestamp = nowTimestamp; + resolve(plugins); + }); }); }; -exports.search = function(searchTerm, maxCacheAge, cb) { - exports.getAvailablePlugins(maxCacheAge, function(er, results) { - if (er) return cb && cb(er); - +exports.search = function(searchTerm, maxCacheAge) { + return exports.getAvailablePlugins(maxCacheAge).then(function(results) { var res = {}; if (searchTerm) { @@ -127,6 +129,6 @@ exports.search = function(searchTerm, maxCacheAge, cb) { res[pluginName] = results[pluginName]; } - cb && cb(null, res); + return res; }); };