<commit_before>var config = require('../lib/config'),
    cors = require('../lib/cors'),
    fs = require('fs');

var paths = [],
    len = 0;

exports.init = function() {
  if (config.mocksEnabled) {
    console.log("Mock server enabled");
  }

  config.on('reload', function() {
    paths = [];

    if (config.projectConfig.mocks) {
      var pathSource = require(config.appDir + '/' + config.projectConfig.mocks);

      for (var name in pathSource) {
        paths.push({
          regex: new RegExp(name),
          source: pathSource[name]
        });
      }
    }

    len = paths.length;
  });
};

exports.middleware = function(req, res, next) {
  if (config.mocksEnabled) {
    for (var i = 0; i < len; i++) {
      var path = paths[i];
      if (path.regex.test(req.url)) {
        console.log('Mocking url', req.url, 'to', (typeof path.source !== 'function' ? 'file' + path.source : 'callback'));
        cors.applyFullCORS(req.headers.host, req, res);

        if (req.method !== 'HEAD') {
          req.method = 'GET';   // The static provider which this eventually uses does not like the options command, which breaks the mocking
        }
        if (typeof path.source === 'function') {
          res.send(path.source.apply(path.source, Array.prototype.slice.call(req.url.match(path.regex), 1)));
        } else {
          res.sendfile(config.appDir + '/' + config.projectConfig.mocks + '/' + path.source);
        }

        return;
      }
    }
  }

  next();
};<commit_msg>Allow mocks to define path via __dirname<commit_after>var config = require('../lib/config'),
    cors = require('../lib/cors'),
    fs = require('fs');

var paths = [],
    pathDir = '',
    len = 0;

exports.init = function() {
  if (config.mocksEnabled) {
    console.log("Mock server enabled");
  }

  config.on('reload', function() {
    paths = [];
    pathDir = '';

    if (config.projectConfig.mocks) {
      var pathSource = require(config.appDir + '/' + config.projectConfig.mocks);

      for (var name in pathSource) {
        if (name === '__dirname') {
          pathDir = pathSource[name];
        } else {
          paths.push({
            regex: new RegExp(name),
            source: pathSource[name]
          });
        }
      }
    }

    len = paths.length;
  });
};

exports.middleware = function(req, res, next) {
  if (config.mocksEnabled) {
    for (var i = 0; i < len; i++) {
      var path = paths[i];
      if (path.regex.test(req.url)) {
        console.log('Mocking url', req.url, 'to', (typeof path.source !== 'function' ? 'file' + path.source : 'callback'));
        cors.applyFullCORS(req.headers.host, req, res);

        if (req.method !== 'HEAD') {
          req.method = 'GET';   // The static provider which this eventually uses does not like the options command, which breaks the mocking
        }
        if (typeof path.source === 'function') {
          res.send(path.source.apply(path.source, Array.prototype.slice.call(req.url.match(path.regex), 1)));
        } else {
          var dir = pathDir || (config.appDir + '/' + config.projectConfig.mocks);
          res.sendfile(pathDir + '/' + path.source);
        }

        return;
      }
    }
  }

  next();
};
{'fraction_non_alphanumeric': 0.16625077978789768, 'fraction_numerical': 0.0018714909544603868, 'mean_word_length': 2.2492401215805473, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4280017', 'n_tokens_mistral': 1024, 'n_tokens_neox': 1001, 'n_words': 230}