<commit_before>/**************************************************************************************
 nexl-server
 Copyright (c) 2016 <NAME>
 License : Apache 2.0

 JavaScript based read only configuration data storage where every single data element is a javascript variable
 **************************************************************************************/

(function () {
	var CMD_LINE_OPTS_DEF = [
		{name: 'nexl-source', alias: "s", desc: "Points to nexl sources directory"},
		{name: 'debug', alias: "d", type: Boolean, defaultValue: false, desc: "Print debug message to console"},
		{name: 'port', alias: "p", type: Number, defaultValue: 8080, desc: "nexl server port"},
		{name: 'binding', alias: "b", type: String, defaultValue: 'localhost', desc: "nexl server binging"},
		{name: 'help', alias: "h", type: Boolean, defaultValue: false, desc: "display this help"}
	];

	// includes
	const nexlEngine = require('nexl-engine');
	const j79 = require('j79-utils');
	const path = require('path');
	const bodyParser = require('body-parser');
	const util = require('util');
	const express = require('express');
	const app = express();
	const osHomeDir = require('os-homedir');
	const favicon = require('serve-favicon');
	const figlet = require('figlet');
	const commandLineArgs = require('command-line-args');
	const chalk = require('chalk');
	const fs = require('fs');

	var NEXL_SOURCES_ROOT;
	const FILE_SEPARATOR = path.sep;

	const NEXL_REST_URL = '/nexl-rest';
	const REST_LIST_SOURCES = NEXL_REST_URL + '/list-nexl-sources';
	const REST_LIST_JS_VARIABLES = NEXL_REST_URL + '/list-js-variables';

	const HR = Array(55).join('-');

	var server;
	var cmdLineOpts;

	// C:\Program Files\nodejs\node.exe => C:\Program Files\nodejs
	function removeLastPathElement(path) {
		return path.replace(/[\\\/][^\\\/]*$/g, "");
	}

	function fixSlashes(path) {
		return path.replace(/[\\\/]/g, FILE_SEPARATOR);
	}

	function assemblePath() {
		// converting arguments to array
		var args = Array.prototype.slice.call(arguments);

		// joining array's elements
		var path = args.join(FILE_SEPARATOR);

		// removing double/triple/... slash characters
		return path.replace(/\/{2,}/, "/").replace(/\\{2,}/, "\\");
	}

	function printDebug(msg) {
		if (cmdLineOpts.debug) {
			console.log(util.format("DEBUG [%s] %s", new Date(), msg));
		}
	}

	function makeJsonResult(field, input, data) {
		var result = {};
		result[field] = data;
		return util.format('%s ( %s )', input.callback, JSON.stringify(result));
	}

	function sendJsonPResult(data, input, res) {
		var jsonPResult = makeJsonResult('data', input, data);
		res.write(jsonPResult);
	}

	function sendResult(result, input, res) {
		if (result === undefined) {
			sendException('Got undefined value', input, res);
			return;
		}

		// is jsonp ?
		if (isJsonP(input)) {
			sendJsonPResult(result, input, res);
			return;
		}

		if (j79.isString(result)) {
			res.write(result);
		} else {
			res.write(JSON.stringify(result));
		}
	}

	function isJsonP(input) {
		return input.callback !== undefined;
	}

	function sendException(err, input, res) {
		err = err.toString();

		if (!isJsonP(input)) {
			res.status(500).write(err);
			return;
		}

		var jsonPResult = makeJsonResult('error', input, err);
		res.write(jsonPResult);
	}<commit_msg>- added content-type in response on nexl-server;<commit_after>/**************************************************************************************
 nexl-server
 Copyright (c) 2016 <NAME>
 License : Apache 2.0

 JavaScript based read only configuration data storage where every single data element is a javascript variable
 **************************************************************************************/

(function () {
	var CMD_LINE_OPTS_DEF = [
		{name: 'nexl-source', alias: "s", desc: "Points to nexl sources directory"},
		{name: 'debug', alias: "d", type: Boolean, defaultValue: false, desc: "Print debug message to console"},
		{name: 'port', alias: "p", type: Number, defaultValue: 8080, desc: "nexl server port"},
		{name: 'binding', alias: "b", type: String, defaultValue: 'localhost', desc: "nexl server binging"},
		{name: 'help', alias: "h", type: Boolean, defaultValue: false, desc: "display this help"}
	];

	// includes
	const nexlEngine = require('nexl-engine');
	const j79 = require('j79-utils');
	const path = require('path');
	const bodyParser = require('body-parser');
	const util = require('util');
	const express = require('express');
	const app = express();
	const osHomeDir = require('os-homedir');
	const favicon = require('serve-favicon');
	const figlet = require('figlet');
	const commandLineArgs = require('command-line-args');
	const chalk = require('chalk');
	const fs = require('fs');

	var NEXL_SOURCES_ROOT;
	const FILE_SEPARATOR = path.sep;

	const NEXL_REST_URL = '/nexl-rest';
	const REST_LIST_SOURCES = NEXL_REST_URL + '/list-nexl-sources';
	const REST_LIST_JS_VARIABLES = NEXL_REST_URL + '/list-js-variables';

	const HR = Array(55).join('-');

	var server;
	var cmdLineOpts;

	// C:\Program Files\nodejs\node.exe => C:\Program Files\nodejs
	function removeLastPathElement(path) {
		return path.replace(/[\\\/][^\\\/]*$/g, "");
	}

	function fixSlashes(path) {
		return path.replace(/[\\\/]/g, FILE_SEPARATOR);
	}

	function assemblePath() {
		// converting arguments to array
		var args = Array.prototype.slice.call(arguments);

		// joining array's elements
		var path = args.join(FILE_SEPARATOR);

		// removing double/triple/... slash characters
		return path.replace(/\/{2,}/, "/").replace(/\\{2,}/, "\\");
	}

	function printDebug(msg) {
		if (cmdLineOpts.debug) {
			console.log(util.format("DEBUG [%s] %s", new Date(), msg));
		}
	}

	function makeJsonResult(field, input, data) {
		var result = {};
		result[field] = data;
		return util.format('%s ( %s )', input.callback, JSON.stringify(result));
	}

	function sendJsonPResult(data, input, res) {
		var jsonPResult = makeJsonResult('data', input, data);
		res.write(jsonPResult);
	}

	function sendResult(result, input, res) {
		if (result === undefined) {
			sendException('Got undefined value', input, res);
			return;
		}

		// is jsonp ?
		if (isJsonP(input)) {
			sendJsonPResult(result, input, res);
			return;
		}

		// setting up content-type
		if (j79.isArray(result) || j79.isObject(result)) {
			res.header("Content-Type", 'application/json');
		} else {
			res.header("Content-Type", 'text/plain');
		}

		// string sends as is. all other must be stringified
		if (j79.isString(result)) {
			res.write(result);
		} else {
			res.write(JSON.stringify(result));
		}
	}

	function isJsonP(input) {
		return input.callback !== undefined;
	}

	function sendException(err, input, res) {
		err = err.toString();

		if (!isJsonP(input)) {
			res.status(500).write(err);
			return;
		}

		var jsonPResult = makeJsonResult('error', input, err);
		res.write(jsonPResult);
	}
{'fraction_non_alphanumeric': 0.2085996513654852, 'fraction_numerical': 0.00726321905868681, 'mean_word_length': 4.37890625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3798259', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2205, 'n_words': 558}