Skip to content

Commit e4f2a09

Browse files
authored
feat: JsonRpcEngineV2 (#6176)
## Explanation Introduces `JsonRpcEngineV2` and `JsonRpcServer`, intended to replace all existing usage of the existing `JsonRpcEngine` implementation. For the motivation behind this change, see #6088 and/or [this ADR](https://github.com/MetaMask/decisions/blob/main/decisions/core/0013-json-rpc-engine-rewrite.md). ### Implementation In order to resolve the problems listed in the motivation, V2 engine is split in two: - `JsonRpcEngineV2` - Orchestrates middleware and composes sub-engines - JSON-RPC requests go in, result values (not JSON-RPC responses) come out - Re-throws middleware errors directly - `JsonRpcServer` - JSON-RPC requests go in, JSON-RPC responses come out - Accepts an `errorHandler` constructor parameter for capturing engine / middleware errors See the updated package `README.md` for details. ### Migration While this PR is substantial, migrating our existing JSON-RPC pipelines will be a significant project involving multiple teams over many releases cycles. To facilitate this, the PR introduces a forwards-compatibility adapter, `asV2Middleware()`, and backwards-compatibility adapter, `asLegacyMiddleware()`, for the legacy and V2 engines, respectively. In addition, all V2 exports are exposed under the `/v2` export path, making this update completely non-breaking (although all legacy exports are deprecated). ### Note to reviewers I recommend proceeding as follows: 0. Read the readme 1. `JsonRpcEngineV2` 2. `JsonRpcServer` 3. Compatibility adapter functions ## References - Closes #6088 - [ADR: Rewriting `JsonRpcEngine`](https://github.com/MetaMask/decisions/blob/main/decisions/core/0013-json-rpc-engine-rewrite.md) ## 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 - ~I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes~ - Migrating our RPC pipelines left as an exercise to the reader. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Introduce `JsonRpcEngineV2` and `JsonRpcServer` with bidirectional adapters, deprecating the legacy engine, updating docs/exports/deps, and adding comprehensive tests. > > - **Core (v2)**: > - Add `JsonRpcEngineV2` with typed middleware pipeline, immutable request/result handling, and `MiddlewareContext`. > - Add `JsonRpcServer` for spec-compliant request/notification handling and error serialization. > - Provide adapters: `asLegacyMiddleware` (v2 -> legacy) and `asV2Middleware` (legacy -> v2). > - **Exports & Packaging**: > - Expose v2 under `@metamask/json-rpc-engine/v2` (ESM/CJS), add `v2.js` shim for Browserify. > - Update `package.json` exports and add deps (`deep-freeze-strict`, `klona`, `@types/deep-freeze-strict`). > - **Docs**: > - Rewrite root `README.md` to v2 usage; add legacy docs in `src/README.md` and mark legacy APIs as deprecated. > - Update `CHANGELOG.md`: Added v2; Deprecated legacy engine/types. > - **Legacy**: > - Annotate legacy engine/types with deprecation notes; minor internal tweaks (stringify util). > - **Tests**: > - Add extensive v2, server, and adapter tests; adjust legacy tests as needed. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit eca0e39. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 1577c9b commit e4f2a09

34 files changed

+4856
-147
lines changed

eslint-warning-thresholds.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,6 @@
287287
"packages/gas-fee-controller/src/determineGasFeeCalculations.ts": {
288288
"jsdoc/tag-lines": 4
289289
},
290-
"packages/json-rpc-engine/src/JsonRpcEngine.test.ts": {
291-
"jest/no-conditional-in-test": 2
292-
},
293-
"packages/json-rpc-engine/src/JsonRpcEngine.ts": {
294-
"@typescript-eslint/prefer-promise-reject-errors": 2
295-
},
296290
"packages/json-rpc-middleware-stream/src/index.test.ts": {
297291
"@typescript-eslint/prefer-promise-reject-errors": 3,
298292
"no-empty-function": 1

packages/json-rpc-engine/CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Added
11+
12+
- `JsonRpcEngineV2` ([#6176](https://github.com/MetaMask/core/pull/6176))
13+
- This is a complete rewrite of `JsonRpcEngine`, intended to replace the original implementation.
14+
See the readme for details.
15+
1016
## [10.1.1]
1117

1218
### Changed
@@ -20,6 +26,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2026
- Bump `@metamask/utils` from `^11.2.0` to `^11.4.2` ([#6054](https://github.com/MetaMask/core/pull/6054))
2127
- Bump `@metamask/utils` from `^11.4.2` to `^11.8.0` ([#6588](https://github.com/MetaMask/core/pull/6588))
2228

29+
### Deprecated
30+
31+
- `JsonRpcEngine` and related types ([#6176](https://github.com/MetaMask/core/pull/6176))
32+
- To be replaced by `JsonRpcEngineV2`.
33+
2334
## [10.0.3]
2435

2536
### Changed

0 commit comments

Comments
 (0)