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:
- Removed unused imports of the
fs
module - Always import the compressed dictionary data, whether in node or the browser
- Removed circular dependency so module can be bundled with rollup
- Released to NPM as
@pdf-lib/brotli.js
Also see
- https://github.com/Hopding/fontkit
- https://github.com/Hopding/unicode-properties
- https://github.com/Hopding/restructure
- https://github.com/Hopding/png-ts
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.
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.
You can build this fork using Rollup for use in the browser, or other JS environments, if you are not using a package manager.
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');
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'));
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
});