No more bitwise logic - give it a template and let it do the work for you.
npm install protocol --save
const Protocol =const myProtocol =header:firstBit: bitLength: 1secondBit: bitLength: 1payloadLength: bitLength: 4payload: byteLength: 'payloadLength' encoding: 'utf8'/** 0x90 is hex for '1001 0000'* firstBit: 1, secondBit: 0, payloadLength: 4 (bitwise 0100)* payload: 'abcd' (0x61 to 0x64)*/myProtocol/** '0 1 0010 00' to hex -> 0x48* 'bd' -> 0x62, 0x64* result: Buffer <0x48, 0x62, 0x64>*/myProtocol
Create a protocol in a separate file and share it between clients.
Protocol is the exposed class. Create it by using
schema parameter is an object with the following notation:
const schema =header:firstBit: bitLength: 1secondBit: bitLength: 1payloadLength: bitLength: 4payload: byteLength: 'payloadLength'
Protocols are read in top-to-bottom order, with the input in Big Endian (network order as
defined in RFC 1700). This means that a Buffer will be parsed and generated from left to right.
The current supported options are:
bitLengthin amount of bits, or a string that points to another key when variable.
byteLengthin amount of bytes, or a string that points to another key when variable.
dictas an object containing values for parsing and generating.
encodingas a string containg the required encoding.
typeas a class, only supporting Boolean at this moment.
This method generates a Buffer from an Object. It starts with dictionary translation and
type handling, followed by concatenation and outputting a single Buffer.
If there is no encoding given during generation of a Buffer, it uses UTF-8.
If the input already contains a value of type Buffer, it will retain this Buffer.
When a length is variable and points to key
x does not automatically get a value assigned.
This has to be set explicitly!
This method generates an Object from a Buffer. It splits the individual bits and bytes,
followed by type handling and dictionary translation.
If there is no encoding given during parsing of a Buffer, it will retain this Buffer.