Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Changelog

### [v1.1.0](https://github.com/panates/flexy-buffer/compare/v1.0.1...v1.1.0) -
### [v1.1.1](https://github.com/panates/flexy-buffer/compare/v1.1.0...v1.1.1) -

#### 💬 General Changes

- Make position settable, avoid unneeded flush() timer resets @Eray Hanoğlu
- Fill TSDoc gaps: buffer field, read-method @returns/@throws, setter @param @Eray Hanoğlu
- Bump docs/api.md baseline to the commit it now describes @Eray Hanoğlu
8 changes: 4 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!--
docs-baseline
git-commit: fbb35ad5f72bd474c8a42479aca6f862a69af50b
package-version: 1.0.1
date: 2026-09-12
git-commit: 16eea5dc89da3b232e0af14fabb15b550c35893b
package-version: 1.1.0
date: 2026-09-13
verified-against: src/
diff-command: git diff fbb35ad5f72bd474c8a42479aca6f862a69af50b..HEAD -- src/
diff-command: git diff 16eea5dc89da3b232e0af14fabb15b550c35893b..HEAD -- src/
-->

# flexy-buffer API Documentation
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "flexy-buffer",
"description": "Flexible buffer for node.js and browser.",
"version": "1.1.0",
"version": "1.1.1",
"author": "Panates",
"license": "MIT",
"private": true,
Expand Down
122 changes: 104 additions & 18 deletions src/buffer-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/
export class BufferReader {
protected _position = 0;
/**
* The buffer being read from. Exposed directly so callers can inspect or
* patch its bytes directly (e.g. `buffer.writeUInt32BE(...)` to backpatch
* a value at an earlier offset) without going through the read cursor.
*/
buffer: Buffer;

/**
Expand All @@ -30,12 +35,19 @@ export class BufferReader {
return this._position;
}

/**
* @param pos - Target position, clamped to [0, size].
*/
set position(pos: number) {
this.moveTo(pos);
}

/**
* Reads a signed 8-bit integer
* Reads a signed 8-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 1
* byte remains at the current position.
*/
readInt8(): number {
this._checkReadable(1);
Expand All @@ -45,7 +57,11 @@ export class BufferReader {
}

/**
* Reads an unsigned 8-bit integer
* Reads an unsigned 8-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 1
* byte remains at the current position.
*/
readUInt8(): number {
this._checkReadable(1);
Expand All @@ -55,7 +71,11 @@ export class BufferReader {
}

/**
* Reads a signed, big-endian 16-bit integer
* Reads a signed, big-endian 16-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 2
* bytes remain at the current position.
*/
readInt16BE(): number {
this._checkReadable(2);
Expand All @@ -65,7 +85,11 @@ export class BufferReader {
}

/**
* Reads an unsigned, big-endian 16-bit integer
* Reads an unsigned, big-endian 16-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 2
* bytes remain at the current position.
*/
readUInt16BE(): number {
this._checkReadable(2);
Expand All @@ -75,7 +99,11 @@ export class BufferReader {
}

/**
* Reads a signed, little-endian 16-bit integer
* Reads a signed, little-endian 16-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 2
* bytes remain at the current position.
*/
readInt16LE(): number {
this._checkReadable(2);
Expand All @@ -85,7 +113,11 @@ export class BufferReader {
}

/**
* Reads an unsigned, little-endian 16-bit integer
* Reads an unsigned, little-endian 16-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 2
* bytes remain at the current position.
*/
readUInt16LE(): number {
this._checkReadable(2);
Expand All @@ -95,7 +127,11 @@ export class BufferReader {
}

/**
* Reads a signed, big-endian 32-bit integer
* Reads a signed, big-endian 32-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 4
* bytes remain at the current position.
*/
readInt32BE(): number {
this._checkReadable(4);
Expand All @@ -105,7 +141,11 @@ export class BufferReader {
}

/**
* Reads an unsigned, big-endian 32-bit integer
* Reads an unsigned, big-endian 32-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 4
* bytes remain at the current position.
*/
readUInt32BE(): number {
this._checkReadable(4);
Expand All @@ -115,7 +155,11 @@ export class BufferReader {
}

/**
* Reads a signed, little-endian 32-bit integer
* Reads a signed, little-endian 32-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 4
* bytes remain at the current position.
*/
readInt32LE(): number {
this._checkReadable(4);
Expand All @@ -125,7 +169,11 @@ export class BufferReader {
}

/**
* Reads an unsigned, little-endian 32-bit integer
* Reads an unsigned, little-endian 32-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 4
* bytes remain at the current position.
*/
readUInt32LE(): number {
this._checkReadable(4);
Expand All @@ -135,7 +183,11 @@ export class BufferReader {
}

/**
* Reads a signed, big-endian 64-bit integer
* Reads a signed, big-endian 64-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 8
* bytes remain at the current position.
*/
readBigInt64BE(): BigInt {
this._checkReadable(8);
Expand All @@ -145,7 +197,11 @@ export class BufferReader {
}

/**
* Reads an unsigned, big-endian 64-bit integer
* Reads an unsigned, big-endian 64-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 8
* bytes remain at the current position.
*/
readBigUInt64BE(): BigInt {
this._checkReadable(8);
Expand All @@ -155,7 +211,11 @@ export class BufferReader {
}

/**
* Reads a signed, little-endian 64-bit integer
* Reads a signed, little-endian 64-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 8
* bytes remain at the current position.
*/
readBigInt64LE(): BigInt {
this._checkReadable(8);
Expand All @@ -165,7 +225,11 @@ export class BufferReader {
}

/**
* Reads an unsigned, little-endian 64-bit integer
* Reads an unsigned, little-endian 64-bit integer.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 8
* bytes remain at the current position.
*/
readBigUInt64LE(): BigInt {
this._checkReadable(8);
Expand All @@ -175,7 +239,11 @@ export class BufferReader {
}

/**
* Reads a 32-bit, big-endian float
* Reads a 32-bit, big-endian float.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 4
* bytes remain at the current position.
*/
readFloatBE(): number {
this._checkReadable(4);
Expand All @@ -185,7 +253,11 @@ export class BufferReader {
}

/**
* Reads a 32-bit, little-endian float
* Reads a 32-bit, little-endian float.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 4
* bytes remain at the current position.
*/
readFloatLE(): number {
this._checkReadable(4);
Expand All @@ -195,7 +267,11 @@ export class BufferReader {
}

/**
* Reads a 64-bit, little-endian double
* Reads a 64-bit, big-endian double.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 8
* bytes remain at the current position.
*/
readDoubleBE(): number {
this._checkReadable(8);
Expand All @@ -205,7 +281,11 @@ export class BufferReader {
}

/**
* Reads a 64-bit, big-endian double
* Reads a 64-bit, little-endian double.
*
* @returns The decoded value.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if fewer than 8
* bytes remain at the current position.
*/
readDoubleLE(): number {
this._checkReadable(8);
Expand All @@ -219,6 +299,9 @@ export class BufferReader {
*
* @param len - Number of bytes to read. If omitted, reads through the end
* of the buffer.
* @returns A `Buffer` view over the read bytes (not a copy).
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if `len` is given
* and that many bytes aren't available.
*/
readBytes(len?: number): Buffer {
if (len) this._checkReadable(len);
Expand All @@ -234,6 +317,9 @@ export class BufferReader {
* @param len - Number of bytes to read. A negative length reads nothing
* and returns an empty string.
* @param encoding - Text encoding used to decode the bytes.
* @returns The decoded string.
* @throws An error with code 'ERR_BUFFER_OUT_OF_BOUNDS' if `len` is
* non-negative and that many bytes aren't available.
*/
readString(len: number, encoding?: BufferEncoding): string {
if (len < 0) return '';
Expand Down
6 changes: 5 additions & 1 deletion src/flexy-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export class FlexyBuffer extends BufferReader {
return this._houseKeepMs;
}

/**
* @param value - New idle interval in milliseconds.
*/
set houseKeepMs(value: number) {
this._houseKeepMs = value;
if (this._houseKeepTimer) this._startHouseKeepTimer();
Expand Down Expand Up @@ -210,7 +213,8 @@ export class FlexyBuffer extends BufferReader {
* the inserted bytes.
*
* @param buffer - Bytes to insert.
* @returns The number of bytes the buffer grew by to make room.
* @returns The number of bytes inserted (always `buffer.length`; `size`
* grows by the same amount).
*/
insertBytes(buffer: Buffer | number[]): number {
const actual = buffer.length - Math.min(this.size - this._position, 0);
Expand Down