SSH Automation, Agents & SFTP (vael:ssh)
vael:ssh provides synchronous SSH-2 command execution and native SFTP v3 (conn.sftp) across both vjs and vjs-lite without spawning a shell or reading implicit user config files (~/.ssh/config or ~/.ssh/known_hosts).
1. Mandatory Host-Key Pinning
Section titled “1. Mandatory Host-Key Pinning”connect() never reads ~/.ssh/known_hosts or ~/.ssh/config. You must supply exactly one of:
knownHosts: An in-memory string ofknown_hostsrecords (commonly loaded explicitly from a repository-pinned file usingreadTextFilefromvael:fs).hostKey: An explicit SSH-format Ed25519 host public-key blob as aUint8Array.
Omitting both throws VAEL_SSH_HOST_KEY_REQUIRED, and providing both throws TypeError.
import { readTextFile } from "vael:fs";import { connect } from "vael:ssh";
const conn = connect({ host: "app-01.internal", port: 22, username: "deploy", knownHosts: readTextFile("./ops/ssh/known_hosts", { maxBytes: 65_536 }), agent: true,});
try { const res = conn.exec("uname -srm"); console.log("Exit status:", res.status);} finally { conn.close();}2. Authentication: SSH Agents (agent / agentSocket), Keys & vael:age
Section titled “2. Authentication: SSH Agents (agent / agentSocket), Keys & vael:age”connect() requires exactly one authentication method: SSH agent (agent: true and/or agentSocket), privateKey (with optional passphrase), or password.
Unix ssh-agent & Windows OpenSSH Named Pipe (agent / agentSocket)
Section titled “Unix ssh-agent & Windows OpenSSH Named Pipe (agent / agentSocket)”vael:ssh supports SSH agent authentication on both Unix and Windows:
- Default agent discovery (
agent: true): Uses$SSH_AUTH_SOCKon Unix, or the Windows OpenSSH agent named pipe (\\.\pipe\openssh-ssh-agent) on Windows. - Explicit socket / pipe override (
agentSocket): Pass a Unix domain socket path or Windows named pipe (max 4096 bytes) to target a specific agent socket without mutating process environment variables (agent: trueis optional whenagentSocketis set;agent: falseis rejected). - Verified before querying identities: Host-key verification completes before
vael:sshcontacts the agent and tries up to 16 identities (and never forwards your local agent socket to the remote host).
import { readTextFile } from "vael:fs";import { connect } from "vael:ssh";
const conn = connect({ host: "app-01.internal", username: "deploy", knownHosts: readTextFile("./ops/ssh/known_hosts", { maxBytes: 65_536 }), // Explicit Unix socket or Windows named pipe (e.g. "\\\\.\\pipe\\openssh-ssh-agent") agent: true, agentSocket: "/run/user/1000/openssh_agent",});conn.close();Private Key Files (privateKey) & vael:age Custody
Section titled “Private Key Files (privateKey) & vael:age Custody”For CI runners or dedicated deploy keys, read the key bytes explicitly with readFile (vael:fs). In vjs, you can also store the private key encrypted with vael:age and decrypt it in memory right before dialing:
import { decrypt } from "vael:age";import { readFile, readTextFile } from "vael:fs";import { connect } from "vael:ssh";import { env } from "node:process";
const ageIdentity = env.VAEL_AGE_IDENTITY;if (!ageIdentity) { throw new Error("VAEL_AGE_IDENTITY is required");}
const privateKey = decrypt( ageIdentity, readFile("./ops/secrets/deploy_ed25519.age", { maxBytes: 65_536 }),);
const conn = connect({ host: "app-01.internal", username: "deploy", knownHosts: readTextFile("./ops/ssh/known_hosts", { maxBytes: 65_536 }), privateKey,});
try { console.log(conn.exec("systemctl is-active myapp").status);} finally { conn.close();}3. Managing SshConnection Sessions
Section titled “3. Managing SshConnection Sessions”- Always close in
finally:close()is idempotent; subsequentexec()orsftpcalls throwVAEL_SSH_CLOSED. - Each
exec()opens a fresh non-interactive channel: Shell state such ascd /srv/apporexport FOO=bardoes not persist across calls. - Explicit timeouts & bounds:
connectTimeoutMsdefaults to15_000ms (1..=120_000),execTimeoutMsdefaults to30_000ms (1..=600_000), andsftpTimeoutMsdefaults to60_000ms (1..=600_000). - Command & stdout caps: Commands are capped at
MAX_COMMAND_BYTES(64 KiB) and captured stdout atMAX_OUTPUT_BYTES(8 MiB). Remotestderris discarded by the exec channel; checkres.status !== 0(or redirect2>&1when needed).
4. Native SFTP (conn.sftp)
Section titled “4. Native SFTP (conn.sftp)”conn.sftp exposes a synchronous SFTP v3 client scoped to the authenticated SshConnection across both vjs and vjs-lite:
| Method | Purpose & Options |
|---|---|
conn.sftp.stat(remotePath) |
Query metadata (size, isFile, isDir, isSymlink, permissions, uid, gid, atime, mtime), following symlinks. |
conn.sftp.readDir(remotePath, { maxEntries? }) |
List entries (name, longName, stat), excluding . and .. (maxEntries default 100_000). |
conn.sftp.mkdir(remotePath, { mode? }) |
Create a remote directory with optional POSIX mode (0..=0o7777). |
conn.sftp.remove(remotePath) |
Remove a remote file or symlink. |
conn.sftp.rmdir(remotePath) |
Remove an empty remote directory. |
conn.sftp.upload(localPath, remotePath, opts?) |
Stream local file (maxBytes default 64 MiB, max 512 MiB; overwrite default true; mode; followSymlinks default false). Returns { bytesTransferred }. |
conn.sftp.download(remotePath, localPath, opts?) |
Stream remote file to a temp file and atomically commit to localPath (maxBytes default 64 MiB, max 512 MiB; overwrite default true). Returns { bytesTransferred }. |
Pre-flight validation errors and SFTP protocol status errors ("VAEL_SSH_SFTP") leave conn open; fatal transport, session, or sftpTimeoutMs errors close conn.
import { readTextFile } from "vael:fs";import { connect } from "vael:ssh";
const conn = connect({ host: "app-01.internal", username: "deploy", knownHosts: readTextFile("./ops/ssh/known_hosts", { maxBytes: 65_536 }), agent: true, sftpTimeoutMs: 120_000,});
try { // Ensure remote release directory exists const releaseDir = "/srv/app/releases/2026-11-29"; try { conn.sftp.stat(releaseDir); } catch { conn.sftp.mkdir(releaseDir, { mode: 0o755 }); }
// Stream upload with explicit byte bound & mode const up = conn.sftp.upload("./dist/server.tar.gz", `${releaseDir}/server.tar.gz`, { overwrite: true, mode: 0o644, maxBytes: 128 * 1024 * 1024, }); console.log("Uploaded bytes:", up.bytesTransferred);
// List remote directory entries for (const entry of conn.sftp.readDir(releaseDir, { maxEntries: 100 })) { console.log(entry.name, entry.stat.size, entry.stat.isFile); }
// Atomic streaming download const dl = conn.sftp.download(`${releaseDir}/server.tar.gz`, "./tmp/server.tar.gz", { overwrite: true, }); console.log("Downloaded bytes:", dl.bytesTransferred);} finally { conn.close();}See the full vael:ssh API Reference for all SshErrorCode values and options.
