fix(api): 引入 undici ProxyAgent,解决国内直连币安 API 的 Timeout 封锁问题

This commit is contained in:
kennethcheng 2026-04-28 13:46:14 +08:00
parent 48e834e338
commit f1d1b58ed9
3 changed files with 16 additions and 1 deletions

10
package-lock.json generated
View File

@ -29,6 +29,7 @@
"recharts": "^3.8.1", "recharts": "^3.8.1",
"tailwind-merge": "^3.5.0", "tailwind-merge": "^3.5.0",
"tailwindcss-animate": "^1.0.7", "tailwindcss-animate": "^1.0.7",
"undici": "^8.1.0",
"zod": "^4.3.6" "zod": "^4.3.6"
}, },
"devDependencies": { "devDependencies": {
@ -9215,6 +9216,15 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/undici": {
"version": "8.1.0",
"resolved": "https://registry.npmmirror.com/undici/-/undici-8.1.0.tgz",
"integrity": "sha512-E9MkTS4xXLnRPYqxH2e6Hr2/49e7WFDKczKcCaFH4VaZs2iNvHMqeIkyUAD9vM8kujy9TjVrRlQ5KkdEJxB2pw==",
"license": "MIT",
"engines": {
"node": ">=22.19.0"
}
},
"node_modules/undici-types": { "node_modules/undici-types": {
"version": "6.21.0", "version": "6.21.0",
"resolved": "https://registry.npmmirror.com/undici-types/-/undici-types-6.21.0.tgz", "resolved": "https://registry.npmmirror.com/undici-types/-/undici-types-6.21.0.tgz",

View File

@ -33,6 +33,7 @@
"recharts": "^3.8.1", "recharts": "^3.8.1",
"tailwind-merge": "^3.5.0", "tailwind-merge": "^3.5.0",
"tailwindcss-animate": "^1.0.7", "tailwindcss-animate": "^1.0.7",
"undici": "^8.1.0",
"zod": "^4.3.6" "zod": "^4.3.6"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,5 +1,6 @@
'use server'; 'use server';
import { ProxyAgent } from 'undici';
import { db } from '@/db'; import { db } from '@/db';
import { assets } from '@/db/schema'; import { assets } from '@/db/schema';
import { eq } from 'drizzle-orm'; import { eq } from 'drizzle-orm';
@ -22,6 +23,9 @@ export async function syncAllMarketPrices() {
.select() .select()
.from(assets); .from(assets);
const proxyUrl = process.env.HTTPS_PROXY;
const proxyDispatcher = proxyUrl ? new ProxyAgent(proxyUrl) : undefined;
let successCount = 0; let successCount = 0;
for (const asset of allAssets) { for (const asset of allAssets) {
@ -48,7 +52,7 @@ export async function syncAllMarketPrices() {
} }
} else if (asset.type === 'CRYPTO') { } else if (asset.type === 'CRYPTO') {
const cryptoSymbol = asset.symbol.trim().toUpperCase() + 'USDT'; const cryptoSymbol = asset.symbol.trim().toUpperCase() + 'USDT';
const response = await fetch(`https://api.binance.com/api/v3/ticker/price?symbol=${cryptoSymbol}`, { cache: 'no-store' }); const response = await fetch(`https://api.binance.com/api/v3/ticker/price?symbol=${cryptoSymbol}`, { cache: 'no-store', dispatcher: proxyDispatcher } as RequestInit & { dispatcher?: any });
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();