Node.js Buffer Cloning Utility

1.0.0 · abandoned · verified Sun Apr 19

clone-buffer is a lightweight utility designed to facilitate the creation of independent copies of Node.js `Buffer` objects. Its sole purpose is to take an existing `Buffer` instance and return a new `Buffer` that contains the same byte data but occupies a separate memory region, ensuring that modifications to the clone do not affect the original. The package is currently at its initial and only major release, v1.0.0, published in September 2016. Due to its single-release history and the deprecation of its original `new Buffer()` usage pattern, it should be considered a stable but unmaintained utility. Its key differentiator lies in its focused, single-function API, providing a clear and explicit way to clone buffers, contrasting with manual `Buffer.from(buffer)` or `Buffer.slice().copy()` approaches, offering a more direct and semantically clear way to achieve a deep copy for `Buffer` instances. It avoids common pitfalls of shallow copies, making it safer for operations where data integrity of the original buffer is paramount. The package has no active release cadence and appears to be abandoned, with no updates since its initial publication.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use `cloneBuffer` to create an independent copy of a Node.js Buffer, verifying the clone's distinctness and data integrity.

import cloneBuffer from 'clone-buffer';

// Create an original Buffer using the modern Buffer.from() API
const originalBuffer = Buffer.from('Hello, Node.js!', 'utf8');
console.log('Original Buffer:', originalBuffer.toString());

// Clone the buffer using the utility
const clonedBuffer = cloneBuffer(originalBuffer);
console.log('Cloned Buffer:', clonedBuffer.toString());

// Verify they are different objects but contain the same data
console.log('Are they the same object?', originalBuffer === clonedBuffer); // Should be false
console.log('Do they contain the same data?', originalBuffer.equals(clonedBuffer)); // Should be true

// Demonstrate independence: modify the clone
clonedBuffer[0] = 0x57; // Change 'H' to 'W' (ASCII for 'W')
console.log('Modified Cloned Buffer:', clonedBuffer.toString());
console.log('Original Buffer after clone modification:', originalBuffer.toString()); // Should remain 'Hello...' 

/* Expected output:
Original Buffer: Hello, Node.js!
Cloned Buffer: Hello, Node.js!
Are they the same object? false
Do they contain the same data? true
Modified Cloned Buffer: Wello, Node.js!
Original Buffer after clone modification: Hello, Node.js!
*/

view raw JSON →