2021-02-04 19:15:23 +01:00
|
|
|
import ObjectMultiplex from 'obj-multiplex';
|
|
|
|
import pump from 'pump';
|
2016-04-08 23:24:10 +02:00
|
|
|
|
2022-11-18 14:57:01 +01:00
|
|
|
import { EXTENSION_MESSAGES } from '../../../shared/constants/app';
|
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Sets up stream multiplexing for the given stream
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2018-04-16 17:29:43 +02:00
|
|
|
* @param {any} connectionStream - the stream to mux
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {stream.Stream} the multiplexed stream
|
2018-04-16 17:29:43 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function setupMultiplex(connectionStream) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mux = new ObjectMultiplex();
|
2022-11-08 23:38:48 +01:00
|
|
|
/**
|
|
|
|
* We are using this streams to send keep alive message between backend/ui without setting up a multiplexer
|
|
|
|
* We need to tell the multiplexer to ignore them, else we get the " orphaned data for stream " warnings
|
|
|
|
* https://github.com/MetaMask/object-multiplex/blob/280385401de84f57ef57054d92cfeb8361ef2680/src/ObjectMultiplex.ts#L63
|
|
|
|
*/
|
2022-11-18 14:57:01 +01:00
|
|
|
mux.ignoreStream(EXTENSION_MESSAGES.CONNECTION_READY);
|
2022-11-08 23:38:48 +01:00
|
|
|
mux.ignoreStream('ACK_KEEP_ALIVE_MESSAGE');
|
|
|
|
mux.ignoreStream('WORKER_KEEP_ALIVE_MESSAGE');
|
2020-11-03 00:41:28 +01:00
|
|
|
pump(connectionStream, mux, connectionStream, (err) => {
|
|
|
|
if (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(err);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
return mux;
|
2016-06-21 22:18:32 +02:00
|
|
|
}
|