|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +<img src="readme/logo.png" alt="logo" height="150"> |
| 4 | + |
| 5 | +# Typed Array Buffer Schema |
| 6 | + |
| 7 | +## A Schema based Object to Buffer converter |
| 8 | + |
| 9 | +#### Easily convert/compress your JavaScript Objects to Binary Data using a simple to use Schema. |
| 10 | + |
| 11 | +[](https://www.npmjs.com/package/@geckos.io/typed-array-buffer-schema) |
| 12 | +[](https://github.com/geckosio/typed-array-buffer-schema/actions?query=workflow%3ACI) |
| 13 | +[](https://github.com/geckosio/typed-array-buffer-schema/commits/master) |
| 14 | +[](https://www.npmjs.com/package/@geckos.io/typed-array-buffer-schema) |
| 15 | +[](https://codecov.io/gh/geckosio/typed-array-buffer-schema) |
| 16 | +[](https://www.typescriptlang.org/) |
| 17 | + |
| 18 | +</div> |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Schema |
| 23 | + |
| 24 | +```js |
| 25 | +import { BufferSchema, Model, uint8, int16, uint16 } from './index' |
| 26 | +import { string8, int64 } from './views' |
| 27 | + |
| 28 | +const playerSchema = BufferSchema.schema('player', { |
| 29 | + id: uint8, |
| 30 | + name: { type: string8, length: 6 }, |
| 31 | + x: { type: int16, digits: 2 }, |
| 32 | + y: { type: int16, digits: 2 } |
| 33 | +}) |
| 34 | + |
| 35 | +const towerSchema = BufferSchema.schema('tower', { |
| 36 | + id: uint8, |
| 37 | + health: uint8, |
| 38 | + team: uint8 |
| 39 | +}) |
| 40 | + |
| 41 | +const mainSchema = BufferSchema.schema('snapshot', { |
| 42 | + time: int64, |
| 43 | + tick: uint16, |
| 44 | + players: [playerSchema], |
| 45 | + towers: [towerSchema] |
| 46 | +}) |
| 47 | + |
| 48 | +const gameState = { |
| 49 | + time: new Date().getTime(), |
| 50 | + tick: 32580, |
| 51 | + players: [ |
| 52 | + { id: 0, name: 'Mistin', x: -14.43, y: 47.78 }, |
| 53 | + { id: 1, name: 'Coobim', x: 21.85, y: -78.48 } |
| 54 | + ], |
| 55 | + towers: [ |
| 56 | + { id: 0, health: 100, team: 0 }, |
| 57 | + { id: 1, health: 89, team: 0 }, |
| 58 | + { id: 2, health: 45, team: 1 } |
| 59 | + ] |
| 60 | +} |
| 61 | + |
| 62 | +const mainModel = new Model(mainSchema) |
| 63 | +const buffer = mainModel.toBuffer(gameState) |
| 64 | +const data = mainModel.fromBuffer(buffer) |
| 65 | + |
| 66 | +// toBuffer() shrunk the byte size from 241 to only 56 |
| 67 | +// that is -77% compression! |
| 68 | +console.log(JSON.stringify(gameState).length) // 241 |
| 69 | +console.log(buffer.byteLength) // 56 |
| 70 | +console.log(JSON.stringify(data).length) // 241 |
| 71 | +``` |
| 72 | + |
| 73 | +## DataViews |
| 74 | + |
| 75 | +A list of all supported dataViews |
| 76 | + |
| 77 | +```js |
| 78 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays |
| 79 | + |
| 80 | +/** -128 to 127 (1 byte) */ |
| 81 | +export const int8 = { _type: 'Int8Array', _bytes: 1 } |
| 82 | +/** 0 to 255 (1 byte) */ |
| 83 | +export const uint8 = { _type: 'Uint8Array', _bytes: 1 } |
| 84 | + |
| 85 | +/** -32768 to 32767 (2 bytes) */ |
| 86 | +export const int16 = { _type: 'Int16Array', _bytes: 2 } |
| 87 | +/** 0 to 65535 (2 bytes) */ |
| 88 | +export const uint16 = { _type: 'Uint16Array', _bytes: 2 } |
| 89 | + |
| 90 | +/** -2147483648 to 2147483647 (4 bytes) */ |
| 91 | +export const int32 = { _type: 'Int32Array', _bytes: 4 } |
| 92 | +/** 0 to 4294967295 (4 bytes) */ |
| 93 | +export const uint32 = { _type: 'Uint32Array', _bytes: 4 } |
| 94 | + |
| 95 | +/** -2^63 to 2^63-1 (8 bytes) */ |
| 96 | +export const int64 = { _type: 'BigInt64Array', _bytes: 8 } |
| 97 | +/** 0 to 2^64-1 (8 bytes) */ |
| 98 | +export const uint64 = { _type: 'BigUint64Array', _bytes: 8 } |
| 99 | + |
| 100 | +/** 1.2×10-38 to 3.4×1038 (7 significant digits e.g., 1.123456) (4 bytes) */ |
| 101 | +export const float32 = { _type: 'Float32Array', _bytes: 4 } |
| 102 | + |
| 103 | +/** 5.0×10-324 to 1.8×10308 (16 significant digits e.g., 1.123...15) (8 bytes) */ |
| 104 | +export const float64 = { _type: 'Float64Array', _bytes: 8 } |
| 105 | + |
| 106 | +/** 1 byte per character */ |
| 107 | +export const string8 = { _type: 'String8', _bytes: 1 } |
| 108 | +/** 2 bytes per character */ |
| 109 | +export const string16 = { _type: 'String16', _bytes: 2 } |
| 110 | +``` |
0 commit comments