npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@binary-format/binary-format

v1.0.0

Published

a typescript library for reading and writing binary files to and from plain javascript objects

Readme

@binary-format/binary-format

@binary-format/binary-format is a schema-first TypeScript library for decoding binary files into plain JavaScript objects and encoding them again. It favors serializable, immutable-data-friendly values over a lowest-level byte API, with a composable, Valibot-style API.

Many binary libraries make different tradeoffs around speed, generated parsers, or mutable data structures. This package intentionally favors plain objects that work well with serializing and immutable-data tools such as Immer.

Quick start

import * as bf from '@binary-format/binary-format';

const helloWorldSchema = bf.object([
  ['hello', bf.string(5)],
  ['asciiSpace', bf.uint8()],
  ['world', bf.string(5)],
  ['asciiExclamation', bf.uint8()],
]);

type HelloWorld = bf.infer<typeof helloWorldSchema>;

const bytes = new TextEncoder().encode('hello world!');
const value: HelloWorld = bf.decode(helloWorldSchema, bytes);
const encoded = bf.encode(helloWorldSchema, value);

console.log({ value, encoded });
/*
{
  value: {
    hello: 'hello',
    asciiSpace: 32,
    world: 'world',
    asciiExclamation: 33
  },
  encoded: Uint8Array(12) [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
}
*/

Documentation

Concepts

  • Build schemas with primitives, objects, arrays, choices, bytes, text, and bit fields.
  • Use bf.infer<typeof schema> to derive the decoded TypeScript value.
  • Use bf.decode(schema, bytes) and bf.encode(schema, value) at format boundaries.
  • Implement custom schemas with decode(context) and encode(context) when a format needs behavior beyond the built-in schema types.

Objects and arrays

const schema = bf.object([
  ['count', bf.uint8()],
  ['items', bf.array(bf.uint16be(), 4)],
]);

Object fields can include options as a third tuple value. For example, { omitUndefined: true } skips an undefined decoded field while still letting the field encode reserved bytes.

Text

bf.string(length) reads UTF-8 text. bf.latin1(length) reads one-byte Latin-1 text and supports explicit out-of-range handling through its second argument, for example bf.latin1(16, { outOfRange: 'replace' }) or bf.latin1(16, { outOfRange: 'truncate' }). The default is 'throw'. Other encodings can be implemented as custom schemas with an appropriate polyfill or package.

Bit fields

Group bit fields directly inside bf.object() so their widths add up to whole bytes. Use the same bit ordering within each byte. Root encoding and decoding also require whole bytes. Whole-byte bit schemas can be wrapped in root choices or custom schemas. Inside arrays or non-bit object fields, use whole-byte objects containing bit fields instead. See the composition contract for details.

Custom schemas

Any object with decode(context) and encode(context) methods can be used as a schema.

const zeroByte = bf.custom<undefined>({
  kind: 'zeroByte',
  decode: (context) => {
    if (context.buffer[context.byteOffset] !== 0) {
      throw new Error('expected zero byte');
    }
    return { result: undefined, bytesDecoded: 1, bitsDecoded: 0 };
  },
  encode: () => ({
    result: Uint8Array.of(0),
    bytesEncoded: 1,
    bitsEncoded: 0,
  }),
});

v1 migration

v1 replaces the v0 chaining API with composable schemas. The package name remains @binary-format/binary-format.

  • Replace chained format definitions with object([['field', schema], ...]).
  • Replace formatter methods with decode(schema, bytes) and encode(schema, value).
  • Encoding returns Uint8Array. Decoding accepts ArrayBuffer and views, including Node.js Buffer, without a runtime buffer dependency.
  • Use infer<typeof schema> for value types and custom() for extensions.
  • See the BMP, IP, and TCP schemas for complete examples.

This is a breaking release; the v0 chaining API is no longer exported.

Modules and tree shaking

The package provides ESM and CommonJS builds with TypeScript declarations. Development uses TypeScript 7. Package checks validate the emitted declarations, ESM/CommonJS imports, and schema inference with the project compiler.

Use ESM imports with a tree-shaking bundler to omit unused schema factories:

import { decode, uint8 } from '@binary-format/binary-format';

const value = decode(uint8(), Uint8Array.of(42));

Static namespace imports (as in the quick start) also work with modern bundlers. Dynamic property access can retain additional exports. CommonJS consumers can use require('@binary-format/binary-format'), but should use ESM for tree shaking. Each schema includes its encode and decode methods; this does not promise separate decoder-only and encoder-only bundles.

The runtime uses standard typed arrays, DataView, TextEncoder, and TextDecoder. Node.js usage requires Node.js 22 or later. Development uses Node.js 24.11+ (or 22.18+) for tsdown. CI builds once on Node 24 and tests the built package on Node 22 and 24.

Development

corepack enable
pnpm install --frozen-lockfile
pnpm test
pnpm run tsc
pnpm run build
pnpm run check

pnpm run format formats with oxfmt; pnpm run lint checks code with oxlint. pnpm run build generates the package with tsdown. Declaration generation uses Oxc because TypeScript 7 no longer exposes the classic compiler API.

pnpm run check checks formatting, linting, types, coverage, and the packed package's ESM/CommonJS entry points, declarations, and browser tree shaking. No command above publishes a release.

See the development roadmap for proposed follow-up work.

See also

Related libraries for exploring other approaches to binary formats.

Parsing libraries

Bit, byte, and buffer libraries