1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Fix msgpack node to handle more than just objects...

Fix to close Issue #482 and #483
This commit is contained in:
Dave C-J 2014-11-09 14:28:09 +00:00
parent 8b689ca09b
commit 7474905b08
3 changed files with 8 additions and 13 deletions

View File

@ -24,9 +24,8 @@
<script type="text/x-red" data-help-name="msgpack">
<p>A function that converts the <b>msg.payload</b> to and from <a href = "https://github.com/msgpack/msgpack-node"><i>msgpack</i></a> format.</p>
<p>If the input is an object it converts it to a msgpack buffer.</p>
<p>If the input is a msgpack buffer it converts it back to an object.</p>
<p>Current only supports object type.</p>
<p>If the input is NOT a buffer it tries to convert it to a msgpack buffer.</p>
<p>If the input is a msgpack buffer it tries to decode it back.</p>
</script>
<script type="text/javascript">

View File

@ -24,26 +24,22 @@ module.exports = function(RED) {
this.on("input", function(msg) {
if (Buffer.isBuffer(msg.payload)) {
var l = msg.payload.length;
msg.payload = msgpack.decode(msg.payload);
if (typeof msg.payload === "object") {
try {
msg.payload = msgpack.decode(msg.payload);
node.send(msg);
node.status({text:l +" b->o "+ JSON.stringify(msg.payload).length});
}
else {
node.warn("Input not a MsgPack buffer");
catch (e) {
node.warn("Bad decode: "+e);
node.status({text:"not a msgpack buffer"});
}
}
else if (typeof msg.payload === "object") {
else {
var l = JSON.stringify(msg.payload).length;
msg.payload = msgpack.encode(msg.payload);
node.send(msg);
node.status({text:l +" o->b "+ msg.payload.length});
}
else {
node.warn("This node only handles js objects or msgpack buffers.");
node.status({text:"error"});
}
});
}
RED.nodes.registerType("msgpack",MsgPackNode);

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-msgpack",
"version" : "0.0.2",
"version" : "0.0.3",
"description" : "A Node-RED node to pack and unpack objects to msgpack format",
"dependencies" : {
"msgpack-js" : "0.3.0"