1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Use more async funcs in runtime/lib/api to reduce Promise creation

This commit is contained in:
Nick O'Leary 2020-11-30 16:58:05 +00:00
parent 5992ed1fab
commit a1f565f756
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
7 changed files with 419 additions and 484 deletions

View File

@ -106,9 +106,8 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_comms * @memberof @node-red/runtime_comms
*/ */
addConnection: function(opts) { addConnection: async function(opts) {
connections.push(opts.client); connections.push(opts.client);
return Promise.resolve();
}, },
/** /**
@ -119,14 +118,13 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_comms * @memberof @node-red/runtime_comms
*/ */
removeConnection: function(opts) { removeConnection: async function(opts) {
for (var i=0;i<connections.length;i++) { for (var i=0;i<connections.length;i++) {
if (connections[i] === opts.client) { if (connections[i] === opts.client) {
connections.splice(i,1); connections.splice(i,1);
break; break;
} }
} }
return Promise.resolve();
}, },
/** /**
@ -140,14 +138,13 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_comms * @memberof @node-red/runtime_comms
*/ */
subscribe: function(opts) { subscribe: async function(opts) {
var re = new RegExp("^"+opts.topic.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g,"\\$1").replace(/\+/g,"[^/]+").replace(/\/#$/,"(\/.*)?")+"$"); var re = new RegExp("^"+opts.topic.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g,"\\$1").replace(/\+/g,"[^/]+").replace(/\/#$/,"(\/.*)?")+"$");
for (var t in retained) { for (var t in retained) {
if (re.test(t)) { if (re.test(t)) {
opts.client.send(t,retained[t]); opts.client.send(t,retained[t]);
} }
} }
return Promise.resolve();
}, },
/** /**
@ -159,5 +156,5 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_comms * @memberof @node-red/runtime_comms
*/ */
unsubscribe: function(opts) {} unsubscribe: async function(opts) {}
}; };

View File

@ -71,7 +71,7 @@ var api = module.exports = {
* @return {Promise} - the node information * @return {Promise} - the node information
* @memberof @node-red/runtime_context * @memberof @node-red/runtime_context
*/ */
getValue: function(opts) { getValue: async function(opts) {
return new Promise(function(resolve,reject) { return new Promise(function(resolve,reject) {
var scope = opts.scope; var scope = opts.scope;
var id = opts.id; var id = opts.id;
@ -165,7 +165,7 @@ var api = module.exports = {
* @return {Promise} - the node information * @return {Promise} - the node information
* @memberof @node-red/runtime_context * @memberof @node-red/runtime_context
*/ */
delete: function(opts) { delete: async function(opts) {
return new Promise(function(resolve,reject) { return new Promise(function(resolve,reject) {
var scope = opts.scope; var scope = opts.scope;
var id = opts.id; var id = opts.id;

View File

@ -38,10 +38,10 @@ var api = module.exports = {
projects: require("./projects"), projects: require("./projects"),
context: require("./context"), context: require("./context"),
isStarted: function(opts) { isStarted: async function(opts) {
return Promise.resolve(runtime.isStarted()); return runtime.isStarted();
}, },
version: function(opts) { version: async function(opts) {
return Promise.resolve(runtime.version()); return runtime.version();
} }
} }

View File

@ -36,32 +36,30 @@ var api = module.exports = {
* @return {Promise<String|Object>} - resolves when complete * @return {Promise<String|Object>} - resolves when complete
* @memberof @node-red/runtime_library * @memberof @node-red/runtime_library
*/ */
getEntry: function(opts) { getEntry: async function(opts) {
return new Promise(function(resolve,reject) { return runtime.library.getEntry(opts.library,opts.type,opts.path).then(function(result) {
runtime.library.getEntry(opts.library,opts.type,opts.path).then(function(result) {
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path}, opts.req); runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path}, opts.req);
return resolve(result); return result;
}).catch(function(err) { }).catch(function(err) {
if (err) { if (err) {
runtime.log.warn(runtime.log._("api.library.error-load-entry",{library:opts.library,type:opts.type,path:opts.path,message:err.toString()})); runtime.log.warn(runtime.log._("api.library.error-load-entry",{library:opts.library,type:opts.type,path:opts.path,message:err.toString()}));
if (err.code === 'forbidden') { if (err.code === 'forbidden') {
err.status = 403; err.status = 403;
return reject(err); throw err;
} else if (err.code === "not_found") { } else if (err.code === "not_found") {
err.status = 404; err.status = 404;
} else { } else {
err.status = 400; err.status = 400;
} }
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path,error:err.code}, opts.req); runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path,error:err.code}, opts.req);
return reject(err); throw err;
} }
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,error:"not_found"}, opts.req); runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,error:"not_found"}, opts.req);
var error = new Error(); var error = new Error();
error.code = "not_found"; error.code = "not_found";
error.status = 404; error.status = 404;
return reject(error); throw error;
}); });
})
}, },
/** /**
@ -77,23 +75,20 @@ var api = module.exports = {
* @return {Promise} - resolves when complete * @return {Promise} - resolves when complete
* @memberof @node-red/runtime_library * @memberof @node-red/runtime_library
*/ */
saveEntry: function(opts) { saveEntry: async function(opts) {
return new Promise(function(resolve,reject) { return runtime.library.saveEntry(opts.library,opts.type,opts.path,opts.meta,opts.body).then(function() {
runtime.library.saveEntry(opts.library,opts.type,opts.path,opts.meta,opts.body).then(function() {
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path}, opts.req); runtime.log.audit({event: "library.set",type:opts.type,path:opts.path}, opts.req);
return resolve();
}).catch(function(err) { }).catch(function(err) {
runtime.log.warn(runtime.log._("api.library.error-save-entry",{path:opts.path,message:err.toString()})); runtime.log.warn(runtime.log._("api.library.error-save-entry",{path:opts.path,message:err.toString()}));
if (err.code === 'forbidden') { if (err.code === 'forbidden') {
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"forbidden"}, opts.req); runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"forbidden"}, opts.req);
err.status = 403; err.status = 403;
return reject(err); throw err;
} }
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"unexpected_error",message:err.toString()}, opts.req);
var error = new Error(); var error = new Error();
error.status = 400; error.status = 400;
return reject(error); throw error;
}); });
})
} }
} }

