haste-server/lib/static_handler.js

101 lines
3.3 KiB
JavaScript
Raw Normal View History

2011-11-18 21:51:38 +01:00
var path = require('path');
var fs = require('fs');
var winston = require('winston');
2011-11-22 05:13:46 +01:00
var DocumentHandler = require('./document_handler');
2011-11-18 21:51:38 +01:00
// For serving static assets
2011-11-22 05:00:28 +01:00
var StaticHandler = function(path, cacheAssets) {
2011-11-18 22:00:05 +01:00
this.basePath = path;
2011-11-18 21:51:38 +01:00
this.defaultPath = '/index.html';
2011-11-22 05:00:28 +01:00
this.cacheAssets = cacheAssets;
// Grab the list of available files - and move into hash for quick lookup
var available = fs.readdirSync(this.basePath);
this.availablePaths = {};
for (var i = 0; i < available.length; i++) {
this.availablePaths['/' + available[i]] = true;
}
2011-11-18 21:51:38 +01:00
};
2011-11-22 04:19:43 +01:00
StaticHandler.cache = {};
2011-11-18 21:51:38 +01:00
// Determine the content type for a given extension
StaticHandler.contentTypeFor = function(ext) {
if (ext == '.js') return 'text/javascript';
else if (ext == '.css') return 'text/css';
else if (ext == '.html') return 'text/html';
else if (ext == '.ico') return 'image/ico';
else {
winston.error('unable to determine content type for static asset with extension: ' + ext);
return 'text/plain';
}
};
// Handle a request, and serve back the asset if it exists
2011-11-18 22:00:05 +01:00
StaticHandler.prototype.handle = function(incPath, response) {
2011-11-22 05:13:46 +01:00
// If this is a potential key, show the index - otherwise
// bust out a 404
if (!this.availablePaths[incPath]) {
if (incPath === '/' || DocumentHandler.potentialKey(incPath.substring(1))) {
incPath = this.defaultPath;
}
else {
winston.warn('failed to find static asset', { path: incPath });
response.writeHead(404, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'no such file' }));
return;
}
}
2011-11-22 04:53:50 +01:00
// And then stream the file back - either from the cache or from source
2011-11-22 05:13:46 +01:00
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath);
2011-11-22 05:00:28 +01:00
var cached = this.cacheAssets && this.isCached(filePath);
2011-11-22 05:13:46 +01:00
// Go get'er
2011-11-22 05:00:28 +01:00
var _this = this;
2011-11-22 05:13:46 +01:00
var method = cached ? this.serveCached : this.retrieve;
2011-11-22 04:53:50 +01:00
method(filePath, function(error, content) {
2011-11-22 05:13:46 +01:00
// detect errors
if (error) {
winston.error('unable to read file', { path: filePath, error: error });
response.writeHead(500, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'IO: Unable to read file' }));
// If it was cached, bust the cache
if (cached) {
StaticHandler.cache[filePath] = null;
}
}
2011-11-22 04:53:50 +01:00
// Get the content
2011-11-22 05:13:46 +01:00
else {
2011-11-22 04:53:50 +01:00
var contentType = StaticHandler.contentTypeFor(path.extname(filePath));
response.writeHead(200, { 'content-type': contentType });
response.end(content, 'utf-8');
// Stick it in the cache if its not in there
2011-11-22 05:00:28 +01:00
if (!cached && _this.cacheAssets) {
2011-11-22 04:53:50 +01:00
StaticHandler.cache[filePath] = content;
}
}
});
2011-11-22 04:19:43 +01:00
};
// Retrieve from the file
2011-11-22 04:53:50 +01:00
StaticHandler.prototype.retrieve = function(filePath, callback) {
2011-11-18 22:22:00 +01:00
var _this = this;
2011-11-22 05:00:28 +01:00
winston.verbose('loading static asset', { path: filePath });
fs.readFile(filePath, function(error, content) {
2011-11-22 04:53:50 +01:00
callback(error, content);
2011-11-22 05:13:46 +01:00
});
2011-11-18 21:51:38 +01:00
};
2011-11-22 04:19:43 +01:00
// Retrieve from memory cache
2011-11-22 04:53:50 +01:00
StaticHandler.prototype.serveCached = function(filePath, callback) {
callback(undefined, StaticHandler.cache[filePath]);
2011-11-22 04:19:43 +01:00
};
2011-11-22 04:53:50 +01:00
// Determine if a given filePath is cached or not
StaticHandler.prototype.isCached = function(filePath) {
return !!StaticHandler.cache[filePath];
};
2011-11-22 04:19:43 +01:00
2011-11-18 21:51:38 +01:00
module.exports = StaticHandler;