fix: app cache and other issues (#5460)
* fixes * #[serde(untagged)] my BEHATED (still kinda broken) * remove unused hasContent ref * clean up code in fetch instance * ping 3 times for average latency * fix: pinging to be more accurate TCP_NODELAY — Set on the TCP stream right after connect, preventing Nagle's algorithm from buffering the small ping packet (could save up to ~40ms) Instant over Utc::now() — Switched to monotonic std::time::Instant for timing, which is more precise and designed for measuring elapsed time (still using chrono just for the ping magic value) * delete useFetch util and just use native fetch * rename worlds until functions for more clarity * fix lint * fix cache.rs logic * make backend ping use both impls * Add optional timeout to server ping * fix gallery appearing in nav with no items * remove EU countries and add EU option for server country * add uk to europe --------- Co-authored-by: aecsocket <aecsocket@tutanota.com>
This commit is contained in:
@@ -85,7 +85,6 @@ import { debugAnalytics, initAnalytics, trackEvent } from '@/helpers/analytics'
|
||||
import { check_reachable } from '@/helpers/auth.js'
|
||||
import { get_user } from '@/helpers/cache.js'
|
||||
import { command_listener, warning_listener } from '@/helpers/events.js'
|
||||
import { useFetch } from '@/helpers/fetch.js'
|
||||
import { cancelLogin, get as getCreds, login, logout } from '@/helpers/mr_auth.ts'
|
||||
import { list } from '@/helpers/profile.js'
|
||||
import { get as getSettings, set as setSettings } from '@/helpers/settings.ts'
|
||||
@@ -303,11 +302,7 @@ async function setupApp() {
|
||||
}),
|
||||
)
|
||||
|
||||
useFetch(
|
||||
`https://api.modrinth.com/appCriticalAnnouncement.json?version=${version}`,
|
||||
'criticalAnnouncements',
|
||||
true,
|
||||
)
|
||||
fetch(`https://api.modrinth.com/appCriticalAnnouncement.json?version=${version}`)
|
||||
.then((response) => response.json())
|
||||
.then((res) => {
|
||||
if (res && res.header && res.body) {
|
||||
@@ -320,23 +315,21 @@ async function setupApp() {
|
||||
)
|
||||
})
|
||||
|
||||
useFetch(`https://modrinth.com/news/feed/articles.json`, 'news', true)
|
||||
fetch(`https://modrinth.com/news/feed/articles.json`)
|
||||
.then((response) => response.json())
|
||||
.then((res) => {
|
||||
if (res && res.articles) {
|
||||
// Format expected by NewsArticleCard component.
|
||||
news.value = res.articles
|
||||
.map((article) => ({
|
||||
...article,
|
||||
path: article.link,
|
||||
thumbnail: article.thumbnail,
|
||||
title: article.title,
|
||||
summary: article.summary,
|
||||
date: article.date,
|
||||
}))
|
||||
.slice(0, 4)
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Failed to fetch news articles', error)
|
||||
})
|
||||
|
||||
get_opening_command().then(handleCommand)
|
||||
fetchCredentials()
|
||||
|
||||
@@ -33,6 +33,7 @@ async function purgeCache() {
|
||||
'user',
|
||||
'team',
|
||||
'organization',
|
||||
'file',
|
||||
'loader_manifest',
|
||||
'minecraft_manifest',
|
||||
'categories',
|
||||
@@ -40,8 +41,10 @@ async function purgeCache() {
|
||||
'loaders',
|
||||
'game_versions',
|
||||
'donation_platforms',
|
||||
'file_hash',
|
||||
'file_update',
|
||||
'search_results',
|
||||
'search_results_v3',
|
||||
]).catch(handleError)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { getVersion } from '@tauri-apps/api/app'
|
||||
import { fetch } from '@tauri-apps/plugin-http'
|
||||
|
||||
export const useFetch = async (url, item, isSilent) => {
|
||||
try {
|
||||
const version = await getVersion()
|
||||
return await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: { 'User-Agent': `modrinth/theseus/${version} (support@modrinth.com)` },
|
||||
})
|
||||
} catch (err) {
|
||||
if (!isSilent) {
|
||||
throw err
|
||||
} else {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -231,6 +231,25 @@ export function isLinkedWorld(world: World): boolean {
|
||||
return world.type === 'server' && !!world.linked_project_id
|
||||
}
|
||||
|
||||
export async function getServerLatency(
|
||||
address: string,
|
||||
protocolVersion: ProtocolVersion | null = null,
|
||||
): Promise<number | undefined> {
|
||||
const pings: number[] = []
|
||||
for (let i = 0; i < 3; i++) {
|
||||
try {
|
||||
const status = await get_server_status(address, protocolVersion)
|
||||
if (status.ping != null) {
|
||||
pings.push(status.ping)
|
||||
}
|
||||
} catch {
|
||||
// Ignore individual ping failures
|
||||
}
|
||||
}
|
||||
if (pings.length === 0) return undefined
|
||||
return Math.round(pings.reduce((sum, p) => sum + p, 0) / pings.length)
|
||||
}
|
||||
|
||||
export async function refreshServerData(
|
||||
serverData: ServerData,
|
||||
protocolVersion: ProtocolVersion | null,
|
||||
|
||||
@@ -49,7 +49,7 @@ import {
|
||||
} from '@/helpers/profile.js'
|
||||
import { get_categories, get_game_versions, get_loaders } from '@/helpers/tags'
|
||||
import type { GameInstance } from '@/helpers/types'
|
||||
import { get_server_status } from '@/helpers/worlds'
|
||||
import { getServerLatency } from '@/helpers/worlds'
|
||||
import { useBreadcrumbs } from '@/store/breadcrumbs'
|
||||
import { getServerAddress, playServerProject, useInstall } from '@/store/install.js'
|
||||
|
||||
@@ -287,17 +287,18 @@ const {
|
||||
} = useServerSearch({ tags, query, maxResults, currentPage })
|
||||
|
||||
async function pingServerHits(hits: Labrinth.Search.v3.ResultSearchProject[]) {
|
||||
for (const hit of hits) {
|
||||
const address = hit.minecraft_java_server?.address
|
||||
if (!address) continue
|
||||
get_server_status(address)
|
||||
.then((status) => {
|
||||
serverPings.value = { ...serverPings.value, [hit.project_id]: status.ping }
|
||||
})
|
||||
.catch((err) => {
|
||||
const pingsToFetch = hits.filter((hit) => hit.minecraft_java_server?.address)
|
||||
await Promise.all(
|
||||
pingsToFetch.map(async (hit) => {
|
||||
const address = hit.minecraft_java_server!.address!
|
||||
try {
|
||||
const latency = await getServerLatency(address)
|
||||
serverPings.value = { ...serverPings.value, [hit.project_id]: latency }
|
||||
} catch (err) {
|
||||
console.error(`Failed to ping server ${address}:`, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
const previousFilterState = ref('')
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
<ServerPing v-if="ping" :ping="ping" />
|
||||
|
||||
<div
|
||||
v-if="modpackContentProjectV3 && (minecraftServer?.country || ping)"
|
||||
v-if="minecraftServer?.country || ping"
|
||||
class="w-1.5 h-1.5 rounded-full bg-surface-5"
|
||||
></div>
|
||||
|
||||
@@ -312,13 +312,13 @@ import InstanceSettingsModal from '@/components/ui/modal/InstanceSettingsModal.v
|
||||
import UpdateToPlayModal from '@/components/ui/modal/UpdateToPlayModal.vue'
|
||||
import NavTabs from '@/components/ui/NavTabs.vue'
|
||||
import { trackEvent } from '@/helpers/analytics'
|
||||
import { get_project_v3, get_version, get_version_many } from '@/helpers/cache.js'
|
||||
import { get_project_v3, get_version_many } from '@/helpers/cache.js'
|
||||
import { process_listener, profile_listener } from '@/helpers/events'
|
||||
import { get_by_profile_path } from '@/helpers/process'
|
||||
import { finish_install, get, get_full_path, get_projects, kill, run } from '@/helpers/profile'
|
||||
import { finish_install, get, get_full_path, kill, run } from '@/helpers/profile'
|
||||
import type { GameInstance } from '@/helpers/types'
|
||||
import { showProfileInFolder } from '@/helpers/utils.js'
|
||||
import { get_server_status } from '@/helpers/worlds'
|
||||
import { get_server_status, getServerLatency } from '@/helpers/worlds'
|
||||
import { handleSevereError } from '@/store/error.js'
|
||||
import { playServerProject } from '@/store/install.js'
|
||||
import { useBreadcrumbs, useLoading } from '@/store/state'
|
||||
@@ -348,9 +348,7 @@ const exportModal = ref<InstanceType<typeof ExportModal>>()
|
||||
const updateToPlayModal = ref<InstanceType<typeof UpdateToPlayModal>>()
|
||||
|
||||
const isServerInstance = ref(false)
|
||||
const hasContent = ref(true)
|
||||
const linkedProjectV3 = ref<Labrinth.Projects.v3.Project>()
|
||||
const modpackContentProjectV3 = ref<Labrinth.Projects.v3.Project | null>(null)
|
||||
const selected = ref<unknown[]>([])
|
||||
|
||||
const minecraftServer = computed(() => linkedProjectV3.value?.minecraft_server)
|
||||
@@ -362,9 +360,7 @@ const ping = ref<number | undefined>(undefined)
|
||||
async function fetchInstance() {
|
||||
isServerInstance.value = false
|
||||
linkedProjectV3.value = undefined
|
||||
modpackContentProjectV3.value = null
|
||||
modrinthVersions.value = []
|
||||
hasContent.value = true
|
||||
ping.value = undefined
|
||||
|
||||
instance.value = await get(route.params.id as string).catch(handleError)
|
||||
@@ -382,48 +378,31 @@ async function fetchInstance() {
|
||||
(a: Labrinth.Versions.v2.Version, b: Labrinth.Versions.v2.Version) =>
|
||||
dayjs(b.date_published).valueOf() - dayjs(a.date_published).valueOf(),
|
||||
)
|
||||
if (linkedProjectV3.value?.minecraft_server != null) {
|
||||
isServerInstance.value = true
|
||||
}
|
||||
|
||||
const serverAddress = linkedProjectV3.value?.minecraft_java_server?.address
|
||||
if (serverAddress) {
|
||||
get_server_status(serverAddress)
|
||||
.then((status) => {
|
||||
if (status.ping != null) {
|
||||
ping.value = status.ping
|
||||
playersOnline.value = status.players?.online
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`Failed to ping server ${serverAddress}:`, err)
|
||||
})
|
||||
if (linkedProjectV3.value?.minecraft_server != null) {
|
||||
isServerInstance.value = true
|
||||
|
||||
const serverAddress = linkedProjectV3.value?.minecraft_java_server?.address
|
||||
if (serverAddress) {
|
||||
try {
|
||||
const status = await get_server_status(serverAddress)
|
||||
const latency = await getServerLatency(serverAddress)
|
||||
ping.value = latency
|
||||
playersOnline.value = status.players?.online
|
||||
} catch (err) {
|
||||
console.error(`Failed to ping server ${serverAddress}:`, err)
|
||||
}
|
||||
|
||||
await fetchModpackContent()
|
||||
const projects = await get_projects(instance.value!.path).catch(() => ({}))
|
||||
hasContent.value = Object.keys(projects).length > 0
|
||||
}
|
||||
}
|
||||
} catch (error: Error) {
|
||||
handleError(error)
|
||||
} catch (error) {
|
||||
handleError(error as Error)
|
||||
}
|
||||
}
|
||||
|
||||
await updatePlayState()
|
||||
}
|
||||
|
||||
async function fetchModpackContent() {
|
||||
modpackContentProjectV3.value = null
|
||||
const versionId = instance.value?.linked_data?.version_id
|
||||
if (!versionId) return
|
||||
|
||||
const contentVersion = await get_version(versionId, 'must_revalidate')
|
||||
const projectId = contentVersion?.project_id
|
||||
if (projectId) {
|
||||
modpackContentProjectV3.value = await get_project_v3(projectId, 'must_revalidate')
|
||||
}
|
||||
}
|
||||
|
||||
async function updatePlayState() {
|
||||
const runningProcesses = await get_by_profile_path(route.params.id as string).catch(handleError)
|
||||
|
||||
|
||||
@@ -265,7 +265,7 @@ import {
|
||||
list as listInstances,
|
||||
} from '@/helpers/profile'
|
||||
import { get_categories, get_game_versions, get_loaders } from '@/helpers/tags'
|
||||
import { get_server_status } from '@/helpers/worlds'
|
||||
import { getServerLatency } from '@/helpers/worlds'
|
||||
import { useBreadcrumbs } from '@/store/breadcrumbs'
|
||||
import {
|
||||
getServerAddress,
|
||||
@@ -398,15 +398,11 @@ async function fetchProjectData() {
|
||||
serverStatusOnline.value = !!projectV3.value?.minecraft_java_server?.ping?.data
|
||||
if (serverAddress) {
|
||||
serverPing.value = undefined
|
||||
get_server_status(serverAddress)
|
||||
.then((status) => {
|
||||
if (status.ping != null) {
|
||||
serverPing.value = status.ping
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`Failed to ping server ${serverAddress}:`, err)
|
||||
})
|
||||
try {
|
||||
serverPing.value = await getServerLatency(serverAddress)
|
||||
} catch (error) {
|
||||
console.error(`Failed to ping server ${serverAddress}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch server sidebar data (modpack version + project)
|
||||
|
||||
@@ -328,7 +328,7 @@ export const installServerProject = async (serverProjectId) => {
|
||||
})
|
||||
await edit_icon(profilePath, originalIconPath)
|
||||
|
||||
await syncServerAsWorld(profilePath, project.title, serverAddress, serverProjectId)
|
||||
await syncServerProjectAsWorld(profilePath, project.title, serverAddress, serverProjectId)
|
||||
}
|
||||
|
||||
export const getServerAddress = (javaServer) => {
|
||||
@@ -337,7 +337,7 @@ export const getServerAddress = (javaServer) => {
|
||||
return port !== 25565 ? `${address}:${port}` : address
|
||||
}
|
||||
|
||||
const syncServerAsWorld = async (
|
||||
const syncServerProjectAsWorld = async (
|
||||
profilePath,
|
||||
serverName,
|
||||
serverAddress,
|
||||
@@ -405,7 +405,7 @@ const findInstalledInstance = async (projectId) => {
|
||||
return packs.find((pack) => pack.linked_data?.project_id === projectId) ?? null
|
||||
}
|
||||
|
||||
const createVanillaInstance = async (project, gameVersion, serverAddress) => {
|
||||
const createVanillaServerInstance = async (project, gameVersion, serverAddress) => {
|
||||
const profilePath = await create(
|
||||
project.title,
|
||||
gameVersion,
|
||||
@@ -420,7 +420,8 @@ const createVanillaInstance = async (project, gameVersion, serverAddress) => {
|
||||
},
|
||||
)
|
||||
|
||||
await syncServerAsWorld(profilePath, project.title, serverAddress, project.id)
|
||||
//
|
||||
await syncServerProjectAsWorld(profilePath, project.title, serverAddress, project.id)
|
||||
|
||||
return profilePath
|
||||
}
|
||||
@@ -514,6 +515,7 @@ export const playServerProject = async (projectId) => {
|
||||
|
||||
if (projectV3?.minecraft_server == null) {
|
||||
console.warn('playServerProject failed: project is not a server project')
|
||||
return
|
||||
}
|
||||
|
||||
const content = projectV3?.minecraft_java_server?.content
|
||||
@@ -529,7 +531,7 @@ export const playServerProject = async (projectId) => {
|
||||
if (installStore.installingServerProjects.includes(projectId)) return
|
||||
installStore.startInstallingServer(projectId)
|
||||
try {
|
||||
const path = await createVanillaInstance(project, recommendedGameVersion, serverAddress)
|
||||
const path = await createVanillaServerInstance(project, recommendedGameVersion, serverAddress)
|
||||
if (path) {
|
||||
instance = await get(path)
|
||||
showModpackInstallSuccess(installStore, instance, serverAddress)
|
||||
@@ -543,8 +545,6 @@ export const playServerProject = async (projectId) => {
|
||||
installStore.showInstallToPlayModal(projectV3, modpackVersionId, async () => {
|
||||
const newInstance = await findInstalledInstance(project.id)
|
||||
if (!newInstance) return
|
||||
// Ensure the server is in the worlds list after modpack install
|
||||
await syncServerAsWorld(newInstance.path, project.title, serverAddress, project.id)
|
||||
showModpackInstallSuccess(installStore, newInstance, serverAddress)
|
||||
})
|
||||
return
|
||||
@@ -552,7 +552,7 @@ export const playServerProject = async (projectId) => {
|
||||
|
||||
if (!instance) return
|
||||
|
||||
await syncServerAsWorld(instance.path, project.title, serverAddress, project.id)
|
||||
await syncServerProjectAsWorld(instance.path, project.title, serverAddress, project.id)
|
||||
|
||||
// Update existing instance if needed
|
||||
if (isModpack && instance.linked_data?.version_id !== modpackVersionId) {
|
||||
|
||||
Reference in New Issue
Block a user