import type { Patch } from 'immer'; import { BaseController } from '../BaseControllerV2'; import type { RestrictedControllerMessenger } from '../ControllerMessenger'; /** * @type RateLimitState * @property requests - Object containing number of requests in a given interval for each origin and api type combination */ export declare type RateLimitState any>> = { requests: Record>; }; declare const name = "RateLimitController"; export declare type RateLimitStateChange any>> = { type: `${typeof name}:stateChange`; payload: [RateLimitState, Patch[]]; }; export declare type GetRateLimitState any>> = { type: `${typeof name}:getState`; handler: () => RateLimitState; }; export declare type CallApi any>> = { type: `${typeof name}:call`; handler: RateLimitController['call']; }; export declare type RateLimitControllerActions any>> = GetRateLimitState | CallApi; export declare type RateLimitMessenger any>> = RestrictedControllerMessenger, RateLimitStateChange, never, never>; /** * Controller with logic for rate-limiting API endpoints per requesting origin. */ export declare class RateLimitController any>> extends BaseController, RateLimitMessenger> { private implementations; private rateLimitTimeout; private rateLimitCount; /** * Creates a RateLimitController instance. * * @param options - Constructor options. * @param options.messenger - A reference to the messaging system. * @param options.state - Initial state to set on this controller. * @param options.implementations - Mapping from API type to API implementation. * @param options.rateLimitTimeout - The time window in which the rate limit is applied (in ms). * @param options.rateLimitCount - The amount of calls an origin can make in the rate limit time window. */ constructor({ rateLimitTimeout, rateLimitCount, messenger, state, implementations, }: { rateLimitTimeout?: number; rateLimitCount?: number; messenger: RateLimitMessenger; state?: Partial>; implementations: RateLimitedApis; }); /** * Calls an API if the requesting origin is not rate-limited. * * @param origin - The requesting origin. * @param type - The type of API call to make. * @param args - Arguments for the API call. * @returns `false` if rate-limited, and `true` otherwise. */ call(origin: string, type: ApiType, ...args: Parameters): Promise>; /** * Checks whether an origin is rate limited for the a specific API. * * @param api - The API the origin is trying to access. * @param origin - The origin trying to access the API. * @returns `true` if rate-limited, and `false` otherwise. */ private isRateLimited; /** * Records that an origin has made a request to call an API, for rate-limiting purposes. * * @param api - The API the origin is trying to access. * @param origin - The origin trying to access the API. */ private recordRequest; /** * Resets the request count for a given origin and API combination, for rate-limiting purposes. * * @param api - The API in question. * @param origin - The origin in question. */ private resetRequestCount; } export {};