Import pako in CommonJS or ESM
masterDepending on your module system, import pako as follows:
CommonJS:
const { deflate, inflate } = require('pako');ES Modules (Namespace import):
import * as pako from 'pako';repository·master·Indexed 27 days ago
https://github.com/nodeca/pakoA high-performance, zlib-compatible compression library for JavaScript. pako provides binary-equivalent output to the original zlib and supports browser environments. It includes simple one-shot functions like deflate, inflate, deflateRaw, inflateRaw, gzip, and ungzip, as well as Deflate and Inflate classes for chunked streaming. For fine-grained control, it exposes a low-level zlib API including ZStream and various zlib* functions.
Depending on your module system, import pako as follows:
CommonJS:
const { deflate, inflate } = require('pako');ES Modules (Namespace import):
import * as pako from 'pako';Execute the benchmark script to run the performance measurements defined in the repository.
./benchmark.jsYou can profile the execution using Node.js built-in profiling tools. First, run the profile script with the --prof flag to generate a log file, then process that log file to view the results.
node --prof profile.js
node --prof-process isolate-0xnnnnnnnnnnnn-v8.logTo use pako in your project, install it using npm:
npm install pakonpm iPako supports working with strings. deflate automatically recodes string inputs to UTF-8. To restore a compressed buffer back into a JavaScript string, use the inflate helper with the { toText: true } option.
import { deflate, inflate } from 'pako';
const test = { my: 'super', puper: [456, 567], awesome: 'pako' };
const compressed = deflate(JSON.stringify(test));
const restored = JSON.parse(inflate(compressed, { toText: true }));If you need to process data in chunks or want to avoid exceptions, use the Deflate and Inflate classes.
For Deflate, use .push(chunk, flush) where the second parameter indicates if the chunk is the last one.
For Inflate, use .push(chunk) and the end of the stream is auto-detected.
Both classes provide .err (error) and .msg (error message) properties to check for issues without throwing exceptions.
// Chunked Deflate
const deflator = new Deflate();
deflator.push(chunk1, false);
deflator.push(chunk2); // second param is false by default
deflator.push(chunk_last, true); // true indicates the last chunk
if (deflator.err) {
console.log(deflator.msg);
}
const output = deflator.result;
// Chunked Inflate
const inflator = new Inflate();
inflator.push(chunk1);
inflator.push(chunk2);
inflator.push(chunk_last); // end is auto-detected
if (inflator.err) {
console.log(inflator.msg);
}
const output = inflator.result;For basic compression and decompression, use the deflate and inflate functions. deflate accepts a Uint8Array and returns compressed bytes. inflate decompresses bytes but may throw an exception if the input stream is broken, so it should be wrapped in a try...catch block.
import { Deflate, Inflate, deflate, inflate } from 'pako';
// Deflate
const input = new Uint8Array();
//... fill input data here
const output = deflate(input);
// Inflate
const compressed = new Uint8Array();
//... fill data to uncompress here
try {
const result = inflate(compressed);
// ... continue processing
} catch (err) {
console.log(err);
}When instantiating Inflate, you can provide an InflateOptions object to control decompression behavior:
windowBits: Window size. By default, it autodetects deflate/gzip via the wrapper header. See zlib manual.dictionary: An initial dictionary (Uint8Array or ArrayBuffer) used for decompression.chunkSize: Size of generated data chunks (defaults to 64K).raw: If true, performs raw inflate (no wrapper header).When using Deflate, deflate, deflateRaw, or gzip, you can pass an options object to control compression behavior.
Zlib Options:
level: Compression level (see zlib manual).windowBits: Window size (see zlib manual).memLevel: Memory level (see zlib manual).strategy: Compression strategy (see zlib manual).dictionary: Initial dictionary (Uint8Array | ArrayBuffer). Note: dictionary is not supported with gzip.Extensions:
chunkSize: Size of generated data chunks (default: 16384).raw: If true, performs raw deflate (no wrapper). Automatically negates windowBits if provided.gzip: If true, creates a gzip wrapper. Automatically adjusts windowBits if provided.legacyHash: If true, uses the classic zlib hash for byte-for-byte canonical output. Default is false (uses faster ANZAC++ hash).deflateRaw function compresses data using the deflate algorithm but without the zlib wrapper (no header and no adler32 crc).Use the following functions to decompress data. inflate and ungzip expect zlib/gzip headers, while inflateRaw handles raw DEFLATE streams.
inflate(data, options): Decompresses zlib formatted data.inflateRaw(data, options): Decompresses raw DEFLATE data.ungzip(data, options): Decompresses gzip formatted data.Inflate: A class for managing decompression state.InflateOptions can be passed to configure the decompression process.