A high-performance TypeScript library for binary data manipulation in Node.js applications. JSBinaryUtils provides efficient buffer management without the overhead of repeated allocations, making it ideal for real-time applications and network protocols.
- Zero-copy operations - Optimized buffer management with automatic capacity growth
- Comprehensive API - Read/write support for all standard binary data types
- Variable-length encoding - Built-in VarInt and VarLong support
- Endianness control - Big-endian and little-endian operations
- Type-safe - Full TypeScript support with type definitions
- Well-tested - Extensive test coverage
npm install @jsprismarine/jsbinaryutilsimport BinaryStream from '@jsprismarine/jsbinaryutils';
const buffer = Buffer.from([0xFF, 0x00, 0x7F, 0x80]);
const stream = new BinaryStream(buffer);
const byte = stream.readByte(); // 255
const signed = stream.readSignedByte(); // 0
const short = stream.readShort(); // 32640import BinaryStream from '@jsprismarine/jsbinaryutils';
const stream = new BinaryStream();
stream.writeByte(255);
stream.writeShort(32640);
stream.writeVarInt(12345);
const result = stream.getWriteBuffer();readByte()/writeByte(v)- Unsigned byte (0-255)readSignedByte()/writeSignedByte(v)- Signed byte (-128 to 127)readBoolean()/writeBoolean(v)- Boolean value
readShort()/writeShort(v)- 16-bit signed integer (BE)readShortLE()/writeShortLE(v)- 16-bit signed integer (LE)readUnsignedShort()/writeUnsignedShort(v)- 16-bit unsigned integer (BE)readInt()/writeInt(v)- 32-bit signed integer (BE)readUnsignedInt()/writeUnsignedInt(v)- 32-bit unsigned integer (BE)
readTriad()/writeTriad(v)- 24-bit signed integer (BE)readTriadLE()/writeTriadLE(v)- 24-bit signed integer (LE)readUnsignedTriad()/writeUnsignedTriad(v)- 24-bit unsigned integer (BE)
readFloat()/writeFloat(v)- 32-bit float (BE)readFloatLE()/writeFloatLE(v)- 32-bit float (LE)readDouble()/writeDouble(v)- 64-bit double (BE)readDoubleLE()/writeDoubleLE(v)- 64-bit double (LE)
readLong()/writeLong(v)- 64-bit signed BigInt (BE)readLongLE()/writeLongLE(v)- 64-bit signed BigInt (LE)readUnsignedLong()/writeUnsignedLong(v)- 64-bit unsigned BigInt (BE)
readVarInt()/writeVarInt(v)- 32-bit zigzag-encoded VarIntreadUnsignedVarInt()/writeUnsignedVarInt(v)- 32-bit unsigned VarIntreadVarLong()/writeVarLong(v)- 64-bit zigzag-encoded VarLongreadUnsignedVarLong()/writeUnsignedVarLong(v)- 64-bit unsigned VarLong
read(length)- Read raw byteswrite(buffer)- Write raw bytesskip(length)- Skip bytesreadRemaining()- Read all remaining bytesgetReadBuffer()/getWriteBuffer()- Get underlying buffers
setReadBuffer(buffer, index?)- Set read buffersetWriteBuffer(buffer, index?)- Set write buffergetReadIndex()/setReadIndex(index)- Manage read positiongetWriteIndex()/setWriteIndex(index)- Manage write positionclear()- Reset streamreuse(buffer)- Reuse stream with new bufferfeof()- Check end of buffer
Full API documentation with detailed method descriptions and examples is available at: https://jsprismarine.github.io/JSBinaryUtils/
JSBinaryUtils uses a dynamic buffer allocation strategy that minimizes memory overhead:
- Initial allocation: 256 bytes or required size
- Growth strategy: 2x current capacity when needed
- No intermediate allocations during writes
This approach significantly outperforms naive Buffer.concat() operations in high-throughput scenarios.
ISC
Contributions are welcome. Please open an issue or submit a pull request on GitHub.