1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/app/scripts/lib/createMetaRPCHandler.js
Shane cacefc72fc
return early from metaRPCHandler if stream has ended (#11065)
* return early from metaRPCHandler if stream has ended

* add write after end guard to handleUpdate
2021-05-12 15:12:46 -07:00

40 lines
906 B
JavaScript

import { ethErrors, serializeError } from 'eth-rpc-errors';
const createMetaRPCHandler = (api, outStream) => {
return (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;
}
api[data.method](...data.params, (err, result) => {
if (outStream._writableState.ended) {
return;
}
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;