-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (50 loc) · 1.83 KB
/
index.js
File metadata and controls
64 lines (50 loc) · 1.83 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var events = require('events');
var util = require('util');
var caches = require('./lib/cachecollection')();
function CacheOut(app) {
events.EventEmitter.call(this);
var self = this;
caches.viewPath = app.get('views');
caches.viewEngine = app.get('view engine');
caches.on('added', function(cache) {
self.emit('added', cache);
});
caches.on('removed', function(cache) {
self.emit('removed', cache);
});
if (app) {
this.enhanceExpress(app);
}
}
util.inherits(CacheOut, events.EventEmitter);
module.exports = function(app) {
return new CacheOut(app);
}
CacheOut.prototype.enhanceExpress = function (app) {
var versionedUrlMiddleware = require('./lib/middleware/versionedurl');
var conditionMatcherMiddleware = require('./lib/middleware/conditionmatcher');
var outputCacheMiddleware = require('./lib/middleware/outputcache');
var staticMiddleware = require('./lib/middleware/static');
app.use(versionedUrlMiddleware);
app.use(conditionMatcherMiddleware);
this.static = staticMiddleware;
cacheVerb('get');
cacheVerb('post');
cacheVerb('all');
function cacheVerb(verb) {
var fn = app[verb];
app[verb] = function () {
var args = arguments;
if (arguments.length > 1 && typeof(arguments[arguments.length - 1]) == 'object') {
var path = arguments[0];
var options = arguments[arguments.length - 1];
var callbacks = Array.prototype.slice.call(arguments, 1, arguments.length - 1);
args = [
path,
outputCacheMiddleware(callbacks, options) // inject cache handler
];
}
return fn.apply(app, args);
}
}
}