Merge pull request #2653 from node-red-hitachi/dev-messaging-api-cherrypick

Messaging API support of core nodes (cherrypicked)
This commit is contained in:
Nick O'Leary 2020-07-23 15:19:52 +01:00 committed by GitHub
commit a004c61afc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 87 additions and 64 deletions

View File

@ -98,7 +98,7 @@ module.exports = function(RED) {
node.repeaterSetup();
}
this.on("input", function(msg) {
this.on("input", function(msg, send, done) {
var errors = [];
this.props.forEach(p => {
@ -128,9 +128,10 @@ module.exports = function(RED) {
});
if (errors.length) {
node.error(errors.join('; '), msg);
done(errors.join('; '));
} else {
node.send(msg);
send(msg);
done();
}
});
}

View File

@ -21,8 +21,9 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
var node = this;
this.scope = n.scope;
this.on("input",function(msg) {
this.send(msg);
this.on("input",function(msg, send, done) {
send(msg);
done();
});
}

View File

@ -22,8 +22,9 @@ module.exports = function(RED) {
var node = this;
this.scope = n.scope;
this.uncaught = n.uncaught;
this.on("input",function(msg) {
this.send(msg);
this.on("input",function(msg, send, done) {
send(msg);
done();
});
}

View File

@ -21,8 +21,9 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
var node = this;
this.scope = n.scope;
this.on("input", function(msg) {
this.send(msg);
this.on("input", function(msg, send, done) {
send(msg);
done();
});
}

View File

@ -26,8 +26,9 @@ module.exports = function(RED) {
node.receive(msg);
}
RED.events.on(event,handler);
this.on("input", function(msg) {
this.send(msg);
this.on("input", function(msg, send, done) {
send(msg);
done();
});
this.on("close",function() {
RED.events.removeListener(event,handler);
@ -40,10 +41,11 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
var node = this;
var event = "node:"+n.id;
this.on("input", function(msg) {
this.on("input", function(msg, send, done) {
msg._event = event;
RED.events.emit(event,msg)
this.send(msg);
send(msg);
done();
});
}
RED.nodes.registerType("link out",LinkOutNode);

View File

@ -27,7 +27,7 @@ module.exports = function(RED) {
this.property = n.property||"payload";
var node = this;
this.on('input', function (msg) {
this.on('input', function (msg, send, done) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
var n = Number(value);
@ -43,11 +43,12 @@ module.exports = function(RED) {
value = ((n - node.minin) / (node.maxin - node.minin) * (node.maxout - node.minout)) + node.minout;
if (node.round) { value = Math.round(value); }
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
send(msg);
}
else { node.log(RED._("range.errors.notnumber")+": "+value); }
}
else { node.send(msg); } // If no payload - just pass it on.
else { send(msg); } // If no payload - just pass it on.
done();
});
}
RED.nodes.registerType("range", RangeNode);

View File

@ -107,7 +107,7 @@ module.exports = function(RED) {
var node = this;
function output(msg,value) {
function output(msg,value,send,done) {
/* istanbul ignore else */
if (node.outputFormat === "json") {
value = JSON.parse(value);
@ -119,22 +119,24 @@ module.exports = function(RED) {
if (node.fieldType === 'msg') {
RED.util.setMessageProperty(msg, node.field, value);
node.send(msg);
send(msg);
done();
} else if ((node.fieldType === 'flow') ||
(node.fieldType === 'global')) {
var context = RED.util.parseContextStore(node.field);
var target = node.context()[node.fieldType];
target.set(context.key, value, context.store, function (err) {
if (err) {
node.error(err, msg);
done(err);
} else {
node.send(msg);
send(msg);
done();
}
});
}
}
node.on("input", function(msg) {
node.on("input", function(msg, send, done) {
try {
/***
@ -179,16 +181,16 @@ module.exports = function(RED) {
Promise.all(promises).then(function() {
var value = mustache.render(template, new NodeContext(msg, node.context(), null, is_json, resolvedTokens));
output(msg, value);
output(msg, value, send, done);
}).catch(function (err) {
node.error(err.message,msg);
done(err.message);
});
} else {
output(msg, template);
output(msg, template, send, done);
}
}
catch(err) {
node.error(err.message, msg);
done(err.message);
}
});
}

View File

@ -281,7 +281,7 @@ module.exports = function(RED) {
var node = this;
this.headers = n.headers||{};
this.statusCode = n.statusCode;
this.on("input",function(msg) {
this.on("input",function(msg,_send,done) {
if (msg.res) {
var headers = RED.util.cloneMessage(node.headers);
if (msg.headers) {
@ -347,6 +347,7 @@ module.exports = function(RED) {
} else {
node.warn(RED._("httpin.errors.no-response"));
}
done();
});
}
RED.nodes.registerType("http response",HTTPOut);

View File

@ -26,7 +26,7 @@ module.exports = function(RED) {
this.ret = n.ret || "html";
this.as = n.as || "single";
var node = this;
this.on("input", function(msg) {
this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
var tag = node.tag;
@ -57,7 +57,7 @@ module.exports = function(RED) {
type: "string",
ch: ""
};
node.send(new_msg);
send(new_msg);
}
}
if (node.as === "single") {
@ -70,14 +70,15 @@ module.exports = function(RED) {
});
if (node.as === "single") { // Always return an array - even if blank
RED.util.setMessageProperty(msg,node.outproperty,pay);
node.send(msg);
send(msg);
}
done();
}
catch (error) {
node.error(error.message,msg);
done(error.message);
}
}
else { node.send(msg); } // If no payload - just pass it on.
else { send(msg); done(); } // If no payload - just pass it on.
});
}
RED.nodes.registerType("html",CheerioNode);

View File

@ -31,7 +31,7 @@ module.exports = function(RED) {
var node = this;
this.on("input", function(msg) {
this.on("input", function(msg,send,done) {
var validate = false;
if (msg.schema) {
// If input schema is different, re-compile it
@ -42,7 +42,7 @@ module.exports = function(RED) {
} catch(e) {
this.schema = null;
this.compiledSchema = null;
node.error(RED._("json.errors.schema-error-compile"), msg);
done(RED._("json.errors.schema-error-compile"));
return;
}
}
@ -57,28 +57,32 @@ module.exports = function(RED) {
if (validate) {
if (this.compiledSchema(msg[node.property])) {
delete msg.schema;
node.send(msg);
send(msg);
done();
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
node.send(msg);
send(msg);
done();
}
}
catch(e) { node.error(e.message,msg); }
catch(e) { done(e.message); }
} else {
// If node.action is str and value is str
if (validate) {
if (this.compiledSchema(JSON.parse(msg[node.property]))) {
delete msg.schema;
node.send(msg);
send(msg);
done();
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
node.send(msg);
send(msg);
done();
}
}
}
@ -90,37 +94,41 @@ module.exports = function(RED) {
if (this.compiledSchema(value)) {
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
delete msg.schema;
node.send(msg);
send(msg);
done();
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
node.send(msg);
send(msg);
done();
}
}
catch(e) { node.error(RED._("json.errors.dropped-error")); }
catch(e) { done(RED._("json.errors.dropped-error")); }
}
else { node.warn(RED._("json.errors.dropped-object")); }
else { node.warn(RED._("json.errors.dropped-object")); done(); }
} else {
// If node.action is obj and value is object
if (validate) {
if (this.compiledSchema(value)) {
delete msg.schema;
node.send(msg);
send(msg);
done();
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
node.send(msg);
send(msg);
done();
}
}
}
else { node.warn(RED._("json.errors.dropped")); }
else { node.warn(RED._("json.errors.dropped")); done(); }
}
else { node.send(msg); } // If no property - just pass it on.
else { send(msg); done(); } // If no property - just pass it on.
});
}
RED.nodes.registerType("json",JSONNode);

View File

@ -10,7 +10,7 @@ module.exports = function(RED) {
this.charkey = n.chr;
this.property = n.property||"payload";
var node = this;
this.on("input", function(msg) {
this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
var options;
@ -21,7 +21,8 @@ module.exports = function(RED) {
var builder = new xml2js.Builder(options);
value = builder.buildObject(value, options);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
send(msg);
done();
}
else if (typeof value == "string") {
options = {};
@ -30,17 +31,18 @@ module.exports = function(RED) {
options.attrkey = node.attrkey || options.attrkey || '$';
options.charkey = node.charkey || options.charkey || '_';
parseString(value, options, function (err, result) {
if (err) { node.error(err, msg); }
if (err) { done(err); }
else {
value = result;
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
send(msg);
done();
}
});
}
else { node.warn(RED._("xml.errors.xml_js")); }
else { node.warn(RED._("xml.errors.xml_js")); done(); }
}
else { node.send(msg); } // If no property - just pass it on.
else { send(msg); done(); } // If no property - just pass it on.
});
}
RED.nodes.registerType("xml",XMLNode);

View File

@ -6,33 +6,35 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
this.property = n.property||"payload";
var node = this;
this.on("input", function(msg) {
this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
if (typeof value === "string") {
try {
value = yaml.load(value);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
send(msg);
done();
}
catch(e) { node.error(e.message,msg); }
catch(e) { done(e.message); }
}
else if (typeof value === "object") {
if (!Buffer.isBuffer(value)) {
try {
value = yaml.dump(value);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
send(msg);
done();
}
catch(e) {
node.error(RED._("yaml.errors.dropped-error"));
done(RED._("yaml.errors.dropped-error"));
}
}
else { node.warn(RED._("yaml.errors.dropped-object")); }
else { node.warn(RED._("yaml.errors.dropped-object")); done(); }
}
else { node.warn(RED._("yaml.errors.dropped")); }
else { node.warn(RED._("yaml.errors.dropped")); done(); }
}
else { node.send(msg); } // If no payload - just pass it on.
else { send(msg); done(); } // If no payload - just pass it on.
});
}
RED.nodes.registerType("yaml",YAMLNode);