Skip to content

Commit 2c96855

Browse files
authored
Follow up to eth-json-rpc-middleware rewrite PR (#7138)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> 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). ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> Progresses #6327. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds providerAsMiddlewareV2, deprecates OriginalRequest, and updates RPC request types to require Readonly/frozen JSON-RPC requests; tightens inflight-cache typings. > > - **eth-json-rpc-middleware**: > - **Added**: `providerAsMiddlewareV2` to convert an `InternalProvider` to v2 middleware. > - **Changed**: Changelog clarifies migration to `JsonRpcEngineV2` and updated `createWalletMiddleware` hook signatures. > - Tighten `inflight-cache` types: use `Readonly<Json>` for results, `unknown` for errors, and remove unnecessary casts. > - **message-manager**: > - **Changed/Deprecated**: Keep `OriginalRequest` as a deprecated alias of `MessageRequest` in code and changelog. > - **network-controller**: > - **BREAKING**: `request` signatures in `RpcServiceRequestable`/`RpcServiceChain` now accept `Readonly<JsonRpcRequest<...>>` (frozen requests); reflected in changelog. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit ca59fe6. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 3ef1b23 commit 2c96855

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)