Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ccb9ec1
build: [email protected]
dougwilson Mar 30, 2016
99f399b
build: [email protected]
dougwilson Mar 30, 2016
79d8564
build: cache node_modules on Travis CI
dougwilson Mar 30, 2016
15f9e42
build: [email protected]
dougwilson Mar 30, 2016
de2a448
deps: accepts@~1.3.2
dougwilson Mar 30, 2016
13240a4
deps: [email protected]
dougwilson Mar 30, 2016
0220f67
build: [email protected]
dougwilson May 12, 2016
81abf6b
docs: fix unreleased history order
dougwilson May 12, 2016
e75248c
deps: compressible@~2.0.8
dougwilson May 12, 2016
52ff252
lint: use standard style
dougwilson May 12, 2016
133e8c4
Fix jsdoc comment for options parameter
May 11, 2016
60390f6
build: [email protected]
dougwilson May 12, 2016
7dd87b6
build: support Node.js 6.x
dougwilson May 12, 2016
f504603
deps: accepts@~1.3.3
dougwilson May 12, 2016
b9c63ce
1.6.2
dougwilson May 12, 2016
ed51c3a
Update index.js
May 8, 2016
f9c95f9
Update index.js
May 8, 2016
c4ba4af
Adding tests.
jpodwys May 23, 2016
bd60b96
Updating docs.
jpodwys May 23, 2016
5cd1fe2
no message
jpodwys May 23, 2016
2b85837
no message
jpodwys May 23, 2016
31792c1
Testing with and without compression.
jpodwys May 23, 2016
bda469c
Reverting readme change and ensuring callbacks are called in the orde…
jpodwys May 23, 2016
5f122e8
Adding error handling.
jpodwys May 23, 2016
eb1bad8
Fixing lint errors.
jpodwys May 23, 2016
ac8914b
Adding test with no streaming.
jpodwys May 24, 2016
866775d
no message
jpodwys May 24, 2016
c1f3aab
Attempting to handle pre-11 node versions.
jpodwys May 24, 2016
b0055d2
no message
jpodwys May 24, 2016
3ab2f0d
Handling node 0.8 and 0.10.
jpodwys May 24, 2016
79c6b75
Typo.
jpodwys May 24, 2016
0f617c7
Checking OutgoingMessage.prototype.
jpodwys May 24, 2016
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
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
15 changes: 12 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ node_js:
- "1.8"
- "2.5"
- "3.3"
- "4.2"
- "5.4"
- "4.4"
- "5.11"
- "6.1"
sudo: false
cache:
directories:
- node_modules
before_install:
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard istanbul"

# Update Node.js modules
- "test ! -d node_modules || npm prune"
- "test ! -d node_modules || npm rebuild"
script:
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
13 changes: 13 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
1.6.2 / 2016-05-12
==================

* deps: accepts@~1.3.3
- deps: mime-types@~2.1.11
- deps: [email protected]
* deps: [email protected]
- Drop partial bytes on all parsed units
- Fix parsing byte string that looks like hex
- perf: hoist regular expressions
* deps: compressible@~2.0.8
- deps: mime-db@'>= 1.23.0 < 2'

1.6.1 / 2016-01-19
==================

Expand Down
94 changes: 51 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -39,12 +41,12 @@ var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
/**
* Compress response data with gzip / deflate.
*
* @param {Object} options
* @param {Object} [options]
* @return {Function} middleware
* @public
*/

