etherpad-lite/src/static/js/pluginfw/installer.ts

178 lines
5.8 KiB
TypeScript
Raw Normal View History

2021-01-19 16:37:12 +00:00
'use strict';
2024-02-20 20:45:52 +01:00
import log4js from "log4js";
import axios, {AxiosResponse} from "axios";
import {PackageData, PackageInfo} from "../../../node/types/PackageInfo";
import {MapArrayType} from "../../../node/types/MapType";
import path from "path";
import {promises as fs} from "fs";
2021-01-19 16:37:12 +00:00
const plugins = require('./plugins');
const hooks = require('./hooks');
const runCmd = require('../../../node/utils/run_cmd');
2021-02-15 08:52:38 +01:00
const settings = require('../../../node/utils/Settings');
const {PluginManager} = require('live-plugin-manager-pnpm');
2024-02-20 20:45:52 +01:00
const {findEtherpadRoot} = require('../../../node/utils/AbsolutePaths');
const logger = log4js.getLogger('plugins');
2024-02-20 20:45:52 +01:00
export const manager = new PluginManager();
2024-02-20 20:45:52 +01:00
export const installedPluginsPath = path.join(settings.root, 'var/installed_plugins.json');
2021-02-15 08:52:38 +01:00
const onAllTasksFinished = async () => {
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
};
const headers = {
'User-Agent': `Etherpad/${settings.getEpVersion()}`,
};
2020-11-23 13:24:19 -05:00
let tasks = 0;
2024-02-20 20:45:52 +01:00
const wrapTaskCb = (cb:Function|null) => {
tasks++;
2024-02-20 20:45:52 +01:00
return (...args: any) => {
cb && cb(...args);
tasks--;
2021-01-19 16:37:12 +00:00
if (tasks === 0) onAllTasksFinished();
2020-11-23 13:24:19 -05:00
};
};
const migratePluginsFromNodeModules = async () => {
logger.info('start migration of plugins in node_modules');
// 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).
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]) => {
2024-02-20 20:45:52 +01:00
const _info = info as PackageInfo
if (!_info.resolved) {
// Install from node_modules directory
2024-02-20 20:45:52 +01:00
await manager.installFromPath(`${findEtherpadRoot()}/node_modules/${pkg}`);
} else {
2024-02-20 20:45:52 +01:00
await manager.install(pkg);
}
}));
await persistInstalledPlugins();
2024-01-30 22:27:31 +01:00
};
2024-02-20 20:45:52 +01:00
export const checkForMigration = async () => {
2024-01-30 22:27:31 +01:00
logger.info('check installed plugins for migration');
2020-11-13 13:59:20 -05:00
try {
2024-02-20 20:45:52 +01:00
await fs.access(installedPluginsPath, fs.constants.F_OK);
2020-11-13 13:59:20 -05:00
} catch (err) {
await migratePluginsFromNodeModules();
}
2024-02-20 20:45:52 +01:00
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-20 20:45:52 +01:00
await manager.install(plugin.name, plugin.version);
}
}
};
const persistInstalledPlugins = async () => {
2024-02-20 20:45:52 +01:00
const installedPlugins:{
plugins: PackageData[]
} = {plugins: []};
for (const pkg of Object.values(await plugins.getPackages()) as PackageData[]) {
installedPlugins.plugins.push({
name: pkg.name,
version: pkg.version,
});
2020-11-13 13:59:20 -05:00
}
installedPlugins.plugins = [...new Set(installedPlugins.plugins)];
2024-02-20 20:45:52 +01:00
await fs.writeFile(installedPluginsPath, JSON.stringify(installedPlugins));
};
2024-02-20 20:45:52 +01:00
export const uninstall = async (pluginName: string, cb:Function|null = null) => {
cb = wrapTaskCb(cb);
logger.info(`Uninstalling plugin ${pluginName}...`);
2024-02-20 20:45:52 +01:00
await manager.uninstall(pluginName);
logger.info(`Successfully uninstalled plugin ${pluginName}`);
await hooks.aCallAll('pluginUninstall', {pluginName});
2020-11-13 13:59:20 -05:00
cb(null);
2012-03-19 17:16:49 +01:00
};
2024-02-20 20:45:52 +01:00
export const install = async (pluginName: string, cb:Function|null = null) => {
cb = wrapTaskCb(cb);
logger.info(`Installing plugin ${pluginName}...`);
2024-02-20 20:45:52 +01:00
await manager.install(pluginName);
logger.info(`Successfully installed plugin ${pluginName}`);
await hooks.aCallAll('pluginInstall', {pluginName});
2020-11-13 13:59:20 -05:00
cb(null);
2012-03-19 17:16:49 +01:00
};
2024-02-20 20:45:52 +01:00
export let availablePlugins:MapArrayType<PackageInfo>|null = null;
2020-11-23 13:24:19 -05:00
let cacheTimestamp = 0;
2012-04-18 13:43:34 +02:00
2024-02-20 20:45:52 +01:00
export const getAvailablePlugins = (maxCacheAge: number|false) => {
2020-11-23 13:24:19 -05:00
const nowTimestamp = Math.round(Date.now() / 1000);
2024-02-20 20:45:52 +01:00
return new Promise<MapArrayType<PackageInfo>>(async (resolve, reject) => {
// check cache age before making any request
2024-02-20 20:45:52 +01:00
if (availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
return resolve(availablePlugins);
}
await axios.get('https://static.etherpad.org/plugins.json', {headers})
2024-02-20 20:45:52 +01:00
.then((pluginsLoaded:AxiosResponse<MapArrayType<PackageInfo>>) => {
availablePlugins = pluginsLoaded.data;
cacheTimestamp = nowTimestamp;
2024-02-20 20:45:52 +01:00
resolve(availablePlugins);
})
.catch(async (err) => reject(err));
});
};
2024-02-20 20:45:52 +01:00
export const search = (searchTerm: string, maxCacheAge: number) => getAvailablePlugins(maxCacheAge).then(
(results: MapArrayType<PackageInfo>) => {
const res:MapArrayType<PackageData> = {};
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;
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') {
logger.debug(`plugin without Description: ${results[pluginName].name}`);
2021-01-19 16:37:12 +00:00
}
2021-01-19 16:37:12 +00:00
continue;
2020-11-23 13:24:19 -05:00
}
2021-01-19 16:37:12 +00:00
res[pluginName] = results[pluginName];
}
2021-01-19 16:37:12 +00:00
return res;
}
2021-01-19 16:37:12 +00:00
);