mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
deduplicate relay URLs in pool.subscribe() and pool.subscribeMany()
This commit is contained in:
@@ -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 {
|
||||||
|
|||||||
@@ -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,
|
||||||
})
|
})
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -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",
|
||||||
|
|||||||
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user