binbone

A binary encoder/decoder aimed at achieving optimal space utilization.

Usage no npm install needed!

<script type="module">
  import binbone from 'https://cdn.skypack.dev/binbone';
</script>

README

node-binbone

Node.js(io.js) implemention of binbone, A binary encode specification aimed at achieving optimal space utilization.

NPM version Build Status Build status

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); // args are the same as a QueueBuffer

block.writeArray([1, 2, 3]);
block.writeUInt("123456789012345"); // Big integer(use [jsbn](https://github.com/andyperlitch/jsbn))
block.readArray();
block.readUInt();
  • Use encoder/decoder.

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 (outputBlock)

    constructor

    • param: outputBlock { FlexBuffer }

      An BinboneBlock Object

  • writeTo (outputBlock)

    Reset data block

    • param: outputBlock { FlexBuffer }

      An BinboneBlock Object

  • writeByte (value = 0)

    Write a byte.

    • param: value { number=0 }

      byte value

    • return: { number }

      length to write (always 1)

  • writeBoolean (value) (alias: writeBool)

    Write a boolean value.

    • param: value { boolean }

      boolean value

    • return: { number }

      length to write (always 1)

  • writeUInt (num = 0 | string, opts = {}) (alias: writeLength, writeSign)

    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

  • writeInt (opts = {}) (alias: writeLong)

    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

  • writeFloat (value = 0)

    Write a float.

    • param: value { number=0 }

      float point number

    • return: { number }

      length to write (always 4)

  • writeDouble (value = 0)

    Write a double.

    • param: value { number=0 }

      float point number

    • return: { number }

      length to write (always 8)

  • writeBytes (values, opts = {})

    Write bytes.

    • param: values { Array | Buffer }

      bytes

    • param: opts { Object={} }

      options

    • option: length { number }

      number of bytes

    • return: { number }

      length to write

  • writeString (str, opts = {})

    Write a string.

    • param: str { string }

      string

    • param: opts { Object={} }

      options

    • option: length { number }

      byte length of string

    • return: { number }

      length to write

  • writeMap (map = {}, opts = {})

    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

  • writeArray (arr = [], opts = {})

    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

  • writeObject (obj = {}, opts = {})

    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

  • Record type

License

MIT@Jingchen Zhao