<commit_before>var protein = require('protein');
var router = require('router');

var METHODS       = 'all get post put head del delete options'.split(' ');
var MIDDLEWARE    = 'json query log body'.split(' ');
var PROXY_PROTEIN = 'fn getter setter'.split(' ');
var SSL_OPTIONS   = 'cert key'.split(' ');

var noop = function() {};
var defunc = function(fn) {
	return {request:fn.request, response:fn.response};
};
var once = function(fn) {
	var ran = false;

	return function(a,b) {
		if (ran) return;
		ran = true;
		fn(a,b);
	};
};
var reduce = function(args, middleware) {
	var fns = [];

	args = Array.prototype.concat.apply([], args).reduce(function(result, arg) {
		(typeof arg === 'function' ? fns : result).push(arg);
		return result;
	}, []);

	if (!fns.length) return args;

	var fn = fns.length === 1 ? fns.pop() : protein().use(fns);

	args.push(!middleware ? fn : function(req, res, next) {
		middleware(req, res, function(err) {
			if (err) return next(err);
			fn(req, res, next);
		});
	});

	return args;
};

var Branch = function(middleware) {
	this.middleware = protein(middleware);
};
var Root = function() {
	var self = this;

	this.servers = [];
	this.middleware = protein();
	this.route = router();
	this.route.head(function(req, res, next) { // experimental
		var end = res.end;

		req.method = 'GET';
		res.write = res.end = function() {
			res.write = res.end = function() {};
			end.call(res);
		};	
		self.route(req, res, next);
	});
	this.route.onmount = once(function() {
		if (self.using(self.route)) return;
		self.use(self.route);
	});
	this.on('request', function(req, res) {
		self.emit('middleware');
		self.middleware(req, res);
	});
};

Root.prototype.__proto__ = Branch.prototype.__proto__ = process.EventEmitter.prototype;
Root.prototype.error = function(fn) { // experimental
	var self = this;

	this.once('middleware', function() {
		self.use(function(err, req, res, next) {
			if (!err) return next();
			fn(err, req, res, next);
		});
	});
	return this;
};
Root.prototype.fork = function() {
	var self = this;
	var boot = new Root();
	var route = function(req, res) {
		boot.emit('request', req, res);
	};
	
	Array.prototype.concat.apply([], arguments).forEach(function(pattern) {
		var fn = typeof pattern === 'function' && pattern;

		if (!fn) {
			pattern = pattern.match(/^(?:(?:http|https):\/\/)?([^\/:]*)(?:\:\d+)?(.*)$/) || [];

			var host = new RegExp('^'+(pattern[1] || '*').replace(/\./g, '\\.').replace(/^\*$/, '(.+)?').replace(/\*/g, '[^.]+')+'(:|$)', 'i');<commit_msg>fork accepts a root instance to fork to<commit_after>var protein = require('protein');
var router = require('router');

var METHODS       = 'all get post put head del delete options'.split(' ');
var MIDDLEWARE    = 'json query log body'.split(' ');
var PROXY_PROTEIN = 'fn getter setter'.split(' ');
var SSL_OPTIONS   = 'cert key'.split(' ');

var noop = function() {};
var defunc = function(fn) {
	return {request:fn.request, response:fn.response};
};
var once = function(fn) {
	var ran = false;

	return function(a,b) {
		if (ran) return;
		ran = true;
		fn(a,b);
	};
};
var reduce = function(args, middleware) {
	var fns = [];

	args = Array.prototype.concat.apply([], args).reduce(function(result, arg) {
		(typeof arg === 'function' ? fns : result).push(arg);
		return result;
	}, []);

	if (!fns.length) return args;

	var fn = fns.length === 1 ? fns.pop() : protein().use(fns);

	args.push(!middleware ? fn : function(req, res, next) {
		middleware(req, res, function(err) {
			if (err) return next(err);
			fn(req, res, next);
		});
	});

	return args;
};

var Branch = function(middleware) {
	this.middleware = protein(middleware);
};
var Root = function() {
	var self = this;

	this.servers = [];
	this.middleware = protein();
	this.route = router();
	this.route.head(function(req, res, next) { // experimental
		var end = res.end;

		req.method = 'GET';
		res.write = res.end = function() {
			res.write = res.end = function() {};
			end.call(res);
		};	
		self.route(req, res, next);
	});
	this.route.onmount = once(function() {
		if (self.using(self.route)) return;
		self.use(self.route);
	});
	this.on('request', function(req, res) {
		self.emit('middleware');
		self.middleware(req, res);
	});
};

Root.prototype.__proto__ = Branch.prototype.__proto__ = process.EventEmitter.prototype;
Root.prototype.error = function(fn) { // experimental
	var self = this;

	this.once('middleware', function() {
		self.use(function(err, req, res, next) {
			if (!err) return next();
			fn(err, req, res, next);
		});
	});
	return this;
};
Root.prototype.fork = function() {
	var self = this;
	var args = Array.prototype.concat.apply([], arguments);
	var boot = typeof args[args.length-1] === 'object' ? args.pop() : new Root();
	var route = function(req, res) {
		boot.emit('request', req, res);
	};
	
	args.forEach(function(pattern) {
		var fn = typeof pattern === 'function' && pattern;

		if (!fn) {
			pattern = pattern.match(/^(?:(?:http|https):\/\/)?([^\/:]*)(?:\:\d+)?(.*)$/) || [];

			var host = new RegExp('^'+(pattern[1] || '*').replace(/\./g, '\\.').replace(/^\*$/, '(.+)?').replace(/\*/g, '[^.]+')+'(:|$)', 'i');
{'fraction_non_alphanumeric': 0.2181253638657093, 'fraction_numerical': 0.0009703085581214826, 'mean_word_length': 3.8123249299719886, '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': '150710', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1831, 'n_words': 425}