@apica-io/url-xi
Version:
URL Check for integrations and API monitoring
218 lines • 9.71 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const log4js = __importStar(require("log4js"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const server = __importStar(require("../server/run"));
const cli = __importStar(require("./cli"));
const helpers = __importStar(require("../lib/helpers"));
const cliLogConfig = {
appenders: {
app: {
type: 'file',
filename: 'log/url-xi.log',
maxLogSize: 10485760,
numBackups: 3,
},
errorFile: {
type: 'file',
filename: 'log/url-xi-error.log',
},
errors: {
type: 'logLevelFilter',
level: 'ERROR',
appender: 'errorFile',
},
out: {
type: 'stdout',
},
},
categories: {
default: { appenders: ['app', 'errors', 'out'], level: 'INFO' },
},
};
const pack = helpers.getPackageInfo();
const version = pack.version || 'version unknown';
const runOptions = { testFile: '', package: pack };
const program = new commander_1.Command();
program.version(version);
program
.option('-f, --file <test_file>', 'The test configuration file')
.option('-r, --results <result_dir>', 'The result directory')
.option('-xh, --xheaders <headers>', 'extra headers', '{}')
.option('-i, --inputs [inputs...]', 'input variables. Format name=value format')
.option('-u, --url <url>', 'base url')
.option('-l, --log_level <log_level>', 'log level', 'info')
.option('-nd, --nodata', 'no response/request data in report', false)
.option('-m, --mask', 'Mask sensitive data from report', false)
.option('-hs, --hideSteps', 'Remove step data from report', false)
.option('-po, --parse_only', 'parse json only. No not run', false)
.option('-prod, --production', 'Production mode. Minimal logging and content in results', false)
.option('--no_keep_alive', 'No keep alive of http connections')
.option('-rn, --result_name <result_name>', 'name of the result')
.option('-s, --server', 'start as server')
.option('-p, --port <port>', 'server port', '8070')
.option('-mcl, --max_content_length <length>', 'Max request content length ')
.option('-tc, --time_calc <time_calc>', 'Custom request-time calculation', 'totalTime')
.option('-of, --out_format <out_format>', 'Output format in json result. CRS or Default', 'default')
.option('-proj --project <project>', 'Project should be a directory or a zip file')
.option('-ts, --table_server <table_server>', 'Apica Table Server (Experimental testing) ')
.option('-dk, --decryptKey <decryptKey>', 'Cryptify Decrypt key')
.option('--proxy <proxy>', 'Http(s) proxy', '');
//program.storeOptionsAsProperties();
program.parse(process.argv);
runOptions.serverPort = program.opts().port ? Number(program.opts().port) : -1;
runOptions.server = program.opts().server ? true : false;
runOptions.testFile = program.opts().test_file || '';
let log_level = program.opts().log_level || 'info';
log_level = log_level.toUpperCase();
switch (log_level) {
case 'DEBUG':
case 'TRACE':
runOptions.debug = true;
break;
case 'ERROR':
case 'FATAL':
break;
default:
log_level = 'INFO';
break;
}
if (program.opts().proxy) {
runOptions.proxy = program.opts().proxy;
if (/^(http|https):\/\/([\w\\.-]+)-?(:(\d+))?/.test(program.opts().proxy) == false) {
console.error('The proxy settings %s is invalid', program.opts().proxy);
process.exit(9);
}
}
if (!runOptions.server) {
runOptions.testFile = program.opts().file;
if (!runOptions.testFile || !runOptions.testFile.endsWith('.json')) {
console.error('-f is mandatory and must end with .json');
process.exit(9);
}
const tf_path = path_1.default.resolve(runOptions.testFile);
if (!fs_1.default.existsSync(tf_path) || !fs_1.default.lstatSync(tf_path).isFile()) {
console.error('The specified test file [%s] is not valid', runOptions.testFile);
process.exit(9);
}
runOptions.project = program.opts().project || '';
if (runOptions.project) {
const proj_path = path_1.default.resolve(runOptions.project);
if (!fs_1.default.existsSync(proj_path) || !(fs_1.default.lstatSync(proj_path).isDirectory() || proj_path.endsWith('.zip'))) {
console.error('The project directory [%s] is not a valid directory or zip file', proj_path);
process.exit(9);
}
}
runOptions.resultDir = program.opts().results || '';
if (runOptions.resultDir) {
const res_path = path_1.default.resolve(runOptions.resultDir);
if (!fs_1.default.existsSync(res_path) || !fs_1.default.lstatSync(res_path).isDirectory()) {
console.error('The result_dir [%s] is not a valid directory', runOptions.resultDir);
process.exit(9);
}
if (path_1.default.dirname(tf_path) === res_path) {
console.error('The result_dir [%s] and directory of test file [%s] must be different directories ', runOptions.resultDir, runOptions.testFile);
process.exit(9);
}
}
runOptions.resultName = program.opts().result_name || '';
if (!runOptions.resultName) {
const tf = path_1.default.parse(tf_path);
runOptions.resultName = tf.name + '_res';
}
if (program.opts().xheaders) {
runOptions.headers = JSON.parse(program.opts().xheaders);
}
runOptions.baseURL = program.opts().url;
runOptions.timeCalculation = program.opts().time_calc || '';
runOptions.decryptKey = program.opts().decryptKey || '';
runOptions.outputFormat = program.opts().out_format || '';
runOptions.tableServer = program.opts().table_server || '';
runOptions.inputs = program.opts().inputs || [];
runOptions.mask = program.opts().mask ? true : false;
runOptions.noData = program.opts().nodata ? true : false;
runOptions.hideSteps = program.opts().hideSteps ? true : false;
runOptions.parseOnly = program.opts().parse_only ? true : false;
runOptions.noKeepAlive = program.opts().no_keep_alive ? true : false;
runOptions.production = program.opts().production ? true : false;
runOptions.maxContentLength = program.opts().max_content_length ? Number(program.opts().max_content_length) : -1;
}
/*
Configure logging from parameters
*/
const logConfigFile = `${__dirname}/../../config/log4js.json`;
const node_env = process.env.NODE_ENV;
let productionMode = false;
if (runOptions.production || (node_env && node_env.toLowerCase().startsWith('production'))) {
runOptions.mask = true;
runOptions.noData = true;
productionMode = true;
}
if (runOptions.server) {
log4js.configure(logConfigFile);
}
else {
if (!runOptions.resultDir || productionMode) {
cliLogConfig.categories.default.appenders = ['out'];
}
else {
const applicationLog = path_1.default.resolve(runOptions.resultDir, 'log', runOptions.resultName + '.log');
const errorLog = path_1.default.resolve(runOptions.resultDir, 'log', runOptions.resultName + '_error.log');
cliLogConfig.appenders.app.filename = applicationLog;
cliLogConfig.appenders.errorFile.filename = errorLog;
}
cliLogConfig.categories.default.level = log_level;
log4js.configure(cliLogConfig);
}
const logger = log4js.getLogger('url-xi');
logger.debug('%s(%s) started with %s', pack.name, version, process.argv);
if (productionMode) {
logger.debug('%s started in production mode. Nodata and mask options will be set', pack.name);
}
if (runOptions.server) {
server.run(runOptions);
}
else {
run_cli();
}
function run_cli() {
return __awaiter(this, void 0, void 0, function* () {
const success = yield cli.run(logger, runOptions);
process.exit(success ? 0 : 1);
});
}
//# sourceMappingURL=index.js.map