1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
This commit is contained in:
Rory A. Svage 2020-08-07 16:44:52 -04:00
parent ec368ae3fd
commit f81cee0be2
3 changed files with 117 additions and 88 deletions

View File

@ -27,6 +27,7 @@
], ],
"dependencies": { "dependencies": {
"ajv": "6.12.3", "ajv": "6.12.3",
"async-mutex": "0.2.4",
"basic-auth": "2.0.1", "basic-auth": "2.0.1",
"bcryptjs": "2.4.3", "bcryptjs": "2.4.3",
"body-parser": "1.19.0", "body-parser": "1.19.0",

View File

@ -34,6 +34,8 @@
*/ */
var runtime; var runtime;
var Mutex = require('async-mutex').Mutex;
const mutex = new Mutex();
var api = module.exports = { var api = module.exports = {
init: function(_runtime) { init: function(_runtime) {
@ -64,6 +66,7 @@ var api = module.exports = {
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
setFlows: function(opts) { setFlows: function(opts) {
return mutex.runExclusive(function() {
return new Promise(function(resolve,reject) { return new Promise(function(resolve,reject) {
var flows = opts.flows; var flows = opts.flows;
@ -95,6 +98,7 @@ var api = module.exports = {
return reject(err); return reject(err);
}); });
}); });
});
}, },
/** /**
@ -107,19 +111,23 @@ var api = module.exports = {
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
addFlow: function(opts) { addFlow: function(opts) {
return new Promise(function(resolve,reject) { return mutex.runExclusive(function() {
return new Promise(function (resolve, reject) {
var flow = opts.flow; var flow = opts.flow;
runtime.nodes.addFlow(flow).then(function(id) { runtime.nodes.addFlow(flow).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 resolve(id);
}).catch(function(err) { }).catch(function (err) {
runtime.log.audit({event: "flow.add",error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({
event: "flow.add",
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400; err.status = 400;
return reject(err); return reject(err);
}) })
}) })
});
}, },
/** /**
@ -145,7 +153,6 @@ var api = module.exports = {
return reject(err); return reject(err);
} }
}) })
}, },
/** /**
* Updates an existing flow configuration * Updates an existing flow configuration
@ -158,33 +165,42 @@ var api = module.exports = {
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
updateFlow: function(opts) { updateFlow: function(opts) {
return new Promise(function (resolve,reject) { return mutex.runExclusive(function() {
return new Promise(function (resolve, reject) {
var flow = opts.flow; var flow = opts.flow;
var id = opts.id; var id = opts.id;
try { try {
runtime.nodes.updateFlow(id,flow).then(function() { runtime.nodes.updateFlow(id, flow).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 resolve(id);
}).catch(function(err) { }).catch(function (err) {
runtime.log.audit({event: "flow.update",error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({
event: "flow.update",
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400; err.status = 400;
return reject(err); return reject(err);
}) })
} catch(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); return reject(err);
} else { } else {
runtime.log.audit({event: "flow.update",error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({
event: "flow.update",
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400; err.status = 400;
return reject(err); return reject(err);
} }
} }
}); });
});
}, },
/** /**
* Deletes a flow * Deletes a flow
@ -196,31 +212,43 @@ var api = module.exports = {
* @memberof @node-red/runtime_flows * @memberof @node-red/runtime_flows
*/ */
deleteFlow: function(opts) { deleteFlow: function(opts) {
return new Promise(function (resolve,reject) { return mutex.runExclusive(function() {
return new Promise(function (resolve, reject) {
var id = opts.id; var id = opts.id;
try { try {
runtime.nodes.removeFlow(id).then(function() { runtime.nodes.removeFlow(id).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); runtime.log.audit({
event: "flow.remove",
id: id,
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400; err.status = 400;
return reject(err); return reject(err);
}); });
} catch(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
err.status = 404; err.status = 404;
err.code = "not_found"; err.code = "not_found";
return reject(err); return reject(err);
} else { } else {
runtime.log.audit({event: "flow.remove",id:id,error:err.code||"unexpected_error",message:err.toString()}, opts.req); runtime.log.audit({
event: "flow.remove",
id: id,
error: err.code || "unexpected_error",
message: err.toString()
}, opts.req);
err.status = 400; err.status = 400;
return reject(err); return reject(err);
} }
} }
}); });
});
}, },
/** /**
@ -264,5 +292,4 @@ var api = module.exports = {
resolve(sendCredentials); resolve(sendCredentials);
}) })
} }
} }

View File

@ -18,6 +18,7 @@
"dependencies": { "dependencies": {
"@node-red/registry": "1.1.3", "@node-red/registry": "1.1.3",
"@node-red/util": "1.1.3", "@node-red/util": "1.1.3",
"async-mutex": "0.2.4",
"clone": "2.1.2", "clone": "2.1.2",
"express": "4.17.1", "express": "4.17.1",
"fs-extra": "8.1.0", "fs-extra": "8.1.0",