import net from 'net';
import { isTest } from '../env.js';
// Inspired by https://github.com/kessler/find-port/blob/master/lib/findPort.js
async function isAvailable(port) {
    return new Promise((accept, reject) => {
        const server = net.createServer().listen(port);
        const timeoutRef = setTimeout(() => {
            accept(false);
        }, 2000);
        timeoutRef.unref();
        server.once('listening', () => {
            clearTimeout(timeoutRef);
            server.close();
            accept(true);
        });
        server.once('error', (err) => {
            clearTimeout(timeoutRef);
            if (err.code === 'EADDRINUSE') {
                accept(false);
                return;
            }
            reject(err);
        });
    });
}
async function findPort(start) {
    for (let p = start; p < start + 1000; p += 1) {
        if (await isAvailable(p)) {
            return p;
        }
    }
    return 0;
}
async function getEphemeralPort() {
    return new Promise((resolve, reject) => {
        const server = net.createServer();
        server.listen(0, () => {
            const address = server.address();
            if (typeof address === 'string' || !address) {
                reject(new Error('Invalid address'));
                return;
            }
            const port = address.port; // Retrieve the ephemeral port
            server.close((err) => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(port);
                }
            });
        });
    });
}
export async function getAvailablePort(basePort) {
    return isTest() || process.env.TEST_RUNNER ? getEphemeralPort() : findPort(basePort);
}
//# sourceMappingURL=port-finder.js.map