Skip to content

Commit 8ffc354

Browse files
committed
Fix: Use @metamask/json-rpc-engine and @metamask/rpc-errors (#277)
* deps: @metamask/rpc-errors@^5.1.1->^6.0.0 * deps:@metamask/utils@^6.2.0->^7.1.0 * update @metamask/utils v7 usage * deps: @metamask/json-rpc-engine@^7.0.0->^7.1.1 * test: type fixes
1 parent 25014a0 commit 8ffc354

File tree

5 files changed

+130
-106
lines changed

5 files changed

+130
-106
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
"test:watch": "jest --watch"
3838
},
3939
"dependencies": {
40-
"@metamask/json-rpc-engine": "^7.0.0",
40+
"@metamask/json-rpc-engine": "^7.1.1",
4141
"@metamask/object-multiplex": "^1.1.0",
42-
"@metamask/rpc-errors": "^5.1.1",
42+
"@metamask/rpc-errors": "^6.0.0",
4343
"@metamask/safe-event-emitter": "^3.0.0",
44-
"@metamask/utils": "^6.2.0",
44+
"@metamask/utils": "^8.1.0",
4545
"detect-browser": "^5.2.0",
4646
"extension-port-stream": "^2.1.1",
4747
"fast-deep-equal": "^3.1.3",
Lines changed: 98 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
2-
import { Json, JsonRpcFailure, JsonRpcSuccess } from '@metamask/utils';
2+
import {
3+
Json,
4+
JsonRpcFailure,
5+
JsonRpcParams,
6+
JsonRpcRequest,
7+
JsonRpcSuccess,
8+
} from '@metamask/utils';
39

410
import { createRpcWarningMiddleware } from './createRpcWarningMiddleware';
511
import messages from '../messages';
612

7-
const affected = [
13+
type Scenario = {
14+
scenario: string;
15+
method: string;
16+
warning?: string;
17+
params?: JsonRpcParams;
18+
};
19+
20+
const affected: Scenario[] = [
821
{
922
scenario: 'eth_decrypt',
1023
method: 'eth_decrypt',
@@ -35,7 +48,7 @@ const affected = [
3548
},
3649
];
3750

38-
const unaffected = [
51+
const unaffected: Scenario[] = [
3952
{
4053
scenario: 'eth_chainId',
4154
method: 'eth_chainId',
@@ -51,67 +64,84 @@ const unaffected = [
5164
];
5265

5366
describe('createRpcWarningMiddleware', () => {
54-
describe.each(affected)('$scenario', ({ method, params = {}, warning }) => {
55-
it('should warn the first time the method is called', async () => {
56-
const consoleWarnSpy = jest.spyOn(globalThis.console, 'warn');
57-
const middleware = createRpcWarningMiddleware(globalThis.console);
58-
const engine = new JsonRpcEngine();
59-
engine.push(middleware);
60-
61-
await engine.handle({ jsonrpc: '2.0', id: 1, method, params });
62-
63-
expect(consoleWarnSpy).toHaveBeenCalledWith(warning);
64-
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
65-
});
66-
67-
it('should not warn the second time the method is called', async () => {
68-
const consoleWarnSpy = jest.spyOn(globalThis.console, 'warn');
69-
const middleware = createRpcWarningMiddleware(globalThis.console);
70-
const engine = new JsonRpcEngine();
71-
engine.push(middleware);
72-
73-
await engine.handle({ jsonrpc: '2.0', id: 1, method, params });
74-
await engine.handle({ jsonrpc: '2.0', id: 1, method, params });
75-
76-
expect(consoleWarnSpy).toHaveBeenCalledWith(warning);
77-
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
78-
});
79-
80-
it('should allow the method to succeed', async () => {
81-
const middleware = createRpcWarningMiddleware(globalThis.console);
82-
const engine = new JsonRpcEngine();
83-
engine.push(middleware);
84-
engine.push((_req, res, _next, end) => {
85-
res.result = 'success!';
86-
end();
67+
describe.each(affected)(
68+
'$scenario',
69+
({ method, params = {}, warning }: Scenario) => {
70+
it('should warn the first time the method is called', async () => {
71+
const consoleWarnSpy = jest.spyOn(globalThis.console, 'warn');
72+
const middleware = createRpcWarningMiddleware(globalThis.console);
73+
const engine = new JsonRpcEngine();
74+
engine.push(middleware);
75+
76+
await engine.handle({
77+
jsonrpc: '2.0',
78+
id: 1,
79+
method,
80+
params,
81+
} as JsonRpcRequest);
82+
expect(consoleWarnSpy).toHaveBeenCalledWith(warning);
83+
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
8784
});
8885

89-
const response = (await engine.handle({
90-
jsonrpc: '2.0',
91-
id: 1,
92-
method,
93-
})) as JsonRpcSuccess<Json>;
94-
95-
expect(response.result).toBe('success!');
96-
});
97-
98-
it('should allow the method to fail', async () => {
99-
const middleware = createRpcWarningMiddleware(globalThis.console);
100-
const engine = new JsonRpcEngine();
101-
engine.push(middleware);
102-
engine.push(() => {
103-
throw new Error('Failure!');
86+
it('should not warn the second time the method is called', async () => {
87+
const consoleWarnSpy = jest.spyOn(globalThis.console, 'warn');
88+
const middleware = createRpcWarningMiddleware(globalThis.console);
89+
const engine = new JsonRpcEngine();
90+
engine.push(middleware);
91+
92+
await engine.handle({
93+
jsonrpc: '2.0',
94+
id: 1,
95+
method,
96+
params,
97+
} as JsonRpcRequest);
98+
await engine.handle({
99+
jsonrpc: '2.0',
100+
id: 1,
101+
method,
102+
params,
103+
} as JsonRpcRequest);
104+
105+
expect(consoleWarnSpy).toHaveBeenCalledWith(warning);
106+
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
104107
});
105108

106-
const result = (await engine.handle({
107-
jsonrpc: '2.0',
108-
id: 1,
109-
method,
110-
})) as JsonRpcFailure;
109+
it('should allow the method to succeed', async () => {
110+
const middleware = createRpcWarningMiddleware(globalThis.console);
111+
const engine = new JsonRpcEngine();
112+
engine.push(middleware);
113+
engine.push((_req, res, _next, end) => {
114+
res.result = 'success!';
115+
end();
116+
});
117+
118+
const response = (await engine.handle({
119+
jsonrpc: '2.0',
120+
id: 1,
121+
method,
122+
})) as JsonRpcSuccess<Json>;
123+
124+
expect(response.result).toBe('success!');
125+
});
111126

112-
expect(result.error.message).toBe('Failure!');
113-
});
114-
});
127+
it('should allow the method to fail', async () => {
128+
const middleware = createRpcWarningMiddleware(globalThis.console);
129+
const engine = new JsonRpcEngine();
130+
engine.push(middleware);
131+
engine.push(() => {
132+
throw new Error('Failure!');
133+
});
134+
135+
const result = (await engine.handle({
136+
jsonrpc: '2.0',
137+
id: 1,
138+
method,
139+
})) as JsonRpcFailure;
140+
141+
expect(result.error.message).toBe('Internal JSON-RPC error.');
142+
});
143+
},
144+
);
115145

116146
describe.each(unaffected)('$scenario', ({ method, params = {} }) => {
117147
it('should not issue a warning', async () => {
@@ -120,7 +150,12 @@ describe('createRpcWarningMiddleware', () => {
120150
const engine = new JsonRpcEngine();
121151
engine.push(middleware);
122152

123-
await engine.handle({ jsonrpc: '2.0', id: 1, method, params });
153+
await engine.handle({
154+
jsonrpc: '2.0',
155+
id: 1,
156+
method,
157+
params,
158+
} as JsonRpcRequest);
124159

125160
expect(consoleWarnSpy).not.toHaveBeenCalled();
126161
});
@@ -139,7 +174,7 @@ describe('createRpcWarningMiddleware', () => {
139174
id: 1,
140175
method,
141176
params,
142-
})) as JsonRpcSuccess<Json>;
177+
} as JsonRpcRequest)) as JsonRpcSuccess<Json>;
143178

144179
expect(response.result).toBe('success!');
145180
});
@@ -157,9 +192,9 @@ describe('createRpcWarningMiddleware', () => {
157192
id: 1,
158193
method,
159194
params,
160-
})) as JsonRpcFailure;
195+
} as JsonRpcRequest)) as JsonRpcFailure;
161196

162-
expect(result.error.message).toBe('Failure!');
197+
expect(result.error.message).toBe('Internal JSON-RPC error.');
163198
});
164199
});
165200
});

