<commit_before>/**
 * Expose the sessionHandler function.
 */
module.exports = sessionHandler;


/**
 * Dependencies.
 */
var debug = require('debug')('protoo:sessionHandler');
// var debugerror = require('debug')('protoo:ERROR:sessionHandler');
var randomString = require('random-string');
var sender = require('./sender');


function sessionHandler(app, options) {
	options = options || {};

	// Create a Router to be returned by the function.
	var router = app.Router(),
		path = options.path || '/:username/:uuid';

	/**
	 * Initial "session" request handler.
	 */
	router.route(path)
		.session(sender(), function(req) {
			var targetUsername = req.params.username,
				targetUuid = req.params.uuid,
				sessionId = randomString({length: 16}),
				sessionPath,
				found;

			debug('target peer: [username:%s, uuid:%s]', targetUsername, targetUuid);

			sessionPath = req.basePath + '/sessions/' + sessionId;

			req.data.sessionPath = sessionPath;

			var found = app.peers(targetUsername, targetUuid, function(peer) {
				debug('sending session request to %s', peer);

				peer.send(req);
			});

			if (! found) {
				req.reply(404, 'peer not found');
			}

			req.reply(100, 'connecting', {
				sessionPath: sessionPath
			});
		});

	/**
	 * "insession" request handler.
	 */
	router.route('/sessions/:sessionId')
		.insession(sender(), function(req) {
			debug('insession request for sessionId "%s": %s', req.params.sessionId, req);
		});

	return router;
}<commit_msg>Fix doble initialization.<commit_after>/**
 * Expose the sessionHandler function.
 */
module.exports = sessionHandler;


/**
 * Dependencies.
 */
var debug = require('debug')('protoo:sessionHandler');
// var debugerror = require('debug')('protoo:ERROR:sessionHandler');
var randomString = require('random-string');
var sender = require('./sender');


function sessionHandler(app, options) {
	options = options || {};

	// Create a Router to be returned by the function.
	var router = app.Router(),
		path = options.path || '/:username/:uuid';

	/**
	 * Initial "session" request handler.
	 */
	router.route(path)
		.session(sender(), function(req) {
			var targetUsername = req.params.username,
				targetUuid = req.params.uuid,
				sessionId = randomString({length: 16}),
				sessionPath,
				found;

			// TODO: Must ensure that both username and uuid are given. No forking for sessions.

			debug('target peer: [username:%s, uuid:%s]', targetUsername, targetUuid);

			sessionPath = req.basePath + '/sessions/' + sessionId;

			req.data.sessionPath = sessionPath;

			found = app.peers(targetUsername, targetUuid, function(peer) {
				debug('sending session request to %s', peer);

				peer.send(req);
			});

			if (found) {
				req.reply(100, 'connecting', {
					sessionPath: sessionPath
				});
			}
			else {
				req.reply(404, 'peer not found');
			}
		});

	/**
	 * "insession" request handler.
	 */
	router.route('/sessions/:sessionId')
		.insession(sender(), function(req) {
			debug('insession request for sessionId "%s": %s', req.params.sessionId, req);
		});

	return router;
}
{'fraction_non_alphanumeric': 0.16243489583333334, 'fraction_numerical': 0.005208333333333333, 'mean_word_length': 3.727692307692308, 'pattern_counts': {'":': 2, '<': 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': '7127846', 'n_tokens_mistral': 1088, 'n_tokens_neox': 934, 'n_words': 230}