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

structix

v1.0.3

Published

Define binary C-Like structures and (de-)serialize them

Downloads

3

Readme

structix

structix is a TypeScript library for defining and manipulating structured binary data. It provides a type-safe way to create, read, and write binary structures using typed arrays.

Installation

You can install structix via npm:

npm install structix

Usage

Defining a Structure

To define a structure, use the prop function to define the properties of the structure and the struct function to create the structure template.

import struct, { prop } from 'structix';

const MyStruct = struct([
  prop.i32('id'),
  prop.f32('value'),
  prop.u8('flags', 4)
]);

Creating a New Structure Instance

You can create a new instance of the defined structure with default values using struct.new.

const instance = struct.new(MyStruct);
console.log(instance.id);    // Default value for i32, typically 0
console.log(instance.value); // Default value for f32, typically 0.0
console.log(instance.flags); // Default value for u8 array, typically [0, 0, 0, 0]

Reading a Structure from a Buffer

You can read data from an ArrayBuffer into a structure using struct.read.

const buffer = new ArrayBuffer(MyStruct.size);
// Fill buffer with your binary data...
const instance = struct.read(MyStruct, buffer);
console.log(instance.id);
console.log(instance.value);
console.log(instance.flags);

Writing a Structure to a Buffer

You can write the data from a structure instance back into an ArrayBuffer using struct.write.

const buffer = new ArrayBuffer(MyStruct.size);
struct.write(instance, buffer);
// Now buffer contains the binary representation of the instance

API Reference

TypedArray

structix provides a set of typed arrays to define the types of properties in a structure:

  • i8: Int8Array
  • u8: Uint8Array
  • u8_clamped: Uint8ClampedArray
  • i16: Int16Array
  • u16: Uint16Array
  • i32: Int32Array
  • u32: Uint32Array
  • i64: BigInt64Array
  • u64: BigUint64Array
  • f32: Float32Array
  • f64: Float64Array

PropertyType

A utility type representing the keys of TypedArray.

Member

Represents a member (property) of a structure:

type Member<Type extends PropertyType = PropertyType, Name extends string = string, Count extends number = number> = {
  type: Type;
  name: Name;
  count: Count;
};

StructTemplate

Represents the template of a structure:

interface StructTemplate<S extends Member[] = Member[]> {
  size: number;
  alignment: number;
  schema: Schema<S>;
}

prop

A function to define members of a structure. It also provides shorthand functions for each property type:

prop.i32('name');
prop.u8('name', 4);

struct

A function to create a structure template and methods to read and write structures:

const MyStruct = struct([
  prop.i32('id'),
  prop.f32('value'),
  prop.u8('flags', 4)
]);

const instance = struct.new(MyStruct);
const buffer = new ArrayBuffer(MyStruct.size);
struct.write(instance, buffer);
const readInstance = struct.read(MyStruct, buffer);

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.