UNPKG

@hamstudy/flamp

Version:

JavaScript Amateur Multicast Protocol AMP-2 Version 3 Implemented from specification document http://www.w1hkj.com/files/flamp/Amp-2.V3.0.Protocol.pdf • Version 1.0.0 - W5ALT, Walt Fair, Jr. (Derived From) • Version 2.0.0 - W1HKJ, Dave Freese, w

50 lines (49 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Compressor = void 0; const lzma_1 = require("./lzma"); class CompressorHolder { default; cMap = {}; constructor() { const PREFIX_BYTES = new Uint8Array([ 0x02, ...[0x4c, 0x5a, 0x4d, 0x41], // LZMA ]); const prefix = String.fromCharCode(...PREFIX_BYTES); this.addCompressor({ prefix, compress: (str) => { const compressed = (0, lzma_1.encodeSync)(str, 1); const out = new Uint8Array(PREFIX_BYTES.length + compressed.length); out.set(PREFIX_BYTES, 0); out.set(compressed, PREFIX_BYTES.length); return out; }, decompress: (str) => { if (str.startsWith(prefix)) { return (0, lzma_1.decodeSync)(str.substring(prefix.length)); } else { throw new Error("Invalid compressed string"); } }, }); } addCompressor(compressor) { this.default = compressor.prefix; this.cMap[compressor.prefix] = compressor; } getCompressor(prefix = this.default) { return this.cMap[prefix]; } getDecompressor(buffer) { for (let c of Object.values(this.cMap)) { if (buffer.substring(0, c.prefix.length) == c.prefix) { return c; } } return null; } } exports.Compressor = new CompressorHolder();