Skip to content

Commit e0f261e

Browse files
Phillip9587wesleytodd
authored andcommitted
Refactor decompression stream creation to remove code duplication
1 parent 17c3999 commit e0f261e

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

lib/read.js

+26-23
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ function read (req, res, next, parse, debug, options) {
146146
function contentstream (req, debug, inflate) {
147147
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
148148
var length = req.headers['content-length']
149-
var stream
150149

151150
debug('content-encoding "%s"', encoding)
152151

@@ -157,36 +156,40 @@ function contentstream (req, debug, inflate) {
157156
})
158157
}
159158

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) {
160177
switch (encoding) {
161178
case 'deflate':
162-
stream = zlib.createInflate()
163179
debug('inflate body')
164-
req.pipe(stream)
165-
break
180+
return zlib.createInflate()
166181
case 'gzip':
167-
stream = zlib.createGunzip()
168182
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()
175184
case 'br':
176-
stream = zlib.createBrotliDecompress()
177185
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+
})
187192
}
188-
189-
return stream
190193
}
191194

192195
/**

0 commit comments

Comments
 (0)