@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
130 lines (129 loc) • 4.14 kB
JavaScript
import { promisify } from 'node:util';
import zlib from 'node:zlib';
const deflateAsync = promisify(zlib.deflate.bind(zlib));
const inflateAsync = promisify(zlib.inflate.bind(zlib));
const gzipAsync = promisify(zlib.gzip.bind(zlib));
const gunzipAsync = promisify(zlib.gunzip.bind(zlib));
const zstdCompressAsync = promisify(zlib.zstdCompress.bind(zlib));
const zstdDecompressAsync = promisify(zlib.zstdDecompress.bind(zlib));
class Zip2 {
async decompressZstdOrInflateToString(buf) {
return (await this.decompressZstdOrInflate(buf)).toString();
}
decompressZstdOrInflateToStringSync(buf) {
return this.decompressZstdOrInflateSync(buf).toString();
}
/**
* Detects if Buffer is zstd-compressed.
* Otherwise attempts to Inflate.
*/
async decompressZstdOrInflate(buf) {
if (this.isZstdBuffer(buf)) {
return await zstdDecompressAsync(buf);
}
return await inflateAsync(buf);
}
decompressZstdOrInflateSync(buf) {
if (this.isZstdBuffer(buf)) {
return zlib.zstdDecompressSync(buf);
}
return zlib.inflateSync(buf);
}
/**
* deflateBuffer uses `deflate`.
* It's 9 bytes shorter than `gzip`.
*/
async deflate(input, options = {}) {
return await deflateAsync(input, options);
}
/**
* deflateSync uses `deflate`.
* It's 9 bytes shorter than `gzip`.
*/
deflateSync(input, options) {
return zlib.deflateSync(input, options);
}
async inflate(buf, options = {}) {
return await inflateAsync(buf, options);
}
inflateSync(buf, options = {}) {
return zlib.inflateSync(buf, options);
}
async inflateToString(buf, options) {
return (await this.inflate(buf, options)).toString();
}
inflateToStringSync(buf, options) {
return zlib.inflateSync(buf, options).toString();
}
/**
* gzipBuffer uses `gzip`
* It's 9 bytes longer than `deflate`.
*/
async gzip(input, options = {}) {
return await gzipAsync(input, options);
}
/**
* gzipBuffer uses `gzip`
* It's 9 bytes longer than `deflate`.
*/
gzipSync(input, options = {}) {
return zlib.gzipSync(input, options);
}
async gunzip(buf, options = {}) {
return await gunzipAsync(buf, options);
}
gunzipSync(buf, options = {}) {
return zlib.gunzipSync(buf, options);
}
async gunzipToString(buf, options) {
return (await this.gunzip(buf, options)).toString();
}
gunzipToStringSync(buf, options) {
return zlib.gunzipSync(buf, options).toString();
}
async zstdCompress(input, level, // defaults to 3
options = {}) {
return await zstdCompressAsync(input, this.zstdLevelToOptions(level, options));
}
zstdCompressSync(input, level, // defaults to 3
options = {}) {
return zlib.zstdCompressSync(input, this.zstdLevelToOptions(level, options));
}
zstdLevelToOptions(level, opt = {}) {
if (!level)
return opt;
return {
...opt,
params: {
...opt.params,
[zlib.constants.ZSTD_c_compressionLevel]: level,
},
};
}
async zstdDecompressToString(input, options = {}) {
return (await zstdDecompressAsync(input, options)).toString();
}
/**
* Warning! It leaks memory severely. Prefer sync.
*/
async zstdDecompress(input, options = {}) {
return await zstdDecompressAsync(input, options);
}
/**
* Warning! It leaks memory severely. Prefer sync.
*/
zstdDecompressToStringSync(input, options = {}) {
return zlib.zstdDecompressSync(input, options).toString();
}
zstdDecompressSync(input, options = {}) {
return zlib.zstdDecompressSync(input, options);
}
isZstdBuffer(input) {
return input.readUInt32LE(0) === ZSTD_MAGIC_NUMBER;
}
isGzipBuffer(input) {
return input[0] === 0x1f && input[1] === 0x8b;
}
}
export const zip2 = new Zip2();
const ZSTD_MAGIC_NUMBER = 0xfd2fb528;