Skip to content

Commit

Permalink
change defaultEncoding to enforceEnconding
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Nov 5, 2024
1 parent 86d65a9 commit 073f59f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ The default value is `zlib.Z_DEFAULT_WINDOWBITS`, or `15`.
See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
regarding the usage.

##### defaultEncoding
##### enforceEncoding

This is the default encoding to use when the client does not specify an encoding in the request's [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) header.

Expand Down
10 changes: 3 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function compression (options) {
// options
var filter = opts.filter || shouldCompress
var threshold = bytes.parse(opts.threshold)
var defaultEncoding = opts.defaultEncoding || 'identity'
var enforceEncoding = opts.enforceEncoding || 'identity'

if (threshold == null) {
threshold = 1024
Expand Down Expand Up @@ -181,8 +181,8 @@ function compression (options) {
var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip'])

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

// negotiation failed
Expand All @@ -191,10 +191,6 @@ function compression (options) {
return
}

if (opts.defaultEncoding) {
opts.defaultEncoding = undefined
}

// compression stream
debug('%s compression', method)
stream = method === 'gzip'
Expand Down
24 changes: 12 additions & 12 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,9 @@ describe('compression()', function () {
})
})

describe('defaultEncoding', function () {
describe('enforceEncoding', function () {
it('should compress the provided encoding and not the default encoding', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'deflate' }, function (req, res) {
var server = createServer({ threshold: 0, enforceEncoding: 'deflate' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
Expand All @@ -672,8 +672,8 @@ describe('compression()', function () {
.expect(200, 'hello, world', done)
})

it('should not compress when defaultEncoding is identity', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'identity' }, function (req, res) {
it('should not compress when enforceEncoding is identity', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: 'identity' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
Expand All @@ -685,8 +685,8 @@ describe('compression()', function () {
.expect(200, 'hello, world', done)
})

it('should compress when defaultEncoding is gzip', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'gzip' }, function (req, res) {
it('should compress when enforceEncoding is gzip', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: 'gzip' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
Expand All @@ -698,8 +698,8 @@ describe('compression()', function () {
.expect(200, 'hello, world', done)
})

it('should compress when defaultEncoding is deflate', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'deflate' }, function (req, res) {
it('should compress when enforceEncoding is deflate', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: 'deflate' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
Expand All @@ -711,8 +711,8 @@ describe('compression()', function () {
.expect(200, 'hello, world', done)
})

it('should not compress when defaultEncoding is unknown', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'bogus' }, function (req, res) {
it('should not compress when enforceEncoding is unknown', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: 'bogus' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
Expand All @@ -724,8 +724,8 @@ describe('compression()', function () {
.expect(200, 'hello, world', done)
})

it('should be gzip if no accept-encoding is sent when defaultEncoding is *', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: '*' }, function (req, res) {
it('should be gzip if no accept-encoding is sent when enforceEncoding is *', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: '*' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
Expand Down

0 comments on commit 073f59f

Please sign in to comment.