mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-01 03:21:21 +00:00
cache static assets in memory
This commit is contained in:
parent
e4da28de8a
commit
83cb68ada2
2 changed files with 26 additions and 2 deletions
6
TODO
6
TODO
|
@ -1,10 +1,12 @@
|
||||||
cache headers for static assets (and on document GET)
|
cache headers for static assets (and on document GET)
|
||||||
tests
|
tests
|
||||||
add FAVICON (or force 404)
|
add FAVICON (or force 404)
|
||||||
cache static in memory
|
|
||||||
add feedback for errors to UI - esp. too long
|
add feedback for errors to UI - esp. too long
|
||||||
copy URL to clipboard button
|
|
||||||
add about page
|
add about page
|
||||||
|
make asset caching optional
|
||||||
|
todo store buffer instead of string while grabbing contents (if possible)
|
||||||
|
write cache to use callback instead of using non-DRY code
|
||||||
|
|
||||||
# shared version only
|
# shared version only
|
||||||
some way to do announcements easily (and use for ads)
|
some way to do announcements easily (and use for ads)
|
||||||
|
copy URL to clipboard button
|
||||||
|
|
|
@ -16,6 +16,8 @@ var StaticHandler = function(path) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
StaticHandler.cache = {};
|
||||||
|
|
||||||
// Determine the content type for a given extension
|
// Determine the content type for a given extension
|
||||||
StaticHandler.contentTypeFor = function(ext) {
|
StaticHandler.contentTypeFor = function(ext) {
|
||||||
if (ext == '.js') return 'text/javascript';
|
if (ext == '.js') return 'text/javascript';
|
||||||
|
@ -34,6 +36,16 @@ StaticHandler.prototype.handle = function(incPath, response) {
|
||||||
if (!this.availablePaths[incPath]) incPath = this.defaultPath;
|
if (!this.availablePaths[incPath]) incPath = this.defaultPath;
|
||||||
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath);
|
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath);
|
||||||
// And then stream the file back
|
// And then stream the file back
|
||||||
|
if (StaticHandler.cache[filePath]) {
|
||||||
|
this.serveCached(filePath, response);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.retrieve(filePath, response);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Retrieve from the file
|
||||||
|
StaticHandler.prototype.retrieve = function(filePath, response) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
fs.readFile(filePath, function(error, content) {
|
fs.readFile(filePath, function(error, content) {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -45,8 +57,18 @@ StaticHandler.prototype.handle = function(incPath, response) {
|
||||||
var contentType = StaticHandler.contentTypeFor(path.extname(filePath));
|
var contentType = StaticHandler.contentTypeFor(path.extname(filePath));
|
||||||
response.writeHead(200, { 'content-type': contentType });
|
response.writeHead(200, { 'content-type': contentType });
|
||||||
response.end(content, 'utf-8');
|
response.end(content, 'utf-8');
|
||||||
|
// Stick it in the cache
|
||||||
|
StaticHandler.cache[filePath] = content;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Retrieve from memory cache
|
||||||
|
StaticHandler.prototype.serveCached = function(filePath, response) {
|
||||||
|
var contentType = StaticHandler.contentTypeFor(path.extname(filePath));
|
||||||
|
response.writeHead(200, { 'content-type': contentType });
|
||||||
|
response.end(StaticHandler.cache[filePath], 'utf-8');
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = StaticHandler;
|
module.exports = StaticHandler;
|
||||||
|
|
Loading…
Reference in a new issue