ya-struct
v0.0.17
Published
Yet Another Node.js Structure API
Maintainers
Readme
node-ya-struct
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.
Overview
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
Features
- 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
Installation
npm install ya-structQuick Start
import { define, types } from "ya-struct";
// Define a struct
const def = 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 settings
const parser = def.parser({
abi: {
endianness: "little",
dataModel: "LP64",
compiler: "gcc",
}
});
// Parse binary data
const value = {
a: 0n,
b: 1n,
c: 2n,
};
// Format to binary
const buffer = parser.format({ value });
// Parse back
const parsed = parser.parse({ data: buffer });API
define({ definition })
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
parser({ abi })
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
The marshalling code for parse and format is prepared on their first call, see Marshalling Backends. Definitions that cannot be marshalled (e.g. float fields) are therefore reported by the first parse or format call, not by parser().
Built-in Types
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
Custom Types
You can define custom field types:
const definition = {
type: "struct",
packed: false,
fixedAbi: {},
fields: [
{
name: "myField",
definition: {
type: "integer",
sizeInBits: 32,
signed: true,
fixedAbi: {}
}
}
]
} as const;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.)
Data Models
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)
Packed Structures
Set packed: true in your struct definition to disable padding and alignment:
const def = define({
definition: {
type: "struct",
packed: true, // No padding between fields
fixedAbi: {},
fields: [...]
}
});Fixed ABI
Every integer, float, pointer, struct and C type field has a fixedAbi, which overrides the parser's ABI for that field and everything nested in it. Use it for fields whose byte order or data model does not depend on the platform, e.g. big endian network data inside a little endian structure:
const def = define({
definition: {
type: "struct",
packed: false,
fixedAbi: {},
fields: [
{ name: "port", definition: { type: "integer", sizeInBits: 16, signed: false, fixedAbi: { endianness: "big" } } },
{ name: "flags", definition: types.UInt16 }, // parser endianness
]
}
});Marshalling Backends
parse and format pick the fastest backend available on the current platform on their first call, there is nothing to configure:
- codegen (default) - generates a specialized parse and format function for each structure at runtime. Needs code generation from strings (
new Function), which some platforms forbid, e.g. pages with a Content Security Policy without'unsafe-eval'or Node.js with--disallow-code-generation-from-strings. - interpreter - walks the structure layout on every call, works everywhere and is used when code generation is not available.
Both backends produce the same results and the same errors: the generated code only covers valid input and hands everything else to the interpreter.
Benchmarks
The benchmark/ folder contains parse and format benchmarks for structures of increasing complexity, from a struct with three integers to nested structs, integer and struct arrays, and a mixed layout with strings, blobs, pointers and pad fields. Every case runs on each marshalling backend available on the platform, with the speedup relative to the interpreter.
npm run benchmark # run all cases on all backends
npm run benchmark -- uint32-array # only cases whose name contains "uint32-array"
npm run benchmark -- --backend codegen # only the given backend
npm run benchmark -- --time 2000 --warmup 500 # longer measuring and warmup per benchmark (ms)
npm run benchmark -- --json > results.json # machine readable output for comparisonsEach case round-trips its value once before measuring, so a broken definition fails instead of benchmarking an error path. Results report the median over several timed batches, together with ops/s, throughput in MB/s and the relative margin of error.
