API reference / @evolu/common / Buffer / Buffer
Interface: Buffer
Defined in: packages/common/src/Buffer.ts:65
A Buffer is a dynamic, resizable container for binary data, optimized for
scenarios where the final size is unknown. It grows exponentially (doubling
its capacity) to minimize memory reallocations and uses subarray for
efficient, copy-free data access in methods like unwrap and shift.
Recommended Usage
Create as few Buffers as possible—typically one main Buffer for the final
output. Temporary Buffers are allowed when necessary (e.g., for
variable-length headers), but avoid excessive allocations. Buffers can be
reused within functions by leveraging reset to clear contents while
preserving capacity, or truncate to adjust the length to a specific size,
reducing the need for new allocations. Pass Buffers to encode* functions to
append serialized data and use decode* functions to extract data. Both
shift and shiftN throw an BufferError with message "Buffer parse
ended prematurely" on failure, as do higher-level decode* functions,
providing stack traces for debugging instead of using Result. This
avoids allocation overhead in success cases and leverages exceptions'
diagnostic benefits.
Example
const buffer = createBuffer();
encodeNonNegativeInt(buffer, someInt);
encodeId(buffer, someId);
const result = buffer.unwrap(); // Final serialized data
// Decoding example (throws on error)
try {
const num = decodeNonNegativeInt(buffer);
const id = decodeId(buffer);
} catch (e) {
console.error(e.stack); // Stack trace for debugging
}
For more on exponential growth, see: https://blog.mozilla.org/nnethercote/2014/11/04/please-grow-your-buffers-exponentially
Properties
| Property | Type | Description | Defined in |
|---|---|---|---|
extend | (arg) => void | Appends binary data to the buffer, resizing if necessary. Throws if arg.length is not a non-negative integer. | packages/common/src/Buffer.ts:76 |
getCapacity | () => number & Brand<"Int"> & Brand<"NonNegative"> | Returns the current capacity of the buffer. | packages/common/src/Buffer.ts:67 |
getLength | () => number & Brand<"Int"> & Brand<"NonNegative"> | Returns the current number of bytes stored in the buffer. | packages/common/src/Buffer.ts:70 |
reset | () => void | Resets the buffer to its initial empty state, preserving its capacity. This allows efficient buffer reuse without reallocating memory. Use this when you want to clear the buffer and write new data, avoiding unnecessary allocations. | packages/common/src/Buffer.ts:103 |
shift | () => number & Brand<"Int"> & Brand<"NonNegative"> | Removes and returns the first byte. Throws an Error with message "Buffer parse ended prematurely" if the buffer is empty. | packages/common/src/Buffer.ts:82 |
shiftN | (n) => Uint8Array | Removes and returns the first n bytes. Throws an Error with message "Buffer parse ended prematurely" if fewer than n bytes remain. | packages/common/src/Buffer.ts:88 |
truncate | (length) => void | Truncates the buffer to the specified length, discarding data from the end. Throws if the new length is greater than the current length. | packages/common/src/Buffer.ts:94 |
unwrap | () => Uint8Array | Returns a view of the buffer’s current data. Do not modify this array, as it directly alters the buffer’s internal state, potentially breaking subsequent operations. | packages/common/src/Buffer.ts:110 |