relay

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientInterface

type ClientInterface interface {
	SendMessage(msg interface{})
	// SendMessageBlocking enqueues with backpressure for REQ-fulfillment
	// loops that need producer/consumer rate parity. Never call from
	// BroadcastEvent. Returns non-nil if the client has gone away.
	SendMessageBlocking(msg interface{}) error
	GetWS() *websocket.Conn
	GetSubscriptions() map[string][]Filter
	SetSubscription(subID string, filters []Filter)
	DeleteSubscription(subID string)
	SubscriptionCount() int
	// OldestSubscription returns the sub whose latest REQ is oldest, for
	// eviction at the per-client cap.
	OldestSubscription() (string, bool)
	ForEachSubscription(fn func(subID string, filters []Filter))
	// NoticeOnce reports whether the NOTICE keyed by key is new on this
	// connection, marking it sent.
	NoticeOnce(key string) bool
	CloseClient()
	IsConnected() bool
	// AllowReq checks the client's per-connection REQ rate limiter.
	// Returns (true, "") if allowed, (false, reason) if rate limited.
	AllowReq() (bool, string)
	// AllowEvent checks the client's per-connection event rate limiter.
	AllowEvent(kind int, category string) (bool, string)
}

ClientInterface abstracts WebSocket clients

type Event

type Event struct {
	ID        string     `json:"id"`
	PubKey    string     `json:"pubkey"`
	CreatedAt int64      `json:"created_at"`
	Kind      int        `json:"kind"`
	Tags      [][]string `json:"tags"`
	Content   string     `json:"content"`
	Sig       string     `json:"sig"`
}

func (Event) CanonicalJSON added in v0.8.0

func (e Event) CanonicalJSON() []byte

CanonicalJSON returns the event as a JSON object for nostrdb's ingester. It escapes like Commitment except that control characters stay raw: nostrdb's parser can't read \u escapes but takes raw control characters, and its own id derivation escapes them the way Commitment does, so the id it computes matches. Nil tags encode as [].

func (Event) Commitment added in v0.8.0

func (e Event) Commitment() []byte

Commitment returns the NIP-01 id preimage [0,<pubkey>,<created_at>,<kind>,<tags>,<content>]; the event id is its SHA-256.

func (Event) ForLog added in v0.8.0

func (e Event) ForLog(max int) string

ForLog returns the event as JSON for a log line, cut to max bytes. encoding/json escapes control characters and U+2028/U+2029, so the exact characters an id disagreement turns on stay visible in the log.

type Filter

type Filter struct {
	IDs     []string            `json:"ids,omitempty"`
	Authors []string            `json:"authors,omitempty"`
	Kinds   []int               `json:"kinds,omitempty"`
	Tags    map[string][]string `json:"#,omitempty"` // Fixed: should be "#" for tag filters
	Since   *time.Time          `json:"since,omitempty"`
	Until   *time.Time          `json:"until,omitempty"`
	Limit   *int                `json:"limit,omitempty"`
	Search  string              `json:"search,omitempty"` // NIP-50: fulltext search query
}

Filter represents the criteria used to query events

func ParseFilter added in v0.8.0

func ParseFilter(raw map[string]interface{}) (f Filter, matchable bool, err error)

ParseFilter builds a Filter from one decoded NIP-01 filter object. It is strict on purpose: a malformed field is an error, never silently skipped, because skipping a constraint WIDENS the filter — a typo'd {"kinds":["1"]} used to lose its kinds and match every event in the database.

matchable=false with a nil error means the filter is well-formed but can never match anything (an empty ids/authors/kinds/#tag list, same as nostr-tools and go-nostr); callers drop it instead of querying.

ids and authors accept 1–64 hex chars. Anything shorter than 64 is matched as a prefix — nonstandard since NIP-01 moved to exact ids, but deliberately kept: tools like glyphbyte.dev look events up by their leading bytes. See HasPrefixMatch.

func (Filter) HasPrefixMatch added in v0.8.0

func (f Filter) HasPrefixMatch() bool

HasPrefixMatch reports whether any ids/authors entry is shorter than a full 64-char id, i.e. will be matched as a prefix rather than exactly.

func (Filter) MatchesEvent added in v0.5.0

func (f Filter) MatchesEvent(evt Event) bool

MatchesEvent returns true if the event satisfies all filter criteria per NIP-01. An empty/zero field means "match all" for that field.

func (Filter) ToSubscriptionFilter added in v0.4.1

func (f Filter) ToSubscriptionFilter() map[string]interface{}

ToSubscriptionFilter converts Filter to a relay-compatible format

type Subscription

type Subscription struct {
	ID      string
	Filters []Filter
}

Jump to

Keyboard shortcuts

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