package
Easy, fast, writers and readers for implementing custom binary protocols.
npm install binary-protocolGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 2 packages.
617.6 kB
Includes dependencies.
Easy, fast, writers and readers for implementing custom binary protocols in node.js.
Via npm:
npm install --save binary-protocolvar 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;
var writer = protocol.createWriter();
writer
.Int32BE(100) // op code
.String('admin') // user name
.String('password'); // password
var buffer = writer.buffer;
someWritableStream.write(buffer);
var reader = protocol.createReader(buffer);
reader
.Int32BE('opCode')
.String('username')
.String('password');
console.log(reader.next()); // {opCode: 100, username: 'admin', password: 'password'}
You can also define aggregate commands which represent a request / response
protocol.define('Login', { write: function (data) { this .Int32BE(100) // op code .String(data.username) .String(data.password); }, read: function () { this .Int32BE('status') .tap(function (data) { if (data.status === 0) { return this.String('error'); } else { return this .String('nickname') .Int32BE('totalUnreadMessages'); } }); } });
var net = require('net'), client = net.createClient('localhost', 3030), commander = protocol.createCommander(client);
var details = { username: 'admin', password: 'password' };
commander.Login(details).then(function (response) { console.log(response); });
First, npm install, then npm test. Code coverage generated with npm run coverage.
MIT, see LICENSE.md.