Fork me on GitHub

pako

CI NPM version

zlib port to javascript, very fast!

Why pako is cool:

  • Results are binary equal to well known zlib (now contains ported zlib v1.2.8).
  • Almost as fast in modern JS engines as C implementation (see benchmarks).
  • Works in browsers, you can browserify any separate component.

This project was done to understand how fast JS can be and is it necessary to develop native C modules for CPU-intensive tasks. Enjoy the result!

Benchmarks:

node v12.16.3 (zlib 1.2.9), 1mb input sample:

deflate-imaya x 4.75 ops/sec ±4.93% (15 runs sampled)
deflate-pako x 10.38 ops/sec ±0.37% (29 runs sampled)
deflate-zlib x 17.74 ops/sec ±0.77% (46 runs sampled)
gzip-pako x 8.86 ops/sec ±1.41% (29 runs sampled)
inflate-imaya x 107 ops/sec ±0.69% (77 runs sampled)
inflate-pako x 131 ops/sec ±1.74% (82 runs sampled)
inflate-zlib x 258 ops/sec ±0.66% (88 runs sampled)
ungzip-pako x 115 ops/sec ±1.92% (80 runs sampled)

node v14.15.0 (google's zlib), 1mb output sample:

deflate-imaya x 4.93 ops/sec ±3.09% (16 runs sampled)
deflate-pako x 10.22 ops/sec ±0.33% (29 runs sampled)
deflate-zlib x 18.48 ops/sec ±0.24% (48 runs sampled)
gzip-pako x 10.16 ops/sec ±0.25% (28 runs sampled)
inflate-imaya x 110 ops/sec ±0.41% (77 runs sampled)
inflate-pako x 134 ops/sec ±0.66% (83 runs sampled)
inflate-zlib x 402 ops/sec ±0.74% (87 runs sampled)
ungzip-pako x 113 ops/sec ±0.62% (80 runs sampled)

zlib's test is partially affected by marshalling (that make sense for inflate only). You can change deflate level to 0 in benchmark source, to investigate details. For deflate level 6 results can be considered as correct.

Install:

npm install pako

Examples / API

Full docs - http://nodeca.github.io/pako/

const pako = require('pako');

// Deflate
//
const input = new Uint8Array();
//... fill input data here
const output = pako.deflate(input);

// Inflate (simple wrapper can throw exception on broken stream)
//
const compressed = new Uint8Array();
//... fill data to uncompress here
try {
  const result = pako.inflate(compressed);
  // ... continue processing
} catch (err) {
  console.log(err);
}

//
// Alternate interface for chunking & without exceptions
//

const deflator = new pako.Deflate();

deflator.push(chunk1, false);
deflator.push(chunk2); // second param is false by default.
...
deflator.push(chunk_last, true); // `true` says this chunk is last

if (deflator.err) {
  console.log(deflator.msg);
}

const output = deflator.result;


const inflator = new pako.Inflate();

inflator.push(chunk1);
inflator.push(chunk2);
...
inflator.push(chunk_last); // no second param because end is auto-detected

if (inflator.err) {
  console.log(inflator.msg);
}

const output = inflator.result;

Sometime you can wish to work with strings. For example, to send stringified objects to server. Pako's deflate detects input data type, and automatically recode strings to utf-8 prior to compress. Inflate has special option, to say compressed data has utf-8 encoding and should be recoded to javascript's utf-16.

const pako = require('pako');

const test = { my: 'super', puper: [456, 567], awesome: 'pako' };

const compressed = pako.deflate(JSON.stringify(test));

const restored = JSON.parse(pako.inflate(compressed, { to: 'string' }));

Notes

Pako does not contain some specific zlib functions:

  • deflate - methods deflateCopy, deflateBound, deflateParams, deflatePending, deflatePrime, deflateTune.
  • inflate - methods inflateCopy, inflateMark, inflatePrime, inflateGetDictionary, inflateSync, inflateSyncPoint, inflateUndermine.
  • High level inflate/deflate wrappers (classes) may not support some flush modes.

pako for enterprise

Available as part of the Tidelift Subscription

The maintainers of pako and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Authors

Personal thanks to:

  • Vyacheslav Egorov (@mraleph) for his awesome tutorials about optimising JS code for v8, IRHydra tool and his advices.
  • David Duponchel (@dduponchel) for help with testing.

Original implementation (in C):

  • zlib by Jean-loup Gailly and Mark Adler.

License

  • MIT - all files, except /lib/zlib folder
  • ZLIB - /lib/zlib content

Deflate

Description

Generic JS-style wrapper for zlib calls. If you don't need streaming behaviour - use more simple functions: deflate, deflateRaw and gzip.

Constructor

Class properties

Instance methods

Deflate.new

    • new Deflate(options)
    • options
      • Object
    • zlib deflate options.

Creates new deflator instance with specified params. Throws exception on bad params. Supported options:

  • level
  • windowBits
  • memLevel
  • strategy
  • dictionary

http://zlib.net/manual.html#Advanced for more information on these.

Additional options, for internal needs:

  • chunkSize - size of generated data chunks (16K by default)
  • raw (Boolean) - do raw deflate
  • gzip (Boolean) - create gzip wrapper
  • header (Object) - custom header for gzip
    • text (Boolean) - true if compressed data believed to be text
    • time (Number) - modification time, unix timestamp
    • os (Number) - operation system code
    • extra (Array) - array of bytes with extra data (max 65536)
    • name (String) - file name (binary string)
    • comment (String) - comment (binary string)
    • hcrc (Boolean) - true if header crc should be added
Example:
const pako = require('pako')
  , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
  , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);

