From 5d8c8f90927ca56236551dd7b8276e8adbd5c55e Mon Sep 17 00:00:00 2001 From: SamTV12345 <40429738+samtv12345@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:25:55 +0100 Subject: [PATCH] Fixed. --- src/node/utils/Minify.ts | 20 ++++++++++++++------ src/node/utils/MinifyWorker.ts | 4 +--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/node/utils/Minify.ts b/src/node/utils/Minify.ts index ad4514a23..034e84eda 100644 --- a/src/node/utils/Minify.ts +++ b/src/node/utils/Minify.ts @@ -32,7 +32,6 @@ const RequireKernel = require('etherpad-require-kernel'); import mime from 'mime-types'; import log4js from 'log4js'; const sanitizePathname = require('./sanitizePathname'); - const logger = log4js.getLogger('Minify'); const ROOT_DIR = path.join(settings.root, 'src/static/'); @@ -274,6 +273,17 @@ const _requireLastModified = new Date(); const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`; const getFileCompressed = async (filename: string, contentType: string) => { + + const ensureBuffer = (input: string|Buffer|object)=> { + if (Buffer.isBuffer(input)) { + return input; + } else if (typeof input === 'string') { + return Buffer.from(input); + } else { + throw new Error('Input should be a Buffer or a string'); + } + } + let content = await getFile(filename); if (!content || !settings.minify) { return content; @@ -281,10 +291,9 @@ const getFileCompressed = async (filename: string, contentType: string) => { try { logger.info('Compress JS file %s.', filename); - content = content.toString(); - const compressResult = compressJS(content); + const compressResult = compressJS(ensureBuffer(content).toString()); - content = compressResult.code.toString(); // Convert content obj code to string + return compressResult.code.toString(); // Convert content obj code to string } catch (error) { console.error('getFile() returned an error in ' + `getFileCompressed(${filename}, ${contentType}): ${error}`); @@ -293,8 +302,7 @@ const getFileCompressed = async (filename: string, contentType: string) => { } else if (contentType === 'text/css') { try { logger.info('Compress CSS file %s.', filename); - content = compressCSS(filename, ROOT_DIR, Buffer.from(content)).toString(); - return content + return compressCSS(filename, ROOT_DIR, ensureBuffer(content)); } catch (error) { console.error(`CleanCSS.minify() returned an error on ${filename}: ${error}`); } diff --git a/src/node/utils/MinifyWorker.ts b/src/node/utils/MinifyWorker.ts index 08a365996..b40f76992 100644 --- a/src/node/utils/MinifyWorker.ts +++ b/src/node/utils/MinifyWorker.ts @@ -4,7 +4,6 @@ */ -import {promises as fsp} from "fs"; import path from 'path'; import {minifySync} from "@swc/core"; import lightminify from 'lightningcss'; @@ -14,13 +13,12 @@ export const compressJS = (content: string) => minifySync(content); export const compressCSS = (filename: string, ROOT_DIR: string, content: Buffer) => { const absPath = path.resolve(ROOT_DIR, filename); try { - const basePath = path.dirname(absPath); let { code } = lightminify.transform({ filename: absPath, minify: true, code: content }); - return code; + return code.toString(); } catch (error) { // on error, just yield the un-minified original, but write a log message console.error(`Unexpected error minifying ${filename} (${absPath}): ${error}`);