sifters

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Provides a set of out-of-the-box event-sifters and sifter combinators.

Sifter combinators can be used to build a complex sifter logic from small parts, built-in sifters as well as custom sifters from scratch.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Non-Parametarized Replaceable events: kind 0, 3, 41 and 10000 <= kind < 20000
	KindsAllNonParamReplaceable = func(k int) bool {
		return k == 0 || k == 3 || k == 41 || (10000 <= k && k < 20000)
	}
	// Parameterized replaceable events: kind 30000 <= kind < 40000
	KindsAllParamReplaceable = func(k int) bool {
		return 30000 <= k && k < 40000
	}
	// General replaceable events (including both parametarized and non-parameterized)
	KindsAllReplaceable = func(k int) bool {
		return KindsAllNonParamReplaceable(k) || KindsAllParamReplaceable(k)
	}
	// Ephemeral events: kind 20000 <= kind < 30000
	KindsAllEphemeral = func(k int) bool {
		return 20000 <= k && k < 30000
	}
	// Regular events
	KindsAllRegular = func(k int) bool {
		return !(KindsAllReplaceable(k) || KindsAllEphemeral(k))
	}
)

Functions

func ParseStringIPList

func ParseStringIPList(strIPs []string) ([]netip.Prefix, error)

ParseStringIPList parses a list of IP address and CIDRs in string form as a list of netip.Prefix.

IP addresses (without "/") are treated as IP prefixes that only contain the very address (e.g. 192.168.1.1 → 192.168.1.1/32, 2001:db8::1 → 2001:db8::1/128).

Types

type ModdedSifter

type ModdedSifter struct {
	// contains filtered or unexported fields
}

ModdedSifter is a sifter with modifiers, that change its behavior (especially in Pipeline / OneOf).

This type is exposed only for document organization purpose. You shouldn't initialize this struct directly.

func WithMod

func WithMod(s strfrui.Sifter) *ModdedSifter

WithMod makes the sifter "modifiable" by sifter modifiers. You can chain modification methods to modify behavior of the sifter.

func (*ModdedSifter) AcceptEarly

func (s *ModdedSifter) AcceptEarly() *ModdedSifter

AcceptEarly sets "accept early" flag to the sifter.

If a sifter that is modified by this method is used in [PipelineSifter]s and it accept event, pipelines accept it immediately, and all sifters after the sifter are skipped.

func (*ModdedSifter) Label

func (s *ModdedSifter) Label(label string) *ModdedSifter

Label labels the sifter.

func (*ModdedSifter) OnlyIf

func (s *ModdedSifter) OnlyIf(cond strfrui.Sifter) *ModdedSifter

OnlyIf makes the sifter is applied only if the given condition is met if it is used in [PipelineSifter]s or [OneOfSifter]s.

When the evaluation of a combined sifter come across a sifter modified by this, it first applies cond to an input. Then:

  • if cond accepts the input, the modified sifter is applied to the input normally.
  • if cond rejects the input, the modified sifter is skipped and move to next.

func (*ModdedSifter) OnlyIfNot

func (s *ModdedSifter) OnlyIfNot(cond strfrui.Sifter) *ModdedSifter

OnlyIfNot makes the sifter is applied only if the given condition is not met if it is used in [PipelineSifter]s or [OneOfSifter]s.

When the evaluation of a combined sifter come across a sifter modified by this, it first applies cond to an input. Then:

  • if cond rejects the input, the modified sifter is applied to the input normally.
  • if cond accepts the input, the modified sifter is skipped and move to next.

func (*ModdedSifter) Sift

func (s *ModdedSifter) Sift(input *strfrui.Input) (*strfrui.Result, error)

type Mode

type Mode int

Mode specifies the behavior of sifters when input matches the condition defined by the sifter:

  • Allow: Accept the input if the input matches the condition (i.e. whitelist).
  • Deny: Reject the input if the input matches the condition (i.e. blacklist).
const (
	Allow Mode = iota + 1
	Deny
)

type OneOfSifter

type OneOfSifter struct {
	// contains filtered or unexported fields
}

OneOfSifter is an event-sifter combinator that combines multiple sifters into one. The resulting sifter accepts an input if one of sub-sifters accept it. Otherwise, i.e. all sub-sifter rejects, the resulting sifter rejects.

OneOfSifter rejects with message: "blocked: any of sub-sifters didn't accept the event" by default. If you want to customize rejection behavior, call OneOfSifter.RejectWithMsg, OneOfSifter.RejectWithMsgFromInput or OneOfSifter.ShadowReject methods on it.

