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

Move msg pack node to more supported lib msgpack-lite

This commit is contained in:
Dave Conway-Jones 2017-08-09 16:58:15 +01:00
parent dbff575e24
commit 7950b7d39a
No known key found for this signature in database
GPG Key ID: 81B04231572A9A2D
6 changed files with 21 additions and 9 deletions

View File

@ -35,6 +35,6 @@
"poplib": "^0.1.7", "poplib": "^0.1.7",
"mailparser": "^0.6.1", "mailparser": "^0.6.1",
"imap": "^0.8.19", "imap": "^0.8.19",
"msgpack-js": "^0.3.0" "msgpack-lite": "^0.1.26"
} }
} }

View File

@ -7,9 +7,12 @@
</script> </script>
<script type="text/x-red" data-help-name="msgpack"> <script type="text/x-red" data-help-name="msgpack">
<p>A function that converts the <code>msg.payload</code> to and from <a href = "https://github.com/msgpack/msgpack-node"><i>msgpack</i></a> format.</p> <p>A function that converts the <code>msg.payload</code> to and from
<a href = "http://msgpack.org/"><i>msgpack</i></a> format.</p>
<p>If the input is NOT a buffer it tries to convert 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 tries to decode it back.</p> <p>If the input is a msgpack buffer it tries to decode it back.</p>
<p><b>Note</b>: this node does not currently encode raw <code>buffer</code> types.
It will automatically try to <i>decode</i> any buffer received, and may not cause an error.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -1,7 +1,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var msgpack = require('msgpack-js'); var msgpack = require('msgpack-lite');
function MsgPackNode(n) { function MsgPackNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -16,15 +16,15 @@ module.exports = function(RED) {
node.status({text:l +" b->o "+ JSON.stringify(msg.payload).length}); node.status({text:l +" b->o "+ JSON.stringify(msg.payload).length});
} }
catch (e) { catch (e) {
node.warn("Bad decode: "+e); node.error("Bad decode",msg);
node.status({text:"not a msgpack buffer"}); node.status({text:"not a msgpack buffer"});
} }
} }
else { else {
var le = JSON.stringify(msg.payload).length; var le = JSON.stringify(msg.payload).length;
msg.payload = msgpack.encode(msg.payload); msg.payload = msgpack.encode(msg.payload);
node.send(msg);
node.status({text:le +" o->b "+ msg.payload.length}); node.status({text:le +" o->b "+ msg.payload.length});
node.send(msg);
} }
} }
else { node.warn("No payload found to process"); } else { node.warn("No payload found to process"); }

View File

@ -10,11 +10,19 @@ Run the following command in your Node-RED user directory - typically `~/.node-r
npm install node-red-node-msgpack npm install node-red-node-msgpack
Changes
-------
Version 1.0.0 - move to msgpack-lite library (more supported and faster) - This uses the more
recent msgpack specification so please ensure all your endpoints are also using the latest spec.
Usage Usage
----- -----
Uses the <a href="https://www.npmjs.org/package/msgpack">msgpack npm</a> to pack and unpack msg.payload objects to <a href="http://msgpack.org/">msgpack</a> format buffers. Uses the <a href="https://www.npmjs.org/package/msgpack-lite">msgpack-lite npm</a> to pack and unpack msg.payload objects to <a href="http://msgpack.org/">msgpack</a> format buffers.
**Note**: this node does not currently encode raw <code>buffer</code> types.
It will automatically try to *decode* any buffer received, and may not cause an error.
If the input is NOT a buffer it converts it into a msgpack buffer. If the input is NOT a buffer it converts it into a msgpack buffer.

View File

@ -1,9 +1,9 @@
{ {
"name" : "node-red-node-msgpack", "name" : "node-red-node-msgpack",
"version" : "0.0.6", "version" : "1.0.0",
"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-lite" : "^0.1.26"
}, },
"repository" : { "repository" : {
"type":"git", "type":"git",

View File

@ -66,6 +66,7 @@ describe('msgpack node', function() {
}); });
it('should error if the buffer fails to decode', function(done) { it('should error if the buffer fails to decode', function(done) {
buf[0] = 0x87;
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] }, var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() { helper.load(testNode, flow, function() {
@ -77,7 +78,7 @@ describe('msgpack node', function() {
setTimeout(function() { setTimeout(function() {
done(); done();
}, 25); }, 25);
n1.emit("input", {payload:Buffer("12345")}); n1.emit("input", {payload:buf});
}); });
}); });