Skip to content

Commit

Permalink
Added ETag and Last-Modified support
Browse files Browse the repository at this point in the history
  • Loading branch information
mekwall committed Sep 12, 2011
1 parent 1f86102 commit 3a19182
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
87 changes: 56 additions & 31 deletions lib/resmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,65 @@ module.exports.middleware = function(backend, dirPath, options) {
contentType = mime.lookup(filename);
charset = mime.charsets.lookup(contentType);
acceptEncoding = req.headers['accept-encoding'] || '';

function pass(name) {
var o = Object.create(options);
o.path = name;
staticSend(req, res, next, o);
}

function sendGzipped(data) {
contentType = contentType + (charset ? '; charset=' + charset : '');
res.setHeader('Content-Type', contentType);
res.setHeader('Content-Encoding', 'gzip');
res.setHeader('Vary', 'Accept-Encoding');
res.setHeader('Content-Length', data.length);
res.end(data, 'binary');
}

function gzipAndSend(filename, gzipName, mtime) {
gzipFile(filename, charset, function(gzippedData) {
gzipFileCache[gzipName] = {
'ctime': Date.now(),
'mtime': mtime,
'content': gzippedData
};
sendGzipped(gzippedData);
});
}

if (filename.substring(filename.length-3) === '.gz') {
contentType = mime.lookup(filename.substring(0, filename.length-3));
charset = mime.charsets.lookup(contentType);
fs.readFile(filename, function (err, data) {
if (err) throw err;
sendGzipped(data);
});
fs.stat(filename, function(err, stats) {
if (err || !stats.isFile()) throw err;

// create etag and compare
var etag = '"' + stats.ino + '-' + stats.size + '-' + Date.parse(stats.mtime) + '"';
var ifMatch = req.headers['if-match'] || req.headers['if-none-match'];
if (ifMatch == etag) {
res.send(304);
return;
}

// check modified since
var ifModifiedSince = req.headers['if-modified-since'];
if (ifModifiedSince) {
var req_date = new Date(ifModifiedSince);
if (stats.mtime <= req_date && req_date <= Date.now()) {
res.send(304);
return;
}
}

// no etag/modified since or file have been modified
res.setHeader('ETag', etag);
res.setHeader('Last-Modified', stats.mtime);
fs.readFile(filename, function (err, data) {
if (err) throw err;
sendGzipped(data);
});
});
} else {

if (filename.substring(filename.length-5) === '.less') {
Expand All @@ -265,33 +316,7 @@ module.exports.middleware = function(backend, dirPath, options) {

if (!~acceptEncoding.indexOf('gzip'))
return pass(filename);

function pass(name) {
var o = Object.create(options);
o.path = name;
staticSend(req, res, next, o);
}

function sendGzipped(data) {
contentType = contentType + (charset ? '; charset=' + charset : '');
res.setHeader('Content-Type', contentType);
res.setHeader('Content-Encoding', 'gzip');
res.setHeader('Vary', 'Accept-Encoding');
res.setHeader('Content-Length', data.length);
res.end(data, 'binary');
}

function gzipAndSend(filename, gzipName, mtime) {
gzipFile(filename, charset, function(gzippedData) {
gzipFileCache[gzipName] = {
'ctime': Date.now(),
'mtime': mtime,
'content': gzippedData
};
sendGzipped(gzippedData);
});
}


fs.stat(filename, function(err, stat) {
if (err || stat.isDirectory()) {
return pass(filename);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "resmin",
"version" : "0.0.5-2",
"version" : "0.0.6-1",
"author" : "Marcus Ekwall",
"description" : "All-in-one compressor/merger/minifier middleware for connect/express",
"homepage" : "https://github.com/mekwall/node-resmin",
Expand Down

0 comments on commit 3a19182

Please sign in to comment.