Merge pull request #822 from embark-framework/bugfix/circular-json

Fix circular JSON and swarm api require
This commit is contained in:
Iuri Matias 2018-09-14 18:11:34 -04:00 committed by GitHub
commit 9b2fa9402b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 25 deletions

View File

@ -296,11 +296,21 @@ class EmbarkController {
if(!engine.ipc.connected || engine.ipc.isServer()) { if(!engine.ipc.connected || engine.ipc.isServer()) {
return callback(); return callback();
} }
const Provider = require('../lib/modules/blockchain_connector/provider');
const Web3 = require('web3'); const Web3 = require('web3');
let web3 = new Web3(); let web3 = new Web3();
engine.ipc.request("runcode:getCommands", null, (_, {web3Config, commands}) => { engine.ipc.request("runcode:getCommands", null, (_, {web3Config, commands}) => {
web3.setProvider(web3Config.provider.host); const providerOptions = {
web3.eth.defaultAccount = web3Config.defaultAccount; web3: web3,
accountsConfig: engine.config.contractsConfig.deployment.accounts,
blockchainConfig: engine.config.blockchainConfig,
logger: engine.logger,
isDev: engine.isDev,
type: engine.config.contractsConfig.deployment.type,
web3Endpoint: web3Config.providerUrl
};
const provider = new Provider(providerOptions);
provider.startWeb3Provider(() => {
engine.events.emit("runcode:register", "web3", web3); engine.events.emit("runcode:register", "web3", web3);
async.each(commands, ({varName, code}, next) => { async.each(commands, ({varName, code}, next) => {
if (varName) { if (varName) {
@ -311,6 +321,7 @@ class EmbarkController {
next(); next();
}, callback); }, callback);
}); });
});
}, },
function deploy(callback) { function deploy(callback) {
// Skip if we are connected to a websocket, the server will do it // Skip if we are connected to a websocket, the server will do it

View File

@ -1,5 +1,6 @@
let fs = require('./fs.js'); const fs = require('./fs.js');
let ipc = require('node-ipc'); const ipc = require('node-ipc');
const {parse, stringify} = require('flatted/cjs');
class IPC { class IPC {
@ -47,35 +48,40 @@ class IPC {
on(action, done) { on(action, done) {
const self = this; const self = this;
ipc.server.on('message', function(data, socket) { ipc.server.on('message', function(messageString, socket) {
if (data.action !== action) { const message = parse(messageString);
if (message.action !== action) {
return; return;
} }
let reply = function(error, replyData) { let reply = function(error, replyData) {
self.reply(socket, action, error, replyData); self.reply(socket, action, error, replyData);
}; };
done(data.message, reply, socket); done(message.data, reply, socket);
}); });
} }
reply(client, action, error, data) { reply(client, action, error, data) {
ipc.server.emit(client, 'message', {action: action, message: data, error: (error && error.stack)}); const message = stringify({action, data, error: (error && error.stack)});
ipc.server.emit(client, 'message', message);
} }
listenTo(action, callback) { listenTo(action, callback) {
ipc.of['embark'].on(action, callback); ipc.of['embark'].on(action, (messageString) => {
callback(parse(messageString));
});
} }
broadcast(action, data) { broadcast(action, data) {
ipc.server.broadcast(action, data); ipc.server.broadcast(action, stringify(data));
} }
once(action, cb) { once(action, cb) {
ipc.of['embark'].once('message', function(msg) { ipc.of['embark'].once('message', function(messageString) {
if (msg.action !== action) { const message = parse(messageString);
if (message.action !== action) {
return; return;
} }
cb(msg.error, msg.message); cb(message.error, message.data);
}); });
} }
@ -83,7 +89,7 @@ class IPC {
if (cb) { if (cb) {
this.once(action, cb); this.once(action, cb);
} }
ipc.of['embark'].emit('message', {action: action, message: data}); ipc.of['embark'].emit('message', stringify({action: action, data: data}));
} }
isClient() { isClient() {

View File

@ -29,7 +29,7 @@ class RunCode {
} }
getWeb3Config() { getWeb3Config() {
return {defaultAccount: this.context.web3.eth.defaultAccount, provider: this.context.web3.currentProvider}; return {defaultAccount: this.context.web3.eth.defaultAccount, providerUrl: this.context.web3.currentProvider.connection._url};
} }
} }

View File

@ -1,6 +1,9 @@
/*global web3 */ /*global web3 */
let __embarkSwarm = {_swarmConnection: undefined}; let __embarkSwarm = {_swarmConnection: undefined};
import SwarmAPI from 'swarm-api'; let SwarmAPI = require('swarm-api');
if (SwarmAPI.default) {
SwarmAPI = SwarmAPI.default;
}
__embarkSwarm.setProvider = function (options) { __embarkSwarm.setProvider = function (options) {
let protocol = options.protocol || 'http'; let protocol = options.protocol || 'http';

5
package-lock.json generated
View File

@ -3719,6 +3719,11 @@
"resolved": "https://registry.npmjs.org/flatmap/-/flatmap-0.0.3.tgz", "resolved": "https://registry.npmjs.org/flatmap/-/flatmap-0.0.3.tgz",
"integrity": "sha1-Hxik2TgVLUlZZfnJWNkjqy3WabQ=" "integrity": "sha1-Hxik2TgVLUlZZfnJWNkjqy3WabQ="
}, },
"flatted": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/flatted/-/flatted-0.2.3.tgz",
"integrity": "sha512-C4B5UtK3kOrLAyZ1ftqEWprxCfLmCIqEcNufZrtsJhiZ/fcI5mvCgtAtC3pu7BC9KE7aUIrPXwTgcT1fiI7QhA=="
},
"flatten": { "flatten": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/flatten/-/flatten-1.0.2.tgz", "resolved": "https://registry.npmjs.org/flatten/-/flatten-1.0.2.tgz",

View File

@ -47,6 +47,7 @@
"ethereumjs-wallet": "0.6.0", "ethereumjs-wallet": "0.6.0",
"file-loader": "^1.1.5", "file-loader": "^1.1.5",
"finalhandler": "^1.1.1", "finalhandler": "^1.1.1",
"flatted": "^0.2.3",
"follow-redirects": "^1.2.4", "follow-redirects": "^1.2.4",
"fs-extra": "^2.0.0", "fs-extra": "^2.0.0",
"ganache-cli": "^6.1.6", "ganache-cli": "^6.1.6",