@@ -44,7 +44,7 @@ var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
4444 * @public
4545 */
4646
47- function compression ( options ) {
47+ function compression ( options ) {
4848 var opts = options || { }
4949
5050 // options
@@ -55,25 +55,26 @@ function compression(options) {
5555 threshold = 1024
5656 }
5757
58- return function compression ( req , res , next ) {
58+ return function compression ( req , res , next ) {
5959 var ended = false
6060 var length
6161 var listeners = [ ]
62- var write = res . write
63- var on = res . on
64- var end = res . end
6562 var stream
6663
64+ var _end = res . end
65+ var _on = res . on
66+ var _write = res . write
67+
6768 // flush
68- res . flush = function flush ( ) {
69+ res . flush = function flush ( ) {
6970 if ( stream ) {
7071 stream . flush ( )
7172 }
7273 }
7374
7475 // proxy
7576
76- res . write = function ( chunk , encoding ) {
77+ res . write = function write ( chunk , encoding ) {
7778 if ( ended ) {
7879 return false
7980 }
@@ -84,10 +85,10 @@ function compression(options) {
8485
8586 return stream
8687 ? stream . write ( new Buffer ( chunk , encoding ) )
87- : write . call ( this , chunk , encoding )
88- } ;
88+ : _write . call ( this , chunk , encoding )
89+ }
8990
90- res . end = function ( chunk , encoding ) {
91+ res . end = function end ( chunk , encoding ) {
9192 if ( ended ) {
9293 return false
9394 }
@@ -102,7 +103,7 @@ function compression(options) {
102103 }
103104
104105 if ( ! stream ) {
105- return end . call ( this , chunk , encoding )
106+ return _end . call ( this , chunk , encoding )
106107 }
107108
108109 // mark ended
@@ -112,11 +113,11 @@ function compression(options) {
112113 return chunk
113114 ? stream . end ( new Buffer ( chunk , encoding ) )
114115 : stream . end ( )
115- } ;
116+ }
116117
117- res . on = function ( type , listener ) {
118+ res . on = function on ( type , listener ) {
118119 if ( ! listeners || type !== 'drain' ) {
119- return on . call ( this , type , listener )
120+ return _on . call ( this , type , listener )
120121 }
121122
122123 if ( stream ) {
@@ -129,13 +130,13 @@ function compression(options) {
129130 return this
130131 }
131132
132- function nocompress ( msg ) {
133+ function nocompress ( msg ) {
133134 debug ( 'no compression: %s' , msg )
134- addListeners ( res , on , listeners )
135+ addListeners ( res , _on , listeners )
135136 listeners = null
136137 }
137138
138- onHeaders ( res , function ( ) {
139+ onHeaders ( res , function onResponseHeaders ( ) {
139140 // determine if request is filtered
140141 if ( ! filter ( req , res ) ) {
141142 nocompress ( 'filtered' )
@@ -157,16 +158,16 @@ function compression(options) {
157158 return
158159 }
159160
160- var encoding = res . getHeader ( 'Content-Encoding' ) || 'identity' ;
161+ var encoding = res . getHeader ( 'Content-Encoding' ) || 'identity'
161162
162163 // already encoded
163- if ( 'identity' !== encoding ) {
164+ if ( encoding !== 'identity' ) {
164165 nocompress ( 'already encoded' )
165166 return
166167 }
167168
168169 // head
169- if ( 'HEAD' === req . method ) {
170+ if ( req . method === 'HEAD' ) {
170171 nocompress ( 'HEAD request' )
171172 return
172173 }
@@ -196,35 +197,35 @@ function compression(options) {
196197 addListeners ( stream , stream . on , listeners )
197198
198199 // header fields
199- res . setHeader ( 'Content-Encoding' , method ) ;
200- res . removeHeader ( 'Content-Length' ) ;
200+ res . setHeader ( 'Content-Encoding' , method )
201+ res . removeHeader ( 'Content-Length' )
201202
202203 // compression
203- stream . on ( 'data' , function ( chunk ) {
204- if ( write . call ( res , chunk ) === false ) {
204+ stream . on ( 'data' , function onStreamData ( chunk ) {
205+ if ( _write . call ( res , chunk ) === false ) {
205206 stream . pause ( )
206207 }
207- } ) ;
208+ } )
208209
209- stream . on ( 'end' , function ( ) {
210- end . call ( res ) ;
211- } ) ;
210+ stream . on ( 'end' , function onStreamEnd ( ) {
211+ _end . call ( res )
212+ } )
212213
213- on . call ( res , 'drain' , function ( ) {
214+ _on . call ( res , 'drain' , function onResponseDrain ( ) {
214215 stream . resume ( )
215- } ) ;
216- } ) ;
216+ } )
217+ } )
217218
218- next ( ) ;
219- } ;
219+ next ( )
220+ }
220221}
221222
222223/**
223224 * Add bufferred listeners to stream
224225 * @private
225226 */
226227
227- function addListeners ( stream , on , listeners ) {
228+ function addListeners ( stream , on , listeners ) {
228229 for ( var i = 0 ; i < listeners . length ; i ++ ) {
229230 on . apply ( stream , listeners [ i ] )
230231 }
@@ -234,7 +235,7 @@ function addListeners(stream, on, listeners) {
234235 * Get the length of a given chunk
235236 */
236237
237- function chunkLength ( chunk , encoding ) {
238+ function chunkLength ( chunk , encoding ) {
238239 if ( ! chunk ) {
239240 return 0
240241 }
@@ -249,7 +250,7 @@ function chunkLength(chunk, encoding) {
249250 * @private
250251 */
251252
252- function shouldCompress ( req , res ) {
253+ function shouldCompress ( req , res ) {
253254 var type = res . getHeader ( 'Content-Type' )
254255
255256 if ( type === undefined || ! compressible ( type ) ) {
@@ -265,11 +266,11 @@ function shouldCompress(req, res) {
265266 * @private
266267 */
267268
268- function shouldTransform ( req , res ) {
269+ function shouldTransform ( req , res ) {
269270 var cacheControl = res . getHeader ( 'Cache-Control' )
270271
271272 // Don't compress for Cache-Control: no-transform
272273 // https://tools.ietf.org/html/rfc7234#section-5.2.2.4
273- return ! cacheControl
274- || ! cacheControlNoTransformRegExp . test ( cacheControl )
274+ return ! cacheControl ||
275+ ! cacheControlNoTransformRegExp . test ( cacheControl )
275276}
0 commit comments