etherpad-lite/src/node/hooks/express/static.js

52 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-01-21 22:06:52 +01:00
'use strict';
2020-11-23 19:24:19 +01:00
const minify = require('../../utils/Minify');
2021-01-21 22:06:52 +01:00
const plugins = require('../../../static/js/pluginfw/plugin_defs');
2020-11-23 19:24:19 +01:00
const CachingMiddleware = require('../../utils/caching_middleware');
const Yajsml = require('etherpad-yajsml');
2021-01-21 22:06:52 +01:00
exports.expressCreateServer = (hookName, args, cb) => {
// Cache both minified and static.
2020-11-23 19:24:19 +01:00
const assetCache = new CachingMiddleware();
args.app.all(/\/javascripts\/(.*)/, assetCache.handle);
// Minify will serve static files compressed (minify enabled). It also has
// file-specific hacks for ace/require-kernel/etc.
args.app.all('/static/:filename(*)', minify.minify);
// Setup middleware that will package JavaScript files served by minify for
// CommonJS loader on the client-side.
// Hostname "invalid.invalid" is a dummy value to allow parsing as a URI.
2020-11-23 19:24:19 +01:00
const jsServer = new (Yajsml.Server)({
rootPath: 'javascripts/src/',
rootURI: 'http://invalid.invalid/static/js/',
libraryPath: 'javascripts/lib/',
libraryURI: 'http://invalid.invalid/static/plugins/',
requestURIs: minify.requestURIs, // Loop-back is causing problems, this is a workaround.
2012-02-25 19:43:00 +01:00
});
2020-11-23 19:24:19 +01:00
const StaticAssociator = Yajsml.associators.StaticAssociator;
const associations = Yajsml.associators.associationsForSimpleMapping(minify.getTar());
2020-11-23 19:24:19 +01:00
const associator = new StaticAssociator(associations);
jsServer.setAssociator(associator);
2015-04-07 14:55:05 +02:00
args.app.use(jsServer.handle.bind(jsServer));
// serve plugin definitions
2021-01-21 22:06:52 +01:00
// not very static, but served here so that client can do
// require("pluginfw/static/js/plugin-definitions.js");
2020-11-23 19:24:19 +01:00
args.app.get('/pluginfw/plugin-definitions.json', (req, res, next) => {
const clientParts = plugins.parts.filter((part) => part.client_hooks != null);
2020-11-23 19:24:19 +01:00
const clientPlugins = {};
for (const name of new Set(clientParts.map((part) => part.plugin))) {
clientPlugins[name] = {...plugins.plugins[name]};
delete clientPlugins[name].package;
}
2020-11-23 19:24:19 +01:00
res.header('Content-Type', 'application/json; charset=utf-8');
res.write(JSON.stringify({plugins: clientPlugins, parts: clientParts}));
res.end();
});
return cb();
2020-11-23 19:24:19 +01:00
};