ping endpoint before connecting to see if available

This commit is contained in:
Jonathan Rainville 2018-06-19 09:24:36 -04:00
parent aa02aeb74c
commit 1a91f3c688
2 changed files with 27 additions and 11 deletions

View File

@ -7,6 +7,7 @@ const Events = require('../core/events');
const cloneDeep = require('clone-deep');
const AccountParser = require('../contracts/accountParser');
const Provider = require('../contracts/provider');
const utils = require('../utils/utils');
const EmbarkJS = require('../../js/embark_node');
@ -44,18 +45,29 @@ class Test {
initWeb3Provider(callback) {
if (this.simOptions.host) {
const protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws';
let {host, port, type, protocol, accounts} = this.simOptions;
if (!protocol) {
protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws';
}
const endpoint = `${protocol}://${host}:${port}`;
const providerOptions = {
web3: this.web3,
type: this.simOptions.type,
accountsConfig: this.simOptions.accounts,
type,
accountsConfig: accounts,
blockchainConfig: this.engine.config.blockchainConfig,
logger: this.engine.logger,
isDev: false,
web3Endpoint: `${protocol}://${this.simOptions.host}:${this.simOptions.port}`
web3Endpoint: endpoint
};
this.provider = new Provider(providerOptions);
return this.provider.startWeb3Provider(callback);
console.info(`Connecting to node at ${endpoint}`.cyan);
return utils.pingEndpoint(host, port, type, protocol, this.engine.config.blockchainConfig.wsOrigins.split(',')[0], (err) => {
if (err) {
console.error(`Error connecting to the node, there might be an error in ${endpoint}`.red);
return callback(err);
}
this.provider = new Provider(providerOptions);
return this.provider.startWeb3Provider(callback);
});
}
if (this.simOptions.accounts) {
@ -214,7 +226,12 @@ class Test {
next(null, accounts);
});
}
], callback);
], (err, accounts) => {
if (err) {
process.exit(1);
}
callback(null, accounts);
});
}
_deploy(config, callback) {

View File

@ -73,8 +73,7 @@ function httpsGetJson(url, callback) {
});
}
function pingEndpoint(host, port, type, protocol, wsOrigins, callback) {
const origin = wsOrigins.split(',')[0];
function pingEndpoint(host, port, type, protocol, origin, callback) {
const options = {
protocolVersion: 13,
perMessageDeflate: true,
@ -93,10 +92,10 @@ function pingEndpoint(host, port, type, protocol, wsOrigins, callback) {
}
let req;
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
if(options.host.indexOf('/') > -1){
if (options.host.indexOf('/') > -1){
options.host = options.host.split('/')[0];
}
if(protocol === 'https') {
if (protocol === 'https') {
req = require('https').get(options);
} else {
req = require('http').get(options);