@metamask-previews/rate-limit-controller 3.0.3-preview.85b1745 → 3.0.3-preview.eb58a59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
- import type { RestrictedControllerMessenger } from '@metamask/base-controller';
1
+ import type { ActionConstraint, RestrictedControllerMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from '@metamask/base-controller';
2
2
  import { BaseControllerV2 as BaseController } from '@metamask/base-controller';
3
- import type { Patch } from 'immer';
4
3
  /**
5
4
  * @type RateLimitedApi
6
5
  * @property method - The method that is rate-limited.
@@ -8,7 +7,7 @@ import type { Patch } from 'immer';
8
7
  * @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.
9
8
  */
10
9
  export declare type RateLimitedApi = {
11
- method: (...args: any[]) => any;
10
+ method: ActionConstraint['handler'];
12
11
  rateLimitTimeout?: number;
13
12
  rateLimitCount?: number;
14
13
  };
@@ -20,20 +19,15 @@ export declare type RateLimitState<RateLimitedApis extends Record<string, RateLi
20
19
  requests: Record<keyof RateLimitedApis, Record<string, number>>;
21
20
  };
22
21
  declare const name = "RateLimitController";
23
- export declare type RateLimitStateChange<RateLimitedApis extends Record<string, RateLimitedApi>> = {
24
- type: `${typeof name}:stateChange`;
25
- payload: [RateLimitState<RateLimitedApis>, Patch[]];
26
- };
27
- export declare type GetRateLimitState<RateLimitedApis extends Record<string, RateLimitedApi>> = {
28
- type: `${typeof name}:getState`;
29
- handler: () => RateLimitState<RateLimitedApis>;
30
- };
22
+ export declare type RateLimitStateChange<RateLimitedApis extends Record<string, RateLimitedApi>> = ControllerStateChangeEvent<typeof name, RateLimitedApis>;
23
+ export declare type GetRateLimitState<RateLimitedApis extends Record<string, RateLimitedApi>> = ControllerGetStateAction<typeof name, RateLimitedApis>;
31
24
  export declare type CallApi<RateLimitedApis extends Record<string, RateLimitedApi>> = {
32
25
  type: `${typeof name}:call`;
33
26
  handler: RateLimitController<RateLimitedApis>['call'];
34
27
  };
35
28
  export declare type RateLimitControllerActions<RateLimitedApis extends Record<string, RateLimitedApi>> = GetRateLimitState<RateLimitedApis> | CallApi<RateLimitedApis>;
36
- export declare type RateLimitMessenger<RateLimitedApis extends Record<string, RateLimitedApi>> = RestrictedControllerMessenger<typeof name, RateLimitControllerActions<RateLimitedApis>, RateLimitStateChange<RateLimitedApis>, never, never>;
29
+ export declare type RateLimitControllerEvents<RateLimitedApis extends Record<string, RateLimitedApi>> = RateLimitStateChange<RateLimitedApis>;
30
+ export declare type RateLimitMessenger<RateLimitedApis extends Record<string, RateLimitedApi>> = RestrictedControllerMessenger<typeof name, RateLimitControllerActions<RateLimitedApis>, RateLimitControllerEvents<RateLimitedApis>, never, never>;
37
31
  /**
38
32
  * Controller with logic for rate-limiting API endpoints per requesting origin.
39
33
  */
@@ -64,7 +58,6 @@ export declare class RateLimitController<RateLimitedApis extends Record<string,
64
58
  * @param origin - The requesting origin.
65
59
  * @param type - The type of API call to make.
66
60
  * @param args - Arguments for the API call.
67
- * @returns `false` if rate-limited, and `true` otherwise.
68
61
  */
69
62
  call<ApiType extends keyof RateLimitedApis>(origin: string, type: ApiType, ...args: Parameters<RateLimitedApis[ApiType]['method']>): Promise<ReturnType<RateLimitedApis[ApiType]['method']>>;
