use fastest cdn to fetch data

This commit is contained in:
HolographicHat
2022-04-07 22:50:36 +08:00
parent ea168ce96b
commit dd582437bc
4 changed files with 28 additions and 32 deletions

BIN
2MB.bin Normal file

Binary file not shown.

BIN
65536.bin

Binary file not shown.

View File

@@ -35,7 +35,7 @@
[issues](https://github.com/HolographicHat/genshin-achievement-export/issues)或[QQ群: 913777414](https://qm.qq.com/cgi-bin/qm/qr?k=9UGz-chQVTjZa4b82RA_A41vIcBVNpms&jump_from=webapi) [issues](https://github.com/HolographicHat/genshin-achievement-export/issues)或[QQ群: 913777414](https://qm.qq.com/cgi-bin/qm/qr?k=9UGz-chQVTjZa4b82RA_A41vIcBVNpms&jump_from=webapi)
## 常见问题 ## 常见问题
0. Q: 程序异常退出或被强行终止后,原神启动时报错: 无法连接网络(4201) 0. Q: 程序异常退出或被强行终止后,程序报错:网络错误(22-1) 或 原神启动时报错: 无法连接网络(4201)
A: 用文本编辑器打开```C:\Windows\System32\drivers\etc\hosts```,删除```127.0.0.1 dispatch**global.yuanshen.com```后保存,重启原神 A: 用文本编辑器打开```C:\Windows\System32\drivers\etc\hosts```,删除```127.0.0.1 dispatch**global.yuanshen.com```后保存,重启原神
1. Q: 原神启动时报错: 数据异常(31-4302) 1. Q: 原神启动时报错: 数据异常(31-4302)

View File

@@ -129,7 +129,7 @@ const md5 = str => {
return h.digest("hex") return h.digest("hex")
} }
let cdnUrlFormat = null let cdnUrlFormat = undefined
String.prototype.format = function() { String.prototype.format = function() {
const args = arguments; const args = arguments;
@@ -137,35 +137,31 @@ String.prototype.format = function() {
} }
const checkCDN = async () => { const checkCDN = async () => {
try { const controller = new AbortController()
cdnUrlFormat = "https://cdn.jsdelivr.net/gh/{0}@master/{1}" const a = new Promise(resolve => axios.get("https://cdn.jsdelivr.net/gh/HolographicHat/genshin-achievement-export@master/2MB.bin", {
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore")) signal: controller.signal
return }).then(_ => resolve(["jsdelivr", "https://cdn.jsdelivr.net/gh/{0}@master/{1}"])))
} catch (e) {} const b = new Promise(resolve => axios.get("https://raw.githubusercontent.com/HolographicHat/genshin-achievement-export/master/2MB.bin", {
try { signal: controller.signal
cdnUrlFormat = "https://raw.githubusercontent.com/{0}/master/{1}" }).then(_ => resolve(["githubusercontent", "https://raw.githubusercontent.com/{0}/master/{1}"])))
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore")) const c = new Promise(resolve => axios.get("https://ghproxy.net/https://raw.githubusercontent.com/HolographicHat/genshin-achievement-export/master/2MB.bin", {
return signal: controller.signal
} catch (e) {} }).then(_ => resolve(["ghproxy", "https://ghproxy.net/https://raw.githubusercontent.com/{0}/master/{1}"])))
try { const d = new Promise(resolve => {
const s = conf === undefined ? "" : conf.customCDN.trim() const s = conf === undefined ? "" : conf.customCDN.trim() // 🤔
if (s.length > 0) { if (s.length === 0) throw "empty"
cdnUrlFormat = s axios.get(s.format("HolographicHat/genshin-achievement-export", "2MB.bin"), {
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore")) signal: controller.signal
return }).then(_ => resolve(["customCDN", s]))
} })
} catch (e) {} await Promise.any([a, b, c, d]).then(([name, url]) => {
try { debug(`Fastest: ${name}`)
cdnUrlFormat = "https://raw.fastgit.org/{0}/master/{1}" controller.abort()
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore")) cdnUrlFormat = url
return }).catch()
} catch (e) {} if (cdnUrlFormat === undefined) {
try { throw "网络错误,请检查配置文件/网络后重试 (11-3)"
cdnUrlFormat = "https://ghproxy.net/https://raw.githubusercontent.com/{0}/master/{1}" }
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore"))
return
} catch (e) {}
throw "网络错误,请检查配置文件/网络后重试 (11-3)"
} }
const loadCache = async (fp, repo = "Dimbreath/GenshinData") => { const loadCache = async (fp, repo = "Dimbreath/GenshinData") => {
@@ -203,7 +199,7 @@ const loadCache = async (fp, repo = "Dimbreath/GenshinData") => {
} }
} }
const isDebug = false const isDebug = true
const debug = (msg, ...params) => { const debug = (msg, ...params) => {
if (isDebug) log(msg, ...params) if (isDebug) log(msg, ...params)