2021-03-18 19:23:46 +01:00
|
|
|
import { ethErrors, serializeError } from 'eth-rpc-errors';
|
|
|
|
|
|
|
|
const createMetaRPCHandler = (api, outStream) => {
|
|
|
|
return (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;
|
|
|
|
}
|
|
|
|
api[data.method](...data.params, (err, result) => {
|
2021-05-13 00:12:46 +02:00
|
|
|
if (outStream._writableState.ended) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-18 19:23:46 +01:00
|
|
|
if (err) {
|
|
|
|
outStream.write({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
error: serializeError(err, { shouldIncludeStack: true }),
|
|
|
|
id: data.id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
outStream.write({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result,
|
|
|
|
id: data.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default createMetaRPCHandler;
|