Yet Another Node.js Structure API - A TypeScript library for defining, parsing, and formatting binary data structures with precise control over memory layout, alignment, and ABI compatibility.
node-ya-struct provides a type-safe way to work with binary data structures in Node.js. It handles complex memory layout calculations including padding, alignment, and platform-specific differences (data models, endianness, compilers). The library is particularly useful for:
- Interfacing with native C libraries and system APIs
- Parsing binary file formats
- Working with hardware interfaces and low-level protocols
- Cross-platform binary data serialization
- Type-safe definitions - Full TypeScript support with inferred types for parsed values
- Multiple data models - Support for LP64 (64-bit Unix) and ILP32 (32-bit) data models
- Packed structs - Support for packed structures without padding
- Endianness control - Little and big endian support
- Zero dependencies - Pure TypeScript implementation
npm install ya-structimport{define,types}from"ya-struct";// Define a structconstdef=define({definition: {type: "struct",packed: false,fixedAbi: {},fields: [{name: "a",definition: types.Int16},{name: "b",definition: types.UInt16},{name: "c",definition: types.UInt32},]}});// Create a parser with specific ABI settingsconstparser=def.parser({abi: {endianness: "little",dataModel: "LP64",compiler: "gcc",}});// Parse binary dataconstvalue={a: 0n,b: 1n,c: 2n,};// Format to binaryconstbuffer=parser.format({ value });// Parse backconstparsed=parser.parse({data: buffer});Creates a struct definition that can be used to create parsers with specific ABI settings.
Parameters:
definition- A struct definition object
Returns: An object with a parser() method
Creates a parser for the defined structure with specific ABI settings.
Parameters:
abi.endianness-"little"or"big"abi.dataModel-"LP64"(64-bit Unix/Linux) or"ILP32"(32-bit)abi.compiler-"gcc"(currently supported)
Returns: Parser object with:
size- Size of the structure in bytesparse({ data })- Parse binary data to JavaScript valuesformat({ value })- Format JavaScript values to binary datalayout- Detailed memory layout information
Available via types export:
- Integers:
Int16,UInt16,Int32,UInt32,Int64,UInt64 - Strings:
ascii({ length })- Fixed-length null-terminated ASCII strings - Pointers:
pointer- Platform-specific pointer size
You can define custom field types:
constdefinition={type: "struct",packed: false,fixedAbi: {},fields: [{name: "myField",definition: {type: "integer",sizeInBits: 32,signed: true,fixedAbi: {}}}]}asconst;Supported type kinds:
integer- Custom integer with specified bit widthfloat- Floating point numberspointer- Pointer typesarray- Fixed-length arraysstruct- Nested structuresstring- Fixed-length stringsc-type- Native C types (char, int, long, etc.)
The library supports different C data models:
- LP64 - Used on 64-bit Unix/Linux systems (long and pointer are 64-bit)
- ILP32 - Used on 32-bit systems (int, long, and pointer are 32-bit)
Set packed: true in your struct definition to disable padding and alignment:
constdef=define({definition: {type: "struct",packed: true,// No padding between fieldsfixedAbi: {},fields: [...]}});