<commit_before>module Exclusive {
	export class Connection {
		private parsedUrl: any;
		private requestUrl: string;
		private requestPath: string;
		private request: any;
		get Request() { return this.request; }
		private response: any;
		get Response() { return this.response; }
		private authorisation: any;

		constructor(parsedUrl: any, request: any, response: any) {
			this.parsedUrl = parsedUrl;
			this.requestUrl = parsedUrl.href
			this.requestPath = parsedUrl.path;
			this.request = request;
			this.response = response;
			this.authorisation = request.headers["authorization"];
		}
		/**Sends a response to the client.
		 * Response.end() will be called automatically.
		 * @param message Type of string the message to be sent.
		 * @param statusCode Type of number status code for this response.
		 * @param headers Type of any object to be sent along with response as a header.
		 */
		public Write(message: string, statusCode: number, headers: any) {
			this.SetHeader(statusCode, headers);
			this.response.write(message);
			this.response.end();
		}
		/**Sends all users in Exclusive in a response to the client.
		 * Sets the header of Content-Type as application/json.
		 */
		public WriteAllUsers() {
			this.response.writeHead(200, "ok", { 'Content-Type': 'application/json' });
			var i = 0;
			var toPrint = "[\n";
			while (i < DataStore.Users.length) {
				if (i != 0)
					toPrint = "";
				for (var n = 0; i < DataStore.Users.length && n < 50; n++ , i++)
					toPrint += DataStore.Users[i].ToJSON() + ",\n";
				if (i != DataStore.Users.length)
					this.response.write(toPrint);
			}
			if (toPrint != "[\n")
				toPrint = toPrint.slice(0, -2);
			toPrint += "\n]";
			this.response.write(toPrint);
			this.response.end();
		}
		/**Sends a file to the client in the response.
		 * Response.end() will be called automatically.
		 * @param file Type of string name of the required file.
		 * @param log Type of boolean determining if this method is being called when making a log for a user to response accordingly.
		 * @param onCompleted The callback is passed one argument (statusCode), type of number that holds the proper status code for the response.
		 */
		public WriteFile(file: string, log?: boolean, onCompleted?: (statusCode: number) => void) {
			if (this.requestPath.substr(-1) == "/")
				file = path.join(file, 'index.html');
			fs.stat(file, (error: any, stats: any) => {
				if (stats.isDirectory()) {
					this.Write("Redirecting", 301, { 'Location': this.requestUrl + "/" });
					if (onCompleted)
						onCompleted(301);
				}
				else {
					fs.readFile(file, (error: any, data: any) => {
						if (data) {
							var contentType = Connection.ContentType(file);
							if (stats.size) {
								if ((this.request.headers['range']) && (this.request.headers.range.split('=')[0] == "bytes")) {
									var positions = this.request.headers.range.replace(/bytes=/, "").split("-");
									var start = parseInt(positions[0], 10);
									var total = stats.size;
									var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
									var chunksize = (end - start) + 1;
									this.response.writeHead(206, { 'Content-Range': 'bytes' + start + "-" + end + "/" + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': contentType });
									var stream = fs.createReadStream(file, { start: start, end: end })
										.on("open", () => {
											stream.pipe(this.response);
										}).on("error", (err: any) => {
											this.response.end(err);
										});
									if (onCompleted)
										onCompleted(206);
								}
								else {
									this.Write(data, 200, { 'Accept-Ranges': 'bytes', 'Content-Lenth': stats.size, 'Content-Type': contentType });
									if (onCompleted)
										onCompleted(200);
								}
							}
							else {
								this.Write(data, 200, {'Content-Type': contentType });
								if (onCompleted)
									onCompleted(200);
							}
						}
						else {
							this.Write("Not Found", 404, { 'Content-Type': 'text/html' });
							if (onCompleted)
								onCompleted(404);
						}
					});
				}
			});
		}
		/**Receives data from a client in this connection and creates a type of User object according to the received data.
		 * @param onCompleted the callback is passed one argument (result) type of User holds the created object of User.
		*/
		public Receive(onCompleted: (result: User) => void) {
			var result: User = null;
			this.ReceivingData((fullbody) => {
				if (fullbody) {
					var contents: string[] = [];
					var jsonUser = JSON.parse(fullbody);
					if ((jsonUser.company || jsonUser.Company) && (jsonUser.contact || jsonUser.Contact) && (jsonUser.crm || jsonUser.Crm)) {
						var theCompany: any;
						var theContact: any;
						var theCrm: any;
						(jsonUser.company) ? theCompany = jsonUser.company : theCompany = jsonUser.Company;
						(jsonUser.contact) ? theContact = jsonUser.contact : theContact = jsonUser.Contact;
						(jsonUser.crm) ? theCrm = jsonUser.crm : theCrm = jsonUser.Crm;
						result = new User(theCompany, theContact, theCrm);<commit_msg>minor improvment - implementing (log?) for a better response to the user<commit_after>module Exclusive {
	export class Connection {
		private parsedUrl: any;
		private requestUrl: string;
		private requestPath: string;
		private request: any;
		get Request() { return this.request; }
		private response: any;
		get Response() { return this.response; }
		private authorisation: any;

		constructor(parsedUrl: any, request: any, response: any) {
			this.parsedUrl = parsedUrl;
			this.requestUrl = parsedUrl.href
			this.requestPath = parsedUrl.path;
			this.request = request;
			this.response = response;
			this.authorisation = request.headers["authorization"];
		}
		/**Sends a response to the client.
		 * Response.end() will be called automatically.
		 * @param message Type of string the message to be sent.
		 * @param statusCode Type of number status code for this response.
		 * @param headers Type of any object to be sent along with response as a header.
		 */
		public Write(message: string, statusCode: number, headers: any) {
			this.SetHeader(statusCode, headers);
			this.response.write(message);
			this.response.end();
		}
		/**Sends all users in Exclusive in a response to the client.
		 * Sets the header of Content-Type as application/json.
		 */
		public WriteAllUsers() {
			this.response.writeHead(200, "ok", { 'Content-Type': 'application/json' });
			var i = 0;
			var toPrint = "[\n";
			while (i < DataStore.Users.length) {
				if (i != 0)
					toPrint = "";
				for (var n = 0; i < DataStore.Users.length && n < 50; n++ , i++)
					toPrint += DataStore.Users[i].ToJSON() + ",\n";
				if (i != DataStore.Users.length)
					this.response.write(toPrint);
			}
			if (toPrint != "[\n")
				toPrint = toPrint.slice(0, -2);
			toPrint += "\n]";
			this.response.write(toPrint);
			this.response.end();
		}
		/**Sends a file to the client in the response.
		 * Response.end() will be called automatically.
		 * @param file Type of string name of the required file.
		 * @param log Type of boolean determining if this method is being called when making a log for a user to response accordingly.
		 * @param onCompleted The callback is passed one argument (statusCode), type of number that holds the proper status code for the response.
		 */
		public WriteFile(file: string, log?: boolean, onCompleted?: (statusCode: number) => void) {
			if (this.requestPath.substr(-1) == "/")
				file = path.join(file, 'index.html');
			fs.stat(file, (error: any, stats: any) => {
				if (stats.isDirectory()) {
					this.Write("Redirecting", 301, { 'Location': this.requestUrl + "/" });
					if (onCompleted)
						onCompleted(301);
				}
				else {
					fs.readFile(file, (error: any, data: any) => {
						if (data) {
							var contentType = Connection.ContentType(file);
							if (stats.size) {
								if ((this.request.headers['range']) && (this.request.headers.range.split('=')[0] == "bytes")) {
									var positions = this.request.headers.range.replace(/bytes=/, "").split("-");
									var start = parseInt(positions[0], 10);
									var total = stats.size;
									var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
									var chunksize = (end - start) + 1;
									this.response.writeHead(206, { 'Content-Range': 'bytes' + start + "-" + end + "/" + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': contentType });
									var stream = fs.createReadStream(file, { start: start, end: end })
										.on("open", () => {
											stream.pipe(this.response);
										}).on("error", (err: any) => {
											this.response.end(err);
										});
									if (onCompleted)
										onCompleted(206);
								}
								else {
									this.Write(data, 200, { 'Accept-Ranges': 'bytes', 'Content-Lenth': stats.size, 'Content-Type': contentType });
									if (onCompleted)
										onCompleted(200);
								}
							}
							else {
								this.Write(data, 200, { 'Content-Type': contentType });
								if (onCompleted)
									onCompleted(200);
							}
						}
						else {
							if (log) {
								this.Write("Moved Permanently", 301, { 'Content-Type': 'text/html' });
								if (onCompleted)
									onCompleted(301);
							}
							else {
								this.Write("Not Found", 404, { 'Content-Type': 'text/html' });
								if (onCompleted)
									onCompleted(404);
							}
						}
					});
				}
			});
		}
		/**Receives data from a client in this connection and creates a type of User object according to the received data.
		 * @param onCompleted the callback is passed one argument (result) type of User holds the created object of User.
		*/
		public Receive(onCompleted: (result: User) => void) {
			var result: User = null;
			this.ReceivingData((fullbody) => {
				if (fullbody) {
					var contents: string[] = [];
					var jsonUser = JSON.parse(fullbody);
					if ((jsonUser.company || jsonUser.Company) && (jsonUser.contact || jsonUser.Contact) && (jsonUser.crm || jsonUser.Crm)) {
						var theCompany: any;
						var theContact: any;
						var theCrm: any;
						(jsonUser.company) ? theCompany = jsonUser.company : theCompany = jsonUser.Company;
						(jsonUser.contact) ? theContact = jsonUser.contact : theContact = jsonUser.Contact;
						(jsonUser.crm) ? theCrm = jsonUser.crm : theCrm = jsonUser.Crm;
						result = new User(theCompany, theContact, theCrm);
{'fraction_non_alphanumeric': 0.1419293218720153, 'fraction_numerical': 0.010315186246418338, 'mean_word_length': 2.799346879535559, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '213419', 'n_tokens_mistral': 4191, 'n_tokens_neox': 3131, 'n_words': 928}