Skip to content

Commit 051fd20

Browse files
committed
feat: support brotli
1 parent becc1c0 commit 051fd20

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-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+
- brotli
1415

1516
## Install
1617

index.js

Lines changed: 9 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,11 @@ function compression (options) {
175175

176176
// compression method
177177
var accept = accepts(req)
178-
var method = accept.encoding(['gzip', 'deflate', 'identity'])
178+
var method = accept.encoding(['br', 'gzip', 'deflate', 'identity'])
179179

180180
// we really don't prefer deflate
181-
if (method === 'deflate' && accept.encoding(['gzip'])) {
182-
method = accept.encoding(['gzip', 'identity'])
181+
if (method === 'deflate' && accept.encoding(['br', 'gzip'])) {
182+
method = accept.encoding(['br', 'gzip', 'identity'])
183183
}
184184

185185
// negotiation failed
@@ -190,9 +190,11 @@ function compression (options) {
190190

191191
// compression stream
192192
debug('%s compression', method)
193-
stream = method === 'gzip'
194-
? zlib.createGzip(opts)
195-
: zlib.createDeflate(opts)
193+
switch (method) {
194+
case 'br': stream = zlib.createBrotliDecompress(opts);break;
195+
case 'gzip': stream = zlib.createGzip(opts);break;
196+
case 'deflate': stream = zlib.createDeflate(opts);break;
197+
}
196198

197199
// add buffered listeners to stream
198200
addListeners(stream, stream.on, listeners)

0 commit comments

Comments
 (0)