1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
package nostr
import (
"context"
"crypto/rand"
"fmt"
"sync"
"code.northwest.io/nostr/internal/websocket"
)
// Relay represents a connection to a Nostr relay.
type Relay struct {
URL string
conn *websocket.Conn
mu sync.Mutex
subscriptions map[string]*Subscription
subscriptionsMu sync.RWMutex
okChannels map[string]chan *OKEnvelope
okChannelsMu sync.Mutex
}
// Connect establishes a WebSocket connection to the relay.
func Connect(ctx context.Context, url string) (*Relay, error) {
conn, err := websocket.Dial(ctx, url)
if err != nil {
return nil, fmt.Errorf("failed to connect to relay: %w", err)
}
r := &Relay{
URL: url,
conn: conn,
subscriptions: make(map[string]*Subscription),
okChannels: make(map[string]chan *OKEnvelope),
}
go r.Listen(ctx)
return r, nil
}
// Close closes the WebSocket connection.
func (r *Relay) Close() error {
r.mu.Lock()
defer r.mu.Unlock()
if r.conn == nil {
return nil
}
err := r.conn.Close(websocket.StatusNormalClosure, "")
r.conn = nil
return err
}
// Send sends an envelope to the relay.
func (r *Relay) Send(ctx context.Context, env Envelope) error {
data, err := env.MarshalJSON()
if err != nil {
return fmt.Errorf("failed to marshal envelope: %w", err)
}
r.mu.Lock()
defer r.mu.Unlock()
if r.conn == nil {
return fmt.Errorf("connection closed")
}
return r.conn.Write(ctx, websocket.MessageText, data)
}
// Receive reads the next envelope from the relay.
func (r *Relay) Receive(ctx context.Context) (Envelope, error) {
r.mu.Lock()
conn := r.conn
r.mu.Unlock()
if conn == nil {
return nil, fmt.Errorf("connection closed")
}
_, data, err := conn.Read(ctx)
if err != nil {
return nil, fmt.Errorf("failed to read message: %w", err)
}
return ParseEnvelope(data)
}
// Publish sends an event to the relay and waits for the OK response.
func (r *Relay) Publish(ctx context.Context, event *Event) error {
ch := make(chan *OKEnvelope, 1)
r.okChannelsMu.Lock()
r.okChannels[event.ID] = ch
r.okChannelsMu.Unlock()
defer func() {
r.okChannelsMu.Lock()
delete(r.okChannels, event.ID)
r.okChannelsMu.Unlock()
}()
env := EventEnvelope{Event: event}
if err := r.Send(ctx, env); err != nil {
return fmt.Errorf("failed to send event: %w", err)
}
select {
case ok := <-ch:
if !ok.OK {
return fmt.Errorf("event rejected: %s", ok.Message)
}
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func genID() string {
buf := make([]byte, 5)
rand.Read(buf)
return fmt.Sprintf("%x", buf)
}
// subscribe is the internal implementation for Subscribe and Fetch.
func (r *Relay) subscribe(ctx context.Context, closeOnEOSE bool, filters ...Filter) *Subscription {
id := genID()
sub := &Subscription{
ID: id,
relay: r,
Filters: filters,
Events: make(chan *Event, 100),
closeOnEOSE: closeOnEOSE,
}
r.subscriptionsMu.Lock()
r.subscriptions[id] = sub
r.subscriptionsMu.Unlock()
go func() {
<-ctx.Done()
sub.stop(ctx.Err())
r.subscriptionsMu.Lock()
delete(r.subscriptions, id)
r.subscriptionsMu.Unlock()
}()
env := ReqEnvelope{
SubscriptionID: id,
Filters: filters,
}
if err := r.Send(ctx, env); err != nil {
r.subscriptionsMu.Lock()
delete(r.subscriptions, id)
r.subscriptionsMu.Unlock()
sub.stop(fmt.Errorf("failed to send subscription request: %w", err))
}
return sub
}
// Subscribe creates a subscription with the given filters.
// Events are received on the Events channel until the context is cancelled.
// After EOSE (end of stored events), the subscription continues to receive
// real-time events per the Nostr protocol.
func (r *Relay) Subscribe(ctx context.Context, filters ...Filter) *Subscription {
return r.subscribe(ctx, false, filters...)
}
// Fetch creates a subscription that closes automatically when EOSE is received.
// Use this for one-shot queries where you only want stored events.
func (r *Relay) Fetch(ctx context.Context, filters ...Filter) *Subscription {
return r.subscribe(ctx, true, filters...)
}
// dispatchEnvelope routes incoming messages to the appropriate subscription.
func (r *Relay) dispatchEnvelope(env Envelope) {
switch e := env.(type) {
case *EventEnvelope:
r.subscriptionsMu.RLock()
sub, ok := r.subscriptions[e.SubscriptionID]
r.subscriptionsMu.RUnlock()
if ok {
sub.send(e.Event)
}
case *EOSEEnvelope:
r.subscriptionsMu.RLock()
sub, ok := r.subscriptions[e.SubscriptionID]
r.subscriptionsMu.RUnlock()
if ok && sub.closeOnEOSE {
r.subscriptionsMu.Lock()
delete(r.subscriptions, e.SubscriptionID)
r.subscriptionsMu.Unlock()
sub.stop(nil)
}
case *ClosedEnvelope:
r.subscriptionsMu.Lock()
sub, ok := r.subscriptions[e.SubscriptionID]
if ok {
delete(r.subscriptions, e.SubscriptionID)
}
r.subscriptionsMu.Unlock()
if ok {
sub.stop(fmt.Errorf("subscription closed by relay: %s", e.Message))
}
case *OKEnvelope:
r.okChannelsMu.Lock()
ch, ok := r.okChannels[e.EventID]
r.okChannelsMu.Unlock()
if ok {
select {
case ch <- e:
default:
}
}
}
}
// Listen reads messages from the relay and dispatches them to subscriptions.
func (r *Relay) Listen(ctx context.Context) error {
defer func() {
r.subscriptionsMu.Lock()
subs := make([]*Subscription, 0, len(r.subscriptions))
for id, sub := range r.subscriptions {
subs = append(subs, sub)
delete(r.subscriptions, id)
}
r.subscriptionsMu.Unlock()
for _, sub := range subs {
sub.stop(fmt.Errorf("connection closed"))
}
}()
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
env, err := r.Receive(ctx)
if err != nil {
return err
}
r.dispatchEnvelope(env)
}
}
// Subscription represents an active subscription to a relay.
type Subscription struct {
ID string
relay *Relay
Filters []Filter
Events chan *Event
Err error
closeOnEOSE bool
mu sync.Mutex
done bool
}
// send delivers an event to the subscription's Events channel.
func (s *Subscription) send(ev *Event) {
s.mu.Lock()
defer s.mu.Unlock()
if s.done {
return
}
select {
case s.Events <- ev:
default:
}
}
// stop closes the subscription's Events channel and sets the error.
// It is idempotent — only the first call has any effect.
func (s *Subscription) stop(err error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.done {
return
}
s.done = true
s.Err = err
close(s.Events)
}
// Close unsubscribes from the relay.
func (s *Subscription) Close(ctx context.Context) error {
s.stop(nil)
s.relay.subscriptionsMu.Lock()
delete(s.relay.subscriptions, s.ID)
s.relay.subscriptionsMu.Unlock()
env := CloseEnvelope{SubscriptionID: s.ID}
return s.relay.Send(ctx, env)
}
|