Brotli Compression in Nodejs11

What’s Brotli Format

From WIKI

Brotli is a data format specification[1] for data streams compressed with a specific combination of the general-purpose LZ77 lossless compression algorithm, Huffman coding and 2nd order context modelling. Brotli was initially developed to decrease the size of transmissions of WOFF2 web fonts, and in that context was a continuation of the development of zopfli, which is a zlib-compatible implementation of the standard gzip and deflate specifications.

Nodejs Native Support

Node v11.7.0 start to support Brotli format natively.

How to Use

Code Example

const zlib = require('zlib');
const fs = require('fs');

const filename = 'testdata.txt';

async function compressFile1() {
  return new Promise((resolve, reject) => {
    const compress = zlib.createBrotliCompress();
    const input = fs.createReadStream(filename);
    const output = fs.createWriteStream(filename + '.br');

    input.pipe(compress).pipe(output);
    output.on('finish', () => {
      resolve();
    });
    output.on('error', ex => {
      reject(ex);
    });
  });
}

async function uncompressFile1() {
  return new Promise((resolve, reject) => {
    const decompress = zlib.createBrotliDecompress();
    const input = fs.createReadStream(filename + '.br');
    const output = fs.createWriteStream(filename + '.br.txt');

    input.pipe(decompress).pipe(output);
    output.on('finish', () => {
      resolve();
    });
    output.on('error', ex => {
      reject(ex);
    });
  });
}

(async () => {
  console.log('start compress');
  await compressFile1();
  console.log('start uncompress');
  await uncompressFile1();
  console.log('done');
})();

setTimeout(() => {}, 5000);

The result is, as it promised, the brotli compression outperform gzip.

The Official Document


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1