1.8.6 โข Published 9 months ago
tinybuf v1.8.6
๐ tinybuf ย

โกFast, compressed binary serializers in Node.js and HTML5
๐ฎ Simple, declarative API | ๐ฅ Blazing fast serialization |
๐๏ธ Powerful compression | ๐พ ^50% smaller than FlatBuffers |
๐ Zero dependencies | ๐ Strong, inferred types |
๐ Node / browser | ๐ก๏ธ Built-in validation/transforms |
๐ค ~4.4kb minzipped | โ Property mangling (Terser) |
๐ฟ Install
npm install tinybuf
๐น Example
import { defineFormat, Type } from 'tinybuf';
export const GameWorldData = defineFormat({
frameNo: Type.UInt,
timeRemaining: Type.Float16,
players: [
{
id: Type.UInt,
position: {
x: Type.Float32,
y: Type.Float32
},
joystick: {
x: Type.Scalar8,
y: Type.Scalar8
},
actions: Type.Bools // [ jump, attack ]
}
]
});
Encode
Formats can be encoded directly:
let bytes = GameWorldData.encode({
frameNo: 50,
timeRemaining: 59.334,
players: [
{
id: 1,
position: { x: 123.5, y: 456.75 },
joystick: { x: 0.75, y: -0.662 },
actions: [ /* jump: */ true,
/* attack: */ false ]
}
]
});
bytes.byteLength
// 16
Or directly from objects:
let bytes = GameWorldData.encode( obj );
bytes.byteLength
// 16
Decode
Formats can be read in a number of ways:
- Simple โ decode to object
- In-place โ decode into an existing object
- Parser โ register / decode many formats
Simple
Decode as a strongly-typed object.
let obj = GameWorldData.decode( bytes );
// { frameNo: number; timeRemaining: number; โฆ }
In-place
Use for memory effiency - extract fields directly into an existing object instance. This prevents allocating new memory.
let obj: Decoded<typeof GameWorldData> = {} as any;
GameWorldData.decode( bytes, obj );
Parser โ Decoding registered formats
- Register formats with
.on(format, handler, options?)
- Trigger format handlers with
.processBuffer(bytes)
import { bufferParser } from 'tinybuf';
// register
const parser = bufferParser()
.on(MyChatMessage, msg => myHud.showChat(msg))
.on(GameWorldData, data => myWorld.update(data), {
decodeInPlace: true, // `data` gets recycled
});
// parse
parser.processBuffer( bytes );
๐ Documentation
๐ Quick start: | Quick start guide,Types |
๐ Advanced: | Async safety mode,Format header collisions,Compression tips,Validation/transforms |
Credits
tinybuf is based on Guilherme Souza's js-binary
1.8.6
9 months ago
1.8.5
9 months ago
1.8.2
11 months ago
1.8.1
11 months ago
1.8.4
10 months ago
1.8.3
10 months ago
1.8.1-rc.0
11 months ago
1.7.3
12 months ago
1.7.2
12 months ago
1.8.0
12 months ago
1.7.1
12 months ago
1.7.0
12 months ago
1.6.7
1 year ago
1.7.5
12 months ago
1.7.4
12 months ago
1.6.6
1 year ago
1.6.5
1 year ago
1.6.4
1 year ago