0.0.6 • Published 6 years ago

bytable v0.0.6

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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 FooTYPEOFFSET (bytes)SIZE (bytes)
aintx_ay_a
bstringx_by_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.

GroupDecoratorAliasesSize (bytes)
Unsigned Integer@uint8@u81
@uint16@u162
@uint32@u324
Signed Integer@int8@i81
@int16@i162
@int32@i324
Floatin Point@float@f4
@double@d8
Boolean@boolean@bool1
String@string@sdynamic
Raw Binary@binarydynamic
BSON@bson@objdynamic

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:

FIELDTYPEOFFSET (bytes)SIZE (bytes)
requestId_SIZEUInt32BE04
requestIdString4123
indexUInt81271
countUInt16BE1282
payload_SIZEUInt32BE1304
payloadBSON13442

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.