All files / src/core/noop NoopRateLimiter.ts

100% Statements 4/4
100% Branches 1/1
100% Functions 3/3
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                                        9x                       3x                 2x                 1x      
import { RateLimiter } from '@/types/capabilities/RateLimiter'
 
/**
 * @class
 *
 * A limiter that counts nothing.
 *
 * @remarks
 * Unlike the other no-ops, this one takes a decision in its constructor,
 * because "no limiter" has two defensible meanings and picking silently is how
 * an open endpoint gets shipped:
 *
 * - **Open** (the default) — every caller is admitted. Right when the limiter
 *   protects an upstream against load, where its absence is a capacity problem.
 * - **Closed** — nobody is admitted. Right when the limiter is the only thing
 *   between an unauthenticated endpoint and someone else's bill, where its
 *   absence should be a visible outage rather than a silent open door.
 *
 * It mirrors `rateLimit.failOpen` in the configuration layer deliberately: the
 * same question, answered in the same two ways, whichever implementation is in
 * use.
 *
 * @typeParam TRuntime The runtime handle, ignored throughout.
 *
 * @author Bayu Dwiyan Satria
 * @version 1.0.0
 * @since 1.0.0
 */
export class NoopRateLimiter<TRuntime = unknown> implements RateLimiter<TRuntime> {
  /**
   * Whether callers are admitted.
   */
  private readonly open: boolean
 
  /**
   * Constructs a NoopRateLimiter.
   *
   * @param open `true` admits every caller, `false` refuses every caller.
   */
  constructor(open = true) {
    this.open = open
  }
 
  /**
   * Admits or refuses, per the constructor.
   *
   * @returns Whatever this limiter was constructed to answer.
   */
  public async admit(): Promise<boolean> {
    return this.open
  }
 
  /**
   * Never available.
   *
   * @returns `false`.
   */
  public isAvailable(): boolean {
    return false
  }
}