Getting Started
Gives tools to describe binary structures with full TypeScript support. Encodes and decodes into pure JavaScript objects, while giving type context for the parsed data.
We recommend importing the library as a namespace (import * as bin from 'typed-binary'), which is what the guides
use. Bundlers only include the parts of the library you use either way, so named imports
(import { struct, u32 } from 'typed-binary') work just as well.
Install
Section titled “Install”npm install --save typed-binarypnpm add typed-binaryyarn add typed-binaryUse in the browser
Section titled “Use in the browser”import * as bin from 'typed-binary';
// Define a schemaconst Vec2f = bin.tuple([bin.f32, bin.f32]);const Vec2fSize = Vec2f.measure(bin.MaxValue);
const buffer = new ArrayBuffer(Vec2fSize);Vec2f.write(bin.cursor(buffer), [0.5, 3.14]);
// Infer the type of values the schema readstype Vec2f = bin.ExtractOut<typeof Vec2f>; // [number, number]Use in Node.js
Section titled “Use in Node.js”import * as bin from 'typed-binary';
// Define a schemaconst Vec2f = bin.tuple([bin.f32, bin.f32]);const Vec2fSize = Vec2f.measure(bin.MaxValue);
const buffer = Buffer.alloc(Vec2fSize);Vec2f.write(bin.cursor(buffer), [0.5, 3.14]);