Skip to content

Commit

Permalink
fix: don't handle * in enforceEncoding
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Jan 16, 2025
1 parent 82c9cb5 commit 6071e4e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
6 changes: 2 additions & 4 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 @@ -199,8 +197,8 @@ function compression (options) {
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
if (!req.headers['accept-encoding']) {
method = ['gzip', 'deflate', 'identity', 'br'].includes(enforceEncoding) ? enforceEncoding : method
}

// negotiation failed
Expand Down
6 changes: 3 additions & 3 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ describe('compression()', function () {
.expect(200, 'hello, world', done)
})

it('should be gzip if no accept-encoding is sent when enforceEncoding is *', function (done) {
it('should not compress when enforceEnconding is *', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: '*' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand All @@ -969,8 +969,8 @@ describe('compression()', function () {
request(server)
.get('/')
.set('Accept-Encoding', '')
.expect('Content-Encoding', 'gzip')
.expect(200, 'hello, world', done)
.expect(shouldNotHaveHeader('Content-Encoding'))
.expect(200, done)
})
})
})
Expand Down

0 comments on commit 6071e4e

Please sign in to comment.