diff --git a/lib/web/fetch/decompress.js b/lib/web/fetch/decompress.js index 68becc44695..841390cce2f 100644 --- a/lib/web/fetch/decompress.js +++ b/lib/web/fetch/decompress.js @@ -10,20 +10,7 @@ const { createInflate } = require('./util') * @returns {ReadableStream | null} */ function decompress (request, response) { - const contentEncoding = response.headers.get('content-encoding') - - if (!contentEncoding) { - return response.body - } - - // https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1 - // "All content-coding values are case-insensitive..." - const codings = contentEncoding - .toLowerCase() - .split(',') - .map((coding) => coding.trim()) - - if (codings.length === 0) { + if (response.body === null) { return response.body } @@ -41,6 +28,33 @@ function decompress (request, response) { return response.body } + return decompressStream( + response.body, + response.headers.get('content-encoding') + ) +} + +/** + * Decompress the given stream based on the "Content-Encoding" response header. + * @param {ReadableStream} stream + * @param {string | null} contentEncoding The value of the "Content-Encoding" response header. + */ +function decompressStream (stream, contentEncoding) { + if (!contentEncoding) { + return stream + } + + // https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1 + // "All content-coding values are case-insensitive..." + const codings = contentEncoding + .toLowerCase() + .split(',') + .map((coding) => coding.trim()) + + if (codings.length === 0) { + return stream + } + const decoders = [] for (let i = codings.length - 1; i >= 0; --i) { @@ -69,12 +83,13 @@ function decompress (request, response) { } if (decoders.length === 0) { - return response.body + return stream } - return pipeline(response.body, ...decoders, () => {}) + return pipeline(stream, ...decoders, () => {}) } module.exports = { - decompress + decompress, + decompressStream }