node-red-contrib-buffer-parser
Advanced tools
Comparing version 3.2.0 to 3.2.1
@@ -538,3 +538,4 @@ | ||
const b = item.value.slice(0, _end); | ||
bufferExpectedLength += length; | ||
const _length = _end || b.length; | ||
bufferExpectedLength += _length; | ||
buf = appendBuffer(buf, b); | ||
@@ -541,0 +542,0 @@ } |
@@ -19,2 +19,3 @@ | ||
module.exports = function (RED) { | ||
const SUPPORTS_BIGINT = parseFloat(process.versions.node) >= 10.4; | ||
const RESULTYPEOPTS = ["object", "keyvalue", "value", "array", "buffer"]; | ||
@@ -346,10 +347,11 @@ const { setObjectProperty, bcd2number, byteToBits, wordToBits, isNumber, TYPEOPTS, SWAPOPTS } = require('./common-functions.js'); | ||
} | ||
function sanitizeMask(mask, throwError) { | ||
var _mask = mask | ||
function sanitizeMask(mask, numberFn, throwError) { | ||
let _mask = mask | ||
try { | ||
if (_mask) { | ||
if (typeof _mask == "string" && _mask.trim() != "") { | ||
_mask = Number(_mask) | ||
if (typeof _mask == "string" && _mask.trim() == "") { | ||
return 0; | ||
} | ||
if (isNaN(_mask)) { | ||
_mask = numberFn(_mask) | ||
if (isNaN(Number(_mask))) { | ||
if (throwError) throw new Error("mask " + mask + " is invalid") | ||
@@ -365,3 +367,4 @@ } | ||
function dataGetter(buffer, startByte, dataCount, bufferFunction, dataSize, mask, scaler) { | ||
var _mask = sanitizeMask(mask); | ||
const numberConvertor = bufferFunction.indexOf("BigInt") > 0 ? BigInt : Number | ||
const _mask = sanitizeMask(mask, numberConvertor, true); | ||
let index = 0; | ||
@@ -378,7 +381,9 @@ let value; | ||
} | ||
var fn = buffer[bufferFunction].bind(buffer); | ||
const fn = buffer[bufferFunction].bind(buffer); | ||
for (index = 0; index < dataCount; index++) { | ||
let bufPos = startByte + (index * dataSize); | ||
const bufPos = startByte + (index * dataSize); | ||
let val = fn(bufPos);//call specified function on the buffer | ||
if (_mask) val = (val & _mask); | ||
if (_mask != 0) { | ||
val = (val & _mask); | ||
} | ||
if (scaler && scaler.operator && scalingOps[scaler.operator]) { | ||
@@ -458,2 +463,5 @@ val = scalingOps[scaler.operator](val, scaler.operand); | ||
case 'bigint64le': | ||
if(!SUPPORTS_BIGINT) { | ||
throw new Error("BigInt operations require NODE v10.4.0 or greater") | ||
} | ||
itemReader(item, buf, "readBigInt64LE", 8); | ||
@@ -464,2 +472,5 @@ break; | ||
case 'bigint64be': | ||
if(!SUPPORTS_BIGINT) { | ||
throw new Error("BigInt operations require NODE v10.4.0 or greater") | ||
} | ||
itemReader(item, buf, "readBigInt64BE", 8); | ||
@@ -469,2 +480,5 @@ break; | ||
case 'biguint64le': | ||
if(!SUPPORTS_BIGINT) { | ||
throw new Error("BigInt operations require NODE v10.4.0 or greater") | ||
} | ||
itemReader(item, buf, "readBigUInt64LE", 8); | ||
@@ -474,2 +488,5 @@ break; | ||
case 'biguint64be': | ||
if(!SUPPORTS_BIGINT) { | ||
throw new Error("BigInt operations require NODE v10.4.0 or greater") | ||
} | ||
itemReader(item, buf, "readBigUInt64BE", 8); | ||
@@ -476,0 +493,0 @@ break; |
{ | ||
"name": "node-red-contrib-buffer-parser", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "Node-red nodes to convert values to and from buffer/array. Supports Big/Little Endian, BCD, byte swapping and much more", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -38,3 +38,4 @@ var should = require("should"); | ||
{ "name": "item26", "type": "hex", "length": 4, "dataType": "str", "data": "2626" }, | ||
{ "name": "item27", "type": "buffer", "length": 24, "dataType": "bin", "data": "[50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50]" } | ||
{ "name": "item27", "type": "buffer", "length": 24, "dataType": "bin", "data": "[50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55]" }, | ||
{ "name": "item28", "type": "buffer", "length": -1, "dataType": "bin", "data": "[50,55,50,55]" } | ||
], | ||
@@ -89,3 +90,3 @@ "swap1": "", "swap2": "", "swap3": "", "swap1Type": "swap", "swap2Type": "swap", "swap3Type": "swap", "msgPropertyType": "str", | ||
rp.should.instanceOf(Buffer); | ||
var expectedPayload = Buffer.from([1,254,3,252,255,255,251,6,0,7,0,248,255,255,255,255,255,255,247,10,0,0,0,0,0,0,11,0,80,113,15,228,255,255,255,255,255,255,225,187,101,108,0,0,12,119,66,3,0,0,0,0,0,0,3,126,17,214,0,253,74,129,65,65,137,95,173,159,29,121,247,81,246,116,60,60,65,179,143,43,255,224,32,101,101,101,101,101,35,35,36,36,50,53,38,38,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55]).toString("hex"); | ||
var expectedPayload = Buffer.from([1,254,3,252,255,255,251,6,0,7,0,248,255,255,255,255,255,255,247,10,0,0,0,0,0,0,11,0,80,113,15,228,255,255,255,255,255,255,225,187,101,108,0,0,12,119,66,3,0,0,0,0,0,0,3,126,17,214,0,253,74,129,65,65,137,95,173,159,29,121,247,81,246,116,60,60,65,179,143,43,255,224,32,101,101,101,101,101,35,35,36,36,50,53,38,38,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55,50,55]).toString("hex"); | ||
rp.toString("hex").should.equal(expectedPayload); | ||
@@ -92,0 +93,0 @@ done(); |
@@ -31,4 +31,4 @@ var should = require("should"); | ||
try { | ||
const n = helper.getNode('testNode'); | ||
n.should.have.property('name', 'test--buffer-parser'); | ||
const testNode = helper.getNode('testNode'); | ||
testNode.should.have.property('name', 'test--buffer-parser'); | ||
done(); | ||
@@ -41,2 +41,36 @@ } catch (error) { | ||
}); | ||
it('should make BigInt values with and without mask', done => { | ||
const flow = [{ id: 'helperNode1', type: 'helper' }, {"id":"testNode","type":"buffer-parser","name":"test--buffer-parser","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bigint64be","name":"MASK_00000001FFFFFFFF","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"0x00000001FFFFFFFF"},{"type":"bigint64be","name":"MASK_000001FFFFFFFFFF","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"0x000001FFFFFFFFFF"},{"type":"bigint64be","name":"MASK_0001FFFFFFFFFFFF","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"0x0001FFFFFFFFFFFF"},{"type":"bigint64be","name":"MASK_000FFFFFFFFFFFFF","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"0x000FFFFFFFFFFFFF"},{"type":"bigint64be","name":"NO_MASK","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"wires":[["helperNode1"]]}] | ||
helper.load(bufferParser, flow, () => { | ||
try { | ||
const testNode = helper.getNode('testNode'); | ||
const helperNode1 = helper.getNode("helperNode1"); | ||
helperNode1.on("input", function (msg) { | ||
try { | ||
msg.should.have.property("payload"); | ||
msg.payload.should.have.property("MASK_00000001FFFFFFFF"); | ||
msg.payload.should.have.property("MASK_000001FFFFFFFFFF"); | ||
msg.payload.should.have.property("MASK_0001FFFFFFFFFFFF"); | ||
msg.payload.should.have.property("MASK_000FFFFFFFFFFFFF"); | ||
msg.payload.should.have.property("NO_MASK"); | ||
msg.payload.MASK_00000001FFFFFFFF.should.eql(8589934591n); | ||
msg.payload.MASK_000001FFFFFFFFFF.should.eql(2199023255551n); | ||
msg.payload.MASK_0001FFFFFFFFFFFF.should.eql(562949953421311n); | ||
msg.payload.MASK_000FFFFFFFFFFFFF.should.eql(4503599627370495n); | ||
msg.payload.NO_MASK.should.eql(4503599627370495n); | ||
done(); | ||
} catch(err) { | ||
done(err); | ||
} | ||
}); | ||
testNode.should.have.property('name', 'test--buffer-parser'); | ||
testNode.receive({ payload: Buffer.from([0,15,255,255,255,255,255,255]) }); //fire input of testNode with a buffer of 0x000FFFFFFFFFFFFF (4503599627370495) | ||
} catch (error) { | ||
done(error); | ||
} | ||
}); | ||
}); | ||
@@ -108,6 +142,6 @@ it('should generate 5 values (fan out test)', done => { | ||
* all output types | ||
* byteswaps | ||
* scalling operators | ||
* byte swaps | ||
* scaling operators | ||
* dynamic spec | ||
*/ | ||
}); |
650882
1898