diff --git a/src/config.ts b/src/config.ts index 8013e81..aa8ac5f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -165,7 +165,7 @@ function loadFromEnv(config: SBSConfig, prefix = "") { const fullKey = (prefix ? `${prefix}_` : "") + key; const data = config[key]; - if (typeof data === "object" && !Array.isArray(data)) { + if (data && typeof data === "object" && !Array.isArray(data)) { loadFromEnv(data, fullKey); } else if (process.env[fullKey]) { const value = process.env[fullKey]; diff --git a/src/middleware/requestRateLimit.ts b/src/middleware/requestRateLimit.ts index 06fde41..853738e 100644 --- a/src/middleware/requestRateLimit.ts +++ b/src/middleware/requestRateLimit.ts @@ -6,9 +6,10 @@ import { RateLimitConfig } from "../types/config.model"; import { Request } from "express"; import { isUserVIP } from "../utils/isUserVIP"; import { UserID } from "../types/user.model"; -import RedisStore from "rate-limit-redis"; +import RedisStore, { RedisReply } from "rate-limit-redis"; import redis from "../utils/redis"; import { config } from "../config"; +import { Logger } from "../utils/logger"; export function rateLimitMiddleware(limitConfig: RateLimitConfig, getUserID?: (req: Request) => UserID): RateLimitRequestHandler { return rateLimit({ @@ -29,7 +30,7 @@ export function rateLimitMiddleware(limitConfig: RateLimitConfig, getUserID?: (r } }, store: config.redis?.enabled ? new RedisStore({ - sendCommand: (...args: string[]) => redis.sendCommand(args), + sendCommand: (...args: string[]) => redis.sendCommand(args).catch((err) => Logger.error(err)) as Promise, }) : null, }); } diff --git a/src/middleware/userCounter.ts b/src/middleware/userCounter.ts index a03ad96..958f67e 100644 --- a/src/middleware/userCounter.ts +++ b/src/middleware/userCounter.ts @@ -6,8 +6,10 @@ import { getHash } from "../utils/getHash"; import { NextFunction, Request, Response } from "express"; export function userCounter(req: Request, res: Response, next: NextFunction): void { - axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getHash(getIP(req), 1)}`) - .catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`)); + if (req.method !== "OPTIONS") { + axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getHash(getIP(req), 1)}`) + .catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`)); + } next(); } diff --git a/src/utils/diskCache.ts b/src/utils/diskCache.ts index 78ecd63..c03c750 100644 --- a/src/utils/diskCache.ts +++ b/src/utils/diskCache.ts @@ -31,7 +31,11 @@ class DiskCache { return result.status === 200 ? result.data : null; } catch (err) { - Logger.error(`DiskCache: Error getting key ${key}: ${err}`); + const response = (err as AxiosError).response; + if (!response || response.status !== 404) { + Logger.error(`DiskCache: Error getting key ${key}: ${err}`); + } + return null; } } diff --git a/src/utils/getHashCache.ts b/src/utils/getHashCache.ts index f801545..b3d0d7b 100644 --- a/src/utils/getHashCache.ts +++ b/src/utils/getHashCache.ts @@ -30,7 +30,7 @@ async function getFromRedis(key: HashedValue): Promise Logger.error(err)); return data as T & HashedValue; } \ No newline at end of file diff --git a/src/utils/queryCacher.ts b/src/utils/queryCacher.ts index 028c4e7..434d898 100644 --- a/src/utils/queryCacher.ts +++ b/src/utils/queryCacher.ts @@ -16,7 +16,7 @@ async function get(fetchFromDB: () => Promise, key: string): Promise { const data = await fetchFromDB(); - redis.set(key, JSON.stringify(data)); + redis.set(key, JSON.stringify(data)).catch((err) => Logger.error(err)); return data; } @@ -67,7 +67,7 @@ async function getAndSplit(fetchFromDB: (values: U[]) => Pr } for (const key in newResults) { - redis.set(key, JSON.stringify(newResults[key])); + redis.set(key, JSON.stringify(newResults[key])).catch((err) => Logger.error(err)); } }); } @@ -77,16 +77,16 @@ async function getAndSplit(fetchFromDB: (values: U[]) => Pr function clearSegmentCache(videoInfo: { videoID: VideoID; hashedVideoID: VideoIDHash; service: Service; userID?: UserID; }): void { if (videoInfo) { - redis.del(skipSegmentsKey(videoInfo.videoID, videoInfo.service)); - redis.del(skipSegmentGroupsKey(videoInfo.videoID, videoInfo.service)); - redis.del(skipSegmentsHashKey(videoInfo.hashedVideoID, videoInfo.service)); - if (videoInfo.userID) redis.del(reputationKey(videoInfo.userID)); + redis.del(skipSegmentsKey(videoInfo.videoID, videoInfo.service)).catch((err) => Logger.error(err)); + redis.del(skipSegmentGroupsKey(videoInfo.videoID, videoInfo.service)).catch((err) => Logger.error(err)); + redis.del(skipSegmentsHashKey(videoInfo.hashedVideoID, videoInfo.service)).catch((err) => Logger.error(err)); + if (videoInfo.userID) redis.del(reputationKey(videoInfo.userID)).catch((err) => Logger.error(err)); } } function clearRatingCache(videoInfo: { hashedVideoID: VideoIDHash; service: Service;}): void { if (videoInfo) { - redis.del(ratingHashKey(videoInfo.hashedVideoID, videoInfo.service)); + redis.del(ratingHashKey(videoInfo.hashedVideoID, videoInfo.service)).catch((err) => Logger.error(err)); } } diff --git a/src/utils/redis.ts b/src/utils/redis.ts index e1bc2e9..7627d0f 100644 --- a/src/utils/redis.ts +++ b/src/utils/redis.ts @@ -31,7 +31,7 @@ if (config.redis?.enabled) { client.connect(); exportClient = client; - const timeoutDuration = 200; + const timeoutDuration = 40; const get = client.get.bind(client); exportClient.get = (key) => new Promise((resolve, reject) => { const timeout = setTimeout(() => reject(), timeoutDuration);