UNPKG

@geckos.io/typed-array-buffer-schema

Version:

A Schema based Object to Buffer converter

54 lines 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); const views_1 = require("./views"); const playerSchema = index_1.BufferSchema.schema('player', { id: index_1.uint8, name: { type: views_1.string8, length: 6 }, x: { type: index_1.int16, digits: 2 }, y: { type: index_1.int16, digits: 2 } }); const towerSchema = index_1.BufferSchema.schema('tower', { id: index_1.uint8, health: index_1.uint8, team: index_1.uint8 }); const mainSchema = index_1.BufferSchema.schema('snapshot', { time: views_1.int64, tick: index_1.uint16, players: [playerSchema], towers: [towerSchema] }); const gameState = { time: new Date().getTime(), tick: 32580, players: [ { id: 0, name: 'Mistin', x: -14.43, y: 47.78 }, { id: 1, name: 'Coobim', x: 21.85, y: -78.48 } ], towers: [ { id: 0, health: 100, team: 0 }, { id: 1, health: 89, team: 0 }, { id: 2, health: 45, team: 1 } ] }; const mainModel = new index_1.Model(mainSchema); const buffer = mainModel.toBuffer(gameState); const data = mainModel.fromBuffer(buffer); // toBuffer() shrunk the byte size from 241 to only 56 // that is -77% compression! console.log(JSON.stringify(gameState).length); // 241 console.log(buffer.byteLength); // 56 console.log(JSON.stringify(data).length); // 241 //------------------------------------------------------------------ // Get the Schema IDs //------------------------------------------------------------------ const bufferId = index_1.BufferSchema.getIdFromBuffer(buffer); const schemaId = index_1.BufferSchema.getIdFromSchema(mainSchema); const modelId = index_1.BufferSchema.getIdFromModel(mainModel); console.log(`bufferId: ${bufferId}`); console.log(`schemaId: ${schemaId}`); console.log(`modelId: ${modelId}`); if (bufferId === schemaId && schemaId === modelId) console.log(`Schema name is "${mainSchema.name}"`); //# sourceMappingURL=dev.js.map