11import SafeEventEmitter from '@metamask/safe-event-emitter' ;
22import { 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
1216interface 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 }
0 commit comments