View File

@ -18,7 +18,7 @@
* @mixin @node-red/runtime_nodes * @mixin @node-red/runtime_nodes
*/ */
var fs = require("fs"); var fs = require("fs-extra");
var runtime; var runtime;
@ -52,22 +52,20 @@ var api = module.exports = {
* @return {Promise<NodeInfo>} - the node information * @return {Promise<NodeInfo>} - the node information
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getNodeInfo: function(opts) { getNodeInfo: async function(opts) {
return new Promise(function(resolve,reject) {
var id = opts.id; var id = opts.id;
var result = runtime.nodes.getNodeInfo(id); var result = runtime.nodes.getNodeInfo(id);
if (result) { if (result) {
runtime.log.audit({event: "nodes.info.get",id:id}, opts.req); runtime.log.audit({event: "nodes.info.get",id:id}, opts.req);
delete result.loaded; delete result.loaded;
return resolve(result); return result;
} else { } else {
runtime.log.audit({event: "nodes.info.get",id:id,error:"not_found"}, opts.req); runtime.log.audit({event: "nodes.info.get",id:id,error:"not_found"}, opts.req);
var err = new Error("Node not found"); var err = new Error("Node not found");
err.code = "not_found"; err.code = "not_found";
err.status = 404; err.status = 404;
return reject(err); throw err;
} }
})
}, },
/** /**
@ -78,11 +76,9 @@ var api = module.exports = {
* @return {Promise<NodeList>} - the list of node modules * @return {Promise<NodeList>} - the list of node modules
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getNodeList: function(opts) { getNodeList: async function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "nodes.list.get"}, opts.req); runtime.log.audit({event: "nodes.list.get"}, opts.req);
return resolve(runtime.nodes.getNodeList()); return runtime.nodes.getNodeList();
})
}, },
/** /**
@ -95,22 +91,20 @@ var api = module.exports = {
* @return {Promise<String>} - the node html content * @return {Promise<String>} - the node html content
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getNodeConfig: function(opts) { getNodeConfig: async function(opts) {
return new Promise(function(resolve,reject) {
var id = opts.id; var id = opts.id;
var lang = opts.lang; var lang = opts.lang;
var result = runtime.nodes.getNodeConfig(id,lang); var result = runtime.nodes.getNodeConfig(id,lang);
if (result) { if (result) {
runtime.log.audit({event: "nodes.config.get",id:id}, opts.req); runtime.log.audit({event: "nodes.config.get",id:id}, opts.req);
return resolve(result); return result;
} else { } else {
runtime.log.audit({event: "nodes.config.get",id:id,error:"not_found"}, opts.req); runtime.log.audit({event: "nodes.config.get",id:id,error:"not_found"}, opts.req);
var err = new Error("Node not found"); var err = new Error("Node not found");
err.code = "not_found"; err.code = "not_found";
err.status = 404; err.status = 404;
return reject(err); throw err;
} }
});
}, },
/** /**
* Gets all node html content * Gets all node html content
@ -121,11 +115,9 @@ var api = module.exports = {
* @return {Promise<String>} - the node html content * @return {Promise<String>} - the node html content
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getNodeConfigs: function(opts) { getNodeConfigs: async function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "nodes.configs.get"}, opts.req); runtime.log.audit({event: "nodes.configs.get"}, opts.req);
return resolve(runtime.nodes.getNodeConfigs(opts.lang)); return runtime.nodes.getNodeConfigs(opts.lang);
});
}, },
/** /**
@ -137,20 +129,18 @@ var api = module.exports = {
* @return {Promise<ModuleInfo>} - the node module info * @return {Promise<ModuleInfo>} - the node module info
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getModuleInfo: function(opts) { getModuleInfo: async function(opts) {
return new Promise(function(resolve,reject) {
var result = runtime.nodes.getModuleInfo(opts.module); var result = runtime.nodes.getModuleInfo(opts.module);
if (result) { if (result) {
runtime.log.audit({event: "nodes.module.get",id:opts.module}, opts.req); runtime.log.audit({event: "nodes.module.get",id:opts.module}, opts.req);
return resolve(result); return result;
} else { } else {
runtime.log.audit({event: "nodes.module.get",id:opts.module,error:"not_found"}, opts.req); runtime.log.audit({event: "nodes.module.get",id:opts.module,error:"not_found"}, opts.req);
var err = new Error("Module not found"); var err = new Error("Module not found");
err.code = "not_found"; err.code = "not_found";
err.status = 404; err.status = 404;
return reject(err); throw err;
} }
})
}, },
/** /**
@ -165,14 +155,13 @@ var api = module.exports = {
* @return {Promise<ModuleInfo>} - the node module info * @return {Promise<ModuleInfo>} - the node module info
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
addModule: function(opts) { addModule: async function(opts) {
return new Promise(function(resolve,reject) {
if (!runtime.settings.available()) { if (!runtime.settings.available()) {
runtime.log.audit({event: "nodes.install",error:"settings_unavailable"}, opts.req); runtime.log.audit({event: "nodes.install",error:"settings_unavailable"}, opts.req);
var err = new Error("Settings unavailable"); var err = new Error("Settings unavailable");
err.code = "settings_unavailable"; err.code = "settings_unavailable";
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
if (opts.tarball) { if (opts.tarball) {
if (runtime.settings.editorTheme && runtime.settings.editorTheme.palette && runtime.settings.editorTheme.palette.upload === false) { if (runtime.settings.editorTheme && runtime.settings.editorTheme.palette && runtime.settings.editorTheme.palette.upload === false) {
@ -180,20 +169,19 @@ var api = module.exports = {
var err = new Error("Invalid request"); var err = new Error("Invalid request");
err.code = "invalid_request"; err.code = "invalid_request";
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
if (opts.module || opts.version || opts.url) { if (opts.module || opts.version || opts.url) {
runtime.log.audit({event: "nodes.install",tarball:opts.tarball.file,module:opts.module,error:"invalid_request"}, opts.req); runtime.log.audit({event: "nodes.install",tarball:opts.tarball.file,module:opts.module,error:"invalid_request"}, opts.req);
var err = new Error("Invalid request"); var err = new Error("Invalid request");
err.code = "invalid_request"; err.code = "invalid_request";
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
runtime.nodes.installModule(opts.tarball.buffer).then(function(info) { return runtime.nodes.installModule(opts.tarball.buffer).then(function(info) {
runtime.log.audit({event: "nodes.install",tarball:opts.tarball.file,module:info.id}, opts.req); runtime.log.audit({event: "nodes.install",tarball:opts.tarball.file,module:info.id}, opts.req);
return resolve(info); return info;
}).catch(function(err) { }).catch(function(err) {
if (err.code) { if (err.code) {
err.status = 400; err.status = 400;
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code}, opts.req); runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code}, opts.req);
@ -201,9 +189,8 @@ var api = module.exports = {
err.status = 400; err.status = 400;
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
} }
return reject(err); throw err;
}) })
return;
} }
if (opts.module) { if (opts.module) {
var existingModule = runtime.nodes.getModuleInfo(opts.module); var existingModule = runtime.nodes.getModuleInfo(opts.module);
@ -213,12 +200,12 @@ var api = module.exports = {
var err = new Error("Module already loaded"); var err = new Error("Module already loaded");
err.code = "module_already_loaded"; err.code = "module_already_loaded";
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
} }
runtime.nodes.installModule(opts.module,opts.version,opts.url).then(function(info) { return runtime.nodes.installModule(opts.module,opts.version,opts.url).then(function(info) {
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url}, opts.req); runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url}, opts.req);
return resolve(info); return info;
}).catch(function(err) { }).catch(function(err) {
if (err.code === 404) { if (err.code === 404) {
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:"not_found"}, opts.req); runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:"not_found"}, opts.req);
@ -231,17 +218,15 @@ var api = module.exports = {
err.status = 400; err.status = 400;
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
} }
return reject(err); throw err;
}) })
} else { } else {
runtime.log.audit({event: "nodes.install",module:opts.module,error:"invalid_request"}, opts.req); runtime.log.audit({event: "nodes.install",module:opts.module,error:"invalid_request"}, opts.req);
var err = new Error("Invalid request"); var err = new Error("Invalid request");
err.code = "invalid_request"; err.code = "invalid_request";
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
});
}, },
/** /**
* Removes a module from the runtime * Removes a module from the runtime
@ -252,14 +237,13 @@ var api = module.exports = {
* @return {Promise} - resolves when complete * @return {Promise} - resolves when complete
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
removeModule: function(opts) { removeModule: async function(opts) {
return new Promise(function(resolve,reject) {
if (!runtime.settings.available()) { if (!runtime.settings.available()) {
runtime.log.audit({event: "nodes.install",error:"settings_unavailable"}, opts.req); runtime.log.audit({event: "nodes.install",error:"settings_unavailable"}, opts.req);
var err = new Error("Settings unavailable"); var err = new Error("Settings unavailable");
err.code = "settings_unavailable"; err.code = "settings_unavailable";
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
var module = runtime.nodes.getModuleInfo(opts.module); var module = runtime.nodes.getModuleInfo(opts.module);
if (!module) { if (!module) {
@ -267,23 +251,21 @@ var api = module.exports = {
var err = new Error("Module not found"); var err = new Error("Module not found");
err.code = "not_found"; err.code = "not_found";
err.status = 404; err.status = 404;
return reject(err); throw err;
} }
try { try {
runtime.nodes.uninstallModule(opts.module).then(function() { return runtime.nodes.uninstallModule(opts.module).then(function() {
runtime.log.audit({event: "nodes.remove",module:opts.module}, opts.req); runtime.log.audit({event: "nodes.remove",module:opts.module}, opts.req);
resolve();
}).catch(function(err) { }).catch(function(err) {
err.status = 400; err.status = 400;
runtime.log.audit({event: "nodes.remove",module:opts.module,error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({event: "nodes.remove",module:opts.module,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
return reject(err); throw err;
}) })
} catch(error) { } catch(error) {
runtime.log.audit({event: "nodes.remove",module:opts.module,error:error.code||"unexpected_error",message:error.toString()}, opts.req); runtime.log.audit({event: "nodes.remove",module:opts.module,error:error.code||"unexpected_error",message:error.toString()}, opts.req);
error.status = 400; error.status = 400;
return reject(error); throw err;
} }
});
}, },
/** /**
@ -296,15 +278,14 @@ var api = module.exports = {
* @return {Promise<ModuleInfo>} - the module info object * @return {Promise<ModuleInfo>} - the module info object
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
setModuleState: function(opts) { setModuleState: async function(opts) {
var mod = opts.module; var mod = opts.module;
return new Promise(function(resolve,reject) {
if (!runtime.settings.available()) { if (!runtime.settings.available()) {
runtime.log.audit({event: "nodes.module.set",error:"settings_unavailable"}, opts.req); runtime.log.audit({event: "nodes.module.set",error:"settings_unavailable"}, opts.req);
var err = new Error("Settings unavailable"); var err = new Error("Settings unavailable");
err.code = "settings_unavailable"; err.code = "settings_unavailable";
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
try { try {
var module = runtime.nodes.getModuleInfo(mod); var module = runtime.nodes.getModuleInfo(mod);
@ -313,7 +294,7 @@ var api = module.exports = {
var err = new Error("Module not found"); var err = new Error("Module not found");
err.code = "not_found"; err.code = "not_found";
err.status = 404; err.status = 404;
return reject(err); throw err;
} }
var nodes = module.nodes; var nodes = module.nodes;
@ -321,18 +302,17 @@ var api = module.exports = {
for (var i = 0; i < nodes.length; ++i) { for (var i = 0; i < nodes.length; ++i) {
promises.push(putNode(nodes[i],opts.enabled)); promises.push(putNode(nodes[i],opts.enabled));
} }
Promise.all(promises).then(function() { return Promise.all(promises).then(function() {
return resolve(runtime.nodes.getModuleInfo(mod)); return runtime.nodes.getModuleInfo(mod);
}).catch(function(err) { }).catch(function(err) {
err.status = 400; err.status = 400;
return reject(err); throw err;
}); });
} catch(error) { } catch(error) {
runtime.log.audit({event: "nodes.module.set",module:mod,enabled:opts.enabled,error:error.code||"unexpected_error",message:error.toString()}, opts.req); runtime.log.audit({event: "nodes.module.set",module:mod,enabled:opts.enabled,error:error.code||"unexpected_error",message:error.toString()}, opts.req);
error.status = 400; error.status = 400;
return reject(error); throw err;
} }
});
}, },
/** /**
@ -345,14 +325,13 @@ var api = module.exports = {
* @return {Promise<ModuleInfo>} - the module info object * @return {Promise<ModuleInfo>} - the module info object
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
setNodeSetState: function(opts) { setNodeSetState: async function(opts) {
return new Promise(function(resolve,reject) {
if (!runtime.settings.available()) { if (!runtime.settings.available()) {
runtime.log.audit({event: "nodes.info.set",error:"settings_unavailable"}, opts.req); runtime.log.audit({event: "nodes.info.set",error:"settings_unavailable"}, opts.req);
var err = new Error("Settings unavailable"); var err = new Error("Settings unavailable");
err.code = "settings_unavailable"; err.code = "settings_unavailable";
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
var id = opts.id; var id = opts.id;
@ -364,24 +343,23 @@ var api = module.exports = {
var err = new Error("Node not found"); var err = new Error("Node not found");
err.code = "not_found"; err.code = "not_found";
err.status = 404; err.status = 404;
return reject(err); throw err;
} else { } else {
delete node.loaded; delete node.loaded;
putNode(node,enabled).then(function(result) { return putNode(node,enabled).then(function(result) {
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled}, opts.req); runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled}, opts.req);
return resolve(result); return result;
}).catch(function(err) { }).catch(function(err) {
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
err.status = 400; err.status = 400;
return reject(err); throw err;
}); });
} }
} catch(error) { } catch(error) {
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:error.code||"unexpected_error",message:error.toString()}, opts.req); runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:error.code||"unexpected_error",message:error.toString()}, opts.req);
error.status = 400; error.status = 400;
return reject(error); throw err;
} }
});
}, },
/** /**
@ -393,13 +371,12 @@ var api = module.exports = {
* @return {Promise<Object>} - the message catalogs * @return {Promise<Object>} - the message catalogs
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getModuleCatalogs: function(opts) { getModuleCatalogs: async function(opts) {
return new Promise(function(resolve,reject) {
var namespace = opts.module; var namespace = opts.module;
var lang = opts.lang; var lang = opts.lang;
var prevLang = runtime.i18n.i.language; var prevLang = runtime.i18n.i.language;
// Trigger a load from disk of the language if it is not the default // Trigger a load from disk of the language if it is not the default
runtime.i18n.i.changeLanguage(lang, function(){ return runtime.i18n.i.changeLanguage(lang, function(){
var nodeList = runtime.nodes.getNodeList(); var nodeList = runtime.nodes.getNodeList();
var result = {}; var result = {};
nodeList.forEach(function(n) { nodeList.forEach(function(n) {
@ -407,9 +384,8 @@ var api = module.exports = {
result[n.id] = runtime.i18n.i.getResourceBundle(lang, n.id)||{}; result[n.id] = runtime.i18n.i.getResourceBundle(lang, n.id)||{};
} }
}); });
resolve(result);
});
runtime.i18n.i.changeLanguage(prevLang); runtime.i18n.i.changeLanguage(prevLang);
return result;
}); });
}, },
@ -423,17 +399,15 @@ var api = module.exports = {
* @return {Promise<Object>} - the message catalog * @return {Promise<Object>} - the message catalog
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getModuleCatalog: function(opts) { getModuleCatalog: async function(opts) {
return new Promise(function(resolve,reject) {
var namespace = opts.module; var namespace = opts.module;
var lang = opts.lang; var lang = opts.lang;
var prevLang = runtime.i18n.i.language; var prevLang = runtime.i18n.i.language;
// Trigger a load from disk of the language if it is not the default // Trigger a load from disk of the language if it is not the default
runtime.i18n.i.changeLanguage(lang, function(){ return runtime.i18n.i.changeLanguage(lang, function(){
var catalog = runtime.i18n.i.getResourceBundle(lang, namespace); var catalog = runtime.i18n.i.getResourceBundle(lang, namespace);
resolve(catalog||{});
});
runtime.i18n.i.changeLanguage(prevLang); runtime.i18n.i.changeLanguage(prevLang);
return catalog||{};
}); });
}, },
@ -445,12 +419,9 @@ var api = module.exports = {
* @return {Promise<IconList>} - the list of all icons * @return {Promise<IconList>} - the list of all icons
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getIconList: function(opts) { getIconList: async function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "nodes.icons.get"}, opts.req); runtime.log.audit({event: "nodes.icons.get"}, opts.req);
return resolve(runtime.nodes.getNodeIcons()); return runtime.nodes.getNodeIcons();
});
}, },
/** /**
* Gets a node icon * Gets a node icon
@ -462,20 +433,15 @@ var api = module.exports = {
* @return {Promise<Buffer>} - the icon file as a Buffer or null if no icon available * @return {Promise<Buffer>} - the icon file as a Buffer or null if no icon available
* @memberof @node-red/runtime_nodes * @memberof @node-red/runtime_nodes
*/ */
getIcon: function(opts) { getIcon: async function(opts) {
return new Promise(function(resolve,reject) {
var iconPath = runtime.nodes.getNodeIconPath(opts.module,opts.icon); var iconPath = runtime.nodes.getNodeIconPath(opts.module,opts.icon);
if (iconPath) { if (iconPath) {
fs.readFile(iconPath,function(err,data) { return fs.readFile(iconPath).catch(err => {
if (err) {
err.status = 400; err.status = 400;
return reject(err); throw err;
}
return resolve(data)
}); });
} else { } else {
resolve(null); return null
} }
});
} }
} }

