From e022f0f633d8c0084372f92976fccc9957d670bd Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Sun, 1 Feb 2026 11:03:19 -0500 Subject: [PATCH] Handle web seed peers in torrent probe --- js/webtorrent.js | 67 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/js/webtorrent.js b/js/webtorrent.js index 447582f7..47f8eec6 100644 --- a/js/webtorrent.js +++ b/js/webtorrent.js @@ -345,16 +345,53 @@ export class TorrentClient { return; } - const settleHealthy = () => { - const peers = Math.max(1, Math.floor(normalizeNumber(torrent?.numPeers, 1))); - finalize({ healthy: true, peers, reason: "peer" }); + let webSeedCount = 0; + + const getWebSeedCount = () => { + if (torrent?.wires?.length) { + const wireCount = torrent.wires.filter( + (wire) => wire?.type === "webSeed" + ).length; + return Math.max(webSeedCount, wireCount); + } + return webSeedCount; }; - torrent.once("wire", settleHealthy); + const getPeerCounts = () => { + const regularPeers = Math.max( + 0, + Math.floor(normalizeNumber(torrent?.numPeers, 0)) + ); + const webSeeds = getWebSeedCount(); + return { + regularPeers, + webSeeds, + totalPeers: regularPeers + webSeeds, + }; + }; + + const settleHealthy = (counts = getPeerCounts()) => { + const peers = Math.max(1, counts.totalPeers); + const webseedOnly = counts.regularPeers === 0 && counts.webSeeds > 0; + finalize({ + healthy: true, + peers, + reason: webseedOnly ? "webseed" : "peer", + webseedOnly, + }); + }; + + torrent.on("wire", (wire) => { + if (wire?.type === "webSeed") { + webSeedCount += 1; + } + settleHealthy(); + }); torrent.once("error", (err) => { - const peers = Math.max(0, Math.floor(normalizeNumber(torrent?.numPeers, 0))); - const webseedOnly = peers === 0 && hasWebSeed; + const counts = getPeerCounts(); + const webseedOnly = counts.regularPeers === 0 && counts.webSeeds > 0; + const peers = Math.max(0, counts.totalPeers); finalize({ healthy: webseedOnly, reason: webseedOnly ? "webseed" : "error", @@ -366,8 +403,9 @@ export class TorrentClient { if (safeTimeout > 0) { timeoutId = setTimeout(() => { - const peers = Math.max(0, Math.floor(normalizeNumber(torrent?.numPeers, 0))); - const webseedOnly = peers === 0 && hasWebSeed; + const counts = getPeerCounts(); + const webseedOnly = counts.regularPeers === 0 && counts.webSeeds > 0; + const peers = Math.max(0, counts.totalPeers); finalize({ healthy: webseedOnly, peers, @@ -386,9 +424,16 @@ export class TorrentClient { if (!torrent || settled) { return; } - const peers = Math.max(0, Math.floor(normalizeNumber(torrent.numPeers, 0))); - if (peers > 0) { - finalize({ healthy: true, peers, reason: "peer" }); + const counts = getPeerCounts(); + if (counts.totalPeers > 0) { + const webseedOnly = + counts.regularPeers === 0 && counts.webSeeds > 0; + finalize({ + healthy: true, + peers: counts.totalPeers, + reason: webseedOnly ? "webseed" : "peer", + webseedOnly, + }); } }, pollInterval); }, pollInterval);