aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-05-27 21:10:51 -0500
committermat <github@matdoes.dev>2021-05-27 21:10:51 -0500
commit65d21ebe1ead9faa2e1dc6ccad0d24e990632627 (patch)
tree1f2811547398f23353655792eaf722ccd225a9b5 /src
parentf2f18c27d2e849fdc3b2a6766dc6ab4005cde7df (diff)
downloadskyblock-api-65d21ebe1ead9faa2e1dc6ccad0d24e990632627.tar.gz
skyblock-api-65d21ebe1ead9faa2e1dc6ccad0d24e990632627.tar.bz2
skyblock-api-65d21ebe1ead9faa2e1dc6ccad0d24e990632627.zip
fix some bugs with it loading forever
Diffstat (limited to 'src')
-rw-r--r--src/constants.ts4
-rw-r--r--src/database.ts15
-rw-r--r--src/hypixel.ts3
-rw-r--r--src/hypixelCached.ts26
-rw-r--r--src/index.ts28
5 files changed, 40 insertions, 36 deletions
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<nodeFetch.Response> {
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<void> {
if (debug) console.debug('Caching raw leaderboards!')
- const promises: Promise<DatabaseMemberLeaderboardItem[]>[] = []
-
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<void> {
export async function createSession(refreshToken: string, userData: discord.DiscordUser): Promise<string> {
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<SessionSchema> {
- return await sessionsCollection.findOne({ _id: sessionId })
+ return await sessionsCollection?.findOne({ _id: sessionId })
}
export async function fetchAccount(minecraftUuid: string): Promise<AccountSchema> {
- return await accountsCollection.findOne({ minecraftUuid })
+ return await accountsCollection?.findOne({ minecraftUuid })
}
export async function fetchAccountFromDiscord(discordId: string): Promise<AccountSchema> {
- 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<string> {
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<string> {
// 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<s
else if (undashUuid(p.uuid) === undashUuid(profileUuid))
return undashUuid(p.uuid)
}
+ return null
}
/**
@@ -298,6 +301,8 @@ export async function fetchProfile(user: string, profile: string): Promise<Clean
const playerUuid = await uuidFromUser(user)
const profileUuid = await fetchProfileUuid(playerUuid, profile)
+ if (!profileUuid) return null
+
if (profileCache.has(profileUuid)) {
// we have the profile cached, return it :)
if (debug) console.debug('Cache hit! fetchProfile', profileUuid)
@@ -352,6 +357,8 @@ export async function fetchBasicProfileFromUuid(profileUuid: string): Promise<Cl
export async function fetchProfileName(user: string, profile: string): Promise<string> {
// 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<s
profileNameCache.set(`${playerUuid}.${profileUuid}`, profileName)
return profileName
-}
-
-// setInterval(() => {
- // 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) => {