{"id":15554,"library":"bufio","title":"Bufio: Buffer & Serialization Utilities","description":"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.","status":"abandoned","version":"1.2.3","language":"javascript","source_language":"en","source_url":"git://github.com/bcoin-org/bufio","tags":["javascript","buffer","serialization"],"install":[{"cmd":"npm install bufio","lang":"bash","label":"npm"},{"cmd":"yarn add bufio","lang":"bash","label":"yarn"},{"cmd":"pnpm add bufio","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Bufio is primarily a CommonJS library. There is no official ESM entry point as of v1.2.3, so direct `import` statements will likely fail in pure ESM contexts.","wrong":"import bio from 'bufio';","symbol":"bio","correct":"const bio = require('bufio');"},{"note":"`write` is a factory method on the main `bio` object, not a named export. It returns a `BufferWriter` instance.","wrong":"import { write } from 'bufio';","symbol":"BufferWriter (via bio.write)","correct":"const bw = bio.write();"},{"note":"`read` is a factory method on the main `bio` object, not a named export. It returns a `BufferReader` instance.","wrong":"import { read } from 'bufio';","symbol":"BufferReader (via bio.read)","correct":"const br = bio.read(buffer);"},{"note":"`Struct` is a constructor property on the main `bio` object, not a named export. It serves as a base class for custom serialization schemas.","wrong":"import { Struct } from 'bufio';","symbol":"Struct","correct":"class MyStruct extends bio.Struct {}"}],"quickstart":{"code":"const assert = require('assert');\nconst bio = require('bufio');\n\n// Basic Usage: Write and Read\nconst bw = bio.write();\nbw.writeU64(100);\nbw.writeString('foo');\nconst data = bw.render();\n\nconst br = bio.read(data);\nassert.strictEqual(br.readU64(), 100);\nassert.strictEqual(br.readString(3), 'foo');\n\n// Struct Usage: Define custom serializable objects\nclass MyStruct extends bio.Struct {\n  constructor() {\n    super();\n    this.str = 'hello';\n    this.value = 0;\n  }\n\n  write(bw) {\n    bw.writeVarString(this.str, 'ascii');\n    bw.writeU64(this.value);\n    return this;\n  }\n\n  read(br) {\n    this.str = br.readVarString('ascii');\n    this.value = br.readU64();\n    return this;\n  }\n}\n\nconst obj = new MyStruct();\nobj.str = 'world';\nobj.value = 12345;\n\nconsole.log('Original object:', obj);\nconsole.log('Encoded Buffer (Hex):', obj.toHex());\n\nconst decodedObj = MyStruct.fromHex(obj.toHex());\nconsole.log('Decoded object:', decodedObj);\nassert.deepStrictEqual(obj, decodedObj);\n","lang":"javascript","description":"Demonstrates both basic buffer writer/reader usage and the creation of a custom serializable `Struct` class, including encoding and decoding to/from various formats."},"warnings":[{"fix":"Use `const bio = require('bufio');` for all imports. If in an ESM module, consider a wrapper or a build step to handle CommonJS modules, or use dynamic import `import('bufio').then(bio => ...)`. Alternatively, for Node.js 12+, you might be able to use `createRequire` from `module`.","message":"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'.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate whether the unmaintained status poses a risk for your application, especially for security-critical operations involving low-level buffer manipulation. Consider auditing the code for known vulnerabilities or exploring more actively maintained alternatives if possible.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Create a `bufio.d.ts` file in your project or use a declaration merging pattern to provide type definitions for the `bufio` module. Example: `declare module 'bufio' { export = any; }` (less ideal) or more specific typings for `Writer`, `Reader`, and `Struct`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Thoroughly test all serialization and deserialization routines. Ensure that byte order, string encodings, and numerical sizes are explicitly matched between writing and reading operations.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"For 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.","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.","error":"ReferenceError: require is not defined"},{"fix":"Import the entire module using `const bio = require('bufio');` and access `write` as a property: `const bw = bio.write();`.","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.","error":"TypeError: bio.write is not a function"},{"fix":"After importing the entire module with `const bio = require('bufio');`, access `Struct` as a property: `class MyStruct extends bio.Struct {}`.","cause":"Similar to `bio.write`, this error occurs if `Struct` is attempted to be imported as a named export or accessed incorrectly.","error":"TypeError: bio.Struct is not a constructor"}],"ecosystem":"npm"}