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

Remove when.js from runtime/lib/flow/index

This commit is contained in:
Nick O'Leary 2020-09-29 17:20:01 +01:00
parent 22a301b55e
commit ea45dde63a
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 131 additions and 169 deletions

View File

@ -49,11 +49,9 @@ var api = module.exports = {
* @return {Promise<Flows>} - the active flow configuration * @return {Promise<Flows>} - the active flow configuration
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
getFlows: function(opts) { getFlows: async function(opts) {
return new Promise(function(resolve,reject) { runtime.log.audit({event: "flows.get"}, opts.req);
runtime.log.audit({event: "flows.get"}, opts.req); return runtime.flows.getFlows();
return resolve(runtime.flows.getFlows());
});
}, },
/** /**
* Sets the current flow configuration * Sets the current flow configuration
@ -65,38 +63,35 @@ var api = module.exports = {
* @return {Promise<Flows>} - the active flow configuration * @return {Promise<Flows>} - the active flow configuration
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
setFlows: function(opts) { setFlows: async function(opts) {
return mutex.runExclusive(function() { return mutex.runExclusive(async function() {
return new Promise(function(resolve,reject) { var flows = opts.flows;
var deploymentType = opts.deploymentType||"full";
runtime.log.audit({event: "flows.set",type:deploymentType}, opts.req);
var flows = opts.flows; var apiPromise;
var deploymentType = opts.deploymentType||"full"; if (deploymentType === 'reload') {
runtime.log.audit({event: "flows.set",type:deploymentType}, opts.req); apiPromise = runtime.flows.loadFlows(true);
} else {
var apiPromise; if (flows.hasOwnProperty('rev')) {
if (deploymentType === 'reload') { var currentVersion = runtime.flows.getFlows().rev;
apiPromise = runtime.flows.loadFlows(true); if (currentVersion !== flows.rev) {
} else { var err;
if (flows.hasOwnProperty('rev')) { err = new Error();
var currentVersion = runtime.flows.getFlows().rev; err.code = "version_mismatch";
if (currentVersion !== flows.rev) { err.status = 409;
var err; //TODO: log warning
err = new Error(); throw err;
err.code = "version_mismatch";
err.status = 409;
//TODO: log warning
return reject(err);
}
} }
apiPromise = runtime.flows.setFlows(flows.flows,flows.credentials,deploymentType,null,null,opts.user);
} }
apiPromise.then(function(flowId) { apiPromise = runtime.flows.setFlows(flows.flows,flows.credentials,deploymentType,null,null,opts.user);
return resolve({rev:flowId}); }
}).catch(function(err) { return apiPromise.then(function(flowId) {
runtime.log.warn(runtime.log._("api.flows.error-"+(deploymentType === 'reload'?'reload':'save'),{message:err.message})); return {rev:flowId};
runtime.log.warn(err.stack); }).catch(function(err) {
return reject(err); runtime.log.warn(runtime.log._("api.flows.error-"+(deploymentType === 'reload'?'reload':'save'),{message:err.message}));
}); runtime.log.warn(err.stack);
throw err
}); });
}); });
}, },
@ -110,22 +105,20 @@ var api = module.exports = {
* @return {Promise<String>} - the id of the added flow * @return {Promise<String>} - the id of the added flow
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
addFlow: function(opts) { addFlow: async function(opts) {
return mutex.runExclusive(function() { return mutex.runExclusive(async function() {
return new Promise(function (resolve, reject) { var flow = opts.flow;
var flow = opts.flow; return runtime.flows.addFlow(flow, opts.user).then(function (id) {
runtime.flows.addFlow(flow, opts.user).then(function (id) { runtime.log.audit({event: "flow.add", id: id}, opts.req);
runtime.log.audit({event: "flow.add", id: id}, opts.req); return id;
return resolve(id); }).catch(function (err) {
}).catch(function (err) { runtime.log.audit({
runtime.log.audit({ event: "flow.add",
event: "flow.add", error: err.code || "unexpected_error",
error: err.code || "unexpected_error", message: err.toString()
message: err.toString() }, opts.req);
}, opts.req); err.status = 400;
err.status = 400; throw err;
return reject(err);
})
}) })
}); });
}, },
@ -139,20 +132,18 @@ var api = module.exports = {
* @return {Promise<Flow>} - the active flow configuration * @return {Promise<Flow>} - the active flow configuration
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
getFlow: function(opts) { getFlow: async function(opts) {
return new Promise(function (resolve,reject) { var flow = runtime.flows.getFlow(opts.id);
var flow = runtime.flows.getFlow(opts.id); if (flow) {
if (flow) { runtime.log.audit({event: "flow.get",id:opts.id}, opts.req);
runtime.log.audit({event: "flow.get",id:opts.id}, opts.req); return flow;
return resolve(flow); } else {
} else { runtime.log.audit({event: "flow.get",id:opts.id,error:"not_found"}, opts.req);
runtime.log.audit({event: "flow.get",id:opts.id,error:"not_found"}, opts.req); var err = new Error();
var err = new Error(); err.code = "not_found";
err.code = "not_found"; err.status = 404;
err.status = 404; throw err;
return reject(err); }
}
})
}, },
/** /**
* Updates an existing flow configuration * Updates an existing flow configuration
@ -164,42 +155,29 @@ var api = module.exports = {
* @return {Promise<String>} - the id of the updated flow * @return {Promise<String>} - the id of the updated flow
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
updateFlow: function(opts) { updateFlow: async function(opts) {
return mutex.runExclusive(function() { return mutex.runExclusive(async function() {
return new Promise(function (resolve, reject) { var flow = opts.flow;
var flow = opts.flow; var id = opts.id;
var id = opts.id; return runtime.flows.updateFlow(id, flow, opts.user).then(function () {
try { runtime.log.audit({event: "flow.update", id: id}, opts.req);
runtime.flows.updateFlow(id, flow, opts.user).then(function () { return id;
runtime.log.audit({event: "flow.update", id: id}, opts.req); }).catch(function (err) {
return resolve(id); if (err.code === 404) {
}).catch(function (err) { runtime.log.audit({event: "flow.update", id: id, error: "not_found"}, opts.req);
runtime.log.audit({ // TODO: this swap around of .code and .status isn't ideal
event: "flow.update", err.status = 404;
error: err.code || "unexpected_error", err.code = "not_found";
message: err.toString() } else {
}, opts.req); runtime.log.audit({
err.status = 400; event: "flow.update",
return reject(err); error: err.code || "unexpected_error",
}) message: err.toString()
} catch (err) { }, opts.req);
if (err.code === 404) { err.status = 400;
runtime.log.audit({event: "flow.update", id: id, error: "not_found"}, opts.req);
// TODO: this swap around of .code and .status isn't ideal
err.status = 404;
err.code = "not_found";
return reject(err);
} else {
runtime.log.audit({
event: "flow.update",
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
}
} }
}); throw err;
})
}); });
}, },
/** /**
@ -211,42 +189,29 @@ var api = module.exports = {
* @return {Promise} - resolves if successful * @return {Promise} - resolves if successful
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
deleteFlow: function(opts) { deleteFlow: async function(opts) {
return mutex.runExclusive(function() { return mutex.runExclusive(function() {
return new Promise(function (resolve, reject) { var id = opts.id;
var id = opts.id; return runtime.flows.removeFlow(id, opts.user).then(function () {
try { runtime.log.audit({event: "flow.remove", id: id}, opts.req);
runtime.flows.removeFlow(id, opts.user).then(function () { return resolve();
runtime.log.audit({event: "flow.remove", id: id}, opts.req); }).catch(function (err) {
return resolve(); if (err.code === 404) {
}).catch(function (err) { runtime.log.audit({event: "flow.remove", id: id, error: "not_found"}, opts.req);
runtime.log.audit({ // TODO: this swap around of .code and .status isn't ideal
event: "flow.remove", err.status = 404;
id: id, err.code = "not_found";
error: err.code || "unexpected_error", return reject(err);
message: err.toString() } else {
}, opts.req); runtime.log.audit({
err.status = 400; event: "flow.remove",
return reject(err); id: id,
}); error: err.code || "unexpected_error",
} catch (err) { message: err.toString()
if (err.code === 404) { }, opts.req);
runtime.log.audit({event: "flow.remove", id: id, error: "not_found"}, opts.req); err.status = 400;
// TODO: this swap around of .code and .status isn't ideal
err.status = 404;
err.code = "not_found";
return reject(err);
} else {
runtime.log.audit({
event: "flow.remove",
id: id,
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
}
} }
throw err;
}); });
}); });
}, },
@ -261,35 +226,33 @@ var api = module.exports = {
* @return {Promise<Object>} - the safe credentials * @return {Promise<Object>} - the safe credentials
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
getNodeCredentials: function(opts) { getNodeCredentials: async function(opts) {
return new Promise(function(resolve,reject) { runtime.log.audit({event: "credentials.get",type:opts.type,id:opts.id}, opts.req);
runtime.log.audit({event: "credentials.get",type:opts.type,id:opts.id}, opts.req); var credentials = runtime.nodes.getCredentials(opts.id);
var credentials = runtime.nodes.getCredentials(opts.id); if (!credentials) {
if (!credentials) { return {};
return resolve({}); }
} var sendCredentials = {};
var sendCredentials = {}; var cred;
var cred; if (/^subflow(:|$)/.test(opts.type)) {
if (/^subflow(:|$)/.test(opts.type)) { for (cred in credentials) {
for (cred in credentials) { if (credentials.hasOwnProperty(cred)) {
if (credentials.hasOwnProperty(cred)) { sendCredentials['has_'+cred] = credentials[cred] != null && credentials[cred] !== '';
sendCredentials['has_'+cred] = credentials[cred] != null && credentials[cred] !== '';
}
}
} else {
var definition = runtime.nodes.getCredentialDefinition(opts.type) || {};
for (cred in definition) {
if (definition.hasOwnProperty(cred)) {
if (definition[cred].type == "password") {
var key = 'has_' + cred;
sendCredentials[key] = credentials[cred] != null && credentials[cred] !== '';
continue;
}
sendCredentials[cred] = credentials[cred] || '';
}
} }
} }
resolve(sendCredentials); } else {
}) var definition = runtime.nodes.getCredentialDefinition(opts.type) || {};
for (cred in definition) {
if (definition.hasOwnProperty(cred)) {
if (definition[cred].type == "password") {
var key = 'has_' + cred;
sendCredentials[key] = credentials[cred] != null && credentials[cred] !== '';
continue;
}
sendCredentials[cred] = credentials[cred] || '';
}
}
}
return sendCredentials;
} }
} }

View File

@ -15,7 +15,6 @@
**/ **/
var clone = require("clone"); var clone = require("clone");
var when = require("when");
var Flow = require('./Flow'); var Flow = require('./Flow');
@ -488,7 +487,7 @@ function updateMissingTypes() {
} }
} }
function addFlow(flow, user) { async function addFlow(flow, user) {
var i,node; var i,node;
if (!flow.hasOwnProperty('nodes')) { if (!flow.hasOwnProperty('nodes')) {
throw new Error('missing nodes property'); throw new Error('missing nodes property');
@ -513,10 +512,10 @@ function addFlow(flow, user) {
node = flow.nodes[i]; node = flow.nodes[i];
if (activeFlowConfig.allNodes[node.id]) { if (activeFlowConfig.allNodes[node.id]) {
// TODO nls // TODO nls
return when.reject(new Error('duplicate id')); throw new Error('duplicate id');
} }
if (node.type === 'tab' || node.type === 'subflow') { if (node.type === 'tab' || node.type === 'subflow') {
return when.reject(new Error('invalid node type: '+node.type)); throw new Error('invalid node type: '+node.type);
} }
node.z = flow.id; node.z = flow.id;
nodes.push(node); nodes.push(node);
@ -526,10 +525,10 @@ function addFlow(flow, user) {
node = flow.configs[i]; node = flow.configs[i];
if (activeFlowConfig.allNodes[node.id]) { if (activeFlowConfig.allNodes[node.id]) {
// TODO nls // TODO nls
return when.reject(new Error('duplicate id')); throw new Error('duplicate id');
} }
if (node.type === 'tab' || node.type === 'subflow') { if (node.type === 'tab' || node.type === 'subflow') {
return when.reject(new Error('invalid node type: '+node.type)); throw new Error('invalid node type: '+node.type);
} }
node.z = flow.id; node.z = flow.id;
nodes.push(node); nodes.push(node);
@ -614,7 +613,7 @@ function getFlow(id) {
return result; return result;
} }
function updateFlow(id,newFlow, user) { async function updateFlow(id,newFlow, user) {
var label = id; var label = id;
if (id !== 'global') { if (id !== 'global') {
if (!activeFlowConfig.flows[id]) { if (!activeFlowConfig.flows[id]) {
@ -674,7 +673,7 @@ function updateFlow(id,newFlow, user) {
}) })
} }
function removeFlow(id, user) { async function removeFlow(id, user) {
if (id === 'global') { if (id === 'global') {
// TODO: nls + error code // TODO: nls + error code
throw new Error('not allowed to remove global'); throw new Error('not allowed to remove global');