<commit_before>var _ = require('lodash'),
    Environment = require('./Environment'),
    debugLib = require('debug'),
    debug = debugLib('PhantomEnvironment'),
    phantomError = debugLib('Phantom:error'),
    debugParser = debugLib('RedParser'),
    phantom = require('phantom'),
    path = require('path'),
    mkdir = require('mkdir-p'),
    url = require('url'),
    vowNode = require('vow-node'),
    vow = require('vow');

/**
 * @typedef {object} PhantomEnvironmentOptions
 */
var defaultOptions = {
    timeout: 60000,
    interval: 0,

    weak: true,
    loadImages: false,
    ignoreSslErrors: true,
    sslProtocol: 'any',
    proxy: null,
    proxyType: null,
    proxyAuth: null,
    cookiesFile: null,
    webSecurity: true,
    phantomPath: null,
    snapshot: false,

    reportsDir: 'parser_reports',
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12',
    screen: {
        width: 1080,
        height: 768
    }
};

var port = 13200;

/**
 * @param {PhantomEnvironmentOptions} options
 * @constructor
 */
function PhantomEnvironment(options) {
    debug('Initializing..');
    Environment.call(this, options);

    this._options = _.defaults(_.clone(options) || {}, defaultOptions);
    this._url = options.url;

    if (!this._url) {
        throw new Error('You must pass `url` to PhantomEnvironment');
    }

    this._phantomJS = null;
    this._page = null;
    this._navigationActions = [];
}

PhantomEnvironment.prototype = _.create(Environment.prototype, /**@lends PhantomEnvironment*/{
    prepare: function () {
        debug('Preparing..');
        return Environment.prototype.prepare
            .call(this)
            .then(this._setup, this)
            .then(this._navigateTo.bind(this, this._url))
            .then(this._handlePhantomEvents, this)
            .then(this._setViewport, this)
            .then(this._setUserAgent, this)
            .then(this._inject, this);
    },

    evaluateJs: function () {
        var deferred = vow.defer(),
            page = this._page,
            args = Array.prototype.slice.call(arguments, 0);

        var evalFunc = args.pop();
        if (typeof evalFunc !== 'function') {
            throw new Error('You must pass function as last argument to PhantomEnvironment.evaluateJs');
        }
        args.unshift(evalFunc, deferred.resolve.bind(deferred));

        page.evaluate.apply(page, args);
        return deferred.promise();
    },

    /**
     * Take screen snapshot
     * @param {string} fileName
     * @returns {Promise}
     */
    snapshot: function (fileName) {
        debug('.snapshot()');
        var options = this._options;
        if (!options.snapshot) {
            debug('skip snapshot');
            return vow.resolve();
        }

        var screenShotFilePath = path.join(options.reportsDir, this._getHostName(this._url));
        var screenShotFileName = path.join(screenShotFilePath, fileName + '.png');
        return vowNode
            .invoke(mkdir, screenShotFilePath)
            .then(function () {
                this._page.clipRect = {
                    left: 0,
                    top: 0,
                    width: options.screen.width,
                    height: options.screen.height
                };
                this._page.render(screenShotFileName);
            }, this);
    },

    waitForPage: function (timeout) {
        timeout = timeout || 5000;

        var deferred = vow.defer();
        this._navigationActions.push(function (err) {
            if (err) {
                deferred.reject(err);
            } else {
                deferred.resolve();
            }
        }.bind(this));

        setTimeout(function () {
            this._navigationActions = [];
            deferred.reject(new Error('Page navigation timeout'));
        }.bind(this), timeout);
<commit_msg>Fixed cleaning interval when promise is resolved<commit_after>var _ = require('lodash'),
    Environment = require('./Environment'),
    debugLib = require('debug'),
    debug = debugLib('PhantomEnvironment'),
    phantomError = debugLib('Phantom:error'),
    debugParser = debugLib('RedParser'),
    phantom = require('phantom'),
    path = require('path'),
    mkdir = require('mkdir-p'),
    url = require('url'),
    vowNode = require('vow-node'),
    vow = require('vow');

/**
 * @typedef {object} PhantomEnvironmentOptions
 */
var defaultOptions = {
    timeout: 60000,
    interval: 0,

    weak: true,
    loadImages: false,
    ignoreSslErrors: true,
    sslProtocol: 'any',
    proxy: null,
    proxyType: null,
    proxyAuth: null,
    cookiesFile: null,
    webSecurity: true,
    phantomPath: null,
    snapshot: false,

    reportsDir: 'parser_reports',
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12',
    screen: {
        width: 1080,
        height: 768
    }
};

var port = 13200;

/**
 * @param {PhantomEnvironmentOptions} options
 * @constructor
 */
function PhantomEnvironment(options) {
    debug('Initializing..');
    Environment.call(this, options);

    this._options = _.defaults(_.clone(options) || {}, defaultOptions);
    this._url = options.url;

    if (!this._url) {
        throw new Error('You must pass `url` to PhantomEnvironment');
    }

    this._phantomJS = null;
    this._page = null;
    this._navigationActions = [];
}

PhantomEnvironment.prototype = _.create(Environment.prototype, /**@lends PhantomEnvironment*/{
    prepare: function () {
        debug('Preparing..');
        return Environment.prototype.prepare
            .call(this)
            .then(this._setup, this)
            .then(this._setViewport, this)
            .then(this._setUserAgent, this)
            .then(this._navigateTo.bind(this, this._url))
            .then(this._handlePhantomEvents, this)
            .then(this._inject, this);
    },

    evaluateJs: function () {
        var deferred = vow.defer(),
            page = this._page,
            args = Array.prototype.slice.call(arguments, 0);

        var evalFunc = args.pop();
        if (typeof evalFunc !== 'function') {
            throw new Error('You must pass function as last argument to PhantomEnvironment.evaluateJs');
        }
        args.unshift(evalFunc, deferred.resolve.bind(deferred));

        page.evaluate.apply(page, args);
        return deferred.promise();
    },

    /**
     * Take screen snapshot
     * @param {string} fileName
     * @returns {Promise}
     */
    snapshot: function (fileName) {
        debug('.snapshot()');
        var options = this._options;
        if (!options.snapshot) {
            debug('skip snapshot');
            return vow.resolve();
        }

        var screenShotFilePath = path.join(options.reportsDir, this._getHostName(this._url));
        var screenShotFileName = path.join(screenShotFilePath, fileName + '.png');
        return vowNode
            .invoke(mkdir, screenShotFilePath)
            .then(function () {
                this._page.clipRect = {
                    left: 0,
                    top: 0,
                    width: options.screen.width,
                    height: options.screen.height
                };
                this._page.render(screenShotFileName);
            }, this);
    },

    waitForPage: function (timeout) {
        timeout = timeout || 5000;

        var deferred = vow.defer();
        var timeoutId = setTimeout(function () {
            debug('Timeout %s has reached on page load', timeout);
            this._navigationActions = [];
            deferred.reject(new Error('Page navigation timeout'));
        }.bind(this), timeout);

        this._navigationActions.push(function (err) {
            clearTimeout(timeoutId);
            if (err) {
                deferred.reject(err);
            } else {
                deferred.resolve();
            }
        }.bind(this));
        debug('Added page load callback');

{'fraction_non_alphanumeric': 0.1301966641772467, 'fraction_numerical': 0.011700273836196167, 'mean_word_length': 2.1696252465483234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7287788', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2206, 'n_words': 491}