massive CPU efficiency boost.

This commit is contained in:
Keep Creating Online
2025-02-06 11:56:13 -05:00
parent 6f05fd8ca3
commit 6cf1d53624
3 changed files with 263 additions and 216 deletions

397
js/app.js
View File

@@ -14,6 +14,48 @@ function fakeDecrypt(str) {
return str.split("").reverse().join(""); return str.split("").reverse().join("");
} }
/**
* Simple IntersectionObserver-based lazy loader for images (or videos).
*
* Usage:
* const mediaLoader = new MediaLoader();
* mediaLoader.observe(imgElement);
*
* This will load the real image source from `imgElement.dataset.lazy`
* once the image enters the viewport.
*/
class MediaLoader {
constructor(rootMargin = "50px") {
this.observer = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
const el = entry.target;
const lazySrc = el.dataset.lazy;
if (lazySrc) {
el.src = lazySrc;
delete el.dataset.lazy;
}
// Stop observing once loaded
this.observer.unobserve(el);
}
}
},
{ rootMargin }
);
}
observe(el) {
if (el.dataset.lazy) {
this.observer.observe(el);
}
}
disconnect() {
this.observer.disconnect();
}
}
class bitvidApp { class bitvidApp {
constructor() { constructor() {
// Basic auth/display elements // Basic auth/display elements
@@ -22,6 +64,9 @@ class bitvidApp {
this.userStatus = document.getElementById("userStatus") || null; this.userStatus = document.getElementById("userStatus") || null;
this.userPubKey = document.getElementById("userPubKey") || null; this.userPubKey = document.getElementById("userPubKey") || null;
// Lazy-loading helper for images
this.mediaLoader = new MediaLoader();
// Optional: a "profile" button or avatar (if used) // Optional: a "profile" button or avatar (if used)
this.profileButton = document.getElementById("profileButton") || null; this.profileButton = document.getElementById("profileButton") || null;
this.profileAvatar = document.getElementById("profileAvatar") || null; this.profileAvatar = document.getElementById("profileAvatar") || null;
@@ -478,22 +523,36 @@ class bitvidApp {
await this.cleanup(); await this.cleanup();
}); });
// 8) Handle back/forward nav => hide video modal // 8) Handle back/forward navigation => hide video modal
window.addEventListener("popstate", async () => { window.addEventListener("popstate", async () => {
console.log("[popstate] user navigated back/forward; cleaning modal..."); console.log("[popstate] user navigated back/forward; cleaning modal...");
await this.hideModal(); await this.hideModal();
}); });
// Event delegation for the “Application Form” button inside the login modal // 9) Event delegation on the video list container for playing videos
if (this.videoList) {
this.videoList.addEventListener("click", (event) => {
const magnetTrigger = event.target.closest("[data-play-magnet]");
if (magnetTrigger) {
// For a normal left-click (button 0, no Ctrl/Cmd), prevent navigation:
if (event.button === 0 && !event.ctrlKey && !event.metaKey) {
event.preventDefault(); // Stop browser from following the href
const magnet = magnetTrigger.dataset.playMagnet;
this.playVideo(magnet);
}
}
});
}
// 10) Event delegation for the “Application Form” button inside the login modal
document.addEventListener("click", (event) => { document.addEventListener("click", (event) => {
if (event.target && event.target.id === "openApplicationModal") { if (event.target && event.target.id === "openApplicationModal") {
// 1) Hide the login modal // Hide the login modal
const loginModal = document.getElementById("loginModal"); const loginModal = document.getElementById("loginModal");
if (loginModal) { if (loginModal) {
loginModal.classList.add("hidden"); loginModal.classList.add("hidden");
} }
// Show the application modal
// 2) Show the application modal
const appModal = document.getElementById("nostrFormModal"); const appModal = document.getElementById("nostrFormModal");
if (appModal) { if (appModal) {
appModal.classList.remove("hidden"); appModal.classList.remove("hidden");
@@ -535,6 +594,56 @@ class bitvidApp {
} }
} }
async fetchAndRenderProfile(pubkey, forceRefresh = false) {
const now = Date.now();
// 1) Check if we have a cached entry
const cacheEntry = this.profileCache.get(pubkey);
if (!forceRefresh && cacheEntry && now - cacheEntry.timestamp < 60000) {
// If it's less than 60 seconds old, just update DOM with it
this.updateProfileInDOM(pubkey, cacheEntry.profile);
return;
}
// 2) Otherwise, fetch from Nostr
try {
const userEvents = await nostrClient.pool.list(nostrClient.relays, [
{ kinds: [0], authors: [pubkey], limit: 1 },
]);
if (userEvents.length > 0 && userEvents[0].content) {
const data = JSON.parse(userEvents[0].content);
const profile = {
name: data.name || data.display_name || "Unknown",
picture: data.picture || "assets/svg/default-profile.svg",
};
// Cache it
this.profileCache.set(pubkey, { profile, timestamp: now });
// Update DOM
this.updateProfileInDOM(pubkey, profile);
}
} catch (err) {
console.error("Profile fetch error:", err);
}
}
updateProfileInDOM(pubkey, profile) {
// For any .author-pic[data-pubkey=...]
const picEls = document.querySelectorAll(
`.author-pic[data-pubkey="${pubkey}"]`
);
picEls.forEach((el) => {
el.src = profile.picture;
});
// For any .author-name[data-pubkey=...]
const nameEls = document.querySelectorAll(
`.author-name[data-pubkey="${pubkey}"]`
);
nameEls.forEach((el) => {
el.textContent = profile.name;
});
}
/** /**
* Actually handle the upload form submission. * Actually handle the upload form submission.
*/ */
@@ -815,10 +924,10 @@ class bitvidApp {
return olderMatches.length > 0; return olderMatches.length > 0;
} }
// 4) Build the DOM for each video in newestActive
async renderVideoList(videos) { async renderVideoList(videos) {
if (!this.videoList) return; if (!this.videoList) return;
// Check if there's anything to show
if (!videos || videos.length === 0) { if (!videos || videos.length === 0) {
this.videoList.innerHTML = ` this.videoList.innerHTML = `
<p class="flex justify-center items-center h-full w-full text-center text-gray-500"> <p class="flex justify-center items-center h-full w-full text-center text-gray-500">
@@ -830,13 +939,14 @@ class bitvidApp {
// Sort newest first // Sort newest first
videos.sort((a, b) => b.created_at - a.created_at); videos.sort((a, b) => b.created_at - a.created_at);
// <-- NEW: Convert allEvents map => array to check older overshadowed events // Convert allEvents to an array for checking older overshadowed events
const fullAllEventsArray = Array.from(nostrClient.allEvents.values()); const fullAllEventsArray = Array.from(nostrClient.allEvents.values());
const fragment = document.createDocumentFragment();
const htmlList = videos.map((video, index) => { videos.forEach((video, index) => {
if (!video.id || !video.title) { if (!video.id || !video.title) {
console.error("Video missing ID/title:", video); console.error("Video missing ID/title:", video);
return ""; return;
} }
const nevent = window.NostrTools.nip19.neventEncode({ id: video.id }); const nevent = window.NostrTools.nip19.neventEncode({ id: video.id });
@@ -850,32 +960,31 @@ class bitvidApp {
: "border-none"; : "border-none";
const timeAgo = this.formatTimeAgo(video.created_at); const timeAgo = this.formatTimeAgo(video.created_at);
// 1) Do we have an older version? // Check if there's an older version (for revert button)
let hasOlder = false; let hasOlder = false;
if (canEdit && video.videoRootId) { if (canEdit && video.videoRootId) {
hasOlder = this.hasOlderVersion(video, fullAllEventsArray); hasOlder = this.hasOlderVersion(video, fullAllEventsArray);
} }
// 2) If we do => show revert button
const revertButton = hasOlder const revertButton = hasOlder
? ` ? `
<button <button
class="block w-full text-left px-4 py-2 text-sm text-red-400 hover:bg-red-700 hover:text-white" class="block w-full text-left px-4 py-2 text-sm text-red-400 hover:bg-red-700 hover:text-white"
onclick="app.handleRevertVideo(${index}); document.getElementById('settingsDropdown-${index}').classList.add('hidden');" data-revert-index="${index}"
> >
Revert Revert
</button> </button>
` `
: ""; : "";
// 3) Gear menu // Gear menu (only shown if canEdit)
const gearMenu = canEdit const gearMenu = canEdit
? ` ? `
<div class="relative inline-block ml-3 overflow-visible"> <div class="relative inline-block ml-3 overflow-visible">
<button <button
type="button" type="button"
class="inline-flex items-center p-2 rounded-full text-gray-400 hover:text-gray-200 hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-blue-500" class="inline-flex items-center p-2 rounded-full text-gray-400 hover:text-gray-200 hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-blue-500"
onclick="document.getElementById('settingsDropdown-${index}').classList.toggle('hidden')" data-settings-dropdown="${index}"
> >
<img <img
src="assets/svg/video-settings-gear.svg" src="assets/svg/video-settings-gear.svg"
@@ -890,14 +999,14 @@ class bitvidApp {
<div class="py-1"> <div class="py-1">
<button <button
class="block w-full text-left px-4 py-2 text-sm text-gray-100 hover:bg-gray-700" class="block w-full text-left px-4 py-2 text-sm text-gray-100 hover:bg-gray-700"
onclick="app.handleEditVideo(${index}); document.getElementById('settingsDropdown-${index}').classList.add('hidden');" data-edit-index="${index}"
> >
Edit Edit
</button> </button>
${revertButton} ${revertButton}
<button <button
class="block w-full text-left px-4 py-2 text-sm text-red-400 hover:bg-red-700 hover:text-white" class="block w-full text-left px-4 py-2 text-sm text-red-400 hover:bg-red-700 hover:text-white"
onclick="app.handleFullDeleteVideo(${index}); document.getElementById('settingsDropdown-${index}').classList.add('hidden');" data-delete-all-index="${index}"
> >
Delete All Delete All
</button> </button>
@@ -907,42 +1016,26 @@ class bitvidApp {
` `
: ""; : "";
// 4) Build the card markup... // Card markup
const cardHtml = ` const cardHtml = `
<div class="video-card bg-gray-900 rounded-lg overflow-hidden shadow-lg hover:shadow-2xl transition-all duration-300 ${highlightClass}"> <div class="video-card bg-gray-900 rounded-lg overflow-hidden shadow-lg hover:shadow-2xl transition-all duration-300 ${highlightClass}">
<a <a
href="${shareUrl}" href="${shareUrl}"
target="_blank" data-play-magnet="${encodeURIComponent(video.magnet)}"
rel="noopener noreferrer"
class="block cursor-pointer relative group" class="block cursor-pointer relative group"
onclick="if (event.button === 0 && !event.ctrlKey && !event.metaKey) {
event.preventDefault();
app.playVideo('${encodeURIComponent(video.magnet)}');
}"
> >
<div class="ratio-16-9"> <div class="ratio-16-9">
<img <img
src="assets/jpg/video-thumbnail-fallback.jpg" src="assets/jpg/video-thumbnail-fallback.jpg"
data-real-src="${this.escapeHTML(video.thumbnail)}" data-lazy="${this.escapeHTML(video.thumbnail)}"
alt="${this.escapeHTML(video.title)}" alt="${this.escapeHTML(video.title)}"
onload="
const realSrc = this.getAttribute('data-real-src');
if (realSrc) {
const that = this;
const testImg = new Image();
testImg.onload = function() {
that.src = realSrc;
};
testImg.src = realSrc;
}
"
/> />
</div> </div>
</a> </a>
<div class="p-4"> <div class="p-4">
<h3 <h3
class="text-lg font-bold text-white line-clamp-2 hover:text-blue-400 cursor-pointer mb-3" class="text-lg font-bold text-white line-clamp-2 hover:text-blue-400 cursor-pointer mb-3"
onclick="app.playVideo('${encodeURIComponent(video.magnet)}')" data-play-magnet="${encodeURIComponent(video.magnet)}"
> >
${this.escapeHTML(video.title)} ${this.escapeHTML(video.title)}
</h3> </h3>
@@ -974,132 +1067,83 @@ class bitvidApp {
</div> </div>
`; `;
// Fire off a background fetch for the author's profile // Turn the HTML into an element
const template = document.createElement("template");
template.innerHTML = cardHtml.trim();
const cardEl = template.content.firstElementChild;
// Fetch the author's profile info in the background
this.fetchAndRenderProfile(video.pubkey); this.fetchAndRenderProfile(video.pubkey);
return cardHtml; // Add the finished card to our fragment
fragment.appendChild(cardEl);
}); });
// Filter out any empty strings // Clear the list and add our fragment
const valid = htmlList.filter((x) => x.length > 0); this.videoList.innerHTML = "";
if (valid.length === 0) { this.videoList.appendChild(fragment);
this.videoList.innerHTML = `
<p class="text-center text-gray-500">
No valid videos to display.
</p>`;
return;
}
// Finally inject into DOM // Lazy-load images
this.videoList.innerHTML = valid.join(""); const lazyEls = this.videoList.querySelectorAll("[data-lazy]");
} lazyEls.forEach((el) => this.mediaLoader.observe(el));
/** // -------------------------------
* Retrieve the profile for a given pubkey (kind:0) and update the DOM. // Gear menu / button event listeners
*/ // -------------------------------
async fetchAndRenderProfile(pubkey, forceRefresh = false) {
const now = Date.now();
// Check if we already have a cached entry for this pubkey: // Toggle the gear menu
const cacheEntry = this.profileCache.get(pubkey); const gearButtons = this.videoList.querySelectorAll(
"[data-settings-dropdown]"
// If not forcing refresh, and we have a cache entry less than 60 sec old, use it:
if (!forceRefresh && cacheEntry && now - cacheEntry.timestamp < 60000) {
this.updateProfileInDOM(pubkey, cacheEntry.profile);
return;
}
// Otherwise, go fetch from the relay
try {
const userEvents = await nostrClient.pool.list(nostrClient.relays, [
{ kinds: [0], authors: [pubkey], limit: 1 },
]);
if (userEvents.length > 0 && userEvents[0].content) {
const data = JSON.parse(userEvents[0].content);
const profile = {
name: data.name || data.display_name || "Unknown",
picture: data.picture || "assets/svg/default-profile.svg",
};
// Store into the cache with a timestamp
this.profileCache.set(pubkey, {
profile,
timestamp: now,
});
// Now update the DOM elements
this.updateProfileInDOM(pubkey, profile);
}
} catch (err) {
console.error("Profile fetch error for pubkey:", pubkey, err);
}
}
/**
* Update all DOM elements that match this pubkey, e.g. .author-pic[data-pubkey=...]
*/
updateProfileInDOM(pubkey, profile) {
const picEls = document.querySelectorAll(
`.author-pic[data-pubkey="${pubkey}"]`
); );
picEls.forEach((el) => { gearButtons.forEach((button) => {
el.src = profile.picture; button.addEventListener("click", () => {
const index = button.getAttribute("data-settings-dropdown");
const dropdown = document.getElementById(`settingsDropdown-${index}`);
if (dropdown) {
dropdown.classList.toggle("hidden");
}
});
}); });
const nameEls = document.querySelectorAll(
`.author-name[data-pubkey="${pubkey}"]` // Edit button
const editButtons = this.videoList.querySelectorAll("[data-edit-index]");
editButtons.forEach((button) => {
button.addEventListener("click", () => {
const index = button.getAttribute("data-edit-index");
const dropdown = document.getElementById(`settingsDropdown-${index}`);
if (dropdown) dropdown.classList.add("hidden");
// Assuming you have a method like this in your code:
this.handleEditVideo(index);
});
});
// Revert button
const revertButtons = this.videoList.querySelectorAll(
"[data-revert-index]"
); );
nameEls.forEach((el) => { revertButtons.forEach((button) => {
el.textContent = profile.name; button.addEventListener("click", () => {
const index = button.getAttribute("data-revert-index");
const dropdown = document.getElementById(`settingsDropdown-${index}`);
if (dropdown) dropdown.classList.add("hidden");
// Assuming you have a method like this in your code:
this.handleRevertVideo(index);
});
}); });
}
/** // Delete All button
* Plays a video given its magnet URI. const deleteAllButtons = this.videoList.querySelectorAll(
* We simply look up which event has this magnet "[data-delete-all-index]"
* and then delegate to playVideoByEventId for );
* consistent modal and metadata handling. deleteAllButtons.forEach((button) => {
*/ button.addEventListener("click", () => {
async playVideo(magnetURI) { const index = button.getAttribute("data-delete-all-index");
try { const dropdown = document.getElementById(`settingsDropdown-${index}`);
if (!magnetURI) { if (dropdown) dropdown.classList.add("hidden");
this.showError("Invalid Magnet URI."); // Assuming you have a method like this in your code:
return; this.handleFullDeleteVideo(index);
} });
});
const decodedMagnet = decodeURIComponent(magnetURI);
// If we are already playing this exact magnet, do nothing.
if (this.currentMagnetUri === decodedMagnet) {
this.log("Same video requested - already playing");
return;
}
// 1) Check local 'videosMap' or 'nostrClient.getActiveVideos()'
let matchedVideo = Array.from(this.videosMap.values()).find(
(v) => v.magnet === decodedMagnet
);
if (!matchedVideo) {
// Instead of forcing a full `fetchVideos()`,
// try looking in the activeVideos from local cache:
const activeVideos = nostrClient.getActiveVideos();
matchedVideo = activeVideos.find((v) => v.magnet === decodedMagnet);
}
// If still not found, you can do a single event-based approach or just show an error:
if (!matchedVideo) {
this.showError("No matching video found in local cache.");
return;
}
// Update tracking
this.currentMagnetUri = decodedMagnet;
// Delegate to the main method
await this.playVideoByEventId(matchedVideo.id);
} catch (error) {
console.error("Error in playVideo:", error);
this.showError(`Playback error: ${error.message}`);
}
} }
/** /**
@@ -1371,55 +1415,48 @@ class bitvidApp {
* Helper to open a video by event ID (like ?v=...). * Helper to open a video by event ID (like ?v=...).
*/ */
async playVideoByEventId(eventId) { async playVideoByEventId(eventId) {
// First, check if this event is blacklisted by event ID
if (this.blacklistedEventIds.has(eventId)) { if (this.blacklistedEventIds.has(eventId)) {
this.showError("This content has been removed or is not allowed."); this.showError("This content has been removed or is not allowed.");
return; return;
} }
try { try {
// 1) Check local subscription map
let video = this.videosMap.get(eventId); let video = this.videosMap.get(eventId);
// 2) If not in local map, attempt fallback fetch from getOldEventById
if (!video) { if (!video) {
video = await this.getOldEventById(eventId); video = await this.getOldEventById(eventId);
} }
// 3) If still not found, show error and return
if (!video) { if (!video) {
this.showError("Video not found."); this.showError("Video not found.");
return; return;
} }
// **Check if videos author is blacklisted**
const authorNpub = this.safeEncodeNpub(video.pubkey) || video.pubkey; const authorNpub = this.safeEncodeNpub(video.pubkey) || video.pubkey;
if (initialBlacklist.includes(authorNpub)) { if (initialBlacklist.includes(authorNpub)) {
this.showError("This content has been removed or is not allowed."); this.showError("This content has been removed or is not allowed.");
return; return;
} }
// 4) Decrypt magnet if private & owned
if ( if (
video.isPrivate && video.isPrivate &&
video.pubkey === this.pubkey && video.pubkey === this.pubkey &&
!video.alreadyDecrypted !video.alreadyDecrypted
) { ) {
this.log("Decrypting private magnet link...");
video.magnet = fakeDecrypt(video.magnet); video.magnet = fakeDecrypt(video.magnet);
video.alreadyDecrypted = true; video.alreadyDecrypted = true;
} }
// 5) Show the modal and set the "please stand by" poster
this.currentVideo = video; this.currentVideo = video;
this.currentMagnetUri = video.magnet; this.currentMagnetUri = video.magnet;
this.showModalWithPoster(); this.showModalWithPoster();
// 6) Update ?v= param in the URL // Update ?v= param in the URL
const nevent = window.NostrTools.nip19.neventEncode({ id: eventId }); const nevent = window.NostrTools.nip19.neventEncode({ id: eventId });
const newUrl = const newUrl = `${window.location.pathname}?v=${encodeURIComponent(
window.location.pathname + `?v=${encodeURIComponent(nevent)}`; nevent
)}`;
window.history.pushState({}, "", newUrl); window.history.pushState({}, "", newUrl);
// 7) Optionally fetch the author profile // Fetch author profile
let creatorProfile = { let creatorProfile = {
name: "Unknown", name: "Unknown",
picture: `https://robohash.org/${video.pubkey}`, picture: `https://robohash.org/${video.pubkey}`,
@@ -1439,7 +1476,6 @@ class bitvidApp {
this.log("Error fetching creator profile:", error); this.log("Error fetching creator profile:", error);
} }
// 8) Render video details in modal
const creatorNpub = this.safeEncodeNpub(video.pubkey) || video.pubkey; const creatorNpub = this.safeEncodeNpub(video.pubkey) || video.pubkey;
if (this.videoTitle) { if (this.videoTitle) {
this.videoTitle.textContent = video.title || "Untitled"; this.videoTitle.textContent = video.title || "Untitled";
@@ -1465,43 +1501,27 @@ class bitvidApp {
this.creatorAvatar.alt = creatorProfile.name; this.creatorAvatar.alt = creatorProfile.name;
} }
// 9) Clean up any existing torrent instance before starting a new stream
await torrentClient.cleanup(); await torrentClient.cleanup();
// 10) Append a cache-busting parameter to the magnet URI
const cacheBustedMagnet = video.magnet + "&ts=" + Date.now(); const cacheBustedMagnet = video.magnet + "&ts=" + Date.now();
this.log("Starting video stream with:", cacheBustedMagnet); this.log("Starting video stream with:", cacheBustedMagnet);
// 11) Set autoplay preferences: // Autoplay preferences
// Read user preference from localStorage (if not set, default to muted)
const storedUnmuted = localStorage.getItem("unmutedAutoplay"); const storedUnmuted = localStorage.getItem("unmutedAutoplay");
const userWantsUnmuted = storedUnmuted === "true"; const userWantsUnmuted = storedUnmuted === "true";
this.modalVideo.muted = !userWantsUnmuted; this.modalVideo.muted = !userWantsUnmuted;
this.log(
"Autoplay preference - unmuted:",
userWantsUnmuted,
"=> muted:",
this.modalVideo.muted
);
// Attach a volumechange listener to update the stored preference
this.modalVideo.addEventListener("volumechange", () => { this.modalVideo.addEventListener("volumechange", () => {
localStorage.setItem( localStorage.setItem(
"unmutedAutoplay", "unmutedAutoplay",
(!this.modalVideo.muted).toString() (!this.modalVideo.muted).toString()
); );
this.log(
"Volume changed, new unmuted preference:",
!this.modalVideo.muted
);
}); });
// 12) Start torrent streaming
const realTorrent = await torrentClient.streamVideo( const realTorrent = await torrentClient.streamVideo(
cacheBustedMagnet, cacheBustedMagnet,
this.modalVideo this.modalVideo
); );
// 13) Attempt to autoplay; if unmuted autoplay fails, fall back to muted
this.modalVideo.play().catch((err) => { this.modalVideo.play().catch((err) => {
this.log("Autoplay failed:", err); this.log("Autoplay failed:", err);
if (!this.modalVideo.muted) { if (!this.modalVideo.muted) {
@@ -1513,7 +1533,7 @@ class bitvidApp {
} }
}); });
// 14) Start intervals to update torrent stats (every 3 seconds) // Update torrent stats every 3s
const updateInterval = setInterval(() => { const updateInterval = setInterval(() => {
if (!document.body.contains(this.modalVideo)) { if (!document.body.contains(this.modalVideo)) {
clearInterval(updateInterval); clearInterval(updateInterval);
@@ -1523,7 +1543,7 @@ class bitvidApp {
}, 3000); }, 3000);
this.activeIntervals.push(updateInterval); this.activeIntervals.push(updateInterval);
// 15) (Optional) Mirror small inline stats into the modal // Mirror stats into the modal if needed
const mirrorInterval = setInterval(() => { const mirrorInterval = setInterval(() => {
if (!document.body.contains(this.modalVideo)) { if (!document.body.contains(this.modalVideo)) {
clearInterval(mirrorInterval); clearInterval(mirrorInterval);
@@ -1534,7 +1554,6 @@ class bitvidApp {
const peers = document.getElementById("peers"); const peers = document.getElementById("peers");
const speed = document.getElementById("speed"); const speed = document.getElementById("speed");
const downloaded = document.getElementById("downloaded"); const downloaded = document.getElementById("downloaded");
if (status && this.modalStatus) { if (status && this.modalStatus) {
this.modalStatus.textContent = status.textContent; this.modalStatus.textContent = status.textContent;
} }

View File

@@ -1,3 +1,5 @@
//js/sidebar.js
import { loadView } from "./viewManager.js"; import { loadView } from "./viewManager.js";
import { viewInitRegistry } from "./viewManager.js"; import { viewInitRegistry } from "./viewManager.js";

View File

@@ -1,10 +1,19 @@
//js/webtorrent.js
import WebTorrent from "./webtorrent.min.js"; import WebTorrent from "./webtorrent.min.js";
export class TorrentClient { export class TorrentClient {
constructor() { constructor() {
this.client = null; // Do NOT instantiate right away // Reusable objects and flags
this.client = null;
this.currentTorrent = null; this.currentTorrent = null;
this.TIMEOUT_DURATION = 60000; // 60 seconds
// Service worker registration is cached
this.swRegistration = null;
this.serverCreated = false; // Indicates if we've called createServer on this.client
// Timeout for SW operations
this.TIMEOUT_DURATION = 60000;
} }
log(msg) { log(msg) {
@@ -21,6 +30,22 @@ export class TorrentClient {
return /firefox/i.test(window.navigator.userAgent); return /firefox/i.test(window.navigator.userAgent);
} }
/**
* Makes sure we have exactly one WebTorrent client instance and one SW registration.
* Called once from streamVideo.
*/
async init() {
// 1) If the client doesn't exist, create it
if (!this.client) {
this.client = new WebTorrent();
}
// 2) If we havent registered the service worker yet, do it now
if (!this.swRegistration) {
this.swRegistration = await this.setupServiceWorker();
}
}
async waitForServiceWorkerActivation(registration) { async waitForServiceWorkerActivation(registration) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
@@ -67,6 +92,7 @@ export class TorrentClient {
throw new Error("Service Worker not supported or disabled"); throw new Error("Service Worker not supported or disabled");
} }
// Brave-specific logic
if (isBraveBrowser) { if (isBraveBrowser) {
this.log("Checking Brave configuration..."); this.log("Checking Brave configuration...");
if (!navigator.serviceWorker) { if (!navigator.serviceWorker) {
@@ -78,6 +104,7 @@ export class TorrentClient {
throw new Error("Please enable WebRTC in Brave Shield settings"); throw new Error("Please enable WebRTC in Brave Shield settings");
} }
// Unregister all existing service workers before installing a fresh one
const registrations = await navigator.serviceWorker.getRegistrations(); const registrations = await navigator.serviceWorker.getRegistrations();
for (const reg of registrations) { for (const reg of registrations) {
await reg.unregister(); await reg.unregister();
@@ -135,8 +162,8 @@ export class TorrentClient {
// Force the SW to check for updates // Force the SW to check for updates
registration.update(); registration.update();
this.log("Service worker ready"); this.log("Service worker ready");
return registration; return registration;
} catch (error) { } catch (error) {
this.log("Service worker setup error:", error); this.log("Service worker setup error:", error);
@@ -144,7 +171,7 @@ export class TorrentClient {
} }
} }
// Minimal handleChromeTorrent // Handle Chrome-based browsers
handleChromeTorrent(torrent, videoElement, resolve, reject) { handleChromeTorrent(torrent, videoElement, resolve, reject) {
torrent.on("warning", (err) => { torrent.on("warning", (err) => {
if (err && typeof err.message === "string") { if (err && typeof err.message === "string") {
@@ -204,7 +231,7 @@ export class TorrentClient {
}); });
} }
// Minimal handleFirefoxTorrent // Handle Firefox-based browsers
handleFirefoxTorrent(torrent, videoElement, resolve, reject) { handleFirefoxTorrent(torrent, videoElement, resolve, reject) {
const file = torrent.files.find((f) => const file = torrent.files.find((f) =>
/\.(mp4|webm|mkv)$/.test(f.name.toLowerCase()) /\.(mp4|webm|mkv)$/.test(f.name.toLowerCase())
@@ -227,7 +254,7 @@ export class TorrentClient {
}); });
try { try {
file.streamTo(videoElement, { highWaterMark: 32 * 1024 }); file.streamTo(videoElement, { highWaterMark: 256 * 1024 });
this.currentTorrent = torrent; this.currentTorrent = torrent;
resolve(torrent); resolve(torrent);
} catch (err) { } catch (err) {
@@ -243,32 +270,27 @@ export class TorrentClient {
/** /**
* Initiates streaming of a torrent magnet to a <video> element. * Initiates streaming of a torrent magnet to a <video> element.
* Ensures the service worker is registered first. * Ensures the service worker is set up only once and the client is reused.
*/ */
async streamVideo(magnetURI, videoElement) { async streamVideo(magnetURI, videoElement) {
try { try {
// 1) Instantiate client on-demand: // 1) Make sure we have a WebTorrent client and a valid SW registration.
if (!this.client) { await this.init();
this.client = new WebTorrent();
}
// 2) Setup service worker
const registration = await this.setupServiceWorker();
if (!registration || !registration.active) {
throw new Error("Service worker setup failed");
}
// 3) Create the WebTorrent server with the registered service worker. // 2) Create the server once if not already created.
// Force the server to use '/webtorrent' as the URL prefix. if (!this.serverCreated) {
this.client.createServer({ this.client.createServer({
controller: registration, controller: this.swRegistration,
pathPrefix: location.origin + "/webtorrent", pathPrefix: location.origin + "/webtorrent",
}); });
this.serverCreated = true;
this.log("WebTorrent server created"); this.log("WebTorrent server created");
}
const isFirefoxBrowser = this.isFirefox(); const isFirefoxBrowser = this.isFirefox();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 3) Add the torrent to the client and handle accordingly.
if (isFirefoxBrowser) { if (isFirefoxBrowser) {
this.log("Starting torrent download (Firefox path)"); this.log("Starting torrent download (Firefox path)");
this.client.add( this.client.add(
@@ -281,31 +303,35 @@ export class TorrentClient {
); );
} else { } else {
this.log("Starting torrent download (Chrome path)"); this.log("Starting torrent download (Chrome path)");
this.client.add(magnetURI, (torrent) => { this.client.add(magnetURI, { strategy: "sequential" }, (torrent) => {
this.log("Torrent added (Chrome path):", torrent.name); this.log("Torrent added (Chrome path):", torrent.name);
this.handleChromeTorrent(torrent, videoElement, resolve, reject); this.handleChromeTorrent(torrent, videoElement, resolve, reject);
}); });
} }
}); });
} catch (error) { } catch (error) {
this.log("Failed to setup video streaming:", error); this.log("Failed to set up video streaming:", error);
throw error; throw error;
} }
} }
/** /**
* Clean up resources. * Clean up resources.
* You might decide to keep the client alive if you want to reuse torrents.
* Currently, this fully destroys the client and resets everything.
*/ */
async cleanup() { async cleanup() {
try { try {
if (this.currentTorrent) { if (this.currentTorrent) {
this.currentTorrent.destroy(); this.currentTorrent.destroy();
} }
// Destroy client entirely and set to null // Destroy client entirely and set to null so a future streamVideo call starts fresh
if (this.client) { if (this.client) {
await this.client.destroy(); await this.client.destroy();
this.client = null; this.client = null;
} }
this.currentTorrent = null;
this.serverCreated = false;
} catch (error) { } catch (error) {
this.log("Cleanup error:", error); this.log("Cleanup error:", error);
} }