const deflate = new pako.Deflate({ level: 3});

deflate.push(chunk1, false);
deflate.push(chunk2, true);  // true -> last chunk

if (deflate.err) { throw new Error(deflate.err); }

console.log(deflate.result);

Deflate.err

    • Deflate.err
      • Number

Error code after deflate finished. 0 (Z_OK) on success. You will not need it in real life, because deflate errors are possible only on wrong options or bad onData / onEnd custom handlers.

Deflate.msg

    • Deflate.msg
      • String

Error message, if Deflate.err != 0

Deflate.result

    • Deflate.result
      • Uint8Array

Compressed result, generated by default Deflate#onData and Deflate#onEnd handlers. Filled after you push last chunk (call Deflate#push with Z_FINISH / true param).

Deflate#onData

    • Deflate#onData(chunk)
      • Void
    • chunk
      • Uint8Array
    • output data.

By default, stores data blocks in chunks[] property and glue those in onEnd. Override this handler, if you need another behaviour.

Deflate#onEnd

    • Deflate#onEnd(status)
      • Void
    • status
      • Number
    • deflate status. 0 (Z_OK) on success, other if not.

Called once after you tell deflate that the input stream is complete (Z_FINISH). By default - join collected chunks, free memory and fill results / err properties.

Deflate#push

    • Deflate#push(data[, flush_mode])
      • Boolean
    • data
      • Uint8Array
      • ArrayBuffer
      • String
    • input data. Strings will be converted to utf8 byte sequence.

    • flush_mode
      • Number
      • Boolean
    • 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes. See constants. Skipped or false means Z_NO_FLUSH, true means Z_FINISH.

Sends input data to deflate pipe, generating Deflate#onData calls with new compressed chunks. Returns true on success. The last data block must have flush_mode Z_FINISH (or true). That will flush internal pending buffers and call Deflate#onEnd.

On fail call Deflate#onEnd with error code and return false.

Example
push(chunk, false); // push one of data chunks
...
push(chunk, true);  // push last chunk

deflate

    • deflate(data[, options])
      • Uint8Array
    • data
      • Uint8Array
      • ArrayBuffer
      • String
    • input data to compress.

    • options
      • Object
    • zlib deflate options.

Compress data with deflate algorithm and options.

Supported options are:

  • level
  • windowBits
  • memLevel
  • strategy
  • dictionary

http://zlib.net/manual.html#Advanced for more information on these.

Sugar (options):

  • raw (Boolean) - say that we work with raw stream, if you don't wish to specify negative windowBits implicitly.
Example:
const pako = require('pako')
const data = new Uint8Array([1,2,3,4,5,6,7,8,9]);

console.log(pako.deflate(data));

deflateRaw

    • deflateRaw(data[, options])
      • Uint8Array
    • data
      • Uint8Array
      • ArrayBuffer
      • String
    • input data to compress.

    • options
      • Object
    • zlib deflate options.

The same as deflate, but creates raw data, without wrapper (header and adler32 crc).

gzip

    • gzip(data[, options])
      • Uint8Array
    • data
      • Uint8Array
      • ArrayBuffer
      • String
    • input data to compress.

    • options
      • Object
    • zlib deflate options.

The same as deflate, but create gzip wrapper instead of deflate one.

Inflate

Description

