1 import * as express from 'express'
2 import { createReadStream, createWriteStream } from 'fs'
3 import { outputFile, readJSON } from 'fs-extra'
4 import { basename, join } from 'path'
5 import { MOAuthTokenUser, MUser } from '@server/types/models'
6 import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
7 import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks'
11 PluginTranslationPaths as PackagePluginTranslations
12 } from '../../../shared/models/plugins/plugin-package-json.model'
13 import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model'
14 import { PluginType } from '../../../shared/models/plugins/plugin.type'
15 import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model'
16 import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins'
17 import { logger } from '../../helpers/logger'
18 import { CONFIG } from '../../initializers/config'
19 import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants'
20 import { PluginModel } from '../../models/server/plugin'
21 import { PluginLibrary, RegisterServerAuthExternalOptions, RegisterServerAuthPassOptions, RegisterServerOptions } from '../../types/plugins'
22 import { ClientHtml } from '../client-html'
23 import { RegisterHelpers } from './register-helpers'
24 import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn'
26 export interface RegisteredPlugin {
31 peertubeEngine: string
37 staticDirs: { [name: string]: string }
38 clientScripts: { [name: string]: ClientScript }
42 // Only if this is a plugin
43 registerHelpers?: RegisterHelpers
47 export interface HookInformationValue {
54 type PluginLocalesTranslations = {
55 [locale: string]: PluginTranslation
58 export class PluginManager implements ServerHook {
60 private static instance: PluginManager
62 private registeredPlugins: { [name: string]: RegisteredPlugin } = {}
64 private hooks: { [name: string]: HookInformationValue[] } = {}
65 private translations: PluginLocalesTranslations = {}
67 private constructor () {
70 // ###################### Getters ######################
72 isRegistered (npmName: string) {
73 return !!this.getRegisteredPluginOrTheme(npmName)
76 getRegisteredPluginOrTheme (npmName: string) {
77 return this.registeredPlugins[npmName]
80 getRegisteredPluginByShortName (name: string) {
81 const npmName = PluginModel.buildNpmName(name, PluginType.PLUGIN)
82 const registered = this.getRegisteredPluginOrTheme(npmName)
84 if (!registered || registered.type !== PluginType.PLUGIN) return undefined
89 getRegisteredThemeByShortName (name: string) {
90 const npmName = PluginModel.buildNpmName(name, PluginType.THEME)
91 const registered = this.getRegisteredPluginOrTheme(npmName)
93 if (!registered || registered.type !== PluginType.THEME) return undefined
98 getRegisteredPlugins () {
99 return this.getRegisteredPluginsOrThemes(PluginType.PLUGIN)
102 getRegisteredThemes () {
103 return this.getRegisteredPluginsOrThemes(PluginType.THEME)
106 getIdAndPassAuths () {
107 return this.getRegisteredPlugins()
112 idAndPassAuths: p.registerHelpers.getIdAndPassAuths()
114 .filter(v => v.idAndPassAuths.length !== 0)
117 getExternalAuths () {
118 return this.getRegisteredPlugins()
123 externalAuths: p.registerHelpers.getExternalAuths()
125 .filter(v => v.externalAuths.length !== 0)
128 getRegisteredSettings (npmName: string) {
129 const result = this.getRegisteredPluginOrTheme(npmName)
130 if (!result || result.type !== PluginType.PLUGIN) return []
132 return result.registerHelpers.getSettings()
135 getRouter (npmName: string) {
136 const result = this.getRegisteredPluginOrTheme(npmName)
137 if (!result || result.type !== PluginType.PLUGIN) return null
139 return result.registerHelpers.getRouter()
142 getTranslations (locale: string) {
143 return this.translations[locale] || {}
146 async isTokenValid (token: MOAuthTokenUser, type: 'access' | 'refresh') {
147 const auth = this.getAuth(token.User.pluginAuth, token.authName)
148 if (!auth) return true
150 if (auth.hookTokenValidity) {
152 const { valid } = await auth.hookTokenValidity({ token, type })
154 if (valid === false) {
155 logger.info('Rejecting %s token validity from auth %s of plugin %s', type, token.authName, token.User.pluginAuth)
160 logger.warn('Cannot run check token validity from auth %s of plugin %s.', token.authName, token.User.pluginAuth, { err })
168 // ###################### External events ######################
170 async onLogout (npmName: string, authName: string, user: MUser, req: express.Request) {
171 const auth = this.getAuth(npmName, authName)
173 if (auth?.onLogout) {
174 logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)
177 // Force await, in case or onLogout returns a promise
178 const result = await auth.onLogout(user, req)
180 return typeof result === 'string'
184 logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })
191 onSettingsChanged (name: string, settings: any) {
192 const registered = this.getRegisteredPluginByShortName(name)
194 logger.error('Cannot find plugin %s to call on settings changed.', name)
197 for (const cb of registered.registerHelpers.getOnSettingsChangedCallbacks()) {
201 logger.error('Cannot run on settings changed callback for %s.', registered.npmName, { err })
206 // ###################### Hooks ######################
208 async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> {
209 if (!this.hooks[hookName]) return Promise.resolve(result)
211 const hookType = getHookType(hookName)
213 for (const hook of this.hooks[hookName]) {
214 logger.debug('Running hook %s of plugin %s.', hookName, hook.npmName)
216 result = await internalRunHook(hook.handler, hookType, result, params, err => {
217 logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err })
224 // ###################### Registration ######################
226 async registerPluginsAndThemes () {
227 await this.resetCSSGlobalFile()
229 const plugins = await PluginModel.listEnabledPluginsAndThemes()
231 for (const plugin of plugins) {
233 await this.registerPluginOrTheme(plugin)
235 // Try to unregister the plugin
237 await this.unregister(PluginModel.buildNpmName(plugin.name, plugin.type))
239 // we don't care if we cannot unregister it
242 logger.error('Cannot register plugin %s, skipping.', plugin.name, { err })
246 this.sortHooksByPriority()
249 // Don't need the plugin type since themes cannot register server code
250 async unregister (npmName: string) {
251 logger.info('Unregister plugin %s.', npmName)
253 const plugin = this.getRegisteredPluginOrTheme(npmName)
256 throw new Error(`Unknown plugin ${npmName} to unregister`)
259 delete this.registeredPlugins[plugin.npmName]
261 this.deleteTranslations(plugin.npmName)
263 if (plugin.type === PluginType.PLUGIN) {
264 await plugin.unregister()
266 // Remove hooks of this plugin
267 for (const key of Object.keys(this.hooks)) {
268 this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName)
271 const store = plugin.registerHelpers
272 store.reinitVideoConstants(plugin.npmName)
273 store.reinitTranscodingProfilesAndEncoders(plugin.npmName)
275 logger.info('Regenerating registered plugin CSS to global file.')
276 await this.regeneratePluginGlobalCSS()
280 // ###################### Installation ######################
282 async install (toInstall: string, version?: string, fromDisk = false) {
283 let plugin: PluginModel
286 logger.info('Installing plugin %s.', toInstall)
290 ? await installNpmPluginFromDisk(toInstall)
291 : await installNpmPlugin(toInstall, version)
293 npmName = fromDisk ? basename(toInstall) : toInstall
294 const pluginType = PluginModel.getTypeFromNpmName(npmName)
295 const pluginName = PluginModel.normalizePluginName(npmName)
297 const packageJSON = await this.getPackageJSON(pluginName, pluginType)
299 this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, pluginType);
301 [ plugin ] = await PluginModel.upsert({
303 description: packageJSON.description,
304 homepage: packageJSON.homepage,
306 version: packageJSON.version,
309 peertubeEngine: packageJSON.engine.peertube
310 }, { returning: true })
312 logger.error('Cannot install plugin %s, removing it...', toInstall, { err })
315 await removeNpmPlugin(npmName)
317 logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err })
323 logger.info('Successful installation of plugin %s.', toInstall)
325 await this.registerPluginOrTheme(plugin)
330 async update (toUpdate: string, version?: string, fromDisk = false) {
331 const npmName = fromDisk ? basename(toUpdate) : toUpdate
333 logger.info('Updating plugin %s.', npmName)
335 // Unregister old hooks
336 await this.unregister(npmName)
338 return this.install(toUpdate, version, fromDisk)
341 async uninstall (npmName: string) {
342 logger.info('Uninstalling plugin %s.', npmName)
345 await this.unregister(npmName)
347 logger.warn('Cannot unregister plugin %s.', npmName, { err })
350 const plugin = await PluginModel.loadByNpmName(npmName)
351 if (!plugin || plugin.uninstalled === true) {
352 logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', npmName)
356 plugin.enabled = false
357 plugin.uninstalled = true
361 await removeNpmPlugin(npmName)
363 logger.info('Plugin %s uninstalled.', npmName)
366 // ###################### Private register ######################
368 private async registerPluginOrTheme (plugin: PluginModel) {
369 const npmName = PluginModel.buildNpmName(plugin.name, plugin.type)
371 logger.info('Registering plugin or theme %s.', npmName)
373 const packageJSON = await this.getPackageJSON(plugin.name, plugin.type)
374 const pluginPath = this.getPluginPath(plugin.name, plugin.type)
376 this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, plugin.type)
378 let library: PluginLibrary
379 let registerHelpers: RegisterHelpers
380 if (plugin.type === PluginType.PLUGIN) {
381 const result = await this.registerPlugin(plugin, pluginPath, packageJSON)
382 library = result.library
383 registerHelpers = result.registerStore
386 const clientScripts: { [id: string]: ClientScript } = {}
387 for (const c of packageJSON.clientScripts) {
388 clientScripts[c.script] = c
391 this.registeredPlugins[npmName] = {
395 version: plugin.version,
396 description: plugin.description,
397 peertubeEngine: plugin.peertubeEngine,
399 staticDirs: packageJSON.staticDirs,
401 css: packageJSON.css,
402 registerHelpers: registerHelpers || undefined,
403 unregister: library ? library.unregister : undefined
406 await this.addTranslations(plugin, npmName, packageJSON.translations)
409 private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) {
410 const npmName = PluginModel.buildNpmName(plugin.name, plugin.type)
412 // Delete cache if needed
413 const modulePath = join(pluginPath, packageJSON.library)
414 delete require.cache[modulePath]
415 const library: PluginLibrary = require(modulePath)
417 if (!isLibraryCodeValid(library)) {
418 throw new Error('Library code is not valid (miss register or unregister function)')
421 const { registerOptions, registerStore } = this.getRegisterHelpers(npmName, plugin)
422 library.register(registerOptions)
423 .catch(err => logger.error('Cannot register plugin %s.', npmName, { err }))
425 logger.info('Add plugin %s CSS to global file.', npmName)
427 await this.addCSSToGlobalFile(pluginPath, packageJSON.css)
429 return { library, registerStore }
432 // ###################### Translations ######################
434 private async addTranslations (plugin: PluginModel, npmName: string, translationPaths: PackagePluginTranslations) {
435 for (const locale of Object.keys(translationPaths)) {
436 const path = translationPaths[locale]
437 const json = await readJSON(join(this.getPluginPath(plugin.name, plugin.type), path))
439 if (!this.translations[locale]) this.translations[locale] = {}
440 this.translations[locale][npmName] = json
442 logger.info('Added locale %s of plugin %s.', locale, npmName)
446 private deleteTranslations (npmName: string) {
447 for (const locale of Object.keys(this.translations)) {
448 delete this.translations[locale][npmName]
450 logger.info('Deleted locale %s of plugin %s.', locale, npmName)
454 // ###################### CSS ######################
456 private resetCSSGlobalFile () {
457 ClientHtml.invalidCache()
459 return outputFile(PLUGIN_GLOBAL_CSS_PATH, '')
462 private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) {
463 for (const cssPath of cssRelativePaths) {
464 await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH)
467 ClientHtml.invalidCache()
470 private concatFiles (input: string, output: string) {
471 return new Promise<void>((res, rej) => {
472 const inputStream = createReadStream(input)
473 const outputStream = createWriteStream(output, { flags: 'a' })
475 inputStream.pipe(outputStream)
477 inputStream.on('end', () => res())
478 inputStream.on('error', err => rej(err))
482 private async regeneratePluginGlobalCSS () {
483 await this.resetCSSGlobalFile()
485 for (const plugin of this.getRegisteredPlugins()) {
486 await this.addCSSToGlobalFile(plugin.path, plugin.css)
490 // ###################### Utils ######################
492 private sortHooksByPriority () {
493 for (const hookName of Object.keys(this.hooks)) {
494 this.hooks[hookName].sort((a, b) => {
495 return b.priority - a.priority
500 private getPackageJSON (pluginName: string, pluginType: PluginType) {
501 const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json')
503 return readJSON(pluginPath) as Promise<PluginPackageJson>
506 private getPluginPath (pluginName: string, pluginType: PluginType) {
507 const npmName = PluginModel.buildNpmName(pluginName, pluginType)
509 return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', npmName)
512 private getAuth (npmName: string, authName: string) {
513 const plugin = this.getRegisteredPluginOrTheme(npmName)
514 if (!plugin || plugin.type !== PluginType.PLUGIN) return null
516 let auths: (RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions)[] = plugin.registerHelpers.getIdAndPassAuths()
517 auths = auths.concat(plugin.registerHelpers.getExternalAuths())
519 return auths.find(a => a.authName === authName)
522 // ###################### Private getters ######################
524 private getRegisteredPluginsOrThemes (type: PluginType) {
525 const plugins: RegisteredPlugin[] = []
527 for (const npmName of Object.keys(this.registeredPlugins)) {
528 const plugin = this.registeredPlugins[npmName]
529 if (plugin.type !== type) continue
537 // ###################### Generate register helpers ######################
539 private getRegisterHelpers (
542 ): { registerStore: RegisterHelpers, registerOptions: RegisterServerOptions } {
543 const onHookAdded = (options: RegisterServerHookOptions) => {
544 if (!this.hooks[options.target]) this.hooks[options.target] = []
546 this.hooks[options.target].push({
548 pluginName: plugin.name,
549 handler: options.handler,
550 priority: options.priority || 0
554 const registerHelpers = new RegisterHelpers(npmName, plugin, onHookAdded.bind(this))
557 registerStore: registerHelpers,
558 registerOptions: registerHelpers.buildRegisterHelpers()
562 private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) {
563 if (!packageJSON.staticDirs) packageJSON.staticDirs = {}
564 if (!packageJSON.css) packageJSON.css = []
565 if (!packageJSON.clientScripts) packageJSON.clientScripts = []
566 if (!packageJSON.translations) packageJSON.translations = {}
568 const { result: packageJSONValid, badFields } = isPackageJSONValid(packageJSON, pluginType)
569 if (!packageJSONValid) {
570 const formattedFields = badFields.map(f => `"${f}"`)
573 throw new Error(`PackageJSON is invalid (invalid fields: ${formattedFields}).`)
577 static get Instance () {
578 return this.instance || (this.instance = new this())