19 Commits

Author SHA1 Message Date
fiatjaf
defc349e57 use coder/websocket for everything, get rid of gobwas.
supposedly it is faster, and anyway it's better to use it since we're already using it for wasm/js.

(previously named nhooyr/websocket).
2025-01-03 01:15:17 -03:00
reis
63919cf685
support wasm (#163) 2025-01-02 09:42:04 -03:00
fiatjaf
72d9aac9b1 replace all ocurrences of encoding/json with json-iterator so we get rid of HTML escaping and hopefully get faster too. 2024-12-03 00:49:27 -03:00
K
c91e7b9765
using testify instead of testing.T methods. (#143) 2024-09-09 07:20:56 -03:00
fiatjaf
fabd5160a8
fix sign in relay test just recently updated. 2023-12-09 13:26:50 -03:00
fiatjaf
64eb395dc1
fix tests that relied on the defunct PublishStatus enum. 2023-12-07 21:39:43 -03:00
Isaque Veras
0f66883dc7 feat(kind): using constants 2023-09-07 07:05:01 -03:00
fiatjaf
9cafea7e2a
ensure relay context and subscriptions are closed when we lose connectivity. 2023-05-30 17:44:25 -03:00
Patrick Bennett
ebae5d41e6
Add most NIP-11 extension structures to the RelayInformationDocument struct. (#80)
* Fix race condition on status in Relay.Publish method and failure to send

A race-condition exists between setting of the (unprotected) status and the callback which sets the status upon receiving an OK.
The message is sent which can receive an OK in separate goroutine (setting status) prior to the status being set to 'sent.'
The OK can be received prior to the status being set.

This fix also sets the status to PublishStatusFailed if the WriteJSON call fails.

* Add some NIP-11 extension structures to the RelayInformationDocument struct.

Added additional NIP-11 fields for relays that want to provide additional details based on NIP-11 extensions.
The retention structure has been left out as it doesn't have a clean schema for kinds (array of kinds, or pairs of kinds?)
Specified the fields w/ omitempty so marshaled will be same as original NIP-11 if nothing else is specified.
Nested structs defined as pointers so they are omitted if not specified.

* Fix TestPublishWriteFailed so that the socket is given a brief amount of time to close prior to publish being called.
The test relies on Publish always failing.
2023-04-26 08:06:05 -03:00
Patrick Bennett
b077a41f83 Fix race condition on status in Relay.Publish method and failure to send
A race-condition exists between setting of the (unprotected) status and the callback which sets the status upon receiving an OK.
The message is sent which can receive an OK in separate goroutine (setting status) prior to the status being set to 'sent.'
The OK can be received prior to the status being set.

This fix also sets the status to PublishStatusFailed if the WriteJSON call fails.
2023-04-21 07:48:08 -03:00
fiatjaf
c42059f4b4
tests run (but not pass) and fine-tuning (specially tag filters) on filter_easyjson.go 2023-04-16 16:16:16 -03:00
fiatjaf
32768b1a5b
improve debug logging, fix stringer interface, debuglog events sent, fix debuglogging affecting the actual values. 2023-04-11 11:02:35 -03:00
fiatjaf
a666994ae7
fix test that was broken when the context passed to RelayConnect() became the relay context for its full timeline.
30e0e1040a8ee667508a538ac896d24f6ceaa8cd
2023-04-06 16:22:24 -03:00
fiatjaf
3f66c60b5f
subscription labels: GetID() and SetLabel(). 2023-03-18 08:40:12 -03:00
Jasper Rädisch
74c646fe21 add RequestHeader option for websocket conn 2023-02-25 16:06:46 -03:00
fiatjaf
4a62a753e6
contexts everywhere. 2023-01-01 20:58:43 -03:00
alex
28663f21f0 relaypool: add a unique events subscription smoke test
was trying to reproduce the issue described in
https://github.com/nbd-wtf/go-nostr/issues/23

no success in reproducing that specific problem, but i believe the test
can still be useful to help in avoiding regression bugs in the future.
2022-12-26 16:03:00 -03:00
alex
435579dc75 publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:

    ["OK", event-id, false, "blocked"]

this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:

    func (r Relay) Publish(event Event) chan Status {
      // uses a **copy** of statusChans here:
      r.statusChans.Store(event.ID, statusChan)
      ...
    }

the bugfix is a one character change:

    func (r *Relay) Publish(event Event) chan Status

but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:

    // ok, which is a command status from earlier, is shadowed here:
    if statusChan, ok := r.statusChans.Load(eventId); ok {
      statusChan <- ...
    }

as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 14:44:40 -03:00
alex
c327f622f3 relay: introduce ConnectContext for better control over network latency
A websocket dial may hand for an unreasonably long time and a nostr client
has no control over this when trying to connect to a relay.

Go started introducing context in networking since 2014 -
see https://go.dev/blog/context - and by now many net functions have
XxxContext equivalent, such as DialContext.

Example usage of the change introduced by this commit:

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()
    r, err := nostr.RelayConnectContext(ctx, "ws://relay.example.org")

The code above makes RelayConnectContext last at most 3 sec, returning
an error if a connection cannot be established in the given time.
This helps whenever a tight control over connection latency is required,
such as distributed systems.

The change is backwards-compatible except the case where RelayPool.Add
sent an error over the returned channel without actually closing said
channel. I believe it was a bug.
2022-12-17 22:33:05 -03:00