1.1.1 β’ Published 6 months ago
binspector v1.1.1
π΅οΈ binspector, your binary file assistant
A truly declarative TypeScript library to help you create binary file and protocol definitions.
- π£οΈ Declarative β Define binary structures using decorators.
- π Read & Write Support β Seamlessly parse & serialize binary data.
- β¬οΈ Extensible - Write custom decorators.
- ποΈ Typed β Leverage TypeScriptβs type system for validation.
- π Works in the Browser β Use Binspector for frontend or backend binary processing.
- π¦ Zero Dependencies β No external dependencies.
π What does it looks like ?
See examples for real files formats.
class ProtocolHeader {
// Ensure the header starts with specific magic number
@Match(".bin")
@Count(4)
@Ascii
extension: string
@Uint32
len: number
@Uint32
string_map_offset: number
@Uint32
string_map_size: number
}
enum RecordTypes {
RecordStart = 0x01,
RecordMsg = 0x02,
RecordEnd = 0x03,
}
class RecordMessage {
@Uint32
size: number
@Size('size')
@Utf8
message: string
}
class Record {
@Uint32
id: number
// Supports TypesSript enums
@Enum(RecordTypes)
@Uint8
type: RecordTypes
// Dynamically select a subtype based on `type`
@Choice('type', {
[RecordTypes.RecordMsg]: RecordMessage,
[RecordTypes.RecordStart]: undefined,
[RecordTypes.RecordEnd]: undefined,
})
data: RecordMessage;
}
class Protocol {
// Nested structure with a reference to another class
@Relation(ProtocolHeader)
header: ProtocolHeader
// Use values from previously read properties
@Count('header.len')
@Relation(Record)
records: Record
// Jump to an arbitrary offset and read data until size is reached
// to create an array of strings
@Offset('header.string_map_offset')
@Size('header.string_map_size')
@NullTerminatedString()
strings: string[]
}
π Features
- Declarative Class-Based Approach β Define binary structures as TypeScript classes.
- Leverages TypeScript's Type System β No need to write separate type definitions.
- Fully Extensible β Unlike DSL-based libraries, Binspector is easily extensible to create custom decorators.
- Supports Complex Binary Operations:
- Endianness
- Magic numbers and enums validation
- Conditional parsing
- Bitfields
- Nested Structure & recursive references
- Dynamic offset, variable length properties
- String encodings (UTF-8, UTF-16, UTF-32, ASCII)
- Shared context
π¦ Installation
Install Binspector from npm:
> npm install binspector
π Usage
Hereβs a simple example of reading and writing a binary coordinate system.
import { Relation, Uint8, Count } from 'binspector'
class Coord {
@Uint8
x: number
@Uint8
y: number
}
class Protocol {
@Uint8
len: number
@Count('len')
@Relation(Coord)
coords: Coord[]
}
π Reading an ArrayBuffer into Objects
import { binread, BinaryReader } from 'binspector'
const buf = new Uint8Array([0x02, 0x01, 0x02, 0x03, 0x04])
binread(new BinaryReader(buf), Protocol)
// => { len: 2, coords: [{ x: 1, y: 2 }, { x: 3, y: 4 }] }
βοΈ Writing Objects to ArrayBuffer
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]
π Learn more
- π Documentation: Getting Started
- π Examples: /example
1.1.1
6 months ago
1.1.0
6 months ago
1.0.0
6 months ago
0.5.3
7 months ago
0.5.2
8 months ago
0.5.1
9 months ago
0.5.0
9 months ago
0.4.0
9 months ago
0.3.0
9 months ago
0.2.1
10 months ago
0.2.0
10 months ago
0.1.0
10 months ago
0.1.2
10 months ago
0.1.1
10 months ago
0.0.10
11 months ago
0.0.11
11 months ago
0.0.9
11 months ago
0.0.8
11 months ago
0.0.7
11 months ago
0.0.6
11 months ago
0.0.5
1 year ago
0.0.4
1 year ago
0.0.3
1 year ago
0.0.2
1 year ago
0.0.1
2 years ago