更新新版本获取地址

This commit is contained in:
lyswhut
2023-08-30 13:01:27 +08:00
parent 903ca18b75
commit 358142c1cc
8 changed files with 92 additions and 40 deletions
+56 -35
View File
@@ -1,50 +1,71 @@
import { httpGet } from './request'
import { name, author } from '../../../package.json'
import pkg from '../../../package.json'
// TODO add Notice
export const getVersionInfo = async(retryNum = 0) => {
const author = pkg.author.name
const name = pkg.name
const address = [
[`https://raw.githubusercontent.com/${author}/${name}/master/publish/version.json`, 'direct'],
['https://registry.npmjs.org/lx-music-desktop-version-info/latest', 'npm'],
['https://registry.npmmirror.com/lx-music-desktop-version-info/latest', 'npm'],
[`https://cdn.jsdelivr.net/gh/${author}/${name}/publish/version.json`, 'direct'],
[`https://fastly.jsdelivr.net/gh/${author}/${name}/publish/version.json`, 'direct'],
[`https://gcore.jsdelivr.net/gh/${author}/${name}/publish/version.json`, 'direct'],
['https://gitee.com/lyswhut/lx-music-desktop-versions/raw/master/version.json', 'direct'],
['http://cdn.stsky.cn/lx-music/desktop/version.json', 'direct'],
]
const request = async(url, retryNum = 0) => {
return await new Promise((resolve, reject) => {
httpGet(`https://raw.githubusercontent.com/${author.name}/${name}/master/publish/version.json`, {
timeout: 20000,
httpGet(url, {
timeout: 10000,
}, (err, resp, body) => {
if (err) {
return ++retryNum > 3
? getVersionInfo2().then(resolve).catch(reject)
: getVersionInfo(retryNum).then(resolve).catch(reject)
}
resolve(body)
if (err || resp.statusCode != 200) {
++retryNum >= 3
? reject(err || new Error(resp.statusMessage || resp.statusCode))
: request(url, retryNum).then(resolve).catch(reject)
} else resolve(body)
})
})
}
const getVersionInfo2 = async(retryNum = 0) => {
return await new Promise((resolve, reject) => {
httpGet('https://gitee.com/lyswhut/lx-music-desktop-versions/raw/master/version.json', {
timeout: 20000,
}, (err, resp, body) => {
if (!err && !body.version) err = new Error(JSON.stringify(body))
if (err) {
return ++retryNum > 3
? getVersionInfo3().then(resolve).catch(reject)
: getVersionInfo2(retryNum).then(resolve).catch(reject)
}
resolve(body)
})
const getDirectInfo = async(url) => {
return request(url).then(info => {
if (info.version == null) throw new Error('failed')
return info
})
}
const getVersionInfo3 = async(retryNum = 0) => {
return await new Promise((resolve, reject) => {
httpGet('https://cdn.stsky.cn/lx-music/desktop/version.json', {
timeout: 20000,
}, (err, resp, body) => {
if (err) {
return ++retryNum > 3
? reject(err)
: getVersionInfo3(retryNum).then(resolve).catch(reject)
}
resolve(body)
})
const getNpmPkgInfo = async(url) => {
return request(url).then(json => {
if (!json.versionInfo) throw new Error('failed')
const info = JSON.parse(json.versionInfo)
if (info.version == null) throw new Error('failed')
return info
})
}
export const getVersionInfo = async(index = 0) => {
const [url, source] = address[index]
let promise
switch (source) {
case 'direct':
promise = getDirectInfo(url)
break
case 'npm':
promise = getNpmPkgInfo(url)
break
}
return promise.catch(async(err) => {
index++
if (index >= address.length) throw err
return getVersionInfo(index)
})
}
// getVersionInfo().then(info => {
// console.log(info)
// })