@tapepack/core 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/prices.mjs +8 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tapepack/core",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "description": "TapePack:把静态站点压缩后部署到 DeWEB(TapeOut)链上,上链 Gas 直降——压缩、估算、增量更新、一条命令部署",
6
6
  "license": "MIT",
package/src/prices.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // 原生币实时美元价(BNB / OKB / ETH),浏览器 / Node 共用。服务费的封顶 $2 与免费门槛 $0.10 按它折算。
2
- // 依次取 CoinGecko → OKX → Binance(都允许浏览器跨域),前一个缺的币由后一个补;全部取不到时返回 null——
2
+ // 来源优先级 CoinGecko → OKX → Binance(都允许浏览器跨域),前一个缺的币由后一个补;全部取不到时返回 null——
3
3
  // 调用方据此不收服务费(chains.mjs 的快照价可能偏差数倍)。CoinGecko 免费接口限流严、国内常连不上,所以要有后备。
4
4
 
5
5
  export const PRICE_IDS = { bnb: 'binancecoin', xlayer: 'okb', base: 'ethereum' };
@@ -23,16 +23,18 @@ const SOURCES = [
23
23
  },
24
24
  ];
25
25
 
26
- /** @returns {Promise<Record<string, number> | null>} 链 key → 美元价;全部取不到时 null */
27
- export async function fetchLivePrices({ fetchImpl = (...a) => fetch(...a), timeoutMs = 8000 } = {}) {
26
+ /**
27
+ * 各来源并行请求、按优先级合并(经代理时单个接口常要 6–7 秒,依次请求容易全部超时)
28
+ * @returns {Promise<Record<string, number> | null>} 链 key → 美元价;全部取不到时 null
29
+ */
30
+ export async function fetchLivePrices({ fetchImpl = (...a) => fetch(...a), timeoutMs = 15000 } = {}) {
28
31
  const get = (url) => fetchImpl(url, { signal: AbortSignal.timeout ? AbortSignal.timeout(timeoutMs) : undefined }).then((r) => {
29
32
  if (!r.ok) throw new Error('http ' + r.status);
30
33
  return r.json();
31
34
  });
35
+ const results = await Promise.all(SOURCES.map((src) => src(get).catch(() => ({}))));
32
36
  const m = {};
33
- for (const src of SOURCES) {
34
- if (Object.keys(SYMBOLS).every((k) => m[k])) break;
35
- const got = await src(get).catch(() => ({}));
37
+ for (const got of results) {
36
38
  for (const [k, v] of Object.entries(got)) { const n = Number(v); if (!m[k] && n > 0) m[k] = n; }
37
39
  }
38
40
  return Object.keys(m).length ? m : null;