|
@@ -29,6 +29,7 @@ const rateLimiter = new RateLimiterMongo(opts);
|
|
|
const apiRateLimitConfigWithoutRegExp = generateApiRateLimitConfig(false);
|
|
const apiRateLimitConfigWithoutRegExp = generateApiRateLimitConfig(false);
|
|
|
const apiRateLimitConfigWithRegExp = generateApiRateLimitConfig(true);
|
|
const apiRateLimitConfigWithRegExp = generateApiRateLimitConfig(true);
|
|
|
const keysWithRegExp = Object.keys(apiRateLimitConfigWithRegExp).map(key => new RegExp(key));
|
|
const keysWithRegExp = Object.keys(apiRateLimitConfigWithRegExp).map(key => new RegExp(key));
|
|
|
|
|
+const valuesWithRegExp = Object.values(apiRateLimitConfigWithRegExp);
|
|
|
|
|
|
|
|
const consumePoints = async(rateLimiter: RateLimiterMongo, key: string, points: number) => {
|
|
const consumePoints = async(rateLimiter: RateLimiterMongo, key: string, points: number) => {
|
|
|
const consumePoints = defaultMaxPoints / points;
|
|
const consumePoints = defaultMaxPoints / points;
|
|
@@ -43,13 +44,16 @@ module.exports = () => {
|
|
|
const key = md5(req.ip + endpoint);
|
|
const key = md5(req.ip + endpoint);
|
|
|
|
|
|
|
|
let customizedConfig;
|
|
let customizedConfig;
|
|
|
- keysWithRegExp.forEach((key) => {
|
|
|
|
|
- if (key.test(endpoint)) {
|
|
|
|
|
- customizedConfig = apiRateLimitConfigWithRegExp[key.toString()];
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- customizedConfig = apiRateLimitConfigWithoutRegExp[endpoint];
|
|
|
|
|
|
|
+ if (keysWithRegExp.some(key => key.test(endpoint))) {
|
|
|
|
|
+ keysWithRegExp.forEach((key, index) => {
|
|
|
|
|
+ if (key.test(endpoint)) {
|
|
|
|
|
+ customizedConfig = valuesWithRegExp[index];
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ customizedConfig = apiRateLimitConfigWithoutRegExp[endpoint];
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
if (customizedConfig === undefined) {
|
|
if (customizedConfig === undefined) {
|