Skip to content

Commit

Permalink
[feature/#67-support-no-transform] Add supports for "Cache-Control: n…
Browse files Browse the repository at this point in the history
…o-transform"
  • Loading branch information
Pawda authored and jonathanong committed Apr 16, 2020
1 parent 31a35ca commit 1724d10
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 1724d10

Please sign in to comment.