-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
575 lines (445 loc) · 12 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
'use strict';
/**
* Module dependencies
*/
var noop = function () {};
var fs = require("fs");
var fsp = require("fs-promise");
var crypto = require('crypto');
var path = require('path');
var async = require('async');
var extend = require('extend');
var uuid = require('uuid');
var zlib = require('zlib');
const gzip = zlib.createGzip();
/**
* Export 'DiskStore'
*/
module.exports = {
create : function (args) {
return new DiskStore(args && args.options ? args.options : args);
}
};
/**
* Helper function that revives buffers from object representation on JSON.parse
*/
function bufferReviver(k, v) {
if (
v !== null &&
typeof v === 'object' &&
'type' in v &&
v.type === 'Buffer' &&
'data' in v &&
Array.isArray(v.data)) {
return new Buffer(v.data);
}
return v;
}
/**
* helper object with meta-informations about the cached data
*/
function MetaData () {
// the key for the storing
this.key = null;
// data to store
this.value = null;
// temporary filename for the cached file because filenames cannot represend urls completely
this.filename = null;
// expirydate of the entry
this.expires = null;
// size of the current entry
this.size = null;
}
/**
* construction of the disk storage
*/
function DiskStore (options) {
options = options || {};
this.options = extend({
path: 'cache/',
ttl: 60,
maxsize: 0,
zip: false
}, options);
// check storage directory for existence (or create it)
if (!fs.existsSync(this.options.path)) {
fs.mkdirSync(this.options.path);
}
this.name = 'diskstore';
// current size of the cache
this.currentsize = 0;
// internal array for informations about the cached files - resists in memory
this.collection = {};
// fill the cache on startup with already existing files
if (!options.preventfill) {
this.intializefill(options.fillcallback);
}
}
/**
* indicate, whether a key is cacheable
*/
DiskStore.prototype.isCacheableValue = function (value) {
return value !== null && value !== undefined;
};
/**
* delete an entry from the cache
*/
DiskStore.prototype.del = function (key, options, cb) {
if (typeof options === 'function') {
cb = options;
options = null;
}
cb = typeof cb === 'function' ? cb : noop;
// get the metainformations for the key
var metaData = this.collection[key];
if (!metaData) {
return cb(null);
}
// check if the filename is set
if (!metaData.filename) {
return cb(null);
}
// check for existance of the file
fsp.exists(metaData.filename).
then(function(exists) {
if (exists) {
return;
}
reject();
})
.then(function() {
// delete the file
return fsp.unlink(metaData.filename);
}, function() {
// not found
cb(null);
}).then(function() {
// update internal properties
this.currentsize -= metaData.size;
this.collection[key] = null;
delete this.collection[key];
cb(null);
}.bind(this)).catch(function(err) {
cb(null);
});
};
/**
* zip an input string if options want that
*/
DiskStore.prototype.zipIfNeeded = function(data, cb)
{
if (this.options.zip)
{
zlib.deflate(data, function(err, buffer) {
if (!err) {
cb(null, buffer);
}
else
{
cb(err, null);
}
});
}
else
{
cb(null, data);
}
}
/**
*unpzip an input string if options want that
*/
DiskStore.prototype.unzipIfNeeded = function (data, cb) {
if (this.options.zip) {
zlib.unzip(data, function (err, buffer) {
if (!err) {
cb(null, buffer);
}
else {
cb(err, null);
}
});
}
else {
cb(null, data);
}
}
/**
* set a key into the cache
*/
DiskStore.prototype.set = function (key, val, options, cb) {
cb = typeof cb === 'function' ? cb : noop;
if (typeof options === 'function') {
cb = options;
options = null;
}
// get ttl
var ttl = (options && (options.ttl || options.ttl === 0)) ? options.ttl : this.options.ttl;
var metaData = extend({}, new MetaData(), {
key: key,
value: val,
expires: Date.now() + ((ttl || 60) * 1000),
filename: this.options.path + '/cache_' + uuid.v4() + '.dat'
});
var stream = JSON.stringify(metaData);
metaData.size = stream.length;
if (this.options.maxsize && metaData.size > this.options.maxsize) {
return cb('Item size too big.');
}
// remove the key from the cache (if it already existed, this updates also the current size of the store)
this.del(key, function (err) {
if (err) {
return cb(err);
}
// check used space and remove entries if we use to much space
this.freeupspace(function () {
try {
this.zipIfNeeded(stream, function(err, processedStream) {
// write data into the cache-file
fs.writeFile(metaData.filename, processedStream, function (err) {
if (err) {
return cb(err);
}
// remove data value from memory
metaData.value = null;
delete metaData.value;
this.currentsize += metaData.size;
// place element with metainfos in internal collection
this.collection[metaData.key] = metaData;
return cb(null, val);
}.bind(this));
}.bind(this));
} catch(err) {
return cb(err);
}
}.bind(this));
}.bind(this));
};
/**
* helper method to free up space in the cache (regarding the given spacelimit)
*/
DiskStore.prototype.freeupspace = function (cb) {
cb = typeof cb === 'function' ? cb : noop;
if (!this.options.maxsize) {
return cb(null);
}
// do we use to much space? then cleanup first the expired elements
if (this.currentsize > this.options.maxsize) {
this.cleanExpired();
}
// when the spaceusage is to high, remove the oldest entries until we gain enough diskspace
if (this.currentsize <= this.options.maxsize) {
return cb(null);
}
// for this we need a sorted list basend on the expire date of the entries (descending)
var tuples = [], key;
for (key in this.collection) {
tuples.push([key, this.collection[key].expires]);
}
tuples.sort(function sort (a, b) {
a = a[1];
b = b[1];
return a < b ? 1 : (a > b ? -1 : 0);
});
return this.freeupspacehelper(tuples, cb);
};
/**
* freeup helper for asnyc space freeup
*/
DiskStore.prototype.freeupspacehelper = function (tuples, cb) {
// check, if we have any entry to process
if (tuples.length === 0) {
return cb(null);
}
// get an entry from the list
var tuple = tuples.pop();
var key = tuple[0];
// delete an entry from the store
this.del(key, function deleted (err) {
// return when an error occures
if (err) {
return cb(err);
}
// stop processing when enouth space has been cleaned up
if (this.currentsize <= this.options.maxsize) {
return cb(err);
}
// ok - we need to free up more space
return this.freeupspacehelper(tuples, cb);
}.bind(this));
};
/**
* get entry from the cache
*/
DiskStore.prototype.get = function (key, options, cb) {
if (typeof options === 'function') {
cb = options;
}
cb = typeof cb === 'function' ? cb : noop;
// get the metadata from the collection
var data = this.collection[key];
if (!data) {
// not found
return cb(null, null);
}
// found but expired
if (data.expires < new Date()) {
// delete the elemente from the store
this.del(key, function (err) {
return cb(err, null);
});
} else {
// try to read the file
try {
fs.readFile(data.filename, function (err, fileContent) {
if (err) {
return cb(err);
}
this.unzipIfNeeded(fileContent, function(err, decompressedContent) {
if (err) {
return cb(err);
}
var diskdata;
if(this.options.reviveBuffers) {
diskdata = JSON.parse(decompressedContent, bufferReviver);
} else {
diskdata = JSON.parse(decompressedContent);
}
cb(null, diskdata.value);
}.bind(this));
}.bind(this));
} catch(err) {
cb(err);
}
}
};
/**
* get keys stored in cache
* @param {Function} cb
*/
DiskStore.prototype.keys = function (cb) {
cb = typeof cb === 'function' ? cb : noop;
var keys = Object.keys(this.collection);
cb(null, keys);
};
/**
* cleanup cache on disk -> delete all used files from the cache
*/
DiskStore.prototype.reset = function (key, cb) {
cb = typeof cb === 'function' ? cb : noop;
if (typeof key === 'function') {
cb = key;
key = null;
}
if (Object.keys(this.collection).length === 0) {
return cb(null);
}
try {
// delete special key
if (key !== null) {
this.del(key);
return cb(null);
}
async.eachSeries(this.collection,
function (elementKey, callback) {
this.del(elementKey.key, callback);
}.bind(this),
function (err) {
cb(null);
}
);
} catch(err) {
return cb(err);
}
};
/**
* helper method to clean all expired files
*/
DiskStore.prototype.cleanExpired = function () {
var key, entry;
for (key in this.collection) {
entry = this.collection[key];
if (entry.expires < new Date()) {
this.del(entry.key);
}
}
}
/**
* clean the complete cache and all(!) files in the cache directory
*/
DiskStore.prototype.cleancache = function (cb) {
cb = typeof cb === 'function' ? cb : noop;
// clean all current used files
this.reset();
// check, if other files still resist in the cache and clean them, too
var files = fs.readdirSync(this.options.path);
files
.map(function (file) {
return path.join(this.options.path, file);
}.bind(this))
.filter(function (file) {
return fs.statSync(file).isFile();
}.bind(this))
.forEach(function (file) {
fs.unlinkSync(file);
}.bind(this));
cb(null);
};
/**
* fill the cache from the cache directory (usefull e.g. on server/service restart)
*/
DiskStore.prototype.intializefill = function (cb) {
cb = typeof cb === 'function' ? cb : noop;
// get the current working directory
fs.readdir(this.options.path, function (err, files) {
// get potential files from disk
files = files.map(function (filename) {
return path.join(this.options.path, filename);
}.bind(this)).filter(function (filename) {
return fs.statSync(filename).isFile();
}).filter(function (filename) {
var re = /^cache_[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}.dat$/i
return re.test(path.basename(filename));
});
// use async to process the files and send a callback after completion
async.eachSeries(files, function (filename, callback) {
fs.readFile(filename, function (err, data) {
// stop file processing when there was an reading error
if (err) {
return callback();
}
this.unzipIfNeeded(data, function(err, unzippedData) {
try {
// get the json out of the data
var diskdata = JSON.parse(unzippedData);
} catch(err) {
// when the deserialize doesn't work, probably the file is uncomplete - so we delete it and ignore the error
try {
fs.unlinkSync(filename);
} catch(ignore) {
}
return callback();
}
// update the size in the metadata - this value isn't correctly stored in the file
diskdata.size = data.length;
// update collection size
this.currentsize+=data.length;
// remove the entrys content - we don't want the content in the memory (only the meta informations)
diskdata.value = null;
delete diskdata.value;
// and put the entry in the store
this.collection[diskdata.key] = diskdata;
// check for expiry - in this case we instantly delete the entry
if (diskdata.expires < new Date()) {
this.del(diskdata.key, function () {
return callback();
});
} else {
return callback();
}
}.bind(this));
}.bind(this));
}.bind(this), function (err) {
cb(err || null);
});
}.bind(this));
};