<commit_before>jsio('import std.uri as uri'); 
jsio('import std.base64 as base64');
jsio('from util.browserdetect import BrowserDetect');

;(function() {
	var doc;
	exports.getDoc = function() {
		if (doc) { return doc; }
		try {
			doc = window.ActiveXObject && new ActiveXObject('htmlfile');
			if (doc) {
				doc.open().write('<html></html>');
				doc.close();
				window.attachEvent('onunload', function() {
					try { doc.body.innerHTML = ''; } catch(e) {}
					doc = null;
				});
			}
		} catch(e) {}
		
		if (!doc) { doc = document; }
		return doc;
	};

	exports.XHR = function() {
		var win = window,
			doc = exports.getDoc();
		//if (doc.parentWindow) { win = doc.parentWindow; }
		
		return new (exports.XHR = win.XMLHttpRequest ? win.XMLHttpRequest
			: function() { return win.ActiveXObject && new win.ActiveXObject('Msxml2.XMLHTTP') || null; });
	}
	
	exports.createXHR = function() { return new exports.XHR(); }

})();

function isLocalFile(url) { return /^file:\/\//.test(url); }
function isWindowDomain(url) { return uri.isSameDomain(url, window.location.href); }

function canUseXHR(url) {
	// always use jsonp for local files
	if (isLocalFile(url)) { return false; }
	
	// try to create an XHR using the same function the XHR transport uses
	var xhr = new exports.XHR();
	if (!xhr) { return false; }
	
	// if the URL requested is the same domain as the window,
	// then we can use same-domain XHRs
	if (isWindowDomain(url)) { return true; }
	
	// if the URL requested is a different domain than the window,
	// then we need to check for cross-domain support
	if (window.XMLHttpRequest
			&& (xhr.__proto__ == XMLHttpRequest.prototype // WebKit Bug 25205
				|| xhr instanceof window.XMLHttpRequest)
			&& xhr.withCredentials !== undefined
		|| window.XDomainRequest 
			&& xhr instanceof window.XDomainRequest) {
		return true;
	}
};

var transports = exports.transports = {};

exports.chooseTransport = function(url, options) {
	switch(options.preferredTransport) {
		case 'jsonp':
			return transports.jsonp;
		case 'xhr':
		default:
			if (canUseXHR(url)) { return transports.xhr; };
			return transports.jsonp;
	}
};

// TODO: would be nice to use these somewhere...

var PARAMS = {
	'xhrstream':   {"is": "1", "bs": "\n"},
	'xhrpoll':     {"du": "0"},
	'xhrlongpoll': {},
	'sselongpoll': {"bp": "data: ", "bs": "\r\n", "se": "1"},
	'ssestream':   {"bp": "data: ", "bs": "\r\n", "se": "1", "is": "1"}
};

exports.Transport = Class(function(supr) {
	this.handshake = function(url, options) {
		throw new Error("handshake Not Implemented"); 
	};
	this.comet = function(url, sessionKey, lastEventId, options) { 
		throw new Error("comet Not Implemented"); 
	};
	this.send = function(url, sessionKey, data, options) { 
		throw new Error("send Not Implemented");
	};
	this.encodePacket = function(packetId, data, options) { 
		throw new Error("encodePacket Not Implemented"); 
	};
	this.abort = function() { 
		throw new Error("abort Not Implemented"); 
	};
});

var baseTransport = Class(exports.Transport, function(supr) {
	this.init = function() {
		this._aborted = false;
		this._handshakeArgs = {
			d:'{}',
			ct:'application/javascript'
		};
	};
	
	this.handshake = function(url, options) {
		logger.debug('handshake:', url, options);
		this._makeRequest('send', url + '/handshake', 
						  this._handshakeArgs, 
						  this.handshakeSuccess, 
						  this.handshakeFailure);
	};
	
	this.comet = function(url, sessionKey, lastEventId, options) {
		logger.debug('comet:', url, sessionKey, lastEventId, options);
		args = {
			s: sessionKey,
			a: lastEventId
		};
		this._makeRequest('comet', url + '/comet', 
						  args, 
						  this.cometSuccess, 
						  this.cometFailure);
	};
	
	this.send = function(url, sessionKey, lastEventId, data, options) {
		logger.debug('send:', url, sessionKey, data, options);
		args = {
			d: data,
			s: sessionKey,
			a: lastEventId
		};
		this._makeRequest('send', url + '/send', 
						  args, 
						  this.sendSuccess, 
						  this.sendFailure);
	};
});

transports.xhr = Class(baseTransport, function(supr) {
	
	this.init = function() {
		supr(this, 'init');<commit_msg>missing var<commit_after>jsio('import std.uri as uri'); 
jsio('import std.base64 as base64');
jsio('from util.browserdetect import BrowserDetect');

;(function() {
	var doc;
	exports.getDoc = function() {
		if (doc) { return doc; }
		try {
			doc = window.ActiveXObject && new ActiveXObject('htmlfile');
			if (doc) {
				doc.open().write('<html></html>');
				doc.close();
				window.attachEvent('onunload', function() {
					try { doc.body.innerHTML = ''; } catch(e) {}
					doc = null;
				});
			}
		} catch(e) {}
		
		if (!doc) { doc = document; }
		return doc;
	};

	exports.XHR = function() {
		var win = window,
			doc = exports.getDoc();
		//if (doc.parentWindow) { win = doc.parentWindow; }
		
		return new (exports.XHR = win.XMLHttpRequest ? win.XMLHttpRequest
			: function() { return win.ActiveXObject && new win.ActiveXObject('Msxml2.XMLHTTP') || null; });
	}
	
	exports.createXHR = function() { return new exports.XHR(); }

})();

function isLocalFile(url) { return /^file:\/\//.test(url); }
function isWindowDomain(url) { return uri.isSameDomain(url, window.location.href); }

function canUseXHR(url) {
	// always use jsonp for local files
	if (isLocalFile(url)) { return false; }
	
	// try to create an XHR using the same function the XHR transport uses
	var xhr = new exports.XHR();
	if (!xhr) { return false; }
	
	// if the URL requested is the same domain as the window,
	// then we can use same-domain XHRs
	if (isWindowDomain(url)) { return true; }
	
	// if the URL requested is a different domain than the window,
	// then we need to check for cross-domain support
	if (window.XMLHttpRequest
			&& (xhr.__proto__ == XMLHttpRequest.prototype // WebKit Bug 25205
				|| xhr instanceof window.XMLHttpRequest)
			&& xhr.withCredentials !== undefined
		|| window.XDomainRequest 
			&& xhr instanceof window.XDomainRequest) {
		return true;
	}
};

var transports = exports.transports = {};

exports.chooseTransport = function(url, options) {
	switch(options.preferredTransport) {
		case 'jsonp':
			return transports.jsonp;
		case 'xhr':
		default:
			if (canUseXHR(url)) { return transports.xhr; };
			return transports.jsonp;
	}
};

// TODO: would be nice to use these somewhere...

var PARAMS = {
	'xhrstream':   {"is": "1", "bs": "\n"},
	'xhrpoll':     {"du": "0"},
	'xhrlongpoll': {},
	'sselongpoll': {"bp": "data: ", "bs": "\r\n", "se": "1"},
	'ssestream':   {"bp": "data: ", "bs": "\r\n", "se": "1", "is": "1"}
};

exports.Transport = Class(function(supr) {
	this.handshake = function(url, options) {
		throw new Error("handshake Not Implemented"); 
	};
	this.comet = function(url, sessionKey, lastEventId, options) { 
		throw new Error("comet Not Implemented"); 
	};
	this.send = function(url, sessionKey, data, options) { 
		throw new Error("send Not Implemented");
	};
	this.encodePacket = function(packetId, data, options) { 
		throw new Error("encodePacket Not Implemented"); 
	};
	this.abort = function() { 
		throw new Error("abort Not Implemented"); 
	};
});

var baseTransport = Class(exports.Transport, function(supr) {
	this.init = function() {
		this._aborted = false;
		this._handshakeArgs = {
			d:'{}',
			ct:'application/javascript'
		};
	};
	
	this.handshake = function(url, options) {
		logger.debug('handshake:', url, options);
		this._makeRequest('send', url + '/handshake', 
						  this._handshakeArgs, 
						  this.handshakeSuccess, 
						  this.handshakeFailure);
	};
	
	this.comet = function(url, sessionKey, lastEventId, options) {
		logger.debug('comet:', url, sessionKey, lastEventId, options);
		var args = {
			s: sessionKey,
			a: lastEventId
		};
		this._makeRequest('comet', url + '/comet', 
						  args, 
						  this.cometSuccess, 
						  this.cometFailure);
	};
	
	this.send = function(url, sessionKey, lastEventId, data, options) {
		logger.debug('send:', url, sessionKey, data, options);
		args = {
			d: data,
			s: sessionKey,
			a: lastEventId
		};
		this._makeRequest('send', url + '/send', 
						  args, 
						  this.sendSuccess, 
						  this.sendFailure);
	};
});

transports.xhr = Class(baseTransport, function(supr) {
	
	this.init = function() {
		supr(this, 'init');
{'fraction_non_alphanumeric': 0.16244267439053825, 'fraction_numerical': 0.003620564808110065, 'mean_word_length': 3.506253398586188, 'pattern_counts': {'":': 20, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1982357', 'n_tokens_mistral': 3147, 'n_tokens_neox': 2778, 'n_words': 695}