thunderhub/server/helpers/rateLimiter.ts
Anthony Potdevin 581185e6b0
chore: 🔧 remove client (#111)
* 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>
2020-08-05 08:37:02 +02:00

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);
}
};