Skip to content
VaelJS Blog

vael:ssh

Synchronous SSH command execution and native SFTP (conn.sftp.*) with mandatory host-key verification.

Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes. Enforces explicit host-key pinning via hostKey or in-memory knownHosts, supports password, in-memory private-key, and explicit SSH agent authentication (including agentSocket for Unix sockets and Windows OpenSSH named pipes such as \\.\pipe\openssh-ssh-agent), and never reads user ~/.ssh/ configuration or identity files.

import ... from "vael:ssh"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: Always enabled in vaeljs-core; vael-ssh (default) in vaeljs-lite-core
Supports synchronous SSH command execution, native SFTP (conn.sftp), and agentSocket (Unix sockets and Windows OpenSSH named pipes) on both vjs and vjs-lite.
import { connect } from "vael:ssh";
const conn = connect({
host: "127.0.0.1",
port: 2222,
username: "deploy",
knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...",
agent: true,
});
try {
const res = conn.exec("uname -a");
console.log(res.status, res.stdout.byteLength);
const entries = conn.sftp.readDir("/tmp");
console.log(entries.length);
} finally {
conn.close();
}
API Symbols
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function connect(options: SshConnectOptions): SshConnection;

Synchronously dial, verify host identity, authenticate, and return an SshConnection.

Never reads ssh_config, identity files, or ~/.ssh/known_hosts.

Parameters
NameTypeDescription
optionsSshConnectOptionsConnection, mandatory host-trust, and authentication options.
Returns
SshConnection — Authenticated SshConnection handle.
Exceptions
  • TypeError — on invalid options or unknown option keys.
  • RangeError — on out-of-range port or timeout values.
  • Error — (name = "SshError") with code: SshErrorCode.
import { connect } from "vael:ssh";
const conn = connect({
host: "10.0.0.5",
username: "ops",
hostKey: new Uint8Array(32),
privateKey: new Uint8Array(64),
});
conn.close();
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface SshExecResult {
  stdout: Uint8Array;
  status: number;
}

Result returned by SshConnection.exec(command).

Properties
PropertyTypeDescriptionSince
stdout Uint8ArrayCaptured remote stdout bytes (at most MAX_OUTPUT_BYTES). Remote stderr is discarded.v0.0.0-alpha.0
status numberRemote exit status code.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface SftpStat {
  readonly size: number;
  readonly isFile: boolean;
  readonly isDir: boolean;
  readonly isSymlink: boolean;
  readonly permissions: number | null;
  readonly uid: number | null;
  readonly gid: number | null;
  readonly atime: number | null;
  readonly mtime: number | null;
}

Remote file or directory metadata returned by SshSftp.stat and SshSftp.readDir.

Properties
PropertyTypeDescriptionSince
size readonlynumberFile size in bytes (0 when omitted by the server).v0.0.0-alpha.2
isFile readonlybooleanWhether POSIX mode bits indicate a regular file.v0.0.0-alpha.2
isDir readonlybooleanWhether POSIX mode bits indicate a directory.v0.0.0-alpha.2
isSymlink readonlybooleanWhether POSIX mode bits indicate a symbolic link.v0.0.0-alpha.2
permissions readonlynumber | nullRaw POSIX st_mode permissions word when provided by the server, or null.v0.0.0-alpha.2
uid readonlynumber | nullOwning user ID when provided by the server, or null.v0.0.0-alpha.2
gid readonlynumber | nullOwning group ID when provided by the server, or null.v0.0.0-alpha.2
atime readonlynumber | nullLast access timestamp (Unix seconds) when provided by the server, or null.v0.0.0-alpha.2
mtime readonlynumber | nullLast modification timestamp (Unix seconds) when provided by the server, or null.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface SftpDirEntry {
  readonly name: string;
  readonly longName: string;
  readonly stat: SftpStat;
}

One directory entry returned by SshSftp.readDir.

Properties
PropertyTypeDescriptionSince
name readonlystringEntry name (excluding "." and "..").v0.0.0-alpha.2
longName readonlystringLong listing string supplied by the SFTP server.v0.0.0-alpha.2
stat readonlySftpStatEntry metadata attributes.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface SftpReadDirOptions {
  maxEntries?: number;
}

Options accepted by SshSftp.readDir.

