bison-types
- Are you stuck with integrating with a system that only speaks binary?
- Are you sick of manually decoding and encoding from json to binary?
- Do you want an easy way to define your types in javascript?
If you answered yes to any of the above questions, then bison-types
is for you
How does it work?
bison-types
allows you to define custom types.
With these custom types you can build up a message definition
Pass these types and the buffer to bison-types and it will do the rest.
For example:
var bison = ; var types = bison; // time to read! var buf = Buffer;var reader = buf types bigEndian: false ;var json = reader; // or write ! var buf = Buffer;var writer = buf types bigEndian: false ; writer;
Note: bison-types uses clever-buffer under the hood for all buffer manipulation.
Provided types
The following types can be declared as a string, for example: {timestamp: 'uint16'}
.
uint8
- unsigned 8 bit integeruint16
- unsigned 16 bit integeruint32
- unsigned 32 bit integeruint64
- unsigned 64 bit integerint8
- signed 8 bit integerint16
- signed 16 bit integerint32
- signed 32 bit integerint64
- signed 64 bit integerutf-8
- utf-8 encoded stringlatin1
- latin1 encoded stringbool
- boolean (stored as 8 bit integer)skip
- will skip specified bytes
There is also an enumeration
type, which can be used to represent enums from arrays of objects:
will store the index in the arrayvar levelIndex = bison; // will store the value in the objectvar levelCode = bison; bison;
Creating your own custom types
There are 2 different ways that you can define a custom type
By mapping it to another type
var types = bison;
would create an object like
var myType = c: a: 12 b: 123 d: 1234
By explicitly creating a _read function
We expose the underlying clever-buffer as @buffer.
You can call any of its methods
var types = bison;
would multiply the value read from the buffer before returning it when reading and multiply the value to be written when writing
Reader
You need to pass in a buffer to read from, and any custom types that you may have.
You can also pass in options, look at clever-buffer for a full list of options
Reading some integers
var bison = ;var buf = Buffer;var types = bison; var reader = buf types bigEndian: false ;var myType = reader;
Reading a string
var bison = ;var buf = Buffer;var types = bison; var reader = buf types bigEndian: false ;var myType = reader;
Reading a string with latin1 encoding
var bison = ;var buf = Buffer;var types = bison; var reader = buf types bigEndian: false ;var myType = reader;
Reading a multi-byte string
var bison = ;var buf = Buffer;var types = bison; var reader = buf types bigEndian: false ;var myType = reader;// myType = { a: 'HÉLLO' }
Complex types
The power of bison-types is evident as you define more complex types
var bison = ;var buf = Buffer;var types = bison; var reader = buf types bigEndian: false ;var myType = reader;
Using previous values as parameters
You can use previously resolved values as parameters to types The power of bison-types is evident as you define more complex types
var bison = ;var buf = Buffer;var types = bison; var reader = buf types bigEndian: false ;var myType = reader;
Arrays
You can specify arrays in a similar matter
var bison = ;var buf = Buffer;var types = bison; var reader = buf types bigEndian: false ;var myType = reader;// myType = { a: 3, b:[{c:1},{c:2},{c:3}] }
Writer
You need to pass in a buffer to write to, and any custom types that you may have.
You can also pass in options, look at clever-buffer for a full list of options
Writing some integers
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer;// buf will equal [ 0x01, 0x02, 0x03, 0x04 ]
Writing a string
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer;// buf will equal [0x48, 0x45, 0x4C, 0x4C, 0x4F]
Writing a string with latin1 encoding
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer;// buf will equal [0x48, 0xC9, 0x4C, 0x4C, 0x4F]
Only writing a certain length of string
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer;// buf will equal [0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x00, 0x00, 0x00, 0x00, 0x00]
Writing a multi-byte string
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer;// buf will equal [0x48, 0xC3, 0x89, 0x4C, 0x4C, 0x4F]
Complex types
The power of bison-types is evident as you define more complex types
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer;// buf will equal [ 0x01, 0x03, 0x04 ]
Using other values as parameters
You can use other values as parameters to types The power of bison-types is evident as you define more complex types
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer;// buf will equal [ 0x04, 0x02 ]
Overriding a value
You can specify a specific value using the following syntax
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer; // buf will equal [ 0x01, 0x02 ]
Arrays
You can specify arrays in a similar matter
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer; // buf will equal [ 0x03, 0x01, 0x02, 0x03 ]
Using an array length as a parameter
This is a shorthand of the above example
var bison = ;var buf = Buffer;var types = bison; var writer = buf types bigEndian: false ;writer; // buf will equal [ 0x03, 0x01, 0x02, 0x03 ]
Testing
npm test