-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (57 loc) · 1.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import axios from "axios";
import RNFetchBlob from "rn-fetch-blob";
class ApiSpeed {
static async getTestUrls(urlCount = 5) {
try {
const response = await axios.get(
`https://api.fast.com/netflix/speedtest/v2?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=${urlCount}`
);
const urls = response.data.targets.map((target) => target.url);
return urls;
} catch (error) {
console.error("Erro ao obter URLs do teste de velocidade", error);
throw error;
}
}
static async downloadFile(url) {
try {
const response = await RNFetchBlob.config({
fileCache: true,
}).fetch("GET", url);
const data = await response.blob();
return data;
} catch (error) {
console.error("Erro ao baixar o arquivo", error);
throw { error };
}
}
static async calculateSpeed(urlCount = 5) {
try {
const urls = await ApiSpeed.getTestUrls(urlCount);
const startTime = new Date().getTime();
const promises = urls.map((url) => ApiSpeed.downloadFile(url));
const files = await Promise.all(promises);
const endTime = new Date().getTime();
const elapsedSeconds = (endTime - startTime) / 1000;
const totalSize = files.reduce((acc, file) => acc + file.size, 0);
const downloadSpeed = (totalSize * 8) / elapsedSeconds / 1024 / 1024;
let formattedSpeed;
if (downloadSpeed >= 1000) {
formattedSpeed = [(downloadSpeed / 1000).toFixed(0), "Gbps"];
} else if (downloadSpeed >= 1) {
formattedSpeed = [downloadSpeed.toFixed(0), "Mbps"];
} else {
formattedSpeed = [(downloadSpeed * 1000).toFixed(0), "Kbps"];
}
const unlinkPromises = files.map((file) =>
RNFetchBlob.fs.unlink(file._ref)
);
await Promise.all(unlinkPromises);
return formattedSpeed;
} catch (error) {
console.error("Erro ao calcular a velocidade de download", error);
throw error;
}
}
}
export default ApiSpeed;