2023-03-24 21:08:36 +00:00
|
|
|
import { InputMessage, OutputMessage, Policy } from './types.ts';
|
2023-03-24 19:36:11 +00:00
|
|
|
|
2023-03-25 00:55:58 +00:00
|
|
|
type PolicyTuple<Opts = any> = [policy: Policy<Opts>, opts?: Opts];
|
|
|
|
|
|
|
|
type PolicyTuplesRest<T extends PolicyTuple[]> = {
|
|
|
|
[K in keyof T]: PolicyTuple<T[K]>
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:08:36 +00:00
|
|
|
/** Processes messages through multiple policies, bailing early on rejection. */
|
2023-03-25 00:55:58 +00:00
|
|
|
async function pipeline<P extends any[]>(msg: InputMessage, policies: [...PolicyTuplesRest<P>]): Promise<OutputMessage> {
|
|
|
|
for (const tuple of policies) {
|
|
|
|
const [policy, opts] = tuple;
|
|
|
|
const result = await policy(msg, opts);
|
2023-03-24 21:08:36 +00:00
|
|
|
if (result.action !== 'accept') {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: msg.event.id,
|
|
|
|
action: 'accept',
|
|
|
|
msg: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default pipeline;
|