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
* @memberof @node-red/runtime_flows
*/
getFlows: function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "flows.get"}, opts.req);
return resolve(runtime.flows.getFlows());
});
getFlows: async function(opts) {
runtime.log.audit({event: "flows.get"}, opts.req);
return runtime.flows.getFlows();
},
/**
* Sets the current flow configuration
@ -65,38 +63,35 @@ var api = module.exports = {
* @return {Promise<Flows>} - the active flow configuration
* @memberof @node-red/runtime_flows
*/
setFlows: function(opts) {
return mutex.runExclusive(function() {
return new Promise(function(resolve,reject) {
setFlows: async function(opts) {
return mutex.runExclusive(async function() {
var flows = opts.flows;
var deploymentType = opts.deploymentType||"full";
runtime.log.audit({event: "flows.set",type:deploymentType}, opts.req);
var flows = opts.flows;
var deploymentType = opts.deploymentType||"full";
runtime.log.audit({event: "flows.set",type:deploymentType}, opts.req);
var apiPromise;
if (deploymentType === 'reload') {
apiPromise = runtime.flows.loadFlows(true);
} else {
if (flows.hasOwnProperty('rev')) {
var currentVersion = runtime.flows.getFlows().rev;
if (currentVersion !== flows.rev) {
var err;
err = new Error();
err.code = "version_mismatch";
err.status = 409;
//TODO: log warning
return reject(err);
}
var apiPromise;
if (deploymentType === 'reload') {
apiPromise = runtime.flows.loadFlows(true);
} else {
if (flows.hasOwnProperty('rev')) {
var currentVersion = runtime.flows.getFlows().rev;
if (currentVersion !== flows.rev) {
var err;
err = new Error();
err.code = "version_mismatch";
err.status = 409;
//TODO: log warning
throw err;
}
apiPromise = runtime.flows.setFlows(flows.flows,flows.credentials,deploymentType,null,null,opts.user);
}
apiPromise.then(function(flowId) {
return resolve({rev:flowId});
}).catch(function(err) {
runtime.log.warn(runtime.log._("api.flows.error-"+(deploymentType === 'reload'?'reload':'save'),{message:err.message}));
runtime.log.warn(err.stack);
return reject(err);
});
apiPromise = runtime.flows.setFlows(flows.flows,flows.credentials,deploymentType,null,null,opts.user);
}
return apiPromise.then(function(flowId) {
return {rev:flowId};
}).catch(function(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
* @memberof @node-red/runtime_flows
*/
addFlow: function(opts) {
return mutex.runExclusive(function() {
return new Promise(function (resolve, reject) {
var flow = opts.flow;
runtime.flows.addFlow(flow, opts.user).then(function (id) {
runtime.log.audit({event: "flow.add", id: id}, opts.req);
return resolve(id);
}).catch(function (err) {
runtime.log.audit({
event: "flow.add",
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
})
addFlow: async function(opts) {
return mutex.runExclusive(async function() {
var flow = opts.flow;
return runtime.flows.addFlow(flow, opts.user).then(function (id) {
runtime.log.audit({event: "flow.add", id: id}, opts.req);
return id;
}).catch(function (err) {
runtime.log.audit({
event: "flow.add",
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
throw err;
})
});
},
@ -139,20 +132,18 @@ var api = module.exports = {
* @return {Promise<Flow>} - the active flow configuration
* @memberof @node-red/runtime_flows
*/
getFlow: function(opts) {
return new Promise(function (resolve,reject) {
var flow = runtime.flows.getFlow(opts.id);
if (flow) {
runtime.log.audit({event: "flow.get",id:opts.id}, opts.req);
return resolve(flow);
} else {
runtime.log.audit({event: "flow.get",id:opts.id,error:"not_found"}, opts.req);
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}
})
getFlow: async function(opts) {
var flow = runtime.flows.getFlow(opts.id);
if (flow) {
runtime.log.audit({event: "flow.get",id:opts.id}, opts.req);
return flow;
} else {
runtime.log.audit({event: "flow.get",id:opts.id,error:"not_found"}, opts.req);
var err = new Error();
err.code = "not_found";
err.status = 404;
throw err;
}
},
/**
* Updates an existing flow configuration
@ -164,42 +155,29 @@ var api = module.exports = {
* @return {Promise<String>} - the id of the updated flow
* @memberof @node-red/runtime_flows
*/
updateFlow: function(opts) {
return mutex.runExclusive(function() {
return new Promise(function (resolve, reject) {
var flow = opts.flow;
var id = opts.id;
try {
runtime.flows.updateFlow(id, flow, opts.user).then(function () {
runtime.log.audit({event: "flow.update", id: id}, opts.req);
return resolve(id);
}).catch(function (err) {
runtime.log.audit({
event: "flow.update",
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
})
} catch (err) {
if (err.code === 404) {
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);
}
updateFlow: async function(opts) {
return mutex.runExclusive(async function() {
var flow = opts.flow;
var id = opts.id;
return runtime.flows.updateFlow(id, flow, opts.user).then(function () {
runtime.log.audit({event: "flow.update", id: id}, opts.req);
return id;
}).catch(function (err) {
if (err.code === 404) {
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";
} else {
runtime.log.audit({
event: "flow.update",
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
}
});
throw err;
})
});
},
/**
@ -211,42 +189,29 @@ var api = module.exports = {
* @return {Promise} - resolves if successful
* @memberof @node-red/runtime_flows
*/
deleteFlow: function(opts) {
deleteFlow: async function(opts) {
return mutex.runExclusive(function() {
return new Promise(function (resolve, reject) {
var id = opts.id;
try {
runtime.flows.removeFlow(id, opts.user).then(function () {
runtime.log.audit({event: "flow.remove", id: id}, opts.req);
return resolve();
}).catch(function (err) {
runtime.log.audit({
event: "flow.remove",
id: id,
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
});
} catch (err) {
if (err.code === 404) {
runtime.log.audit({event: "flow.remove", 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.remove",
id: id,
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
}
var id = opts.id;
return runtime.flows.removeFlow(id, opts.user).then(function () {
runtime.log.audit({event: "flow.remove", id: id}, opts.req);
return resolve();
}).catch(function (err) {
if (err.code === 404) {
runtime.log.audit({event: "flow.remove", 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.remove",
id: id,
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400;
}
throw err;
});
});
},
@ -261,35 +226,33 @@ var api = module.exports = {
* @return {Promise<Object>} - the safe credentials
* @memberof @node-red/runtime_flows
*/
getNodeCredentials: function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "credentials.get",type:opts.type,id:opts.id}, opts.req);
var credentials = runtime.nodes.getCredentials(opts.id);
if (!credentials) {
return resolve({});
}
var sendCredentials = {};
var cred;
if (/^subflow(:|$)/.test(opts.type)) {
for (cred in credentials) {
if (credentials.hasOwnProperty(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] || '';
}
getNodeCredentials: async function(opts) {
runtime.log.audit({event: "credentials.get",type:opts.type,id:opts.id}, opts.req);
var credentials = runtime.nodes.getCredentials(opts.id);
if (!credentials) {
return {};
}
var sendCredentials = {};
var cred;
if (/^subflow(:|$)/.test(opts.type)) {
for (cred in credentials) {
if (credentials.hasOwnProperty(cred)) {
sendCredentials['has_'+cred] = credentials[cred] != null && 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 when = require("when");
var Flow = require('./Flow');
@ -488,7 +487,7 @@ function updateMissingTypes() {
}
}
function addFlow(flow, user) {
async function addFlow(flow, user) {
var i,node;
if (!flow.hasOwnProperty('nodes')) {
throw new Error('missing nodes property');
@ -513,10 +512,10 @@ function addFlow(flow, user) {
node = flow.nodes[i];
if (activeFlowConfig.allNodes[node.id]) {
// TODO nls
return when.reject(new Error('duplicate id'));
throw new Error('duplicate id');
}
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;
nodes.push(node);
@ -526,10 +525,10 @@ function addFlow(flow, user) {
node = flow.configs[i];
if (activeFlowConfig.allNodes[node.id]) {
// TODO nls
return when.reject(new Error('duplicate id'));
throw new Error('duplicate id');
}
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;
nodes.push(node);
@ -614,7 +613,7 @@ function getFlow(id) {
return result;
}
function updateFlow(id,newFlow, user) {
async function updateFlow(id,newFlow, user) {
var label = id;
if (id !== 'global') {
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') {
// TODO: nls + error code
throw new Error('not allowed to remove global');