Add comments to types, deno fmt
This commit is contained in:
parent
f2f4dd7b8e
commit
5a6af74413
@ -1,15 +1,20 @@
|
|||||||
import { InputMessage, OutputMessage, Policy } from './types.ts';
|
import { InputMessage, OutputMessage, Policy } from './types.ts';
|
||||||
|
|
||||||
|
/** A policy function with opts to run it with. Used by the pipeline. */
|
||||||
type PolicyTuple<Opts = unknown> = [policy: Policy<Opts>, opts?: Opts];
|
type PolicyTuple<Opts = unknown> = [policy: Policy<Opts>, opts?: Opts];
|
||||||
|
|
||||||
|
/** Helper type for proper type inference of PolicyTuples in the pipeline. */
|
||||||
// https://stackoverflow.com/a/75806165
|
// https://stackoverflow.com/a/75806165
|
||||||
// https://stackoverflow.com/a/54608401
|
// https://stackoverflow.com/a/54608401
|
||||||
type PolicyTuplesRest<T extends PolicyTuple[]> = {
|
type PolicyTuplesRest<T extends PolicyTuple[]> = {
|
||||||
[K in keyof T]: PolicyTuple<T[K]> | Policy<T[K]>
|
[K in keyof T]: PolicyTuple<T[K]> | Policy<T[K]>;
|
||||||
}
|
};
|
||||||
|
|
||||||
/** Processes messages through multiple policies, bailing early on rejection. */
|
/** Processes messages through multiple policies, bailing early on rejection. */
|
||||||
async function pipeline<P extends any[]>(msg: InputMessage, policies: [...PolicyTuplesRest<P>]): Promise<OutputMessage> {
|
async function pipeline<P extends any[]>(
|
||||||
|
msg: InputMessage,
|
||||||
|
policies: [...PolicyTuplesRest<P>],
|
||||||
|
): Promise<OutputMessage> {
|
||||||
for (const item of policies) {
|
for (const item of policies) {
|
||||||
const [policy, opts] = toTuple(item);
|
const [policy, opts] = toTuple(item);
|
||||||
const result = await policy(msg, opts);
|
const result = await policy(msg, opts);
|
||||||
|
24
src/types.ts
24
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 {
|
interface InputMessage {
|
||||||
|
/** Either `new` or `lookback`. */
|
||||||
type: 'new' | 'lookback';
|
type: 'new' | 'lookback';
|
||||||
|
/** The event posted by the client, with all the required fields such as `id`, `pubkey`, etc. */
|
||||||
event: Event;
|
event: Event;
|
||||||
|
/** Unix timestamp of when this event was received by the relay. */
|
||||||
receivedAt: number;
|
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';
|
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;
|
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 {
|
interface OutputMessage {
|
||||||
|
/** The event ID taken from the `event.id` field of the input message. */
|
||||||
id: string;
|
id: string;
|
||||||
|
/** Either `accept`, `reject`, or `shadowReject`. */
|
||||||
action: 'accept' | 'reject' | 'shadowReject';
|
action: 'accept' | 'reject' | 'shadowReject';
|
||||||
|
/** The NIP-20 response message to be sent to the client. Only used for `reject`. */
|
||||||
msg: string;
|
msg: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nostr event.
|
||||||
|
* https://github.com/nostr-protocol/nips/blob/master/01.md
|
||||||
|
*/
|
||||||
interface Event<K extends number = number> {
|
interface Event<K extends number = number> {
|
||||||
id: string;
|
id: string;
|
||||||
sig: string;
|
sig: string;
|
||||||
@ -22,6 +42,10 @@ interface Event<K extends number = number> {
|
|||||||
created_at: number;
|
created_at: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A policy function in this library.
|
||||||
|
* It accepts an input message, opts, and returns an output message.
|
||||||
|
*/
|
||||||
type Policy<Opts = unknown> = (msg: InputMessage, opts?: Opts) => Promise<OutputMessage> | OutputMessage;
|
type Policy<Opts = unknown> = (msg: InputMessage, opts?: Opts) => Promise<OutputMessage> | OutputMessage;
|
||||||
|
|
||||||
export type { Event, InputMessage, OutputMessage, Policy };
|
export type { Event, InputMessage, OutputMessage, Policy };
|
||||||
|
Loading…
Reference in New Issue
Block a user