zlib port to javascript - fast, modularized, with browser support
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
High-performance data compression using the deflate algorithm with support for raw deflate, standard deflate with wrapper, and gzip formats. Provides both one-shot function API and streaming class API for different use cases.
Compresses data using the deflate algorithm with optional wrapper.
/**
* Compress data with deflate algorithm
* @param input - Data to compress (string, Uint8Array, or ArrayBuffer)
* @param options - Compression options
* @returns Compressed data as Uint8Array
*/
function deflate(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;Usage Examples:
import { deflate } from 'pako';
// Compress string
const text = "Hello World!";
const compressed = deflate(text);
// Compress with options
const compressedFast = deflate(text, { level: 1 }); // Fast compression
const compressedBest = deflate(text, { level: 9 }); // Best compression
// Compress binary data
const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
const compressedBinary = deflate(binaryData);Compresses data using raw deflate format (no wrapper/header).
/**
* Compress data with raw deflate (no wrapper/header)
* @param input - Data to compress (string, Uint8Array, or ArrayBuffer)
* @param options - Compression options (raw: true is automatically set)
* @returns Compressed data as Uint8Array
*/
function deflateRaw(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;Usage Examples:
import { deflateRaw } from 'pako';
// Raw deflate compression
const text = "Hello World!";
const compressed = deflateRaw(text);
// With custom window size
const compressedCustom = deflateRaw(text, { windowBits: 12 });Compresses data using gzip format with optional custom headers.
/**
* Compress data with gzip format
* @param input - Data to compress (string, Uint8Array, or ArrayBuffer)
* @param options - Compression options (gzip: true is automatically set)
* @returns Compressed data as Uint8Array in gzip format
*/
function gzip(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;Usage Examples:
import { gzip } from 'pako';
// Basic gzip compression
const text = "Hello World!";
const compressed = gzip(text);
// Gzip with custom header
const compressedWithHeader = gzip(text, {
header: {
name: 'hello.txt',
comment: 'Sample file',
time: Date.now(),
text: true
}
});Streaming compression interface for processing large data in chunks.
/**
* Generic streaming wrapper for zlib deflate operations (compression)
* @param options - Compression options
*/
class Deflate {
constructor(options?: DeflateOptions);
/** Final compressed output (available after completion) */
result: Uint8Array;
/** Error code (0 = success) */
err: number;
/** Error message */
msg: string;
/** Internal output chunks */
chunks: Array<Uint8Array>;
/** Whether compression has ended */
ended: boolean;
/** Configuration options */
options: DeflateOptions;
/** Internal zlib stream structure */
strm: any;
/**
* Process input data chunk
* @param data - Input data chunk
* @param flush_mode - Flush mode (false=Z_NO_FLUSH, true=Z_FINISH, or constant)
* @returns true if successful, false on error
*/
push(data: Uint8Array | ArrayBuffer | string, flush_mode?: number | boolean): boolean;
/**
* Data callback (override for custom handling)
* @param chunk - Output data chunk
*/
onData(chunk: Uint8Array): void;
/**
* Completion callback
* @param status - Final status code
*/
onEnd(status: number): void;
}Usage Examples:
import { Deflate, constants } from 'pako';
// Basic streaming compression
const deflater = new Deflate();
deflater.push('Hello ', false);
deflater.push('World!', true); // true = Z_FINISH
console.log(deflater.result); // Uint8Array with compressed data
// Custom chunk processing
const deflater2 = new Deflate({ level: 6 });
deflater2.onData = function(chunk) {
console.log('Compressed chunk:', chunk);
};
deflater2.onEnd = function(status) {
console.log('Compression finished with status:', status);
};
deflater2.push('Large data chunk 1', constants.Z_NO_FLUSH);
deflater2.push('Large data chunk 2', constants.Z_NO_FLUSH);
deflater2.push('Final chunk', constants.Z_FINISH);
// Gzip streaming
const gzipper = new Deflate({
gzip: true,
header: {
name: 'data.txt',
text: true
}
});
gzipper.push('Text content', true);interface DeflateOptions {
/** Compression level (-1 for default, 0 = no compression, 1 = best speed, 9 = best compression) */
level?: number;
/** Window size (8-15, default 15) - larger values use more memory but may compress better */
windowBits?: number;
/** Memory usage level (1-9, default 8) - higher values use more memory but may be faster */
memLevel?: number;
/** Compression strategy (0 = default, 1 = filtered, 2 = huffman only, 3 = RLE, 4 = fixed) */
strategy?: number;
/** Preset dictionary for improved compression of similar data */
dictionary?: Uint8Array | ArrayBuffer | string;
/** Output chunk size in bytes (default 16384) */
chunkSize?: number;
/** Use raw deflate format without wrapper */
raw?: boolean;
/** Create gzip wrapper instead of deflate wrapper */
gzip?: boolean;
/** Custom gzip header (only used when gzip: true) */
header?: GzipHeader;
}
interface GzipHeader {
/** Mark compressed data as text */
text?: boolean;
/** Unix timestamp */
time?: number;
/** Operating system code */
os?: number;
/** Extra data bytes (max 65536) */
extra?: Array<number>;
/** Original file name */
name?: string;
/** Comment string */
comment?: string;
/** Add header CRC */
hcrc?: boolean;
}Check the err property of Deflate instances for error codes:
import { Deflate, constants } from 'pako';
const deflater = new Deflate();
deflater.push('data', true);
if (deflater.err !== constants.Z_OK) {
console.error('Compression error:', deflater.msg);
} else {
console.log('Compression successful');
}Install with Tessl CLI
npx tessl i tessl/npm-pako