Declarative Specification Parser and Generator for Binary Data Formats
Speck is a TypeScript library for parsing and generating binary data formats based on declarative specifications. It is designed to be flexible and extensible, allowing users to define their own data structures and parsing rules.
and, or, not,
equals, includes, greaterThan, lessThan)boolean, int, uint, float, string, enum,
endpoint, pointerWith Deno:
deno add jsr:@unyt/speck
or with npm:
npm install @unyt/speck
or with yarn:
yarn add jsr:@unyt/speck
{
"name": "ExampleStruct",
"sections": [
{
"name": "FirstSection",
"fields": [
{
"name": "FieldA",
"byteSize": 4,
"parser": { "type": "int" }
}
]
}
]
}
import { parseStructure, type StructureDefinition } from "@unyt/speck";
const definition: StructureDefinition = {
name: "ExampleStruct",
sections: [
{
name: "FirstSection",
fields: [
{
name: "FieldA",
byteSize: 4,
parser: { type: "int" },
},
],
},
],
};
const bytes = new Uint8Array([0x01, 0x00, 0x00, 0x00]);
parseStructure(definition, bytes);
Results in:
[
{
name: "FirstSection",
fields: [
{
name: "FieldA",
bytes: Uint8Array(4)[1, 0, 0, 0],
parsedValue: 1,
},
],
},
];
name: string – Name of the structureendian: "little" | "big" (default "little")sections: list of SectionDefinitionname: stringfields: list of FieldDefinitionid: optional string identifier (used in conditions or repeat)name: stringdescription: optional stringcategory: optional stringusage: optional "omit"byteSize: number of bytes to readrepeat: number | field reference for repetitionif: optional FieldCondition to conditionally include the fieldparser: optional ValueParser to interpret bytessubFields: for nested structuresbitMasks: for extracting bit-level subfieldsLogical and comparison operators for conditional parsing:
equals, greaterThan, lessThan, includesand, or, notbooleanint, uint, floatstringenum (with mapping)endpointpointerThe result of parsing is a structured array of ParsedSection, each containing
ParsedField objects with:
namebytes (raw Uint8Array slice)parsedValue (if a parser is provided)subFields (if nested or bit-masked){
"name": "optionalField",
"byteSize": 2,
"parser": { "type": "uint" },
"if": { "equals": ["controlField", 1] }
}
This field is only parsed if controlField == 1.
{
"name": "flags",
"byteSize": 1,
"bitMasks": [
{ "name": "flagA", "length": 1, "parser": { "type": "boolean" } },
{ "name": "flagB", "length": 2, "parser": { "type": "uint" } }
]
}
This extracts flagA and flagB from a single byte.
© unyt 2025 • unyt.org
feef6bfc1ada2067453734556a58e96c96acb416