Skip to content

Commit

Permalink
mayonnaise
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Rodriguez committed Jun 2, 2014
1 parent d946af0 commit 44eeb61
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
93 changes: 80 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,85 @@
var mayonnaise = require('mayonnaise');
var Mayonnaise = require('mayonnaise').Mayonnaise
, inherits = require('util').inherits
, dish = require('dish')
, parseUrl = require('url').parse
, parsedUrls = {}

module.exports = function (root, options) {
function Buffet (specs, options) {
if (specs.constructor === Object) {
options = specs;
specs = null;
}
if (!specs) specs = 'public/**/*';
Mayonnaise.call(this, specs, options);
this.on('all', function (op, file) {
switch (op) {
case 'add': case 'update': case 'cleanup':
dish.clearCache(file.fullPath);
break;
}
});
}
inherits(Buffet, Mayonnaise);

Buffet.prototype.middleware = function (options) {
var self = this;
options || (options = {});
var specs = [
{
cwd: root || options.root,
globs: '**/*'
options.defaultContentType = options.defaultContentType || 'application/octet-stream';
if (typeof options.maxAge === 'undefined') options.maxAge = 300;
options.index || (options.index = 'index.html');
options.notFoundPath || (options.notFoundPath = '/404.html');
if (options.notFoundPath[0] !== '/') {
options.notFoundPath = '/' + options.notFoundPath;
}

var mw = function (req, res, next) {
if (req.method.match(/^get|head$/i)) {
// check for static file
if (self.ready) checkCache();
else {
self.once('ready', checkCache);
}
}
else next && next();

function checkCache () {
var urlPath = parsedUrls[req.url];
if (!urlPath) {
urlPath = parseUrl(req.url).pathname;
// Decode and strip nullbytes for security
try {
urlPath = decodeURIComponent(urlPath).replace(/\0/g, '');
}
catch (err) {
if (err.message === 'URI malformed') {
res.writeHead(400);
return res.end();
}
if (!next) throw err;
return next(err);
}
parsedUrls[req.url] = urlPath;
}
var file = self.get(urlPath);
if (file && file.stat.isFile()) return dish.file(file.fullPath, options)(req, res, next);
if (options.index) {
if (urlPath !== '/') urlPath += '/';
urlPath += options.index;
file = self.get(urlPath);
if (file) return file.serve(options)(req, res, next);
}
next && next();
}
];
var mayo = mayonnaise(specs, options);
var middleware = mayo.middleware('static', options);
middleware.notFound = function (req, res, next) {
};
mw.notFound = function (req, res, next) {
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.writeHead(405, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('Method not allowed\n');
}
else {
if (options.notFoundPath) {
var cached = mayo.get(options.notFoundPath);
if (cached) cached.serve({status: 404})(req, res, next);
var cached = self.get(options.notFoundPath);
if (cached) dish.file(cached.fullPath, {status: 404})(req, res, next);
else default404();
}
else default404();
Expand All @@ -30,5 +91,11 @@ module.exports = function (root, options) {
}
}
};
return middleware;
return mw;
};

module.exports = function (specs, options) {
return new Buffet(specs, options).middleware(options);
};

module.exports.Buffet = Buffet;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"dependencies": {
"accesslog": "~0.0.1",
"commander": "~1.0.2",
"mayonnaise": "git+https://gist.github.com/cd931ba95481a7570602.git"
"mayonnaise": "git+https://gist.github.com/cd931ba95481a7570602.git",
"dish": "^1.0.4"
},
"keywords": [
"static",
Expand Down

0 comments on commit 44eeb61

Please sign in to comment.