Promisifying and error handling

This commit is contained in:
Ryan Blenis 2019-11-05 00:11:14 -05:00
parent bbb402b533
commit ed701dff39
4 changed files with 106 additions and 57 deletions

View File

@ -3122,14 +3122,28 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
} }
case 'pluginLatestCheck': { case 'pluginLatestCheck': {
if ((user.siteadmin & 0xFFFFFFFF) == 0 || parent.parent.pluginHandler == null) break; // must be full admin with plugins enabled if ((user.siteadmin & 0xFFFFFFFF) == 0 || parent.parent.pluginHandler == null) break; // must be full admin with plugins enabled
parent.parent.pluginHandler.getPluginLatest(function(latest) { parent.parent.pluginHandler.getPluginLatest()
.then(function(latest) {
try { ws.send(JSON.stringify({ action: 'pluginVersionsAvailable', list: latest })); } catch (ex) { } try { ws.send(JSON.stringify({ action: 'pluginVersionsAvailable', list: latest })); } catch (ex) { }
}); });
break; break;
} }
case 'addplugin': { case 'addplugin': {
if ((user.siteadmin & 0xFFFFFFFF) == 0 || parent.parent.pluginHandler == null) break; // must be full admin, plugins enabled if ((user.siteadmin & 0xFFFFFFFF) == 0 || parent.parent.pluginHandler == null) break; // must be full admin, plugins enabled
parent.parent.pluginHandler.addPlugin(command.url); try {
//parent.parent.pluginHandler.addPlugin(command.url)
parent.parent.pluginHandler.getPluginConfig(command.url)
.then(parent.parent.pluginHandler.addPlugin)
.then(function(docs){ console.log('landed');
var targets = ['*', 'server-users'];
parent.parent.DispatchEvent(targets, obj, { action: 'updatePluginList', list: docs });
})
.catch(function(err) {
if (typeof err == 'object') err = err.message;
try { ws.send(JSON.stringify({ action: 'pluginError', msg: err })); } catch (er) { }
});
} catch(e) { console.log('Cannot add plugin: ' + e); }
break; break;
} }
case 'installplugin': { case 'installplugin': {

View File

@ -13,6 +13,7 @@
/*jshint strict: false */ /*jshint strict: false */
/*jshint esversion: 6 */ /*jshint esversion: 6 */
"use strict"; "use strict";
require('promise');
module.exports.pluginHandler = function (parent) { module.exports.pluginHandler = function (parent) {
var obj = {}; var obj = {};
@ -214,54 +215,72 @@ module.exports.pluginHandler = function (parent) {
return plugins; return plugins;
} }
obj.getPluginConfig = function(configUrl, func) { obj.getPluginConfig = function(configUrl) {
var https = require('https'); return new Promise(function(resolve, reject) {
if (configUrl.indexOf('://') === -1) return; // @TODO error here var https = require('https');
https.get(configUrl, function(res) { if (configUrl.indexOf('://') === -1) reject('Unable to fetch the config: Bad URL (' + configUrl + ')');
var configStr = ''; https.get(configUrl, function(res) {
res.on('data', function(chunk){ var configStr = '';
configStr += chunk; res.on('data', function(chunk){
}); configStr += chunk;
res.on('end', function(){
if (configStr[0] == '{') { // let's be sure we're JSON
try {
var pluginConfig = JSON.parse(configStr);
if (Array.isArray(pluginConfig) && pluginConfig.length == 1) pluginConfig = pluginConfig[0];
if (obj.isValidConfig(pluginConfig, configUrl)) {
func(pluginConfig);
}
} catch (e) { console.log('Error getting plugin config. Check that you have valid JSON.', e.stack); }
}
});
}).on('error', function(e) {
console.log("Error getting plugin config. Check that the URL is correct.: " + e.message);
});
};
obj.getPluginLatest = function(func) {
parent.db.getPlugins(function(err, plugins){
plugins.forEach(function(curconf){
obj.getPluginConfig(curconf.configUrl, function(newconf){
var s = require('semver');
func({
"id": curconf._id,
"installedVersion": curconf.version,
"version": newconf.version,
"hasUpdate": s.gt(newconf.version, curconf.version),
"meshCentralCompat": s.satisfies(s.coerce(parent.currentVer), newconf.meshCentralCompat),
"changelogUrl": curconf.changelogUrl,
"status": curconf.status
});
}); });
}); res.on('end', function(){
}); if (configStr[0] == '{') { // let's be sure we're JSON
try {
var pluginConfig = JSON.parse(configStr);
if (Array.isArray(pluginConfig) && pluginConfig.length == 1) pluginConfig = pluginConfig[0];
if (obj.isValidConfig(pluginConfig, configUrl)) {
resolve(pluginConfig);
} else {
reject("This does not appear to be a valid plugin configuration.");
}
} catch (e) { reject('Error getting plugin config. Check that you have valid JSON.'); }
} else {
reject('Error getting plugin config. Check that you have valid JSON.');
}
});
}).on('error', function(e) {
reject("Error getting plugin config: " + e.message);
});
})
}; };
obj.addPlugin = function(url) { obj.getPluginLatest = function() {
obj.getPluginConfig(url, function(pluginConfig){ return new Promise(function(resolve, reject) {
parent.db.getPlugins(function(err, plugins) {
var proms = [];
plugins.forEach(function(curconf) {
proms.push(obj.getPluginConfig(curconf.configUrl));
});
var latestRet = [];
Promise.all(proms).then(function(newconfs) {
newconfs.forEach(function(newconf) {
var curconf = null;
plugins.forEach(function(conf) {
if (conf.configUrl == newconf.configUrl) curconf = conf;
});
if (curconf == null) reject('Some plugin configs could not be parsed');
var s = require('semver');
latestRet.push({
"id": curconf._id,
"installedVersion": curconf.version,
"version": newconf.version,
"hasUpdate": s.gt(newconf.version, curconf.version),
"meshCentralCompat": s.satisfies(s.coerce(parent.currentVer), newconf.meshCentralCompat),
"changelogUrl": curconf.changelogUrl,
"status": curconf.status
});
resolve(latestRet);
});
}).catch((e) => { console.log('Error reaching plugins, update call aborted. ', e)});
});
});
};
obj.addPlugin = function(pluginConfig) {
return new Promise(function(resolve, reject) {
parent.db.addPlugin({ parent.db.addPlugin({
"name": pluginConfig.name, "name": pluginConfig.name,
"shortName": pluginConfig.shortName, "shortName": pluginConfig.shortName,
@ -279,13 +298,12 @@ module.exports.pluginHandler = function (parent) {
"meshCentralCompat": pluginConfig.meshCentralCompat, "meshCentralCompat": pluginConfig.meshCentralCompat,
"status": 0 // 0: disabled, 1: enabled "status": 0 // 0: disabled, 1: enabled
}, function() { }, function() {
parent.db.getPlugins(function(err, docs){ parent.db.getPlugins(function(err, docs){
var targets = ['*', 'server-users']; if (err) reject(err);
parent.DispatchEvent(targets, obj, { action: 'updatePluginList', list: docs }); else resolve(docs);
});
}); });
}); });
});
}; };
obj.installPlugin = function(id, func) { obj.installPlugin = function(id, func) {

View File

@ -2653,6 +2653,13 @@ a {
position: absolute; position: absolute;
cursor: pointer; /* Add a pointer on hover */ cursor: pointer; /* Add a pointer on hover */
} }
.pluginOverlayContent {
width: 100%; /* Full width (cover the whole page) */
height: 100%; /* Full height (cover the whole page) */
background-color: #FFFFFF; /* Black background with opacity */
z-index: 2; /* Specify a stack order in case you're using a different order for other elements */
position: relative;
}
.pluginTitleBar { .pluginTitleBar {
padding: 4px; padding: 4px;

View File

@ -2368,6 +2368,10 @@
updatePluginList(message.list); updatePluginList(message.list);
break; break;
} }
case 'pluginError': {
setDialogMode(2, 'Oops!', 1, null, message.msg);
break;
}
case 'plugin': { case 'plugin': {
if ((pluginHandler == null) || (typeof message.plugin != 'string')) break; if ((pluginHandler == null) || (typeof message.plugin != 'string')) break;
try { pluginHandler[message.plugin][message.method](server, message); } catch (e) { console.log('Error loading plugin handler ('+ e + ')'); } try { pluginHandler[message.plugin][message.method](server, message); } catch (e) { console.log('Error loading plugin handler ('+ e + ')'); }
@ -9419,6 +9423,9 @@
} }
function updatePluginList(versInfo) { function updatePluginList(versInfo) {
if (Array.isArray(versInfo)) {
versInfo.forEach(function(v) { updatePluginList(v); });
}
if (installedPluginList.length) { if (installedPluginList.length) {
if (versInfo != null) { if (versInfo != null) {
if (installedPluginList['version_info'] == null) installedPluginList['version_info'] = []; if (installedPluginList['version_info'] == null) installedPluginList['version_info'] = [];
@ -9479,6 +9486,7 @@
if (!vin.meshCentralCompat) { if (!vin.meshCentralCompat) {
p.upgradeAvail += vers_not_compat; p.upgradeAvail += vers_not_compat;
cant_action.push('install'); cant_action.push('install');
cant_action.push('upgrade');
} }
} }
@ -9533,8 +9541,9 @@
} }
function goPlugin(pname, title) { function goPlugin(pname, title) {
let xwin = `<div class="pluginTitleBar"><span>${title}</span><span class="pluginCloseBtn"><button onclick="return noGoPlugin(this);">X</button></span></div>` let xwin = `<div class="pluginTitleBar"><span>${title}</span><span class="pluginCloseBtn"><button onclick="return noGoPlugin(this);">X</button></span></div>`;
let dif = document.createElement('div'); let dif = document.createElement('div');
let cdif = document.createElement('div');
dif.classList.add('pluginOverlay'); dif.classList.add('pluginOverlay');
dif.innerHTML = xwin; dif.innerHTML = xwin;
let pif = document.createElement('iframe'); let pif = document.createElement('iframe');
@ -9543,8 +9552,9 @@
pif.style.width = '100%'; pif.style.width = '100%';
pif.style.height = '100%'; pif.style.height = '100%';
pif.setAttribute('frameBorder', '0'); pif.setAttribute('frameBorder', '0');
cdif.classList.add('pluginOverlayContent');
dif.append(pif); cdif.append(pif);
dif.append(cdif);
let x = Q('p7'); let x = Q('p7');
x.parentNode.insertBefore(dif, x.nextSibling); x.parentNode.insertBefore(dif, x.nextSibling);
Q('p7').classList.add('pluginOverlayBg'); Q('p7').classList.add('pluginOverlayBg');