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

binspector

v0.2.0

Published

A _truly_ declarative library for binary file and protocol definition written in typescript. Read binary files based on class definition and decorators directly from your webapp.

Downloads

617

Readme

🕵️ binspector, your binary file assistant

A truly declarative library for binary file and protocol definition written in typescript. Read & Write binary files based on class definition and decorators directly on your webapp.

class ProtocolHeader {
  // Validate magic number
  @Match(0x0E)
  @Relation(PrimitiveSymbol.u8)
  magic: number

  // Read the subtype relation multiple time
  @Count(4)
  @Relation(PrimitiveSymbol.char)
  extension: string

  @Relation(PrimitiveSymbol.u32)
  len: number

  @Relation(PrimitiveSymbol.u32)
  string_map_offset: number

  @Relation(PrimitiveSymbol.u32)
  string_map_size: number

  @Relation(PrimitiveSymbol.u32)
  crc: number
}

enum RecordTypes {
  RecordStart = 0x01,
  RecordMsg = 0x02,
  RecordEnd = 0x03,
}

class RecordMessage {
  @Until('\0')
  @Relation(PrimitiveSymbol.char)
  message: string
}

class Record {
  @Relation(PrimitiveSymbol.u32)
  id: number

  // Typescript enums are supported.
  @Enum(RecordTypes)
  @Relation(PrimitiveSymbol.u8)
  type: RecordTypes

  // You can select the subtype that's gonna be {read,written} based on a
  // condition
  @Choice('type', {
    [RecordTypes.RecordMsg]: RecordMessage,
    [RecordTypes.RecordStart]: undefined,
    [RecordTypes.RecordEnd]: undefined,
  })
  data: RecordMessage;
}

class Protocol {
  // Refer to other object as subtype
  @Relation(ProtocolHeader)
  header: ProtocolHeader

  // Refer to properties directly to use dynamic value 
  @Count('header.len')
  @Relation(Record)
  records: Record

  // Jump to an arbitrary address to continue reading the file
  // Here null terminated strings would keep being read until the overall size
  // is reached
  @Offset('header.string_map_offset')  
  @Size('header.string_map_size')  
  @NullTerminatedString()
  @Relation(PrimitiveSymbol.char)
  strings: string[]
}

Features

  • It's a class ! Write method that will directly handle the binary content from the definition.
  • The binary file definition is re-used for typescript type checking.
  • Extensible. Binary readers based on DSL are hard to extend.
  • Support parsing and serialisation of binary file definition.
  • It works on the browser. You can create binary file decoder and encoder on your webapp frontend without depending on other library on your server.
  • Support the common operation done on binary files.
    • Endianness
    • Matching magic numbers
    • Bit fields and enum
    • Reference other structure
    • Conditions
  • No dependencies

Usage

Imagine the following binary file definition.

import { Relation, Count } from 'binspector'

class Coord {
  @Relation(PrimitiveSymbol.u8)
  x: number

  @Relation(PrimitiveSymbol.u8)
  y: number
}

class Protocol {
  @Relation(PrimitiveSymbol.u8)
  len: number

  @Count('len')
  coords: Coord[]
}

Reading bytes buffer into objects

import { binread, BinaryReader } from 'binspector'

const buf = new Uint8Array([0x02, 0x01, 0x02, 0x03, 0x04]).buffer

binread(new BinaryHeader(buf), Protocol) // => { len: 2, coords: [{ x: 1, y: 2 }, { x: 3, y: 4 }] }

Writing objects to bytes buffer

import { binwrite, BinaryWriter } from 'binspector'

const obj = { len: 2, coords: [{ x: 1, y: 2 }, { x: 3, y: 4 }] }

binwrite(new BinaryWriter(), Protocol, obj).buffer() // => [0x02, 0x01, 0x02, 0x03, 0x04]

Installation

> npm install binspector

Generate the documentation with the following command.

> npx typedoc --options typedoc.json