refactor(@embark/utils): move buildUrl and buildUrlFromConfig to utils

This commit is contained in:
Pascal Precht 2019-05-06 10:54:01 +02:00 committed by Pascal Precht
parent 0f2a8231e0
commit daaa0e649d
9 changed files with 57 additions and 55 deletions

View File

@ -180,7 +180,48 @@ function fuzzySearch(text, list, filter) {
return fuzzy.filter(text, list, {extract: (filter || function () {})});
}
/**
* Builds a URL
*
* @param {string} protocol
* The URL protocol, defaults to http.
* @param {string} host
* The URL host, required.
* @param {string} port
* The URL port, default to empty string.
* @param {string} [type]
* Type of connection
* @returns {string} the constructued URL, with defaults
*/
function buildUrl(protocol, host, port, type) {
if (!host) throw new Error('utils.buildUrl: parameter \'host\' is required');
if (port) port = ':' + port;
else port = '';
if (!protocol) {
protocol = type === 'ws' ? 'ws' : 'http';
}
return `${protocol}://${host}${port}`;
}
/**
* Builds a URL
*
* @param {object} configObj Object containing protocol, host, and port to be used to construct the url.
* * protocol {String} (optional) The URL protocol, defaults to http.
* * host {String} (required) The URL host.
* * port {String} (optional) The URL port, default to empty string.
* @returns {string} the constructued URL, with defaults
*/
function buildUrlFromConfig(configObj) {
if (!configObj) throw new Error('[utils.buildUrlFromConfig]: config object must cannot be null');
if (!configObj.host) throw new Error('[utils.buildUrlFromConfig]: object must contain a \'host\' property');
return buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port, configObj.type);
}
const Utils = {
buildUrl,
buildUrlFromConfig,
joinPath: function() {
const path = require('path');
return path.join.apply(path.join, arguments);

View File

@ -6,6 +6,7 @@ const deepEqual = require('deep-equal');
const web3 = require('web3');
const constants = require('embark-core/constants');
import {
buildUrlFromConfig,
canonicalHost,
defaultHost,
recursiveMerge,
@ -170,7 +171,7 @@ Config.prototype._updateBlockchainCors = function(){
}
if(webServerConfig && webServerConfig.host) {
corsParts.push(utils.buildUrlFromConfig(webServerConfig));
corsParts.push(buildUrlFromConfig(webServerConfig));
}
if(storageConfig && storageConfig.enabled) {
// if getUrl is specified in the config, that needs to be included in cors
@ -186,7 +187,7 @@ Config.prototype._updateBlockchainCors = function(){
}
// use our modified getUrl or in case it wasn't specified, use a built url
else{
corsParts.push(utils.buildUrlFromConfig(storageConfig.upload));
corsParts.push(buildUrlFromConfig(storageConfig.upload));
}
}
// Add cors for the proxy and whisper

View File

@ -2,11 +2,11 @@ const Web3 = require('web3');
const async = require('async');
const Provider = require('./provider.js');
const ethUtil = require('ethereumjs-util');
const utils = require('../../utils/utils');
const constants = require('embark-core/constants');
const embarkJsUtils = require('embarkjs').Utils;
const {bigNumberify} = require('ethers/utils/bignumber');
const RLP = require('ethers/utils/rlp');
import { buildUrl } from 'embark-utils';
const WEB3_READY = 'blockchain:ready';
@ -157,7 +157,7 @@ class BlockchainConnector {
protocol = (type === "rpc") ? protocol : 'ws';
this.web3Endpoint = utils.buildUrl(protocol, host, port);
this.web3Endpoint = buildUrl(protocol, host, port);
const providerOptions = {
web3: this.web3,

View File

@ -1,5 +1,5 @@
import { buildUrl } from 'embark-utils';
const Web3 = require('web3');
const {buildUrl} = require('../../utils/utils.js');
const constants = require('embark-core/constants');
class DevTxs {

View File

@ -1,6 +1,6 @@
const async = require('async');
const utils = require('../../utils/utils.js');
const {normalizeInput} = require('embark-utils');
const {normalizeInput, buildUrlFromConfig} = require('embark-utils');
const constants = require('embark-core/constants');
const BlockchainProcessLauncher = require('./blockchainProcessLauncher');
@ -36,7 +36,7 @@ class BlockchainModule {
if (!this.ipc.isServer()) return;
this.ipc.on('blockchain:node', (_message, cb) => {
cb(null, utils.buildUrlFromConfig(this.contractsConfig.deployment));
cb(null, buildUrlFromConfig(this.contractsConfig.deployment));
});
}

View File

@ -4,6 +4,7 @@ const IpfsApi = require('ipfs-api');
// TODO: not great, breaks module isolation
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
const constants = require('embark-core/constants');
import { buildUrlFromConfig } from 'embark-utils';
class IPFS {
@ -118,7 +119,7 @@ class IPFS {
}
_getNodeUrl() {
return utils.buildUrlFromConfig(this._getNodeUrlConfig()) + '/api/v0/version';
return buildUrlFromConfig(this._getNodeUrlConfig()) + '/api/v0/version';
}
_checkService(cb) {

View File

@ -1,6 +1,5 @@
const shellJs = require('shelljs');
const utils = require('../../utils/utils');
import {joinPath, canonicalHost} from 'embark-utils';
import {joinPath, canonicalHost, buildUrlFromConfig} from 'embark-utils';
const ProcessLauncher = require('../../core/processes/processLauncher');
const constants = require('embark-core/constants');
const cloneDeep = require('lodash.clonedeep');
@ -38,7 +37,7 @@ class StorageProcessesLauncher {
// add our webserver CORS
if(this.webServerConfig.enabled){
if (this.webServerConfig && this.webServerConfig.host) {
corsParts.push(utils.buildUrlFromConfig(this.webServerConfig));
corsParts.push(buildUrlFromConfig(this.webServerConfig));
}
else corsParts.push('http://localhost:8000');
}
@ -61,7 +60,7 @@ class StorageProcessesLauncher {
}
// in case getUrl wasn't specified, use a built url
else{
corsParts.push(utils.buildUrlFromConfig(dappConn));
corsParts.push(buildUrlFromConfig(dappConn));
}
}
});

View File

@ -1,10 +1,10 @@
const UploadSwarm = require('./upload.js');
const utils = require('../../utils/utils.js');
const SwarmAPI = require('swarm-api');
// TODO: not great, breaks module isolation
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
const constants = require('embark-core/constants');
require('colors');
import { buildUrl } from 'embark-utils';
class Swarm {
@ -39,7 +39,7 @@ class Swarm {
return this.events.emit("swarm:process:started", null, false);
}
this.providerUrl = utils.buildUrl(this.storageConfig.upload.protocol, this.storageConfig.upload.host, this.storageConfig.upload.port);
this.providerUrl = buildUrl(this.storageConfig.upload.protocol, this.storageConfig.upload.host, this.storageConfig.upload.port);
this.getUrl = this.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/';

View File

@ -1,6 +1,6 @@
let http = require('follow-redirects').http;
let https = require('follow-redirects').https;
import {canonicalHost, normalizeInput} from 'embark-utils';
import {normalizeInput} from 'embark-utils';
function dirname() {
const path = require('path');
@ -282,44 +282,6 @@ function isValidDomain(v) {
return isValid;
}
/**
* Builds a URL
*
* @param {string} protocol
* The URL protocol, defaults to http.
* @param {string} host
* The URL host, required.
* @param {string} port
* The URL port, default to empty string.
* @param {string} [type]
* Type of connection
* @returns {string} the constructued URL, with defaults
*/
function buildUrl(protocol, host, port, type) {
if (!host) throw new Error('utils.buildUrl: parameter \'host\' is required');
if (port) port = ':' + port;
else port = '';
if (!protocol) {
protocol = type === 'ws' ? 'ws' : 'http';
}
return `${protocol}://${host}${port}`;
}
/**
* Builds a URL
*
* @param {object} configObj Object containing protocol, host, and port to be used to construct the url.
* * protocol {String} (optional) The URL protocol, defaults to http.
* * host {String} (required) The URL host.
* * port {String} (optional) The URL port, default to empty string.
* @returns {string} the constructued URL, with defaults
*/
function buildUrlFromConfig(configObj) {
if (!configObj) throw new Error('[utils.buildUrlFromConfig]: config object must cannot be null');
if (!configObj.host) throw new Error('[utils.buildUrlFromConfig]: object must contain a \'host\' property');
return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port, configObj.type);
}
function compact(array) {
return array.filter(n => n);
}
@ -435,8 +397,6 @@ module.exports = {
extractZip,
getExternalContractUrl,
normalizeInput,
buildUrl,
buildUrlFromConfig,
compact,
groupBy,
interceptLogs,