1.0.0 • Published 2 years ago

binary-struct-ts v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

BinaryStruct.ts

A small, performant Typescript decorator-based module for working directly with raw binary data structures.

Examples

Basic Usage

import { BinaryStruct, BinaryType, sizeOf, binaryStruct, binaryField } from "binary-struct-ts";

@binaryStruct()
class ExampleStruct extends BinaryStruct
{
  @binaryField(BinaryType.Int32)
  id: number;
  @binaryField(BinaryType.Float32)
  value: number;
  @binaryField(BinaryType.Uint8)
  isDeleted: number;
  @binaryField(BinaryType.BinaryStruct, AnotherStruct)
  details: AnotherStruct;
};

const data = new ExampleStruct(new ArrayBuffer(sizeOf(ExampleStruct)));
data.id = 1234;
data.value = 3.14;
data.details = anotherStructInstance;

Read Structured Data

const data = new ExampleStruct(arrayBuffer, offsetBytes);
id = data.id;
value = data.value;
detailsValue = data.details.value;

Copy Data

const src = new ExampleStruct(arrayBuffer, srcOffsetBytes);
const dst = new ExampleStruct(arrayBuffer, dstOffsetBytes);
dst.copyFrom(src);

Additional Options

@binaryStruct({ endianness: Endianness.BigEndian,
  packingStrategy: new AlignedPackingStrategy(4) })
class ExampleStruct extends BinaryStruct
{
  @binaryField(BinaryType.Int32)
  id: number;
  @binaryField(BinaryType.Float32)
  value: number;
  @binaryField(BinaryType.Uint8)
  isDeleted: number;
  @binaryField(BinaryType.BinaryStruct, AnotherStruct)
  details: AnotherStruct;
};