Fix circular JSON and swarm api require

This commit is contained in:
Anthony Laibe 2018-09-13 11:13:52 +01:00
parent 323ec0d529
commit c22901ab95
6 changed files with 49 additions and 26 deletions

View File

@ -296,20 +296,31 @@ class EmbarkController {
if(!engine.ipc.connected || engine.ipc.isServer()) {
return callback();
}
const Provider = require('../lib/modules/blockchain_connector/provider');
const Web3 = require('web3');
let web3 = new Web3();
engine.ipc.request("runcode:getCommands", null, (_, {web3Config, commands}) => {
web3.setProvider(web3Config.provider.host);
web3.eth.defaultAccount = web3Config.defaultAccount;
engine.events.emit("runcode:register", "web3", web3);
async.each(commands, ({varName, code}, next) => {
if (varName) {
engine.events.emit("runcode:register", varName, code);
} else {
engine.events.request("runcode:eval", code);
}
next();
}, callback);
const providerOptions = {
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);
async.each(commands, ({varName, code}, next) => {
if (varName) {
engine.events.emit("runcode:register", varName, code);
} else {
engine.events.request("runcode:eval", code);
}
next();
}, callback);
});
});
},
function deploy(callback) {

View File

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

View File

@ -29,7 +29,7 @@ class RunCode {
}
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,6 @@
/*global web3 */
let __embarkSwarm = {_swarmConnection: undefined};
import SwarmAPI from 'swarm-api';
const SwarmAPI = require('swarm-api');
__embarkSwarm.setProvider = function (options) {
let protocol = options.protocol || 'http';

7
package-lock.json generated
View File

@ -3687,6 +3687,11 @@
"resolved": "https://registry.npmjs.org/flatmap/-/flatmap-0.0.3.tgz",
"integrity": "sha1-Hxik2TgVLUlZZfnJWNkjqy3WabQ="
},
"flatted": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/flatted/-/flatted-0.2.3.tgz",
"integrity": "sha512-C4B5UtK3kOrLAyZ1ftqEWprxCfLmCIqEcNufZrtsJhiZ/fcI5mvCgtAtC3pu7BC9KE7aUIrPXwTgcT1fiI7QhA=="
},
"flatten": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/flatten/-/flatten-1.0.2.tgz",
@ -7926,7 +7931,7 @@
},
"readable-stream": {
"version": "2.3.6",
"resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
"integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==",
"requires": {
"core-util-is": "~1.0.0",

View File

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