"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.getControlPath = getControlPath;
/**
 * SSH Connection Pool - Stateless
 *
 * Generates deterministic ControlPath from SSH config to enable connection
 * multiplexing across SSHRuntime instances targeting the same host.
 *
 * Design:
 * - Pure function: same config → same controlPath
 * - No state: filesystem is the state
 * - No cleanup: ControlPersist + OS handle it
 */
const crypto = __importStar(require("crypto"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
/**
 * Get deterministic controlPath for SSH config.
 * Multiple calls with identical config return the same path,
 * enabling ControlMaster to multiplex connections.
 *
 * Socket files are created by SSH and cleaned up automatically:
 * - ControlPersist=60: Removes socket 60s after last use
 * - OS: Cleans /tmp on reboot
 *
 * Includes local username in hash to prevent cross-user collisions on
 * multi-user systems (different users connecting to same remote would
 * otherwise generate same socket path, causing permission errors).
 */
function getControlPath(config) {
    const key = makeConnectionKey(config);
    const hash = hashKey(key);
    return path.join(os.tmpdir(), `cmux-ssh-${hash}`);
}
/**
 * Generate stable key from config.
 * Identical configs produce identical keys.
 * Includes local username to prevent cross-user socket collisions.
 */
function makeConnectionKey(config) {
    const parts = [
        os.userInfo().username, // Include local user to prevent cross-user collisions
        config.host,
        config.port?.toString() ?? "22",
        config.srcBaseDir,
        config.identityFile ?? "default",
    ];
    return parts.join(":");
}
/**
 * Generate deterministic hash for controlPath naming.
 * Uses first 12 chars of SHA-256 for human-readable uniqueness.
 */
function hashKey(key) {
    return crypto.createHash("sha256").update(key).digest("hex").substring(0, 12);
}
//# sourceMappingURL=sshConnectionPool.js.map