diff options
-rw-r--r-- | build/hypixelCached.js | 8 | ||||
-rw-r--r-- | build/index.js | 8 | ||||
-rw-r--r-- | package-lock.json | 11 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/constants.ts | 6 | ||||
-rw-r--r-- | src/hypixelCached.ts | 20 | ||||
-rw-r--r-- | src/index.ts | 31 |
7 files changed, 51 insertions, 35 deletions
diff --git a/build/hypixelCached.js b/build/hypixelCached.js index 56671ab..8018dc4 100644 --- a/build/hypixelCached.js +++ b/build/hypixelCached.js @@ -7,7 +7,7 @@ import * as mojang from './mojang.js'; import NodeCache from 'node-cache'; import { debug } from './index.js'; import LRUCache from 'lru-cache'; -import { TLSSocket } from 'tls'; +// import { TLSSocket } from 'tls' // cache usernames for 30 minutes /** uuid: username */ export const usernameCache = new NodeCache({ @@ -16,9 +16,9 @@ export const usernameCache = new NodeCache({ checkperiod: 60, useClones: false, }); -usernameCache.setMaxListeners(50); -// @ts-ignore for some reason the typings don't have setMaxListeners but it does -TLSSocket.setMaxListeners(50); +// usernameCache.setMaxListeners(50) +// // @ts-ignore for some reason the typings don't have setMaxListeners but it does +// TLSSocket.setMaxListeners(50) export const basicProfilesCache = new NodeCache({ stdTTL: 60 * 10, checkperiod: 60, diff --git a/build/index.js b/build/index.js index 8521a1c..050e22a 100644 --- a/build/index.js +++ b/build/index.js @@ -4,6 +4,7 @@ import rateLimit from 'express-rate-limit'; import * as constants from './constants.js'; import * as discord from './discord.js'; import express from 'express'; +import { basicPlayerCache, basicProfilesCache, playerCache, profileCache, profileNameCache, profilesCache, usernameCache } from './hypixelCached.js'; const app = express(); export const debug = false; const mainSiteUrl = 'https://skyblock.matdoes.dev'; @@ -33,6 +34,13 @@ app.get('/', async (req, res) => { finishedCachingRawLeaderboards, leaderboardUpdateMemberQueueSize: leaderboardUpdateMemberQueue.size, leaderboardUpdateProfileQueueSize: leaderboardUpdateProfileQueue.size, + usernameCacheSize: usernameCache.keys().length, + basicProfilesCacheSize: basicProfilesCache.keys().length, + playerCacheSize: playerCache.keys().length, + basicPlayerCacheSize: basicPlayerCache.keys().length, + profileCacheSize: profileCache.keys().length, + profilesCacheSize: profilesCache.keys().length, + profileNameCacheSize: profileNameCache.keys().length, // key: getKeyUsage() }); }); diff --git a/package-lock.json b/package-lock.json index 56bbb6e..4a371f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "lru-cache": "^6.0.0", "mongodb": "^4.1.2", "node-cache": "^5.1.2", - "node-fetch": "^3.0.0", + "node-fetch": "github:tekwiz/node-fetch#fix/chunked-encoding-keepalive", "prismarine-nbt": "^1.6.0", "queue-promise": "^2.2.1", "uuid": "^8.3.2" @@ -1387,8 +1387,8 @@ }, "node_modules/node-fetch": { "version": "3.0.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz", - "integrity": "sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==", + "resolved": "git+ssh://git@github.com/tekwiz/node-fetch.git#1c43dd773db587a064083d15fedf9d1d65c261c7", + "license": "MIT", "dependencies": { "data-uri-to-buffer": "^3.0.1", "fetch-blob": "^3.1.2" @@ -3194,9 +3194,8 @@ } }, "node-fetch": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz", - "integrity": "sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==", + "version": "git+ssh://git@github.com/tekwiz/node-fetch.git#1c43dd773db587a064083d15fedf9d1d65c261c7", + "from": "node-fetch@https://github.com/tekwiz/node-fetch#fix/chunked-encoding-keepalive", "requires": { "data-uri-to-buffer": "^3.0.1", "fetch-blob": "^3.1.2" diff --git a/package.json b/package.json index 992aeaf..b0e5bf2 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "lru-cache": "^6.0.0", "mongodb": "^4.1.2", "node-cache": "^5.1.2", - "node-fetch": "^3.0.0", + "node-fetch": "github:tekwiz/node-fetch#fix/chunked-encoding-keepalive", "prismarine-nbt": "^1.6.0", "queue-promise": "^2.2.1", "uuid": "^8.3.2" diff --git a/src/constants.ts b/src/constants.ts index 13fc21d..3cc77d2 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -76,7 +76,7 @@ const fileCache = new NodeCache({ */ function fetchFile(path: string): Promise<GithubFile> { return new Promise(resolve => { - queue.enqueue(async() => { + queue.enqueue(async () => { if (fileCache.has(path)) return resolve(fileCache.get(path)!) @@ -137,7 +137,7 @@ export let fetchJSONConstant = async function fetchJSONConstant(filename: string } /** Add stats to skyblock-constants. This has caching so it's fine to call many times */ -export let addJSONConstants = async function addJSONConstants(filename: string, addingValues: string[], unit: string='stat'): Promise<void> { +export let addJSONConstants = async function addJSONConstants(filename: string, addingValues: string[], unit: string = 'stat'): Promise<void> { if (addingValues.length === 0) return // no stats provided, just return let file: GithubFile = await fetchFile(filename) @@ -268,7 +268,7 @@ export async function setConstantValues(newValues: constantValues) { const commitMessage = 'Update values' try { await editFile(file, commitMessage, JSON.stringify(updatedStats, null, 2)) - } catch {} + } catch { } } diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts index 07566f9..a80f4d2 100644 --- a/src/hypixelCached.ts +++ b/src/hypixelCached.ts @@ -10,7 +10,7 @@ import * as mojang from './mojang.js' import NodeCache from 'node-cache' import { debug } from './index.js' import LRUCache from 'lru-cache' -import { TLSSocket } from 'tls' +// import { TLSSocket } from 'tls' // cache usernames for 30 minutes /** uuid: username */ @@ -22,9 +22,9 @@ export const usernameCache = new NodeCache({ useClones: false, }) -usernameCache.setMaxListeners(50) -// @ts-ignore for some reason the typings don't have setMaxListeners but it does -TLSSocket.setMaxListeners(50) +// usernameCache.setMaxListeners(50) +// // @ts-ignore for some reason the typings don't have setMaxListeners but it does +// TLSSocket.setMaxListeners(50) export const basicProfilesCache = new NodeCache({ @@ -107,8 +107,8 @@ export async function uuidFromUser(user: string): Promise<string | undefined> { } // check if the username is a value - const uuidToUsername: { [ key: string ]: string | Promise<KeyValue> } = usernameCache.mget(usernameCache.keys()) - for (const [ uuid, username ] of Object.entries(uuidToUsername)) { + const uuidToUsername: { [key: string]: string | Promise<KeyValue> } = usernameCache.mget(usernameCache.keys()) + for (const [uuid, username] of Object.entries(uuidToUsername)) { if (username && (<string>username).toLowerCase && user.toLowerCase() === (<string>username).toLowerCase()) return uuid } @@ -119,7 +119,7 @@ export async function uuidFromUser(user: string): Promise<string | undefined> { // set it as waitForCacheSet (a promise) in case uuidFromUser gets called while its fetching mojang usernameCache.set(undashedUser, waitForCacheSet(usernameCache, user, user)) - + // not cached, actually fetch mojang api now let { uuid, username } = await mojang.profileFromUser(user) if (!uuid) { @@ -186,7 +186,7 @@ export async function fetchPlayer(user: string): Promise<CleanPlayer | null> { // clone in case it gets modified somehow later playerCache.set(playerUuid, cleanPlayer) usernameCache.set(playerUuid, cleanPlayer.username) - + const cleanBasicPlayer = Object.assign({}, cleanPlayer) delete cleanBasicPlayer.profiles basicPlayerCache.set(playerUuid, cleanBasicPlayer) @@ -202,7 +202,7 @@ export async function fetchBasicPlayer(user: string): Promise<CleanPlayer | null if (basicPlayerCache.has(playerUuid)) return basicPlayerCache.get(playerUuid)! - + const player = await fetchPlayer(playerUuid) if (!player) { console.debug('no player? this should never happen, perhaps the uuid is invalid or the player hasn\'t played hypixel', playerUuid) @@ -390,7 +390,7 @@ export async function fetchProfileName(user: string, profile: string): Promise<s if (debug) console.debug('Cache miss: fetchProfileName', user, profile) const basicProfiles = await fetchBasicProfiles(playerUuid) - + if (!basicProfiles) return null let profileName: string | null = null diff --git a/src/index.ts b/src/index.ts index 1327771..d0e4fd8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ import * as constants from './constants.js' import * as discord from './discord.js' import express from 'express' import { getKeyUsage } from './hypixelApi.js' +import { basicPlayerCache, basicProfilesCache, playerCache, profileCache, profileNameCache, profilesCache, usernameCache } from './hypixelCached.js' const app = express() @@ -32,7 +33,7 @@ app.use((req, res, next) => { }) const startTime = Date.now() -app.get('/', async(req, res) => { +app.get('/', async (req, res) => { const currentTime = Date.now() res.json({ ok: true, @@ -40,11 +41,19 @@ app.get('/', async(req, res) => { finishedCachingRawLeaderboards, leaderboardUpdateMemberQueueSize: leaderboardUpdateMemberQueue.size, leaderboardUpdateProfileQueueSize: leaderboardUpdateProfileQueue.size, + + usernameCacheSize: usernameCache.keys().length, + basicProfilesCacheSize: basicProfilesCache.keys().length, + playerCacheSize: playerCache.keys().length, + basicPlayerCacheSize: basicPlayerCache.keys().length, + profileCacheSize: profileCache.keys().length, + profilesCacheSize: profilesCache.keys().length, + profileNameCacheSize: profileNameCache.keys().length, // key: getKeyUsage() }) }) -app.get('/player/:user', async(req, res) => { +app.get('/player/:user', async (req, res) => { try { const user = await fetchUser( { user: req.params.user }, @@ -61,7 +70,7 @@ app.get('/player/:user', async(req, res) => { } }) -app.get('/discord/:id', async(req, res) => { +app.get('/discord/:id', async (req, res) => { try { res.json( await fetchAccountFromDiscord(req.params.id) @@ -72,7 +81,7 @@ app.get('/discord/:id', async(req, res) => { } }) -app.get('/player/:user/:profile', async(req, res) => { +app.get('/player/:user/:profile', async (req, res) => { try { const profile = await fetchMemberProfile(req.params.user, req.params.profile, req.query.customization as string === 'true') if (profile) @@ -85,7 +94,7 @@ app.get('/player/:user/:profile', async(req, res) => { } }) -app.get('/player/:user/:profile/leaderboards', async(req, res) => { +app.get('/player/:user/:profile/leaderboards', async (req, res) => { try { res.json( await fetchMemberLeaderboardSpots(req.params.user, req.params.profile) @@ -96,7 +105,7 @@ app.get('/player/:user/:profile/leaderboards', async(req, res) => { } }) -app.get('/leaderboard/:name', async(req, res) => { +app.get('/leaderboard/:name', async (req, res) => { try { res.json( await fetchLeaderboard(req.params.name) @@ -107,7 +116,7 @@ app.get('/leaderboard/:name', async(req, res) => { } }) -app.get('/leaderboards', async(req, res) => { +app.get('/leaderboards', async (req, res) => { try { res.json( await fetchAllLeaderboardsCategorized() @@ -118,7 +127,7 @@ app.get('/leaderboards', async(req, res) => { } }) -app.get('/constants', async(req, res) => { +app.get('/constants', async (req, res) => { try { res.json( await constants.fetchConstantValues() @@ -129,7 +138,7 @@ app.get('/constants', async(req, res) => { } }) -app.post('/accounts/createsession', async(req, res) => { +app.post('/accounts/createsession', async (req, res) => { try { const { code } = req.body const codeExchange = await discord.exchangeCode(`${mainSiteUrl}/loggedin`, code) @@ -149,7 +158,7 @@ app.post('/accounts/createsession', async(req, res) => { } }) -app.post('/accounts/session', async(req, res) => { +app.post('/accounts/session', async (req, res) => { try { const { uuid } = req.body const session = await fetchSession(uuid) @@ -164,7 +173,7 @@ app.post('/accounts/session', async(req, res) => { }) -app.post('/accounts/update', async(req, res) => { +app.post('/accounts/update', async (req, res) => { // it checks against the key, so it's kind of secure if (req.headers.key !== process.env.key) return console.log('bad key!') try { |