Skip to content
Open
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
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Skip compression for `206 Partial Content` responses

1.8.1 / 2025-07-17
==========

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ the middleware, based on the given `options`.

This middleware will never compress responses that include a `Cache-Control`
header with the [`no-transform` directive](https://tools.ietf.org/html/rfc7234#section-5.2.2.4),
as compressing will transform the body.
as compressing will transform the body. It will also never compress
[`206 Partial Content`](https://www.rfc-editor.org/rfc/rfc9110#section-15.3.7)
responses, as the `Content-Range` header describes the uncompressed body.

#### Options

Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ function compression (options) {
return
}

// response is partial content
if (res.statusCode === 206) {
nocompress('partial content')
return
}

// vary
vary(res, 'Accept-Encoding')

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

it('should skip partial content responses', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.statusCode = 206
res.setHeader('Content-Type', 'text/plain')
res.setHeader('Content-Range', 'bytes 0-4/12')
res.setHeader('Content-Length', '5')
res.end('hello')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect(shouldNotHaveHeader('Content-Encoding'))
.expect(shouldNotHaveHeader('Vary'))
.expect('Content-Range', 'bytes 0-4/12')
.expect('Content-Length', '5')
.expect(206, 'hello', done)
})

it('should skip partial content responses set via writeHead', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.writeHead(206, {
'Content-Type': 'text/plain',
'Content-Range': 'bytes 0-4/12',
'Content-Length': '5'
})
res.end('hello')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect(shouldNotHaveHeader('Content-Encoding'))
.expect('Content-Range', 'bytes 0-4/12')
.expect(206, 'hello', done)
})

it('should set Vary', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
Expand Down
Loading