ws

package
v1.2.31 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: Unlicense Imports: 47 Imported by: 0

Documentation

Index

Constants

View Source
const ChallengeHRP = "nchal"
View Source
const ChallengeLength = 16
View Source
const MAX_LOCKS = 50

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	URL             st
	RequestHeader   http.Header // e.g. for origin header
	Connection      *Connection
	Subscriptions   *xsync.MapOf[st, *Subscription]
	ConnectionError er

	AssumeValid bo // this will skip verifying signatures for events received from this relay
	// contains filtered or unexported fields
}

func NewRelay

func NewRelay(c cx, url st, opts ...RelayOption) *Client

NewRelay returns a new relay. The relay connection will be closed when the context is canceled.

func RelayConnect

func RelayConnect(ctx cx, url st, opts ...RelayOption) (*Client, er)

RelayConnect returns a relay object connected to url. Once successfully connected, cancelling ctx has no effect. To close the connection, call r.Close().

func (*Client) Auth

func (r *Client) Auth(c cx, sign signer.I) er

Auth sends an "AUTH" command client->relay as in NIP-42 and waits for an OK response.

func (*Client) Close

func (r *Client) Close() er

func (*Client) Connect

func (r *Client) Connect(c cx) er

Connect tries to establish a websocket connection to r.URL. If the context expires before the connection is complete, an error is returned. Once successfully connected, context expiration has no effect: call r.Close to close the connection.

The underlying relay connection will use a background context. If you want to pass a custom context to the underlying relay connection, use NewRelay() and then Client.Connect().

func (*Client) ConnectWithTLS

func (r *Client) ConnectWithTLS(ctx cx, tlsConfig *tls.Config) er

