2014-09-17 22:02:23 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2017-08-09 17:58:15 +02:00
|
|
|
var msgpack = require('msgpack-lite');
|
2014-09-17 22:02:23 +02:00
|
|
|
|
|
|
|
function MsgPackNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2018-01-31 12:18:30 +01:00
|
|
|
this.property = n.property||"payload";
|
2014-09-17 22:02:23 +02:00
|
|
|
var node = this;
|
|
|
|
this.on("input", function(msg) {
|
2018-01-31 12:18:30 +01:00
|
|
|
var value = RED.util.getMessageProperty(msg,node.property);
|
|
|
|
if (value !== undefined) {
|
|
|
|
if (Buffer.isBuffer(value)) {
|
|
|
|
var l = value.length;
|
2015-03-31 14:18:25 +02:00
|
|
|
try {
|
2018-01-31 12:18:30 +01:00
|
|
|
value = msgpack.decode(value);
|
|
|
|
RED.util.setMessageProperty(msg,node.property,value);
|
2015-03-31 14:18:25 +02:00
|
|
|
node.send(msg);
|
2018-01-31 12:18:30 +01:00
|
|
|
node.status({text:l +" b->o "+ JSON.stringify(value).length});
|
2015-03-31 14:18:25 +02:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-08-09 17:58:15 +02:00
|
|
|
node.error("Bad decode",msg);
|
2015-03-31 14:18:25 +02:00
|
|
|
node.status({text:"not a msgpack buffer"});
|
|
|
|
}
|
2014-09-17 22:02:23 +02:00
|
|
|
}
|
2015-03-31 14:18:25 +02:00
|
|
|
else {
|
2018-01-31 12:18:30 +01:00
|
|
|
var le = JSON.stringify(value).length;
|
|
|
|
value = msgpack.encode(value);
|
|
|
|
RED.util.setMessageProperty(msg,node.property,value);
|
|
|
|
node.status({text:le +" o->b "+ value.length});
|
2017-08-09 17:58:15 +02:00
|
|
|
node.send(msg);
|
2014-09-17 22:02:23 +02:00
|
|
|
}
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else { node.warn("No payload found to process"); }
|
2014-09-17 22:02:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("msgpack",MsgPackNode);
|
|
|
|
}
|