Combine existense and date checks.

pull/468/head
Chad Weider 2012-02-19 17:34:43 -08:00
parent a5006255b7
commit ff264aa69a
1 changed files with 30 additions and 46 deletions

View File

@ -54,7 +54,7 @@ exports.minifyJS = function(req, res, next)
var filename = req.params['filename']; var filename = req.params['filename'];
res.header("Content-Type","text/javascript"); res.header("Content-Type","text/javascript");
lastModifiedDateOf(filename, function (error, date) { statFile(filename, function (error, date, exists) {
date = new Date(date); date = new Date(date);
res.setHeader('last-modified', date.toUTCString()); res.setHeader('last-modified', date.toUTCString());
res.setHeader('date', (new Date()).toUTCString()); res.setHeader('date', (new Date()).toUTCString());
@ -64,33 +64,31 @@ exports.minifyJS = function(req, res, next)
res.setHeader('cache-control', 'max-age=' + server.maxAge); res.setHeader('cache-control', 'max-age=' + server.maxAge);
} }
fileExists(filename, function (error, exists) { if (error) {
if (error) { res.writeHead(500, {});
res.writeHead(500, {}); res.end();
} else if (!exists) {
res.writeHead(404, {});
res.end();
} else if (new Date(req.headers['if-modified-since']) >= date) {
res.writeHead(304, {});
res.end();
} else {
if (req.method == 'HEAD') {
res.writeHead(200, {});
res.end(); res.end();
} else if (!exists) { } else if (req.method == 'GET') {
res.writeHead(404, {}); getFileCompressed(filename, function (error, content) {
res.end(); if(ERR(error)) return;
} else if (new Date(req.headers['if-modified-since']) >= date) {
res.writeHead(304, {});
res.end();
} else {
if (req.method == 'HEAD') {
res.writeHead(200, {}); res.writeHead(200, {});
res.write(content);
res.end(); res.end();
} else if (req.method == 'GET') { });
getFileCompressed(filename, function (error, content) { } else {
if(ERR(error)) return; res.writeHead(405, {'allow': 'HEAD, GET'});
res.writeHead(200, {}); res.end();
res.write(content);
res.end();
});
} else {
res.writeHead(405, {'allow': 'HEAD, GET'});
res.end();
}
} }
}); }
}); });
} }
@ -161,9 +159,11 @@ function getAceFile(callback) {
}); });
} }
function lastModifiedDateOf(filename, callback) { function statFile(filename, callback) {
if (filename == 'ace.js') { if (filename == 'ace.js') {
lastModifiedDateOfEverything(callback); lastModifiedDateOfEverything(function (error, date) {
callback(error, date, !error);
});
} else { } else {
fs.stat(JS_DIR + filename, function (error, stats) { fs.stat(JS_DIR + filename, function (error, stats) {
if (error) { if (error) {
@ -171,15 +171,17 @@ function lastModifiedDateOf(filename, callback) {
fs.stat(JS_DIR, function (error, stats) { fs.stat(JS_DIR, function (error, stats) {
if (error) { if (error) {
callback(error); callback(error);
} else if (filename == 'require-kernel.js') {
callback(null, stats.mtime.getTime(), true);
} else { } else {
callback(null, stats.mtime.getTime()); callback(null, stats.mtime.getTime(), false);
} }
}); });
} else { } else {
callback(error); callback(error);
} }
} else { } else {
callback(null, stats.mtime.getTime()); callback(null, stats.mtime.getTime(), true);
} }
}); });
} }
@ -255,24 +257,6 @@ function getFile(filename, callback) {
} }
} }
function fileExists(filename, callback) {
if (filename == 'require-kernel.js') {
callback(undefined, true);
} else {
fs.stat(JS_DIR + filename, function (error, stats) {
if (error) {
if (error.code == "ENOENT") {
callback(undefined, false);
} else {
callback(error, undefined);
}
} else {
callback(undefined, stats.isFile());
}
});
}
}
function tarCode(jsFiles, write, callback) { function tarCode(jsFiles, write, callback) {
write('require.define({'); write('require.define({');
var initialEntry = true; var initialEntry = true;