Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add brotli support #156

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -14,6 +14,7 @@ node_js:
- "9.11"
- "10.15"
- "11.10"
- "12.13.0"
patrickmichalina marked this conversation as resolved.
Show resolved Hide resolved
sudo: false
cache:
directories:
Expand Down
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -11,6 +11,7 @@ The following compression codings are supported:

- deflate
- gzip
- br (requires node v10.16.0 or higher)

## Install

Expand Down Expand Up @@ -169,6 +170,19 @@ function shouldCompress (req, res) {
This module adds a `res.flush()` method to force the partially-compressed
response to be flushed to the client.

#### brotli

The default `filter` function. This is used to construct a custom filter
function that is an extension of the default function.

```js
var compression = require('compression')
var express = require('express')

var app = express()
app.use(compression({ brotli: { enabled: true, zlib: { } } }))
```

## Examples

### express/connect
Expand Down
30 changes: 18 additions & 12 deletions index.js
Expand Up @@ -36,6 +36,7 @@ module.exports.filter = shouldCompress
*/

var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
var defaultThreshold = 1024

/**
* Compress response data with gzip / deflate.
Expand All @@ -50,10 +51,14 @@ function compression (options) {

// options
var filter = opts.filter || shouldCompress
var brotli = opts.brotli || {}
var brotliZlib = brotli.zlib || {}
var threshold = bytes.parse(opts.threshold)
var supportsBrotli = typeof zlib.createBrotliCompress === 'function'
var brotliEnabled = brotli.enabled && supportsBrotli

if (threshold == null) {
threshold = 1024
if (threshold === null) {
threshold = defaultThreshold
}

return function compression (req, res, next) {
Expand Down Expand Up @@ -174,25 +179,26 @@ function compression (options) {
}

// compression method
var filterBrotliIfNotSupported = function (encoding) { return encoding !== 'br' || brotliEnabled }
var checkEncoding = function (accept) { return function (encoding) { return accept.encoding(encoding) } }
var accept = accepts(req)
var method = accept.encoding(['gzip', 'deflate', 'identity'])

// we really don't prefer deflate
if (method === 'deflate' && accept.encoding(['gzip'])) {
method = accept.encoding(['gzip', 'identity'])
}
var method = ['br', 'gzip', 'deflate']
.filter(filterBrotliIfNotSupported)
.filter(checkEncoding(accept))[0] || 'identity'

// negotiation failed
if (!method || method === 'identity') {
if (method === 'identity') {
nocompress('not acceptable')
return
}

// compression stream
debug('%s compression', method)
stream = method === 'gzip'
? zlib.createGzip(opts)
: zlib.createDeflate(opts)
switch (method) {
case 'br': stream = zlib.createBrotliCompress(brotliZlib); break
case 'gzip': stream = zlib.createGzip(opts); break
case 'deflate': stream = zlib.createDeflate(opts); break
}

// add buffered listeners to stream
addListeners(stream, stream.on, listeners)
Expand Down