npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hoffer

v1.0.5

Published

Network buffering library (Package Manager)

Downloads

14

Readme

Hoffer Library

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 requires net module import).

Installation

Ensure you have Node.js and TypeScript installed. You can install the required dependencies using npm or yarn:

npm install
# or
yarn install

Usage

Importing the Class

First, import the Hoffer class in your TypeScript file:

import { Hoffer } from 'hoffer';

or

const { Hoffer } = require("hoffer")

Writing Data

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]);

Reading Data

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'));

Sending Data Over a Socket

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();
});

API

Methods

putNumber(value: number): void

Writes a 32-bit integer to the buffer.

putString(value: string): void

Writes a UTF-8 encoded string to the buffer.

putDouble(value: number): void

Writes a 64-bit double to the buffer.

putByte(value: number): void

Writes a single byte to the buffer.

putByteArray(value: number[]): void

Writes an array of bytes to the buffer.

putValue(type: string, value: any): void

Dynamically writes a value to the buffer based on the specified type.

getNumber(): number

Reads a 32-bit integer from the buffer.

getString(): string

Reads a UTF-8 encoded string from the buffer.

getDouble(): number

Reads a 64-bit double from the buffer.

getByte(): number

Reads a single byte from the buffer.

getByteArray(): number[]

Reads an array of bytes from the buffer.

getValue(type: string): any

Dynamically reads a value from the buffer based on the specified type.

reset(): void

Resets the buffer and read offset.

getData(): Buffer

Returns the current buffer.

setData(data: Buffer): void

Sets the buffer with the provided data and resets the read offset.

sendData(socket: net.Socket): void

Sends the buffer data over the provided network socket.

License

This project is licensed under the MIT License. See the LICENSE file for details.