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

bytable

v0.0.6

Published

Byte Tables for binary serialization

Downloads

5

Readme

Bytable - The Core Package

npm version

The platform-agnostic library used as a core building block for browser and nodejs binary serializers. Before using this package check out bytable-node and bytable-client.

npm install bytable

The terminology defined here by author will introduce core concepts and principals the library is relying on.

Byte Table

Byte Table is a data structure describing memory allocation for underlying binary representation of an object. It can be visualized as a table with three key columns: type, offset and size, - the rows are corresponding class fields.

class Foo { 
    a: int; 
    b: string;
}

| class Foo | TYPE | OFFSET (bytes) | SIZE (bytes)| | ----------- | ------- | --------------- | ----------- | | a | int | x_a | y_a | | b | string | x_b | y_b |

Please see example below.

Static & Dynamic Types

Static Types are atomic fixed-size types (the size is known at compile time). There are limited to integer (Int8, Int16 and Int32), unsigned integer (UInt8, UInt16 and UInt32), floating point (Float and Double) and boolean (Boolean). A projection of static type data into binary representation can be identified by two parameters: offset and size. Offset is showing for how many bytes the data is shifted in memory. The size is a number of bytes to be allocated in memory starting with the offset pointer.

Dynamic Types are atomic types of dynmic sizes known only at run-time. Supported out of the box dynamic types are String and raw Binary, but it can be extended with BSON, for instance. A dynamic type is a combination of a static type header saying the data size (bynary length) and body of this size. By convention a header name is suffixed with "_SIZE" and it's UInt32 (i.e. max capacity of dynamic type is limited by UInt32.Max bytes).

Object Metadata

By design byte tables should compliment serializable classes. Probably the best option available today in javascript or typescript is decorators. The linbrary exposes a set of decorators, which can attach platform-agnostic types to javascript objects.

Each serializable class should be decorated with @proto (from protocol) and each seriazable field - with one of the decorators from a table below.

| Group | Decorator | Aliases | Size (bytes) | | ---------------- | ---------- | ---------| -------------| | Unsigned Integer | @uint8 | @u8 | 1 | | | @uint16 | @u16 | 2 | | | @uint32 | @u32 | 4 | | Signed Integer | @int8 | @i8 | 1 | | | @int16 | @i16 | 2 | | | @int32 | @i32 | 4 | | Floatin Point | @float | @f | 4 | | | @double | @d | 8 | | Boolean | @boolean | @bool | 1 | | String | @string | @s | dynamic | | Raw Binary | @binary | | dynamic | | BSON | @bson | @obj | dynamic |

Here is a typical POST Request class written in TypeScript with type decorators:

import { proto, uint8, uint16, bson, string } from 'bytable';

@proto
class Request {
    @string
    requestId: string;

    @uint8
    readonly index: number;

    @uint16
    readonly count: number;

    @bson
    payload: IPayload;
}

The underlying Byte Table, generated in run-time:

| FIELD | TYPE | OFFSET (bytes) | SIZE (bytes) | | -------------- | ----------------- | -------------- | ------------- | | requestId_SIZE | UInt32BE | 0 | 4 | | requestId | String | 4 | 123 | | index | UInt8 | 127 | 1 | | count | UInt16BE | 128 | 2 | | payload_SIZE | UInt32BE | 130 | 4 | | payload | BSON | 134 | 42 |

Dynamic fields are accomponied with a header holding the field size. The name of this header has a suffix "_SIZE" by convention.

Platform Specific

The next step is to convert an object into raw binary using a byte table. Two abstract classes are responsible for this: Reader and Writer. They are a layer of abstraction exposed to the library consumers, which should implement platform-specific bindings such as memory allocation and read/write from static types.

NodeJS and Browser implementations:

Summarizing there are two fundamental points of extension: Reader/Writer and decorators for custom dynamic types such as BSON.