nostr-tools 0.7.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/pool.js +6 -2
- package/relay.js +14 -8
package/package.json
CHANGED
package/pool.js
CHANGED
|
@@ -29,19 +29,23 @@ export function relayPool(globalPrivateKey) {
|
|
|
29
29
|
const activeFilters = filter
|
|
30
30
|
|
|
31
31
|
activeSubscriptions[id] = {
|
|
32
|
-
sub: ({cb = activeCallback, filter = activeFilters}) =>
|
|
32
|
+
sub: ({cb = activeCallback, filter = activeFilters}) => {
|
|
33
33
|
Object.entries(subControllers).map(([relayURL, sub]) => [
|
|
34
34
|
relayURL,
|
|
35
35
|
sub.sub({cb, filter}, id)
|
|
36
|
-
])
|
|
36
|
+
])
|
|
37
|
+
return activeSubscriptions[id]
|
|
38
|
+
},
|
|
37
39
|
addRelay: relay => {
|
|
38
40
|
subControllers[relay.url] = relay.sub({cb, filter})
|
|
41
|
+
return activeSubscriptions[id]
|
|
39
42
|
},
|
|
40
43
|
removeRelay: relayURL => {
|
|
41
44
|
if (relayURL in subControllers) {
|
|
42
45
|
subControllers[relayURL].unsub()
|
|
43
46
|
if (Object.keys(subControllers).length === 0) unsub()
|
|
44
47
|
}
|
|
48
|
+
return activeSubscriptions[id]
|
|
45
49
|
},
|
|
46
50
|
unsub: () => {
|
|
47
51
|
Object.values(subControllers).forEach(sub => sub.unsub())
|
package/relay.js
CHANGED
|
@@ -13,7 +13,7 @@ export function normalizeRelayURL(url) {
|
|
|
13
13
|
export function relayConnect(url, onNotice) {
|
|
14
14
|
url = normalizeRelayURL(url)
|
|
15
15
|
|
|
16
|
-
var ws, resolveOpen, untilOpen
|
|
16
|
+
var ws, resolveOpen, untilOpen, wasClosed
|
|
17
17
|
var openSubs = {}
|
|
18
18
|
let attemptNumber = 1
|
|
19
19
|
let nextAttemptSeconds = 1
|
|
@@ -34,10 +34,13 @@ export function relayConnect(url, onNotice) {
|
|
|
34
34
|
resolveOpen()
|
|
35
35
|
|
|
36
36
|
// restablish old subscriptions
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let
|
|
40
|
-
|
|
37
|
+
if (wasClosed) {
|
|
38
|
+
wasClosed = false
|
|
39
|
+
for (let channel in openSubs) {
|
|
40
|
+
let filters = openSubs[channel]
|
|
41
|
+
let cb = channels[channel]
|
|
42
|
+
sub({cb, filter: filters}, channel)
|
|
43
|
+
}
|
|
41
44
|
}
|
|
42
45
|
}
|
|
43
46
|
ws.onerror = () => {
|
|
@@ -46,7 +49,10 @@ export function relayConnect(url, onNotice) {
|
|
|
46
49
|
ws.onclose = () => {
|
|
47
50
|
resetOpenState()
|
|
48
51
|
attemptNumber++
|
|
49
|
-
nextAttemptSeconds += attemptNumber
|
|
52
|
+
nextAttemptSeconds += attemptNumber ** 3
|
|
53
|
+
if (nextAttemptSeconds > 14400) {
|
|
54
|
+
nextAttemptSeconds = 14400 // 4 hours
|
|
55
|
+
}
|
|
50
56
|
console.log(
|
|
51
57
|
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
|
|
52
58
|
)
|
|
@@ -55,6 +61,8 @@ export function relayConnect(url, onNotice) {
|
|
|
55
61
|
connect()
|
|
56
62
|
} catch (err) {}
|
|
57
63
|
}, nextAttemptSeconds * 1000)
|
|
64
|
+
|
|
65
|
+
wasClosed = true
|
|
58
66
|
}
|
|
59
67
|
|
|
60
68
|
ws.onmessage = async e => {
|
|
@@ -84,8 +92,6 @@ export function relayConnect(url, onNotice) {
|
|
|
84
92
|
if (channels[channel]) {
|
|
85
93
|
channels[channel](event)
|
|
86
94
|
}
|
|
87
|
-
} else {
|
|
88
|
-
console.warn('got event with invalid signature from ' + url, event)
|
|
89
95
|
}
|
|
90
96
|
return
|
|
91
97
|
}
|