From 5a6af744132415c4905fbf082936c686465209e3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 24 Mar 2023 20:17:51 -0500 Subject: [PATCH] Add comments to types, deno fmt --- src/pipeline.ts | 11 ++++++++--- src/types.ts | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/pipeline.ts b/src/pipeline.ts index 7297844..3c5d1dc 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,15 +1,20 @@ import { InputMessage, OutputMessage, Policy } from './types.ts'; +/** A policy function with opts to run it with. Used by the pipeline. */ type PolicyTuple = [policy: Policy, opts?: Opts]; +/** Helper type for proper type inference of PolicyTuples in the pipeline. */ // https://stackoverflow.com/a/75806165 // https://stackoverflow.com/a/54608401 type PolicyTuplesRest = { - [K in keyof T]: PolicyTuple | Policy -} + [K in keyof T]: PolicyTuple | Policy; +}; /** Processes messages through multiple policies, bailing early on rejection. */ -async function pipeline

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

]): Promise { +async function pipeline

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

], +): Promise { for (const item of policies) { const [policy, opts] = toTuple(item); const result = await policy(msg, opts); diff --git a/src/types.ts b/src/types.ts index 6243eaa..f5e45c5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,17 +1,37 @@ +/** + * strfry input message from stdin. + * https://github.com/hoytech/strfry/blob/master/docs/plugins.md#input-messages + */ interface InputMessage { + /** Either `new` or `lookback`. */ type: 'new' | 'lookback'; + /** The event posted by the client, with all the required fields such as `id`, `pubkey`, etc. */ event: Event; + /** Unix timestamp of when this event was received by the relay. */ receivedAt: number; + /** Where this event came from. Typically will be `IP4` or `IP6`, but in lookback can also be `Import`, `Stream`, or `Sync`. */ sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync'; + /** Specifics of the event's source. Either an IP address or a relay URL (for stream/sync). */ sourceInfo: string; } +/** + * strfry output message to be printed as JSONL (minified JSON followed by a newline) to stdout. + * https://github.com/hoytech/strfry/blob/master/docs/plugins.md#output-messages + */ interface OutputMessage { + /** The event ID taken from the `event.id` field of the input message. */ id: string; + /** Either `accept`, `reject`, or `shadowReject`. */ action: 'accept' | 'reject' | 'shadowReject'; + /** The NIP-20 response message to be sent to the client. Only used for `reject`. */ msg: string; } +/** + * Nostr event. + * https://github.com/nostr-protocol/nips/blob/master/01.md + */ interface Event { id: string; sig: string; @@ -22,6 +42,10 @@ interface Event { created_at: number; } +/** + * A policy function in this library. + * It accepts an input message, opts, and returns an output message. + */ type Policy = (msg: InputMessage, opts?: Opts) => Promise | OutputMessage; export type { Event, InputMessage, OutputMessage, Policy };