/** * ============================================ * 📦 推荐静态资源模块 (popular.js) * 功能:常用CSS/JS库展示、版本检测、多CDN链接生成 * 作者:gentpan * 更新:2026-01-03 * ============================================ */ import { config } from './config.js'; // ====================== // 基础配置与常量 // ====================== const CACHE_TTL = 5 * 60 * 1000; // 缓存有效期:5分钟 const versionCache = new Map(); const DEFAULT_TIMEOUT = 4000; // 请求超时:4 秒 const MIN_LOADING_MS = 1500; // 加载最少展示时长 const POPULAR_CACHE_URL = '/data/popular-cache.json'; const JSDELIVR_PACKAGE_API = 'https://data.jsdelivr.com/v1/package/npm'; const loadingSpinnerSvg = ` `; async function fetchWithTimeout(url, options = {}, timeout = DEFAULT_TIMEOUT) { const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), timeout); try { const response = await fetch(url, { ...options, signal: controller.signal }); return response; } finally { clearTimeout(timer); } } async function fetchPopularCache() { try { const response = await fetchWithTimeout(POPULAR_CACHE_URL, { cache: 'no-cache' }, 1200); if (!response.ok) return null; const data = await response.json(); if (!data || !Array.isArray(data.css) || !Array.isArray(data.js)) { return null; } return data; } catch (error) { console.warn('[popular] 本地热门资源缓存不可用,回退在线加载:', error); return null; } } async function getCachedPopularLibraries(type) { const cache = await fetchPopularCache(); if (!cache) return null; const items = type === 'js' ? cache.js : cache.css; return items.length ? items : null; } function showLoading(container, type) { container.innerHTML = `