import { InputMessage, OutputMessage, Policy } from './types.ts'; type PolicyTuple = [policy: Policy, opts?: Opts]; // https://stackoverflow.com/a/75806165 // https://stackoverflow.com/a/54608401 type PolicyTuplesRest = { [K in keyof T]: PolicyTuple | Policy } /** Processes messages through multiple policies, bailing early on rejection. */ async function pipeline

(msg: InputMessage, policies: [...PolicyTuplesRest

]): Promise { for (const item of policies) { const [policy, opts] = toTuple(item); const result = await policy(msg, opts); if (result.action !== 'accept') { return result; } } return { id: msg.event.id, action: 'accept', msg: '', }; } /** Coerce item into a tuple if it isn't already. */ function toTuple(item: PolicyTuple | Policy): PolicyTuple { return typeof item === 'function' ? [item] : item; } export default pipeline;