diff --git a/entrypoint.example.ts b/entrypoint.example.ts index 5898a94..335e1f8 100644 --- a/entrypoint.example.ts +++ b/entrypoint.example.ts @@ -13,10 +13,10 @@ import { const msg = await readStdin(); const result = await pipeline(msg, [ - noopPolicy, - hellthreadPolicy, - antiDuplicationPolicy, - rateLimitPolicy, + [noopPolicy], + [hellthreadPolicy, { limit: 100 }], + [antiDuplicationPolicy], + [rateLimitPolicy], ]); writeStdout(result); diff --git a/src/pipeline.ts b/src/pipeline.ts index 70a0260..7d8e6c3 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,9 +1,16 @@ import { InputMessage, OutputMessage, Policy } from './types.ts'; +type PolicyTuple = [policy: Policy, opts?: Opts]; + +type PolicyTuplesRest = { + [K in keyof T]: PolicyTuple +} + /** Processes messages through multiple policies, bailing early on rejection. */ -async function pipeline(msg: InputMessage, policies: Policy[]): Promise { - for (const policy of policies) { - const result = await policy(msg); +async function pipeline

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

]): Promise { + for (const tuple of policies) { + const [policy, opts] = tuple; + const result = await policy(msg, opts); if (result.action !== 'accept') { return result; } diff --git a/src/policies/hellthread-policy.ts b/src/policies/hellthread-policy.ts index 419415b..bb980ee 100755 --- a/src/policies/hellthread-policy.ts +++ b/src/policies/hellthread-policy.ts @@ -1,17 +1,21 @@ import type { Policy } from '../types.ts'; -const HELLTHREAD_LIMIT = Number(Deno.env.get('HELLTHREAD_LIMIT') || 100); +interface Hellthread { + limit: number; +} /** Reject messages that tag too many participants. */ -const hellthreadPolicy: Policy = (msg) => { +const hellthreadPolicy: Policy = (msg, opts) => { + const limit = opts?.limit || 100; + if (msg.event.kind === 1) { const p = msg.event.tags.filter((tag) => tag[0] === 'p'); - if (p.length > HELLTHREAD_LIMIT) { + if (p.length > limit) { return { id: msg.event.id, action: 'reject', - msg: `Event rejected due to ${p.length} "p" tags (${HELLTHREAD_LIMIT} is the limit).`, + msg: `Event rejected due to ${p.length} "p" tags (${limit} is the limit).`, }; } } diff --git a/src/types.ts b/src/types.ts index 8f3f4a6..6243eaa 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,6 +22,6 @@ interface Event { created_at: number; } -type Policy = (msg: InputMessage) => Promise | OutputMessage; +type Policy = (msg: InputMessage, opts?: Opts) => Promise | OutputMessage; export type { Event, InputMessage, OutputMessage, Policy };