Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #8 from eflexsystems/master
Browse files Browse the repository at this point in the history
added raw buffer type
  • Loading branch information
asbaker committed Sep 15, 2014
2 parents 1610979 + 2a18efc commit 21aecd7
Show file tree
Hide file tree
Showing 14 changed files with 468 additions and 84 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,3 +1,2 @@
.vagrant
lib
node_modules
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# CHANGELOG

## 0.4.0 (2013-03-26)
- Added default value field

## 0.3.2 (2013-03-24)
- Performance improvements for field encoding, write to the same buffer when encoding fields

Expand Down
63 changes: 36 additions & 27 deletions README.md
Expand Up @@ -35,6 +35,7 @@ The Javascript object specification is used to specify endianness and a descript
{name: "field12", start: 38, type: 'uint32'},
{name: "field13", start: 42, type: 'int32'}
{name: "field14", start: 46, type: 'int8', default: 42 }
{name: "field15", start: 50, type: 'buffer', length: 4 }
]
}

Expand All @@ -44,6 +45,7 @@ The Javascript object specification is used to specify endianness and a descript
All fields must have a name, a starting byte, and a type. The name is used for assigning the property in the resulting javascript object. Additionally, fields can have a default value which is used when encoding an object's properties that have null or undefined values.

#### Types
* **buffer** - Raw buffer
* **int8** - Signed 8-bit integer
* **uint8** - Unsigned 8-bit integer
* **int16** - Signed 16-bit integer
Expand Down Expand Up @@ -74,23 +76,26 @@ All fields must have a name, a starting byte, and a type. The name is used for a
### Example

```javascript
var DecoderRing = require("decoder-ring")
var DecoderRing = require("decoder-ring");
var decoderRing = new DecoderRing();

var bufferBE = new Buffer(47)
bufferBE.fill(0)
bufferBE.writeInt8(-127, 0)
bufferBE.writeUInt8(254, 1)
bufferBE.writeInt16BE(5327, 2)
bufferBE.writeUInt16BE(5328, 4)
bufferBE.writeFloatBE(-15.33, 6)
bufferBE.writeDoubleBE(-1534.98, 10)
bufferBE.write("ascii", 18, 10,'ascii')
bufferBE.write("utf8 text", 28, 9, 'utf8')
bufferBE.writeUInt8(129, 37)
bufferBE.writeUInt32BE(79001, 38)
bufferBE.writeInt32BE(-79001, 42)
bufferBE.writeInt8(1, 46)
var bufferBE = new Buffer(51);
bufferBE.fill(0);
bufferBE.writeInt8(-127, 0);
bufferBE.writeUInt8(254, 1);
bufferBE.writeInt16BE(5327, 2);
bufferBE.writeUInt16BE(5328, 4);
bufferBE.writeFloatBE(-15.33, 6);
bufferBE.writeDoubleBE(-1534.98, 10);
bufferBE.write("ascii", 18, 10,'ascii');
bufferBE.write("utf8 text", 28, 9, 'utf8');
bufferBE.writeUInt8(129, 37);
bufferBE.writeUInt32BE(79001, 38);
bufferBE.writeInt32BE(-79001, 42);
bufferBE.writeInt8(1, 46);

var testBuffer = new Buffer("test");
testBuffer.copy(bufferBE, 47, 0, 4);

var spec = {
bigEndian: true,
Expand All @@ -101,37 +106,39 @@ var spec = {
{ name: "field4", start: 4, type: 'uint16'},
{ name: "field5", start: 6, type: 'float' },
{ name: "field6", start: 10, type: 'double'},
{ name: "field7", start: 18, type: 'ascii',length: 10 },
{ name: "field7", start: 18, type: 'ascii', length: 10 },
{ name: "field8", start: 28, type: 'utf8', length: 9 },
{ name: "field9", start: 37, type: 'bit', position: 7 },
{ name: "field10", start: 37, type: 'bit', position: 6 },
{ name: "field11", start: 37, type: 'bit', position: 0 },
{ name: "field12", start: 38, type: 'uint32' },
{ name: "field13", start: 42, type: 'int32' },
{ name: "field14", start: 46, type: 'int8', default: 42 }
{ name: "field14", start: 46, type: 'int8', default: 42 },
{ name: "field15", start: 47, type: 'buffer', length: 4 }
]
}
};

// Decode the buffer into a javascript object
var result = decoderRing.decode(bufferBE, spec)
console.log(result)
var result = decoderRing.decode(bufferBE, spec);
console.log(result);

// Assign field14 to undefined to test default value on encoding
result.field14 = undefined
result.field14 = undefined;

// Encode the object to a buffer
var buffer = decoderRing.encode(result, spec)
console.log(buffer)
var buffer = decoderRing.encode(result, spec);
console.log(buffer);

// Decode buffer to object and check field14 for default value
var resultWithDefaultValue = decoderRing.decode(buffer, spec)
console.log("Field14 default value: " + resultWithDefaultValue.field14)
var resultWithDefaultValue = decoderRing.decode(buffer, spec);
console.log("Field14 default value: " + resultWithDefaultValue.field14);
```

