]>
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 { PluginLibrary } from '../../../shared/models/plugins/plugin-library.model' |
8 | import { createReadStream, createWriteStream } from 'fs' | |
9 | import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' | |
10 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | |
f023a19c | 11 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' |
2c053942 | 12 | import { outputFile } from 'fs-extra' |
ad91e700 C |
13 | import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model' |
14 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' | |
15 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' | |
345da516 C |
16 | |
17 | export interface RegisteredPlugin { | |
18 | name: string | |
19 | version: string | |
20 | description: string | |
21 | peertubeEngine: string | |
22 | ||
23 | type: PluginType | |
24 | ||
25 | path: string | |
26 | ||
27 | staticDirs: { [name: string]: string } | |
2c053942 | 28 | clientScripts: { [name: string]: ClientScript } |
345da516 C |
29 | |
30 | css: string[] | |
31 | ||
32 | // Only if this is a plugin | |
33 | unregister?: Function | |
34 | } | |
35 | ||
36 | export interface HookInformationValue { | |
37 | pluginName: string | |
38 | handler: Function | |
39 | priority: number | |
40 | } | |
41 | ||
42 | export class PluginManager { | |
43 | ||
44 | private static instance: PluginManager | |
45 | ||
46 | private registeredPlugins: { [ name: string ]: RegisteredPlugin } = {} | |
ad91e700 | 47 | private settings: { [ name: string ]: RegisterSettingOptions[] } = {} |
345da516 C |
48 | private hooks: { [ name: string ]: HookInformationValue[] } = {} |
49 | ||
50 | private constructor () { | |
51 | } | |
52 | ||
ad91e700 | 53 | // ###################### Getters ###################### |
345da516 | 54 | |
7cd4d2ba C |
55 | getRegisteredPluginOrTheme (name: string) { |
56 | return this.registeredPlugins[name] | |
57 | } | |
58 | ||
345da516 | 59 | getRegisteredPlugin (name: string) { |
7cd4d2ba C |
60 | const registered = this.getRegisteredPluginOrTheme(name) |
61 | ||
62 | if (!registered || registered.type !== PluginType.PLUGIN) return undefined | |
63 | ||
64 | return registered | |
345da516 C |
65 | } |
66 | ||
67 | getRegisteredTheme (name: string) { | |
7cd4d2ba | 68 | const registered = this.getRegisteredPluginOrTheme(name) |
345da516 C |
69 | |
70 | if (!registered || registered.type !== PluginType.THEME) return undefined | |
71 | ||
72 | return registered | |
73 | } | |
74 | ||
18a6f04c | 75 | getRegisteredPlugins () { |
7cd4d2ba C |
76 | return this.getRegisteredPluginsOrThemes(PluginType.PLUGIN) |
77 | } | |
78 | ||
79 | getRegisteredThemes () { | |
80 | return this.getRegisteredPluginsOrThemes(PluginType.THEME) | |
18a6f04c C |
81 | } |
82 | ||
ad91e700 C |
83 | getSettings (name: string) { |
84 | return this.settings[name] || [] | |
85 | } | |
86 | ||
87 | // ###################### Hooks ###################### | |
88 | ||
18a6f04c C |
89 | async runHook (hookName: string, param?: any) { |
90 | let result = param | |
91 | ||
92 | const wait = hookName.startsWith('static:') | |
93 | ||
94 | for (const hook of this.hooks[hookName]) { | |
95 | try { | |
ad91e700 C |
96 | if (wait) { |
97 | result = await hook.handler(param) | |
98 | } else { | |
99 | result = hook.handler() | |
100 | } | |
18a6f04c C |
101 | } catch (err) { |
102 | logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) | |
103 | } | |
104 | } | |
105 | ||
106 | return result | |
107 | } | |
108 | ||
ad91e700 C |
109 | // ###################### Registration ###################### |
110 | ||
111 | async registerPluginsAndThemes () { | |
112 | await this.resetCSSGlobalFile() | |
113 | ||
114 | const plugins = await PluginModel.listEnabledPluginsAndThemes() | |
115 | ||
116 | for (const plugin of plugins) { | |
117 | try { | |
118 | await this.registerPluginOrTheme(plugin) | |
119 | } catch (err) { | |
120 | logger.error('Cannot register plugin %s, skipping.', plugin.name, { err }) | |
121 | } | |
122 | } | |
123 | ||
124 | this.sortHooksByPriority() | |
125 | } | |
126 | ||
345da516 C |
127 | async unregister (name: string) { |
128 | const plugin = this.getRegisteredPlugin(name) | |
129 | ||
130 | if (!plugin) { | |
131 | throw new Error(`Unknown plugin ${name} to unregister`) | |
132 | } | |
133 | ||
134 | if (plugin.type === PluginType.THEME) { | |
135 | throw new Error(`Cannot unregister ${name}: this is a theme`) | |
136 | } | |
137 | ||
138 | await plugin.unregister() | |
2c053942 C |
139 | |
140 | // Remove hooks of this plugin | |
141 | for (const key of Object.keys(this.hooks)) { | |
142 | this.hooks[key] = this.hooks[key].filter(h => h.pluginName !== name) | |
143 | } | |
144 | ||
145 | delete this.registeredPlugins[plugin.name] | |
146 | ||
147 | logger.info('Regenerating registered plugin CSS to global file.') | |
148 | await this.regeneratePluginGlobalCSS() | |
345da516 C |
149 | } |
150 | ||
ad91e700 C |
151 | // ###################### Installation ###################### |
152 | ||
153 | async install (toInstall: string, version?: string, fromDisk = false) { | |
f023a19c C |
154 | let plugin: PluginModel |
155 | let name: string | |
156 | ||
157 | logger.info('Installing plugin %s.', toInstall) | |
158 | ||
159 | try { | |
160 | fromDisk | |
161 | ? await installNpmPluginFromDisk(toInstall) | |
162 | : await installNpmPlugin(toInstall, version) | |
163 | ||
164 | name = fromDisk ? basename(toInstall) : toInstall | |
165 | const pluginType = name.startsWith('peertube-theme-') ? PluginType.THEME : PluginType.PLUGIN | |
166 | const pluginName = this.normalizePluginName(name) | |
167 | ||
168 | const packageJSON = this.getPackageJSON(pluginName, pluginType) | |
169 | if (!isPackageJSONValid(packageJSON, pluginType)) { | |
170 | throw new Error('PackageJSON is invalid.') | |
171 | } | |
172 | ||
173 | [ plugin ] = await PluginModel.upsert({ | |
174 | name: pluginName, | |
175 | description: packageJSON.description, | |
176 | type: pluginType, | |
177 | version: packageJSON.version, | |
178 | enabled: true, | |
179 | uninstalled: false, | |
180 | peertubeEngine: packageJSON.engine.peertube | |
181 | }, { returning: true }) | |
182 | } catch (err) { | |
183 | logger.error('Cannot install plugin %s, removing it...', toInstall, { err }) | |
184 | ||
185 | try { | |
186 | await removeNpmPlugin(name) | |
187 | } catch (err) { | |
188 | logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err }) | |
189 | } | |
190 | ||
191 | throw err | |
192 | } | |
193 | ||
194 | logger.info('Successful installation of plugin %s.', toInstall) | |
195 | ||
196 | await this.registerPluginOrTheme(plugin) | |
197 | } | |
198 | ||
199 | async uninstall (packageName: string) { | |
2c053942 C |
200 | logger.info('Uninstalling plugin %s.', packageName) |
201 | ||
202 | const pluginName = this.normalizePluginName(packageName) | |
203 | ||
204 | try { | |
205 | await this.unregister(pluginName) | |
206 | } catch (err) { | |
207 | logger.warn('Cannot unregister plugin %s.', pluginName, { err }) | |
208 | } | |
209 | ||
210 | const plugin = await PluginModel.load(pluginName) | |
211 | if (!plugin || plugin.uninstalled === true) { | |
212 | logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', packageName) | |
213 | return | |
214 | } | |
215 | ||
216 | plugin.enabled = false | |
217 | plugin.uninstalled = true | |
218 | ||
219 | await plugin.save() | |
f023a19c C |
220 | |
221 | await removeNpmPlugin(packageName) | |
2c053942 C |
222 | |
223 | logger.info('Plugin %s uninstalled.', packageName) | |
f023a19c C |
224 | } |
225 | ||
ad91e700 C |
226 | // ###################### Private register ###################### |
227 | ||
345da516 C |
228 | private async registerPluginOrTheme (plugin: PluginModel) { |
229 | logger.info('Registering plugin or theme %s.', plugin.name) | |
230 | ||
f023a19c C |
231 | const packageJSON = this.getPackageJSON(plugin.name, plugin.type) |
232 | const pluginPath = this.getPluginPath(plugin.name, plugin.type) | |
345da516 C |
233 | |
234 | if (!isPackageJSONValid(packageJSON, plugin.type)) { | |
235 | throw new Error('Package.JSON is invalid.') | |
236 | } | |
237 | ||
238 | let library: PluginLibrary | |
239 | if (plugin.type === PluginType.PLUGIN) { | |
240 | library = await this.registerPlugin(plugin, pluginPath, packageJSON) | |
241 | } | |
242 | ||
2c053942 C |
243 | const clientScripts: { [id: string]: ClientScript } = {} |
244 | for (const c of packageJSON.clientScripts) { | |
245 | clientScripts[c.script] = c | |
246 | } | |
247 | ||
345da516 C |
248 | this.registeredPlugins[ plugin.name ] = { |
249 | name: plugin.name, | |
250 | type: plugin.type, | |
251 | version: plugin.version, | |
252 | description: plugin.description, | |
253 | peertubeEngine: plugin.peertubeEngine, | |
254 | path: pluginPath, | |
255 | staticDirs: packageJSON.staticDirs, | |
2c053942 | 256 | clientScripts, |
345da516 C |
257 | css: packageJSON.css, |
258 | unregister: library ? library.unregister : undefined | |
259 | } | |
260 | } | |
261 | ||
262 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { | |
263 | const registerHook = (options: RegisterHookOptions) => { | |
264 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | |
265 | ||
266 | this.hooks[options.target].push({ | |
267 | pluginName: plugin.name, | |
268 | handler: options.handler, | |
269 | priority: options.priority || 0 | |
270 | }) | |
271 | } | |
272 | ||
ad91e700 C |
273 | const registerSetting = (options: RegisterSettingOptions) => { |
274 | if (!this.settings[plugin.name]) this.settings[plugin.name] = [] | |
275 | ||
276 | this.settings[plugin.name].push(options) | |
277 | } | |
278 | ||
279 | const settingsManager: PluginSettingsManager = { | |
280 | getSetting: (name: string) => PluginModel.getSetting(plugin.name, name), | |
281 | ||
282 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, name, value) | |
283 | } | |
284 | ||
345da516 | 285 | const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) |
f023a19c | 286 | |
345da516 C |
287 | if (!isLibraryCodeValid(library)) { |
288 | throw new Error('Library code is not valid (miss register or unregister function)') | |
289 | } | |
290 | ||
ad91e700 | 291 | library.register({ registerHook, registerSetting, settingsManager }) |
345da516 C |
292 | |
293 | logger.info('Add plugin %s CSS to global file.', plugin.name) | |
294 | ||
295 | await this.addCSSToGlobalFile(pluginPath, packageJSON.css) | |
296 | ||
297 | return library | |
298 | } | |
299 | ||
ad91e700 | 300 | // ###################### CSS ###################### |
345da516 | 301 | |
2c053942 C |
302 | private resetCSSGlobalFile () { |
303 | return outputFile(PLUGIN_GLOBAL_CSS_PATH, '') | |
304 | } | |
305 | ||
345da516 C |
306 | private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) { |
307 | for (const cssPath of cssRelativePaths) { | |
308 | await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH) | |
309 | } | |
310 | } | |
311 | ||
312 | private concatFiles (input: string, output: string) { | |
313 | return new Promise<void>((res, rej) => { | |
2c053942 C |
314 | const inputStream = createReadStream(input) |
315 | const outputStream = createWriteStream(output, { flags: 'a' }) | |
345da516 C |
316 | |
317 | inputStream.pipe(outputStream) | |
318 | ||
319 | inputStream.on('end', () => res()) | |
320 | inputStream.on('error', err => rej(err)) | |
321 | }) | |
322 | } | |
323 | ||
ad91e700 C |
324 | private async regeneratePluginGlobalCSS () { |
325 | await this.resetCSSGlobalFile() | |
326 | ||
327 | for (const key of Object.keys(this.registeredPlugins)) { | |
328 | const plugin = this.registeredPlugins[key] | |
329 | ||
330 | await this.addCSSToGlobalFile(plugin.path, plugin.css) | |
331 | } | |
332 | } | |
333 | ||
334 | // ###################### Utils ###################### | |
335 | ||
336 | private sortHooksByPriority () { | |
337 | for (const hookName of Object.keys(this.hooks)) { | |
338 | this.hooks[hookName].sort((a, b) => { | |
339 | return b.priority - a.priority | |
340 | }) | |
341 | } | |
342 | } | |
343 | ||
f023a19c C |
344 | private getPackageJSON (pluginName: string, pluginType: PluginType) { |
345 | const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json') | |
346 | ||
347 | return require(pluginPath) as PluginPackageJson | |
348 | } | |
349 | ||
350 | private getPluginPath (pluginName: string, pluginType: PluginType) { | |
351 | const prefix = pluginType === PluginType.PLUGIN ? 'peertube-plugin-' : 'peertube-theme-' | |
352 | ||
353 | return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', prefix + pluginName) | |
354 | } | |
355 | ||
356 | private normalizePluginName (name: string) { | |
357 | return name.replace(/^peertube-((theme)|(plugin))-/, '') | |
358 | } | |
359 | ||
ad91e700 | 360 | // ###################### Private getters ###################### |
2c053942 | 361 | |
7cd4d2ba C |
362 | private getRegisteredPluginsOrThemes (type: PluginType) { |
363 | const plugins: RegisteredPlugin[] = [] | |
364 | ||
365 | for (const pluginName of Object.keys(this.registeredPlugins)) { | |
366 | const plugin = this.registeredPlugins[ pluginName ] | |
367 | if (plugin.type !== type) continue | |
368 | ||
369 | plugins.push(plugin) | |
370 | } | |
371 | ||
372 | return plugins | |
373 | } | |
374 | ||
345da516 C |
375 | static get Instance () { |
376 | return this.instance || (this.instance = new this()) | |
377 | } | |
378 | } |