Update web-torrent-data.ts

Small adjustment to webTorrentData logic
This commit is contained in:
Zamitto 2024-05-18 14:55:56 -03:00 committed by GitHub
parent d8937b3672
commit d924c64710
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,25 +5,34 @@ type WebTorrentHealthData = {
peers: number;
};
const MILLISECONDS = 1000;
const SECONDS = 1.5;
export const webTorrentData = {
async getSeedersAndPeers(
magnet: string
): Promise<{ seeders: number; peers: number }> {
): Promise<{ seeders: number; peers: number } | null> {
let peers = 0;
let seeds = 0;
let retry = 0;
let timeout = SECONDS * MILLISECONDS;
while (retry < 3 && (!peers || !seeds)) {
while (retry < 3) {
try {
const data: WebTorrentHealthData = await webTorrentHealth(magnet, {
timeout: 1500 * (retry + 1),
timeout,
});
peers = data.peers;
seeds = data.seeds;
if (peers || seeds) break;
} catch (e) {
retry++;
return null;
}
timeout *= 2;
retry++;
}
return { peers, seeders: seeds };