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

Commit 99fd157

Browse files
committed
Add DuplexJsonRpcEngine
1 parent 907a6da commit 99fd157

1 file changed

Lines changed: 162 additions & 0 deletions

File tree

src/DuplexJsonRpcEngine.ts

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import {
2+
isJsonRpcRequest,
3+
JsonRpcNotification,
4+
JsonRpcRequest,
5+
JsonRpcResponse,
6+
} from '@metamask/utils';
7+
import {
8+
JsonRpcEngine,
9+
JsonRpcMiddleware,
10+
JsonRpcNotificationHandler,
11+
} from './JsonRpcEngine';
12+
13+
type HandleArgument =
14+
| JsonRpcRequest<unknown>
15+
| JsonRpcNotification<unknown>
16+
| (JsonRpcRequest<unknown> | JsonRpcNotification<unknown>)[];
17+
18+
interface DuplexJsonRpcEngineArgs {
19+
receiverNotificationHandler: JsonRpcNotificationHandler<unknown>;
20+
senderNotificationHandler: JsonRpcNotificationHandler<unknown>;
21+
}
22+
23+
export class DuplexJsonRpcEngine {
24+
readonly #receiver: JsonRpcEngine;
25+
26+
readonly #sender: JsonRpcEngine;
27+
28+
get receiver(): JsonRpcEngine {
29+
return this.#receiver;
30+
}
31+
32+
get sender(): JsonRpcEngine {
33+
return this.#sender;
34+
}
35+
36+
constructor({
37+
receiverNotificationHandler,
38+
senderNotificationHandler,
39+
}: DuplexJsonRpcEngineArgs) {
40+
this.#receiver = new JsonRpcEngine({
41+
notificationHandler: receiverNotificationHandler,
42+
});
43+
44+
this.#sender = new JsonRpcEngine({
45+
notificationHandler: senderNotificationHandler,
46+
});
47+
}
48+
49+
/**
50+
* Add a middleware function to the receiving middleware stack.
51+
*
52+
* @param middleware - The middleware function to add.
53+
*/
54+
pushReceiverMiddleware(
55+
middleware: JsonRpcMiddleware<unknown, unknown>,
56+
): void {
57+
this.#receiver.push(middleware as JsonRpcMiddleware<unknown, unknown>);
58+
}
59+
60+
/**
61+
* Add a middleware function to the sending middleware stack.
62+
*
63+
* @param middleware - The middleware function to add.
64+
*/
65+
pushSenderMiddleware(middleware: JsonRpcMiddleware<unknown, unknown>): void {
66+
this.#sender.push(middleware as JsonRpcMiddleware<unknown, unknown>);
67+
}
68+
69+
/**
70+
* Returns the receiving pipeline as a middleware function that can be pushed
71+
* to other engines.
72+
*
73+
* @returns The receiving pipeline as a middleware function.
74+
*/
75+
receiverAsMiddleware(): JsonRpcMiddleware<unknown, unknown> {
76+
return this.#receiver.asMiddleware();
77+
}
78+
79+
/**
80+
* Returns the sending pipeline as a middleware function that can be pushed
81+
* to other engines.
82+
*
83+
* @returns The sending pipeline as a middleware function.
84+
*/
85+
senderAsMiddleware(): JsonRpcMiddleware<unknown, unknown> {
86+
return this.#sender.asMiddleware();
87+
}
88+
89+
/**
90+
* Receive a JSON-RPC request, and return a response.
91+
*
92+
* @param request - The JSON-RPC request to receive.
93+
* @returns The JSON-RPC response.
94+
*/
95+
receive<Params, Result>(
96+
request: JsonRpcRequest<Params>,
97+
): Promise<JsonRpcResponse<Result>>;
98+
99+
/**
100+
* Receive a JSON-RPC notification.
101+
*
102+
* @param notification - The notification to receive.
103+
*/
104+
receive<Params>(notification: JsonRpcNotification<Params>): Promise<void>;
105+
106+
/**
107+
* Receive an array of JSON-RPC requests and/or notifications, and return an
108+
* array of responses to any included requests.
109+
*
110+
* @param request - The JSON-RPC requests to receive.
111+
* @returns An array of JSON-RPC responses.
112+
*/
113+
receive<Params, Result>(
114+
requests: (JsonRpcRequest<Params> | JsonRpcNotification<Params>)[],
115+
): Promise<JsonRpcResponse<Result>[]>;
116+
117+
async receive(argument: HandleArgument) {
118+
if (Array.isArray(argument)) {
119+
return this.#receiver.handle(argument);
120+
} else if (isJsonRpcRequest(argument)) {
121+
return this.#receiver.handle(argument);
122+
}
123+
return this.#receiver.handle(argument);
124+
}
125+
126+
/**
127+
* Send a JSON-RPC request, and receive a response.
128+
*
129+
* @param request - The JSON-RPC request to send.
130+
* @returns The JSON-RPC response.
131+
*/
132+
send<Params, Result>(
133+
request: JsonRpcRequest<Params>,
134+
): Promise<JsonRpcResponse<Result>>;
135+
136+
/**
137+
* Send a JSON-RPC notification.
138+
*
139+
* @param notification - The notification to send.
140+
*/
141+
send<Params>(notification: JsonRpcNotification<Params>): Promise<void>;
142+
143+
/**
144+
* Send an array of JSON-RPC requests and/or notifications, and receive an
145+
* array of responses to any included requests.
146+
*
147+
* @param request - The JSON-RPC requests to send.
148+
* @returns An array of JSON-RPC responses.
149+
*/
150+
send<Params, Result>(
151+
requests: (JsonRpcRequest<Params> | JsonRpcNotification<Params>)[],
152+
): Promise<JsonRpcResponse<Result>[]>;
153+
154+
async send(argument: HandleArgument) {
155+
if (Array.isArray(argument)) {
156+
return this.#sender.handle(argument);
157+
} else if (isJsonRpcRequest(argument)) {
158+
return this.#sender.handle(argument);
159+
}
160+
return this.#sender.handle(argument);
161+
}
162+
}

0 commit comments

Comments
 (0)