ConnectWithTLS tries to establish a secured websocket connection to r.URL using customized tls.Config (CA's, etc).

func (*Client) Context

func (r *Client) Context() cx

Context retrieves the context that is associated with this relay connection.

func (*Client) Count

func (r *Client) Count(c cx, ff *filters.T, opts ...SubscriptionOption) (no, er)

func (*Client) IsConnected

func (r *Client) IsConnected() bo

IsConnected returns true if the connection to this relay seems to be active.

func (*Client) PrepareSubscription

func (r *Client) PrepareSubscription(c cx, ff *filters.T,
	opts ...SubscriptionOption) *Subscription

PrepareSubscription creates a subscription, but doesn't fire it.

Remember to cancel subscriptions, either by calling `.Unsub()` on them or ensuring their `context.Context` will be canceled at some point. Failure to do that will result in a huge number of halted goroutines being created.

func (*Client) Publish

func (r *Client) Publish(c cx, ev *event.T) er

Publish sends an "EVENT" command to the relay r as in NIP-01 and waits for an OK response.

func (*Client) QuerySync

func (r *Client) QuerySync(ctx cx, f *filter.T,
	opts ...SubscriptionOption) ([]*event.T, er)

func (*Client) String

func (r *Client) String() st

String just returns the relay URL.

func (*Client) Subscribe

func (r *Client) Subscribe(c cx, ff *filters.T,
	opts ...SubscriptionOption) (*Subscription, er)

Subscribe sends a "REQ" command to the relay r as in NIP-01. Events are returned through the channel sub.Events. The subscription is closed when context ctx is cancelled ("CLOSE" in NIP-01).

Remember to cancel subscriptions, either by calling `.Unsub()` on them or ensuring their `context.Context` will be canceled at some point. Failure to do that will result in a huge number of halted goroutines being created.

func (*Client) Write

func (r *Client) Write(msg by) <-chan er

Write queues a message to be sent to the relay.

type Connection

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

func NewConnection

func NewConnection(c cx, url string, requestHeader http.Header,
	tlsConfig *tls.Config) (*Connection, er)

func (*Connection) Close

func (cn *Connection) Close() er

func (*Connection) ReadMessage

func (cn *Connection) ReadMessage(c cx, buf io.Writer) er

func (*Connection) WriteMessage

func (cn *Connection) WriteMessage(c cx, data by) er

type DirectedFilters

type DirectedFilters struct {
	Filters *filters.T
	Client  st
}

type EventMessage

type EventMessage struct {
	Event event.T
	Relay st
}

type IncomingEvent

type IncomingEvent struct {
	Event  *event.T
	Client *Client
}

func (IncomingEvent) String

func (ie IncomingEvent) String() st

type MessageType

type MessageType no

type PoolOption

type PoolOption interface {
	ApplyPoolOption(*SimplePool)
}

type RelayOption

type RelayOption interface {
	ApplyRelayOption(*Client)
}

RelayOption is the type of the argument passed for that.

type Serv

type Serv struct {
	Ctx    cx
	Cancel context.F
	Conn   *websocket.Conn

	Request *http.Request // original request

	Pending atomic.Value // for DM CLI authentication

	Authed qu.C
	// contains filtered or unexported fields
}

Serv is a wrapper around a fasthttp/websocket with mutex locking and NIP-42 IsAuthed support for handling inbound connections from clients.

func New

func New(c cx, conn *websocket.Conn, r *http.Request, maxMsg no) (ws *Serv)

func (*Serv) AuthPub

func (ws *Serv) AuthPub() (a by)

AuthPub returns the current authed Pubkey.

func (*Serv) Challenge

func (ws *Serv) Challenge() (challenge by)

Challenge returns the current challenge on a websocket.

func (*Serv) Close

func (ws *Serv) Close() (err er)

Close signals the other side to close the connection.

func (*Serv) HasAuth

func (ws *Serv) HasAuth() bo

func (*Serv) Ping

func (ws *Serv) Ping() (err er)

Ping sends a ping to see if the other side is still responsive.

func (*Serv) Pong

func (ws *Serv) Pong() (err er)

Pong sends a Pong message, should be the response to receiving Ping.

func (*Serv) Remote

func (ws *Serv) Remote() (remote st)

Remote returns the current real remote.

func (*Serv) Write

func (ws *Serv) Write(b by) (n no, err er)

Write implements the standard io.Writer interface.

func (*Serv) WriteTextMessage

func (ws *Serv) WriteTextMessage(b by) (err er)

WriteTextMessage writes a text (binary?) message

type SimplePool

type SimplePool struct {
	Relays  *xsync.MapOf[st, *Client]
	Context cx

	// custom things not often used
	SignatureChecker func(*event.T) bo
	// contains filtered or unexported fields
}

func NewSimplePool

func NewSimplePool(c cx, opts ...PoolOption) *SimplePool

func (*SimplePool) BatchedSubMany

func (pool *SimplePool) BatchedSubMany(c cx, dfs []DirectedFilters) chan IncomingEvent

BatchedSubMany fires subscriptions only to specific relays, but batches them when they are the same.

func (*SimplePool) BatchedSubManyEose

func (pool *SimplePool) BatchedSubManyEose(c cx, dfs []DirectedFilters) chan IncomingEvent

BatchedSubManyEose is like BatchedSubMany, but ends upon receiving EOSE from all relays.

func (*SimplePool) EnsureRelay

func (pool *SimplePool) EnsureRelay(url st) (*Client, er)

func (*SimplePool) QuerySingle

func (pool *SimplePool) QuerySingle(c cx, urls []st, f *filter.T) *IncomingEvent

QuerySingle returns the first event returned by the first relay, cancels everything else.

func (*SimplePool) SubMany

func (pool *SimplePool) SubMany(c cx, urls []st, ff *filters.T) chan IncomingEvent

SubMany opens a subscription with the given filters to multiple relays the subscriptions only end when the context is canceled

func (*SimplePool) SubManyEose

func (pool *SimplePool) SubManyEose(c cx, urls []st, ff *filters.T) chan IncomingEvent

SubManyEose is like SubMany, but it stops subscriptions and closes the channel when gets a EOSE

func (*SimplePool) SubManyEoseNonUnique

func (pool *SimplePool) SubManyEoseNonUnique(c cx, urls []st,
	ff *filters.T) chan IncomingEvent

SubManyEoseNonUnique is like SubManyEose, but returns duplicate events if they come from different relays

func (*SimplePool) SubManyNonUnique

func (pool *SimplePool) SubManyNonUnique(c cx, urls []st, ff *filters.T) chan IncomingEvent

SubManyNonUnique is like SubMany, but returns duplicate events if they come from different relays

type Status

type Status no

type Subscription

type Subscription struct {
	Relay   *Client
	Filters *filters.T

	// The Events channel emits all EVENTs that come in a Subscription will be closed when the
	// subscription ends
	Events event.C

	// The EndOfStoredEvents channel is closed when an EOSE comes for that subscription
	EndOfStoredEvents chan struct{}

	// The ClosedReason channel emits the reason when a CLOSED message is received
	ClosedReason chan st

	// Context will be .Done() when the subscription ends
	Context cx
	// contains filtered or unexported fields
}

func (*Subscription) Close

func (sub *Subscription) Close()

Close just sends a CLOSE message. You probably want Unsub() instead.

func (*Subscription) Fire

func (sub *Subscription) Fire() (err er)

Fire sends the "REQ" command to the realy.

func (*Subscription) GetID

func (sub *Subscription) GetID() (id *subscription.Id)

GetID return the Nostr subscription ID as given to the Client it is a concatenation of the label and a serial number.

func (*Subscription) Sub

func (sub *Subscription) Sub(_ cx, ff *filters.T)

Sub sets sub.Filters and then calls sub.Fire(ctx). The subscription will be closed if the context expires.

func (*Subscription) Unsub

func (sub *Subscription) Unsub()

Unsub closes the subscription, sending "CLOSE" to realy as in NIP-01. Unsub() also closes the channel sub.Events and makes a new one.

type SubscriptionOption

type SubscriptionOption interface {
	IsSubscriptionOption()
}

SubscriptionOption is the type of the argument passed for that. Some examples are WithLabel.

type WithAuthHandler

type WithAuthHandler func() signer.I

WithAuthHandler must be a function that signs the auth event when called. it will be called whenever any relay in the pool returns a `CLOSED` message with the "auth-required:" prefix, only once for each relay

func (WithAuthHandler) ApplyPoolOption

func (h WithAuthHandler) ApplyPoolOption(pool *SimplePool)

type WithEventMiddleware

type WithEventMiddleware func(IncomingEvent)

WithEventMiddleware is a function that will be called with all events received. more than one can be passed at a time.

func (WithEventMiddleware) ApplyPoolOption

func (h WithEventMiddleware) ApplyPoolOption(pool *SimplePool)

type WithLabel

type WithLabel st

WithLabel puts a label on the subscription (it is prepended to the automatic id) that is sent to relays.

func (WithLabel) IsSubscriptionOption

func (_ WithLabel) IsSubscriptionOption()

type WithNoticeHandler

type WithNoticeHandler func(notice by)

WithNoticeHandler just takes notices and is expected to do something with them. when not given, defaults to logging the notices.

func (WithNoticeHandler) ApplyRelayOption

func (nh WithNoticeHandler) ApplyRelayOption(r *Client)

type WithSignatureChecker

type WithSignatureChecker func(*event.T) bo

WithSignatureChecker must be a function that checks the signature of an event and returns true or false.

func (WithSignatureChecker) ApplyRelayOption

func (sc WithSignatureChecker) ApplyRelayOption(r *Client)

Jump to

Keyboard shortcuts

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