mirror of https://github.com/embarklabs/embark.git
rebase fixes
This commit is contained in:
parent
d9ae5cbfc7
commit
7532562e28
437
js/embark.js
437
js/embark.js
|
@ -1,437 +0,0 @@
|
|||
import {detectSeries} from 'async';
|
||||
|
||||
var EmbarkJS = {
|
||||
onReady: function (cb) {
|
||||
if (typeof (__embarkContext) === 'undefined') {
|
||||
return cb();
|
||||
}
|
||||
return __embarkContext.execWhenReady(cb);
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.isNewWeb3 = function (web3Obj) {
|
||||
var _web3 = web3Obj || (new Web3());
|
||||
if (typeof(_web3.version) === "string") {
|
||||
return true;
|
||||
}
|
||||
return parseInt(_web3.version.api.split('.')[0], 10) >= 1;
|
||||
};
|
||||
|
||||
EmbarkJS.Contract = function (options) {
|
||||
var self = this;
|
||||
var i, abiElement;
|
||||
var ContractClass;
|
||||
|
||||
this.abi = options.abi;
|
||||
this.address = options.address;
|
||||
this.gas = options.gas;
|
||||
this.code = '0x' + options.code;
|
||||
//this.web3 = options.web3 || web3;
|
||||
this.web3 = options.web3;
|
||||
if (!this.web3 && typeof (web3) !== 'undefined') {
|
||||
this.web3 = web3;
|
||||
} else if (!this.web3) {
|
||||
this.web3 = window.web3;
|
||||
}
|
||||
|
||||
if (EmbarkJS.isNewWeb3(this.web3)) {
|
||||
ContractClass = new this.web3.eth.Contract(this.abi, this.address);
|
||||
ContractClass.setProvider(this.web3.currentProvider);
|
||||
ContractClass.options.data = this.code;
|
||||
ContractClass.options.from = this.from;
|
||||
ContractClass.abi = ContractClass.options.abi;
|
||||
ContractClass.address = this.address;
|
||||
ContractClass.gas = this.gas;
|
||||
|
||||
let originalMethods = Object.keys(ContractClass);
|
||||
|
||||
ContractClass._jsonInterface.forEach((abi) => {
|
||||
if (originalMethods.indexOf(abi.name) >= 0) {
|
||||
console.log(abi.name + " is a reserved word and cannot be used as a contract method, property or event");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!abi.inputs) {
|
||||
return;
|
||||
}
|
||||
|
||||
let numExpectedInputs = abi.inputs.length;
|
||||
|
||||
if (abi.type === 'function' && abi.constant) {
|
||||
ContractClass[abi.name] = function () {
|
||||
let options = {}, cb = null, args = Array.from(arguments || []).slice(0, numExpectedInputs);
|
||||
if (typeof (arguments[numExpectedInputs]) === 'function') {
|
||||
cb = arguments[numExpectedInputs];
|
||||
} else if (typeof (arguments[numExpectedInputs]) === 'object') {
|
||||
options = arguments[numExpectedInputs];
|
||||
cb = arguments[numExpectedInputs + 1];
|
||||
}
|
||||
|
||||
let ref = ContractClass.methods[abi.name];
|
||||
let call = ref.apply(ref, ...arguments).call;
|
||||
return call.apply(call, []);
|
||||
};
|
||||
} else if (abi.type === 'function') {
|
||||
ContractClass[abi.name] = function () {
|
||||
let options = {}, cb = null, args = Array.from(arguments || []).slice(0, numExpectedInputs);
|
||||
if (typeof (arguments[numExpectedInputs]) === 'function') {
|
||||
cb = arguments[numExpectedInputs];
|
||||
} else if (typeof (arguments[numExpectedInputs]) === 'object') {
|
||||
options = arguments[numExpectedInputs];
|
||||
cb = arguments[numExpectedInputs + 1];
|
||||
}
|
||||
|
||||
let ref = ContractClass.methods[abi.name];
|
||||
let send = ref.apply(ref, args).send;
|
||||
return send.apply(send, [options, cb]);
|
||||
};
|
||||
} else if (abi.type === 'event') {
|
||||
ContractClass[abi.name] = function (options, cb) {
|
||||
let ref = ContractClass.events[abi.name];
|
||||
return ref.apply(ref, [options, cb]);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return ContractClass;
|
||||
} else {
|
||||
ContractClass = this.web3.eth.contract(this.abi);
|
||||
|
||||
this.eventList = [];
|
||||
|
||||
if (this.abi) {
|
||||
for (i = 0; i < this.abi.length; i++) {
|
||||
abiElement = this.abi[i];
|
||||
if (abiElement.type === 'event') {
|
||||
this.eventList.push(abiElement.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var messageEvents = function () {
|
||||
this.cb = function () {
|
||||
};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function (cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function (err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
this._originalContractObject = ContractClass.at(this.address);
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) {
|
||||
// TODO: check for forbidden properties
|
||||
if (self.eventList.indexOf(p) >= 0) {
|
||||
|
||||
self[p] = function () {
|
||||
var promise = new messageEvents();
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
args.push(function (err, result) {
|
||||
if (err) {
|
||||
promise.error(err);
|
||||
} else {
|
||||
promise.cb(result);
|
||||
}
|
||||
});
|
||||
|
||||
self._originalContractObject[p].apply(self._originalContractObject[p], args);
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
} else if (typeof self._originalContractObject[p] === 'function') {
|
||||
self[p] = function (_args) {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var fn = self._originalContractObject[p];
|
||||
var props = self.abi.find((x) => x.name == p);
|
||||
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
args.push(function (err, transaction) {
|
||||
promise.tx = transaction;
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
var getConfirmation = function () {
|
||||
self.web3.eth.getTransactionReceipt(transaction, function (err, receipt) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
if (receipt !== null) {
|
||||
return resolve(receipt);
|
||||
}
|
||||
|
||||
setTimeout(getConfirmation, 1000);
|
||||
});
|
||||
};
|
||||
|
||||
if (typeof transaction !== "string" || props.constant) {
|
||||
resolve(transaction);
|
||||
} else {
|
||||
getConfirmation();
|
||||
}
|
||||
});
|
||||
|
||||
fn.apply(fn, args);
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.deploy = function (args, _options) {
|
||||
var self = this;
|
||||
var contractParams;
|
||||
var options = _options || {};
|
||||
|
||||
contractParams = args || [];
|
||||
|
||||
contractParams.push({
|
||||
from: this.web3.eth.accounts[0],
|
||||
data: this.code,
|
||||
gas: options.gas || 800000
|
||||
});
|
||||
|
||||
var contractObject = this.web3.eth.contract(this.abi);
|
||||
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
contractParams.push(function (err, transaction) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({
|
||||
abi: self.abi,
|
||||
code: self.code,
|
||||
address: transaction.address
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
// returns promise
|
||||
// deploys contract
|
||||
// wraps it around EmbarkJS.Contract
|
||||
contractObject["new"].apply(contractObject, contractParams);
|
||||
});
|
||||
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.new = EmbarkJS.Contract.prototype.deploy;
|
||||
|
||||
EmbarkJS.Contract.prototype.at = function (address) {
|
||||
return new EmbarkJS.Contract({abi: this.abi, code: this.code, address: address});
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.send = function (value, unit, _options) {
|
||||
var options, wei;
|
||||
if (typeof unit === 'object') {
|
||||
options = unit;
|
||||
wei = value;
|
||||
} else {
|
||||
options = _options || {};
|
||||
wei = this.web3.toWei(value, unit);
|
||||
}
|
||||
|
||||
options.to = this.address;
|
||||
options.value = wei;
|
||||
|
||||
this.web3.eth.sendTransaction(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage = {};
|
||||
|
||||
EmbarkJS.Storage.Providers = {};
|
||||
|
||||
EmbarkJS.Storage.saveText = function (text) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.saveText(text);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.get = function (hash) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.get(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.uploadFile = function (inputSelector) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.uploadFile(inputSelector);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.getUrl = function (hash) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.getUrl(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.registerProvider = function (providerName, obj) {
|
||||
EmbarkJS.Storage.Providers[providerName] = obj;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.setProvider = function (provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown storage provider');
|
||||
}
|
||||
|
||||
this.currentStorage = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.isAvailable = function () {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.isAvailable();
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.setProviders = async function (dappConnOptions) {
|
||||
try {
|
||||
await detectSeries(dappConnOptions, async (dappConn, callback) => {
|
||||
if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){
|
||||
let options = dappConn;
|
||||
if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true};
|
||||
try{
|
||||
await EmbarkJS.Storage.setProvider('swarm', options);
|
||||
let isAvailable = await EmbarkJS.Storage.isAvailable();
|
||||
callback(null, isAvailable);
|
||||
}catch(err){
|
||||
callback(null, false); // catch errors for when bzz object not initialised but config has requested it to be used
|
||||
}
|
||||
}
|
||||
else if(dappConn.provider === 'ipfs') {
|
||||
// set the provider then check the connection, if true, use that provider, else, check next provider
|
||||
try{
|
||||
await EmbarkJS.Storage.setProvider('ipfs', dappConn);
|
||||
let isAvailable = await EmbarkJS.Storage.isAvailable();
|
||||
callback(null, isAvailable);
|
||||
} catch(err) {
|
||||
callback(null, false); // catch but keep looping by not passing err to callback
|
||||
}
|
||||
}
|
||||
}, function(err, result){
|
||||
if(!result) throw new Error('Could not connect to a storage provider using any of the dappConnections in the storage config');
|
||||
});
|
||||
} catch (err) {
|
||||
throw new Error('Failed to connect to a storage provider: ' + err.message);
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Messages = {};
|
||||
|
||||
EmbarkJS.Messages.Providers = {};
|
||||
|
||||
EmbarkJS.Messages.registerProvider = function (providerName, obj) {
|
||||
EmbarkJS.Messages.Providers[providerName] = obj;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function (provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown messages provider');
|
||||
}
|
||||
|
||||
this.currentMessages = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.isAvailable = function () {
|
||||
return this.currentMessages.isAvailable();
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.sendMessage = function (options) {
|
||||
if (!this.currentMessages) {
|
||||
throw new Error('Messages provider not set; e.g EmbarkJS.Messages.setProvider("whisper")');
|
||||
}
|
||||
return this.currentMessages.sendMessage(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.listenTo = function (options, callback) {
|
||||
if (!this.currentMessages) {
|
||||
throw new Error('Messages provider not set; e.g EmbarkJS.Messages.setProvider("whisper")');
|
||||
}
|
||||
return this.currentMessages.listenTo(options, callback);
|
||||
};
|
||||
|
||||
EmbarkJS.Names = {};
|
||||
|
||||
EmbarkJS.Names.Providers = {};
|
||||
|
||||
EmbarkJS.Names.registerProvider = function (providerName, obj) {
|
||||
EmbarkJS.Names.Providers[providerName] = obj;
|
||||
};
|
||||
|
||||
EmbarkJS.Names.setProvider = function (provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown name system provider');
|
||||
}
|
||||
|
||||
this.currentNameSystems = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
// resolve resolves a name into an identifier of some kind
|
||||
EmbarkJS.Names.resolve = function (name, callback) {
|
||||
if (!this.currentNameSystems) {
|
||||
throw new Error('Name system provider not set; e.g EmbarkJS.Names.setProvider("ens")');
|
||||
}
|
||||
return this.currentNameSystems.resolve(name, callback);
|
||||
};
|
||||
|
||||
// the reverse of resolve, resolves using an identifier to get to a name
|
||||
EmbarkJS.Names.lookup = function (identifier, callback) {
|
||||
if (!this.currentNameSystems) {
|
||||
throw new Error('Name system provider not set; e.g EmbarkJS.Names.setProvider("ens")');
|
||||
}
|
||||
return this.currentNameSystems.lookup(identifier, callback);
|
||||
};
|
||||
|
||||
EmbarkJS.Names.isAvailable = function () {
|
||||
return this.currentNameSystems.isAvailable();
|
||||
};
|
||||
|
||||
// To Implement
|
||||
|
||||
|
||||
// register a name
|
||||
EmbarkJS.Names.register = function(name, options) {
|
||||
if (!this.currentNameSystems) {
|
||||
throw new Error('Name system provider not set; e.g EmbarkJS.Names.setProvider("ens")');
|
||||
}
|
||||
return this.currentNameSystems.register(name, options);
|
||||
};
|
||||
|
||||
EmbarkJS.Utils = {
|
||||
fromAscii: function (str) {
|
||||
var _web3 = new Web3();
|
||||
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);
|
||||
},
|
||||
toAscii: function (str) {
|
||||
var _web3 = new Web3();
|
||||
return _web3.utils.toAscii(str);
|
||||
}
|
||||
};
|
||||
|
||||
export default EmbarkJS;
|
|
@ -1,42 +0,0 @@
|
|||
class Scaffolding {
|
||||
constructor(embark, options){
|
||||
this.embark = embark;
|
||||
this.options = options;
|
||||
this.framework = options.framework;
|
||||
this.frameworkPlugin = null;
|
||||
}
|
||||
|
||||
isContract(contractName){
|
||||
return this.embark.config.contractsConfig.contracts[contractName] !== undefined;
|
||||
}
|
||||
|
||||
generate(contractName, contractConfiguration){
|
||||
if(this.framework == 'react'){
|
||||
this.embark.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
||||
}
|
||||
|
||||
let dappGenerators = this.embark.plugins.getPluginsFor('dappGenerator');
|
||||
let build = null;
|
||||
dappGenerators.forEach((plugin) => {
|
||||
plugin.dappGenerators.forEach((d) => {
|
||||
if(d.framework == this.framework){
|
||||
build = d.cb;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if(build === null){
|
||||
throw new Error("Could not find plugin for framework '" + this.framework + "'");
|
||||
}
|
||||
|
||||
if(!this.isContract(contractName)){
|
||||
return new Error("contract '" + contractName + "' does not exist");
|
||||
}
|
||||
|
||||
const contract = contractConfiguration.contracts[contractName];
|
||||
build(contract);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = Scaffolding;
|
|
@ -1,53 +0,0 @@
|
|||
const path = require('path');
|
||||
let shelljs = require('shelljs');
|
||||
let proxy = require('../contracts/proxy');
|
||||
const Ipc = require('../core/ipc');
|
||||
const constants = require('../constants.json');
|
||||
const {defaultHost, dockerHostSwap} = require('../utils/host');
|
||||
|
||||
class Simulator {
|
||||
constructor(options) {
|
||||
this.blockchainConfig = options.blockchainConfig;
|
||||
this.logger = options.logger;
|
||||
}
|
||||
|
||||
run(options) {
|
||||
let cmds = [];
|
||||
|
||||
const ganache = path.join(__dirname, '../../node_modules/.bin/ganache-cli');
|
||||
|
||||
let useProxy = this.blockchainConfig.proxy || false;
|
||||
let host = (dockerHostSwap(options.host || this.blockchainConfig.rpcHost) || defaultHost);
|
||||
let port = (options.port || this.blockchainConfig.rpcPort || 8545);
|
||||
|
||||
cmds.push("-p " + (port + (useProxy ? constants.blockchain.servicePortOnProxy : 0)));
|
||||
cmds.push("-h " + host);
|
||||
cmds.push("-a " + (options.numAccounts || 10));
|
||||
cmds.push("-e " + (options.defaultBalance || 100));
|
||||
cmds.push("-l " + (options.gasLimit || 8000000));
|
||||
|
||||
// adding mnemonic only if it is defined in the blockchainConfig or options
|
||||
let simulatorMnemonic = this.blockchainConfig.simulatorMnemonic || options.simulatorMnemonic;
|
||||
if (simulatorMnemonic) {
|
||||
cmds.push("--mnemonic \"" + (simulatorMnemonic) +"\"");
|
||||
}
|
||||
|
||||
// adding blocktime only if it is defined in the blockchainConfig or options
|
||||
let simulatorBlocktime = this.blockchainConfig.simulatorBlocktime || options.simulatorBlocktime;
|
||||
if (simulatorBlocktime) {
|
||||
cmds.push("-b \"" + (simulatorBlocktime) +"\"");
|
||||
}
|
||||
|
||||
const programName = 'ganache-cli';
|
||||
const program = ganache;
|
||||
console.log(`running: ${programName} ${cmds.join(' ')}`);
|
||||
shelljs.exec(`${program} ${cmds.join(' ')}`, {async : true});
|
||||
|
||||
if(useProxy){
|
||||
let ipcObject = new Ipc({ipcRole: 'client'});
|
||||
proxy.serve(ipcObject, host, port, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Simulator;
|
|
@ -1,129 +0,0 @@
|
|||
const httpProxy = require('http-proxy');
|
||||
const http = require('http');
|
||||
const constants = require('../constants.json');
|
||||
|
||||
let commList = {};
|
||||
let transactions = {};
|
||||
let receipts = {};
|
||||
|
||||
const {canonicalHost, defaultHost} = require('../utils/host');
|
||||
|
||||
const parseRequest = function(reqBody) {
|
||||
let jsonO;
|
||||
try {
|
||||
jsonO = JSON.parse(reqBody);
|
||||
} catch (e) {
|
||||
return; // Request is not a json. Do nothing
|
||||
}
|
||||
if (jsonO.method === "eth_sendTransaction") {
|
||||
commList[jsonO.id] = {
|
||||
type: 'contract-log',
|
||||
address: jsonO.params[0].to,
|
||||
data: jsonO.params[0].data
|
||||
};
|
||||
} else if (jsonO.method === "eth_getTransactionReceipt") {
|
||||
if (transactions[jsonO.params[0]]) {
|
||||
transactions[jsonO.params[0]].receiptId = jsonO.id;
|
||||
receipts[jsonO.id] = transactions[jsonO.params[0]].commListId;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const parseResponse = function(ipc, resBody) {
|
||||
let jsonO;
|
||||
try {
|
||||
jsonO = JSON.parse(resBody);
|
||||
} catch (e) {
|
||||
return; // Response is not a json. Do nothing
|
||||
}
|
||||
|
||||
if (commList[jsonO.id]) {
|
||||
commList[jsonO.id].transactionHash = jsonO.result;
|
||||
transactions[jsonO.result] = {commListId: jsonO.id};
|
||||
} else if (receipts[jsonO.id] && jsonO.result && jsonO.result.blockNumber) {
|
||||
commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber;
|
||||
commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed;
|
||||
commList[receipts[jsonO.id]].status = jsonO.result.status;
|
||||
|
||||
if (ipc.connected && !ipc.connecting) {
|
||||
ipc.request('log', commList[receipts[jsonO.id]]);
|
||||
} else {
|
||||
ipc.connecting = true;
|
||||
ipc.connect(() => {
|
||||
ipc.connecting = false;
|
||||
});
|
||||
}
|
||||
|
||||
delete transactions[commList[receipts[jsonO.id]].transactionHash];
|
||||
delete receipts[jsonO.id];
|
||||
delete commList[jsonO.id];
|
||||
}
|
||||
};
|
||||
|
||||
exports.serve = function(ipc, host, port, ws) {
|
||||
let proxy = httpProxy.createProxyServer({
|
||||
target: {
|
||||
host: canonicalHost(host),
|
||||
port: port + constants.blockchain.servicePortOnProxy
|
||||
},
|
||||
ws: ws
|
||||
});
|
||||
|
||||
proxy.on('error', function(e) {
|
||||
console.error(__("Error forwarding requests to blockchain/simulator"), e.message);
|
||||
});
|
||||
|
||||
proxy.on('proxyRes', (proxyRes) => {
|
||||
let resBody = [];
|
||||
proxyRes.on('data', (b) => resBody.push(b));
|
||||
proxyRes.on('end', function() {
|
||||
resBody = Buffer.concat(resBody).toString();
|
||||
if (resBody) {
|
||||
parseResponse(ipc, resBody);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let server = http.createServer((req, res) => {
|
||||
let reqBody = [];
|
||||
req.on('data', (b) => {
|
||||
reqBody.push(b);
|
||||
})
|
||||
.on('end', () => {
|
||||
reqBody = Buffer.concat(reqBody).toString();
|
||||
if (reqBody) {
|
||||
parseRequest(reqBody);
|
||||
}
|
||||
});
|
||||
|
||||
if (!ws) {
|
||||
proxy.web(req, res);
|
||||
}
|
||||
});
|
||||
|
||||
if (ws) {
|
||||
const WsParser = require('simples/lib/parsers/ws'); // npm install simples
|
||||
|
||||
server.on('upgrade', function(req, socket, head) {
|
||||
proxy.ws(req, socket, head);
|
||||
});
|
||||
|
||||
proxy.on('open', (proxySocket) => {
|
||||
proxySocket.on('data', (data) => {
|
||||
parseResponse(ipc, data.toString().substr(data.indexOf("{")));
|
||||
});
|
||||
});
|
||||
|
||||
proxy.on('proxyReqWs', (proxyReq, req, socket) => {
|
||||
var parser = new WsParser(0, false);
|
||||
socket.pipe(parser);
|
||||
parser.on('frame', function(frame) {
|
||||
parseRequest(frame.data);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
server.listen(port, defaultHost);
|
||||
return server;
|
||||
};
|
|
@ -103,6 +103,7 @@ class CodeRunner {
|
|||
cb(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = CodeRunner;
|
||||
|
|
|
@ -47,6 +47,9 @@ class ProcessLauncher {
|
|||
_subscribeToMessages() {
|
||||
const self = this;
|
||||
this.process.on('message', (msg) => {
|
||||
if (msg.error) {
|
||||
self.logger.error(msg.error);
|
||||
}
|
||||
if (msg.result === constants.process.log) {
|
||||
return self._handleLog(msg);
|
||||
}
|
||||
|
|
|
@ -1,221 +0,0 @@
|
|||
const child_process = require('child_process');
|
||||
const constants = require('../../constants');
|
||||
const path = require('path');
|
||||
const utils = require('../../utils/utils');
|
||||
|
||||
class ProcessLauncher {
|
||||
|
||||
/**
|
||||
* Constructor of ProcessLauncher. Forks the module and sets up the message handling
|
||||
* @param {Object} options Options tp start the process
|
||||
* * modulePath {String} Absolute path to the module to fork
|
||||
* * logger {Object} Logger
|
||||
* * events {Function} Events Emitter instance
|
||||
* @return {ProcessLauncher} The ProcessLauncher instance
|
||||
*/
|
||||
constructor(options) {
|
||||
this.name = path.basename(options.modulePath);
|
||||
this.process = child_process.fork(options.modulePath);
|
||||
this.logger = options.logger;
|
||||
this.events = options.events;
|
||||
this.silent = options.silent;
|
||||
this.exitCallback = options.exitCallback;
|
||||
|
||||
this.subscriptions = {};
|
||||
this._subscribeToMessages();
|
||||
}
|
||||
|
||||
// Subscribes to messages from the child process and delegates to the right methods
|
||||
_subscribeToMessages() {
|
||||
const self = this;
|
||||
this.process.on('message', (msg) => {
|
||||
if (msg.error) {
|
||||
self.logger.error(msg.error);
|
||||
}
|
||||
if (msg.result === constants.process.log) {
|
||||
return self._handleLog(msg);
|
||||
}
|
||||
if (msg.event) {
|
||||
return self._handleEvent(msg);
|
||||
}
|
||||
self._checkSubscriptions(msg);
|
||||
});
|
||||
|
||||
this.process.on('exit', (code) => {
|
||||
if (self.exitCallback) {
|
||||
return self.exitCallback(code);
|
||||
}
|
||||
if (code) {
|
||||
this.logger.info(`Child Process ${this.name} exited with code ${code}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Translates logs from the child process to the logger
|
||||
_handleLog(msg) {
|
||||
if (this.silent && msg.type !== 'error') {
|
||||
return;
|
||||
}
|
||||
if (this.logger[msg.type]) {
|
||||
return this.logger[msg.type](utils.normalizeInput(msg.message));
|
||||
}
|
||||
this.logger.debug(utils.normalizeInput(msg.message));
|
||||
}
|
||||
|
||||
// Handle event calls from the child process
|
||||
_handleEvent(msg) {
|
||||
const self = this;
|
||||
if (!self.events[msg.event]) {
|
||||
self.logger.warn('Unknown event method called: ' + msg.event);
|
||||
return;
|
||||
}
|
||||
if (!msg.args || !Array.isArray(msg.args)) {
|
||||
msg.args = [];
|
||||
}
|
||||
// Add callback in the args
|
||||
msg.args.push((result) => {
|
||||
self.process.send({
|
||||
event: constants.process.events.response,
|
||||
result,
|
||||
eventId: msg.eventId
|
||||
});
|
||||
});
|
||||
self.events[msg.event](msg.requestName, ...msg.args);
|
||||
}
|
||||
|
||||
// Looks at the subscriptions to see if there is a callback to call
|
||||
_checkSubscriptions(msg) {
|
||||
const messageKeys = Object.keys(msg);
|
||||
const subscriptionsKeys = Object.keys(this.subscriptions);
|
||||
let subscriptionsForKey;
|
||||
let messageKey;
|
||||
// Find if the message contains a key that we are subscribed to
|
||||
messageKeys.some(_messageKey => {
|
||||
return subscriptionsKeys.some(subscriptionKey => {
|
||||
if (_messageKey === subscriptionKey) {
|
||||
subscriptionsForKey = this.subscriptions[subscriptionKey];
|
||||
messageKey = _messageKey;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
});
|
||||
|
||||
if (subscriptionsForKey) {
|
||||
// Find if we are subscribed to one of the values
|
||||
let subsIndex = [];
|
||||
const subscriptionsForValue = subscriptionsForKey.filter((sub, index) => {
|
||||
if (msg[messageKey] === sub.value) {
|
||||
subsIndex.push(index);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
if (subscriptionsForValue.length) {
|
||||
// We are subscribed to that message, call the callback
|
||||
subscriptionsForValue.forEach((subscription, index) => {
|
||||
subscription.callback(msg);
|
||||
|
||||
if (subscription.once) {
|
||||
// Called only once, we can remove it
|
||||
subscription = null;
|
||||
this.subscriptions[messageKey].splice(subsIndex[index], 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to a message using a key-value pair
|
||||
* @param {String} key Message key to subscribe to
|
||||
* @param {String} value Value that the above key must have for the callback to be called
|
||||
* @param {Function} callback callback(response)
|
||||
* @return {void}
|
||||
*/
|
||||
on(key, value, callback) {
|
||||
if (this.subscriptions[key]) {
|
||||
this.subscriptions[key].push({value, callback});
|
||||
return;
|
||||
}
|
||||
this.subscriptions[key] = [{value, callback}];
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as .on, but only triggers once
|
||||
* @param {String} key Message key to subscribe to
|
||||
* @param {String} value Value that the above key must have for the callback to be called
|
||||
* @param {Function} callback callback(response)
|
||||
* @return {void}
|
||||
*/
|
||||
once(key, value, callback) {
|
||||
const obj = {value, callback, once: true};
|
||||
if (this.subscriptions[key]) {
|
||||
this.subscriptions[key].push(obj);
|
||||
return;
|
||||
}
|
||||
this.subscriptions[key] = [obj];
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribes from a previously subscribed key-value pair (or key if no value)
|
||||
* @param {String} key Message key to unsubscribe
|
||||
* @param {String} value [Optional] Value of the key to unsubscribe
|
||||
* If there is no value, unsubscribes from all the values of that key
|
||||
* @return {void}
|
||||
*/
|
||||
unsubscribeTo(key, value) {
|
||||
if (!value) {
|
||||
this.subscriptions[key] = [];
|
||||
}
|
||||
if (this.subscriptions[key]) {
|
||||
this.subscriptions[key].filter((val, index) => {
|
||||
if (val.value === value) {
|
||||
this.subscriptions[key].splice(index, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribes from all subscriptions
|
||||
* @return {void}
|
||||
*/
|
||||
unsubscribeToAll() {
|
||||
this.subscriptions = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the child process. Same as ChildProcess.send()
|
||||
* @params {Object} message Message to send
|
||||
* For other parameters, see:
|
||||
* https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback
|
||||
* @return {void}
|
||||
*/
|
||||
send() {
|
||||
if (!this.process.connected) {
|
||||
return false;
|
||||
}
|
||||
return this.process.send(...arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the child process. It will exit on its own
|
||||
* @return {void}
|
||||
*/
|
||||
disconnect() {
|
||||
this.process.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Kills the child process
|
||||
* https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal
|
||||
* @return {void}
|
||||
*/
|
||||
kill() {
|
||||
this.process.kill(...arguments);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ProcessLauncher;
|
|
@ -1,34 +0,0 @@
|
|||
|
||||
class ProcessManager {
|
||||
constructor(options) {
|
||||
const self = this;
|
||||
this.logger = options.logger;
|
||||
this.events = options.events;
|
||||
this.plugins = options.plugins;
|
||||
this.processes = {};
|
||||
|
||||
self.events.setCommandHandler('processes:register', (name, cb) => {
|
||||
this.processes[name] = {
|
||||
state: 'unstarted',
|
||||
cb: cb
|
||||
};
|
||||
});
|
||||
|
||||
self.events.setCommandHandler('processes:launch', (name, cb) => {
|
||||
let process = self.processes[name];
|
||||
if (process.state !== 'unstarted') {
|
||||
return cb();
|
||||
}
|
||||
process.state = 'starting';
|
||||
process.cb.apply(process.cb, [
|
||||
() => {
|
||||
process.state = 'running';
|
||||
cb();
|
||||
}
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = ProcessManager;
|
|
@ -1,3 +1,7 @@
|
|||
process.on('uncaughtException', function(e){
|
||||
process.send({error: e.stack});
|
||||
});
|
||||
|
||||
const constants = require('../../constants');
|
||||
const Events = require('./eventsWrapper');
|
||||
|
||||
|
|
|
@ -1,93 +0,0 @@
|
|||
process.on('uncaughtException', function(e){
|
||||
process.send({error: e.stack});
|
||||
});
|
||||
|
||||
const constants = require('../../constants');
|
||||
const Events = require('./eventsWrapper');
|
||||
|
||||
// Set PWD to CWD since Windows doesn't have a value for PWD
|
||||
if (!process.env.PWD) {
|
||||
process.env.PWD = process.cwd();
|
||||
}
|
||||
|
||||
class ProcessWrapper {
|
||||
|
||||
/**
|
||||
* Class from which process extend. Should not be instantiated alone.
|
||||
* Manages the log interception so that all console.* get sent back to the parent process
|
||||
* Also creates an Events instance. To use it, just do `this.events.[on|request]`
|
||||
*
|
||||
* @param {Options} _options Nothing for now
|
||||
*/
|
||||
constructor(_options) {
|
||||
this.interceptLogs();
|
||||
this.events = new Events();
|
||||
|
||||
this.pingParent();
|
||||
}
|
||||
|
||||
// Ping parent to see if it is still alive. Otherwise, let's die
|
||||
pingParent() {
|
||||
const self = this;
|
||||
self.retries = 0;
|
||||
function error() {
|
||||
if (self.retries > 2) {
|
||||
self.kill();
|
||||
process.exit();
|
||||
}
|
||||
self.retries++;
|
||||
}
|
||||
setInterval(() => {
|
||||
try {
|
||||
let result = self.send({action: 'ping'});
|
||||
if (!result) {
|
||||
return error();
|
||||
}
|
||||
self.retries = 0;
|
||||
} catch (e) {
|
||||
error();
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
|
||||
interceptLogs() {
|
||||
const context = {};
|
||||
context.console = console;
|
||||
|
||||
context.console.log = this._log.bind(this, 'log');
|
||||
context.console.warn = this._log.bind(this, 'warn');
|
||||
context.console.error = this._log.bind(this, 'error');
|
||||
context.console.info = this._log.bind(this, 'info');
|
||||
context.console.debug = this._log.bind(this, 'debug');
|
||||
context.console.trace = this._log.bind(this, 'trace');
|
||||
context.console.dir = this._log.bind(this, 'dir');
|
||||
}
|
||||
|
||||
_log(type, ...messages) {
|
||||
const isHardSource = messages.some(message => {
|
||||
return (typeof message === 'string' && message.indexOf('hardsource') > -1);
|
||||
});
|
||||
if (isHardSource) {
|
||||
return;
|
||||
}
|
||||
this.send({result: constants.process.log, message: messages, type});
|
||||
}
|
||||
|
||||
send() {
|
||||
if (!process.connected) {
|
||||
return false;
|
||||
}
|
||||
return process.send(...arguments);
|
||||
}
|
||||
|
||||
kill() {
|
||||
// Should be implemented by derived class
|
||||
console.log('Process killed');
|
||||
}
|
||||
}
|
||||
|
||||
process.on('exit', () => {
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
module.exports = ProcessWrapper;
|
|
@ -1,49 +0,0 @@
|
|||
const repl = require("repl");
|
||||
const util = require("util");
|
||||
|
||||
const Console = require('./console.js');
|
||||
|
||||
class REPL {
|
||||
constructor(options) {
|
||||
this.env = options.env;
|
||||
this.plugins = options.plugins;
|
||||
this.events = options.events;
|
||||
this.console = new Console({
|
||||
events: this.events,
|
||||
plugins: this.plugins,
|
||||
version: options.version
|
||||
});
|
||||
}
|
||||
|
||||
enhancedEval(cmd, context, filename, callback) {
|
||||
this.console.executeCmd(cmd.trim(), (result) => {
|
||||
callback(null, result);
|
||||
});
|
||||
}
|
||||
|
||||
enhancedWriter(output) {
|
||||
if ((typeof output) === "string") {
|
||||
return output;
|
||||
} else {
|
||||
return util.inspect(output, {colors: true});
|
||||
}
|
||||
}
|
||||
|
||||
start(done) {
|
||||
this.replServer = repl.start({
|
||||
prompt: "Embark (" + this.env + ") > ",
|
||||
useGlobal: true,
|
||||
eval: this.enhancedEval.bind(this),
|
||||
writer: this.enhancedWriter.bind(this)
|
||||
});
|
||||
|
||||
this.replServer.on("exit", () => {
|
||||
process.exit();
|
||||
});
|
||||
|
||||
done();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = REPL;
|
|
@ -2,6 +2,8 @@ const Web3 = require('web3');
|
|||
const async = require('async');
|
||||
const Provider = require('./provider.js');
|
||||
const utils = require('../../utils/utils');
|
||||
const constants = require('../../constants');
|
||||
const embarkJsUtils = require('embarkjs').Utils;
|
||||
|
||||
const WEB3_READY = 'blockchain:ready';
|
||||
|
||||
|
@ -38,6 +40,7 @@ class BlockchainConnector {
|
|||
this.registerAPIRequests();
|
||||
this.registerWeb3Object();
|
||||
this.registerEvents();
|
||||
this.subscribeToPendingTransactions();
|
||||
}
|
||||
|
||||
initWeb3(cb) {
|
||||
|
|
|
@ -14,8 +14,6 @@ const Ipc = require('../../core/ipc');
|
|||
|
||||
const {defaultHost, dockerHostSwap} = require('../../utils/host');
|
||||
|
||||
const {defaultHost, dockerHostSwap} = require('../../utils/host');
|
||||
|
||||
/*eslint complexity: ["error", 36]*/
|
||||
var Blockchain = function(userConfig, clientClass) {
|
||||
this.userConfig = userConfig;
|
||||
|
|
|
@ -4,9 +4,6 @@ const ContractDeployer = require('./contract_deployer.js');
|
|||
const utils = require('../../utils/utils.js');
|
||||
//require("../utils/debug_util.js")(__filename, async);
|
||||
|
||||
const utils = require('../utils/utils.js');
|
||||
//require("../utils/debug_util.js")(__filename, async);
|
||||
|
||||
class DeployManager {
|
||||
constructor(options) {
|
||||
const self = this;
|
||||
|
|
Loading…
Reference in New Issue