Skip to content

Commit c351602

Browse files
feat: Add legacy createOriginMiddleware (#8734)
## Explanation Recently we added `createOriginMiddleware` to `v2` for usage in the clients. Though using it via `asLegacyMiddleware` introduces some complications, so for the time being we introduce the same middleware using the legacy API. ## 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 --> ## 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/processes/updating-changelogs.md) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Low risk additive change: introduces a new legacy middleware and export without altering existing middleware behavior, with a small chance of conflicts for consumers that already attach an `origin` field to requests. > > **Overview** > Adds a legacy `createOriginMiddleware` for `@metamask/json-rpc-engine` that mutates the JSON-RPC request object to include an `origin` string (marked *deprecated* in favor of the v2 API). > > Exports the new utility from the package entrypoint, updates export snapshot tests, adds a unit test verifying `origin` is set, and records the addition in the package changelog. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 710f931. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent ec46875 commit c351602

5 files changed

Lines changed: 52 additions & 0 deletions

File tree

packages/json-rpc-engine/CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add legacy `createOriginMiddleware` utility ([#8734](https://github.com/MetaMask/core/pull/8734))
13+
1014
## [10.3.0]
1115

1216
### Added
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { JsonRpcRequest } from '@metamask/utils';
2+
3+
import { makeRequest } from '../tests/utils';
4+
import { createOriginMiddleware } from './createOriginMiddleware';
5+
import { JsonRpcEngine } from './JsonRpcEngine';
6+
7+
describe('createOriginMiddleware', () => {
8+
it('adds the origin property to the request', async () => {
9+
const origin = 'https://metamask.io';
10+
const engine = new JsonRpcEngine();
11+
12+
engine.push(createOriginMiddleware(origin));
13+
14+
engine.push((request, response, _next, end) => {
15+
response.result = (
16+
request as unknown as JsonRpcRequest & { origin: string }
17+
).origin;
18+
end();
19+
});
20+
21+
expect(await engine.handle(makeRequest())).toStrictEqual({
22+
id: '1',
23+
jsonrpc: '2.0',
24+
result: origin,
25+
});
26+
});
27+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Json, JsonRpcRequest } from '@metamask/utils';
2+
3+
import { JsonRpcMiddleware } from './JsonRpcEngine';
4+
5+
/**
6+
* Create a middleware function that adds `origin` to the request object.
7+
*
8+
* @deprecated Use the v2 `createOriginMiddleware` instead.
9+
* @param origin - The origin.
10+
* @returns The middleware.
11+
*/
12+
export function createOriginMiddleware(
13+
origin: string,
14+
): JsonRpcMiddleware<JsonRpcRequest, Json> {
15+
return (request, _result, next) => {
16+
(request as unknown as JsonRpcRequest & { origin: string }).origin = origin;
17+
next();
18+
};
19+
}

packages/json-rpc-engine/src/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe('@metamask/json-rpc-engine', () => {
77
"asV2Middleware",
88
"createAsyncMiddleware",
99
"createMethodMiddleware",
10+
"createOriginMiddleware",
1011
"createScaffoldMiddleware",
1112
"getUniqueId",
1213
"createIdRemapMiddleware",

packages/json-rpc-engine/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type {
1010
MethodHandlerImplementation,
1111
} from './createMethodMiddleware';
1212
export { createMethodMiddleware } from './createMethodMiddleware';
13+
export { createOriginMiddleware } from './createOriginMiddleware';
1314
export { createScaffoldMiddleware } from './createScaffoldMiddleware';
1415
export { getUniqueId } from './getUniqueId';
1516
export { createIdRemapMiddleware } from './idRemapMiddleware';

0 commit comments

Comments
 (0)