@@ -44,7 +44,7 @@ var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
44
44
* @public
45
45
*/
46
46
47
- function compression ( options ) {
47
+ function compression ( options ) {
48
48
var opts = options || { }
49
49
50
50
// options
@@ -55,25 +55,26 @@ function compression(options) {
55
55
threshold = 1024
56
56
}
57
57
58
- return function compression ( req , res , next ) {
58
+ return function compression ( req , res , next ) {
59
59
var ended = false
60
60
var length
61
61
var listeners = [ ]
62
- var write = res . write
63
- var on = res . on
64
- var end = res . end
65
62
var stream
66
63
64
+ var _end = res . end
65
+ var _on = res . on
66
+ var _write = res . write
67
+
67
68
// flush
68
- res . flush = function flush ( ) {
69
+ res . flush = function flush ( ) {
69
70
if ( stream ) {
70
71
stream . flush ( )
71
72
}
72
73
}
73
74
74
75
// proxy
75
76
76
- res . write = function ( chunk , encoding ) {
77
+ res . write = function write ( chunk , encoding ) {
77
78
if ( ended ) {
78
79
return false
79
80
}
@@ -84,10 +85,10 @@ function compression(options) {
84
85
85
86
return stream
86
87
? stream . write ( new Buffer ( chunk , encoding ) )
87
- : write . call ( this , chunk , encoding )
88
- } ;
88
+ : _write . call ( this , chunk , encoding )
89
+ }
89
90
90
- res . end = function ( chunk , encoding ) {
91
+ res . end = function end ( chunk , encoding ) {
91
92
if ( ended ) {
92
93
return false
93
94
}
@@ -102,7 +103,7 @@ function compression(options) {
102
103
}
103
104
104
105
if ( ! stream ) {
105
- return end . call ( this , chunk , encoding )
106
+ return _end . call ( this , chunk , encoding )
106
107
}
107
108
108
109
// mark ended
@@ -112,11 +113,11 @@ function compression(options) {
112
113
return chunk
113
114
? stream . end ( new Buffer ( chunk , encoding ) )
114
115
: stream . end ( )
115
- } ;
116
+ }
116
117
117
- res . on = function ( type , listener ) {
118
+ res . on = function on ( type , listener ) {
118
119
if ( ! listeners || type !== 'drain' ) {
119
- return on . call ( this , type , listener )
120
+ return _on . call ( this , type , listener )
120
121
}
121
122
122
123
if ( stream ) {
@@ -129,13 +130,13 @@ function compression(options) {
129
130
return this
130
131
}
131
132
132
- function nocompress ( msg ) {
133
+ function nocompress ( msg ) {
133
134
debug ( 'no compression: %s' , msg )
134
- addListeners ( res , on , listeners )
135
+ addListeners ( res , _on , listeners )
135
136
listeners = null
136
137
}
137
138
138
- onHeaders ( res , function ( ) {
139
+ onHeaders ( res , function onResponseHeaders ( ) {
139
140
// determine if request is filtered
140
141
if ( ! filter ( req , res ) ) {
141
142
nocompress ( 'filtered' )
@@ -157,16 +158,16 @@ function compression(options) {
157
158
return
158
159
}
159
160
160
- var encoding = res . getHeader ( 'Content-Encoding' ) || 'identity' ;
161
+ var encoding = res . getHeader ( 'Content-Encoding' ) || 'identity'
161
162
162
163
// already encoded
163
- if ( 'identity' !== encoding ) {
164
+ if ( encoding !== 'identity' ) {
164
165
nocompress ( 'already encoded' )
165
166
return
166
167
}
167
168
168
169
// head
169
- if ( 'HEAD' === req . method ) {
170
+ if ( req . method === 'HEAD' ) {
170
171
nocompress ( 'HEAD request' )
171
172
return
172
173
}
@@ -196,35 +197,35 @@ function compression(options) {
196
197
addListeners ( stream , stream . on , listeners )
197
198
198
199
// header fields
199
- res . setHeader ( 'Content-Encoding' , method ) ;
200
- res . removeHeader ( 'Content-Length' ) ;
200
+ res . setHeader ( 'Content-Encoding' , method )
201
+ res . removeHeader ( 'Content-Length' )
201
202
202
203
// 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 ) {
205
206
stream . pause ( )
206
207
}
207
- } ) ;
208
+ } )
208
209
209
- stream . on ( 'end' , function ( ) {
210
- end . call ( res ) ;
211
- } ) ;
210
+ stream . on ( 'end' , function onStreamEnd ( ) {
211
+ _end . call ( res )
212
+ } )
212
213
213
- on . call ( res , 'drain' , function ( ) {
214
+ _on . call ( res , 'drain' , function onResponseDrain ( ) {
214
215
stream . resume ( )
215
- } ) ;
216
- } ) ;
216
+ } )
217
+ } )
217
218
218
- next ( ) ;
219
- } ;
219
+ next ( )
220
+ }
220
221
}
221
222
222
223
/**
223
224
* Add bufferred listeners to stream
224
225
* @private
225
226
*/
226
227
227
- function addListeners ( stream , on , listeners ) {
228
+ function addListeners ( stream , on , listeners ) {
228
229
for ( var i = 0 ; i < listeners . length ; i ++ ) {
229
230
on . apply ( stream , listeners [ i ] )
230
231
}
@@ -234,7 +235,7 @@ function addListeners(stream, on, listeners) {
234
235
* Get the length of a given chunk
235
236
*/
236
237
237
- function chunkLength ( chunk , encoding ) {
238
+ function chunkLength ( chunk , encoding ) {
238
239
if ( ! chunk ) {
239
240
return 0
240
241
}
@@ -249,7 +250,7 @@ function chunkLength(chunk, encoding) {
249
250
* @private
250
251
*/
251
252
252
- function shouldCompress ( req , res ) {
253
+ function shouldCompress ( req , res ) {
253
254
var type = res . getHeader ( 'Content-Type' )
254
255
255
256
if ( type === undefined || ! compressible ( type ) ) {
@@ -265,11 +266,11 @@ function shouldCompress(req, res) {
265
266
* @private
266
267
*/
267
268
268
- function shouldTransform ( req , res ) {
269
+ function shouldTransform ( req , res ) {
269
270
var cacheControl = res . getHeader ( 'Cache-Control' )
270
271
271
272
// Don't compress for Cache-Control: no-transform
272
273
// 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 )
275
276
}
0 commit comments