From 72ee5b9f2b50936ec8e7ed7e30357395faceb15e Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Thu, 15 Jan 2026 11:49:01 -0500 Subject: [PATCH] Guard source toggle with CDN/peer availability --- js/app.js | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/js/app.js b/js/app.js index aaed7860..66d20224 100644 --- a/js/app.js +++ b/js/app.js @@ -406,14 +406,33 @@ class Application { ...(fallbackVideo || {}), ...(modalVideo || {}), }; + const urlCandidate = + typeof video.url === "string" ? video.url.trim() : ""; + const magnetCandidate = + typeof video.magnet === "string" ? video.magnet.trim() : ""; if (!modalVideo && !fallbackVideo) { devLogger.warn("[app] Playback source switch missing video data."); return; } - const magnetAvailable = - typeof video.magnet === "string" && video.magnet.trim(); + const magnetAvailable = Boolean(magnetCandidate); + const cachedStreamHealth = + video?.id && this.streamHealthSnapshots instanceof Map + ? this.streamHealthSnapshots.get(video.id) + : null; + const cachedPeers = Number.isFinite(cachedStreamHealth?.peers) + ? cachedStreamHealth.peers + : null; + const hasActivePeers = + cachedPeers === null ? null : cachedPeers > 0; + const cachedUrlHealth = + video?.id && urlCandidate + ? this.getCachedUrlHealth(video.id, urlCandidate) + : null; + const cdnUnavailable = + !urlCandidate || + ["offline", "timeout"].includes(cachedUrlHealth?.status); if (source === "torrent" && !magnetAvailable) { userLogger.warn( @@ -425,9 +444,29 @@ class Application { return; } + if (source === "torrent" && hasActivePeers === false) { + userLogger.warn( + "[app] Unable to switch to torrent playback: no active peers.", + ); + this.showError( + "Torrent playback is unavailable right now because there are no active peers.", + ); + return; + } + + if (source === "url" && cdnUnavailable) { + userLogger.warn( + "[app] Unable to switch to CDN playback: URL unavailable.", + ); + this.showError( + "CDN playback is unavailable right now, staying on the torrent stream.", + ); + return; + } + this.playVideoWithFallback({ - url: video.url, - magnet: video.magnet, + url: urlCandidate, + magnet: magnetCandidate, forcedSource: source, }).catch((error) => { devLogger.warn(