@@ -146,7 +146,6 @@ function read (req, res, next, parse, debug, options) {
146
146
function contentstream ( req , debug , inflate ) {
147
147
var encoding = ( req . headers [ 'content-encoding' ] || 'identity' ) . toLowerCase ( )
148
148
var length = req . headers [ 'content-length' ]
149
- var stream
150
149
151
150
debug ( 'content-encoding "%s"' , encoding )
152
151
@@ -157,36 +156,40 @@ function contentstream (req, debug, inflate) {
157
156
} )
158
157
}
159
158
159
+ if ( encoding === 'identity' ) {
160
+ req . length = length
161
+ return req
162
+ }
163
+
164
+ var stream = createDecompressionStream ( encoding , debug )
165
+ req . pipe ( stream )
166
+ return stream
167
+ }
168
+
169
+ /**
170
+ * Create a decompression stream for the given encoding.
171
+ * @param {string } encoding
172
+ * @param {function } debug
173
+ * @return {object }
174
+ * @api private
175
+ */
176
+ function createDecompressionStream ( encoding , debug ) {
160
177
switch ( encoding ) {
161
178
case 'deflate' :
162
- stream = zlib . createInflate ( )
163
179
debug ( 'inflate body' )
164
- req . pipe ( stream )
165
- break
180
+ return zlib . createInflate ( )
166
181
case 'gzip' :
167
- stream = zlib . createGunzip ( )
168
182
debug ( 'gunzip body' )
169
- req . pipe ( stream )
170
- break
171
- case 'identity' :
172
- stream = req
173
- stream . length = length
174
- break
183
+ return zlib . createGunzip ( )
175
184
case 'br' :
176
- stream = zlib . createBrotliDecompress ( )
177
185
debug ( 'brotli decompress body' )
178
- req . pipe ( stream )
179
- break
180
- }
181
-
182
- if ( stream === undefined ) {
183
- throw createError ( 415 , 'unsupported content encoding "' + encoding + '"' , {
184
- encoding : encoding ,
185
- type : 'encoding.unsupported'
186
- } )
186
+ return zlib . createBrotliDecompress ( )
187
+ default :
188
+ throw createError ( 415 , 'unsupported content encoding "' + encoding + '"' , {
189
+ encoding : encoding ,
190
+ type : 'encoding.unsupported'
191
+ } )
187
192
}
188
-
189
- return stream
190
193
}
191
194
192
195
/**
0 commit comments