From dd7ea1a8f9ee91e16db13379f0b0124e061fea6e Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 11 Feb 2021 16:54:25 -0500 Subject: [PATCH] Minify: Asyncify `statFile()` --- src/node/utils/Minify.js | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/node/utils/Minify.js b/src/node/utils/Minify.js index 53357aa28..2346d57ca 100644 --- a/src/node/utils/Minify.js +++ b/src/node/utils/Minify.js @@ -145,7 +145,7 @@ const minify = (req, res) => { const contentType = mime.lookup(filename); - statFile(filename, (error, date, exists) => { + util.callbackify(statFile)(filename, 3, (error, [date, exists]) => { if (date) { date = new Date(date); date.setMilliseconds(0); @@ -186,7 +186,7 @@ const minify = (req, res) => { res.writeHead(405, {allow: 'HEAD, GET'}); res.end(); } - }, 3); + }); }; // 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. -const statFile = (filename, callback, dirStatLimit) => { +const statFile = async (filename, dirStatLimit) => { /* * The only external call to this function provides an explicit value for * dirStatLimit: this check could be removed. @@ -241,32 +241,26 @@ const statFile = (filename, callback, dirStatLimit) => { } if (dirStatLimit < 1 || filename === '' || filename === '/') { - callback(null, null, false); + return [null, false]; } else if (filename === 'js/ace.js') { // Sometimes static assets are inlined into this file, so we have to stat // everything. - lastModifiedDateOfEverything().then( - (date) => callback(null, date, true), - (err) => callback(err || new Error(err))); + return [await lastModifiedDateOfEverything(), true]; } else if (filename === 'js/require-kernel.js') { - callback(null, requireLastModified(), true); + return [requireLastModified(), true]; } else { - fs.stat(ROOT_DIR + filename, (error, stats) => { - if (error) { - if (error.code === 'ENOENT') { - // Stat the directory instead. - statFile(path.dirname(filename), (error, date, exists) => { - callback(error, date, false); - }, dirStatLimit - 1); - } else { - callback(error); - } - } else if (stats.isFile()) { - callback(null, stats.mtime.getTime(), true); - } else { - callback(null, stats.mtime.getTime(), false); + let stats; + try { + stats = await util.promisify(fs.stat)(ROOT_DIR + filename); + } catch (err) { + if (err.code === 'ENOENT') { + // Stat the directory instead. + const [date] = await statFile(path.dirname(filename), dirStatLimit - 1); + return [date, false]; } - }); + throw err; + } + return [stats.mtime.getTime(), stats.isFile()]; } };