forked from koa-modules/koa-no-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (41 loc) · 1.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
module.exports = noCache;
function setNoCacheHeaders(ctx) {
if (ctx.skipNoCache) {
return;
}
ctx.set('Cache-Control', 'no-store, no-cache, must-revalidate');
ctx.set('Pragma', 'no-cache');
ctx.set('Expires', 0);
}
function noCache(options) {
options = options || {};
options.global = !!options.global;
var paths = options.paths || [];
var types = options.types || [];
var config = options.config || {
sensitive: true,
strict: true
};
if (options.global) {
return function * noCache(next) {
yield * next;
setNoCacheHeaders(this);
};
} else {
// only load modules if needed
var pathToRegExp = require('path-to-regexp');
var typeis = require('type-is').is;
return function * noCache(next) {
yield * next;
if (typeis(this.type, types) || match(this.path)) {
setNoCacheHeaders(this);
}
};
}
function match(path) {
for (var i = 0; i < paths.length; i++) {
if (pathToRegExp(paths[i], [], config).exec(path)) return true;
}
}
}