Skip to content

Commit faa66e5

Browse files
committed
refactor: migrate RateLimitController to @metamask/messenger
1 parent 640c2e4 commit faa66e5

File tree

6 files changed

+83
-52
lines changed

6 files changed

+83
-52
lines changed

eslint-warning-thresholds.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,7 @@
323323
"@typescript-eslint/prefer-readonly": 1
324324
},
325325
"packages/rate-limit-controller/src/RateLimitController.ts": {
326-
"jsdoc/check-tag-names": 4,
327-
"jsdoc/require-returns": 1,
328-
"jsdoc/tag-lines": 3
326+
"jsdoc/check-tag-names": 4
329327
},
330328
"packages/remote-feature-flag-controller/src/client-config-api-service/client-config-api-service.test.ts": {
331329
"import-x/order": 1,

packages/rate-limit-controller/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- **BREAKING:** Use new `Messenger` from `@metamask/messenger` ([#6503](https://github.com/MetaMask/core/pull/6503))
13+
- Previously, `RateLimitController` accepted a `RestrictedMessenger` instance from `@metamask/base-controller`.
1214
- Bump `@metamask/utils` from `^11.2.0` to `^11.4.2` ([#6054](https://github.com/MetaMask/core/pull/6054))
1315
- Bump `@metamask/base-controller` from `^8.0.0` to `^8.3.0` ([#5722](https://github.com/MetaMask/core/pull/5722), [#6284](https://github.com/MetaMask/core/pull/6284), [#6355](https://github.com/MetaMask/core/pull/6355), [#6465](https://github.com/MetaMask/core/pull/6465))
1416

packages/rate-limit-controller/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"@metamask/base-controller": "^8.3.0",
51+
"@metamask/messenger": "^0.2.0",
5152
"@metamask/rpc-errors": "^7.0.2",
5253
"@metamask/utils": "^11.4.2"
5354
},

packages/rate-limit-controller/src/RateLimitController.test.ts

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { Messenger } from '@metamask/base-controller';
2-
3-
import type {
4-
RateLimitControllerActions,
5-
RateLimitControllerEvents,
6-
} from './RateLimitController';
1+
import {
2+
Messenger,
3+
MOCK_ANY_NAMESPACE,
4+
type MessengerActions,
5+
type MessengerEvents,
6+
type MockAnyNamespace,
7+
} from '@metamask/messenger';
8+
9+
import type { RateLimitMessenger } from './RateLimitController';
710
import { RateLimitController } from './RateLimitController';
811

9-
const name = 'RateLimitController';
12+
const controllerName = 'RateLimitController';
1013

1114
const implementations = {
1215
apiWithoutCustomLimit: {
@@ -21,29 +24,48 @@ const implementations = {
2124

2225
type RateLimitedApis = typeof implementations;
2326

27+
type AllRateLimitControllerActions = MessengerActions<
28+
RateLimitMessenger<RateLimitedApis>
29+
>;
30+
31+
type AllRateLimitControllerEvents = MessengerEvents<
32+
RateLimitMessenger<RateLimitedApis>
33+
>;
34+
35+
type RootMessenger = Messenger<
36+
MockAnyNamespace,
37+
AllRateLimitControllerActions,
38+
AllRateLimitControllerEvents
39+
>;
40+
2441
/**
25-
* Constructs an unrestricted messenger.
42+
* Creates and returns a root messenger for testing
2643
*
27-
* @returns An unrestricted messenger.
44+
* @returns A messenger instance
2845
*/
29-
function getUnrestrictedMessenger() {
30-
return new Messenger<
31-
RateLimitControllerActions<RateLimitedApis>,
32-
RateLimitControllerEvents<RateLimitedApis>
33-
>();
46+
function getRootMessenger(): RootMessenger {
47+
return new Messenger({
48+
namespace: MOCK_ANY_NAMESPACE,
49+
});
3450
}
3551

3652
/**
37-
* Constructs a restricted messenger.
53+
* Constructs a messenger for the RateLimitController.
3854
*
39-
* @param messenger - An optional unrestricted messenger
40-
* @returns A restricted messenger.
55+
* @param rootMessenger - An optional root messenger
56+
* @returns A messenger for the RateLimitController.
4157
*/
42-
function getRestrictedMessenger(messenger = getUnrestrictedMessenger()) {
43-
return messenger.getRestricted({
44-
name,
45-
allowedActions: [],
46-
allowedEvents: [],
58+
function getMessenger(
59+
rootMessenger = getRootMessenger(),
60+
): RateLimitMessenger<RateLimitedApis> {
61+
return new Messenger<
62+
typeof controllerName,
63+
AllRateLimitControllerActions,
64+
AllRateLimitControllerEvents,
65+
RootMessenger
66+
>({
67+
namespace: controllerName,
68+
parent: rootMessenger,
4769
});
4870
}
4971

@@ -62,8 +84,8 @@ describe('RateLimitController', () => {
6284
});
6385

6486
it('action: RateLimitController:call', async () => {
65-
const unrestricted = getUnrestrictedMessenger();
66-
const messenger = getRestrictedMessenger(unrestricted);
87+
const rootMessenger = getRootMessenger();
88+
const messenger = getMessenger(rootMessenger);
6789

6890
// Registers action handlers
6991
new RateLimitController({
@@ -72,7 +94,7 @@ describe('RateLimitController', () => {
7294
});
7395

7496
expect(
75-
await unrestricted.call(
97+
await rootMessenger.call(
7698
'RateLimitController:call',
7799
origin,
78100
'apiWithoutCustomLimit',
@@ -88,7 +110,7 @@ describe('RateLimitController', () => {
88110
});
89111

90112
it('uses apiWithoutCustomLimit method', async () => {
91-
const messenger = getRestrictedMessenger();
113+
const messenger = getMessenger();
92114

93115
const controller = new RateLimitController({
94116
implementations,
@@ -105,7 +127,7 @@ describe('RateLimitController', () => {
105127
});
106128

107129
it('returns false if rate-limited', async () => {
108-
const messenger = getRestrictedMessenger();
130+
const messenger = getMessenger();
109131
const controller = new RateLimitController({
110132
implementations,
111133
messenger,
@@ -154,7 +176,7 @@ describe('RateLimitController', () => {
154176
});
155177

156178
it('rate limit is reset after timeout', async () => {
157-
const messenger = getRestrictedMessenger();
179+
const messenger = getMessenger();
158180
const controller = new RateLimitController({
159181
implementations,
160182
messenger,
@@ -198,7 +220,7 @@ describe('RateLimitController', () => {
198220
});
199221

200222
it('timeout is only applied once per window', async () => {
201-
const messenger = getRestrictedMessenger();
223+
const messenger = getMessenger();
202224
const controller = new RateLimitController({
203225
implementations,
204226
messenger,

packages/rate-limit-controller/src/RateLimitController.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type {
2-
ActionConstraint,
3-
RestrictedMessenger,
4-
ControllerGetStateAction,
5-
ControllerStateChangeEvent,
6-
} from '@metamask/base-controller';
7-
import { BaseController } from '@metamask/base-controller';
1+
import {
2+
BaseController,
3+
type ControllerGetStateAction,
4+
type ControllerStateChangeEvent,
5+
} from '@metamask/base-controller/next';
6+
import type { Messenger, ActionConstraint } from '@metamask/messenger';
87
import { rpcErrors } from '@metamask/rpc-errors';
98
import { getKnownPropertyNames } from '@metamask/utils';
109

1110
/**
1211
* A rate-limited API endpoint.
12+
*
1313
* @property method - The method that is rate-limited.
1414
* @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).
1515
* @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.
@@ -27,34 +27,42 @@ export type RateLimitedApiMap = Record<string, RateLimitedApi>;
2727

2828
/**
2929
* A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.
30+
*
3031
* @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.
3132
*/
3233
export type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =
3334
Record<keyof RateLimitedApis, Record<string, number>>;
3435

3536
/**
3637
* The state of the {@link RateLimitController}.
38+
*
3739
* @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.
3840
* @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.
3941
*/
4042
export type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {
4143
requests: RateLimitedRequests<RateLimitedApis>;
4244
};
4345

44-
const name = 'RateLimitController';
46+
const controllerName = 'RateLimitController';
4547

4648
export type RateLimitControllerStateChangeEvent<
4749
RateLimitedApis extends RateLimitedApiMap,
48-
> = ControllerStateChangeEvent<typeof name, RateLimitState<RateLimitedApis>>;
50+
> = ControllerStateChangeEvent<
51+
typeof controllerName,
52+
RateLimitState<RateLimitedApis>
53+
>;
4954

5055
export type RateLimitControllerGetStateAction<
5156
RateLimitedApis extends RateLimitedApiMap,
52-
> = ControllerGetStateAction<typeof name, RateLimitState<RateLimitedApis>>;
57+
> = ControllerGetStateAction<
58+
typeof controllerName,
59+
RateLimitState<RateLimitedApis>
60+
>;
5361

5462
export type RateLimitControllerCallApiAction<
5563
RateLimitedApis extends RateLimitedApiMap,
5664
> = {
57-
type: `${typeof name}:call`;
65+
type: `${typeof controllerName}:call`;
5866
handler: RateLimitController<RateLimitedApis>['call'];
5967
};
6068

@@ -69,12 +77,10 @@ export type RateLimitControllerEvents<
6977
> = RateLimitControllerStateChangeEvent<RateLimitedApis>;
7078

7179
export type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =
72-
RestrictedMessenger<
73-
typeof name,
80+
Messenger<
81+
typeof controllerName,
7482
RateLimitControllerActions<RateLimitedApis>,
75-
RateLimitControllerEvents<RateLimitedApis>,
76-
never,
77-
never
83+
RateLimitControllerEvents<RateLimitedApis>
7884
>;
7985

8086
const metadata = {
@@ -87,7 +93,7 @@ const metadata = {
8793
export class RateLimitController<
8894
RateLimitedApis extends RateLimitedApiMap,
8995
> extends BaseController<
90-
typeof name,
96+
typeof controllerName,
9197
RateLimitState<RateLimitedApis>,
9298
RateLimitMessenger<RateLimitedApis>
9399
> {
@@ -126,7 +132,7 @@ export class RateLimitController<
126132
>((acc, key) => ({ ...acc, [key]: {} }), {} as never),
127133
};
128134
super({
129-
name,
135+
name: controllerName,
130136
metadata,
131137
messenger,
132138
state: { ...defaultState, ...state },
@@ -135,8 +141,8 @@ export class RateLimitController<
135141
this.rateLimitTimeout = rateLimitTimeout;
136142
this.rateLimitCount = rateLimitCount;
137143

138-
this.messagingSystem.registerActionHandler(
139-
`${name}:call`,
144+
this.messenger.registerActionHandler(
145+
`${controllerName}:call`,
140146
(
141147
origin: string,
142148
type: keyof RateLimitedApis,
@@ -151,6 +157,7 @@ export class RateLimitController<
151157
* @param origin - The requesting origin.
152158
* @param type - The type of API call to make.
153159
* @param args - Arguments for the API call.
160+
* @returns The result of the API call.
154161
*/
155162
async call<ApiType extends keyof RateLimitedApis>(
156163
origin: string,

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4316,6 +4316,7 @@ __metadata:
43164316
dependencies:
43174317
"@metamask/auto-changelog": "npm:^3.4.4"
43184318
"@metamask/base-controller": "npm:^8.3.0"
4319+
"@metamask/messenger": "npm:^0.2.0"
43194320
"@metamask/rpc-errors": "npm:^7.0.2"
43204321
"@metamask/utils": "npm:^11.4.2"
43214322
"@types/jest": "npm:^27.4.1"

0 commit comments

Comments
 (0)