aboutsummaryrefslogtreecommitdiffstats
path: root/src/nostr.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/nostr.js')
-rw-r--r--src/nostr.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/nostr.js b/src/nostr.js
index 4b7f5c3..d2c9e44 100644
--- a/src/nostr.js
+++ b/src/nostr.js
@@ -602,6 +602,31 @@ export async function queryStreamOn(relays, filter, onEvent, maxWait = 1500) {
});
} catch {} finally { await Promise.all(pending); }
}
+// queryStreamOn that also says how deep each relay's answer went: resolves
+// with [{ url, n, min }] — how many events that relay sent for the filter and
+// the oldest created_at among them. A caller paging backwards needs it:
+// relays hold different parts of an author's history, and the oldest event
+// of the merged answer may be one relay's stray copy from months earlier.
+export async function queryDepthOn(relays, filter, onEvent, maxWait = 1500) {
+ probeRelays(relays);
+ const pending = [], stats = [], seen = new Set();
+ try {
+ const live = readRelays(relays);
+ await Promise.all(live.map((url) => {
+ const s = { url, n: 0, min: Infinity };
+ stats.push(s);
+ return askRelay(url, [filter], maxWait, (event) => {
+ if (!event) return;
+ s.n++;
+ if (event.created_at < s.min) s.min = event.created_at;
+ if (seen.has(event.id)) return;
+ seen.add(event.id);
+ pending.push(verifyOneAsync(event).then((ok) => ok && onEvent(event)).catch(() => {}));
+ });
+ }));
+ } catch {} finally { await Promise.all(pending); }
+ return stats;
+}
export async function publishOn(relays, evt, { onauth = null, onrefused = null } = {}) {
try {
const live = liveRelays(relays);