All checks were successful
deploy to server / build-and-deploy (push) Successful in 2m57s
- 将高德地图改为百度地图
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
async function testSpeed(url: string, timeout = 1500) {
|
|
const start = performance.now();
|
|
|
|
try {
|
|
await Promise.race([
|
|
fetch(url, { method: 'HEAD', mode: 'no-cors' }),
|
|
new Promise((_, reject) => setTimeout(() => reject('timeout'), timeout)),
|
|
]);
|
|
|
|
return performance.now() - start;
|
|
} catch {
|
|
return Infinity; // unreachable or timed out
|
|
}
|
|
}
|
|
|
|
async function selectBestMap() {
|
|
const testTargets = {
|
|
// amap: 'https://www.amap.com/favicon.ico',
|
|
baidu: 'https://map.baidu.com/favicon.ico',
|
|
google: 'https://maps.google.com/favicon.ico',
|
|
};
|
|
|
|
const results: Record<string, number> = {};
|
|
|
|
for (const key in testTargets) {
|
|
results[key] = await testSpeed(testTargets[key]);
|
|
}
|
|
|
|
logger.debug(results);
|
|
|
|
// 根据延迟排序,选择最稳最快的平台
|
|
return Object.entries(results).sort((a, b) => a[1] - b[1])[0][0];
|
|
}
|
|
|
|
export async function getAutoMappedService(): Promise<string> {
|
|
const target = {
|
|
// amap: 'https://surl.amap.com/2dYNorIJ1dgoN',
|
|
baidu: 'https://j.map.baidu.com/f9/c3x',
|
|
google: 'https://maps.app.goo.gl/9LqvMwEq7VaRkqnM6',
|
|
};
|
|
|
|
const fastestMap = await selectBestMap();
|
|
|
|
return target[fastestMap];
|
|
}
|