import { shellQuote } from "@shellbook/core"; import type { HostInfo } from "@shellbook/core"; /** Copy transport only: the script body is preserved byte for byte. */ export function sshCommand( host: HostInfo, script: string, sudo = false, ): string { if ( !Number.isInteger(host.port) || host.port < 1 || host.port > 65535 || !host.user || !host.address || /[\r\n\0]/.test(host.user + host.address) ) { throw new Error("Invalid SSH destination"); } let delimiter = "EOF"; const lines = script.split(/\r?\n/); while (lines.includes(delimiter)) delimiter += "_"; return `ssh -p ${host.port} -- ${shellQuote(`${host.user}@${host.address}`)} ${shellQuote(`${sudo ? "sudo -n " : ""}bash -euo pipefail -s`)} <<'${delimiter}'\n${script}${script.endsWith("\n") ? "" : "\n"}${delimiter}`; }