Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pako

nodeca82m(MIT AND Zlib)2.1.0TypeScript support: definitely-typed

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

zlib, deflate, inflate, gzip

readme

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

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

2.1.0 - 2022-11-07

Changed

  • Sync with zlib 1.2.12.

Fixed

  • Updated comments in tree.js for Babel compatibility, #262.

2.0.4 - 2021-07-29

Fixed

  • Use TextEncoder and TextDecoder if available, #228.
  • Use pre-generated fixtures instead of node.js zlib.

2.0.3 - 2021-01-09

Fixed

  • Add all files explicit to package exports (since behaviour changed after adding .export field)

2.0.2 - 2020-11-19

Fixed

  • Fix esm build named exports.

2.0.1 - 2020-11-17

Changed

  • Changed esm build .js => .mjs to fix node.js import.
  • Added module entry in package.json for some bundlers.

2.0.0 - 2020-11-17

Changed

  • Removed binary strings and Array support.
  • Removed fallbacks for TypedArray methods (.set(), .subarray()).
  • Rewritten top-level wrappers.
  • Removed support of Inflate & Deflate instance create without new.
  • Inflate.push() no longer needs second param (end is auto-detected).
  • Increased default inflate chunk size to 64K.
  • Moved exported constants to .constants.
  • Switched to es6. Legacy es5 builds available in /dist.
  • Added esm build.
  • Structure of /dist folder changed.
  • Upgraded build tools to modern ones.

[1.0.11] - 2020-01-29

Fixed

  • Fix tests in node.js v12+, #179.

1.0.10 - 2019-02-28

Fixed

  • Fix minified version, #161.

1.0.9 - 2019-02-28

Fixed

  • Fix new Buffer() warning, #154.

1.0.8 - 2019-01-14

Fixed

  • Fix raw inflate with dictionary, #155.

1.0.7 - 2018-11-29

Fixed

  • Fixed RangeError in Crome 72, #150.

1.0.6 - 2017-09-14

Changed

  • Improve @std/esm compatibility.

1.0.5 - 2017-03-17

Changed

  • Maintenance. More formal zlib attribution and related changes, #93. Thanks to @bastien-roucaries for the help.

1.0.4 - 2016-12-15

Changed

  • Bump dev dependencies.

Fixed

  • Make sure err.message is filled on throw.

Added

  • Code examples for utf-16 string encoding & object compression.

1.0.3 - 2016-07-25

Fixed

  • Maintenance: re-release to properly display latest version in npm registry and badges. Because npm publish timestamp used instead of versions.

1.0.2 - 2016-07-21

Fixed

  • Fixed nasty bug in deflate (wrong d_buf offset), which could cause broken data in some rare cases.
  • Also released as 0.2.9 to give chance to old dependents, not updated to 1.x version.

1.0.1 - 2016-04-01

Added

  • Added dictionary support. Thanks to @dignifiedquire.

1.0.0 - 2016-02-17

Changed

  • Maintenance release (semver, coding style).

0.2.8 - 2015-09-14

Fixed

  • Fixed regression after 0.2.4 for edge conditions in inflate wrapper (#65). Added more tests to cover possible cases.

0.2.7 - 2015-06-09

Added

  • Added Z_SYNC_FLUSH support. Thanks to @TinoLange.

0.2.6 - 2015-03-24

Added

  • Allow ArrayBuffer input.

0.2.5 - 2014-07-19

Fixed

  • Workaround for Chrome 38.0.2096.0 script parser bug, #30.

0.2.4 - 2014-07-07

Fixed

  • Fixed bug in inflate wrapper, #29

0.2.3 - 2014-06-09

Changed

  • Maintenance release, dependencies update.

0.2.2 - 2014-06-04

Fixed

  • Fixed iOS 5.1 Safari issue with apply(typed_array), #26.

0.2.1 - 2014-05-01

Fixed

  • Fixed collision on switch dynamic/fixed tables.

0.2.0 - 2014-04-18

Added

  • Added custom gzip headers support.
  • Added strings support.
  • More coverage tests.

Fixed

  • Improved memory allocations for small chunks.
  • ZStream properties rename/cleanup.

0.1.1 - 2014-03-20

Fixed

  • Bugfixes for inflate/deflate.

0.1.0 - 2014-03-15

Added

  • First release.