What is iltorb?
The iltorb npm package is a Node.js binding for the Brotli compression algorithm, which is known for its high compression ratio and speed. It allows you to compress and decompress data using Brotli, making it useful for web applications, file storage, and data transfer.
What are iltorb's main functionalities?
Compression
This feature allows you to compress data using the Brotli algorithm. The code sample demonstrates how to compress a file named 'input.txt' and save the compressed output to 'output.txt.br'.
const iltorb = require('iltorb');
const fs = require('fs');
const input = fs.createReadStream('input.txt');
const output = fs.createWriteStream('output.txt.br');
input.pipe(iltorb.compressStream()).pipe(output);
Decompression
This feature allows you to decompress Brotli-compressed data. The code sample shows how to decompress a file named 'output.txt.br' and save the decompressed output to 'decompressed.txt'.
const iltorb = require('iltorb');
const fs = require('fs');
const input = fs.createReadStream('output.txt.br');
const output = fs.createWriteStream('decompressed.txt');
input.pipe(iltorb.decompressStream()).pipe(output);
Synchronous Compression
This feature provides a synchronous method to compress data. The code sample demonstrates how to read a file synchronously, compress its contents, and write the compressed data to a new file.
const iltorb = require('iltorb');
const fs = require('fs');
const input = fs.readFileSync('input.txt');
const compressed = iltorb.compressSync(input);
fs.writeFileSync('output.txt.br', compressed);
Synchronous Decompression
This feature provides a synchronous method to decompress data. The code sample shows how to read a compressed file synchronously, decompress its contents, and write the decompressed data to a new file.
const iltorb = require('iltorb');
const fs = require('fs');
const compressed = fs.readFileSync('output.txt.br');
const decompressed = iltorb.decompressSync(compressed);
fs.writeFileSync('decompressed.txt', decompressed);
Other packages similar to iltorb
brotli
The 'brotli' npm package is another implementation of the Brotli compression algorithm for Node.js. It provides similar functionality to iltorb, including both compression and decompression capabilities. However, 'brotli' is a pure JavaScript implementation, which might be slower compared to iltorb's native bindings.
node-zopfli
The 'node-zopfli' package provides bindings for the Zopfli compression algorithm, which is known for its high compression ratio, similar to Brotli. While it offers similar compression and decompression functionalities, Zopfli is generally slower than Brotli but can achieve slightly better compression ratios.
pako
The 'pako' package is a fast zlib port to JavaScript, providing compression and decompression using the DEFLATE algorithm. While it does not use Brotli, it offers similar functionalities for compressing and decompressing data, making it a good alternative for applications that do not specifically require Brotli.
iltorb
iltorb is a Node.js package offering native bindings for the brotli compression library.
Install
This module uses prebuild
to download a pre-compiled binary for your platform, if it exists. Otherwise, it will use node-gyp
to build the module.
npm install iltorb
Prerequisites for Building
The following is required to build from source or when a pre-compiled binary does not exist.
Methods
Async
Omitting the callback argument will result in the compress and decompress methods to return a Promise.
compress(buffer[, brotliEncodeParams][, callback])
const compress = require('iltorb').compress;
compress(input, function(err, output) {
});
compress(input)
.then(output => )
.catch(err => );
try {
const output = await compress(input);
} catch(err) {
}
decompress(buffer[, callback])
const decompress = require('iltorb').decompress;
decompress(input, function(err, output) {
});
decompress(input)
.then(output => )
.catch(err => );
try {
const output = await decompress(input);
} catch(err) {
}
Sync
compressSync(buffer[, brotliEncodeParams])
const compressSync = require('iltorb').compressSync;
try {
var output = compressSync(input);
} catch(err) {
}
decompressSync(buffer)
const decompressSync = require('iltorb').decompressSync;
try {
var output = decompressSync(input);
} catch(err) {
}
Stream
compressStream([brotliEncodeParams])
const compressStream = require('iltorb').compressStream;
const fs = require('fs');
fs.createReadStream('path/to/input')
.pipe(compressStream())
.pipe(fs.createWriteStream('path/to/output'));
compressionStream.flush()
Call this method to flush pending data. Don't call this frivolously, premature flushes negatively impact the effectiveness of the compression algorithm.
decompressStream()
const decompressStream = require('iltorb').decompressStream;
const fs = require('fs');
fs.createReadStream('path/to/input')
.pipe(decompressStream())
.pipe(fs.createWriteStream('path/to/output'));
brotliEncodeParams
The compress
, compressSync
and compressStream
methods may accept an optional brotliEncodeParams
object to define some or all of brotli's compression parameters:
const brotliEncodeParams = {
mode: 0,
quality: 11,
lgwin: 22,
lgblock: 0,
disable_literal_context_modeling: false,
size_hint: 0,
large_window: false,
npostfix: 0,
ndirect: 0
};
Troubleshooting
-
I am unable to install iltorb
because the host (GitHub) that serves the binaries is blocked by my firewall.
a) By default, if the binaries could not be downloaded for any reason, the install script will attempt to compile the binaries locally on your machine. This requires having all of the build requirements fulfilled.
b) You can override the binary.host
value found in package.json
with the following methods:
- using the following ENV variable
npm_config_iltorb_binary_host=https://domain.tld/path
- as an additional argument with npm install
--iltorb_binary_host=https://domain.tld/path
Note: Both of these would result in downloading the binary from https://domain.tld/path/vX.X.X/iltorb-vX.X.X-node-vXX-arch.tar.gz