Properties
PropertyTypeDescriptionSince
maxEntries optionalnumberMaximum number of directory entries to return (1..=100_000, default 100_000).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface SftpMkdirOptions {
  mode?: number;
}

Options accepted by SshSftp.mkdir.

Properties
PropertyTypeDescriptionSince
mode optionalnumberPOSIX permission bits (0..=0o7777) applied to the new directory.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface SftpUploadOptions {
  maxBytes?: number;
  overwrite?: boolean;
  mode?: number;
  followSymlinks?: boolean;
}

Options accepted by SshSftp.upload.

Properties
PropertyTypeDescriptionSince
maxBytes optionalnumberMaximum bytes allowed to transfer (1..=536_870_912, default 67_108_864).v0.0.0-alpha.2
overwrite optionalbooleanWhether an existing remote file may be overwritten (default true).v0.0.0-alpha.2
mode optionalnumberOptional POSIX permission bits (0..=0o7777) applied to the remote file.v0.0.0-alpha.2
followSymlinks optionalbooleanWhether symbolic links are followed on the local source path (default false).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface SftpDownloadOptions {
  maxBytes?: number;
  overwrite?: boolean;
}

Options accepted by SshSftp.download.

Properties
PropertyTypeDescriptionSince
maxBytes optionalnumberMaximum bytes allowed to transfer (1..=536_870_912, default 67_108_864).v0.0.0-alpha.2
overwrite optionalbooleanWhether an existing local destination file may be overwritten (default true).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface SftpTransferResult {
  readonly bytesTransferred: number;
}

Summary returned by SshSftp.upload and SshSftp.download.

Properties
PropertyTypeDescriptionSince
bytesTransferred readonlynumberTotal number of payload bytes transferred.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface SshSftp {
  stat(remotePath: string): SftpStat;
  readDir(remotePath: string, options?: SftpReadDirOptions): readonly SftpDirEntry[];
  mkdir(remotePath: string, options?: SftpMkdirOptions): void;
  remove(remotePath: string): void;
  rmdir(remotePath: string): void;
  upload(
      localPath: string,
      remotePath: string,
      options?: SftpUploadOptions,
    ): SftpTransferResult;
  download(
      remotePath: string,
      localPath: string,
      options?: SftpDownloadOptions,
    ): SftpTransferResult;
}

Synchronous SFTP v3 client scoped to an authenticated SshConnection.

Pre-flight validation errors and SFTP protocol status errors ("VAEL_SSH_SFTP") leave the parent SshConnection open. Fatal transport, session, or sftpTimeoutMs deadline errors close the connection.

Methods
methodSince v0.0.0-alpha.2
stat(remotePath: string): SftpStat;

Query metadata (following symbolic links) for a remote path.

Parameters
NameTypeDescription
remotePathstringRemote file or directory path (non-empty, max 4096 bytes, no NUL bytes).
Returns
SftpStat — Remote metadata attributes.
Exceptions
  • TypeError — on wrong argument count or non-string remotePath.
  • Error — (name = "SshError") with code "VAEL_SSH_CLOSED", "VAEL_SSH_ARGUMENT", "VAEL_SSH_SFTP", "VAEL_SSH_TIMEOUT", "VAEL_SSH_IO", "VAEL_SSH_SESSION", or "VAEL_SSH_INTERNAL".
methodSince v0.0.0-alpha.2
readDir(remotePath: string, options?: SftpReadDirOptions): readonly SftpDirEntry[];

List directory entries at remotePath (excluding "." and "..").

Parameters
NameTypeDescription
remotePathstringRemote directory path (non-empty, max 4096 bytes, no NUL bytes).
options optionalSftpReadDirOptionsOptional directory listing limits.
Returns
readonly SftpDirEntry[] — Array of directory entries and their metadata.
Exceptions
  • TypeError — on invalid arguments or unknown option keys.
  • RangeError — on out-of-range maxEntries.
  • Error — (name = "SshError") with code "VAEL_SSH_CLOSED", "VAEL_SSH_ARGUMENT", "VAEL_SSH_LIMIT", "VAEL_SSH_SFTP", "VAEL_SSH_TIMEOUT", "VAEL_SSH_IO", "VAEL_SSH_SESSION", or "VAEL_SSH_INTERNAL".
methodSince v0.0.0-alpha.2
mkdir(remotePath: string, options?: SftpMkdirOptions): void;

Create a remote directory at remotePath.

