Treat multiple Buffers as a single contiguous Buffer.
This library can be used to manipulate data from multiple buffers without merging the buffers. In this way, the expensive concat operation could be avoided. Internally it interates over the items from the array of Buffer objects.
The standard Buffer functions like indexOf, slice, etc. are rewritten to work with chunked data.
The API is compatible with the Node.js Buffer API, so it seamlessly integrates with your existing codebase.
Contains type definitions for TypeScript.
npm i buffer-collection
const BufferCollection = require('buffer-collection');
const buf = new BufferCollection();
buf.push(Buffer.from([1, 2, 3]));
buf.push([4]); // automatic conversion to Buffer
buf.push([5, 6, 7]);
// <BufferCollection 02 03 04 05>
console.log(buf.slice(1, 5));
const needle = Buffer.from([2, 3]);
// 1
console.log(buf.indexOf(needle));
// <BufferCollection 01 02 03 01 02 03 01>
console.log(buf.fill(Buffer.from([1, 2, 3])));
// 4
console.log(buf.lastIndexOf(needle));
buf.writeInt32BE(0xdeadbeef, 1)
// <BufferCollection 01 de ad be ef 03 01>
console.log(buf);
const bytes = [];
for (const b of buf) {
bytes.push(b.toString(16));
}
// [ '1', 'de', 'ad', 'be', 'ef', '3', '1' ]
console.log(bytes);
The API is a superset of the Node.js Buffer API, so it seamlessly integrates with your existing codebase. The only Buffer feature what is missing from BufferCollection is the [] operator. But you can use .get() in place of that.
Adds a new Buffer to the end of the current collection.
Merges internal array of buffers to a single buffer. It should be called when data becomes very fragmented.
Returns the number of Buffer instances in collection.
Gets the byte value from the specified offset. Can be used as replacement for buf[offset]. Same as buf.readUInt8(offset)
Sets the byte value at the specified offset. Can be used as replacement for buf[offset]. Same as buf.writeUInt8(value, offset)
Removes the first Buffer from collection and returns it. This method changes the length of the data of BufferCollection.
Returns a new BufferCollection that references the same memory as the original, but offset and cropped by the start and end indices. Works similarly as the slice method of Buffers.
These methods were adapted to work on multiple Buffer instances without merging them into one contiguous memory section.
Allocates a new Buffer of size bytes. If fill is undefined, the Buffer will be zero-filled.
See Node.js documentation regarding the usage.
Allocates a new Buffer of size bytes. The underlying memory for Buffer instances created in this way is not initialized. The contents of the newly created Buffer are unknown and may contain sensitive data.
See Node.js documentation regarding the usage.
Allocates a new Buffer of size bytes. The underlying memory for Buffer instances created in this way is not initialized. The contents of the newly created Buffer are unknown and may contain sensitive data.
See Node.js documentation regarding the usage.
Allocates a new Buffer of size bytes. The underlying memory for Buffer instances created in this way is not initialized. Not using allocation pools.
See Node.js documentation regarding the usage.
Compares buf with target and returns a number indicating whether buf comes before, after, or is the same as target in sort order.
See Node.js documentation regarding the usage.
Copies data from a region of buf to a region in target even if the target memory region overlaps with buf.
See Node.js documentation regarding the usage.
Returns a new BufferCollection
, which contains all the Buffer
and BufferCollection
instances from the list
. If the combined length of the Buffers
in list exceeds totalLength
, the result is truncated to totalLength
.
See Node.js documentation regarding the usage.
Creates and returns an iterator of [index, byte] pairs from the contents of buf.
See Node.js documentation regarding the usage.
Returns true if both buf and otherBuffer have exactly the same bytes, false otherwise.
See Node.js documentation regarding the usage.
Fills buf with the specified value.
See Node.js documentation regarding the usage.
Equivalent to buf.indexOf() !== -1.
See Node.js documentation regarding the usage.
Returns the first index at which a given element can be found in the Buffers, or -1 if it is not present.
See Node.js documentation regarding the usage.
Creates and returns an iterator of buf keys (indices).
See Node.js documentation regarding the usage.
Identical to buf.indexOf(), except buf is searched from back to front instead of front to back.
See Node.js documentation regarding the usage.
Returns the amount of memory allocated for buf in bytes. Note that this does not necessarily reflect the amount of "usable" data within buf.
See Node.js documentation regarding the usage.
Reads a 64-bit double from buf at the specified offset with the corresponding endianness.
See Node.js documentation regarding the usage.
Reads a 32-bit float from buf at the specified offset with the corresponding endianness.
See Node.js documentation regarding the usage.
Reads a signed integer from buf at the specified offset.
See Node.js documentation regarding the usage.
Reads byteLength number of bytes from buf at the specified offset and interprets the result as a two's complement signed value with the big endian format. Supports up to 48 bits of accuracy.
See Node.js documentation regarding the usage.
Reads byteLength number of bytes from buf at the specified offset and interprets the result as a two's complement signed value with the little endian format. Supports up to 48 bits of accuracy.
See Node.js documentation regarding the usage.
Reads an unsigned integer from buf at the specified offset.
See Node.js documentation regarding the usage.
Reads byteLength number of bytes from buf at the specified offset and interprets the result as an unsigned integer with the big endian format. Supports up to 48 bits of accuracy.
See Node.js documentation regarding the usage.
Reads byteLength number of bytes from buf at the specified offset and interprets the result as an unsigned integer with the little endian format. Supports up to 48 bits of accuracy.
See Node.js documentation regarding the usage.
Returns a new BufferCollection that references the same memory as the original, but offset and cropped by the start and end indices.
See Node.js documentation regarding the usage.
Interprets buf as an array of unsigned 16-bit integers and swaps the byte-order in-place.
See Node.js documentation regarding the usage.
Interprets buf as an array of unsigned 32-bit integers and swaps the byte-order in-place.
See Node.js documentation regarding the usage.
Interprets buf as an array of unsigned 64-bit integers and swaps the byte-order in-place.
See Node.js documentation regarding the usage.
Returns a JSON representation of buf. JSON.stringify() implicitly calls this function when stringifying a BufferCollection instance.
See Node.js documentation regarding the usage.
Decodes buf to a string according to the specified character encoding in encoding.
See Node.js documentation regarding the usage.
Creates and returns an iterator for buf values (bytes). This function is called automatically when a Buffer is used in a for..of statement.
See Node.js documentation regarding the usage.
Writes string to buf at offset according to the character encoding in encoding. The length parameter is the number of bytes to write.
See Node.js documentation regarding the usage.
Writes value to buf at the specified offset with the corresponding endianness. value should be a valid 64-bit double.
See Node.js documentation regarding the usage.
Writes value to buf at the specified offset with the corresponding endianness. value should be a valid 32-bit float.
See Node.js documentation regarding the usage.
Writes value to buf at the specified offset. value should be a valid signed integer.
See Node.js documentation regarding the usage.
Writes byteLength bytes of value to buf at the specified offset with big endian format. Supports up to 48 bits of accuracy. Behavior is undefined when value is anything other than a signed integer.
See Node.js documentation regarding the usage.
Writes byteLength bytes of value to buf at the specified offset with little endian format. Supports up to 48 bits of accuracy. Behavior is undefined when value is anything other than a signed integer.
See Node.js documentation regarding the usage.
Writes value to buf at the specified offset. value should be a valid unsigned integer.
See Node.js documentation regarding the usage.
Writes byteLength bytes of value to buf at the specified offset with big endian format. Supports up to 48 bits of accuracy. Behavior is undefined when value is anything other than an unsigned integer.
See Node.js documentation regarding the usage.
Writes byteLength bytes of value to buf at the specified offset with little endian format. Supports up to 48 bits of accuracy. Behavior is undefined when value is anything other than an unsigned integer.
See Node.js documentation regarding the usage.
❌ buf[index] - please use buf.get(index) and buf.set(offset, value)
MIT