diff --git a/packages/plugin-console-logger/src/index.ts b/packages/plugin-console-logger/src/index.ts index a1dabc3b..9fc532a9 100644 --- a/packages/plugin-console-logger/src/index.ts +++ b/packages/plugin-console-logger/src/index.ts @@ -1,6 +1,5 @@ import { LogLevelName, - PocketHostAction, PocketHostPlugin, isLevelGte, isLevelLte, @@ -18,35 +17,38 @@ export const LogLevelConsoleMap = { } as const const plugin: PocketHostPlugin = async ({ registerAction }) => { - registerAction(PocketHostAction.Log, (currentLevel, levelIn, args) => { - if (!isLevelGte(levelIn, currentLevel)) return - const finalArgs = [args.shift()] - while (args.length > 0) { - let arg = args.shift() - const t = typeof arg - if (arg instanceof Error) { - finalArgs.push(...[arg.name, arg.message.toString()]) - if (isLevelLte(levelIn, LogLevelName.Debug) && arg.stack) { - finalArgs.push(...arg.stack.split(/\n/)) + registerAction( + `log`, + async (currentLevel: LogLevelName, levelIn: LogLevelName, args: any[]) => { + if (!isLevelGte(levelIn, currentLevel)) return + const finalArgs = [args.shift()] + while (args.length > 0) { + let arg = args.shift() + const t = typeof arg + if (arg instanceof Error) { + finalArgs.push(...[arg.name, arg.message.toString()]) + if (isLevelLte(levelIn, LogLevelName.Debug) && arg.stack) { + finalArgs.push(...arg.stack.split(/\n/)) + } + continue } - continue + if (t === 'string' && !!arg.match(/\n/)) { + finalArgs.push(...arg.split(/\n/)) + continue + } + if (t === 'object') { + finalArgs.push(...stringify(arg, undefined, 2).split(/\n/)) + continue + } + if (t === 'function') { + finalArgs.push(`<>`) + continue + } + finalArgs.push(arg) } - if (t === 'string' && !!arg.match(/\n/)) { - finalArgs.push(...arg.split(/\n/)) - continue - } - if (t === 'object') { - finalArgs.push(...stringify(arg, undefined, 2).split(/\n/)) - continue - } - if (t === 'function') { - finalArgs.push(`<>`) - continue - } - finalArgs.push(arg) - } - LogLevelConsoleMap[levelIn](...finalArgs) - }) + LogLevelConsoleMap[levelIn](...finalArgs) + }, + ) } export default plugin diff --git a/packages/pockethost/src/common/Logger.ts b/packages/pockethost/src/common/Logger.ts index 5df76376..ff48d578 100644 --- a/packages/pockethost/src/common/Logger.ts +++ b/packages/pockethost/src/common/Logger.ts @@ -2,7 +2,7 @@ import chalk from 'chalk' import stringify from 'json-stringify-safe' -import { PocketHostAction, action, mergeConfig, mkSingleton } from '.' +import { action, mergeConfig, mkSingleton } from '.' export type LoggerConfig = { level: LogLevelName @@ -74,7 +74,7 @@ export const createLogger = (config: Partial) => { .join(' ') const _log = (levelIn: LogLevelName, ...args: any[]) => { - action(PocketHostAction.Log, _config.level, levelIn, args) + action(`log`, _config.level, levelIn, args) } const raw = (...args: any[]) => { diff --git a/packages/pockethost/src/common/plugin.ts b/packages/pockethost/src/common/plugin.ts index e9d97a9a..0b0dc5fa 100644 --- a/packages/pockethost/src/common/plugin.ts +++ b/packages/pockethost/src/common/plugin.ts @@ -1,107 +1,49 @@ import { isString } from '@s-libs/micro-dash' -import { Request } from 'express' -import { InstanceFields, LogLevelName } from '.' -export enum PocketHostAction { - Request = 'request', - BeforeDaemonStart = 'before_daemon_start', - Log = 'log', - InstanceApiTimeout = 'instance_api_timeout', - UpdateInstance = 'update_instance', -} +export type PocketHostAction = string -export enum PocketHostFilter { - ExtraBinds = 'extra_binds', -} +export type PocketHostFilter = string export type PocketHostPlugin = (api: PocketHostPluginApi) => Promise -const filters: { [key in PocketHostFilter]: FilterHandler[] } = { - [PocketHostFilter.ExtraBinds]: [], -} +const filters: { [key in PocketHostFilter]: FilterHandler[] } = {} const actions: { [key in PocketHostAction]: ActionHandler[] -} = { - [PocketHostAction.Request]: [], - [PocketHostAction.BeforeDaemonStart]: [], - [PocketHostAction.Log]: [], - [PocketHostAction.InstanceApiTimeout]: [], - [PocketHostAction.UpdateInstance]: [], -} +} = {} export type FilterHandler = (carry: any) => any export type ActionHandler = (...args: any[]) => void export async function registerFilter( - filter: PocketHostFilter.ExtraBinds, - handler: (carry: string[]) => string[], + filter: PocketHostFilter, + handler: (carry: any) => any, ) { - filters[filter].push(handler) + if (!(filter in filters)) filters[filter] = [] + filters[filter]!.push(handler) } -export async function registerAction( - action: PocketHostAction.BeforeDaemonStart, - handler: (req: Request) => void, -): Promise -export async function registerAction( - action: PocketHostAction.Request, - handler: (req: Request) => void, -): Promise -export async function registerAction( - action: PocketHostAction.Log, - handler: ( - currentLevel: LogLevelName, - level: LogLevelName, - args: any[], - ) => void, -): Promise -export async function registerAction( - action: PocketHostAction.InstanceApiTimeout, - handler: (instance: InstanceFields) => void, -): Promise -export async function registerAction( - action: PocketHostAction.UpdateInstance, - handler: (id: string, fields: InstanceFields) => void, -): Promise export async function registerAction( action: PocketHostAction, - handler: ActionHandler, -): Promise { - actions[action].push(handler) -} - -export async function filter( - filter: PocketHostFilter.ExtraBinds, - initial: string[], + handler: (...args: any[]) => Promise, ) { - return filters[filter].reduce((carry, handler) => handler(carry), initial) + if (!(action in actions)) actions[action] = [] + actions[action]!.push(handler) } -export async function action( - action: PocketHostAction.Request, - req: Request, -): Promise -export async function action( - action: PocketHostAction.BeforeDaemonStart, -): Promise -export async function action( - action: PocketHostAction.Log, - currentLevel: LogLevelName, - levelIn: LogLevelName, - ...rest: any[] -): Promise -export async function action( - action: PocketHostAction.InstanceApiTimeout, - instance: InstanceFields, -): Promise -export async function action( - action: PocketHostAction.UpdateInstance, - id: string, - fields: Partial, -): Promise -export async function action(action: PocketHostAction, ...args: any[]) { - await Promise.all(actions[action].map((handler) => handler(...args))) +export async function filter(filterName: PocketHostFilter, initialValue: T) { + const filter = filters[filterName] + if (!filter) return initialValue + return filter.reduce( + (carry, handler) => carry.then((carryRes) => handler(carryRes)), + Promise.resolve(initialValue), + ) +} + +export async function action(actionName: PocketHostAction, ...rest: any[]) { + const action = actions[actionName] + if (!action) return + await Promise.all(action.map((handler) => handler(...rest))) } export type PocketHostPluginApi = {