From ee46b36a25f5b159410620be9d33ad93d4f61616 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 1 Feb 2026 19:35:02 +0000 Subject: [PATCH] docs: add detailed webtorrent architecture and regression prevention comments - Create `docs/webtorrent-architecture.md` detailing the "Webseeds are Peers" strategy. - Update `docs/video-availability-checks.md` with cross-reference. - Add "CRITICAL" warning comments to `js/webtorrent.js` and `js/gridHealth.js` to prevent regressions. - Enhance `tests/webtorrent-regression.test.mjs` to explicitly verify `urlList` passing in mocks. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- docs/video-availability-checks.md | 2 ++ docs/webtorrent-architecture.md | 50 ++++++++++++++++++++++++++++ js/gridHealth.js | 10 ++++++ js/webtorrent.js | 18 ++++++++++ tests/webtorrent-regression.test.mjs | 8 +++++ 5 files changed, 88 insertions(+) create mode 100644 docs/webtorrent-architecture.md diff --git a/docs/video-availability-checks.md b/docs/video-availability-checks.md index c2781181..a1fa88fb 100644 --- a/docs/video-availability-checks.md +++ b/docs/video-availability-checks.md @@ -38,6 +38,8 @@ This document explains how bitvid probes URL and torrent availability, caches re ## Torrent (stream) health checks +For a detailed architectural overview of the WebTorrent implementation, including webseed handling and Service Worker integration, see [WebTorrent Architecture & Strategy](webtorrent-architecture.md). + ### Card-level probes (`js/gridHealth.js`) The grid health module manages per-card WebTorrent probes and badge updates: diff --git a/docs/webtorrent-architecture.md b/docs/webtorrent-architecture.md new file mode 100644 index 00000000..aeccbf0d --- /dev/null +++ b/docs/webtorrent-architecture.md @@ -0,0 +1,50 @@ +# WebTorrent Architecture & Strategy + +This document details the architecture of the WebTorrent implementation in bitvid, focusing on the client management, Service Worker integration, and the critical logic for handling webseeds. + +## Core Principle: Webseeds Are Peers + +**Crucial Logic (Regression Risk):** +A recurring regression in this codebase involves treating "Webseeds" (HTTP sources) as distinct from "Peers" (P2P sources) in a way that marks webseed-only videos as "unhealthy". + +**The Correct Behavior:** +* We **must** pass webseeds (`urlList`) directly to the WebTorrent client via `client.add(url, { urlList: [...] })`. +* We **must** rely on the WebTorrent client to count that webseed as a connected peer. +* **Health = Peers > 0**. If a webseed connects, `numPeers` becomes 1. This is "Healthy". + +**Anti-Pattern (Do Not Implement):** +* *Do not* attempt to manually check if `peers === 0 && hasWebseed` and label it as a special "healthy" state in the UI logic. +* *Do not* fail to pass `urlList` to `client.add`. If you don't pass it, WebTorrent won't see the peer, `numPeers` stays 0, and the video is marked unhealthy. + +## Architecture Components + +### 1. The Singleton Client (`TorrentClient`) +`js/webtorrent.js` exports a singleton `torrentClient`. We do not instantiate multiple clients. +* **Reason:** WebTorrent clients are expensive. They open many socket connections and maintain DHT state. +* **Reuse:** The same client instance is used for both "Probing" (checking health) and "Streaming" (playback). + +### 2. The Service Worker (`sw.min.js`) +Browser-based WebTorrent cannot write directly to the filesystem or stream directly to a `