Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Handle JSON-RPC notifications #104

Merged
merged 9 commits into from
Aug 15, 2022
Merged

Handle JSON-RPC notifications #104

merged 9 commits into from
Aug 15, 2022

Conversation

rekmarks
Copy link
Member

@rekmarks rekmarks commented Apr 8, 2022

This PR adds JSON-RPC 2.0-compliant notification handling for JsonRpcEngine.

  • JSON-RPC notifications are defined as JSON-RPC request objects without an id property.
  • A new constructor parameter, notificationHandler, is introduced. This parameter is a function that accepts JSON-RPC notification objects and returns void | Promise<void>.
  • When JsonRpcEngine.handle is called, if a notificationHandler exists, any request objects duck-typed as notifications will be handled as such. This means that:
    • Validation errors that occur after duck-typing will be ignored. At the moment, this just means that no error will be thrown if the method field is not a string.
    • If basic validation succeeds, the notification object will be passed to the handler function without touching the middleware stack.
    • The response from handle() will be undefined.
    • No error will be returned or thrown, unless the notification handler itself throws or rejects.
      • Notification handlers should not throw or reject, and it is the implementer's responsibility to ensure that they do not.
  • If JsonRpcEngine.handle is called and no notificationHandler exists, notifications will be treated just like requests. This is the current behavior.

@rekmarks rekmarks changed the base branch from main to use-jest April 8, 2022 18:38
@rekmarks rekmarks force-pushed the handle-notifications branch 3 times, most recently from 9c89dca to be37c73 Compare April 8, 2022 19:14
Base automatically changed from use-jest to main May 16, 2022 01:06
@rekmarks rekmarks force-pushed the handle-notifications branch 2 times, most recently from e904f32 to 99bc8d7 Compare May 16, 2022 04:33
@rekmarks rekmarks force-pushed the handle-notifications branch from bd9c430 to fdd6da9 Compare May 16, 2022 23:39
@rekmarks rekmarks marked this pull request as ready for review May 18, 2022 00:35
@rekmarks rekmarks requested a review from a team as a code owner May 18, 2022 00:35
@rekmarks rekmarks force-pushed the handle-notifications branch 3 times, most recently from 0569f2d to d7e6984 Compare May 18, 2022 05:11
@rekmarks rekmarks force-pushed the handle-notifications branch from d7e6984 to 907a6da Compare May 18, 2022 05:11
Copy link

@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.

Did a quick pass over this. I'll try to look at this more closely tomorrow.

src/JsonRpcEngine.ts Outdated Show resolved Hide resolved
src/JsonRpcEngine.ts Outdated Show resolved Hide resolved
src/JsonRpcEngine.ts Outdated Show resolved Hide resolved
src/JsonRpcEngine.ts Show resolved Hide resolved
@rekmarks rekmarks requested a review from mcmire August 12, 2022 07:08
@shanejonas
Copy link

I think we should note that this is for "client side" sending notifications to the server. I believe "server side" notifications end up being an event emitter which isnt in json-rpc-engine but json-rpc-middleware-stream: https://github.com/MetaMask/json-rpc-middleware-stream/blob/main/src/createStreamMiddleware.ts#L71

Copy link

@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.

JsonRpcEngine sure is getting complicated isn't it 😅 Still, these seem like reasonable updates. LGTM.

```js
const engine = new JsonRpcEngine({ notificationHandler });

// A notification is defined as a JSON-RPC request without an `id` property.
Copy link

Choose a reason for hiding this comment

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

This seems like it could cause all of kinds of runtime bugs. Glad we're using TypeScript 😬

}

const req: JsonRpcRequest<unknown> = { ...callerReq };
// Handle notifications.
// We can't use isJsonRpcNotification here because that narrows callerReq to
Copy link

Choose a reason for hiding this comment

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

I am not 100% sure on this, but my guess is that it's because there is no correlation between this._notificationHandler and the type of callerReq. If there is a way to tell TypeScript that when this_.notificationHandler is given then all requests must be notifications then this could work, but I'm not sure quite how to do that. This seems sufficient. I appreciate the comment!

@rekmarks rekmarks merged commit 040a6ba into main Aug 15, 2022
@rekmarks rekmarks deleted the handle-notifications branch August 15, 2022 22:59
@rekmarks
Copy link
Member Author

@shanejonas not when I'm done with it 😎
MetaMask/json-rpc-middleware-stream#24

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants