<commit_before>// TODO: make it safe to call methods during callbacks

// don't break if there's no console
if(typeof(console) === "undefined")
{
	console = { log: function() {} };
}

var FO = FO || {};
var Fpp = Fpp || {};

var fo_jsonp_callback_id = 0;
var fo_jsonp_callbacks = {};

function fo_jsonp_get_callback(cb_id)
{
	if(fo_jsonp_callbacks[cb_id])
	{
		return fo_jsonp_callbacks[cb_id];
	}
	else
	{
		console.log("no callback with id " + cb_id);
		return function(result) {};
	}
}

// FO.Request has onFinished(int code, object result) and onError(int reason) callback members
// error reasons: 0=http, 1=timeout
// note: failed json parse on http body results in empty object {}, not error

FO.Request = function()
{
	if(!(this instanceof FO.Request))
		throw new Error("Constructor called as a function");
}

FO.Request.prototype.start = function(method, url, headers, body)
{
	this._cors = false;
	if("withCredentials" in new XMLHttpRequest())
		this._cors = true;

	var self = this;
	this._timer = setTimeout(function() { self._timeout(); }, 60000);

	if(this._cors)
	{
		this._xhr = new XMLHttpRequest();

		this._xhr.onreadystatechange = function() { self._xhr_readystatechange(); };
		this._xhr.open(method, url, true);

		for(var key in headers)
		{
			if(!headers.hasOwnProperty(key))
				continue;

			this._xhr.setRequestHeader(key, headers[key]);
		}

		this._xhr.send(body);
	}
	else
	{
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement("script");

		script.type = "text/javascript";

		this._cb_id = fo_jsonp_callback_id.toString();
		++fo_jsonp_callback_id;

		this._script_id = "fo-jsonp-script-" + this._cb_id;
		script.id = this._script_id;

		fo_jsonp_callbacks[this._cb_id] = function(result) { self._jsonp_callback(result); };

		var param_list = new Array();

		param_list.push("callback=" + encodeURIComponent("fo_jsonp_get_callback(\"" + this._cb_id + "\")"));
		if(method != "GET")
			param_list.push("method=" + encodeURIComponent(method));
		if(headers)
			param_list.push("headers=" + encodeURIComponent(JSON.stringify(headers)));
		if(body)
			param_list.push("body=" + encodeURIComponent(body));

		var params = param_list.join("&");

		var src;
		if(url.indexOf("?") != -1)
			src = url + "&" + params;
		else
			src = url + "?" + params;

		script.src = src;
		console.log("FO.Request json-p " + this._cb_id + " " + src);
		head.appendChild(script);
	}
}

FO.Request.prototype.abort = function()
{<commit_msg>use correct json-p format<commit_after>// TODO: make it safe to call methods during callbacks

// don't break if there's no console
if(typeof(console) === "undefined")
{
	console = { log: function() {} };
}

var FO = FO || {};
var Fpp = Fpp || {};

var fo_jsonp_callback_id = 0;
var fo_jsonp_callbacks = {};

function fo_jsonp_get_callback(cb_id)
{
	if(fo_jsonp_callbacks[cb_id])
	{
		return fo_jsonp_callbacks[cb_id];
	}
	else
	{
		console.log("no callback with id " + cb_id);
		return function(result) {};
	}
}

// FO.Request has onFinished(int code, object result) and onError(int reason) callback members
// error reasons: 0=http, 1=timeout
// note: failed json parse on http body results in empty object {}, not error

FO.Request = function()
{
	if(!(this instanceof FO.Request))
		throw new Error("Constructor called as a function");
}

FO.Request.prototype.start = function(method, url, headers, body)
{
	this._cors = false;
	if("withCredentials" in new XMLHttpRequest())
		this._cors = true;

	var self = this;
	this._timer = setTimeout(function() { self._timeout(); }, 60000);

	if(this._cors)
	{
		this._xhr = new XMLHttpRequest();

		this._xhr.onreadystatechange = function() { self._xhr_readystatechange(); };
		this._xhr.open(method, url, true);

		for(var key in headers)
		{
			if(!headers.hasOwnProperty(key))
				continue;

			this._xhr.setRequestHeader(key, headers[key]);
		}

		this._xhr.send(body);
	}
	else
	{
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement("script");

		script.type = "text/javascript";

		this._cb_id = fo_jsonp_callback_id.toString();
		++fo_jsonp_callback_id;

		this._script_id = "fo-jsonp-script-" + this._cb_id;
		script.id = this._script_id;

		fo_jsonp_callbacks[this._cb_id] = function(result) { self._jsonp_callback(result); };

		var param_list = new Array();

		param_list.push("callback=" + encodeURIComponent("fo_jsonp_get_callback(\"" + this._cb_id + "\")"));
		if(method != "GET")
			param_list.push("_method=" + encodeURIComponent(method));
		if(headers)
			param_list.push("_headers=" + encodeURIComponent(JSON.stringify(headers)));
		if(body)
			param_list.push("_body=" + encodeURIComponent(body));

		var params = param_list.join("&");

		var src;
		if(url.indexOf("?") != -1)
			src = url + "&" + params;
		else
			src = url + "?" + params;

		script.src = src;
		console.log("FO.Request json-p " + this._cb_id + " " + src);
		head.appendChild(script);
	}
}

FO.Request.prototype.abort = function()
{
{'fraction_non_alphanumeric': 0.15863172634526906, 'fraction_numerical': 0.004000800160032006, 'mean_word_length': 4.279831045406547, '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': '1816223', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1764, 'n_words': 371}