UNPKG

@devgrid/messagepack

Version:
45 lines 1.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const smartbuffer_1 = require("@devgrid/smartbuffer"); const encoder_1 = __importDefault(require("./encoder")); const decoder_1 = __importDefault(require("./decoder")); class Serializer { constructor(initialCapacity = 64) { this.initialCapacity = initialCapacity; this.encodingTypes = new Map(); this.decodingTypes = new Map(); this.encoder = new encoder_1.default(this.encodingTypes); this.decoder = new decoder_1.default(this.decodingTypes); } registerEncoder(type, check, encode) { this.encodingTypes.set(type, { check, encode }); return this; } registerDecoder(type, decode) { this.decodingTypes.set(type, decode); return this; } register(type, constructor, encode, decode) { if (type < 0 || type > 127) { throw new RangeError(`Bad type: 0 <= ${type} <= 127`); } this.registerEncoder(type, (obj) => obj instanceof constructor, (obj) => { const extBuf = new smartbuffer_1.SmartBuffer(this.initialCapacity, true); encode(obj, extBuf); return extBuf; }); this.registerDecoder(type, decode); return this; } encode(x, buf) { return this.encoder.encode(x, buf); } decode(buf) { return this.decoder.decode(buf); } } exports.default = Serializer; //# sourceMappingURL=serializer.js.map