From d772b4fdaf388dedab925f9921a5416a006eb76e Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Mon, 27 Aug 2018 16:12:28 +0100 Subject: [PATCH 01/16] Add embarkjs to console --- lib/core/modules/coderunner/codeRunner.js | 12 ++++++--- lib/core/modules/coderunner/runCode.js | 6 ++++- lib/modules/code_generator/index.js | 31 +++++++++++++++++++++-- lib/modules/ipfs/embarkjs.js | 2 +- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 4924c5f48..728918b61 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -1,6 +1,5 @@ -// still needs to be run on a separate file due to the global context -var RunCode = require('./runCode.js'); - +const RunCode = require('./runCode.js'); +const EmbarkJS = require('embarkjs'); class CodeRunner { constructor(options) { this.plugins = options.plugins; @@ -26,6 +25,13 @@ class CodeRunner { self.events.request("runcode:eval", command.code); } }); + } else { + this.runCode.registerVar('EmbarkJS', EmbarkJS); + this.events.on('code-generator-ready', () => { + this.events.request('code-generator:embarkjs:initialization-code', (code) => { + this.runCode.doEval(code); + }); + }) } this.events.on("runcode:register", (varName, code) => { diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index 730780911..0ce3fd1f4 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -6,7 +6,11 @@ class RunCode { } doEval(code) { - return vm.runInNewContext(code, this.context); + try { + return vm.runInNewContext(code, this.context); + } catch(e) { + console.log(e) + } } registerVar(varName, code) { diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index 8c13819b0..3dfe8d61a 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -99,6 +99,10 @@ class CodeGenerator { self.buildPlaceholderPage(cb); }); + self.events.setCommandHandler('code-generator:embarkjs:initialization-code', (cb) => { + console.log("command") + cb(self.getEmbarkJsInitializationCode()); + }); } generateContext() { @@ -117,7 +121,7 @@ class CodeGenerator { result += Templates.main_context(); result += Templates.load_manager(); result += Templates.define_when_env_loaded(); - + if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) { return result; } @@ -216,7 +220,6 @@ class CodeGenerator { return result; } - generateStorageInitialization(useEmbarkJS) { if (!useEmbarkJS || this.storageConfig === {}) return ""; @@ -313,6 +316,8 @@ class CodeGenerator { web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes code += "\nimport Web3 from '" + web3Location + "';\n"; code += "\nimport web3 from 'Embark/web3';\n"; + code += "\nimport IpfsApi from 'ipfs-api';\n"; + next(); }, function getJSCode(next) { @@ -325,6 +330,7 @@ class CodeGenerator { code += self.generateCommunicationInitialization(true); code += self.generateStorageInitialization(true); code += self.generateNamesInitialization(true); + next(); }, function writeFile(next) { @@ -337,6 +343,27 @@ class CodeGenerator { }); } + getEmbarkJsInitializationCode() { + let code = ''; + let embarkjsCodes = this.plugins.getPluginsFor('embarkjsCode'); + for (let plugin of embarkjsCodes) { + code += plugin.embarkjs_code.join('\n'); + } + + let initCodes = this.plugins.getPluginsFor('initCode'); + for (let plugin of initCodes) { + let initCodes = plugin.embarkjs_init_code['communication'] || []; + for (let initCode of initCodes) { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, this.communicationConfig)) { + code += block; + } + } + } + + return code; + } + buildContractJS(contractName, contractJSON, cb) { let contractCode = ""; contractCode += "import web3 from 'Embark/web3';\n"; diff --git a/lib/modules/ipfs/embarkjs.js b/lib/modules/ipfs/embarkjs.js index 105739d49..3f4541338 100644 --- a/lib/modules/ipfs/embarkjs.js +++ b/lib/modules/ipfs/embarkjs.js @@ -1,4 +1,4 @@ -import IpfsApi from 'ipfs-api'; +/*global IpfsApi*/ let __embarkIPFS = {}; From 307b7dc8e7d64a2254262b2b0c8de1fc8968489a Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 28 Aug 2018 11:47:40 +0100 Subject: [PATCH 02/16] EmbarkJS available --- cmd/cmd_controller.js | 2 ++ lib/core/config.js | 4 ---- lib/core/modules/coderunner/codeRunner.js | 7 +++++++ lib/core/modules/coderunner/runCode.js | 4 +++- lib/modules/code_generator/index.js | 18 ++++++++++++------ lib/modules/ens/index.js | 1 - lib/modules/ens/register.js | 3 +-- 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index b9819b78e..305d6ea28 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -279,6 +279,7 @@ class EmbarkController { engine.startService("storage"); engine.startService("codeGenerator"); engine.startService("webServer"); + engine.startService("namingSystem"); return callback(); } @@ -315,6 +316,7 @@ class EmbarkController { if(engine.ipc.connected && engine.ipc.isClient()) { return callback(); } + engine.config.reloadConfig(); engine.events.request('deploy:contracts', function (err) { callback(err); }); diff --git a/lib/core/config.js b/lib/core/config.js index eacff5268..fb95558e4 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -63,10 +63,7 @@ Config.prototype.loadConfigFiles = function(options) { this.loadStorageConfigFile(); this.loadCommunicationConfigFile(); this.loadNameSystemConfigFile(); - - this.loadContractsConfigFile(); this.loadPipelineConfigFile(); - this.loadContractsConfigFile(); this.loadExternalContractsFiles(); this.loadWebServerConfigFile(); @@ -82,7 +79,6 @@ Config.prototype.reloadConfig = function() { this.loadStorageConfigFile(); this.loadCommunicationConfigFile(); this.loadNameSystemConfigFile(); - this.loadContractsConfigFile(); this.loadPipelineConfigFile(); this.loadContractsConfigFile(); this.loadExternalContractsFiles(); diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 728918b61..951e6c541 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -1,5 +1,9 @@ const RunCode = require('./runCode.js'); const EmbarkJS = require('embarkjs'); +const IpfsApi = require('ipfs-api'); +const Web3 = require('web3'); +const namehash = require('eth-ens-namehash'); + class CodeRunner { constructor(options) { this.plugins = options.plugins; @@ -26,6 +30,9 @@ class CodeRunner { } }); } else { + this.runCode.registerVar('IpfsApi', IpfsApi); + this.runCode.registerVar('Web3', Web3); + this.runCode.registerVar('namehash', namehash); this.runCode.registerVar('EmbarkJS', EmbarkJS); this.events.on('code-generator-ready', () => { this.events.request('code-generator:embarkjs:initialization-code', (code) => { diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index 0ce3fd1f4..d3c422a28 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -9,7 +9,7 @@ class RunCode { try { return vm.runInNewContext(code, this.context); } catch(e) { - console.log(e) + console.log(e.message) } } @@ -19,6 +19,8 @@ class RunCode { // /*global web3*/ if (varName === 'web3') { global.web3 = code; + } else if (varName === 'namehash') { + global.namehash = code; } this.context[varName] = code; } diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index 3dfe8d61a..3e01c1f64 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -350,17 +350,23 @@ class CodeGenerator { code += plugin.embarkjs_code.join('\n'); } + const codeTypes = {'communication': this.communicationConfig, + 'names': this.namesystemConfig, + 'storage': this.storageConfig}; + let initCodes = this.plugins.getPluginsFor('initCode'); for (let plugin of initCodes) { - let initCodes = plugin.embarkjs_init_code['communication'] || []; - for (let initCode of initCodes) { - let [block, shouldInit] = initCode; - if (shouldInit.call(plugin, this.communicationConfig)) { - code += block; + for (let codeTypeName of Object.keys(codeTypes)) { + let initCodes = plugin.embarkjs_init_code[codeTypeName] || []; + for (let initCode of initCodes) { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, codeTypes[codeTypeName])) { + code += block; + } } } } - + return code; } diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 9455e2a73..1cae88a3f 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -307,7 +307,6 @@ class ENS { } addSetProvider(config) { - let code = "\nEmbarkJS.Names.setProvider('ens'," + JSON.stringify(config) + ");"; let shouldInit = (namesConfig) => { diff --git a/lib/modules/ens/register.js b/lib/modules/ens/register.js index 1ebf34783..5a34e71e2 100644 --- a/lib/modules/ens/register.js +++ b/lib/modules/ens/register.js @@ -1,5 +1,4 @@ -/*global web3*/ -const namehash = require('eth-ens-namehash'); +/*global web3, namehash*/ function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) { const subnode = namehash.hash(subdomain); From 6a2b91d99ba5416ac59466bda2b8b0a9f4b303d2 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 28 Aug 2018 12:16:36 +0100 Subject: [PATCH 03/16] Add console/require to codeRunner --- lib/core/modules/coderunner/codeRunner.js | 8 ++++---- lib/core/modules/coderunner/runCode.js | 4 +--- lib/modules/ens/register.js | 3 ++- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 951e6c541..be1bb964d 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -2,7 +2,6 @@ const RunCode = require('./runCode.js'); const EmbarkJS = require('embarkjs'); const IpfsApi = require('ipfs-api'); const Web3 = require('web3'); -const namehash = require('eth-ens-namehash'); class CodeRunner { constructor(options) { @@ -29,16 +28,17 @@ class CodeRunner { self.events.request("runcode:eval", command.code); } }); - } else { + } + + if (!this.ipc.connected) { this.runCode.registerVar('IpfsApi', IpfsApi); this.runCode.registerVar('Web3', Web3); - this.runCode.registerVar('namehash', namehash); this.runCode.registerVar('EmbarkJS', EmbarkJS); this.events.on('code-generator-ready', () => { this.events.request('code-generator:embarkjs:initialization-code', (code) => { this.runCode.doEval(code); }); - }) + }); } this.events.on("runcode:register", (varName, code) => { diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index d3c422a28..1e080a523 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -2,7 +2,7 @@ const vm = require('vm'); class RunCode { constructor() { - this.context = Object.assign({}, global.this); + this.context = Object.assign({}, {require, console, ...global.this}); } doEval(code) { @@ -19,8 +19,6 @@ class RunCode { // /*global web3*/ if (varName === 'web3') { global.web3 = code; - } else if (varName === 'namehash') { - global.namehash = code; } this.context[varName] = code; } diff --git a/lib/modules/ens/register.js b/lib/modules/ens/register.js index 5a34e71e2..1ebf34783 100644 --- a/lib/modules/ens/register.js +++ b/lib/modules/ens/register.js @@ -1,4 +1,5 @@ -/*global web3, namehash*/ +/*global web3*/ +const namehash = require('eth-ens-namehash'); function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) { const subnode = namehash.hash(subdomain); From 6c63179fa1716218117b69ef48a7e22be9830254 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 28 Aug 2018 12:17:47 +0100 Subject: [PATCH 04/16] Fix lint --- lib/core/modules/coderunner/runCode.js | 2 +- lib/modules/code_generator/index.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index 1e080a523..3db6de7bf 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -2,7 +2,7 @@ const vm = require('vm'); class RunCode { constructor() { - this.context = Object.assign({}, {require, console, ...global.this}); + this.context = Object.assign({}, {require, console}, global.this); } doEval(code) { diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index 3e01c1f64..367ee46dd 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -100,7 +100,6 @@ class CodeGenerator { }); self.events.setCommandHandler('code-generator:embarkjs:initialization-code', (cb) => { - console.log("command") cb(self.getEmbarkJsInitializationCode()); }); } From 00ccad1d9b73fac8445c10b885ced7f6c2006871 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 28 Aug 2018 13:42:42 +0100 Subject: [PATCH 05/16] Fix isavailable --- lib/core/modules/coderunner/runCode.js | 4 ++-- lib/modules/ens/embarkjs.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index 3db6de7bf..cdf3b5ae5 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -2,14 +2,14 @@ const vm = require('vm'); class RunCode { constructor() { - this.context = Object.assign({}, {require, console}, global.this); + this.context = Object.assign({}, {console, exports, require, module, __filename, __dirname}, global.this); } doEval(code) { try { return vm.runInNewContext(code, this.context); } catch(e) { - console.log(e.message) + console.log(e.message); } } diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index 3a8418daa..7e84f1046 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -156,7 +156,7 @@ __embarkENS.setProvider = function (config) { web3.eth.net.getId() .then((id) => { const registryAddress = self.registryAddresses[id] || config.registryAddress; - self.isAvailable = true; + self._isAvailable = true; self.ens = new EmbarkJS.Contract({abi: config.registryAbi, address: registryAddress}); self.registrar = new EmbarkJS.Contract({abi: config.registrarAbi, address: config.registrarAddress}); self.resolver = new EmbarkJS.Contract({abi: config.resolverAbi, address: config.resolverAddress}); @@ -245,5 +245,5 @@ __embarkENS.registerSubDomain = function (name, address, callback) { }; __embarkENS.isAvailable = function () { - return Boolean(this.isAvailable); + return Boolean(this._isAvailable); }; From 83c01cffdc91bbe9586603000a05aaea41f97642 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 28 Aug 2018 14:43:52 +0100 Subject: [PATCH 06/16] Storage and Whisper connect --- lib/core/engine.js | 1 + lib/core/modules/coderunner/codeRunner.js | 23 +++++++++++++- lib/core/plugin.js | 7 +++++ lib/modules/code_generator/index.js | 38 +++++------------------ lib/modules/ens/index.js | 1 + lib/modules/storage/index.js | 1 + lib/modules/whisper/index.js | 17 +++++----- lib/modules/whisper/js/embarkjs.js | 2 +- 8 files changed, 49 insertions(+), 41 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 3c54aa368..acba25ccb 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -132,6 +132,7 @@ class Engine { codeRunnerService(_options) { const CodeRunner = require('./modules/coderunner/codeRunner.js'); this.codeRunner = new CodeRunner({ + config: this.config, plugins: this.plugins, events: this.events, logger: this.logger, diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index be1bb964d..1e98850b1 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -5,6 +5,7 @@ const Web3 = require('web3'); class CodeRunner { constructor(options) { + this.config = options.config; this.plugins = options.plugins; this.logger = options.logger; this.events = options.events; @@ -35,8 +36,28 @@ class CodeRunner { this.runCode.registerVar('Web3', Web3); this.runCode.registerVar('EmbarkJS', EmbarkJS); this.events.on('code-generator-ready', () => { - this.events.request('code-generator:embarkjs:initialization-code', (code) => { + this.events.request('code-generator:embarkjs:provider-code', (code) => { this.runCode.doEval(code); + const codeTypes = { + 'communication': this.config.communicationConfig || {}, + 'names': this.config.namesystemConfig || {}, + 'storage': this.config.storageConfig || {} + }; + + let initProvidersCode = ''; + let initCodes = this.plugins.getPluginsFor('initConsoleCode'); + for (let plugin of initCodes) { + for (let codeTypeName of Object.keys(codeTypes)) { + let initCodes = plugin.embarkjs_init_console_code[codeTypeName] || []; + for (let initCode of initCodes) { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, codeTypes[codeTypeName])) { + initProvidersCode += block; + } + } + } + } + this.runCode.doEval(initProvidersCode); }); }); } diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 80e14ec05..70957ffac 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -25,6 +25,7 @@ var Plugin = function(options) { this.imports = []; this.embarkjs_code = []; this.embarkjs_init_code = {}; + this.embarkjs_init_console_code = {}; this.afterContractsDeployActions = []; this.onDeployActions = []; this.eventActions = {}; @@ -189,6 +190,12 @@ Plugin.prototype.addProviderInit = function(providerType, code, initCondition) { this.addPluginType('initCode'); }; +Plugin.prototype.addConsoleProviderInit = function(providerType, code, initCondition) { + this.embarkjs_init_console_code[providerType] = this.embarkjs_init_console_code[providerType] || []; + this.embarkjs_init_console_code[providerType].push([code, initCondition]); + this.addPluginType('initConsoleCode'); +}; + Plugin.prototype.registerImportFile = function(importName, importLocation) { this.imports.push([importName, importLocation]); this.addPluginType('imports'); diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index 367ee46dd..cd5dfdf81 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -99,8 +99,8 @@ class CodeGenerator { self.buildPlaceholderPage(cb); }); - self.events.setCommandHandler('code-generator:embarkjs:initialization-code', (cb) => { - cb(self.getEmbarkJsInitializationCode()); + self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => { + cb(self.getEmbarkJsProviderCode()); }); } @@ -321,11 +321,8 @@ class CodeGenerator { }, function getJSCode(next) { code += "\n" + embarkjsCode + "\n"; - let pluginsWithCode = self.plugins.getPluginsFor('embarkjsCode'); - for (let plugin of pluginsWithCode) { - code += plugin.embarkjs_code.join('\n'); - } + code += self.getEmbarkJsProviderCode(); code += self.generateCommunicationInitialization(true); code += self.generateStorageInitialization(true); code += self.generateNamesInitialization(true); @@ -342,31 +339,10 @@ class CodeGenerator { }); } - getEmbarkJsInitializationCode() { - let code = ''; - let embarkjsCodes = this.plugins.getPluginsFor('embarkjsCode'); - for (let plugin of embarkjsCodes) { - code += plugin.embarkjs_code.join('\n'); - } - - const codeTypes = {'communication': this.communicationConfig, - 'names': this.namesystemConfig, - 'storage': this.storageConfig}; - - let initCodes = this.plugins.getPluginsFor('initCode'); - for (let plugin of initCodes) { - for (let codeTypeName of Object.keys(codeTypes)) { - let initCodes = plugin.embarkjs_init_code[codeTypeName] || []; - for (let initCode of initCodes) { - let [block, shouldInit] = initCode; - if (shouldInit.call(plugin, codeTypes[codeTypeName])) { - code += block; - } - } - } - } - - return code; + getEmbarkJsProviderCode() { + return this.plugins.getPluginsFor('embarkjsCode').reduce((code, plugin) => ( + code += plugin.embarkjs_code.join('\n') + ), ''); } buildContractJS(contractName, contractJSON, cb) { diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 1cae88a3f..23fa652e5 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -314,6 +314,7 @@ class ENS { }; this.embark.addProviderInit('names', code, shouldInit); + this.embark.addConsoleProviderInit('names', code, shouldInit); } } diff --git a/lib/modules/storage/index.js b/lib/modules/storage/index.js index c6bbb32f7..186434367 100644 --- a/lib/modules/storage/index.js +++ b/lib/modules/storage/index.js @@ -35,6 +35,7 @@ class Storage { }; this.embark.addProviderInit('storage', code, shouldInit); + this.embark.addConsoleProviderInit('storage', code, shouldInit); } } diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 7b4a6dfb1..2211d5893 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -73,21 +73,22 @@ class Whisper { addSetProvider() { let connection = this.communicationConfig.connection || {}; + const shouldInit = (communicationConfig) => { + return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true); + }; // todo: make the add code a function as well - let config = { + const config = { server: canonicalHost(connection.host || defaultHost), port: connection.port || '8546', type: connection.type || 'ws' }; - - let code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`; - - let shouldInit = (communicationConfig) => { - return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true); - }; - + const code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`; this.embark.addProviderInit('communication', code, shouldInit); + + const consoleConfig = Object.assign({}, config, {providerOptions: {headers: {Origin: "embark"}}}); + const consoleCode = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(consoleConfig)});`; + this.embark.addConsoleProviderInit('communication', consoleCode, shouldInit); } } diff --git a/lib/modules/whisper/js/embarkjs.js b/lib/modules/whisper/js/embarkjs.js index ea72b9d0e..7796b3cc3 100644 --- a/lib/modules/whisper/js/embarkjs.js +++ b/lib/modules/whisper/js/embarkjs.js @@ -12,7 +12,7 @@ __embarkWhisperNewWeb3.setProvider = function (options) { provider = options.server + ':' + options.port; } // TODO: take into account type - self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider)); + self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider, options.providerOptions)); self.getWhisperVersion(function (err, version) { if (err) { console.log("whisper not available"); From a8c5205b670f955deb0b0df1dbde2b16a82b5b10 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 28 Aug 2018 15:09:07 +0100 Subject: [PATCH 07/16] Extract global and web3js --- lib/core/modules/coderunner/codeRunner.js | 3 +++ lib/core/modules/coderunner/runCode.js | 2 +- lib/modules/code_generator/index.js | 2 -- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 1e98850b1..c4b471de1 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -36,6 +36,9 @@ class CodeRunner { this.runCode.registerVar('Web3', Web3); this.runCode.registerVar('EmbarkJS', EmbarkJS); this.events.on('code-generator-ready', () => { + this.events.request('code-generator:web3js', ((_err, code) => { + this.runCode.doEval(code); + })); this.events.request('code-generator:embarkjs:provider-code', (code) => { this.runCode.doEval(code); const codeTypes = { diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index cdf3b5ae5..4dcac3a69 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -2,7 +2,7 @@ const vm = require('vm'); class RunCode { constructor() { - this.context = Object.assign({}, {console, exports, require, module, __filename, __dirname}, global.this); + this.context = Object.assign({}, {global, console, exports, require, module, __filename, __dirname}); } doEval(code) { diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index cd5dfdf81..966fc51cb 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -379,7 +379,6 @@ class CodeGenerator { }, function getImports(web3Location, next) { web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes - code += "\nimport Web3 from '" + web3Location + "';\n"; code += "\nglobal.Web3 = Web3;\n"; code += "\n if (typeof web3 === 'undefined') {"; @@ -390,7 +389,6 @@ class CodeGenerator { let providerCode = self.generateProvider(false); code += providerCode; code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n"; - code += "\nexport default web3;\n"; next(null, code); } ], cb); From 0a261b050d153ab27d2164e7762b836163dabdd4 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 29 Aug 2018 09:35:04 +0100 Subject: [PATCH 08/16] Name available in console --- lib/core/modules/coderunner/codeRunner.js | 3 --- lib/modules/ens/embarkjs.js | 6 +++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index c4b471de1..1e98850b1 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -36,9 +36,6 @@ class CodeRunner { this.runCode.registerVar('Web3', Web3); this.runCode.registerVar('EmbarkJS', EmbarkJS); this.events.on('code-generator-ready', () => { - this.events.request('code-generator:web3js', ((_err, code) => { - this.runCode.doEval(code); - })); this.events.request('code-generator:embarkjs:provider-code', (code) => { this.runCode.doEval(code); const codeTypes = { diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index 7e84f1046..31535aca8 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -157,9 +157,9 @@ __embarkENS.setProvider = function (config) { .then((id) => { const registryAddress = self.registryAddresses[id] || config.registryAddress; self._isAvailable = true; - self.ens = new EmbarkJS.Contract({abi: config.registryAbi, address: registryAddress}); - self.registrar = new EmbarkJS.Contract({abi: config.registrarAbi, address: config.registrarAddress}); - self.resolver = new EmbarkJS.Contract({abi: config.resolverAbi, address: config.resolverAddress}); + self.ens = new EmbarkJS.Contract({abi: config.registryAbi, address: registryAddress, web3: web3}); + self.registrar = new EmbarkJS.Contract({abi: config.registrarAbi, address: config.registrarAddress, web3: web3}); + self.resolver = new EmbarkJS.Contract({abi: config.resolverAbi, address: config.resolverAddress, web3: web3}); }) .catch(err => { if (err.message.indexOf('Provider not set or invalid') > -1) { From b9944b18d2b3d98ba9f9d0e87b03db34d6606696 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 29 Aug 2018 09:59:39 +0100 Subject: [PATCH 09/16] Pass web3 as params --- lib/modules/ens/embarkjs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index 31535aca8..fdd746531 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -192,7 +192,7 @@ __embarkENS.resolve = function (name, callback) { if (resolverAddress === voidAddress) { return cb('Name not yet registered'); } - let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress}); + let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress, web3: web3}); resolverContract.methods.addr(node).call(cb); }); }; @@ -221,7 +221,7 @@ __embarkENS.lookup = function (address, callback) { if (resolverAddress === voidAddress) { return cb('Address not associated to a resolver'); } - let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress}); + let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress, web3: web3 }); resolverContract.methods.name(node).call(cb); }); }; From deb433c0fb5fe0067adac9e85a49eaac8de329e3 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 29 Aug 2018 10:22:43 +0100 Subject: [PATCH 10/16] Fix ens enabled --- lib/core/modules/coderunner/runCode.js | 1 + lib/modules/code_generator/index.js | 2 ++ templates/demo/app/components/ens.js | 2 +- templates/demo/app/dapp.js | 12 ++++-------- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index 4dcac3a69..ce269d8b9 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -20,6 +20,7 @@ class RunCode { if (varName === 'web3') { global.web3 = code; } + this.context["global"][varName] = code; this.context[varName] = code; } diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index 966fc51cb..cd5dfdf81 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -379,6 +379,7 @@ class CodeGenerator { }, function getImports(web3Location, next) { web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes + code += "\nimport Web3 from '" + web3Location + "';\n"; code += "\nglobal.Web3 = Web3;\n"; code += "\n if (typeof web3 === 'undefined') {"; @@ -389,6 +390,7 @@ class CodeGenerator { let providerCode = self.generateProvider(false); code += providerCode; code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n"; + code += "\nexport default web3;\n"; next(null, code); } ], cb); diff --git a/templates/demo/app/components/ens.js b/templates/demo/app/components/ens.js index 64e5a4e2a..b5dd7c223 100644 --- a/templates/demo/app/components/ens.js +++ b/templates/demo/app/components/ens.js @@ -146,7 +146,7 @@ class ENS extends React.Component { -

Register subdomain for embark

+

Register subdomain for eth

this.checkEnter(e, this.registerSubDomain)}> {this.state.responseRegister && diff --git a/templates/demo/app/dapp.js b/templates/demo/app/dapp.js index 2117558b3..87f5c4a06 100644 --- a/templates/demo/app/dapp.js +++ b/templates/demo/app/dapp.js @@ -21,8 +21,7 @@ class App extends React.Component { error: null, activeKey: 1, whisperEnabled: false, - storageEnabled: false, - ensEnabled: false + storageEnabled: false }; } @@ -56,10 +55,6 @@ class App extends React.Component { }).catch(() => { this.setState({storageEnabled: false}); }); - - this.setState({ - ensEnabled: EmbarkJS.Names.isAvailable() - }); }); } @@ -76,6 +71,7 @@ class App extends React.Component { } render() { + const ensEnabled = EmbarkJS.Names.currentNameSystems && EmbarkJS.Names.isAvailable(); if (this.state.error) { return (
Something went wrong connecting to ethereum. Please make sure you have a node running or are using metamask to connect to the ethereum network:
@@ -94,8 +90,8 @@ class App extends React.Component { - - + +
); From cc312a91b6479841b743dce19e7a432bbe8c9b6a Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 29 Aug 2018 10:49:53 +0100 Subject: [PATCH 11/16] Refactor code runner --- lib/core/modules/coderunner/codeRunner.js | 198 ++++++++++++---------- lib/core/modules/coderunner/runCode.js | 2 +- lib/modules/whisper/js/message_events.js | 1 - 3 files changed, 112 insertions(+), 89 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 1e98850b1..0a3d0aaf1 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -12,99 +12,123 @@ class CodeRunner { this.ipc = options.ipc; this.commands = []; this.runCode = new RunCode(); - let self = this; + this.registerIpcEvents(); + this.IpcClientListen(); + this.registerEvents(); + this.registerCommands(); + this.registerEmbarkJs(); + } - if (this.ipc.isServer()) { - this.ipc.on('runcode:getCommands', (_err, callback) => { - let result = {web3Config: self.runCode.getWeb3Config(), commands: self.commands}; - callback(null, result); - }); + registerIpcEvents() { + if (!this.ipc.isServer()) { + return; } - if (this.ipc.isClient() && this.ipc.connected) { - this.ipc.listenTo('runcode:newCommand', function (command) { - if (command.varName) { - self.events.emit("runcode:register", command.varName, command.code); - } else { - self.events.request("runcode:eval", command.code); - } - }); - } - - if (!this.ipc.connected) { - this.runCode.registerVar('IpfsApi', IpfsApi); - this.runCode.registerVar('Web3', Web3); - this.runCode.registerVar('EmbarkJS', EmbarkJS); - this.events.on('code-generator-ready', () => { - this.events.request('code-generator:embarkjs:provider-code', (code) => { - this.runCode.doEval(code); - const codeTypes = { - 'communication': this.config.communicationConfig || {}, - 'names': this.config.namesystemConfig || {}, - 'storage': this.config.storageConfig || {} - }; - - let initProvidersCode = ''; - let initCodes = this.plugins.getPluginsFor('initConsoleCode'); - for (let plugin of initCodes) { - for (let codeTypeName of Object.keys(codeTypes)) { - let initCodes = plugin.embarkjs_init_console_code[codeTypeName] || []; - for (let initCode of initCodes) { - let [block, shouldInit] = initCode; - if (shouldInit.call(plugin, codeTypes[codeTypeName])) { - initProvidersCode += block; - } - } - } - } - this.runCode.doEval(initProvidersCode); - }); - }); - } - - this.events.on("runcode:register", (varName, code) => { - if (self.ipc.isServer() && varName !== 'web3') { - self.commands.push({varName, code}); - self.ipc.broadcast("runcode:newCommand", {varName, code}); - } - self.runCode.registerVar(varName, code); - }); - - this.events.setCommandHandler('runcode:getContext', (cb) => { - cb(self.runCode.context); - }); - - this.events.setCommandHandler('runcode:eval', (code, cb, forConsoleOnly = false) => { - if (!cb) { - cb = function() {}; - } - const awaitIdx = code.indexOf('await'); - if (awaitIdx > -1) { - if (awaitIdx < 2) { - let end = code.length; - if (code[end - 1] === ';') { - end--; // Remove the `;` because we add function calls - } - code = code.substring(5, end); // remove await keyword - } else { - code = `(async function() {${code}})();`; - } - } - let result = self.runCode.doEval(code); - - if (forConsoleOnly && self.ipc.isServer()) { - self.commands.push({code}); - self.ipc.broadcast("runcode:newCommand", {code}); - } - - if (result instanceof Promise) { - return result.then((value) => cb(null, value)).catch(cb); - } - - cb(null, result); + this.ipc.on('runcode:getCommands', (_err, callback) => { + let result = {web3Config: this.runCode.getWeb3Config(), commands: this.commands}; + callback(null, result); }); } + IpcClientListen() { + if (!this.ipc.isClient() || !this.ipc.connected) { + return; + } + + this.ipc.listenTo('runcode:newCommand', (command) => { + if (command.varName) { + this.events.emit("runcode:register", command.varName, command.code); + } else { + this.events.request("runcode:eval", command.code); + } + }); + } + + registerEvents() { + this.events.on("runcode:register", this.registerVar.bind(this)); + } + + registerCommands() { + this.events.setCommandHandler('runcode:getContext', (cb) => { + cb(this.runCode.context); + }); + this.events.setCommandHandler('runcode:eval', this.evalCode.bind(this)); + } + + registerEmbarkJs() { + if (this.ipc.connected) { + return; + } + + this.registerVar('IpfsApi', IpfsApi); + this.registerVar('Web3', Web3); + this.registerVar('EmbarkJS', EmbarkJS); + this.events.on('code-generator-ready', () => { + this.events.request('code-generator:embarkjs:provider-code', (code) => { + this.evalCode(code); + this.evalCode(this.getInitProviderCode()); + }); + }); + } + + registerVar(varName, code) { + if (this.ipc.isServer() && varName !== 'web3') { + this.commands.push({varName, code}); + this.ipc.broadcast("runcode:newCommand", {varName, code}); + } + this.runCode.registerVar(varName, code); + } + + evalCode(code, cb, forConsoleOnly = false) { + if (!cb) { + cb = function() {}; + } + const awaitIdx = code.indexOf('await'); + if (awaitIdx > -1) { + if (awaitIdx < 2) { + let end = code.length; + if (code[end - 1] === ';') { + end--; // Remove the `;` because we add function calls + } + code = code.substring(5, end); // remove await keyword + } else { + code = `(async function() {${code}})();`; + } + } + let result = this.runCode.doEval(code); + + if (forConsoleOnly && this.ipc.isServer()) { + this.commands.push({code}); + this.ipc.broadcast("runcode:newCommand", {code}); + } + + if (result instanceof Promise) { + return result.then((value) => cb(null, value)).catch(cb); + } + + cb(null, result); + } + + getInitProviderCode() { + const codeTypes = { + 'communication': this.config.communicationConfig || {}, + 'names': this.config.namesystemConfig || {}, + 'storage': this.config.storageConfig || {} + }; + + return this.plugins.getPluginsFor('initConsoleCode').reduce((acc, plugin) => { + Object.keys(codeTypes).forEach(codeTypeName => { + (plugin.embarkjs_init_console_code[codeTypeName] || []).forEach(initCode => { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, codeTypes[codeTypeName])) { + acc += block; + } + }); + }); + return acc; + }, ''); + + } } module.exports = CodeRunner; diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index ce269d8b9..0fc2ff6cc 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -9,7 +9,7 @@ class RunCode { try { return vm.runInNewContext(code, this.context); } catch(e) { - console.log(e.message); + console.error(e.message); } } diff --git a/lib/modules/whisper/js/message_events.js b/lib/modules/whisper/js/message_events.js index 302d62bea..dd9319035 100644 --- a/lib/modules/whisper/js/message_events.js +++ b/lib/modules/whisper/js/message_events.js @@ -1,4 +1,3 @@ - let __MessageEvents = function() { this.cb = function() {}; }; From 98c785a4bbec2d35e47d2805fd108add7c142cfd Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 29 Aug 2018 11:03:06 +0100 Subject: [PATCH 12/16] Fix register var and runcode --- lib/core/modules/coderunner/codeRunner.js | 17 +++++++++-------- lib/modules/blockchain_connector/index.js | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 0a3d0aaf1..66f995780 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -56,23 +56,24 @@ class CodeRunner { } registerEmbarkJs() { + this.registerVar('IpfsApi', IpfsApi, false); + this.registerVar('Web3', Web3, false); + this.registerVar('EmbarkJS', EmbarkJS, false); + if (this.ipc.connected) { return; } - - this.registerVar('IpfsApi', IpfsApi); - this.registerVar('Web3', Web3); - this.registerVar('EmbarkJS', EmbarkJS); this.events.on('code-generator-ready', () => { this.events.request('code-generator:embarkjs:provider-code', (code) => { - this.evalCode(code); - this.evalCode(this.getInitProviderCode()); + const func = () => {}; + this.evalCode(code, func, true); + this.evalCode(this.getInitProviderCode(), func, true); }); }); } - registerVar(varName, code) { - if (this.ipc.isServer() && varName !== 'web3') { + registerVar(varName, code, toRecord = true) { + if (this.ipc.isServer() && toRecord) { this.commands.push({varName, code}); this.ipc.broadcast("runcode:newCommand", {varName, code}); } diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 6d812ef5e..ce179cce5 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -254,7 +254,7 @@ class BlockchainConnector { registerWeb3Object() { // doesn't feel quite right, should be a cmd or plugin method // can just be a command without a callback - this.events.emit("runcode:register", "web3", this.web3); + this.events.emit("runcode:register", "web3", this.web3, false); } subscribeToPendingTransactions() { From 581b0c9f424ef1ea96884145375c35110dd39581 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 29 Aug 2018 11:08:40 +0100 Subject: [PATCH 13/16] Fix lint --- lib/modules/ens/embarkjs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index fdd746531..26c711476 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -221,7 +221,7 @@ __embarkENS.lookup = function (address, callback) { if (resolverAddress === voidAddress) { return cb('Address not associated to a resolver'); } - let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress, web3: web3 }); + let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress, web3: web3}); resolverContract.methods.name(node).call(cb); }); }; From 31cb79bf5b698b8476dadda53d5249804bc3b84b Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 30 Aug 2018 10:41:13 +0100 Subject: [PATCH 14/16] PR feedback --- cmd/cmd_controller.js | 24 +++++++----- cmd/dashboard/console.js | 47 ++++++++++++++++++++++- cmd/dashboard/dashboard.js | 4 +- cmd/dashboard/repl.js | 4 +- lib/core/modules/coderunner/codeRunner.js | 47 +---------------------- templates/demo/app/components/ens.js | 2 +- 6 files changed, 69 insertions(+), 59 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 305d6ea28..42265c7a2 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -99,7 +99,8 @@ class EmbarkController { version: engine.version, events: engine.events, logger: engine.logger, - ipc: engine.ipc + ipc: engine.ipc, + config: engine.config }).startConsole(); return callback(); } @@ -110,7 +111,8 @@ class EmbarkController { plugins: engine.plugins, version: self.version, env: engine.env, - ipc: engine.ipc + ipc: engine.ipc, + config: engine.config }); dashboard.start(function () { engine.logger.info(__('dashboard start')); @@ -260,6 +262,16 @@ class EmbarkController { webpackConfigName: options.webpackConfigName }); engine.init(); + const repl = new REPL({ + env: engine.env, + plugins: engine.plugins, + version: engine.version, + events: engine.events, + logger: engine.logger, + ipc: engine.ipc, + config: engine.config + }); + repl.startConsole(); async.waterfall([ function startServices(callback) { let pluginList = engine.plugins.listPlugins(); @@ -333,14 +345,6 @@ class EmbarkController { }); }, function startREPL(callback) { - let repl = new REPL({ - env: engine.env, - plugins: engine.plugins, - version: engine.version, - events: engine.events, - logger: engine.logger, - ipc: engine.ipc - }); repl.start(callback); } ], function (err, _result) { diff --git a/cmd/dashboard/console.js b/cmd/dashboard/console.js index 793993012..31036ed7b 100644 --- a/cmd/dashboard/console.js +++ b/cmd/dashboard/console.js @@ -1,5 +1,7 @@ let utils = require('../../lib/utils/utils.js'); - +const EmbarkJS = require('embarkjs'); +const IpfsApi = require('ipfs-api'); +const Web3 = require('web3'); class Console { constructor(options) { this.events = options.events; @@ -7,10 +9,13 @@ class Console { this.version = options.version; this.logger = options.logger; this.ipc = options.ipc; + this.config = options.config; if (this.ipc.isServer()) { this.ipc.on('console:executeCmd', this.executeCmd.bind(this)); } + + this.registerEmbarkJs(); } processEmbarkCmd (cmd) { @@ -65,6 +70,46 @@ class Console { callback(e); } } + + registerEmbarkJs() { + this.events.on('runcode:ready', () => { + this.events.emit('runcode:register', 'IpfsApi', IpfsApi, false); + this.events.emit('runcode:register', 'Web3', Web3, false); + this.events.emit('runcode:register', 'EmbarkJS', EmbarkJS, false); + }); + + this.events.on('code-generator-ready', () => { + if (this.ipc.connected) { + return; + } + + this.events.request('code-generator:embarkjs:provider-code', (code) => { + const func = () => {}; + this.events.request('runcode:eval', code, func, true); + this.events.request('runcode:eval', this.getInitProviderCode(), func, true); + }); + }); + } + + getInitProviderCode() { + const codeTypes = { + 'communication': this.config.communicationConfig || {}, + 'names': this.config.namesystemConfig || {}, + 'storage': this.config.storageConfig || {} + }; + + return this.plugins.getPluginsFor('initConsoleCode').reduce((acc, plugin) => { + Object.keys(codeTypes).forEach(codeTypeName => { + (plugin.embarkjs_init_console_code[codeTypeName] || []).forEach(initCode => { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, codeTypes[codeTypeName])) { + acc += block; + } + }); + }); + return acc; + }, ''); + } } module.exports = Console; diff --git a/cmd/dashboard/dashboard.js b/cmd/dashboard/dashboard.js index 107ebb979..4c707faaa 100644 --- a/cmd/dashboard/dashboard.js +++ b/cmd/dashboard/dashboard.js @@ -12,6 +12,7 @@ class Dashboard { this.version = options.version; this.env = options.env; this.ipc = options.ipc; + this.config = options.config; this.events.on('firstDeploymentDone', this.checkWindowSize.bind(this)); this.events.on('outputDone', this.checkWindowSize.bind(this)); @@ -35,7 +36,8 @@ class Dashboard { plugins: self.plugins, version: self.version, ipc: self.ipc, - logger: self.logger + logger: self.logger, + config: self.config }); callback(); }, diff --git a/cmd/dashboard/repl.js b/cmd/dashboard/repl.js index b89c20f1a..a99bcd5b5 100644 --- a/cmd/dashboard/repl.js +++ b/cmd/dashboard/repl.js @@ -11,6 +11,7 @@ class REPL { this.events = options.events; this.version = options.version; this.ipc = options.ipc; + this.config = options.config; } startConsole(){ @@ -19,7 +20,8 @@ class REPL { plugins: this.plugins, version: this.version, ipc: this.ipc, - logger: this.logger + logger: this.logger, + config: this.config }); } diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 66f995780..daaf9e43d 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -1,7 +1,4 @@ const RunCode = require('./runCode.js'); -const EmbarkJS = require('embarkjs'); -const IpfsApi = require('ipfs-api'); -const Web3 = require('web3'); class CodeRunner { constructor(options) { @@ -16,7 +13,7 @@ class CodeRunner { this.IpcClientListen(); this.registerEvents(); this.registerCommands(); - this.registerEmbarkJs(); + this.events.emit('runcode:ready'); } registerIpcEvents() { @@ -55,23 +52,6 @@ class CodeRunner { this.events.setCommandHandler('runcode:eval', this.evalCode.bind(this)); } - registerEmbarkJs() { - this.registerVar('IpfsApi', IpfsApi, false); - this.registerVar('Web3', Web3, false); - this.registerVar('EmbarkJS', EmbarkJS, false); - - if (this.ipc.connected) { - return; - } - this.events.on('code-generator-ready', () => { - this.events.request('code-generator:embarkjs:provider-code', (code) => { - const func = () => {}; - this.evalCode(code, func, true); - this.evalCode(this.getInitProviderCode(), func, true); - }); - }); - } - registerVar(varName, code, toRecord = true) { if (this.ipc.isServer() && toRecord) { this.commands.push({varName, code}); @@ -81,9 +61,7 @@ class CodeRunner { } evalCode(code, cb, forConsoleOnly = false) { - if (!cb) { - cb = function() {}; - } + cb = cb || function() {}; const awaitIdx = code.indexOf('await'); if (awaitIdx > -1) { if (awaitIdx < 2) { @@ -109,27 +87,6 @@ class CodeRunner { cb(null, result); } - - getInitProviderCode() { - const codeTypes = { - 'communication': this.config.communicationConfig || {}, - 'names': this.config.namesystemConfig || {}, - 'storage': this.config.storageConfig || {} - }; - - return this.plugins.getPluginsFor('initConsoleCode').reduce((acc, plugin) => { - Object.keys(codeTypes).forEach(codeTypeName => { - (plugin.embarkjs_init_console_code[codeTypeName] || []).forEach(initCode => { - let [block, shouldInit] = initCode; - if (shouldInit.call(plugin, codeTypes[codeTypeName])) { - acc += block; - } - }); - }); - return acc; - }, ''); - - } } module.exports = CodeRunner; diff --git a/templates/demo/app/components/ens.js b/templates/demo/app/components/ens.js index b5dd7c223..1afaf5100 100644 --- a/templates/demo/app/components/ens.js +++ b/templates/demo/app/components/ens.js @@ -146,7 +146,7 @@ class ENS extends React.Component {
-

Register subdomain for eth

+

Register subdomain

this.checkEnter(e, this.registerSubDomain)}> {this.state.responseRegister && From 234d2d39a1cd8bcdcf39dcc78359c0264df536f6 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 30 Aug 2018 11:17:44 +0100 Subject: [PATCH 15/16] Fix test --- test/console.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/console.js b/test/console.js index 0ed7eebcc..68aab0ad4 100644 --- a/test/console.js +++ b/test/console.js @@ -8,7 +8,8 @@ let version = require('../package.json').version; describe('embark.Console', function() { let ipc = new IPC({ipcRole: 'none'}); let plugins = new Plugins({plugins: {}}); - let console = new Console({plugins, version, ipc}); + let events = {on: () => {}} + let console = new Console({plugins, version, ipc, events}); describe('#executeCmd', function() { From e0f73c7d22db7bb78b1c5d3ab596d4e4983c2705 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Fri, 31 Aug 2018 09:24:45 +0100 Subject: [PATCH 16/16] Make console a module --- cmd/cmd_controller.js | 29 ++---------- cmd/dashboard/dashboard.js | 47 +++++-------------- cmd/dashboard/monitor.js | 7 ++- cmd/dashboard/repl.js | 24 +--------- lib/core/engine.js | 12 +++++ lib/core/modules/coderunner/codeRunner.js | 1 - .../modules/console/index.js | 15 +++--- test/console.js | 6 +-- 8 files changed, 43 insertions(+), 98 deletions(-) rename cmd/dashboard/console.js => lib/modules/console/index.js (91%) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 42265c7a2..12b35c899 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -55,7 +55,6 @@ class EmbarkController { let self = this; self.context = options.context || [constants.contexts.run, constants.contexts.build]; let Dashboard = require('./dashboard/dashboard.js'); - let REPL = require('./dashboard/repl.js'); let webServerConfig = { enabled: options.runWebserver @@ -93,15 +92,6 @@ class EmbarkController { async.parallel([ function startDashboard(callback) { if (!options.useDashboard) { - new REPL({ - env: engine.env, - plugins: engine.plugins, - version: engine.version, - events: engine.events, - logger: engine.logger, - ipc: engine.ipc, - config: engine.config - }).startConsole(); return callback(); } @@ -110,9 +100,7 @@ class EmbarkController { logger: engine.logger, plugins: engine.plugins, version: self.version, - env: engine.env, - ipc: engine.ipc, - config: engine.config + env: engine.env }); dashboard.start(function () { engine.logger.info(__('dashboard start')); @@ -135,6 +123,7 @@ class EmbarkController { engine.startService("storage"); engine.startService("codeGenerator"); engine.startService("namingSystem"); + engine.startService("console"); engine.events.on('check:backOnline:Ethereum', function () { engine.logger.info(__('Ethereum node detected') + '..'); @@ -262,16 +251,6 @@ class EmbarkController { webpackConfigName: options.webpackConfigName }); engine.init(); - const repl = new REPL({ - env: engine.env, - plugins: engine.plugins, - version: engine.version, - events: engine.events, - logger: engine.logger, - ipc: engine.ipc, - config: engine.config - }); - repl.startConsole(); async.waterfall([ function startServices(callback) { let pluginList = engine.plugins.listPlugins(); @@ -292,11 +271,13 @@ class EmbarkController { engine.startService("codeGenerator"); engine.startService("webServer"); engine.startService("namingSystem"); + engine.startService("console"); return callback(); } engine.startService("codeRunner"); + engine.startService("console"); callback(); }); }, @@ -345,7 +326,7 @@ class EmbarkController { }); }, function startREPL(callback) { - repl.start(callback); + new REPL({events: engine.events, env: engine.env}).start(callback); } ], function (err, _result) { if (err) { diff --git a/cmd/dashboard/dashboard.js b/cmd/dashboard/dashboard.js index 4c707faaa..12715b789 100644 --- a/cmd/dashboard/dashboard.js +++ b/cmd/dashboard/dashboard.js @@ -2,7 +2,6 @@ let async = require('async'); let windowSize = require('window-size'); let Monitor = require('./monitor.js'); -let Console = require('./console.js'); class Dashboard { constructor(options) { @@ -11,8 +10,6 @@ class Dashboard { this.plugins = options.plugins; this.version = options.version; this.env = options.env; - this.ipc = options.ipc; - this.config = options.config; this.events.on('firstDeploymentDone', this.checkWindowSize.bind(this)); this.events.on('outputDone', this.checkWindowSize.bind(this)); @@ -26,45 +23,23 @@ class Dashboard { } start(done) { - let console, monitor; - let self = this; + let monitor; - async.waterfall([ - function startConsole(callback) { - console = new Console({ - events: self.events, - plugins: self.plugins, - version: self.version, - ipc: self.ipc, - logger: self.logger, - config: self.config - }); - callback(); - }, - function startMonitor(callback) { - monitor = new Monitor({env: self.env, console: console, events: self.events}); - self.logger.logFunction = monitor.logEntry; + monitor = new Monitor({env: this.env, events: this.events}); + this.logger.logFunction = monitor.logEntry; - self.events.on('contractsState', monitor.setContracts); - self.events.on('status', monitor.setStatus.bind(monitor)); - self.events.on('servicesState', monitor.availableServices.bind(monitor)); + this.events.on('contractsState', monitor.setContracts); + this.events.on('status', monitor.setStatus.bind(monitor)); + this.events.on('servicesState', monitor.availableServices.bind(monitor)); - self.events.setCommandHandler("console:command", monitor.executeCmd.bind(monitor)); + this.events.setCommandHandler("console:command", monitor.executeCmd.bind(monitor)); - self.logger.info('========================'.bold.green); - self.logger.info((__('Welcome to Embark') + ' ' + self.version).yellow.bold); - self.logger.info('========================'.bold.green); + this.logger.info('========================'.bold.green); + this.logger.info((__('Welcome to Embark') + ' ' + this.version).yellow.bold); + this.logger.info('========================'.bold.green); - // TODO: do this after monitor is rendered - callback(); - } - ], function () { - self.console = console; - self.monitor = monitor; - done(); - }); + done(); } - } module.exports = Dashboard; diff --git a/cmd/dashboard/monitor.js b/cmd/dashboard/monitor.js index f5ba4bbdf..eb2a256c7 100644 --- a/cmd/dashboard/monitor.js +++ b/cmd/dashboard/monitor.js @@ -366,11 +366,10 @@ class Monitor { } executeCmd(cmd, cb) { - const self = this; - self.logText.log('console> '.bold.green + cmd); - self.console.executeCmd(cmd, function (err, result) { + this.logText.log('console> '.bold.green + cmd); + this.events.request('console:executeCmd', cmd, (err, result) => { let message = err || result; - self.logText.log(message); + this.logText.log(message); if (cb) { cb(message); } diff --git a/cmd/dashboard/repl.js b/cmd/dashboard/repl.js index a99bcd5b5..cdc724a95 100644 --- a/cmd/dashboard/repl.js +++ b/cmd/dashboard/repl.js @@ -1,32 +1,13 @@ const repl = require("repl"); const util = require("util"); - -const Console = require('./console.js'); - class REPL { constructor(options) { - this.logger = options.logger; - this.env = options.env; - this.plugins = options.plugins; this.events = options.events; - this.version = options.version; - this.ipc = options.ipc; - this.config = options.config; - } - - startConsole(){ - this.console = new Console({ - events: this.events, - plugins: this.plugins, - version: this.version, - ipc: this.ipc, - logger: this.logger, - config: this.config - }); + this.env = options.env } enhancedEval(cmd, context, filename, callback) { - this.console.executeCmd(cmd.trim(), callback); + this.events.request('console:executeCmd', cmd.trim(), callback); } enhancedWriter(output) { @@ -38,7 +19,6 @@ class REPL { } start(done) { - this.startConsole(); this.replServer = repl.start({ prompt: "Embark (" + this.env + ") > ", useGlobal: true, diff --git a/lib/core/engine.js b/lib/core/engine.js index acba25ccb..67f716a66 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -60,6 +60,7 @@ class Engine { "fileWatcher": this.fileWatchService, "webServer": this.webServerService, "namingSystem": this.namingSystem, + "console": this.console, "web3": this.web3Service, "libraryManager": this.libraryManagerService, "processManager": this.processManagerService, @@ -129,6 +130,17 @@ class Engine { this.registerModule('ens'); } + console(_options) { + this.registerModule('console', { + events: this.events, + plugins: this.plugins, + version: this.version, + ipc: this.ipc, + logger: this.logger, + config: this.config + }); + } + codeRunnerService(_options) { const CodeRunner = require('./modules/coderunner/codeRunner.js'); this.codeRunner = new CodeRunner({ diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index daaf9e43d..3fccdfd29 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -13,7 +13,6 @@ class CodeRunner { this.IpcClientListen(); this.registerEvents(); this.registerCommands(); - this.events.emit('runcode:ready'); } registerIpcEvents() { diff --git a/cmd/dashboard/console.js b/lib/modules/console/index.js similarity index 91% rename from cmd/dashboard/console.js rename to lib/modules/console/index.js index 31036ed7b..97fa303ba 100644 --- a/cmd/dashboard/console.js +++ b/lib/modules/console/index.js @@ -1,9 +1,10 @@ -let utils = require('../../lib/utils/utils.js'); +let utils = require('../../utils/utils'); const EmbarkJS = require('embarkjs'); const IpfsApi = require('ipfs-api'); const Web3 = require('web3'); + class Console { - constructor(options) { + constructor(_embark, options) { this.events = options.events; this.plugins = options.plugins; this.version = options.version; @@ -14,7 +15,7 @@ class Console { if (this.ipc.isServer()) { this.ipc.on('console:executeCmd', this.executeCmd.bind(this)); } - + this.events.setCommandHandler("console:executeCmd", this.executeCmd.bind(this)); this.registerEmbarkJs(); } @@ -72,11 +73,9 @@ class Console { } registerEmbarkJs() { - this.events.on('runcode:ready', () => { - this.events.emit('runcode:register', 'IpfsApi', IpfsApi, false); - this.events.emit('runcode:register', 'Web3', Web3, false); - this.events.emit('runcode:register', 'EmbarkJS', EmbarkJS, false); - }); + this.events.emit('runcode:register', 'IpfsApi', IpfsApi, false); + this.events.emit('runcode:register', 'Web3', Web3, false); + this.events.emit('runcode:register', 'EmbarkJS', EmbarkJS, false); this.events.on('code-generator-ready', () => { if (this.ipc.connected) { diff --git a/test/console.js b/test/console.js index 68aab0ad4..dac075a7f 100644 --- a/test/console.js +++ b/test/console.js @@ -1,5 +1,5 @@ /*globals describe, it*/ -let Console = require('../cmd/dashboard/console.js'); +let Console = require('../lib/modules/console/'); let Plugins = require('../lib/core/plugins.js'); let IPC = require('../lib/core/ipc.js'); let assert = require('assert'); @@ -8,8 +8,8 @@ let version = require('../package.json').version; describe('embark.Console', function() { let ipc = new IPC({ipcRole: 'none'}); let plugins = new Plugins({plugins: {}}); - let events = {on: () => {}} - let console = new Console({plugins, version, ipc, events}); + let events = {on: () => {}, setCommandHandler: () => {}, emit: () => {}}; + let console = new Console({}, {plugins, version, ipc, events}); describe('#executeCmd', function() {