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,44 +50,44 @@ 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 = [];
const mockRequest = { return await new Promise((resolve) => {
url, const parsedURL = urlutil.parse(url);
method, let status = 500;
params: {filename: parsedURL.path.replace(/^\/static\//, '')}, const content = [];
headers, const mockRequest = {
}; url,
const mockResponse = { method,
writeHead: (_status, _headers) => { params: {filename: parsedURL.path.replace(/^\/static\//, '')},
status = _status; headers,
for (const header in _headers) { };
if (Object.prototype.hasOwnProperty.call(_headers, header)) { const mockResponse = {
headers[header] = _headers[header]; writeHead: (_status, _headers) => {
status = _status;
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('')]);
callback(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)}] = ${