-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
305 lines (262 loc) · 10.1 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
var urlParse = require('url').parse;
var util = require('util');
var memjs = require('memjs');
module.exports = function(options, Source) {
if (!Source) throw new Error('No source provided');
if (!Source.prototype.get) throw new Error('No get method found on source');
function Caching() { return Source.apply(this, arguments); }
// Inheritance.
util.inherits(Caching, Source);
// References for testing, convenience, post-call overriding.
Caching.memcached = options;
Caching.prototype.get = module.exports.cachingGet('TL', options, Source.prototype.get);
return Caching;
};
module.exports.cachingGet = function(namespace, options, get) {
if (!get) throw new Error('No get function provided');
if (!namespace) throw new Error('No namespace provided');
options = options || {};
options.client = ('client' in options) ? options.client : memjs.Client.create(null, {'keepAlive': true});
options.expires = ('expires' in options) ? options.expires : 300;
options.mode = ('mode' in options) ? options.mode : 'readthrough';
options.logger = ('logger' in options) ? options.logger: console;
if (!options.client) throw new Error('No memcached client');
if (!options.expires) throw new Error('No expires option set');
var caching;
if (options.mode === 'readthrough') {
caching = readthrough;
} else if (options.mode === 'race') {
caching = race;
} else if (options.mode === 'relay') {
caching = relay;
} else {
throw new Error('Invalid value for options.mode ' + options.mode);
}
function race(url, callback) {
var key = namespace + '-' + url;
var source = this;
var client = options.client;
var expires;
if (typeof options.expires === 'number') {
expires = options.expires;
} else {
expires = options.expires[urlParse(url).hostname] || options.expires.default || 300;
}
var sent = false;
var cached = null;
var current = null;
// GET upstream.
get.call(source, url, function(err, buffer, headers) {
current = encode(err, buffer, headers);
if (cached && current) finalize();
if (sent) return;
sent = true;
callback(err, buffer, headers);
});
// GET memcached.
client.get(key, function(err, encoded) {
// If error on memcached, do not flip first flag.
// Finalize will never occur (no cache set).
if (err) {
err.key = key;
return options.logger.log(err);
}
cached = encoded || '500';
if (cached && current) finalize();
if (sent || !encoded) return;
var data;
try {
data = decode(cached);
} catch(e) {
e.key = key;
options.logger.log(e);
cached = '500';
}
if (data) {
sent = true;
callback(data.err, data.buffer, data.headers);
}
});
function finalize() {
if (cached === current) return;
client.set(key, current, function(err) {
if (err) {
err.key = key;
options.logger.log(err);
}
}, expires);
}
}
function readthrough(url, callback) {
var key = namespace + '-' + url;
var source = this;
var client = options.client;
var expires;
if (typeof options.expires === 'number') {
expires = options.expires;
} else {
expires = options.expires[urlParse(url).hostname] || options.expires.default || 300;
}
client.get(key, function(err, encoded) {
// If error on memcached get, pass through to original source
// without attempting a set after retrieval.
if (err) {
err.key = key;
options.logger.log(err);
return get(url, callback);
}
// Cache hit.
var data;
if (encoded) try {
data = decode(encoded);
} catch(e) {
e.key = key;
options.logger.log(e);
}
if (data) return callback(data.err, data.buffer, data.headers);
// Cache miss, error, or otherwise no data
get.call(source, url, function(err, buffer, headers) {
if (err && !errcode(err)) return callback(err);
callback(err, buffer, headers);
// Callback does not need to wait for memcached set to occur.
client.set(key, encode(err, buffer, headers), function(err) {
if (!err) return;
err.key = key;
options.logger.log(err);
}, expires);
});
});
}
function relay(url, callback) {
var key = namespace + '-' + url;
var source = this;
var client = options.client;
var expires;
if (typeof options.expires === 'number') {
expires = options.expires;
} else {
expires = options.expires[urlParse(url).hostname] || options.expires.default || 600;
}
var ttl;
if (options.ttl === undefined) {
ttl = 300;
} else if (typeof options.ttl === 'number') {
ttl = options.ttl;
} else {
ttl = options.ttl[urlParse(url).hostname] || options.ttl.default || 300;
}
client.get(key, function(err, encoded) {
// If error on memcached get, pass through to original source
// without attempting a set after retrieval.
if (err) {
err.key = key;
options.logger.log(err);
return get.call(source,url, callback);
}
// Cache hit.
var data;
if (encoded) try {
data = decode(encoded);
} catch(e) {
e.key = key;
options.logger.log(e);
}
if (data) {
callback(data.err, data.buffer, data.headers);
if (isFresh(data)) return;
// Update cache & bump `expires` header
get.call(source, url, function(err, buffer, headers) {
if (err && !errcode(err)) {
return options.logger.log(err);
}
headers = headers || {};
headers.expires = (new Date(Date.now() + (ttl * 1000))).toUTCString();
client.set(key, encode(err, buffer, headers), function(err) {
if (err) {
err.key = key;
options.logger.log(err);
}
}, expires);
});
} else {
// Cache miss, error, or otherwise no data
get.call(source, url, function(err, buffer, headers) {
if (err && !errcode(err)) return callback(err);
headers = headers || {};
headers.expires = (new Date(Date.now() + (ttl * 1000))).toUTCString();
callback(err, buffer, headers);
client.set(key, encode(err, buffer, headers), function(err) {
if (err) {
err.key = key;
options.logger.log(err);
}
}, expires);
});
}
});
function isFresh(d) {
// When we don't have an expires header just assume staleness
if (d.headers === undefined || !d.headers.expires) return false;
return (+(new Date(d.headers.expires)) > Date.now());
}
}
return caching;
};
module.exports.memjs = memjs;
module.exports.encode = encode;
module.exports.decode = decode;
function errcode(err) {
if (!err) return;
if (err.statusCode === 404) return 404;
if (err.statusCode === 403) return 403;
return;
}
function encode(err, buffer, headers) {
if (errcode(err)) return errcode(err).toString();
// Unhandled error.
if (err) return null;
headers = headers || {};
// Turn objects into JSON string buffers.
if (buffer && typeof buffer === 'object' && !(buffer instanceof Buffer)) {
headers['x-memcached-json'] = true;
buffer = new Buffer(JSON.stringify(buffer));
// Turn strings into buffers.
} else if (buffer && !(buffer instanceof Buffer)) {
buffer = new Buffer(buffer);
}
headers = new Buffer(JSON.stringify(headers), 'utf8');
if (headers.length > 1024) {
throw new Error('Invalid cache value - headers exceed 1024 bytes: ' + JSON.stringify(headers));
}
var padding = new Buffer(1024 - headers.length);
padding.fill(' ');
var len = headers.length + padding.length + buffer.length;
return Buffer.concat([headers, padding, buffer], len);
}
function decode(encoded) {
if (encoded.length == 3) {
encoded = encoded.toString();
if (encoded === '404' || encoded === '403') {
var err = new Error(encoded === '404' ? 'Not found' : 'forbidden');
err.statusCode = parseInt(encoded, 10);
err.memcached = true;
return { err: err };
}
}
// First 1024 bytes reserved for header + padding.
var offset = 1024;
var data = {};
data.headers = encoded.slice(0, offset).toString().trim();
try {
data.headers = JSON.parse(data.headers);
} catch(e) {
throw new Error('Invalid cache value');
}
data.headers['x-memcached'] = 'hit';
data.buffer = encoded.slice(offset);
// Return JSON-encoded objects to true form.
if (data.headers['x-memcached-json']) data.buffer = JSON.parse(data.buffer);
if (data.headers['content-length'] && data.headers['content-length'] != data.buffer.length)
throw new Error('Content length does not match');
return data;
}