]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/plugin-manager.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-manager.ts
1 import decache from 'decache'
2 import * as express from 'express'
3 import { createReadStream, createWriteStream } from 'fs'
4 import { ensureDir, outputFile, readJSON } from 'fs-extra'
5 import { basename, join } from 'path'
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 } catch (err) {
308 logger.error('Cannot install plugin %s, removing it...', toInstall, { err })
309
310 try {
311 await removeNpmPlugin(npmName)
312 } catch (err) {
313 logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err })
314 }
315
316 throw err
317 }
318
319 logger.info('Successful installation of plugin %s.', toInstall)
320
321 await this.registerPluginOrTheme(plugin)
322
323 return plugin
324 }
325
326 async update (toUpdate: string, fromDisk = false) {
327 const npmName = fromDisk ? basename(toUpdate) : toUpdate
328
329 logger.info('Updating plugin %s.', npmName)
330
331 // Use the latest version from DB, to not upgrade to a version that does not support our PeerTube version
332 let version: string
333 if (!fromDisk) {
334 const plugin = await PluginModel.loadByNpmName(toUpdate)
335 version = plugin.latestVersion
336 }
337
338 // Unregister old hooks
339 await this.unregister(npmName)
340
341 return this.install(toUpdate, version, fromDisk)
342 }
343
344 async uninstall (npmName: string) {
345 logger.info('Uninstalling plugin %s.', npmName)
346
347 try {
348 await this.unregister(npmName)
349 } catch (err) {
350 logger.warn('Cannot unregister plugin %s.', npmName, { err })
351 }
352
353 const plugin = await PluginModel.loadByNpmName(npmName)
354 if (!plugin || plugin.uninstalled === true) {
355 logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', npmName)
356 return
357 }
358
359 plugin.enabled = false
360 plugin.uninstalled = true
361
362 await plugin.save()
363
364 await removeNpmPlugin(npmName)
365
366 logger.info('Plugin %s uninstalled.', npmName)
367 }
368
369 // ###################### Private register ######################
370
371 private async registerPluginOrTheme (plugin: PluginModel) {
372 const npmName = PluginModel.buildNpmName(plugin.name, plugin.type)
373
374 logger.info('Registering plugin or theme %s.', npmName)
375
376 const packageJSON = await this.getPackageJSON(plugin.name, plugin.type)
377 const pluginPath = this.getPluginPath(plugin.name, plugin.type)
378
379 this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, plugin.type)
380
381 let library: PluginLibrary
382 let registerHelpers: RegisterHelpers
383 if (plugin.type === PluginType.PLUGIN) {
384 const result = await this.registerPlugin(plugin, pluginPath, packageJSON)
385 library = result.library
386 registerHelpers = result.registerStore
387 }
388
389 const clientScripts: { [id: string]: ClientScript } = {}
390 for (const c of packageJSON.clientScripts) {
391 clientScripts[c.script] = c
392 }
393
394 this.registeredPlugins[npmName] = {
395 npmName,
396 name: plugin.name,
397 type: plugin.type,
398 version: plugin.version,
399 description: plugin.description,
400 peertubeEngine: plugin.peertubeEngine,
401 path: pluginPath,
402 staticDirs: packageJSON.staticDirs,
403 clientScripts,
404 css: packageJSON.css,
405 registerHelpers: registerHelpers || undefined,
406 unregister: library ? library.unregister : undefined
407 }
408
409 await this.addTranslations(plugin, npmName, packageJSON.translations)
410 }
411
412 private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) {
413 const npmName = PluginModel.buildNpmName(plugin.name, plugin.type)
414
415 // Delete cache if needed
416 const modulePath = join(pluginPath, packageJSON.library)
417 decache(modulePath)
418 const library: PluginLibrary = require(modulePath)
419
420 if (!isLibraryCodeValid(library)) {
421 throw new Error('Library code is not valid (miss register or unregister function)')
422 }
423
424 const { registerOptions, registerStore } = this.getRegisterHelpers(npmName, plugin)
425
426 await ensureDir(registerOptions.peertubeHelpers.plugin.getDataDirectoryPath())
427
428 library.register(registerOptions)
429 .catch(err => logger.error('Cannot register plugin %s.', npmName, { err }))
430
431 logger.info('Add plugin %s CSS to global file.', npmName)
432
433 await this.addCSSToGlobalFile(pluginPath, packageJSON.css)
434
435 return { library, registerStore }
436 }
437
438 // ###################### Translations ######################
439
440 private async addTranslations (plugin: PluginModel, npmName: string, translationPaths: PluginTranslationPaths) {
441 for (const locale of Object.keys(translationPaths)) {
442 const path = translationPaths[locale]
443 const json = await readJSON(join(this.getPluginPath(plugin.name, plugin.type), path))
444
445 const completeLocale = getCompleteLocale(locale)
446
447 if (!this.translations[completeLocale]) this.translations[completeLocale] = {}
448 this.translations[completeLocale][npmName] = json
449
450 logger.info('Added locale %s of plugin %s.', completeLocale, npmName)
451 }
452 }
453
454 private deleteTranslations (npmName: string) {
455 for (const locale of Object.keys(this.translations)) {
456 delete this.translations[locale][npmName]
457
458 logger.info('Deleted locale %s of plugin %s.', locale, npmName)
459 }
460 }
461
462 // ###################### CSS ######################
463
464 private resetCSSGlobalFile () {
465 ClientHtml.invalidCache()
466
467 return outputFile(PLUGIN_GLOBAL_CSS_PATH, '')
468 }
469
470 private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) {
471 for (const cssPath of cssRelativePaths) {
472 await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH)
473 }
474
475 ClientHtml.invalidCache()
476 }
477
478 private concatFiles (input: string, output: string) {
479 return new Promise<void>((res, rej) => {
480 const inputStream = createReadStream(input)
481 const outputStream = createWriteStream(output, { flags: 'a' })
482
483 inputStream.pipe(outputStream)
484
485 inputStream.on('end', () => res())
486 inputStream.on('error', err => rej(err))
487 })
488 }
489
490 private async regeneratePluginGlobalCSS () {
491 await this.resetCSSGlobalFile()
492
493 for (const plugin of this.getRegisteredPlugins()) {
494 await this.addCSSToGlobalFile(plugin.path, plugin.css)
495 }
496 }
497
498 // ###################### Utils ######################
499
500 private sortHooksByPriority () {
501 for (const hookName of Object.keys(this.hooks)) {
502 this.hooks[hookName].sort((a, b) => {
503 return b.priority - a.priority
504 })
505 }
506 }
507
508 private getPackageJSON (pluginName: string, pluginType: PluginType) {
509 const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json')
510
511 return readJSON(pluginPath) as Promise<PluginPackageJson>
512 }
513
514 private getPluginPath (pluginName: string, pluginType: PluginType) {
515 const npmName = PluginModel.buildNpmName(pluginName, pluginType)
516
517 return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', npmName)
518 }
519
520 private getAuth (npmName: string, authName: string) {
521 const plugin = this.getRegisteredPluginOrTheme(npmName)
522 if (!plugin || plugin.type !== PluginType.PLUGIN) return null
523
524 let auths: (RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions)[] = plugin.registerHelpers.getIdAndPassAuths()
525 auths = auths.concat(plugin.registerHelpers.getExternalAuths())
526
527 return auths.find(a => a.authName === authName)
528 }
529
530 // ###################### Private getters ######################
531
532 private getRegisteredPluginsOrThemes (type: PluginType) {
533 const plugins: RegisteredPlugin[] = []
534
535 for (const npmName of Object.keys(this.registeredPlugins)) {
536 const plugin = this.registeredPlugins[npmName]
537 if (plugin.type !== type) continue
538
539 plugins.push(plugin)
540 }
541
542 return plugins
543 }
544
545 // ###################### Generate register helpers ######################
546
547 private getRegisterHelpers (
548 npmName: string,
549 plugin: PluginModel
550 ): { registerStore: RegisterHelpers, registerOptions: RegisterServerOptions } {
551 const onHookAdded = (options: RegisterServerHookOptions) => {
552 if (!this.hooks[options.target]) this.hooks[options.target] = []
553
554 this.hooks[options.target].push({
555 npmName: npmName,
556 pluginName: plugin.name,
557 handler: options.handler,
558 priority: options.priority || 0
559 })
560 }
561
562 const registerHelpers = new RegisterHelpers(npmName, plugin, onHookAdded.bind(this))
563
564 return {
565 registerStore: registerHelpers,
566 registerOptions: registerHelpers.buildRegisterHelpers()
567 }
568 }
569
570 private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) {
571 if (!packageJSON.staticDirs) packageJSON.staticDirs = {}
572 if (!packageJSON.css) packageJSON.css = []
573 if (!packageJSON.clientScripts) packageJSON.clientScripts = []
574 if (!packageJSON.translations) packageJSON.translations = {}
575
576 const { result: packageJSONValid, badFields } = isPackageJSONValid(packageJSON, pluginType)
577 if (!packageJSONValid) {
578 const formattedFields = badFields.map(f => `"${f}"`)
579 .join(', ')
580
581 throw new Error(`PackageJSON is invalid (invalid fields: ${formattedFields}).`)
582 }
583 }
584
585 static get Instance () {
586 return this.instance || (this.instance = new this())
587 }
588 }