static.js
1.62 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
'use strict';
const range = require('koa-range');
const compose = require('koa-compose');
const staticCache = require('koa-static-cache');
const assert = require('assert');
const mkdirp = require('mkdirp');
const LRU = require('ylru');
const is = require('is-type-of');
module.exports = (options, app) => {
let dirs = options.dir;
if (!is.array(dirs)) dirs = [ dirs ];
const prefixs = [];
function rangeMiddleware(ctx, next) {
// if match static file, and use range middleware.
const isMatch = prefixs.some(p => ctx.path.startsWith(p));
if (isMatch) {
return range(ctx, next);
}
return next();
}
const middlewares = [ rangeMiddleware ];
for (const dirObj of dirs) {
assert(is.object(dirObj) || is.string(dirObj), '`config.static.dir` must be `string | Array<string|object>`.');
let newOptions;
if (is.string(dirObj)) {
// copy origin options to new options ensure the safety of objects
newOptions = Object.assign({}, options, { dir: dirObj });
} else {
assert(is.string(dirObj.dir), '`config.static.dir` should contains `[].dir` property when object style.');
newOptions = Object.assign({}, options, dirObj);
}
if (newOptions.dynamic && !newOptions.files) {
newOptions.files = new LRU(newOptions.maxFiles);
}
if (newOptions.prefix) {
prefixs.push(newOptions.prefix);
}
// ensure directory exists
mkdirp.sync(newOptions.dir);
app.loggers.coreLogger.info('[egg-static] starting static serve %s -> %s', newOptions.prefix, newOptions.dir);
middlewares.push(staticCache(newOptions));
}
return compose(middlewares);
};