-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathrelay.ts
More file actions
366 lines (332 loc) · 9.62 KB
/
relay.ts
File metadata and controls
366 lines (332 loc) · 9.62 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* global WebSocket */
import 'websocket-polyfill'
import { throttle } from 'lodash'
import {Event, verifySignature, validateEvent} from './event'
import {Filter, matchFilters} from './filter'
export type Relay = {
url: string
filters: Filter[]
status: number
connect: () => void
close: () => void
sub: (filters: Filter[], opts: SubscriptionOptions) => Sub
publish: (event: Event) => Pub
on: (type: 'connect' | 'disconnect' | 'notice', cb: any) => void
off: (type: 'connect' | 'disconnect' | 'notice', cb: any) => void
}
export type Pub = {
on: (type: 'ok' | 'seen' | 'failed', cb: any) => void
off: (type: 'ok' | 'seen' | 'failed', cb: any) => void
}
export type Sub = {
sub: (filters: Filter[], opts: SubscriptionOptions) => Sub
unsub: () => void
on: (type: 'event' | 'eose', cb: any) => void
off: (type: 'event' | 'eose', cb: any) => void
}
type SubscriptionOptions = {
skipVerification?: boolean
id?: string
}
export function relayInit(url: string, knownEvents?: Map<string, Event>): Relay {
var ws: WebSocket
var resolveOpen: () => void
var resolveClose: () => void
var untilOpen: Promise<void>
var wasClosed: boolean
var closed: boolean
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
var listeners: {
connect: Array<() => void>
disconnect: Array<() => void>
error: Array<() => void>
notice: Array<(msg: string) => void>
} = {
connect: [],
disconnect: [],
error: [],
notice: []
}
var subListeners: {
[subid: string]: {
event: Array<(event: Event) => void>
eose: Array<() => void>
}
} = {}
var pubListeners: {
[eventid: string]: {
ok: Array<() => void>
seen: Array<() => void>
failed: Array<(reason: string) => void>
}
} = {}
let attemptNumber = 1
let nextAttemptSeconds = 1
let isConnected = false
const split = url.split('?');
let filters = [];
if (split.length > 1) {
filters = split[1].split('&')
.map((filter) => {
if(filter.split('=')[0] == 'z') console.log('FOUND ONE')
let rVal: Filter[] = { ['#' + filter.split('=')[0]]: [filter.split('=')[1]] }
return rVal;
})
}
function resetOpenState() {
untilOpen = new Promise(resolve => {
resolveOpen = resolve
})
}
function connectRelay() {
ws = new WebSocket(url.split('?')[0])
ws.onopen = () => {
listeners.connect.forEach(cb => cb())
resolveOpen()
isConnected = true
// restablish old subscriptions
if (wasClosed) {
wasClosed = false
for (let id in openSubs) {
let {filters} = openSubs[id]
sub(filters, openSubs[id])
}
}
}
ws.onerror = () => {
isConnected = false
listeners.error.forEach(cb => cb())
}
ws.onclose = async () => {
isConnected = false
listeners.disconnect.forEach(cb => cb())
if (closed) {
// we've closed this because we wanted, so end everything
resolveClose()
return
}
// otherwise keep trying to reconnect
resetOpenState()
attemptNumber++
nextAttemptSeconds += attemptNumber ** 3
if (nextAttemptSeconds > 14400) {
nextAttemptSeconds = 14400 // 4 hours
}
console.log(
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
)
setTimeout(async () => {
try {
connectRelay()
} catch (err) {}
}, nextAttemptSeconds * 1000)
wasClosed = true
}
const log = throttle((msg: string) => {
console.log(msg)
}, 3000);
var idRegex = /"id":"([a-fA-F0-9]+)"/;
var queue: any[] = []
let handleNextInterval: any
const handleNext = () => {
if (queue.length === 0) {
clearInterval(handleNextInterval)
handleNextInterval = null
return
}
log(`relay ${url} msg processing queue length: ${queue.length}`)
var data = queue.shift()
if (data) {
const match = idRegex.exec(data)
if (match) {
const id = match[1];
if (knownEvents?.has(id)) {
// we already know about this event, so don't process it
//console.log('received a message that we already have. leaving this here just to show that nostr protocol could be improved.');
return
}
}
}
try {
// TODO try regex parsing msg id and seeing if we have it, before parse whole json
data = JSON.parse(data)
} catch (err) {}
if (data.length >= 1) {
switch (data[0]) {
case 'EVENT':
if (data.length !== 3) return // ignore empty or malformed EVENT
let id = data[1]
let event = data[2]
if (
validateEvent(event) &&
openSubs[id] &&
(openSubs[id].skipVerification || verifySignature(event)) &&
matchFilters(openSubs[id].filters, event)
) {
openSubs[id]
subListeners[id]?.event.forEach(cb => cb(event))
}
return
case 'EOSE': {
if (data.length !== 2) return // ignore empty or malformed EOSE
let id = data[1]
subListeners[id]?.eose.forEach(cb => cb())
return
}
case 'OK': {
if (data.length < 3) return // ignore empty or malformed OK
let id: string = data[1]
let ok: boolean = data[2]
let reason: string = data[3] || ''
if (ok) pubListeners[id]?.ok.forEach(cb => cb())
else pubListeners[id]?.failed.forEach(cb => cb(reason))
return
}
case 'NOTICE':
if (data.length !== 2) return // ignore empty or malformed NOTICE
let notice = data[1]
listeners.notice.forEach(cb => cb(notice))
return
}
}
};
ws.onmessage = async e => {
queue.push(e.data)
if (!handleNextInterval) {
handleNextInterval = setInterval(handleNext, 0)
}
}
}
resetOpenState()
async function connect(): Promise<void> {
if (ws?.readyState && ws.readyState === 1) return // ws already open
try {
connectRelay()
} catch (err) {}
}
async function trySend(params: [string, ...any]) {
let msg = JSON.stringify(params)
await untilOpen
try {
ws.send(msg)
} catch (err) {
console.log(err)
}
}
const sub = (
filters: Filter[],
{
skipVerification = false,
id = Math.random().toString().slice(2)
}: SubscriptionOptions = {},
relayFilters?: Filter[],
): Sub => {
let subid = id
if(relayFilters) {
filters = filters.map((item, index) => Object.assign({}, item, relayFilters[index]));
}
openSubs[subid] = {
id: subid,
filters,
skipVerification
}
trySend(['REQ', subid, ...filters])
return {
sub: (newFilters, newOpts = {}) =>
sub(newFilters || filters, {
skipVerification: newOpts.skipVerification || skipVerification,
id: subid
}),
unsub: () => {
delete openSubs[subid]
delete subListeners[subid]
trySend(['CLOSE', subid])
},
on: (type: 'event' | 'eose', cb: any): void => {
subListeners[subid] = subListeners[subid] || {
event: [],
eose: []
}
subListeners[subid][type].push(cb)
},
off: (type: 'event' | 'eose', cb: any): void => {
let idx = subListeners[subid][type].indexOf(cb)
if (idx >= 0) subListeners[subid][type].splice(idx, 1)
}
}
}
return {
url,
filters,
sub,
on: (type: 'connect' | 'disconnect' | 'notice', cb: any): void => {
listeners[type].push(cb)
if (type === 'connect' && isConnected) {
cb()
}
},
off: (type: 'connect' | 'disconnect' | 'notice', cb: any): void => {
let index = listeners[type].indexOf(cb)
if (index !== -1) listeners[type].splice(index, 1)
},
publish(event: Event): Pub {
if (!event.id) throw new Error(`event ${event} has no id`)
let id = event.id
var sent = false
var mustMonitor = false
trySend(['EVENT', event])
.then(() => {
sent = true
if (mustMonitor) {
startMonitoring()
mustMonitor = false
}
})
.catch(() => {})
const startMonitoring = () => {
let monitor = sub([{ids: [id]}], {
id: `monitor-${id.slice(0, 5)}`
})
let willUnsub = setTimeout(() => {
pubListeners[id].failed.forEach(cb =>
cb('event not seen after 5 seconds')
)
monitor.unsub()
}, 5000)
monitor.on('event', () => {
clearTimeout(willUnsub)
pubListeners[id].seen.forEach(cb => cb())
})
}
return {
on: (type: 'ok' | 'seen' | 'failed', cb: any) => {
pubListeners[id] = pubListeners[id] || {
ok: [],
seen: [],
failed: []
}
pubListeners[id][type].push(cb)
if (type === 'seen') {
if (sent) startMonitoring()
else mustMonitor = true
}
},
off: (type: 'ok' | 'seen' | 'failed', cb: any) => {
let idx = pubListeners[id][type].indexOf(cb)
if (idx >= 0) pubListeners[id][type].splice(idx, 1)
}
}
},
connect,
close(): Promise<void> {
closed = true // prevent ws from trying to reconnect
ws.close()
return new Promise(resolve => {
resolveClose = resolve
})
},
get status() {
return ws.readyState
}
}
}