Skip to content

Commit

Permalink
Pawda feature/#67 support no transform (#98)
Browse files Browse the repository at this point in the history
* [feature/#67-support-no-transform] Implements unit tests for "Cache-Control: no-transform"

* [feature/#67-support-no-transform] Add supports for "Cache-Control: no-transform"

* add change history

Co-authored-by: Memoria <[email protected]>
  • Loading branch information
jonathanong and Pawda authored Apr 16, 2020
1 parent 077c6ed commit 6f344a4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

3.1.0 / 2020-04-15

* support no-transform @Pawda

3.0.0 / 2018-04-14
==================

Expand Down
57 changes: 57 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,61 @@ describe('Compress', () => {
done()
})
})

describe('Cache-Control', () => {
['no-transform', 'public, no-transform', 'no-transform, private', 'no-transform , max-age=1000', 'max-age=1000 , no-transform'].forEach(headerValue => {
it(`should skip Cache-Control: ${headerValue}`, done => {
const app = new Koa()

app.use(compress())
app.use((ctx, next) => {
ctx.set('Cache-Control', headerValue)
next()
})
app.use(sendString)

request(app.listen())
.get('/')
.expect(200)
.end((err, res) => {
if (err) { return done(err) }

assert.equal(res.headers['content-length'], '2048')
assert.equal(res.headers.vary, 'Accept-Encoding')
assert(!res.headers['content-encoding'])
assert(!res.headers['transfer-encoding'])
assert.equal(res.text, string)

done()
})
})
});

['not-no-transform', 'public', 'no-transform-thingy'].forEach(headerValue => {
it(`should not skip Cache-Control: ${headerValue}`, done => {
const app = new Koa()

app.use(compress())
app.use((ctx, next) => {
ctx.set('Cache-Control', headerValue)
next()
})
app.use(sendString)

request(app.listen())
.get('/')
.expect(200)
.end((err, res) => {
if (err) { return done(err) }

assert.equal(res.headers['transfer-encoding'], 'chunked')
assert.equal(res.headers.vary, 'Accept-Encoding')
assert(!res.headers['content-length'])
assert.equal(res.text, string)

done()
})
})
})
})
})
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 6f344a4

Please sign in to comment.