diff --git a/lib/read.js b/lib/read.js index b9edc95d..eee8b111 100644 --- a/lib/read.js +++ b/lib/read.js @@ -146,7 +146,6 @@ function read (req, res, next, parse, debug, options) { function contentstream (req, debug, inflate) { var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase() var length = req.headers['content-length'] - var stream debug('content-encoding "%s"', encoding) @@ -157,36 +156,40 @@ function contentstream (req, debug, inflate) { }) } + if (encoding === 'identity') { + req.length = length + return req + } + + var stream = createDecompressionStream(encoding, debug) + req.pipe(stream) + return stream +} + +/** + * Create a decompression stream for the given encoding. + * @param {string} encoding + * @param {function} debug + * @return {object} + * @api private + */ +function createDecompressionStream (encoding, debug) { switch (encoding) { case 'deflate': - stream = zlib.createInflate() debug('inflate body') - req.pipe(stream) - break + return zlib.createInflate() case 'gzip': - stream = zlib.createGunzip() debug('gunzip body') - req.pipe(stream) - break - case 'identity': - stream = req - stream.length = length - break + return zlib.createGunzip() case 'br': - stream = zlib.createBrotliDecompress() debug('brotli decompress body') - req.pipe(stream) - break - } - - if (stream === undefined) { - throw createError(415, 'unsupported content encoding "' + encoding + '"', { - encoding: encoding, - type: 'encoding.unsupported' - }) + return zlib.createBrotliDecompress() + default: + throw createError(415, 'unsupported content encoding "' + encoding + '"', { + encoding: encoding, + type: 'encoding.unsupported' + }) } - - return stream } /**