"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.containsAddress = containsAddress;
exports.checkPortListening = checkPortListening;
const util_1 = require("../../util");
const node_util_1 = require("node:util");
const CP = __importStar(require("node:child_process"));
const cpExec = (0, node_util_1.promisify)(CP.exec);
function containsAddress(x, port, address) {
    const readPorts = x
        .split('\n')
        .filter(Boolean)
        .splice(1)
        .map((x) => x.split(' ').filter(Boolean)[1]?.split(':'))
        .filter((x) => x?.length > 1)
        .map(([addr, p]) => [BigInt(`0x${addr}`), Number.parseInt(p, 16)]);
    return !!readPorts.find(([addr, p]) => (address === undefined || address === addr) && port === p);
}
/**
 * This is used to check if a port is listening on the system.
 * Used during the health check fn or the check main fn.
 */
async function checkPortListening(effects, port, options) {
    return Promise.race([
        Promise.resolve().then(async () => {
            const hasAddress = containsAddress(await cpExec(`cat /proc/net/tcp`, {}).then(util_1.stringFromStdErrOut), port) ||
                containsAddress(await cpExec(`cat /proc/net/tcp6`, {}).then(util_1.stringFromStdErrOut), port, BigInt(0)) ||
                containsAddress(await cpExec('cat /proc/net/udp', {}).then(util_1.stringFromStdErrOut), port) ||
                containsAddress(await cpExec('cat /proc/net/udp6', {}).then(util_1.stringFromStdErrOut), port, BigInt(0));
            if (hasAddress) {
                return { result: 'success', message: options.successMessage };
            }
            return {
                result: 'failure',
                message: options.errorMessage,
            };
        }),
        new Promise((resolve) => {
            setTimeout(() => resolve({
                result: 'failure',
                message: options.timeoutMessage || `Timeout trying to check port ${port}`,
            }), options.timeout ?? 1_000);
        }),
    ]);
}
//# sourceMappingURL=checkPortListening.js.map