mirror of
https://github.com/apotdevin/thunderhub.git
synced 2025-02-22 22:25:21 +01:00
* chore: 🔧 remove client * chore: 🔧 change cookie name * chore: 🔧 remove auth param * chore: 🔧 remove auth components * chore: 🔧 add getaccount query * fix: 🐛 tests * chore: 🔧 get account * chore: 🔧 status check * chore: 🔧 remove log * chore: 🔧 update apollo client * refactor: ♻️ server side props * chore: 🔧 ssr queries * chore: 🔧 more ssr queries * chore: 🔧 type check * chore: 🔧 increase nodeinfo limit Co-authored-by: apotdevin <apotdevincab@gmail.com>
36 lines
907 B
TypeScript
36 lines
907 B
TypeScript
import { getGraphQLRateLimiter } from 'graphql-rate-limit';
|
|
import { logger } from './logger';
|
|
|
|
interface RateConfigProps {
|
|
[key: string]: {
|
|
max: number;
|
|
window: string;
|
|
};
|
|
}
|
|
|
|
export const RateConfig: RateConfigProps = {
|
|
getMessages: { max: 10, window: '5s' },
|
|
nodeInfo: { max: 10, window: '5s' },
|
|
};
|
|
|
|
const rateLimiter = getGraphQLRateLimiter({
|
|
identifyContext: (ctx: string) => ctx,
|
|
formatError: () => 'Rate Limit Reached',
|
|
});
|
|
|
|
export const requestLimiter = async (rate: string, field: string) => {
|
|
const { max, window } = RateConfig[field] || { max: 5, window: '5s' };
|
|
const errorMessage = await rateLimiter(
|
|
{
|
|
parent: rate,
|
|
args: {},
|
|
context: rate,
|
|
info: { fieldName: field } as any,
|
|
},
|
|
{ max, window }
|
|
);
|
|
if (errorMessage) {
|
|
logger.warn(`Rate limit reached for '${field}' from ip ${rate}`);
|
|
throw new Error(errorMessage);
|
|
}
|
|
};
|