2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-02-22 11:36:43 +01:00
|
|
|
import {MapArrayType} from "../../types/MapType";
|
|
|
|
import {PartType} from "../../types/PartType";
|
|
|
|
|
2021-02-09 17:26:46 -05:00
|
|
|
const fs = require('fs').promises;
|
2020-11-23 13:24:19 -05:00
|
|
|
const minify = require('../../utils/Minify');
|
2021-02-11 13:43:10 -05:00
|
|
|
const path = require('path');
|
2021-01-21 21:06:52 +00:00
|
|
|
const plugins = require('../../../static/js/pluginfw/plugin_defs');
|
2021-02-11 13:43:10 -05:00
|
|
|
const settings = require('../../utils/Settings');
|
2024-03-17 23:28:58 +01:00
|
|
|
import CachingMiddleware from '../../utils/caching_middleware';
|
2012-02-24 20:45:02 +01:00
|
|
|
|
2021-02-11 13:43:10 -05:00
|
|
|
// Rewrite tar to include modules with no extensions and proper rooted paths.
|
2021-02-09 17:26:46 -05:00
|
|
|
const getTar = async () => {
|
2024-02-22 11:36:43 +01:00
|
|
|
const prefixLocalLibraryPath = (path:string) => {
|
2021-02-11 13:43:10 -05:00
|
|
|
if (path.charAt(0) === '$') {
|
|
|
|
return path.slice(1);
|
|
|
|
} else {
|
|
|
|
return `ep_etherpad-lite/static/js/${path}`;
|
|
|
|
}
|
|
|
|
};
|
2024-02-22 11:36:43 +01:00
|
|
|
|
2021-02-09 17:26:46 -05:00
|
|
|
const tarJson = await fs.readFile(path.join(settings.root, 'src/node/utils/tar.json'), 'utf8');
|
2024-02-22 11:36:43 +01:00
|
|
|
const tar:MapArrayType<string[]> = {};
|
|
|
|
for (const [key, relativeFiles] of Object.entries(JSON.parse(tarJson)) as [string, string[]][]) {
|
2021-02-11 13:43:10 -05:00
|
|
|
const files = relativeFiles.map(prefixLocalLibraryPath);
|
|
|
|
tar[prefixLocalLibraryPath(key)] = files
|
|
|
|
.concat(files.map((p) => p.replace(/\.js$/, '')))
|
|
|
|
.concat(files.map((p) => `${p.replace(/\.js$/, '')}/index.js`));
|
|
|
|
}
|
|
|
|
return tar;
|
|
|
|
};
|
|
|
|
|
2024-02-22 11:36:43 +01:00
|
|
|
exports.expressPreSession = async (hookName:string, {app}:any) => {
|
2012-03-04 23:45:33 +01:00
|
|
|
// Cache both minified and static.
|
2020-11-23 13:24:19 -05:00
|
|
|
const assetCache = new CachingMiddleware();
|
2024-03-17 23:28:58 +01:00
|
|
|
// Cache static assets
|
|
|
|
app.all(/\/js\/(.*)/, assetCache.handle.bind(assetCache));
|
|
|
|
app.all(/\/css\/(.*)/, assetCache.handle.bind(assetCache));
|
2012-03-04 23:45:33 +01:00
|
|
|
|
|
|
|
// Minify will serve static files compressed (minify enabled). It also has
|
|
|
|
// file-specific hacks for ace/require-kernel/etc.
|
2021-12-17 17:01:55 -05:00
|
|
|
app.all('/static/:filename(*)', minify.minify);
|
2012-03-04 23:45:33 +01:00
|
|
|
|
|
|
|
// serve plugin definitions
|
2021-01-21 21:06:52 +00:00
|
|
|
// not very static, but served here so that client can do
|
|
|
|
// require("pluginfw/static/js/plugin-definitions.js");
|
2024-02-22 11:36:43 +01:00
|
|
|
app.get('/pluginfw/plugin-definitions.json', (req: any, res:any, next:Function) => {
|
|
|
|
const clientParts = plugins.parts.filter((part: PartType) => part.client_hooks != null);
|
|
|
|
const clientPlugins:MapArrayType<string> = {};
|
|
|
|
for (const name of new Set(clientParts.map((part: PartType) => part.plugin))) {
|
|
|
|
// @ts-ignore
|
2021-02-09 16:54:25 -05:00
|
|
|
clientPlugins[name] = {...plugins.plugins[name]};
|
2024-02-22 11:36:43 +01:00
|
|
|
// @ts-ignore
|
2021-02-09 16:54:25 -05:00
|
|
|
delete clientPlugins[name].package;
|
|
|
|
}
|
2021-02-13 08:13:48 +00:00
|
|
|
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
|
|
res.setHeader('Cache-Control', `public, max-age=${settings.maxAge}`);
|
2020-11-23 13:24:19 -05:00
|
|
|
res.write(JSON.stringify({plugins: clientPlugins, parts: clientParts}));
|
2012-03-04 23:45:33 +01:00
|
|
|
res.end();
|
2012-02-24 20:45:02 +01:00
|
|
|
});
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|