Fluent Stream
Parsing octet-data from stream/buffer can be painful, this is what fluent-stream aiming for.
Quick Example
var fluent = fluent // pop 2 bytes // async pop // unpredictable length // remaining data will be emited(Stream) // write or pipe data to itf
Install
$ npm install fluent-stream
Main Methods
var FluentStream =
var fluent = FluentStream.create([buffer])
- buffer <Buffer>
Return a fluent-stream object, it is a duplex stream
fluent.pop([nbytes,] [callback])
- nbytes <Number> : default 1
- callback(buffer, [done]) <Function>
Pop n length buffer and exec callback when fluent received enough bytes(>= nbytes)
Discard those data if callback not exists
If callback has more than 1 params, it will be treat as a async callback, you must call done(2nd param) to finish this pop.
If callback return a Number(sync: return n, async: done(n)), it indicate that next pop action will pop n bytes. This is very useful in handling unpredictable length buffer.
Convience Methods: stash
fluent.stash
There is a 'stash' prop in fluent, it is a Object, all stashed buffers will be stored in it.
fluent.pop([nbytes,] [stashName])
- nbytes <Number> : default 1
- stashName <String>
Pop buffer and stash it in 'stash' keyed 'stashName'.
fluent.apply(callback)
- callback(stash) <Function>
Apply stash, you can accese stashed buffers in it, such as console.log(stash.stashName)
Convience Methods: read*()
There are a dozen of methods can pre-parse buffer to corresponding types, such as int, float, double
fluentfluent
They are…
- readUInt8
- readUInt16LE
- readUInt16BE
- readUInt32LE
- readUInt32BE
- readInt8
- readInt16LE
- readInt16BE
- readInt32LE
- readInt32BE
- readFloatLE
- readFloatBE
- readDoubleLE
- readDoubleBE
Details see here Node Buffer Doc
fluent.read*(callback)
- callback(value, [done]) <Function>
fluent.read*(stashName)
- stashName <String>
(TODO)fluent.readString(length, callback)
(TODO)fluent.readString(length, stashName)
Stream implementation
FluentStream is duplex stream implemented, you can pipe readable stream to it and pipe it to other writable stream.
Fluent will pop buffer of specified length then emit remaining buffer.
eg.
var http = FluentStream = http
LICENSE
MIT