mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 23:58:06 +01:00
d91ce9c4b5
This reverts commit308033edbf
, reversing changes made tob8d71d6d1d
.
51 lines
1021 B
JavaScript
51 lines
1021 B
JavaScript
import { ethErrors, serializeError } from 'eth-rpc-errors';
|
|
|
|
const createMetaRPCHandler = (api, outStream) => {
|
|
return async (data) => {
|
|
if (outStream._writableState.ended) {
|
|
return;
|
|
}
|
|
if (!api[data.method]) {
|
|
outStream.write({
|
|
jsonrpc: '2.0',
|
|
error: ethErrors.rpc.methodNotFound({
|
|
message: `${data.method} not found`,
|
|
}),
|
|
id: data.id,
|
|
});
|
|
return;
|
|
}
|
|
|
|
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);
|
|
}
|
|
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,
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
export default createMetaRPCHandler;
|