WebAssembly compiled Zopfli library.
npm install -S wasm-zopfli
The awesome thing about
wasm-zopfli
is that it does not need to compile or download any prebuilt binaries!
Because WebAssembly is supported on both Node.js and several browsers,
wasm-zopfli
is super easy to use.
An example of compressing something and saving it to a file via Node.js.
import { gzip } from 'wasm-zopfli';
import { writeFile } from 'fs';
import { promisify } from 'util';
const writeFileAsync = promisify(writeFile);
const content = Buffer.from('Hello, world!', 'utf8');
(async () => {
try {
const compressedContent = await gzip(content);
await writeFileAsync('./hello_world.txt.gz', compressedContent);
} catch (err) {
console.error(err);
}
})();
An example of compressing something and downloading it from the browser.
import { gzip } from 'wasm-zopfli';
const content = new TextEncoder('utf-8').encode('Hello, world!');
(async () => {
try {
const compressedContent = await gzip(content);
const file = new File([compressedContent], 'hello_world.txt.gz', { type: 'application/gzip' });
const link = document.createElement('a');
link.setAttribute('href', URL.createObjectURL(file));
link.setAttribute('download', file.name);
link.click();
} catch (err) {
console.error(err);
}
})();
data
<Uint8Array>
Compress data
using deflate. This is is referred to as "deflate raw" by
Node.js' documentation.
data
<Uint8Array>
Compress data
using gzip.
data
<Uint8Array>
Compress data
using zlib. This is is referred to as "deflate" by Node.js'
documentation.
format
<FORMAT_DEFLATE>
| <FORMAT_GZIP>
| <FORMAT_ZLIB>
data
<Uint8Array>
The function that deflate
, gzip
, and zlib
wrap. Pass any of the constants
below and data to compress.
Constant, reference, for compressing data with zopfli
using deflate.
Constant, reference, for compressing data with zopfli
using gzip.
Constant, reference, for compressing data with zopfli
using zlib.
Want to see how fast this is? Go to the benchmark directory to see results, instructions on running your own benchmark, and more.
To build wasm-zopfli
you will need to install Docker, and
pull rustlang/rust:nightly
. After that all that is needed is
to do the following:
npm install
npm run build
npm test