forked from node-red/node-red-nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
70-msgpack.js
38 lines (36 loc) · 1.42 KB
/
70-msgpack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module.exports = function(RED) {
"use strict";
var msgpack = require('msgpack-lite');
function MsgPackNode(n) {
RED.nodes.createNode(this,n);
this.property = n.property||"payload";
var node = this;
this.on("input", function(msg) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
if (Buffer.isBuffer(value)) {
var l = value.length;
try {
value = msgpack.decode(value);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
node.status({text:l +" b->o "+ JSON.stringify(value).length});
}
catch (e) {
node.error("Bad decode",msg);
node.status({text:"not a msgpack buffer"});
}
}
else {
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});
node.send(msg);
}
}
else { node.warn("No payload found to process"); }
});
}
RED.nodes.registerType("msgpack",MsgPackNode);
}