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

update to @metamask/json-rpc-engine #54

Merged
merged 5 commits into from
Sep 13, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# json-rpc-middleware-stream

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).
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).
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ module.exports = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 91.3,
branches: 90.9,
functions: 100,
lines: 98.51,
statements: 98.51,
lines: 98.56,
statements: 98.56,
},
},

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/json-rpc-engine": "^7.1.1",
"@metamask/safe-event-emitter": "^3.0.0",
"json-rpc-engine": "^6.1.0",
"@metamask/utils": "^8.1.0",
"readable-stream": "^2.3.3"
},
"devDependencies": {
Expand All @@ -51,7 +52,6 @@
"extension-port-stream": "^2.0.1",
"jest": "^27.5.1",
"jest-it-up": "^2.0.2",
"json-rpc-engine": "^6.1.0",
"prettier": "^2.2.1",
"prettier-plugin-packagejson": "^2.2.17",
"rimraf": "^3.0.2",
Expand Down
5 changes: 3 additions & 2 deletions src/createEngineStream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Duplex } from 'readable-stream';
import { JsonRpcEngine, JsonRpcRequest } from 'json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { JsonRpcRequest, JsonRpcParams } from '@metamask/utils';

interface EngineStreamOptions {
engine: JsonRpcEngine;
Expand Down Expand Up @@ -35,7 +36,7 @@ export default function createEngineStream(opts: EngineStreamOptions): Duplex {
* @param cb - The stream write callback.
*/
function write(
req: JsonRpcRequest<unknown>,
req: JsonRpcRequest<JsonRpcParams>,
_encoding: unknown,
cb: (error?: Error | null) => void,
) {
Expand Down
28 changes: 17 additions & 11 deletions src/createStreamMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import SafeEventEmitter from '@metamask/safe-event-emitter';
import { Duplex } from 'readable-stream';
import {
import type {
JsonRpcEngineNextCallback,
JsonRpcEngineEndCallback,
JsonRpcNotification,
JsonRpcMiddleware,
} from '@metamask/json-rpc-engine';

import type {
JsonRpcNotification,
JsonRpcParams,
JsonRpcRequest,
PendingJsonRpcResponse,
} from 'json-rpc-engine';
} from '@metamask/utils';

interface IdMapValue {
req: JsonRpcRequest<unknown>;
res: PendingJsonRpcResponse<unknown>;
req: JsonRpcRequest<JsonRpcParams>;
res: PendingJsonRpcResponse<JsonRpcParams>;
next: JsonRpcEngineNextCallback;
end: JsonRpcEngineEndCallback;
retryCount?: number;
Expand Down Expand Up @@ -44,7 +48,7 @@ export default function createStreamMiddleware(options: Options = {}) {

const events = new SafeEventEmitter();

const middleware: JsonRpcMiddleware<unknown, unknown> = (
const middleware: JsonRpcMiddleware<JsonRpcParams, JsonRpcParams> = (
req,
res,
next,
Expand All @@ -63,7 +67,7 @@ export default function createStreamMiddleware(options: Options = {}) {
*
* @param req - The JSON-RPC request object.
*/
function sendToStream(req: JsonRpcRequest<unknown>) {
function sendToStream(req: JsonRpcRequest<JsonRpcParams>) {
// TODO: limiting retries could be implemented here
stream.push(req);
}
Expand All @@ -76,15 +80,17 @@ export default function createStreamMiddleware(options: Options = {}) {
* @param cb - The stream write callback.
*/
function processMessage(
res: PendingJsonRpcResponse<unknown>,
res: PendingJsonRpcResponse<JsonRpcParams>,
_encoding: unknown,
cb: (error?: Error | null) => void,
) {
let err: Error | null = null;
try {
const isNotification = !res.id;
if (isNotification) {
processNotification(res as unknown as JsonRpcNotification<unknown>);
processNotification(
res as unknown as JsonRpcNotification<JsonRpcParams>,
);
} else {
processResponse(res);
}
Expand All @@ -100,7 +106,7 @@ export default function createStreamMiddleware(options: Options = {}) {
*
* @param res - The response to process.
*/
function processResponse(res: PendingJsonRpcResponse<unknown>) {
function processResponse(res: PendingJsonRpcResponse<JsonRpcParams>) {
const context = idMap[res.id as unknown as string];
if (!context) {
console.warn(`StreamMiddleware - Unknown response id "${res.id}"`);
Expand All @@ -120,7 +126,7 @@ export default function createStreamMiddleware(options: Options = {}) {
*
* @param notif - The notification to process.
*/
function processNotification(notif: JsonRpcNotification<unknown>) {
function processNotification(notif: JsonRpcNotification<JsonRpcParams>) {
if (options?.retryOnMessage && notif.method === options.retryOnMessage) {
retryStuckRequests();
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Duplex } from 'stream';
import { JsonRpcEngine } from 'json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import PortStream from 'extension-port-stream';
import type { Runtime } from 'webextension-polyfill-ts';
import { createStreamMiddleware, createEngineStream } from '.';
Expand Down
Loading
Loading