Node-protoparse is a project mainly written in JavaScript, it's free.
(Binary) protocol parsing for node.js
Protoparse (short for protocol parser because my name creativity isn't so good) allows you to unpack binary data (and normal data) from streams and buffers.
Much like substacks node-binary (it even uses some code), but with a focus on speed, stability and extensibility.
var Parser = require('protoparse')
var stdin = process.openStdin()
Parser.Stream(stdin)
.word32lu('x')
.word16bs('y')
.word16bu('z')
.tap(function(vars) {
console.dir(vars)
})
var Parser = require('protoparse')
var buffer = Buffer([ 97, 98, 99, 100, 101, 102, 0 ])
Parser(buffer)
.word16ls('ab')
.word32bu('cf')
.word8('x')
.tap(function(vars) {
console.dir(vars)
})
.run()
Note that the buffer parser requires a run() call, because I'm too lazy to implement it properly :)
After working with node-binary for a while, one of the things that kept bothering me was the lack of ability to add my own methods. Needless to say, this has been one of my main goals when writing this.
var Parser = require('protoparse')
var BinaryChainProvider = require('protoparse/binary.js')
var MyOwnProtocolChainMaker = Object.create(BinaryChainProvider)
MyOwnProtocolChainMaker.myCustomHandshake = function() {
return this.word16bu('packettype')
.tap(function(vars) {
if (vars.packettype == 2)
this.word8('strlen')
.buffer('name', 'strlen')
else this.word32bs('money')
}
}
Parser.Stream(stream, callbackname, MyOwnProtocolChainMaker)
.myCustomHandshake()
var p = Parser(null, MyOwnProtocolChainMaker)
stream.on('data', p.addData)
For the available methods, see binary.js. Internally used methods are also in other files.
Lots of things are still missing, including (in no particular order):
The word64 functions will only return approximations since javascript uses ieee floating point for all number types. Mind the loss of precision.