Calippo
Binary stream parser for node
Callipo is a more modern implementation of an existing binary stream parser for node called node-strtok. The biggest difference is that with node 0.10 we can leverage the more modern Streams2 API which provides us with out of the box methods for consuming and transforming streams of binary data.
Calippo is not intended to be a high level DSL for parsing binary streams ala node-binary, this sits lower in the stack, capable of implementing more novel abstractions on top of callipo.
Installation
Install via npm:
npm install calippo
Usage
Pulls out the data in an int32 prefixed message stream: [int32][data][int32][data]...
var Calippo = var pos var parser = parser somestream
API
Calippo(callback, options)
callback
takes a single argument, the value just read from the stream, and is expected to return the type of value to read next, e.g. this.Buffer(len)
. It is this callback that ultimately implements the application protocol, you are expected to manage state yourself.
options
is an object passed down into the node Transform api, pass {objectMode: true}
if you want to output non binary data with this.push()
All expected values are returned from the callback
, and and the types of values available are all attached to the this
scope within the callback:
this.Buffer(len)
this.String(len, encoding)
this.Skip(len)
- skip (len) bytes, calls callback with null
Calippo also implements all the standard node buffer readXXX methods for parsing numbers, see http://nodejs.org/api/buffer.html
this.readUInt32LE
this.readUInt8
...
We also implement one special type, DEFER
and one method defer
. These are used together for when you don't yet know what type of value you want to read next, perhaps you need to wait on some network event:
var parser =
this.defer
takes one argument, the next expected type, you must return this.DEFER
before your callback is called.
Licence
MIT