70
63
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"RateLimitController.d.ts","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,2BAA2B,CAAC;AAC/E,OAAO,EAAE,gBAAgB,IAAI,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE/E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAEnC;;;;;GAKG;AACH,oBAAY,cAAc,GAAG;IAC3B,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;IAChC,gBAAgB,CAAC,EAAE,MAAM,CAAC;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H,OAAO,CAAC,aAAa;IAMrB;;;;;OAKG;IACH,OAAO,CAAC,aAAa;IAarB;;;;;OAKG;IACH,OAAO,CAAC,iBAAiB;CAK1B"}
@@ -43,7 +43,7 @@ class RateLimitController extends base_controller_1.BaseControllerV2 {
43
43
  this.implementations = implementations;
44
44
  this.rateLimitTimeout = rateLimitTimeout;
45
45
  this.rateLimitCount = rateLimitCount;
46
- this.messagingSystem.registerActionHandler(`${name}:call`, ((origin, type, ...args) => this.call(origin, type, ...args)));
46
+ this.messagingSystem.registerActionHandler(`${name}:call`, (origin, type, ...args) => this.call(origin, type, ...args));
47
47
  }
48
48
  /**
49
49
  * Calls an API if the requesting origin is not rate-limited.
@@ -51,7 +51,6 @@ class RateLimitController extends base_controller_1.BaseControllerV2 {
51
51
  * @param origin - The requesting origin.
52
52
  * @param type - The type of API call to make.
53
53
  * @param args - Arguments for the API call.
54
- * @returns `false` if rate-limited, and `true` otherwise.
55
54
  */