This type is exposed only for document organization purpose. You shouldn't initialize this struct directly. Instead, use OneOf function to construct an instance of OneOfSifter.

func OneOf

func OneOf(ss ...strfrui.Sifter) *OneOfSifter

OneOf combines the given sifters as a OneOfSifter.

For more details about the behavior of a resulting combined sifter, see the doc of OneOfSifter type.

func (*OneOfSifter) RejectWithMsg

func (s *OneOfSifter) RejectWithMsg(msg string) *OneOfSifter

RejectWithMsg makes the sifter reject the input with the given message.

func (*OneOfSifter) RejectWithMsgFromInput

func (s *OneOfSifter) RejectWithMsgFromInput(getMsg func(*strfrui.Input) string) *OneOfSifter

RejectWithMsgFromInput makes the sifter reject the input with the message derived from the input by the given function.

func (*OneOfSifter) ShadowReject

func (s *OneOfSifter) ShadowReject() *OneOfSifter

ShadowReject sets the sifter's rejection behavior to "shadow-reject", which pretend to accept the input but actually reject it.

func (*OneOfSifter) Sift

func (s *OneOfSifter) Sift(input *strfrui.Input) (*strfrui.Result, error)

type PipelineSifter

type PipelineSifter struct {
	// contains filtered or unexported fields
}

PipelineSifter is an event-sifter combinator that combines multiple sifters into one. The resulting sifter accepts an input if all sub-sifters accept it. Otherwise, i.e. one of sub-sifter rejects, the resulting sifter rejects with the result from that sub-sifter.

This type is exposed only for document organization purpose. You shouldn't initialize this struct directly. Instead, use Pipeline function to construct an instance of PipelineSifter.

func Pipeline

func Pipeline(ss ...strfrui.Sifter) *PipelineSifter

Pipeline combines the given sifters as a PipelineSifter.

For more details about the behavior of a resulting combined sifter, see the doc of PipelineSifter type.

func (*PipelineSifter) Sift

func (s *PipelineSifter) Sift(input *strfrui.Input) (*strfrui.Result, error)

type RelativeTimeRange

type RelativeTimeRange struct {
	MaxPastDelta   time.Duration
	MaxFutureDelta time.Duration
}

RelativeTimeRange represents a time range defined as a pair of maximum allowed duration in the past and future.

Either of the durations can be zero (or left unspecified), means the corresponding side of the range is unbounded.

func (RelativeTimeRange) Contains

func (r RelativeTimeRange) Contains(t time.Time) bool

Contains checks if the given time is in the time range.

func (RelativeTimeRange) String

func (r RelativeTimeRange) String() string

String returns a string representation of the time range.

type SifterUnit

type SifterUnit struct {
	// contains filtered or unexported fields
}

SifterUnit is base structure of composable event-sifter logic. All built-in sifters are instances of this struct.

If it comes to reject inputs, each built-in sifter responds to the client with its own predefined message. If you want to customize the rejection behavior, use SifterUnit.RejectWithMsg, SifterUnit.RejectWithMsgFromInput or SifterUnit.ShadowReject.

This type is exposed only for document organization purpose. You shouldn't initialize this struct directly.

func AuthorList

func AuthorList(authors []string, mode Mode) *SifterUnit

AuthorList makes an event-sifter that checks if the author (pubkey) of a Nostr event is in the given list.

func AuthorMatcher

func AuthorMatcher(matcher func(string) (bool, error), mode Mode) *SifterUnit

AuthorMatcher makes an event-sifter that matches the author (pubkey) of a Nostr event with the given matcher function.

If the matcher returns non-nil error, this sifter always rejects the input.

func ContentHasAllWords

func ContentHasAllWords(words []string, mode Mode) *SifterUnit

ContentHasAllWords makes an event-sifter that checks if a content of a Nostr event has all words in the given list.

Note that it performs case-sensitive match.

func ContentHasAnyWord

func ContentHasAnyWord(words []string, mode Mode) *SifterUnit

ContentHasAnyWord makes an event-sifter that checks if a content of a Nostr event has any word in the given list.

Note that it performs case-sensitive match.

func ContentMatcher

func ContentMatcher(matcher func(string) (bool, error), mode Mode) *SifterUnit

ContentMatcher makes an event-sifter that matches a content of a Nostr event with the matcher function.

If the matcher returns non-nil error, this sifter always rejects the input.

func ContentMatchesAllRegexps

func ContentMatchesAllRegexps(regexps []*regexp.Regexp, mode Mode) *SifterUnit

ContentMatchesAllRegexps makes an event-sifter that checks if a content of a Nostr event matches all of the given list of regular expressions.

func ContentMatchesAnyRegexp

func ContentMatchesAnyRegexp(regexps []*regexp.Regexp, mode Mode) *SifterUnit

ContentMatchesAnyRegexp makes an event-sifter that checks if a content of a Nostr event matches any of the given list of regular expressions.

func CreatedAtRange

func CreatedAtRange(timeRange RelativeTimeRange, mode Mode) *SifterUnit

CreatedAtRange makes an event-sifter that checks if the creation timestamp (created_at) of a Nostr event is in the given time range.

func KindList

func KindList(kinds []int, mode Mode) *SifterUnit

KindList makes an event-sifter that checks if the kind of a Nostr event is in the given list.

func KindMatcher

func KindMatcher(matcher func(int) bool, mode Mode) *SifterUnit

KindMatcher makes an event-sifter that matches the kind of a Nostr event with the given matcher function.

Note that the matcher function can't return any error unlike other XxxMatcher sifters. To use a fallible matcher, you may want to KindMatcherFallible instead.

func KindMatcherFallible

func KindMatcherFallible(matcher func(int) (bool, error), mode Mode) *SifterUnit

KindMatcherFallible makes an event-sifter that matches the kind of a Nostr event with the given matcher function that is fallible (i.e. can return error).

If the matcher returns non-nil error, this sifter always rejects the input.

func MatchesFilters

func MatchesFilters(filters []nostr.Filter, mode Mode) *SifterUnit

MatchesFilters makes an event-sifter that matches a Nostr event against the given Nostr filters.

func PoWMinDifficulty

func PoWMinDifficulty(minDifficulty uint) *SifterUnit

PoWMinDifficulty makes an event-sifter that checks if the Proof of Work (PoW) difficulty of a Nostr event is higher than or equal to the given minimum difficulty.

About PoW for Nostr events, see NIP-13. Note that this sifter doesn't check if the "target difficulty" declared by the nonce tag is achieved.

func SourceIPMatcher

func SourceIPMatcher(matcher func(netip.Addr) (bool, error), mode Mode, modeForUnknownSource Mode) *SifterUnit

SourceIPMatcher makes an event-sifter that matches the source IP address of a Nostr event with the matcher function. modeForUnknownSource specifies the behavior when the source IP address can't be determied.

Note that this sifter always accepts events not from end-users (i.e. events imported from other relays).

If the matcher returns non-nil error, this sifter always rejects the input.

func SourceIPPrefixList

func SourceIPPrefixList(ipPrefixes []netip.Prefix, mode Mode, modeForUnknownSource Mode) *SifterUnit

SourceIPPrefixList makes an event-sifter that checks the source IP address of a Nostr event with list of IP address prefixes (CIDRs). modeForUnknownSource specifies the behavior when the source IP address can't be determied.

You can use ParseStringIPList to parse a list of string IP address and CIDRs.

Note that this sifter always accepts events not from end-users (i.e. events imported from other relays).

func TagsMatcher

func TagsMatcher(matcher func(nostr.Tags) (bool, error), mode Mode) *SifterUnit

TagsMatcher makes an event-sifter that matches the tag list of a Nostr event with the given matcher function. You can utilize various matching methods on github.com/nbd-wtf/go-nostr.Tags in the matcher.

If the matcher returns non-nil error, this sifter always rejects the input.

func (*SifterUnit) RejectWithMsg

func (s *SifterUnit) RejectWithMsg(msg string) *SifterUnit

RejectWithMsg makes the sifter reject the input with the given message.

func (*SifterUnit) RejectWithMsgFromInput

func (s *SifterUnit) RejectWithMsgFromInput(getMsg func(*strfrui.Input) string) *SifterUnit

RejectWithMsgFromInput makes the sifter reject the input with the message derived from the input by the given function.

func (*SifterUnit) ShadowReject

func (s *SifterUnit) ShadowReject() *SifterUnit

ShadowReject sets the sifter's rejection behavior to "shadow-reject", which pretend to accept the input but actually reject it.

func (*SifterUnit) Sift

func (s *SifterUnit) Sift(input *strfrui.Input) (*strfrui.Result, error)

Directories

Path Synopsis
Provides out-of-the-box rate limiting event-sifters.
Provides out-of-the-box rate limiting event-sifters.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL