aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-09-12 05:30:30 +0800
committerkumavis <aaron@kumavis.me>2017-09-12 05:30:30 +0800
commit8545453a9d58d7a54c10beef52e38107ed937117 (patch)
treeabd8f38ddb5875fb38d205d4274c7f4cf518f9ba
parent41164f61dbba700fbbe1eead4f1b436dabcfc331 (diff)
downloadtangerine-wallet-browser-8545453a9d58d7a54c10beef52e38107ed937117.tar
tangerine-wallet-browser-8545453a9d58d7a54c10beef52e38107ed937117.tar.gz
tangerine-wallet-browser-8545453a9d58d7a54c10beef52e38107ed937117.tar.bz2
tangerine-wallet-browser-8545453a9d58d7a54c10beef52e38107ed937117.tar.lz
tangerine-wallet-browser-8545453a9d58d7a54c10beef52e38107ed937117.tar.xz
tangerine-wallet-browser-8545453a9d58d7a54c10beef52e38107ed937117.tar.zst
tangerine-wallet-browser-8545453a9d58d7a54c10beef52e38107ed937117.zip
contentscript - fix obj-multiplex instantiation and use pump for streams
-rw-r--r--app/scripts/contentscript.js45
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 () {