2014-09-17 22:02:23 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2014-10-02 20:38:23 +02:00
|
|
|
var msgpack = require('msgpack-js');
|
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) {
|
|
|
|
node.warn("Bad decode: "+e);
|
|
|
|
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.send(msg);
|
|
|
|
node.status({text:le +" o->b "+ msg.payload.length});
|
2014-09-17 22:02:23 +02:00
|
|
|
}
|
2015-03-31 14:25:59 +02:00
|
|
|
} else { node.warn("No payload found to process"); }
|
2014-09-17 22:02:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("msgpack",MsgPackNode);
|
|
|
|
}
|