fix some bugs

This commit is contained in:
2026-07-01 22:28:25 +03:30
parent 2feeefc26f
commit e097f6b2bb
24 changed files with 579 additions and 189 deletions
+21 -9
View File
@@ -9,16 +9,24 @@ type VersionResponse =
}
const VERSION_SUCCESS_URL_KEY = 'VERSION_Success_URL'
const VERSION_RETRY_ROUNDS = 5
export const VERSION_RETRY_ROUNDS = 5
let versionFetchPromise: Promise<string | null> | null = null
let cachedVersionResult: string | null | undefined
function getProtocol() {
return window.location.protocol === 'https:' ? 'https:' : 'http:'
}
function getVersionUrls() {
const protocol = getProtocol()
return [
'https://nerkh.gold/get_version',
'https://tablo.talasevin.ir/get_version',
'https://abshode-service.montgit.ir/server/get_version'
'https://abshode-service.montgit.ir/server/get_version',
`${protocol}//212.23.201.173:8381/get_version`,
`${protocol}//91.212.174.137:4040/server/get_version`
]
}
@@ -33,6 +41,12 @@ function sortVersionUrls(urls: string[]) {
return [lastSuccessUrl, ...others]
}
function clearStaleVersionSuccessUrl(url: string) {
if (localStorage.getItem(VERSION_SUCCESS_URL_KEY) === url) {
localStorage.removeItem(VERSION_SUCCESS_URL_KEY)
}
}
function readUserVersion(user: unknown) {
if (!user || typeof user !== 'object' || Array.isArray(user)) {
return ''
@@ -82,7 +96,6 @@ async function fetchVersion(url: string, timeout = 3500): Promise<string | null>
})
if (!response.ok) {
// Non-2xx/timeout should be treated as "request failed" and we may try the next URL.
throw new Error(`Version request failed with status ${response.status}`)
}
@@ -94,10 +107,8 @@ async function fetchVersion(url: string, timeout = 3500): Promise<string | null>
const version = normalizeVersionResponse(data)
// IMPORTANT: if the request returned 200/ok, we stop here even if we couldn't extract a version.
// This prevents unnecessary looping when the endpoint is "reachable" but returns an empty payload.
if (!version) {
return null
throw new Error('Version response is empty')
}
localStorage.setItem(VERSION_SUCCESS_URL_KEY, url)
@@ -118,10 +129,11 @@ async function fetchVersionWithRetry(rounds = VERSION_RETRY_ROUNDS): Promise<str
try {
const version = await fetchVersion(url, timeout)
// fetchVersion only throws on real request failures (non-ok / timeout).
// If it returns null, we got an HTTP 200 but empty body => stop immediately.
return version
if (version) {
return version
}
} catch (error) {
clearStaleVersionSuccessUrl(url)
console.warn(`Version failed from ${url}. Round: ${round}`, error)
}
}