Brotli.js
Brotli.js is port of the Brotli compression algorithm (as used in the WOFF2 font format) to JavaScript using Emscripten. The original C++ source code can be found
here and in this repo as a submodule.
Installation and usage
Install using npm.
npm install brotli
If you want to use brotli in the browser, you should use Browserify to build it.
In node, or in browserify, you can load brotli in the standard way:
var brotli = require('brotli');
API
brotli.decode(buffer, outSize)
Decompresses the given buffer to produce the original input to the compressor.
This function works best if you know the original size of the data (e.g. to decompressed size).
Inside a WOFF2 file, this is encoded as a field in the header of the binary file.
If you don't know the size of the input, don't include the outSize argument and
the function will guess a size that it thinks will be
big enough. If it guessed too small, or there was a decoding error, null
is returned.
brotli.decode(compressedData, uncompressedLength);
brotli.decode(fs.readFileSync('compressed.bin'));
brotli.encode(buffer, isText = false)
Compresses the given buffer. Pass true
as the second argument if the input
buffer is text data. Pass false
or nothing if it is binary. This function
is known to be quite slow.
brotli.encode(fs.readFileSync('myfile.bin'));
brotli.encode(fs.readFileSync('myfile.bin'), true);
License
MIT