Skip to content

Commit

Permalink
feat: add default option (#191)
Browse files Browse the repository at this point in the history
* add test for default encoding

* add functionality

* support *

* add docs

* fix logic

* change defaultEncoding to enforceEnconding

* sort conditional
  • Loading branch information
bjohansebas authored Dec 5, 2024
1 parent b7d5d77 commit f4e596c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
unreleased
==========
* Use `res.headersSent` when available
* Add the enforceEncoding option for requests without `Accept-Encoding` header

1.7.5 / 2024-10-31
==========

* deps: Replace accepts with negotiator@~0.6.4
- Add preference option
* deps: bytes@3.1.2
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ 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.

##### 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.

The default value is `identity`.

#### .filter

The default `filter` function. This is used to construct a custom filter
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ module.exports.filter = shouldCompress

var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/

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

/**
* Compress response data with gzip / deflate.
*
Expand All @@ -51,6 +53,7 @@ function compression (options) {
// options
var filter = opts.filter || shouldCompress
var threshold = bytes.parse(opts.threshold)
var enforceEncoding = opts.enforceEncoding || 'identity'

if (threshold == null) {
threshold = 1024
Expand Down Expand Up @@ -177,6 +180,11 @@ function compression (options) {
var negotiator = new Negotiator(req)
var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip'])

// 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
80 changes: 80 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,86 @@ describe('compression()', function () {
.end()
})
})

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

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect('Content-Encoding', 'gzip')
.expect(200, 'hello, world', done)
})

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')
})

request(server)
.get('/')
.set('Accept-Encoding', '')
.expect(shouldNotHaveHeader('Content-Encoding'))
.expect(200, 'hello, world', done)
})

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')
})

request(server)
.get('/')
.set('Accept-Encoding', '')
.expect('Content-Encoding', 'gzip')
.expect(200, 'hello, world', done)
})

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')
})

request(server)
.get('/')
.set('Accept-Encoding', '')
.expect('Content-Encoding', 'deflate')
.expect(200, 'hello, world', done)
})

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')
})

request(server)
.get('/')
.set('Accept-Encoding', '')
.expect(shouldNotHaveHeader('Content-Encoding'))
.expect(200, 'hello, world', done)
})

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')
})

request(server)
.get('/')
.set('Accept-Encoding', '')
.expect('Content-Encoding', 'gzip')
.expect(200, 'hello, world', done)
})
})
})

function createServer (opts, fn) {
Expand Down

0 comments on commit f4e596c

Please # to comment.