<commit_before>var util = require('util');
var events = require('events');
var Context = require('./context');
var Registry = require('./registry');

module.exports = Router;

/**
 * The application router.
 *
 * This takes a route (in any supported format) and resolves
 * it to a particular chain of commands.
 */
function Router(registry) {
  this.registry = registry;
  this.resolver = null;
}

// Router generates events.
util.inherits(Router, events.EventEmitter);

/*
 * Set a new registry.
 * 
 * Use with caution.
 */
Router.prototype.setRegistry = function(registry) {
  this.registry = registry;
}

/**
 * Take the given request name and resolve it to a 
 * real request name.
 */
Router.prototype.resolveRequest = function(requestName) {
  
  if (this.resolver != null) {
    //console.log('RESOLVING ' + this.resolver.resolve(requestName));
    return this.resolver.resolve(requestName);
  }
  
  // Return just the request name for now.
  return requestName;
}

Router.prototype.setRequestResolver = function(resolver) {
  this.resolver = resolver;
}

/**
 * Given a request, perform the associated commands.
 *
 * Emits:
 * - error: Given an Error object. Emitted when request cannot be found, or when any part of the request fails.
 * - commandsStart:  Given request spec and context. Emitted immediately before the chain is run.
 * - commandsLaunched: Trigger immediately after a chain is kicked off. This may fire before the first command is complete.
 * - commandListComplete: Emitted when the entire chain has been executed.
 * - commandContinue: Emitted when the command is "done enough" for processing to continue. The next 
 *    command will start upon seeing this.
 * - commandComplete: Emitted when a command is totally complete. This may happen after other commands have begun
 *    executing.
 * - beforeCommand: A command emits this before it executes.
 * - beforeCommandInit: A command emits this before it runs its init() routine.
 * - commandListInterrupted
 * - reroute
 *
 * @param string request
 * @param pronto.Context cxt (optional)
 */
Router.prototype.handleRequest = function(request, cxt, tainted) {
  request = this.resolveRequest(request);
  
  var spec = this.registry.getRequestSpec(request);
  if (spec == undefined || spec == null) {
    var err = new Error('Request not found');
    this.emit('error', err, cxt);
    return;
  }
  
  if (!!tainted && request.length > 0 && request[0] == '@') {
    throw new Error('A tainted route cannot call an internal @-method.');
  }
  
  // BEGIN NEW<commit_msg>Pass Context to request resolver.<commit_after>var util = require('util');
var events = require('events');
var Context = require('./context');
var Registry = require('./registry');

module.exports = Router;

/**
 * The application router.
 *
 * This takes a route (in any supported format) and resolves
 * it to a particular chain of commands.
 */
function Router(registry) {
  this.registry = registry;
  this.resolver = null;
}

// Router generates events.
util.inherits(Router, events.EventEmitter);

/*
 * Set a new registry.
 * 
 * Use with caution.
 */
Router.prototype.setRegistry = function(registry) {
  this.registry = registry;
}

/**
 * Take the given request name and resolve it to a 
 * real request name.
 */
Router.prototype.resolveRequest = function(requestName, context) {
  
  if (this.resolver != null) {
    //console.log('RESOLVING ' + this.resolver.resolve(requestName));
    return this.resolver.resolve(requestName, context);
  }
  
  // Return just the request name for now.
  return requestName;
}

Router.prototype.setRequestResolver = function(resolver) {
  this.resolver = resolver;
}

/**
 * Given a request, perform the associated commands.
 *
 * Emits:
 * - error: Given an Error object. Emitted when request cannot be found, or when any part of the request fails.
 * - commandsStart:  Given request spec and context. Emitted immediately before the chain is run.
 * - commandsLaunched: Trigger immediately after a chain is kicked off. This may fire before the first command is complete.
 * - commandListComplete: Emitted when the entire chain has been executed.
 * - commandContinue: Emitted when the command is "done enough" for processing to continue. The next 
 *    command will start upon seeing this.
 * - commandComplete: Emitted when a command is totally complete. This may happen after other commands have begun
 *    executing.
 * - beforeCommand: A command emits this before it executes.
 * - beforeCommandInit: A command emits this before it runs its init() routine.
 * - commandListInterrupted
 * - reroute
 *
 * @param string request
 * @param pronto.Context cxt (optional)
 */
Router.prototype.handleRequest = function(request, cxt, tainted) {
  request = this.resolveRequest(request, cxt);
  
  var spec = this.registry.getRequestSpec(request);
  if (spec == undefined || spec == null) {
    var err = new Error('Request not found');
    this.emit('error', err, cxt);
    return;
  }
  
  if (!!tainted && request.length > 0 && request[0] == '@') {
    throw new Error('A tainted route cannot call an internal @-method.');
  }
  
  // BEGIN NEW
{'fraction_non_alphanumeric': 0.10927474236826755, 'fraction_numerical': 0.0007777561734396267, 'mean_word_length': 3.638412984670875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '860561', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1432, 'n_words': 538}