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
4 changes: 0 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ function compression (options) {

var shouldCache = opts.cache || function () { return true }

var dummyBrotliFlush = function () { }

return function compression (req, res, next) {
var ended = false
var length
Expand Down Expand Up @@ -239,8 +237,6 @@ function compression (options) {
switch (method) {
case 'br':
stream = iltorb.compressStream(brotliOpts)
// brotli has no flush method. add a dummy flush method here.
stream.flush = dummyBrotliFlush
break
case 'gzip':
stream = zlib.createGzip(zlibOpts)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"bytes": "2.5.0",
"compressible": "2.0.10",
"debug": "2.6.3",
"iltorb": "1.0.13",
"iltorb": "2.0.2",
"lru-cache": "4.0.2",
"multipipe": "1.0.2",
"node-zopfli": "2.0.2",
Expand Down
72 changes: 61 additions & 11 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,17 +527,6 @@ describe('compression()', function () {
done()
})
})

it('should not throw if flush() is called', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('hello, ')
res.flush()
res.end('world')
})

brotliRequest(server).expect('Content-Encoding', 'br', done)
})
})

describe('when caching is turned on', function () {
Expand Down Expand Up @@ -813,6 +802,37 @@ describe('compression()', function () {
.end()
})

it('should flush the response for brotli', function (done) {
var chunks = 0
var resp
var server = createServer({ threshold: 0 }, function (req, res) {
resp = res
res.setHeader('Content-Type', 'text/plain')
res.setHeader('Content-Length', '2048')
write()
})

function write () {
chunks++
if (chunks === 2) return resp.end()
if (chunks > 2) return chunks--
resp.write(new Buffer(1024))
resp.flush()
}

brotliRequest(server)
.request()
.on('response', function (res) {
assert.equal(res.headers['content-encoding'], 'br')
res.on('data', write)
res.on('end', function () {
assert.equal(chunks, 2)
done()
})
})
.end()
})

it('should flush small chunks for gzip', function (done) {
var chunks = 0
var resp
Expand Down Expand Up @@ -872,6 +892,36 @@ describe('compression()', function () {
})
.end()
})

it('should flush small chunks for brotli', function (done) {
var chunks = 0
var resp
var server = createServer({ threshold: 0 }, function (req, res) {
resp = res
res.setHeader('Content-Type', 'text/plain')
write()
})

function write () {
chunks++
if (chunks === 20) return resp.end()
if (chunks > 20) return chunks--
resp.write('..')
resp.flush()
}

brotliRequest(server)
.request()
.on('response', function (res) {
assert.equal(res.headers['content-encoding'], 'br')
res.on('data', write)
res.on('end', function () {
assert.equal(chunks, 20)
done()
})
})
.end()
})
})
})

Expand Down