diff --git a/index.js b/index.js index 558c449..973b7cc 100644 --- a/index.js +++ b/index.js @@ -84,9 +84,7 @@ function compression (options) { this.writeHead(this.statusCode) } - return stream - ? stream.write(toBuffer(chunk, encoding)) - : _write.call(this, chunk, encoding) + return stream ? stream.write(toBuffer(chunk, encoding)) : _write.call(this, chunk, encoding) } res.end = function end (chunk, encoding) { @@ -111,9 +109,7 @@ function compression (options) { ended = true // write Buffer for Node.js 0.8 - return chunk - ? stream.end(toBuffer(chunk, encoding)) - : stream.end() + return chunk ? stream.end(toBuffer(chunk, encoding)) : stream.end() } res.on = function on (type, listener) { @@ -190,9 +186,7 @@ function compression (options) { // compression stream debug('%s compression', method) - stream = method === 'gzip' - ? zlib.createGzip(opts) - : zlib.createDeflate(opts) + stream = method === 'gzip' ? zlib.createGzip(opts) : zlib.createDeflate(opts) // add buffered listeners to stream addListeners(stream, stream.on, listeners) @@ -241,9 +235,7 @@ function chunkLength (chunk, encoding) { return 0 } - return !Buffer.isBuffer(chunk) - ? Buffer.byteLength(chunk, encoding) - : chunk.length + return !Buffer.isBuffer(chunk) ? Buffer.byteLength(chunk, encoding) : chunk.length } /** @@ -272,8 +264,7 @@ function shouldTransform (req, res) { // Don't compress for Cache-Control: no-transform // https://tools.ietf.org/html/rfc7234#section-5.2.2.4 - return !cacheControl || - !cacheControlNoTransformRegExp.test(cacheControl) + return !cacheControl || !cacheControlNoTransformRegExp.test(cacheControl) } /** @@ -282,7 +273,5 @@ function shouldTransform (req, res) { */ function toBuffer (chunk, encoding) { - return !Buffer.isBuffer(chunk) - ? Buffer.from(chunk, encoding) - : chunk + return !Buffer.isBuffer(chunk) ? Buffer.from(chunk, encoding) : chunk } diff --git a/test/compression.js b/test/compression.js index 554477e..6dae3b7 100644 --- a/test/compression.js +++ b/test/compression.js @@ -26,11 +26,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .head('/') - .set('Accept-Encoding', 'gzip') - .expect(shouldNotHaveHeader('Content-Encoding')) - .expect(200, done) + request(server).head('/').set('Accept-Encoding', 'gzip').expect(shouldNotHaveHeader('Content-Encoding')).expect(200, done) }) it('should skip unknown accept-encoding', function (done) { @@ -39,11 +35,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'bogus') - .expect(shouldNotHaveHeader('Content-Encoding')) - .expect(200, done) + request(server).get('/').set('Accept-Encoding', 'bogus').expect(shouldNotHaveHeader('Content-Encoding')).expect(200, done) }) it('should skip if content-encoding already set', function (done) { @@ -53,11 +45,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Content-Encoding', 'x-custom') - .expect(200, 'hello, world', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Content-Encoding', 'x-custom').expect(200, 'hello, world', done) }) it('should set Vary', function (done) { @@ -66,11 +54,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Content-Encoding', 'gzip') - .expect('Vary', 'Accept-Encoding', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Content-Encoding', 'gzip').expect('Vary', 'Accept-Encoding', done) }) it('should set Vary even if Accept-Encoding is not set', function (done) { @@ -79,11 +63,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .expect('Vary', 'Accept-Encoding') - .expect(shouldNotHaveHeader('Content-Encoding')) - .expect(200, done) + request(server).get('/').expect('Vary', 'Accept-Encoding').expect(shouldNotHaveHeader('Content-Encoding')).expect(200, done) }) it('should not set Vary if Content-Type does not pass filter', function (done) { @@ -92,10 +72,7 @@ describe('compression()', function () { res.end() }) - request(server) - .get('/') - .expect(shouldNotHaveHeader('Vary')) - .expect(200, done) + request(server).get('/').expect(shouldNotHaveHeader('Vary')).expect(200, done) }) it('should set Vary for HEAD request', function (done) { @@ -104,10 +81,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .head('/') - .set('Accept-Encoding', 'gzip') - .expect('Vary', 'Accept-Encoding', done) + request(server).head('/').set('Accept-Encoding', 'gzip').expect('Vary', 'Accept-Encoding', done) }) it('should transfer chunked', function (done) { @@ -116,10 +90,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Transfer-Encoding', 'chunked', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Transfer-Encoding', 'chunked', done) }) it('should remove Content-Length for chunked', function (done) { @@ -128,11 +99,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .expect('Content-Encoding', 'gzip') - .expect(shouldNotHaveHeader('Content-Length')) - .expect(200, done) + request(server).get('/').expect('Content-Encoding', 'gzip').expect(shouldNotHaveHeader('Content-Length')).expect(200, done) }) it('should work with encoding arguments', function (done) { @@ -142,11 +109,7 @@ describe('compression()', function () { res.end('world', 'utf8') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Transfer-Encoding', 'chunked') - .expect(200, 'hello, world', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Transfer-Encoding', 'chunked').expect(200, 'hello, world', done) }) it('should allow writing after close', function (done) { @@ -229,17 +192,24 @@ describe('compression()', function () { var client var drained = false var resp - var server = createServer({ filter: function () { return false } }, function (req, res) { - resp = res - - res.on('drain', function () { - drained = true - }) + var server = createServer( + { + filter: function () { + return false + } + }, + function (req, res) { + resp = res + + res.on('drain', function () { + drained = true + }) - res.setHeader('Content-Type', 'text/plain') - res.write('start') - pressure() - }) + res.setHeader('Content-Type', 'text/plain') + res.write('start') + pressure() + } + ) crypto.randomBytes(1024 * 128, function (err, chunk) { if (err) return done(err) @@ -329,12 +299,13 @@ describe('compression()', function () { request.on('response', function (headers) { assert.strictEqual(headers['content-encoding'], 'gzip') }) - var chunks = []; + var chunks = [] request.on('data', function (chunk) { - chunks.push(chunk); + chunks.push(chunk) }) request.on('end', function () { - zlib.gunzip(Buffer.concat(chunks), function(err, data) { + zlib.gunzip(Buffer.concat(chunks), function (err, data) { + if (err) return done(err) assert.strictEqual(data.toString(), 'hello, world') closeHttp2(client, server, done) }) @@ -352,11 +323,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect(shouldNotHaveHeader('Content-Encoding')) - .expect(200, done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect(shouldNotHaveHeader('Content-Encoding')).expect(200, done) }) it('should compress responses above the threshold size', function (done) { @@ -366,10 +333,7 @@ describe('compression()', function () { res.end(Buffer.alloc(2048)) }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Content-Encoding', 'gzip', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Content-Encoding', 'gzip', done) }) it('should compress when streaming without a content-length', function (done) { @@ -381,10 +345,7 @@ describe('compression()', function () { }, 10) }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Content-Encoding', 'gzip', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Content-Encoding', 'gzip', done) }) it('should not compress when streaming and content-length is lower than threshold', function (done) { @@ -397,11 +358,7 @@ describe('compression()', function () { }, 10) }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect(shouldNotHaveHeader('Content-Encoding')) - .expect(200, done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect(shouldNotHaveHeader('Content-Encoding')).expect(200, done) }) it('should compress when streaming and content-length is larger than threshold', function (done) { @@ -414,10 +371,7 @@ describe('compression()', function () { }, 10) }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Content-Encoding', 'gzip', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Content-Encoding', 'gzip', done) }) // res.end(str, encoding) broken in node.js 0.8 @@ -428,11 +382,7 @@ describe('compression()', function () { res.end('2e2e2e2e', 'hex') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect(shouldNotHaveHeader('Content-Encoding')) - .expect(200, '....', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect(shouldNotHaveHeader('Content-Encoding')).expect(200, '....', done) }) it('should consider res.end() as 0 length', function (done) { @@ -441,11 +391,7 @@ describe('compression()', function () { res.end() }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect(shouldNotHaveHeader('Content-Encoding')) - .expect(200, '', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect(shouldNotHaveHeader('Content-Encoding')).expect(200, '', done) }) it('should work with res.end(null)', function (done) { @@ -454,11 +400,7 @@ describe('compression()', function () { res.end(null) }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect(shouldNotHaveHeader('Content-Encoding')) - .expect(200, '', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect(shouldNotHaveHeader('Content-Encoding')).expect(200, '', done) }) }) @@ -469,10 +411,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Content-Encoding', 'gzip', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Content-Encoding', 'gzip', done) }) it('should return false writing after end', function (done) { @@ -483,10 +422,7 @@ describe('compression()', function () { assert.ok(res.end() === false) }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Content-Encoding', 'gzip', done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Content-Encoding', 'gzip', done) }) }) @@ -497,10 +433,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'deflate') - .expect('Content-Encoding', 'deflate', done) + request(server).get('/').set('Accept-Encoding', 'deflate').expect('Content-Encoding', 'deflate', done) }) }) @@ -511,10 +444,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip, deflate') - .expect('Content-Encoding', 'gzip', done) + request(server).get('/').set('Accept-Encoding', 'gzip, deflate').expect('Content-Encoding', 'gzip', done) }) }) @@ -525,10 +455,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'deflate, gzip') - .expect('Content-Encoding', 'gzip', done) + request(server).get('/').set('Accept-Encoding', 'deflate, gzip').expect('Content-Encoding', 'gzip', done) }) }) @@ -555,12 +482,7 @@ describe('compression()', function () { res.end('hello, world') }) - request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .expect('Cache-Control', 'no-transform') - .expect(shouldNotHaveHeader('Vary')) - .expect(200, done) + request(server).get('/').set('Accept-Encoding', 'gzip').expect('Cache-Control', 'no-transform').expect(shouldNotHaveHeader('Vary')).expect(200, done) }) }) @@ -574,9 +496,7 @@ describe('compression()', function () { res.end(String(compression.filter(req, res))) }) - request(server) - .get('/') - .expect(200, 'false', done) + request(server).get('/').expect(200, 'false', done) }) it('should return true for "text/plain"', function (done) { @@ -585,9 +505,7 @@ describe('compression()', function () { res.end(String(compression.filter(req, res))) }) - request(server) - .get('/') - .expect(200, 'true', done) + request(server).get('/').expect(200, 'true', done) }) it('should return false for "application/x-bogus"', function (done) { @@ -596,25 +514,19 @@ describe('compression()', function () { res.end(String(compression.filter(req, res))) }) - request(server) - .get('/') - .expect(200, 'false', done) + request(server).get('/').expect(200, 'false', done) }) }) describe('res.flush()', function () { it('should always be present', function (done) { var server = createServer(null, function (req, res) { - res.statusCode = typeof res.flush === 'function' - ? 200 - : 500 + res.statusCode = typeof res.flush === 'function' ? 200 : 500 res.flush() res.end() }) - request(server) - .get('/') - .expect(200, done) + request(server).get('/').expect(200, done) }) it('should flush the response', function (done) { @@ -637,10 +549,13 @@ describe('compression()', function () { .get('/') .set('Accept-Encoding', 'gzip') .request() - .on('response', unchunk('gzip', onchunk, function (err) { - if (err) return done(err) - server.close(done) - })) + .on( + 'response', + unchunk('gzip', onchunk, function (err) { + if (err) return done(err) + server.close(done) + }) + ) .end() }) @@ -663,10 +578,13 @@ describe('compression()', function () { .get('/') .set('Accept-Encoding', 'gzip') .request() - .on('response', unchunk('gzip', onchunk, function (err) { - if (err) return done(err) - server.close(done) - })) + .on( + 'response', + unchunk('gzip', onchunk, function (err) { + if (err) return done(err) + server.close(done) + }) + ) .end() }) @@ -689,10 +607,13 @@ describe('compression()', function () { .get('/') .set('Accept-Encoding', 'deflate') .request() - .on('response', unchunk('deflate', onchunk, function (err) { - if (err) return done(err) - server.close(done) - })) + .on( + 'response', + unchunk('deflate', onchunk, function (err) { + if (err) return done(err) + server.close(done) + }) + ) .end() }) }) @@ -737,20 +658,20 @@ function createHttp2Client (port) { function closeHttp2 (client, server, callback) { if (typeof client.shutdown === 'function') { // this is the node v8.x way of closing the connections - client.shutdown({}, function () { - server.close(function () { - callback() - }) + client.shutdown({}, function () { + server.close(function () { + callback() }) + }) } else { // this is the node v9.x onwards way of closing the connections - client.close(function () { - // force existing connections to time out after 1ms. - // this is done to force the server to close in some cases where it wouldn't do it otherwise. - server.close(function () { - callback() - }) + client.close(function () { + // force existing connections to time out after 1ms. + // this is done to force the server to close in some cases where it wouldn't do it otherwise. + server.close(function () { + callback() }) + }) } }