<commit_before>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as http from 'http';
import * as url from 'url';
import * as fs from 'fs';
import * as net from 'net';
import { getPathFromAmdModule } from 'vs/base/common/amd';
import { assertIsDefined } from 'vs/base/common/types';

interface Deferred<T> {
	resolve: (result: T | Promise<T>) => void;
	reject: (reason: any) => void;
}

export function createTerminateServer(server: http.Server) {
	const sockets: Record<number, net.Socket> = {};
	let socketCount = 0;
	server.on('connection', socket => {
		const id = socketCount++;
		sockets[id] = socket;
		socket.on('close', () => {
			delete sockets[id];
		});
	});
	return async () => {
		const result = new Promise<Error | undefined>(resolve => server.close(resolve));
		for (const id in sockets) {
			sockets[id].destroy();
		}
		return result;
	};
}

export async function startServer(server: http.Server): Promise<string> {
	let portTimer: NodeJS.Timer;

	function cancelPortTimer() {
		clearTimeout(portTimer);
	}

	const port = new Promise<string>((resolve, reject) => {
		portTimer = setTimeout(() => {
			reject(new Error('Timeout waiting for port'));
		}, 5000);

		server.on('listening', () => {
			const address = server.address();
			if (typeof address === 'string') {
				resolve(address);
			} else {
				resolve(assertIsDefined(address).port.toString());
			}
		});

		server.on('error', err => {
			reject(err);
		});

		server.on('close', () => {
			reject(new Error('Closed'));
		});

		server.listen(0);
	});

	port.then(cancelPortTimer, cancelPortTimer);
	return port;
}

function sendFile(res: http.ServerResponse, filepath: string, contentType: string) {
	fs.readFile(filepath, (err, body) => {
		if (err) {
			console.error(err);<commit_msg>Return 404 if file is not found on local server<commit_after>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as http from 'http';
import * as url from 'url';
import * as fs from 'fs';
import * as net from 'net';
import { getPathFromAmdModule } from 'vs/base/common/amd';
import { assertIsDefined } from 'vs/base/common/types';

interface Deferred<T> {
	resolve: (result: T | Promise<T>) => void;
	reject: (reason: any) => void;
}

export function createTerminateServer(server: http.Server) {
	const sockets: Record<number, net.Socket> = {};
	let socketCount = 0;
	server.on('connection', socket => {
		const id = socketCount++;
		sockets[id] = socket;
		socket.on('close', () => {
			delete sockets[id];
		});
	});
	return async () => {
		const result = new Promise<Error | undefined>(resolve => server.close(resolve));
		for (const id in sockets) {
			sockets[id].destroy();
		}
		return result;
	};
}

export async function startServer(server: http.Server): Promise<string> {
	let portTimer: NodeJS.Timer;

	function cancelPortTimer() {
		clearTimeout(portTimer);
	}

	const port = new Promise<string>((resolve, reject) => {
		portTimer = setTimeout(() => {
			reject(new Error('Timeout waiting for port'));
		}, 5000);

		server.on('listening', () => {
			const address = server.address();
			if (typeof address === 'string') {
				resolve(address);
			} else {
				resolve(assertIsDefined(address).port.toString());
			}
		});

		server.on('error', err => {
			reject(err);
		});

		server.on('close', () => {
			reject(new Error('Closed'));
		});

		server.listen(0);
	});

	port.then(cancelPortTimer, cancelPortTimer);
	return port;
}

function sendFile(res: http.ServerResponse, filepath: string, contentType: string) {
	fs.readFile(filepath, (err, body) => {
		if (err) {
			console.error(err);
			res.writeHead(404);
			res.end();
{'fraction_non_alphanumeric': 0.23659976387249115, 'fraction_numerical': 0.004250295159386069, 'mean_word_length': 2.7486725663716816, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 39, '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': '2762527', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1230, 'n_words': 326}