2024-04-15 17:01:47 -04:00
|
|
|
import { RateLimiterRedis } from "rate-limiter-flexible";
|
|
|
|
import * as redis from "redis";
|
2024-04-20 14:02:22 -07:00
|
|
|
import { RateLimiterMode } from "../../src/types";
|
2024-04-15 17:01:47 -04:00
|
|
|
|
2024-05-30 14:31:36 -07:00
|
|
|
const RATE_LIMITS = {
|
|
|
|
crawl: {
|
|
|
|
free: 1,
|
|
|
|
starter: 3,
|
|
|
|
standard: 5,
|
|
|
|
scale: 20,
|
|
|
|
hobby: 3,
|
|
|
|
standardNew: 10,
|
|
|
|
growth: 50,
|
|
|
|
},
|
|
|
|
scrape: {
|
|
|
|
free: 5,
|
|
|
|
starter: 20,
|
|
|
|
standardOld: 40,
|
|
|
|
scale: 50,
|
|
|
|
hobby: 10,
|
|
|
|
standardNew: 50,
|
|
|
|
growth: 500,
|
|
|
|
},
|
|
|
|
search: {
|
|
|
|
free: 5,
|
|
|
|
starter: 20,
|
|
|
|
standard: 40,
|
|
|
|
scale: 50,
|
|
|
|
hobby: 10,
|
|
|
|
standardNew: 50,
|
|
|
|
growth: 500,
|
|
|
|
},
|
|
|
|
preview: 5,
|
|
|
|
account: 20,
|
|
|
|
crawlStatus: 150,
|
|
|
|
testSuite: 10000,
|
|
|
|
};
|
2024-04-20 14:02:22 -07:00
|
|
|
|
2024-04-15 17:01:47 -04:00
|
|
|
export const redisClient = redis.createClient({
|
|
|
|
url: process.env.REDIS_URL,
|
|
|
|
legacyMode: true,
|
|
|
|
});
|
|
|
|
|
2024-05-30 14:31:36 -07:00
|
|
|
const createRateLimiter = (keyPrefix, points) => new RateLimiterRedis({
|
2024-04-15 17:01:47 -04:00
|
|
|
storeClient: redisClient,
|
2024-05-30 14:31:36 -07:00
|
|
|
keyPrefix,
|
|
|
|
points,
|
2024-04-15 17:01:47 -04:00
|
|
|
duration: 60, // Duration in seconds
|
|
|
|
});
|
|
|
|
|
2024-05-30 14:31:36 -07:00
|
|
|
export const previewRateLimiter = createRateLimiter("preview", RATE_LIMITS.preview);
|
|
|
|
export const serverRateLimiter = createRateLimiter("server", RATE_LIMITS.account);
|
|
|
|
export const crawlStatusRateLimiter = createRateLimiter("crawl-status", RATE_LIMITS.crawlStatus);
|
|
|
|
export const testSuiteRateLimiter = createRateLimiter("test-suite", RATE_LIMITS.testSuite);
|
2024-04-15 17:01:47 -04:00
|
|
|
|
2024-05-30 14:31:36 -07:00
|
|
|
export function getRateLimiter(mode: RateLimiterMode, token: string, plan?: string) {
|
|
|
|
if (token.includes("5089cefa58") || token.includes("6254cf9")) {
|
|
|
|
return testSuiteRateLimiter;
|
|
|
|
}
|
2024-04-20 14:02:22 -07:00
|
|
|
|
2024-05-30 14:31:36 -07:00
|
|
|
const rateLimitConfig = RATE_LIMITS[mode];
|
|
|
|
if (!rateLimitConfig) return serverRateLimiter;
|
2024-05-08 15:14:39 -07:00
|
|
|
|
2024-05-30 14:31:36 -07:00
|
|
|
const planKey = plan ? plan.replace("-", "") : "starter";
|
|
|
|
const points = rateLimitConfig[planKey] || rateLimitConfig.free;
|
2024-04-15 17:01:47 -04:00
|
|
|
|
2024-05-30 14:31:36 -07:00
|
|
|
return createRateLimiter(`${mode}-${planKey}`, points);
|
2024-04-15 17:01:47 -04:00
|
|
|
}
|