1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 15:50:28 +01:00
metamask-extension/app/scripts/lib/stream-utils.js
Ariella Vu e01b067d0c
Minor scripts/ui.js file cleanup (#16566)
* clean:ui: mv comments

* clean:ui: fix typo recieved -> received

* clean:ui: rn handle -> keepAliveInterval

* clean:ui: rn timeoutHandle -> ackTimeoutToDisplayError

* clean: replace "Xs" w/ variable name

* clean:ui: rm duplicate comment

* add EXTENSION_MESSAGES.CONNECTION_READY const

* clean:ui: bit more clean up
2022-11-18 20:57:01 +07:00

29 lines
1.0 KiB
JavaScript

import ObjectMultiplex from 'obj-multiplex';
import pump from 'pump';
import { EXTENSION_MESSAGES } from '../../../shared/constants/app';
/**
* Sets up stream multiplexing for the given stream
*
* @param {any} connectionStream - the stream to mux
* @returns {stream.Stream} the multiplexed stream
*/
export function setupMultiplex(connectionStream) {
const mux = new ObjectMultiplex();
/**
* 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
*/
mux.ignoreStream(EXTENSION_MESSAGES.CONNECTION_READY);
mux.ignoreStream('ACK_KEEP_ALIVE_MESSAGE');
mux.ignoreStream('WORKER_KEEP_ALIVE_MESSAGE');
pump(connectionStream, mux, connectionStream, (err) => {
if (err) {
console.error(err);
}
});
return mux;
}