diff --git a/index.js b/index.js index 82a96f2..1c6f130 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,11 @@ const encodingMethods = { deflate: zlib.createDeflate } +/** +* Regex to match no-transform directive in a cache-control header +*/ +const NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/ + /** * Compress middleware. * @@ -48,6 +53,11 @@ module.exports = (options = {}) => { // forced compression or implied if (!(ctx.compress === true || filter(ctx.response.type))) return + // Don't compress for Cache-Control: no-transform + // https://tools.ietf.org/html/rfc7234#section-5.2.1.6 + const cacheControl = ctx.response.get('Cache-Control') + if (cacheControl && NO_TRANSFORM_REGEX.test(cacheControl)) return + // identity const encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity') if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')