Minify: Asyncify minify()

This commit is contained in:
Richard Hansen 2021-02-11 17:15:31 -05:00 committed by John McLear
parent 3eefe71834
commit 073052ac66

View file

@ -21,7 +21,6 @@
* limitations under the License. * limitations under the License.
*/ */
const ERR = require('async-stacktrace');
const settings = require('./Settings'); const settings = require('./Settings');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -53,7 +52,7 @@ const LIBRARY_WHITELIST = [
const requestURI = async (url, method, headers) => { const requestURI = async (url, method, headers) => {
var headers = {}; var headers = {};
return await new Promise((resolve) => { return await new Promise((resolve, reject) => {
const parsedURL = urlutil.parse(url); const parsedURL = urlutil.parse(url);
let status = 500; let status = 500;
const content = []; const content = [];
@ -86,7 +85,7 @@ const requestURI = async (url, method, headers) => {
resolve([status, headers, content.join('')]); resolve([status, headers, content.join('')]);
}, },
}; };
minify(mockRequest, mockResponse); minify(mockRequest, mockResponse).catch(reject);
}); });
}; };
@ -104,7 +103,7 @@ const requestURIs = (locations, method, headers, callback) => {
* @param req the Express request * @param req the Express request
* @param res the Express response * @param res the Express response
*/ */
const minify = (req, res) => { const minify = async (req, res) => {
let filename = req.params.filename; let filename = req.params.filename;
// No relative paths, especially if they may go up the file hierarchy. // No relative paths, especially if they may go up the file hierarchy.
@ -145,8 +144,10 @@ const minify = (req, res) => {
const contentType = mime.lookup(filename); const contentType = mime.lookup(filename);
util.callbackify(statFile)(filename, 3, (error, [date, exists]) => { let date, exists;
if (error) { try {
[date, exists] = await statFile(filename, 3);
} catch (err) {
res.writeHead(500, {}); res.writeHead(500, {});
res.end(); res.end();
return; return;
@ -174,21 +175,22 @@ const minify = (req, res) => {
res.writeHead(200, {}); res.writeHead(200, {});
res.end(); res.end();
} else if (req.method === 'GET') { } else if (req.method === 'GET') {
util.callbackify(getFileCompressed)(filename, contentType, (error, content) => { let content;
if (ERR(error, () => { try {
content = await getFileCompressed(filename, contentType);
} catch (err) {
res.writeHead(500, {}); res.writeHead(500, {});
res.end(); res.end();
})) return; return;
}
res.header('Content-Type', contentType); res.header('Content-Type', contentType);
res.writeHead(200, {}); res.writeHead(200, {});
res.write(content); res.write(content);
res.end(); res.end();
});
} else { } else {
res.writeHead(405, {allow: 'HEAD, GET'}); res.writeHead(405, {allow: 'HEAD, GET'});
res.end(); res.end();
} }
});
}; };
// find all includes in ace.js and embed them. // find all includes in ace.js and embed them.
@ -349,7 +351,7 @@ const getFile = async (filename) => {
return await util.promisify(fs.readFile)(ROOT_DIR + filename); return await util.promisify(fs.readFile)(ROOT_DIR + filename);
}; };
exports.minify = minify; exports.minify = (req, res, next) => minify(req, res).catch((err) => next(err || new Error(err)));
exports.requestURIs = requestURIs; exports.requestURIs = requestURIs;