Bufio: Buffer & Serialization Utilities
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
-
ReferenceError: require is not defined
cause Attempting to import the CommonJS-only `bufio` library using an ESM `import` statement in an environment that does not support it, or in a file configured as an ES module.fixFor Node.js projects, ensure the file using `bufio` is a CommonJS module (e.g., `.js` file without `"type": "module"` in `package.json`, or explicitly `.cjs`). Use `const bio = require('bufio');` to import the library. -
TypeError: bio.write is not a function
cause Incorrectly attempting to destructure named exports (`import { write } from 'bufio';`) when `bufio` exports a single object or function as its default (and only) export in CommonJS.fixImport the entire module using `const bio = require('bufio');` and access `write` as a property: `const bw = bio.write();`. -
TypeError: bio.Struct is not a constructor
cause Similar to `bio.write`, this error occurs if `Struct` is attempted to be imported as a named export or accessed incorrectly.fixAfter importing the entire module with `const bio = require('bufio');`, access `Struct` as a property: `class MyStruct extends bio.Struct {}`.
Warnings
- gotcha Bufio is primarily a CommonJS library. Attempting to use `import` statements directly in a pure ESM project will result in errors like 'require is not defined' or 'bufio does not provide an export named X'.
- breaking The library is not actively maintained since its last update in 2018. This means it may not receive updates for security vulnerabilities, bug fixes, or compatibility with newer Node.js versions or JavaScript language features.
- gotcha Bufio does not provide TypeScript declaration files (`.d.ts`). Developers using TypeScript will need to write their own declaration files or use `any` types, losing type safety.
- gotcha This library assumes a deep understanding of binary serialization and buffer manipulation. Errors in specifying lengths, types, or endianness can lead to corrupted data or security vulnerabilities.
Install
-
npm install bufio -
yarn add bufio -
pnpm add bufio
Imports
- bio
import bio from 'bufio';
const bio = require('bufio'); - BufferWriter (via bio.write)
import { write } from 'bufio';const bw = bio.write();
- BufferReader (via bio.read)
import { read } from 'bufio';const br = bio.read(buffer);
- Struct
import { Struct } from 'bufio';class MyStruct extends bio.Struct {}
Quickstart
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);