Skip to content

Commit

Permalink
refactor: simplify encoding negotiation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Jan 14, 2025
1 parent 82c9cb5 commit 0b35719
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 9 deletions.
9 changes: 1 addition & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
var SUPPORTED_ENCODING = hasBrotliSupport ? ['br', 'gzip', 'deflate', 'identity'] : ['gzip', 'deflate', 'identity']
var PREFERRED_ENCODING = hasBrotliSupport ? ['br', 'gzip'] : ['gzip']

var encodingSupported = ['*', 'gzip', 'deflate', 'identity', 'br']

/**
* Compress response data with gzip / deflate.
*
Expand Down Expand Up @@ -195,14 +193,9 @@ function compression (options) {
}

// compression method
var negotiator = new Negotiator(req)
var negotiator = new Negotiator({ headers: req.headers['accept-encoding'] ? req.headers : { 'accept-encoding': enforceEncoding } })
var method = negotiator.encoding(SUPPORTED_ENCODING, PREFERRED_ENCODING)

// if no method is found, use the default encoding
if (!req.headers['accept-encoding'] && encodingSupported.indexOf(enforceEncoding) !== -1) {
method = enforceEncoding === '*' ? 'gzip' : enforceEncoding
}

// negotiation failed
if (!method || method === 'identity') {
nocompress('not acceptable')
Expand Down
2 changes: 1 addition & 1 deletion test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ describe('compression()', function () {
request(server)
.get('/')
.set('Accept-Encoding', '')
.expect('Content-Encoding', 'gzip')
.expect('Content-Encoding', 'br')
.expect(200, 'hello, world', done)
})
})
Expand Down

0 comments on commit 0b35719

Please sign in to comment.