Result of the above example

```javascript
{ field1: -127,
{
field1: -127,
field2: 254,
field3: 5327,
field4: 5328,
Expand All @@ -144,7 +151,9 @@ Result of the above example
field11: true,
field12: 79001,
field13: -79001,
field14: 1 }
field14: 1,
field15: <Buffer 74 65 73 74>
}

<Buffer 81 fe 14 cf 14 d0 c1 75 47 ae c0 97 fb eb 85 1e b8 52 61 73 63 69 69 20 20 20 20 20 75 74 66 38 20 74 65 78 74 81 00 01 34 99 ff fe cb 67 2a>

Expand Down
55 changes: 30 additions & 25 deletions example.js
@@ -1,20 +1,23 @@
var DecoderRing = require("decoder-ring")
var DecoderRing = require("decoder-ring");
var decoderRing = new DecoderRing();

var bufferBE = new Buffer(47)
bufferBE.fill(0)
bufferBE.writeInt8(-127, 0)
bufferBE.writeUInt8(254, 1)
bufferBE.writeInt16BE(5327, 2)
bufferBE.writeUInt16BE(5328, 4)
bufferBE.writeFloatBE(-15.33, 6)
bufferBE.writeDoubleBE(-1534.98, 10)
bufferBE.write("ascii", 18, 10,'ascii')
bufferBE.write("utf8 text", 28, 9, 'utf8')
bufferBE.writeUInt8(129, 37)
bufferBE.writeUInt32BE(79001, 38)
bufferBE.writeInt32BE(-79001, 42)
bufferBE.writeInt8(1, 46)
var bufferBE = new Buffer(51);
bufferBE.fill(0);
bufferBE.writeInt8(-127, 0);
bufferBE.writeUInt8(254, 1);
bufferBE.writeInt16BE(5327, 2);
bufferBE.writeUInt16BE(5328, 4);
bufferBE.writeFloatBE(-15.33, 6);
bufferBE.writeDoubleBE(-1534.98, 10);
bufferBE.write("ascii", 18, 10,'ascii');
bufferBE.write("utf8 text", 28, 9, 'utf8');
bufferBE.writeUInt8(129, 37);
bufferBE.writeUInt32BE(79001, 38);
bufferBE.writeInt32BE(-79001, 42);
bufferBE.writeInt8(1, 46);

var testBuffer = new Buffer("test");
testBuffer.copy(bufferBE, 47, 0, 4);

var spec = {
bigEndian: true,
Expand All @@ -25,28 +28,30 @@ var spec = {
{ name: "field4", start: 4, type: 'uint16'},
{ name: "field5", start: 6, type: 'float' },
{ name: "field6", start: 10, type: 'double'},
{ name: "field7", start: 18, type: 'ascii',length: 10 },
{ name: "field7", start: 18, type: 'ascii', length: 10 },
{ name: "field8", start: 28, type: 'utf8', length: 9 },
{ name: "field9", start: 37, type: 'bit', position: 7 },
{ name: "field10", start: 37, type: 'bit', position: 6 },
{ name: "field11", start: 37, type: 'bit', position: 0 },
{ name: "field12", start: 38, type: 'uint32' },
{ name: "field13", start: 42, type: 'int32' },
{ name: "field14", start: 46, type: 'int8', default: 42 }
{ name: "field14", start: 46, type: 'int8', default: 42 },
{ name: "field15", start: 47, type: 'buffer', length: 4 }
]
}
};

// Decode the buffer into a javascript object
var result = decoderRing.decode(bufferBE, spec)
console.log(result)
var result = decoderRing.decode(bufferBE, spec);
console.log(result);

// Assign field14 to undefined to test default value on encoding
result.field14 = undefined
result.field14 = undefined;

// Encode the object to a buffer
var buffer = decoderRing.encode(result, spec)
console.log(buffer)
var buffer = decoderRing.encode(result, spec);
console.log(buffer);

// Decode buffer to object and check field14 for default value
var resultWithDefaultValue = decoderRing.decode(buffer, spec)
console.log("Field14 default value: " + resultWithDefaultValue.field14)
var resultWithDefaultValue = decoderRing.decode(buffer, spec);
console.log("Field14 default value: " + resultWithDefaultValue.field14);

72 changes: 72 additions & 0 deletions lib/DecoderRing.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions lib/FieldDecoder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 21aecd7

Please sign in to comment.