diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 90 | ||||
-rw-r--r-- | server/typings/plugins/index.d.ts | 2 | ||||
-rw-r--r-- | server/typings/plugins/plugin-library.model.ts | 7 | ||||
-rw-r--r-- | server/typings/plugins/register-options.model.ts | 19 |
4 files changed, 79 insertions, 39 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index e76dbb53e..ac31b06dc 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -4,7 +4,6 @@ import { basename, join } from 'path' | |||
4 | import { CONFIG } from '../../initializers/config' | 4 | import { CONFIG } from '../../initializers/config' |
5 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' | 5 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' |
6 | import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model' | 6 | import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model' |
7 | import { PluginLibrary } from '../../../shared/models/plugins/plugin-library.model' | ||
8 | import { createReadStream, createWriteStream } from 'fs' | 7 | import { createReadStream, createWriteStream } from 'fs' |
9 | import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' | 8 | import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' |
10 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | 9 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
@@ -14,10 +13,10 @@ import { RegisterSettingOptions } from '../../../shared/models/plugins/register- | |||
14 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' | 13 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' |
15 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' | 14 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' |
16 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' | 15 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' |
17 | import { ServerHookName, ServerHook } from '../../../shared/models/plugins/server-hook.model' | 16 | import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model' |
18 | import { isCatchable, isPromise } from '../../../shared/core-utils/miscs/miscs' | ||
19 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' | 17 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' |
20 | import { HookType } from '../../../shared/models/plugins/hook-type.enum' | 18 | import { RegisterOptions } from '../../typings/plugins/register-options.model' |
19 | import { PluginLibrary } from '../../typings/plugins' | ||
21 | 20 | ||
22 | export interface RegisteredPlugin { | 21 | export interface RegisteredPlugin { |
23 | npmName: string | 22 | npmName: string |
@@ -287,47 +286,15 @@ export class PluginManager implements ServerHook { | |||
287 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { | 286 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { |
288 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) | 287 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
289 | 288 | ||
290 | const registerHook = (options: RegisterHookOptions) => { | ||
291 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | ||
292 | |||
293 | this.hooks[options.target].push({ | ||
294 | npmName, | ||
295 | pluginName: plugin.name, | ||
296 | handler: options.handler, | ||
297 | priority: options.priority || 0 | ||
298 | }) | ||
299 | } | ||
300 | |||
301 | const registerSetting = (options: RegisterSettingOptions) => { | ||
302 | if (!this.settings[npmName]) this.settings[npmName] = [] | ||
303 | |||
304 | this.settings[npmName].push(options) | ||
305 | } | ||
306 | |||
307 | const settingsManager: PluginSettingsManager = { | ||
308 | getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name), | ||
309 | |||
310 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) | ||
311 | } | ||
312 | |||
313 | const storageManager: PluginStorageManager = { | ||
314 | getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key), | ||
315 | |||
316 | storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data) | ||
317 | } | ||
318 | |||
319 | const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) | 289 | const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) |
320 | 290 | ||
321 | if (!isLibraryCodeValid(library)) { | 291 | if (!isLibraryCodeValid(library)) { |
322 | throw new Error('Library code is not valid (miss register or unregister function)') | 292 | throw new Error('Library code is not valid (miss register or unregister function)') |
323 | } | 293 | } |
324 | 294 | ||
325 | library.register({ | 295 | const registerHelpers = this.getRegisterHelpers(npmName, plugin) |
326 | registerHook, | 296 | library.register(registerHelpers) |
327 | registerSetting, | 297 | .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) |
328 | settingsManager, | ||
329 | storageManager | ||
330 | }).catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) | ||
331 | 298 | ||
332 | logger.info('Add plugin %s CSS to global file.', npmName) | 299 | logger.info('Add plugin %s CSS to global file.', npmName) |
333 | 300 | ||
@@ -407,6 +374,51 @@ export class PluginManager implements ServerHook { | |||
407 | return plugins | 374 | return plugins |
408 | } | 375 | } |
409 | 376 | ||
377 | // ###################### Generate register helpers ###################### | ||
378 | |||
379 | private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterOptions { | ||
380 | const registerHook = (options: RegisterHookOptions) => { | ||
381 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | ||
382 | |||
383 | this.hooks[options.target].push({ | ||
384 | npmName, | ||
385 | pluginName: plugin.name, | ||
386 | handler: options.handler, | ||
387 | priority: options.priority || 0 | ||
388 | }) | ||
389 | } | ||
390 | |||
391 | const registerSetting = (options: RegisterSettingOptions) => { | ||
392 | if (!this.settings[npmName]) this.settings[npmName] = [] | ||
393 | |||
394 | this.settings[npmName].push(options) | ||
395 | } | ||
396 | |||
397 | const settingsManager: PluginSettingsManager = { | ||
398 | getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name), | ||
399 | |||
400 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) | ||
401 | } | ||
402 | |||
403 | const storageManager: PluginStorageManager = { | ||
404 | getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key), | ||
405 | |||
406 | storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data) | ||
407 | } | ||
408 | |||
409 | const peertubeHelpers = { | ||
410 | logger | ||
411 | } | ||
412 | |||
413 | return { | ||
414 | registerHook, | ||
415 | registerSetting, | ||
416 | settingsManager, | ||
417 | storageManager, | ||
418 | peertubeHelpers | ||
419 | } | ||
420 | } | ||
421 | |||
410 | static get Instance () { | 422 | static get Instance () { |
411 | return this.instance || (this.instance = new this()) | 423 | return this.instance || (this.instance = new this()) |
412 | } | 424 | } |
diff --git a/server/typings/plugins/index.d.ts b/server/typings/plugins/index.d.ts new file mode 100644 index 000000000..bddb3089e --- /dev/null +++ b/server/typings/plugins/index.d.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './register-options.model' | ||
2 | export * from './plugin-library.model' | ||
diff --git a/server/typings/plugins/plugin-library.model.ts b/server/typings/plugins/plugin-library.model.ts new file mode 100644 index 000000000..fd90a3b46 --- /dev/null +++ b/server/typings/plugins/plugin-library.model.ts | |||
@@ -0,0 +1,7 @@ | |||
1 | import { RegisterOptions } from './register-options.model' | ||
2 | |||
3 | export interface PluginLibrary { | ||
4 | register: (options: RegisterOptions) => Promise<any> | ||
5 | |||
6 | unregister: () => Promise<any> | ||
7 | } | ||
diff --git a/server/typings/plugins/register-options.model.ts b/server/typings/plugins/register-options.model.ts new file mode 100644 index 000000000..007ef5e1b --- /dev/null +++ b/server/typings/plugins/register-options.model.ts | |||
@@ -0,0 +1,19 @@ | |||
1 | import { logger } from '../../helpers/logger' | ||
2 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' | ||
3 | import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model' | ||
4 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' | ||
5 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' | ||
6 | |||
7 | export type RegisterOptions = { | ||
8 | registerHook: (options: RegisterHookOptions) => void | ||
9 | |||
10 | registerSetting: (options: RegisterSettingOptions) => void | ||
11 | |||
12 | settingsManager: PluginSettingsManager | ||
13 | |||
14 | storageManager: PluginStorageManager | ||
15 | |||
16 | peertubeHelpers: { | ||
17 | logger: typeof logger | ||
18 | } | ||
19 | } | ||