From b4055e1c23eeefb0c8a85a77f312b2827d98f483 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 18 Jul 2019 14:28:37 +0200 Subject: Add server hooks --- server/lib/plugins/hooks.ts | 26 ++++++++++++++++++++++++++ server/lib/plugins/plugin-manager.ts | 22 +++++++++------------- 2 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 server/lib/plugins/hooks.ts (limited to 'server/lib/plugins') diff --git a/server/lib/plugins/hooks.ts b/server/lib/plugins/hooks.ts new file mode 100644 index 000000000..7bb907e6a --- /dev/null +++ b/server/lib/plugins/hooks.ts @@ -0,0 +1,26 @@ +import { ServerActionHookName, ServerFilterHookName } from '../../../shared/models/plugins/server-hook.model' +import { PluginManager } from './plugin-manager' +import { logger } from '../../helpers/logger' +import * as Bluebird from 'bluebird' + +// Helpers to run hooks +const Hooks = { + wrapObject: (obj: T, hookName: U) => { + return PluginManager.Instance.runHook(hookName, obj) as Promise + }, + + wrapPromise: async (fun: Promise | Bluebird, hookName: U) => { + const result = await fun + + return PluginManager.Instance.runHook(hookName, result) + }, + + runAction: (hookName: U, params?: T) => { + PluginManager.Instance.runHook(hookName, params) + .catch(err => logger.error('Fatal hook error.', { err })) + } +} + +export { + Hooks +} diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 570b56193..85ee3decb 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -14,6 +14,10 @@ import { RegisterSettingOptions } from '../../../shared/models/plugins/register- import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' +import { ServerHookName, ServerHook } from '../../../shared/models/plugins/server-hook.model' +import { isCatchable, isPromise } from '../../../shared/core-utils/miscs/miscs' +import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' +import { HookType } from '../../../shared/models/plugins/hook-type.enum' export interface RegisteredPlugin { npmName: string @@ -42,7 +46,7 @@ export interface HookInformationValue { priority: number } -export class PluginManager { +export class PluginManager implements ServerHook { private static instance: PluginManager @@ -95,25 +99,17 @@ export class PluginManager { // ###################### Hooks ###################### - async runHook (hookName: string, param?: any) { + async runHook (hookName: ServerHookName, param?: any) { let result = param if (!this.hooks[hookName]) return result - const wait = hookName.startsWith('static:') + const hookType = getHookType(hookName) for (const hook of this.hooks[hookName]) { - try { - const p = hook.handler(param) - - if (wait) { - result = await p - } else if (p.catch) { - p.catch(err => logger.warn('Hook %s of plugin %s thrown an error.', hookName, hook.pluginName, { err })) - } - } catch (err) { + result = await internalRunHook(hook.handler, hookType, param, err => { logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) - } + }) } return result -- cgit v1.2.3