From 65d21ebe1ead9faa2e1dc6ccad0d24e990632627 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 27 May 2021 21:10:51 -0500 Subject: fix some bugs with it loading forever --- src/constants.ts | 4 +++- src/database.ts | 15 ++++++--------- src/hypixel.ts | 3 ++- src/hypixelCached.ts | 26 ++++++++++---------------- src/index.ts | 28 +++++++++++++++++++--------- 5 files changed, 40 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/constants.ts b/src/constants.ts index 52c104c..48f9619 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -36,7 +36,7 @@ const queue = new Queue({ async function fetchGithubApi(method: string, route: string, headers?: any, json?: any): Promise { try { if (debug) console.debug('fetching github api', method, route) - return await fetch( + const data = await fetch( githubApiBase + route, { agent: () => httpsAgent, @@ -47,6 +47,8 @@ async function fetchGithubApi(method: string, route: string, headers?: any, json }, headers), } ) + if (debug) console.debug('fetched github api', method, route) + return data } catch { // if there's an error, wait a second and try again await new Promise((resolve) => setTimeout(resolve, 1000)) diff --git a/src/database.ts b/src/database.ts index bb3bf3a..535000e 100644 --- a/src/database.ts +++ b/src/database.ts @@ -638,11 +638,8 @@ async function fetchAllLeaderboards(fast?: boolean): Promise { if (debug) console.debug('Caching raw leaderboards!') - const promises: Promise[] = [] - for (const leaderboard of shuffle(leaderboards)) - promises.push(fetchMemberLeaderboardRaw(leaderboard)) - await Promise.all(promises) + await fetchMemberLeaderboardRaw(leaderboard) // shuffle so if the application is restarting many times itll still be useful if (debug) console.debug('Caching leaderboards!') @@ -660,7 +657,7 @@ async function fetchAllLeaderboards(fast?: boolean): Promise { export async function createSession(refreshToken: string, userData: discord.DiscordUser): Promise { const sessionId = uuid4() - await sessionsCollection.insertOne({ + await sessionsCollection?.insertOne({ _id: sessionId, refresh_token: refreshToken, discord_user: { @@ -673,19 +670,19 @@ export async function createSession(refreshToken: string, userData: discord.Disc } export async function fetchSession(sessionId: string): Promise { - return await sessionsCollection.findOne({ _id: sessionId }) + return await sessionsCollection?.findOne({ _id: sessionId }) } export async function fetchAccount(minecraftUuid: string): Promise { - return await accountsCollection.findOne({ minecraftUuid }) + return await accountsCollection?.findOne({ minecraftUuid }) } export async function fetchAccountFromDiscord(discordId: string): Promise { - return await accountsCollection.findOne({ discordId }) + return await accountsCollection?.findOne({ discordId }) } export async function updateAccount(discordId: string, schema: AccountSchema) { - await accountsCollection.updateOne({ + await accountsCollection?.updateOne({ discordId }, { $set: schema }, { upsert: true }) } diff --git a/src/hypixel.ts b/src/hypixel.ts index 99bc671..f8041f5 100644 --- a/src/hypixel.ts +++ b/src/hypixel.ts @@ -122,8 +122,9 @@ export async function fetchUser({ user, uuid, username }: UserAny, included: Inc } let websiteAccount: AccountSchema = undefined - if (websiteAccountPromise) + if (websiteAccountPromise) { websiteAccount = await websiteAccountPromise + } return { player: playerData ?? null, profiles: profilesData ?? basicProfilesData, diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts index 35bfcc2..8bdd81e 100644 --- a/src/hypixelCached.ts +++ b/src/hypixelCached.ts @@ -108,8 +108,10 @@ export async function uuidFromUser(user: string): Promise { if (debug) console.debug('Cache miss: uuidFromUser', user) + const undashedUser = undashUuid(user) + // set it as waitForCacheSet (a promise) in case uuidFromUser gets called while its fetching mojang - usernameCache.set(undashUuid(user), waitForCacheSet(usernameCache, user, user)) + usernameCache.set(undashedUser, waitForCacheSet(usernameCache, user, user)) // not cached, actually fetch mojang api now let { uuid, username } = await mojang.profileFromUser(user) @@ -121,7 +123,7 @@ export async function uuidFromUser(user: string): Promise { // remove dashes from the uuid so its more normal uuid = undashUuid(uuid) - if (user !== uuid) usernameCache.del(user) + usernameCache.del(undashedUser) usernameCache.set(uuid, username) return uuid @@ -287,6 +289,7 @@ export async function fetchProfileUuid(user: string, profile: string): Promise { // we're fetching the profile and player uuid again in case we were given a name, but it's cached so it's not much of a problem const profileUuid = await fetchProfileUuid(user, profile) + if (!profileUuid) + return null const playerUuid = await uuidFromUser(user) if (profileNameCache.has(`${playerUuid}.${profileUuid}`)) { @@ -370,17 +377,4 @@ export async function fetchProfileName(user: string, profile: string): Promise { - // const keys = basicPlayerCache.keys() - // if (keys) - // console.log(basicPlayerCache.get(keys[keys.length - 1])) - // console.log('basicPlayerCache', basicPlayerCache.getStats()) - // console.log('usernameCache', usernameCache.getStats()) - // console.log('profileCache', profileCache.getStats()) - // console.log('cachedRawLeaderboards size', cachedRawLeaderboards.size) - // console.log( - // Math.floor((process.memoryUsage().heapUsed / 1024 / 1024) * 10) / 10 - // + 'mb') -// }, 60 * 1000) \ No newline at end of file +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 176b73f..de3e21f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,13 +35,18 @@ app.get('/', async(req, res) => { }) app.get('/player/:user', async(req, res) => { - res.json( - await fetchUser( - { user: req.params.user }, - [req.query.basic as string === 'true' ? undefined : 'profiles', 'player'], - req.query.customization as string === 'true' + try { + res.json( + await fetchUser( + { user: req.params.user }, + [req.query.basic as string === 'true' ? undefined : 'profiles', 'player'], + req.query.customization as string === 'true' + ) ) - ) + } catch (err) { + console.error(err) + res.json({ 'error': true }) + } }) app.get('/discord/:id', async(req, res) => { @@ -51,9 +56,14 @@ app.get('/discord/:id', async(req, res) => { }) app.get('/player/:user/:profile', async(req, res) => { - res.json( - await fetchMemberProfile(req.params.user, req.params.profile, req.query.customization as string === 'true') - ) + try { + res.json( + await fetchMemberProfile(req.params.user, req.params.profile, req.query.customization as string === 'true') + ) + } catch (err) { + console.error(err) + res.json({ 'error': true }) + } }) app.get('/player/:user/:profile/leaderboards', async(req, res) => { -- cgit