<commit_before>
module.exports = function(opts) {
  'use strict';
  opts = opts || {};

  var j = require('path').join;

  var resMap = {};

  var cleanCacheOnRefresh = opts.cleanCacheOnRefresh || true
    , appBase = opts.appBase = j(process.cwd(), 'app')
    , mockBase = opts.mockBase || '/mock';

  return function(req, res, next) {
    if('/' === req.url && cleanCacheOnRefresh) {
      resMap = {};
    }

    if(req.url.indexOf(mockBase) !== 0) {
      // Pass through if this is not a request for mock data
      return next();
    }

    var fs = require('fs')
      , j = require('path').join
      , dir = j(appBase, req.url.replace(/[?#].*/,''));
    fs.readdir(dir, function(err, files) {
      if(err || !files.length) { return next(); }
      files.sort();
      var f, fIx;
      if(resMap[dir]) {
        fIx = files.indexOf(resMap[dir]);
        f = files[(fIx + 1) % files.length];
      } else {
        f = files[0];
      }
      resMap[dir] = f;
      fs.createReadStream(j(dir, f)).pipe(res);
    });
  };
};
<commit_msg>fix: Don't try to send folders, only files<commit_after>
module.exports = function(opts) {
  'use strict';
  opts = opts || {};

  var fs = require('fs')
    , j = require('path').join;

  var resMap = {};

  var cleanCacheOnRefresh = opts.cleanCacheOnRefresh || true
    , appBase = opts.appBase = j(process.cwd(), 'app')
    , mockBase = opts.mockBase || '/mock';

  var filesOnly = function(dir, paths, cb) {
    // Asynchronously filter down a bunch of paths to just files (i.e. no
    // directories)... we can't stream a directory :p
    var numToCheck = paths.length
      , checksMade = 0
      , files = [];

    if(!numToCheck) { return cb(null, files); }

    var done = function() {
      checksMade++;
      if(numToCheck === checksMade) {
        cb(null, files);
      }
    };

    paths.forEach(function(p) {
      var fullP = j(dir, p);
      fs.stat(fullP, function(err, stats) {
        if(!err && stats.isFile()) {
          files.push(p);
        }
        done();
      });
    });
  };

  return function(req, res, next) {
    if('/' === req.url && cleanCacheOnRefresh) {
      resMap = {};
    }

    if(req.url.indexOf(mockBase) !== 0) {
      // Pass through if this is not a request for mock data
      return next();
    }

    var dir = j(appBase, req.url.replace(/[?#].*/,''));

    fs.readdir(dir, function(err, paths) {
      if(err || !paths.length) { return next(); }
      filesOnly(dir, paths, function(error, files) {
        if(err || !files.length) { return next(); }
        files.sort();
        var f, fIx;
        if(resMap[dir]) {
          fIx = files.indexOf(resMap[dir]);
          f = files[(fIx + 1) % files.length];
        } else {
          f = files[0];
        }
        resMap[dir] = f;
        fs.createReadStream(j(dir, f)).pipe(res);
      });
    });
  };
};

{'fraction_non_alphanumeric': 0.17137096774193547, 'fraction_numerical': 0.0023521505376344087, 'mean_word_length': 1.9446092977250247, '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': '1899901', 'n_tokens_mistral': 1025, 'n_tokens_neox': 963, 'n_words': 225}