feat(dynamic): eliminate all hardcoded config — JSON-driven org schema, auto-mask, rate-limits, cookies, reviews; management README
This commit is contained in:
@@ -1,3 +1,35 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
interface RateLimitConfig {
|
||||
perVisitorPerDay: number;
|
||||
perIpPerHour: number;
|
||||
dailyBudgetCalls: number;
|
||||
devOverrides?: {
|
||||
perVisitorPerDay: number;
|
||||
perIpPerHour: number;
|
||||
dailyBudgetCalls: number;
|
||||
};
|
||||
}
|
||||
|
||||
let _rateLimitConfig: RateLimitConfig | null = null;
|
||||
|
||||
function getRateLimitConfig(): RateLimitConfig {
|
||||
if (_rateLimitConfig) return _rateLimitConfig;
|
||||
try {
|
||||
const configPath = path.join(process.cwd(), 'content', 'features', 'rate-limits.config.json');
|
||||
_rateLimitConfig = JSON.parse(readFileSync(configPath, 'utf-8')) as RateLimitConfig;
|
||||
} catch {
|
||||
_rateLimitConfig = {
|
||||
perVisitorPerDay: 5,
|
||||
perIpPerHour: 25,
|
||||
dailyBudgetCalls: 5000,
|
||||
devOverrides: { perVisitorPerDay: 10000, perIpPerHour: 10000, dailyBudgetCalls: 50000 },
|
||||
};
|
||||
}
|
||||
return _rateLimitConfig;
|
||||
}
|
||||
|
||||
interface VisitorRecord {
|
||||
count: number;
|
||||
dateStr: string;
|
||||
@@ -16,15 +48,21 @@ let dailyBudgetResetDate = new Date().toDateString();
|
||||
export function checkRateLimits(
|
||||
visitorId: string,
|
||||
ip: string,
|
||||
limits = { perVisitorPerDay: 5, perIpPerHour: 25, dailyBudgetCalls: 5000 }
|
||||
overrideLimits?: { perVisitorPerDay: number; perIpPerHour: number; dailyBudgetCalls: number }
|
||||
): { allowed: boolean; reason?: 'visitor_daily' | 'ip_hourly' | 'budget_exhausted'; remainingToday: number } {
|
||||
const cfg = getRateLimitConfig();
|
||||
|
||||
// Relax rate limits only for loopback addresses in development; never in production or test
|
||||
const isLoopback = ip === '127.0.0.1' || ip === '::1' || ip === 'localhost';
|
||||
const isDev = process.env.NODE_ENV === 'development';
|
||||
const isLocal = isDev && isLoopback;
|
||||
const effectiveLimits = isLocal
|
||||
? { perVisitorPerDay: 10000, perIpPerHour: 10000, dailyBudgetCalls: 50000 }
|
||||
: limits;
|
||||
|
||||
// Priority: explicit override (for tests) > dev-loopback override > config defaults
|
||||
const limits = overrideLimits
|
||||
? overrideLimits
|
||||
: isLocal && cfg.devOverrides
|
||||
? cfg.devOverrides
|
||||
: { perVisitorPerDay: cfg.perVisitorPerDay, perIpPerHour: cfg.perIpPerHour, dailyBudgetCalls: cfg.dailyBudgetCalls };
|
||||
|
||||
const today = new Date().toDateString();
|
||||
|
||||
@@ -36,7 +74,7 @@ export function checkRateLimits(
|
||||
}
|
||||
|
||||
// Check Daily Budget
|
||||
if (dailyBudgetCount >= effectiveLimits.dailyBudgetCalls) {
|
||||
if (dailyBudgetCount >= limits.dailyBudgetCalls) {
|
||||
return { allowed: false, reason: 'budget_exhausted', remainingToday: 0 };
|
||||
}
|
||||
|
||||
@@ -47,7 +85,7 @@ export function checkRateLimits(
|
||||
visitorStore.set(visitorId, visitor);
|
||||
}
|
||||
|
||||
if (visitor.count >= effectiveLimits.perVisitorPerDay) {
|
||||
if (visitor.count >= limits.perVisitorPerDay) {
|
||||
return { allowed: false, reason: 'visitor_daily', remainingToday: 0 };
|
||||
}
|
||||
|
||||
@@ -61,8 +99,8 @@ export function checkRateLimits(
|
||||
}
|
||||
ipRec.timestamps = ipRec.timestamps.filter((t) => t > oneHourAgo);
|
||||
|
||||
if (ipRec.timestamps.length >= effectiveLimits.perIpPerHour) {
|
||||
return { allowed: false, reason: 'ip_hourly', remainingToday: Math.max(0, effectiveLimits.perVisitorPerDay - visitor.count) };
|
||||
if (ipRec.timestamps.length >= limits.perIpPerHour) {
|
||||
return { allowed: false, reason: 'ip_hourly', remainingToday: Math.max(0, limits.perVisitorPerDay - visitor.count) };
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user