ws

package
v1.0.15 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: CC0-1.0 Imports: 48 Imported by: 1

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 added in v0.1.5

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

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

func NewRelay

func NewRelay(c Ctx, url S, opts ...RelayOption) *Client

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

func RelayConnect

func RelayConnect(ctx Ctx, url string, opts ...RelayOption) (*Client, error)

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 added in v0.1.5

func (r *Client) Auth(c Ctx, signer crypto.Signer) error

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

func (*Client) Close added in v0.1.5

func (r *Client) Close() error

func (*Client) Connect added in v0.1.5

func (r *Client) Connect(c Ctx) error

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 added in v0.1.5

func (r *Client) ConnectWithTLS(ctx Ctx, tlsConfig *tls.Config) error

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

func (*Client) Context added in v0.1.5

func (r *Client) Context() Ctx

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

func (*Client) Count added in v0.1.5

func (r *Client) Count(c Ctx, ff *filters.T, opts ...SubscriptionOption) (int, error)

func (*Client) IsConnected added in v0.1.5

func (r *Client) IsConnected() bool

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

func (*Client) PrepareSubscription added in v0.1.5

func (r *Client) PrepareSubscription(c Ctx, 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 added in v0.1.5

func (r *Client) Publish(c Ctx, ev *event.T) E

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

func (*Client) QuerySync added in v0.1.5

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

func (*Client) String added in v0.1.5

func (r *Client) String() string

String just returns the relay URL.

func (*Client) Subscribe added in v0.1.5

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

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 added in v0.1.5

func (r *Client) Write(msg []byte) <-chan error

Write queues a message to be sent to the relay.

type Connection

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

func NewConnection

func NewConnection(ctx context.Context, url string, requestHeader http.Header,
	tlsConfig *tls.Config) (*Connection, error)

func (*Connection) Close

func (c *Connection) Close() error

func (*Connection) ReadMessage

func (c *Connection) ReadMessage(ctx context.Context, buf io.Writer) error

func (*Connection) WriteMessage

func (c *Connection) WriteMessage(ctx context.Context, data []byte) error

type DirectedFilters added in v0.1.5

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

type EventMessage

type EventMessage struct {
	Event event.T
	Relay string
}

type IncomingEvent added in v0.1.5

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

func (IncomingEvent) String added in v0.1.5

func (ie IncomingEvent) String() S

type MessageType

type MessageType int

type PoolOption added in v0.1.5

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    Ctx
	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 Ctx, conn *websocket.Conn, r *http.Request, maxMsg int) (ws *Serv)

func (*Serv) AuthPub

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

AuthPub returns the current authed Pubkey.

func (*Serv) Challenge

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

Challenge returns the current challenge on a websocket.

func (*Serv) Close

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

Close signals the other side to close the connection.

func (*Serv) HasAuth

func (ws *Serv) HasAuth() bool

func (*Serv) Ping

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

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

func (*Serv) Pong

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

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

func (*Serv) Remote

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

Remote returns the current real remote.

func (*Serv) Write

func (ws *Serv) Write(b []byte) (n int, err error)

Write implements the standard io.Writer interface.

func (*Serv) WriteTextMessage

func (ws *Serv) WriteTextMessage(b B) (err E)

WriteTextMessage writes a text (binary?) message

type SimplePool added in v0.1.5

type SimplePool struct {
	Relays  *xsync.MapOf[S, *Client]
	Context Ctx

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

func NewSimplePool added in v0.1.5

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

func (*SimplePool) BatchedSubMany added in v0.1.5

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

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

func (*SimplePool) BatchedSubManyEose added in v0.1.5

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

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

func (*SimplePool) EnsureRelay added in v0.1.5

func (pool *SimplePool) EnsureRelay(url S) (*Client, error)

func (*SimplePool) QuerySingle added in v0.1.5

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

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

func (*SimplePool) SubMany added in v0.1.5

func (pool *SimplePool) SubMany(c Ctx, urls []S, 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 added in v0.1.5

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

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

func (*SimplePool) SubManyEoseNonUnique added in v0.1.5

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

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

func (*SimplePool) SubManyNonUnique added in v0.1.5

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

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

type Status

type Status int

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 string

	// Context will be .Done() when the subscription ends
	Context Ctx
	// 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 E)

Fire sends the "REQ" command to the relay.

func (*Subscription) GetID

func (sub *Subscription) GetID() (id *subscriptionid.T)

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(_ Ctx, 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 relay 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 added in v0.1.5

type WithAuthHandler func() crypto.Signer

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 added in v0.1.5

func (h WithAuthHandler) ApplyPoolOption(pool *SimplePool)

type WithEventMiddleware added in v0.1.5

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 added in v0.1.5

func (h WithEventMiddleware) ApplyPoolOption(pool *SimplePool)

type WithLabel

type WithLabel string

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 B)

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) bool

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
⌂ 换网址