2018-08-27 15:12:28 +00:00
|
|
|
const RunCode = require('./runCode.js');
|
|
|
|
const EmbarkJS = require('embarkjs');
|
2018-08-28 10:47:40 +00:00
|
|
|
const IpfsApi = require('ipfs-api');
|
|
|
|
const Web3 = require('web3');
|
|
|
|
|
2018-05-21 21:22:19 +00:00
|
|
|
class CodeRunner {
|
|
|
|
constructor(options) {
|
|
|
|
this.plugins = options.plugins;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.events = options.events;
|
2018-08-08 12:42:45 +00:00
|
|
|
this.ipc = options.ipc;
|
|
|
|
this.commands = [];
|
2018-08-24 13:05:19 +00:00
|
|
|
this.runCode = new RunCode();
|
2018-08-08 12:42:45 +00:00
|
|
|
let self = this;
|
2018-05-23 15:16:56 +00:00
|
|
|
|
2018-08-08 12:42:45 +00:00
|
|
|
if (this.ipc.isServer()) {
|
2018-08-10 13:22:45 +00:00
|
|
|
this.ipc.on('runcode:getCommands', (_err, callback) => {
|
2018-08-24 13:05:19 +00:00
|
|
|
let result = {web3Config: self.runCode.getWeb3Config(), commands: self.commands};
|
2018-08-08 12:42:45 +00:00
|
|
|
callback(null, result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2018-08-28 11:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.ipc.connected) {
|
2018-08-28 10:47:40 +00:00
|
|
|
this.runCode.registerVar('IpfsApi', IpfsApi);
|
|
|
|
this.runCode.registerVar('Web3', Web3);
|
2018-08-27 15:12:28 +00:00
|
|
|
this.runCode.registerVar('EmbarkJS', EmbarkJS);
|
|
|
|
this.events.on('code-generator-ready', () => {
|
|
|
|
this.events.request('code-generator:embarkjs:initialization-code', (code) => {
|
|
|
|
this.runCode.doEval(code);
|
|
|
|
});
|
2018-08-28 11:16:36 +00:00
|
|
|
});
|
2018-08-08 12:42:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 15:16:56 +00:00
|
|
|
this.events.on("runcode:register", (varName, code) => {
|
2018-08-08 12:42:45 +00:00
|
|
|
if (self.ipc.isServer() && varName !== 'web3') {
|
|
|
|
self.commands.push({varName, code});
|
|
|
|
self.ipc.broadcast("runcode:newCommand", {varName, code});
|
|
|
|
}
|
2018-08-24 13:05:19 +00:00
|
|
|
self.runCode.registerVar(varName, code);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.events.setCommandHandler('runcode:getContext', (cb) => {
|
|
|
|
cb(self.runCode.context);
|
2018-05-23 15:16:56 +00:00
|
|
|
});
|
2018-05-21 21:22:19 +00:00
|
|
|
|
2018-08-15 14:07:48 +00:00
|
|
|
this.events.setCommandHandler('runcode:eval', (code, cb, forConsoleOnly = false) => {
|
2018-06-14 15:10:31 +00:00
|
|
|
if (!cb) {
|
|
|
|
cb = function() {};
|
|
|
|
}
|
2018-08-22 18:09:10 +00:00
|
|
|
const awaitIdx = code.indexOf('await');
|
|
|
|
if (awaitIdx > -1) {
|
|
|
|
if (awaitIdx < 2) {
|
2018-08-22 18:23:23 +00:00
|
|
|
let end = code.length;
|
|
|
|
if (code[end - 1] === ';') {
|
|
|
|
end--; // Remove the `;` because we add function calls
|
|
|
|
}
|
2018-08-22 18:09:10 +00:00
|
|
|
code = code.substring(5, end); // remove await keyword
|
|
|
|
} else {
|
2018-08-22 18:23:23 +00:00
|
|
|
code = `(async function() {${code}})();`;
|
2018-08-22 18:09:10 +00:00
|
|
|
}
|
2018-08-22 15:32:15 +00:00
|
|
|
}
|
2018-08-24 13:05:19 +00:00
|
|
|
let result = self.runCode.doEval(code);
|
|
|
|
|
2018-08-15 14:07:48 +00:00
|
|
|
if (forConsoleOnly && self.ipc.isServer()) {
|
2018-08-08 12:42:45 +00:00
|
|
|
self.commands.push({code});
|
|
|
|
self.ipc.broadcast("runcode:newCommand", {code});
|
2018-05-23 15:16:56 +00:00
|
|
|
}
|
2018-08-24 13:05:19 +00:00
|
|
|
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
return result.then((value) => cb(null, value)).catch(cb);
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:42:45 +00:00
|
|
|
cb(null, result);
|
2018-05-23 15:16:56 +00:00
|
|
|
});
|
2018-05-21 21:22:19 +00:00
|
|
|
}
|
2018-08-08 12:42:45 +00:00
|
|
|
|
2018-05-21 21:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CodeRunner;
|