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

Add cbor object packing node

and test
This commit is contained in:
Dave Conway-Jones 2022-07-22 10:18:02 +01:00
parent 6830ce4fba
commit 67611f6bb6
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF
7 changed files with 252 additions and 0 deletions

44
parsers/cbor/70-cbor.html Normal file
View File

@ -0,0 +1,44 @@
<script type="text/html" data-template-name="cbor">
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/html" data-help-name="cbor">
<p>A function that converts the <code>msg.payload</code> to and from <a href = "https://cbor.io"><i>cbor</i></a> format.</p>
<p>If the input is NOT a buffer it tries to convert it to a cbor buffer.</p>
<p>If the input is a cbor buffer it tries to decode it back.</p>
<p><b>Note</b>: this node does not currently encode raw <code>buffer</code> types.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('cbor',{
category: 'parser',
color:"#DEBD5C",
defaults: {
name: {value:""},
property: {value:"payload",required:true}
},
inputs:1,
outputs:1,
icon: "parser-cbor.png",
label: function() {
return this.name||"cbor";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
if (this.property === undefined) {
$("#node-input-property").val("payload");
}
$("#node-input-property").typedInput({default:'msg',types:['msg']});
}
});
</script>

38
parsers/cbor/70-cbor.js Normal file
View File

@ -0,0 +1,38 @@
module.exports = function(RED) {
"use strict";
var cbor = require('cbor-x');
function CborNode(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 = cbor.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 cbor buffer"});
}
}
else {
var le = JSON.stringify(value).length;
value = cbor.encode(value);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
node.status({text:le +" o->b "+ value.length});
}
}
else { node.warn("No payload found to process"); }
});
}
RED.nodes.registerType("cbor",CborNode);
}

14
parsers/cbor/LICENSE Normal file
View File

@ -0,0 +1,14 @@
Copyright 2016 JS Foundation and other contributors, https://js.foundation/
Copyright 2013-2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

27
parsers/cbor/README.md Normal file
View File

@ -0,0 +1,27 @@
node-red-node-cbor
==================
A <a href="http://nodered.org" target="_new">Node-RED</a> node to pack and unpack objects to cbor format buffers.
Install
-------
Run the following command in your Node-RED user directory - typically `~/.node-red`
npm install node-red-node-cbor
Changes
-------
Version 1.0.0 - move to cbor-x library (more supported and faster).
Usage
-----
Uses the <a href="https://www.npmjs.org/package/cbor-x">cbor-x npm</a> to pack and unpack msg.payload objects to <a href="https://cbor.io/">cbor</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 a msgpack buffer it converts it back to the original type.

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

28
parsers/cbor/package.json Normal file
View File

@ -0,0 +1,28 @@
{
"name" : "node-red-node-cbor",
"version" : "1.0.0",
"description" : "A Node-RED node to pack and unpack objects to cbor format",
"dependencies" : {
"cbor-x" : "^1.3.2"
},
"bundledDependencies": [
"cbor-x"
],
"repository" : {
"type":"git",
"url":"https://github.com/node-red/node-red-nodes.git",
"directory": "tree/master/parsers/cbor"
},
"license": "Apache-2.0",
"keywords": [ "node-red", "cbor" ],
"node-red" : {
"nodes" : {
"cbor": "70-cbor.js"
}
},
"author": {
"name": "Dave Conway-Jones",
"email": "dceejay@gmail.com",
"url": "http://nodered.org"
}
}

View File

@ -0,0 +1,101 @@
var should = require("should");
var helper = require("node-red-node-test-helper");
var testNode = require('../../../parsers/cbor/70-cbor.js');
describe('cbor node', function() {
"use strict";
beforeEach(function(done) {
helper.startServer(done);
});
afterEach(function(done) {
helper.unload().then(function() {
helper.stopServer(done);
});
});
it("should be loaded with correct defaults", function(done) {
var flow = [{"id":"n1", "type":"cbor", "name":"cbor1", "wires":[[]]}];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property("name", "cbor1");
done();
});
});
var buf;
it('should pack an object', function(done) {
var flow = [{"id":"n1", "type":"cbor", wires:[["n2"]] },
{id:"n2", type:"helper"}];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload");
msg.payload.should.be.a.Object;
msg.payload.should.have.length(47);
buf = msg.payload;
done();
});
n1.emit("input", {payload:{A:1, B:"string", C:true, D:[1,true,"string"], E:{Y:9,Z:"string"}}});
});
});
it('should unpack a Buffer', function(done) {
var flow = [{"id":"n1", "type":"cbor", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload");
msg.payload.should.have.a.property("A",1);
msg.payload.should.have.a.property("B",'string');
msg.payload.should.have.a.property("C",true);
msg.payload.should.have.a.property("D",[1,true,"string"]);
msg.payload.should.have.a.property("E");
msg.payload.E.should.have.a.property("Y",9);
msg.payload.E.should.have.a.property("Z","string");
done();
});
n1.emit("input", {payload:buf});
});
});
it('should error if the buffer fails to decode', function(done) {
buf[0] = 0x87;
var flow = [{"id":"n1", "type":"cbor", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here if there is an error.");
});
setTimeout(function() {
done();
}, 25);
n1.emit("input", {payload:buf});
});
});
it('ignore msg with no payload', function(done) {
var flow = [{"id":"n1", "type":"cbor", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here with no payload.");
});
setTimeout(function() {
done();
}, 25);
n1.emit("input", {topic:1});
});
});
});