<commit_before>var gzip = require('zlib').gzip;
var fingerprint = require('./fingerprint');
var testSuffix = /\.[a-zA-Z0-9]+$/;

// the index client
var client;

// index document http headers
var resHeaders = {
    'Cache-Control': 'public, max-age=31536000',
    'Vary': 'Accept-Encoding',
    'Content-Type': 'text/html; charset=utf-8',
    'Content-Encoding': 'gzip',
    'Server': 'JCES'
};

// create file caches
var publicCache = engine.cache.file('public', {wd: engine.paths.repo + 'public/'});
var fileClient = engine.cache.file('client', {wd: engine.root + 'client/', noWrap: true});

// listen to core operations events
engine.on('client', getClient);
engine.on('script', getScript);

/**
 * Send the index document or the requeted public file.
 *
 * @private
 * @param {array} The url paht splitted into an array.
 * @param {object} The http request object.
 * @param {object} The http respone object.
 */
module.exports = function (pathname, req, res) {
    var self = this;

    // send client
    if (pathname === '/') {
        return sendClient(res, req._sidInvalid);
    }

    publicCache.set(pathname, function (err, file) {

        if (err) {

            // return not found if path has a suffix
            if (testSuffix.test(pathname)) {

                res.writeHead(404, {'Content-Type': 'text/plain; charset=utf-8'});
                res.end('File "' + pathname + '" not found.');

                return;
            }

            // send client
            return sendClient(res, req._sidInvalid);
        }

        res.writeHead(200, file.http);
        res.end(file.data);
    });
};

function sendClient (res, removeSid) {

    // remove sid
    if (removeSid) {
        resHeaders['Set-Cookie'] = 'sid=;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT';
    } else if (resHeaders['Set-Cookie']) {
        delete resHeaders['Set-Cookie'];
    }

    if (client) {
        res.writeHead(200, resHeaders);
        return res.end(client);
    }

    // get z path with fingerprint
    // TODO get fingerpints for all client files
    // TODO combine the client files in production mode
    fingerprint.getZ(function (err, z_path) {

        gzip(
            '<!DOCTYPE html><html><head>' +
                '<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">' +
                '<link rel="icon" href="/favicon.ico"/>' +
                '<script src="/@/0/client/main.js"></script>' +
                '<script src="/@/0/client/socket.js"></script>' +
                '<script src="/@/0/client/module.js"></script>' +
                '<script src="/@/0/client/router.js"></script>' +
                //'<script src="' + z_path + '"></script>' +
            '</head><body></body></html>',
            function (err, data) {

                client = data;
                resHeaders['Content-Length'] = client.length;

                resHeaders['Last-Modified'] = new Date().toString();

                res.writeHead(200, resHeaders);
                res.end(client);
            }
        );
    });
}<commit_msg>added the "client/link.js" to the client dependecies<commit_after>var gzip = require('zlib').gzip;
var fingerprint = require('./fingerprint');
var testSuffix = /\.[a-zA-Z0-9]+$/;

// the index client
var client;

// index document http headers
var resHeaders = {
    'Cache-Control': 'public, max-age=31536000',
    'Vary': 'Accept-Encoding',
    'Content-Type': 'text/html; charset=utf-8',
    'Content-Encoding': 'gzip',
    'Server': 'JCES'
};

// create file caches
var publicCache = engine.cache.file('public', {wd: engine.paths.repo + 'public/'});
var fileClient = engine.cache.file('client', {wd: engine.root + 'client/', noWrap: true});

// listen to core operations events
engine.on('client', getClient);
engine.on('script', getScript);

/**
 * Send the index document or the requeted public file.
 *
 * @private
 * @param {array} The url paht splitted into an array.
 * @param {object} The http request object.
 * @param {object} The http respone object.
 */
module.exports = function (pathname, req, res) {
    var self = this;

    // send client
    if (pathname === '/') {
        return sendClient(res, req._sidInvalid);
    }

    publicCache.set(pathname, function (err, file) {

        if (err) {

            // return not found if path has a suffix
            if (testSuffix.test(pathname)) {

                res.writeHead(404, {'Content-Type': 'text/plain; charset=utf-8'});
                res.end('File "' + pathname + '" not found.');

                return;
            }

            // send client
            return sendClient(res, req._sidInvalid);
        }

        res.writeHead(200, file.http);
        res.end(file.data);
    });
};

function sendClient (res, removeSid) {

    // remove sid
    if (removeSid) {
        resHeaders['Set-Cookie'] = 'sid=;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT';
    } else if (resHeaders['Set-Cookie']) {
        delete resHeaders['Set-Cookie'];
    }

    if (client) {
        res.writeHead(200, resHeaders);
        return res.end(client);
    }

    // get z path with fingerprint
    // TODO get fingerpints for all client files
    // TODO combine the client files in production mode
    fingerprint.getZ(function (err, z_path) {

        gzip(
            '<!DOCTYPE html><html><head>' +
                '<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">' +
                '<link rel="icon" href="/favicon.ico"/>' +
                '<script src="/@/0/client/main.js"></script>' +
                '<script src="/@/0/client/socket.js"></script>' +
                '<script src="/@/0/client/link.js"></script>' +
                '<script src="/@/0/client/module.js"></script>' +
                '<script src="/@/0/client/router.js"></script>' +
                //'<script src="' + z_path + '"></script>' +
            '</head><body></body></html>',
            function (err, data) {

                client = data;
                resHeaders['Content-Length'] = client.length;

                resHeaders['Last-Modified'] = new Date().toString();

                res.writeHead(200, resHeaders);
                res.end(client);
            }
        );
    });
}
{'fraction_non_alphanumeric': 0.16759156492785793, 'fraction_numerical': 0.014111304899318219, 'mean_word_length': 2.1858585858585857, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '174044', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1885, 'n_words': 456}