What is bufrw?
The 'bufrw' npm package is a buffer read/write library designed to facilitate the reading and writing of binary data. It provides a set of tools to handle binary data serialization and deserialization, making it easier to work with binary protocols.
What are bufrw's main functionalities?
Buffer Reading
This feature allows you to read binary data from a buffer. In this example, a 32-bit unsigned integer is read from a buffer in big-endian format.
const bufrw = require('bufrw');
const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04]);
const result = bufrw.UInt32BE.readFrom(buffer, 0);
console.log(result.value); // 16909060
Buffer Writing
This feature allows you to write binary data into a buffer. In this example, a 32-bit unsigned integer is written into a buffer in big-endian format.
const bufrw = require('bufrw');
const buffer = Buffer.alloc(4);
bufrw.UInt32BE.writeInto(16909060, buffer, 0);
console.log(buffer); // <Buffer 01 02 03 04>
Custom Serialization
This feature allows you to define custom serialization structures. In this example, a structure consisting of an 8-bit unsigned integer followed by a 16-bit unsigned integer in big-endian format is defined and written into a buffer.
const bufrw = require('bufrw');
const MyStruct = bufrw.Struct([bufrw.UInt8, bufrw.UInt16BE]);
const buffer = Buffer.alloc(3);
MyStruct.writeInto([0x01, 0x0203], buffer, 0);
console.log(buffer); // <Buffer 01 02 03>
Other packages similar to bufrw
buffer
The 'buffer' package is a core Node.js module that provides a way of handling binary data directly in JavaScript. It is similar to 'bufrw' in that it allows reading and writing of binary data, but it is more general-purpose and does not provide the same level of abstraction for custom serialization.
binary
The 'binary' package provides a declarative way to work with binary data in Node.js. It allows you to define binary structures and parse them from buffers. It is similar to 'bufrw' in that it provides tools for binary data serialization and deserialization, but it uses a different approach with a focus on declarative syntax.
struct
The 'struct' package allows you to define and work with binary data structures in Node.js. It is similar to 'bufrw' in that it provides tools for defining and working with binary data structures, but it offers a different API and focuses on ease of use for defining complex binary structures.
bufrw
Buffer Reading and Writing
Example
Simple length-prefixed string:
var bufrw = require("bufrw");
var buf = bufrw.toBuffer(bufrw.str1, "hello world");
var str = bufrw.fromBuffer(bufrw.str1, buf)
// TODO more examples
Concept and Motivation
A combinatoric library for synchronous binary buffer reading and writing.
The design is to combine:
- needed byte length calculation
- writing into a pre-allocated buffer
- reading from a buffer
Into a single re-combinable data type, eventually supporting code generation
for efficiency.
Any of those three steps may result in an error, so rather than rely on error
throw/catching we use an error-able result type.
API Documentation
See docs.jsig
Installation
npm install bufrw
Tests
npm test
NPM scripts
npm run add-licence
This will add the licence headers.npm run cover
This runs the tests with code coveragenpm run lint
This will run the linter on your codenpm test
This will run the tests.npm run trace
This will run your tests in tracing mode.npm run travis
This is run by travis.CI to run your testsnpm run view-cover
This will show code coverage in a browser
Contributors
MIT Licenced