Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

1.4 KB
Raw Blame
import { InMemoryRateLimitStore, RateLimitStore } from './rate-limit.store';
import { Injectable } from '@nestjs/common';

/**
 * Per-IP rate limiting for the auth-adjacent routes. `check` resolves false
 * once a key exceeds `max` within `windowMs` (the caller answers 429).
 *
 * The counting lives in an injected RateLimitStore. RateLimitModule provides
 * the in-memory one; a hand-built service (the no-Nest test harnesses) gets
 * its own in-memory store, exactly as it got its own maps before.
 */
@Injectable()
export class RateLimitService {
  constructor(private readonly store: RateLimitStore = new InMemoryRateLimitStore()) {}

  /** Resolves true when the request is allowed, false when it should be rejected (429). */
  check(bucket: string, key: string, max: number, windowMs: number, now: number): Promise<boolean> {
    return this.store.hit(bucket, key, max, windowMs, now);
  }

  /**
   * Give back an attempt `check` counted, for a route that only holds the
   * requests it refuses against the limit. Counting at the door and giving back
   * afterwards keeps concurrent requests capped while they are still running.
   */
  release(bucket: string, key: string, windowMs: number, now: number): Promise<void> {
    return this.store.release(bucket, key, windowMs, now);
  }

  /** Test helper: clear a bucket (mirrors the legacy exported maps used for resets). */
  reset(bucket?: string): Promise<void> {
    return this.store.reset(bucket);
  }
}