var BinaryProtocol = require('binary-protocol');var protocol = new BinaryProtocol();// define a type called 'Bytes', which is simply// a series of raw bytes prefixed by length.protocol.define('Bytes', { read: function (propertyName) { this .pushStack({length: null, value: null}) // allocate a new object to read the data into. .Int32BE('length') // read a 32 bit integer into the `length` property. .tap(function (data) { if (data.length === -1) { // if the length is -1, then there are no bytes and the value is null. data.value = null;
Writing data
var writer = protocol.createWriter();writer.Int32BE(100) // op code.String('admin') // user name.String('password'); // passwordvar buffer = writer.buffer;someWritableStream.write(buffer);