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);
|
|
|
|
var node = this;
|
|
|
|
this.on("input", function(msg) {
|
2015-03-31 14:18:25 +02:00
|
|
|
if (msg.hasOwnProperty("payload")) {
|
|
|
|
if (Buffer.isBuffer(msg.payload)) {
|
|
|
|
var l = msg.payload.length;
|
|
|
|
try {
|
|
|
|
msg.payload = msgpack.decode(msg.payload);
|
|
|
|
node.send(msg);
|
|
|
|
node.status({text:l +" b->o "+ JSON.stringify(msg.payload).length});
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
var le = JSON.stringify(msg.payload).length;
|
|
|
|
msg.payload = msgpack.encode(msg.payload);
|
|
|
|
node.status({text:le +" o->b "+ msg.payload.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);
|
|
|
|
}
|