function compression(options) {
function compression (options) {
var opts = options || {}

// options
Expand All @@ -55,43 +57,49 @@ function compression(options) {
threshold = 1024
}

return function compression(req, res, next){
return function compression (req, res, next) {
var ended = false
var length
var listeners = []
var write = res.write
var on = res.on
var end = res.end
var stream
var noop = function () {}

res._end = res.end
res._write = res.write
var _on = res.on

// flush
res.flush = function flush() {
res.flush = function flush () {
if (stream) {
stream.flush()
}
}

// proxy

res.write = function(chunk, encoding){
res.write = function write (chunk, encoding, cb) {
if (ended) {
return false
}

cb = hasCallback ? cb : null

if (!this._header) {
this._implicitHeader()
}

return stream
? stream.write(new Buffer(chunk, encoding))
: write.call(this, chunk, encoding)
};
? stream.write(new Buffer(chunk, encoding), cb)
: res._write.call(this, chunk, encoding, cb)
}

res.end = function(chunk, encoding){
res.end = function end (chunk, encoding, cb) {
if (ended) {
return false
}

cb = hasCallback ? cb : null

if (!this._header) {
// estimate the length
if (!this.getHeader('Content-Length')) {
Expand All @@ -102,21 +110,21 @@ function compression(options) {
}

if (!stream) {
return end.call(this, chunk, encoding)
return res._end.call(this, chunk, encoding, cb)
}

// mark ended
ended = true

// write Buffer for Node.js 0.8
return chunk
? stream.end(new Buffer(chunk, encoding))
: stream.end()
};
? stream.end(new Buffer(chunk, encoding), null, cb)
: stream.end(null, null, cb)
}

res.on = function(type, listener){
res.on = function on (type, listener) {
if (!listeners || type !== 'drain') {
return on.call(this, type, listener)
return _on.call(this, type, listener)
}

if (stream) {
Expand All @@ -129,13 +137,13 @@ function compression(options) {
return this
}

function nocompress(msg) {
function nocompress (msg) {
debug('no compression: %s', msg)
addListeners(res, on, listeners)
addListeners(res, _on, listeners)
listeners = null
}

onHeaders(res, function(){
onHeaders(res, function onResponseHeaders () {
// determine if request is filtered
if (!filter(req, res)) {
nocompress('filtered')
Expand All @@ -157,16 +165,16 @@ function compression(options) {
return
}

var encoding = res.getHeader('Content-Encoding') || 'identity';
var encoding = res.getHeader('Content-Encoding') || 'identity'

// already encoded
if ('identity' !== encoding) {
if (encoding !== 'identity') {
nocompress('already encoded')
return
}

// head
if ('HEAD' === req.method) {
if (req.method === 'HEAD') {
nocompress('HEAD request')
return
}
Expand Down Expand Up @@ -196,35 +204,35 @@ function compression(options) {
addListeners(stream, stream.on, listeners)

// header fields
res.setHeader('Content-Encoding', method);
res.removeHeader('Content-Length');
res.setHeader('Content-Encoding', method)
res.removeHeader('Content-Length')

// compression
stream.on('data', function(chunk){
if (write.call(res, chunk) === false) {
stream.on('data', function onStreamData (chunk) {
if (res._write.call(res, chunk) === false) {
stream.pause()
}
});
})

stream.on('end', function(){
end.call(res);
});
stream.on('end', function onStreamEnd () {
res._end.call(res)
})

on.call(res, 'drain', function() {
_on.call(res, 'drain', function onResponseDrain () {
stream.resume()
});
});
})
})

next();
};
next()
}
}

/**
* Add bufferred listeners to stream
* @private
*/

function addListeners(stream, on, listeners) {
function addListeners (stream, on, listeners) {
for (var i = 0; i < listeners.length; i++) {
on.apply(stream, listeners[i])
}
Expand All @@ -234,7 +242,7 @@ function addListeners(stream, on, listeners) {
* Get the length of a given chunk
*/

function chunkLength(chunk, encoding) {
function chunkLength (chunk, encoding) {
if (!chunk) {
return 0
}
Expand All @@ -249,7 +257,7 @@ function chunkLength(chunk, encoding) {
* @private
*/

function shouldCompress(req, res) {
function shouldCompress (req, res) {
var type = res.getHeader('Content-Type')

if (type === undefined || !compressible(type)) {
Expand All @@ -265,11 +273,11 @@ function shouldCompress(req, res) {
* @private
*/

function shouldTransform(req, res) {
function shouldTransform (req, res) {
var cacheControl = res.getHeader('Cache-Control')

// 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)
}
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
{
"name": "compression",
"description": "Node.js compression middleware",
"version": "1.6.1",
"version": "1.6.2",
"contributors": [
"Douglas Christopher Wilson <[email protected]>",
"Jonathan Ong <[email protected]> (http://jongleberry.com)"
],
"license": "MIT",
"repository": "expressjs/compression",
"dependencies": {
"accepts": "~1.3.1",
"bytes": "2.2.0",
"compressible": "~2.0.7",
"accepts": "~1.3.3",
"bytes": "2.3.0",
"compressible": "~2.0.8",
"debug": "~2.2.0",
"on-headers": "~1.0.1",
"vary": "~1.1.0"
},
"devDependencies": {
"istanbul": "0.4.2",
"mocha": "2.3.4",
"eslint": "2.9.0",
"eslint-config-standard": "5.3.1",
"eslint-plugin-promise": "1.1.0",
"eslint-plugin-standard": "1.3.2",
"istanbul": "0.4.3",
"mocha": "2.4.5",
"supertest": "1.1.0"
},
"files": [
Expand All @@ -30,6 +34,7 @@
"node": ">= 0.8.0"
},
"scripts": {
"lint": "eslint **/*.js",
"test": "mocha --check-leaks --reporter spec --bail",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --check-leaks --reporter spec"
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}
Loading