deps - use web3-stream-provider module

feature/default_network_editable
kumavis 9 years ago
parent 41df7bb8f6
commit 2a62d63b4f
  1. 34
      app/scripts/background.js
  2. 2
      app/scripts/lib/inpage-provider.js
  3. 72
      app/scripts/lib/stream-provider.js
  4. 2
      app/scripts/popup.js

@ -1,11 +1,12 @@
const Dnode = require('dnode') const Dnode = require('dnode')
const ObjectMultiplex = require('./lib/obj-multiplex')
const eos = require('end-of-stream') const eos = require('end-of-stream')
const combineStreams = require('pumpify') const combineStreams = require('pumpify')
const extend = require('xtend') const extend = require('xtend')
const EthStore = require('eth-store') const EthStore = require('eth-store')
const PortStream = require('./lib/port-stream.js')
const MetaMaskProvider = require('web3-provider-engine/zero.js') const MetaMaskProvider = require('web3-provider-engine/zero.js')
const handleRequestsFromStream = require('web3-stream-provider/handler')
const ObjectMultiplex = require('./lib/obj-multiplex')
const PortStream = require('./lib/port-stream.js')
const IdentityStore = require('./lib/idStore') const IdentityStore = require('./lib/idStore')
const createTxNotification = require('./lib/notifications.js').createTxNotification const createTxNotification = require('./lib/notifications.js').createTxNotification
const createMsgNotification = require('./lib/notifications.js').createMsgNotification const createMsgNotification = require('./lib/notifications.js').createMsgNotification
@ -132,25 +133,6 @@ function storeSetFromObj(store, obj){
} }
// handle rpc requests
function onRpcRequest(remoteStream, payload){
// console.log('MetaMaskPlugin - incoming payload:', payload)
provider.sendAsync(payload, function onPayloadHandled(err, response){
// provider engine errors are included in response objects
if (!payload.isMetamaskInternal) {
console.log('MetaMaskPlugin - RPC complete:', payload, '->', response)
if (response.error) console.error('Error in RPC response:\n'+response.error.message)
}
try {
remoteStream.write(response)
} catch (err) {
console.error(err)
}
})
}
// //
// remote features // remote features
// //
@ -161,7 +143,15 @@ function setupPublicConfig(stream){
} }
function setupProviderConnection(stream){ function setupProviderConnection(stream){
stream.on('data', onRpcRequest.bind(null, stream)) handleRequestsFromStream(stream, provider, logger)
function logger(err, request, response){
if (err) return console.error(err.stack)
if (!request.isMetamaskInternal) {
console.log('MetaMaskPlugin - RPC complete:', request, '->', response)
if (response.error) console.error('Error in RPC response:\n'+response.error.message)
}
}
} }
function setupControllerConnection(stream){ function setupControllerConnection(stream){

@ -1,7 +1,7 @@
const HttpProvider = require('web3/lib/web3/httpprovider') const HttpProvider = require('web3/lib/web3/httpprovider')
const Streams = require('mississippi') const Streams = require('mississippi')
const ObjectMultiplex = require('./obj-multiplex') const ObjectMultiplex = require('./obj-multiplex')
const StreamProvider = require('./stream-provider.js') const StreamProvider = require('web3-stream-provider')
const RemoteStore = require('./remote-store.js').RemoteStore const RemoteStore = require('./remote-store.js').RemoteStore
const MetamaskConfig = require('../config.js') const MetamaskConfig = require('../config.js')

@ -1,72 +0,0 @@
const Duplex = require('readable-stream').Duplex
const inherits = require('util').inherits
module.exports = StreamProvider
inherits(StreamProvider, Duplex)
function StreamProvider(){
Duplex.call(this, {
objectMode: true,
})
this._payloads = {}
}
// public
StreamProvider.prototype.send = function(payload){
throw new Error('StreamProvider - does not support synchronous RPC calls. called: "'+payload.method+'"')
}
StreamProvider.prototype.sendAsync = function(payload, callback){
// console.log('StreamProvider - sending payload', payload)
var id = payload.id
if (Array.isArray(payload)) {
id = 'batch'+payload[0].id
}
this._payloads[id] = [payload, callback]
// console.log('payload for plugin:', payload)
this.push(payload)
}
StreamProvider.prototype.isConnected = function(){
return true
}
// private
StreamProvider.prototype._onResponse = function(response){
// console.log('StreamProvider - got response', payload)
var id = response.id
if (Array.isArray(response)) {
id = 'batch'+response[0].id
}
var data = this._payloads[id]
if (!data) throw new Error('StreamProvider - Unknown response id')
delete this._payloads[id]
var payload = data[0]
var callback = data[1]
// logging
var res = Array.isArray(response) ? response : [response]
// ;(Array.isArray(payload) ? payload : [payload]).forEach(function(payload, index){
// console.log('plugin response:', payload.id, payload.method, payload.params, '->', res[index].result)
// })
callback(null, response)
}
// stream plumbing
StreamProvider.prototype._read = noop
StreamProvider.prototype._write = function(msg, encoding, cb){
this._onResponse(msg)
cb()
}
// util
function noop(){}

@ -7,7 +7,7 @@ const MetaMaskUi = require('../../ui')
const MetaMaskUiCss = require('../../ui/css') const MetaMaskUiCss = require('../../ui/css')
const injectCss = require('inject-css') const injectCss = require('inject-css')
const PortStream = require('./lib/port-stream.js') const PortStream = require('./lib/port-stream.js')
const StreamProvider = require('./lib/stream-provider.js') const StreamProvider = require('web3-stream-provider')
const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
// setup app // setup app

Loading…
Cancel
Save