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
compress(buffer[, brotliEncodeParams], callback)
const compress = require('iltorb').compress;
compress(input, function(err, output) {
});
decompress(buffer[, brotliDecodeParams], callback)
const decompress = require('iltorb').decompress;
decompress(input, function(err, output) {
});
Sync
compressSync(buffer[, brotliEncodeParams])
const compressSync = require('iltorb').compressSync;
try {
var output = compressSync(input);
} catch(err) {
}
decompressSync(buffer[, brotliDecodeParams])
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([brotliDecodeParams])
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,
size_hint: 0,
disable_literal_context_modeling: false,
dictionary: Buffer
};
brotliDecodeParams
The decompress
, decompressSync
and decompressStream
methods may accept an optional brotliDecodeParams
object to provide a custom dictionary.
const brotliDecodeParams = {
dictionary: Buffer
};