<commit_before>// Generated by CoffeeScript 1.10.0
var Err, __checkAuth, __checkParams, __endpoints, __handle, __hasObjectValue, __mergeObjects, __methods, __options, bodyParser, compression, express, p;

express = require("express");

compression = require("compression");

bodyParser = require("body-parser");

Err = require("./error");

p = require("../package");

__options = {
  port: 8080,
  dataKey: "data",
  errorKey: "error",
  log: true,
  logStack: true,
  meta: true,
  requestLimit: "1mb",
  docs: {
    enabled: true,
    route: "/docs",
    auth: false
  },
  auth: function(req, res, next) {
    if (!req.headers["x-token"]) {
      return res.send401();
    }
    return next();
  }
};

__methods = ["get", "post", "put", "delete", "head"];

__endpoints = {};

__hasObjectValue = function(o, path) {
  var i, k, len, t, v;
  t = {};
  for (k in o) {
    v = o[k];
    t[k] = v;
  }
  for (i = 0, len = path.length; i < len; i++) {
    p = path[i];
    if (!t[p]) {
      return false;
    }
    t = t[p];
  }
  return true;
};

__checkParams = function(params, req, res, next) {
  var i, len;
  if (params.length === 0) {
    return next();
  }
  for (i = 0, len = params.length; i < len; i++) {
    p = params[i];
    if (!req.body[p]) {
      if (p.indexOf(".") < 0) {
        return next(new Err("Parameter '" + p + "' is missing", "missing_parameter"));
      }
      if (!__hasObjectValue(req.body, p.split("."))) {
        return next(new Err("Parameter '" + p + "' is missing", "missing_parameter"));
      }
    }
  }
  return next();
};

__checkAuth = function(req, res, requiredAuth, authMethod, cb) {
  if (!requiredAuth) {
    return cb();
  }
  return authMethod(req, res, cb);
};

__handle = function(app, options, method, route, requiredAuth, requiredParams, docs, callback) {
  if (typeof requiredAuth === "function") {
    callback = requiredAuth;
    requiredAuth = false;
    requiredParams = [];
    docs = null;
  } else if (typeof requiredParams === "function") {
    callback = requiredParams;
    requiredParams = [];
    docs = null;
  } else if (typeof docs === "function") {
    callback = docs;
    docs = null;
  }
  __endpoints[(method.toUpperCase()) + " " + route] = {
    docs: docs,
    required_params: requiredParams,
    required_auth: requiredAuth
  };
  return app[method](route, function(req, res, next) {
    return __checkAuth(req, res, requiredAuth, options.auth, function(err) {
      if (err) {
        return next(err);
      }
      return __checkParams(requiredParams, req, res, function(err) {
        if (err) {
          return next(err);
        }
        return callback(req, res, function(err, data) {
          if (err) {
            return next(err);
          }
          return res.sendData(data);
        });
      });
    });
  });
};

__mergeObjects = function(o1, o2) {
  var k, o, v;
  o = {};
  for (k in o2) {
    v = o2[k];
    if (o1[k] === void 0) {
      o[k] = v;
      continue;
    }
    if (typeof v !== "object") {
      o[k] = o1[k];
      continue;
    }
    o[k] = __mergeObjects(o1[k], v);
  }
  return o;
};