56
55
  call(origin, type, ...args) {
57
56
  return __awaiter(this, void 0, void 0, function* () {
@@ -1 +1 @@
1
- {"version":3,"file":"RateLimitController.js","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,+DAA+E;AAC/E,qDAAiD;AAyBjD,MAAM,IAAI,GAAG,qBAAqB,CAAC;AAmCnC,MAAM,QAAQ,GAAG;IACf,QAAQ,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE;CAC/C,CAAC;AAEF;;GAEG;AACH,MAAa,mBAEX,SAAQ,kCAIT;IAOC;;;;;;;;;OASG;IACH,YAAY,EACV,gBAAgB,GAAG,IAAI,EACvB,cAAc,GAAG,CAAC,EAClB,SAAS,EACT,KAAK,EACL,eAAe,GAOhB;QACC,MAAM,YAAY,GAAG;YACnB,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,MAAM,CAC3C,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAA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AAG,CAAC,CAAC,cAAc,mCAAI,IAAI,CAAC,cAAc,CAAC;QAClE,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,cAAc,CAAC;IAC5D,CAAC;IAED;;;;;OAKG;IACK,aAAa,CAAC,GAA0B,EAAE,MAAc;;QAC9D,MAAM,gBAAgB,GACpB,MAAA,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,gBAAgB,mCAAI,IAAI,CAAC,gBAAgB,CAAC;QACtE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;;YACpB,MAAM,QAAQ,GAAG,MAAC,KAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,mCAAI,CAAC,CAAC;YAC1D,KAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,QAAQ,GAAG,CAAC,CAAC;YAEpD,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC;aACzE;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACnB,KAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAtID,kDAsIC","sourcesContent":["import type { RestrictedControllerMessenger } from '@metamask/base-controller';\nimport { BaseControllerV2 as BaseController } from '@metamask/base-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { Patch } from 'immer';\n\n/**\n * @type RateLimitedApi\n * @property method - The method that is rate-limited.\n * @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\nexport type RateLimitedApi = {\n method: (...args: any[]) => any;\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n};\n\n/**\n * @type RateLimitState\n * @property requests - Object containing number of requests in a given interval for each origin and api type combination\n */\nexport type RateLimitState<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = {\n requests: Record<keyof RateLimitedApis, Record<string, number>>;\n};\n\nconst name = 'RateLimitController';\n\nexport type RateLimitStateChange<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = {\n type: `${typeof name}:stateChange`;\n payload: [RateLimitState<RateLimitedApis>, Patch[]];\n};\n\nexport type GetRateLimitState<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = {\n type: `${typeof name}:getState`;\n handler: () => RateLimitState<RateLimitedApis>;\n};\n\nexport type CallApi<RateLimitedApis extends Record<string, RateLimitedApi>> = {\n type: `${typeof name}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = GetRateLimitState<RateLimitedApis> | CallApi<RateLimitedApis>;\n\nexport type RateLimitMessenger<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = RestrictedControllerMessenger<\n typeof name,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitStateChange<RateLimitedApis>,\n never,\n never\n>;\n\nconst metadata = {\n requests: { persist: false, anonymous: false },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> extends BaseController<\n typeof name,\n RateLimitState<RateLimitedApis>,\n RateLimitMessenger<RateLimitedApis>\n> {\n private readonly implementations;\n\n private readonly rateLimitTimeout;\n\n private readonly rateLimitCount;\n\n /**\n * Creates a RateLimitController instance.\n *\n * @param options - Constructor options.\n * @param options.messenger - A reference to the messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.implementations - Mapping from API type to API implementation.\n * @param options.rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @param options.rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\n constructor({\n rateLimitTimeout = 5000,\n rateLimitCount = 1,\n messenger,\n state,\n implementations,\n }: {\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n messenger: RateLimitMessenger<RateLimitedApis>;\n state?: Partial<RateLimitState<RateLimitedApis>>;\n implementations: RateLimitedApis;\n }) {\n const defaultState = {\n requests: Object.keys(implementations).reduce(\n (acc, key) => ({ ...acc, [key]: {} }),\n {} as Record<keyof RateLimitedApis, Record<string, number>>,\n ),\n };\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.implementations = implementations;\n this.rateLimitTimeout = rateLimitTimeout;\n this.rateLimitCount = rateLimitCount;\n\n this.messagingSystem.registerActionHandler(\n `${name}:call` as const,\n ((\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[keyof RateLimitedApis]['method']>\n ) => this.call(origin, type, ...args)) as any,\n );\n }\n\n /**\n * Calls an API if the requesting origin is not rate-limited.\n *\n * @param origin - The requesting origin.\n * @param type - The type of API call to make.\n * @param args - Arguments for the API call.\n * @returns `false` if rate-limited, and `true` otherwise.\n */\n async call<ApiType extends keyof RateLimitedApis>(\n origin: string,\n type: ApiType,\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ): Promise<ReturnType<RateLimitedApis[ApiType]['method']>> {\n if (this.isRateLimited(type, origin)) {\n throw rpcErrors.limitExceeded({\n message: `\"${type.toString()}\" is currently rate-limited. Please try again later.`,\n });\n }\n this.recordRequest(type, origin);\n\n const implementation = this.implementations[type].method;\n\n if (!implementation) {\n throw new Error('Invalid api type');\n }\n\n return implementation(...args);\n }\n\n /**\n * Checks whether an origin is rate limited for the a specific API.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n * @returns `true` if rate-limited, and `false` otherwise.\n */\n private isRateLimited(api: keyof RateLimitedApis, origin: string) {\n const rateLimitCount =\n this.implementations[api].rateLimitCount ?? this.rateLimitCount;\n return this.state.requests[api][origin] >= rateLimitCount;\n }\n\n /**\n * Records that an origin has made a request to call an API, for rate-limiting purposes.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n */\n private recordRequest(api: keyof RateLimitedApis, origin: string) {\n const rateLimitTimeout =\n this.implementations[api].rateLimitTimeout ?? this.rateLimitTimeout;\n this.update((state) => {\n const previous = (state as any).requests[api][origin] ?? 0;\n (state as any).requests[api][origin] = previous + 1;\n\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n });\n }\n\n /**\n * Resets the request count for a given origin and API combination, for rate-limiting purposes.\n *\n * @param api - The API in question.\n * @param origin - The origin in question.\n */\n private resetRequestCount(api: keyof RateLimitedApis, origin: string) {\n this.update((state) => {\n (state as any).requests[api][origin] = 0;\n });\n }\n}\n"]}
1
+ {"version":3,"file":"RateLimitController.js","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA,+DAA+E;AAC/E,qDAAiD;AAwBjD,MAAM,IAAI,GAAG,qBAAqB,CAAC;AAiCnC,MAAM,QAAQ,GAAG;IACf,QAAQ,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE;CAC/C,CAAC;AAEF;;GAEG;AACH,MAAa,mBAEX,SAAQ,kCAIT;IAOC;;;;;;;;;OASG;IACH,YAAY,EACV,gBAAgB,GAAG,IAAI,EACvB,cAAc,GAAG,CAAC,EAClB,SAAS,EACT,KAAK,EACL,eAAe,GAOhB;QACC,MAAM,YAAY,GAAG;YACnB,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,MAAM,CAC3C,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,iCAAM,GAAG,KAAE,CAAC,GAAG,CAAC,EAAE,EAAE,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IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,cAAc,CAAC;IAC5D,CAAC;IAED;;;;;OAKG;IACK,aAAa,CAAC,GAA0B,EAAE,MAAc;;QAC9D,MAAM,gBAAgB,GACpB,MAAA,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,gBAAgB,mCAAI,IAAI,CAAC,gBAAgB,CAAC;QACtE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;;YACpB,MAAM,QAAQ,GAAG,MAAC,KAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,mCAAI,CAAC,CAAC;YAC1D,KAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,QAAQ,GAAG,CAAC,CAAC;YAEpD,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC;aACzE;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACnB,KAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAvID,kDAuIC","sourcesContent":["import type {\n ActionConstraint,\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseControllerV2 as BaseController } from '@metamask/base-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\n\n/**\n * @type RateLimitedApi\n * @property method - The method that is rate-limited.\n * @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\nexport type RateLimitedApi = {\n method: ActionConstraint['handler'];\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n};\n\n/**\n * @type RateLimitState\n * @property requests - Object containing number of requests in a given interval for each origin and api type combination\n */\nexport type RateLimitState<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = {\n requests: Record<keyof RateLimitedApis, Record<string, number>>;\n};\n\nconst name = 'RateLimitController';\n\nexport type RateLimitStateChange<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = ControllerStateChangeEvent<typeof name, RateLimitedApis>;\n\nexport type GetRateLimitState<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = ControllerGetStateAction<typeof name, RateLimitedApis>;\n\nexport type CallApi<RateLimitedApis extends Record<string, RateLimitedApi>> = {\n type: `${typeof name}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = GetRateLimitState<RateLimitedApis> | CallApi<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = RateLimitStateChange<RateLimitedApis>;\n\nexport type RateLimitMessenger<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> = RestrictedControllerMessenger<\n typeof name,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>,\n never,\n never\n>;\n\nconst metadata = {\n requests: { persist: false, anonymous: false },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends Record<string, RateLimitedApi>,\n> extends BaseController<\n typeof name,\n RateLimitState<RateLimitedApis>,\n RateLimitMessenger<RateLimitedApis>\n> {\n private readonly implementations;\n\n private readonly rateLimitTimeout;\n\n private readonly rateLimitCount;\n\n /**\n * Creates a RateLimitController instance.\n *\n * @param options - Constructor options.\n * @param options.messenger - A reference to the messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.implementations - Mapping from API type to API implementation.\n * @param options.rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @param options.rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\n constructor({\n rateLimitTimeout = 5000,\n rateLimitCount = 1,\n messenger,\n state,\n implementations,\n }: {\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n messenger: RateLimitMessenger<RateLimitedApis>;\n state?: Partial<RateLimitState<RateLimitedApis>>;\n implementations: RateLimitedApis;\n }) {\n const defaultState = {\n requests: Object.keys(implementations).reduce(\n (acc, key) => ({ ...acc, [key]: {} }),\n {} as Record<keyof RateLimitedApis, Record<string, number>>,\n ),\n };\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.implementations = implementations;\n this.rateLimitTimeout = rateLimitTimeout;\n this.rateLimitCount = rateLimitCount;\n\n this.messagingSystem.registerActionHandler(\n `${name}:call` as const,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[keyof RateLimitedApis]['method']>\n ) => this.call(origin, type, ...args),\n );\n }\n\n /**\n * Calls an API if the requesting origin is not rate-limited.\n *\n * @param origin - The requesting origin.\n * @param type - The type of API call to make.\n * @param args - Arguments for the API call.\n */\n async call<ApiType extends keyof RateLimitedApis>(\n origin: string,\n type: ApiType,\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ): Promise<ReturnType<RateLimitedApis[ApiType]['method']>> {\n if (this.isRateLimited(type, origin)) {\n throw rpcErrors.limitExceeded({\n message: `\"${type.toString()}\" is currently rate-limited. Please try again later.`,\n });\n }\n this.recordRequest(type, origin);\n\n const implementation = this.implementations[type].method;\n\n if (!implementation) {\n throw new Error('Invalid api type');\n }\n\n return implementation(...args) as ReturnType<\n RateLimitedApis[ApiType]['method']\n >;\n }\n\n /**\n * Checks whether an origin is rate limited for the a specific API.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n * @returns `true` if rate-limited, and `false` otherwise.\n */\n private isRateLimited(api: keyof RateLimitedApis, origin: string) {\n const rateLimitCount =\n this.implementations[api].rateLimitCount ?? this.rateLimitCount;\n return this.state.requests[api][origin] >= rateLimitCount;\n }\n\n /**\n * Records that an origin has made a request to call an API, for rate-limiting purposes.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n */\n private recordRequest(api: keyof RateLimitedApis, origin: string) {\n const rateLimitTimeout =\n this.implementations[api].rateLimitTimeout ?? this.rateLimitTimeout;\n this.update((state) => {\n const previous = (state as any).requests[api][origin] ?? 0;\n (state as any).requests[api][origin] = previous + 1;\n\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n });\n }\n\n /**\n * Resets the request count for a given origin and API combination, for rate-limiting purposes.\n *\n * @param api - The API in question.\n * @param origin - The origin in question.\n */\n private resetRequestCount(api: keyof RateLimitedApis, origin: string) {\n this.update((state) => {\n (state as any).requests[api][origin] = 0;\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/rate-limit-controller",
3
- "version": "3.0.3-preview.85b1745",
3
+ "version": "3.0.3-preview.eb58a59",
4
4
  "description": "Contains logic for rate-limiting API endpoints by requesting origin",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -24,17 +24,17 @@
24
24
  "build:docs": "typedoc",
25
25
  "changelog:validate": "../../scripts/validate-changelog.sh @metamask/rate-limit-controller",
26
26
  "publish:preview": "yarn npm publish --tag preview",
27
- "test": "jest",
27
+ "test": "jest --reporters=jest-silent-reporter",
28
28
  "test:clean": "jest --clearCache",
29
+ "test:verbose": "jest --verbose",
29
30
  "test:watch": "jest --watch"
30
31
  },
31
32
  "dependencies": {
32
33
  "@metamask/base-controller": "^3.2.3",
33
- "@metamask/rpc-errors": "^6.1.0",
34
- "immer": "^9.0.6"
34
+ "@metamask/rpc-errors": "^6.1.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@metamask/auto-changelog": "^3.1.0",
37
+ "@metamask/auto-changelog": "^3.4.3",
38
38
  "@types/jest": "^27.4.1",
39
39
  "deepmerge": "^4.2.2",
40
40
  "jest": "^27.5.1",