<commit_before>module.exports = function (sails) {

	
	/**
	 * Module dependencies.
	 */

	var util		= require('sails-util'),
		ResStream	= require('./ResStream'),
		getVerb		= require('./getVerb'),
		saveSessionAndThen = require('./saveSessionAndThen'),
		getSDKMetadata = require('../getSDKMetadata'),
		Qs			= require ('querystring');


	/**
	 * Module errors
	 */
	var Err = {
		invalidRequestCallback: function (invalidFn) {
			return new Error(
				'Invalid callback specified in socket request :: ' +
				util.inspect(invalidFn)
			);
		},
		invalidRedirect: function (location) {
			return new Error(
				'\n' +
				'res.redirect() :: [socket.io] ::' +
				'\n' +
				'Cannot redirect socket to invalid location :: ' + 
				util.inspect(location)
			);
		}
	};


	/**
	 * Interpret an incoming socket.io "request"
	 * Emulates Express semantics by mocking request (`req`) and response (`res`)
	 * 
	 * @param  {[type]}   socketReq        [incoming sails.io-formatted socket msg]
	 * @param  {[type]}   socketIOCallback [an ack callback useful for sending a response back to the client]
	 * @param  {[type]}   socket           [the socket that originated this request]
	 * @param  {[type]}   messageName      [the name of the message]
	 * @param  {Function} cb               [called when request interpretation is complete]
	 */
	
	return function interpretSocketReq (socketReq, socketIOCallback, socket, messageName, cb) {

		var msg;

		// If invalid callback function specified, freak out
		if (socketIOCallback && !util.isFunction(socketIOCallback)) {
			msg = 'Invalid socket request Could not be parse :: '+socketReq;
			return sails.log.error(msg);
		}

		// Parse request as JSON (or just use the object if we have one)
		if (! util.isObject(socketReq) && util.isString(socketReq) ) {
			try {
				socketReq = JSON.parse(socketReq);
			} catch (e) {
				msg = 'Invalid socket request! The following JSON could not be parsed :: '+socketReq;
				return emitError(msg);
			}
		}

		// If no URL specified, error out
		if (!socketReq.url) {
			msg = 'No url provided in request: '+socketReq;
			return emitError(msg);
		}

		if (!util.isString(socketReq.url)) {
			msg = 'Invalid url provided in request: ' + socketReq.url;
			return emitError(msg);
		}


		// Grab the metadata for the SDK
		var sdk = getSDKMetadata(socket.handshake);


		// Parse query string (`req.query`)
		var queryStringPos = socketReq.url.indexOf('?');
		var queryParams =	( queryStringPos === -1 ) ? 
							{} : 
							Qs.parse( socketReq.url.substr(queryStringPos + 1) );

		// Attached data becomes simulated HTTP body (`req.body`)
		var bodyParams = util.extend({}, socketReq.params || {}, socketReq.data || {});

		// Build request object
		var req = {
			
			// TODO: grab actual transports from socket.io
			transport: 'socket.io',

			method	: getVerb(socketReq, messageName),

			protocol: 'ws',

			ip      : socket.handshake.address.address ,

			port	: socket.handshake.address.port ,

			url		: socketReq.url,

			socket	: socket,

			isSocket: true,

			// Request params (`req.params`) are automatically parsed from URL path by slave router
			query	: queryParams || {},
			body	: bodyParams || {},

			// Lookup parameter<commit_msg>solves part of https://github.com/balderdashy/sails/issues/1517<commit_after>module.exports = function (sails) {

	
	/**
	 * Module dependencies.
	 */

	var util		= require('sails-util'),
		ResStream	= require('./ResStream'),
		getVerb		= require('./getVerb'),
		saveSessionAndThen = require('./saveSessionAndThen'),
		getSDKMetadata = require('../getSDKMetadata'),
		Qs			= require ('querystring');


	/**
	 * Module errors
	 */
	var Err = {
		invalidRequestCallback: function (invalidFn) {
			return new Error(
				'Invalid callback specified in socket request :: ' +
				util.inspect(invalidFn)
			);
		},
		invalidRedirect: function (location) {
			return new Error(
				'\n' +
				'res.redirect() :: [socket.io] ::' +
				'\n' +
				'Cannot redirect socket to invalid location :: ' + 
				util.inspect(location)
			);
		}
	};


	/**
	 * Interpret an incoming socket.io "request"
	 * Emulates Express semantics by mocking request (`req`) and response (`res`)
	 * 
	 * @param  {[type]}   socketReq        [incoming sails.io-formatted socket msg]
	 * @param  {[type]}   socketIOCallback [an ack callback useful for sending a response back to the client]
	 * @param  {[type]}   socket           [the socket that originated this request]
	 * @param  {[type]}   messageName      [the name of the message]
	 * @param  {Function} cb               [called when request interpretation is complete]
	 */
	
	return function interpretSocketReq (socketReq, socketIOCallback, socket, messageName, cb) {

		var msg;

		// If invalid callback function specified, freak out
		if (socketIOCallback && !util.isFunction(socketIOCallback)) {
			msg = 'Invalid socket request Could not be parse :: '+socketReq;
			return sails.log.error(msg);
		}

		// Parse request as JSON (or just use the object if we have one)
		if (! util.isObject(socketReq) && util.isString(socketReq) ) {
			try {
				socketReq = JSON.parse(socketReq);
			} catch (e) {
				msg = 'Invalid socket request! The following JSON could not be parsed :: '+socketReq;
				return emitError(msg);
			}
		}

		// If no URL specified, error out
		if (!socketReq.url) {
			msg = 'No url provided in request: '+socketReq;
			return emitError(msg);
		}

		if (!util.isString(socketReq.url)) {
			msg = 'Invalid url provided in request: ' + socketReq.url;
			return emitError(msg);
		}


		// Grab the metadata for the SDK
		var sdk = getSDKMetadata(socket.handshake);


		// Parse query string (`req.query`)
		var queryStringPos = socketReq.url.indexOf('?');
		var queryParams =	( queryStringPos === -1 ) ? 
							{} : 
							Qs.parse( socketReq.url.substr(queryStringPos + 1) );

		// Attached data becomes simulated HTTP body (`req.body`)
		var bodyParams = util.extend({}, socketReq.params || {}, socketReq.data || {});

		// Build request object
		var req = {
			
			// TODO: grab actual transports from socket.io
			transport: 'socket.io',

			method	: getVerb(socketReq, messageName),

			protocol: 'ws',

			ip      : socket.handshake.address && socket.handshake.address.address ,

			port	: socket.handshake.address && socket.handshake.address.port ,

			url		: socketReq.url,

			socket	: socket,

			isSocket: true,

			// Request params (`req.params`) are automatically parsed from URL path by slave router
			query	: queryParams || {},
			body	: bodyParams || {},

			// Lookup parameter
{'fraction_non_alphanumeric': 0.13069908814589665, 'fraction_numerical': 0.0012158054711246201, 'mean_word_length': 3.0548367221195316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '3821221', 'n_tokens_mistral': 2332, 'n_tokens_neox': 1955, 'n_words': 588}