@cliz/inlets
Version:
Cloud Native Tunnel
130 lines (129 loc) • 4.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decompressBuffer = exports.compressBuffer = exports.decompress = exports.compress = void 0;
const zlib = require("zlib");
async function compress(data, algorithm = 'brotli') {
if (algorithm === 'none') {
const buffer = typeof data === 'string' ? Buffer.from(data) : data;
return buffer.toString('base64');
}
const buffer = typeof data === 'string' ? Buffer.from(data) : data;
return new Promise((resolve, reject) => {
if (algorithm === 'brotli') {
zlib.brotliCompress(buffer, {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 4,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: buffer.length,
}
}, (error, res) => {
if (error) {
return reject(error);
}
return resolve(res.toString('base64'));
});
}
else if (algorithm === 'gzip') {
zlib.gzip(buffer, {
level: zlib.constants.Z_DEFAULT_COMPRESSION,
}, (error, res) => {
if (error) {
return reject(error);
}
return resolve(res.toString('base64'));
});
}
else {
return reject(new Error(`Unsupported compression algorithm: ${algorithm}`));
}
});
}
exports.compress = compress;
async function decompress(data, algorithm = 'brotli') {
if (algorithm === 'none') {
return Buffer.from(data, 'base64').toString();
}
const buffer = Buffer.from(data, 'base64');
return new Promise((resolve, reject) => {
if (algorithm === 'brotli') {
zlib.brotliDecompress(buffer, (error, res) => {
if (error) {
return reject(error);
}
return resolve(res.toString());
});
}
else if (algorithm === 'gzip') {
zlib.gunzip(buffer, (error, res) => {
if (error) {
return reject(error);
}
return resolve(res.toString());
});
}
else {
return reject(new Error(`Unsupported compression algorithm: ${algorithm}`));
}
});
}
exports.decompress = decompress;
async function compressBuffer(data, algorithm = 'brotli') {
if (algorithm === 'none') {
return data;
}
return new Promise((resolve, reject) => {
if (algorithm === 'brotli') {
zlib.brotliCompress(data, {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 4,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: data.length,
}
}, (error, res) => {
if (error) {
return reject(error);
}
return resolve(res);
});
}
else if (algorithm === 'gzip') {
zlib.gzip(data, {
level: zlib.constants.Z_DEFAULT_COMPRESSION,
}, (error, res) => {
if (error) {
return reject(error);
}
return resolve(res);
});
}
else {
return reject(new Error(`Unsupported compression algorithm: ${algorithm}`));
}
});
}
exports.compressBuffer = compressBuffer;
async function decompressBuffer(data, algorithm = 'brotli') {
if (algorithm === 'none') {
return data;
}
return new Promise((resolve, reject) => {
if (algorithm === 'brotli') {
zlib.brotliDecompress(data, (error, res) => {
if (error) {
return reject(error);
}
return resolve(res);
});
}
else if (algorithm === 'gzip') {
zlib.gunzip(data, (error, res) => {
if (error) {
return reject(error);
}
return resolve(res);
});
}
else {
return reject(new Error(`Unsupported compression algorithm: ${algorithm}`));
}
});
}
exports.decompressBuffer = decompressBuffer;