Skip to content

Commit 6202eba

Browse files
author
Wolfgang Frank
committed
Merge branch 'master' of https://github.com/kwhitley/apicache
* 'master' of https://github.com/kwhitley/apicache: 0.10.0 added ability to blacklist headers through PR Provide a default to filterBlacklistedHeaders if cacheObject.headers is falsy since filterBlacklistedHeaders doesn't accept non-objects. Node 4.0.0 does not have Array.includes method Change a blanket disabling of caching of all headers to a blacklist of specific headers. Update documentation. Fix tests. Add option to disable header caching.
2 parents 473c7b6 + a74f3ce commit 6202eba

File tree

6 files changed

+59
-15
lines changed

6 files changed

+59
-15
lines changed

README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ let cache5min = cache('5 min') // continue to use normally
165165
enabled: true|false, // if false, turns off caching globally (useful on dev)
166166
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)
167167
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']
168+
headerBlacklist: [], // list of headers that should never be cached
168169
statusCodes: {
169170
exclude: [], // list status codes to specifically exclude (e.g. [404, 403] cache all responses unless they had a 404 or 403 status)
170171
include: [], // list status codes to require (e.g. [200] caches ONLY responses with a success/200 code)
@@ -257,14 +258,11 @@ Special thanks to all those that use this library and report issues, but especia
257258
- [@danielsogl](https://github.com/danielsogl) - Keeping dev deps up to date
258259
- [@peteboere](https://github.com/peteboere) - Node v7 headers update
259260
- [@vectart](https://github.com/vectart) - Added middleware local options support
261+
- [@andredigenova](https://github.com/andredigenova) - Added header blacklist as options
260262

261-
### Bugfixes
263+
### Bugfixes, Documentation, etc.
262264

263-
- @Amhri, @Webcascade, @conmarap, @cjfurelid, @scambier, @lukechilds, @Red-Lv
264-
265-
### Documentation
266-
267-
- @gesposito, @viebel
265+
- @Amhri, @Webcascade, @conmarap, @cjfurelid, @scambier, @lukechilds, @Red-Lv, @gesposito, @viebel
268266

269267
### Changelog
270268
- **v0.4.0** - dropped lodash and memory-cache external dependencies, and bumped node version requirements to 4.0.0+ to allow Object.assign native support
@@ -284,4 +282,4 @@ Special thanks to all those that use this library and report issues, but especia
284282
middleware.localOptions support (thanks @vectart). Added ability to overwrite/embed headers
285283
(e.g. "cache-control": "no-cache") through options.
286284
- **v0.9.1** - added eslint in prep for v1.x branch, minor ES6 to ES5 in master branch tests
287-
285+
- **v0.10.0** - added ability to blacklist headers (prevents caching) via options.headersBlacklist (thanks @andredigenova)

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apicache",
3-
"version": "0.9.1",
3+
"version": "0.10.0",
44
"scripts": {
55
"lint": "eslint **/*.js",
66
"pretest": "npm run lint",

src/apicache.js

+16-5
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: [],
@@ -103,10 +104,19 @@ function ApiCache() {
103104
index.all.unshift(key)
104105
}
105106

107+
function filterBlacklistedHeaders(headers) {
108+
return Object.keys(headers).filter(function (key) {
109+
return globalOptions.headerBlacklist.indexOf(key) === -1;
110+
}).reduce(function (acc, header) {
111+
acc[header] = headers[header];
112+
return acc;
113+
}, {});
114+
}
115+
106116
function createCacheObject(status, headers, data, encoding) {
107117
return {
108118
status: status,
109-
headers: Object.assign({}, headers),
119+
headers: filterBlacklistedHeaders(headers),
110120
data: data,
111121
encoding: encoding
112122
}
@@ -201,7 +211,8 @@ function ApiCache() {
201211

202212
function sendCachedResponse(response, cacheObject) {
203213
var headers = (typeof response.getHeaders === 'function') ? response.getHeaders() : response._headers;
204-
Object.assign(headers, cacheObject.headers || {}, {
214+
215+
Object.assign(headers, filterBlacklistedHeaders(cacheObject.headers || {}), {
205216
'apicache-store': globalOptions.redisClient ? 'redis' : 'memory',
206217
'apicache-version': pkg.version
207218
})
@@ -239,7 +250,7 @@ function ApiCache() {
239250
try {
240251
redis.del(key)
241252
} catch(err) {
242-
throw Error('[apicache] error in redis.del("' + key + '"")')
253+
console.log('[apicache] error in redis.del("' + key + '")')
243254
}
244255
}
245256
index.all = index.all.filter(doesntMatch(key))
@@ -256,7 +267,7 @@ function ApiCache() {
256267
try {
257268
redis.del(target)
258269
} catch(err) {
259-
throw Error('[apicache] error in redis.del("' + target + '"")')
270+
console.log('[apicache] error in redis.del("' + target + '")')
260271
}
261272
}
262273

@@ -283,7 +294,7 @@ function ApiCache() {
283294
try {
284295
redis.del(key)
285296
} catch(err) {
286-
throw Error('[apicache] error in redis.del("' + key + '"")')
297+
console.log('[apicache] error in redis.del("' + key + '")')
287298
}
288299
})
289300
}

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
shortTermMemory: '0 seconds',
165166
events: { expire: undefined },
@@ -172,6 +173,7 @@ describe('.middleware {MIDDLEWARE}', function() {
172173
appendKey: [ 'test' ],
173174
jsonp: false,
174175
redisClient: false,
176+
headerBlacklist: [],
175177
statusCodes: { include: [], exclude: [] },
176178
shortTermMemory: '0 seconds',
177179
events: { expire: undefined },
@@ -209,6 +211,7 @@ describe('.middleware {MIDDLEWARE}', function() {
209211
appendKey: [ 'bar' ],
210212
jsonp: false,
211213
redisClient: false,
214+
headerBlacklist: [],
212215
statusCodes: { include: [], exclude: ['400'] },
213216
shortTermMemory: '0 seconds',
214217
events: { expire: undefined },
@@ -223,6 +226,7 @@ describe('.middleware {MIDDLEWARE}', function() {
223226
appendKey: [ 'foo' ],
224227
jsonp: false,
225228
redisClient: false,
229+
headerBlacklist: [],
226230
statusCodes: { include: [], exclude: ['200'] },
227231
shortTermMemory: '0 seconds',
228232
events: { expire: undefined },
@@ -254,6 +258,7 @@ describe('.middleware {MIDDLEWARE}', function() {
254258
appendKey: [ 'foo' ],
255259
jsonp: false,
256260
redisClient: false,
261+
headerBlacklist: [],
257262
statusCodes: { include: [], exclude: ['400'] },
258263
shortTermMemory: '0 seconds',
259264
events: { expire: undefined },
@@ -266,6 +271,7 @@ describe('.middleware {MIDDLEWARE}', function() {
266271
appendKey: [ 'foo' ],
267272
jsonp: false,
268273
redisClient: false,
274+
headerBlacklist: [],
269275
statusCodes: { include: [], exclude: ['200'] },
270276
shortTermMemory: '0 seconds',
271277
events: { expire: undefined },
@@ -306,6 +312,7 @@ describe('.middleware {MIDDLEWARE}', function() {
306312
appendKey: [ 'foo' ],
307313
jsonp: false,
308314
redisClient: false,
315+
headerBlacklist: [],
309316
statusCodes: { include: [], exclude: [] },
310317
shortTermMemory: '0 seconds',
311318
events: { expire: undefined },
@@ -320,6 +327,7 @@ describe('.middleware {MIDDLEWARE}', function() {
320327
appendKey: [ 'foo' ],
321328
jsonp: false,
322329
redisClient: false,
330+
headerBlacklist: [],
323331
statusCodes: { include: [], exclude: [] },
324332
shortTermMemory: '0 seconds',
325333
events: { expire: undefined },
@@ -372,7 +380,7 @@ describe('.middleware {MIDDLEWARE}', function() {
372380
})
373381
})
374382

375-
it('skips cache when using header "x-apicache-force-fetch (legacy)"', function() {
383+
it('skips cache when using header "x-apicache-force-fetch (legacy)"', function() {
376384
var app = mockAPI.create('10 seconds')
377385

378386
return request(app)
@@ -394,6 +402,25 @@ describe('.middleware {MIDDLEWARE}', function() {
394402
})
395403
})
396404

405+
it('does not cache header in headerBlacklist', function() {
406+
var app = mockAPI.create('10 seconds', {headerBlacklist: ['x-blacklisted']})
407+
408+
return request(app)
409+
.get('/api/testheaderblacklist')
410+
.expect(200, movies)
411+
.then(function(res) {
412+
expect(res.headers['x-blacklisted']).to.equal(res.headers['x-notblacklisted'])
413+
return request(app)
414+
.get('/api/testheaderblacklist')
415+
.set('Accept', 'application/json')
416+
.expect('Content-Type', /json/)
417+
.expect(200, movies)
418+
.then(function(res2) {
419+
expect(res2.headers['x-blacklisted']).to.not.equal(res2.headers['x-notblacklisted'])
420+
})
421+
})
422+
})
423+
397424
it('properly returns a cached JSON request', function() {
398425
var app = mockAPI.create('10 seconds')
399426

0 commit comments

Comments
 (0)