deduplicate relay URLs in pool.subscribe() and pool.subscribeMany()

This commit is contained in:
fiatjaf
2025-08-06 10:37:14 -03:00
parent b39dac3551
commit 86235314c4
4 changed files with 25 additions and 12 deletions

View File

@@ -74,19 +74,32 @@ export class AbstractSimplePool {
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser { subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
params.onauth = params.onauth || params.doauth params.onauth = params.onauth || params.doauth
return this.subscribeMap( const request: { url: string; filter: Filter }[] = []
relays.map(url => ({ url, filter })), for (let i = 0; i < relays.length; i++) {
params, const url = normalizeURL(relays[i])
) if (!request.find(r => r.url === url)) {
request.push({ url, filter })
}
}
return this.subscribeMap(request, params)
} }
subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser { subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser {
params.onauth = params.onauth || params.doauth params.onauth = params.onauth || params.doauth
return this.subscribeMap( const request: { url: string; filter: Filter }[] = []
relays.flatMap(url => filters.map(filter => ({ url, filter }))), const uniqUrls: string[] = []
params, for (let i = 0; i < relays.length; i++) {
) const url = normalizeURL(relays[i])
if (uniqUrls.indexOf(url) === -1) {
for (let f = 0; f < filters.length; f++) {
request.push({ url, filter: filters[f] })
}
}
}
return this.subscribeMap(request, params)
} }
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser { subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {

View File

@@ -156,13 +156,13 @@ export class AbstractRelay {
} }
private async waitForDummyReq() { private async waitForDummyReq() {
return new Promise((res, err) => { return new Promise((resolve, _) => {
// make a dummy request with expected empty eose reply // make a dummy request with expected empty eose reply
// ["REQ", "_", {"ids":["aaaa...aaaa"]}] // ["REQ", "_", {"ids":["aaaa...aaaa"]}]
const sub = this.subscribe([{ ids: ['a'.repeat(64)] }], { const sub = this.subscribe([{ ids: ['a'.repeat(64)] }], {
oneose: () => { oneose: () => {
sub.close() sub.close()
res(true) resolve(true)
}, },
eoseTimeout: this.pingTimeout + 1000, eoseTimeout: this.pingTimeout + 1000,
}) })

View File

@@ -1,6 +1,6 @@
{ {
"name": "@nostr/tools", "name": "@nostr/tools",
"version": "2.16.1", "version": "2.16.2",
"exports": { "exports": {
".": "./index.ts", ".": "./index.ts",
"./core": "./core.ts", "./core": "./core.ts",

View File

@@ -1,7 +1,7 @@
{ {
"type": "module", "type": "module",
"name": "nostr-tools", "name": "nostr-tools",
"version": "2.16.1", "version": "2.16.2",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",