1 | pako
|
2 | ==========================================
|
3 |
|
4 | [![Build Status](https://travis-ci.org/nodeca/pako.svg?branch=master)](https://travis-ci.org/nodeca/pako)
|
5 | [![NPM version](https://img.shields.io/npm/v/pako.svg)](https://www.npmjs.org/package/pako)
|
6 |
|
7 | > zlib port to javascript, very fast!
|
8 |
|
9 | __Why pako is cool:__
|
10 |
|
11 | - Almost as fast in modern JS engines as C implementation (see benchmarks).
|
12 | - Works in browsers, you can browserify any separate component.
|
13 | - Chunking support for big blobs.
|
14 | - Results are binary equal to well known [zlib](http://www.zlib.net/) (now contains ported zlib v1.2.8).
|
15 |
|
16 | This project was done to understand how fast JS can be and is it necessary to
|
17 | develop native C modules for CPU-intensive tasks. Enjoy the result!
|
18 |
|
19 |
|
20 | __Famous projects, using pako:__
|
21 |
|
22 | - [browserify](http://browserify.org/) (via [browserify-zlib](https://github.com/devongovett/browserify-zlib))
|
23 | - [JSZip](http://stuk.github.io/jszip/)
|
24 | - [mincer](https://github.com/nodeca/mincer)
|
25 | - [JS-Git](https://github.com/creationix/js-git) and
|
26 | [Tedit](https://chrome.google.com/webstore/detail/tedit-development-environ/ooekdijbnbbjdfjocaiflnjgoohnblgf)
|
27 | by [@creationix](https://github.com/creationix)
|
28 |
|
29 |
|
30 | __Benchmarks:__
|
31 |
|
32 | ```
|
33 | node v0.10.26, 1mb sample:
|
34 |
|
35 | deflate-dankogai x 4.73 ops/sec ±0.82% (15 runs sampled)
|
36 | deflate-gildas x 4.58 ops/sec ±2.33% (15 runs sampled)
|
37 | deflate-imaya x 3.22 ops/sec ±3.95% (12 runs sampled)
|
38 | ! deflate-pako x 6.99 ops/sec ±0.51% (21 runs sampled)
|
39 | deflate-pako-string x 5.89 ops/sec ±0.77% (18 runs sampled)
|
40 | deflate-pako-untyped x 4.39 ops/sec ±1.58% (14 runs sampled)
|
41 | * deflate-zlib x 14.71 ops/sec ±4.23% (59 runs sampled)
|
42 | inflate-dankogai x 32.16 ops/sec ±0.13% (56 runs sampled)
|
43 | inflate-imaya x 30.35 ops/sec ±0.92% (53 runs sampled)
|
44 | ! inflate-pako x 69.89 ops/sec ±1.46% (71 runs sampled)
|
45 | inflate-pako-string x 19.22 ops/sec ±1.86% (49 runs sampled)
|
46 | inflate-pako-untyped x 17.19 ops/sec ±0.85% (32 runs sampled)
|
47 | * inflate-zlib x 70.03 ops/sec ±1.64% (81 runs sampled)
|
48 |
|
49 | node v0.11.12, 1mb sample:
|
50 |
|
51 | deflate-dankogai x 5.60 ops/sec ±0.49% (17 runs sampled)
|
52 | deflate-gildas x 5.06 ops/sec ±6.00% (16 runs sampled)
|
53 | deflate-imaya x 3.52 ops/sec ±3.71% (13 runs sampled)
|
54 | ! deflate-pako x 11.52 ops/sec ±0.22% (32 runs sampled)
|
55 | deflate-pako-string x 9.53 ops/sec ±1.12% (27 runs sampled)
|
56 | deflate-pako-untyped x 5.44 ops/sec ±0.72% (17 runs sampled)
|
57 | * deflate-zlib x 14.05 ops/sec ±3.34% (63 runs sampled)
|
58 | inflate-dankogai x 42.19 ops/sec ±0.09% (56 runs sampled)
|
59 | inflate-imaya x 79.68 ops/sec ±1.07% (68 runs sampled)
|
60 | ! inflate-pako x 97.52 ops/sec ±0.83% (80 runs sampled)
|
61 | inflate-pako-string x 45.19 ops/sec ±1.69% (57 runs sampled)
|
62 | inflate-pako-untyped x 24.35 ops/sec ±2.59% (40 runs sampled)
|
63 | * inflate-zlib x 60.32 ops/sec ±1.36% (69 runs sampled)
|
64 | ```
|
65 |
|
66 | zlib's test is partially affected by marshalling (that make sense for inflate only).
|
67 | You can change deflate level to 0 in benchmark source, to investigate details.
|
68 | For deflate level 6 results can be considered as correct.
|
69 |
|
70 | __Install:__
|
71 |
|
72 | node.js:
|
73 |
|
74 | ```
|
75 | npm install pako
|
76 | ```
|
77 |
|
78 | browser:
|
79 |
|
80 | ```
|
81 | bower install pako
|
82 | ```
|
83 |
|
84 |
|
85 | Example & API
|
86 | -------------
|
87 |
|
88 | Full docs - http://nodeca.github.io/pako/
|
89 |
|
90 | ```javascript
|
91 | var pako = require('pako');
|
92 |
|
93 | // Deflate
|
94 | //
|
95 | var input = new Uint8Array();
|
96 | //... fill input data here
|
97 | var output = pako.deflate(input);
|
98 |
|
99 | // Inflate (simple wrapper can throw exception on broken stream)
|
100 | //
|
101 | var compressed = new Uint8Array();
|
102 | //... fill data to uncompress here
|
103 | try {
|
104 | var result = pako.inflate(compressed);
|
105 | } catch (err) {
|
106 | console.log(err);
|
107 | }
|
108 |
|
109 | //
|
110 | // Alternate interface for chunking & without exceptions
|
111 | //
|
112 |
|
113 | var inflator = new pako.Inflate();
|
114 |
|
115 | inflator.push(chunk1, false);
|
116 | inflator.push(chunk2, false);
|
117 | ...
|
118 | inflator.push(chunkN, true); // true -> last chunk
|
119 |
|
120 | if (inflator.err) {
|
121 | console.log(inflator.msg);
|
122 | }
|
123 |
|
124 | var output = inflator.result;
|
125 |
|
126 | ```
|
127 |
|
128 | Sometime you can wish to work with strings. For example, to send
|
129 | big objects as json to server. Pako detects input data type. You can
|
130 | force output to be string with option `{ to: 'string' }`.
|
131 |
|
132 | ```javascript
|
133 | var pako = require('pako');
|
134 |
|
135 | var test = { my: 'super', puper: [456, 567], awesome: 'pako' };
|
136 |
|
137 | var binaryString = pako.deflate(JSON.stringify(test), { to: 'string' });
|
138 |
|
139 | //
|
140 | // Here you can do base64 encode, make xhr requests and so on.
|
141 | //
|
142 |
|
143 | var restored = JSON.parse(pako.inflate(binaryString, { to: 'string' }));
|
144 | ```
|
145 |
|
146 |
|
147 | Notes
|
148 | -----
|
149 |
|
150 | Pako does not contain some specific zlib functions:
|
151 |
|
152 | - __deflate__ - methods `deflateCopy`, `deflateBound`, `deflateParams`,
|
153 | `deflatePending`, `deflatePrime`, `deflateTune`.
|
154 | - __inflate__ - methods `inflateCopy`, `inflateMark`,
|
155 | `inflatePrime`, `inflateGetDictionary`, `inflateSync`, `inflateSyncPoint`, `inflateUndermine`.
|
156 | - High level inflate/deflate wrappers (classes) may not support some flush
|
157 | modes. Those should work: Z_NO_FLUSH, Z_FINISH, Z_SYNC_FLUSH.
|
158 |
|
159 |
|
160 | pako for enterprise
|
161 | -------------------
|
162 |
|
163 | Available as part of the Tidelift Subscription
|
164 |
|
165 | 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.](https://tidelift.com/subscription/pkg/npm-pako?utm_source=npm-pako&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
166 |
|
167 |
|
168 | Authors
|
169 | -------
|
170 |
|
171 | - Andrey Tupitsin [@anrd83](https://github.com/andr83)
|
172 | - Vitaly Puzrin [@puzrin](https://github.com/puzrin)
|
173 |
|
174 | Personal thanks to:
|
175 |
|
176 | - Vyacheslav Egorov ([@mraleph](https://github.com/mraleph)) for his awesome
|
177 | tutorials about optimising JS code for v8, [IRHydra](http://mrale.ph/irhydra/)
|
178 | tool and his advices.
|
179 | - David Duponchel ([@dduponchel](https://github.com/dduponchel)) for help with
|
180 | testing.
|
181 |
|
182 | Original implementation (in C):
|
183 |
|
184 | - [zlib](http://zlib.net/) by Jean-loup Gailly and Mark Adler.
|
185 |
|
186 |
|
187 | License
|
188 | -------
|
189 |
|
190 | - MIT - all files, except `/lib/zlib` folder
|
191 | - ZLIB - `/lib/zlib` content
|