Skip to content

Commit 75b1532

Browse files
committed
Add debug messages
1 parent 252a2e6 commit 75b1532

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Add `debug` messages
45
* deps: accepts@~1.0.7
56
67

index.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var zlib = require('zlib');
1313
var accepts = require('accepts');
1414
var bytes = require('bytes');
15+
var debug = require('debug')('compression')
1516
var onHeaders = require('on-headers');
1617
var compressible = require('compressible');
1718
var vary = require('vary');
@@ -29,8 +30,15 @@ exports.methods = {
2930
* Default filter function.
3031
*/
3132

32-
exports.filter = function(req, res){
33-
return compressible(res.getHeader('Content-Type'));
33+
exports.filter = function filter(req, res) {
34+
var type = res.getHeader('Content-Type')
35+
36+
if (type === undefined || !compressible(type)) {
37+
debug('%s not compressible', type)
38+
return false
39+
}
40+
41+
return true
3442
};
3543

3644
/**
@@ -78,8 +86,8 @@ module.exports = function compression(options) {
7886
if (!this._header) {
7987
// if content-length is set and is lower
8088
// than the threshold, don't compress
81-
var length = res.getHeader('content-length');
82-
if (!isNaN(length) && length < threshold) compress = false;
89+
var len = Number(res.getHeader('Content-Length'))
90+
checkthreshold(len)
8391
this._implicitHeader();
8492
}
8593
return stream
@@ -97,7 +105,7 @@ module.exports = function compression(options) {
97105
}
98106

99107
if (!this._header) {
100-
compress = len && len >= threshold
108+
checkthreshold(len)
101109
}
102110

103111
if (chunk) {
@@ -124,36 +132,60 @@ module.exports = function compression(options) {
124132
return this
125133
}
126134

127-
function nocompress(){
135+
function checkthreshold(len) {
136+
if (compress && len < threshold) {
137+
debug('size below threshold')
138+
compress = false
139+
}
140+
}
141+
142+
function nocompress(msg) {
143+
debug('no compression' + (msg ? ': ' + msg : ''))
128144
addListeners(res, on, listeners)
129145
listeners = null
130146
}
131147

132148
onHeaders(res, function(){
133-
// default request filter
134-
if (!filter(req, res)) return nocompress()
149+
// determine if request is filtered
150+
if (!filter(req, res)) {
151+
nocompress('filtered')
152+
return
153+
}
135154

136155
// vary
137156
vary(res, 'Accept-Encoding')
138157

139-
if (!compress) return nocompress()
158+
if (!compress) {
159+
nocompress()
160+
return
161+
}
140162

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

143165
// already encoded
144-
if ('identity' !== encoding) return nocompress()
166+
if ('identity' !== encoding) {
167+
nocompress('already encoded')
168+
return
169+
}
145170

146171
// head
147-
if ('HEAD' === req.method) return nocompress()
172+
if ('HEAD' === req.method) {
173+
nocompress('HEAD request')
174+
return
175+
}
148176

149177
// compression method
150178
var accept = accepts(req);
151179
var method = accept.encodings(['gzip', 'deflate', 'identity']);
152180

153181
// negotiation failed
154-
if (!method || method === 'identity') return nocompress()
182+
if (!method || method === 'identity') {
183+
nocompress('not acceptable')
184+
return
185+
}
155186

156187
// compression stream
188+
debug('%s compression', method)
157189
stream = exports.methods[method](options);
158190
addListeners(stream, stream.on, listeners)
159191

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"accepts": "~1.0.7",
1313
"bytes": "1.0.0",
1414
"compressible": "1.1.0",
15+
"debug": "1.0.4",
1516
"on-headers": "0.0.0",
1617
"vary": "0.1.0"
1718
},
@@ -25,7 +26,7 @@
2526
"node": ">= 0.8.0"
2627
},
2728
"scripts": {
28-
"test": "mocha --check-leaks --reporter dot",
29+
"test": "mocha --check-leaks --reporter spec --bail",
2930
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot",
3031
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --check-leaks --reporter spec"
3132
}

0 commit comments

Comments
 (0)