BinaryStream is a TypeScript package designed to facilitate the reading and writing of binary data. This package leverages the ArrayBuffer and DataView interfaces, making it compatible with both Node.js and browser environments.
- Supports both Node.js and browser environments.
- Provides an easy-to-use API for handling binary data.
- Methods for reading and writing various data types (integers, floats, strings, etc.).
- Allows for dynamic buffer creation without worrying about resizing.
You can install the BinaryStream package via npm:
npm install @pocketnode/binarystreamor
bun install @pocketnode/binarystreamIn a TypeScript or JavaScript file, import the BinaryStream module:
import { BinaryStream } from "@pocketnode/binarystream";To create a new instance of BinaryStream, you can either provide an existing ArrayBuffer or specify the size of a new buffer:
// Create a BinaryStream with a new ArrayBuffer of 1024 bytes
const stream = new BinaryStream(1024);
// Create a BinaryStream from an existing ArrayBuffer
const buffer = new ArrayBuffer(1024);
const streamFromBuffer = new BinaryStream(buffer);Here is a complete example demonstrating how to write and read data using BinaryStream:
import { BinaryStream } from "@pocketnode/binarystream";
// Create a BinaryStream with a new ArrayBuffer of 1024 bytes
const stream = new BinaryStream(1024);
// Write data to the stream
stream.writeUInt8(255);
stream.writeInt16(-32768);
stream.writeFloat32(3.14);
stream.writeString("Hello, BinaryStream!");
// Reset the position to the beginning of the stream for reading
stream.flip();
// Read data from the stream
const uint8 = stream.readUInt8();
const int16 = stream.readInt16();
const float32 = stream.readFloat32();
const str = stream.readString();
console.log(uint8); // 255
console.log(int16); // -32768
console.log(float32); // 3.14
console.log(str); // Hello, BinaryStream!BinaryStream works in both Node.js and browser environments. It relies on ArrayBuffer and DataView, which are available in modern browsers and Node.js.
This project is licensed under the GNU GPLv3 License. See the LICENSE file for details.
For questions or feedback, please open an issue on GitHub.