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 = {}; 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 { 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]; }