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.
// TODO: Serve files from another service, or directly from the file system.
const requestURI = (url, method, headers, callback) => {
const parsedURL = urlutil.parse(url);
let status = 500;
const requestURI = async (url, method, headers) => {
var headers = {};
const content = [];
const mockRequest = {
url,
method,
params: {filename: parsedURL.path.replace(/^\/static\//, '')},
headers,
};
const mockResponse = {
writeHead: (_status, _headers) => {
status = _status;
for (const header in _headers) {
if (Object.prototype.hasOwnProperty.call(_headers, header)) {
headers[header] = _headers[header];
return await new Promise((resolve) => {
const parsedURL = urlutil.parse(url);
let status = 500;
const content = [];
const mockRequest = {
url,
method,
params: {filename: parsedURL.path.replace(/^\/static\//, '')},
headers,
};
const mockResponse = {
writeHead: (_status, _headers) => {
status = _status;
for (const header in _headers) {
if (Object.prototype.hasOwnProperty.call(_headers, header)) {
headers[header] = _headers[header];
}
}
}
},
setHeader: (header, value) => {
headers[header.toLowerCase()] = value.toString();
},
header: (header, value) => {
headers[header.toLowerCase()] = value.toString();
},
write: (_content) => {
_content && content.push(_content);
},
end: (_content) => {
_content && content.push(_content);
callback(status, headers, content.join(''));
},
};
minify(mockRequest, mockResponse);
},
setHeader: (header, value) => {
headers[header.toLowerCase()] = value.toString();
},
header: (header, value) => {
headers[header.toLowerCase()] = value.toString();
},
write: (_content) => {
_content && content.push(_content);
},
end: (_content) => {
_content && content.push(_content);
resolve([status, headers, content.join('')]);
},
};
minify(mockRequest, mockResponse);
});
};
const requestURIs = (locations, method, headers, callback) => {
@ -101,15 +101,15 @@ const requestURIs = (locations, method, headers, callback) => {
callback(statuss, headerss, contentss);
};
const respondFor = (i) => (status, headers, content) => {
responses[i] = [status, headers, content];
const respondFor = (i) => (response) => {
responses[i] = response;
if (--pendingRequests === 0) {
completed();
}
};
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));
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);
if (!error) {
data += `Ace2Editor.EMBEDED[${JSON.stringify(filename)}] = ${