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) {
getFlows: async function(opts) {
runtime.log.audit({event: "flows.get"}, opts.req);
return resolve(runtime.flows.getFlows());
});
return runtime.flows.getFlows();
},
/**
* Sets the current flow configuration
@ -65,10 +63,8 @@ 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);
@ -85,18 +81,17 @@ var api = module.exports = {
err.code = "version_mismatch";
err.status = 409;
//TODO: log warning
return reject(err);
throw err;
}
}
apiPromise = runtime.flows.setFlows(flows.flows,flows.credentials,deploymentType,null,null,opts.user);
}
apiPromise.then(function(flowId) {
return resolve({rev:flowId});
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);
return reject(err);
});
throw err
});
});
},
@ -110,13 +105,12 @@ 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) {
addFlow: async function(opts) {
return mutex.runExclusive(async function() {
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);
return resolve(id);
return id;
}).catch(function (err) {
runtime.log.audit({
event: "flow.add",
@ -124,8 +118,7 @@ var api = module.exports = {
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
})
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) {
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 resolve(flow);
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;
return reject(err);
throw err;
}
})
},
/**
* Updates an existing flow configuration
@ -164,31 +155,19 @@ 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) {
updateFlow: async function(opts) {
return mutex.runExclusive(async function() {
var flow = opts.flow;
var id = opts.id;
try {
runtime.flows.updateFlow(id, flow, opts.user).then(function () {
return runtime.flows.updateFlow(id, flow, opts.user).then(function () {
runtime.log.audit({event: "flow.update", id: id}, opts.req);
return resolve(id);
return 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",
@ -196,10 +175,9 @@ var api = module.exports = {
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
}
}
});
throw err;
})
});
},
/**
@ -211,25 +189,13 @@ 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 () {
return 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
@ -244,9 +210,8 @@ var api = module.exports = {
message: err.toString()
}, opts.req);
err.status = 400;
return reject(err);
}
}
throw err;
});
});
},
@ -261,12 +226,11 @@ var api = module.exports = {
* @return {Promise<Object>} - the safe credentials
* @memberof @node-red/runtime_flows
*/
getNodeCredentials: function(opts) {
return new Promise(function(resolve,reject) {
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 resolve({});
return {};
}
var sendCredentials = {};
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 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');