binary-data
Declarative binary data encoder / decoder. This module works almost like as binary
or restructure
but provided modern and clean api. It inspired by abstract-encoding interface.
Support
Usage
decode
const decode createDecode types: uint8 array string = // 1. Define your own schema as plain objectconst protocol = type: uint8 value: const message = Buffer; // Just decode messageconst packet = // 2 Also you may decode messages from streamsconst net = ; const socket = net;const istream = ; socket;
encode
const encode createEncode types: uint8 string = const protocol = type: uint8 value: const hello = type: 12 value: 'my random data' // Just encode messageconst ostream = ;const packet = ostream; // Or you may encode messages into a streamconst net = ; const ostream = ;const socket = net; ostream; // You may combine multiple schemes into one streamconst ostream = ; ;;; const packet = ostream;
See stun or dtls module for complete example.
Perfomance
Decoding DTLS ClientHello packet, nodejs 10.14.1 / Ubuntu 16.04 x64
name | time |
---|---|
binary data | 637.900ms |
binary | 2229.218ms |
API
encode(obj: any, [target: BinaryStream], type: Object): BinaryStream
decode(source: BinaryStream|Buffer, type: Object): any
encodingLength(item: any, type: Object): Number
createEncodeStream([type: Object]): BinaryStream
createDecodeStream([type: Object|Buffer]): BinaryStream
createEncode([type: Object]): BinaryStream
createDecode([type: Object|Buffer]): BinaryStream
- Types
decode(source: BinaryStream|Buffer, type: Object): any
Reads any data from stream rstream
using data type type
. See examples above.
encode(obj: any, [target: BinaryStream], type: Object): BinaryStream
Writes any data obj
to stream target
using data type type
. See examples above.
encodingLength(item: any, type: Object): Number
Return the amount of bytes needed to encode item
using type
.
createEncodeStream([type: Object]): BinaryStream
createEncode([type: Object]): BinaryStream
Create instance of BinaryStream.
createDecodeStream([type: Object|Buffer]): BinaryStream
createDecode([type: Object|Buffer]): BinaryStream
Create instance of BinaryStream.
types: Object
Contains all primitive data types.
Types
(u)int(8, 16, 24, 32, 40, 48)(be, le)
Low-level integer types.
const schema = type: int8 // define int64 as buffer and use your loved library for big numbersconst int64 =
(double, float)(be, le)
Low-level floating-point types.
const schema = size: doublele
array(type: Object, length: number|Object|Function, lengthType: string)
Array of low-level or user defined types. Argument type
should be primitive type or user defined scheme. Argument length
should be a number, number type or function. Argument lengthType
should be bytes
or count
(default).
// 3 x uint8 const schema = length: uint8 type: uint32be items: // difference between `bytes` or `count` // | 0x2 | 0x0 0x1 | 0x0 0x2 |// | length | item 1 | item 2 |// bytes = 1 + 4, length = 2 // | 0x4 | 0x0 0x1 | 0x0 0x2 |// | length | item 1 | item 2 |// bytes = 1 + 4, length = 4
string(length)
Low-level string type. Argument length
can be number, null for C - strings, type for size-prefixed data or function.
bool(type: any)
Convert provided type to / from boolean. Argument type
should be an number type.
const schema = const rstream = // 0x01 0 // return true // return false
buffer(length: Object|null|number)
Low-level buffer type. Argument length
can be number, number type for size-prefixed data, function or null.
// buffer should be 5 bytes // length prefixed buffer// | 0x3 | 0xa 0xb 0xc// | length | data const packet = header: length: uint16be data: // function should return actual length
reserved(type, count)
Special type to skip any data. Argument count
should be a number, number type or function.
const packet = type: uint8 _padding: // return { type }
when(fn: function(context): boolean, type)
Special type for conditions. Argument fn
should be a function and should return boolean value. The type
argument will be evaluated when the first one returns positive value.
const schema = type: uint8 bytes: list:
select(when, ..., defaultType)
The second type for conditions. The same as switch
operator in js. Argument defaultType
may be any known
type excluding user schemas.
const schema = id: uint8 payload:
License
MIT, 2017 (c) Dmitriy Tsvettsikh