<commit_before>/* istanbul ignore file */
// Istanbul coverage is disabled for this file,
// because it would mess up the functions sent to puppeteer

let puppeteer = require('puppeteer'),
    assert    = require('assert'),
    Hawkejs   = require('../index.js'),
    http      = require('http'),
    fs        = require('fs'),
    hawkejs;

let navigations = 0,
    do_coverage = !!global.__coverage__,
    coverage,
    browser,
    page;

global.do_coverage = do_coverage;

global.fetchCoverage = async function fetchCoverage() {

	if (!page) {
		return;
	}

	let temp = await page.evaluate(function getCoverage() {
		return window.__coverage__;
	});

	if (temp) {
		coverage = temp;
	}

	return coverage;
};

global.setLocation = async function setLocation(path) {

	let url;

	if (!page) {
		await loadBrowser();
	}

	if (navigations && do_coverage) {
		await fetchCoverage();
	}

	navigations++;

	if (path.indexOf('http') == -1) {
		url = 'http://127.0.0.1:' + port + path;
	} else {
		url = path;
	}

	await page.goto(url);

	if (coverage) {
		await page.evaluate(function setCoverage(coverage) {
			window.__coverage__ = coverage;
		}, coverage);
	}
};

global.evalPage = function evalPage(fnc) {
	return page.evaluate(fnc);
};

global.despace = function despace(text) {
	return text.trim().replace(/\n/g, ' ').replace(/\s\s+/g, ' ');
};

async function loadBrowser() {
	browser = await puppeteer.launch();
	page = await browser.newPage();
	global.browser = browser;

	page.on('console', function(msg) {
		var pieces = ['[BROWSER]'],
		    args = msg.args(),
		    args;

		for (arg of args) {
			let remote = arg._remoteObject;

			if (remote.type == 'string') {
				pieces.push(remote.value);
			} else if (remote.subtype == 'node') {
				pieces.push('\x1b[1m\x1b[36m<' + remote.description + '>\x1b[0m');
				//console.log(remote.preview);
			} else if (remote.className) {
				pieces.push('\x1b[1m\x1b[33m{' + remote.type + ' ' + remote.className + '}\x1b[0m');
			} else if (remote.value != null) {
				pieces.push(remote.value);
			} else {
				pieces.push(remote);
			}
		}

		console.log(...pieces);
	});

	hawkejs = new Hawkejs();
	hawkejs.addViewDirectory(__dirname + '/templates');

	await new Promise(function(resolve, reject) {
		server = http.createServer(function onReq(req, res) {

			var url = __Protoblast.Classes.RURL.parse(req.url),
			    renderer = hawkejs.createRenderer(),
			    templates;

			req.renderer = renderer;
			renderer.internal('url', url);

			if (url.pathname == '/hawkejs/hawkejs-client.js') {

				__Protoblast.getClientPath({
					modify_prototypes : true,
					ua                : req.headers.useragent,
					enable_coverage   : !!global.__coverage__,
				}).done(function gotClientFile(err, path) {

					if (err) {
						throw err;
					}

					res.writeHead(200, {'Content-Type': 'application/javascript'});

					fs.createReadStream(path).pipe(res);
				});

				return;
			}

			// Serve multiple template files
			if (url.pathname == '/hawkejs/templates') {

				var names = url.param('name');

				if (!names) {
					res.status = 500;
					return res.end('No template names have been given');
				}

				hawkejs.getFirstAvailableSource(names, function gotResult(err, result) {

					if (err) {
						res.status = 500;
						return res.end(String(err));<commit_msg>Try to fix getting browser coverage on travis-ci<commit_after>/* istanbul ignore file */
// Istanbul coverage is disabled for this file,
// because it would mess up the functions sent to puppeteer

let puppeteer = require('puppeteer'),
    assert    = require('assert'),
    Hawkejs   = require('../index.js'),
    http      = require('http'),
    fs        = require('fs'),
    hawkejs;

let navigations = 0,
    coverage,
    browser,
    page;

global.do_coverage = !!global.__coverage__;

global.fetchCoverage = async function fetchCoverage() {

	if (!page) {
		return;
	}

	let temp = await page.evaluate(function getCoverage() {
		return window.__coverage__;
	});

	if (temp) {
		coverage = temp;
	}

	return coverage;
};

global.setLocation = async function setLocation(path) {

	let url;

	if (!page) {
		await loadBrowser();
	}

	if (navigations && do_coverage) {
		await fetchCoverage();
	}

	navigations++;

	if (path.indexOf('http') == -1) {
		url = 'http://127.0.0.1:' + port + path;
	} else {
		url = path;
	}

	await page.goto(url);

	if (coverage) {
		await page.evaluate(function setCoverage(coverage) {
			window.__coverage__ = coverage;
		}, coverage);
	}
};

global.evalPage = function evalPage(fnc) {
	return page.evaluate(fnc);
};

global.despace = function despace(text) {
	return text.trim().replace(/\n/g, ' ').replace(/\s\s+/g, ' ');
};

async function loadBrowser() {
	browser = await puppeteer.launch();
	page = await browser.newPage();
	global.browser = browser;

	page.on('console', function(msg) {
		var pieces = ['[BROWSER]'],
		    args = msg.args(),
		    args;

		for (arg of args) {
			let remote = arg._remoteObject;

			if (remote.type == 'string') {
				pieces.push(remote.value);
			} else if (remote.subtype == 'node') {
				pieces.push('\x1b[1m\x1b[36m<' + remote.description + '>\x1b[0m');
				//console.log(remote.preview);
			} else if (remote.className) {
				pieces.push('\x1b[1m\x1b[33m{' + remote.type + ' ' + remote.className + '}\x1b[0m');
			} else if (remote.value != null) {
				pieces.push(remote.value);
			} else {
				pieces.push(remote);
			}
		}

		console.log(...pieces);
	});

	hawkejs = new Hawkejs();
	hawkejs.addViewDirectory(__dirname + '/templates');

	await new Promise(function(resolve, reject) {
		server = http.createServer(function onReq(req, res) {

			var url = __Protoblast.Classes.RURL.parse(req.url),
			    renderer = hawkejs.createRenderer(),
			    templates;

			req.renderer = renderer;
			renderer.internal('url', url);

			if (url.pathname == '/hawkejs/hawkejs-client.js') {

				__Protoblast.getClientPath({
					modify_prototypes : true,
					ua                : req.headers.useragent,
					enable_coverage   : do_coverage,
				}).done(function gotClientFile(err, path) {

					if (err) {
						throw err;
					}

					res.writeHead(200, {'Content-Type': 'application/javascript'});

					fs.createReadStream(path).pipe(res);
				});

				return;
			}

			// Serve multiple template files
			if (url.pathname == '/hawkejs/templates') {

				var names = url.param('name');

				if (!names) {
					res.status = 500;
					return res.end('No template names have been given');
				}

				hawkejs.getFirstAvailableSource(names, function gotResult(err, result) {

					if (err) {
						res.status = 500;
						return res.end(String(err));
{'fraction_non_alphanumeric': 0.1578313253012048, 'fraction_numerical': 0.009337349397590362, 'mean_word_length': 3.0469226081657528, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2173554', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2258, 'n_words': 482}