diff --git a/js/webtorrent.js b/js/webtorrent.js index 4dee682a..4c67e45e 100644 --- a/js/webtorrent.js +++ b/js/webtorrent.js @@ -357,16 +357,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", @@ -401,9 +438,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); @@ -1005,6 +1049,15 @@ export class TorrentClient { chromeOptions.urlList = candidateUrls; } + const addExplicitWebSeeds = (torrent) => { + if (!candidateUrls.length || typeof torrent?.addWebSeed !== "function") { + return; + } + candidateUrls.forEach((url) => { + torrent.addWebSeed(url); + }); + }; + return new Promise((resolve, reject) => { // 3) Add the torrent to the client and handle accordingly. if (isFirefoxBrowser) { @@ -1014,6 +1067,7 @@ export class TorrentClient { { ...chromeOptions, maxWebConns: 4 }, (torrent) => { this.log("Torrent added (Firefox path):", torrent.name); + addExplicitWebSeeds(torrent); this.handleFirefoxTorrent(torrent, videoElement, resolve, reject); } ); @@ -1021,6 +1075,7 @@ export class TorrentClient { this.log("Starting torrent download (Chrome path)"); this.client.add(effectiveMagnetURI, chromeOptions, (torrent) => { this.log("Torrent added (Chrome path):", torrent.name); + addExplicitWebSeeds(torrent); this.handleChromeTorrent(torrent, videoElement, resolve, reject); }); }