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

deps: Switch from json-rpc-engine to @metamask/json-rpc-engine #16

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/safe-event-emitter": "^2.0.0",
"json-rpc-engine": "^6.1.0"
"@metamask/json-rpc-engine": "^7.0.0",
"@metamask/safe-event-emitter": "^3.0.0",
"@metamask/utils": "^5.0.1"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^2.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/provider-from-engine.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonRpcEngine } from 'json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { promisify } from 'util';

import { providerFromEngine } from './provider-from-engine';
Expand Down
2 changes: 1 addition & 1 deletion src/provider-from-engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JsonRpcEngine } from 'json-rpc-engine';
import type { JsonRpcEngine } from '@metamask/json-rpc-engine';

import { SafeEventEmitterProvider } from './safe-event-emitter-provider';

Expand Down
9 changes: 2 additions & 7 deletions src/provider-from-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { JsonRpcMiddleware } from 'json-rpc-engine';
import { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import { promisify } from 'util';

import { providerFromMiddleware } from './provider-from-middleware';

describe('providerFromMiddleware', () => {
it('handle a successful request', async () => {
const middleware: JsonRpcMiddleware<unknown, unknown> = (
_req,
res,
_next,
end,
) => {
const middleware: JsonRpcMiddleware<any, any> = (_req, res, _next, end) => {
res.result = 42;
end();
};
Expand Down
12 changes: 7 additions & 5 deletions src/provider-from-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JsonRpcEngine } from 'json-rpc-engine';
import type { JsonRpcMiddleware } from 'json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import type { Json, JsonRpcParams } from '@metamask/utils';

import { providerFromEngine } from './provider-from-engine';
import type { SafeEventEmitterProvider } from './safe-event-emitter-provider';
Expand All @@ -10,9 +11,10 @@ import type { SafeEventEmitterProvider } from './safe-event-emitter-provider';
* @param middleware - The middleware to construct a provider from.
* @returns An Ethereum provider.
*/
export function providerFromMiddleware(
middleware: JsonRpcMiddleware<unknown, unknown>,
): SafeEventEmitterProvider {
export function providerFromMiddleware<
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, this is much better.

Params extends JsonRpcParams,
Result extends Json,
>(middleware: JsonRpcMiddleware<Params, Result>): SafeEventEmitterProvider {
const engine: JsonRpcEngine = new JsonRpcEngine();
engine.push(middleware);
const provider: SafeEventEmitterProvider = providerFromEngine(engine);
Expand Down
3 changes: 2 additions & 1 deletion src/safe-event-emitter-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonRpcEngine } from 'json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { promisify } from 'util';

import { SafeEventEmitterProvider } from './safe-event-emitter-provider';
Expand All @@ -14,6 +14,7 @@ describe('SafeEventEmitterProvider', () => {
// `json-rpc-engine` v6 does not support JSON-RPC notifications directly,
// so this is the best way to emulate this behavior.
// We should replace this with `await engine.handle(notification)` when we update to v7
// TODO: v7 is now integrated; fix this
engine.emit('notification', 'test');

expect(notificationListener).toHaveBeenCalledWith(null, 'test');
Expand Down
7 changes: 4 additions & 3 deletions src/safe-event-emitter-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { JsonRpcEngine } from '@metamask/json-rpc-engine';
import SafeEventEmitter from '@metamask/safe-event-emitter';
import type { JsonRpcEngine, JsonRpcRequest } from 'json-rpc-engine';
import type { JsonRpcRequest } from '@metamask/utils';

/**
* An Ethereum provider.
Expand Down Expand Up @@ -34,7 +35,7 @@ export class SafeEventEmitterProvider extends SafeEventEmitter {
* @param callback - A function that is called upon the success or failure of the request.
*/
sendAsync = (
req: JsonRpcRequest<unknown>,
req: JsonRpcRequest,
callback: (error: unknown, providerRes?: any) => void,
) => {
this.#engine.handle(req, callback);
Expand All @@ -51,7 +52,7 @@ export class SafeEventEmitterProvider extends SafeEventEmitter {
* @param callback - A function that is called upon the success or failure of the request.
*/
send = (
req: JsonRpcRequest<unknown>,
req: JsonRpcRequest,
callback: (error: unknown, providerRes?: any) => void,
) => {
if (typeof callback !== 'function') {
Expand Down
Loading