Minify: Refactor requestURI() for readability

This commit is contained in:
Richard Hansen 2021-04-21 16:24:27 -04:00
parent 3a8d66ba6a
commit aaacbd3a7a

View file

@ -48,7 +48,7 @@ const LIBRARY_WHITELIST = [
// What follows is a terrible hack to avoid loop-back within the server. // What follows is a terrible hack to avoid loop-back within the server.
// TODO: Serve files from another service, or directly from the file system. // TODO: Serve files from another service, or directly from the file system.
const requestURI = async (url, method, headers) => await new Promise((resolve, reject) => { const requestURI = async (url, method, headers) => {
const parsedUrl = new URL(url); const parsedUrl = new URL(url);
let status = 500; let status = 500;
const content = []; const content = [];
@ -58,31 +58,35 @@ const requestURI = async (url, method, headers) => await new Promise((resolve, r
params: {filename: (parsedUrl.pathname + parsedUrl.search).replace(/^\/static\//, '')}, params: {filename: (parsedUrl.pathname + parsedUrl.search).replace(/^\/static\//, '')},
headers, headers,
}; };
const mockResponse = { let mockResponse;
writeHead: (_status, _headers) => { const p = new Promise((resolve) => {
status = _status; mockResponse = {
for (const header in _headers) { writeHead: (_status, _headers) => {
if (Object.prototype.hasOwnProperty.call(_headers, header)) { status = _status;
headers[header] = _headers[header]; for (const header in _headers) {
if (Object.prototype.hasOwnProperty.call(_headers, header)) {
headers[header] = _headers[header];
}
} }
} },
}, setHeader: (header, value) => {
setHeader: (header, value) => { headers[header.toLowerCase()] = value.toString();
headers[header.toLowerCase()] = value.toString(); },
}, header: (header, value) => {
header: (header, value) => { headers[header.toLowerCase()] = value.toString();
headers[header.toLowerCase()] = value.toString(); },
}, write: (_content) => {
write: (_content) => { _content && content.push(_content);
_content && content.push(_content); },
}, end: (_content) => {
end: (_content) => { _content && content.push(_content);
_content && content.push(_content); resolve([status, headers, content.join('')]);
resolve([status, headers, content.join('')]); },
}, };
}; });
minify(mockRequest, mockResponse).catch(reject); await minify(mockRequest, mockResponse);
}); return await p;
};
const requestURIs = (locations, method, headers, callback) => { const requestURIs = (locations, method, headers, callback) => {
Promise.all(locations.map((loc) => requestURI(loc, method, headers))).then((responses) => { Promise.all(locations.map((loc) => requestURI(loc, method, headers))).then((responses) => {