Skip to content

Commit ca2f4d9

Browse files
committed
fix: merge remote-tracking branch 'clarkdo/brotli'
2 parents df7d798 + 09dce4f commit ca2f4d9

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The following compression codings are supported:
1111

1212
- deflate
1313
- gzip
14+
- br
1415

1516
## Install
1617

index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports.filter = shouldCompress
3838
var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
3939

4040
/**
41-
* Compress response data with gzip / deflate.
41+
* Compress response data with gzip / deflate / brotli.
4242
*
4343
* @param {Object} [options]
4444
* @return {Function} middleware
@@ -175,11 +175,20 @@ function compression (options) {
175175

176176
// compression method
177177
var accept = accepts(req)
178-
var method = accept.encoding(['gzip', 'deflate', 'identity'])
178+
var methods = ['gzip', 'deflate', 'identity']
179+
var preferredMethods = ['gzip']
180+
181+
if (zlib.createBrotliDecompress) {
182+
methods.unshift('br')
183+
preferredMethods.unshift('br')
184+
}
185+
186+
var method = accept.encoding(methods)
179187

180188
// we really don't prefer deflate
181-
if (method === 'deflate' && accept.encoding(['gzip'])) {
182-
method = accept.encoding(['gzip', 'identity'])
189+
if (method === 'deflate' && accept.encoding(preferredMethods)) {
190+
preferredMethods.push('identity')
191+
method = accept.encoding(preferredMethods)
183192
}
184193

185194
// negotiation failed
@@ -190,9 +199,11 @@ function compression (options) {
190199

191200
// compression stream
192201
debug('%s compression', method)
193-
stream = method === 'gzip'
194-
? zlib.createGzip(opts)
195-
: zlib.createDeflate(opts)
202+
switch (method) {
203+
case 'br': stream = zlib.createBrotliDecompress(opts); break
204+
case 'gzip': stream = zlib.createGzip(opts); break
205+
case 'deflate': stream = zlib.createDeflate(opts); break
206+
}
196207

197208
// add buffered listeners to stream
198209
addListeners(stream, stream.on, listeners)

0 commit comments

Comments
 (0)