Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
easy-buffer
Advanced tools
Easily read and write Node.js buffers. TypeScript and JavaScript supported!
Yes, yet another Buffer wrapper library. Unique selling points of this one compared to other libraries I've seen:
read
method (i.e. no readInt8
, readUInt32LE
, etc) and all writes through a single write
method.offset
method and then followed up with a chained read
call.offset
method.readArray
and writeArray
.Install the package via npm:
$ npm install easy-buffer
A quick example:
import { BufferReader, BufferWriter } from 'easy-buffer';
const writer = new BufferWriter();
const buffer = writer
.write({ type: 'UInt32LE', value: 37 })
.write({ type: 'StringNT', value: 'easy', encoding: 'ascii' })
.offset(5)
.write({ type: 'Buffer', value: Buffer.from([0x01, 0x02, 0x03]) })
.buffer();
const reader = new BufferReader(buffer);
const a = reader.read({ type: 'UInt32LE' });
const b = reader.read({ type: 'StringNT', encoding: 'ascii' });
const c = reader.offset(5).read({ type: 'Buffer', length: 3 });
console.log(a, b, c); // = 37 'easy' <Buffer 01 02 03>
See the API section below for more.
A class that facilitates easy reading of buffers.
Create a new BufferReader
instance from the specified buffer
.
The current read offset will automatically default to 0 (i.e. the position of the first byte in the buffer).
Example:
const reader = new BufferReader(buffer);
Read a value from the buffer at the current read offset, with the format of the value being determined by the specified readable
configuration.
The current read offset will be auto-incremented by the byte length of the value that was read.
Example:
const a = reader.read({ type: 'UInt16LE' });
const b = reader.read({ type: 'StringNT', encoding: 'ascii' });
const c = reader.read({ type: 'Buffer', length: 3 });
Read multiple values from the buffer, starting at the current read offset.
The specified callbackFn
function will be called continuously until the end of the buffer is reached. Therefore, please ensure that it reads one or more values from the buffer to avoid it being called infinitely. The function can return a value of your choosing (custom type T
).
Returns all of the return values of callbackFn
as an array (T[]
).
Example:
const buffer = Buffer.from([0x01, 0x02, 0x03]);
const reader = new BufferReader(buffer);
const items = reader.readArray(() => {
return { id: reader.read({ type: 'Int8' }) };
});
console.log(items); // = [{ id: 1 }, { id: 2 }, { id: 3 }]
In essence, this method is somewhat analogous to Array.prototype.map()
.
Increment/decrement the current read offset by the relative amount specified by offset
. If absolute
is true
, offset
will be treated as an exact byte position (i.e. not relative).
If the specified offset
extends beyond the bounds of the buffer (either the start or the end), an error will be thrown.
Returns the BufferReader
instance so you can chain further calls.
Example:
// move 2 bytes ahead and read
const a = reader.offset(2).read({ type: 'DoubleBE' });
// move 2 bytes back and read
const b = reader.offset(-2).read({ type: 'String' });
// move directly to byte 3 and read
const c = reader.offset(3, true).read({ type: 'Buffer' });
// move directly to the last byte (i.e. buffer.length - 1) and read
const d = reader.offset(-1, true).read({ type: 'UInt8' });
Get the remaining portion of the buffer that hasn't been read yet (i.e. beyond the current read offset).
A class that facilitates easy writing of buffers.
Create a new BufferWriter
instance.
The current write offset will automatically default to 0, since no data has been written to the buffer yet.
Example:
const writer = new BufferWriter();
Write a value to the buffer at the current write offset, with the format of the value being determined by the specified writable
configuration.
The current write offset will be auto-incremented by the byte length of the value that was written.
Returns the BufferWriter
instance so you can chain further calls.
Example:
writer.write({ type: 'Int16BE', value: -45 });
writer.write({ type: 'StringNT', value: 'hello', encoding: 'base64' });
writer.write({ type: 'Buffer', value: Buffer.from([0x01, 0x02, 0x03]) });
writer.write({ type: 'Int8', value: 7 }).write({ type: 'String', value: 'hi' });
Write multiple values to the buffer, starting at the current write offset.
The specified callbackFn
function will be called for each item in array
, allowing you to write items to the buffer sequentially.
Returns the BufferWriter
instance so you can chain further calls.
Example:
const writer = new BufferWriter();
writer.writeArray([1, 2, 3], (_, item) => {
writer.write({ type: 'Int8', value: item });
});
console.log(writer.buffer()); // = <Buffer 01 02 03>
In essence, this method is somewhat analogous to Array.prototype.map()
.
Increment/decrement the current write offset by the relative amount specified by offset
. If absolute
is true
, offset
will be treated as an exact byte position (i.e. not relative).
If the specified offset
extends beyond the current bounds of the buffer (either the start or the end), the buffer will be automatically zero-padded from the boundary of the buffer to the new offset. This is by design, but might not be to everyone's taste, so be careful!
Returns the BufferWriter
instance so you can chain further calls.
Example:
// move 2 bytes ahead and write
writer.offset(2).write({ type: 'FloatLE', value: 123.45 });
// move 2 bytes back and write
writer.offset(-2).write({ type: 'StringNT', value: 'cool', encoding: 'hex' });
// move directly to byte 3 and write
writer.offset(3, true).write({ type: 'Buffer', value: Buffer.from([0x01]) });
// move directly to the last byte (i.e. buffer.length - 1) and write
writer.offset(-1, true).write({ type: 'UInt16BE' });
Get the buffer that has been written so far.
An object literal representing a value to be read from a buffer. Essentially, a Bufferable without a value
property.
An object literal representing a value to be written to a buffer. Essentially, a Bufferable without a length
property.
An object literal representing a value to be read from or written to a buffer.
The structure of the object differs depending on what the type
property has been set to.
A signed 8-bit integer.
An unsigned 8-bit integer.
A signed, little-endian 16-bit integer.
An unsigned, little-endian 16-bit integer.
A signed, big-endian 16-bit integer.
An unsigned, big-endian 16-bit integer.
A signed, little-endian 32-bit integer.
An unsigned, little-endian 32-bit integer.
A signed, big-endian 32-bit integer.
An unsigned, big-endian 32-bit integer.
A little-endian 32-bit float.
A big-endian 32-bit float.
A little-endian 64-bit double.
A big-endian 64-bit double.
A string.
A null-terminated string.
A buffer.
FAQs
Easily read and write Node.js buffers.
The npm package easy-buffer receives a total of 2 weekly downloads. As such, easy-buffer popularity was classified as not popular.
We found that easy-buffer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.