Safer Node.js Buffer API Polyfill

5.2.1 · maintenance · verified Tue Apr 21

safe-buffer is a JavaScript library that provides a consistent and safer API for Node.js `Buffer` operations, particularly for older Node.js environments. It backports the modern `Buffer.from`, `Buffer.alloc`, and `Buffer.allocUnsafe` methods, which address security and stability concerns related to uninitialized memory in older `new Buffer(size)` constructors. For Node.js versions where these safer methods are natively available (v6.0.0 and above), `safe-buffer` transparently defers to the built-in implementation, acting as a compatibility layer. The current stable version is 5.2.1, with releases being infrequent given its role as a polyfill for established Node.js core APIs, with the last major update occurring approximately 6 years ago. Its primary differentiator is ensuring safe `Buffer` allocation across a wide range of Node.js versions without requiring conditional logic in application code.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `safe-buffer`'s core functionality, including safe and unsafe buffer allocation methods (`Buffer.alloc`, `Buffer.allocUnsafe`), and creating buffers from strings or arrays using `Buffer.from`, contrasting them with the deprecated `new Buffer()`.

const Buffer = require('safe-buffer').Buffer;

console.log("Using safe-buffer for Buffer operations:\n");

// The old, potentially unsafe way: new Buffer(size) or Buffer(size)
// While safe-buffer provides compatibility, this can create uninitialized buffers
// in older Node.js versions and is deprecated.
console.log('Old (deprecated) uninitialized buffer via new Buffer(10):');
try {
  const oldBuffer = new Buffer(10);
  console.log(oldBuffer); // Might show garbage data in older Node.js
} catch (e) {
  console.log('  Caught error for new Buffer(10):', e.message);
}

// Modern, safe way: zero-filled buffer (recommended for new allocations)
console.log('\nSafe zero-filled buffer via Buffer.alloc(10):');
const safeBuffer = Buffer.alloc(10);
console.log(safeBuffer);

// Modern way: uninitialized buffer (faster, use if you immediately overwrite contents)
// Be cautious: this memory may contain sensitive data if not fully overwritten.
console.log('\nPotentially unsafe uninitialized buffer via Buffer.allocUnsafe(10):');
const unsafeAllocBuffer = Buffer.allocUnsafe(10);
console.log(unsafeAllocBuffer); // Contents are unpredictable

// Creating a buffer from a string
console.log('\nBuffer from string via Buffer.from("Hello, safe-buffer!"):');
const stringBuffer = Buffer.from('Hello, safe-buffer!', 'utf8');
console.log(stringBuffer.toString('utf8'));

// Creating a buffer from an array of octets
console.log('\nBuffer from array via Buffer.from([0x68, 0x65, 0x6C, 0x6C, 0x6F]):');
const arrayBuffer = Buffer.from([0x68, 0x65, 0x6C, 0x6C, 0x6F]);
console.log(arrayBuffer.toString('utf8'));

// Copying an existing buffer
console.log('\nCopying an existing buffer:');
const originalBuf = Buffer.from('original');
const copiedBuf = Buffer.from(originalBuf); // Creates a new buffer with copied data
originalBuf[0] = 0x58; // Modify the original buffer
console.log('  Original after change:', originalBuf.toString());
console.log('  Copied buffer (should remain "original"):', copiedBuf.toString());

view raw JSON →