forked from SockDrawer/SockSite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.js
124 lines (113 loc) · 3.2 KB
/
cache.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
'use strict';
var async = require('async'),
fs = require('fs'),
path = require('path'),
jsmin = require('jsmin').jsmin,
cssmin = require('cssmin');
var config = require('./config.json'),
database = require('./database');
var cache;
function readall(dir, filter, callback) {
fs.readdir(dir, function (err, files) {
if (err) {
callback(err);
}
var tpl = files.filter(function (file) {
return filter.test(file);
});
async.map(tpl.map(function (file) {
return path.join(dir, file);
}), function (file, next) {
fs.readFile(file, 'binary', next);
}, function (errs, data) {
if (errs) {
return callback(errs);
}
callback(null, tpl.map(function (name, idx) {
return {
name: name,
data: data[idx]
};
}));
});
});
}
function readTemplates(callback) {
var suffix = /[.]html?$/;
readall('templates', suffix, function (err, files) {
if (err) {
return callback(err);
}
var templates = {};
files.forEach(function (file) {
templates[file.name] = file.data;
templates[file.name.replace(suffix, '')] = file.data;
});
callback(null, templates);
});
}
function readScripts(callback) {
readall('static/scripts', /[.]js/, function (err, files) {
if (err) {
return callback(err);
}
files.forEach(function (file) {
file.data = jsmin(file.data);
});
callback(null, files);
});
}
function readStyles(callback) {
readall('static/styles', /[.]css/, function (err, files) {
if (err) {
return callback(err);
}
files.forEach(function (file) {
file.data = cssmin(file.data);
});
callback(null, files);
});
}
exports.buildCache = function buildCache(callback) {
async.parallel({
templates: readTemplates,
scripts: readScripts,
styles: readStyles
}, function (err, results) {
if (err) {
return callback(err);
}
exports.templates = results.templates;
exports.scripts = results.scripts;
exports.styles = results.styles;
callback();
});
};
exports.templates = {};
exports.scripts = {};
exports.styles = {};
function setData(data) {
cache.push(data);
var limit = Date.now() - (config.dataPeriod * 1000),
minimum = config.minimumEntries || 10,
old = cache;
cache = cache.filter(function (d) {
return d.checkedAt > limit;
});
if (cache.length < minimum) {
cache = old.slice(Math.max(old.length - minimum, 0));
}
cache.dataPeriod = config.dataPeriod;
exports.summary = database.summarizeData(cache);
}
database.registerListener(setData);
database.getRecentChecks(config.dataPeriod, function (err, data) {
if (err) {
/*eslint-disable no-console */
return console.warn(err);
/*eslint-enable no-console */
}
cache = data;
exports.summary = database.summarizeData(cache);
});
exports.summary = {};