1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00
node-red-nodes/parsers/msgpack/70-msgpack.js
2017-01-29 17:45:44 +00:00

35 lines
1.2 KiB
JavaScript

module.exports = function(RED) {
"use strict";
var msgpack = require('msgpack-js');
function MsgPackNode(n) {
RED.nodes.createNode(this,n);
var node = this;
this.on("input", function(msg) {
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"});
}
}
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});
}
}
else { node.warn("No payload found to process"); }
});
}
RED.nodes.registerType("msgpack",MsgPackNode);
}