This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (172 loc) · 5.6 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
var fs = require("fs");
var path = require("path");
var MD5 = require("md5-file");
var async = require("async");
var mime = require("mime");
var debug = require("debug")("express-blinker");
function getTimedDateString(target) {
var now = (typeof target != "undefined" ?
target+(Date.now()) : Date.now()
);
var inst = new Date(now);
var str = inst.toUTCString();
return str;
}
function checkLastModified(data, cb) {
// Skip!
if(data._changed) return cb(null, data);
if("if-modified-since" in data.req.headers) {
fs.stat(data.file, function(err, stats) {
if(err) return cb(err, data);
data.stats = stats;
var req_mtime = new Date(data.req.headers["if-none-match"]).getTime();
var file_mtime = new Date(stats.mtime).getTime();
if(file_mtime > req_mtime) {
// Instruct the engine to serve the file.
data._changed = true;
data.status = 200;
} else {
// The file is unchanged. Serve a 304.
data.status = 304;
}
cb(null, data);
});
} else {
return cb(null, data);
}
}
function checkETag(data, cb) {
// Skip!
if(data._changed) return cb(null, data);
// Do our thing.
MD5(data.file, function(err, msg){
if(err) return cb(err, data);
data.hash = msg;
if(data.req.headers["if-none-match"] != msg) {
// The entity has been changed.
data._changed = true;
data.status = 200;
} else {
// Stuff is as it was before.
data._changed = false;
data.status = 304;
}
cb(null, data);
});
}
function checkSanity(data, cb) {
// This is useful if both content checkers are off.
if(!data.options.etag && !data.options.lastModified) {
data._changed = true;
data.status = 200;
}
cb(null, data);
}
function makeExpires(data, cb) {
data.headers.push(["Expires", getTimedDateString(data.options.age)]);
cb(null, data);
}
function makeCacheControl(data, cb) {
var parts = [];
parts.push(data.options.cacheKeywords || "public");
parts.push("max-age="+data.options.age);
data.headers.push(["Cache-Control", parts.join(", ")]);
cb(null, data);
}
// More risky
function makeLastModified(data, cb) {
var stats = data.stats;
var dateStr = (new Date(stats.mtime)).toUTCString();
data.headers.push(["Last-Modified", dateStr]);
cb(null, data);
}
function makeETag(data, cb) {
data.headers.push(["ETag", data.hash]);
cb(null, data);
}
/* [
{
test: /\.(png|jp?g)$/,
age: "10 days",
expires: true,
cacheControl: true,
cacheKeywords: "public",
lastModified: false,
etag: false
}
] */
module.exports = function Blinker(basePath, confs) {
return function(req, res, next) {
// Search for a fitting set of options...
var options;
for(var k in confs) {
var segment = confs[k];
if(segment.test.test(req.url)) {
// Aha! This is the one. Save, break, continue.
options = segment;
break;
}
}
var filePath = basePath + req.url;
async.series({
fileExists: function fileExists(cb) {
fs.exists(filePath, function(exists){
if(exists) cb(null, exists);
else cb(new Error(filePath+" does not exist."));
});
},
isFile: function isFile(cb) {
fs.stat(filePath, function(err, stats){
if(stats.isFile()) cb(null, true);
else cb(new Error("Not a file."));
});
}
}, function(err){
if(err) {
debug(err);
next();
} else {
var wave = [];
// Add checks
if(options.lastModified) wave.push(checkLastModified);
if(options.etag) wave.push(checkETag);
wave.push(checkSanity);
// Add makers
if(options.expires)
wave.push(makeExpires);
if(options.cacheControl)
wave.push(makeCacheControl);
if(options.lastModified)
wave.push(makeLastModified);
if(options.etag)
wave.push(makeETag);
var runner = async.seq.apply(async.seq, wave);
var data = {
res: res,
req: req,
file: basePath + req.url,
options: options,
headers: [],
_changed: false,
status: 200
};
// Execute the wave and return.
runner(data, function(err, result){
if(err) return next(err);
if(result._changed) {
// Serve the file
for(var rk in result.headers) {
var pair = result.headers[rk];
res.setHeader(pair[0], pair[1]);
}
res.setHeader("Content-Type", mime.lookup(result.file));
res.status(result.status);
fs.createReadStream(result.file).pipe(res);
} else {
res.status(result.status).end();
}
});
}
});
}
}