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.
npm install safe-bufferVerified import paths — ran on the pinned version, not inferred.
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()`.
Always use `Buffer.alloc(size)` for new buffer allocations unless `Buffer.allocUnsafe(size)` is explicitly required for performance and you are certain the buffer's contents will be fully overwritten immediately.
Migrate all uses of `new Buffer()` or `Buffer()` (as a function) to `Buffer.alloc()`, `Buffer.allocUnsafe()`, or `Buffer.from()` based on the desired allocation and initialization behavior.
For new projects targeting Node.js 6.0.0+, avoid `safe-buffer` and use the native `Buffer` APIs directly. For existing projects, consider removing `safe-buffer` if dropping support for Node.js versions below 6.0.0.
Strictly avoid `new Buffer(size)` and `Buffer(size)` in all code, even when using `safe-buffer`. Always explicitly use `Buffer.alloc(size)` for safe zero-filled buffers or `Buffer.allocUnsafe(size)` when raw uninitialized memory is intentionally handled.
Replace all instances of `new Buffer(...)` with `Buffer.alloc(...)` for zero-filled buffers, `Buffer.allocUnsafe(...)` for uninitialized buffers, or `Buffer.from(...)` for creating buffers from existing data.
If creating a new buffer, use `new Buffer(...)` (deprecated in modern Node.js) or preferably `Buffer.alloc(...)`, `Buffer.allocUnsafe(...)`, or `Buffer.from(...)`.
For all new allocations, use `Buffer.alloc(size)` which guarantees zero-filled memory. If `Buffer.allocUnsafe(size)` is used for performance, ensure the entire allocated memory segment is explicitly written to before any part of the buffer is exposed or returned.
No dependency data recorded yet.