UNPKG

packme-js

Version:

Blazing fast binary serialization via auto-generated classes from simple JSON manifest files.

281 lines (236 loc) 5.54 kB
import { PackMe, PackMeMessage } from 'packme'; export class Animal extends PackMeMessage { /** @type {!number} */ hp; $kinIds = new Map([ [Animal, 0], [RedCat, 149136089], [Cat, 809760615], ]); static $emptyKin(id) { switch (id) { case 149136089: return new RedCat(); case 809760615: return new Cat(); default: return new Animal(); } } /** * @param {!number} hp */ constructor (hp) { if (arguments.length === 0) return super(); super(); this.hp = this.$ensure('hp', hp); } $estimate() { this.$reset(); return 12; } $pack() { this.$packUint32(this.$kinIds.get(Object.getPrototypeOf(this).constructor) ?? 0); this.$packDouble(this.hp); } $unpack() { this.hp = this.$unpackDouble(); } /** @returns {string} */ toString() { return `Animal\x1b[0m(hp: ${PackMe.dye(this.hp)})`; } } export class AnimalMessage extends PackMeMessage { /** @type {!number} */ hp; /** * @param {!number} hp */ constructor (hp) { if (arguments.length === 0) return super(); super(); this.hp = this.$ensure('hp', hp); } $estimate() { this.$reset(); return 8; } $pack() { this.$packDouble(this.hp); } $unpack() { this.hp = this.$unpackDouble(); } /** @returns {string} */ toString() { return `AnimalMessage\x1b[0m(hp: ${PackMe.dye(this.hp)})`; } } export class Cat extends Animal { /** @type {!string} */ color; /** * @param {!number} hp * @param {!string} color */ constructor (hp, color) { if (arguments.length === 0) return super(); super(hp); this.color = this.$ensure('color', color); } $estimate() { let bytes = super.$estimate(); bytes += this.$stringBytes(this.color); return bytes; } $pack() { super.$pack(); this.$packString(this.color); } $unpack() { super.$unpack(); this.color = this.$unpackString(); } /** @returns {string} */ toString() { return `Cat\x1b[0m(hp: ${PackMe.dye(this.hp)}, color: ${PackMe.dye(this.color)})`; } } export class RedCat extends Cat { /** @type {!number} */ stripes; /** * @param {!number} hp * @param {!string} color * @param {!number} stripes */ constructor (hp, color, stripes) { if (arguments.length === 0) return super(); super(hp, color); this.stripes = this.$ensure('stripes', stripes); } $estimate() { let bytes = super.$estimate(); bytes += 4; return bytes; } $pack() { super.$pack(); this.$packUint32(this.stripes); } $unpack() { super.$unpack(); this.stripes = this.$unpackUint32(); } /** @returns {string} */ toString() { return `RedCat\x1b[0m(hp: ${PackMe.dye(this.hp)}, color: ${PackMe.dye(this.color)}, stripes: ${PackMe.dye(this.stripes)})`; } } export class TestMessageItem extends PackMeMessage { /** @type {!number} */ a; /** @type {!number} */ b; /** @type {!number} */ c; /** * @param {!number} a * @param {!number} b * @param {!number} c */ constructor (a, b, c) { if (arguments.length === 0) return super(); super(); this.a = this.$ensure('a', a); this.b = this.$ensure('b', b); this.c = this.$ensure('c', c); } $estimate() { this.$reset(); return 3; } $pack() { this.$packUint8(this.a); this.$packUint8(this.b); this.$packUint8(this.c); } $unpack() { this.a = this.$unpackUint8(); this.b = this.$unpackUint8(); this.c = this.$unpackUint8(); } /** @returns {string} */ toString() { return `TestMessageItem\x1b[0m(a: ${PackMe.dye(this.a)}, b: ${PackMe.dye(this.b)}, c: ${PackMe.dye(this.c)})`; } } export class TestMessageStats extends PackMeMessage { /** @type {!number} */ a; /** @type {!number} */ b; /** @type {!number} */ c; /** * @param {!number} a * @param {!number} b * @param {!number} c */ constructor (a, b, c) { if (arguments.length === 0) return super(); super(); this.a = this.$ensure('a', a); this.b = this.$ensure('b', b); this.c = this.$ensure('c', c); } $estimate() { this.$reset(); return 3; } $pack() { this.$packUint8(this.a); this.$packUint8(this.b); this.$packUint8(this.c); } $unpack() { this.a = this.$unpackUint8(); this.b = this.$unpackUint8(); this.c = this.$unpackUint8(); } /** @returns {string} */ toString() { return `TestMessageStats\x1b[0m(a: ${PackMe.dye(this.a)}, b: ${PackMe.dye(this.b)}, c: ${PackMe.dye(this.c)})`; } } export class TestMessage extends PackMeMessage { /** @type {!TestMessageStats} */ stats; /** @type {!TestMessageItem[]} */ items; /** * @param {!TestMessageStats} stats * @param {!TestMessageItem[]} items */ constructor (stats, items) { if (arguments.length === 0) return super(); super(); this.stats = this.$ensure('stats', stats); this.items = this.$ensure('items', items); } $estimate() { this.$reset(); let bytes = 8; bytes += this.stats.$estimate(); bytes += 4 + this.items.reduce((a, b) => a + b.$estimate(), 0); return bytes; } $pack() { this.$initPack(617692482); this.$packMessage(this.stats); this.$packUint32(this.items.length); for (let i5 = 0; i5 < this.items.length; i5++) { this.$packMessage(this.items[i5]); } } $unpack() { this.$initUnpack(); this.stats = this.$unpackMessage(new TestMessageStats()); this.items = Array.from({ length: this.$unpackUint32() }, () => { return this.$unpackMessage(new TestMessageItem()); }) } /** @returns {string} */ toString() { return `TestMessage\x1b[0m(stats: ${PackMe.dye(this.stats)}, items: ${PackMe.dye(this.items)})`; } } export const p1MessageFactory = Object.freeze({ 617692482: () => new TestMessage(), });