node-binbone
Node.js(io.js) implemention of binbone, A binary encode specification aimed at achieving optimal space utilization.
Installation
npm i binbone -S
Usage
- Use Block. Block can be use as both an encoder and a decoder.
Block = require("binbone");
block = new Block(1024);
block.writeArray([1, 2, 3]);
block.writeUInt("123456789012345");
block.readArray();
block.readUInt();
Directly:
Encoder = require("binbone").Encoder;
encodeBlock = new Encoder();
encodeBlock.writeInt(123);
Specify a Buffer for data:
binbone = require('binbone');
buf = new binbone.QueueBuffer();
buf.writeUInt16BE(12);
decoder = new binbone.Decoder(buf);
decoder.readUInt({length: 2});
API
Encoder
-
constructor
-
Reset data block
-
Write a byte.
-
Write a boolean value.
-
Write an unsigned integer, using variable-length coding.
-
param: num
{ number=0 | string }
integer, use string for any big integer
-
param: opts
{ Object={} }
options
-
option: length
{ number }
byte length of integer (1, 2, 4, 8)
-
return: { number }
length to write
-
Write an signed integer, using zig-zag variable-length coding.
-
param: opts
{ Object={} }
options
-
option: length
{ number }
byte length of integer (1, 2, 4, 8)
-
return: { number }
length to write
-
Write a float.
-
Write a double.
-
Write bytes.
-
param: values
{ Array | Buffer }
bytes
-
param: opts
{ Object={} }
options
-
option: length
{ number }
number of bytes
-
return: { number }
length to write
-
Write a string.
-
Write a map.
-
param: map
{ Object | Map = {} }
key-value map
-
param: opts
{ Object={} }
options
-
option: length
{ number }
size of map
-
option: keyType
{ string|Object }
type of key [required]
-
option: valueType
{ string|Object }
type of value [required]
-
return: { number }
length to write
-
Write an array of data.
-
param: arr
{ Array=[] }
Array
-
param: opts
{ Object={} }
options
-
option: length
{ number }
length of array
-
option: valueType
{ string|Object }
type of array item
-
return: { number }
length to write
-
Write an object.
-
param: obj
{ Object={} }
object
-
param: opts
{ Object={} }
options
-
option: length
{ number }
size of object
-
option: valueType
{ string|Object }
type of object value
-
return: { number }
length to write
Decoder
Test
npm test
TODO
License
MIT@Jingchen Zhao