<commit_before>
exports.Agent = Agent;
function Agent(methods) {
	this.methods = methods || {};
}

Agent.prototype.attach = function attach(transport, callback) {
	var remote = makeRemote(this, transport);
	// Listen for the other agent to tell us it's ready.
	var keys = Object.keys(this.methods);

	// When the other side is ready, tell it our methods
  remote.ready = function (callback) {
		delete remote.ready;
		callback(keys);
	};

	// Tell the other agent we're ready and ask for it's methods
	remote.call("ready", [function (methodNames) {
		methodNames.forEach(function (name) {
			// Create a local proxy function for easy calling.
			remote[name] = function () {
				remote.call(name, Array.prototype.slice.call(arguments));
			}
		});
		callback(remote);
	}]);
};

// `agent` is the local agent. `transport` is a transport to the remote agent.
function makeRemote(agent, transport) {
	// `remote` inherits from the local methods to serve the functions
	var remote = Object.create(agent.methods);
	var callbacks = {};
	var nextKey = 0;

	// Handle incoming messages.
	transport.on('message', function (message) {
		if (!(Array.isArray(message) && message.length)) throw new Error("Should be array");
		message = liven(message, function (id) {
			var key = getKey();
			var fn = function () {
				delete callbacks[key];
				remote.call(id, Array.prototype.slice.call(arguments));
			}
			callbacks[key] = fn;
			return fn;
		});
		var target = message[0];
		if (typeof target === "string") {
			// Route named messages to named events
			remote[target].apply(remote, message.slice(1));
		}
		else {
			// Route others to one-shot callbacks
			var fn = callbacks[target];
			if (!(typeof fn === "function")) throw new Error("Should be function");
			fn.apply(null, message.slice(1));
		}
	});

	function getKey() {
		var key = (nextKey + 1) >> 0;
		while (callbacks.hasOwnProperty(key)) {
			key = (key + 1) >> 0;
			if (key === nextKey) {
				throw new Error("Ran out of keys!!");
			}
		}
		nextKey = key;

		return key;
	}

	// Enable outgoing messages
	// Warning, this mutates args, don't plan on reusing them.
	remote.call = function call(name, args) {
		args.unshift(name);
		var message = freeze(args, function (fn) {
			var key = getKey();
			callbacks[key] = function () {<commit_msg>Don't require transports to be EventEmitter instances<commit_after>
exports.Agent = Agent;
function Agent(methods) {
	this.methods = methods || {};
}

Agent.prototype.attach = function attach(transport, callback) {
	var remote = makeRemote(this, transport);
	// Listen for the other agent to tell us it's ready.
	var keys = Object.keys(this.methods);

	// When the other side is ready, tell it our methods
  remote.ready = function (callback) {
		delete remote.ready;
		callback(keys);
	};

	// Tell the other agent we're ready and ask for it's methods
	remote.call("ready", [function (methodNames) {
		methodNames.forEach(function (name) {
			// Create a local proxy function for easy calling.
			remote[name] = function () {
				remote.call(name, Array.prototype.slice.call(arguments));
			}
		});
		callback(remote);
	}]);
};

// `agent` is the local agent. `transport` is a transport to the remote agent.
function makeRemote(agent, transport) {
	// `remote` inherits from the local methods to serve the functions
	var remote = Object.create(agent.methods);
	var callbacks = {};
	var nextKey = 0;

	// Handle incoming messages.
	if (transport.on) transport.on("message", onMessage);
	else transport.onMessage = onMessage;
	function onMessage(message) {
		if (!(Array.isArray(message) && message.length)) throw new Error("Should be array");
		message = liven(message, function (id) {
			var key = getKey();
			var fn = function () {
				delete callbacks[key];
				remote.call(id, Array.prototype.slice.call(arguments));
			}
			callbacks[key] = fn;
			return fn;
		});
		var target = message[0];
		if (typeof target === "string") {
			// Route named messages to named events
			remote[target].apply(remote, message.slice(1));
		}
		else {
			// Route others to one-shot callbacks
			var fn = callbacks[target];
			if (!(typeof fn === "function")) throw new Error("Should be function");
			fn.apply(null, message.slice(1));
		}
	};

	function getKey() {
		var key = (nextKey + 1) >> 0;
		while (callbacks.hasOwnProperty(key)) {
			key = (key + 1) >> 0;
			if (key === nextKey) {
				throw new Error("Ran out of keys!!");
			}
		}
		nextKey = key;

		return key;
	}

	// Enable outgoing messages
	// Warning, this mutates args, don't plan on reusing them.
	remote.call = function call(name, args) {
		args.unshift(name);
		var message = freeze(args, function (fn) {
			var key = getKey();
			callbacks[key] = function () {
{'fraction_non_alphanumeric': 0.1417689377909437, 'fraction_numerical': 0.0033855268726195515, 'mean_word_length': 3.562741312741313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5729053', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1454, 'n_words': 466}