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

Commit fafa136

Browse files
update to @metamask/json-rpc-engine (#54)
--------- Co-authored-by: Frederik Bolding <[email protected]>
1 parent 1971a09 commit fafa136

File tree

7 files changed

+203
-320
lines changed

7 files changed

+203
-320
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# json-rpc-middleware-stream
22

3-
A small toolset for streaming JSON RPC data and matching requests and responses. Made to be used with [`json-rpc-engine`](https://npmjs.com/package/json-rpc-engine).
3+
A small toolset for streaming JSON RPC data and matching requests and responses. Made to be used with [`@metamask/json-rpc-engine`](https://npmjs.com/package/@metamask/json-rpc-engine).

jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ module.exports = {
2121
// An object that configures minimum threshold enforcement for coverage results
2222
coverageThreshold: {
2323
global: {
24-
branches: 91.3,
24+
branches: 90.9,
2525
functions: 100,
26-
lines: 98.51,
27-
statements: 98.51,
26+
lines: 98.56,
27+
statements: 98.56,
2828
},
2929
},
3030

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
"test:watch": "jest --watch"
2525
},
2626
"dependencies": {
27+
"@metamask/json-rpc-engine": "^7.1.1",
2728
"@metamask/safe-event-emitter": "^3.0.0",
28-
"json-rpc-engine": "^6.1.0",
29+
"@metamask/utils": "^8.1.0",
2930
"readable-stream": "^2.3.3"
3031
},
3132
"devDependencies": {
@@ -51,7 +52,6 @@
5152
"extension-port-stream": "^2.0.1",
5253
"jest": "^27.5.1",
5354
"jest-it-up": "^2.0.2",
54-
"json-rpc-engine": "^6.1.0",
5555
"prettier": "^2.2.1",
5656
"prettier-plugin-packagejson": "^2.2.17",
5757
"rimraf": "^3.0.2",

src/createEngineStream.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Duplex } from 'readable-stream';
2-
import { JsonRpcEngine, JsonRpcRequest } from 'json-rpc-engine';
2+
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
3+
import { JsonRpcRequest, JsonRpcParams } from '@metamask/utils';
34

45
interface EngineStreamOptions {
56
engine: JsonRpcEngine;
@@ -35,7 +36,7 @@ export default function createEngineStream(opts: EngineStreamOptions): Duplex {
3536
* @param cb - The stream write callback.
3637
*/
3738
function write(
38-
req: JsonRpcRequest<unknown>,
39+
req: JsonRpcRequest<JsonRpcParams>,
3940
_encoding: unknown,
4041
cb: (error?: Error | null) => void,
4142
) {

src/createStreamMiddleware.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import SafeEventEmitter from '@metamask/safe-event-emitter';
22
import { Duplex } from 'readable-stream';
3-
import {
3+
import type {
44
JsonRpcEngineNextCallback,
55
JsonRpcEngineEndCallback,
6-
JsonRpcNotification,
76
JsonRpcMiddleware,
7+
} from '@metamask/json-rpc-engine';
8+
9+
import type {
10+
JsonRpcNotification,
11+
JsonRpcParams,
812
JsonRpcRequest,
913
PendingJsonRpcResponse,
10-
} from 'json-rpc-engine';
14+
} from '@metamask/utils';
1115

1216
interface IdMapValue {
13-
req: JsonRpcRequest<unknown>;
14-
res: PendingJsonRpcResponse<unknown>;
17+
req: JsonRpcRequest<JsonRpcParams>;
18+
res: PendingJsonRpcResponse<JsonRpcParams>;
1519
next: JsonRpcEngineNextCallback;
1620
end: JsonRpcEngineEndCallback;
1721
retryCount?: number;
@@ -44,7 +48,7 @@ export default function createStreamMiddleware(options: Options = {}) {
4448

4549
const events = new SafeEventEmitter();
4650

47-
const middleware: JsonRpcMiddleware<unknown, unknown> = (
51+
const middleware: JsonRpcMiddleware<JsonRpcParams, JsonRpcParams> = (
4852
req,
4953
res,
5054
next,
@@ -63,7 +67,7 @@ export default function createStreamMiddleware(options: Options = {}) {
6367
*
6468
* @param req - The JSON-RPC request object.
6569
*/
66-
function sendToStream(req: JsonRpcRequest<unknown>) {
70+
function sendToStream(req: JsonRpcRequest<JsonRpcParams>) {
6771
// TODO: limiting retries could be implemented here
6872
stream.push(req);
6973
}
@@ -76,15 +80,17 @@ export default function createStreamMiddleware(options: Options = {}) {
7680
* @param cb - The stream write callback.
7781
*/
7882
function processMessage(
79-
res: PendingJsonRpcResponse<unknown>,
83+
res: PendingJsonRpcResponse<JsonRpcParams>,
8084
_encoding: unknown,
8185
cb: (error?: Error | null) => void,
8286
) {
8387
let err: Error | null = null;
8488
try {
8589
const isNotification = !res.id;
8690
if (isNotification) {
87-
processNotification(res as unknown as JsonRpcNotification<unknown>);
91+
processNotification(
92+
res as unknown as JsonRpcNotification<JsonRpcParams>,
93+
);
8894
} else {
8995
processResponse(res);
9096
}
@@ -100,7 +106,7 @@ export default function createStreamMiddleware(options: Options = {}) {
100106
*
101107
* @param res - The response to process.
102108
*/
103-
function processResponse(res: PendingJsonRpcResponse<unknown>) {
109+
function processResponse(res: PendingJsonRpcResponse<JsonRpcParams>) {
104110
const context = idMap[res.id as unknown as string];
105111
if (!context) {
106112
console.warn(`StreamMiddleware - Unknown response id "${res.id}"`);
@@ -120,7 +126,7 @@ export default function createStreamMiddleware(options: Options = {}) {
120126
*
121127
* @param notif - The notification to process.
122128
*/
123-
function processNotification(notif: JsonRpcNotification<unknown>) {
129+
function processNotification(notif: JsonRpcNotification<JsonRpcParams>) {
124130
if (options?.retryOnMessage && notif.method === options.retryOnMessage) {
125131
retryStuckRequests();
126132
}

src/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Duplex } from 'stream';
2-
import { JsonRpcEngine } from 'json-rpc-engine';
2+
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
33
import PortStream from 'extension-port-stream';
44
import type { Runtime } from 'webextension-polyfill-ts';
55
import { createStreamMiddleware, createEngineStream } from '.';

0 commit comments

Comments
 (0)