Binary-parser is a JavaScript/TypeScript library designed for declaratively building efficient parsers for structured binary data. It allows developers to define complex binary structures using a fluent API, supporting a wide range of data types including various integer sizes (8, 16, 32, 64-bit, signed/unsigned, big/little endian), floating-point numbers, bit fields, strings, arrays, and nested user-defined types. The library dynamically generates and compiles optimized parser code at runtime, aiming for performance comparable to hand-written parsers. The current stable version is 2.3.0, with an active development cadence including regular patch and minor releases, alongside significant major updates like v2.0.0. Key differentiators include its declarative syntax for defining complex structures and its focus on runtime performance through code generation.
npm install binary-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining and parsing a simple IP packet header from a hexadecimal buffer using the fluent API. This example uses ESM syntax and explicit Buffer import for clarity.
Upgrade your Node.js runtime to version 14 or newer. If targeting browsers, ensure modern browser compatibility where `TextDecoder` and `BigInt` are available.
Explicitly enable context variables by calling `.useContextVars()` on your parser instance, e.g., `new Parser().useContextVars().endianness('big')...`.Ensure your runtime environment supports `BigInt`. For Node.js, upgrade to v12 or newer. Note that the library's overall requirement is Node.js >=14.
Always create an instance of `Parser` using `new Parser()` before chaining methods or calling `.parse()`. Example: `const parser = new Parser(); parser.parse(buffer);`.
Upgrade your Node.js runtime to version 12 or higher. For full library compatibility and best performance, Node.js 14 or newer is recommended.
Enable context variables by adding `.useContextVars()` to your parser chain immediately after instantiation: `new Parser().useContextVars().endianness('big')...`.No dependency data recorded yet.