diff --git a/.travis.yml b/.travis.yml index 107d1187..7fd9a70e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: node_js node_js: - - "0.8" - - "0.10" - "0.12" - "1.8" - "2.5" diff --git a/HISTORY.md b/HISTORY.md index 9df34d1c..d7501094 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ unreleased ========== + * Callbacks available in `write` and `end` when supported in nodejs (requires nodejs >= 0.12.x) * deps: bytes@2.4.0 * deps: compressible@~2.0.9 - Fix regex fallback to not override `compressible: false` in db diff --git a/index.js b/index.js index c0e801e7..87d3b6cf 100644 --- a/index.js +++ b/index.js @@ -74,7 +74,7 @@ function compression (options) { // proxy - res.write = function write (chunk, encoding) { + res.write = function write (chunk, encoding, cb) { if (ended) { return false } @@ -84,11 +84,11 @@ function compression (options) { } return stream - ? stream.write(new Buffer(chunk, encoding)) - : _write.call(this, chunk, encoding) + ? stream.write(new Buffer(chunk, encoding), cb) + : _write.call(this, chunk, encoding, cb) } - res.end = function end (chunk, encoding) { + res.end = function end (chunk, encoding, cb) { if (ended) { return false } @@ -103,7 +103,7 @@ function compression (options) { } if (!stream) { - return _end.call(this, chunk, encoding) + return _end.call(this, chunk, encoding, cb) } // mark ended @@ -111,8 +111,8 @@ function compression (options) { // write Buffer for Node.js 0.8 return chunk - ? stream.end(new Buffer(chunk, encoding)) - : stream.end() + ? stream.end(new Buffer(chunk, encoding), cb) + : stream.end(null, null, cb) } res.on = function on (type, listener) { diff --git a/test/compression.js b/test/compression.js index 81c00149..181de7ef 100644 --- a/test/compression.js +++ b/test/compression.js @@ -676,6 +676,68 @@ describe('compression()', function () { .end() }) }) + + describe('when callbacks are used', function () { + it('should call the passed callbacks in the order passed when compressing', function (done) { + var callbackOutput = [] + var server = createServer(null, function (req, res) { + res.setHeader('Content-Type', 'text/plain') + res.write('Hello', null, function () { + callbackOutput.push(0) + }) + res.write(' World', null, function () { + callbackOutput.push(1) + }) + res.end(null, null, function () { + callbackOutput.push(2) + }) + }) + + request(server) + .get('/') + .set('Accept-Encoding', 'gzip') + .expect('Content-Encoding', 'gzip') + .end(function (err) { + if (err) { + throw new Error(err) + } + assert.equal(callbackOutput.length, 3) + assert.deepEqual(callbackOutput, [0, 1, 2]) + done() + }) + }) + + it('should call the passed callbacks in the order passed when not compressing', function (done) { + var callbackOutput = [] + var server = createServer(null, function (req, res) { + res.setHeader('Cache-Control', 'no-transform') + res.setHeader('Content-Type', 'text/plain') + res.write('hello,', null, function () { + callbackOutput.push(0) + }) + res.write(' world', null, function () { + callbackOutput.push(1) + }) + res.end(null, null, function () { + callbackOutput.push(2) + }) + }) + + request(server) + .get('/') + .set('Accept-Encoding', 'gzip') + .expect('Cache-Control', 'no-transform') + .expect(shouldNotHaveHeader('Content-Encoding')) + .end(function (err) { + if (err) { + throw new Error(err) + } + assert.equal(callbackOutput.length, 3) + assert.deepEqual(callbackOutput, [0, 1, 2]) + done() + }) + }) + }) }) function createServer (opts, fn) {