mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-02 10:52:27 +00:00
feat: persist webtorrent probes and tracker health checks to localstorage
- Add storage helpers for torrent probes and tracker health in `js/utils/storage.js` - Update `js/gridHealth.js` to read/write torrent probes from localStorage - Update `js/healthService.js` to read/write tracker health from localStorage - Ensure TTLs (5m for probes, 2m for trackers) are respected across sessions - Add verification test `tests/verify-storage-persistence.mjs` (removed before completion)
This commit is contained in:
1 parent
3de038dc08
commit
f06b2c7b1d
3 files changed
+158
-4
No files matched your search
@@ -2,11 +2,21 @@ import { userLogger } from "./logger.js";
|
||||
// js/utils/storage.js
|
||||
|
||||
const URL_HEALTH_STORAGE_PREFIX = "bitvid:urlHealth:";
|
||||
const TORRENT_PROBE_STORAGE_PREFIX = "bitvid:torrentProbe:";
|
||||
const TRACKER_HEALTH_STORAGE_PREFIX = "bitvid:trackerHealth:";
|
||||
|
||||
export function getUrlHealthStorageKey(eventId) {
|
||||
return `${URL_HEALTH_STORAGE_PREFIX}${eventId}`;
|
||||
}
|
||||
|
||||
export function getTorrentProbeStorageKey(infoHash) {
|
||||
return `${TORRENT_PROBE_STORAGE_PREFIX}${infoHash}`;
|
||||
}
|
||||
|
||||
export function getTrackerHealthStorageKey(infoHash) {
|
||||
return `${TRACKER_HEALTH_STORAGE_PREFIX}${infoHash}`;
|
||||
}
|
||||
|
||||
export function readUrlHealthFromStorage(eventId) {
|
||||
if (!eventId || typeof localStorage === "undefined") {
|
||||
return null;
|
||||
@@ -55,3 +65,107 @@ export function removeUrlHealthFromStorage(eventId) {
|
||||
userLogger.warn(`Failed to remove URL health for ${eventId}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
export function readTorrentProbeFromStorage(infoHash) {
|
||||
if (!infoHash || typeof localStorage === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const raw = localStorage.getItem(getTorrentProbeStorageKey(infoHash));
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
const parsed = JSON.parse(raw);
|
||||
if (parsed && typeof parsed === "object") {
|
||||
return parsed;
|
||||
}
|
||||
} catch (err) {
|
||||
userLogger.warn(
|
||||
`Failed to parse stored torrent probe for ${infoHash}:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
|
||||
removeTorrentProbeFromStorage(infoHash);
|
||||
return null;
|
||||
}
|
||||
|
||||
export function writeTorrentProbeToStorage(infoHash, entry) {
|
||||
if (!infoHash || typeof localStorage === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
localStorage.setItem(
|
||||
getTorrentProbeStorageKey(infoHash),
|
||||
JSON.stringify(entry)
|
||||
);
|
||||
} catch (err) {
|
||||
userLogger.warn(`Failed to persist torrent probe for ${infoHash}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
export function removeTorrentProbeFromStorage(infoHash) {
|
||||
if (!infoHash || typeof localStorage === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
localStorage.removeItem(getTorrentProbeStorageKey(infoHash));
|
||||
} catch (err) {
|
||||
userLogger.warn(`Failed to remove torrent probe for ${infoHash}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
export function readTrackerHealthFromStorage(infoHash) {
|
||||
if (!infoHash || typeof localStorage === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const raw = localStorage.getItem(getTrackerHealthStorageKey(infoHash));
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
const parsed = JSON.parse(raw);
|
||||
if (parsed && typeof parsed === "object") {
|
||||
return parsed;
|
||||
}
|
||||
} catch (err) {
|
||||
userLogger.warn(
|
||||
`Failed to parse stored tracker health for ${infoHash}:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
|
||||
removeTrackerHealthFromStorage(infoHash);
|
||||
return null;
|
||||
}
|
||||
|
||||
export function writeTrackerHealthToStorage(infoHash, entry) {
|
||||
if (!infoHash || typeof localStorage === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
localStorage.setItem(
|
||||
getTrackerHealthStorageKey(infoHash),
|
||||
JSON.stringify(entry)
|
||||
);
|
||||
} catch (err) {
|
||||
userLogger.warn(`Failed to persist tracker health for ${infoHash}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
export function removeTrackerHealthFromStorage(infoHash) {
|
||||
if (!infoHash || typeof localStorage === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
localStorage.removeItem(getTrackerHealthStorageKey(infoHash));
|
||||
} catch (err) {
|
||||
userLogger.warn(`Failed to remove tracker health for ${infoHash}:`, err);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user