feat: Replace OpenWebTorrent service with WebTorrentData service

This commit is contained in:
ChristoferMendes 2024-05-12 21:05:46 -03:00
parent 4616f69a29
commit 6b9f902b2c
2 changed files with 20 additions and 45 deletions

View File

@ -1,45 +0,0 @@
import axios, { AxiosResponse } from "axios";
import { Agent } from "https";
type OpenWebTorrentResponse =
| {
seeds: number;
peers: number;
}
| {
error: {
code: number;
message: string;
};
};
const httpsAgent = new Agent({
rejectUnauthorized: false,
});
const axiosRef = axios.create({
baseURL: "https://checker.openwebtorrent.com",
httpsAgent,
});
export const openWebTorrent = {
transformResponseError(response: AxiosResponse<OpenWebTorrentResponse>) {
if ("error" in response.data) {
throw new Error(response.data.error.message);
}
return response.data;
},
async getSeedersAndPeers(magnet: string) {
const endpoint = "/check";
const params = new URLSearchParams();
params.append("magnet", magnet);
const response = await axiosRef.get(endpoint, { params });
const { seeds, peers } = this.transformResponseError(response);
return {
seeders: seeds,
peers: peers,
};
},
};

View File

@ -0,0 +1,20 @@
import WebTorrentHealth from "webtorrent-health";
type WebTorrentHealthData = {
seeds: number;
peers: number;
};
export const webTorrentData = {
async getSeedersAndPeers(magnet: string) {
return new Promise((resolve, reject) => {
WebTorrentHealth(magnet, (err: Error, data: WebTorrentHealthData) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
},
};