From 1ec29e0d4583dace70a2ac797b9c2f7b580a7607 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 11 Feb 2021 15:25:36 -0500 Subject: [PATCH] Minify: Asyncify `requestURI()` --- src/node/utils/Minify.js | 78 ++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/node/utils/Minify.js b/src/node/utils/Minify.js index 19d6c0f5d..2bb8a8d66 100644 --- a/src/node/utils/Minify.js +++ b/src/node/utils/Minify.js @@ -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)}] = ${