<commit_before>/**
 * @typedef {String} key
 */
/**
 * @typedef {String} html
 */

/**
 * @typedef {Object} File
 * @property {String} file
 */
/**
 * @typedef {Object} URL
 * @property {String} url
 */
/**
 * @typedef {Object} HTML
 * @property {String} html
 */

/**
 * @callback renderCallback
 * @param {Error|null} err
 * @param {String} html
 */


/** @type {Nightmare} */
var Nightmare = require("nightmare");
var fs = require('fs');
var os = require('os');
var path = require("path");

/**
 * @typedef {function(html)} grabHtml
 * @lends Nightmare
 */
/**
 * @desc defines grabHtml
 */
Nightmare.action('grabHtml', function (done) {
    this.evaluate_now(function () {
        return "<html>" + document.documentElement.innerHTML + "</html>";
    }, done)
});

/**
 * @typedef {function(html)} inject
 * @lends Nightmare
 */
/**
 * @desc defines inject
 */
Nightmare.action('inject', function (html, done) {
    this.evaluate_now(function (html) {
        return document.documentElement.innerHTML = html;
    }, done, html)
});

var getTempFilePath = function() {
    return path.join((os.tmpdir || os.tmpDir)(), Math.random().toString().replace('.', '') + '.html');
};

/**
 * Rendered HTML
 * @param {URL|File|HTML} options
 * @param {Cache} [options.cache=null]
 * @param {Number} [options.width=1280]
 * @param {Number} [options.height=720]
 * @param {renderCallback} callback
 */
var serverRender = function serverRender (options, callback) {
    "use strict";

    if (options.cache) {
        options.cache.get(options.url || options.file || options.html, function (err, html) {
            if (err) return callback(err);

            if (!html) return coldRender();

            return callback(null, html);
        });
    } else {
        return coldRender();
    }


    function coldRender () {
        var nightmare = Nightmare({
            show: false,
            width: options.width || 1280,
            height: options.height || 720
        });

        if (options.url) {
            nightmare = nightmare.goto(options.url);
            return _doRender();
        }
        if (options.file) {
            nightmare = nightmare.goto("file://" + path.resolve(options.file));
            return _doRender();
        }

        if (options.html) {
            var tempPath = getTempFilePath();
            return fs.writeFile(tempPath, options.html, function(err) {
                if(err){
                  return callback(err);
                }

                nightmare = nightmare.goto("file://" + tempPath);
                setTimeout(function(){
                    fs.unlink(tempPath, function(){});
                }, 1000);
                return _doRender();
            });
        }

        nightmare.end();
        return callback(module.exports.Errors.noInput);

        function _doRender() {
          nightmare
            .wait("html")
            .grabHtml()
            .then(
              function(html) {
                if (options.cache) options.cache.set(options.url || options.file || options.html, html, new Function);

                return callback(null, html);
              },
              function(err) {
                if (!(err instanceof Error)) err = new Error(err);
                return callback(err);
              }
            );
          nightmare.end();
        }
    }<commit_msg>Update: rendering callback awaits cache set and may throw if cache errors on set<commit_after>/**
 * @typedef {String} key
 */
/**
 * @typedef {String} html
 */

/**
 * @typedef {Object} File
 * @property {String} file
 */
/**
 * @typedef {Object} URL
 * @property {String} url
 */
/**
 * @typedef {Object} HTML
 * @property {String} html
 */

/**
 * @callback renderCallback
 * @param {Error|null} err
 * @param {String} html
 */


/** @type {Nightmare} */
var Nightmare = require("nightmare");
var fs = require('fs');
var os = require('os');
var path = require("path");

/**
 * @typedef {function(html)} grabHtml
 * @lends Nightmare
 */
/**
 * @desc defines grabHtml
 */
Nightmare.action('grabHtml', function (done) {
    this.evaluate_now(function () {
        return "<html>" + document.documentElement.innerHTML + "</html>";
    }, done)
});

/**
 * @typedef {function(html)} inject
 * @lends Nightmare
 */
/**
 * @desc defines inject
 */
Nightmare.action('inject', function (html, done) {
    this.evaluate_now(function (html) {
        return document.documentElement.innerHTML = html;
    }, done, html)
});

var getTempFilePath = function() {
    return path.join((os.tmpdir || os.tmpDir)(), Math.random().toString().replace('.', '') + '.html');
};

/**
 * Rendered HTML
 * @param {URL|File|HTML} options
 * @param {Cache} [options.cache=null]
 * @param {Number} [options.width=1280]
 * @param {Number} [options.height=720]
 * @param {renderCallback} callback
 */
var serverRender = function serverRender (options, callback) {
    "use strict";

    if (options.cache) {
        options.cache.get(options.url || options.file || options.html, function (err, html) {
            if (err) return callback(err);

            if (!html) return coldRender();

            return callback(null, html);
        });
    } else {
        return coldRender();
    }


    function coldRender () {
        var nightmare = Nightmare({
            show: false,
            width: options.width || 1280,
            height: options.height || 720
        });

        if (options.url) {
            nightmare = nightmare.goto(options.url);
            return _doRender();
        }
        if (options.file) {
            nightmare = nightmare.goto("file://" + path.resolve(options.file));
            return _doRender();
        }

        if (options.html) {
            var tempPath = getTempFilePath();
            return fs.writeFile(tempPath, options.html, function(err) {
                if(err){
                  return callback(err);
                }

                nightmare = nightmare.goto("file://" + tempPath);
                setTimeout(function(){
                    fs.unlink(tempPath, function(){});
                }, 1000);
                return _doRender();
            });
        }

        nightmare.end();
        return callback(module.exports.Errors.noInput);

        function _doRender() {
          nightmare
            .wait("html")
            .grabHtml()
            .then(
              function(html) {
                if (options.cache) {
                  return options.cache.set(options.url || options.file || options.html, html, function(err){
                    return callback(err, html);
                  });
                }

                return callback(null, html);
              },
              function(err) {
                if (!(err instanceof Error)) err = new Error(err);
                return callback(err);
              }
            );
          nightmare.end();
        }
    }
{'fraction_non_alphanumeric': 0.15309965237543452, 'fraction_numerical': 0.005214368482039398, 'mean_word_length': 1.9609777015437393, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1382932', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1886, 'n_words': 450}