videogen/apps/web/lib/rate-limit.ts
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

87 lines
3.2 KiB
TypeScript

/**
* Small per-key token bucket for public, unauthenticated routes.
*
* State lives in module memory, so on Cloudflare Workers it is PER ISOLATE:
* a determined abuser spread across isolates/colos gets more than `capacity`.
* It stops casual scripted abuse; the durable limit belongs at the edge.
* Recommended: add a Cloudflare WAF rate-limiting rule for the same path
* (e.g. /api/newsletter, 5 requests / hour / IP, action: block).
*
* Keys are SHA-256 hashes of the client IP, so raw IPs are never kept in memory.
*/
export interface RateLimitOptions {
/** Max requests allowed in a burst (bucket size). */
readonly capacity: number;
/** Window over which a full bucket refills, in milliseconds. */
readonly refillMs: number;
/** Upper bound on tracked keys; the oldest are evicted first. */
readonly maxKeys?: number;
}
export type RateLimitDecision =
| { readonly allowed: true; readonly remaining: number }
| { readonly allowed: false; readonly retryAfterSeconds: number };
interface Bucket {
readonly tokens: number;
readonly updatedAt: number;
}
const DEFAULT_MAX_KEYS = 10_000;
export interface RateLimiter {
check(key: string, now?: number): RateLimitDecision;
}
export function createRateLimiter(options: RateLimitOptions): RateLimiter {
const { capacity, refillMs } = options;
const maxKeys = options.maxKeys ?? DEFAULT_MAX_KEYS;
const refillPerMs = capacity / refillMs;
const buckets = new Map<string, Bucket>();
function evictOverflow(): void {
while (buckets.size > maxKeys) {
const oldest = buckets.keys().next();
if (oldest.done) return;
buckets.delete(oldest.value);
}
}
return {
check(key: string, now: number = Date.now()): RateLimitDecision {
const previous = buckets.get(key);
const elapsed = previous ? Math.max(0, now - previous.updatedAt) : 0;
const available = previous ? Math.min(capacity, previous.tokens + elapsed * refillPerMs) : capacity;
// Re-insert so Map order tracks recency for eviction.
buckets.delete(key);
if (available < 1) {
buckets.set(key, { tokens: available, updatedAt: now });
return { allowed: false, retryAfterSeconds: Math.ceil((1 - available) / refillPerMs / 1000) };
}
buckets.set(key, { tokens: available - 1, updatedAt: now });
evictOverflow();
return { allowed: true, remaining: Math.floor(available - 1) };
},
};
}
/**
* Best-effort client IP. `cf-connecting-ip` is set (and not spoofable) on
* Cloudflare; the other headers cover local/proxied dev. `null` when unknown.
*/
export function clientIp(request: Request): string | null {
const headers = request.headers;
const direct = headers.get("cf-connecting-ip") ?? headers.get("x-real-ip");
if (direct && direct.trim()) return direct.trim();
const forwarded = headers.get("x-forwarded-for")?.split(",")[0]?.trim();
return forwarded || null;
}
/** Hex SHA-256 of the IP, so the limiter never stores a raw address. */
export async function hashIp(ip: string): Promise<string> {
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(ip));
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
}