<commit_before>var RSVP = require('rsvp')
  , _ = require('lodash')
  , LinkingBooster = require('./linking-booster');

// constants
var MIME = {
  standard: ['application/vnd.api+json', 'application/json'],
  patch: ['application/json-patch+json']
},
    errorMessages = {
      400: 'Request was malformed.',
      403: 'Access forbidden.',
      404: 'Resource not found.',
      405: 'Method not permitted.',
      412: 'Request header "Content-Type" must be one of: ' + MIME.standard.join(', '),
      500: 'Oops, something went wrong.',
      501: 'Feature not implemented.'
    };

/**
 * Setup routes for a resource, given a name and model.
 *
 * @param {String} name
 * @param {Object} model
 * @param {Object} resources - a list with all registered resources
 * @param {Object} inflect
 * @param {Object} querytree
 */
function route(name, model, resources, inflect, querytree) {
  var _this = this
  , router = this.router
  , adapter = this.adapter
  , director = this.director
  , booster = LinkingBooster.init(this.director, inflect, resources)

  // options
  , baseUrl = this.options.baseUrl
  , namespace = this.options.namespace
  , suffix = this.options.suffix
  , production = this.options.production

  // routes
  , collection = inflect.pluralize(name)
  , collectionRoute = namespace + '/' + collection + suffix
  , individualRoute = namespace + '/' + collection + '/:id' + suffix;

  var options = this.options;


  this.director.registerResource(collection,{
    create: createResource,
    update: updateResource,
    replace: replaceResource,
    destroy: deleteResource,
    destroyAll: deleteResources,
    get: getResource,
    getAll: getResources
  });

  //Init linker-booster after director is set up

  // response emitters
  var sendError = function (req, res, status, error) {
    if (!!error) console.trace(error.stack || error);
    var object = {
      error: errorMessages[status],
      detail: error ? error.toString() : ''
    }, str = production ?
          JSON.stringify(object, null, null) :
          JSON.stringify(object, null, 2) + '\n';

    res.set('Content-Type', MIME.standard[1]);
    res.send(status, str);
  };
  var sendResponse = function (req, res, status, object) {
    if (status === 204) return res.send(status);

    object = object || {};
    var linked;
    if (req.linker){
      linked = appendLinks.call(_this, object, req, res).then(function(body){
        return booster.mergeResults(req, req.linker, body);
      });
    }else {
      linked = appendLinks.call(_this, object, req, res);
    }

    linked.then(function(object) {
      var str = production ?
            JSON.stringify(object, null, null) :
            JSON.stringify(object, null, 2) + '\n';

      res.set('Content-Type', !req.get('User-Agent') ? MIME.standard[0] : MIME.standard[1]);

      res.send(status, str);
    });
  };
  var methodNotAllowed = function (req, res) {
    sendError(req, res, 405);
  };

  var beforeReadHook = Hook('_before', 'read');
  var beforeWriteHook = Hook('_before', 'write');
  var afterReadHook = Hook('_after', 'read');
  var afterWriteHook = Hook('_after', 'write');

  function Hook(time, type){
    return function(model, resource, request, response, isNew){
      if (_.isUndefined(response) || _.isNull(response)) {
        response = request;<commit_msg>error catch in sendResponse to prevent silent deaths<commit_after>var RSVP = require('rsvp')
  , _ = require('lodash')
  , LinkingBooster = require('./linking-booster');

// constants
var MIME = {
  standard: ['application/vnd.api+json', 'application/json'],
  patch: ['application/json-patch+json']
},
    errorMessages = {
      400: 'Request was malformed.',
      403: 'Access forbidden.',
      404: 'Resource not found.',
      405: 'Method not permitted.',
      412: 'Request header "Content-Type" must be one of: ' + MIME.standard.join(', '),
      500: 'Oops, something went wrong.',
      501: 'Feature not implemented.'
    };

/**
 * Setup routes for a resource, given a name and model.
 *
 * @param {String} name
 * @param {Object} model
 * @param {Object} resources - a list with all registered resources
 * @param {Object} inflect
 * @param {Object} querytree
 */
function route(name, model, resources, inflect, querytree) {
  var _this = this
  , router = this.router
  , adapter = this.adapter
  , director = this.director
  , booster = LinkingBooster.init(this.director, inflect, resources)

  // options
  , baseUrl = this.options.baseUrl
  , namespace = this.options.namespace
  , suffix = this.options.suffix
  , production = this.options.production

  // routes
  , collection = inflect.pluralize(name)
  , collectionRoute = namespace + '/' + collection + suffix
  , individualRoute = namespace + '/' + collection + '/:id' + suffix;

  var options = this.options;


  this.director.registerResource(collection,{
    create: createResource,
    update: updateResource,
    replace: replaceResource,
    destroy: deleteResource,
    destroyAll: deleteResources,
    get: getResource,
    getAll: getResources
  });

  //Init linker-booster after director is set up

  // response emitters
  var sendError = function (req, res, status, error) {
    if (!!error) console.trace(error.stack || error);
    var object = {
      error: errorMessages[status],
      detail: error ? error.toString() : ''
    }, str = production ?
          JSON.stringify(object, null, null) :
          JSON.stringify(object, null, 2) + '\n';

    res.set('Content-Type', MIME.standard[1]);
    res.send(status, str);
  };
  var sendResponse = function (req, res, status, object) {
    if (status === 204) return res.send(status);

    object = object || {};
    var linked;
    if (req.linker){
      linked = appendLinks.call(_this, object, req, res).then(function(body){
        return booster.mergeResults(req, req.linker, body);
      });
    }else {
      linked = appendLinks.call(_this, object, req, res);
    }

    linked.then(function(object) {
      var str = production ?
            JSON.stringify(object, null, null) :
            JSON.stringify(object, null, 2) + '\n';

      res.set('Content-Type', !req.get('User-Agent') ? MIME.standard[0] : MIME.standard[1]);

      res.send(status, str);
    }).catch(function(err){ console.trace(err); throw err; });
  };
  var methodNotAllowed = function (req, res) {
    sendError(req, res, 405);
  };

  var beforeReadHook = Hook('_before', 'read');
  var beforeWriteHook = Hook('_before', 'write');
  var afterReadHook = Hook('_after', 'read');
  var afterWriteHook = Hook('_after', 'write');

  function Hook(time, type){
    return function(model, resource, request, response, isNew){
      if (_.isUndefined(response) || _.isNull(response)) {
        response = request;
{'fraction_non_alphanumeric': 0.14657210401891252, 'fraction_numerical': 0.009456264775413711, 'mean_word_length': 3.2043478260869565, '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': '4761937', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2015, 'n_words': 540}