View File

@ -24,8 +24,8 @@ var api = module.exports = {
init: function(_runtime) { init: function(_runtime) {
runtime = _runtime; runtime = _runtime;
}, },
available: function(opts) { available: async function(opts) {
return Promise.resolve(!!runtime.storage.projects); return !!runtime.storage.projects;
}, },
/** /**
* List projects known to the runtime * List projects known to the runtime
@ -36,7 +36,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
listProjects: function(opts) { listProjects: async function(opts) {
return runtime.storage.projects.listProjects(opts.user).then(function(list) { return runtime.storage.projects.listProjects(opts.user).then(function(list) {
var active = runtime.storage.projects.getActiveProject(opts.user); var active = runtime.storage.projects.getActiveProject(opts.user);
var response = { var response = {
@ -61,7 +61,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
createProject: function(opts) { createProject: async function(opts) {
runtime.log.audit({event: "projects.create",name:opts.project?opts.project.name:"missing-name"}, opts.req); runtime.log.audit({event: "projects.create",name:opts.project?opts.project.name:"missing-name"}, opts.req);
return runtime.storage.projects.createProject(opts.user, opts.project) return runtime.storage.projects.createProject(opts.user, opts.project)
}, },
@ -76,7 +76,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
initialiseProject: function(opts) { initialiseProject: async function(opts) {
// Initialised set when creating default files for an empty repo // Initialised set when creating default files for an empty repo
runtime.log.audit({event: "projects.initialise",id:opts.id}, opts.req); runtime.log.audit({event: "projects.initialise",id:opts.id}, opts.req);
return runtime.storage.projects.initialiseProject(opts.user, opts.id, opts.project) return runtime.storage.projects.initialiseProject(opts.user, opts.id, opts.project)
@ -90,8 +90,8 @@ var api = module.exports = {
* @return {Promise<Object>} - the active project * @return {Promise<Object>} - the active project
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getActiveProject: function(opts) { getActiveProject: async function(opts) {
return Promise.resolve(runtime.storage.projects.getActiveProject(opts.user)); return runtime.storage.projects.getActiveProject(opts.user);
}, },
/** /**
@ -103,13 +103,11 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
setActiveProject: function(opts) { setActiveProject: async function(opts) {
var currentProject = runtime.storage.projects.getActiveProject(opts.user); var currentProject = runtime.storage.projects.getActiveProject(opts.user);
runtime.log.audit({event: "projects.set",id:opts.id}, opts.req); runtime.log.audit({event: "projects.set",id:opts.id}, opts.req);
if (!currentProject || opts.id !== currentProject.name) { if (!currentProject || opts.id !== currentProject.name) {
return runtime.storage.projects.setActiveProject(opts.user, opts.id); return runtime.storage.projects.setActiveProject(opts.user, opts.id);
} else {
return Promise.resolve();
} }
}, },
@ -122,7 +120,7 @@ var api = module.exports = {
* @return {Promise<Object>} - the project metadata * @return {Promise<Object>} - the project metadata
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getProject: function(opts) { getProject: async function(opts) {
return runtime.storage.projects.getProject(opts.user, opts.id) return runtime.storage.projects.getProject(opts.user, opts.id)
}, },
@ -136,7 +134,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
updateProject: function(opts) { updateProject: async function(opts) {
runtime.log.audit({event: "projects.update",id:opts.id}, opts.req); runtime.log.audit({event: "projects.update",id:opts.id}, opts.req);
return runtime.storage.projects.updateProject(opts.user, opts.id, opts.project); return runtime.storage.projects.updateProject(opts.user, opts.id, opts.project);
}, },
@ -150,7 +148,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
deleteProject: function(opts) { deleteProject: async function(opts) {
runtime.log.audit({event: "projects.delete",id:opts.id}, opts.req); runtime.log.audit({event: "projects.delete",id:opts.id}, opts.req);
return runtime.storage.projects.deleteProject(opts.user, opts.id); return runtime.storage.projects.deleteProject(opts.user, opts.id);
}, },
@ -165,7 +163,7 @@ var api = module.exports = {
* @return {Promise<Object>} - the project status * @return {Promise<Object>} - the project status
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getStatus: function(opts) { getStatus: async function(opts) {
return runtime.storage.projects.getStatus(opts.user, opts.id, opts.remote) return runtime.storage.projects.getStatus(opts.user, opts.id, opts.remote)
}, },
@ -179,7 +177,7 @@ var api = module.exports = {
* @return {Promise<Object>} - a list of the local branches * @return {Promise<Object>} - a list of the local branches
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getBranches: function(opts) { getBranches: async function(opts) {
return runtime.storage.projects.getBranches(opts.user, opts.id, opts.remote); return runtime.storage.projects.getBranches(opts.user, opts.id, opts.remote);
}, },
@ -193,7 +191,7 @@ var api = module.exports = {
* @return {Promise<Object>} - the status of the branch * @return {Promise<Object>} - the status of the branch
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getBranchStatus: function(opts) { getBranchStatus: async function(opts) {
return runtime.storage.projects.getBranchStatus(opts.user, opts.id, opts.branch); return runtime.storage.projects.getBranchStatus(opts.user, opts.id, opts.branch);
}, },
@ -208,7 +206,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
setBranch: function(opts) { setBranch: async function(opts) {
runtime.log.audit({event: "projects.branch.set",id:opts.id, branch: opts.branch, create:opts.create}, opts.req); runtime.log.audit({event: "projects.branch.set",id:opts.id, branch: opts.branch, create:opts.create}, opts.req);
return runtime.storage.projects.setBranch(opts.user, opts.id, opts.branch, opts.create) return runtime.storage.projects.setBranch(opts.user, opts.id, opts.branch, opts.create)
}, },
@ -224,7 +222,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
deleteBranch: function(opts) { deleteBranch: async function(opts) {
runtime.log.audit({event: "projects.branch.delete",id:opts.id, branch: opts.branch, force:opts.force}, opts.req); runtime.log.audit({event: "projects.branch.delete",id:opts.id, branch: opts.branch, force:opts.force}, opts.req);
return runtime.storage.projects.deleteBranch(opts.user, opts.id, opts.branch, false, opts.force); return runtime.storage.projects.deleteBranch(opts.user, opts.id, opts.branch, false, opts.force);
}, },
@ -239,7 +237,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
commit: function(opts) { commit: async function(opts) {
runtime.log.audit({event: "projects.commit",id:opts.id}, opts.req); runtime.log.audit({event: "projects.commit",id:opts.id}, opts.req);
return runtime.storage.projects.commit(opts.user, opts.id,{message: opts.message}); return runtime.storage.projects.commit(opts.user, opts.id,{message: opts.message});
}, },
@ -254,7 +252,7 @@ var api = module.exports = {
* @return {Promise<Object>} - the commit details * @return {Promise<Object>} - the commit details
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getCommit: function(opts) { getCommit: async function(opts) {
return runtime.storage.projects.getCommit(opts.user, opts.id, opts.sha); return runtime.storage.projects.getCommit(opts.user, opts.id, opts.sha);
}, },
@ -269,7 +267,7 @@ var api = module.exports = {
* @return {Promise<Array>} - an array of commits * @return {Promise<Array>} - an array of commits
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getCommits: function(opts) { getCommits: async function(opts) {
return runtime.storage.projects.getCommits(opts.user, opts.id, { return runtime.storage.projects.getCommits(opts.user, opts.id, {
limit: opts.limit || 20, limit: opts.limit || 20,
before: opts.before before: opts.before
@ -285,7 +283,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
abortMerge: function(opts) { abortMerge: async function(opts) {
runtime.log.audit({event: "projects.merge.abort",id:opts.id}, opts.req); runtime.log.audit({event: "projects.merge.abort",id:opts.id}, opts.req);
return runtime.storage.projects.abortMerge(opts.user, opts.id); return runtime.storage.projects.abortMerge(opts.user, opts.id);
}, },
@ -301,7 +299,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
resolveMerge: function(opts) { resolveMerge: async function(opts) {
runtime.log.audit({event: "projects.merge.resolve",id:opts.id, file:opts.path}, opts.req); runtime.log.audit({event: "projects.merge.resolve",id:opts.id, file:opts.path}, opts.req);
return runtime.storage.projects.resolveMerge(opts.user, opts.id, opts.path, opts.resolution); return runtime.storage.projects.resolveMerge(opts.user, opts.id, opts.path, opts.resolution);
}, },
@ -315,7 +313,7 @@ var api = module.exports = {
* @return {Promise<Object>} - the file listing * @return {Promise<Object>} - the file listing
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getFiles: function(opts) { getFiles: async function(opts) {
return runtime.storage.projects.getFiles(opts.user, opts.id); return runtime.storage.projects.getFiles(opts.user, opts.id);
}, },
@ -330,7 +328,7 @@ var api = module.exports = {
* @return {Promise<String>} - the content of the file * @return {Promise<String>} - the content of the file
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getFile: function(opts) { getFile: async function(opts) {
return runtime.storage.projects.getFile(opts.user, opts.id,opts.path,opts.tree); return runtime.storage.projects.getFile(opts.user, opts.id,opts.path,opts.tree);
}, },
@ -344,7 +342,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
stageFile: function(opts) { stageFile: async function(opts) {
runtime.log.audit({event: "projects.file.stage",id:opts.id, file:opts.path}, opts.req); runtime.log.audit({event: "projects.file.stage",id:opts.id, file:opts.path}, opts.req);
return runtime.storage.projects.stageFile(opts.user, opts.id, opts.path); return runtime.storage.projects.stageFile(opts.user, opts.id, opts.path);
}, },
@ -359,7 +357,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
unstageFile: function(opts) { unstageFile: async function(opts) {
runtime.log.audit({event: "projects.file.unstage",id:opts.id, file:opts.path}, opts.req); runtime.log.audit({event: "projects.file.unstage",id:opts.id, file:opts.path}, opts.req);
return runtime.storage.projects.unstageFile(opts.user, opts.id, opts.path); return runtime.storage.projects.unstageFile(opts.user, opts.id, opts.path);
}, },
@ -374,7 +372,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
revertFile: function(opts) { revertFile: async function(opts) {
runtime.log.audit({event: "projects.file.revert",id:opts.id, file:opts.path}, opts.req); runtime.log.audit({event: "projects.file.revert",id:opts.id, file:opts.path}, opts.req);
return runtime.storage.projects.revertFile(opts.user, opts.id,opts.path) return runtime.storage.projects.revertFile(opts.user, opts.id,opts.path)
}, },
@ -390,7 +388,7 @@ var api = module.exports = {
* @return {Promise<Object>} - the requested diff * @return {Promise<Object>} - the requested diff
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getFileDiff: function(opts) { getFileDiff: async function(opts) {
return runtime.storage.projects.getFileDiff(opts.user, opts.id, opts.path, opts.type); return runtime.storage.projects.getFileDiff(opts.user, opts.id, opts.path, opts.type);
}, },
@ -403,7 +401,7 @@ var api = module.exports = {
* @return {Promise<Object>} - a list of project remotes * @return {Promise<Object>} - a list of project remotes
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
getRemotes: function(opts) { getRemotes: async function(opts) {
return runtime.storage.projects.getRemotes(opts.user, opts.id); return runtime.storage.projects.getRemotes(opts.user, opts.id);
}, },
@ -420,7 +418,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
addRemote: function(opts) { addRemote: async function(opts) {
runtime.log.audit({event: "projects.remote.add",id:opts.id, remote:opts.remote.name}, opts.req); runtime.log.audit({event: "projects.remote.add",id:opts.id, remote:opts.remote.name}, opts.req);
return runtime.storage.projects.addRemote(opts.user, opts.id, opts.remote) return runtime.storage.projects.addRemote(opts.user, opts.id, opts.remote)
}, },
@ -435,7 +433,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
removeRemote: function(opts) { removeRemote: async function(opts) {
runtime.log.audit({event: "projects.remote.delete",id:opts.id, remote:opts.remote}, opts.req); runtime.log.audit({event: "projects.remote.delete",id:opts.id, remote:opts.remote}, opts.req);
return runtime.storage.projects.removeRemote(opts.user, opts.id, opts.remote); return runtime.storage.projects.removeRemote(opts.user, opts.id, opts.remote);
}, },
@ -451,7 +449,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
updateRemote: function(opts) { updateRemote: async function(opts) {
runtime.log.audit({event: "projects.remote.update",id:opts.id, remote:opts.remote.name}, opts.req); runtime.log.audit({event: "projects.remote.update",id:opts.id, remote:opts.remote.name}, opts.req);
return runtime.storage.projects.updateRemote(opts.user, opts.id, opts.remote.name, opts.remote) return runtime.storage.projects.updateRemote(opts.user, opts.id, opts.remote.name, opts.remote)
}, },
@ -467,7 +465,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
pull: function(opts) { pull: async function(opts) {
runtime.log.audit({event: "projects.pull",id:opts.id, remote: opts.remote, track:opts.track}, opts.req); runtime.log.audit({event: "projects.pull",id:opts.id, remote: opts.remote, track:opts.track}, opts.req);
return runtime.storage.projects.pull(opts.user, opts.id, opts.remote, opts.track, opts.allowUnrelatedHistories); return runtime.storage.projects.pull(opts.user, opts.id, opts.remote, opts.track, opts.allowUnrelatedHistories);
}, },
@ -483,7 +481,7 @@ var api = module.exports = {
* @return {Promise<Object>} - resolves when complete * @return {Promise<Object>} - resolves when complete
* @memberof @node-red/runtime_projects * @memberof @node-red/runtime_projects
*/ */
push: function(opts) { push: async function(opts) {
runtime.log.audit({event: "projects.push",id:opts.id, remote: opts.remote, track:opts.track}, opts.req); runtime.log.audit({event: "projects.push",id:opts.id, remote: opts.remote, track:opts.track}, opts.req);
return runtime.storage.projects.push(opts.user, opts.id, opts.remote, opts.track); return runtime.storage.projects.push(opts.user, opts.id, opts.remote, opts.track);
} }

View File

@ -64,9 +64,7 @@ var api = module.exports = {
* @return {Promise<Object>} - the runtime settings * @return {Promise<Object>} - the runtime settings
* @memberof @node-red/runtime_settings * @memberof @node-red/runtime_settings
*/ */
getRuntimeSettings: function(opts) { getRuntimeSettings: async function(opts) {
return new Promise(function(resolve,reject) {
try {
var safeSettings = { var safeSettings = {
httpNodeRoot: runtime.settings.httpNodeRoot||"/", httpNodeRoot: runtime.settings.httpNodeRoot||"/",
version: runtime.settings.version version: runtime.settings.version
@ -116,12 +114,7 @@ var api = module.exports = {
runtime.settings.exportNodeSettings(safeSettings); runtime.settings.exportNodeSettings(safeSettings);
} }
return safeSettings;
resolve(safeSettings);
}catch(err) {
console.log(err);
}
});
}, },
/** /**
@ -132,14 +125,14 @@ var api = module.exports = {
* @return {Promise<Object>} - the user settings * @return {Promise<Object>} - the user settings
* @memberof @node-red/runtime_settings * @memberof @node-red/runtime_settings
*/ */
getUserSettings: function(opts) { getUserSettings: async function(opts) {
var username; var username;
if (!opts.user || opts.user.anonymous) { if (!opts.user || opts.user.anonymous) {
username = '_'; username = '_';
} else { } else {
username = opts.user.username; username = opts.user.username;
} }
return Promise.resolve(runtime.settings.getUserSettings(username)||{}); return runtime.settings.getUserSettings(username)||{};
}, },
/** /**
@ -151,32 +144,30 @@ var api = module.exports = {
* @return {Promise<Object>} - the user settings * @return {Promise<Object>} - the user settings
* @memberof @node-red/runtime_settings * @memberof @node-red/runtime_settings
*/ */
updateUserSettings: function(opts) { updateUserSettings: async function(opts) {
var username; var username;
if (!opts.user || opts.user.anonymous) { if (!opts.user || opts.user.anonymous) {
username = '_'; username = '_';
} else { } else {
username = opts.user.username; username = opts.user.username;
} }
return new Promise(function(resolve,reject) {
var currentSettings = runtime.settings.getUserSettings(username)||{}; var currentSettings = runtime.settings.getUserSettings(username)||{};
currentSettings = extend(currentSettings, opts.settings); currentSettings = extend(currentSettings, opts.settings);
try { try {
runtime.settings.setUserSettings(username, currentSettings).then(function() { return runtime.settings.setUserSettings(username, currentSettings).then(function() {
runtime.log.audit({event: "settings.update",username:username}, opts.req); runtime.log.audit({event: "settings.update",username:username}, opts.req);
return resolve(); return;
}).catch(function(err) { }).catch(function(err) {
runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
err.status = 400; err.status = 400;
return reject(err); throw err;
}); });
} catch(err) { } catch(err) {
runtime.log.warn(runtime.log._("settings.user-not-available",{message:runtime.log._("settings.not-available")})); runtime.log.warn(runtime.log._("settings.user-not-available",{message:runtime.log._("settings.not-available")}));
runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
err.status = 400; err.status = 400;
return reject(err); throw err;
} }
});
}, },
/** /**
@ -187,16 +178,13 @@ var api = module.exports = {
* @return {Promise<Object>} - the user's ssh keys * @return {Promise<Object>} - the user's ssh keys
* @memberof @node-red/runtime_settings * @memberof @node-red/runtime_settings
*/ */
getUserKeys: function(opts) { getUserKeys: async function(opts) {
return new Promise(function(resolve,reject) {
var username = getSSHKeyUsername(opts.user); var username = getSSHKeyUsername(opts.user);
runtime.storage.projects.ssh.listSSHKeys(username).then(function(list) { return runtime.storage.projects.ssh.listSSHKeys(username).catch(function(err) {
return resolve(list);
}).catch(function(err) {
err.status = 400; err.status = 400;
throw err;
return reject(err); return reject(err);
}); });
});
}, },
/** /**
@ -208,23 +196,23 @@ var api = module.exports = {
* @return {Promise<String>} - the user's ssh public key * @return {Promise<String>} - the user's ssh public key
* @memberof @node-red/runtime_settings * @memberof @node-red/runtime_settings
*/ */
getUserKey: function(opts) { getUserKey: async function(opts) {
return new Promise(function(resolve,reject) {
var username = getSSHKeyUsername(opts.user); var username = getSSHKeyUsername(opts.user);
// console.log('username:', username); // console.log('username:', username);
runtime.storage.projects.ssh.getSSHKey(username, opts.id).then(function(data) { return runtime.storage.projects.ssh.getSSHKey(username, opts.id).then(function(data) {
if (data) { if (data) {
return resolve(data); return data;
} else { } else {
var err = new Error("Key not found"); var err = new Error("Key not found");
err.code = "not_found"; err.code = "not_found";
err.status = 404; err.status = 404;
return reject(err); throw err;
} }
}).catch(function(err) { }).catch(function(err) {
if (!err.status) {
err.status = 400; err.status = 400;
return reject(err); }
}); throw err;
}); });
}, },
@ -240,15 +228,11 @@ var api = module.exports = {
* @return {Promise<String>} - the id of the generated key * @return {Promise<String>} - the id of the generated key
* @memberof @node-red/runtime_settings * @memberof @node-red/runtime_settings
*/ */
generateUserKey: function(opts) { generateUserKey: async function(opts) {
return new Promise(function(resolve,reject) {
var username = getSSHKeyUsername(opts.user); var username = getSSHKeyUsername(opts.user);
runtime.storage.projects.ssh.generateSSHKey(username, opts).then(function(name) { return runtime.storage.projects.ssh.generateSSHKey(username, opts).catch(function(err) {
return resolve(name);
}).catch(function(err) {
err.status = 400; err.status = 400;
return reject(err); throw err;
});
}); });
}, },
@ -261,16 +245,11 @@ var api = module.exports = {
* @return {Promise} - resolves when deleted * @return {Promise} - resolves when deleted
* @memberof @node-red/runtime_settings * @memberof @node-red/runtime_settings
*/ */
removeUserKey: function(opts) { removeUserKey: async function(opts) {
return new Promise(function(resolve,reject) {
var username = getSSHKeyUsername(opts.user); var username = getSSHKeyUsername(opts.user);
runtime.storage.projects.ssh.deleteSSHKey(username, opts.id).then(function() { return runtime.storage.projects.ssh.deleteSSHKey(username, opts.id).catch(function(err) {
return resolve();
}).catch(function(err) {
err.status = 400; err.status = 400;
return reject(err); throw err;
}); });
});
} }
} }