<commit_before>var _ = require('lodash');
var express = require('express');

var runStack = require('./run-stack');

/**
 * initialize an api instance
 */
var HttpHandler = module.exports = function(api){

	function handler(req, res, next){
		return handler._router(req, res, next);
	}

	handler.__proto__ = HttpHandler;
	handler.api = api;
	handler._router = express.Router();

	handler._middlewareStacks = {};
	
	return handler;
};

HttpHandler.use = function(route, fn, priority){
	var self = this;

	if(typeof route === 'function'){
		priority = fn;
		fn = route;
		route = undefined;
	}

	if(!fn || typeof fn !== 'function'){
		throw new Error('expecting middleware function');
	}

	route = route || '';
	priority = priority || 0;

	var stack = self._middlewareStacks[route] || [];
	stack.push({priority:priority, route:route, fn: fn});
	self._middlewareStacks[route] = _.sortBy(stack, 'priority');
};

HttpHandler.mapCommand = function(operation, runCommand){
	var self = this;

	if(!operation.http || !operation.http.uri){
		return;
	}
	//translate the url template to express routepath by changing {paramName} to :paramName
	var routePath = operation.http.uri;
	_.forEach(routePath.match(/\{\w+\}/g), function(p){
		routePath = routePath.replace(p, p.replace('{', ':').replace('}', ''));
	});
	operation.http.routePath = routePath;

	self._router[operation.http.method](
		routePath,
		function(req, res, next){
			req.operation = operation;
			req.command = self._makeHttpCommand(req, res, operation);

			var stack = self._middlewareStacks[''] || [];
			stack = stack.concat(self._middlewareStacks[operation.name]||[]);

			runStack(stack, req, res, next);
		},
		function(req, res, next){
			var command = self._makeHttpCommand(req, res, operation);
			self.api.runCommand(command, function(err, response, metaResponse){
				if(err){
					err.errorId = err.errorId || 500;
					next(err);
				}

				metaResponse = metaResponse || {};
				metaResponse.http = metaResponse.http || {};

				var headers = metaResponse.http.headers ||{};
				var status = metaResponse.http.status || metaResponse.status || 200;

				var negotiation = _.extend({
					'default': function(){
						res.send(response);
					}
				}, _.mapValues(_.extend(req.negotiation || {}, metaResponse.http.negotiation||{}), function makeFormatter(f){
					return function(){
						if(typeof f === 'function'){<commit_msg>quick fix for headers sent after request<commit_after>var _ = require('lodash');
var express = require('express');

var runStack = require('./run-stack');

/**
 * initialize an api instance
 */
var HttpHandler = module.exports = function(api){

	function handler(req, res, next){
		return handler._router(req, res, next);
	}

	handler.__proto__ = HttpHandler;
	handler.api = api;
	handler._router = express.Router();

	handler._middlewareStacks = {};
	
	return handler;
};

HttpHandler.use = function(route, fn, priority){
	var self = this;

	if(typeof route === 'function'){
		priority = fn;
		fn = route;
		route = undefined;
	}

	if(!fn || typeof fn !== 'function'){
		throw new Error('expecting middleware function');
	}

	route = route || '';
	priority = priority || 0;

	var stack = self._middlewareStacks[route] || [];
	stack.push({priority:priority, route:route, fn: fn});
	self._middlewareStacks[route] = _.sortBy(stack, 'priority');
};

HttpHandler.mapCommand = function(operation, runCommand){
	var self = this;

	if(!operation.http || !operation.http.uri){
		return;
	}
	//translate the url template to express routepath by changing {paramName} to :paramName
	var routePath = operation.http.uri;
	_.forEach(routePath.match(/\{\w+\}/g), function(p){
		routePath = routePath.replace(p, p.replace('{', ':').replace('}', ''));
	});
	operation.http.routePath = routePath;

	self._router[operation.http.method](
		routePath,
		function(req, res, next){
			req.operation = operation;
			req.command = self._makeHttpCommand(req, res, operation);

			var stack = self._middlewareStacks[''] || [];
			stack = stack.concat(self._middlewareStacks[operation.name]||[]);

			runStack(stack, req, res, next);
		},
		function(req, res, next){
			var command = self._makeHttpCommand(req, res, operation);
			self.api.runCommand(command, function(err, response, metaResponse){
				if(err){
					err.errorId = err.errorId || 500;
					return next(err);
				}

				metaResponse = metaResponse || {};
				metaResponse.http = metaResponse.http || {};

				var headers = metaResponse.http.headers ||{};
				var status = metaResponse.http.status || metaResponse.status || 200;

				var negotiation = _.extend({
					'default': function(){
						res.send(response);
					}
				}, _.mapValues(_.extend(req.negotiation || {}, metaResponse.http.negotiation||{}), function makeFormatter(f){
					return function(){
						if(typeof f === 'function'){
{'fraction_non_alphanumeric': 0.16521739130434782, 'fraction_numerical': 0.002898550724637681, 'mean_word_length': 4.128450106157112, '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': '6556496', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1479, 'n_words': 344}