Skip to content

Commit 205d9a7

Browse files
committed
passthrough batch requests
1 parent c3abd42 commit 205d9a7

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

server.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,30 @@ function getNextSub() {
8484
return `0x${id.toString(16).padStart(32, "0")}`;
8585
}
8686

87+
async function passthroughRPC(json: object, ws: WebSocket) {
88+
if (RPC) {
89+
const response = (
90+
await axios.post(RPC, json, {
91+
// don't parse the response
92+
transformResponse: (x) => x,
93+
})
94+
)?.data;
95+
logger.debug(response);
96+
ws.send(response);
97+
}
98+
}
99+
87100
wss.on("connection", (ws: WebSocket) => {
88101
logger.info("New client connected");
89102

90103
ws.on("message", (message: string) => {
91104
logger.debug(`Received message: ${message}`);
92105
try {
93106
const json = JSON.parse(message);
107+
if (json.length) {
108+
passthroughRPC(json, ws);
109+
return;
110+
}
94111
if (!json.id || typeof json.id !== "number") {
95112
return;
96113
}
@@ -151,17 +168,7 @@ wss.on("connection", (ws: WebSocket) => {
151168
ws.send(`{"jsonrpc":"2.0","result":true,"id":${json.id}}`);
152169
}
153170
} else {
154-
// pass through
155-
(async () => {
156-
const response = (
157-
await axios.post(RPC, json, {
158-
// don't parse the response
159-
transformResponse: (x) => x,
160-
})
161-
)?.data;
162-
logger.debug(response);
163-
ws.send(response);
164-
})();
171+
passthroughRPC(json, ws);
165172
}
166173
} catch (e) {
167174
// ignore bad messages

0 commit comments

Comments
 (0)