@binary-format/binary-format
v1.0.0
Published
a typescript library for reading and writing binary files to and from plain javascript objects
Maintainers
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
- Public API reference: schemas, contexts, errors, and limitations.
- Bit-field composition: supported layouts and ordering.
- v1 release notes and release procedure.
- Migration guide: v0 to v1 examples and behavior to audit.
- Executable examples: checked against the packed package.
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)andbf.encode(schema, value)at format boundaries. - Implement custom schemas with
decode(context)andencode(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)andencode(schema, value). - Encoding returns
Uint8Array. Decoding acceptsArrayBufferand views, including Node.jsBuffer, without a runtime buffer dependency. - Use
infer<typeof schema>for value types andcustom()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 checkpnpm 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
- binary-parser
- bin-grammar
- node-binary
- binary-data
- restructure
- Kaitai Struct
- bin-format
- BinaryFormat
- memory-efficient-object
- @astronautlabs/bitstream
