<commit_before>var url = require("url");
var scriptServingMiddleware = require("../script-serving-middleware");
var resource = require("./resource");
var scriptInjectionProcessor = require("./processors/script-injector");
var proxyMiddleware = require("../http-proxy");
var parser = require("uglify-js").parser;
var uglify = require("uglify-js").uglify;

module.exports = {
    contextPath: "",

    create: function (data) {
        var session = Object.create(this);

        session.resources = {};
        for (var key in data.resources) {
            session.resources[key] = resource.create(key, data.resources[key]);
        }

        session.load = data.load;
        session.rootResource = data.rootResource;
        session.resourceContextPath = data.contextPath + "/res";
        session.internalsContextPath = data.contextPath + "/_";

        if (!("/" in session.resources)) {
            session.resources["/"] = resource.create("/", {
                content: "<!DOCTYPE html><html><head></head><body></body></html>",
                headers: {"Content-Type": "text/html"}
            });
        }

        var p = Object.create(scriptInjectionProcessor);
        p.scripts = session.rootResourceScripts();
        session.resources["/"].addProcessor(p);

        return session;
    },

    /*
     * Takes the data for a session. Returns a string with an error message, or
     * nothing (undefined) if there was no error.
     */
    validate: function (data) {
        if (!data.hasOwnProperty("resources")) {
            return "Missing property 'resources'.";
        }

        if (!data.hasOwnProperty("load")) {
            return "Missing property 'load'.";
        }

        for (var i = 0, ii = data.load.length; i < ii; i++) {
            var resourceFound = false;
            for (var resource in data.resources) {
                if (data.load[i] == resource) resourceFound = true;
            }

            if (!resourceFound) {
                return "'load' entry '" + data.load[i] + "' missing corresponding 'resources' entry.";
            }
        }

        for (var resource in data.resources) {
            if ("content" in data.resources[resource]) {
                if (data.resources[resource].content instanceof Buffer) {
                    continue;
                }

                if (typeof(data.resources[resource].content) == "string") {
                    continue;
                } else {
                    return "The resource '" + resource + "' was not a string."
                }
            }
        }
    },

    respond: function (req, res) {
        if (this.scriptServingMiddleware.respond(req, res)) return true;

        var path, resource;

        for (path in this.resources) {
            resource = this.getResource(path);

            if (this.serveResourceContent(path, resource, req, res)) {
                return true;
            } else if (this.serveProxyRequest(path, resource, req, res)) {
                return true;
            }
        }
    },

    serveResourceContent: function (path, resource, req, res) {
        if (this.resourceContextPath + path != req.url) {
            return;
        }

        res.writeHead(200, resource.getHeaders());
        res.write(resource.getContent());
        res.end();
        return true;
    },

    serveProxyRequest: function (path, resource, req, res) {
        if (!resource.proxy ||
            req.url.indexOf(this.resourceContextPath + path) != 0) {
            return;
        }

        resource.proxy.respond(req, res);
        return true;
    },

    rootResourceScripts: function () {
        var scripts = [];

        for (var i = 0, ii = this.scriptServingMiddleware.scripts.length; i < ii; i++) {
            scripts.push(this.internalsContextPath + this.scriptServingMiddleware.scripts[i].path);
        }

        for (var i = 0, ii = this.load.length; i < ii; i++) {
            scripts.push(this.resourceContextPath + this.load[i]);
        }

        return scripts;
    },

    get scriptServingMiddleware() {
        return this._scriptServingMiddleware || this.createScriptServingMiddleware();
    },

    createScriptServingMiddleware: function () {
        var self = this;
        var middleware = Object.create(scriptServingMiddleware);
        middleware.contextPath = this.internalsContextPath;
        middleware.requireFile(require.resolve("buster-core"));
        middleware.requireFile(require.resolve("./../browser/cross-frame"));
        middleware.requireFile(require.resolve("./../browser/client-frame-load"));

        return this._scriptServingMiddleware = middleware;
    },

    getResource: function (path) {
        var resource = this.resources[path];

        if (resource.backend && !resource.proxy) {
            var parsed = url.parse(resource.backend);
            resource.proxy = proxyMiddleware.create(
                parsed.hostname, parsed.port, parsed.path);
            resource.proxy.proxyPath = this.resourceContextPath;
        }

        if (resource.combine && !resource.content) {
            resource.content = this.combineResources(resource.combine);
        }

        if (resource.minify) {
            resource.content = this.minify(resource.content);
        }

        return resource;
    },

    combineResources: function (resources) {
        var content = "";

        for (var i = 0, l = resources.length; i < l; ++i) {
            content += this.getResource(resources[i]).content + "\n";
        }

        return content;
    },

    minify: function (content) {
        var ast = parser.parse(content);
        ast = uglify.ast_mangle(ast);
        ast = uglify.ast_squeeze(ast);

        return uglify.gen_code(ast);
    }
};
<commit_msg>More descriptive variable name - it's intent is to serve internal scripts.<commit_after>var url = require("url");
var scriptServingMiddleware = require("../script-serving-middleware");
var resource = require("./resource");
var scriptInjectionProcessor = require("./processors/script-injector");
var proxyMiddleware = require("../http-proxy");
var parser = require("uglify-js").parser;
var uglify = require("uglify-js").uglify;

