<commit_before>var fs = require('fs'),
    methods = require('methods'),
    Promise = require('bluebird'),
    utils = require('./utils');

var resourceDir;
var resourceStack = [];
var apiTimeout;
var prefix;

function productionMode () {
  return process.env.NODE_ENV === 'production';
}

function Handler (file, method, path, isCustom, funcName, config) {
  this.file = file;
  this.method = method;
  this.path = path;
  this.isCustom = isCustom;
  this.funcName = funcName;
  this.config = config;
  this.resources = resourceStack.slice();
}

Handler.prototype.func = function (direct) {
  var handler = this;
  var func = require(this.file)[this.funcName];
  if (func.length === 1) {
    func = func({});
  }
  func = Promise.method(func);
  return (this.func = function (req, res, next) {
    return utils.pTimeout(func(req, res), apiTimeout, 'API Request timed out')
    .then(function (result) {
      if (direct) return result;
      if (result) res.send(result);
    })
    .catch(function (err) {
      if (direct) return null;

      console.error(
        'Error thrown by ' + handler.method.toUpperCase() + ' ' + handler.path +
        ' : ' + ( err.message || JSON.stringify(err) )
      );

      /* Pass along the error to the client */
      var status = err.statusCode || 500;
      if (productionMode() && !err.public) {
        res.send(status, 'An error occurred');
      } else {
        res.send(status, err.message || err);
      }
    });
  });
};

function parsePath (file, action, method) {
  var pathArr = file.replace(resourceDir, '').split('/');
  pathArr.pop(); // Remove the script file from the path, will be replaced with the action
  pathArr.shift(); // Remove empty string at front

  var path = '';
  var isCustom = false;

  for (var i = 0; i < pathArr.length; i++) {
    path += pathArr[i];
    if (i < pathArr.length - 1) {
      path += '/:' + pathArr[i] + 'Id/';
    }
  }
  action = action.replace(method, '');
  if (method == 'post' && !action || method == 'get' && action == 'Index') {
    // Default action for post, and index action for get do not have an id
  } else if (!action) {
    // Default action for get, put, and delete
    path += '/:id';<commit_msg>Undo memoization<commit_after>var fs = require('fs'),
    methods = require('methods'),
    Promise = require('bluebird'),
    utils = require('./utils');

var resourceDir;
var resourceStack = [];
var apiTimeout;
var prefix;

function productionMode () {
  return process.env.NODE_ENV === 'production';
}

function Handler (file, method, path, isCustom, funcName, config) {
  this.file = file;
  this.method = method;
  this.path = path;
  this.isCustom = isCustom;
  this.funcName = funcName;
  this.config = config;
  this.resources = resourceStack.slice();
}

Handler.prototype.func = function (direct) {
  var handler = this;
  var func = require(this.file)[this.funcName];
  if (func.length === 1) {
    func = func({});
  }
  func = Promise.method(func);
  return function (req, res, next) {
    return utils.pTimeout(func(req, res), apiTimeout, 'API Request timed out')
    .then(function (result) {
      if (direct) return result;
      if (result) res.send(result);
    })
    .catch(function (err) {
      if (direct) return null;

      console.error(
        'Error thrown by ' + handler.method.toUpperCase() + ' ' + handler.path +
        ' : ' + ( err.message || JSON.stringify(err) )
      );

      /* Pass along the error to the client */
      var status = err.statusCode || 500;
      if (productionMode() && !err.public) {
        res.send(status, 'An error occurred');
      } else {
        res.send(status, err.message || err);
      }
    });
  };
};

function parsePath (file, action, method) {
  var pathArr = file.replace(resourceDir, '').split('/');
  pathArr.pop(); // Remove the script file from the path, will be replaced with the action
  pathArr.shift(); // Remove empty string at front

  var path = '';
  var isCustom = false;

  for (var i = 0; i < pathArr.length; i++) {
    path += pathArr[i];
    if (i < pathArr.length - 1) {
      path += '/:' + pathArr[i] + 'Id/';
    }
  }
  action = action.replace(method, '');
  if (method == 'post' && !action || method == 'get' && action == 'Index') {
    // Default action for post, and index action for get do not have an id
  } else if (!action) {
    // Default action for get, put, and delete
    path += '/:id';
{'fraction_non_alphanumeric': 0.1601270129281016, 'fraction_numerical': 0.0027217056021773644, 'mean_word_length': 2.858267716535433, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2810062', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1378, 'n_words': 409}