File size: 31,339 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 | import { readFile } from 'node:fs/promises';
import { isAbsolute, join, normalize, resolve } from 'pathe';
import type { Readable, Writable } from 'node:stream';
import * as ssh2 from 'ssh2';
import type {
AnyAuthMethod,
Client,
ClientChannel,
ConnectConfig,
SFTPWrapper,
Stats as SFTPStats,
} from 'ssh2';
import type { Environment } from './environment';
import { KaosError, KaosFileExistsError, KaosValueError } from './errors';
import { BufferedReadable, decodeTextWithErrors, globPatternToRegex } from './internal';
import type { Kaos } from './kaos';
import type { KaosProcess } from './process';
import type { StatResult } from './types';
// ββ stat mode constants ββββββββββββββββββββββββββββββββββββββββββββββββ
const S_IFMT = 0o170000;
const S_IFREG = 0o100000;
const S_IFDIR = 0o040000;
const S_IFLNK = 0o120000;
const S_IFSOCK = 0o140000;
const S_IFCHR = 0o020000;
const S_IFBLK = 0o060000;
const S_IFIFO = 0o010000;
const DEFAULT_SFTP_STATUS_CODE = {
BAD_MESSAGE: 5,
CONNECTION_LOST: 7,
FAILURE: 4,
NO_CONNECTION: 6,
NO_SUCH_FILE: 2,
OP_UNSUPPORTED: 8,
PERMISSION_DENIED: 3,
} as const;
// ββ SSH options ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Advanced ssh2 connect options that may be passed through `SSHKaosOptions.extraOptions`.
*
* Excludes fields that SSHKaos manages itself (`host`, `port`, `username`,
* `password`, `privateKey`, `authHandler`, `hostVerifier`) β those are derived
* from the top-level `SSHKaosOptions` fields and cannot be overridden here.
*/
export type SSHKaosExtraOptions = Omit<
ConnectConfig,
'host' | 'port' | 'username' | 'password' | 'privateKey' | 'authHandler' | 'hostVerifier'
>;
export interface SSHKaosOptions {
host: string;
port?: number;
username: string;
password?: string;
keyPaths?: string[];
keyContents?: string[];
cwd?: string;
/**
* Pass-through for advanced ssh2 `ConnectConfig` fields such as `algorithms`,
* `keepaliveInterval`, `readyTimeout`, `debug`, `tryKeyboard`, `agent`, etc.
*
* Managed fields (`host`, `port`, `username`, `password`, `privateKey`,
* `authHandler`, `hostVerifier`) are excluded from this type and will take
* precedence over anything set here.
*/
extraOptions?: SSHKaosExtraOptions;
}
// ββ SSH error types βββββββββββββββββββββββββββββββββββββββββββββββββββ
export class KaosSSHError extends KaosError {
readonly code: number | undefined;
constructor(message: string, code?: number) {
super(message);
this.name = 'KaosSSHError';
this.code = code;
}
}
export class KaosFileNotFoundError extends KaosSSHError {
constructor(message: string, code?: number) {
super(message, code);
this.name = 'KaosFileNotFoundError';
}
}
export class KaosPermissionError extends KaosSSHError {
constructor(message: string, code?: number) {
super(message, code);
this.name = 'KaosPermissionError';
}
}
export class KaosConnectionError extends KaosSSHError {
constructor(message: string, code?: number) {
super(message, code);
this.name = 'KaosConnectionError';
}
}
// ββ shell quoting ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Shell-escape a single argument (POSIX sh compatible).
* Mirrors Python's shlex.quote().
*/
function shellQuote(arg: string): string {
if (arg === '') return "''";
// If the string is safe (only contains safe chars), return as-is
if (/^[A-Za-z0-9_./:=@%^,+-]+$/.test(arg)) return arg;
// Otherwise wrap in single quotes, escaping any embedded single quotes
return "'" + arg.replaceAll("'", "'\"'\"'") + "'";
}
// ββ stat mode builder ββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Build a POSIX st_mode from SFTP Stats.
* ssh2's Stats has .mode which already includes both file-type bits and
* permission bits, but we also check the boolean helpers as a fallback.
*/
function buildStMode(attrs: SFTPStats): number {
const raw = attrs.mode;
// If mode already contains file-type bits, return as-is
if ((raw & S_IFMT) !== 0) return raw;
// Derive file-type bits from the is* helpers
let typeBits = 0;
if (attrs.isDirectory()) typeBits = S_IFDIR;
else if (attrs.isFile()) typeBits = S_IFREG;
else if (attrs.isSymbolicLink()) typeBits = S_IFLNK;
else if (attrs.isSocket()) typeBits = S_IFSOCK;
else if (attrs.isCharacterDevice()) typeBits = S_IFCHR;
else if (attrs.isBlockDevice()) typeBits = S_IFBLK;
else if (attrs.isFIFO()) typeBits = S_IFIFO;
return (raw & ~S_IFMT) | typeBits;
}
function getSftpStatusCode(): typeof DEFAULT_SFTP_STATUS_CODE {
return {
...DEFAULT_SFTP_STATUS_CODE,
...ssh2.utils?.sftp?.STATUS_CODE,
};
}
function getErrorCode(error: unknown): number | undefined {
if (typeof error !== 'object' || error === null) {
return undefined;
}
const { code } = error as { code?: unknown };
return typeof code === 'number' ? code : undefined;
}
function getErrorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message;
}
return String(error);
}
function mapSftpError(operation: string, error: unknown): KaosSSHError {
const code = getErrorCode(error);
const message = `${operation} failed: ${getErrorMessage(error)}`;
const statusCode = getSftpStatusCode();
if (code === statusCode.NO_SUCH_FILE) {
return new KaosFileNotFoundError(message, code);
}
if (code === statusCode.PERMISSION_DENIED) {
return new KaosPermissionError(message, code);
}
if (code === statusCode.NO_CONNECTION || code === statusCode.CONNECTION_LOST) {
return new KaosConnectionError(message, code);
}
return new KaosSSHError(message, code);
}
function buildAuthHandler(
username: string,
privateKeys: readonly (Buffer | string)[],
password?: string,
): ConnectConfig['authHandler'] {
const authQueue: AnyAuthMethod[] = privateKeys.map((key) => ({
key,
type: 'publickey',
username,
}));
if (password !== undefined) {
authQueue.push({
password,
type: 'password',
username,
});
}
let index = 0;
return (_authsLeft, _partialSuccess, next) => {
const nextAuth = authQueue[index];
index += 1;
const nextWithFalse = next as (auth: AnyAuthMethod | false) => void;
nextWithFalse(nextAuth ?? false);
};
}
// ββ SSH process ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/** Exported for unit tests only. Do not use directly. */
export class SSHProcess implements KaosProcess {
readonly stdin: Writable;
readonly stdout: Readable;
readonly stderr: Readable;
readonly pid: number = -1;
private _exitCode: number | null = null;
private readonly _exitPromise: Promise<number>;
private readonly _channel: ClientChannel;
private _disposed = false;
constructor(channel: ClientChannel) {
this._channel = channel;
this.stdin = channel;
this.stdout = new BufferedReadable(channel as unknown as Readable);
this.stderr = new BufferedReadable(channel.stderr);
this._exitPromise = new Promise<number>((resolve) => {
// Listen to 'close' on the channel, not 'exit', to ensure all
// buffered output is flushed before we resolve.
channel.on('close', (code: number | null) => {
// Some ssh2 backends surface the exit status only on 'close'.
this._exitCode ??= code ?? 1;
resolve(this._exitCode);
});
channel.on('exit', (code: number | null) => {
this._exitCode = code ?? 1;
});
});
}
get exitCode(): number | null {
return this._exitCode;
}
async wait(): Promise<number> {
return this._exitPromise;
}
kill(signal?: NodeJS.Signals): Promise<void> {
// SSH signals must be stripped of the "SIG" prefix (RFC 4254 Β§6.9):
// e.g. 'SIGTERM' β 'TERM', 'SIGKILL' β 'KILL', 'SIGINT' β 'INT'.
// Honor the caller's requested signal so that remote processes can
// perform graceful shutdown on SIGTERM/SIGINT.
const rawSignal = signal ?? 'SIGTERM';
const sshSignal = rawSignal.startsWith('SIG') ? rawSignal.slice(3) : rawSignal;
this._channel.signal(sshSignal);
return Promise.resolve();
}
dispose(): void {
if (this._disposed) return;
this._disposed = true;
this.stdin.destroy();
this.stdout.destroy();
this.stderr.destroy();
}
}
// ββ Promisified SSH helpers ββββββββββββββββββββββββββββββββββββββββββββ
function connectClient(config: ConnectConfig): Promise<Client> {
const client = new ssh2.Client();
return new Promise<Client>((resolve, reject) => {
client.on('ready', () => {
resolve(client);
});
client.on('error', (err: Error) => {
reject(err);
});
client.connect(config);
});
}
function getSftp(client: Client): Promise<SFTPWrapper> {
return new Promise<SFTPWrapper>((resolve, reject) => {
client.sftp((err, sftp) => {
if (err) {
reject(err);
} else {
resolve(sftp);
}
});
});
}
// Every promisified SFTP helper funnels rejections through `mapSftpError` so
// callers see a KaosSSHError subclass (KaosFileNotFoundError / KaosPermissionError /
// KaosConnectionError / generic KaosSSHError) instead of the raw ssh2 error.
// The operation label is the underlying SFTP RPC name β it shows up in the
// error message for debugging and is the same label used by `stat()` before
// this was hoisted into the helpers.
function sftpRealpath(sftp: SFTPWrapper, path: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
sftp.realpath(path, (err, absPath) => {
if (err) {
reject(mapSftpError('realpath', err));
} else {
resolve(absPath);
}
});
});
}
function sftpStat(sftp: SFTPWrapper, path: string): Promise<SFTPStats> {
return new Promise<SFTPStats>((resolve, reject) => {
sftp.stat(path, (err, stats) => {
if (err) {
reject(mapSftpError('stat', err));
} else {
resolve(stats);
}
});
});
}
function sftpLstat(sftp: SFTPWrapper, path: string): Promise<SFTPStats> {
return new Promise<SFTPStats>((resolve, reject) => {
sftp.lstat(path, (err, stats) => {
if (err) {
reject(mapSftpError('lstat', err));
} else {
resolve(stats);
}
});
});
}
interface SFTPFileEntry {
filename: string;
attrs: SFTPStats;
}
function sftpReaddir(sftp: SFTPWrapper, path: string): Promise<SFTPFileEntry[]> {
return new Promise<SFTPFileEntry[]>((resolve, reject) => {
sftp.readdir(path, (err, list) => {
if (err) {
reject(mapSftpError('readdir', err));
} else {
resolve(list as SFTPFileEntry[]);
}
});
});
}
function sftpMkdir(sftp: SFTPWrapper, path: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
sftp.mkdir(path, (err) => {
if (err) {
reject(mapSftpError('mkdir', err));
} else {
resolve();
}
});
});
}
function sftpExists(sftp: SFTPWrapper, path: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
sftp.exists(path, (exists) => {
resolve(exists);
});
});
}
function sftpReadFile(sftp: SFTPWrapper, path: string): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
sftp.readFile(path, (err, data) => {
if (err) {
reject(mapSftpError('readFile', err));
} else {
resolve(data);
}
});
});
}
function sftpWriteFile(sftp: SFTPWrapper, path: string, data: string | Buffer): Promise<void> {
return new Promise<void>((resolve, reject) => {
sftp.writeFile(path, data, (err) => {
if (err) {
reject(mapSftpError('writeFile', err));
} else {
resolve();
}
});
});
}
function sftpAppendFile(sftp: SFTPWrapper, path: string, data: string | Buffer): Promise<void> {
return new Promise<void>((resolve, reject) => {
sftp.appendFile(path, data, (err) => {
if (err) {
reject(mapSftpError('appendFile', err));
} else {
resolve();
}
});
});
}
function clientExec(client: Client, command: string): Promise<ClientChannel> {
return new Promise<ClientChannel>((resolve, reject) => {
client.exec(command, (err: Error | undefined, channel: ClientChannel) => {
if (err) {
reject(err);
} else {
resolve(channel);
}
});
});
}
// ββ SSHKaos ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* A KAOS implementation that interacts with a remote machine via SSH and SFTP.
*/
export class SSHKaos implements Kaos {
readonly name: string = 'ssh';
private _client: Client;
private _sftp: SFTPWrapper;
private _home: string;
private _cwd: string;
private readonly _envLayers: readonly Record<string, string>[];
// Stub: real wiring (probing the remote host via `uname` / `$SHELL` over the
// SSH transport) is deferred.
get osEnv(): Environment {
throw new KaosError(
'SSHKaos.osEnv is not yet wired β remote environment probing is not implemented.',
);
}
private constructor(
client: Client,
sftp: SFTPWrapper,
home: string,
cwd: string,
envLayers: readonly Record<string, string>[] = [],
) {
this._client = client;
this._sftp = sftp;
this._home = home;
this._cwd = cwd;
this._envLayers = envLayers;
}
withCwd(cwd: string): SSHKaos {
return new SSHKaos(this._client, this._sftp, this._home, cwd, this._envLayers);
}
withEnv(env: Record<string, string>): SSHKaos {
return new SSHKaos(this._client, this._sftp, this._home, this._cwd, [...this._envLayers, env]);
}
private _resolvePath(path: string): string {
if (isAbsolute(path)) return path;
return join(this._cwd, path);
}
/**
* Factory method to create an SSHKaos instance.
* Establishes the SSH connection and SFTP session.
*/
static async create(options: SSHKaosOptions): Promise<SSHKaos> {
// Start from extraOptions (advanced ssh2 options) so our managed fields
// below take precedence.
const config: ConnectConfig = {
...options.extraOptions,
host: options.host,
port: options.port ?? 22,
username: options.username,
};
if (options.password !== undefined) {
config.password = options.password;
}
// Build private keys from keyContents and keyPaths
const privateKeys: (Buffer | string)[] = [];
if (options.keyContents) {
for (const content of options.keyContents) {
privateKeys.push(content);
}
}
if (options.keyPaths) {
const keyPromises = options.keyPaths.map((keyPath) => readFile(keyPath, 'utf-8'));
const keyData = await Promise.all(keyPromises);
for (const key of keyData) {
privateKeys.push(key);
}
}
if (privateKeys.length > 0) {
const authHandler = buildAuthHandler(options.username, privateKeys, options.password);
if (authHandler !== undefined) {
config.authHandler = authHandler;
}
}
// Disable host key verification (like asyncssh known_hosts=None)
config.hostVerifier = () => true;
const client = await connectClient(config);
try {
const sftp = await getSftp(client);
// Determine home and cwd
const home = await sftpRealpath(sftp, '.');
let cwd: string;
if (options.cwd === undefined) {
cwd = home;
} else {
cwd = await sftpRealpath(sftp, options.cwd);
const attrs = await sftpStat(sftp, cwd);
if (!attrs.isDirectory()) {
throw new KaosValueError(`${cwd} is not a directory`);
}
}
return new SSHKaos(client, sftp, home, cwd);
} catch (error) {
client.end();
throw error;
}
}
// ββ Path operations (sync) βββββββββββββββββββββββββββββββββββββββββ
pathClass(): 'posix' | 'win32' {
return 'posix';
}
normpath(path: string): string {
return normalize(path);
}
gethome(): string {
return this._home;
}
getcwd(): string {
return this._cwd;
}
// ββ Directory operations (async) βββββββββββββββββββββββββββββββββββ
async chdir(path: string): Promise<void> {
let target: string;
if (isAbsolute(path)) {
target = path;
} else {
target = resolve(this._cwd, path);
}
// Resolve to the real path via SFTP
const resolved = await sftpRealpath(this._sftp, target);
// Verify the resolved target is actually a directory. Without this
// guard, `realpath` happily returns file paths, causing later relative
// reads/writes/execs to treat a regular file as a working directory.
const attrs = await sftpStat(this._sftp, resolved);
if (!attrs.isDirectory()) {
throw new KaosValueError(`${resolved} is not a directory`);
}
this._cwd = resolved;
}
async stat(path: string, options?: { followSymlinks?: boolean }): Promise<StatResult> {
const resolved = this._resolvePath(path);
const followSymlinks = options?.followSymlinks ?? true;
// sftpStat / sftpLstat already wrap errors via mapSftpError.
const st = followSymlinks
? await sftpStat(this._sftp, resolved)
: await sftpLstat(this._sftp, resolved);
return {
stMode: buildStMode(st),
// SFTP does not provide inode
stIno: 0,
// SFTP does not provide device
stDev: 0,
// ssh2 Stats does not expose nlink
stNlink: 0,
stUid: st.uid,
stGid: st.gid,
stSize: st.size,
stAtime: st.atime,
stMtime: st.mtime,
// SFTP v3 has no ctime, fallback to mtime
stCtime: st.mtime,
};
}
async *iterdir(path: string): AsyncGenerator<string> {
const resolved = this._resolvePath(path);
const entries = await sftpReaddir(this._sftp, resolved);
for (const entry of entries) {
if (entry.filename === '.' || entry.filename === '..') continue;
yield join(resolved, entry.filename);
}
}
async *glob(
path: string,
pattern: string,
options?: { caseSensitive?: boolean },
): AsyncGenerator<string> {
const resolved = this._resolvePath(path);
const caseSensitive = options?.caseSensitive ?? true;
if (!caseSensitive) {
throw new KaosValueError('Case insensitive glob is not supported in current environment');
}
// Use local glob implementation over SFTP readdir
const patternParts = pattern.split('/');
yield* this._globWalk(resolved, patternParts, caseSensitive);
}
private async *_globWalk(
basePath: string,
patternParts: string[],
caseSensitive: boolean,
): AsyncGenerator<string> {
if (patternParts.length === 0) return;
const [currentPattern, ...remainingParts] = patternParts;
if (currentPattern === '**') {
// `**` matches zero or more directory components.
//
// Two cases to handle:
// (a) `**` matches zero directories β continue at basePath with
// the remaining pattern parts (or yield basePath when `**` is
// the final segment).
// (b) `**` matches one or more directories β recurse into each
// subdirectory, keeping `**` (the full patternParts) at the
// front. The "zero directories" case is re-evaluated at the
// subdirectory level by that recursive call.
//
// Do NOT additionally recurse with `remainingParts` on subdirectories
// β that would double-count matches at depth β₯ 1 because case (a)
// inside the child recursion already yields those results.
if (remainingParts.length > 0) {
yield* this._globWalk(basePath, remainingParts, caseSensitive);
} else {
// Pattern ends with `**`: yield basePath itself (zero-dir match).
yield basePath;
}
let entries: SFTPFileEntry[];
try {
entries = await sftpReaddir(this._sftp, basePath);
} catch {
return;
}
for (const entry of entries) {
if (entry.filename === '.' || entry.filename === '..') continue;
const fullPath = join(basePath, entry.filename);
if (entry.attrs.isDirectory()) {
yield* this._globWalk(fullPath, patternParts, caseSensitive);
} else if (remainingParts.length === 0) {
// Pattern ends with `**`: non-directory entries match too.
yield fullPath;
}
}
} else {
const regex = globPatternToRegex(currentPattern ?? '', caseSensitive);
let entries: SFTPFileEntry[];
try {
entries = await sftpReaddir(this._sftp, basePath);
} catch {
return;
}
for (const entry of entries) {
if (entry.filename === '.' || entry.filename === '..') continue;
if (!regex.test(entry.filename)) continue;
const fullPath = join(basePath, entry.filename);
if (remainingParts.length === 0) {
yield fullPath;
} else if (entry.attrs.isDirectory()) {
yield* this._globWalk(fullPath, remainingParts, caseSensitive);
}
}
}
}
// ββ File operations (async) ββββββββββββββββββββββββββββββββββββββββ
async readBytes(path: string, n?: number): Promise<Buffer> {
const data = await sftpReadFile(this._sftp, this._resolvePath(path));
if (n === undefined) return data;
return data.subarray(0, n);
}
async readText(
path: string,
options?: { encoding?: BufferEncoding; errors?: 'strict' | 'replace' | 'ignore' },
): Promise<string> {
const encoding = options?.encoding ?? 'utf-8';
const errors = options?.errors ?? 'strict';
const data = await sftpReadFile(this._sftp, this._resolvePath(path));
return decodeTextWithErrors(data, encoding, errors);
}
async *readLines(
path: string,
options?: { encoding?: BufferEncoding; errors?: 'strict' | 'replace' | 'ignore' },
): AsyncGenerator<string> {
// SFTP does not support streaming line reads; read all then split.
// Match Python's splitlines() semantics: returned lines do NOT include
// the line terminator, and a trailing newline does not create an extra
// empty line.
const text = await this.readText(this._resolvePath(path), options);
if (text === '') {
return;
}
const lines = text.split(/\r\n|[\n\r]/u);
if (/(?:\r\n|[\n\r])$/u.test(text)) {
lines.pop();
}
for (const line of lines) {
yield line ?? '';
}
}
async writeBytes(path: string, data: Buffer): Promise<number> {
await sftpWriteFile(this._sftp, this._resolvePath(path), data);
return data.length;
}
async writeText(
path: string,
data: string,
options?: { mode?: 'w' | 'a'; encoding?: BufferEncoding },
): Promise<number> {
const resolved = this._resolvePath(path);
const mode = options?.mode ?? 'w';
const encoding = options?.encoding ?? 'utf-8';
const buf = Buffer.from(data, encoding);
if (mode === 'a') {
await sftpAppendFile(this._sftp, resolved, buf);
} else {
await sftpWriteFile(this._sftp, resolved, buf);
}
return data.length;
}
async mkdir(path: string, options?: { parents?: boolean; existOk?: boolean }): Promise<void> {
const resolved = this._resolvePath(path);
const parents = options?.parents ?? false;
const existOk = options?.existOk ?? false;
if (parents) {
await this._mkdirRecursive(resolved, existOk);
} else {
const exists = await sftpExists(this._sftp, resolved);
if (exists) {
if (!existOk) {
throw new KaosFileExistsError(`${resolved} already exists`);
}
// `existOk` only applies when the conflicting path is itself a
// directory. A regular file sitting at the target path is still
// a conflict β we must not pretend mkdir succeeded.
const st = await sftpStat(this._sftp, resolved);
if (!st.isDirectory()) {
throw new KaosFileExistsError(`${resolved} already exists but is not a directory`);
}
return;
}
await sftpMkdir(this._sftp, resolved);
}
}
private async _mkdirRecursive(path: string, existOk: boolean): Promise<void> {
// Split path into components and create each level.
const parts = path.split('/').filter(Boolean);
let current = path.startsWith('/') ? '/' : '';
const lastIndex = parts.length - 1;
for (const [i, part] of parts.entries()) {
current = current ? join(current, part) : part;
const isFinal = i === lastIndex;
// eslint-disable-next-line no-await-in-loop
const exists = await sftpExists(this._sftp, current);
if (exists) {
// For intermediate components, it's fine (and expected) for the
// path to already exist. For the final target, honor `existOk`.
if (isFinal && !existOk) {
throw new KaosFileExistsError(`${current} already exists`);
}
// Regardless of whether this is an intermediate or the final
// component, an existing path must actually be a directory.
// An intermediate non-directory would cause the next `sftpMkdir`
// to fail with a confusing error; a final non-directory would
// otherwise be silently accepted when `existOk` is true.
// eslint-disable-next-line no-await-in-loop
const st = await sftpStat(this._sftp, current);
if (!st.isDirectory()) {
throw new KaosFileExistsError(`${current} already exists but is not a directory`);
}
continue;
}
try {
// eslint-disable-next-line no-await-in-loop
await sftpMkdir(this._sftp, current);
} catch (error) {
// Race condition: another process may have created it.
// eslint-disable-next-line no-await-in-loop
const nowExists = await sftpExists(this._sftp, current);
if (!nowExists) throw new Error(`Failed to create directory: ${current}`, { cause: error });
// A raced path must still be a directory. Another process may have
// created a regular file at the same pathname after our exists()
// check but before mkdir(), which must remain a hard conflict.
// eslint-disable-next-line no-await-in-loop
const st = await sftpStat(this._sftp, current);
if (!st.isDirectory()) {
throw new KaosFileExistsError(`${current} already exists but is not a directory`);
}
// If the final component lost a race and existOk=false, surface the
// conflict to match the non-race path above.
if (isFinal && !existOk) {
throw new KaosFileExistsError(`${current} already exists`);
}
}
}
}
// ββ Process execution ββββββββββββββββββββββββββββββββββββββββββββββ
exec(...args: string[]): Promise<KaosProcess> {
if (args.length === 0) {
throw new KaosValueError(
'SSHKaos.exec(): at least one argument (the command to run) is required.',
);
}
return this._execInternal(args, this._buildExecEnv());
}
execWithEnv(args: string[], env?: Record<string, string>): Promise<KaosProcess> {
if (args.length === 0) {
throw new KaosValueError(
'SSHKaos.execWithEnv(): at least one argument (the command to run) is required.',
);
}
return this._execInternal(args, this._buildExecEnv(env));
}
private _buildExecEnv(invocationEnv?: Record<string, string>): Record<string, string> | undefined {
if (this._envLayers.length === 0) return invocationEnv;
const merged: Record<string, string> = { ...invocationEnv };
for (const layer of this._envLayers) {
Object.assign(merged, layer);
}
return merged;
}
/**
* Build the full remote shell command string that will be handed to
* `client.exec`. Exposed as a static so it can be unit-tested without
* needing a live SSH connection β see `ssh.test.ts`.
*
* Shape: `cd '<cwd>' && KEY1='v1' KEY2='v2' <cmd> <arg1> <arg2> ...`
*
* Environment variables are injected as POSIX inline assignments instead
* of being passed through ssh2's `ExecOptions.env`. The env-request path
* silently drops anything not whitelisted by sshd's `AcceptEnv` directive
* (stock OpenSSH only allows LANG/LC_*), which is a well-known footgun
* inherited from the Python / asyncssh implementation. Inline assignments
* run inside the remote shell itself, so they bypass AcceptEnv entirely
* and reach the command regardless of server configuration.
*/
private static _buildExecCommand(
args: string[],
cwd: string,
env?: Record<string, string>,
): string {
let command = args.map((arg) => shellQuote(arg)).join(' ');
if (env !== undefined) {
const assignments: string[] = [];
for (const [key, value] of Object.entries(env)) {
// Reject anything that isn't a POSIX-valid shell variable name so
// the injected prefix can never become a shell-injection vector.
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
throw new KaosValueError(
`SSHKaos.execWithEnv(): invalid env variable name ${JSON.stringify(key)}`,
);
}
assignments.push(`${key}=${shellQuote(value)}`);
}
if (assignments.length > 0) {
command = `${assignments.join(' ')} ${command}`;
}
}
if (cwd !== '') {
command = `cd ${shellQuote(cwd)} && ${command}`;
}
return command;
}
private async _execInternal(args: string[], env?: Record<string, string>): Promise<KaosProcess> {
const command = SSHKaos._buildExecCommand(args, this._cwd, env);
const channel = await clientExec(this._client, command);
return new SSHProcess(channel);
}
// ββ SSH lifecycle ββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Close the SSH connection. After this, the SSHKaos instance is unusable.
*/
close(): Promise<void> {
this._sftp.end();
return new Promise<void>((resolve) => {
this._client.once('close', () => {
resolve();
});
this._client.end();
});
}
}
|