Skip to content
BSD
Esc
navigateopen⌘Jpreview
On this page

Getting Started

Install BSD and decode a typed binary record.

Install BSD, define a schema, and decode a Uint8Array. BSD is ESM-only and ships TypeScript declarations.

Install and decode

Install BSD

Add BSD to a new or existing TypeScript project.

npm install @marceloclp/bsd
pnpm add @marceloclp/bsd
yarn add @marceloclp/bsd
bun add @marceloclp/bsd

Define the schema

Schemas consume fields sequentially. This record contains a version, ID, and length-prefixed UTF-8 name.

import { bytes, struct, u8, u16 } from "@marceloclp/bsd";
import type { BsdInfer } from "@marceloclp/bsd";

export const Profile = struct({
  version: u8().is(1),
  id: u16(),
  name: bytes(u8()).utf8(),
});

export type Profile = BsdInfer<typeof Profile>;

Decode the input

decode() accepts a Uint8Array. Strict mode rejects trailing bytes.

const input = Uint8Array.of(
  1,          // version
  42, 0,      // id: 42
  3,          // name byte length
  65, 100, 97 // "Ada"
);

const profile = Profile.decode(input, { strict: true });
// { version: 1, id: 42, name: "Ada" }

Understand the schema

Schema Bytes consumed Result
u8().is(1) 1 Literal 1; other values fail
u16() 2 Little-endian unsigned integer
bytes(u8()).utf8() 1 + stored length UTF-8 string

The name length controls decoding but is absent from the result. struct() returns only its declared fields, and BsdInfer derives their exact TypeScript type.

Was this page helpful?