<commit_before>import * as http from 'http';
import * as https from 'https';

export default {
	test: (uri: string): boolean => /^https*:\/\//.test(uri),

	read: (url: string): Promise<http.IncomingMessage> => {
		return new Promise((resolve, reject) => {
			const get = url.indexOf('https') === 0 ? https.get : http.get;

			// @ts-ignore
			get(url, (res) => {
				const statusCode = res['statusCode'];
				const contentType = res['headers']['content-type'];

				if (statusCode === 200 && contentType.indexOf('audio/mpeg') > -1) {
					resolve(res);
				} else {
					reject({
						statusCode: statusCode,
						contentType: contentType
					});
				}
			});
		});
	}
}<commit_msg>Fix uncaught exception in request<commit_after>import * as http from 'http';
import * as https from 'https';

export default {
	test: (uri: string): boolean => /^https*:\/\//.test(uri),

	read: (url: string): Promise<http.IncomingMessage> => {
		return new Promise((resolve, reject) => {
			const get = url.indexOf('https') === 0 ? https.get : http.get;

			let res_ = null;

			const onError = (err) => {
				if (res_) {
					res_.emit('error', err);
				}
			};

			const clientRequest = get(url, (res) => {
				const statusCode = res['statusCode'];
				const contentType = res['headers']['content-type'];

				if (statusCode === 200 && contentType.indexOf('audio/mpeg') > -1) {
					const onEnd = () => {
						clientRequest.off('error', onError);
						res.off('end', onEnd);
					};

					res.on('end', onEnd);
					res_ = res;

					resolve(res);
				} else {
					reject({
						statusCode: statusCode,
						contentType: contentType
					});
				}
			});

			clientRequest.on('error', onError);
		});
	},
};
{'fraction_non_alphanumeric': 0.18705882352941178, 'fraction_numerical': 0.0058823529411764705, 'mean_word_length': 2.8835616438356166, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2713169', 'n_tokens_mistral': 705, 'n_tokens_neox': 548, 'n_words': 131}