<commit_before>(function() {
	'use strict';

	var SockJS = require('sockjs-client'),
		fetch = require('fetch'),
		_ = require('lodash');

	module.exports = Channel;

	function Channel(apiToken) {

		var handlers = {},
			self = this,
			client = null,
			clientStarted = false,
			clientClosed = false;
		
		this.startClient = function() {
			if (clientStarted) {
				return;
			}
			clientStarted = true;
			clientClosed = false;

			client = new SockJS((process.env.PIPEDRIVE_API_PROTOCOL || 'https')+'://'+(process.env.PIPEDRIVE_CHANNEL_HOST || 'channel.pipedrive.com')+'/sockjs');

			client.onopen = function () {
				var options = {
					rejectUnauthorized: false
				};
				fetch.fetchUrl((process.env.PIPEDRIVE_API_PROTOCOL || 'https')+'://'+(process.env.PIPEDRIVE_API_HOST || 'api.pipedrive.com')+'/v1/authorizations/nonce?api_token=' + encodeURIComponent(apiToken), options, function(error, meta, body) {
					var data = {};
					try {
						data = JSON.parse(body);
						if (data.data) {
							data = data.data;
						}
					}
					catch (e) {
						throw new Error('Could not parse API response');
					}

					if (data && data.nonce) {
						client.send(JSON.stringify({
								company_id: data.company_id,
								user_id: data.user_id,
								user_name: 'client-nodejs-user',
								host: 'app.pipedrive.com',
								timestamp: Math.round(new Date().getTime() / 1000),
								nonce: data.nonce
						}));
					}
					else {
						throw new Error('Authorization failed');
					}
				});
			};
			client.onmessage = function (msg) {
				if (msg && msg.type === 'message') {
					var data = {},
						eventPatterns = [];

					try {
						data = JSON.parse(msg.data);
					}
					catch (e) {
						throw new Error('Malformed JSON received from socket');
					}

					if (data && data.meta && data.meta.v === 1) {

						eventPatterns = [
							data.meta.object + '.' + data.meta.action,
							'*.' + data.meta.action,
							data.meta.object + '.*',
							'*.*'
						];

						_.each(eventPatterns, function(pattern) {
							if (handlers[pattern]) {
								_.each(handlers[pattern], function(handler) {
									handler(data, data.data);
								});
							}
						});
					}

					if (data.rabbitStateChange === 'open') {
						if (handlers.connect) {
							_.each(handlers.connect, function(handler) {
								handler();
							});
						}
					}
				}
			};
			client.onclose = function (e) {
				if (!clientClosed) {
					// not closed by user - we have some connection error.
					self.restartClient();
					return;
				}

				clientStarted = false;
				if (handlers.close) {
					_.each(handlers.close, function(handler) {
						handler(e);
					});
				}
			};
		};

		this.restartClient = function() {
			client.onopen = null;
			client.onclose = null;
			client.onmessage = null;
			client = null;

			clientStarted = false;

			setTimeout(self.startClient, (1+Math.random()*4)*1000);
		};

		this.on = function(method, handler) {
			if (!clientStarted) {
				self.startClient();
			}
			handlers[method] = handlers[method] || [];
			handlers[method].push(handler);
		};

		this.removeListener = function(method, handler) {
			var index = handlers[method].indexOf(handler);
			if (index > -1) {
				handlers[method].splice(index, 1);
			}
			if (!_.keys(handlers).length) {
				this.removeAllListeners();
			}
		};

		this.removeAllListeners = function() {
			handlers = {};<commit_msg>add multi-channel support for the 3.0 branch<commit_after>(function() {
	'use strict';

	var SockJS = require('sockjs-client'),
		fetch = require('fetch'),
		_ = require('lodash');

	module.exports = Channel;

	function Channel(apiToken) {

		var handlers = {},
			self = this,
			client = null,
			clientStarted = false,
			clientClosed = false;
		
		this.startClient = function() {
			if (clientStarted) {
				return;
			}
			clientStarted = true;
			clientClosed = false;

			client = new SockJS((process.env.PIPEDRIVE_API_PROTOCOL || 'https')+'://'+(process.env.PIPEDRIVE_CHANNEL_HOST || 'channel.pipedrive.com')+'/sockjs');

			client.onopen = function () {
				var options = {
					rejectUnauthorized: false
				};
				fetch.fetchUrl((process.env.PIPEDRIVE_API_PROTOCOL || 'https')+'://'+(process.env.PIPEDRIVE_API_HOST || 'api.pipedrive.com')+'/v1/authorizations/nonce?api_token=' + encodeURIComponent(apiToken), options, function(error, meta, body) {
					var data = {};
					try {
						data = JSON.parse(body);
						if (data.data) {
							data = data.data;
						}
					}
					catch (e) {
						throw new Error('Could not parse API response');
					}

					if (data && data.nonce) {
						client.send(JSON.stringify({
								company_id: data.company_id,
								user_id: data.user_id,
								user_name: 'client-nodejs-user',
								host: 'app.pipedrive.com',
								timestamp: Math.round(new Date().getTime() / 1000),
								nonce: data.nonce
						}));
					}
					else {
						throw new Error('Authorization failed');
					}
				});
			};
			client.onmessage = function (msg) {
				if (msg && msg.type === 'message') {
					var data = {},
						eventPatterns = [];

					try {
						data = JSON.parse(msg.data);
					}
					catch (e) {
						throw new Error('Malformed JSON received from socket');
					}

					if (data && data.meta && data.meta.v === 1) {

						eventPatterns = [
							data.meta.object + '.' + data.meta.action,
							'*.' + data.meta.action,
							data.meta.object + '.*',
							'*.*'
						];

						_.each(eventPatterns, function(pattern) {
							if (handlers[pattern]) {
								_.each(handlers[pattern], function(handler) {
									handler(data, data.data);
								});
							}
						});
					}

					if (!!data.switch_to_channel && data.switch_to_channel.toString().match(/^\d+$/)) {
						process.env.PIPEDRIVE_CHANNEL_HOST = 'channel' + data.switch_to_channel.toString() + '.pipedrive.com';
						self.restartClient();
					}

					if (data.rabbitStateChange === 'open') {
						if (handlers.connect) {
							_.each(handlers.connect, function(handler) {
								handler();
							});
						}
					}
				}
			};
			client.onclose = function (e) {
				if (!clientClosed) {
					// not closed by user - we have some connection error.
					self.startClient();
					return;
				}

				clientStarted = false;
				if (handlers.close) {
					_.each(handlers.close, function(handler) {
						handler(e);
					});
				}
			};
		};

		this.restartClient = function() {
			client.onopen = null;
			client.onmessage = null;
			clientStarted = false;
			clientClosed = false;
			client.close();
		};

		this.on = function(method, handler) {
			if (!clientStarted) {
				self.startClient();
			}
			handlers[method] = handlers[method] || [];
			handlers[method].push(handler);
		};

		this.removeListener = function(method, handler) {
			var index = handlers[method].indexOf(handler);
			if (index > -1) {
				handlers[method].splice(index, 1);
			}
			if (!_.keys(handlers).length) {
				this.removeAllListeners();
			}
		};

		this.removeAllListeners = function() {
			handlers = {};
{'fraction_non_alphanumeric': 0.15980698268521146, 'fraction_numerical': 0.0034061879080329264, 'mean_word_length': 2.432537749634681, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3896086', 'n_tokens_mistral': 3231, 'n_tokens_neox': 2311, 'n_words': 417}