Generic JS-style wrapper for zlib calls. If you don't need streaming behaviour - use more simple functions: inflate and inflateRaw.

Constructor

Class properties

Instance methods

Inflate.new

    • new Inflate(options)
    • options
      • Object
    • zlib inflate options.

Creates new inflator instance with specified params. Throws exception on bad params. Supported options:

  • windowBits
  • dictionary

http://zlib.net/manual.html#Advanced for more information on these.

Additional options, for internal needs:

  • chunkSize - size of generated data chunks (16K by default)
  • raw (Boolean) - do raw inflate
  • to (String) - if equal to 'string', then result will be converted from utf8 to utf16 (javascript) string. When string output requested, chunk length can differ from chunkSize, depending on content.

By default, when no options set, autodetect deflate/gzip data format via wrapper header.

Example:
const pako = require('pako')
const chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
const chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);

const inflate = new pako.Inflate({ level: 3});

inflate.push(chunk1, false);
inflate.push(chunk2, true);  // true -> last chunk

if (inflate.err) { throw new Error(inflate.err); }

console.log(inflate.result);

Inflate.err

    • Inflate.err
      • Number

Error code after inflate finished. 0 (Z_OK) on success. Should be checked if broken data possible.

Inflate.msg

    • Inflate.msg
      • String

Error message, if Inflate.err != 0

Inflate.result

    • Inflate.result
      • Uint8Array
      • String

Uncompressed result, generated by default Inflate#onData and Inflate#onEnd handlers. Filled after you push last chunk (call Inflate#push with Z_FINISH / true param).

Inflate#onData

    • Inflate#onData(chunk)
      • Void
    • chunk
      • Uint8Array
      • String
    • output data. When string output requested, each chunk will be string.

By default, stores data blocks in chunks[] property and glue those in onEnd. Override this handler, if you need another behaviour.

Inflate#onEnd

    • Inflate#onEnd(status)
      • Void
    • status
      • Number
    • inflate status. 0 (Z_OK) on success, other if not.

Called either after you tell inflate that the input stream is complete (Z_FINISH). By default - join collected chunks, free memory and fill results / err properties.

Inflate#push

    • Inflate#push(data[, flush_mode])
      • Boolean
    • data
      • Uint8Array
      • ArrayBuffer
    • input data

    • flush_mode
      • Number
      • Boolean
    • 0..6 for corresponding Z_NO_FLUSH..Z_TREE flush modes. See constants. Skipped or false means Z_NO_FLUSH, true means Z_FINISH.

Sends input data to inflate pipe, generating Inflate#onData calls with new output chunks. Returns true on success. If end of stream detected, Inflate#onEnd will be called.

flush_mode is not needed for normal operation, because end of stream detected automatically. You may try to use it for advanced things, but this functionality was not tested.

On fail call Inflate#onEnd with error code and return false.

Example
push(chunk, false); // push one of data chunks
...
push(chunk, true);  // push last chunk

inflate

    • inflate(data[, options])
      • Uint8Array
      • String
    • data
      • Uint8Array
      • ArrayBuffer
    • input data to decompress.

    • options
      • Object
    • zlib inflate options.

Decompress data with inflate/ungzip and options. Autodetect format via wrapper header by default. That's why we don't provide separate ungzip method.

Supported options are:

  • windowBits

http://zlib.net/manual.html#Advanced for more information.

Sugar (options):

  • raw (Boolean) - say that we work with raw stream, if you don't wish to specify negative windowBits implicitly.
  • to (String) - if equal to 'string', then result will be converted from utf8 to utf16 (javascript) string. When string output requested, chunk length can differ from chunkSize, depending on content.
Example:
const pako = require('pako');
const input = pako.deflate(new Uint8Array([1,2,3,4,5,6,7,8,9]));
let output;

try {
  output = pako.inflate(input);
} catch (err) {
  console.log(err);
}

inflateRaw

    • inflateRaw(data[, options])
      • Uint8Array
      • String
    • data
      • Uint8Array
      • ArrayBuffer
    • input data to decompress.

    • options
      • Object
    • zlib inflate options.

The same as inflate, but creates raw data, without wrapper (header and adler32 crc).

ungzip

    • ungzip(data[, options])
      • Uint8Array
      • String
    • data
      • Uint8Array
      • ArrayBuffer
    • input data to decompress.

    • options
      • Object
    • zlib inflate options.

Just shortcut to inflate, because it autodetects format by header.content. Done for convenience.