refactor(@embark/utils): move timer and deconstructUrl helper functions

This commit is contained in:
Pascal Precht 2019-04-29 16:18:00 +02:00 committed by Pascal Precht
parent a75fa7941f
commit b9fe741585
5 changed files with 24 additions and 25 deletions

View File

@ -10,6 +10,13 @@ const toposortGraph = require('./toposort');
import { last, recursiveMerge } from './collections';
function timer(ms) {
const then = Date.now();
return new Promise(resolve => (
setTimeout(() => resolve(Date.now() - then), ms)
));
}
function checkIsAvailable(url, callback) {
const protocol = url.split(':')[0];
const httpObj = (protocol === 'https') ? https : http;
@ -106,6 +113,16 @@ function toposort(graph) {
return toposortGraph(graph);
}
function deconstructUrl(endpoint) {
const matches = endpoint.match(/(ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?/);
return {
protocol: matches[1],
host: matches[2],
port: matches[3],
type: matches[1] === 'ws' ? 'ws' : 'rpc'
};
}
const Utils = {
joinPath: function() {
const path = require('path');
@ -113,6 +130,7 @@ const Utils = {
},
canonicalHost,
copyToClipboard,
deconstructUrl,
defaultCorsHost,
defaultHost,
dockerHostSwap,
@ -126,6 +144,7 @@ const Utils = {
soliditySha3,
recursiveMerge,
sha512,
timer,
runCmd,
escapeHtml: logUtils.escapeHtml,
normalizeInput: logUtils.normalizeInput,

View File

@ -2,7 +2,7 @@
require('./httpProxyOverride');
const Asm = require('stream-json/Assembler');
import {canonicalHost} from 'embark-utils';
import {canonicalHost, timer} from 'embark-utils';
const constants = require('embark-core/constants');
const {Duplex} = require('stream');
const http = require('http');
@ -218,7 +218,7 @@ class Proxy {
if (!err || (Date.now() - start > 10000)) {
resolve();
} else {
utils.timer(250).then(waitOnTarget).then(resolve);
timer(250).then(waitOnTarget).then(resolve);
}
}
);

View File

@ -1,8 +1,7 @@
const async = require('async');
const Mocha = require('mocha');
const path = require('path');
const {timer} = require('../../utils/utils');
const { runCmd } = require('embark-utils');
const { runCmd, timer } = require('embark-utils');
const assert = require('assert');
const Test = require('./test');
const {EmbarkSpec, EmbarkApiSpec} = require('./reporter');

View File

@ -1,8 +1,8 @@
import * as utilsContractsConfig from "../../utils/contractsConfig";
import { deconstructUrl } from 'embark-utils';
const async = require('async');
const AccountParser = require('../../utils/accountParser');
const utils = require('../../utils/utils');
const constants = require('embark-core/constants');
const web3Utils = require('web3-utils');
@ -80,7 +80,7 @@ class Test {
} else if (this.simOptions.host || (this.options.node && this.options.node !== 'vm')) {
let options = this.simOptions;
if (this.options.node && this.options.node !== 'vm') {
options = utils.deconstructUrl(this.options.node);
options = deconstructUrl(this.options.node);
}
if (!options.protocol) {

View File

@ -342,16 +342,6 @@ function buildUrlFromConfig(configObj) {
return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port, configObj.type);
}
function deconstructUrl(endpoint) {
const matches = endpoint.match(/(ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?/);
return {
protocol: matches[1],
host: matches[2],
port: matches[3],
type: matches[1] === 'ws' ? 'ws' : 'rpc'
};
}
function getWeiBalanceFromString(balanceString, web3){
if(!web3){
throw new Error(__('[utils.getWeiBalanceFromString]: Missing parameter \'web3\''));
@ -436,13 +426,6 @@ function errorMessage(e) {
return e;
}
function timer(ms) {
const then = Date.now();
return new Promise(resolve => (
setTimeout(() => resolve(Date.now() - then), ms)
));
}
function isFolder(node) {
return node.children && node.children.length;
}
@ -532,14 +515,12 @@ module.exports = {
normalizeInput,
buildUrl,
buildUrlFromConfig,
deconstructUrl,
getWeiBalanceFromString,
getHexBalanceFromString,
compact,
groupBy,
interceptLogs,
errorMessage,
timer,
fileTreeSort,
fuzzySearch,
jsonFunctionReplacer,