Compare commits

...

4 Commits

Author SHA1 Message Date
fiatjaf
9d345a8f01 configurable list and get timeout on relay. 2023-02-26 21:23:09 -03:00
fiatjaf
c362212778 make pool.publish() return a single Pub object. 2023-02-26 17:44:51 -03:00
fiatjaf
a8938a3a0f wait a second before failing to send on a not yet connected websocket. 2023-02-26 16:53:03 -03:00
fiatjaf
a21329da3f make timeouts configurable for pool. 2023-02-26 16:50:49 -03:00
3 changed files with 42 additions and 13 deletions

View File

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

34
pool.ts
View File

@@ -8,8 +8,13 @@ export class SimplePool {
private _conn: {[url: string]: Relay}
private _seenOn: {[id: string]: Set<string>} = {} // a map of all events we've seen in each relay
constructor() {
private eoseSubTimeout: number
private getTimeout: number
constructor(options: {eoseSubTimeout?: number; getTimeout?: number} = {}) {
this._conn = {}
this.eoseSubTimeout = options.eoseSubTimeout || 3400
this.getTimeout = options.getTimeout || 3400
}
close(relays: string[]): void {
@@ -24,7 +29,10 @@ export class SimplePool {
const existing = this._conn[nm]
if (existing) return existing
const relay = relayInit(nm)
const relay = relayInit(nm, {
getTimeout: this.getTimeout * 0.9,
listTimeout: this.getTimeout * 0.9
})
this._conn[nm] = relay
await relay.connect()
@@ -51,7 +59,7 @@ export class SimplePool {
let eoseTimeout = setTimeout(() => {
eoseSent = true
for (let cb of eoseListeners.values()) cb()
}, 3500)
}, this.eoseSubTimeout)
relays.forEach(async relay => {
let r
@@ -120,7 +128,7 @@ export class SimplePool {
let timeout = setTimeout(() => {
sub.unsub()
resolve(null)
}, 3500)
}, this.getTimeout)
sub.on('event', (event: Event) => {
resolve(event)
clearTimeout(timeout)
@@ -150,13 +158,23 @@ export class SimplePool {
})
}
publish(relays: string[], event: Event): Pub[] {
return relays.map(relay => {
publish(relays: string[], event: Event): Pub {
let pubs = relays.map(relay => {
let r = this._conn[normalizeURL(relay)]
if (!r) return badPub(relay)
let s = r.publish(event)
return s
return r.publish(event)
})
return {
on(type, cb) {
pubs.forEach((pub, i) => {
pub.on(type, () => cb(relays[i]))
})
},
off() {
// do nothing here, FIXME
}
}
}
seenOn(id: string): string[] {

View File

@@ -35,7 +35,15 @@ export type SubscriptionOptions = {
alreadyHaveEvent?: null | ((id: string, relay: string) => boolean)
}
export function relayInit(url: string): Relay {
export function relayInit(
url: string,
options: {
getTimeout?: number
listTimeout?: number
} = {}
): Relay {
let {listTimeout = 3000, getTimeout = 3000} = options
var ws: WebSocket
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
var listeners: {
@@ -175,7 +183,10 @@ export function relayInit(url: string): Relay {
async function trySend(params: [string, ...any]) {
let msg = JSON.stringify(params)
if (!connected()) {
return
await new Promise(resolve => setTimeout(resolve, 1000))
if (!connected()) {
return
}
}
try {
ws.send(msg)
@@ -249,7 +260,7 @@ export function relayInit(url: string): Relay {
let timeout = setTimeout(() => {
s.unsub()
resolve(events)
}, 1500)
}, listTimeout)
s.on('eose', () => {
s.unsub()
clearTimeout(timeout)
@@ -265,7 +276,7 @@ export function relayInit(url: string): Relay {
let timeout = setTimeout(() => {
s.unsub()
resolve(null)
}, 1500)
}, getTimeout)
s.on('event', (event: Event) => {
s.unsub()
clearTimeout(timeout)