Skip to content

Commit 6f344a4

Browse files
jonathanongPawda
andauthored
Pawda feature/#67 support no transform (#98)
* [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 <Pawda@users.noreply.github.com>
1 parent 077c6ed commit 6f344a4

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
3.1.0 / 2020-04-15
3+
4+
* support no-transform @Pawda
5+
26
3.0.0 / 2018-04-14
37
==================
48

__tests__/index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,61 @@ describe('Compress', () => {
297297
done()
298298
})
299299
})
300+
301+
describe('Cache-Control', () => {
302+
['no-transform', 'public, no-transform', 'no-transform, private', 'no-transform , max-age=1000', 'max-age=1000 , no-transform'].forEach(headerValue => {
303+
it(`should skip Cache-Control: ${headerValue}`, done => {
304+
const app = new Koa()
305+
306+
app.use(compress())
307+
app.use((ctx, next) => {
308+
ctx.set('Cache-Control', headerValue)
309+
next()
310+
})
311+
app.use(sendString)
312+
313+
request(app.listen())
314+
.get('/')
315+
.expect(200)
316+
.end((err, res) => {
317+
if (err) { return done(err) }
318+
319+
assert.equal(res.headers['content-length'], '2048')
320+
assert.equal(res.headers.vary, 'Accept-Encoding')
321+
assert(!res.headers['content-encoding'])
322+
assert(!res.headers['transfer-encoding'])
323+
assert.equal(res.text, string)
324+
325+
done()
326+
})
327+
})
328+
});
329+
330+
['not-no-transform', 'public', 'no-transform-thingy'].forEach(headerValue => {
331+
it(`should not skip Cache-Control: ${headerValue}`, done => {
332+
const app = new Koa()
333+
334+
app.use(compress())
335+
app.use((ctx, next) => {
336+
ctx.set('Cache-Control', headerValue)
337+
next()
338+
})
339+
app.use(sendString)
340+
341+
request(app.listen())
342+
.get('/')
343+
.expect(200)
344+
.end((err, res) => {
345+
if (err) { return done(err) }
346+
347+
assert.equal(res.headers['transfer-encoding'], 'chunked')
348+
assert.equal(res.headers.vary, 'Accept-Encoding')
349+
assert(!res.headers['content-length'])
350+
assert.equal(res.text, string)
351+
352+
done()
353+
})
354+
})
355+
})
356+
})
300357
})

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const encodingMethods = {
2020
deflate: zlib.createDeflate
2121
}
2222

23+
/**
24+
* Regex to match no-transform directive in a cache-control header
25+
*/
26+
const NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
27+
2328
/**
2429
* Compress middleware.
2530
*
@@ -48,6 +53,11 @@ module.exports = (options = {}) => {
4853
// forced compression or implied
4954
if (!(ctx.compress === true || filter(ctx.response.type))) return
5055

56+
// Don't compress for Cache-Control: no-transform
57+
// https://tools.ietf.org/html/rfc7234#section-5.2.1.6
58+
const cacheControl = ctx.response.get('Cache-Control')
59+
if (cacheControl && NO_TRANSFORM_REGEX.test(cacheControl)) return
60+
5161
// identity
5262
const encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity')
5363
if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')

0 commit comments

Comments
 (0)