or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

compression.mddecompression.mdindex.md
tile.json

tessl/npm-pako

zlib port to javascript - fast, modularized, with browser support

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/pako@2.1.x

To install, run

npx @tessl/cli install tessl/npm-pako@2.1.0

index.mddocs/

Pako

Pako is a high-performance JavaScript port of the zlib compression library, enabling developers to perform deflate/inflate compression and decompression operations directly in JavaScript environments including browsers and Node.js. It offers binary-compatible results with the original zlib library while achieving near-native performance in modern JavaScript engines.

Package Information

  • Package Name: pako
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install pako

Core Imports

import pako from 'pako';
import { deflate, inflate, Deflate, Inflate, constants } from 'pako';

For CommonJS:

const pako = require('pako');
const { deflate, inflate, Deflate, Inflate, constants } = require('pako');

Basic Usage

import { deflate, inflate } from 'pako';

// Compress string data (automatically converted to UTF-8 bytes)
const input = "Hello World!";
const compressed = deflate(input);

// Decompress back to original
const decompressed = inflate(compressed, { to: 'string' });
console.log(decompressed); // "Hello World!"

// Working with binary data
const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
const compressedBinary = deflate(binaryData);
const decompressedBinary = inflate(compressedBinary);

// All input types supported: string, Uint8Array, ArrayBuffer
const arrayBuffer = new ArrayBuffer(10);
const compressedFromBuffer = deflate(arrayBuffer);

Architecture

Pako is built around several key components:

  • Function API: Simple one-shot compression/decompression functions (deflate, inflate, gzip, ungzip)
  • Class API: Streaming interfaces (Deflate, Inflate) for processing large data chunks
  • Format Support: Raw deflate, deflate with wrapper, and gzip formats
  • Data Handling: Automatic type conversion with configurable UTF-8 encoding
  • Browser Optimization: Separate builds for deflate-only and inflate-only operations

Data Type Handling

Pako automatically handles different input types:

Input Types Supported:

  • string - Automatically converted to UTF-8 bytes using TextEncoder when available, or manual UTF-8 encoding
  • Uint8Array - Used directly as binary data
  • ArrayBuffer - Converted to Uint8Array for processing

Output Types:

  • Default output is Uint8Array for all compression functions and binary decompression
  • String output available for decompression functions when { to: 'string' } option is specified
  • String output uses UTF-8 decoding via TextDecoder when available, or manual UTF-8 decoding

Capabilities

Compression Operations

High-performance data compression using deflate algorithm with support for raw deflate, standard deflate with wrapper, and gzip formats.

function deflate(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;
function deflateRaw(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;
function gzip(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;

class Deflate {
  constructor(options?: DeflateOptions);
  push(data: Uint8Array | ArrayBuffer | string, flush_mode?: number | boolean): boolean;
  result: Uint8Array;
  err: number;
  msg: string;
  chunks: Array<Uint8Array>;
  ended: boolean;
  options: DeflateOptions;
  strm: any;
}

Compression

Decompression Operations

Fast data decompression with automatic format detection for deflate and gzip formats, supporting both binary and string output.

function inflate(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;
function inflateRaw(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;
function ungzip(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;

class Inflate {
  constructor(options?: InflateOptions);
  push(data: Uint8Array | ArrayBuffer, flush_mode?: number | boolean): boolean;
  result: Uint8Array | string;
  err: number;
  msg: string;
  chunks: Array<Uint8Array>;
  ended: boolean;
  options: InflateOptions;
  strm: any;
  header: any;
}

Decompression

Types

interface DeflateOptions {
  level?: number;          // Compression level (-1 for default, 0-9)
  windowBits?: number;     // Window size (15 default, 8-15 range)
  memLevel?: number;       // Memory usage level (8 default, 1-9 range)
  strategy?: number;       // Compression strategy (0 default)
  dictionary?: Uint8Array | ArrayBuffer | string; // Preset dictionary
  chunkSize?: number;      // Output chunk size (16384 default)
  raw?: boolean;           // Raw deflate without wrapper
  gzip?: boolean;          // Create gzip wrapper
  header?: GzipHeader;     // Custom gzip header
}

interface InflateOptions {
  windowBits?: number;     // Window size (15 default, 8-15 range)
  dictionary?: Uint8Array | ArrayBuffer | string; // Preset dictionary
  chunkSize?: number;      // Output chunk size (65536 default)
  raw?: boolean;           // Raw inflate without wrapper
  to?: string;             // Output format ('string' for UTF-16, default binary)
}

interface GzipHeader {
  text?: boolean;          // Compressed data is text
  time?: number;           // Unix timestamp
  os?: number;             // Operating system code
  extra?: Array<number>;   // Extra data bytes (max 65536)
  name?: string;           // File name
  comment?: string;        // Comment
  hcrc?: boolean;          // Add header CRC
}

Constants

const constants: {
  // Flush values
  Z_NO_FLUSH: 0;
  Z_PARTIAL_FLUSH: 1;
  Z_SYNC_FLUSH: 2;
  Z_FULL_FLUSH: 3;
  Z_FINISH: 4;
  Z_BLOCK: 5;
  Z_TREES: 6;
  
  // Return codes
  Z_OK: 0;
  Z_STREAM_END: 1;
  Z_NEED_DICT: 2;
  Z_ERRNO: -1;
  Z_STREAM_ERROR: -2;
  Z_DATA_ERROR: -3;
  Z_MEM_ERROR: -4;
  Z_BUF_ERROR: -5;
  
  // Compression levels
  Z_NO_COMPRESSION: 0;
  Z_BEST_SPEED: 1;
  Z_BEST_COMPRESSION: 9;
  Z_DEFAULT_COMPRESSION: -1;
  
  // Compression strategies
  Z_FILTERED: 1;
  Z_HUFFMAN_ONLY: 2;
  Z_RLE: 3;
  Z_FIXED: 4;
  Z_DEFAULT_STRATEGY: 0;
  
  // Data types
  Z_BINARY: 0;
  Z_TEXT: 1;
  Z_UNKNOWN: 2;
  
  // Compression method
  Z_DEFLATED: 8;
};