From d7869f50140db698186302c774e80fe1ba30097b Mon Sep 17 00:00:00 2001 From: SamTV12345 <40429738+SamTV12345@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:28:58 +0100 Subject: [PATCH] Fixed cache server not working due to wrong (#6236) --- src/node/hooks/express/static.ts | 6 ++++-- src/node/utils/caching_middleware.ts | 17 +++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/node/hooks/express/static.ts b/src/node/hooks/express/static.ts index 6b0b75593..1d9ba01e9 100644 --- a/src/node/hooks/express/static.ts +++ b/src/node/hooks/express/static.ts @@ -8,7 +8,7 @@ const minify = require('../../utils/Minify'); const path = require('path'); const plugins = require('../../../static/js/pluginfw/plugin_defs'); const settings = require('../../utils/Settings'); -const CachingMiddleware = require('../../utils/caching_middleware'); +import CachingMiddleware from '../../utils/caching_middleware'; const Yajsml = require('etherpad-yajsml'); // Rewrite tar to include modules with no extensions and proper rooted paths. @@ -35,7 +35,9 @@ const getTar = async () => { exports.expressPreSession = async (hookName:string, {app}:any) => { // Cache both minified and static. const assetCache = new CachingMiddleware(); - app.all(/\/javascripts\/(.*)/, assetCache.handle.bind(assetCache)); + // Cache static assets + app.all(/\/js\/(.*)/, assetCache.handle.bind(assetCache)); + app.all(/\/css\/(.*)/, assetCache.handle.bind(assetCache)); // Minify will serve static files compressed (minify enabled). It also has // file-specific hacks for ace/require-kernel/etc. diff --git a/src/node/utils/caching_middleware.ts b/src/node/utils/caching_middleware.ts index d5866b019..74712871c 100644 --- a/src/node/utils/caching_middleware.ts +++ b/src/node/utils/caching_middleware.ts @@ -16,14 +16,14 @@ * limitations under the License. */ -const Buffer = require('buffer').Buffer; -const fs = require('fs'); +import {Buffer} from 'node:buffer' +import fs from 'fs'; const fsp = fs.promises; -const path = require('path'); -const zlib = require('zlib'); +import path from 'path'; +import zlib from 'zlib'; const settings = require('./Settings'); const existsSync = require('./path_exists'); -const util = require('util'); +import util from 'util'; /* * The crypto module can be absent on reduced node installations. @@ -37,10 +37,10 @@ const util = require('util'); */ -const _crypto = require('crypto'); +import _crypto from 'crypto'; -let CACHE_DIR = path.join(settings.root, 'var/'); +let CACHE_DIR: string|undefined = path.join(settings.root, 'var/'); CACHE_DIR = existsSync(CACHE_DIR) ? CACHE_DIR : undefined; type Headers = { @@ -84,7 +84,7 @@ if (_crypto) { should replace this. */ -module.exports = class CachingMiddleware { +export default class CachingMiddleware { handle(req: any, res: any, next: any) { this._handle(req, res, next).catch((err) => next(err || new Error(err))); } @@ -188,6 +188,7 @@ module.exports = class CachingMiddleware { await Promise.all([ fsp.writeFile(`${CACHE_DIR}minified_${cacheKey}`, buffer).catch(() => {}), util.promisify(zlib.gzip)(buffer) + // @ts-ignore .then((content: string) => fsp.writeFile(`${CACHE_DIR}minified_${cacheKey}.gz`, content)) .catch(() => {}), ]);