mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
return early from metaRPCHandler if stream has ended (#11065)
* return early from metaRPCHandler if stream has ended * add write after end guard to handleUpdate
This commit is contained in:
parent
9cbb37b4ba
commit
d53bb61d47
@ -2,6 +2,9 @@ import { ethErrors, serializeError } from 'eth-rpc-errors';
|
|||||||
|
|
||||||
const createMetaRPCHandler = (api, outStream) => {
|
const createMetaRPCHandler = (api, outStream) => {
|
||||||
return (data) => {
|
return (data) => {
|
||||||
|
if (outStream._writableState.ended) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!api[data.method]) {
|
if (!api[data.method]) {
|
||||||
outStream.write({
|
outStream.write({
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
@ -13,6 +16,9 @@ const createMetaRPCHandler = (api, outStream) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
api[data.method](...data.params, (err, result) => {
|
api[data.method](...data.params, (err, result) => {
|
||||||
|
if (outStream._writableState.ended) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (err) {
|
if (err) {
|
||||||
outStream.write({
|
outStream.write({
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
|
@ -58,4 +58,40 @@ describe('createMetaRPCHandler', function () {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it('can not throw an error for writing an error after end', function (done) {
|
||||||
|
const api = {
|
||||||
|
foo: (param1, cb) => {
|
||||||
|
assert.strictEqual(param1, 'bar');
|
||||||
|
cb(new Error('foo-error'));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const streamTest = createThoughStream();
|
||||||
|
const handler = createMetaRPCHandler(api, streamTest);
|
||||||
|
streamTest.end();
|
||||||
|
handler({
|
||||||
|
id: 1,
|
||||||
|
method: 'foo',
|
||||||
|
params: ['bar'],
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
it('can not throw an error for write after end', function (done) {
|
||||||
|
const api = {
|
||||||
|
foo: (param1, cb) => {
|
||||||
|
assert.strictEqual(param1, 'bar');
|
||||||
|
cb(undefined, {
|
||||||
|
foo: 'bar',
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const streamTest = createThoughStream();
|
||||||
|
const handler = createMetaRPCHandler(api, streamTest);
|
||||||
|
streamTest.end();
|
||||||
|
handler({
|
||||||
|
id: 1,
|
||||||
|
method: 'foo',
|
||||||
|
params: ['bar'],
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -2024,6 +2024,9 @@ export default class MetamaskController extends EventEmitter {
|
|||||||
// set up postStream transport
|
// set up postStream transport
|
||||||
outStream.on('data', createMetaRPCHandler(api, outStream));
|
outStream.on('data', createMetaRPCHandler(api, outStream));
|
||||||
const handleUpdate = (update) => {
|
const handleUpdate = (update) => {
|
||||||
|
if (outStream._writableState.ended) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// send notification to client-side
|
// send notification to client-side
|
||||||
outStream.write({
|
outStream.write({
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
|
Loading…
Reference in New Issue
Block a user