@metamask-previews/rate-limit-controller 6.1.1-preview-77daf0c7 → 7.0.0-preview-46d2c977
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.
- package/CHANGELOG.md +11 -1
- package/dist/RateLimitController.cjs +5 -4
- package/dist/RateLimitController.cjs.map +1 -1
- package/dist/RateLimitController.d.cts +12 -8
- package/dist/RateLimitController.d.cts.map +1 -1
- package/dist/RateLimitController.d.mts +12 -8
- package/dist/RateLimitController.d.mts.map +1 -1
- package/dist/RateLimitController.mjs +5 -4
- package/dist/RateLimitController.mjs.map +1 -1
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [7.0.0]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **BREAKING:** Use new `Messenger` from `@metamask/messenger` ([#6503](https://github.com/MetaMask/core/pull/6503))
|
|
15
|
+
- Previously, `RateLimitController` accepted a `RestrictedMessenger` instance from `@metamask/base-controller`.
|
|
16
|
+
- **BREAKING:** Metadata property `anonymous` renamed to `includeInDebugSnapshot` ([#6503](https://github.com/MetaMask/core/pull/6503))
|
|
17
|
+
- Bump `@metamask/base-controller` from `^8.4.2` to `^9.0.0` ([#6962](https://github.com/MetaMask/core/pull/6962))
|
|
18
|
+
|
|
10
19
|
## [6.1.1]
|
|
11
20
|
|
|
12
21
|
### Changed
|
|
@@ -194,7 +203,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
194
203
|
|
|
195
204
|
All changes listed after this point were applied to this package following the monorepo conversion.
|
|
196
205
|
|
|
197
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@
|
|
206
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@7.0.0...HEAD
|
|
207
|
+
[7.0.0]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@6.1.1...@metamask/rate-limit-controller@7.0.0
|
|
198
208
|
[6.1.1]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@6.1.0...@metamask/rate-limit-controller@6.1.1
|
|
199
209
|
[6.1.0]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@6.0.3...@metamask/rate-limit-controller@6.1.0
|
|
200
210
|
[6.0.3]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@6.0.2...@metamask/rate-limit-controller@6.0.3
|
|
@@ -4,12 +4,12 @@ exports.RateLimitController = void 0;
|
|
|
4
4
|
const base_controller_1 = require("@metamask/base-controller");
|
|
5
5
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
6
6
|
const utils_1 = require("@metamask/utils");
|
|
7
|
-
const
|
|
7
|
+
const controllerName = 'RateLimitController';
|
|
8
8
|
const metadata = {
|
|
9
9
|
requests: {
|
|
10
10
|
includeInStateLogs: false,
|
|
11
11
|
persist: false,
|
|
12
|
-
|
|
12
|
+
includeInDebugSnapshot: false,
|
|
13
13
|
usedInUi: false,
|
|
14
14
|
},
|
|
15
15
|
};
|
|
@@ -32,7 +32,7 @@ class RateLimitController extends base_controller_1.BaseController {
|
|
|
32
32
|
requests: (0, utils_1.getKnownPropertyNames)(implementations).reduce((acc, key) => ({ ...acc, [key]: {} }), {}),
|
|
33
33
|
};
|
|
34
34
|
super({
|
|
35
|
-
name,
|
|
35
|
+
name: controllerName,
|
|
36
36
|
metadata,
|
|
37
37
|
messenger,
|
|
38
38
|
state: { ...defaultState, ...state },
|
|
@@ -40,7 +40,7 @@ class RateLimitController extends base_controller_1.BaseController {
|
|
|
40
40
|
this.implementations = implementations;
|
|
41
41
|
this.rateLimitTimeout = rateLimitTimeout;
|
|
42
42
|
this.rateLimitCount = rateLimitCount;
|
|
43
|
-
this.
|
|
43
|
+
this.messenger.registerActionHandler(`${controllerName}:call`, (origin, type, ...args) => this.call(origin, type, ...args));
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Calls an API if the requesting origin is not rate-limited.
|
|
@@ -48,6 +48,7 @@ class RateLimitController extends base_controller_1.BaseController {
|
|
|
48
48
|
* @param origin - The requesting origin.
|
|
49
49
|
* @param type - The type of API call to make.
|
|
50
50
|
* @param args - Arguments for the API call.
|
|
51
|
+
* @returns The result of the API call.
|
|
51
52
|
*/
|
|
52
53
|
async call(origin, type, ...args) {
|
|
53
54
|
if (this.isRateLimited(type, origin)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RateLimitController.cjs","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":";;;AAMA,+DAA2D;AAC3D,qDAAiD;AACjD,2CAAwD;AAmCxD,MAAM,IAAI,GAAG,qBAAqB,CAAC;AAoCnC,MAAM,QAAQ,GAAG;IACf,QAAQ,EAAE;QACR,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;QAChB,QAAQ,EAAE,KAAK;KAChB;CACF,CAAC;AAEF;;GAEG;AACH,MAAa,mBAEX,SAAQ,gCAIT;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,IAAA,6BAAqB,EAAC,eAAe,CAAC,CAAC,MAAM,CAErD,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAW,CAAC;SACtD,CAAC;QACF,KAAK,CAAC;YACJ,IAAI;YACJ,QAAQ;YACR,SAAS;YACT,KAAK,EAAE,EAAE,GAAG,YAAY,EAAE,GAAG,KAAK,EAAE;SACrC,C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;QAC9D,MAAM,gBAAgB,GACpB,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,gBAAgB,IAAI,IAAI,CAAC,gBAAgB,CAAC;QACtE,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACvD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC;aACzE;YACD,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,gFAAgF;oBAChF,qEAAqE;oBACrE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE;iBAClC;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAjJD,kDAiJC","sourcesContent":["import type {\n ActionConstraint,\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { getKnownPropertyNames } from '@metamask/utils';\n\n/**\n * A rate-limited API endpoint.\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 * A map of rate-limited API types to APIs.\n */\nexport type RateLimitedApiMap = Record<string, RateLimitedApi>;\n\n/**\n * A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n */\nexport type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =\n Record<keyof RateLimitedApis, Record<string, number>>;\n\n/**\n * The state of the {@link RateLimitController}.\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n * @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.\n */\nexport type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {\n requests: RateLimitedRequests<RateLimitedApis>;\n};\n\nconst name = 'RateLimitController';\n\nexport type RateLimitControllerStateChangeEvent<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerStateChangeEvent<typeof name, RateLimitState<RateLimitedApis>>;\n\nexport type RateLimitControllerGetStateAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerGetStateAction<typeof name, RateLimitState<RateLimitedApis>>;\n\nexport type RateLimitControllerCallApiAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = {\n type: `${typeof name}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends RateLimitedApiMap,\n> =\n | RateLimitControllerGetStateAction<RateLimitedApis>\n | RateLimitControllerCallApiAction<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends RateLimitedApiMap,\n> = RateLimitControllerStateChangeEvent<RateLimitedApis>;\n\nexport type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =\n RestrictedMessenger<\n typeof name,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>,\n never,\n never\n >;\n\nconst metadata = {\n requests: {\n includeInStateLogs: false,\n persist: false,\n anonymous: false,\n usedInUi: false,\n },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends RateLimitedApiMap,\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: getKnownPropertyNames(implementations).reduce<\n RateLimitedRequests<RateLimitedApis>\n >((acc, key) => ({ ...acc, [key]: {} }), {} as never),\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`,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[typeof type]['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 as (\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ) => ReturnType<RateLimitedApis[ApiType]['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 const previous = this.state.requests[api][origin] ?? 0;\n this.update((state) => {\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n [api]: { [origin]: previous + 1 },\n },\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 Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n [api]: { [origin]: 0 },\n },\n });\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"RateLimitController.cjs","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":";;;AAAA,+DAImC;AAEnC,qDAAiD;AACjD,2CAAwD;AAsCxD,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAwC7C,MAAM,QAAQ,GAAG;IACf,QAAQ,EAAE;QACR,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,KAAK;KAChB;CACF,CAAC;AAEF;;GAEG;AACH,MAAa,mBAEX,SAAQ,gCAIT;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,IAAA,6BAAqB,EAAC,eAAe,CAAC,CAAC,MAAM,CAErD,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAW,CAAC;SACtD,CAAC;QACF,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ;YACR,SAAS;YACT,KAAK,EAAE,EAAE,GAAG,YAAY,EAAE,GAAG,KAAK,EAAE;SACrC,CAAC,CAAC;QACH,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QAErC,IAAI,CAAC,SAAS,CAAC,qBAAqB,CAClC,GAAG,cAAc,OAAO,EACxB,CACE,MAAc,EACd,IAA2B,EAC3B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AAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC;aACzE;YACD,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,gFAAgF;oBAChF,qEAAqE;oBACrE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE;iBAClC;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAlJD,kDAkJC","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { Messenger, ActionConstraint } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { getKnownPropertyNames } from '@metamask/utils';\n\n/**\n * A rate-limited API endpoint.\n *\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 * A map of rate-limited API types to APIs.\n */\nexport type RateLimitedApiMap = Record<string, RateLimitedApi>;\n\n/**\n * A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n */\nexport type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =\n Record<keyof RateLimitedApis, Record<string, number>>;\n\n/**\n * The state of the {@link RateLimitController}.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n * @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.\n */\nexport type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {\n requests: RateLimitedRequests<RateLimitedApis>;\n};\n\nconst controllerName = 'RateLimitController';\n\nexport type RateLimitControllerStateChangeEvent<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerStateChangeEvent<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerGetStateAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerGetStateAction<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerCallApiAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = {\n type: `${typeof controllerName}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends RateLimitedApiMap,\n> =\n | RateLimitControllerGetStateAction<RateLimitedApis>\n | RateLimitControllerCallApiAction<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends RateLimitedApiMap,\n> = RateLimitControllerStateChangeEvent<RateLimitedApis>;\n\nexport type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =\n Messenger<\n typeof controllerName,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>\n >;\n\nconst metadata = {\n requests: {\n includeInStateLogs: false,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends RateLimitedApiMap,\n> extends BaseController<\n typeof controllerName,\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: getKnownPropertyNames(implementations).reduce<\n RateLimitedRequests<RateLimitedApis>\n >((acc, key) => ({ ...acc, [key]: {} }), {} as never),\n };\n super({\n name: controllerName,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.implementations = implementations;\n this.rateLimitTimeout = rateLimitTimeout;\n this.rateLimitCount = rateLimitCount;\n\n this.messenger.registerActionHandler(\n `${controllerName}:call`,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[typeof type]['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 * @returns The result of 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 as (\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ) => ReturnType<RateLimitedApis[ApiType]['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 const previous = this.state.requests[api][origin] ?? 0;\n this.update((state) => {\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n [api]: { [origin]: previous + 1 },\n },\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 Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n [api]: { [origin]: 0 },\n },\n });\n });\n }\n}\n"]}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { BaseController, type ControllerGetStateAction, type ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import type { Messenger, ActionConstraint } from "@metamask/messenger";
|
|
3
3
|
/**
|
|
4
4
|
* A rate-limited API endpoint.
|
|
5
|
+
*
|
|
5
6
|
* @property method - The method that is rate-limited.
|
|
6
7
|
* @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).
|
|
7
8
|
* @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.
|
|
@@ -17,31 +18,33 @@ export type RateLimitedApi = {
|
|
|
17
18
|
export type RateLimitedApiMap = Record<string, RateLimitedApi>;
|
|
18
19
|
/**
|
|
19
20
|
* A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.
|
|
21
|
+
*
|
|
20
22
|
* @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.
|
|
21
23
|
*/
|
|
22
24
|
export type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> = Record<keyof RateLimitedApis, Record<string, number>>;
|
|
23
25
|
/**
|
|
24
26
|
* The state of the {@link RateLimitController}.
|
|
27
|
+
*
|
|
25
28
|
* @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.
|
|
26
29
|
* @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.
|
|
27
30
|
*/
|
|
28
31
|
export type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {
|
|
29
32
|
requests: RateLimitedRequests<RateLimitedApis>;
|
|
30
33
|
};
|
|
31
|
-
declare const
|
|
32
|
-
export type RateLimitControllerStateChangeEvent<RateLimitedApis extends RateLimitedApiMap> = ControllerStateChangeEvent<typeof
|
|
33
|
-
export type RateLimitControllerGetStateAction<RateLimitedApis extends RateLimitedApiMap> = ControllerGetStateAction<typeof
|
|
34
|
+
declare const controllerName = "RateLimitController";
|
|
35
|
+
export type RateLimitControllerStateChangeEvent<RateLimitedApis extends RateLimitedApiMap> = ControllerStateChangeEvent<typeof controllerName, RateLimitState<RateLimitedApis>>;
|
|
36
|
+
export type RateLimitControllerGetStateAction<RateLimitedApis extends RateLimitedApiMap> = ControllerGetStateAction<typeof controllerName, RateLimitState<RateLimitedApis>>;
|
|
34
37
|
export type RateLimitControllerCallApiAction<RateLimitedApis extends RateLimitedApiMap> = {
|
|
35
|
-
type: `${typeof
|
|
38
|
+
type: `${typeof controllerName}:call`;
|
|
36
39
|
handler: RateLimitController<RateLimitedApis>['call'];
|
|
37
40
|
};
|
|
38
41
|
export type RateLimitControllerActions<RateLimitedApis extends RateLimitedApiMap> = RateLimitControllerGetStateAction<RateLimitedApis> | RateLimitControllerCallApiAction<RateLimitedApis>;
|
|
39
42
|
export type RateLimitControllerEvents<RateLimitedApis extends RateLimitedApiMap> = RateLimitControllerStateChangeEvent<RateLimitedApis>;
|
|
40
|
-
export type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =
|
|
43
|
+
export type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> = Messenger<typeof controllerName, RateLimitControllerActions<RateLimitedApis>, RateLimitControllerEvents<RateLimitedApis>>;
|
|
41
44
|
/**
|
|
42
45
|
* Controller with logic for rate-limiting API endpoints per requesting origin.
|
|
43
46
|
*/
|
|
44
|
-
export declare class RateLimitController<RateLimitedApis extends RateLimitedApiMap> extends BaseController<typeof
|
|
47
|
+
export declare class RateLimitController<RateLimitedApis extends RateLimitedApiMap> extends BaseController<typeof controllerName, RateLimitState<RateLimitedApis>, RateLimitMessenger<RateLimitedApis>> {
|
|
45
48
|
private readonly implementations;
|
|
46
49
|
private readonly rateLimitTimeout;
|
|
47
50
|
private readonly rateLimitCount;
|
|
@@ -68,6 +71,7 @@ export declare class RateLimitController<RateLimitedApis extends RateLimitedApiM
|
|
|
68
71
|
* @param origin - The requesting origin.
|
|
69
72
|
* @param type - The type of API call to make.
|
|
70
73
|
* @param args - Arguments for the API call.
|
|
74
|
+
* @returns The result of the API call.
|
|
71
75
|
*/
|
|
72
76
|
call<ApiType extends keyof RateLimitedApis>(origin: string, type: ApiType, ...args: Parameters<RateLimitedApis[ApiType]['method']>): Promise<ReturnType<RateLimitedApis[ApiType]['method']>>;
|
|
73
77
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RateLimitController.d.cts","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"RateLimitController.d.cts","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAChC,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,4BAA4B;AAIvE;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,MAAM,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC;IACpC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;AAE/D;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,CAAC,eAAe,SAAS,iBAAiB,IACvE,MAAM,CAAC,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAExD;;;;;GAKG;AACH,MAAM,MAAM,cAAc,CAAC,eAAe,SAAS,iBAAiB,IAAI;IACtE,QAAQ,EAAE,mBAAmB,CAAC,eAAe,CAAC,CAAC;CAChD,CAAC;AAEF,QAAA,MAAM,cAAc,wBAAwB,CAAC;AAE7C,MAAM,MAAM,mCAAmC,CAC7C,eAAe,SAAS,iBAAiB,IACvC,0BAA0B,CAC5B,OAAO,cAAc,EACrB,cAAc,CAAC,eAAe,CAAC,CAChC,CAAC;AAEF,MA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AC,EAAE,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC,CAAC;QACjD,eAAe,EAAE,eAAe,CAAC;KAClC;IA0BD;;;;;;;OAOG;IACG,IAAI,CAAC,OAAO,SAAS,MAAM,eAAe,EAC9C,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,GAAG,IAAI,EAAE,UAAU,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,GACtD,OAAO,CAAC,UAAU,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;IAmB1D;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAMrB;;;;;OAKG;IACH,OAAO,CAAC,aAAa;IAmBrB;;;;;OAKG;IACH,OAAO,CAAC,iBAAiB;CAU1B"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { BaseController, type ControllerGetStateAction, type ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import type { Messenger, ActionConstraint } from "@metamask/messenger";
|
|
3
3
|
/**
|
|
4
4
|
* A rate-limited API endpoint.
|
|
5
|
+
*
|
|
5
6
|
* @property method - The method that is rate-limited.
|
|
6
7
|
* @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).
|
|
7
8
|
* @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.
|
|
@@ -17,31 +18,33 @@ export type RateLimitedApi = {
|
|
|
17
18
|
export type RateLimitedApiMap = Record<string, RateLimitedApi>;
|
|
18
19
|
/**
|
|
19
20
|
* A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.
|
|
21
|
+
*
|
|
20
22
|
* @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.
|
|
21
23
|
*/
|
|
22
24
|
export type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> = Record<keyof RateLimitedApis, Record<string, number>>;
|
|
23
25
|
/**
|
|
24
26
|
* The state of the {@link RateLimitController}.
|
|
27
|
+
*
|
|
25
28
|
* @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.
|
|
26
29
|
* @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.
|
|
27
30
|
*/
|
|
28
31
|
export type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {
|
|
29
32
|
requests: RateLimitedRequests<RateLimitedApis>;
|
|
30
33
|
};
|
|
31
|
-
declare const
|
|
32
|
-
export type RateLimitControllerStateChangeEvent<RateLimitedApis extends RateLimitedApiMap> = ControllerStateChangeEvent<typeof
|
|
33
|
-
export type RateLimitControllerGetStateAction<RateLimitedApis extends RateLimitedApiMap> = ControllerGetStateAction<typeof
|
|
34
|
+
declare const controllerName = "RateLimitController";
|
|
35
|
+
export type RateLimitControllerStateChangeEvent<RateLimitedApis extends RateLimitedApiMap> = ControllerStateChangeEvent<typeof controllerName, RateLimitState<RateLimitedApis>>;
|
|
36
|
+
export type RateLimitControllerGetStateAction<RateLimitedApis extends RateLimitedApiMap> = ControllerGetStateAction<typeof controllerName, RateLimitState<RateLimitedApis>>;
|
|
34
37
|
export type RateLimitControllerCallApiAction<RateLimitedApis extends RateLimitedApiMap> = {
|
|
35
|
-
type: `${typeof
|
|
38
|
+
type: `${typeof controllerName}:call`;
|
|
36
39
|
handler: RateLimitController<RateLimitedApis>['call'];
|
|
37
40
|
};
|
|
38
41
|
export type RateLimitControllerActions<RateLimitedApis extends RateLimitedApiMap> = RateLimitControllerGetStateAction<RateLimitedApis> | RateLimitControllerCallApiAction<RateLimitedApis>;
|
|
39
42
|
export type RateLimitControllerEvents<RateLimitedApis extends RateLimitedApiMap> = RateLimitControllerStateChangeEvent<RateLimitedApis>;
|
|
40
|
-
export type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =
|
|
43
|
+
export type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> = Messenger<typeof controllerName, RateLimitControllerActions<RateLimitedApis>, RateLimitControllerEvents<RateLimitedApis>>;
|
|
41
44
|
/**
|
|
42
45
|
* Controller with logic for rate-limiting API endpoints per requesting origin.
|
|
43
46
|
*/
|
|
44
|
-
export declare class RateLimitController<RateLimitedApis extends RateLimitedApiMap> extends BaseController<typeof
|
|
47
|
+
export declare class RateLimitController<RateLimitedApis extends RateLimitedApiMap> extends BaseController<typeof controllerName, RateLimitState<RateLimitedApis>, RateLimitMessenger<RateLimitedApis>> {
|
|
45
48
|
private readonly implementations;
|
|
46
49
|
private readonly rateLimitTimeout;
|
|
47
50
|
private readonly rateLimitCount;
|
|
@@ -68,6 +71,7 @@ export declare class RateLimitController<RateLimitedApis extends RateLimitedApiM
|
|
|
68
71
|
* @param origin - The requesting origin.
|
|
69
72
|
* @param type - The type of API call to make.
|
|
70
73
|
* @param args - Arguments for the API call.
|
|
74
|
+
* @returns The result of the API call.
|
|
71
75
|
*/
|
|
72
76
|
call<ApiType extends keyof RateLimitedApis>(origin: string, type: ApiType, ...args: Parameters<RateLimitedApis[ApiType]['method']>): Promise<ReturnType<RateLimitedApis[ApiType]['method']>>;
|
|
73
77
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RateLimitController.d.mts","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"RateLimitController.d.mts","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAChC,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,4BAA4B;AAIvE;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,MAAM,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC;IACpC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,CAA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eAAe,CAAC,CAC3C,CAAC;AAWJ;;GAEG;AACH,qBAAa,mBAAmB,CAC9B,eAAe,SAAS,iBAAiB,CACzC,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,cAAc,CAAC,eAAe,CAAC,EAC/B,kBAAkB,CAAC,eAAe,CAAC,CACpC;IACC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAC;IAEjC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAC;IAElC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC;IAEhC;;;;;;;;;OASG;gBACS,EACV,gBAAuB,EACvB,cAAkB,EAClB,SAAS,EACT,KAAK,EACL,eAAe,GAChB,EAAE;QACD,gBAAgB,CAAC,EAAE,MAAM,CAAC;QAC1B,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,SAAS,EAAE,kBAAkB,CAAC,eAAe,CAAC,CAAC;QAC/C,KAAK,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC,CAAC;QACjD,eAAe,EAAE,eAAe,CAAC;KAClC;IA0BD;;;;;;;OAOG;IACG,IAAI,CAAC,OAAO,SAAS,MAAM,eAAe,EAC9C,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,GAAG,IAAI,EAAE,UAAU,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,GACtD,OAAO,CAAC,UAAU,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;IAmB1D;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAMrB;;;;;OAKG;IACH,OAAO,CAAC,aAAa;IAmBrB;;;;;OAKG;IACH,OAAO,CAAC,iBAAiB;CAU1B"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { BaseController } from "@metamask/base-controller";
|
|
2
2
|
import { rpcErrors } from "@metamask/rpc-errors";
|
|
3
3
|
import { getKnownPropertyNames } from "@metamask/utils";
|
|
4
|
-
const
|
|
4
|
+
const controllerName = 'RateLimitController';
|
|
5
5
|
const metadata = {
|
|
6
6
|
requests: {
|
|
7
7
|
includeInStateLogs: false,
|
|
8
8
|
persist: false,
|
|
9
|
-
|
|
9
|
+
includeInDebugSnapshot: false,
|
|
10
10
|
usedInUi: false,
|
|
11
11
|
},
|
|
12
12
|
};
|
|
@@ -29,7 +29,7 @@ export class RateLimitController extends BaseController {
|
|
|
29
29
|
requests: getKnownPropertyNames(implementations).reduce((acc, key) => ({ ...acc, [key]: {} }), {}),
|
|
30
30
|
};
|
|
31
31
|
super({
|
|
32
|
-
name,
|
|
32
|
+
name: controllerName,
|
|
33
33
|
metadata,
|
|
34
34
|
messenger,
|
|
35
35
|
state: { ...defaultState, ...state },
|
|
@@ -37,7 +37,7 @@ export class RateLimitController extends BaseController {
|
|
|
37
37
|
this.implementations = implementations;
|
|
38
38
|
this.rateLimitTimeout = rateLimitTimeout;
|
|
39
39
|
this.rateLimitCount = rateLimitCount;
|
|
40
|
-
this.
|
|
40
|
+
this.messenger.registerActionHandler(`${controllerName}:call`, (origin, type, ...args) => this.call(origin, type, ...args));
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* Calls an API if the requesting origin is not rate-limited.
|
|
@@ -45,6 +45,7 @@ export class RateLimitController extends BaseController {
|
|
|
45
45
|
* @param origin - The requesting origin.
|
|
46
46
|
* @param type - The type of API call to make.
|
|
47
47
|
* @param args - Arguments for the API call.
|
|
48
|
+
* @returns The result of the API call.
|
|
48
49
|
*/
|
|
49
50
|
async call(origin, type, ...args) {
|
|
50
51
|
if (this.isRateLimited(type, origin)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RateLimitController.mjs","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EAAE,qBAAqB,EAAE,wBAAwB;AA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,CAAC,QAAiD;oBAC3D,gFAAgF;oBAChF,qEAAqE;oBACrE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE;iBAClC;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {\n ActionConstraint,\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { getKnownPropertyNames } from '@metamask/utils';\n\n/**\n * A rate-limited API endpoint.\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 * A map of rate-limited API types to APIs.\n */\nexport type RateLimitedApiMap = Record<string, RateLimitedApi>;\n\n/**\n * A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n */\nexport type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =\n Record<keyof RateLimitedApis, Record<string, number>>;\n\n/**\n * The state of the {@link RateLimitController}.\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n * @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.\n */\nexport type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {\n requests: RateLimitedRequests<RateLimitedApis>;\n};\n\nconst name = 'RateLimitController';\n\nexport type RateLimitControllerStateChangeEvent<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerStateChangeEvent<typeof name, RateLimitState<RateLimitedApis>>;\n\nexport type RateLimitControllerGetStateAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerGetStateAction<typeof name, RateLimitState<RateLimitedApis>>;\n\nexport type RateLimitControllerCallApiAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = {\n type: `${typeof name}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends RateLimitedApiMap,\n> =\n | RateLimitControllerGetStateAction<RateLimitedApis>\n | RateLimitControllerCallApiAction<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends RateLimitedApiMap,\n> = RateLimitControllerStateChangeEvent<RateLimitedApis>;\n\nexport type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =\n RestrictedMessenger<\n typeof name,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>,\n never,\n never\n >;\n\nconst metadata = {\n requests: {\n includeInStateLogs: false,\n persist: false,\n anonymous: false,\n usedInUi: false,\n },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends RateLimitedApiMap,\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: getKnownPropertyNames(implementations).reduce<\n RateLimitedRequests<RateLimitedApis>\n >((acc, key) => ({ ...acc, [key]: {} }), {} as never),\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`,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[typeof type]['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 as (\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ) => ReturnType<RateLimitedApis[ApiType]['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 const previous = this.state.requests[api][origin] ?? 0;\n this.update((state) => {\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n [api]: { [origin]: previous + 1 },\n },\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 Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n [api]: { [origin]: 0 },\n },\n });\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"RateLimitController.mjs","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAGf,kCAAkC;AAEnC,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EAAE,qBAAqB,EAAE,wBAAwB;AAsCxD,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAwC7C,MAAM,QAAQ,GAAG;IACf,QAAQ,EAAE;QACR,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,KAAK;KAChB;CACF,CAA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ACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { Messenger, ActionConstraint } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { getKnownPropertyNames } from '@metamask/utils';\n\n/**\n * A rate-limited API endpoint.\n *\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 * A map of rate-limited API types to APIs.\n */\nexport type RateLimitedApiMap = Record<string, RateLimitedApi>;\n\n/**\n * A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n */\nexport type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =\n Record<keyof RateLimitedApis, Record<string, number>>;\n\n/**\n * The state of the {@link RateLimitController}.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n * @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.\n */\nexport type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {\n requests: RateLimitedRequests<RateLimitedApis>;\n};\n\nconst controllerName = 'RateLimitController';\n\nexport type RateLimitControllerStateChangeEvent<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerStateChangeEvent<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerGetStateAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerGetStateAction<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerCallApiAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = {\n type: `${typeof controllerName}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends RateLimitedApiMap,\n> =\n | RateLimitControllerGetStateAction<RateLimitedApis>\n | RateLimitControllerCallApiAction<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends RateLimitedApiMap,\n> = RateLimitControllerStateChangeEvent<RateLimitedApis>;\n\nexport type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =\n Messenger<\n typeof controllerName,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>\n >;\n\nconst metadata = {\n requests: {\n includeInStateLogs: false,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends RateLimitedApiMap,\n> extends BaseController<\n typeof controllerName,\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: getKnownPropertyNames(implementations).reduce<\n RateLimitedRequests<RateLimitedApis>\n >((acc, key) => ({ ...acc, [key]: {} }), {} as never),\n };\n super({\n name: controllerName,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.implementations = implementations;\n this.rateLimitTimeout = rateLimitTimeout;\n this.rateLimitCount = rateLimitCount;\n\n this.messenger.registerActionHandler(\n `${controllerName}:call`,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[typeof type]['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 * @returns The result of 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 as (\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ) => ReturnType<RateLimitedApis[ApiType]['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 const previous = this.state.requests[api][origin] ?? 0;\n this.update((state) => {\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n [api]: { [origin]: previous + 1 },\n },\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 Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n [api]: { [origin]: 0 },\n },\n });\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
|
+
"version": "7.0.0-preview-46d2c977",
|
|
4
4
|
"description": "Contains logic for rate-limiting API endpoints by requesting origin",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -47,7 +47,8 @@
|
|
|
47
47
|
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@metamask/base-controller": "^
|
|
50
|
+
"@metamask/base-controller": "^9.0.0",
|
|
51
|
+
"@metamask/messenger": "^0.3.0",
|
|
51
52
|
"@metamask/rpc-errors": "^7.0.2",
|
|
52
53
|
"@metamask/utils": "^11.8.1"
|
|
53
54
|
},
|