Compare commits

..

4 Commits

Author SHA1 Message Date
fiatjaf
22e895c7c2 use exponential backoff for reconnections. 2021-12-12 11:39:56 -03:00
fiatjaf
02cacd4446 return sub object from .sub() and other methods. 2021-12-12 06:47:52 -03:00
fiatjaf
a99188e4cf remove log line for events with invalid signature. 2021-12-12 05:54:48 -03:00
fiatjaf
93b22e48a6 add nip06. 2021-12-11 19:46:51 -03:00
4 changed files with 12 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
import createHmac from 'create-hmac'
import randomBytes from 'randombytes'
import * as bip39 from 'bip39'
export function privateKeyFromSeed(seed) {

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "0.7.0",
"version": "0.8.1",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",

View File

@@ -29,19 +29,23 @@ export function relayPool(globalPrivateKey) {
const activeFilters = filter
activeSubscriptions[id] = {
sub: ({cb = activeCallback, filter = activeFilters}) =>
sub: ({cb = activeCallback, filter = activeFilters}) => {
Object.entries(subControllers).map(([relayURL, sub]) => [
relayURL,
sub.sub({cb, filter}, id)
]),
])
return activeSubscriptions[id]
},
addRelay: relay => {
subControllers[relay.url] = relay.sub({cb, filter})
return activeSubscriptions[id]
},
removeRelay: relayURL => {
if (relayURL in subControllers) {
subControllers[relayURL].unsub()
if (Object.keys(subControllers).length === 0) unsub()
}
return activeSubscriptions[id]
},
unsub: () => {
Object.values(subControllers).forEach(sub => sub.unsub())

View File

@@ -46,7 +46,10 @@ export function relayConnect(url, onNotice) {
ws.onclose = () => {
resetOpenState()
attemptNumber++
nextAttemptSeconds += attemptNumber
nextAttemptSeconds += attemptNumber ** 3
if (nextAttemptSeconds > 14400) {
nextAttemptSeconds = 14400 // 4 hours
}
console.log(
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
)
@@ -84,8 +87,6 @@ export function relayConnect(url, onNotice) {
if (channels[channel]) {
channels[channel](event)
}
} else {
console.warn('got event with invalid signature from ' + url, event)
}
return
}