Skip to content
VaelJS Blog

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).

connect() never reads ~/.ssh/known_hosts or ~/.ssh/config. You must supply exactly one of:

  • knownHosts: An in-memory string of known_hosts records (commonly loaded explicitly from a repository-pinned file using readTextFile from vael:fs).
  • hostKey: An explicit SSH-format Ed25519 host public-key blob as a Uint8Array.

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_SOCK on 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: true is optional when agentSocket is set; agent: false is rejected).
  • Verified before querying identities: Host-key verification completes before vael:ssh contacts 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();
}
  • Always close in finally: close() is idempotent; subsequent exec() or sftp calls throw VAEL_SSH_CLOSED.
  • Each exec() opens a fresh non-interactive channel: Shell state such as cd /srv/app or export FOO=bar does not persist across calls.
  • Explicit timeouts & bounds: connectTimeoutMs defaults to 15_000 ms (1..=120_000), execTimeoutMs defaults to 30_000 ms (1..=600_000), and sftpTimeoutMs defaults to 60_000 ms (1..=600_000).
  • Command & stdout caps: Commands are capped at MAX_COMMAND_BYTES (64 KiB) and captured stdout at MAX_OUTPUT_BYTES (8 MiB). Remote stderr is discarded by the exec channel; check res.status !== 0 (or redirect 2>&1 when needed).

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.