Minify: Asyncify `getFileCompressed()`

pull/4765/head
Richard Hansen 2021-02-11 17:10:13 -05:00 committed by John McLear
parent dd7ea1a8f9
commit 84190793dc
1 changed files with 16 additions and 15 deletions

View File

@ -172,7 +172,7 @@ const minify = (req, res) => {
res.writeHead(200, {}); res.writeHead(200, {});
res.end(); res.end();
} else if (req.method === 'GET') { } else if (req.method === 'GET') {
getFileCompressed(filename, contentType, (error, content) => { util.callbackify(getFileCompressed)(filename, contentType, (error, content) => {
if (ERR(error, () => { if (ERR(error, () => {
res.writeHead(500, {}); res.writeHead(500, {});
res.end(); res.end();
@ -298,11 +298,12 @@ const _requireLastModified = new Date();
const requireLastModified = () => _requireLastModified.toUTCString(); const requireLastModified = () => _requireLastModified.toUTCString();
const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`; const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`;
const getFileCompressed = (filename, contentType, callback) => { const getFileCompressed = async (filename, contentType) => {
util.callbackify(getFile)(filename, (error, content) => { let content = await getFile(filename);
if (error || !content || !settings.minify) { if (!content || !settings.minify) {
callback(error, content); return content;
} else if (contentType === 'application/javascript') { } else if (contentType === 'application/javascript') {
return await new Promise((resolve) => {
threadsPool.queue(async ({compressJS}) => { threadsPool.queue(async ({compressJS}) => {
try { try {
logger.info('Compress JS file %s.', filename); logger.info('Compress JS file %s.', filename);
@ -319,10 +320,11 @@ const getFileCompressed = (filename, contentType, callback) => {
console.error('getFile() returned an error in ' + console.error('getFile() returned an error in ' +
`getFileCompressed(${filename}, ${contentType}): ${error}`); `getFileCompressed(${filename}, ${contentType}): ${error}`);
} }
resolve(content);
callback(null, content);
}); });
} else if (contentType === 'text/css') { });
} else if (contentType === 'text/css') {
return await new Promise((resolve) => {
threadsPool.queue(async ({compressCSS}) => { threadsPool.queue(async ({compressCSS}) => {
try { try {
logger.info('Compress CSS file %s.', filename); logger.info('Compress CSS file %s.', filename);
@ -331,13 +333,12 @@ const getFileCompressed = (filename, contentType, callback) => {
} catch (error) { } catch (error) {
console.error(`CleanCSS.minify() returned an error on ${filename}: ${error}`); console.error(`CleanCSS.minify() returned an error on ${filename}: ${error}`);
} }
resolve(content);
callback(null, content);
}); });
} else { });
callback(null, content); } else {
} return content;
}); }
}; };
const getFile = async (filename) => { const getFile = async (filename) => {