From 7d34c2c821bdec8ee2b46ab4bd7ebeffd357730e Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Tue, 25 Aug 2020 14:33:11 +0200 Subject: [PATCH] refactor clean-css to use builtin promise --- src/node/utils/Minify.js | 20 +++++-- src/node/utils/MinifyWorker.js | 79 +++++++++++++--------------- src/node/utils/caching_middleware.js | 3 ++ 3 files changed, 56 insertions(+), 46 deletions(-) diff --git a/src/node/utils/Minify.js b/src/node/utils/Minify.js index 66a1926a9..8a97e5c32 100644 --- a/src/node/utils/Minify.js +++ b/src/node/utils/Minify.js @@ -53,6 +53,14 @@ const LIBRARY_WHITELIST = [ // Rewrite tar to include modules with no extensions and proper rooted paths. const LIBRARY_PREFIX = 'ep_etherpad-lite/static/js'; exports.tar = {}; + +/** + * Prefix a path with `LIBRARY_PREFIX` + * If path starts with '$' it is not prefixed + * + * @param {string} path + * @returns {string} + */ const prefixLocalLibraryPath = (path) => { if (path.charAt(0) === '$') { return path.slice(1); @@ -134,8 +142,7 @@ const requestURIs = (locations, method, headers, callback) => { /** * creates the minifed javascript for the given minified name - * @param req the Express request - * @param res the Express response + * */ const minify = (req, res) => { let filename = req.params.filename; @@ -349,6 +356,11 @@ const _requireLastModified = new Date(); const requireLastModified = () => _requireLastModified.toUTCString(); const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`; +/** + * @param {string} filename + * @param {string} contentType + * @param {Function} callback + */ const getFileCompressed = (filename, contentType, callback) => { getFile(filename, (error, content) => { if (error || !content || !settings.minify) { @@ -379,11 +391,11 @@ const getFileCompressed = (filename, contentType, callback) => { logger.info('Compress CSS file %s.', filename); content = await compressCSS(filename, ROOT_DIR); + callback(null, content.styles); } catch (error) { console.error(`CleanCSS.minify() returned an error on ${filename}: ${error}`); + callback(null, content) } - - callback(null, content); }); } else { callback(null, content); diff --git a/src/node/utils/MinifyWorker.js b/src/node/utils/MinifyWorker.js index c8bc09eb1..564fecb3b 100644 --- a/src/node/utils/MinifyWorker.js +++ b/src/node/utils/MinifyWorker.js @@ -7,56 +7,51 @@ const Terser = require('terser'); const path = require('path'); const Threads = require('threads'); +/** + * Returns content compressed with Terser + * + * @param {string} content javascript to be compressed + * @returns {MinifyOutput} compressed javascript + */ function compressJS(content) { return Terser.minify(content); } function compressCSS(filename, ROOT_DIR) { - return new Promise((res, rej) => { - try { - const absPath = path.join(ROOT_DIR, filename); + const absPath = path.join(ROOT_DIR, filename); - /* - * Changes done to migrate CleanCSS 3.x -> 4.x: - * - * 1. Rework the rebase logic, because the API was simplified (but we have - * less control now). See: - * https://github.com/jakubpawlowicz/clean-css/blob/08f3a74925524d30bbe7ac450979de0a8a9e54b2/README.md#important-40-breaking-changes - * - * EXAMPLE: - * The URLs contained in a CSS file (including all the stylesheets - * imported by it) residing on disk at: - * /home/muxator/etherpad/src/static/css/pad.css - * - * Will be rewritten rebasing them to: - * /home/muxator/etherpad/src/static/css - * - * 2. CleanCSS.minify() can either receive a string containing the CSS, or - * an array of strings. In that case each array element is interpreted as - * an absolute local path from which the CSS file is read. - * - * In version 4.x, CleanCSS API was simplified, eliminating the - * relativeTo parameter, and thus we cannot use our already loaded - * "content" argument, but we have to wrap the absolute path to the CSS - * in an array and ask the library to read it by itself. - */ + /* + * Changes done to migrate CleanCSS 3.x -> 4.x: + * + * 1. Rework the rebase logic, because the API was simplified (but we have + * less control now). See: + * https://github.com/jakubpawlowicz/clean-css/blob/08f3a74925524d30bbe7ac450979de0a8a9e54b2/README.md#important-40-breaking-changes + * + * EXAMPLE: + * The URLs contained in a CSS file (including all the stylesheets + * imported by it) residing on disk at: + * /home/muxator/etherpad/src/static/css/pad.css + * + * Will be rewritten rebasing them to: + * /home/muxator/etherpad/src/static/css + * + * 2. CleanCSS.minify() can either receive a string containing the CSS, or + * an array of strings. In that case each array element is interpreted as + * an absolute local path from which the CSS file is read. + * + * In version 4.x, CleanCSS API was simplified, eliminating the + * relativeTo parameter, and thus we cannot use our already loaded + * "content" argument, but we have to wrap the absolute path to the CSS + * in an array and ask the library to read it by itself. + */ - const basePath = path.dirname(absPath); + const basePath = path.dirname(absPath); - new CleanCSS({ - rebase: true, - rebaseTo: basePath, - }).minify([absPath], (errors, minified) => { - if (errors) return rej(errors); - - return res(minified.styles); - }); - } catch (error) { - // on error, just yield the un-minified original, but write a log message - console.error(`Unexpected error minifying ${filename} (${absPath}): ${error}`); - callback(null, content); - } - }); + return new CleanCSS({ + rebase: true, + rebaseTo: basePath, + returnPromise: true + }).minify([absPath]) } Threads.expose({ diff --git a/src/node/utils/caching_middleware.js b/src/node/utils/caching_middleware.js index 8bb3ab00d..98e239ca3 100644 --- a/src/node/utils/caching_middleware.js +++ b/src/node/utils/caching_middleware.js @@ -77,6 +77,9 @@ if (_crypto) { should replace this. */ +/** + * @class + */ function CachingMiddleware() { } CachingMiddleware.prototype = new function () {