-
Notifications
You must be signed in to change notification settings - Fork 120
/
route.js
223 lines (166 loc) · 5.44 KB
/
route.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module.exports = (function () {
'use strict';
/** route() - Create a middleware
*
* @method ExpressRedisCache.route
* @return {Function} middleware
*/
function route () {
/** The middleware to return
*
* @type Function
*/
var middleware;
/** A reference to this
*
* @type ExpressRedisCache
*/
var self = this;
/** The route options
*
* @type List
*/
var options = arguments;
/** The domain handler
*
* @type Domain
*/
var domain = require('domain').create();
/** The domain error handler
*
* @type Domain
*/
domain.on('error', function (error) {
self.emit('error', error);
});
domain.run(function () {
// Build the middleware function
/**
* @function
* @arg {IncomingMessage} req
* @arg {HttpResponse} res
* @arg {Function} next
*/
middleware = function expressRedisCache_Middleware (req, res, next) {
if ( res.expressRedisCache ) {
self.emit('deprecated', {
deprecated: 'expressRedisCache',
substitute: 'use_express_redis_cache',
file: __fileName,
line: __line
});
res.use_express_redis_cache = res.expressRedisCache;
}
// If cache is disabled, call next()
if ( res.use_express_redis_cache === false ) {
return next();
}
// If the cache isn't connected, call next()
if ( self.connected === false || self.client.connected === false ) {
return next();
}
/** Cache entry name
*
* @type String
* @description The name the cache entry will be saved under
* @default req.originalUrl
*/
var name = req.originalUrl;
/**
* @deprecated `res.expressRedisCacheName` is deprecated starting on v0.1.1, `use res.express_redis_cache_name` instead
*/
if ( res.expressRedisCacheName ) {
self.emit('deprecated', {
deprecated: 'expressRedisCacheName',
substitute: 'express_redis_cache_name',
file: __fileName,
line: __line
});
res.express_redis_cache_name = res.expressRedisCacheName;
}
// If a cache has been explicitly attached to `res` then use it as name
if ( res.express_redis_cache_name ) {
name = res.express_redis_cache_name;
}
// If route() was called with a string as its first argument, use this string as name
if ( typeof options[0] === 'string' ) {
name = options[0];
}
if ( typeof options[0] === 'object' && typeof options[0].name === 'string' ) {
name = options[0].name;
}
/** Name cannot have wildcards in them */
if ( /\*/.test(name) ) {
return next(new Error('Name can not have wildcards'));
}
/** The seconds entry lives
*
* @type Number
* @default this.expire
*/
var expire = self.expire;
if ( typeof options[0] === 'object' ) {
if ( typeof options[0].expire === 'number' || typeof options[0].expire === 'object' || typeof options[0].expire === 'function') {
expire = options[0].expire;
}
}
if ( typeof options[0] === 'number' ) {
expire = options[0];
}
else if ( typeof options[1] === 'number' || typeof options[1] === 'object') {
expire = options[1];
}
var binary = false;
if ( typeof options[0] === 'object' && typeof options[0].binary === 'boolean' ) {
binary = options[0].binary;
}
var expirationPolicy = require('./expire')(expire);
/** attempt to get cache **/
self.get(name, domain.bind(function (error, cache) {
/** If there was an error with cache then call next **/
if ( error ) {
return next();
}
/** if it's cached, display cache **/
if ( cache.length && cache[0].body != null ) {
res.contentType(cache[0].type || "text/html");
res.status(cache[0].status);
if(binary){ //Convert back to binary buffer
res.send(new Buffer(cache[0].body, 'base64'));
}else{
res.send(cache[0].body);
}
}
/** otherwise, cache request **/
else {
/** wrap res.send **/
var send = res.send.bind(res);
res.send = function (body) {
/** send output to HTTP client **/
var ret = send(body);
/** convert binary to base64 string **/
if(binary && typeof body !== 'string'){
body = new Buffer(body).toString('base64');
}
/** save only strings to cache **/
if ( typeof body !== 'string' ) {
return ret;
}
/** Create the new cache **/
self.add(name, body, {
type: this._headers['content-type'],
status: this.statusCode,
expire: expirationPolicy(req, res)
},
domain.intercept(function (name, cache) {}));
return ret;
};
return next();
}
}));
};
});
return middleware;
}
return route;
}) ();