Improve plugins docker build and fixed plugin loading when dependencies are specified (#6164)

* Install pnpm only local - not global

* Install plugins during docker build with live-plugin-manager

* Migrated installer to ts.

* Added missing workspace script.

* Fixed docker build.

* Fix Dockerfile

* Fixed installer not being yet initialized.

* Ported installer to correct install path.

* Fixed pnpm installation.

* Fixed docker build.

* Fixed plugin loading.

* Fixed plugins not being able to be loaded.

* Fix plugin installation instructions in README

* Fixed startup.

* Fixed folder not present.

* Added unlinking dependencies.

* Added deleting dependencies.

* Fixed listing plugins.

---------

Co-authored-by: SamTV12345 <40429738+samtv12345@users.noreply.github.com>
This commit is contained in:
Stefan Müller 2024-03-14 16:06:32 +01:00 committed by GitHub
parent f9e3416d78
commit fe106f0afc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 413 additions and 66 deletions

View file

@ -108,8 +108,8 @@ FROM build as development
COPY --chown=etherpad:etherpad ./src/package.json .npmrc ./src/pnpm-lock.yaml ./src/ COPY --chown=etherpad:etherpad ./src/package.json .npmrc ./src/pnpm-lock.yaml ./src/
COPY --chown=etherpad:etherpad --from=adminBuild /opt/etherpad-lite/admin/dist ./src/templates/admin COPY --chown=etherpad:etherpad --from=adminBuild /opt/etherpad-lite/admin/dist ./src/templates/admin
RUN bin/installDeps.sh && { [ -z "${ETHERPAD_PLUGINS}" ] || \ RUN bin/installDeps.sh && \
pnpm install --workspace-root ${ETHERPAD_PLUGINS}; } { [ -z "${ETHERPAD_PLUGINS}" ] || pnpm run install-plugins ${ETHERPAD_PLUGINS}; }
FROM build as production FROM build as production
@ -119,9 +119,9 @@ ENV ETHERPAD_PRODUCTION=true
COPY --chown=etherpad:etherpad ./src ./src COPY --chown=etherpad:etherpad ./src ./src
COPY --chown=etherpad:etherpad --from=adminBuild /opt/etherpad-lite/admin/dist ./src/templates/admin COPY --chown=etherpad:etherpad --from=adminBuild /opt/etherpad-lite/admin/dist ./src/templates/admin
RUN bin/installDeps.sh && { [ -z "${ETHERPAD_PLUGINS}" ] || \ RUN bin/installDeps.sh && rm -rf ~/.npm && \
pnpm install --workspace-root ${ETHERPAD_PLUGINS}; } && \ { [ -z "${ETHERPAD_PLUGINS}" ] || pnpm run install-plugins ${ETHERPAD_PLUGINS}; }
rm -rf ~/.npm
# Copy the configuration file. # Copy the configuration file.
COPY --chown=etherpad:etherpad ${SETTINGS} "${EP_DIR}"/settings.json COPY --chown=etherpad:etherpad ${SETTINGS} "${EP_DIR}"/settings.json

View file

@ -139,9 +139,7 @@ Alternatively, you can install plugins from the command line:
```sh ```sh
cd /path/to/etherpad-lite cd /path/to/etherpad-lite
# The `--no-save` and `--legacy-peer-deps` arguments are necessary to work pnpm run install-plugins ep_${plugin_name}
# around npm quirks.
npm install --no-save --legacy-peer-deps ep_${plugin_name}
``` ```
Also see [the plugin wiki Also see [the plugin wiki
@ -153,7 +151,7 @@ Run the following command in your Etherpad folder to get all of the features
visible in the above demo gif: visible in the above demo gif:
```sh ```sh
npm install --no-save --legacy-peer-deps \ pnpm run install-plugins \
ep_align \ ep_align \
ep_comments_page \ ep_comments_page \
ep_embedded_hyperlinks2 \ ep_embedded_hyperlinks2 \

37
bin/installPlugins.ts Normal file
View file

@ -0,0 +1,37 @@
'use strict';
import {writeFileSync} from 'fs'
import {manager, installedPluginsPath} from "ep_etherpad-lite/static/js/pluginfw/installer";
import {PackageData} from "ep_etherpad-lite/node/types/PackageInfo";
const pluginsModule = require('ep_etherpad-lite/static/js/pluginfw/plugins');
if (process.argv.length === 2) {
console.error('Expected at least one argument!');
process.exit(1);
}
const plugins = process.argv.slice(2);
const persistInstalledPlugins = async () => {
const plugins:PackageData[] = []
const installedPlugins = {plugins: plugins};
for (const pkg of Object.values(await pluginsModule.getPackages()) as PackageData[]) {
installedPlugins.plugins.push({
name: pkg.name,
version: pkg.version,
});
}
installedPlugins.plugins = [...new Set(installedPlugins.plugins)];
writeFileSync(installedPluginsPath, JSON.stringify(installedPlugins));
};
async function run() {
for (const plugin of plugins) {
await manager.install(plugin);
}
}
(async () => {
await run();
await persistInstalledPlugins();
})();

View file

@ -30,7 +30,8 @@
"migrateDirtyDBtoRealDB": "node --import tsx migrateDirtyDBtoRealDB.ts", "migrateDirtyDBtoRealDB": "node --import tsx migrateDirtyDBtoRealDB.ts",
"rebuildPad": "node --import tsx rebuildPad.ts", "rebuildPad": "node --import tsx rebuildPad.ts",
"stalePlugins": "node --import tsx ./plugins/stalePlugins.ts", "stalePlugins": "node --import tsx ./plugins/stalePlugins.ts",
"checkPlugins": "node --import tsx ./plugins/checkPlugins.ts" "checkPlugins": "node --import tsx ./plugins/checkPlugins.ts",
"install-plugins": "node --import tsx ./installPlugins.ts"
}, },
"author": "", "author": "",
"license": "ISC" "license": "ISC"

View file

@ -29,6 +29,14 @@ fi
# Prepare the environment # Prepare the environment
bin/installDeps.sh "$@" || exit 1 bin/installDeps.sh "$@" || exit 1
## Create the admin ui
if [ -z "$NODE_ENV" ] || [ "$NODE_ENV" = "development" ]; then
log "Creating the admin UI..."
(cd ../admin && pnpm run build)
else
log "Cannot create the admin UI in production mode"
fi
# Move to the node folder and start # Move to the node folder and start
log "Starting Etherpad..." log "Starting Etherpad..."

View file

@ -2,6 +2,7 @@
"name": "etherpad", "name": "etherpad",
"description": "A free and open source realtime collaborative editor", "description": "A free and open source realtime collaborative editor",
"homepage": "https://etherpad.org", "homepage": "https://etherpad.org",
"type": "module",
"keywords": [ "keywords": [
"etherpad", "etherpad",
"realtime", "realtime",
@ -22,7 +23,8 @@
"test-ui": "pnpm --filter ep_etherpad-lite run test-ui", "test-ui": "pnpm --filter ep_etherpad-lite run test-ui",
"test-ui:ui": "pnpm --filter ep_etherpad-lite run test-ui:ui", "test-ui:ui": "pnpm --filter ep_etherpad-lite run test-ui:ui",
"test-admin": "pnpm --filter ep_etherpad-lite run test-admin", "test-admin": "pnpm --filter ep_etherpad-lite run test-admin",
"test-admin:ui": "pnpm --filter ep_etherpad-lite run test-admin:ui" "test-admin:ui": "pnpm --filter ep_etherpad-lite run test-admin:ui",
"install-plugins": "pnpm --filter bin run install-plugins"
}, },
"dependencies": { "dependencies": {
"ep_etherpad-lite": "workspace:./src" "ep_etherpad-lite": "workspace:./src"
@ -42,3 +44,4 @@
"version": "1.9.7", "version": "1.9.7",
"license": "Apache-2.0" "license": "Apache-2.0"
} }

View file

@ -26,6 +26,7 @@ const hooks = require('../../static/js/pluginfw/hooks.js');
const path = require('path'); const path = require('path');
const resolve = require('resolve'); const resolve = require('resolve');
const settings = require('../utils/Settings'); const settings = require('../utils/Settings');
import {pluginInstallPath} from '../../static/js/pluginfw/installer'
const templateCache = new Map(); const templateCache = new Map();
@ -82,7 +83,13 @@ exports.require = (name:string, args:{
basedir = path.dirname(mod.filename); basedir = path.dirname(mod.filename);
paths = mod.paths; paths = mod.paths;
} }
paths.push(settings.root + '/plugin_packages')
/**
* Add the plugin install path to the paths array
*/
if (!paths.includes(pluginInstallPath)) {
paths.push(pluginInstallPath)
}
const ejspath = resolve.sync(name, {paths, basedir, extensions: ['.html', '.ejs']}); const ejspath = resolve.sync(name, {paths, basedir, extensions: ['.html', '.ejs']});

View file

@ -1,17 +1,14 @@
'use strict'; 'use strict';
import {ArgsExpressType} from "../../types/ArgsExpressType"; import {ArgsExpressType} from "../../types/ArgsExpressType";
import {Socket} from "node:net";
import {ErrorCaused} from "../../types/ErrorCaused"; import {ErrorCaused} from "../../types/ErrorCaused";
import {QueryType} from "../../types/QueryType"; import {QueryType} from "../../types/QueryType";
import {PluginType} from "../../types/Plugin";
const eejs = require('../../eejs'); import {getAvailablePlugins, install, search, uninstall} from "../../../static/js/pluginfw/installer";
const settings = require('../../utils/Settings'); import {PackageData} from "../../types/PackageInfo";
const installer = require('../../../static/js/pluginfw/installer');
const pluginDefs = require('../../../static/js/pluginfw/plugin_defs'); const pluginDefs = require('../../../static/js/pluginfw/plugin_defs');
const plugins = require('../../../static/js/pluginfw/plugins'); import semver from 'semver';
const semver = require('semver');
exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => { exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
@ -32,7 +29,7 @@ exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
socket.on('checkUpdates', async () => { socket.on('checkUpdates', async () => {
// Check plugins for updates // Check plugins for updates
try { try {
const results = await installer.getAvailablePlugins(/* maxCacheAge:*/ 60 * 10); const results = await getAvailablePlugins(/* maxCacheAge:*/ 60 * 10);
const updatable = Object.keys(pluginDefs.plugins).filter((plugin) => { const updatable = Object.keys(pluginDefs.plugins).filter((plugin) => {
if (!results[plugin]) return false; if (!results[plugin]) return false;
@ -54,7 +51,7 @@ exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
socket.on('getAvailable', async (query:string) => { socket.on('getAvailable', async (query:string) => {
try { try {
const results = await installer.getAvailablePlugins(/* maxCacheAge:*/ false); const results = await getAvailablePlugins(/* maxCacheAge:*/ false);
socket.emit('results:available', results); socket.emit('results:available', results);
} catch (er) { } catch (er) {
console.error(er); console.error(er);
@ -64,7 +61,7 @@ exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
socket.on('search', async (query: QueryType) => { socket.on('search', async (query: QueryType) => {
try { try {
const results = await installer.search(query.searchTerm, /* maxCacheAge:*/ 60 * 10); const results = await search(query.searchTerm, /* maxCacheAge:*/ 60 * 10);
let res = Object.keys(results) let res = Object.keys(results)
.map((pluginName) => results[pluginName]) .map((pluginName) => results[pluginName])
.filter((plugin) => !pluginDefs.plugins[plugin.name]); .filter((plugin) => !pluginDefs.plugins[plugin.name]);
@ -79,7 +76,7 @@ exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
}); });
socket.on('install', (pluginName: string) => { socket.on('install', (pluginName: string) => {
installer.install(pluginName, (err: ErrorCaused) => { install(pluginName, (err: ErrorCaused) => {
if (err) console.warn(err.stack || err.toString()); if (err) console.warn(err.stack || err.toString());
socket.emit('finished:install', { socket.emit('finished:install', {
@ -91,7 +88,7 @@ exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
}); });
socket.on('uninstall', (pluginName:string) => { socket.on('uninstall', (pluginName:string) => {
installer.uninstall(pluginName, (err:ErrorCaused) => { uninstall(pluginName, (err:ErrorCaused) => {
if (err) console.warn(err.stack || err.toString()); if (err) console.warn(err.stack || err.toString());
socket.emit('finished:uninstall', {plugin: pluginName, error: err ? err.message : null}); socket.emit('finished:uninstall', {plugin: pluginName, error: err ? err.message : null});
@ -108,7 +105,7 @@ exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
* @param {String} dir The directory of the plugin * @param {String} dir The directory of the plugin
* @return {Object[]} * @return {Object[]}
*/ */
const sortPluginList = (plugins:PluginType[], property:string, /* ASC?*/dir:string): object[] => plugins.sort((a, b) => { const sortPluginList = (plugins:PackageData[], property:string, /* ASC?*/dir:string): PackageData[] => plugins.sort((a, b) => {
// @ts-ignore // @ts-ignore
if (a[property] < b[property]) { if (a[property] < b[property]) {
return dir ? -1 : 1; return dir ? -1 : 1;

View file

@ -1,7 +1,5 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict';
/** /**
* This module is started with src/bin/run.sh. It sets up a Express HTTP and a Socket.IO Server. * This module is started with src/bin/run.sh. It sets up a Express HTTP and a Socket.IO Server.
* Static file Requests are answered directly from this module, Socket.IO messages are passed * Static file Requests are answered directly from this module, Socket.IO messages are passed
@ -26,10 +24,10 @@
import {PluginType} from "./types/Plugin"; import {PluginType} from "./types/Plugin";
import {ErrorCaused} from "./types/ErrorCaused"; import {ErrorCaused} from "./types/ErrorCaused";
import {PromiseHooks} from "node:v8";
import log4js from 'log4js'; import log4js from 'log4js';
import {checkForMigration} from "../static/js/pluginfw/installer";
const settings = require('./utils/Settings'); const settings = require('./utils/Settings');
let wtfnode: any; let wtfnode: any;
@ -53,7 +51,6 @@ const express = require('./hooks/express');
const hooks = require('../static/js/pluginfw/hooks'); const hooks = require('../static/js/pluginfw/hooks');
const pluginDefs = require('../static/js/pluginfw/plugin_defs'); const pluginDefs = require('../static/js/pluginfw/plugin_defs');
const plugins = require('../static/js/pluginfw/plugins'); const plugins = require('../static/js/pluginfw/plugins');
const installer = require('../static/js/pluginfw/installer');
const {Gate} = require('./utils/promises'); const {Gate} = require('./utils/promises');
const stats = require('./stats') const stats = require('./stats')
@ -147,7 +144,7 @@ exports.start = async () => {
} }
await db.init(); await db.init();
await installer.checkForMigration(); await checkForMigration();
await plugins.update(); await plugins.update();
const installedPlugins = (Object.values(pluginDefs.plugins) as PluginType[]) const installedPlugins = (Object.values(pluginDefs.plugins) as PluginType[])
.filter((plugin) => plugin.package.name !== 'ep_etherpad-lite') .filter((plugin) => plugin.package.name !== 'ep_etherpad-lite')

View file

@ -0,0 +1,20 @@
export type PackageInfo = {
from: string,
name: string,
version: string,
resolved: string,
description: string,
license: string,
author: {
name: string
},
homepage: string,
repository: string,
path: string
}
export type PackageData = {
version: string,
name: string
}

View file

@ -87,6 +87,7 @@
"@types/node": "^20.11.27", "@types/node": "^20.11.27",
"@types/sinon": "^17.0.3", "@types/sinon": "^17.0.3",
"@types/supertest": "^6.0.2", "@types/supertest": "^6.0.2",
"@types/semver": "^7.5.7",
"@types/underscore": "^1.11.15", "@types/underscore": "^1.11.15",
"eslint": "^8.57.0", "eslint": "^8.57.0",
"eslint-config-etherpad": "^3.0.22", "eslint-config-etherpad": "^3.0.22",

View file

@ -145,6 +145,9 @@ devDependencies:
'@types/node': '@types/node':
specifier: ^20.11.19 specifier: ^20.11.19
version: 20.11.19 version: 20.11.19
'@types/semver':
specifier: ^7.5.7
version: 7.5.7
'@types/underscore': '@types/underscore':
specifier: ^1.11.15 specifier: ^1.11.15
version: 1.11.15 version: 1.11.15

View file

@ -0,0 +1,230 @@
import {IPluginInfo, PluginManager} from "live-plugin-manager-pnpm";
import path from "path";
import {node_modules, pluginInstallPath} from "./installer";
import {accessSync, constants, rmSync, symlinkSync, unlinkSync} from "node:fs";
import {dependencies, name} from '../../../package.json'
import {pathToFileURL} from 'node:url';
const settings = require('../../../node/utils/Settings');
import {readFileSync} from "fs";
export class LinkInstaller {
private livePluginManager: PluginManager;
private loadedPlugins: IPluginInfo[] = [];
/*
* A map of dependencies to their dependents
*
*/
private readonly dependenciesMap: Map<string, Set<string>>;
constructor() {
this.livePluginManager = new PluginManager({
pluginsPath: pluginInstallPath,
cwd: path.join(settings.root, 'src')
});
this.dependenciesMap = new Map();
}
public async init() {
// Insert Etherpad lite dependencies
for (let [dependency] of Object.entries(dependencies)) {
if (this.dependenciesMap.has(dependency)) {
this.dependenciesMap.get(dependency)?.add(name)
} else {
this.dependenciesMap.set(dependency, new Set([name]))
}
}
}
public async installFromPath(path: string) {
const installedPlugin = await this.livePluginManager.installFromPath(path)
await this.checkLinkedDependencies(installedPlugin)
}
public async installPlugin(pluginName: string, version?: string) {
if (version) {
const installedPlugin = await this.livePluginManager.install(pluginName, version);
this.linkDependency(pluginName)
await this.checkLinkedDependencies(installedPlugin)
} else {
const installedPlugin = await this.livePluginManager.install(pluginName);
this.linkDependency(pluginName)
await this.checkLinkedDependencies(installedPlugin)
}
}
public async listPlugins() {
const plugins = this.livePluginManager.list()
if (plugins && plugins.length > 0 && this.loadedPlugins.length == 0) {
this.loadedPlugins = plugins
// Check already installed plugins
for (let plugin of plugins) {
await this.checkLinkedDependencies(plugin)
}
}
return plugins
}
public async uninstallPlugin(pluginName: string) {
const installedPlugin = this.livePluginManager.getInfo(pluginName)
if (installedPlugin) {
console.debug(`Uninstalling plugin ${pluginName}`)
await this.removeSymlink(installedPlugin)
await this.livePluginManager.uninstall(pluginName)
await this.removeSubDependencies(installedPlugin)
}
}
private async removeSubDependencies(plugin: IPluginInfo) {
const pluginDependencies = Object.keys(plugin.dependencies)
console.debug("Removing sub dependencies",pluginDependencies)
for (let dependency of pluginDependencies) {
await this.removeSubDependency(plugin.name, dependency)
}
}
private async removeSubDependency(_name: string, dependency:string) {
if (this.dependenciesMap.has(dependency)) {
console.debug(`Dependency ${dependency} is still being used by other plugins`)
return
}
// Read sub dependencies
try {
const json:IPluginInfo = JSON.parse(
readFileSync(pathToFileURL(path.join(pluginInstallPath, dependency, 'package.json'))) as unknown as string);
if(json.dependencies){
for (let [subDependency] of Object.entries(json.dependencies)) {
await this.removeSubDependency(dependency, subDependency)
}
}
} catch (e){}
this.uninstallDependency(dependency)
}
private uninstallDependency(dependency: string) {
try {
console.debug(`Uninstalling dependency ${dependency}`)
// Check if the dependency is already installed
accessSync(path.join(pluginInstallPath, dependency), constants.F_OK)
rmSync(path.join(pluginInstallPath, dependency), {
force: true,
recursive: true
})
} catch (err) {
// Symlink does not exist
// So nothing to do
}
}
private async removeSymlink(plugin: IPluginInfo) {
try {
accessSync(path.join(node_modules, plugin.name), constants.F_OK)
await this.unlinkSubDependencies(plugin)
// Remove the plugin itself
this.unlinkDependency(plugin.name)
} catch (err) {
console.error(`Symlink for ${plugin.name} does not exist`)
// Symlink does not exist
// So nothing to do
}
}
private async unlinkSubDependencies(plugin: IPluginInfo) {
const pluginDependencies = Object.keys(plugin.dependencies)
for (let dependency of pluginDependencies) {
this.dependenciesMap.get(dependency)?.delete(plugin.name)
await this.unlinkSubDependency(plugin.name, dependency)
}
}
private async unlinkSubDependency(plugin: string, dependency: string) {
if (this.dependenciesMap.has(dependency)) {
this.dependenciesMap.get(dependency)?.delete(plugin)
if (this.dependenciesMap.get(dependency)!.size > 0) {
// We have other dependants so do not uninstall
return
}
}
this.unlinkDependency(dependency)
// Read sub dependencies
try {
const json:IPluginInfo = JSON.parse(
readFileSync(pathToFileURL(path.join(pluginInstallPath, dependency, 'package.json'))) as unknown as string);
if(json.dependencies){
for (let [subDependency] of Object.entries(json.dependencies)) {
await this.unlinkSubDependency(dependency, subDependency)
}
}
} catch (e){}
console.debug("Unlinking sub dependency",dependency)
this.dependenciesMap.delete(dependency)
}
private async addSubDependencies(plugin: IPluginInfo) {
const pluginDependencies = Object.keys(plugin.dependencies)
for (let dependency of pluginDependencies) {
await this.addSubDependency(plugin.name, dependency)
}
}
private async addSubDependency(plugin: string, dependency: string) {
if (this.dependenciesMap.has(dependency)) {
// We already added the sub dependency
this.dependenciesMap.get(dependency)?.add(plugin)
} else {
this.linkDependency(dependency)
// Read sub dependencies
try {
const json:IPluginInfo = JSON.parse(
readFileSync(pathToFileURL(path.join(pluginInstallPath, dependency, 'package.json'))) as unknown as string);
if(json.dependencies){
Object.keys(json.dependencies).forEach((subDependency: string) => {
this.addSubDependency(dependency, subDependency)
})
}
} catch (err) {
console.error(`Error reading package.json ${err} for ${pathToFileURL(path.join(pluginInstallPath, dependency, 'package.json')).toString()}`)
}
this.dependenciesMap.set(dependency, new Set([plugin]))
}
}
private linkDependency(dependency: string) {
try {
// Check if the dependency is already installed
accessSync(path.join(node_modules, dependency), constants.F_OK)
} catch (err) {
symlinkSync(path.join(pluginInstallPath, dependency), path.join(node_modules, dependency), 'dir')
}
}
private unlinkDependency(dependency: string) {
try {
// Check if the dependency is already installed
accessSync(path.join(node_modules, dependency), constants.F_OK)
unlinkSync(path.join(node_modules, dependency))
} catch (err) {
// Symlink does not exist
// So nothing to do
}
}
private async checkLinkedDependencies(plugin: IPluginInfo) {
// Check if the plugin really exists at source
try {
accessSync(path.join(pluginInstallPath, plugin.name), constants.F_OK)
// Skip if the plugin is already linked
} catch (err) {
// The plugin is not installed
console.debug(`Plugin ${plugin.name} is not installed`)
}
await this.addSubDependencies(plugin)
this.dependenciesMap.set(plugin.name, new Set())
}
}

View file

@ -1,20 +1,28 @@
'use strict'; 'use strict';
const log4js = require('log4js'); 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";
const plugins = require('./plugins'); const plugins = require('./plugins');
const hooks = require('./hooks'); const hooks = require('./hooks');
const runCmd = require('../../../node/utils/run_cmd'); const runCmd = require('../../../node/utils/run_cmd');
const settings = require('../../../node/utils/Settings'); const settings = require('../../../node/utils/Settings');
const axios = require('axios'); import {LinkInstaller} from "./LinkInstaller";
const {PluginManager} = require('live-plugin-manager-pnpm');
const {promises: fs} = require('fs');
const path = require('path');
const {findEtherpadRoot} = require('../../../node/utils/AbsolutePaths'); const {findEtherpadRoot} = require('../../../node/utils/AbsolutePaths');
const logger = log4js.getLogger('plugins'); const logger = log4js.getLogger('plugins');
exports.manager = new PluginManager(); export const pluginInstallPath = path.join(settings.root, 'src','plugin_packages');
export const node_modules = path.join(findEtherpadRoot(),'src', 'node_modules');
const installedPluginsPath = path.join(settings.root, 'var/installed_plugins.json'); export const installedPluginsPath = path.join(settings.root, 'var/installed_plugins.json');
const onAllTasksFinished = async () => { const onAllTasksFinished = async () => {
await plugins.update(); await plugins.update();
@ -30,10 +38,12 @@ const headers = {
let tasks = 0; let tasks = 0;
const wrapTaskCb = (cb) => { export const linkInstaller = new LinkInstaller();
const wrapTaskCb = (cb:Function|null) => {
tasks++; tasks++;
return (...args) => { return (...args: any) => {
cb && cb(...args); cb && cb(...args);
tasks--; tasks--;
if (tasks === 0) onAllTasksFinished(); if (tasks === 0) onAllTasksFinished();
@ -53,18 +63,23 @@ const migratePluginsFromNodeModules = async () => {
await Promise.all(Object.entries(dependencies) await Promise.all(Object.entries(dependencies)
.filter(([pkg, info]) => pkg.startsWith(plugins.prefix) && pkg !== 'ep_etherpad-lite') .filter(([pkg, info]) => pkg.startsWith(plugins.prefix) && pkg !== 'ep_etherpad-lite')
.map(async ([pkg, info]) => { .map(async ([pkg, info]) => {
if (!info.resolved) { const _info = info as PackageInfo
if (!_info.resolved) {
// Install from node_modules directory // Install from node_modules directory
await exports.manager.installFromPath(`${findEtherpadRoot()}/node_modules/${pkg}`); await linkInstaller.installFromPath(`${findEtherpadRoot()}/node_modules/${pkg}`);
} else { } else {
await exports.manager.install(pkg); await linkInstaller.installPlugin(pkg);
} }
})); }));
await persistInstalledPlugins(); await persistInstalledPlugins();
}; };
exports.checkForMigration = async () => { export const checkForMigration = async () => {
logger.info('check installed plugins for migration'); logger.info('check installed plugins for migration');
// Initialize linkInstaller
await linkInstaller.init()
try { try {
await fs.access(installedPluginsPath, fs.constants.F_OK); await fs.access(installedPluginsPath, fs.constants.F_OK);
@ -72,19 +87,47 @@ exports.checkForMigration = async () => {
await migratePluginsFromNodeModules(); await migratePluginsFromNodeModules();
} }
/*
* Check if the plugin is already installed in node_modules
* If not, create a symlink to node_modules
* This is necessary as
* 1. Live Plugin Manager does not support loading plugins from the directory so that node can access them normally
* 2. Plugins can't be directly installed to node_modules otherwise upgrading Etherpad will remove them
*/
fs.stat(pluginInstallPath).then(async (err) => {
const files = await fs.readdir(pluginInstallPath);
for (let file of files){
const moduleName = path.basename(file);
try {
await fs.access(path.join(node_modules, moduleName), fs.constants.F_OK);
logger.debug(`plugin ${moduleName} already exists in node_modules`);
} catch (err) {
// Create symlink to node_modules
logger.debug(`create symlink for ${file} to ${path.join(node_modules,moduleName)}`)
await fs.symlink(path.join(pluginInstallPath,file), path.join(node_modules,moduleName), 'dir')
}
}
}).catch(()=>{
logger.debug('plugin directory does not exist');
})
const fileContent = await fs.readFile(installedPluginsPath); const fileContent = await fs.readFile(installedPluginsPath);
const installedPlugins = JSON.parse(fileContent.toString()); const installedPlugins = JSON.parse(fileContent.toString());
for (const plugin of installedPlugins.plugins) { for (const plugin of installedPlugins.plugins) {
if (plugin.name.startsWith(plugins.prefix) && plugin.name !== 'ep_etherpad-lite') { if (plugin.name.startsWith(plugins.prefix) && plugin.name !== 'ep_etherpad-lite') {
await exports.manager.install(plugin.name, plugin.version); await linkInstaller.installPlugin(plugin.name, plugin.version);
} }
} }
}; };
const persistInstalledPlugins = async () => { const persistInstalledPlugins = async () => {
const installedPlugins = {plugins: []}; const installedPlugins:{
for (const pkg of Object.values(await plugins.getPackages())) { plugins: PackageData[]
} = {plugins: []};
for (const pkg of Object.values(await plugins.getPackages()) as PackageData[]) {
installedPlugins.plugins.push({ installedPlugins.plugins.push({
name: pkg.name, name: pkg.name,
version: pkg.version, version: pkg.version,
@ -94,50 +137,51 @@ const persistInstalledPlugins = async () => {
await fs.writeFile(installedPluginsPath, JSON.stringify(installedPlugins)); await fs.writeFile(installedPluginsPath, JSON.stringify(installedPlugins));
}; };
exports.uninstall = async (pluginName, cb = null) => { export const uninstall = async (pluginName: string, cb:Function|null = null) => {
cb = wrapTaskCb(cb); cb = wrapTaskCb(cb);
logger.info(`Uninstalling plugin ${pluginName}...`); logger.info(`Uninstalling plugin ${pluginName}...`);
await exports.manager.uninstall(pluginName);
await linkInstaller.uninstallPlugin(pluginName);
logger.info(`Successfully uninstalled plugin ${pluginName}`); logger.info(`Successfully uninstalled plugin ${pluginName}`);
await hooks.aCallAll('pluginUninstall', {pluginName}); await hooks.aCallAll('pluginUninstall', {pluginName});
cb(null); cb(null);
}; };
exports.install = async (pluginName, cb = null) => { export const install = async (pluginName: string, cb:Function|null = null) => {
cb = wrapTaskCb(cb); cb = wrapTaskCb(cb);
logger.info(`Installing plugin ${pluginName}...`); logger.info(`Installing plugin ${pluginName}...`);
await exports.manager.install(pluginName); await linkInstaller.installPlugin(pluginName);
logger.info(`Successfully installed plugin ${pluginName}`); logger.info(`Successfully installed plugin ${pluginName}`);
await hooks.aCallAll('pluginInstall', {pluginName}); await hooks.aCallAll('pluginInstall', {pluginName});
cb(null); cb(null);
}; };
exports.availablePlugins = null; export let availablePlugins:MapArrayType<PackageInfo>|null = null;
let cacheTimestamp = 0; let cacheTimestamp = 0;
exports.getAvailablePlugins = (maxCacheAge) => { export const getAvailablePlugins = (maxCacheAge: number|false) => {
const nowTimestamp = Math.round(Date.now() / 1000); const nowTimestamp = Math.round(Date.now() / 1000);
return new Promise(async (resolve, reject) => { return new Promise<MapArrayType<PackageInfo>>(async (resolve, reject) => {
// check cache age before making any request // check cache age before making any request
if (exports.availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) { if (availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
return resolve(exports.availablePlugins); return resolve(availablePlugins);
} }
await axios.get('https://static.etherpad.org/plugins.json', {headers}) await axios.get('https://static.etherpad.org/plugins.json', {headers})
.then((pluginsLoaded) => { .then((pluginsLoaded:AxiosResponse<MapArrayType<PackageInfo>>) => {
exports.availablePlugins = pluginsLoaded.data; availablePlugins = pluginsLoaded.data;
cacheTimestamp = nowTimestamp; cacheTimestamp = nowTimestamp;
resolve(exports.availablePlugins); resolve(availablePlugins);
}) })
.catch(async (err) => reject(err)); .catch(async (err) => reject(err));
}); });
}; };
exports.search = (searchTerm, maxCacheAge) => exports.getAvailablePlugins(maxCacheAge).then( export const search = (searchTerm: string, maxCacheAge: number) => getAvailablePlugins(maxCacheAge).then(
(results) => { (results: MapArrayType<PackageInfo>) => {
const res = {}; const res:MapArrayType<PackageData> = {};
if (searchTerm) { if (searchTerm) {
searchTerm = searchTerm.toLowerCase(); searchTerm = searchTerm.toLowerCase();

View file

@ -8,7 +8,6 @@ const runCmd = require('../../../node/utils/run_cmd');
const tsort = require('./tsort'); const tsort = require('./tsort');
const pluginUtils = require('./shared'); const pluginUtils = require('./shared');
const defs = require('./plugin_defs'); const defs = require('./plugin_defs');
const {manager} = require('./installer');
const settings = require('../../../node/utils/Settings'); const settings = require('../../../node/utils/Settings');
const logger = log4js.getLogger('plugins'); const logger = log4js.getLogger('plugins');
@ -122,7 +121,8 @@ exports.update = async () => {
}; };
exports.getPackages = async () => { exports.getPackages = async () => {
const plugins = manager.list(); const {linkInstaller} = require("./installer");
const plugins = await linkInstaller.listPlugins();
const newDependencies = {}; const newDependencies = {};
for (const plugin of plugins) { for (const plugin of plugins) {

View file

@ -6,12 +6,13 @@
/* Language and Environment */ /* Language and Environment */
"target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ "target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
/* Modules */ /* Modules */
"module": "commonjs", /* Specify what module code is generated. */ "module": "CommonJS", /* Specify what module code is generated. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */ /* Type Checking */
"strict": true, /* Enable all strict type-checking options. */ "strict": true, /* Enable all strict type-checking options. */
/* Completeness */ /* Completeness */
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */,
"resolveJsonModule": true
} }
} }