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.
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');
You can also require just the decompress
function or just the compress
function, which is useful for browserify builds.
For example, here's how you'd require just the decompress
function.
var decompress = require('brotli/decompress');
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.
// decode a buffer where the output size is known
brotli.decompress(compressedData, uncompressedLength);
// decode a buffer where the output size is not known
brotli.decompress(fs.readFileSync('compressed.bin'));
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.
// encode a buffer of binary data
brotli.compress(fs.readFileSync('myfile.bin'));
// encode some text data
brotli.compress(fs.readFileSync('myfile.bin'), true);
MIT