Parameters
NameTypeDescription
remotePathstringRemote directory path (non-empty, max 4096 bytes, no NUL bytes).
options optionalSftpMkdirOptionsOptional POSIX directory permissions.
Returns
void
Exceptions
  • TypeError — on invalid arguments or unknown option keys.
  • RangeError — on out-of-range mode.
  • Error — (name = "SshError") with code "VAEL_SSH_CLOSED", "VAEL_SSH_ARGUMENT", "VAEL_SSH_SFTP", "VAEL_SSH_TIMEOUT", "VAEL_SSH_IO", "VAEL_SSH_SESSION", or "VAEL_SSH_INTERNAL".
methodSince v0.0.0-alpha.2
remove(remotePath: string): void;

Remove a remote file or symbolic link at remotePath.

Parameters
NameTypeDescription
remotePathstringRemote file or symlink path (non-empty, max 4096 bytes, no NUL bytes).
Returns
void
Exceptions
  • TypeError — on wrong argument count or non-string remotePath.
  • Error — (name = "SshError") with code "VAEL_SSH_CLOSED", "VAEL_SSH_ARGUMENT", "VAEL_SSH_SFTP", "VAEL_SSH_TIMEOUT", "VAEL_SSH_IO", "VAEL_SSH_SESSION", or "VAEL_SSH_INTERNAL".
methodSince v0.0.0-alpha.2
rmdir(remotePath: string): void;

Remove an empty remote directory at remotePath.

Parameters
NameTypeDescription
remotePathstringRemote directory path (non-empty, max 4096 bytes, no NUL bytes).
Returns
void
Exceptions
  • TypeError — on wrong argument count or non-string remotePath.
  • Error — (name = "SshError") with code "VAEL_SSH_CLOSED", "VAEL_SSH_ARGUMENT", "VAEL_SSH_SFTP", "VAEL_SSH_TIMEOUT", "VAEL_SSH_IO", "VAEL_SSH_SESSION", or "VAEL_SSH_INTERNAL".
methodSince v0.0.0-alpha.2
upload(
  localPath: string,
  remotePath: string,
  options?: SftpUploadOptions,
): SftpTransferResult;

Upload a local file to remotePath over SFTP.

Parameters
NameTypeDescription
localPathstringLocal source file path.
remotePathstringRemote destination file path (non-empty, max 4096 bytes, no NUL bytes).
options optionalSftpUploadOptionsOptional transfer size, overwrite, mode, and symlink settings.
Returns
SftpTransferResult — Transfer summary containing bytesTransferred.
Exceptions
  • TypeError — on invalid arguments or unknown option keys.
  • RangeError — on out-of-range maxBytes or mode.
  • Error — (name = "SshError") with code "VAEL_SSH_CLOSED", "VAEL_SSH_ARGUMENT", "VAEL_SSH_LIMIT", "VAEL_SSH_SFTP", "VAEL_SSH_TIMEOUT", "VAEL_SSH_IO", "VAEL_SSH_SESSION", or "VAEL_SSH_INTERNAL".
methodSince v0.0.0-alpha.2
download(
  remotePath: string,
  localPath: string,
  options?: SftpDownloadOptions,
): SftpTransferResult;

Download a remote file at remotePath to localPath over SFTP.

Parameters
NameTypeDescription
remotePathstringRemote source file path (non-empty, max 4096 bytes, no NUL bytes).
localPathstringLocal destination file path.
options optionalSftpDownloadOptionsOptional transfer size and overwrite settings.
Returns
SftpTransferResult — Transfer summary containing bytesTransferred.
Exceptions
  • TypeError — on invalid arguments or unknown option keys.
  • RangeError — on out-of-range maxBytes.
  • Error — (name = "SshError") with code "VAEL_SSH_CLOSED", "VAEL_SSH_ARGUMENT", "VAEL_SSH_LIMIT", "VAEL_SSH_SFTP", "VAEL_SSH_TIMEOUT", "VAEL_SSH_IO", "VAEL_SSH_SESSION", or "VAEL_SSH_INTERNAL".
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface SshConnection {
  readonly closed: boolean;
  readonly sftp: SshSftp;
  exec(command: string | Uint8Array): SshExecResult;
  close(): void;
}

Synchronous, authenticated SSH command-execution and SFTP connection returned by connect(options).

