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

refactor: simplify encoding negotiation logic #213

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
name:
- Node.js 0.8
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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']
var encodingSupported = ['gzip', 'deflate', 'identity', 'br']

/**
* Compress response data with gzip / deflate.
Expand Down Expand Up @@ -200,7 +200,7 @@ function compression (options) {

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

// 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
Loading