diff options
-rw-r--r-- | build/constants.js | 5 | ||||
-rw-r--r-- | build/database.js | 18 | ||||
-rw-r--r-- | build/hypixel.js | 3 | ||||
-rw-r--r-- | build/hypixelCached.js | 23 | ||||
-rw-r--r-- | build/index.js | 16 | ||||
-rw-r--r-- | src/constants.ts | 4 | ||||
-rw-r--r-- | src/database.ts | 15 | ||||
-rw-r--r-- | src/hypixel.ts | 3 | ||||
-rw-r--r-- | src/hypixelCached.ts | 26 | ||||
-rw-r--r-- | src/index.ts | 28 |
10 files changed, 76 insertions, 65 deletions
diff --git a/build/constants.js b/build/constants.js index 3c0c7e9..c620a32 100644 --- a/build/constants.js +++ b/build/constants.js @@ -55,7 +55,7 @@ async function fetchGithubApi(method, route, headers, json) { try { if (_1.debug) console.debug('fetching github api', method, route); - return await node_fetch_1.default(githubApiBase + route, { + const data = await node_fetch_1.default(githubApiBase + route, { agent: () => httpsAgent, body: json ? JSON.stringify(json) : null, method, @@ -63,6 +63,9 @@ async function fetchGithubApi(method, route, headers, json) { 'Authorization': `token ${process.env.github_token}` }, headers), }); + if (_1.debug) + console.debug('fetched github api', method, route); + return data; } catch { // if there's an error, wait a second and try again diff --git a/build/database.js b/build/database.js index 07ed52e..f388c8c 100644 --- a/build/database.js +++ b/build/database.js @@ -506,10 +506,8 @@ async function fetchAllLeaderboards(fast) { const leaderboards = await fetchAllMemberLeaderboardAttributes(); if (_1.debug) console.debug('Caching raw leaderboards!'); - const promises = []; for (const leaderboard of util_1.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 (_1.debug) console.debug('Caching leaderboards!'); @@ -526,7 +524,7 @@ async function fetchAllLeaderboards(fast) { } async function createSession(refreshToken, userData) { const sessionId = uuid_1.v4(); - await sessionsCollection.insertOne({ + await (sessionsCollection === null || sessionsCollection === void 0 ? void 0 : sessionsCollection.insertOne({ _id: sessionId, refresh_token: refreshToken, discord_user: { @@ -534,26 +532,26 @@ async function createSession(refreshToken, userData) { name: userData.username + '#' + userData.discriminator }, lastUpdated: new Date() - }); + })); return sessionId; } exports.createSession = createSession; async function fetchSession(sessionId) { - return await sessionsCollection.findOne({ _id: sessionId }); + return await (sessionsCollection === null || sessionsCollection === void 0 ? void 0 : sessionsCollection.findOne({ _id: sessionId })); } exports.fetchSession = fetchSession; async function fetchAccount(minecraftUuid) { - return await accountsCollection.findOne({ minecraftUuid }); + return await (accountsCollection === null || accountsCollection === void 0 ? void 0 : accountsCollection.findOne({ minecraftUuid })); } exports.fetchAccount = fetchAccount; async function fetchAccountFromDiscord(discordId) { - return await accountsCollection.findOne({ discordId }); + return await (accountsCollection === null || accountsCollection === void 0 ? void 0 : accountsCollection.findOne({ discordId })); } exports.fetchAccountFromDiscord = fetchAccountFromDiscord; async function updateAccount(discordId, schema) { - await accountsCollection.updateOne({ + await (accountsCollection === null || accountsCollection === void 0 ? void 0 : accountsCollection.updateOne({ discordId - }, { $set: schema }, { upsert: true }); + }, { $set: schema }, { upsert: true })); } exports.updateAccount = updateAccount; // make sure it's not in a test diff --git a/build/hypixel.js b/build/hypixel.js index 8e58ffc..b00774d 100644 --- a/build/hypixel.js +++ b/build/hypixel.js @@ -101,8 +101,9 @@ async function fetchUser({ user, uuid, username }, included = ['player'], custom } } let websiteAccount = undefined; - if (websiteAccountPromise) + if (websiteAccountPromise) { websiteAccount = await websiteAccountPromise; + } return { player: playerData !== null && playerData !== void 0 ? playerData : null, profiles: profilesData !== null && profilesData !== void 0 ? profilesData : basicProfilesData, diff --git a/build/hypixelCached.js b/build/hypixelCached.js index b36fd8d..453f3d1 100644 --- a/build/hypixelCached.js +++ b/build/hypixelCached.js @@ -111,8 +111,9 @@ async function uuidFromUser(user) { } if (_1.debug) console.debug('Cache miss: uuidFromUser', user); + const undashedUser = util_1.undashUuid(user); // set it as waitForCacheSet (a promise) in case uuidFromUser gets called while its fetching mojang - exports.usernameCache.set(util_1.undashUuid(user), waitForCacheSet(exports.usernameCache, user, user)); + exports.usernameCache.set(undashedUser, waitForCacheSet(exports.usernameCache, user, user)); // not cached, actually fetch mojang api now let { uuid, username } = await mojang.profileFromUser(user); if (!uuid) { @@ -121,8 +122,7 @@ async function uuidFromUser(user) { } // remove dashes from the uuid so its more normal uuid = util_1.undashUuid(uuid); - if (user !== uuid) - exports.usernameCache.del(user); + exports.usernameCache.del(undashedUser); exports.usernameCache.set(uuid, username); return uuid; } @@ -266,6 +266,7 @@ async function fetchProfileUuid(user, profile) { else if (util_1.undashUuid(p.uuid) === util_1.undashUuid(profileUuid)) return util_1.undashUuid(p.uuid); } + return null; } exports.fetchProfileUuid = fetchProfileUuid; /** @@ -276,6 +277,8 @@ exports.fetchProfileUuid = fetchProfileUuid; async function fetchProfile(user, profile) { const playerUuid = await uuidFromUser(user); const profileUuid = await fetchProfileUuid(playerUuid, profile); + if (!profileUuid) + return null; if (exports.profileCache.has(profileUuid)) { // we have the profile cached, return it :) if (_1.debug) @@ -326,6 +329,8 @@ exports.fetchBasicProfileFromUuid = fetchBasicProfileFromUuid; async function fetchProfileName(user, profile) { // 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 (exports.profileNameCache.has(`${playerUuid}.${profileUuid}`)) { // Return the profile name if it's cached @@ -344,15 +349,3 @@ async function fetchProfileName(user, profile) { return profileName; } exports.fetchProfileName = fetchProfileName; -// 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) diff --git a/build/index.js b/build/index.js index fb2789d..dd6c3b6 100644 --- a/build/index.js +++ b/build/index.js @@ -54,13 +54,25 @@ app.get('/', async (req, res) => { res.json({ ok: true }); }); app.get('/player/:user', async (req, res) => { - res.json(await hypixel_1.fetchUser({ user: req.params.user }, [req.query.basic === 'true' ? undefined : 'profiles', 'player'], req.query.customization === 'true')); + try { + res.json(await hypixel_1.fetchUser({ user: req.params.user }, [req.query.basic === 'true' ? undefined : 'profiles', 'player'], req.query.customization === 'true')); + } + catch (err) { + console.error(err); + res.json({ 'error': true }); + } }); app.get('/discord/:id', async (req, res) => { res.json(await database_1.fetchAccountFromDiscord(req.params.id)); }); app.get('/player/:user/:profile', async (req, res) => { - res.json(await hypixel_1.fetchMemberProfile(req.params.user, req.params.profile, req.query.customization === 'true')); + try { + res.json(await hypixel_1.fetchMemberProfile(req.params.user, req.params.profile, req.query.customization === 'true')); + } + catch (err) { + console.error(err); + res.json({ 'error': true }); + } }); app.get('/player/:user/:profile/leaderboards', async (req, res) => { res.json(await database_1.fetchMemberLeaderboardSpots(req.params.user, req.params.profile)); 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) => { |