Skip to content

A JavaScript port of the Brotli compression algorithm, as used in WOFF2

Notifications You must be signed in to change notification settings

lorejoh12/brotli.js

 
 

Repository files navigation

Purpose of this Fork

This project is a fork of https://github.com/foliojs/brotli.js created for use in https://github.com/Hopding/pdf-lib.

Listed below are changes that have been made in this fork:

Also see

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

NPM Module

To install the latest stable version:

# With npm
npm install --save @pdf-lib/brotli.js

# With yarn
yarn add @pdf-lib/brotli.js

This assumes you're using npm or yarn as your package manager.

UMD Module

You can build this fork using Rollup for use in the browser, or other JS environments, if you are not using a package manager.

Importing

In node, or in browserify, you can load brotli in the standard way:

// ES6
import brotli from 'brotli';

// CommonJS
const brotli = require('brotli');

You can also import just the decompress of compress functions. For example, here's how you'd import just the decompress function.

// ES6
import decompress from 'brotli/decompress';

// CommonJS
const 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.

// 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'));

brotli.compress(buffer, isText = false)

Compresses the given buffer. Pass optional parameters as the second argument.

// encode a buffer of binary data
brotli.compress(fs.readFileSync('myfile.bin'));

// encode some data with options (default options shown)
brotli.compress(fs.readFileSync('myfile.bin'), {
  mode: 0, // 0 = generic, 1 = text, 2 = font (WOFF2)
  quality: 11, // 0 - 11
  lgwin: 22 // window size
});

License

MIT

About

A JavaScript port of the Brotli compression algorithm, as used in WOFF2

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 99.9%
  • Other 0.1%