Skip to content

feat: Add context parameter to InternalProvider and JsonRpcServer methods - #7061

Merged
rekmarks merged 18 commits into
mainfrom
rekm/jrpce-internal-provider-context
Nov 6, 2025
Merged

feat: Add context parameter to InternalProvider and JsonRpcServer methods#7061
rekmarks merged 18 commits into
mainfrom
rekm/jrpce-internal-provider-context

Conversation

@rekmarks

@rekmarksrekmarks commented Nov 5, 2025

Copy link
Copy Markdown
Member

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-controllerProvider.
  • 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
rekmarksforce-pushed the rekm/jrpce-internal-provider-context branch from 922dafc to 36a1cb5CompareNovember 5, 2025 07:26
Base automatically changed from rekm/ejrpcp-v2 to mainNovember 5, 2025 19:18
@rekmarks
rekmarksforce-pushed the rekm/jrpce-internal-provider-context branch from dd3379c to 86dc3b8CompareNovember 5, 2025 19:19
@rekmarks
rekmarksforce-pushed the rekm/jrpce-internal-provider-context branch from 86dc3b8 to c0ef5d0CompareNovember 5, 2025 19:29
@rekmarksrekmarks changed the title feat: Add MiddlewareContext generic to InternalProviderfeat: Add context parameter to InternalProvider and JsonRpcServer methodsNov 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 ownersNovember 5, 2025 19:34
Comment threadpackages/json-rpc-engine/src/v2/MiddlewareContext.ts
Comment threadpackages/json-rpc-engine/src/v2/MiddlewareContext.ts
Comment threadpackages/json-rpc-engine/src/v2/MiddlewareContext.ts
@rekmarks
rekmarks requested a review from GudahttNovember 5, 2025 20:20
Comment threadpackages/json-rpc-engine/src/v2/MiddlewareContext.ts
@rekmarks
rekmarks requested a review from mcmireNovember 5, 2025 23:03
Comment threadpackages/json-rpc-engine/src/v2/utils.ts
@rekmarks
rekmarksforce-pushed the rekm/jrpce-internal-provider-context branch from 1273941 to c2632f3CompareNovember 5, 2025 23:34

@mcmiremcmire left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: Indentation?

Suggested change
ContextextendsContextConstraint=MiddlewareContext,
>
ContextextendsContextConstraint=MiddlewareContext,
>

@rekmarksrekmarksNov 6, 2025

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
Collaborator

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
Copy Markdown
Collaborator

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?

@rekmarksrekmarksNov 6, 2025

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
Collaborator

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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice feature :)

@mcmiremcmire left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
MemberAuthor

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

@rekmarks
rekmarks merged commit 1894123 into mainNov 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 freeto 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.

2 participants

@rekmarks@mcmire