<commit_before>'use strict';

var Trace = require("./lib/trace.js");
var debugLogger;

module.exports = function(logger) {
	debugLogger = logger;
	return module.exports;
}

module.exports.api = require("./lib/api.js");

module.exports.client = require("./lib/client.js").RpcClient;

module.exports.transport = {
	WebSocket: function(httpServer) {
		var transport = require("./lib/ws-transport");
		transport = new transport(httpServer);
		transport.trace = new Trace(debugLogger);
		return transport;
	},

	HTTP: function(httpServer, expressApp) {
		if (!httpServer || !expressApp) {
			throw new Error('HTTP transport requires an HTTP server and an Express application instance.');
		}
		var transport = require("./lib/rest-transport");
		transport = new transport(httpServer, expressApp);
		transport.trace = new Trace(debugLogger);
		return transport;
	},

	// TODO: Socket.io transport is deprecated, remove
	SocketIO: function(httpServer) {
		if (!httpServer) {
			throw new Error('SocketIO transport requires an HTTP server.');
		}
		var transport = require("./lib/sio-transport.js");
		transport = new transport(httpServer);
		transport.trace = new Trace(debugLogger);
		return transport;
	}
};

module.exports.transport.WebSocket.type = 'WebSocket';
module.exports.transport.HTTP.type = 'HTTP';
module.exports.transport.SocketIO.type = 'SocketIO';

/**
 * Fetches proxy code from a URL
 * Attempts to run the script in the VM and return to result to the caller
 * @param {String} proxyUrl
 * @param {Object} sslSettings
 * @param {Function} callback
 */
module.exports.proxy = function(proxyUrl, sslSettings, callback) {
	if (!callback) {
		callback = sslSettings;
		sslSettings = {};
	}
	var request = require('request');
	request(proxyUrl, {
		agentOptions: sslSettings
	}, function(err, response, body) {
		if (err) { callback(err); return; }

		if (response.statusCode !== 200) {
			callback(new Error('Proxy not available at ' + proxyUrl));
			return;
		}

		var fileName = proxyUrl.substring(0, proxyUrl.indexOf('?'));
		var proxyExports = {};

		var vm = require('vm');
		try {
			vm.runInNewContext(body, {
				module: {exports: proxyExports},
				require: function(moduleName) {
					if ('json-ws' === moduleName) {
						return module.exports;
					} else {
						return require(moduleName);
					}
				}
			}, fileName);
		} catch (vmError) {
			callback(new TypeError('Error loading proxy: ' + vmError.message));
			return;
		}

		callback(null, proxyExports);
	});
};

module.exports.getClientProxy = function(apiRoot, apiType, version, sslSettings, callback) {
	if (!callback) {
		callback = sslSettings;
		sslSettings = {};
	}
	var serviceUrl = apiRoot + '/' + apiType + '/' + version;
	var proxyClassName = apiType.split(/\W+/).map(function(string) {
		return string[0].toUpperCase() + string.slice(1).toLowerCase();
	}).join('') + 'Proxy';
	var proxyUrl = serviceUrl + '?proxy=JavaScript&localName=' + proxyClassName;
	module.exports.proxy(proxyUrl, sslSettings, function(err, proxy) {
		if (err) {
			callback(err, null);
		} else {
			var proxyClass = proxy[proxyClassName];
			if (proxyClass) {
				callback(null, new proxyClass(serviceUrl, sslSettings));
			} else {
				callback(new Error('Proxy not available'));
			}
		}
	});
}

/**
 * API Registry middleware for Express/Connect
 * Responds to OPTIONS request, renders a page listing all registered services
 * and serves the NodeJS/browser client library.
 * @param {String} rootPath Mount point of the service registry.
 */
module.exports.registry = function(rootPath) {
	var registry = require('./lib/registry');
	return registry(rootPath);
};
<commit_msg>Expose the "getLanguageProxy" function to the world<commit_after>'use strict';

var Trace = require("./lib/trace.js");
var debugLogger;

module.exports = function(logger) {
	debugLogger = logger;
	return module.exports;
}

module.exports.api = require("./lib/api.js");

module.exports.client = require("./lib/client.js").RpcClient;

module.exports.transport = {
	WebSocket: function(httpServer) {
		var transport = require("./lib/ws-transport");
		transport = new transport(httpServer);
		transport.trace = new Trace(debugLogger);
		return transport;
	},

	HTTP: function(httpServer, expressApp) {
		if (!httpServer || !expressApp) {
			throw new Error('HTTP transport requires an HTTP server and an Express application instance.');
		}
		var transport = require("./lib/rest-transport");
		transport = new transport(httpServer, expressApp);
		transport.trace = new Trace(debugLogger);
		return transport;
	},

	// TODO: Socket.io transport is deprecated, remove
	SocketIO: function(httpServer) {
		if (!httpServer) {
			throw new Error('SocketIO transport requires an HTTP server.');
		}
		var transport = require("./lib/sio-transport.js");
		transport = new transport(httpServer);
		transport.trace = new Trace(debugLogger);
		return transport;
	}
};

module.exports.transport.WebSocket.type = 'WebSocket';
module.exports.transport.HTTP.type = 'HTTP';
module.exports.transport.SocketIO.type = 'SocketIO';

/**
 * Fetches proxy code from a URL
 * Attempts to run the script in the VM and return to result to the caller
 * @param {String} proxyUrl
 * @param {Object} sslSettings
 * @param {Function} callback
 */
module.exports.proxy = function(proxyUrl, sslSettings, callback) {
	if (!callback) {
		callback = sslSettings;
		sslSettings = {};
	}
	var request = require('request');
	request(proxyUrl, {
		agentOptions: sslSettings
	}, function(err, response, body) {
		if (err) { callback(err); return; }

		if (response.statusCode !== 200) {
			callback(new Error('Proxy not available at ' + proxyUrl));
			return;
		}

		var fileName = proxyUrl.substring(0, proxyUrl.indexOf('?'));
		var proxyExports = {};

		var vm = require('vm');
		try {
			vm.runInNewContext(body, {
				module: {exports: proxyExports},
				require: function(moduleName) {
					if ('json-ws' === moduleName) {
						return module.exports;
					} else {
						return require(moduleName);
					}
				}
			}, fileName);
		} catch (vmError) {
			callback(new TypeError('Error loading proxy: ' + vmError.message));
			return;
		}

		callback(null, proxyExports);
	});
};

module.exports.getClientProxy = function(apiRoot, apiType, version, sslSettings, callback) {
	if (!callback) {
		callback = sslSettings;
		sslSettings = {};
	}
	var serviceUrl = apiRoot + '/' + apiType + '/' + version;
	var proxyClassName = apiType.split(/\W+/).map(function(string) {
		return string[0].toUpperCase() + string.slice(1).toLowerCase();
	}).join('') + 'Proxy';
	var proxyUrl = serviceUrl + '?proxy=JavaScript&localName=' + proxyClassName;
	module.exports.proxy(proxyUrl, sslSettings, function(err, proxy) {
		if (err) {
			callback(err, null);
		} else {
			var proxyClass = proxy[proxyClassName];
			if (proxyClass) {
				callback(null, new proxyClass(serviceUrl, sslSettings));
			} else {
				callback(new Error('Proxy not available'));
			}
		}
	});
}

/**
 * API Registry middleware for Express/Connect
 * Responds to OPTIONS request, renders a page listing all registered services
 * and serves the NodeJS/browser client library.
 * @param {String} rootPath Mount point of the service registry.
 */
module.exports.registry = function(rootPath) {
	var registry = require('./lib/registry');
	return registry(rootPath);
};

module.exports.getLanguageProxy = require('./lib/get-language-proxy');

{'fraction_non_alphanumeric': 0.14241276710846631, 'fraction_numerical': 0.0016229375169055991, 'mean_word_length': 4.178571428571429, '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': '5403967', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2239, 'n_words': 576}