2021-01-19 16:37:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-13 14:32:00 -05:00
|
|
|
const log4js = require('log4js');
|
2021-01-19 16:37:12 +00:00
|
|
|
const plugins = require('./plugins');
|
|
|
|
const hooks = require('./hooks');
|
2021-02-17 22:53:50 -05:00
|
|
|
const runCmd = require('../../../node/utils/run_cmd');
|
2021-02-15 08:52:38 +01:00
|
|
|
const settings = require('../../../node/utils/Settings');
|
2023-06-27 21:20:53 +02:00
|
|
|
const axios = require('axios');
|
2024-02-11 09:51:42 +01:00
|
|
|
const {PluginManager} = require('live-plugin-manager-pnpm');
|
|
|
|
const {promises: fs} = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const {findEtherpadRoot} = require('../../../node/utils/AbsolutePaths');
|
2021-02-10 01:34:38 -05:00
|
|
|
const logger = log4js.getLogger('plugins');
|
|
|
|
|
2024-01-14 11:54:57 +01:00
|
|
|
exports.manager = new PluginManager();
|
|
|
|
|
|
|
|
const installedPluginsPath = path.join(settings.root, 'var/installed_plugins.json');
|
|
|
|
|
2021-02-15 08:52:38 +01:00
|
|
|
const onAllTasksFinished = async () => {
|
2024-01-14 11:54:57 +01:00
|
|
|
await plugins.update();
|
|
|
|
await persistInstalledPlugins();
|
2021-02-15 08:52:38 +01:00
|
|
|
settings.reloadSettings();
|
|
|
|
await hooks.aCallAll('loadSettings', {settings});
|
|
|
|
await hooks.aCallAll('restartServer');
|
2021-01-19 16:37:12 +00:00
|
|
|
};
|
|
|
|
|
2023-12-17 11:05:32 +00:00
|
|
|
const headers = {
|
2024-02-11 09:51:42 +01:00
|
|
|
'User-Agent': `Etherpad/${settings.getEpVersion()}`,
|
|
|
|
};
|
2023-12-17 11:05:32 +00:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
let tasks = 0;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-02-10 01:28:10 -05:00
|
|
|
const wrapTaskCb = (cb) => {
|
2019-02-08 23:20:57 +01:00
|
|
|
tasks++;
|
|
|
|
|
2021-02-10 01:28:10 -05:00
|
|
|
return (...args) => {
|
|
|
|
cb && cb(...args);
|
2013-04-08 16:14:03 +02:00
|
|
|
tasks--;
|
2021-01-19 16:37:12 +00:00
|
|
|
if (tasks === 0) onAllTasksFinished();
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2021-02-10 01:28:10 -05:00
|
|
|
};
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2024-01-14 11:54:57 +01:00
|
|
|
const migratePluginsFromNodeModules = async () => {
|
2024-02-11 09:51:42 +01:00
|
|
|
logger.info('start migration of plugins in node_modules');
|
2024-01-14 11:54:57 +01:00
|
|
|
// Notes:
|
|
|
|
// * Do not pass `--prod` otherwise `npm ls` will fail if there is no `package.json`.
|
|
|
|
// * The `--no-production` flag is required (or the `NODE_ENV` environment variable must be
|
|
|
|
// unset or set to `development`) because otherwise `npm ls` will not mention any packages
|
|
|
|
// that are not included in `package.json` (which is expected to not exist).
|
2024-02-11 09:51:42 +01:00
|
|
|
const cmd = ['pnpm', 'ls', '--long', '--json', '--depth=0', '--no-production'];
|
|
|
|
const [{dependencies = {}}] = JSON.parse(await runCmd(cmd,
|
|
|
|
{stdio: [null, 'string']}));
|
|
|
|
await Promise.all(Object.entries(dependencies)
|
|
|
|
.filter(([pkg, info]) => pkg.startsWith(plugins.prefix) && pkg !== 'ep_etherpad-lite')
|
|
|
|
.map(async ([pkg, info]) => {
|
|
|
|
if (!info._resolved) {
|
|
|
|
// Install from node_modules directory
|
|
|
|
await exports.manager.installFromPath(`${findEtherpadRoot()}/node_modules/${pkg}`);
|
|
|
|
} else {
|
|
|
|
await exports.manager.install(pkg);
|
|
|
|
}
|
|
|
|
}));
|
2024-01-14 11:54:57 +01:00
|
|
|
await persistInstalledPlugins();
|
2024-01-30 22:27:31 +01:00
|
|
|
};
|
2024-01-14 11:54:57 +01:00
|
|
|
|
|
|
|
exports.checkForMigration = async () => {
|
2024-01-30 22:27:31 +01:00
|
|
|
logger.info('check installed plugins for migration');
|
2024-01-14 11:54:57 +01:00
|
|
|
|
2020-11-13 13:59:20 -05:00
|
|
|
try {
|
2024-01-30 22:27:31 +01:00
|
|
|
await fs.access(installedPluginsPath, fs.constants.F_OK);
|
2020-11-13 13:59:20 -05:00
|
|
|
} catch (err) {
|
2024-01-14 11:54:57 +01:00
|
|
|
await migratePluginsFromNodeModules();
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileContent = await fs.readFile(installedPluginsPath);
|
|
|
|
const installedPlugins = JSON.parse(fileContent.toString());
|
|
|
|
|
|
|
|
for (const plugin of installedPlugins.plugins) {
|
|
|
|
if (plugin.name.startsWith(plugins.prefix) && plugin.name !== 'ep_etherpad-lite') {
|
2024-02-11 09:51:42 +01:00
|
|
|
await exports.manager.install(plugin.name, plugin.version);
|
2024-01-14 11:54:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const persistInstalledPlugins = async () => {
|
2024-02-11 09:51:42 +01:00
|
|
|
const installedPlugins = {plugins: []};
|
2024-01-14 11:54:57 +01:00
|
|
|
for (const pkg of Object.values(await plugins.getPackages())) {
|
|
|
|
installedPlugins.plugins.push({
|
|
|
|
name: pkg.name,
|
|
|
|
version: pkg.version,
|
2024-02-11 09:51:42 +01:00
|
|
|
});
|
2020-11-13 13:59:20 -05:00
|
|
|
}
|
2024-01-14 11:54:57 +01:00
|
|
|
installedPlugins.plugins = [...new Set(installedPlugins.plugins)];
|
|
|
|
await fs.writeFile(installedPluginsPath, JSON.stringify(installedPlugins));
|
2024-02-11 09:51:42 +01:00
|
|
|
};
|
2024-01-14 11:54:57 +01:00
|
|
|
|
|
|
|
exports.uninstall = async (pluginName, cb = null) => {
|
|
|
|
cb = wrapTaskCb(cb);
|
|
|
|
logger.info(`Uninstalling plugin ${pluginName}...`);
|
|
|
|
await exports.manager.uninstall(pluginName);
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.info(`Successfully uninstalled plugin ${pluginName}`);
|
2021-02-18 02:36:50 -05:00
|
|
|
await hooks.aCallAll('pluginUninstall', {pluginName});
|
2020-11-13 13:59:20 -05:00
|
|
|
cb(null);
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|
|
|
|
|
2020-11-13 13:59:20 -05:00
|
|
|
exports.install = async (pluginName, cb = null) => {
|
2019-02-08 23:20:57 +01:00
|
|
|
cb = wrapTaskCb(cb);
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.info(`Installing plugin ${pluginName}...`);
|
2024-01-14 11:54:57 +01:00
|
|
|
await exports.manager.install(pluginName);
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.info(`Successfully installed plugin ${pluginName}`);
|
2021-02-18 02:36:50 -05:00
|
|
|
await hooks.aCallAll('pluginInstall', {pluginName});
|
2020-11-13 13:59:20 -05:00
|
|
|
cb(null);
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|
2012-03-15 18:25:06 +01:00
|
|
|
|
2013-03-25 16:51:12 +01:00
|
|
|
exports.availablePlugins = null;
|
2020-11-23 13:24:19 -05:00
|
|
|
let cacheTimestamp = 0;
|
2012-04-18 13:43:34 +02:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
exports.getAvailablePlugins = (maxCacheAge) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const nowTimestamp = Math.round(Date.now() / 1000);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2023-06-27 21:20:53 +02:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2024-01-14 11:54:57 +01:00
|
|
|
// check cache age before making any request
|
|
|
|
if (exports.availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
|
|
|
|
return resolve(exports.availablePlugins);
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2024-02-11 09:51:42 +01:00
|
|
|
await axios.get('https://static.etherpad.org/plugins.json', {headers})
|
2024-01-14 11:54:57 +01:00
|
|
|
.then((pluginsLoaded) => {
|
|
|
|
exports.availablePlugins = pluginsLoaded.data;
|
|
|
|
cacheTimestamp = nowTimestamp;
|
2024-02-11 09:51:42 +01:00
|
|
|
resolve(exports.availablePlugins);
|
|
|
|
})
|
|
|
|
.catch(async (err) => reject(err));
|
2024-01-14 11:54:57 +01:00
|
|
|
});
|
|
|
|
};
|
2013-03-25 16:51:12 +01:00
|
|
|
|
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
exports.search = (searchTerm, maxCacheAge) => exports.getAvailablePlugins(maxCacheAge).then(
|
|
|
|
(results) => {
|
|
|
|
const res = {};
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
if (searchTerm) {
|
|
|
|
searchTerm = searchTerm.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const pluginName in results) {
|
|
|
|
// for every available plugin
|
|
|
|
// TODO: Also search in keywords here!
|
|
|
|
if (pluginName.indexOf(plugins.prefix) !== 0) continue;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
if (searchTerm && !~results[pluginName].name.toLowerCase().indexOf(searchTerm) &&
|
2024-01-30 22:27:31 +01:00
|
|
|
(typeof results[pluginName].description !== 'undefined' &&
|
|
|
|
!~results[pluginName].description.toLowerCase().indexOf(searchTerm))
|
2021-01-19 16:37:12 +00:00
|
|
|
) {
|
|
|
|
if (typeof results[pluginName].description === 'undefined') {
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.debug(`plugin without Description: ${results[pluginName].name}`);
|
2021-01-19 16:37:12 +00:00
|
|
|
}
|
2013-09-23 19:55:35 +02:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
continue;
|
2020-11-23 13:24:19 -05:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
res[pluginName] = results[pluginName];
|
2014-07-03 14:24:41 +02:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
return res;
|
2013-03-25 16:51:12 +01:00
|
|
|
}
|
2021-01-19 16:37:12 +00:00
|
|
|
);
|