module.exports = function(options) {
  var app, cb, i, len, method, o, squirrel;
  if (options == null) {
    options = {};
  }
  o = __mergeObjects(options, __options);
  app = express();
  app.use(function(req, res, next) {
    var d;
    d = new Date;
    res.send404 = function(message) {
      if (message == null) {
        message = "Page not found";
      }
      res.status(404);
      return next(new Err(message, "page_not_found"));
    };
    res.send401 = function(message) {
      if (message == null) {
        message = "Unauthorized request";
      }
      res.status(401);
      return next(new Err(message, "unauthorized_request"));
    };
    res.sendData = function(data, key) {
      var body, error, r, took;
      if (key == null) {
        key = o.dataKey;
      }
      r = {};
      r[key] = data;
      body = req.body;
      if (JSON.stringify(body).length > 1024) {
        body = "Body too long";
      }
      took = Date.now() - d.getTime();
      if (o.meta) {
        r._meta = {
          took: took,
          endpoint: req.method + " " + req.path,
          body: body,
          "rs": {
            version: p.version,
            module: "https://www.npmjs.com/package/" + p.name
          },
          request: {
            endpoint: req.method + " " + req.path,
            body: body,<commit_msg>variable references in package version<commit_after>// Generated by CoffeeScript 1.10.0
var Err, __checkAuth, __checkParams, __endpoints, __handle, __hasObjectValue, __mergeObjects, __methods, __options, bodyParser, compression, express, pkg;

express = require("express");

compression = require("compression");

bodyParser = require("body-parser");

Err = require("./error");

pkg = require("../package");

__options = {
  port: 8080,
  dataKey: "data",
  errorKey: "error",
  log: true,
  logStack: true,
  meta: true,
  requestLimit: "1mb",
  docs: {
    enabled: true,
    route: "/docs",
    auth: false
  },
  auth: function(req, res, next) {
    if (!req.headers["x-token"]) {
      return res.send401();
    }
    return next();
  }
};

__methods = ["get", "post", "put", "delete", "head"];

__endpoints = {};

__hasObjectValue = function(o, path) {
  var i, k, len, p, t, v;
  t = {};
  for (k in o) {
    v = o[k];
    t[k] = v;
  }
  for (i = 0, len = path.length; i < len; i++) {
    p = path[i];
    if (!t[p]) {
      return false;
    }
    t = t[p];
  }
  return true;
};

__checkParams = function(params, req, res, next) {
  var i, len, p;
  if (params.length === 0) {
    return next();
  }
  for (i = 0, len = params.length; i < len; i++) {
    p = params[i];
    if (!req.body[p]) {
      if (p.indexOf(".") < 0) {
        return next(new Err("Parameter '" + p + "' is missing", "missing_parameter"));
      }
      if (!__hasObjectValue(req.body, p.split("."))) {
        return next(new Err("Parameter '" + p + "' is missing", "missing_parameter"));
      }
    }
  }
  return next();
};

__checkAuth = function(req, res, requiredAuth, authMethod, cb) {
  if (!requiredAuth) {
    return cb();
  }
  return authMethod(req, res, cb);
};

__handle = function(app, options, method, route, requiredAuth, requiredParams, docs, callback) {
  if (typeof requiredAuth === "function") {
    callback = requiredAuth;
    requiredAuth = false;
    requiredParams = [];
    docs = null;
  } else if (typeof requiredParams === "function") {
    callback = requiredParams;
    requiredParams = [];
    docs = null;
  } else if (typeof docs === "function") {
    callback = docs;
    docs = null;
  }
  __endpoints[(method.toUpperCase()) + " " + route] = {
    docs: docs,
    required_params: requiredParams,
    required_auth: requiredAuth
  };
  return app[method](route, function(req, res, next) {
    return __checkAuth(req, res, requiredAuth, options.auth, function(err) {
      if (err) {
        return next(err);
      }
      return __checkParams(requiredParams, req, res, function(err) {
        if (err) {
          return next(err);
        }
        return callback(req, res, function(err, data) {
          if (err) {
            return next(err);
          }
          return res.sendData(data);
        });
      });
    });
  });
};

__mergeObjects = function(o1, o2) {
  var k, o, v;
  o = {};
  for (k in o2) {
    v = o2[k];
    if (o1[k] === void 0) {
      o[k] = v;
      continue;
    }
    if (typeof v !== "object") {
      o[k] = o1[k];
      continue;
    }
    o[k] = __mergeObjects(o1[k], v);
  }
  return o;
};

module.exports = function(options) {
  var app, cb, i, len, method, o, squirrel;
  if (options == null) {
    options = {};
  }
  o = __mergeObjects(options, __options);
  app = express();
  app.use(function(req, res, next) {
    var d;
    d = new Date;
    res.send404 = function(message) {
      if (message == null) {
        message = "Page not found";
      }
      res.status(404);
      return next(new Err(message, "page_not_found"));
    };
    res.send401 = function(message) {
      if (message == null) {
        message = "Unauthorized request";
      }
      res.status(401);
      return next(new Err(message, "unauthorized_request"));
    };
    res.sendData = function(data, key) {
      var body, error, r, took;
      if (key == null) {
        key = o.dataKey;
      }
      r = {};
      r[key] = data;
      body = req.body;
      if (JSON.stringify(body).length > 1024) {
        body = "Body too long";
      }
      took = Date.now() - d.getTime();
      if (o.meta) {
        r._meta = {
          took: took,
          endpoint: req.method + " " + req.path,
          body: body,
          rs: {
            version: pkg.version,
            module: "https://www.npmjs.com/package/" + pkg.name
          },
          request: {
            endpoint: req.method + " " + req.path,
            body: body,
{'fraction_non_alphanumeric': 0.16668533661924498, 'fraction_numerical': 0.008961577237593816, 'mean_word_length': 2.285977180714023, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2519271', 'n_tokens_mistral': 3095, 'n_tokens_neox': 3017, 'n_words': 773}