Minify: Asyncify `statFile()`

pull/4765/head
Richard Hansen 2021-02-11 16:54:25 -05:00 committed by John McLear
parent 947dc8eeed
commit dd7ea1a8f9
1 changed files with 17 additions and 23 deletions

View File

@ -145,7 +145,7 @@ const minify = (req, res) => {
const contentType = mime.lookup(filename); const contentType = mime.lookup(filename);
statFile(filename, (error, date, exists) => { util.callbackify(statFile)(filename, 3, (error, [date, exists]) => {
if (date) { if (date) {
date = new Date(date); date = new Date(date);
date.setMilliseconds(0); date.setMilliseconds(0);
@ -186,7 +186,7 @@ const minify = (req, res) => {
res.writeHead(405, {allow: 'HEAD, GET'}); res.writeHead(405, {allow: 'HEAD, GET'});
res.end(); res.end();
} }
}, 3); });
}; };
// find all includes in ace.js and embed them. // find all includes in ace.js and embed them.
@ -231,7 +231,7 @@ const getAceFile = async () => {
}; };
// Check for the existance of the file and get the last modification date. // Check for the existance of the file and get the last modification date.
const statFile = (filename, callback, dirStatLimit) => { const statFile = async (filename, dirStatLimit) => {
/* /*
* The only external call to this function provides an explicit value for * The only external call to this function provides an explicit value for
* dirStatLimit: this check could be removed. * dirStatLimit: this check could be removed.
@ -241,32 +241,26 @@ const statFile = (filename, callback, dirStatLimit) => {
} }
if (dirStatLimit < 1 || filename === '' || filename === '/') { if (dirStatLimit < 1 || filename === '' || filename === '/') {
callback(null, null, false); return [null, false];
} else if (filename === 'js/ace.js') { } else if (filename === 'js/ace.js') {
// Sometimes static assets are inlined into this file, so we have to stat // Sometimes static assets are inlined into this file, so we have to stat
// everything. // everything.
lastModifiedDateOfEverything().then( return [await lastModifiedDateOfEverything(), true];
(date) => callback(null, date, true),
(err) => callback(err || new Error(err)));
} else if (filename === 'js/require-kernel.js') { } else if (filename === 'js/require-kernel.js') {
callback(null, requireLastModified(), true); return [requireLastModified(), true];
} else { } else {
fs.stat(ROOT_DIR + filename, (error, stats) => { let stats;
if (error) { try {
if (error.code === 'ENOENT') { stats = await util.promisify(fs.stat)(ROOT_DIR + filename);
} catch (err) {
if (err.code === 'ENOENT') {
// Stat the directory instead. // Stat the directory instead.
statFile(path.dirname(filename), (error, date, exists) => { const [date] = await statFile(path.dirname(filename), dirStatLimit - 1);
callback(error, date, false); return [date, false];
}, dirStatLimit - 1);
} else {
callback(error);
} }
} else if (stats.isFile()) { throw err;
callback(null, stats.mtime.getTime(), true);
} else {
callback(null, stats.mtime.getTime(), false);
} }
}); return [stats.mtime.getTime(), stats.isFile()];
} }
}; };