Hoffer.ts: Network Data Serialization and Deserialization Library
This TypeScript library, Hoffer.ts
, provides a convenient and efficient way to serialize and deserialize various data types for network communication. It offers functionalities for both writing and reading data in a structured format, making it ideal for building network protocols or exchanging data between applications.
Key Features:
- Supported Data Types: Handles numbers, strings, doubles, bytes, and byte arrays.
- Write Methods:
putNumber(value: number)
: Writes an integer value.putString(value: string)
: Writes a string, preceded by its length.putDouble(value: number)
: Writes a double-precision floating-point number.putByte(value: number)
: Writes a single byte value.putByteArray(value: number[])
: Writes an array of bytes.putValue(type: string, value: any)
: Generic method for writing data based on type.
- Read Methods:
getNumber()
: Reads an integer value.getString()
: Reads a string, respecting its preceding length.getDouble()
: Reads a double-precision floating-point number.getByte()
: Reads a single byte value.getByteArray()
: Reads an array of bytes.getValue(type: string)
: Generic method for reading data based on type.
- Buffer Management:
reset()
: Clears the internal buffer for reuse.getData()
: Retrieves the current data buffer for sending.setData(data: Buffer)
: Sets the internal buffer with received data for reading.
- Network Integration:
sendData(socket: net.Socket)
: Sends the data buffer through a provided socket connection (example usage provided, but requiresnet
module import).
Ensure you have Node.js and TypeScript installed. You can install the required dependencies using npm or yarn:
npm install
# or
yarn install
First, import the Hoffer
class in your TypeScript file:
import { Hoffer } from 'hoffer';
or
const { Hoffer } = require("hoffer")
You can write various data types to the buffer using the provided methods:
const hoffer = new Hoffer();
hoffer.putNumber(123);
hoffer.putString("John Doe");
hoffer.putDouble(4567.89);
hoffer.putByte(0x1F);
hoffer.putByteArray([0x01, 0x02, 0x03, 0x04]);
// Using putValue for dynamic type handling
hoffer.putValue('number', 42);
hoffer.putValue('string', "Hello, World!");
hoffer.putValue('double', 3.14159);
hoffer.putValue('byte', 0x2A);
hoffer.putValue('byteArray', [0x10, 0x20, 0x30]);
Read the data back from the buffer:
const readHoffer = new Hoffer();
readHoffer.setData(data); // the data == buffer
const id = readHoffer.getNumber();
const name = readHoffer.getString();
const balance = readHoffer.getDouble();
const byteValue = readHoffer.getByte();
const byteArrayValue = readHoffer.getByteArray();
console.log("ID:", id);
console.log("Name:", name);
console.log("Balance:", balance);
console.log("Byte Value:", byteValue);
console.log("Byte Array Value:", byteArrayValue);
// Using getValue for dynamic type handling
console.log("Generic Number:", readHoffer.getValue('number'));
console.log("Generic String:", readHoffer.getValue('string'));
console.log("Generic Double:", readHoffer.getValue('double'));
console.log("Generic Byte:", readHoffer.getValue('byte'));
console.log("Generic Byte Array:", readHoffer.getValue('byteArray'));
To send data over a network socket, use the sendData
method:
const client = new net.Socket();
client.connect(12345, 'localhost', () => {
readHoffer.sendData(client);
client.end();
});
Writes a 32-bit integer to the buffer.
Writes a UTF-8 encoded string to the buffer.
Writes a 64-bit double to the buffer.
Writes a single byte to the buffer.
Writes an array of bytes to the buffer.
Dynamically writes a value to the buffer based on the specified type.
Reads a 32-bit integer from the buffer.
Reads a UTF-8 encoded string from the buffer.
Reads a 64-bit double from the buffer.
Reads a single byte from the buffer.
Reads an array of bytes from the buffer.
Dynamically reads a value from the buffer based on the specified type.
Resets the buffer and read offset.
Returns the current buffer.
Sets the buffer with the provided data and resets the read offset.
Sends the buffer data over the provided network socket.
This project is licensed under the MIT License. See the LICENSE file for details.