<commit_before>var fs = require('fs'),
    Path = require('path'),
    Buffer = require('buffer').Buffer,
    parseUrl = require('url').parse,
    queryString = require('querystring');

module.exports = function staticProvider(options){
    var maxAge, root;

        root = options.root;
        frm = options.frm;
        diffableRoot = options.diffableRoot;

    return function staticProvider(req, res, next) {
        if (req.method != 'GET' && req.method != 'HEAD') return next();

        var head = req.method == 'HEAD',
            filename, url = parseUrl(req.url);

        // Absolute path
        filename = Path.join(root, queryString.unescape(url.pathname));

        // Index.html support
        if (filename[filename.length - 1] === '/') {
            filename += "index.html";
        }
            
            fs.stat(filename, function (err, stat) {

                // Pass through for missing files, thow error for other problems
                if (err) {
                    if (filename.match(/\/diffable\/(.)*\.diff/gi)) {
                        var diffHash = filename.split('/').reverse()[0];
                        diffHash = diffHash.split('_');
                        fs.readFile(diffableRoot + '/' + diffHash[0] + '/' +
                            diffHash[1] + '_' + diffHash[2], onDiffRead)
                    } else if(filename.match(/\/diffable\/(.)*/gi)) {
                        var resHash = filename.split('/').reverse()[0],
                            verHash = frm.getVersionHash(resHash);
                        if (verHash) {
                            fs.readFile(diffableRoot + '/' + resHash + '/' + verHash + '.version', onJsRead);
                        } else {
                            return next();
                        }
                    } else {
                        return err.errno === process.ENOENT ? next() : next(err);
                    }
                } else if (stat.isDirectory()) {
                    return next();
                }
                
                function onDiffRead(err, diffData) {
                    if (err) {
                        return next();
                    }
                    fs.readFile(diffableRoot + '/DeltaBootstrap.js', function (err, data) {
                        if (err) {
                            throw err;
                        }
                        var script = data.toString();
                        script = script.replace('{{DJS_RESOURCE_IDENTIFIER}}', diffHash[0]);
                        script = script.replace('{{DJS_DIFF_CONTENT}}', diffData.toString());
                        
                        var headers = {
                            "Content-Type": 'text/javascript',
                            "Content-Length": script.length,
                            "Cache-Control": "public, max-age=63072000",
                            "Last-Modified": new Date(2000, 01, 01).toUTCString(),
                            "Expires": new Date().toUTCString()
                        };

                        res.writeHead(200, headers);
                        res.end(head ? undefined : script);
                    });
                }
                
                
                function onJsRead(err, versiondata) {
                    if (err) {
                        return next();
                    }
                    fs.readFile(diffableRoot + '/JsDictionaryBootstrap.js', function (err, data) {
                        if (err) {
                            throw err;
                        }
                        
                        var script = data.toString();
                        script = script.replace('{{DJS_RESOURCE_IDENTIFIER}}', resHash);
                        script = script.replace('{{DJS_CODE}}', JSON.stringify(versiondata.toString()));
                        script = script.replace('{{DJS_BOOTSTRAP_VERSION}}', verHash);
                        script = script.replace('{{DJS_DIFF_URL}}', '/diffable/' + resHash + '/');
                        var headers = {
                            "Content-Type": 'text/javascript',
                            "Content-Length": script.length,
                            "Cache-Control": "public, max-age=63072000",
                            "Last-Modified": new Date(2000, 01, 01).toUTCString(),
                            "Expires": new Date().toUTCString()
                        };

                        res.writeHead(200, headers);
                        res.end(head ? undefined : script);
                    });
                }
                
                
                function onHtmlRead (err, data) {
                    if (err) {
                        return next(err);
                    }
                    var strData = data.toString(), 
                        matches = strData.match(/\{\{DIFFABLE(.)*\}\}/gi), 
                        i, len, resource, script = "", counter = 0;<commit_msg>fixed paths to resources<commit_after>var fs = require('fs'),
    Path = require('path'),
    Buffer = require('buffer').Buffer,
    parseUrl = require('url').parse,
    queryString = require('querystring');

module.exports = function staticProvider(options){
    var maxAge, root;

        root = options.root;
        frm = options.frm;
        diffableRoot = options.diffableRoot;

    return function staticProvider(req, res, next) {
        if (req.method != 'GET' && req.method != 'HEAD') return next();

        var head = req.method == 'HEAD',
            filename, url = parseUrl(req.url);

        // Absolute path
        filename = Path.join(root, queryString.unescape(url.pathname));

        // Index.html support
        if (filename[filename.length - 1] === '/') {
            filename += "index.html";
        }
            
            fs.stat(filename, function (err, stat) {

                // Pass through for missing files, thow error for other problems
                if (err) {
                    if (filename.match(/\/diffable\/(.)*\.diff/gi)) {
                        var diffHash = filename.split('/').reverse()[0];
                        diffHash = diffHash.split('_');
                        fs.readFile(diffableRoot + '/' + diffHash[0] + '/' +
                            diffHash[1] + '_' + diffHash[2], onDiffRead)
                    } else if(filename.match(/\/diffable\/(.)*/gi)) {
                        var resHash = filename.split('/').reverse()[0],
                            verHash = frm.getVersionHash(resHash);
                        if (verHash) {
                            fs.readFile(diffableRoot + '/' + resHash + '/' + verHash + '.version', onJsRead);
                        } else {
                            return next();
                        }
                    } else {
                        return err.errno === process.ENOENT ? next() : next(err);
                    }
                } else if (stat.isDirectory()) {
                    return next();
                }
                
                function onDiffRead(err, diffData) {
                    if (err) {
                        return next();
                    }
                    fs.readFile(__dirname + '/resources/DeltaBootstrap.js', function (err, data) {
                        if (err) {
                            throw err;
                        }
                        var script = data.toString();
                        script = script.replace('{{DJS_RESOURCE_IDENTIFIER}}', diffHash[0]);
                        script = script.replace('{{DJS_DIFF_CONTENT}}', diffData.toString());
                        
                        var headers = {
                            "Content-Type": 'text/javascript',
                            "Content-Length": script.length,
                            "Cache-Control": "public, max-age=63072000",
                            "Last-Modified": new Date(2000, 01, 01).toUTCString(),
                            "Expires": new Date().toUTCString()
                        };

                        res.writeHead(200, headers);
                        res.end(head ? undefined : script);
                    });
                }
                
                
                function onJsRead(err, versiondata) {
                    if (err) {
                        return next();
                    }
                    fs.readFile(__dirname + '/resources/JsDictionaryBootstrap.js', function (err, data) {
                        if (err) {
                            throw err;
                        }
                        
                        var script = data.toString();
                        script = script.replace('{{DJS_RESOURCE_IDENTIFIER}}', resHash);
                        script = script.replace('{{DJS_CODE}}', JSON.stringify(versiondata.toString()));
                        script = script.replace('{{DJS_BOOTSTRAP_VERSION}}', verHash);
                        script = script.replace('{{DJS_DIFF_URL}}', '/diffable/' + resHash + '/');
                        var headers = {
                            "Content-Type": 'text/javascript',
                            "Content-Length": script.length,
                            "Cache-Control": "public, max-age=63072000",
                            "Last-Modified": new Date(2000, 01, 01).toUTCString(),
                            "Expires": new Date().toUTCString()
                        };

                        res.writeHead(200, headers);
                        res.end(head ? undefined : script);
                    });
                }
                
                
                function onHtmlRead (err, data) {
                    if (err) {
                        return next(err);
                    }
                    var strData = data.toString(), 
                        matches = strData.match(/\{\{DIFFABLE(.)*\}\}/gi), 
                        i, len, resource, script = "", counter = 0;
{'fraction_non_alphanumeric': 0.12242049646097099, 'fraction_numerical': 0.009171568138769813, 'mean_word_length': 1.0406834825061024, 'pattern_counts': {'":': 20, '<': 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': '7175232', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2280, 'n_words': 462}