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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,33 +6,35 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.property = n.property||"payload"; this.property = n.property||"payload";
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property); var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) { if (value !== undefined) {
if (typeof value === "string") { if (typeof value === "string") {
try { try {
value = yaml.load(value); value = yaml.load(value);
RED.util.setMessageProperty(msg,node.property,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") { else if (typeof value === "object") {
if (!Buffer.isBuffer(value)) { if (!Buffer.isBuffer(value)) {
try { try {
value = yaml.dump(value); value = yaml.dump(value);
RED.util.setMessageProperty(msg,node.property,value); RED.util.setMessageProperty(msg,node.property,value);
node.send(msg); send(msg);
done();
} }
catch(e) { 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); RED.nodes.registerType("yaml",YAMLNode);