Properties
PropertyTypeDescriptionSince
closed readonlybooleanWhether this connection is closed and can no longer run commands or SFTP operations.v0.0.0-alpha.0
sftp readonlySshSftpSynchronous SFTP v3 client bound to this connection.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.0
exec(command: string | Uint8Array): SshExecResult;

Synchronously execute a single command on the remote host.

Blocks until exit status arrives and the session channel closes, or until execTimeoutMs expires. Fatal transport/session/timeout/output-limit errors close the connection; pre-flight validation errors leave it open.

Parameters
NameTypeDescription
commandstring | Uint8ArrayUTF-8 string or raw bytes (max MAX_COMMAND_BYTES).
Returns
SshExecResult — { stdout, status } result from the remote command.
Exceptions
  • TypeError — on wrong argument type or arity.
  • Error — (name = "SshError") with code "VAEL_SSH_CLOSED", "VAEL_SSH_LIMIT", "VAEL_SSH_TIMEOUT", "VAEL_SSH_IO", "VAEL_SSH_SESSION", or "VAEL_SSH_INTERNAL".
import { connect } from "vael:ssh";
const conn = connect({
host: "127.0.0.1",
username: "deploy",
knownHosts: "127.0.0.1 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...",
password: "demo",
});
const res = conn.exec("id -u");
console.log(res.status);
conn.close();
methodSince v0.0.0-alpha.0
close(): void;

Close the underlying socket. Idempotent; does not send an SSH disconnect packet. Subsequent exec and sftp calls throw VAEL_SSH_CLOSED.

Returns
void
typeSince v0.0.0-alpha.0vjs & vjs-lite
export type SshErrorCode =
  | "VAEL_SSH_ARGUMENT"
  | "VAEL_SSH_HOST_KEY_REQUIRED"
  | "VAEL_SSH_HOST_KEY"
  | "VAEL_SSH_KNOWN_HOSTS"
  | "VAEL_SSH_AUTH_METHOD"
  | "VAEL_SSH_CREDENTIAL"
  | "VAEL_SSH_AGENT"
  | "VAEL_SSH_CONNECT"
  | "VAEL_SSH_TIMEOUT"
  | "VAEL_SSH_HANDSHAKE"
  | "VAEL_SSH_AUTH"
  | "VAEL_SSH_SESSION"
  | "VAEL_SSH_LIMIT"
  | "VAEL_SSH_CLOSED"
  | "VAEL_SSH_IO"
  /** SFTP protocol or remote filesystem error. @since 0.0.0-alpha.2 */
  | "VAEL_SSH_SFTP"
  | "VAEL_SSH_INTERNAL";

Error codes reported on SshError.code (name = "SshError").

"VAEL_SSH_SFTP" was added in 0.0.0-alpha.2.

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type SshHostTrustOptions =
  | {
      /** Explicit SSH-format Ed25519 host public-key blob. @since 0.0.0-alpha.0 */
      hostKey: Uint8Array;
      /** Must be omitted when `hostKey` is provided. @since 0.0.0-alpha.0 */
      knownHosts?: undefined;
    }
  | {
      /** Must be omitted when `knownHosts` is provided. @since 0.0.0-alpha.0 */
      hostKey?: undefined;
      /** In-memory `known_hosts` host-key records. @since 0.0.0-alpha.0 */
      knownHosts: string;
    };

