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

59 lines (58 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.encodeSync = encodeSync; exports.encode = encode; exports.decodeSync = decodeSync; exports.decode = decode; const tslib_1 = require("tslib"); const lzma = tslib_1.__importStar(require("./lzma-js")); const encoder = new TextEncoder(); function normalizeToUint8Array(res) { if (res instanceof Uint8Array) { return res; } else if (typeof res === 'string') { return encoder.encode(res); } else { return Uint8Array.from(res); } } function encodeSync(str, mode = 4) { const res = lzma.compress(str, mode); return normalizeToUint8Array(res); } function encode(str, mode = 4) { return new Promise((resolve, reject) => { lzma.compress(str, mode, (res, err) => { if (err) { reject(err); } else { resolve(normalizeToUint8Array(res)); } }); }); } function decodeSync(stringToDecode) { const byteArray = stringToDecode instanceof Uint8Array ? stringToDecode : Uint8Array.from(stringToDecode.split('').map((c) => c.charCodeAt(0))); const res = lzma.decompress(byteArray); return normalizeToUint8Array(res); } function decode(stringToDecode) { const byteArray = stringToDecode instanceof Uint8Array ? stringToDecode : Uint8Array.from(stringToDecode.split('').map((c) => c.charCodeAt(0))); return new Promise((resolve, reject) => { lzma.decompress(byteArray, (res, err) => { if (err) { reject(err); } else { resolve(normalizeToUint8Array(res)); } }); }); }