mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
20986e17b7
* persist state in metaRPCHandler so that we are sure state is persisted before sending back response to actions
56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
import { ethErrors, serializeError } from 'eth-rpc-errors';
|
|
import { isManifestV3 } from '../../../shared/modules/mv3.utils';
|
|
|
|
const createMetaRPCHandler = (api, outStream, store, localStoreApiWrapper) => {
|
|
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;
|
|
} finally {
|
|
if (isManifestV3 && store && data.method !== 'getState') {
|
|
localStoreApiWrapper.set(store.getState());
|
|
}
|
|
}
|
|
|
|
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;
|