Skip to content

Conversation

@rekmarks
Copy link
Member

@rekmarks rekmarks commented Nov 5, 2025

Explanation

Adds a MiddlewareContext parameter to InternalProvider.request() and JsonRpcServer.handle(). This is essentially a missing feature from the JsonRpcEngineV2 implementation / migration, since callers are no longer able to add non-JSON-RPC properties to request objects and instead need to use the context object.

As part of this, permit specifying a plain object as the context to avoid forcing callers to import MiddlewareContext whenever they want to make a JSON-RPC call with some particular context. Also opportunistically fixes a bug with the static isInstance methods of V2 engine classes, where we wouldn't walk the prototype chain when checking for the symbol property.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

Note

Adds a context option to InternalProvider.request()/JsonRpcServer.handle(), enables passing plain-object contexts across the V2 engine, makes PollingBlockTracker context-generic, and narrows Network Controller’s Provider type; updates tests/docs accordingly.

  • JSON-RPC Engine (v2):
    • Add HandleOptions with context (accepts MiddlewareContext or plain object) and forward it through handle() and JsonRpcServer.handle().
    • Enhance MiddlewareContext (construct from KeyValues object, isInstance) and export InferKeyValues; add shared isInstance util; minor server refactor (static request coercion).
    • Update README and tests for context passing and utils.
  • eth-json-rpc-provider:
    • Make InternalProvider generic over Context; request() accepts { context } and forwards to engine.
    • Update tests and CHANGELOG.
  • eth-block-tracker:
    • Genericize PollingBlockTracker with Context; type provider as InternalProvider<Context>; update CHANGELOG.
  • Network Controller:
    • Narrow Provider to InternalProvider<MiddlewareContext<{ origin: string; skipCache: boolean } & Record<string, unknown>>>.
    • Type updates in create-network-client, tests, and helpers; update CHANGELOG.
  • Tests/Utilities:
    • Update fakes (FakeProvider, FakeBlockTracker) and consumers to new generic/context types.
    • Bridge controller test now uses @metamask/network-controller Provider.
  • Deps:
    • Add @metamask/network-controller to root package.json.

Written by Cursor Bugbot for commit c853283. This will update automatically on new commits. Configure here.

@rekmarks rekmarks force-pushed the rekm/jrpce-internal-provider-context branch from 922dafc to 36a1cb5 Compare November 5, 2025 07:26
Base automatically changed from rekm/ejrpcp-v2 to main November 5, 2025 19:18
@rekmarks rekmarks force-pushed the rekm/jrpce-internal-provider-context branch from dd3379c to 86dc3b8 Compare November 5, 2025 19:19
@rekmarks rekmarks force-pushed the rekm/jrpce-internal-provider-context branch from 86dc3b8 to c0ef5d0 Compare November 5, 2025 19:29
@rekmarks rekmarks changed the title feat: Add MiddlewareContext generic to InternalProvider feat: Add context parameter to InternalProvider and JsonRpcServer methods Nov 5, 2025
@rekmarks rekmarks marked this pull request as ready for review November 5, 2025 19:34
@rekmarks rekmarks requested review from a team as code owners November 5, 2025 19:34
@rekmarks rekmarks requested a review from Gudahtt November 5, 2025 20:20
@rekmarks rekmarks requested a review from mcmire November 5, 2025 23:03
@rekmarks rekmarks force-pushed the rekm/jrpce-internal-provider-context branch from 1273941 to c2632f3 Compare November 5, 2025 23:34
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question, but overall looks good to me.

Comment on lines +43 to +44
Context extends ContextConstraint = MiddlewareContext,
>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Indentation?

Suggested change
Context extends ContextConstraint = MiddlewareContext,
>
Context extends ContextConstraint = MiddlewareContext,
>

Copy link
Member Author

@rekmarks rekmarks Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what Prettier wants (there are no lint warnings or errors in the entire package).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boo. Fine then 😆

}

#coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall {
static #coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know you could have a static private property 🤔

Thoughts on making it public? Or maybe it doesn't need to be a static property on JsonRpcServer, it can be a function in utils?

Copy link
Member Author

@rekmarks rekmarks Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, static private properties are neat! I only made this one static because I noticed that I could.

I don't really want to make the method public, because using it "correctly" requires capturing the original id beforehand as we do in handle(). I could make it into a external utility function and not export it if you have a strong preference for that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, for some reason I thought it was being referenced outside of this class and got confused how that was working.

I don't have a strong preference for this in that case.

console.log(result); // 'bar'
```

You can also pass a plain object as a shorthand for a `MiddlewareContext` instance:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice feature :)

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rekmarks rekmarks enabled auto-merge (squash) November 6, 2025 19:52
@rekmarks rekmarks disabled auto-merge November 6, 2025 20:37
@rekmarks
Copy link
Member Author

rekmarks commented Nov 6, 2025

Skipping review from @MetaMask/swaps-engineers; we changed a type in a test.

@rekmarks rekmarks merged commit 1894123 into main Nov 6, 2025
273 of 277 checks passed
@rekmarks rekmarks deleted the rekm/jrpce-internal-provider-context branch November 6, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants