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

ts-buff

v0.0.9

Published

A simple type-safe schema-based buffer serialization library in TypeScript

Downloads

19

Readme

Ts-Buff - A buffer serialization library


Ts-Buff is a simple and small serialization library that allows you to serialize and deserialize objects in a buffer format with a schema-based api. Serialized chunks can only have fixed sizes, so you will need to define maximum sizes for dynamic data types like strings or decimal types.

Installation

npm install ts-buff

Usage

Define a schema with the fields you need. Then create a buffer context with the appropriate size. (The required size can be acquired using schema.size). Once you create a buffer context you can call schema.write to write the object to the buffer, and schema.read to read it back.

Note that the buffer context will move with each read/write operation, so you will need to reset it with tsbuff.rewindContext if you want to read the buffer again.

import tsbuff from 'ts-buff';

//Define a schema
const schema = tsbuff.object({
    someNumber: tsbuff.uint32(),
    someBool: tsbuff.boolean()
});

//Create a buffer context and write to it
const context = tsbuff.bufferContext(schema.size);

schema.write(context, { 
    someNumber: 1,
    someBool: true
});

console.log(context.buffer); // -> <Buffer 01 00 00 00 01>


//Reset the context offset and read it
tsbuff.rewindContext(context);

console.log(schema.read(context)); // -> { someNumber: 1, someBool: true }

Currently the serialization completely fails up on mismatched types or malformed input, so make sure to use the correct types when reading and writing.


Due to how the serialization is implemented dynamically sized types like strings or arrays must be defined with a predefined size. This means that you will need to define a maximum size for these types.

import tsbuff from 'ts-buff';

tsbuff.string(10); // A string with a maximum size of 10
tsbuff.array(tsbuff.uint32(), 10); // An array with a maximum size of 10
Numeric types
  • tsbuff.int8(): 8-bit signed integer
  • tsbuff.int16(): 16-bit signed integer
  • tsbuff.int32(): 32-bit signed integer
  • tsbuff.uint8(): 8-bit unsigned integer
  • tsbuff.uint16(): 16-bit unsigned integer
  • tsbuff.uint32(): 32-bit unsigned integer
  • tsbuff.float32(): 32-bit floating point number
  • tsbuff.float64(): 64-bit floating point number
Other types
  • tsbuff.boolean(): a boolean
  • tsbuff.date(): a date object
  • tsbuff.string(size): a string with a fixed size
  • tsbuff.array(schema, size): an array with a fixed size
  • tsbuff.object(schema): an object with a fixed size

Utility functions

//Creates a buffer context with the given size.
tsbuff.bufferContext(size: number): BufferContext 

//Resets the buffer context offset to 0.
tsbuff.rewindContext(context: BufferContext): void 

Creating Custom Types

You can also create your own custom serializable types. Every type must have a size property that returns the size of the type in bytes, a write and read method that handle the serialization and deserialization of the type.

import { createSerializable } from 'ts-buff/core';

function int512(): Serializable<number> {
    return createSerializable({
        size: 512,
        write: (context, value) => context.buffer.writeIntLE(value, context.offset, 512),
        read: (context) => context.buffer.readIntLE(context.offset, 512) 
    });
}