16 lines · 585 B
const OPENS_PER_WINDOW = 30;
const WINDOW_MS = 60 * 1000;

/** Read-only viewers may open files, but not churn the shared doc for every writer at will. */
export class ReadonlyOpenLimiter {
  private readonly windows = new WeakMap<object, { count: number; since: number }>();

  allow(socket: object, now = Date.now()): boolean {
    const current = this.windows.get(socket);
    const window = current && now - current.since < WINDOW_MS ? current : { count: 0, since: now };
    window.count++;
    this.windows.set(socket, window);
    return window.count <= OPENS_PER_WINDOW;
  }
}