@craftycodie/cstruct
v1.1.1
Published
Stage 3 struct decorators for packed binary layouts
Downloads
1,530
Maintainers
Readme
@craftycodie/cstruct
Declarative packed binary readers and writers for TypeScript, inspired by binrw.
Features
- Struct layouts with Stage 3 decorators (
@c.struct,@c.field) c.read,c.write, andc.sizeofoverUint8Arraywith little or big endian- Padding, nested structs, discriminated unions, and validated enums
- Built-in
c.String,c.WString, andc.Time64; extend withc.AdvancedType
Usage
import { c } from "@craftycodie/cstruct";
@c.struct()
class Dog {
@c.field("u8")
bone_pile_count!: number;
@c.field("u16", { pad_before: 1 })
favorite_bone!: number;
@c.field(c.String(16))
name!: string;
}
const dog = new Dog();
dog.bone_pile_count = 2;
dog.favorite_bone = 0x12;
dog.name = "Rudy";
const bytes = c.write(Dog, dog, "little");
const parsed = c.read(Dog, bytes, "little");
console.log(parsed.favorite_bone); // 18
console.log(parsed.name); // "Rudy"For guides, API reference, changelog, and more examples, see the documentation.
Install
npm install @craftycodie/cstructRequires Stage 3 decorators (experimentalDecorators: false, decoratorVersion: "2022-03" with SWC/Vitest). Import @craftycodie/cstruct before defining struct classes.
License
MIT
