diff options
author | Dan Finlay <flyswatter@users.noreply.github.com> | 2017-09-15 10:40:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-15 10:40:00 +0800 |
commit | 693655e2da7cacf5a5326b50bddc37bcece9422e (patch) | |
tree | f5f6c429649af579d97eaa5940cab94dbc365a29 /app/scripts/contentscript.js | |
parent | 1d3cd9768cdd372d02e7e34674dde9d86af536f5 (diff) | |
parent | 0687c822baf7dcde8e96afa25ebc84491a061d07 (diff) | |
download | tangerine-wallet-browser-693655e2da7cacf5a5326b50bddc37bcece9422e.tar tangerine-wallet-browser-693655e2da7cacf5a5326b50bddc37bcece9422e.tar.gz tangerine-wallet-browser-693655e2da7cacf5a5326b50bddc37bcece9422e.tar.bz2 tangerine-wallet-browser-693655e2da7cacf5a5326b50bddc37bcece9422e.tar.lz tangerine-wallet-browser-693655e2da7cacf5a5326b50bddc37bcece9422e.tar.xz tangerine-wallet-browser-693655e2da7cacf5a5326b50bddc37bcece9422e.tar.zst tangerine-wallet-browser-693655e2da7cacf5a5326b50bddc37bcece9422e.zip |
Merge pull request #2070 from MetaMask/filter-leak-fix3
Memory leak fixes - stream and filter life cycles
Diffstat (limited to 'app/scripts/contentscript.js')
-rw-r--r-- | app/scripts/contentscript.js | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index acacf5d4c..90a0f1f22 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -1,11 +1,12 @@ +const fs = require('fs') +const path = require('path') +const pump = require('pump') const LocalMessageDuplexStream = require('post-message-stream') const PongStream = require('ping-pong-stream/pong') -const PortStream = require('./lib/port-stream.js') -const ObjectMultiplex = require('./lib/obj-multiplex') +const ObjectMultiplex = require('obj-multiplex') const extension = require('extensionizer') +const PortStream = require('./lib/port-stream.js') -const fs = require('fs') -const path = require('path') const inpageText = fs.readFileSync(path.join(__dirname, 'inpage.js')).toString() // Eventually this streaming injection could be replaced with: @@ -50,22 +51,42 @@ function setupStreams () { pageStream.pipe(pluginStream).pipe(pageStream) // setup local multistream channels - const mx = ObjectMultiplex() - mx.on('error', console.error) - mx.pipe(pageStream).pipe(mx) - mx.pipe(pluginStream).pipe(mx) + const mux = new ObjectMultiplex() + pump( + mux, + pageStream, + mux, + (err) => logStreamDisconnectWarning('MetaMask Inpage', err) + ) + pump( + mux, + pluginStream, + mux, + (err) => logStreamDisconnectWarning('MetaMask Background', err) + ) // connect ping stream const pongStream = new PongStream({ objectMode: true }) - pongStream.pipe(mx.createStream('pingpong')).pipe(pongStream) + pump( + mux, + pongStream, + mux, + (err) => logStreamDisconnectWarning('MetaMask PingPongStream', err) + ) // connect phishing warning stream - const phishingStream = mx.createStream('phishing') + const phishingStream = mux.createStream('phishing') phishingStream.once('data', redirectToPhishingWarning) // ignore unused channels (handled by background, inpage) - mx.ignoreStream('provider') - mx.ignoreStream('publicConfig') + mux.ignoreStream('provider') + mux.ignoreStream('publicConfig') +} + +function logStreamDisconnectWarning (remoteLabel, err) { + let warningMsg = `MetamaskContentscript - lost connection to ${remoteLabel}` + if (err) warningMsg += '\n' + err.stack + console.warn(warningMsg) } function shouldInjectWeb3 () { |