refactor: improve maintainability by reducing LOC/reuse

This commit is contained in:
2026-03-28 16:52:19 +04:00
parent 8831c9c7b4
commit 6dcb5c0a52
29 changed files with 311 additions and 468 deletions
-42
View File
@@ -1,42 +0,0 @@
export type Plan = "free" | "pro" | "pro2x" | "pro4x" | "lifetime";
export interface PlanLimits {
maxMonitors: number;
minIntervalS: number;
maxRegions: number;
}
const PLANS: Record<Plan, PlanLimits> = {
free: { maxMonitors: 10, minIntervalS: 30, maxRegions: 1 },
pro: { maxMonitors: 200, minIntervalS: 5, maxRegions: 99 },
pro2x: { maxMonitors: 400, minIntervalS: 5, maxRegions: 99 },
pro4x: { maxMonitors: 800, minIntervalS: 5, maxRegions: 99 },
lifetime: { maxMonitors: 200, minIntervalS: 5, maxRegions: 99 },
};
export function getPlanLimits(plan: string): PlanLimits {
return PLANS[plan as Plan] || PLANS.free;
}
// Display helpers
export const PLAN_LABELS: Record<string, string> = {
free: "Free", pro: "Pro", pro2x: "Pro 2x", pro4x: "Pro 4x", lifetime: "Lifetime",
};
export const PRO_MULTIPLIERS = [
{ plan: "pro", label: "1x", monitors: 200, interval: "5s", priceMultiplier: 1 },
{ plan: "pro2x", label: "2x", monitors: 400, interval: "5s", priceMultiplier: 2 },
{ plan: "pro4x", label: "4x", monitors: 800, interval: "5s", priceMultiplier: 4 },
];
export const PRO_MONTHLY_USD = 12;
export const LIFETIME_USD = 140;
// Tier ranking for plan stacking decisions
const PLAN_RANK: Record<string, number> = {
free: 0, pro: 1, lifetime: 1, pro2x: 2, pro4x: 3,
};
export function planTier(plan: string): number {
return PLAN_RANK[plan] ?? 0;
}
-21
View File
@@ -1,21 +0,0 @@
export function createRateLimiter(windowMs = 60_000, cleanupIntervalMs = 5 * 60_000) {
const map = new Map<string, { count: number; resetAt: number }>();
setInterval(() => {
const now = Date.now();
for (const [key, entry] of map) {
if (now > entry.resetAt) map.delete(key);
}
}, cleanupIntervalMs);
return function check(key: string, max: number): boolean {
const now = Date.now();
const entry = map.get(key);
if (!entry || now > entry.resetAt) {
map.set(key, { count: 1, resetAt: now + windowMs });
return true;
}
entry.count++;
return entry.count <= max;
};
}
-9
View File
@@ -1,9 +0,0 @@
import { timingSafeEqual } from "crypto";
export function safeTokenCompare(a: string | undefined, b: string | undefined): boolean {
if (!a || !b) return false;
const bufA = Buffer.from(a);
const bufB = Buffer.from(b);
if (bufA.length !== bufB.length) return false;
return timingSafeEqual(bufA, bufB);
}