<commit_before>package thx.http.core;

import js.node.Http;
import js.node.Https;
import js.node.http.IncomingMessage;
import js.node.Buffer;
import haxe.io.Bytes;
import thx.Error;
using thx.nodejs.io.Buffers;
using thx.promise.Promise;
using thx.stream.Bus;
using thx.stream.Emitter;

class NodeJSRequest {
	public static function make(requestInfo : RequestInfo) : Promise<Response> {
		return Promise.create(function(resolve : Response -> Void, reject) {
			function callbackResponse(res : IncomingMessage) {
				resolve(new NodeJSResponse(res));
			}
			var url = requestInfo.url,
					req : js.node.http.ClientRequest = switch url.protocol {
						case "http":
							Http.request({
								hostname: url.hostName,
								port: url.port,
								method: (requestInfo.method : String),
								path: url.path,
								headers: requestInfo.headers.toObject()
								//auth: Basic authentication i.e. 'user:password' to compute an Authorization header.
							}, callbackResponse);
						case "https":
							Https.request({
								hostname: url.hostName,
								port: url.port,
								method: (requestInfo.method : String),
								path: url.path,
								headers: requestInfo.headers.toObject()
								//auth: Basic authentication i.e. 'user:password' to compute an Authorization header.
							}, callbackResponse);
						case other:
							throw 'unexpected protocol $other';
					};
			req.on("error", function(e) {
				trace("ERROR", e);
				reject(Error.fromDynamic(e));
			});
			req.end();
		});
	}
}

class NodeJSResponse extends thx.http.Response {
	var res : IncomingMessage;
	public function new(res : IncomingMessage) {
		this.res = res;
		headers = res.headers;
		var bus = new Bus();
		res.on("readable", function() {
			var buf : Buffer = res.read();
			if(buf == null)
				bus.end();
			else
				bus.pulse(buf.toBytes());
		});
		this.emitter = bus;
	}

	override function get_statusCode() return res.statusCode;
}<commit_msg>fixed nodejs implementation (was never terminating)<commit_after>package thx.http.core;

import js.node.Http;
import js.node.Https;
import js.node.http.IncomingMessage;
import js.node.Buffer;
import haxe.io.Bytes;
import thx.Error;
using thx.nodejs.io.Buffers;
using thx.promise.Promise;
using thx.stream.Bus;
using thx.stream.Emitter;

class NodeJSRequest {
	public static function make(requestInfo : RequestInfo) : Promise<Response> {
		return Promise.create(function(resolve : Response -> Void, reject) {
			function callbackResponse(res : IncomingMessage) {
				resolve(new NodeJSResponse(res));
			}
			var url = requestInfo.url,
					req : js.node.http.ClientRequest = switch url.protocol {
						case "http":
							Http.request({
								hostname: url.hostName,
								port: url.port,
								method: (requestInfo.method : String),
								path: url.path,
								headers: requestInfo.headers.toObject()
								//auth: Basic authentication i.e. 'user:password' to compute an Authorization header.
							}, callbackResponse);
						case "https":
							Https.request({
								hostname: url.hostName,
								port: url.port,
								method: (requestInfo.method : String),
								path: url.path,
								headers: requestInfo.headers.toObject()
								//auth: Basic authentication i.e. 'user:password' to compute an Authorization header.
							}, callbackResponse);
						case other:
							throw 'unexpected protocol $other';
					};
			req.on("error", function(e) {
				trace("ERROR", e);
				reject(Error.fromDynamic(e));
			});
			req.end();
		});
	}
}

class NodeJSResponse extends thx.http.Response {
	var res : IncomingMessage;
	public function new(res : IncomingMessage) {
		this.res = res;
		headers = res.headers;
		var bus = new Bus();
		res.on("readable", function() {
			var buf : Buffer = res.read();
			if(buf != null)
				bus.pulse(buf.toBytes());
		});
		res.on("end", function() {
			bus.end();
		});
		this.emitter = bus;
	}

	override function get_statusCode() return res.statusCode;
}
{'fraction_non_alphanumeric': 0.1297883064516129, 'fraction_numerical': 0.0, 'mean_word_length': 3.058282208588957, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 7, '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': '6065694', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1225, 'n_words': 298}