Skip to content

Commit ca59fe6

Browse files
committed
Follow up to eth-json-rpc-middleware rewrite PR
The following changes extend c26899a: - `eth-json-rpc-middleware` - There were some unnecessary type assertions in `inflight-cache.ts` that have been removed. - `message-manager` - The rename of `OriginalRequest` introduced a breaking change. We don't strictly need to do this now, so we keep the old type around for backward compatibility. - `network-controller` - The changelog has been updated to mention that a new export was added, and to detail more changes to `createWalletMiddleware`. - The signatures of `request` in `RpcServiceRequestable` and `RpcServiceChain` were updated to match the same method in `RpcService` (and the change to `RpcServiceRequestable` was marked as breaking).
1 parent c26899a commit ca59fe6

File tree

7 files changed

+42
-18
lines changed

7 files changed

+42
-18
lines changed

packages/eth-json-rpc-middleware/CHANGELOG.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add new function `providerAsMiddlewareV2` for converting an `InternalProvider` into a `JsonRpcEngine` v2-compatible middleware ([#7138](https://github.com/MetaMask/core/pull/7138))
13+
1014
### Changed
1115

12-
- **BREAKING:** Migrate to `JsonRpcEngineV2` ([#7065](https://github.com/MetaMask/core/pull/7065))
13-
- Migrates all middleware from `JsonRpcEngine` to `JsonRpcEngineV2`.
14-
- Signatures of various middleware dependencies, e.g. `processTransaction` of `createWalletMiddleware`, have changed
15-
and must be updated by consumers.
16-
- Be advised that request objects are now deeply frozen, and cannot be mutated.
16+
- **BREAKING:** Migrate all middleware from `JsonRpcEngine` to `JsonRpcEngineV2` ([#7065](https://github.com/MetaMask/core/pull/7065))
1717
- To continue using this package with the legacy `JsonRpcEngine`, use the `asLegacyMiddleware` backwards compatibility function.
18+
- **BREAKING:** Change the signatures of hooks for `createWalletMiddleware` ([#7065](https://github.com/MetaMask/core/pull/7065))
19+
- To wit:
20+
- `getAccounts` takes an origin argument (`string`) instead of a `JsonRpcRequest`
21+
- `processDecryptMessage` and `processEncryptionPublicKey` take a `MessageRequest` from `@metamask/message-manager` instead of `JsonRpcRequest`
22+
- `processPersonalMessage`, `processTransaction`, `processSignTransaction`, `processTypedMessage`, `processTypedMessageV3` and `processTypedMessageV4` take a `context` as the third argument, before any other arguments
23+
- Be advised that request objects are now deeply frozen, and cannot be mutated.
1824
- **BREAKING:** Use `InternalProvider` instead of `SafeEventEmitterProvider` ([#6796](https://github.com/MetaMask/core/pull/6796))
1925
- Wherever a `SafeEventEmitterProvider` was expected, an `InternalProvider` is now expected instead.
2026
- **BREAKING:** Stop retrying `undefined` results for methods that include a block tag parameter ([#7001](https://github.com/MetaMask/core/pull/7001))
2127
- The `retryOnEmpty` middleware will now throw an error if it encounters an `undefined` result when dispatching
2228
a request with a later block number than the originally requested block number.
2329
- In practice, this should happen rarely if ever.
24-
- Migrate all uses of `interface` to `type` ([#6885](https://github.com/MetaMask/core/pull/6885))
30+
- **BREAKING:** Migrate all uses of `interface` to `type` ([#6885](https://github.com/MetaMask/core/pull/6885))
2531

2632
## [21.0.0]
2733

packages/eth-json-rpc-middleware/src/inflight-cache.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
import { projectLogger, createModuleLogger } from './logging-utils';
1212
import { cacheIdentifierForRequest } from './utils/cache';
1313

14-
type RequestHandler = [(result: Json) => void, (error: Error) => void];
14+
type RequestHandler = [
15+
(result: Readonly<Json>) => void,
16+
(error: unknown) => void,
17+
];
1518

1619
type InflightRequest = {
1720
[cacheId: string]: RequestHandler[];
@@ -64,7 +67,7 @@ export function createInflightCacheMiddleware(): JsonRpcMiddleware<
6467
// allow request to be handled normally
6568
log('Carrying original request forward %o', request);
6669
try {
67-
const result = (await next()) as Json;
70+
const result = (await next()) as Readonly<Json>;
6871
log(
6972
'Running %i collected handler(s) for successful request %o',
7073
activeRequestHandlers.length,
@@ -78,7 +81,7 @@ export function createInflightCacheMiddleware(): JsonRpcMiddleware<
7881
activeRequestHandlers.length,
7982
request,
8083
);
81-
runRequestHandlers({ error: error as Error }, activeRequestHandlers);
84+
runRequestHandlers({ error }, activeRequestHandlers);
8285
throw error;
8386
} finally {
8487
delete inflightRequests[cacheId];
@@ -94,11 +97,11 @@ export function createInflightCacheMiddleware(): JsonRpcMiddleware<
9497
*/
9598
function createActiveRequestHandler(
9699
activeRequestHandlers: RequestHandler[],
97-
): Promise<Json> {
98-
const { resolve, promise, reject } = createDeferredPromise<Json>();
100+
): Promise<Readonly<Json>> {
101+
const { resolve, promise, reject } = createDeferredPromise<Readonly<Json>>();
99102
activeRequestHandlers.push([
100-
(result: Json) => resolve(result),
101-
(error: Error) => reject(error),
103+
(result: Readonly<Json>) => resolve(result),
104+
(error: unknown) => reject(error),
102105
]);
103106
return promise;
104107
}
@@ -110,7 +113,7 @@ function createActiveRequestHandler(
110113
* @param activeRequestHandlers - The active request handlers.
111114
*/
112115
function runRequestHandlers(
113-
resultOrError: { result: Json } | { error: Error },
116+
resultOrError: { result: Readonly<Json> } | { error: unknown },
114117
activeRequestHandlers: RequestHandler[],
115118
): void {
116119
// use setTimeout so we can handle the original request first

packages/message-manager/CHANGELOG.md

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

1212
- Rename `OriginalRequest` type to `MessageRequest` and permit string `id` values ([#7065](https://github.com/MetaMask/core/pull/7065))
1313
- Previously, only number values were permitted for the `id` property.
14+
- `OriginalRequest` is kept for backward compatibility.
15+
16+
### Deprecated
17+
18+
- Deprecate `OriginalRequest`; use `MessageRequest` instead ([#7138](https://github.com/MetaMask/core/pull/7138))
1419

1520
## [14.0.0]
1621

packages/message-manager/src/AbstractMessageManager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export type MessageRequest = {
4545
securityAlertResponse?: Record<string, Json>;
4646
};
4747

48+
/**
49+
* Represents the request adding a message.
50+
*
51+
* @deprecated Please use `MessageRequest` instead.
52+
*/
53+
export type OriginalRequest = MessageRequest;
54+
4855
/**
4956
* @type AbstractMessage
5057
*

packages/network-controller/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- In practice, this should happen rarely if ever.
2020
- **BREAKING:** Migrate `NetworkClient` to `JsonRpcEngineV2` ([#7065](https://github.com/MetaMask/core/pull/7065))
2121
- This ought to be unobservable, but we mark it as breaking out of an abundance of caution.
22+
- **BREAKING:** Update signature of `request` in `AbstractRpcService` and `RpcServiceRequestable` so that the JSON-RPC request must be frozen ([#7138](https://github.com/MetaMask/core/pull/7138))
2223
- Bump `@metamask/controller-utils` from `^11.14.1` to `^11.15.0` ([#7003](https://github.com/MetaMask/core/pull/7003))
2324

2425
### Fixed

packages/network-controller/src/rpc-service/rpc-service-chain.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export class RpcServiceChain implements RpcServiceRequestable {
106106
* @throws A "parse" error if the response is not valid JSON.
107107
*/
108108
async request<Params extends JsonRpcParams, Result extends Json>(
109-
jsonRpcRequest: JsonRpcRequest<Params> & { method: 'eth_getBlockByNumber' },
109+
jsonRpcRequest: Readonly<JsonRpcRequest<Params>> & {
110+
method: 'eth_getBlockByNumber';
111+
},
110112
fetchOptions?: FetchOptions,
111113
): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>>;
112114

@@ -129,12 +131,12 @@ export class RpcServiceChain implements RpcServiceRequestable {
129131
* @throws A "parse" error if the response is not valid JSON.
130132
*/
131133
async request<Params extends JsonRpcParams, Result extends Json>(
132-
jsonRpcRequest: JsonRpcRequest<Params>,
134+
jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,
133135
fetchOptions?: FetchOptions,
134136
): Promise<JsonRpcResponse<Result>>;
135137

136138
async request<Params extends JsonRpcParams, Result extends Json>(
137-
jsonRpcRequest: JsonRpcRequest<Params>,
139+
jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,
138140
fetchOptions: FetchOptions = {},
139141
): Promise<JsonRpcResponse<Result | null>> {
140142
return this.#services[0].request(jsonRpcRequest, fetchOptions);

packages/network-controller/src/rpc-service/rpc-service-requestable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export type RpcServiceRequestable = {
6262
* Makes a request to the target.
6363
*/
6464
request<Params extends JsonRpcParams, Result extends Json>(
65-
jsonRpcRequest: JsonRpcRequest<Params>,
65+
jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,
6666
fetchOptions?: FetchOptions,
6767
): Promise<JsonRpcResponse<Result | null>>;
6868
};

0 commit comments

Comments
 (0)