aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/plugins/hooks.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-18 14:28:37 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commitb4055e1c23eeefb0c8a85a77f312b2827d98f483 (patch)
tree51b6b04c1ad10897047817d2eaaa037d1331fa6a /shared/core-utils/plugins/hooks.ts
parent66e001c848c009412c65cbce41be344d8985fd83 (diff)
downloadPeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.gz
PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.zst
PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.zip
Add server hooks
Diffstat (limited to 'shared/core-utils/plugins/hooks.ts')
-rw-r--r--shared/core-utils/plugins/hooks.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/shared/core-utils/plugins/hooks.ts b/shared/core-utils/plugins/hooks.ts
new file mode 100644
index 000000000..047c04f7b
--- /dev/null
+++ b/shared/core-utils/plugins/hooks.ts
@@ -0,0 +1,41 @@
1import { HookType } from '../../models/plugins/hook-type.enum'
2import { isCatchable, isPromise } from '../miscs/miscs'
3
4function getHookType (hookName: string) {
5 if (hookName.startsWith('filter:')) return HookType.FILTER
6 if (hookName.startsWith('action:')) return HookType.ACTION
7
8 return HookType.STATIC
9}
10
11async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) {
12 let result = param
13
14 try {
15 const p = handler(result)
16
17 switch (hookType) {
18 case HookType.FILTER:
19 if (isPromise(p)) result = await p
20 else result = p
21 break
22
23 case HookType.STATIC:
24 if (isPromise(p)) await p
25 break
26
27 case HookType.ACTION:
28 if (isCatchable(p)) p.catch(err => onError(err))
29 break
30 }
31 } catch (err) {
32 onError(err)
33 }
34
35 return result
36}
37
38export {
39 getHookType,
40 internalRunHook
41}