BinarySchema
Describe a message once and get generated encoders and decoders, exact TypeScript types, and output measured in bits. Built for game networking, where every byte is paid for on every tick.
export default {
Player: { fields: {
id: field.uint16(), // 2 B
x: field.float32({ min: -1000, max: 1000 }), // 4 B
y: field.float32({ min: -1000, max: 1000 }), // 4 B
health: field.integer(7, { max: 100 }), // 7 bits
name: field.string({ maxLength: 32 }), // 2 + 4 B
isAlive: field.boolean({ default: true }), // 1 bit
} },
} satisfies Schemas; 77% smaller, health and isAlive share byte d5
Overview
Game servers send the same message shapes thousands of times a second, and JSON spends most of that bandwidth on keys and numbers written as text. BinarySchema turns a declarative schema into dedicated encode and decode functions ahead of time. The hot path is straight-line code with fixed shifts and one capacity check per message.
Generated code means no eval or new Function at runtime. It works under strict Content Security Policies, in Cloudflare Workers and with ahead-of-time compilers, and you can read and debug the output.
What I built
- 01 Integers of any width from 1 to 32 bits; consecutive booleans and integers share bytes, even across nested objects.
- 02 Optional fields cost one presence bit, and dependent fields cost zero, gated by boolean siblings that are already encoded.
- 03 Lists, nested objects, defaults, and exact min/max byte lengths computed when generating.
- 04 Validated when generating (bad widths, circular dependencies) and when encoding (ranges, lengths, patterns).
- 05 Compiles under the strictest TypeScript settings; hovering a field shows its wire type.