Skip to content

Commit

Permalink
Converted the gzippoCache object into a replaceable Object with a sim…
Browse files Browse the repository at this point in the history
…ple get/set API for future extension (such as a MongoDB driver)
  • Loading branch information
David Ellis committed Jul 21, 2011
1 parent 270ac48 commit ba41838
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions lib/staticGzip.js
Expand Up @@ -36,10 +36,21 @@ var removeContentHeaders = function(res){
};

/**
* gzipped cache.
* gzipped in-memory cache.
*/

var gzippoCache = {};
var gzippoCache = (function() {
var cache = {};
this.get = function(pathname) {
return cache[pathname];
};
this.set = function(pathname, cacheObj) {
var oldCache = typeof(cache[pathname]) == "object" ? cache[pathname] : undefined;
cache[pathname] = cacheObj;
return oldCache;
};
return this;
})();

/**
* gzip file.
Expand All @@ -65,6 +76,7 @@ var gzippo = function(filename, charset, callback) {
* - `clientMaxAge` client cache-control max-age directive, defaulting to 1 week
* - `matchType` - A regular expression tested against the Content-Type header to determine whether the response
* should be gzipped or not. The default value is `/text|javascript|json/`.
* - `cacheStore` - An object that implements a simple get/set API for storing/retrieving cached data, defaulting to a built-in in-memory store
*
* Examples:
*
Expand All @@ -87,6 +99,7 @@ exports = module.exports = function staticGzip(dirPath, options){
var maxAge = options.maxAge || 86400000,
contentTypeMatch = options.contentTypeMatch || /text|javascript|json/,
clientMaxAge = options.clientMaxAge || 604800000;
cacheStore = options.cacheStore || gzippoCache;

if (!dirPath) throw new Error('You need to provide the directory to your static content.');
if (!contentTypeMatch.test) throw new Error('contentTypeMatch: must be a regular expression.');
Expand Down Expand Up @@ -119,12 +132,13 @@ exports = module.exports = function staticGzip(dirPath, options){

function gzipAndSend(filename, gzipName, mtime) {
gzippo(filename, charset, function(gzippedData) {
gzippoCache[gzipName] = {
var cacheObj = {
'ctime': Date.now(),
'mtime': mtime,
'content': gzippedData
};
sendGzipped(gzippoCache[gzipName]);
cacheStore.set(gzipName, cacheObj);
sendGzipped(cacheObj);
});
}

Expand Down Expand Up @@ -159,26 +173,26 @@ exports = module.exports = function staticGzip(dirPath, options){

var base = path.basename(filename),
dir = path.dirname(filename),
gzipName = path.join(dir, base + '.gz');

gzipName = path.join(dir, base + '.gz'),
cacheObj = cacheStore.get(gzipName);
if (req.headers['if-modified-since'] &&
gzippoCache[gzipName] &&
+gzippoCache[gzipName].mtime <= new Date(req.headers['if-modified-since']).getTime()) {
setHeaders(gzippoCache[gzipName]);
cacheObj &&
+cacheObj.mtime <= new Date(req.headers['if-modified-since']).getTime()) {
setHeaders(cacheObj);
removeContentHeaders(res);
res.statusCode = 304;
return res.end();
}

//TODO: check for pre-compressed file
if (typeof gzippoCache[gzipName] === 'undefined') {
if (typeof cacheObj === 'undefined') {
gzipAndSend(filename, gzipName, stat.mtime);
} else {
if ((gzippoCache[gzipName].mtime < stat.mtime) ||
((gzippoCache[gzipName].ctime + maxAge) < Date.now())) {
if ((cacheObj.mtime < stat.mtime) ||
((cacheObj.ctime + maxAge) < Date.now())) {
gzipAndSend(filename, gzipName, stat.mtime);
} else {
sendGzipped(gzippoCache[gzipName]);
sendGzipped(cacheObj);
}
}
});
Expand Down

0 comments on commit ba41838

Please sign in to comment.