test/mocks/MockConnectionStream.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Json,
23
JsonRpcNotification,
34
JsonRpcRequest,
45
JsonRpcResponse,
@@ -71,7 +72,7 @@ export class MockConnectionStream extends Duplex {
7172
* @param substream - The substream this reply is included in.
7273
* @param message - The JSON RPC response.
7374
*/
74-
reply(substream: string, message: JsonRpcResponse) {
75+
reply(substream: string, message: JsonRpcResponse<Json>) {
7576
this.push({ name: substream, data: message });
7677
}
7778

@@ -81,7 +82,7 @@ export class MockConnectionStream extends Duplex {
8182
* @param substream - The substream this notification is included in.
8283
* @param message - The JSON RPC notification.
8384
*/
84-
notify(substream: string, message: JsonRpcNotification<unknown>) {
85+
notify(substream: string, message: JsonRpcNotification) {
8586
this.push({ name: substream, data: message });
8687
}
8788
}

test/mocks/MockPort.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Json,
23
JsonRpcNotification,
34
JsonRpcRequest,
45
JsonRpcResponse,
@@ -77,7 +78,7 @@ export class MockPort {
7778
* @param substream - The substream this reply is included in.
7879
* @param message - The JSON RPC response.
7980
*/
80-
reply(substream: string, message: JsonRpcResponse) {
81+
reply(substream: string, message: JsonRpcResponse<Json>) {
8182
if (!this.#connected) {
8283
throw new Error(
8384
'It is not possible to reply after the port has disconnected',

yarn.lock

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,14 +1071,14 @@ __metadata:
10711071
languageName: node
10721072
linkType: hard
10731073

1074-
"@metamask/json-rpc-engine@npm:^7.0.0":
1075-
version: 7.0.0
1076-
resolution: "@metamask/json-rpc-engine@npm:7.0.0"
1074+
"@metamask/json-rpc-engine@npm:^7.1.1":
1075+
version: 7.1.1
1076+
resolution: "@metamask/json-rpc-engine@npm:7.1.1"
10771077
dependencies:
1078-
"@metamask/rpc-errors": ^5.0.0
1079-
"@metamask/safe-event-emitter": ^2.0.0
1080-
"@metamask/utils": ^5.0.1
1081-
checksum: d22347ee4597bc72cdc34e65a27872ed8e77de188c5b95fed9133c25289cd4abd7aafa72e3e326d8a7449a857c275ed6435642727c30c51319f5d97a579c5f49
1078+
"@metamask/rpc-errors": ^6.0.0
1079+
"@metamask/safe-event-emitter": ^3.0.0
1080+
"@metamask/utils": ^8.1.0
1081+
checksum: 9dddd9142965ccd86313cda5bf13f15bf99c6c14631f93aab78de353317d548a334b5b125cdc134edd7d54e2f2e4961a0bdcd24fba997b2913083955df8fefa1
10821082
languageName: node
10831083
linkType: hard
10841084

@@ -1103,11 +1103,11 @@ __metadata:
11031103
"@metamask/eslint-config-jest": ^11.0.0
11041104
"@metamask/eslint-config-nodejs": ^11.0.1
11051105
"@metamask/eslint-config-typescript": ^11.0.0
1106-
"@metamask/json-rpc-engine": ^7.0.0
1106+
"@metamask/json-rpc-engine": ^7.1.1
11071107
"@metamask/object-multiplex": ^1.1.0
1108-
"@metamask/rpc-errors": ^5.1.1
1108+
"@metamask/rpc-errors": ^6.0.0
11091109
"@metamask/safe-event-emitter": ^3.0.0
1110-
"@metamask/utils": ^6.2.0
1110+
"@metamask/utils": ^8.1.0
11111111
"@types/chrome": ^0.0.233
11121112
"@types/jest": ^28.1.6
11131113
"@types/node": ^17.0.23
@@ -1145,13 +1145,13 @@ __metadata:
11451145
languageName: unknown
11461146
linkType: soft
11471147

1148-
"@metamask/rpc-errors@npm:^5.0.0, @metamask/rpc-errors@npm:^5.1.1":
1149-
version: 5.1.1
1150-
resolution: "@metamask/rpc-errors@npm:5.1.1"
1148+
"@metamask/rpc-errors@npm:^6.0.0":
1149+
version: 6.0.0
1150+
resolution: "@metamask/rpc-errors@npm:6.0.0"
11511151
dependencies:
1152-
"@metamask/utils": ^5.0.0
1152+
"@metamask/utils": ^8.0.0
11531153
fast-safe-stringify: ^2.0.6
1154-
checksum: ccd1b24da66af3ae63960b79c04b86efb8b96acb89ca6f7e0bbfe636d23ba5cddeba533c0692eafb87c44ec6f840085372d0f21b39e05df9a80700ff61538a30
1154+
checksum: 7e1ee1a98972266af4a34f0bbc842cdc11dc565056f0b8fbc93aa95663a7027eab8ff1fecbe3e09c38a1dc199f8219a6c69b2237015b2fdb8de0e5b35027c3f8
11551155
languageName: node
11561156
linkType: hard
11571157

@@ -1169,30 +1169,17 @@ __metadata:
11691169
languageName: node
11701170
linkType: hard
11711171

1172-
"@metamask/utils@npm:^5.0.0, @metamask/utils@npm:^5.0.1":
1173-
version: 5.0.2
1174-
resolution: "@metamask/utils@npm:5.0.2"
1175-
dependencies:
1176-
"@ethereumjs/tx": ^4.1.2
1177-
"@types/debug": ^4.1.7
1178-
debug: ^4.3.4
1179-
semver: ^7.3.8
1180-
superstruct: ^1.0.3
1181-
checksum: eca82e42911b2840deb4f32f0f215c5ffd14d22d68afbbe92d3180e920e509e310777b15eab29def3448f3535b66596ceb4c23666ec846adacc8e1bb093ff882
1182-
languageName: node
1183-
linkType: hard
1184-
1185-
"@metamask/utils@npm:^6.2.0":
1186-
version: 6.2.0
1187-
resolution: "@metamask/utils@npm:6.2.0"
1172+
"@metamask/utils@npm:^8.0.0, @metamask/utils@npm:^8.1.0":
1173+
version: 8.1.0
1174+
resolution: "@metamask/utils@npm:8.1.0"
11881175
dependencies:
11891176
"@ethereumjs/tx": ^4.1.2
11901177
"@noble/hashes": ^1.3.1
11911178
"@types/debug": ^4.1.7
11921179
debug: ^4.3.4
1193-
semver: ^7.3.8
1180+
semver: ^7.5.4
11941181
superstruct: ^1.0.3
1195-
checksum: 0bc675358ecc09b3bc04da613d73666295d7afa51ff6b8554801585966900b24b8545bd93b8b2e9a17db867ebe421fe884baf3558ec4ca3199fa65504f677c1b
1182+
checksum: 4cbee36d0c227f3e528930e83f75a0c6b71b55b332c3e162f0e87f3dd86ae017d0b20405d76ea054ab99e4d924d3d9b8b896ed12a12aae57b090350e5a625999
11961183
languageName: node
11971184
linkType: hard
11981185

@@ -6257,14 +6244,14 @@ __metadata:
62576244
languageName: node
62586245
linkType: hard
62596246

6260-
"semver@npm:7.x, semver@npm:^7.3.2, semver@npm:^7.3.5, semver@npm:^7.3.7, semver@npm:^7.3.8":
6261-
version: 7.5.0
6262-
resolution: "semver@npm:7.5.0"
6247+
"semver@npm:7.x, semver@npm:^7.3.2, semver@npm:^7.3.5, semver@npm:^7.3.7, semver@npm:^7.3.8, semver@npm:^7.5.4":
6248+
version: 7.5.4
6249+
resolution: "semver@npm:7.5.4"
62636250
dependencies:
62646251
lru-cache: ^6.0.0
62656252
bin:
62666253
semver: bin/semver.js
6267-
checksum: 2d266937756689a76f124ffb4c1ea3e1bbb2b263219f90ada8a11aebebe1280b13bb76cca2ca96bdee3dbc554cbc0b24752eb895b2a51577aa644427e9229f2b
6254+
checksum: 12d8ad952fa353b0995bf180cdac205a4068b759a140e5d3c608317098b3575ac2f1e09182206bf2eb26120e1c0ed8fb92c48c592f6099680de56bb071423ca3
62686255
languageName: node
62696256
linkType: hard
62706257

0 commit comments

Comments
 (0)