1 import { PluginModel } from '../../models/server/plugin'
2 import { logger } from '../../helpers/logger'
3 import { RegisterHookOptions } from '../../../shared/models/plugins/register.model'
4 import { join } from 'path'
5 import { CONFIG } from '../../initializers/config'
6 import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins'
7 import { PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model'
8 import { PluginLibrary } from '../../../shared/models/plugins/plugin-library.model'
9 import { createReadStream, createWriteStream } from 'fs'
10 import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants'
11 import { PluginType } from '../../../shared/models/plugins/plugin.type'
13 export interface RegisteredPlugin {
17 peertubeEngine: string
23 staticDirs: { [name: string]: string }
27 // Only if this is a plugin
31 export interface HookInformationValue {
37 export class PluginManager {
39 private static instance: PluginManager
41 private registeredPlugins: { [ name: string ]: RegisteredPlugin } = {}
42 private hooks: { [ name: string ]: HookInformationValue[] } = {}
44 private constructor () {
47 async registerPlugins () {
48 const plugins = await PluginModel.listEnabledPluginsAndThemes()
50 for (const plugin of plugins) {
52 await this.registerPluginOrTheme(plugin)
54 logger.error('Cannot register plugin %s, skipping.', plugin.name, { err })
58 this.sortHooksByPriority()
61 getRegisteredPlugin (name: string) {
62 return this.registeredPlugins[ name ]
65 getRegisteredTheme (name: string) {
66 const registered = this.getRegisteredPlugin(name)
68 if (!registered || registered.type !== PluginType.THEME) return undefined
73 async unregister (name: string) {
74 const plugin = this.getRegisteredPlugin(name)
77 throw new Error(`Unknown plugin ${name} to unregister`)
80 if (plugin.type === PluginType.THEME) {
81 throw new Error(`Cannot unregister ${name}: this is a theme`)
84 await plugin.unregister()
87 private async registerPluginOrTheme (plugin: PluginModel) {
88 logger.info('Registering plugin or theme %s.', plugin.name)
90 const pluginPath = join(CONFIG.STORAGE.PLUGINS_DIR, plugin.name, plugin.version)
91 const packageJSON: PluginPackageJson = require(join(pluginPath, 'package.json'))
93 if (!isPackageJSONValid(packageJSON, plugin.type)) {
94 throw new Error('Package.JSON is invalid.')
97 let library: PluginLibrary
98 if (plugin.type === PluginType.PLUGIN) {
99 library = await this.registerPlugin(plugin, pluginPath, packageJSON)
102 this.registeredPlugins[ plugin.name ] = {
105 version: plugin.version,
106 description: plugin.description,
107 peertubeEngine: plugin.peertubeEngine,
109 staticDirs: packageJSON.staticDirs,
110 css: packageJSON.css,
111 unregister: library ? library.unregister : undefined
115 private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) {
116 const registerHook = (options: RegisterHookOptions) => {
117 if (!this.hooks[options.target]) this.hooks[options.target] = []
119 this.hooks[options.target].push({
120 pluginName: plugin.name,
121 handler: options.handler,
122 priority: options.priority || 0
126 const library: PluginLibrary = require(join(pluginPath, packageJSON.library))
127 if (!isLibraryCodeValid(library)) {
128 throw new Error('Library code is not valid (miss register or unregister function)')
131 library.register({ registerHook })
133 logger.info('Add plugin %s CSS to global file.', plugin.name)
135 await this.addCSSToGlobalFile(pluginPath, packageJSON.css)
140 private sortHooksByPriority () {
141 for (const hookName of Object.keys(this.hooks)) {
142 this.hooks[hookName].sort((a, b) => {
143 return b.priority - a.priority
148 private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) {
149 for (const cssPath of cssRelativePaths) {
150 await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH)
154 private concatFiles (input: string, output: string) {
155 return new Promise<void>((res, rej) => {
156 const outputStream = createWriteStream(input)
157 const inputStream = createReadStream(output)
159 inputStream.pipe(outputStream)
161 inputStream.on('end', () => res())
162 inputStream.on('error', err => rej(err))
166 static get Instance () {
167 return this.instance || (this.instance = new this())