diff --git a/library/lib/setup-untrusted-connection.js b/library/lib/setup-untrusted-connection.js deleted file mode 100644 index b2aeb7905..000000000 --- a/library/lib/setup-untrusted-connection.js +++ /dev/null @@ -1,27 +0,0 @@ - -/* -IFRAME - var pageStream = new LocalMessageDuplexStream({ - name: 'contentscript', - target: 'inpage', - }) -SERVICEWORKER - pageStream.on('error', console.error) - var pluginPort = extension.runtime.connect({name: 'contentscript'}) - var pluginStream = new PortStream(pluginPort) - pluginStream.on('error', console.error) -IFRAME --> SW - // forward communication plugin->inpage - pageStream.pipe(pluginStream).pipe(pageStream) -*/ - -module.exports = SetupUntrustedComunicationWithSW - -function SetupUntrustedComunicationWithSW (connectionStream, readySwStream) { - pageStream.on('error', console.error) - var pluginPort = extension.runtime.connect({name: 'contentscript'}) - var pluginStream = new PortStream(pluginPort) - pluginStream.on('error', console.error) - // forward communication plugin->inpage - pageStream.pipe(pluginStream).pipe(pageStream) -} diff --git a/library/server.js b/library/server.js index a284d3e05..67c89f11b 100644 --- a/library/server.js +++ b/library/server.js @@ -3,11 +3,12 @@ const browserify = require('browserify') const watchify = require('watchify') const babelify = require('babelify') -const zeroBundle = createBundle('./index.js') -const controllerBundle = createBundle('./controller.js') -const popupBundle = createBundle('./popup.js') +const zeroBundle = createBundle('./src/mascara.js') +const controllerBundle = createBundle('./src/dapp-connection.js') +const popupBundle = createBundle('./src/popup.js') +const swBuild = createBundle('./src/background.js') + const appBundle = createBundle('./example/index.js') -const swBuild = createBundle('./sw-core.js') // // Iframe Server diff --git a/library/sw-core.js b/library/src/background.js similarity index 87% rename from library/sw-core.js rename to library/src/background.js index d39bfdbfb..0af692ab8 100644 --- a/library/sw-core.js +++ b/library/src/background.js @@ -4,18 +4,18 @@ const pipe = require('pump') const SwGlobalListener = require('sw-stream/lib/sw-global-listener.js') const connectionListener = new SwGlobalListener(self) -const setupMultiplex = require('../app/scripts/lib/stream-utils.js').setupMultiplex -const PortStream = require('../app/scripts/lib/port-stream.js') +const setupMultiplex = require('../../app/scripts/lib/stream-utils.js').setupMultiplex +const PortStream = require('../../app/scripts/lib/port-stream.js') -const DbController = require('./controllers/index-db-controller') +const DbController = require('./lib/index-db-controller') -const MetamaskController = require('../app/scripts/metamask-controller') -const extension = {} //require('../app/scripts/lib/extension') +const MetamaskController = require('../../app/scripts/metamask-controller') +const extension = {} //require('../../app/scripts/lib/extension') const storeTransform = require('obs-store/lib/transform') -const Migrator = require('../app/scripts/lib/migrator/') -const migrations = require('../app/scripts/migrations/') -const firstTimeState = require('../app/scripts/first-time-state') +const Migrator = require('../../app/scripts/lib/migrator/') +const migrations = require('../../app/scripts/migrations/') +const firstTimeState = require('../../app/scripts/first-time-state') const STORAGE_KEY = 'metamask-config' const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG' diff --git a/library/controller.js b/library/src/dapp-connection.js similarity index 80% rename from library/controller.js rename to library/src/dapp-connection.js index 1e9bc84d2..30680c9d7 100644 --- a/library/controller.js +++ b/library/src/dapp-connection.js @@ -1,14 +1,14 @@ const ParentStream = require('iframe-stream').ParentStream -const SWcontroller = require('./sw-controller') +const SWcontroller = require('client-sw-ready-event/lib/sw-client.js') const SwStream = require('sw-stream/lib/sw-stream.js') const SetupUntrustedComunication = ('./lib/setup-untrusted-connection.js') + const background = new SWcontroller({ fileName: '/popup/sw-build.js', }) const pageStream = new ParentStream() background.on('ready', (_) => { - // var inpageProvider = new MetamaskInpageProvider(SwStream(background.controller)) let swStream = SwStream({ serviceWorker: background.controller, context: 'dapp', diff --git a/library/controllers/index-db-controller.js b/library/src/lib/index-db-controller.js similarity index 100% rename from library/controllers/index-db-controller.js rename to library/src/lib/index-db-controller.js diff --git a/library/lib/setup-iframe.js b/library/src/lib/setup-iframe.js similarity index 100% rename from library/lib/setup-iframe.js rename to library/src/lib/setup-iframe.js diff --git a/library/lib/setup-provider.js b/library/src/lib/setup-provider.js similarity index 87% rename from library/lib/setup-provider.js rename to library/src/lib/setup-provider.js index 1b53e7f54..4f2432ae4 100644 --- a/library/lib/setup-provider.js +++ b/library/src/lib/setup-provider.js @@ -1,5 +1,5 @@ const setupIframe = require('./setup-iframe.js') -const MetamaskInpageProvider = require('../../app/scripts/lib/inpage-provider.js') +const MetamaskInpageProvider = require('../../../app/scripts/lib/inpage-provider.js') module.exports = getProvider diff --git a/library/index.js b/library/src/mascara.js similarity index 100% rename from library/index.js rename to library/src/mascara.js diff --git a/library/popup.js b/library/src/popup.js similarity index 68% rename from library/popup.js rename to library/src/popup.js index f294e5c69..3dcc508bf 100644 --- a/library/popup.js +++ b/library/src/popup.js @@ -1,10 +1,10 @@ const injectCss = require('inject-css') -const MetaMaskUiCss = require('../ui/css') +const MetaMaskUiCss = require('../../ui/css') const setupIframe = require('./lib/setup-iframe.js') -const MetamaskInpageProvider = require('../app/scripts/lib/inpage-provider.js') -const SWcontroller = require('./sw-controller') +const MetamaskInpageProvider = require('../../app/scripts/lib/inpage-provider.js') +const SWcontroller = require('client-sw-ready-event/lib/sw-client.js') const SwStream = require('sw-stream/lib/sw-stream.js') -const startPopup = require('../app/scripts/popup-core') +const startPopup = require('../../app/scripts/popup-core') var css = MetaMaskUiCss() diff --git a/library/sw-controller.js b/library/sw-controller.js deleted file mode 100644 index 1a7b1cad3..000000000 --- a/library/sw-controller.js +++ /dev/null @@ -1,64 +0,0 @@ -const EventEmitter = require('events') - -module.exports = class ClientSideServiceWorker extends EventEmitter{ - constructor (opts) { - super() - this.fileName = opts.fileName - this.startDelay = opts.startDelay - - this.serviceWorkerApi = navigator.serviceWorker - this.serviceWorkerApi.onmessage = (messageEvent) => this.emit('message', messageEvent) - this.serviceWorkerApi.onerror = (err) => this.emit('error', err) - this.on('message', (messageEvent) => {debugger}) - if (opts.initStart) this.startWorker() - } - - get controller () { - return this.sw || this.serviceWorkerApi.controller - } - - - startWorker () { - return this.registerWorker() - .then((sw) => { - this.sw = sw - this.sw.onerror = (err) => this.emit('error', err) - this.sw = sw - this.emit('ready', this.sw) - }) - .catch((err) => this.emit('error', err)) - } - - registerWorker () { - return this.serviceWorkerApi.register(this.fileName) - .then((registerdWorker) => { - return new Promise((resolve, reject) => { - let timeOutId = setTimeout(() => { - if (this.serviceWorkerApi.controller) return resolve(this.serviceWorkerApi.controller) - if (registerdWorker.active) return resolve(registerdWorker.active) - return reject(new Error('ClientSideServiceWorker: No controller found and onupdatefound timed out')) - }, this.startDelay || 1000 ) - - registerdWorker.onupdatefound = (event) => { - this.emit('updatefound') - registerdWorker.update() - } - }) - }) - } - - sendMessage (message) { - const self = this - return new Promise((resolve, reject) => { - var messageChannel = new MessageChannel() - messageChannel.port1.onmessage = (event) => { - if (event.data.err) { - reject(event.data.error) - } else { - resolve(event.data.data) - } - } - this.controller.postMessage(message, [messageChannel.port2]) - }) - } -} diff --git a/package.json b/package.json index d6c80496a..d69dcbb91 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "bip39": "^2.2.0", "browser-passworder": "^2.0.3", "browserify-derequire": "^0.9.4", + "client-sw-ready-event": "^1.0.2", "clone": "^1.0.2", "copy-to-clipboard": "^2.0.0", "debounce": "^1.0.0",