-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.js
More file actions
307 lines (272 loc) · 6.51 KB
/
route.js
File metadata and controls
307 lines (272 loc) · 6.51 KB
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
'use strict';
const
path = require("path"),
fs = require("fs"),
mime = require("mime"),
pathToRe = require("path-to-regexp"),
serveStatic = require("serve-static");
const
log = requireRoot("./log");
function fspromise(f) {
return function(...args) {
return new Promise((ok, no) =>
f.call(fs, ...args, (err, val) => err? no(err) : ok(val))
);
}
}
/**
* TODO: At the time of writing, fs.promises is "experimental"
**/
const
fsreaddir = fspromise(fs.readdir),
fsstat = fspromise(fs.stat),
fsreadFile = fspromise(fs.readFile),
fswriteFile = fspromise(fs.writeFile);
function fsexists(f) {
return new Promise((ok, no) => fs.access(f, err => ok(!err)))
}
class VirtualStats {
constructor(data) {
if(typeof data === 'string') {
data = {name: data};
}
if(typeof data.type === 'undefined') {
if(data.name.endsWith('/')) {
this.name = data.name.slice(0, -1);
this.type = 'dir';
}
else {
this.name = data.name;
this.type = 'file';
}
}
else {
if(data.name.endsWith("/")) {
this.name = data.name.slice(0, -1);
}
else {
this.name = data.name;
}
this.type = data.type;
}
this.note = data.note || null;
this.atime = data.atime || null;
this.mtime = data.mtime || null;
this.ctime = data.ctime || null;
this.size = data.size;
}
isDirectory() {
return this.type === 'dir';
}
isFile() {
return this.type === 'file';
}
}
// Combine public-optimized and public as one fs
async function collateReaddir(d) {
let
od = path.join("public-optimized", d),
pd = path.join("public", d);
let [opt, pub] = await Promise.all([
fsreaddir(od).catch(() => []), fsreaddir(pd).catch(() => [])
]);
let dir = Array.from(new Set([...opt, ...pub]));
for(let i = 0; i < dir.length; ++i) {
let di = dir[i];
// Prioritize public-optimized
dir[i] = fsstat(path.join(
opt.indexOf(di) === -1? pd : od, di
)).then(v => {
v.name = di;
v.type = v.isDirectory()? 'dir' : 'file';
v.mime = mime.lookup(di);
return v;
});
}
return await Promise.all(dir)
}
let dirs = [], handlers = [];
module.exports = {
readdir(f) {
for(let i = 0; i < dirs.length; ++i) {
if(dirs[i](f)) {
return handlers[i];
}
}
return async function(req) {
return null;
}
},
/**
* Ensure that this route never ends with a trailing slash
**/
leaf(handler) {
return function(req, res, next) {
if(req.originalUrl.endsWith("/")) {
res.redirect(req.originalUrl.slice(0, -1));
}
else {
handler(req, res, next);
}
}
},
dir(path, ls) {
if(typeof ls !== 'function') {
let files = ls;
ls = async () => files;
}
if(typeof path === 'string') {
path = pathToRe(path);
}
if(typeof path !== 'function') {
let p = path;
path = v => {
return p.test(v);
}
}
dirs.push(path);
async function readdir(target, req) {
// If ls doesn't return an array, assume the route is bad
let vls = await ls(req);
if(vls === null) {
return null;
}
const
real = await collateReaddir(target),
virt = vls.map(v => new VirtualStats(v)),
files = real,
names = files.map(v => v.name);
// Combine them, prioritizing real files over virtual
for(let i = 0; i < virt.length; ++i) {
let vi = virt[i];
if(names.indexOf(vi.name) === -1) {
files.push(vi);
}
}
return files.map(v => {
function normalizeTime(t) {
if(typeof t === 'number') {
return t;
}
else if(t) {
return t.getTime();
}
else {
return null;
}
}
return {
name: v.name,
mime: v.mime || null,
mtime: normalizeTime(v.mtime),
ctime: normalizeTime(v.ctime),
atime: normalizeTime(v.atime),
size: v.size,
note: v.note || null,
type: v.type || (v.isDirectory()? 'dir' : 'file')
};
});
}
handlers.push(readdir);
return async function(req, res, next) {
if(!path(req.originalUrl)) {
return next();
}
// Handle the trailing slash policy
if(!req.originalUrl.endsWith('/')) {
return res.redirect(req.originalUrl + "/");
}
let files = await readdir(req.originalUrl, req);
if(files === null) {
return res.status(404).end("Not Found");
}
if(req.accepts('html')) {
//Separate dirs and files to be listed separately
const dl = [], fl = [];
for(let f of files) {
if(f.type === 'dir') {
dl.push(f);
}
else if(f.type === 'file') {
fl.push(f);
}
else {
throw new Error("Invalid file type " + f.type);
}
}
dl.sort((a, b) => a.name.localeCompare(b.name));
fl.sort((a, b) => a.name.localeCompare(b.name));
res.render('ls', {dl, fl});
}
// TODO: We'll make this better later
else if(req.accepts('json')) {
res.json(files);
}
else {
res.status(406).end();
}
}
},
cache(out, gen, inp_or_dirty=async ()=>true) {
let origout = out;
if(typeof out === 'string') {
let fn = out;
out = req => fn;
}
if(typeof inp_or_dirty === 'string') {
let inp = inp_or_dirty;
var dirty = async update => {
return !await fsexists(origout) ||
(await fsstat(inp)).mtime > update;
}
var generate = async function(req) {
return await gen(req, await fsreadFile(inp));
}
}
else {
var dirty = inp_or_dirty, generate = gen;
}
// Updates needs to be a dictionary to allow cache to handle
// more than one route. It also allows us to keep track of
// whether or not we've checked the cache.
let start = Date.now(), updates = {};
return async function(req, res, next) {
if(req.originalUrl.endsWith("/")) {
return res.redirect(req.originalUrl.slice(0, -1));
}
let nt = Date.now(), first = false;
if(req.originalUrl in updates) {
var update = updates[req.originalUrl];
}
else {
var update = updates[req.originalUrl] = start;
first = true;
}
// Only even consider checking if the cache is dirty
// when it's been longer than 10 secs
if(first || nt - update > 10000) {
if(await dirty(req, update)) {
log.info("Caching", req.originalUrl);
updates[req.originalUrl] = nt;
let data = await generate(req), outf = out(req);
await fswriteFile(outf, data);
// We already have the data loaded, so send it
// directly rather than using sendFile
return res.type(path.extname(outf)).send(data);
}
}
res.sendFile(path.resolve(out(req)));
}
},
static(f) {
let ss = serveStatic(f);
return function(req, res, next) {
if(req.path.endsWith("/")) {
res.redirect(req.originalUrl.slice(0, -1));
}
else {
ss(req, res, next);
}
}
}
};