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

js-struct

v1.3.0

Published

C-like structs for accessing byte arrays

Downloads

11

Readme

js-struct

Simplified access to complex byte structures in JavaScript.

js-struct makes it easy to access data in binary buffers. Multi-byte fields, byte arrays, and C-style structs can all be extracted from typed arrays.

const Inode = Struct([
  Type.uint16('mode'),
  Type.uint16('uid'),
  Type.uint32('size'),
  Type.uint16('gid'),
  Type.uint32('flags'),
  Type.array(Block, NUM_BLOCKS)('blocks'),
]);

Installing

npm install js-struct

Getting started

js-struct provides tools to pull usable JS primitives, arrays, and objects out of raw bytes. All data is designed to be fetched from Uint8Arrays, which can wrap ECMAScript ArrayBuffers or Node.js Buffers.

The library contains three ways to structure the data: pre-defined Types, Arrays, and custom Structs. These can be composed in various ways to describe the layout of byte data.

// Read the unsigned 16-bit number at index 10 of `arr`
let num = Type.uint16.read(arr, 10);

// Fetch an array of 4 bytes, starting at index 0
let bytes = Type.array(Type.byte, 4).read(arr, 0);

// Retrieve an array of 10 Points
const Point = Struct([
  Type.int32('x'),
  Type.int32('y'),
]);
let points = Type.array(Point, 10).read(arr, 0);

Supported types

The following types are available as properties on require('js-struct').Types

  • uint8, unsigned 8-bit integer
  • byte, alias for uint8
  • int8, signed 8-bit integer
  • uint16, unsigned 16-bit integer
  • ushort, alias for uint16
  • int16, signed 16-bit integer
  • short, alias for uint16
  • uint32, unsigned 32-bit integer
  • ulong, alias for uint32
  • int32, signed 32-bit integer
  • long, alias for int32
  • char, similar to byte, but renders as a String

Any of these types can be read directly from an Uint8Array, with the .read(source, index) method. As parameters, it takes the typed array, and a byte-aligned index to begin reading from.

An array of any type can be constructed as well, with Type.array(). It is a method that takes a type, and a numeric size. Arrays share the same .read() method as the standard types.

Finally, it is possible to construct more complex objects with require('js-struct').Struct. Struct is a method that takes an array of fields.

To allow naming of fields on a Struct, all Types, Arrays, and Structs themselves are also functions that take a field name.

let s = Struct([
  Type.uint8('fieldA'),
  Type.uint8('fieldB'),
]);

// Calling s.read() will return an object with two fields

console.log(s.read(arr, 0));
// {
//   fieldA: ...,
//   fieldB: ...,
// }