bin-protocol
bin-protocol is a library for parsing and creating arbitrary byte buffers with optional protocol buffers support.
You can build your own type definitions based on the following methods:
- Int8
- UInt8
- Int16LE
- UInt16LE
- Int16BE
- UInt16BE
- Int32LE
- Int32BE
- UInt32LE
- UInt32BE
- FloatLE
- FloatBE
- DoubleLE
- DoubleBE
Int64 support (using long.js):
- Int64BE
- Int64LE
- UInt64BE
- UInt64LE
Read or write raw bytes:
- raw
Varints:
- UVarint (unsigned 32 bit varint)
- Varint (signed 32 bit varint)
- SVarint (signed zigzag encoded 32 bit varint)
- UVarint64 (unsigned 64 bit varint)
- Varint64 (signed 64 bit varint)
- SVarint64 (signed zigzag encoded 64 bit varint)
Loops (arrays):
- loop
Protocol buffers support: nested messages, packed repeated fields, maps, enums, packages (namespaces), oneofs
Install
$ npm install bin-protocol
Reader examples
Built-in metods:
var Protocol = ;var protocol = ; var result = protocol result; console; // => { num1: 0, num2: 1, num3: 2, num4: 3 }
Define custom 'char' and 'array' methods:
protocol; protocol; var result = protocolresult; console; // => { chars: [ 'a', 'b', 'c', 'd', 'e' ] }
Writer examples
var buffer = protocol result console; // => <Buffer 01 02 03>
Define an array data type which first writes data array length as a single byte
protocol; var buffer = protocolresult; console; // => <Buffer 03 02 03 04>
Define reader and writer methods together, this one writes (or reads) raw buffer preceeded by its length as 32 bit integer.
protocol;
Loops (arrays)
Given a buffer where first 32bit integer is a number (3) of further 32bit integers (2,3 and 4):
var buffer = 16; buffer;buffer;buffer;buffer;
All next 3 examples are essentialy identical:
Read data with your own code:
protocol; protocolresult; // => { items: [2, 3, 4] }
Read with .loop()
method by providing the length (loop count):
protocol; protocolresult; // => { items: [2, 3, 4] }
Read with .loop()
method until the end()
is called:
protocol; protocolresult; // => { items: [2, 3, 4] }
See Kafka protocol for more examples.
Protocol buffers support
Given a test.proto:
package basic; message Test { optional string string = 15;}
var Protocol = ; var TestProtocol = Protocol; var protocol = ; // encode messagevar encoded = protocolbasicresult; // decode messagevar decoded = protocolbasicresult; // => { string: 'hello' }
Sebinary-protocol protocol](https://github.com/oleksiyk/binary-protocol/blob/master/lib/protocol.js) for another example.
Custom protocols
You can define several independent protocols by using Protocol.createProtocol()
function:
var Protocol1 = Protocol;var Protocol2 = Protocol; Protocol1;Protocol2;
License (MIT)
Copyright (c) 2015 Oleksiy Krivoshey.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.