What is brotli?
The brotli npm package is a compression library that implements the Brotli compression algorithm. It provides methods for compressing and decompressing data using Brotli in Node.js applications.
What are brotli's main functionalities?
Compression
This feature allows you to compress data using the Brotli algorithm. The code sample demonstrates how to read a file, compress its contents, and then write the compressed data to a new file with a .br extension.
const brotli = require('brotli');
const fs = require('fs');
const input = fs.readFileSync('input.txt');
const compressed = brotli.compress(input);
fs.writeFileSync('input.txt.br', compressed);
Decompression
This feature allows you to decompress data that was previously compressed with the Brotli algorithm. The code sample shows how to read a compressed file, decompress its contents, and then write the decompressed data to a new file.
const brotli = require('brotli');
const fs = require('fs');
const compressed = fs.readFileSync('input.txt.br');
const decompressed = brotli.decompress(compressed);
fs.writeFileSync('output.txt', decompressed);
Other packages similar to brotli
zlib
zlib is a compression library that offers compression and decompression functionalities similar to brotli but using different algorithms like Deflate and Gzip. It is built into Node.js, making it readily available without additional installation.
snappy
snappy is a package that provides bindings to Google's Snappy compression library. It is known for its high-speed compression and decompression, although it typically achieves lower compression ratios compared to Brotli.
lz4
lz4 is a package that implements LZ4 compression, which is known for its extremely fast compression and decompression speeds, but like snappy, it may not achieve compression ratios as high as Brotli.
Brotli.js
Brotli.js is port of the Brotli compression algorithm (as used in the WOFF2 font format) to JavaScript. The decompressor is hand ported, and the compressor is ported
with Emscripten. The original C++ source code can be found here.
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');
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');
API
brotli.decompress(buffer, [outSize])
Decompresses the given buffer to produce the original input to the compressor.
The outSize
parameter is optional, and will be computed by the decompressor
if not provided. Inside a WOFF2 file, this can be computed from the WOFF2 directory.
brotli.decompress(compressedData, uncompressedLength);
brotli.decompress(fs.readFileSync('compressed.bin'));
brotli.compress(buffer, isText = false)
Compresses the given buffer. Pass optional parameters as the second argument.
brotli.compress(fs.readFileSync('myfile.bin'));
brotli.compress(fs.readFileSync('myfile.bin'), {
mode: 0,
quality: 11,
lgwin: 22
});
License
MIT