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

Add async context support to Change node

This commit is contained in:
Nick O'Leary 2018-07-07 22:23:26 +01:00
parent afb566b6b4
commit 1b693eed37
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 163 additions and 123 deletions

View File

@ -99,158 +99,175 @@ module.exports = function(RED) {
} }
function applyRule(msg,rule) { function applyRule(msg,rule) {
try { return new Promise(function(resolve, reject){
var property = rule.p; try {
var value = rule.to; var property = rule.p;
if (rule.tot === 'json') { var value = rule.to;
value = JSON.parse(rule.to); if (rule.tot === 'json') {
} else if (rule.tot === 'bin') { value = JSON.parse(rule.to);
value = Buffer.from(JSON.parse(rule.to)) } else if (rule.tot === 'bin') {
} value = Buffer.from(JSON.parse(rule.to))
var current;
var fromValue;
var fromType;
var fromRE;
if (rule.tot === "msg") {
value = RED.util.getMessageProperty(msg,rule.to);
} else if (rule.tot === 'flow') {
value = node.context().flow.get(rule.to);
} else if (rule.tot === 'global') {
value = node.context().global.get(rule.to);
} else if (rule.tot === 'date') {
value = Date.now();
} else if (rule.tot === 'jsonata') {
try{
value = RED.util.evaluateJSONataExpression(rule.to,msg);
} catch(err) {
node.error(RED._("change.errors.invalid-expr",{error:err.message}),msg);
return;
} }
} var current;
if (rule.t === 'change') { var fromValue;
if (rule.fromt === 'msg' || rule.fromt === 'flow' || rule.fromt === 'global') { var fromType;
if (rule.fromt === "msg") { var fromRE;
fromValue = RED.util.getMessageProperty(msg,rule.from); if (rule.tot === "msg") {
} else if (rule.fromt === 'flow') { value = RED.util.getMessageProperty(msg,rule.to);
fromValue = node.context().flow.get(rule.from); } else if (rule.tot === 'flow') {
} else if (rule.fromt === 'global') { value = node.context().flow.get(rule.to);
fromValue = node.context().global.get(rule.from); } else if (rule.tot === 'global') {
value = node.context().global.get(rule.to);
} else if (rule.tot === 'date') {
value = Date.now();
} else if (rule.tot === 'jsonata') {
try{
value = RED.util.evaluateJSONataExpression(rule.to,msg);
} catch(err) {
node.error(RED._("change.errors.invalid-expr",{error:err.message}),msg);
return;
} }
if (typeof fromValue === 'number' || fromValue instanceof Number) { }
fromType = 'num'; if (rule.t === 'change') {
} else if (typeof fromValue === 'boolean') { if (rule.fromt === 'msg' || rule.fromt === 'flow' || rule.fromt === 'global') {
fromType = 'bool' if (rule.fromt === "msg") {
} else if (fromValue instanceof RegExp) { fromValue = RED.util.getMessageProperty(msg,rule.from);
fromType = 're'; } else if (rule.fromt === 'flow') {
fromRE = fromValue; fromValue = node.context().flow.get(rule.from);
} else if (typeof fromValue === 'string') { } else if (rule.fromt === 'global') {
fromType = 'str'; fromValue = node.context().global.get(rule.from);
fromRE = fromValue.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); }
try { if (typeof fromValue === 'number' || fromValue instanceof Number) {
fromRE = new RegExp(fromRE, "g"); fromType = 'num';
} catch (e) { } else if (typeof fromValue === 'boolean') {
valid = false; fromType = 'bool'
node.error(RED._("change.errors.invalid-from",{error:e.message}),msg); } else if (fromValue instanceof RegExp) {
return; fromType = 're';
fromRE = fromValue;
} else if (typeof fromValue === 'string') {
fromType = 'str';
fromRE = fromValue.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
try {
fromRE = new RegExp(fromRE, "g");
} catch (e) {
valid = false;
reject(RED._("change.errors.invalid-from",{error:e.message}));
return;
}
} else {
reject(RED._("change.errors.invalid-from",{error:"unsupported type: "+(typeof fromValue)}));
return
} }
} else { } else {
node.error(RED._("change.errors.invalid-from",{error:"unsupported type: "+(typeof fromValue)}),msg); fromType = rule.fromt;
return fromValue = rule.from;
} fromRE = rule.fromRE;
} else {
fromType = rule.fromt;
fromValue = rule.from;
fromRE = rule.fromRE;
}
}
if (rule.pt === 'msg') {
if (rule.t === 'delete') {
RED.util.setMessageProperty(msg,property,undefined);
} else if (rule.t === 'set') {
RED.util.setMessageProperty(msg,property,value);
} else if (rule.t === 'change') {
current = RED.util.getMessageProperty(msg,property);
if (typeof current === 'string') {
if ((fromType === 'num' || fromType === 'bool' || fromType === 'str') && current === fromValue) {
// str representation of exact from number/boolean
// only replace if they match exactly
RED.util.setMessageProperty(msg,property,value);
} else {
current = current.replace(fromRE,value);
RED.util.setMessageProperty(msg,property,current);
}
} else if ((typeof current === 'number' || current instanceof Number) && fromType === 'num') {
if (current == Number(fromValue)) {
RED.util.setMessageProperty(msg,property,value);
}
} else if (typeof current === 'boolean' && fromType === 'bool') {
if (current.toString() === fromValue) {
RED.util.setMessageProperty(msg,property,value);
}
} }
} }
} if (rule.pt === 'msg') {
else {
var target;
if (rule.pt === 'flow') {
target = node.context().flow;
} else if (rule.pt === 'global') {
target = node.context().global;
}
if (target) {
if (rule.t === 'delete') { if (rule.t === 'delete') {
target.set(property,undefined); RED.util.setMessageProperty(msg,property,undefined);
} else if (rule.t === 'set') { } else if (rule.t === 'set') {
target.set(property,value); RED.util.setMessageProperty(msg,property,value);
} else if (rule.t === 'change') { } else if (rule.t === 'change') {
current = target.get(property); current = RED.util.getMessageProperty(msg,property);
if (typeof current === 'string') { if (typeof current === 'string') {
if ((fromType === 'num' || fromType === 'bool' || fromType === 'str') && current === fromValue) { if ((fromType === 'num' || fromType === 'bool' || fromType === 'str') && current === fromValue) {
// str representation of exact from number/boolean // str representation of exact from number/boolean
// only replace if they match exactly // only replace if they match exactly
target.set(property,value); RED.util.setMessageProperty(msg,property,value);
} else { } else {
current = current.replace(fromRE,value); current = current.replace(fromRE,value);
target.set(property,current); RED.util.setMessageProperty(msg,property,current);
} }
} else if ((typeof current === 'number' || current instanceof Number) && fromType === 'num') { } else if ((typeof current === 'number' || current instanceof Number) && fromType === 'num') {
if (current == Number(fromValue)) { if (current == Number(fromValue)) {
target.set(property,value); RED.util.setMessageProperty(msg,property,value);
} }
} else if (typeof current === 'boolean' && fromType === 'bool') { } else if (typeof current === 'boolean' && fromType === 'bool') {
if (current.toString() === fromValue) { if (current.toString() === fromValue) {
target.set(property,value); RED.util.setMessageProperty(msg,property,value);
} }
} }
} }
} }
else {
var target;
if (rule.pt === 'flow') {
target = node.context().flow;
} else if (rule.pt === 'global') {
target = node.context().global;
}
if (target) {
if (rule.t === 'delete') {
target.set(property,undefined);
} else if (rule.t === 'set') {
target.set(property,value);
} else if (rule.t === 'change') {
current = target.get(property);
if (typeof current === 'string') {
if ((fromType === 'num' || fromType === 'bool' || fromType === 'str') && current === fromValue) {
// str representation of exact from number/boolean
// only replace if they match exactly
target.set(property,value);
} else {
current = current.replace(fromRE,value);
target.set(property,current);
}
} else if ((typeof current === 'number' || current instanceof Number) && fromType === 'num') {
if (current == Number(fromValue)) {
target.set(property,value);
}
} else if (typeof current === 'boolean' && fromType === 'bool') {
if (current.toString() === fromValue) {
target.set(property,value);
}
}
}
}
}
} catch(err) {/*console.log(err.stack)*/}
resolve(msg);
});
}
function applyRules(msg, currentRule) {
var r = node.rules[currentRule];
var rulePromise;
if (r.t === "move") {
if ((r.tot !== r.pt) || (r.p.indexOf(r.to) !== -1)) {
rulePromise = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:r.p, tot:r.pt}).then(
msg => applyRule(msg,{t:"delete", p:r.p, pt:r.pt})
);
} }
} catch(err) {/*console.log(err.stack)*/} else { // 2 step move if we are moving from a child
return msg; rulePromise = applyRule(msg,{t:"set", p:"_temp_move", pt:r.tot, to:r.p, tot:r.pt}).then(
msg => applyRule(msg,{t:"delete", p:r.p, pt:r.pt})
).then(
msg => applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:"_temp_move", tot:r.pt})
).then(
msg => applyRule(msg,{t:"delete", p:"_temp_move", pt:r.pt})
)
}
} else {
rulePromise = applyRule(msg,r);
}
return rulePromise.then(
msg => {
if (!msg) {
return
} else if (currentRule === node.rules.length - 1) {
return msg;
} else {
return applyRules(msg, currentRule+1);
}
}
);
} }
if (valid) { if (valid) {
this.on('input', function(msg) { this.on('input', function(msg) {
for (var i=0; i<this.rules.length; i++) { applyRules(msg, 0)
if (this.rules[i].t === "move") { .then( msg => { if (msg) { node.send(msg) }} )
var r = this.rules[i]; .catch( err => node.error(err, msg))
if ((r.tot !== r.pt) || (r.p.indexOf(r.to) !== -1)) {
msg = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:r.p, tot:r.pt});
applyRule(msg,{t:"delete", p:r.p, pt:r.pt});
}
else { // 2 step move if we are moving from a child
msg = applyRule(msg,{t:"set", p:"_temp_move", pt:r.tot, to:r.p, tot:r.pt});
applyRule(msg,{t:"delete", p:r.p, pt:r.pt});
msg = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:"_temp_move", tot:r.pt});
applyRule(msg,{t:"delete", p:"_temp_move", pt:r.pt});
}
} else {
msg = applyRule(msg,this.rules[i]);
}
if (msg === null) {
return;
}
}
node.send(msg);
}); });
} }
} }

View File

@ -15,6 +15,7 @@
**/ **/
var should = require("should"); var should = require("should");
var sinon = require("sinon");
var changeNode = require("../../../../nodes/core/logic/15-change.js"); var changeNode = require("../../../../nodes/core/logic/15-change.js");
var helper = require("node-red-node-test-helper"); var helper = require("node-red-node-test-helper");
@ -454,6 +455,28 @@ describe('change Node', function() {
}); });
}); });
it('reports invalid jsonata expression', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"$invalid(payload)","tot":"jsonata"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
sinon.spy(changeNode1,"error");
helperNode1.on("input", function(msg) {
done("Invalid jsonata expression passed message through");
});
changeNode1.receive({payload:"Hello World!"});
setTimeout(function() {
try {
changeNode1.error.called.should.be.true();
done();
} catch(err) {
done(err);
}
},50);
});
});
}); });
describe('#change', function() { describe('#change', function() {
it('changes the value of the message property', function(done) { it('changes the value of the message property', function(done) {