Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --cache and --cache-filter arguments for caching. #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 60 additions & 12 deletions lib/middlewares/route.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,86 @@
'use strict';

const pathFn = require('path');
const mime = require('mime');
// arguments:
// `--cache [bytes]` enables the cache, the optinal bytes are the size of the cache (default `10485760` 10MB)
// `--cache-filter [regex]` set a filter of which files should be cached (default `\.(css|js)$` for .css an .js)

var pathFn = require('path');
var mime = require('mime');
var LRU = require('lru-cache');
var stream = require('stream');

module.exports = function(app) {
const { config, route } = this;
const { args = {} } = this.env;
const { root } = config;
var config = this.config;
var args = this.env.args || {};
var root = config.root;
var route = this.route;

if (args.s || args.static) return;

app.use(root, (req, res, next) => {
const { method } = req;
var cache = new LRU({
max: (args.cache === true) ? 10485760 : args.cache || 0,
length: function (value, key) {
return value.byteLength;
}
});
var cacheFilterRegExp = new RegExp(args.cacheFilter || '\.(css|js)$');

// Reset cache if source-files are modified
this.addListener('generateAfter', function() {
cache.reset();
});

app.use(root, function(req, res, next) {
var method = req.method;
if (method !== 'GET' && method !== 'HEAD') return next();

let url = route.format(decodeURIComponent(req.url));
const data = route.get(url);
const extname = pathFn.extname(url);
var url = route.format(decodeURIComponent(req.url));
var data;
if (args.cache && cache.has(url)) {
data = cache.get(url);
} else {
data = route.get(url);
}
var extname = pathFn.extname(url);

// When the URL is `foo/index.html` but users access `foo`, redirect to `foo/`.
if (!data) {
if (extname) return next();

url = encodeURI(url);
res.statusCode = 302;
res.setHeader('Location', `${root + url}/`);
res.setHeader('Location', root + url + '/');
res.end('Redirecting');
return;
}

res.setHeader('Content-Type', extname ? mime.lookup(extname) : 'application/octet-stream');

if (method === 'GET') {
data.pipe(res).on('error', next);
if (args.cache && url.search(cacheFilterRegExp) !== -1) {
if (cache.has(url)) {
// load from cache
var cacheStream = new stream.Readable();
cacheStream.push(cache.get(url));
cacheStream.push(null);
cacheStream.pipe(res).on('error', next);
} else {
// save to cache
var cacheStream = stream.PassThrough();
cacheStream.on('data', function(data) {
if (!cache.has(url)) {
cache.set(url, Buffer.from(data));
} else {
cache.set(url, cache.get(url).data.concat(data) );
}
});

data.pipe(cacheStream).pipe(res).on('error', next);
}
} else {
// stream from hexo
data.pipe(res).on('error', next);
}
} else {
res.end();
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"chalk": "^2.4.1",
"compression": "^1.7.3",
"connect": "^3.6.6",
"lru-cache": "^5.1.1",
"mime": "^1.6.0",
"morgan": "^1.9.0",
"opn": "^5.3.0",
Expand Down