zlib port to javascript - fast, modularized, with browser support
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Fast data decompression with automatic format detection for deflate and gzip formats, supporting both binary and string output. Provides both one-shot function API and streaming class API for different use cases.
Decompresses deflate/gzip data with automatic format detection.
/**
* Decompress deflate/gzip data (auto-detects format)
* @param input - Compressed data (Uint8Array or ArrayBuffer)
* @param options - Decompression options
* @returns Decompressed data as Uint8Array or string (if to: 'string' specified)
*/
function inflate(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;Usage Examples:
import { inflate } from 'pako';
// Decompress to binary
const compressed = new Uint8Array([/* compressed data */]);
const decompressed = inflate(compressed);
// Decompress to string
const decompressedText = inflate(compressed, { to: 'string' });
console.log(decompressedText); // Original text
// Decompress with custom options
const result = inflate(compressed, {
windowBits: 15,
chunkSize: 32768
});Decompresses raw deflate data (no wrapper/header).
/**
* Decompress raw deflate data (no wrapper)
* @param input - Compressed data (Uint8Array or ArrayBuffer)
* @param options - Decompression options (raw: true is automatically set)
* @returns Decompressed data as Uint8Array or string
*/
function inflateRaw(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;Usage Examples:
import { inflateRaw } from 'pako';
// Decompress raw deflate data
const rawCompressed = new Uint8Array([/* raw deflate data */]);
const decompressed = inflateRaw(rawCompressed);
// To string with custom window size
const text = inflateRaw(rawCompressed, {
to: 'string',
windowBits: 12
});Direct alias for the inflate function - decompresses data with automatic format detection (including gzip). This function is identical to inflate and uses the same implementation.
/**
* Direct alias for inflate function (auto-detects format including gzip)
* Note: This is literally assigned to the inflate function in the source code
* @param input - Compressed data (Uint8Array or ArrayBuffer)
* @param options - Decompression options
* @returns Decompressed data as Uint8Array or string
*/
function ungzip(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;Usage Examples:
import { ungzip } from 'pako';
// Decompress gzip data
const gzipData = new Uint8Array([/* gzip data */]);
const decompressed = ungzip(gzipData, { to: 'string' });Streaming decompression interface for processing large compressed data in chunks.
/**
* Generic streaming wrapper for zlib inflate operations (decompression)
* @param options - Decompression options
*/
class Inflate {
constructor(options?: InflateOptions);
/** Final decompressed output (available after completion) */
result: Uint8Array | string;
/** Error code (0 = success) */
err: number;
/** Error message */
msg: string;
/** Internal output chunks */
chunks: Array<Uint8Array>;
/** Whether decompression has ended */
ended: boolean;
/** Configuration options */
options: InflateOptions;
/** Internal zlib stream structure */
strm: any;
/** Gzip header information (when available) */
header: any;
/**
* Process input data chunk
* @param data - Input compressed 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, 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 { Inflate, constants } from 'pako';
// Basic streaming decompression
const inflater = new Inflate({ to: 'string' });
inflater.push(compressedChunk1, false);
inflater.push(compressedChunk2, false);
inflater.push(compressedChunk3, true); // true = Z_FINISH
console.log(inflater.result); // Decompressed string
// Custom chunk processing
const inflater2 = new Inflate();
inflater2.onData = function(chunk) {
console.log('Decompressed chunk:', chunk);
};
inflater2.onEnd = function(status) {
console.log('Decompression finished with status:', status);
if (this.header) {
console.log('Gzip header info:', this.header);
}
};
inflater2.push(gzipData, true);
// Error handling
const inflater3 = new Inflate();
inflater3.push(corruptedData, true);
if (inflater3.err !== constants.Z_OK) {
console.error('Decompression failed:', inflater3.msg);
}interface InflateOptions {
/** Window size (8-15, default 15) - must match the value used during compression */
windowBits?: number;
/** Preset dictionary - must match the dictionary used during compression */
dictionary?: Uint8Array | ArrayBuffer | string;
/** Output chunk size in bytes (default 65536) */
chunkSize?: number;
/** Use raw inflate format (no wrapper) */
raw?: boolean;
/** Output format ('string' for UTF-16 string, default is Uint8Array) */
to?: string;
}The header property on Inflate instances contains gzip header information when decompressing gzip data. The structure varies based on the actual gzip header content and should be accessed after decompression completes.
All decompression functions and classes provide error information:
import { inflate, Inflate, constants } from 'pako';
// Function API error handling
try {
const result = inflate(compressedData);
console.log('Decompression successful');
} catch (error) {
console.error('Decompression failed:', error.message);
}
// Class API error handling
const inflater = new Inflate();
inflater.push(compressedData, true);
if (inflater.err === constants.Z_OK) {
console.log('Success:', inflater.result);
} else if (inflater.err === constants.Z_DATA_ERROR) {
console.error('Invalid or corrupted data:', inflater.msg);
} else if (inflater.err === constants.Z_MEM_ERROR) {
console.error('Insufficient memory:', inflater.msg);
} else {
console.error('Other error:', inflater.msg);
}import { inflate } from 'pako';
// Automatic UTF-8 detection and conversion
const textData = inflate(compressedText, { to: 'string' });
// Manual string conversion for specific encodings
const binaryData = inflate(compressedData);
const customText = new TextDecoder('utf-8').decode(binaryData);import { inflate } from 'pako';
// Default binary output
const binaryResult = inflate(compressedData); // Uint8Array
// Convert to other typed arrays if needed
const uint16Array = new Uint16Array(binaryResult.buffer);
const float32Array = new Float32Array(binaryResult.buffer);Install with Tessl CLI
npx tessl i tessl/npm-pako