Skip to content

Commit 03c2a67

Browse files
author
Kevin R. Whitley
authored
Merge pull request #106 from andredigenova/master
Add an option to disable caching of the response headers Thanks for the clean PR @andredigenova - merging now! 👍
2 parents 4652b5f + c1f674b commit 03c2a67

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ let cache5min = cache('5 min') // continue to use normally
146146
enabled: true|false, // if false, turns off caching globally (useful on dev)
147147
redisClient: client, // if provided, uses the [node-redis](https://github.com/NodeRedis/node_redis) client instead of [memory-cache](https://github.com/ptarjan/node-cache)
148148
appendKey: [], // if you want the key (which is the URL) to be appended by something in the req object, put req properties here that point to what you want appended. I.E. req.session.id would be ['session', 'id']
149+
headerBlacklist: [], // list of headers that should never be cached
149150
statusCodes: {
150151
exclude: [], // list status codes to specifically exclude (e.g. [404, 403] cache all responses unless they had a 404 or 403 status)
151152
include: [], // list status codes to require (e.g. [200] caches ONLY responses with a success/200 code)

src/apicache.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function ApiCache() {
3737
appendKey: [],
3838
jsonp: false,
3939
redisClient: false,
40+
headerBlacklist: [],
4041
statusCodes: {
4142
include: [],
4243
exclude: [],
@@ -87,10 +88,19 @@ function ApiCache() {
8788
index.all.unshift(key)
8889
}
8990

91+
function filterBlacklistedHeaders(headers) {
92+
return Object.keys(headers).filter(function (key) {
93+
return globalOptions.headerBlacklist.indexOf(key) === -1;
94+
}).reduce(function (acc, header) {
95+
acc[header] = headers[header];
96+
return acc;
97+
}, {});
98+
}
99+
90100
function createCacheObject(status, headers, data, encoding) {
91101
return {
92102
status: status,
93-
headers: Object.assign({}, headers),
103+
headers: filterBlacklistedHeaders(headers),
94104
data: data,
95105
encoding: encoding
96106
}
@@ -184,7 +194,8 @@ function ApiCache() {
184194

185195
function sendCachedResponse(response, cacheObject) {
186196
var headers = (typeof response.getHeaders === 'function') ? response.getHeaders() : response._headers;
187-
Object.assign(headers, cacheObject.headers || {}, {
197+
198+
Object.assign(headers, filterBlacklistedHeaders(cacheObject.headers || {}), {
188199
'apicache-store': globalOptions.redisClient ? 'redis' : 'memory',
189200
'apicache-version': pkg.version
190201
})

test/api/lib/routes.js

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ module.exports = function(app) {
4343
res.end()
4444
})
4545

46+
app.get('/api/testheaderblacklist', function(req, res) {
47+
app.requestsProcessed++
48+
res.set('x-blacklisted', app.requestsProcessed)
49+
res.set('x-notblacklisted', app.requestsProcessed)
50+
51+
res.json(movies)
52+
})
53+
4654
app.get('/api/testcachegroup', function(req, res) {
4755
app.requestsProcessed++
4856
req.apicacheGroup = 'cachegroup'

test/apicache_test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ describe('.middleware {MIDDLEWARE}', function() {
160160
appendKey: [ 'test' ],
161161
jsonp: false,
162162
redisClient: false,
163+
headerBlacklist: [],
163164
statusCodes: { include: [], exclude: [] },
164165
events: { expire: undefined },
165166
headers: {}
@@ -171,6 +172,7 @@ describe('.middleware {MIDDLEWARE}', function() {
171172
appendKey: [ 'test' ],
172173
jsonp: false,
173174
redisClient: false,
175+
headerBlacklist: [],
174176
statusCodes: { include: [], exclude: [] },
175177
events: { expire: undefined },
176178
headers: {}
@@ -205,6 +207,7 @@ describe('.middleware {MIDDLEWARE}', function() {
205207
appendKey: [ 'bar' ],
206208
jsonp: false,
207209
redisClient: false,
210+
headerBlacklist: [],
208211
statusCodes: { include: [], exclude: ['400'] },
209212
events: { expire: undefined },
210213
headers: {
@@ -218,6 +221,7 @@ describe('.middleware {MIDDLEWARE}', function() {
218221
appendKey: [ 'foo' ],
219222
jsonp: false,
220223
redisClient: false,
224+
headerBlacklist: [],
221225
statusCodes: { include: [], exclude: ['200'] },
222226
events: { expire: undefined },
223227
headers: {}
@@ -248,6 +252,7 @@ describe('.middleware {MIDDLEWARE}', function() {
248252
appendKey: [ 'foo' ],
249253
jsonp: false,
250254
redisClient: false,
255+
headerBlacklist: [],
251256
statusCodes: { include: [], exclude: ['400'] },
252257
events: { expire: undefined },
253258
headers: {}
@@ -259,6 +264,7 @@ describe('.middleware {MIDDLEWARE}', function() {
259264
appendKey: [ 'foo' ],
260265
jsonp: false,
261266
redisClient: false,
267+
headerBlacklist: [],
262268
statusCodes: { include: [], exclude: ['200'] },
263269
events: { expire: undefined },
264270
headers: {}
@@ -298,6 +304,7 @@ describe('.middleware {MIDDLEWARE}', function() {
298304
appendKey: [ 'foo' ],
299305
jsonp: false,
300306
redisClient: false,
307+
headerBlacklist: [],
301308
statusCodes: { include: [], exclude: [] },
302309
events: { expire: undefined },
303310
headers: {
@@ -311,6 +318,7 @@ describe('.middleware {MIDDLEWARE}', function() {
311318
appendKey: [ 'foo' ],
312319
jsonp: false,
313320
redisClient: false,
321+
headerBlacklist: [],
314322
statusCodes: { include: [], exclude: [] },
315323
events: { expire: undefined },
316324
headers: {}
@@ -362,7 +370,7 @@ describe('.middleware {MIDDLEWARE}', function() {
362370
})
363371
})
364372

365-
it('skips cache when using header "x-apicache-force-fetch (legacy)"', function() {
373+
it('skips cache when using header "x-apicache-force-fetch (legacy)"', function() {
366374
var app = mockAPI.create('10 seconds')
367375

368376
return request(app)
@@ -384,6 +392,25 @@ describe('.middleware {MIDDLEWARE}', function() {
384392
})
385393
})
386394

395+
it('does not cache header in headerBlacklist', function() {
396+
var app = mockAPI.create('10 seconds', {headerBlacklist: ['x-blacklisted']})
397+
398+
return request(app)
399+
.get('/api/testheaderblacklist')
400+
.expect(200, movies)
401+
.then(function(res) {
402+
expect(res.headers['x-blacklisted']).to.equal(res.headers['x-notblacklisted'])
403+
return request(app)
404+
.get('/api/testheaderblacklist')
405+
.set('Accept', 'application/json')
406+
.expect('Content-Type', /json/)
407+
.expect(200, movies)
408+
.then(function(res2) {
409+
expect(res2.headers['x-blacklisted']).to.not.equal(res2.headers['x-notblacklisted'])
410+
})
411+
})
412+
})
413+
387414
it('properly returns a cached JSON request', function() {
388415
var app = mockAPI.create('10 seconds')
389416

0 commit comments

Comments
 (0)