Skip to content

Commit 58fc58c

Browse files
Short-circuit follows collection at FOLLOWS_LIMIT
Address Copilot review on #39: collect up to FOLLOWS_LIMIT valid entries and stop, instead of validating/mapping the whole kind-3 list before slicing.
1 parent 7a4fafe commit 58fc58c

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/diddoc.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ export function buildDidDocument(pubkey, { profile, follows, relays } = {}) {
6767
} else if (Array.isArray(follows?.follows)) {
6868
followHexes = follows.follows;
6969
}
70-
const f = followHexes
71-
.filter((h) => HEX64.test(String(h).toLowerCase()))
72-
.map((h) => `did:nostr:${String(h).toLowerCase()}`);
73-
if (f.length) doc.follows = f.slice(0, FOLLOWS_LIMIT);
70+
// Collect up to FOLLOWS_LIMIT valid entries and stop — avoids validating/mapping
71+
// the entire list (kind-3 events can carry thousands of tags) just to slice it.
72+
const f = [];
73+
for (const h of followHexes) {
74+
const lc = String(h).toLowerCase();
75+
if (!HEX64.test(lc)) continue;
76+
f.push(`did:nostr:${lc}`);
77+
if (f.length >= FOLLOWS_LIMIT) break;
78+
}
79+
if (f.length) doc.follows = f;
7480

7581
// kind 10002 -> service (Relay)
7682
if (Array.isArray(relays?.tags)) {

0 commit comments

Comments
 (0)