Mandatory host-trust pin: supply exactly one of hostKey or knownHosts.

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type SshAuthOptions =
  | {
      /** Password credential. @since 0.0.0-alpha.0 */
      password: string;
      /** Must be omitted for password authentication. @since 0.0.0-alpha.0 */
      privateKey?: undefined;
      /** Must be omitted for password authentication. @since 0.0.0-alpha.0 */
      passphrase?: undefined;
      /** Must be omitted or `false` for password authentication. @since 0.0.0-alpha.0 */
      agent?: false;
      /** Must be omitted for password authentication. @since 0.0.0-alpha.2 */
      agentSocket?: undefined;
    }
  | {
      /**
       * In-memory unencrypted PKCS#8 DER/PEM or OpenSSH (`openssh-key-v1`/PEM)
       * private key bytes.
       *
       * @since 0.0.0-alpha.0
       */
      privateKey: Uint8Array;
      /** Optional passphrase for encrypted `openssh-key-v1` containers only. @since 0.0.0-alpha.0 */
      passphrase?: string;
      /** Must be omitted for key authentication. @since 0.0.0-alpha.0 */
      password?: undefined;
      /** Must be omitted or `false` for key authentication. @since 0.0.0-alpha.0 */
      agent?: false;
      /** Must be omitted for key authentication. @since 0.0.0-alpha.2 */
      agentSocket?: undefined;
    }
  | {
      /**
       * Explicit opt-in to authenticate via the platform SSH agent after
       * host-key verification (tries up to 16 identities).
       *
       * When `agentSocket` is omitted, uses `SSH_AUTH_SOCK` on Unix or the
       * Windows OpenSSH agent named pipe (`\\.\pipe\openssh-ssh-agent`).
       *
       * @since 0.0.0-alpha.0
       */
      agent: true;
      /**
       * Optional explicit Unix socket path or Windows OpenSSH named pipe
       * (`\\.\pipe\openssh-ssh-agent`), max 4096 bytes. Overrides
       * `SSH_AUTH_SOCK` on Unix and the default named pipe on Windows.
       *
       * @since 0.0.0-alpha.2
       */
      agentSocket?: string;
      /** Must be omitted for agent authentication. @since 0.0.0-alpha.0 */
      password?: undefined;
      /** Must be omitted for agent authentication. @since 0.0.0-alpha.0 */
      privateKey?: undefined;
      /** Must be omitted for agent authentication. @since 0.0.0-alpha.0 */
      passphrase?: undefined;
    }
  | {
      /**
       * Optional explicit opt-in to authenticate via the platform SSH agent
       * after host-key verification (tries up to 16 identities).
       *
       * When `agentSocket` is supplied, `agent` may be `true` or omitted;
       * `agent: false` is rejected.
       *
       * @since 0.0.0-alpha.0
       */
      agent?: true;
      /**
       * Explicit Unix socket path or Windows OpenSSH named pipe
       * (`\\.\pipe\openssh-ssh-agent`), max 4096 bytes. Overrides
       * `SSH_AUTH_SOCK` on Unix and the default named pipe on Windows.
       *
       * @since 0.0.0-alpha.2
       */
      agentSocket: string;
      /** Must be omitted for agent authentication. @since 0.0.0-alpha.0 */
      password?: undefined;
      /** Must be omitted for agent authentication. @since 0.0.0-alpha.0 */
      privateKey?: undefined;
      /** Must be omitted for agent authentication. @since 0.0.0-alpha.0 */
      passphrase?: undefined;
    };

Authentication method: supply exactly one of password, privateKey, or SSH agent (agent: true and/or agentSocket).

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type SshConnectOptions = {
  /** Remote hostname or IP address. @since 0.0.0-alpha.0 */
  host: string;
  /** Remote TCP port (`1..=65535`, default `22`). @since 0.0.0-alpha.0 */
  port?: number;
  /** SSH username (max 256 bytes). @since 0.0.0-alpha.0 */
  username: string;
  /** Connect/handshake inactivity timeout in ms (`1..=120_000`, default `15_000`). @since 0.0.0-alpha.0 */
  connectTimeoutMs?: number;
  /** Per-command execution timeout in ms (`1..=600_000`, default `30_000`). @since 0.0.0-alpha.0 */
  execTimeoutMs?: number;
  /** Per-operation SFTP timeout in ms (`1..=600_000`, default `60_000`). @since 0.0.0-alpha.2 */
  sftpTimeoutMs?: number;
} & SshHostTrustOptions &
  SshAuthOptions;

Options accepted by connect(options). Unknown keys throw TypeError.

constSince v0.0.0-alpha.0vjs & vjs-lite
export const MAX_COMMAND_BYTES: 65536;

Maximum command byte length accepted by SshConnection.exec (64 KiB).

constSince v0.0.0-alpha.0vjs & vjs-lite
export const MAX_OUTPUT_BYTES: 8388608;

Maximum captured stdout byte length returned by SshConnection.exec (8 MiB).

constSince v0.0.0-alpha.0vjs & vjs-lite
export const DEFAULT_CONNECT_TIMEOUT_MS: 15000;

Default TCP/handshake connect timeout in milliseconds (15_000).

constSince v0.0.0-alpha.0vjs & vjs-lite
export const DEFAULT_EXEC_TIMEOUT_MS: 30000;

Default per-command execution timeout in milliseconds (30_000).