2016-11-10 22:29:07 +01:00
|
|
|
|
2016-11-10 22:22:05 +01:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var yaml = require('js-yaml');
|
|
|
|
function YAMLNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2018-01-16 11:59:44 +01:00
|
|
|
this.property = n.property||"payload";
|
2016-11-10 22:22:05 +01:00
|
|
|
var node = this;
|
|
|
|
this.on("input", function(msg) {
|
2018-01-16 11:59:44 +01:00
|
|
|
var value = RED.util.getMessageProperty(msg,node.property);
|
|
|
|
if (value !== undefined) {
|
|
|
|
if (typeof value === "string") {
|
2016-11-10 22:22:05 +01:00
|
|
|
try {
|
2018-01-16 11:59:44 +01:00
|
|
|
value = yaml.load(value);
|
|
|
|
RED.util.setMessageProperty(msg,node.property,value);
|
2016-11-10 22:22:05 +01:00
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
catch(e) { node.error(e.message,msg); }
|
|
|
|
}
|
2018-01-16 11:59:44 +01:00
|
|
|
else if (typeof value === "object") {
|
|
|
|
if (!Buffer.isBuffer(value)) {
|
2016-11-10 22:22:05 +01:00
|
|
|
try {
|
2018-01-16 11:59:44 +01:00
|
|
|
value = yaml.dump(value);
|
|
|
|
RED.util.setMessageProperty(msg,node.property,value);
|
2016-11-10 22:22:05 +01:00
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
node.error(RED._("yaml.errors.dropped-error"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { node.warn(RED._("yaml.errors.dropped-object")); }
|
|
|
|
}
|
|
|
|
else { node.warn(RED._("yaml.errors.dropped")); }
|
|
|
|
}
|
|
|
|
else { node.send(msg); } // If no payload - just pass it on.
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("yaml",YAMLNode);
|
|
|
|
};
|