mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 02:09:37 +02:00
Add server hooks
This commit is contained in:
parent
66e001c848
commit
b4055e1c23
17 changed files with 422 additions and 112 deletions
41
shared/core-utils/plugins/hooks.ts
Normal file
41
shared/core-utils/plugins/hooks.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { HookType } from '../../models/plugins/hook-type.enum'
|
||||
import { isCatchable, isPromise } from '../miscs/miscs'
|
||||
|
||||
function getHookType (hookName: string) {
|
||||
if (hookName.startsWith('filter:')) return HookType.FILTER
|
||||
if (hookName.startsWith('action:')) return HookType.ACTION
|
||||
|
||||
return HookType.STATIC
|
||||
}
|
||||
|
||||
async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) {
|
||||
let result = param
|
||||
|
||||
try {
|
||||
const p = handler(result)
|
||||
|
||||
switch (hookType) {
|
||||
case HookType.FILTER:
|
||||
if (isPromise(p)) result = await p
|
||||
else result = p
|
||||
break
|
||||
|
||||
case HookType.STATIC:
|
||||
if (isPromise(p)) await p
|
||||
break
|
||||
|
||||
case HookType.ACTION:
|
||||
if (isCatchable(p)) p.catch(err => onError(err))
|
||||
break
|
||||
}
|
||||
} catch (err) {
|
||||
onError(err)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export {
|
||||
getHookType,
|
||||
internalRunHook
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue