Skip to content

Commit

Permalink
ref: small refactor?
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Mar 4, 2024
1 parent df9c460 commit 53cc0a1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as meter from "./meter.js";
import { settings } from "./settings.js";
import { logInfo } from "./utils.js";

type Module = {
init: () => unknown | Promise<unknown>,
name: string,
type JAPIModule = {
init: () => unknown | Promise<unknown>,
name: string,
};

async function init() {
Expand All @@ -27,7 +27,7 @@ async function init() {

/** Initialize JokeAPI's modules */
async function initModules() {
const modules: Module[] = [
const modules: JAPIModule[] = [
lists,
auth,
server,
Expand Down
18 changes: 6 additions & 12 deletions src/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,18 @@ export async function init() {
}

/** Checks if an IP hash is on the given list */
export function isOnList(ipHash: string, type: ListType) {
return lists![type].find(i => i === ipHash) !== undefined;
export function isOnList(ipHash: string, listTypes: ListType) {
return Boolean(lists![listTypes].find(i => i === ipHash));
}

/** Checks if an IP hash is on any of the given lists */
export function isOnAnyOfLists(ipHash: string, types: ListType[]) {
for(const type of types)
if(lists![type].find(i => i === ipHash))
return true;
return false;
export function isOnAnyOfLists(ipHash: string, listTypes: ListType[]) {
return listTypes.some(type => lists![type].find(i => i === ipHash));
}

/** Checks if an IP hash is on all of the given lists */
export function isOnMultipleLists(ipHash: string, types: ListType[]) {
for(const type of types)
if(!lists![type].find(i => i === ipHash))
return false;
return true;
export function isOnMultipleLists(ipHash: string, listTypes: ListType[]) {
return listTypes.every(type => lists![type].find(i => i === ipHash));
}

/** Loads all lists from the corresponding JSON files and returns them */
Expand Down
31 changes: 16 additions & 15 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export async function init() {
return next();
});

// TODO: Add support for other rate limiters
const addRateLimitHeaders = (res: Response, rlRes: RateLimiterRes) => {
res.setHeader("X-RateLimit-Limit", settings.server.rateLimit.points);
res.setHeader("X-RateLimit-Limit", settings.server.rateLimit.generic.points);
res.setHeader("X-RateLimit-Remaining", rlRes.remainingPoints);
res.setHeader("Retry-After", Math.ceil(rlRes.msBeforeNext / 1000));
};
Expand All @@ -59,20 +60,20 @@ export async function init() {
const ip = clientIp ? hashIp(clientIp) : null;

if(ip) {
genericRateLimit.consume(ip)
.catch((err) => {
if(err instanceof RateLimiterRes) {
addRateLimitHeaders(res, err);
return respond(res, { message: "You are being rate limited" }, 429, format);
}
else
return respond(res, { message: "Internal error in rate limiting middleware. Please try again later." }, 500, format);
})
.then((rlRes) => {
if(rlRes instanceof RateLimiterRes)
addRateLimitHeaders(res, rlRes);
})
.finally(next);
try {
const rlRes = await genericRateLimit.consume(ip);
rlRes instanceof RateLimiterRes && addRateLimitHeaders(res, rlRes);

return next();
}
catch(err) {
if(err instanceof RateLimiterRes) {
addRateLimitHeaders(res, err);
return respond(res, { message: "You are being rate limited" }, 429, format);
}
else
return respond(res, { message: "Internal error in rate limiting middleware. Please try again later." }, 500, format);
}
}
else
return next();
Expand Down

0 comments on commit 53cc0a1

Please sign in to comment.