2021-03-18 19:23:46 +01:00
|
|
|
import { ethErrors, serializeError } from 'eth-rpc-errors';
|
|
|
|
|
2022-11-15 16:00:02 +01:00
|
|
|
const createMetaRPCHandler = (api, outStream) => {
|
2021-12-08 22:36:53 +01:00
|
|
|
return async (data) => {
|
2021-05-13 00:12:46 +02:00
|
|
|
if (outStream._writableState.ended) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-18 19:23:46 +01:00
|
|
|
if (!api[data.method]) {
|
|
|
|
outStream.write({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
error: ethErrors.rpc.methodNotFound({
|
|
|
|
message: `${data.method} not found`,
|
|
|
|
}),
|
|
|
|
id: data.id,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2021-12-08 22:36:53 +01:00
|
|
|
|
|
|
|
let result;
|
|
|
|
let error;
|
|
|
|
try {
|
|
|
|
result = await api[data.method](...data.params);
|
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outStream._writableState.ended) {
|
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
2021-03-18 19:23:46 +01:00
|
|
|
}
|
2021-12-08 22:36:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
outStream.write({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
error: serializeError(error, { shouldIncludeStack: true }),
|
|
|
|
id: data.id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
outStream.write({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result,
|
|
|
|
id: data.id,
|
|
|
|
});
|
|
|
|
}
|
2021-03-18 19:23:46 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default createMetaRPCHandler;
|