mirror of
https://github.com/ZZZure/ZZZ-Plugin.git
synced 2025-12-16 13:17:32 +00:00
fix: gacha
This commit is contained in:
parent
08c15fd378
commit
f838c0823d
11 changed files with 249 additions and 23 deletions
31
utils/network.js
Normal file
31
utils/network.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import http from 'http';
|
||||
import https from 'https';
|
||||
export async function checkLatency(url) {
|
||||
let request = http;
|
||||
if (url.startsWith('https')) {
|
||||
request = https;
|
||||
}
|
||||
return new Promise(resolve => {
|
||||
const start = Date.now();
|
||||
request
|
||||
.get(url, res => {
|
||||
res.on('data', () => {});
|
||||
res.on('end', () => {
|
||||
const latency = Date.now() - start;
|
||||
resolve({ url, latency });
|
||||
});
|
||||
})
|
||||
.on('error', err => {
|
||||
logger.mark(`Error checking ${url}:`, err.message);
|
||||
resolve({ url, latency: Infinity });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function findLowestLatencyUrl(urls) {
|
||||
const results = await Promise.all(urls.map(checkLatency));
|
||||
const lowestLatencyResult = results.reduce((prev, curr) =>
|
||||
prev.latency < curr.latency ? prev : curr
|
||||
);
|
||||
return lowestLatencyResult.url;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue