]>
Commit | Line | Data |
---|---|---|
345da516 C |
1 | import { PluginModel } from '../../models/server/plugin' |
2 | import { logger } from '../../helpers/logger' | |
f023a19c | 3 | import { basename, join } from 'path' |
345da516 C |
4 | import { CONFIG } from '../../initializers/config' |
5 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' | |
2c053942 | 6 | import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model' |
345da516 C |
7 | import { createReadStream, createWriteStream } from 'fs' |
8 | import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' | |
9 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | |
f023a19c | 10 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' |
09071200 | 11 | import { outputFile, readJSON } from 'fs-extra' |
ad91e700 | 12 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' |
b2195faf | 13 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' |
7663e55a | 14 | import { ServerHook, ServerHookName, serverHookObject } from '../../../shared/models/plugins/server-hook.model' |
b4055e1c | 15 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' |
9ae88819 | 16 | import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model' |
32fe0013 | 17 | import { PluginLibrary } from '../../typings/plugins' |
a8b666e9 | 18 | import { ClientHtml } from '../client-html' |
9ae88819 C |
19 | import { RegisterServerHookOptions } from '../../../shared/models/plugins/register-server-hook.model' |
20 | import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model' | |
345da516 C |
21 | |
22 | export interface RegisteredPlugin { | |
b5f919ac | 23 | npmName: string |
345da516 C |
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 } | |
2c053942 | 34 | clientScripts: { [name: string]: ClientScript } |
345da516 C |
35 | |
36 | css: string[] | |
37 | ||
38 | // Only if this is a plugin | |
39 | unregister?: Function | |
40 | } | |
41 | ||
42 | export interface HookInformationValue { | |
b5f919ac | 43 | npmName: string |
345da516 C |
44 | pluginName: string |
45 | handler: Function | |
46 | priority: number | |
47 | } | |
48 | ||
b4055e1c | 49 | export class PluginManager implements ServerHook { |
345da516 C |
50 | |
51 | private static instance: PluginManager | |
52 | ||
53 | private registeredPlugins: { [ name: string ]: RegisteredPlugin } = {} | |
9ae88819 | 54 | private settings: { [ name: string ]: RegisterServerSettingOptions[] } = {} |
345da516 C |
55 | private hooks: { [ name: string ]: HookInformationValue[] } = {} |
56 | ||
57 | private constructor () { | |
58 | } | |
59 | ||
ad91e700 | 60 | // ###################### Getters ###################### |
345da516 | 61 | |
6702a1b2 C |
62 | isRegistered (npmName: string) { |
63 | return !!this.getRegisteredPluginOrTheme(npmName) | |
64 | } | |
65 | ||
b5f919ac C |
66 | getRegisteredPluginOrTheme (npmName: string) { |
67 | return this.registeredPlugins[npmName] | |
7cd4d2ba C |
68 | } |
69 | ||
345da516 | 70 | getRegisteredPlugin (name: string) { |
b5f919ac C |
71 | const npmName = PluginModel.buildNpmName(name, PluginType.PLUGIN) |
72 | const registered = this.getRegisteredPluginOrTheme(npmName) | |
7cd4d2ba C |
73 | |
74 | if (!registered || registered.type !== PluginType.PLUGIN) return undefined | |
75 | ||
76 | return registered | |
345da516 C |
77 | } |
78 | ||
79 | getRegisteredTheme (name: string) { | |
b5f919ac C |
80 | const npmName = PluginModel.buildNpmName(name, PluginType.THEME) |
81 | const registered = this.getRegisteredPluginOrTheme(npmName) | |
345da516 C |
82 | |
83 | if (!registered || registered.type !== PluginType.THEME) return undefined | |
84 | ||
85 | return registered | |
86 | } | |
87 | ||
18a6f04c | 88 | getRegisteredPlugins () { |
7cd4d2ba C |
89 | return this.getRegisteredPluginsOrThemes(PluginType.PLUGIN) |
90 | } | |
91 | ||
92 | getRegisteredThemes () { | |
93 | return this.getRegisteredPluginsOrThemes(PluginType.THEME) | |
18a6f04c C |
94 | } |
95 | ||
b5f919ac C |
96 | getRegisteredSettings (npmName: string) { |
97 | return this.settings[npmName] || [] | |
ad91e700 C |
98 | } |
99 | ||
100 | // ###################### Hooks ###################### | |
101 | ||
89cd1275 C |
102 | async runHook <T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> { |
103 | if (!this.hooks[hookName]) return Promise.resolve(result) | |
dba85a1e | 104 | |
b4055e1c | 105 | const hookType = getHookType(hookName) |
18a6f04c C |
106 | |
107 | for (const hook of this.hooks[hookName]) { | |
89cd1275 C |
108 | logger.debug('Running hook %s of plugin %s.', hookName, hook.npmName) |
109 | ||
110 | result = await internalRunHook(hook.handler, hookType, result, params, err => { | |
18a6f04c | 111 | logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) |
b4055e1c | 112 | }) |
18a6f04c C |
113 | } |
114 | ||
115 | return result | |
116 | } | |
117 | ||
ad91e700 C |
118 | // ###################### Registration ###################### |
119 | ||
120 | async registerPluginsAndThemes () { | |
121 | await this.resetCSSGlobalFile() | |
122 | ||
123 | const plugins = await PluginModel.listEnabledPluginsAndThemes() | |
124 | ||
125 | for (const plugin of plugins) { | |
126 | try { | |
127 | await this.registerPluginOrTheme(plugin) | |
128 | } catch (err) { | |
587568e1 C |
129 | // Try to unregister the plugin |
130 | try { | |
131 | await this.unregister(PluginModel.buildNpmName(plugin.name, plugin.type)) | |
132 | } catch { | |
133 | // we don't care if we cannot unregister it | |
134 | } | |
135 | ||
ad91e700 C |
136 | logger.error('Cannot register plugin %s, skipping.', plugin.name, { err }) |
137 | } | |
138 | } | |
139 | ||
140 | this.sortHooksByPriority() | |
141 | } | |
142 | ||
b5f919ac C |
143 | // Don't need the plugin type since themes cannot register server code |
144 | async unregister (npmName: string) { | |
145 | logger.info('Unregister plugin %s.', npmName) | |
146 | ||
147 | const plugin = this.getRegisteredPluginOrTheme(npmName) | |
345da516 C |
148 | |
149 | if (!plugin) { | |
b5f919ac | 150 | throw new Error(`Unknown plugin ${npmName} to unregister`) |
345da516 C |
151 | } |
152 | ||
60cfd4cb C |
153 | delete this.registeredPlugins[plugin.npmName] |
154 | ||
b5f919ac C |
155 | if (plugin.type === PluginType.PLUGIN) { |
156 | await plugin.unregister() | |
345da516 | 157 | |
b5f919ac C |
158 | // Remove hooks of this plugin |
159 | for (const key of Object.keys(this.hooks)) { | |
160 | this.hooks[key] = this.hooks[key].filter(h => h.pluginName !== npmName) | |
161 | } | |
2c053942 | 162 | |
b5f919ac C |
163 | logger.info('Regenerating registered plugin CSS to global file.') |
164 | await this.regeneratePluginGlobalCSS() | |
2c053942 | 165 | } |
345da516 C |
166 | } |
167 | ||
ad91e700 C |
168 | // ###################### Installation ###################### |
169 | ||
170 | async install (toInstall: string, version?: string, fromDisk = false) { | |
f023a19c | 171 | let plugin: PluginModel |
b5f919ac | 172 | let npmName: string |
f023a19c C |
173 | |
174 | logger.info('Installing plugin %s.', toInstall) | |
175 | ||
176 | try { | |
177 | fromDisk | |
178 | ? await installNpmPluginFromDisk(toInstall) | |
179 | : await installNpmPlugin(toInstall, version) | |
180 | ||
b5f919ac C |
181 | npmName = fromDisk ? basename(toInstall) : toInstall |
182 | const pluginType = PluginModel.getTypeFromNpmName(npmName) | |
183 | const pluginName = PluginModel.normalizePluginName(npmName) | |
f023a19c | 184 | |
09071200 | 185 | const packageJSON = await this.getPackageJSON(pluginName, pluginType) |
f023a19c C |
186 | if (!isPackageJSONValid(packageJSON, pluginType)) { |
187 | throw new Error('PackageJSON is invalid.') | |
188 | } | |
189 | ||
190 | [ plugin ] = await PluginModel.upsert({ | |
191 | name: pluginName, | |
192 | description: packageJSON.description, | |
dba85a1e | 193 | homepage: packageJSON.homepage, |
f023a19c C |
194 | type: pluginType, |
195 | version: packageJSON.version, | |
196 | enabled: true, | |
197 | uninstalled: false, | |
198 | peertubeEngine: packageJSON.engine.peertube | |
199 | }, { returning: true }) | |
200 | } catch (err) { | |
201 | logger.error('Cannot install plugin %s, removing it...', toInstall, { err }) | |
202 | ||
203 | try { | |
b5f919ac | 204 | await removeNpmPlugin(npmName) |
f023a19c C |
205 | } catch (err) { |
206 | logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err }) | |
207 | } | |
208 | ||
209 | throw err | |
210 | } | |
211 | ||
212 | logger.info('Successful installation of plugin %s.', toInstall) | |
213 | ||
214 | await this.registerPluginOrTheme(plugin) | |
b5f919ac C |
215 | |
216 | return plugin | |
217 | } | |
218 | ||
219 | async update (toUpdate: string, version?: string, fromDisk = false) { | |
220 | const npmName = fromDisk ? basename(toUpdate) : toUpdate | |
221 | ||
222 | logger.info('Updating plugin %s.', npmName) | |
223 | ||
224 | // Unregister old hooks | |
225 | await this.unregister(npmName) | |
226 | ||
227 | return this.install(toUpdate, version, fromDisk) | |
f023a19c C |
228 | } |
229 | ||
dba85a1e C |
230 | async uninstall (npmName: string) { |
231 | logger.info('Uninstalling plugin %s.', npmName) | |
2c053942 | 232 | |
2c053942 | 233 | try { |
b5f919ac | 234 | await this.unregister(npmName) |
2c053942 | 235 | } catch (err) { |
b5f919ac | 236 | logger.warn('Cannot unregister plugin %s.', npmName, { err }) |
2c053942 C |
237 | } |
238 | ||
dba85a1e | 239 | const plugin = await PluginModel.loadByNpmName(npmName) |
2c053942 | 240 | if (!plugin || plugin.uninstalled === true) { |
dba85a1e | 241 | logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', npmName) |
2c053942 C |
242 | return |
243 | } | |
244 | ||
245 | plugin.enabled = false | |
246 | plugin.uninstalled = true | |
247 | ||
248 | await plugin.save() | |
f023a19c | 249 | |
dba85a1e | 250 | await removeNpmPlugin(npmName) |
2c053942 | 251 | |
dba85a1e | 252 | logger.info('Plugin %s uninstalled.', npmName) |
f023a19c C |
253 | } |
254 | ||
ad91e700 C |
255 | // ###################### Private register ###################### |
256 | ||
345da516 | 257 | private async registerPluginOrTheme (plugin: PluginModel) { |
b5f919ac C |
258 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
259 | ||
260 | logger.info('Registering plugin or theme %s.', npmName) | |
345da516 | 261 | |
09071200 | 262 | const packageJSON = await this.getPackageJSON(plugin.name, plugin.type) |
f023a19c | 263 | const pluginPath = this.getPluginPath(plugin.name, plugin.type) |
345da516 C |
264 | |
265 | if (!isPackageJSONValid(packageJSON, plugin.type)) { | |
266 | throw new Error('Package.JSON is invalid.') | |
267 | } | |
268 | ||
269 | let library: PluginLibrary | |
270 | if (plugin.type === PluginType.PLUGIN) { | |
271 | library = await this.registerPlugin(plugin, pluginPath, packageJSON) | |
272 | } | |
273 | ||
2c053942 C |
274 | const clientScripts: { [id: string]: ClientScript } = {} |
275 | for (const c of packageJSON.clientScripts) { | |
276 | clientScripts[c.script] = c | |
277 | } | |
278 | ||
b5f919ac C |
279 | this.registeredPlugins[ npmName ] = { |
280 | npmName, | |
345da516 C |
281 | name: plugin.name, |
282 | type: plugin.type, | |
283 | version: plugin.version, | |
284 | description: plugin.description, | |
285 | peertubeEngine: plugin.peertubeEngine, | |
286 | path: pluginPath, | |
287 | staticDirs: packageJSON.staticDirs, | |
2c053942 | 288 | clientScripts, |
345da516 C |
289 | css: packageJSON.css, |
290 | unregister: library ? library.unregister : undefined | |
291 | } | |
292 | } | |
293 | ||
294 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { | |
b5f919ac C |
295 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
296 | ||
09071200 C |
297 | // Delete cache if needed |
298 | const modulePath = join(pluginPath, packageJSON.library) | |
299 | delete require.cache[modulePath] | |
300 | const library: PluginLibrary = require(modulePath) | |
f023a19c | 301 | |
345da516 C |
302 | if (!isLibraryCodeValid(library)) { |
303 | throw new Error('Library code is not valid (miss register or unregister function)') | |
304 | } | |
305 | ||
32fe0013 C |
306 | const registerHelpers = this.getRegisterHelpers(npmName, plugin) |
307 | library.register(registerHelpers) | |
308 | .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) | |
345da516 | 309 | |
b5f919ac | 310 | logger.info('Add plugin %s CSS to global file.', npmName) |
345da516 C |
311 | |
312 | await this.addCSSToGlobalFile(pluginPath, packageJSON.css) | |
313 | ||
314 | return library | |
315 | } | |
316 | ||
ad91e700 | 317 | // ###################### CSS ###################### |
345da516 | 318 | |
2c053942 | 319 | private resetCSSGlobalFile () { |
3e753302 C |
320 | ClientHtml.invalidCache() |
321 | ||
2c053942 C |
322 | return outputFile(PLUGIN_GLOBAL_CSS_PATH, '') |
323 | } | |
324 | ||
345da516 C |
325 | private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) { |
326 | for (const cssPath of cssRelativePaths) { | |
327 | await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH) | |
328 | } | |
a8b666e9 C |
329 | |
330 | ClientHtml.invalidCache() | |
345da516 C |
331 | } |
332 | ||
333 | private concatFiles (input: string, output: string) { | |
334 | return new Promise<void>((res, rej) => { | |
2c053942 C |
335 | const inputStream = createReadStream(input) |
336 | const outputStream = createWriteStream(output, { flags: 'a' }) | |
345da516 C |
337 | |
338 | inputStream.pipe(outputStream) | |
339 | ||
340 | inputStream.on('end', () => res()) | |
341 | inputStream.on('error', err => rej(err)) | |
342 | }) | |
343 | } | |
344 | ||
ad91e700 C |
345 | private async regeneratePluginGlobalCSS () { |
346 | await this.resetCSSGlobalFile() | |
347 | ||
9fa6ca16 | 348 | for (const key of Object.keys(this.getRegisteredPlugins())) { |
ad91e700 C |
349 | const plugin = this.registeredPlugins[key] |
350 | ||
351 | await this.addCSSToGlobalFile(plugin.path, plugin.css) | |
352 | } | |
353 | } | |
354 | ||
355 | // ###################### Utils ###################### | |
356 | ||
357 | private sortHooksByPriority () { | |
358 | for (const hookName of Object.keys(this.hooks)) { | |
359 | this.hooks[hookName].sort((a, b) => { | |
360 | return b.priority - a.priority | |
361 | }) | |
362 | } | |
363 | } | |
364 | ||
f023a19c C |
365 | private getPackageJSON (pluginName: string, pluginType: PluginType) { |
366 | const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json') | |
367 | ||
09071200 | 368 | return readJSON(pluginPath) as Promise<PluginPackageJson> |
f023a19c C |
369 | } |
370 | ||
371 | private getPluginPath (pluginName: string, pluginType: PluginType) { | |
b5f919ac | 372 | const npmName = PluginModel.buildNpmName(pluginName, pluginType) |
f023a19c | 373 | |
b5f919ac | 374 | return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', npmName) |
f023a19c C |
375 | } |
376 | ||
ad91e700 | 377 | // ###################### Private getters ###################### |
2c053942 | 378 | |
7cd4d2ba C |
379 | private getRegisteredPluginsOrThemes (type: PluginType) { |
380 | const plugins: RegisteredPlugin[] = [] | |
381 | ||
b5f919ac C |
382 | for (const npmName of Object.keys(this.registeredPlugins)) { |
383 | const plugin = this.registeredPlugins[ npmName ] | |
7cd4d2ba C |
384 | if (plugin.type !== type) continue |
385 | ||
386 | plugins.push(plugin) | |
387 | } | |
388 | ||
389 | return plugins | |
390 | } | |
391 | ||
32fe0013 C |
392 | // ###################### Generate register helpers ###################### |
393 | ||
9ae88819 C |
394 | private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterServerOptions { |
395 | const registerHook = (options: RegisterServerHookOptions) => { | |
7663e55a C |
396 | if (serverHookObject[options.target] !== true) { |
397 | logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, npmName) | |
398 | return | |
399 | } | |
400 | ||
32fe0013 C |
401 | if (!this.hooks[options.target]) this.hooks[options.target] = [] |
402 | ||
403 | this.hooks[options.target].push({ | |
404 | npmName, | |
405 | pluginName: plugin.name, | |
406 | handler: options.handler, | |
407 | priority: options.priority || 0 | |
408 | }) | |
409 | } | |
410 | ||
9ae88819 | 411 | const registerSetting = (options: RegisterServerSettingOptions) => { |
32fe0013 C |
412 | if (!this.settings[npmName]) this.settings[npmName] = [] |
413 | ||
414 | this.settings[npmName].push(options) | |
415 | } | |
416 | ||
417 | const settingsManager: PluginSettingsManager = { | |
418 | getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name), | |
419 | ||
420 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) | |
421 | } | |
422 | ||
423 | const storageManager: PluginStorageManager = { | |
424 | getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key), | |
425 | ||
426 | storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data) | |
427 | } | |
428 | ||
429 | const peertubeHelpers = { | |
430 | logger | |
431 | } | |
432 | ||
433 | return { | |
434 | registerHook, | |
435 | registerSetting, | |
436 | settingsManager, | |
437 | storageManager, | |
438 | peertubeHelpers | |
439 | } | |
440 | } | |
441 | ||
345da516 C |
442 | static get Instance () { |
443 | return this.instance || (this.instance = new this()) | |
444 | } | |
445 | } |