Skip to content
This repository has been archived by the owner on Sep 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #20 from SerayaEryn/brotli-support
Browse files Browse the repository at this point in the history
Add brotli support
  • Loading branch information
SerayaEryn committed Apr 28, 2018
2 parents d456309 + a890a4c commit 335db60
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Coverage Status](https://coveralls.io/repos/github/SerayaEryn/fastify-compression/badge.svg?branch=master)](https://coveralls.io/github/SerayaEryn/fastify-compression?branch=master)
[![NPM version](https://img.shields.io/npm/v/fastify-compression.svg?style=flat)](https://www.npmjs.com/package/fastify-compression)

A compression plugin for [Fastify](http://fastify.io/). Supports `gzip` and `deflate`.
A compression plugin for [Fastify](http://fastify.io/). Supports `gzip`, `deflate` and `brotli`.

## Usage

Expand All @@ -19,10 +19,12 @@ app.register(fastifyCompression, {threshold: 2048});

## API
### compression(fastify, options, next)
Compresses the payload with `gzip` or `deflate` if the payload length is above the threshold and a `Accept-Encoding` header is send with the request. In case of an asterisk `*` in the `Accept-Encoding` header `gzip` will be chosen.
Compresses the payload with `gzip`, `br` or `deflate` if the payload length is above the threshold and a `Accept-Encoding` header is send with the request. In case of an asterisk `*` in the `Accept-Encoding` header `gzip` will be chosen.
### options
#### threshold (optional)
A `number` that specifies the threshold used to determine if compression should be applied. Defaults to `1024`.
#### brotli
To enable Brotli compression pass the [iltorb](https://www.npmjs.com/package/iltorb) module with the `brotli` option.

## License

Expand Down
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ const pump = require('pump');
function compressionPlugin(fastify, opts, next) {
const threshold = opts.threshold || 1024;
const supportedEncodings = ['gzip', 'deflate'];
const compressionStreams = {
gzip: zlib.createGzip,
deflate: zlib.createDeflate
}

if (opts.brotli) {
compressionStreams.br = opts.brotli.compressStream;
supportedEncodings.push('br');
}

fastify.addHook('onSend', compression)

Expand All @@ -18,15 +27,14 @@ function compressionPlugin(fastify, opts, next) {
const method = encodingNegotiator.negotiate(acceptEncoding, supportedEncodings);

if (shouldCompress(reply, method)) {
let payloadStream;
if (Buffer.byteLength(payload) < threshold) {
done();
return;
}
payloadStream = stringToStream(payload);
let payloadStream = stringToStream(payload);
setVaryHeader(reply);
reply.header('Content-Encoding', method);
const compressionStream = method === 'gzip' ? zlib.createGzip() : zlib.createDeflate();
const compressionStream = compressionStreams[method]();

pump(payloadStream, compressionStream, onEnd.bind(request))
done(null, compressionStream);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A compression plugin for fastify",
"main": "index.js",
"scripts": {
"unit": "tap test/*",
"unit": "tap test/*.test.js",
"test": "npm run lint && npm run unit",
"lint": "./node_modules/eslint/bin/eslint.js index.js lib/*",
"coveralls": "npm run unit -- --cov",
Expand Down Expand Up @@ -34,6 +34,7 @@
},
"devDependencies": {
"eslint": "^4.19.1",
"iltorb": "^2.0.3",
"fastify": "^1.2.1",
"request": "^2.85.0",
"tap": "^11.1.4"
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions test/brotli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const t = require('tap');
const test = t.test;
const Fastify = require('fastify');
const request = require('request');
const fastifyCompression = require('..');
const brotli = require('iltorb');

test('should compress with brotli if larger than threshold', t => {
t.plan(6);

const fastify = Fastify();

const options = {
threshold: 8,
brotli
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'br'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.notOk(response.headers['content-length']);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'br');
t.strictEqual(brotli.decompressSync(body).toString('utf-8'), 'something larger than threshold');
})
});
});

0 comments on commit 335db60

Please sign in to comment.