Minify: Asyncify `requestURI()`

pull/4765/head
Richard Hansen 2021-02-11 15:25:36 -05:00 committed by John McLear
parent a952df2cf5
commit 1ec29e0d45
1 changed files with 39 additions and 39 deletions

View File

@ -50,13 +50,13 @@ 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 = (url, method, headers, callback) => { const requestURI = async (url, method, headers) => {
const parsedURL = urlutil.parse(url);
let status = 500;
var headers = {}; var headers = {};
const content = [];
return await new Promise((resolve) => {
const parsedURL = urlutil.parse(url);
let status = 500;
const content = [];
const mockRequest = { const mockRequest = {
url, url,
method, method,
@ -83,11 +83,11 @@ const requestURI = (url, method, headers, callback) => {
}, },
end: (_content) => { end: (_content) => {
_content && content.push(_content); _content && content.push(_content);
callback(status, headers, content.join('')); resolve([status, headers, content.join('')]);
}, },
}; };
minify(mockRequest, mockResponse); minify(mockRequest, mockResponse);
});
}; };
const requestURIs = (locations, method, headers, callback) => { const requestURIs = (locations, method, headers, callback) => {
@ -101,15 +101,15 @@ const requestURIs = (locations, method, headers, callback) => {
callback(statuss, headerss, contentss); callback(statuss, headerss, contentss);
}; };
const respondFor = (i) => (status, headers, content) => { const respondFor = (i) => (response) => {
responses[i] = [status, headers, content]; responses[i] = response;
if (--pendingRequests === 0) { if (--pendingRequests === 0) {
completed(); completed();
} }
}; };
for (let i = 0, ii = locations.length; i < ii; i++) { for (let i = 0, ii = locations.length; i < ii; i++) {
requestURI(locations[i], method, headers, respondFor(i)); requestURI(locations[i], method, headers).then(respondFor(i));
} }
}; };
@ -233,7 +233,7 @@ const getAceFile = (callback) => {
let resourceURI = baseURI + path.normalize(path.join('/static/', filename)); let resourceURI = baseURI + path.normalize(path.join('/static/', filename));
resourceURI = resourceURI.replace(/\\/g, '/'); // Windows (safe generally?) resourceURI = resourceURI.replace(/\\/g, '/'); // Windows (safe generally?)
requestURI(resourceURI, 'GET', {}, (status, headers, body) => { requestURI(resourceURI, 'GET', {}).then(([status, headers, body]) => {
const error = !(status === 200 || status === 404); const error = !(status === 200 || status === 404);
if (!error) { if (!error) {
data += `Ace2Editor.EMBEDED[${JSON.stringify(filename)}] = ${ data += `Ace2Editor.EMBEDED[${JSON.stringify(filename)}] = ${