refactor(@embark-utils): move logHandler and normalizeInput in utils

This commit is contained in:
Jonathan Rainville 2019-04-25 12:20:46 -04:00 committed by Pascal Precht
parent 483f692e0b
commit 83b0a68eef
7 changed files with 53 additions and 48 deletions

View File

@ -1,12 +0,0 @@
function escapeHtml(message) {
if(typeof message !== "string") return message;
return message
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\"/g, "&quot;")
.replace(/\'/g, "&#39;");
}
module.exports = escapeHtml;

View File

@ -3,6 +3,7 @@ const https = require('follow-redirects').https;
const {canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker} = require('./host');
const {findNextPort} = require('./network');
const logUtils = require('./log-utils');
function checkIsAvailable(url, callback) {
const protocol = url.split(':')[0];
@ -69,7 +70,9 @@ const Utils = {
soliditySha3,
recursiveMerge,
sha512,
escapeHtml: require('./escapeHtml')
escapeHtml: logUtils.escapeHtml,
normalizeInput: logUtils.normalizeInput,
LogHandler: require('./logHandler')
};
module.exports = Utils;

View File

@ -0,0 +1,41 @@
function logUtils(message) {
if(typeof message !== "string") return message;
return message
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\"/g, "&quot;")
.replace(/\'/g, "&#39;");
}
function normalizeInput(input) {
if(typeof input === 'string') return input;
let args = Object.values(input);
if (args.length === 0) {
return "";
}
if (args.length === 1) {
if (Array.isArray(args[0])) {
return args[0].join(',');
}
return args[0] || "";
}
return ('[' + args.map((x) => {
if (x === null) {
return "null";
}
if (x === undefined) {
return "undefined";
}
if (Array.isArray(x)) {
return x.join(',');
}
return x;
}).toString() + ']');
}
module.exports = {
escapeHtml: logUtils,
normalizeInput
};

View File

@ -1,9 +1,8 @@
const utils = require('./utils');
const normalizeInput = require('./log-utils').normalizeInput;
// define max number of logs to keep in memory for this process
// to prevent runaway memory leak
const MAX_LOGS = require('../constants').logs.maxLogLength;
const MAX_LOGS = 1500; // TODO use constants when it's put in a package or something
/**
* Serves as a central point of log handling.
@ -91,9 +90,9 @@ class LogHandler {
return;
}
if (this.logger[msg.type]) {
return this.logger[msg.type](utils.normalizeInput(message));
return this.logger[msg.type](normalizeInput(message));
}
this.logger.debug(utils.normalizeInput(message));
this.logger.debug(normalizeInput(message));
});
}
}

View File

@ -1,5 +1,6 @@
const async = require('async');
const utils = require('../../utils/utils.js');
const {normalizeInput} = require('embark-utils');
const constants = require('../../constants');
const BlockchainProcessLauncher = require('./blockchainProcessLauncher');
@ -103,7 +104,7 @@ class BlockchainModule {
this.blockchainProcess = new BlockchainProcessLauncher({
events: self.events,
logger: self.logger,
normalizeInput: utils.normalizeInput,
normalizeInput,
blockchainConfig: self.blockchainConfig,
locale: self.locale,
client: self.client,

View File

@ -1,5 +1,4 @@
const LogHandler = require('../../utils/logHandler');
const {escapeHtml} = require('embark-utils');
const {escapeHtml, LogHandler} = require('embark-utils');
class ProcessLogsApi {
constructor({embark, processName, silent}) {

View File

@ -1,7 +1,7 @@
let http = require('follow-redirects').http;
let https = require('follow-redirects').https;
let toposortGraph = require('./toposort.js');
import {canonicalHost} from 'embark-utils';
import {canonicalHost, normalizeInput} from 'embark-utils';
const balanceRegex = /([0-9]+) ?([a-zA-Z]*)/;
@ -351,32 +351,6 @@ function sha3(arg) {
return Web3.utils.sha3(arg);
}
function normalizeInput(input) {
if(typeof input === 'string') return input;
let args = Object.values(input);
if (args.length === 0) {
return "";
}
if (args.length === 1) {
if (Array.isArray(args[0])) {
return args[0].join(',');
}
return args[0] || "";
}
return ('[' + args.map((x) => {
if (x === null) {
return "null";
}
if (x === undefined) {
return "undefined";
}
if (Array.isArray(x)) {
return x.join(',');
}
return x;
}).toString() + ']');
}
/**
* Builds a URL
*