From c175308e1d552faf48ce3a275a2b9d3010696ede Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Thu, 30 Nov 2023 02:11:16 +0100 Subject: [PATCH] fix copy nodes loop --- lib/peer-discovery.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/peer-discovery.js b/lib/peer-discovery.js index b1d701a..0c2b218 100644 --- a/lib/peer-discovery.js +++ b/lib/peer-discovery.js @@ -202,11 +202,9 @@ module.exports = class PeerDiscovery { if (!nodes) nodes = this._closestNodes else if (this._closestNodes !== nodes) { + const len = nodes.length for (const newer of this._closestNodes) { - for (const older of nodes) { - if (!older.id || !newer.id || b4a.equals(older.id, newer.id)) continue - nodes.push(newer) - } + if (newer.id && !hasNode(nodes, len, newer)) nodes.push(newer) } } @@ -288,4 +286,13 @@ class PeerDiscoverySession { } } +function hasNode (nodes, len, node) { + for (let i = 0; i < len; i++) { + const existing = nodes[i] + if (existing.id && b4a.equals(existing.id, node.id)) return true + } + + return false +} + function noop () {}