1212var zlib = require ( 'zlib' ) ;
1313var accepts = require ( 'accepts' ) ;
1414var bytes = require ( 'bytes' ) ;
15+ var debug = require ( 'debug' ) ( 'compression' )
1516var onHeaders = require ( 'on-headers' ) ;
1617var compressible = require ( 'compressible' ) ;
1718var 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
0 commit comments