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 🙏

© 2025 – Pkg Stats / Ryan Hefner

bytely

v0.3.0

Published

Easily parse binary data into JavaScript Objects

Readme

Bytely

bytely is a easy-to-use library for parsing binary data into structured objects. It provies an API for defining data structures in a similar way as low-level languages, specifically C. Through features like nesting, custom callbacks and debugging, bytely offers a flexible approach to work with complex binary data in a simple, clear and type-safe manner.


Installation

Install the bytely package via npm:

npm install bytely

Usage

Importing Bytely

import { createStruct } from 'bytely';

Defining Structures

Use createStruct to define a structure, and addMember to add members to the structure:

const exampleStruct = createStruct();
exampleStruct.addMember('name').pointer().string();
exampleStruct.addMember('red').uint8();
exampleStruct.addMember('green').uint8();
exampleStruct.addMember('blue').uint8();
exampleStruct.addMember('number').uint32();

Parsing Data

Once you've defined your structure, use its parse method to parse a DataView:

const view = new DataView(buffer); // DataView of buffer as argument

const data = exampleStruct.parse(view);

Example

// Example binary data setup
const buffer = new ArrayBuffer(20);
const view = new DataView(buffer);
view.setUint32(0, 12, true); // pointer to name (offset 12)
view.setUint8(4, 102); // red
view.setUint8(5, 51); // green
view.setUint8(6, 153); // blue
view.setUint32(7, 12345678, true); // number
const nameBuffer = new TextEncoder().encode('Example');
new Uint8Array(buffer).set(nameBuffer, 12);

// Parsing data
const parsedData = exampleStruct.parse(view);
console.log(parsedData);
// Output: { name: 'Example', red: 102, green: 51, blue: 153, number: 12345678 }

Custom Parsing

Custom parsing allows you to handle non-standard or more complex data. As default parameters the custom callback exposes the DataView, the current offset and all data parsed so far. Define a callback that returns the byte size of the parsed data and the parsed result:

import { createStruct } from 'bytely';

const customStruct = createStruct();
customStruct.addMember('dataLength').uint8();
customStruct.addMember('customData').custom((view, offset, data) => {
  // different handling of byte length depending on already parsed data
  if (data.dataLength === 4) {
    const value = view.getUint32(offset, true);
    return { byteSize: 4, result: value };
  } else {
    const value = view.getUint8(offset);
    return { byteSize: 1, result: value };
  }
});

Extending existing structs

You can extend existing structs to reuse and adapt already existing definitions by passing an existing structure as argument to createStruct:

const point2DStruct = createStruct();
point2DStruct.addMember('id').uint32();
point2DStruct.addMember('x').float32();
point2DStruct.addMember('y').float32();

const point3DStruct = createStruct(point2DStruct);
point3DStruct.addMember('z').float32();

Nested Structures

Define nested structures to parse hierarchical data:

const pointStruct = createStruct();
pointStruct.addMember('x').float32();
pointStruct.addMember('y').float32();

// nested struct inside another struct
const triangleStruct = createStruct();
point2DStruct.addMember('point1').pointer().struct(pointStruct);
point2DStruct.addMember('point2').pointer().struct(pointStruct);
point2DStruct.addMember('point3').pointer().struct(pointStruct);

// handling of arrays
const pointCloudStruct = createStruct();
pointCloudStruct.addMember('pointCount').uint32();
pointCloudStruct.addMember('points').pointer().array('pointCount').struct(pointStruct);

API Reference

createStruct

Creates a new structure for parsing binary data.

createStruct<T>();
createStruct<T>(existingStruct);

addMember

Adds a member to the structure.

addMember(name);

Supported Data Types

  • uint8, uint16, uint32, int32
  • float32
  • pointer
  • string
  • struct, structByType
  • array
  • custom

Endianness

Currently the data type parsing only supports little-endian, but there are plans to add big-endian support in the future.


Debugging

Enable debugging by passing an options object with debug: true to any parsing method. This will log the member name, current offset and parsed data value:

const data = struct.parse(view, 0, { debug: true });
// Output: name offset dataValue

TypeScript Support

bytely is fully typed, allowing you to define and parse structures with strict type checking.

interface Example {
  name: string;
  red: number;
  green: number;
  blue: number;
  number: number;
}

const exampleStruct = createStruct<Example>();
exampleStruct.addMember('name').pointer().string();
exampleStruct.addMember('red').uint8();
exampleStruct.addMember('green').uint8();
exampleStruct.addMember('blue').uint8();
exampleStruct.addMember('number').uint32();

const parsedData = exampleStruct.parse(view);
// parsedData is fully typed according to its struct definition

License

This project is licensed under the MIT License.


Contributing

Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.