Skip to content

LIBERATED AI CHAT

TypeScript
5.1 KB
Raw Blame
/**
 * Rate Limiting Middleware (Tier-Aware)
 *
 * In-memory rate limiter that reads limits from the request's tierConfig
 * (set by auth middleware). Falls back to env-var defaults if no tier is set.
 *
 * Three sliding windows per key:
 *   1. Total lifetime cap (0 = unlimited)
 *   2. Per-minute
 *   3. Per-day
 *
 * Designed for a research preview — not a production-grade limiter.
 * For production, use Redis-backed rate limiting.
 */

import type { Request, Response, NextFunction } from 'express'
import type { TierConfig } from '../lib/tiers'

interface RateBucket {
  totalRequests: number       // lifetime count (never resets)
  minuteRequests: number[]    // timestamps of requests in current window
  dayRequests: number[]
}

const MINUTE_MS = 60 * 1000
const DAY_MS = 24 * 60 * 60 * 1000

// NOTE: In-memory only — resets on process/container restart.
// For production, use Redis-backed rate limiting.
const buckets = new Map<string, RateBucket>()

// Clean up stale sliding-window entries every 10 minutes
// Remove empty buckets that have exhausted their lifetime cap to prevent unbounded growth.
setInterval(() => {
  const now = Date.now()
  for (const [key, bucket] of buckets) {
    bucket.minuteRequests = bucket.minuteRequests.filter(t => now - t < MINUTE_MS)
    bucket.dayRequests = bucket.dayRequests.filter(t => now - t < DAY_MS)
    // Remove buckets that have no sliding-window activity and haven't hit their
    // lifetime cap (i.e. they're inactive and can be safely reclaimed).
    // Buckets that hit the total limit stay so they remain blocked.
    if (bucket.minuteRequests.length === 0 && bucket.dayRequests.length === 0 && bucket.totalRequests === 0) {
      buckets.delete(key)
    }
  }
}, 10 * 60 * 1000)

export function rateLimit(req: Request, res: Response, next: NextFunction): void {
  const keyId = req.apiKeyId || 'unknown'
  const tierConfig: TierConfig | undefined = req.tierConfig
  const now = Date.now()

  // Resolve limits from tier config or fall back to env defaults
  const TOTAL_LIMIT = tierConfig?.rateLimit.total ?? parseInt(process.env.RATE_LIMIT_TOTAL || '5', 10)
  const MINUTE_LIMIT = tierConfig?.rateLimit.perMinute ?? parseInt(process.env.RATE_LIMIT_PER_MINUTE || '60', 10)
  const DAY_LIMIT = tierConfig?.rateLimit.perDay ?? parseInt(process.env.RATE_LIMIT_PER_DAY || '1000', 10)

  if (!buckets.has(keyId)) {
    buckets.set(keyId, { totalRequests: 0, minuteRequests: [], dayRequests: [] })
  }

  const bucket = buckets.get(keyId)!

  // ── Check lifetime total cap (hard cutoff) ──────────────────────
  if (TOTAL_LIMIT > 0 && bucket.totalRequests >= TOTAL_LIMIT) {
    const tier = req.tier || 'free'
    res.status(429).json({
      error: 'Request limit reached for this API key',
      limit: TOTAL_LIMIT,
      used: bucket.totalRequests,
      remaining: 0,
      current_tier: tier,
      note: tier === 'free'
        ? 'Free tier has a limited number of requests. Upgrade to Pro or Enterprise for higher limits.'
        : 'Contact support to increase your limits.',
      upgrade: tier === 'free' ? 'Set GODMODE_TIER_KEYS to assign a higher tier to your API key.' : undefined,
    })
    return
  }

  // Prune expired sliding-window timestamps
  bucket.minuteRequests = bucket.minuteRequests.filter(t => now - t < MINUTE_MS)
  bucket.dayRequests = bucket.dayRequests.filter(t => now - t < DAY_MS)

  // ── Check per-minute limit ──────────────────────────────────────
  if (bucket.minuteRequests.length >= MINUTE_LIMIT) {
    const retryAfter = Math.ceil((bucket.minuteRequests[0] + MINUTE_MS - now) / 1000)
    res.status(429).json({
      error: 'Rate limit exceeded (per-minute)',
      limit: MINUTE_LIMIT,
      window: '1 minute',
      retry_after_seconds: retryAfter,
      current_tier: req.tier || 'free',
    })
    return
  }

  // ── Check per-day limit ─────────────────────────────────────────
  if (bucket.dayRequests.length >= DAY_LIMIT) {
    const retryAfter = Math.ceil((bucket.dayRequests[0] + DAY_MS - now) / 1000)
    res.status(429).json({
      error: 'Rate limit exceeded (daily)',
      limit: DAY_LIMIT,
      window: '24 hours',
      retry_after_seconds: retryAfter,
      current_tier: req.tier || 'free',
    })
    return
  }

  // Record the request
  bucket.totalRequests++
  bucket.minuteRequests.push(now)
  bucket.dayRequests.push(now)

  // Set rate limit headers
  const totalRemaining = TOTAL_LIMIT > 0 ? TOTAL_LIMIT - bucket.totalRequests : Infinity
  res.setHeader('X-RateLimit-Limit-Total', TOTAL_LIMIT > 0 ? TOTAL_LIMIT : 'unlimited')
  res.setHeader('X-RateLimit-Remaining-Total', TOTAL_LIMIT > 0 ? totalRemaining : 'unlimited')
  res.setHeader('X-RateLimit-Limit-Minute', MINUTE_LIMIT)
  res.setHeader('X-RateLimit-Remaining-Minute', MINUTE_LIMIT - bucket.minuteRequests.length)
  res.setHeader('X-RateLimit-Limit-Day', DAY_LIMIT)
  res.setHeader('X-RateLimit-Remaining-Day', DAY_LIMIT - bucket.dayRequests.length)
  res.setHeader('X-Tier', req.tier || 'free')

  next()
}