From ed51c3a16078719265efb7439e4e8cde93774cb0 Mon Sep 17 00:00:00 2001 From: Joe Podwys Date: Sat, 7 May 2016 18:52:20 -0600 Subject: [PATCH 01/20] Update index.js --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index c0e801e7..eed2a7fa 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,19 +84,19 @@ 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 } if (!this._header) { // estimate the length - if (!this.getHeader('Content-Length')) { - length = chunkLength(chunk, encoding) + if (!this.getHeader('Content-Length'), cb) { + length = chunkLength(chunk, encoding, cb) } this._implicitHeader() From f9c95f9821403ec734046bff7f4a14c25a64f12a Mon Sep 17 00:00:00 2001 From: Joe Podwys Date: Sat, 7 May 2016 18:53:51 -0600 Subject: [PATCH 02/20] Update index.js --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index eed2a7fa..552f602a 100644 --- a/index.js +++ b/index.js @@ -95,15 +95,15 @@ function compression (options) { if (!this._header) { // estimate the length - if (!this.getHeader('Content-Length'), cb) { - length = chunkLength(chunk, encoding, cb) + if (!this.getHeader('Content-Length')) { + length = chunkLength(chunk, encoding) } this._implicitHeader() } 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, cb) } res.on = function on (type, listener) { From c4ba4af8b2b3d51ef7fa749840f06c0369144568 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 15:12:07 -0600 Subject: [PATCH 03/20] Adding tests. --- test/compression.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/compression.js b/test/compression.js index 81c00149..f37dce4b 100644 --- a/test/compression.js +++ b/test/compression.js @@ -676,6 +676,35 @@ describe('compression()', function () { .end() }) }) + + describe('when callbacks are used', function () { + it('should call the passed callbacks', function(done){ + var callbacks = 0; + var server = createServer({ threshold: '1kb' }, function (req, res) { + res.setHeader('Content-Type', 'text/plain') + res.write('Hello', null, function(){ + callbacks++ + res.flush() + }); + res.write(' World', null, function(){ + callbacks++ + res.flush() + }); + res.end(null, null, function(){ + callbacks++ + }); + }) + + request(server) + .get('/') + .set('Accept-Encoding', 'gzip') + .expect('Content-Encoding', 'gzip') + .end(function(){ + assert.equal(callbacks, 3) + done(); + }); + }) + }) }) function createServer (opts, fn) { From bd60b964aca5d9d96253aaf2ff0df7d05f93f38a Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 15:15:44 -0600 Subject: [PATCH 04/20] Updating docs. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a5d599db..ccb0032a 100644 --- a/README.md +++ b/README.md @@ -205,10 +205,10 @@ app.get('/events', function (req, res) { // send a ping approx every 2 seconds var timer = setInterval(function () { - res.write('data: ping\n\n') - - // !!! this is the important part - res.flush() + res.write('data: ping\n\n', function(){ + // !!! this is the important part + res.flush() + }) }, 2000) res.on('close', function () { From 5cd1fe283c4fcfe93153385f1d7dc7d5ce712415 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 15:25:56 -0600 Subject: [PATCH 05/20] no message --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 552f602a..9c3f0ca0 100644 --- a/index.js +++ b/index.js @@ -74,7 +74,7 @@ function compression (options) { // proxy - res.write = function write (chunk, encoding, cb){ + res.write = function write (chunk, encoding, cb) { if (ended) { return false } @@ -88,7 +88,7 @@ function compression (options) { : _write.call(this, chunk, encoding, cb) }; - res.end = function end (chunk, encoding, cb){ + res.end = function end (chunk, encoding, cb) { if (ended) { return false } From 2b85837de0b8a54509b1356ab87c0beaa31fc228 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 15:26:42 -0600 Subject: [PATCH 06/20] no message --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9c3f0ca0..2ab7f7a0 100644 --- a/index.js +++ b/index.js @@ -86,7 +86,7 @@ function compression (options) { return stream ? stream.write(new Buffer(chunk, encoding), cb) : _write.call(this, chunk, encoding, cb) - }; + } res.end = function end (chunk, encoding, cb) { if (ended) { From 31792c184df0da6f43d93c4b33501f7a6aaf8c69 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 16:29:49 -0600 Subject: [PATCH 07/20] Testing with and without compression. --- test/compression.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/test/compression.js b/test/compression.js index f37dce4b..98809761 100644 --- a/test/compression.js +++ b/test/compression.js @@ -678,17 +678,40 @@ describe('compression()', function () { }) describe('when callbacks are used', function () { - it('should call the passed callbacks', function(done){ + it('should call the passed callbacks while compressing', function(done){ var callbacks = 0; - var server = createServer({ threshold: '1kb' }, function (req, res) { + var server = createServer({ threshold: 0 }, function (req, res) { + res.setHeader('Content-Type', 'text/plain') + res.write('Hello', null, function(){ + callbacks++ + }); + res.write(' World', null, function(){ + callbacks++ + }); + res.end(null, null, function(){ + callbacks++ + }); + }) + + request(server) + .get('/') + .set('Accept-Encoding', 'gzip') + .expect('Content-Encoding', 'gzip') + .end(function(){ + assert.equal(callbacks, 3) + done(); + }); + }) + + it('should call the passed callbacks while not compressing', function(done){ + var callbacks = 0; + var server = createServer({ threshold: '10kb' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.write('Hello', null, function(){ callbacks++ - res.flush() }); res.write(' World', null, function(){ callbacks++ - res.flush() }); res.end(null, null, function(){ callbacks++ From bda469c06073313ced4182bf6f9f7723ef50ef95 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 17:00:22 -0600 Subject: [PATCH 08/20] Reverting readme change and ensuring callbacks are called in the order they are coded. --- README.md | 8 ++++---- test/compression.js | 44 ++++++++++---------------------------------- 2 files changed, 14 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index ccb0032a..a5d599db 100644 --- a/README.md +++ b/README.md @@ -205,10 +205,10 @@ app.get('/events', function (req, res) { // send a ping approx every 2 seconds var timer = setInterval(function () { - res.write('data: ping\n\n', function(){ - // !!! this is the important part - res.flush() - }) + res.write('data: ping\n\n') + + // !!! this is the important part + res.flush() }, 2000) res.on('close', function () { diff --git a/test/compression.js b/test/compression.js index 98809761..58566eb6 100644 --- a/test/compression.js +++ b/test/compression.js @@ -678,18 +678,18 @@ describe('compression()', function () { }) describe('when callbacks are used', function () { - it('should call the passed callbacks while compressing', function(done){ - var callbacks = 0; + it('should call the passed callbacks in the order passed', function(done){ + var callbackOutput = []; var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') - res.write('Hello', null, function(){ - callbacks++ + res.write('Hello', null, function (err){ + callbackOutput.push(0); }); - res.write(' World', null, function(){ - callbacks++ + res.write(' World', null, function (err){ + callbackOutput.push(1); }); - res.end(null, null, function(){ - callbacks++ + res.end(null, null, function (err){ + callbackOutput.push(2); }); }) @@ -698,32 +698,8 @@ describe('compression()', function () { .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'gzip') .end(function(){ - assert.equal(callbacks, 3) - done(); - }); - }) - - it('should call the passed callbacks while not compressing', function(done){ - var callbacks = 0; - var server = createServer({ threshold: '10kb' }, function (req, res) { - res.setHeader('Content-Type', 'text/plain') - res.write('Hello', null, function(){ - callbacks++ - }); - res.write(' World', null, function(){ - callbacks++ - }); - res.end(null, null, function(){ - callbacks++ - }); - }) - - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Content-Encoding', 'gzip') - .end(function(){ - assert.equal(callbacks, 3) + assert.equal(callbackOutput.length, 3) + assert.deepEqual(callbackOutput, [0, 1, 2]) done(); }); }) From 5f122e82e1f7ad0306023ee58f361bb9ad8ed910 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 17:08:58 -0600 Subject: [PATCH 09/20] Adding error handling. --- test/compression.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/compression.js b/test/compression.js index 58566eb6..ffbfb66f 100644 --- a/test/compression.js +++ b/test/compression.js @@ -682,13 +682,13 @@ describe('compression()', function () { var callbackOutput = []; var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') - res.write('Hello', null, function (err){ + res.write('Hello', null, function () { callbackOutput.push(0); }); - res.write(' World', null, function (err){ + res.write(' World', null, function () { callbackOutput.push(1); }); - res.end(null, null, function (err){ + res.end(null, null, function () { callbackOutput.push(2); }); }) @@ -697,7 +697,10 @@ describe('compression()', function () { .get('/') .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'gzip') - .end(function(){ + .end(function (err) { + if (err) { + throw new Error(err); + } assert.equal(callbackOutput.length, 3) assert.deepEqual(callbackOutput, [0, 1, 2]) done(); From eb1bad8768bd39ba6499254703028be8b6311c87 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 17:13:33 -0600 Subject: [PATCH 10/20] Fixing lint errors. --- test/compression.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/compression.js b/test/compression.js index ffbfb66f..a2b52ef6 100644 --- a/test/compression.js +++ b/test/compression.js @@ -678,19 +678,19 @@ describe('compression()', function () { }) describe('when callbacks are used', function () { - it('should call the passed callbacks in the order passed', function(done){ - var callbackOutput = []; + it('should call the passed callbacks in the order passed', function (done) { + var callbackOutput = [] var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.write('Hello', null, function () { - callbackOutput.push(0); - }); + callbackOutput.push(0) + }) res.write(' World', null, function () { - callbackOutput.push(1); - }); + callbackOutput.push(1) + }) res.end(null, null, function () { - callbackOutput.push(2); - }); + callbackOutput.push(2) + }) }) request(server) @@ -699,12 +699,12 @@ describe('compression()', function () { .expect('Content-Encoding', 'gzip') .end(function (err) { if (err) { - throw new Error(err); + throw new Error(err) } assert.equal(callbackOutput.length, 3) assert.deepEqual(callbackOutput, [0, 1, 2]) - done(); - }); + done() + }) }) }) }) From ac8914b1589d2c6c91417e870bdbeec3a3fb5c1b Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 23:24:02 -0600 Subject: [PATCH 11/20] Adding test with no streaming. --- test/compression.js | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/test/compression.js b/test/compression.js index a2b52ef6..04106cdf 100644 --- a/test/compression.js +++ b/test/compression.js @@ -678,9 +678,9 @@ describe('compression()', function () { }) describe('when callbacks are used', function () { - it('should call the passed callbacks in the order passed', function (done) { + it('should call the passed callbacks in the order passed when compressing', function (done) { var callbackOutput = [] - var server = createServer({ threshold: 0 }, function (req, res) { + var server = createServer(null, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.write('Hello', null, function () { callbackOutput.push(0) @@ -707,6 +707,39 @@ describe('compression()', function () { }) }) }) + + describe('when callbacks are used', function () { + 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) { From 866775db3d4fb31d3582dae5dfeff78542d7408a Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Mon, 23 May 2016 23:30:06 -0600 Subject: [PATCH 12/20] no message --- test/compression.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/compression.js b/test/compression.js index 04106cdf..181de7ef 100644 --- a/test/compression.js +++ b/test/compression.js @@ -706,9 +706,7 @@ describe('compression()', function () { done() }) }) - }) - describe('when callbacks are used', function () { it('should call the passed callbacks in the order passed when not compressing', function (done) { var callbackOutput = [] var server = createServer(null, function (req, res) { From c1f3aab8b4c095451b7d2296d715f79ed44469ec Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Tue, 24 May 2016 00:18:17 -0600 Subject: [PATCH 13/20] Attempting to handle pre-11 node versions. --- index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.js b/index.js index 2ab7f7a0..eeb8a13e 100644 --- a/index.js +++ b/index.js @@ -60,6 +60,7 @@ function compression (options) { var length var listeners = [] var stream + var noop = function () {} var _end = res.end var _on = res.on @@ -79,6 +80,8 @@ function compression (options) { return false } + cb = (res.write.length === 3) ? cb : noop + if (!this._header) { this._implicitHeader() } @@ -93,6 +96,8 @@ function compression (options) { return false } + cb = (res.end.length === 3) ? cb : noop + if (!this._header) { // estimate the length if (!this.getHeader('Content-Length')) { From b0055d2c7a8cf1c242786b440f88703a35a2bb75 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Tue, 24 May 2016 00:25:26 -0600 Subject: [PATCH 14/20] no message --- test/compression.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/test/compression.js b/test/compression.js index 181de7ef..172abe69 100644 --- a/test/compression.js +++ b/test/compression.js @@ -679,8 +679,10 @@ describe('compression()', function () { describe('when callbacks are used', function () { it('should call the passed callbacks in the order passed when compressing', function (done) { + var hasCallbacks = false var callbackOutput = [] var server = createServer(null, function (req, res) { + hasCallbacks = (res.write.length === 3 && res.end.length === 3) res.setHeader('Content-Type', 'text/plain') res.write('Hello', null, function () { callbackOutput.push(0) @@ -701,15 +703,19 @@ describe('compression()', function () { if (err) { throw new Error(err) } - assert.equal(callbackOutput.length, 3) - assert.deepEqual(callbackOutput, [0, 1, 2]) + if (hasCallbacks) { + 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 hasCallbacks = false var callbackOutput = [] var server = createServer(null, function (req, res) { + hasCallbacks = (res.write.length === 3 && res.end.length === 3) res.setHeader('Cache-Control', 'no-transform') res.setHeader('Content-Type', 'text/plain') res.write('hello,', null, function () { @@ -732,8 +738,10 @@ describe('compression()', function () { if (err) { throw new Error(err) } - assert.equal(callbackOutput.length, 3) - assert.deepEqual(callbackOutput, [0, 1, 2]) + if (hasCallbacks) { + assert.equal(callbackOutput.length, 3) + assert.deepEqual(callbackOutput, [0, 1, 2]) + } done() }) }) From 3ab2f0d7b3275dd1b41826dc6b6be34e2afc74d8 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Tue, 24 May 2016 01:15:14 -0600 Subject: [PATCH 15/20] Handling node 0.8 and 0.10. --- index.js | 20 ++++++++++---------- test/compression.js | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index eeb8a13e..0439f691 100644 --- a/index.js +++ b/index.js @@ -62,9 +62,9 @@ function compression (options) { var stream var noop = function () {} - var _end = res.end + res._end = res.end + res._write = res.write var _on = res.on - var _write = res.write // flush res.flush = function flush () { @@ -80,7 +80,7 @@ function compression (options) { return false } - cb = (res.write.length === 3) ? cb : noop + cb = (res._end.length === 3) ? cb : null if (!this._header) { this._implicitHeader() @@ -88,7 +88,7 @@ function compression (options) { return stream ? stream.write(new Buffer(chunk, encoding), cb) - : _write.call(this, chunk, encoding, cb) + : res._write.call(this, chunk, encoding, cb) } res.end = function end (chunk, encoding, cb) { @@ -96,7 +96,7 @@ function compression (options) { return false } - cb = (res.end.length === 3) ? cb : noop + cb = (res._end.length === 3) ? cb : null if (!this._header) { // estimate the length @@ -108,7 +108,7 @@ function compression (options) { } if (!stream) { - return _end.call(this, chunk, encoding, cb) + return res._end.call(this, chunk, encoding, cb) } // mark ended @@ -116,8 +116,8 @@ function compression (options) { // write Buffer for Node.js 0.8 return chunk - ? stream.end(new Buffer(chunk, encoding), cb) - : stream.end(null, cb) + ? stream.end(new Buffer(chunk, encoding), null, cb) + : stream.end(null, null, cb) } res.on = function on (type, listener) { @@ -207,13 +207,13 @@ function compression (options) { // compression stream.on('data', function onStreamData (chunk) { - if (_write.call(res, chunk) === false) { + if (res._write.call(res, chunk) === false) { stream.pause() } }) stream.on('end', function onStreamEnd () { - _end.call(res) + res._end.call(res) }) _on.call(res, 'drain', function onResponseDrain () { diff --git a/test/compression.js b/test/compression.js index 172abe69..20251911 100644 --- a/test/compression.js +++ b/test/compression.js @@ -682,7 +682,7 @@ describe('compression()', function () { var hasCallbacks = false var callbackOutput = [] var server = createServer(null, function (req, res) { - hasCallbacks = (res.write.length === 3 && res.end.length === 3) + hasCallbacks = (res._write.length === 3 && res._end.length === 3) res.setHeader('Content-Type', 'text/plain') res.write('Hello', null, function () { callbackOutput.push(0) @@ -715,7 +715,7 @@ describe('compression()', function () { var hasCallbacks = false var callbackOutput = [] var server = createServer(null, function (req, res) { - hasCallbacks = (res.write.length === 3 && res.end.length === 3) + hasCallbacks = (res._write.length === 3 && res._end.length === 3) res.setHeader('Cache-Control', 'no-transform') res.setHeader('Content-Type', 'text/plain') res.write('hello,', null, function () { From 79c6b75b3a28aa7dacf0c3e555a8392a203f59d8 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Tue, 24 May 2016 02:06:47 -0600 Subject: [PATCH 16/20] Typo. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 0439f691..dbab5d50 100644 --- a/index.js +++ b/index.js @@ -80,7 +80,7 @@ function compression (options) { return false } - cb = (res._end.length === 3) ? cb : null + cb = (res._write.length === 3) ? cb : null if (!this._header) { this._implicitHeader() From 0f617c7f15a1826adf3d8782b282facd6cac4642 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Tue, 24 May 2016 14:05:48 -0600 Subject: [PATCH 17/20] Checking OutgoingMessage.prototype. --- index.js | 6 ++++-- test/compression.js | 6 ++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index dbab5d50..e6c494ce 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,8 @@ var debug = require('debug')('compression') var onHeaders = require('on-headers') var vary = require('vary') var zlib = require('zlib') +var OutgoingMessage = require('http').OutgoingMessage +var hasCallback = (OutgoingMessage.prototype.write.length === 3) /** * Module exports. @@ -80,7 +82,7 @@ function compression (options) { return false } - cb = (res._write.length === 3) ? cb : null + cb = hasCallback ? cb : null if (!this._header) { this._implicitHeader() @@ -96,7 +98,7 @@ function compression (options) { return false } - cb = (res._end.length === 3) ? cb : null + cb = hasCallback ? cb : null if (!this._header) { // estimate the length diff --git a/test/compression.js b/test/compression.js index 20251911..06e31c66 100644 --- a/test/compression.js +++ b/test/compression.js @@ -3,6 +3,8 @@ var bytes = require('bytes') var crypto = require('crypto') var http = require('http') var request = require('supertest') +var OutgoingMessage = http.OutgoingMessage +var hasCallbacks = (OutgoingMessage.prototype.write.length === 3) var compression = require('..') @@ -679,10 +681,8 @@ describe('compression()', function () { describe('when callbacks are used', function () { it('should call the passed callbacks in the order passed when compressing', function (done) { - var hasCallbacks = false var callbackOutput = [] var server = createServer(null, function (req, res) { - hasCallbacks = (res._write.length === 3 && res._end.length === 3) res.setHeader('Content-Type', 'text/plain') res.write('Hello', null, function () { callbackOutput.push(0) @@ -712,10 +712,8 @@ describe('compression()', function () { }) it('should call the passed callbacks in the order passed when not compressing', function (done) { - var hasCallbacks = false var callbackOutput = [] var server = createServer(null, function (req, res) { - hasCallbacks = (res._write.length === 3 && res._end.length === 3) res.setHeader('Cache-Control', 'no-transform') res.setHeader('Content-Type', 'text/plain') res.write('hello,', null, function () { From 4b50f90e1413f1fc683a59fa353a030017d11708 Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Fri, 27 May 2016 20:19:42 -0600 Subject: [PATCH 18/20] Making res.flush synchronous. --- index.js | 38 +++++++++++++------------- test/compression.js | 66 --------------------------------------------- 2 files changed, 18 insertions(+), 86 deletions(-) diff --git a/index.js b/index.js index e6c494ce..07d30d08 100644 --- a/index.js +++ b/index.js @@ -21,8 +21,6 @@ var debug = require('debug')('compression') var onHeaders = require('on-headers') var vary = require('vary') var zlib = require('zlib') -var OutgoingMessage = require('http').OutgoingMessage -var hasCallback = (OutgoingMessage.prototype.write.length === 3) /** * Module exports. @@ -62,44 +60,44 @@ function compression (options) { var length var listeners = [] var stream - var noop = function () {} - res._end = res.end - res._write = res.write + var _end = res.end var _on = res.on + var _write = res.write + + var flushCB = function flushCB () { + stream.flush() + } // flush res.flush = function flush () { if (stream) { - stream.flush() + // call write and pass flushCB to force synchronous behavior + stream.write('', flushCB) } } // proxy - res.write = function write (chunk, encoding, cb) { + res.write = function write (chunk, encoding) { if (ended) { return false } - cb = hasCallback ? cb : null - if (!this._header) { this._implicitHeader() } return stream - ? stream.write(new Buffer(chunk, encoding), cb) - : res._write.call(this, chunk, encoding, cb) + ? stream.write(new Buffer(chunk, encoding)) + : _write.call(this, chunk, encoding) } - res.end = function end (chunk, encoding, cb) { + res.end = function end (chunk, encoding) { if (ended) { return false } - cb = hasCallback ? cb : null - if (!this._header) { // estimate the length if (!this.getHeader('Content-Length')) { @@ -110,7 +108,7 @@ function compression (options) { } if (!stream) { - return res._end.call(this, chunk, encoding, cb) + return _end.call(this, chunk, encoding) } // mark ended @@ -118,8 +116,8 @@ function compression (options) { // write Buffer for Node.js 0.8 return chunk - ? stream.end(new Buffer(chunk, encoding), null, cb) - : stream.end(null, null, cb) + ? stream.end(new Buffer(chunk, encoding)) + : stream.end() } res.on = function on (type, listener) { @@ -209,13 +207,13 @@ function compression (options) { // compression stream.on('data', function onStreamData (chunk) { - if (res._write.call(res, chunk) === false) { + if (_write.call(res, chunk) === false) { stream.pause() } }) stream.on('end', function onStreamEnd () { - res._end.call(res) + _end.call(res) }) _on.call(res, 'drain', function onResponseDrain () { @@ -280,4 +278,4 @@ function shouldTransform (req, res) { // https://tools.ietf.org/html/rfc7234#section-5.2.2.4 return !cacheControl || !cacheControlNoTransformRegExp.test(cacheControl) -} +} \ No newline at end of file diff --git a/test/compression.js b/test/compression.js index 06e31c66..a49006f8 100644 --- a/test/compression.js +++ b/test/compression.js @@ -678,72 +678,6 @@ 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) - } - if (hasCallbacks) { - 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) - } - if (hasCallbacks) { - assert.equal(callbackOutput.length, 3) - assert.deepEqual(callbackOutput, [0, 1, 2]) - } - done() - }) - }) - }) }) function createServer (opts, fn) { From 2e995d03c0f4b12c6de3a0453f68bba611f75a7c Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Fri, 27 May 2016 20:20:32 -0600 Subject: [PATCH 19/20] no message --- test/compression.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/compression.js b/test/compression.js index a49006f8..81c00149 100644 --- a/test/compression.js +++ b/test/compression.js @@ -3,8 +3,6 @@ var bytes = require('bytes') var crypto = require('crypto') var http = require('http') var request = require('supertest') -var OutgoingMessage = http.OutgoingMessage -var hasCallbacks = (OutgoingMessage.prototype.write.length === 3) var compression = require('..') From 74fdf7bd2dbb508c4ef15cd3b63dd4049e05a0ab Mon Sep 17 00:00:00 2001 From: Joseph Podwys Date: Fri, 27 May 2016 20:21:07 -0600 Subject: [PATCH 20/20] no message --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 07d30d08..5d9b0df5 100644 --- a/index.js +++ b/index.js @@ -278,4 +278,4 @@ function shouldTransform (req, res) { // https://tools.ietf.org/html/rfc7234#section-5.2.2.4 return !cacheControl || !cacheControlNoTransformRegExp.test(cacheControl) -} \ No newline at end of file +}