module.exports = {
    contextPath: "",

    create: function (data) {
        var session = Object.create(this);

        session.resources = {};
        for (var key in data.resources) {
            session.resources[key] = resource.create(key, data.resources[key]);
        }

        session.load = data.load;
        session.rootResource = data.rootResource;
        session.resourceContextPath = data.contextPath + "/res";
        session.internalsContextPath = data.contextPath + "/_";

        if (!("/" in session.resources)) {
            session.resources["/"] = resource.create("/", {
                content: "<!DOCTYPE html><html><head></head><body></body></html>",
                headers: {"Content-Type": "text/html"}
            });
        }

        var p = Object.create(scriptInjectionProcessor);
        p.scripts = session.rootResourceScripts();
        session.resources["/"].addProcessor(p);

        return session;
    },

    /*
     * Takes the data for a session. Returns a string with an error message, or
     * nothing (undefined) if there was no error.
     */
    validate: function (data) {
        if (!data.hasOwnProperty("resources")) {
            return "Missing property 'resources'.";
        }

        if (!data.hasOwnProperty("load")) {
            return "Missing property 'load'.";
        }

        for (var i = 0, ii = data.load.length; i < ii; i++) {
            var resourceFound = false;
            for (var resource in data.resources) {
                if (data.load[i] == resource) resourceFound = true;
            }

            if (!resourceFound) {
                return "'load' entry '" + data.load[i] + "' missing corresponding 'resources' entry.";
            }
        }

        for (var resource in data.resources) {
            if ("content" in data.resources[resource]) {
                if (data.resources[resource].content instanceof Buffer) {
                    continue;
                }

                if (typeof(data.resources[resource].content) == "string") {
                    continue;
                } else {
                    return "The resource '" + resource + "' was not a string."
                }
            }
        }
    },

    respond: function (req, res) {
        if (this.internalScriptServer.respond(req, res)) return true;

        var path, resource;

        for (path in this.resources) {
            resource = this.getResource(path);

            if (this.serveResourceContent(path, resource, req, res)) {
                return true;
            } else if (this.serveProxyRequest(path, resource, req, res)) {
                return true;
            }
        }
    },

    serveResourceContent: function (path, resource, req, res) {
        if (this.resourceContextPath + path != req.url) {
            return;
        }

        res.writeHead(200, resource.getHeaders());
        res.write(resource.getContent());
        res.end();
        return true;
    },

    serveProxyRequest: function (path, resource, req, res) {
        if (!resource.proxy ||
            req.url.indexOf(this.resourceContextPath + path) != 0) {
            return;
        }

        resource.proxy.respond(req, res);
        return true;
    },

    rootResourceScripts: function () {
        var scripts = [];

        for (var i = 0, ii = this.internalScriptServer.scripts.length; i < ii; i++) {
            scripts.push(this.internalsContextPath + this.internalScriptServer.scripts[i].path);
        }

        for (var i = 0, ii = this.load.length; i < ii; i++) {
            scripts.push(this.resourceContextPath + this.load[i]);
        }

        return scripts;
    },

    get internalScriptServer() {
        return this._internalScriptServer || this.createInternalScriptServer();
    },

    createInternalScriptServer: function () {
        var self = this;
        var middleware = Object.create(scriptServingMiddleware);
        middleware.contextPath = this.internalsContextPath;
        middleware.requireFile(require.resolve("buster-core"));
        middleware.requireFile(require.resolve("./../browser/cross-frame"));
        middleware.requireFile(require.resolve("./../browser/client-frame-load"));

        return this._internalScriptServer = middleware;
    },

    getResource: function (path) {
        var resource = this.resources[path];

        if (resource.backend && !resource.proxy) {
            var parsed = url.parse(resource.backend);
            resource.proxy = proxyMiddleware.create(
                parsed.hostname, parsed.port, parsed.path);
            resource.proxy.proxyPath = this.resourceContextPath;
        }

        if (resource.combine && !resource.content) {
            resource.content = this.combineResources(resource.combine);
        }

        if (resource.minify) {
            resource.content = this.minify(resource.content);
        }

        return resource;
    },

    combineResources: function (resources) {
        var content = "";

        for (var i = 0, l = resources.length; i < l; ++i) {
            content += this.getResource(resources[i]).content + "\n";
        }

        return content;
    },

    minify: function (content) {
        var ast = parser.parse(content);
        ast = uglify.ast_mangle(ast);
        ast = uglify.ast_squeeze(ast);

        return uglify.gen_code(ast);
    }
};

{'fraction_non_alphanumeric': 0.13002854918245524, 'fraction_numerical': 0.0013842027857081062, 'mean_word_length': 2.015127803860198, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 17, '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': '2619818', 'n_tokens_mistral': 3146, 'n_tokens_neox': 3044, 'n_words': 684}