1.8.6 โ€ข Published 9 months ago

tinybuf v1.8.6

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

๐Ÿ”Œ tinybuf ย NPM version Minzipped Downloads Tests License

โšก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:

  1. Simple โ€“ decode to object
  2. In-place โ€“ decode into an existing object
  3. 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