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"> <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>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 NOT a buffer it tries to convert it to a msgpack buffer.</p>
<p>If the input is a msgpack buffer it converts it back to an object.</p> <p>If the input is a msgpack buffer it tries to decode it back.</p>
<p>Current only supports object type.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

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

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-msgpack", "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", "description" : "A Node-RED node to pack and unpack objects to msgpack format",
"dependencies" : { "dependencies" : {
"msgpack-js" : "0.3.0" "msgpack-js" : "0.3.0"