"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const net = require("net");
class Net {
    constructor($errors) {
        this.$errors = $errors;
    }
    getFreePort() {
        return __awaiter(this, void 0, void 0, function* () {
            const server = net.createServer((sock) => { });
            return new Promise((resolve, reject) => {
                let isResolved = false;
                server.listen(0, () => {
                    const portUsed = server.address().port;
                    server.close();
                    if (!isResolved) {
                        isResolved = true;
                        resolve(portUsed);
                    }
                });
                server.on("error", (err) => {
                    if (!isResolved) {
                        isResolved = true;
                        reject(err);
                    }
                });
            });
        });
    }
    isPortAvailable(port) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => {
                let isResolved = false;
                const server = net.createServer();
                server.on("error", (err) => {
                    if (!isResolved) {
                        isResolved = true;
                        resolve(false);
                    }
                });
                server.once("close", () => {
                    if (!isResolved) {
                        isResolved = true;
                        resolve(true);
                    }
                });
                server.on("listening", (err) => {
                    if (err && !isResolved) {
                        isResolved = true;
                        resolve(true);
                    }
                    server.close();
                });
                server.listen(port, "localhost");
            });
        });
    }
    getAvailablePortInRange(startPort, endPort) {
        return __awaiter(this, void 0, void 0, function* () {
            endPort = endPort || 65534;
            while (!(yield this.isPortAvailable(startPort))) {
                startPort++;
                if (startPort > endPort) {
                    this.$errors.failWithoutHelp("Unable to find free local port.");
                }
            }
            return startPort;
        });
    }
}
exports.Net = Net;
$injector.register("net", Net);
