Bufio: Buffer & Serialization Utilities

1.2.3 · abandoned · verified Tue Apr 21

Bufio is a JavaScript library providing low-level buffer and serialization utilities, primarily designed for Node.js environments. Its current and only stable version is 1.2.3, released in 2018. The library focuses on efficient binary data encoding and decoding, offering classes like `BufferWriter`, `BufferReader`, and an extensible `Struct` base class for defining custom binary data structures. It was originally developed as part of the bcoin-org ecosystem, often used in cryptocurrency and blockchain projects requiring precise control over binary data formats. The library's core differentiator is its explicit control over buffer manipulation and serialization patterns, which, while powerful, requires developers to manage byte lengths and encodings manually. It is not actively maintained, with its last update occurring in 2018.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates both basic buffer writer/reader usage and the creation of a custom serializable `Struct` class, including encoding and decoding to/from various formats.

const assert = require('assert');
const bio = require('bufio');

// Basic Usage: Write and Read
const bw = bio.write();
bw.writeU64(100);
bw.writeString('foo');
const data = bw.render();

const br = bio.read(data);
assert.strictEqual(br.readU64(), 100);
assert.strictEqual(br.readString(3), 'foo');

// Struct Usage: Define custom serializable objects
class MyStruct extends bio.Struct {
  constructor() {
    super();
    this.str = 'hello';
    this.value = 0;
  }

  write(bw) {
    bw.writeVarString(this.str, 'ascii');
    bw.writeU64(this.value);
    return this;
  }

  read(br) {
    this.str = br.readVarString('ascii');
    this.value = br.readU64();
    return this;
  }
}

const obj = new MyStruct();
obj.str = 'world';
obj.value = 12345;

console.log('Original object:', obj);
console.log('Encoded Buffer (Hex):', obj.toHex());

const decodedObj = MyStruct.fromHex(obj.toHex());
console.log('Decoded object:', decodedObj);
assert.deepStrictEqual(obj, decodedObj);

view raw JSON →