Buffer Builder
This package, `buffer-builder`, provides a utility for incrementally constructing a Node.js `Buffer` without prior knowledge of its final size. It offers methods to append strings, existing buffers, and various integer/float types (e.g., `appendUInt32LE`, `appendFloatBE), as well as a byte fill operation. The current stable version is 0.2.0, last published over a decade ago in January 2015. Due to its age, it directly relies on the now-deprecated `new Buffer()` constructor, which poses significant security and usability issues in modern Node.js environments. This package is effectively abandoned, with no active development or updates to address compatibility with contemporary Node.js Buffer APIs (like `Buffer.from`, `Buffer.alloc`). Developers seeking similar functionality should consider actively maintained alternatives or Node.js's built-in `Buffer.concat` with an array of smaller buffers.
Common errors
-
TypeError: Buffer is not a constructor
cause Attempting to instantiate `Buffer` using `new Buffer()` in Node.js v10.0.0 or later, which removed the `Buffer` constructor. This package internally uses `new Buffer()` which leads to this error.fixThis package is incompatible with Node.js v10+. Either downgrade your Node.js version (not recommended) or replace `buffer-builder` with an actively maintained alternative that uses `Buffer.from()` or `Buffer.alloc()`. -
(node:XXXX) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
cause Running `buffer-builder` with Node.js v6.0.0 through v9.x.x. The package uses `new Buffer()` which is formally deprecated during this range.fixThis warning indicates the package uses an outdated and potentially insecure API. While it might still function, it's a strong signal to migrate to a modern, actively maintained buffer building library to avoid future breaking changes and security risks. You can suppress the warning with `--no-deprecation` but this is not recommended.
Warnings
- breaking The `buffer-builder` package internally uses `new Buffer()`, which was deprecated in Node.js v6.0.0 and removed in v10.0.0. Using this package with Node.js v10 or newer will cause runtime errors because `Buffer` is no longer a constructor.
- gotcha The package has not been updated since January 2015, making it highly susceptible to compatibility issues with newer Node.js versions, security vulnerabilities discovered since its last release, and lack of modern JavaScript features (e.g., ESM).
- deprecated The documentation's usage example uses `new Buffer([...])`. This method creates an uninitialized buffer which can expose sensitive memory contents and is a security risk.
Install
-
npm install buffer-builder -
yarn add buffer-builder -
pnpm add buffer-builder
Imports
- BufferBuilder
const BufferBuilder = require('buffer-builder');import BufferBuilder from 'buffer-builder';
- Buffer
const Buffer = require('buffer');import { Buffer } from 'node:buffer';
Quickstart
import BufferBuilder from 'buffer-builder';
import { Buffer } from 'node:buffer';
const helloWorld = new BufferBuilder();
// Append a string, utf8 encoded by default.
helloWorld.appendString('hello');
// Append any type that Buffer has read and write functions for.
helloWorld.appendUInt16LE(0x7720);
// Append a buffer - using Buffer.from() for modern Node.js compatibility
helloWorld.appendBuffer(Buffer.from([111, 114, 108, 100]));
// Appended a repetition of a byte
helloWorld.appendFill(33, 3);
// Convert to an ordinary buffer
const buffer = helloWorld.get();
console.log(buffer.toString()); // Expected: hello world!!!