]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/plugin-manager.ts
Merge branch 'release/4.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-manager.ts
1 import express from 'express'
2 import { createReadStream, createWriteStream } from 'fs'
3 import { ensureDir, outputFile, readJSON } from 'fs-extra'
4 import { basename, join } from 'path'
5 import { decachePlugin } from '@server/helpers/decache'
6 import { MOAuthTokenUser, MUser } from '@server/types/models'
7 import { getCompleteLocale } from '@shared/core-utils'
8 import { ClientScript, PluginPackageJson, PluginTranslation, PluginTranslationPaths, RegisterServerHookOptions } from '@shared/models'
9 import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks'
10 import { PluginType } from '../../../shared/models/plugins/plugin.type'
11 import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server/server-hook.model'
12 import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins'
13 import { logger } from '../../helpers/logger'
14 import { CONFIG } from '../../initializers/config'
15 import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants'
16 import { PluginModel } from '../../models/server/plugin'
17 import { PluginLibrary, RegisterServerAuthExternalOptions, RegisterServerAuthPassOptions, RegisterServerOptions } from '../../types/plugins'
18 import { ClientHtml } from '../client-html'
19 import { RegisterHelpers } from './register-helpers'
20 import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn'
21
22 export interface RegisteredPlugin {
23 npmName: string
24 name: string
25 version: string
26 description: string
27 peertubeEngine: string
28
29 type: PluginType
30
31 path: string
32
33 staticDirs: { [name: string]: string }
34 clientScripts: { [name: string]: ClientScript }
35
36 css: string[]
37
38 // Only if this is a plugin
39 registerHelpers?: RegisterHelpers
40 unregister?: Function
41 }
42
43 export interface HookInformationValue {
44 npmName: string
45 pluginName: string
46 handler: Function
47 priority: number
48 }
49
50 type PluginLocalesTranslations = {
51 [locale: string]: PluginTranslation
52 }
53
54 export class PluginManager implements ServerHook {
55
56 private static instance: PluginManager
57
58 private registeredPlugins: { [name: string]: RegisteredPlugin } = {}
59
60 private hooks: { [name: string]: HookInformationValue[] } = {}
61 private translations: PluginLocalesTranslations = {}
62
63 private constructor () {
64 }
65
66 // ###################### Getters ######################
67
68 isRegistered (npmName: string) {
69 return !!this.getRegisteredPluginOrTheme(npmName)
70 }
71
72 getRegisteredPluginOrTheme (npmName: string) {
73 return this.registeredPlugins[npmName]
74 }
75
76 getRegisteredPluginByShortName (name: string) {
77 const npmName = PluginModel.buildNpmName(name, PluginType.PLUGIN)
78 const registered = this.getRegisteredPluginOrTheme(npmName)
79
80 if (!registered || registered.type !== PluginType.PLUGIN) return undefined
81
82 return registered
83 }
84
85 getRegisteredThemeByShortName (name: string) {
86 const npmName = PluginModel.buildNpmName(name, PluginType.THEME)
87 const registered = this.getRegisteredPluginOrTheme(npmName)
88
89 if (!registered || registered.type !== PluginType.THEME) return undefined
90
91 return registered
92 }
93
94 getRegisteredPlugins () {
95 return this.getRegisteredPluginsOrThemes(PluginType.PLUGIN)
96 }
97
98 getRegisteredThemes () {
99 return this.getRegisteredPluginsOrThemes(PluginType.THEME)
100 }
101
102 getIdAndPassAuths () {
103 return this.getRegisteredPlugins()
104 .map(p => ({
105 npmName: p.npmName,
106 name: p.name,
107 version: p.version,
108 idAndPassAuths: p.registerHelpers.getIdAndPassAuths()
109 }))
110 .filter(v => v.idAndPassAuths.length !== 0)
111 }
112
113 getExternalAuths () {
114 return this.getRegisteredPlugins()
115 .map(p => ({
116 npmName: p.npmName,
117 name: p.name,
118 version: p.version,
119 externalAuths: p.registerHelpers.getExternalAuths()
120 }))
121 .filter(v => v.externalAuths.length !== 0)
122 }
123
124 getRegisteredSettings (npmName: string) {
125 const result = this.getRegisteredPluginOrTheme(npmName)
126 if (!result || result.type !== PluginType.PLUGIN) return []
127
128 return result.registerHelpers.getSettings()
129 }
130
131 getRouter (npmName: string) {
132 const result = this.getRegisteredPluginOrTheme(npmName)
133 if (!result || result.type !== PluginType.PLUGIN) return null
134
135 return result.registerHelpers.getRouter()
136 }
137
138 getTranslations (locale: string) {
139 return this.translations[locale] || {}
140 }
141
142 async isTokenValid (token: MOAuthTokenUser, type: 'access' | 'refresh') {
143 const auth = this.getAuth(token.User.pluginAuth, token.authName)
144 if (!auth) return true
145
146 if (auth.hookTokenValidity) {
147 try {
148 const { valid } = await auth.hookTokenValidity({ token, type })
149
150 if (valid === false) {
151 logger.info('Rejecting %s token validity from auth %s of plugin %s', type, token.authName, token.User.pluginAuth)
152 }
153
154 return valid
155 } catch (err) {
156 logger.warn('Cannot run check token validity from auth %s of plugin %s.', token.authName, token.User.pluginAuth, { err })
157 return true
158 }
159 }
160
161 return true
162 }
163
164 // ###################### External events ######################
165
166 async onLogout (npmName: string, authName: string, user: MUser, req: express.Request) {
167 const auth = this.getAuth(npmName, authName)
168
169 if (auth?.onLogout) {
170 logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)
171
172 try {
173 // Force await, in case or onLogout returns a promise
174 const result = await auth.onLogout(user, req)
175
176 return typeof result === 'string'
177 ? result
178 : undefined
179 } catch (err) {
180 logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })
181 }
182 }
183
184 return undefined
185 }
186
187 async onSettingsChanged (name: string, settings: any) {
188 const registered = this.getRegisteredPluginByShortName(name)
189 if (!registered) {
190 logger.error('Cannot find plugin %s to call on settings changed.', name)
191 }
192
193 for (const cb of registered.registerHelpers.getOnSettingsChangedCallbacks()) {
194 try {
195 await cb(settings)
196 } catch (err) {
197 logger.error('Cannot run on settings changed callback for %s.', registered.npmName, { err })
198 }
199 }
200 }
201
202 // ###################### Hooks ######################
203
204 async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> {
205 if (!this.hooks[hookName]) return Promise.resolve(result)
206
207 const hookType = getHookType(hookName)
208
209 for (const hook of this.hooks[hookName]) {
210 logger.debug('Running hook %s of plugin %s.', hookName, hook.npmName)
211
212 result = await internalRunHook(hook.handler, hookType, result, params, err => {
213 logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err })
214 })
215 }
216
217 return result
218 }
219
220 // ###################### Registration ######################
221
222 async registerPluginsAndThemes () {
223 await this.resetCSSGlobalFile()
224
225 const plugins = await PluginModel.listEnabledPluginsAndThemes()
226
227 for (const plugin of plugins) {
228 try {
229 await this.registerPluginOrTheme(plugin)
230 } catch (err) {
231 // Try to unregister the plugin
232 try {
233 await this.unregister(PluginModel.buildNpmName(plugin.name, plugin.type))
234 } catch {
235 // we don't care if we cannot unregister it
236 }
237
238 logger.error('Cannot register plugin %s, skipping.', plugin.name, { err })
239 }
240 }
241
242 this.sortHooksByPriority()
243 }
244
245 // Don't need the plugin type since themes cannot register server code
246 async unregister (npmName: string) {
247 logger.info('Unregister plugin %s.', npmName)
248
249 const plugin = this.getRegisteredPluginOrTheme(npmName)
250
251 if (!plugin) {
252 throw new Error(`Unknown plugin ${npmName} to unregister`)
253 }
254
255 delete this.registeredPlugins[plugin.npmName]
256
257 this.deleteTranslations(plugin.npmName)
258
259 if (plugin.type === PluginType.PLUGIN) {
260 await plugin.unregister()
261
262 // Remove hooks of this plugin
263 for (const key of Object.keys(this.hooks)) {
264 this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName)
265 }
266
267 const store = plugin.registerHelpers
268 store.reinitVideoConstants(plugin.npmName)
269 store.reinitTranscodingProfilesAndEncoders(plugin.npmName)
270
271 logger.info('Regenerating registered plugin CSS to global file.')
272 await this.regeneratePluginGlobalCSS()
273 }
274 }
275
276 // ###################### Installation ######################
277
278 async install (toInstall: string, version?: string, fromDisk = false) {
279 let plugin: PluginModel
280 let npmName: string
281
282 logger.info('Installing plugin %s.', toInstall)
283
284 try {
285 fromDisk
286 ? await installNpmPluginFromDisk(toInstall)
287 : await installNpmPlugin(toInstall, version)
288
289 npmName = fromDisk ? basename(toInstall) : toInstall
290 const pluginType = PluginModel.getTypeFromNpmName(npmName)
291 const pluginName = PluginModel.normalizePluginName(npmName)
292
293 const packageJSON = await this.getPackageJSON(pluginName, pluginType)
294
295 this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, pluginType);
296
297 [ plugin ] = await PluginModel.upsert({
298 name: pluginName,
299 description: packageJSON.description,
300 homepage: packageJSON.homepage,
301 type: pluginType,
302 version: packageJSON.version,
303 enabled: true,
304 uninstalled: false,
305 peertubeEngine: packageJSON.engine.peertube
306 }, { returning: true })
307
308 logger.info('Successful installation of plugin %s.', toInstall)
309
310 await this.registerPluginOrTheme(plugin)
311 } catch (rootErr) {
312 logger.error('Cannot install plugin %s, removing it...', toInstall, { err: rootErr })
313
314 try {
315 await this.uninstall(npmName)
316 } catch (err) {
317 logger.error('Cannot uninstall plugin %s after failed installation.', toInstall, { err })
318
319 try {
320 await removeNpmPlugin(npmName)
321 } catch (err) {
322 logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err })
323 }
324 }
325
326 throw rootErr
327 }
328
329 return plugin
330 }
331
332 async update (toUpdate: string, fromDisk = false) {
333 const npmName = fromDisk ? basename(toUpdate) : toUpdate
334
335 logger.info('Updating plugin %s.', npmName)
336
337 // Use the latest version from DB, to not upgrade to a version that does not support our PeerTube version
338 let version: string
339 if (!fromDisk) {
340 const plugin = await PluginModel.loadByNpmName(toUpdate)
341 version = plugin.latestVersion
342 }
343
344 // Unregister old hooks
345 await this.unregister(npmName)
346
347 return this.install(toUpdate, version, fromDisk)
348 }
349
350 async uninstall (npmName: string) {
351 logger.info('Uninstalling plugin %s.', npmName)
352
353 try {
354 await this.unregister(npmName)
355 } catch (err) {
356 logger.warn('Cannot unregister plugin %s.', npmName, { err })
357 }
358
359 const plugin = await PluginModel.loadByNpmName(npmName)
360 if (!plugin || plugin.uninstalled === true) {
361 logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', npmName)
362 return
363 }
364
365 plugin.enabled = false
366 plugin.uninstalled = true
367
368 await plugin.save()
369
370 await removeNpmPlugin(npmName)
371
372 logger.info('Plugin %s uninstalled.', npmName)
373 }
374
375 // ###################### Private register ######################
376
377 private async registerPluginOrTheme (plugin: PluginModel) {
378 const npmName = PluginModel.buildNpmName(plugin.name, plugin.type)
379
380 logger.info('Registering plugin or theme %s.', npmName)
381
382 const packageJSON = await this.getPackageJSON(plugin.name, plugin.type)
383 const pluginPath = this.getPluginPath(plugin.name, plugin.type)
384
385 this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, plugin.type)
386
387 let library: PluginLibrary
388 let registerHelpers: RegisterHelpers
389 if (plugin.type === PluginType.PLUGIN) {
390 const result = await this.registerPlugin(plugin, pluginPath, packageJSON)
391 library = result.library
392 registerHelpers = result.registerStore
393 }
394
395 const clientScripts: { [id: string]: ClientScript } = {}
396 for (const c of packageJSON.clientScripts) {
397 clientScripts[c.script] = c
398 }
399
400 this.registeredPlugins[npmName] = {
401 npmName,
402 name: plugin.name,
403 type: plugin.type,
404 version: plugin.version,
405 description: plugin.description,
406 peertubeEngine: plugin.peertubeEngine,
407 path: pluginPath,
408 staticDirs: packageJSON.staticDirs,
409 clientScripts,
410 css: packageJSON.css,
411 registerHelpers: registerHelpers || undefined,
412 unregister: library ? library.unregister : undefined
413 }
414
415 await this.addTranslations(plugin, npmName, packageJSON.translations)
416 }
417
418 private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) {
419 const npmName = PluginModel.buildNpmName(plugin.name, plugin.type)
420
421 // Delete cache if needed
422 const modulePath = join(pluginPath, packageJSON.library)
423 decachePlugin(pluginPath, modulePath)
424 const library: PluginLibrary = require(modulePath)
425
426 if (!isLibraryCodeValid(library)) {
427 throw new Error('Library code is not valid (miss register or unregister function)')
428 }
429
430 const { registerOptions, registerStore } = this.getRegisterHelpers(npmName, plugin)
431
432 await ensureDir(registerOptions.peertubeHelpers.plugin.getDataDirectoryPath())
433
434 await library.register(registerOptions)
435
436 logger.info('Add plugin %s CSS to global file.', npmName)
437
438 await this.addCSSToGlobalFile(pluginPath, packageJSON.css)
439
440 return { library, registerStore }
441 }
442
443 // ###################### Translations ######################
444
445 private async addTranslations (plugin: PluginModel, npmName: string, translationPaths: PluginTranslationPaths) {
446 for (const locale of Object.keys(translationPaths)) {
447 const path = translationPaths[locale]
448 const json = await readJSON(join(this.getPluginPath(plugin.name, plugin.type), path))
449
450 const completeLocale = getCompleteLocale(locale)
451
452 if (!this.translations[completeLocale]) this.translations[completeLocale] = {}
453 this.translations[completeLocale][npmName] = json
454
455 logger.info('Added locale %s of plugin %s.', completeLocale, npmName)
456 }
457 }
458
459 private deleteTranslations (npmName: string) {
460 for (const locale of Object.keys(this.translations)) {
461 delete this.translations[locale][npmName]
462
463 logger.info('Deleted locale %s of plugin %s.', locale, npmName)
464 }
465 }
466
467 // ###################### CSS ######################
468
469 private resetCSSGlobalFile () {
470 ClientHtml.invalidCache()
471
472 return outputFile(PLUGIN_GLOBAL_CSS_PATH, '')
473 }
474
475 private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) {
476 for (const cssPath of cssRelativePaths) {
477 await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH)
478 }
479
480 ClientHtml.invalidCache()
481 }
482
483 private concatFiles (input: string, output: string) {
484 return new Promise<void>((res, rej) => {
485 const inputStream = createReadStream(input)
486 const outputStream = createWriteStream(output, { flags: 'a' })
487
488 inputStream.pipe(outputStream)
489
490 inputStream.on('end', () => res())
491 inputStream.on('error', err => rej(err))
492 })
493 }
494
495 private async regeneratePluginGlobalCSS () {
496 await this.resetCSSGlobalFile()
497
498 for (const plugin of this.getRegisteredPlugins()) {
499 await this.addCSSToGlobalFile(plugin.path, plugin.css)
500 }
501 }
502
503 // ###################### Utils ######################
504
505 private sortHooksByPriority () {
506 for (const hookName of Object.keys(this.hooks)) {
507 this.hooks[hookName].sort((a, b) => {
508 return b.priority - a.priority
509 })
510 }
511 }
512
513 private getPackageJSON (pluginName: string, pluginType: PluginType) {
514 const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json')
515
516 return readJSON(pluginPath) as Promise<PluginPackageJson>
517 }
518
519 private getPluginPath (pluginName: string, pluginType: PluginType) {
520 const npmName = PluginModel.buildNpmName(pluginName, pluginType)
521
522 return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', npmName)
523 }
524
525 private getAuth (npmName: string, authName: string) {
526 const plugin = this.getRegisteredPluginOrTheme(npmName)
527 if (!plugin || plugin.type !== PluginType.PLUGIN) return null
528
529 let auths: (RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions)[] = plugin.registerHelpers.getIdAndPassAuths()
530 auths = auths.concat(plugin.registerHelpers.getExternalAuths())
531
532 return auths.find(a => a.authName === authName)
533 }
534
535 // ###################### Private getters ######################
536
537 private getRegisteredPluginsOrThemes (type: PluginType) {
538 const plugins: RegisteredPlugin[] = []
539
540 for (const npmName of Object.keys(this.registeredPlugins)) {
541 const plugin = this.registeredPlugins[npmName]
542 if (plugin.type !== type) continue
543
544 plugins.push(plugin)
545 }
546
547 return plugins
548 }
549
550 // ###################### Generate register helpers ######################
551
552 private getRegisterHelpers (
553 npmName: string,
554 plugin: PluginModel
555 ): { registerStore: RegisterHelpers, registerOptions: RegisterServerOptions } {
556 const onHookAdded = (options: RegisterServerHookOptions) => {
557 if (!this.hooks[options.target]) this.hooks[options.target] = []
558
559 this.hooks[options.target].push({
560 npmName: npmName,
561 pluginName: plugin.name,
562 handler: options.handler,
563 priority: options.priority || 0
564 })
565 }
566
567 const registerHelpers = new RegisterHelpers(npmName, plugin, onHookAdded.bind(this))
568
569 return {
570 registerStore: registerHelpers,
571 registerOptions: registerHelpers.buildRegisterHelpers()
572 }
573 }
574
575 private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) {
576 if (!packageJSON.staticDirs) packageJSON.staticDirs = {}
577 if (!packageJSON.css) packageJSON.css = []
578 if (!packageJSON.clientScripts) packageJSON.clientScripts = []
579 if (!packageJSON.translations) packageJSON.translations = {}
580
581 const { result: packageJSONValid, badFields } = isPackageJSONValid(packageJSON, pluginType)
582 if (!packageJSONValid) {
583 const formattedFields = badFields.map(f => `"${f}"`)
584 .join(', ')
585
586 throw new Error(`PackageJSON is invalid (invalid fields: ${formattedFields}).`)
587 }
588 }
589
590 static get Instance () {
591 return this.instance || (this.instance = new this())
592 }
593 }