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.
This commit is contained in:
Ray Bellis 2019-01-23 12:24:53 +00:00
parent 5ef4a2d1d5
commit 0c2d662541
2 changed files with 45 additions and 44 deletions

View file

@ -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) {

View file

@ -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;
});
};