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 resolve(runtime.flows.getFlows()); return runtime.flows.getFlows();
});
}, },
/** /**
* Sets the current flow configuration * Sets the current flow configuration
@ -65,10 +63,8 @@ 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 flows = opts.flows;
var deploymentType = opts.deploymentType||"full"; var deploymentType = opts.deploymentType||"full";
runtime.log.audit({event: "flows.set",type:deploymentType}, opts.req); runtime.log.audit({event: "flows.set",type:deploymentType}, opts.req);
@ -85,18 +81,17 @@ var api = module.exports = {
err.code = "version_mismatch"; err.code = "version_mismatch";
err.status = 409; err.status = 409;
//TODO: log warning //TODO: log warning
return reject(err); throw err;
} }
} }
apiPromise = runtime.flows.setFlows(flows.flows,flows.credentials,deploymentType,null,null,opts.user); apiPromise = runtime.flows.setFlows(flows.flows,flows.credentials,deploymentType,null,null,opts.user);
} }
apiPromise.then(function(flowId) { return apiPromise.then(function(flowId) {
return resolve({rev:flowId}); return {rev:flowId};
}).catch(function(err) { }).catch(function(err) {
runtime.log.warn(runtime.log._("api.flows.error-"+(deploymentType === 'reload'?'reload':'save'),{message:err.message})); runtime.log.warn(runtime.log._("api.flows.error-"+(deploymentType === 'reload'?'reload':'save'),{message:err.message}));
runtime.log.warn(err.stack); runtime.log.warn(err.stack);
return reject(err); throw err
});
}); });
}); });
}, },
@ -110,13 +105,12 @@ 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;
runtime.flows.addFlow(flow, opts.user).then(function (id) { return 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 resolve(id); return id;
}).catch(function (err) { }).catch(function (err) {
runtime.log.audit({ runtime.log.audit({
event: "flow.add", event: "flow.add",
@ -124,8 +118,7 @@ var api = module.exports = {
message: err.toString() message: err.toString()
}, opts.req); }, opts.req);
err.status = 400; err.status = 400;
return reject(err); throw 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 resolve(flow); return 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;
return reject(err); throw err;
} }
})
}, },
/** /**
* Updates an existing flow configuration * Updates an existing flow configuration
@ -164,31 +155,19 @@ 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;
try { return runtime.flows.updateFlow(id, flow, opts.user).then(function () {
runtime.flows.updateFlow(id, flow, opts.user).then(function () {
runtime.log.audit({event: "flow.update", id: id}, opts.req); runtime.log.audit({event: "flow.update", id: id}, opts.req);
return resolve(id); return id;
}).catch(function (err) { }).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) { if (err.code === 404) {
runtime.log.audit({event: "flow.update", id: id, error: "not_found"}, opts.req); runtime.log.audit({event: "flow.update", id: id, error: "not_found"}, opts.req);
// TODO: this swap around of .code and .status isn't ideal // TODO: this swap around of .code and .status isn't ideal
err.status = 404; err.status = 404;
err.code = "not_found"; err.code = "not_found";
return reject(err);
} else { } else {
runtime.log.audit({ runtime.log.audit({
event: "flow.update", event: "flow.update",
@ -196,10 +175,9 @@ var api = module.exports = {
message: err.toString() message: err.toString()
}, opts.req); }, opts.req);
err.status = 400; err.status = 400;
return reject(err);
} }
} throw err;
}); })
}); });
}, },
/** /**
@ -211,25 +189,13 @@ 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;
try { return runtime.flows.removeFlow(id, opts.user).then(function () {
runtime.flows.removeFlow(id, opts.user).then(function () {
runtime.log.audit({event: "flow.remove", id: id}, opts.req); runtime.log.audit({event: "flow.remove", id: id}, opts.req);
return resolve(); return resolve();
}).catch(function (err) { }).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) { if (err.code === 404) {
runtime.log.audit({event: "flow.remove", id: id, error: "not_found"}, opts.req); runtime.log.audit({event: "flow.remove", id: id, error: "not_found"}, opts.req);
// TODO: this swap around of .code and .status isn't ideal // TODO: this swap around of .code and .status isn't ideal
@ -244,9 +210,8 @@ var api = module.exports = {
message: err.toString() message: err.toString()
}, opts.req); }, opts.req);
err.status = 400; err.status = 400;
return reject(err);
}
} }
throw err;
}); });
}); });
}, },
@ -261,12 +226,11 @@ 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 resolve({}); return {};
} }
var sendCredentials = {}; var sendCredentials = {};
var cred; var cred;
@ -289,7 +253,6 @@ var api = module.exports = {
} }
} }
} }
resolve(sendCredentials); 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');