@pyrokine/mcp-ssh 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/file-ops.js CHANGED
@@ -1,20 +1,22 @@
1
1
  /**
2
2
  * SSH File Operations - 文件操作
3
3
  */
4
- import { spawn } from 'child_process';
5
- import { OpenSshError } from './openssh.js';
4
+ import { spawn, spawnSync } from 'child_process';
6
5
  import { createHash, randomBytes } from 'crypto';
7
6
  import * as fs from 'fs';
7
+ import { Readable } from 'node:stream';
8
+ import { StringDecoder } from 'node:string_decoder';
8
9
  import * as os from 'os';
9
10
  import * as path from 'path';
10
- import { checkWindowsAcl, windowsAclScript } from './windows-acl.js';
11
11
  import { matchesDirectoryExclude } from './directory-verification.js';
12
12
  import { extractFileMode, FILE_CREATE_MODE_MASK, formatFileMode, formatFilePermissions } from './file-mode.js';
13
+ import { OpenSshError } from './openssh.js';
14
+ import { encodedPowershell, remoteDirname, remoteJoin, remoteNormalize, remotePathModule, remoteShellPath, windowsCaseSemanticsScript, } from './remote-platform.js';
13
15
  import { sessionManager } from './session-manager.js';
14
- import { SftpOperationTimeoutError, withSftpOperationTimeout } from './sftp-timeout.js';
16
+ import { SftpOperationTimeoutError, SftpWriteState, sftpOperationOutcome, withSftpOperationTimeout, } from './sftp-timeout.js';
15
17
  import { escapeShellArg, expandTilde } from './tools/utils.js';
16
- import { encodedPowershell, remoteDirname, remoteJoin, remoteNormalize, remotePathModule, remoteShellPath, windowsCaseSemanticsScript, } from './remote-platform.js';
17
18
  import { ensureUtf8Boundary, isUtf8ContinuationByte } from './utf8.js';
19
+ import { checkWindowsAcl, windowsAclScript } from './windows-acl.js';
18
20
  // 文件类型 mode 常量
19
21
  const S_IFMT = 0o170000; // 文件类型位掩码
20
22
  const S_IFDIR = 0o40000;
@@ -32,10 +34,12 @@ function appendLimitedOutput(current, chunk) {
32
34
  }
33
35
  return (current + chunk).slice(0, LOCAL_PROCESS_OUTPUT_LIMIT);
34
36
  }
35
- function runLocalProcess(command, args, timeout, env, connection) {
37
+ function runLocalProcess(command, args, timeout, env, connection, onStdout) {
36
38
  return new Promise((resolve) => {
37
39
  let stdout = '';
38
40
  let stderr = '';
41
+ const stdoutDecoder = new StringDecoder('utf8');
42
+ let stdoutTruncated = false;
39
43
  let settled = false;
40
44
  let timedOut = false;
41
45
  let killTimer;
@@ -53,12 +57,14 @@ function runLocalProcess(command, args, timeout, env, connection) {
53
57
  process.kill(-child.pid, signal);
54
58
  }
55
59
  catch (error) {
56
- if (error.code !== 'ESRCH')
60
+ if (error.code !== 'ESRCH') {
57
61
  console.error('Local process cleanup failed:', error);
62
+ }
58
63
  }
59
64
  }
60
- else
65
+ else {
61
66
  child.kill(signal);
67
+ }
62
68
  };
63
69
  const timer = setTimeout(() => {
64
70
  timedOut = true;
@@ -66,7 +72,10 @@ function runLocalProcess(command, args, timeout, env, connection) {
66
72
  killTimer = setTimeout(() => kill('SIGKILL'), 5000);
67
73
  }, timeout);
68
74
  child.stdout.on('data', (chunk) => {
69
- stdout = appendLimitedOutput(stdout, chunk.toString('utf-8'));
75
+ const text = stdoutDecoder.write(chunk);
76
+ onStdout?.(text);
77
+ stdoutTruncated ||= stdout.length + text.length > LOCAL_PROCESS_OUTPUT_LIMIT;
78
+ stdout = appendLimitedOutput(stdout, text);
70
79
  });
71
80
  child.stderr.on('data', (chunk) => {
72
81
  stderr = appendLimitedOutput(stderr, chunk.toString('utf-8'));
@@ -80,7 +89,8 @@ function runLocalProcess(command, args, timeout, env, connection) {
80
89
  if (killTimer) {
81
90
  clearTimeout(killTimer);
82
91
  }
83
- resolve({ status: null, stdout, stderr, error, timedOut });
92
+ onStdout?.(stdoutDecoder.end(), true);
93
+ resolve({ status: null, stdout, stderr, error, timedOut, stdoutTruncated });
84
94
  });
85
95
  child.on('close', (status) => {
86
96
  if (settled) {
@@ -91,7 +101,11 @@ function runLocalProcess(command, args, timeout, env, connection) {
91
101
  if (killTimer) {
92
102
  clearTimeout(killTimer);
93
103
  }
94
- resolve({ status, stdout, stderr, timedOut });
104
+ const tail = stdoutDecoder.end();
105
+ onStdout?.(tail, true);
106
+ stdoutTruncated ||= stdout.length + tail.length > LOCAL_PROCESS_OUTPUT_LIMIT;
107
+ stdout = appendLimitedOutput(stdout, tail);
108
+ resolve({ status, stdout, stderr, timedOut, stdoutTruncated });
95
109
  });
96
110
  });
97
111
  }
@@ -107,7 +121,7 @@ function isCurrentRsyncGeneration(alias, generation) {
107
121
  }
108
122
  function assertCurrentRsyncGeneration(alias, generation) {
109
123
  if (!isCurrentRsyncGeneration(alias, generation)) {
110
- throw new Error(`SSH session '${alias}' changed during rsync setup; retry the sync on the current session`);
124
+ throw new Error("SSH session '" + `${alias}` + "' changed during rsync setup; retry the sync on the current session");
111
125
  }
112
126
  }
113
127
  /** 由 SessionManager.disconnect 调用,防止 alias 重连后读到旧主机的判断 */
@@ -212,7 +226,7 @@ export function convertRsyncPath(value, profile) {
212
226
  if (profile === 'native-win32' || profile === 'posix') {
213
227
  return value;
214
228
  }
215
- const drive = /^([a-zA-Z]):[\\/](.*)$/.exec(value);
229
+ const drive = /^(?<drive>[a-zA-Z]):[\\/](?<path>.*)$/.exec(value);
216
230
  if (drive) {
217
231
  const prefix = profile === 'cygwin' ? '/cygdrive/' : '/';
218
232
  return `${prefix}${drive[1].toLowerCase()}/${drive[2].replace(/\\/g, '/')}`;
@@ -227,7 +241,7 @@ export function quoteRsyncSshArg(value, profile = 'posix') {
227
241
  if (/[\r\n\0]/.test(text)) {
228
242
  throw new Error('rsync SSH argument contains a newline or NUL');
229
243
  }
230
- return profile === 'posix' ? escapeShellArg(text) : `"${text.replace(/([\\"])/g, '\\$1')}"`;
244
+ return profile === 'posix' ? escapeShellArg(text) : `"${text.replace(/(?<character>[\\"])/g, '\\$1')}"`;
231
245
  }
232
246
  function configuredAllowDirectories() {
233
247
  const allowEnv = process.env.SSH_MCP_FILE_OPS_ALLOW_DIRS;
@@ -299,8 +313,8 @@ function validateLocalUploadSource(localPath, followSymlinks, symlinkError) {
299
313
  throw new Error(symlinkError);
300
314
  }
301
315
  if (followSymlinks && getAllowListStatus(expandedPath).configured) {
302
- throw new Error('followSymlinks=true cannot be combined with SSH_MCP_FILE_OPS_ALLOW_DIRS because ' +
303
- 'symlink targets can change during transfer');
316
+ throw new Error('followSymlinks=true cannot be combined with SSH_MCP_FILE_OPS_ALLOW_DIRS ' +
317
+ 'because symlink targets can change during transfer');
304
318
  }
305
319
  return {
306
320
  expandedPath,
@@ -348,16 +362,32 @@ export function probeLocalPath(localPath, followSymlinks = false) {
348
362
  /**
349
363
  * sftp.stat 的 Promise 包装
350
364
  */
351
- async function getSftpWithinDeadline(alias, timeout) {
365
+ function remainingSftpTimeout(deadline, signal) {
366
+ throwIfAborted(signal);
367
+ if (deadline === undefined) {
368
+ return undefined;
369
+ }
370
+ const remaining = deadline - Date.now();
371
+ if (remaining <= 0) {
372
+ throw new SftpOperationTimeoutError(0);
373
+ }
374
+ return remaining;
375
+ }
376
+ async function getSftpWithinDeadline(alias, timeout, signal) {
377
+ throwIfAborted(signal);
378
+ if (timeout !== undefined && timeout <= 0) {
379
+ throw new SftpOperationTimeoutError(timeout);
380
+ }
352
381
  const request = sessionManager.getSftp(alias);
353
382
  return withSftpOperationTimeout(request, timeout, () => {
354
383
  void request.then((sftp) => sftp.end(), () => undefined);
355
- });
384
+ }, signal);
356
385
  }
357
- function withSftpDeadline(sftp, operation, timeout) {
358
- return withSftpOperationTimeout(operation, timeout, () => sftp.end());
386
+ function withSftpDeadline(sftp, operation, timeout, signal) {
387
+ return withSftpOperationTimeout(operation, timeout, () => sftp.end(), signal);
359
388
  }
360
- function sftpStat(sftp, remotePath, timeout) {
389
+ function sftpStat(sftp, remotePath, timeout, signal) {
390
+ throwIfAborted(signal);
361
391
  return withSftpDeadline(sftp, new Promise((resolve, reject) => {
362
392
  sftp.stat(remotePath, (err, stats) => {
363
393
  if (err) {
@@ -367,7 +397,7 @@ function sftpStat(sftp, remotePath, timeout) {
367
397
  resolve(stats);
368
398
  }
369
399
  });
370
- }), timeout);
400
+ }), timeout, signal);
371
401
  }
372
402
  function isSftpMissingError(error) {
373
403
  if (!error || typeof error !== 'object') {
@@ -383,9 +413,9 @@ function isSftpMissingError(error) {
383
413
  function isLocalMissingError(error) {
384
414
  return Boolean(error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT');
385
415
  }
386
- async function sftpExists(sftp, remotePath) {
416
+ async function sftpExists(sftp, remotePath, timeout, signal) {
387
417
  try {
388
- await sftpStat(sftp, remotePath);
418
+ await sftpStat(sftp, remotePath, timeout, signal);
389
419
  return true;
390
420
  }
391
421
  catch (error) {
@@ -395,22 +425,34 @@ async function sftpExists(sftp, remotePath) {
395
425
  throw error;
396
426
  }
397
427
  }
398
- async function remoteExists(alias, remotePath) {
399
- const sftp = await getSftpWithinDeadline(alias);
428
+ async function remoteExists(alias, remotePath, deadline, signal) {
429
+ const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
400
430
  try {
401
- return await sftpExists(sftp, remotePath);
431
+ return await sftpExists(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
402
432
  }
403
433
  finally {
404
434
  sftp.end();
405
435
  }
406
436
  }
407
- export async function hashRemoteFileDigest(alias, remotePath, algorithm, timeout, limits) {
437
+ export async function hashRemoteFileDigest(alias, remotePath, algorithm, timeout, limits, signal) {
438
+ const deadline = timeout === undefined ? undefined : Date.now() + timeout;
439
+ throwIfAborted(signal);
408
440
  const connection = sessionManager.getOpenSshConnection(alias);
409
- if (connection)
441
+ if (connection) {
410
442
  return connection.hashFile(remotePath, algorithm, timeout ?? 600_000, limits?.maxBytes, limits?.expectedSize);
411
- const sftp = await getSftpWithinDeadline(alias, timeout);
443
+ }
444
+ const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
445
+ let transferTimeout;
446
+ try {
447
+ transferTimeout = remainingSftpTimeout(deadline, signal);
448
+ }
449
+ catch (error) {
450
+ sftp.end();
451
+ throw error;
452
+ }
412
453
  return withSftpDeadline(sftp, new Promise((resolve, reject) => {
413
454
  const hash = createHash(algorithm);
455
+ throwIfAborted(signal);
414
456
  const stream = sftp.createReadStream(remotePath);
415
457
  let settled = false;
416
458
  let bytes = 0;
@@ -428,21 +470,26 @@ export async function hashRemoteFileDigest(alias, remotePath, algorithm, timeout
428
470
  }
429
471
  };
430
472
  stream.on('data', (chunk) => {
431
- if (settled)
473
+ if (settled) {
432
474
  return;
475
+ }
433
476
  bytes += chunk.length;
434
477
  if (limits && (bytes > limits.maxBytes || bytes > limits.expectedSize)) {
435
478
  stream.destroy();
436
479
  finish(new Error('Remote file grew or exceeded the verification byte budget'));
437
480
  }
438
- else
481
+ else {
439
482
  hash.update(chunk);
483
+ }
440
484
  });
441
485
  stream.once('error', finish);
442
486
  stream.once('end', () => finish(limits && bytes !== limits.expectedSize
443
487
  ? new Error('Remote file changed during verification')
444
488
  : undefined));
445
- }), timeout);
489
+ }), transferTimeout, signal).catch((error) => {
490
+ sftp.end();
491
+ throw error;
492
+ });
446
493
  }
447
494
  export async function atomicReplaceCapability(alias, timeout) {
448
495
  const sftp = await getSftpWithinDeadline(alias, timeout);
@@ -464,7 +511,14 @@ export class RemoteRenameNotStartedError extends Error {
464
511
  }
465
512
  }
466
513
  export async function renameRemoteFile(alias, sourcePath, targetPath, replaceExisting = false, timeout) {
467
- const sftp = await getSftpWithinDeadline(alias, timeout);
514
+ const state = new SftpWriteState(sourcePath);
515
+ const sftp = await getSftpWithinDeadline(alias, timeout).catch((error) => {
516
+ throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
517
+ cause: error,
518
+ details: state.outcome(error),
519
+ });
520
+ });
521
+ const tracked = state.track(sftp);
468
522
  return withSftpDeadline(sftp, new Promise((resolve, reject) => {
469
523
  const callback = (error) => {
470
524
  sftp.end();
@@ -477,17 +531,23 @@ export async function renameRemoteFile(alias, sourcePath, targetPath, replaceExi
477
531
  };
478
532
  try {
479
533
  if (replaceExisting) {
480
- sftp.ext_openssh_rename(sourcePath, targetPath, callback);
534
+ tracked.ext_openssh_rename(sourcePath, targetPath, callback);
481
535
  }
482
536
  else {
483
- sftp.rename(sourcePath, targetPath, callback);
537
+ tracked.rename(sourcePath, targetPath, callback);
484
538
  }
485
539
  }
486
540
  catch (error) {
487
541
  sftp.end();
488
- reject(new RemoteRenameNotStartedError(`SFTP rename request was not sent: ${error instanceof Error ? error.message : String(error)}`));
542
+ reject(new RemoteRenameNotStartedError('SFTP rename request was not sent: ' +
543
+ `${error instanceof Error ? error.message : String(error)}`));
489
544
  }
490
- }), timeout);
545
+ }), timeout).catch((error) => {
546
+ throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
547
+ cause: error,
548
+ details: { ...state.outcome(error), targetPath },
549
+ });
550
+ });
491
551
  }
492
552
  export async function removeRemoteFile(alias, remotePath, timeout) {
493
553
  const sftp = await getSftpWithinDeadline(alias, timeout);
@@ -560,6 +620,18 @@ function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress,
560
620
  if (settled) {
561
621
  return;
562
622
  }
623
+ if (writeStream.errored) {
624
+ cleanup(writeStream.errored);
625
+ return;
626
+ }
627
+ // ssh2 在 _final 中先关闭流,按已确认字节数判断完成
628
+ if (!writeStream.writableFinished &&
629
+ (!writeStream.writableEnded ||
630
+ writeStream.writableLength !== 0 ||
631
+ writeStream.bytesWritten !== totalSize)) {
632
+ cleanup(new Error('Transfer write stream closed before finish'));
633
+ return;
634
+ }
563
635
  if (transferred !== totalSize) {
564
636
  cleanup(new Error(`Transfer size mismatch: expected ${totalSize} bytes, received ${transferred}`));
565
637
  return;
@@ -587,15 +659,17 @@ export class UploadFileError extends Error {
587
659
  this.remoteCreated = remoteCreated;
588
660
  this.name = 'UploadFileError';
589
661
  const cause = options?.cause;
590
- if (cause && typeof cause === 'object' && 'details' in cause) {
591
- this.details = cause.details;
592
- }
662
+ const causeDetails = cause && typeof cause === 'object' && 'details' in cause
663
+ ? cause.details
664
+ : undefined;
665
+ this.details = options?.details ? { ...causeDetails, ...options.details } : causeDetails;
593
666
  }
594
667
  }
595
668
  /**
596
669
  * 上传文件
597
670
  */
598
671
  export async function uploadFile(alias, localPath, remotePath, onProgress, sharedSftp, createMode, signal, followSymlinks = false, exclusive = false, timeout) {
672
+ const deadline = timeout === undefined ? undefined : Date.now() + timeout;
599
673
  throwIfAborted(signal);
600
674
  const source = followSymlinks
601
675
  ? validateLocalUploadSource(localPath, true, 'Refusing to upload symlink at top level (use ssh_sync with followSymlinks=true to traverse)')
@@ -608,21 +682,47 @@ export async function uploadFile(alias, localPath, remotePath, onProgress, share
608
682
  if (!stats.isFile()) {
609
683
  throw new Error('UPLOAD_PATH_IS_NOT_FILE: localPath is not a regular file');
610
684
  }
611
- const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, timeout));
685
+ const state = new SftpWriteState(remotePath);
686
+ const sftp = sharedSftp ??
687
+ (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal).catch((error) => {
688
+ throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
689
+ cause: error,
690
+ details: state.outcome(error),
691
+ });
692
+ }));
693
+ try {
694
+ remainingSftpTimeout(deadline, signal);
695
+ }
696
+ catch (error) {
697
+ if (!sharedSftp) {
698
+ sftp.end();
699
+ }
700
+ throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
701
+ cause: error,
702
+ details: state.outcome(error),
703
+ });
704
+ }
612
705
  const totalSize = stats.size;
613
706
  const safeCreateMode = (createMode ?? stats.mode) & FILE_CREATE_MODE_MASK;
614
- const writeStream = sftp.createWriteStream(remotePath, { mode: safeCreateMode, flags: exclusive ? 'wx' : 'w' });
615
- let remoteCreated = false;
616
- writeStream.once('open', () => {
617
- remoteCreated = true;
618
- });
619
707
  try {
620
- const result = await withSftpDeadline(sftp, pipeWithProgress(fs.createReadStream(localPath), writeStream, sftp, totalSize, onProgress, !sharedSftp, signal), timeout);
708
+ remainingSftpTimeout(deadline, signal);
709
+ const writeStream = state.track(sftp).createWriteStream(remotePath, {
710
+ mode: safeCreateMode,
711
+ flags: exclusive ? 'wx' : 'w',
712
+ });
713
+ writeStream.once('open', () => {
714
+ state.remoteCreated = true;
715
+ });
716
+ const result = await withSftpDeadline(sftp, pipeWithProgress(fs.createReadStream(localPath), writeStream, sftp, totalSize, onProgress, !sharedSftp, signal), remainingSftpTimeout(deadline, signal), signal);
621
717
  return { ...result, createMode: formatFileMode(safeCreateMode) };
622
718
  }
623
719
  catch (error) {
624
- throw new UploadFileError(error instanceof Error ? error.message : String(error), remoteCreated, {
720
+ if (!sharedSftp) {
721
+ sftp.end();
722
+ }
723
+ throw new UploadFileError(error instanceof Error ? error.message : String(error), state.remoteCreated, {
625
724
  cause: error,
725
+ details: state.outcome(error),
626
726
  });
627
727
  }
628
728
  }
@@ -642,10 +742,12 @@ async function existingDownloadTargetMode(localPath) {
642
742
  }
643
743
  return stats.mode & FILE_CREATE_MODE_MASK;
644
744
  }
645
- async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, closeSftpOnFailure, timeout) {
745
+ async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, closeSftpOnFailure, deadline, signal) {
646
746
  let readStream;
647
747
  try {
648
- const totalSize = (await sftpStat(sftp, remotePath, timeout)).size;
748
+ const totalSize = (await sftpStat(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal)).size;
749
+ remainingSftpTimeout(deadline, signal);
750
+ validateLocalPathAgainstAllowList(localDir);
649
751
  if (!fs.existsSync(localDir)) {
650
752
  fs.mkdirSync(localDir, { recursive: true });
651
753
  }
@@ -653,9 +755,10 @@ async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath
653
755
  const handle = await fs.promises.open(temporaryPath, 'wx', 0o600);
654
756
  await handle.close();
655
757
  if (checkWindowsAcl(temporaryPath, true) !== 'key_path_usable') {
656
- throw new Error('privacy_not_enforced_on_windows: temporary download file ACL could not be verified');
758
+ throw new Error('privacy_not_enforced_on_windows: temporary download file ACL could not ' + 'be verified');
657
759
  }
658
760
  }
761
+ remainingSftpTimeout(deadline, signal);
659
762
  readStream = sftp.createReadStream(remotePath);
660
763
  return {
661
764
  totalSize,
@@ -684,31 +787,58 @@ async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath
684
787
  * 下载文件
685
788
  */
686
789
  export async function downloadFile(alias, remotePath, localPath, onProgress, sharedSftp, signal, timeout) {
790
+ const deadline = timeout === undefined ? undefined : Date.now() + timeout;
687
791
  throwIfAborted(signal);
688
792
  localPath = expandTilde(localPath);
689
793
  validateLocalPathAgainstAllowList(localPath);
690
794
  const preservedMode = await existingDownloadTargetMode(localPath);
691
- const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, timeout));
795
+ const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
692
796
  const localDir = path.dirname(localPath);
693
797
  const temporaryPath = path.join(localDir, `.${path.basename(localPath)}.mcp-tmp-${process.pid}-${randomBytes(8).toString('hex')}`);
798
+ let failureStage = 'download';
799
+ let localWriteStream;
694
800
  try {
695
- const { totalSize, readStream, writeStream } = await prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, !sharedSftp, timeout);
696
- const result = await withSftpDeadline(sftp, pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress, !sharedSftp, signal), timeout);
801
+ const { totalSize, readStream, writeStream } = await prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, !sharedSftp, deadline, signal);
802
+ localWriteStream = writeStream;
803
+ const result = await withSftpDeadline(sftp, pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress, !sharedSftp, signal), remainingSftpTimeout(deadline, signal), signal);
697
804
  if (process.platform !== 'win32') {
698
805
  await fs.promises.chmod(temporaryPath, preservedMode ?? 0o600);
699
806
  }
807
+ remainingSftpTimeout(deadline, signal);
808
+ validateLocalPathAgainstAllowList(localPath);
809
+ failureStage = 'local_rename';
700
810
  await fs.promises.rename(temporaryPath, localPath);
701
811
  return { ...result, atomic: true };
702
812
  }
703
- catch (error) {
813
+ catch (cause) {
814
+ const error = new UploadFileError(cause instanceof Error ? cause.message : String(cause), false, {
815
+ cause,
816
+ details: {
817
+ ...sftpOperationOutcome(cause),
818
+ operationStatus: 'failed',
819
+ retryable: false,
820
+ failureStage,
821
+ localPath,
822
+ temporaryPath,
823
+ targetReplaced: false,
824
+ },
825
+ });
704
826
  try {
827
+ const writeStream = localWriteStream;
828
+ if (writeStream && !writeStream.closed) {
829
+ // 尚未完成的 open 会在 unlink 返回 ENOENT 后创建文件
830
+ await new Promise((resolve) => {
831
+ writeStream.once('close', resolve);
832
+ writeStream.destroy();
833
+ });
834
+ }
705
835
  await fs.promises.unlink(temporaryPath);
706
836
  }
707
837
  catch (cleanupError) {
708
838
  if (!isLocalMissingError(cleanupError)) {
709
- throw new AggregateError([error, cleanupError], 'Download failed and temporary file cleanup failed', {
710
- cause: cleanupError,
711
- });
839
+ throw Object.assign(new AggregateError([error, cleanupError], 'Download failed and temporary file cleanup failed', {
840
+ cause,
841
+ }), { details: error.details });
712
842
  }
713
843
  }
714
844
  throw error;
@@ -991,7 +1121,13 @@ export async function readFile(alias, remotePath, options) {
991
1121
  const sftp = await getSftpWithinDeadline(alias, readOptions.timeout);
992
1122
  let actualSize;
993
1123
  try {
994
- actualSize = (await sftpStat(sftp, remotePath, readOptions.timeout)).size;
1124
+ const stats = await sftpStat(sftp, remotePath, readOptions.timeout);
1125
+ if ((stats.mode & S_IFMT) !== S_IFREG) {
1126
+ throw new Error((stats.mode & S_IFMT) === S_IFDIR
1127
+ ? 'Target is a directory; use ssh_list_dir to read its entries'
1128
+ : 'Target is not a regular file');
1129
+ }
1130
+ actualSize = stats.size;
995
1131
  }
996
1132
  catch (error) {
997
1133
  sftp.end();
@@ -1165,22 +1301,24 @@ export async function prepareWindowsRemotePrivateFile(alias, remotePath, timeout
1165
1301
  * 写入远程文件
1166
1302
  */
1167
1303
  export async function writeFile(alias, remotePath, content, append = false, timeout) {
1168
- const sftp = await getSftpWithinDeadline(alias, timeout);
1169
- const flags = append ? 'a' : 'w';
1170
- const contentBuffer = Buffer.from(content, 'utf-8');
1171
- return withSftpDeadline(sftp, new Promise((resolve, reject) => {
1172
- const writeStream = sftp.createWriteStream(remotePath, { flags });
1173
- writeStream.on('close', () => {
1174
- sftp.end();
1175
- resolve({ success: true, size: contentBuffer.length });
1304
+ const state = new SftpWriteState(remotePath);
1305
+ const sftp = await getSftpWithinDeadline(alias, timeout).catch((error) => {
1306
+ throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
1307
+ cause: error,
1308
+ details: state.outcome(error),
1176
1309
  });
1177
- writeStream.on('error', (err) => {
1178
- sftp.end();
1179
- reject(err);
1310
+ });
1311
+ const contentBuffer = Buffer.from(content, 'utf-8');
1312
+ try {
1313
+ const writeStream = state.track(sftp).createWriteStream(remotePath, { flags: append ? 'a' : 'w' });
1314
+ return await withSftpDeadline(sftp, pipeWithProgress(Readable.from([contentBuffer]), writeStream, sftp, contentBuffer.length), timeout);
1315
+ }
1316
+ catch (error) {
1317
+ throw new UploadFileError(error instanceof Error ? error.message : String(error), state.remoteCreated, {
1318
+ cause: error,
1319
+ details: state.outcome(error),
1180
1320
  });
1181
- writeStream.write(contentBuffer);
1182
- writeStream.end();
1183
- }), timeout);
1321
+ }
1184
1322
  }
1185
1323
  function validateSftpEntryName(filename) {
1186
1324
  if (filename.length === 0 ||
@@ -1195,10 +1333,12 @@ function validateSftpEntryName(filename) {
1195
1333
  /**
1196
1334
  * 列出目录内容
1197
1335
  */
1198
- export async function listDir(alias, remotePath, showHidden = false, sharedSftp, timeout, style = 'posix') {
1199
- const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, timeout));
1336
+ export async function listDir(alias, remotePath, showHidden = false, sharedSftp, timeout, style = 'posix', signal) {
1337
+ throwIfAborted(signal);
1338
+ const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, timeout, signal));
1200
1339
  const closeWhenDone = !sharedSftp;
1201
1340
  return withSftpDeadline(sftp, new Promise((resolve, reject) => {
1341
+ throwIfAborted(signal);
1202
1342
  sftp.readdir(remotePath, (err, list) => {
1203
1343
  if (err) {
1204
1344
  if (closeWhenDone) {
@@ -1247,21 +1387,22 @@ export async function listDir(alias, remotePath, showHidden = false, sharedSftp,
1247
1387
  }
1248
1388
  resolve(files);
1249
1389
  });
1250
- }), timeout);
1390
+ }), timeout, signal);
1251
1391
  }
1252
1392
  /**
1253
1393
  * 获取文件信息
1254
1394
  */
1255
- export async function getFileInfo(alias, remotePath, timeout, style = 'posix') {
1395
+ export async function getFileInfo(alias, remotePath, timeout, style = 'posix', signal) {
1396
+ const deadline = timeout === undefined ? undefined : Date.now() + timeout;
1256
1397
  const connection = sessionManager.getOpenSshConnection(alias);
1257
1398
  let stats;
1258
1399
  if (connection) {
1259
1400
  stats = await connection.stat(remotePath, timeout ?? 600_000);
1260
1401
  }
1261
1402
  else {
1262
- const sftp = await getSftpWithinDeadline(alias, timeout);
1403
+ const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
1263
1404
  try {
1264
- stats = await sftpStat(sftp, remotePath, timeout);
1405
+ stats = await sftpStat(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
1265
1406
  }
1266
1407
  finally {
1267
1408
  sftp.end();
@@ -1282,7 +1423,8 @@ export async function getFileInfo(alias, remotePath, timeout, style = 'posix') {
1282
1423
  atime: new Date(stats.atime * 1000),
1283
1424
  };
1284
1425
  }
1285
- function sftpMkdir(sftp, remotePath, timeout) {
1426
+ function sftpMkdir(sftp, remotePath, timeout, signal) {
1427
+ throwIfAborted(signal);
1286
1428
  return withSftpDeadline(sftp, new Promise((resolve, reject) => {
1287
1429
  sftp.mkdir(remotePath, (error) => {
1288
1430
  if (error) {
@@ -1292,16 +1434,17 @@ function sftpMkdir(sftp, remotePath, timeout) {
1292
1434
  resolve();
1293
1435
  }
1294
1436
  });
1295
- }), timeout);
1437
+ }), timeout, signal);
1296
1438
  }
1297
- async function ensureSftpDirectory(sftp, remotePath, timeout, style = 'posix') {
1439
+ async function ensureSftpDirectory(sftp, remotePath, deadline, style = 'posix', signal) {
1440
+ remainingSftpTimeout(deadline, signal);
1298
1441
  const normalizedPath = remoteNormalize(style, remotePath);
1299
1442
  if (normalizedPath === '/' || normalizedPath === '.') {
1300
1443
  return;
1301
1444
  }
1302
1445
  let existingMode;
1303
1446
  try {
1304
- existingMode = (await sftpStat(sftp, normalizedPath, timeout)).mode;
1447
+ existingMode = (await sftpStat(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal)).mode;
1305
1448
  }
1306
1449
  catch (error) {
1307
1450
  if (!isSftpMissingError(error)) {
@@ -1319,14 +1462,14 @@ async function ensureSftpDirectory(sftp, remotePath, timeout, style = 'posix') {
1319
1462
  throw new Error('SFTP UNC share root does not exist; refusing to create a network host path');
1320
1463
  }
1321
1464
  if (parent !== normalizedPath) {
1322
- await ensureSftpDirectory(sftp, parent, timeout, style);
1465
+ await ensureSftpDirectory(sftp, parent, deadline, style, signal);
1323
1466
  }
1324
1467
  try {
1325
- await sftpMkdir(sftp, normalizedPath, timeout);
1468
+ await sftpMkdir(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal);
1326
1469
  }
1327
1470
  catch (error) {
1328
1471
  try {
1329
- const stats = await sftpStat(sftp, normalizedPath, timeout);
1472
+ const stats = await sftpStat(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal);
1330
1473
  if ((stats.mode & S_IFMT) === S_IFDIR) {
1331
1474
  return;
1332
1475
  }
@@ -1340,15 +1483,16 @@ async function ensureSftpDirectory(sftp, remotePath, timeout, style = 'posix') {
1340
1483
  /**
1341
1484
  * 创建目录
1342
1485
  */
1343
- export async function mkdir(alias, remotePath, recursive = false, sharedSftp, timeout, style = 'posix') {
1344
- const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, timeout));
1486
+ export async function mkdir(alias, remotePath, recursive = false, sharedSftp, timeout, style = 'posix', signal) {
1487
+ const deadline = timeout === undefined ? undefined : Date.now() + timeout;
1488
+ const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
1345
1489
  return withSftpDeadline(sftp, (async () => {
1346
1490
  try {
1347
1491
  if (recursive) {
1348
- await ensureSftpDirectory(sftp, remotePath, timeout, style);
1492
+ await ensureSftpDirectory(sftp, remotePath, deadline, style, signal);
1349
1493
  }
1350
1494
  else {
1351
- await sftpMkdir(sftp, remotePath, timeout);
1495
+ await sftpMkdir(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
1352
1496
  }
1353
1497
  return true;
1354
1498
  }
@@ -1357,7 +1501,7 @@ export async function mkdir(alias, remotePath, recursive = false, sharedSftp, ti
1357
1501
  sftp.end();
1358
1502
  }
1359
1503
  }
1360
- })(), timeout);
1504
+ })(), remainingSftpTimeout(deadline, signal), signal);
1361
1505
  }
1362
1506
  /**
1363
1507
  * 检查远程是否安装 rsync
@@ -1447,7 +1591,9 @@ export async function checkRsync(alias, timeout = 10_000, executable = 'rsync',
1447
1591
  retryable: result.failureKind !== 'remote_command',
1448
1592
  exitCode: result.exitCode,
1449
1593
  failureKind: result.failureKind,
1450
- error: result.stderr.trim() || result.stdout.trim() || `rsync probe failed with exit code ${result.exitCode}`,
1594
+ error: result.stderr.trim() ||
1595
+ result.stdout.trim() ||
1596
+ 'rsync probe failed with exit code ' + `${result.exitCode}`,
1451
1597
  };
1452
1598
  }
1453
1599
  catch (error) {
@@ -1460,6 +1606,51 @@ export async function checkRsync(alias, timeout = 10_000, executable = 'rsync',
1460
1606
  };
1461
1607
  }
1462
1608
  }
1609
+ function parseWindowsCaseSemantics(output, names) {
1610
+ const semantics = JSON.parse(output);
1611
+ if (typeof semantics?.caseSensitive !== 'boolean' ||
1612
+ !Object.hasOwn(semantics, 'collision') ||
1613
+ (semantics.collision !== null &&
1614
+ (!Array.isArray(semantics.collision) ||
1615
+ semantics.collision.length !== 2 ||
1616
+ semantics.collision[0] === semantics.collision[1] ||
1617
+ !semantics.collision.every((name) => typeof name === 'string' && names.includes(name)))) ||
1618
+ (semantics.caseSensitive && semantics.collision !== null)) {
1619
+ throw new Error('Windows case semantics query returned incomplete fields');
1620
+ }
1621
+ return semantics.collision;
1622
+ }
1623
+ function verifyWindowsDownloadNames(localPath, remoteNames, timeout) {
1624
+ const existing = fs.existsSync(localPath) ? fs.readdirSync(localPath) : [];
1625
+ const names = [...new Set([...existing, ...remoteNames])];
1626
+ if (names.length < 2) {
1627
+ return;
1628
+ }
1629
+ let parent = localPath;
1630
+ while (!fs.existsSync(parent)) {
1631
+ parent = path.dirname(parent);
1632
+ }
1633
+ try {
1634
+ const script = encodedPowershell(windowsCaseSemanticsScript(parent)).split(' ').at(-1);
1635
+ const result = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-EncodedCommand', script], {
1636
+ input: Buffer.from(JSON.stringify(names)).toString('base64'),
1637
+ encoding: 'utf8',
1638
+ windowsHide: true,
1639
+ timeout: Math.min(timeout ?? 10_000, 10_000),
1640
+ maxBuffer: 16_384,
1641
+ });
1642
+ if (result.error || result.status !== 0) {
1643
+ throw new Error('Windows directory case query failed');
1644
+ }
1645
+ const collision = parseWindowsCaseSemantics(result.stdout, names);
1646
+ if (collision) {
1647
+ throw new Error(`Windows local filename collision at ${localPath}: ${collision.join(' / ')}`);
1648
+ }
1649
+ }
1650
+ catch (error) {
1651
+ throw new Error(`case_semantics_unverified: ${localPath}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
1652
+ }
1653
+ }
1463
1654
  async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
1464
1655
  const candidates = [];
1465
1656
  const style = options.remotePathStyle ?? sessionManager.getRemotePathStyle(alias);
@@ -1474,7 +1665,7 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
1474
1665
  candidates.push({ parent, names });
1475
1666
  }
1476
1667
  });
1477
- const deadline = Date.now() + (options.preflightTimeout ?? 10_000);
1668
+ const deadline = Math.min(Date.now() + (options.preflightTimeout ?? 10_000), options.deadline ?? Infinity);
1478
1669
  for (const { parent, names } of candidates) {
1479
1670
  let collision;
1480
1671
  try {
@@ -1490,18 +1681,7 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
1490
1681
  result.stderrTruncated) {
1491
1682
  throw new Error(result.stderr || 'Windows case semantics query failed or returned truncated output');
1492
1683
  }
1493
- const semantics = JSON.parse(result.stdout);
1494
- if (typeof semantics?.caseSensitive !== 'boolean' ||
1495
- !Object.hasOwn(semantics, 'collision') ||
1496
- (semantics.collision !== null &&
1497
- (!Array.isArray(semantics.collision) ||
1498
- semantics.collision.length !== 2 ||
1499
- semantics.collision[0] === semantics.collision[1] ||
1500
- !semantics.collision.every((name) => typeof name === 'string' && names.includes(name)))) ||
1501
- (semantics.caseSensitive && semantics.collision !== null)) {
1502
- throw new Error('Windows case semantics query returned incomplete fields');
1503
- }
1504
- collision = semantics.collision;
1684
+ collision = parseWindowsCaseSemantics(result.stdout, names);
1505
1685
  }
1506
1686
  catch (error) {
1507
1687
  throw new Error(`case_semantics_unverified: ${parent}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
@@ -1522,10 +1702,12 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
1522
1702
  */
1523
1703
  export async function syncFiles(alias, localPath, remotePath, direction, options = {}) {
1524
1704
  const startedAt = Date.now();
1705
+ options = { ...options, deadline: options.deadline ?? startedAt + (options.operationTimeout ?? 600_000) };
1706
+ remainingSftpTimeout(options.deadline, options.signal);
1525
1707
  localPath = expandTilde(localPath);
1526
1708
  validateLocalPathAgainstAllowList(localPath);
1527
1709
  if (remotePath === '~' || remotePath.startsWith('~/')) {
1528
- throw new Error('remotePath must be absolute or relative to the SSH working directory; ~ is not supported');
1710
+ throw new Error('remotePath must be absolute or relative to the SSH working directory; ~ ' + 'is not supported');
1529
1711
  }
1530
1712
  let sourceIsDirectory;
1531
1713
  if (direction === 'upload') {
@@ -1534,10 +1716,10 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
1534
1716
  sourceIsDirectory = source.stats.isDirectory();
1535
1717
  }
1536
1718
  else {
1537
- sourceIsDirectory = (await getFileInfo(alias, remotePath, undefined, options.remotePathStyle)).isDirectory;
1719
+ sourceIsDirectory = (await getFileInfo(alias, remotePath, remainingSftpTimeout(options.deadline, options.signal), options.remotePathStyle, options.signal)).isDirectory;
1538
1720
  }
1539
1721
  if (sourceIsDirectory && options.recursive === false) {
1540
- throw new Error('recursive=false is not supported for directory sources; use a file source or enable recursion');
1722
+ throw new Error('recursive=false is not supported for directory sources; use a file ' + 'source or enable recursion');
1541
1723
  }
1542
1724
  const capabilityStartedAt = Date.now();
1543
1725
  const rsyncGeneration = getRsyncGeneration(alias);
@@ -1546,10 +1728,12 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
1546
1728
  assertCurrentRsyncGeneration(alias, rsyncGeneration);
1547
1729
  }
1548
1730
  const capability = sessionManager.getExternalTransferCapability(alias);
1549
- const preflightTimeout = options.preflightTimeout ?? 10_000;
1731
+ const preflightTimeout = Math.min(options.preflightTimeout ?? 10_000, remainingSftpTimeout(options.deadline, options.signal));
1550
1732
  const connection = sessionManager.getOpenSshConnection(alias);
1551
1733
  if (connection) {
1552
- await connection.capture('command -v find stat head realpath >/dev/null && find --version >/dev/null && stat --version >/dev/null && head --version >/dev/null && realpath --version >/dev/null', preflightTimeout, 4096);
1734
+ await connection.capture('command -v find stat head realpath >/dev/null && find --version ' +
1735
+ '>/dev/null && stat --version >/dev/null && head --version >/dev/null && ' +
1736
+ 'realpath --version >/dev/null', preflightTimeout, 4096);
1553
1737
  }
1554
1738
  const rsyncProbe = capability.rsyncEligible
1555
1739
  ? await checkRsync(alias, preflightTimeout, capability.rsyncExecutable, capability.commandShell)
@@ -1624,17 +1808,18 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1624
1808
  assertCurrentRsyncGeneration(alias, generation);
1625
1809
  const connection = sessionManager.getOpenSshConnection(alias);
1626
1810
  const fallbackToSftp = (reason) => {
1627
- if (connection)
1811
+ if (connection) {
1628
1812
  throw new OpenSshError('OpenSSH master sync preflight failed; the selected backend was preserved', {
1629
1813
  code: 'openssh_sync_preflight',
1630
1814
  backend: 'openssh',
1631
1815
  reason,
1632
1816
  retryable: false,
1633
1817
  });
1818
+ }
1634
1819
  return syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
1635
1820
  };
1636
1821
  const checkCommand = os.platform() === 'win32' ? 'where' : 'which';
1637
- const checkResult = await runLocalProcess(checkCommand, ['rsync'], options.preflightTimeout ?? 10_000);
1822
+ const checkResult = await runLocalProcess(checkCommand, ['rsync'], Math.min(options.preflightTimeout ?? 10_000, remainingSftpTimeout(options.deadline, options.signal)));
1638
1823
  assertCurrentRsyncGeneration(alias, generation);
1639
1824
  if (checkResult.status !== 0) {
1640
1825
  const fallback = await fallbackToSftp(checkResult.timedOut ? 'local_rsync_preflight_timeout' : 'local_rsync_unavailable');
@@ -1646,6 +1831,9 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1646
1831
  }
1647
1832
  // 构建 rsync 参数
1648
1833
  const args = ['-avz', '--itemize-changes', '--stats'];
1834
+ if (options.onProgress) {
1835
+ args.push('--progress');
1836
+ }
1649
1837
  if (options.delete) {
1650
1838
  args.push('--delete');
1651
1839
  }
@@ -1736,7 +1924,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1736
1924
  : capability.commandShell === 'cmd'
1737
1925
  ? 'cmd.exe /D /C exit 0'
1738
1926
  : 'true',
1739
- ], connectTimeout, childEnv, connection);
1927
+ ], Math.min(connectTimeout, remainingSftpTimeout(options.deadline, options.signal)), childEnv, connection);
1740
1928
  const connectDuration = Date.now() - connectStartedAt;
1741
1929
  assertCurrentRsyncGeneration(alias, generation);
1742
1930
  if (connectResult.error || connectResult.status !== 0) {
@@ -1754,11 +1942,11 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1754
1942
  };
1755
1943
  }
1756
1944
  if (capability.commandShell && capability.commandShell !== 'posix') {
1757
- const remoteProbe = await runLocalProcess('ssh', [...sshOptions, destination, `${remoteCommand} --version`], connectTimeout, childEnv);
1945
+ const remoteProbe = await runLocalProcess('ssh', [...sshOptions, destination, `${remoteCommand} --version`], Math.min(connectTimeout, remainingSftpTimeout(options.deadline, options.signal)), childEnv);
1758
1946
  assertCurrentRsyncGeneration(alias, generation);
1759
1947
  if (remoteProbe.error || remoteProbe.status !== 0) {
1760
1948
  const fallback = await fallbackToSftp(remoteProbe.stderr.trim() || remoteProbe.error?.message || 'remote_rsync_unavailable');
1761
- const reason = remoteProbe.stderr.trim() || remoteProbe.error?.message || `exit code ${remoteProbe.status}`;
1949
+ const reason = remoteProbe.stderr.trim() || remoteProbe.error?.message || 'exit code ' + `${remoteProbe.status}`;
1762
1950
  return {
1763
1951
  ...fallback,
1764
1952
  connectDuration,
@@ -1789,10 +1977,59 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1789
1977
  const rsyncArgs = direction === 'upload'
1790
1978
  ? [...args, '-e', sshCmd, '--', rsyncLocalPath, remoteSpec]
1791
1979
  : [...args, '-e', sshCmd, '--', remoteSpec, localRsyncPath];
1980
+ if (direction === 'download' && !options.dryRun) {
1981
+ try {
1982
+ remainingSftpTimeout(options.deadline, options.signal);
1983
+ validateLocalPathAgainstAllowList(localPath);
1984
+ fs.mkdirSync(path.dirname(path.resolve(localPath)), { recursive: true });
1985
+ }
1986
+ catch (error) {
1987
+ return {
1988
+ success: false,
1989
+ method: 'rsync',
1990
+ connectDuration,
1991
+ connectStatus: 'completed',
1992
+ operationStatus: 'failed',
1993
+ failureStage: 'local_prepare',
1994
+ retryable: false,
1995
+ output: `Local download parent preparation failed: ${error instanceof Error ? error.message : String(error)}`,
1996
+ };
1997
+ }
1998
+ }
1792
1999
  try {
1793
2000
  assertCurrentRsyncGeneration(alias, generation);
1794
- const operationTimeout = options.operationTimeout ?? 600_000;
1795
- const result = await runLocalProcess('rsync', rsyncArgs, operationTimeout, childEnv, connection);
2001
+ const operationTimeout = remainingSftpTimeout(options.deadline, options.signal) ?? options.operationTimeout ?? 600_000;
2002
+ const stats = { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 0 };
2003
+ let partial = '';
2004
+ let discardTail = false;
2005
+ const collectStats = (chunk, final = false) => {
2006
+ for (const [index, segment] of chunk.split(/[\r\n]/).entries()) {
2007
+ if (index > 0) {
2008
+ const progress = /^\s*([\d,]+)\s+(\d+)%\s+[\d.]+[kMGTPE]?B\/s/i.exec(partial);
2009
+ if (progress) {
2010
+ options.onProgress?.({
2011
+ transferred: Number(progress[1].replace(/,/g, '')),
2012
+ total: 0,
2013
+ percent: Number(progress[2]),
2014
+ });
2015
+ }
2016
+ parseRsyncStats(partial, stats);
2017
+ partial = '';
2018
+ discardTail = false;
2019
+ }
2020
+ if (!discardTail) {
2021
+ // Only the itemization prefix and summaries are needed, never an unbounded filename.
2022
+ const available = 65_536 - partial.length;
2023
+ partial += segment.slice(0, available);
2024
+ discardTail = segment.length > available;
2025
+ }
2026
+ }
2027
+ if (final && partial) {
2028
+ parseRsyncStats(partial, stats);
2029
+ partial = '';
2030
+ }
2031
+ };
2032
+ const result = await runLocalProcess('rsync', rsyncArgs, operationTimeout, childEnv, connection, collectStats);
1796
2033
  if (!isCurrentRsyncGeneration(alias, generation)) {
1797
2034
  return {
1798
2035
  success: false,
@@ -1820,7 +2057,10 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1820
2057
  timedOut: true,
1821
2058
  retryable: true,
1822
2059
  fallbackReason: 'rsync_operation_timeout',
1823
- output: `rsync ${reason}${stderr ? ` | stderr: ${stderr}` : ''}; remote transfer state is unknown`,
2060
+ output: 'rsync ' +
2061
+ `${reason}` +
2062
+ `${stderr ? ` | stderr: ${stderr}` : ''}` +
2063
+ '; remote transfer state is unknown',
1824
2064
  };
1825
2065
  }
1826
2066
  return {
@@ -1831,11 +2071,13 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1831
2071
  operationStatus: 'unknown',
1832
2072
  retryable: true,
1833
2073
  fallbackReason: 'rsync_operation_failed',
1834
- output: `rsync failed: ${reason}${stderr ? ` | stderr: ${stderr}` : ''}; remote transfer state is unknown`,
2074
+ output: 'rsync failed: ' +
2075
+ `${reason}` +
2076
+ `${stderr ? ` | stderr: ${stderr}` : ''}` +
2077
+ '; remote transfer state is unknown',
1835
2078
  };
1836
2079
  }
1837
2080
  const output = result.stdout;
1838
- const stats = parseRsyncStats(output);
1839
2081
  const filesTransferred = stats.added + stats.updated;
1840
2082
  return {
1841
2083
  success: true,
@@ -1855,6 +2097,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1855
2097
  usedKeyPath: Boolean(capability.keyPath),
1856
2098
  },
1857
2099
  output,
2100
+ outputTruncated: result.stdoutTruncated || undefined,
1858
2101
  };
1859
2102
  }
1860
2103
  catch (e) {
@@ -1869,7 +2112,9 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1869
2112
  operationStatus: 'unknown',
1870
2113
  retryable: true,
1871
2114
  fallbackReason: 'rsync_execution_error',
1872
- output: `rsync execution failed: ${e instanceof Error ? e.message : String(e)}; remote transfer state is unknown`,
2115
+ output: 'rsync execution failed: ' +
2116
+ `${e instanceof Error ? e.message : String(e)}` +
2117
+ '; remote transfer state is unknown',
1873
2118
  };
1874
2119
  }
1875
2120
  }
@@ -1943,22 +2188,21 @@ function buildSyncResult(fileCount, totalSize, warnings, skippedSymlinks = 0, st
1943
2188
  output: warnings.length ? `Warning: ${warnings.join('; ')}` : undefined,
1944
2189
  };
1945
2190
  }
1946
- function parseRsyncStats(output) {
1947
- const stats = { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 0 };
2191
+ function parseRsyncStats(output, stats) {
1948
2192
  for (const line of output.split('\n')) {
1949
2193
  if (line.startsWith('*deleting')) {
1950
2194
  stats.deleted++;
1951
2195
  }
1952
- else if (/^>f\+{9}\s/.test(line)) {
2196
+ else if (/^[<>]f\+{9}\s/.test(line)) {
1953
2197
  stats.added++;
1954
2198
  }
1955
- else if (/^>f/.test(line)) {
2199
+ else if (/^[<>]f\S{9}\s/.test(line)) {
1956
2200
  stats.updated++;
1957
2201
  }
1958
2202
  }
1959
- const transferredMatch = output.match(/Number of regular files transferred:\s*(?<count>\d+)/);
2203
+ const transferredMatch = output.match(/Number of regular files transferred:\s*(?<count>[\d,]+)/);
1960
2204
  if (transferredMatch?.groups && stats.added + stats.updated === 0) {
1961
- stats.updated = Number(transferredMatch.groups.count);
2205
+ stats.updated = Number(transferredMatch.groups.count.replace(/,/g, ''));
1962
2206
  }
1963
2207
  return stats;
1964
2208
  }
@@ -1974,27 +2218,32 @@ export function hashLocalFile(filePath, algorithm = 'sha256', timeout, limits) {
1974
2218
  let bytes = 0;
1975
2219
  let settled = false;
1976
2220
  const finish = (error) => {
1977
- if (settled)
2221
+ if (settled) {
1978
2222
  return;
2223
+ }
1979
2224
  settled = true;
1980
- if (timer)
2225
+ if (timer) {
1981
2226
  clearTimeout(timer);
2227
+ }
1982
2228
  if (error) {
1983
2229
  stream.destroy();
1984
2230
  reject(error);
1985
2231
  }
1986
- else
2232
+ else {
1987
2233
  resolve(hash.digest('hex'));
2234
+ }
1988
2235
  };
1989
2236
  const timer = timeout === undefined
1990
2237
  ? undefined
1991
2238
  : setTimeout(() => finish(new Error(`Local file hashing timed out after ${timeout}ms`)), timeout);
1992
2239
  stream.on('data', (chunk) => {
1993
2240
  bytes += Buffer.byteLength(chunk);
1994
- if (bytes > before.size || (limits && bytes > limits.maxBytes))
2241
+ if (bytes > before.size || (limits && bytes > limits.maxBytes)) {
1995
2242
  finish(new Error('Local file grew during verification'));
1996
- else
2243
+ }
2244
+ else {
1997
2245
  hash.update(chunk);
2246
+ }
1998
2247
  });
1999
2248
  stream.on('error', finish);
2000
2249
  stream.on('end', () => {
@@ -2014,9 +2263,9 @@ export function hashLocalFile(filePath, algorithm = 'sha256', timeout, limits) {
2014
2263
  });
2015
2264
  });
2016
2265
  }
2017
- async function hashRemoteFile(alias, remotePath) {
2266
+ async function hashRemoteFile(alias, remotePath, timeout, signal) {
2018
2267
  try {
2019
- const digest = await hashRemoteFileDigest(alias, remotePath, 'sha256');
2268
+ const digest = await hashRemoteFileDigest(alias, remotePath, 'sha256', timeout, undefined, signal);
2020
2269
  return { status: 'available', digest, retryable: false };
2021
2270
  }
2022
2271
  catch (error) {
@@ -2027,23 +2276,31 @@ async function hashRemoteFile(alias, remotePath) {
2027
2276
  };
2028
2277
  }
2029
2278
  }
2030
- async function buildSingleFileVerification(alias, localPath, remotePath, direction) {
2279
+ async function buildSingleFileVerification(alias, localPath, remotePath, direction, verifySha256, deadline, signal) {
2031
2280
  try {
2032
2281
  const localStats = fs.statSync(localPath);
2033
2282
  const [remoteInfo, localSha256, remoteHash] = await Promise.all([
2034
- getFileInfo(alias, remotePath),
2035
- hashLocalFile(localPath),
2036
- hashRemoteFile(alias, remotePath),
2283
+ getFileInfo(alias, remotePath, remainingSftpTimeout(deadline, signal), 'posix', signal),
2284
+ verifySha256 ? hashLocalFile(localPath, 'sha256', remainingSftpTimeout(deadline, signal)) : undefined,
2285
+ verifySha256
2286
+ ? hashRemoteFile(alias, remotePath, remainingSftpTimeout(deadline, signal), signal)
2287
+ : undefined,
2037
2288
  ]);
2289
+ const timedOut = signal?.reason instanceof SftpOperationTimeoutError || (deadline !== undefined && Date.now() >= deadline);
2038
2290
  return {
2039
2291
  kind: 'single_file',
2040
2292
  direction,
2041
- status: remoteHash.status === 'available' ? 'completed' : 'error',
2042
- failureStage: remoteHash.status === 'error' ? 'remote_hash' : undefined,
2043
- retryable: remoteHash.retryable,
2044
- error: remoteHash.error,
2045
- hashAlgorithm: 'sha256',
2046
- hashMatch: remoteHash.digest ? localSha256 === remoteHash.digest : undefined,
2293
+ status: timedOut ? 'unknown' : remoteHash?.status === 'error' ? 'error' : 'completed',
2294
+ timedOut: timedOut || undefined,
2295
+ failureStage: timedOut
2296
+ ? 'verification_timeout'
2297
+ : remoteHash?.status === 'error'
2298
+ ? 'remote_hash'
2299
+ : undefined,
2300
+ retryable: remoteHash?.retryable ?? false,
2301
+ error: remoteHash?.error,
2302
+ hashAlgorithm: verifySha256 ? 'sha256' : undefined,
2303
+ hashMatch: remoteHash?.digest ? localSha256 === remoteHash.digest : undefined,
2047
2304
  local: {
2048
2305
  size: localStats.size,
2049
2306
  mode: (localStats.mode & 0o777).toString(8),
@@ -2056,27 +2313,31 @@ async function buildSingleFileVerification(alias, localPath, remotePath, directi
2056
2313
  owner: remoteInfo.owner,
2057
2314
  group: remoteInfo.group,
2058
2315
  mtime: remoteInfo.mtime,
2059
- sha256: remoteHash.digest,
2316
+ sha256: remoteHash?.digest,
2060
2317
  },
2061
2318
  };
2062
2319
  }
2063
2320
  catch (error) {
2321
+ const timedOut = error instanceof SftpOperationTimeoutError ||
2322
+ signal?.reason instanceof SftpOperationTimeoutError ||
2323
+ (deadline !== undefined && Date.now() >= deadline);
2064
2324
  return {
2065
2325
  kind: 'single_file',
2066
2326
  direction,
2067
- status: 'error',
2068
- failureStage: 'remote_metadata_or_hash',
2327
+ status: timedOut ? 'unknown' : 'error',
2328
+ timedOut: timedOut || undefined,
2329
+ failureStage: timedOut ? 'verification_timeout' : 'remote_metadata_or_hash',
2069
2330
  retryable: true,
2070
2331
  error: error instanceof Error ? error.message : String(error),
2071
2332
  };
2072
2333
  }
2073
2334
  }
2074
- async function resolveSftpUploadFileTarget(alias, localPath, remotePath, style = 'posix') {
2335
+ async function resolveSftpUploadFileTarget(alias, localPath, remotePath, style = 'posix', deadline, signal) {
2075
2336
  if (remotePath.endsWith('/') || remotePath.endsWith('\\')) {
2076
2337
  return remoteJoin(style, remotePath, path.basename(localPath));
2077
2338
  }
2078
2339
  try {
2079
- const info = await getFileInfo(alias, remotePath);
2340
+ const info = await getFileInfo(alias, remotePath, remainingSftpTimeout(deadline, signal), 'posix', signal);
2080
2341
  if (info.isDirectory) {
2081
2342
  return remoteJoin(style, remotePath, path.basename(localPath));
2082
2343
  }
@@ -2100,34 +2361,34 @@ function resolveSftpDownloadFileTarget(remotePath, localPath, style = 'posix') {
2100
2361
  return localPath;
2101
2362
  }
2102
2363
  async function syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options) {
2103
- const timeout = options.operationTimeout ?? 600_000;
2364
+ const timeout = remainingSftpTimeout(options.deadline) ?? options.operationTimeout ?? 600_000;
2365
+ const deadline = options.deadline ?? Date.now() + timeout;
2104
2366
  const controller = new AbortController();
2105
2367
  const accumulator = new DirectoryTransferAccumulator();
2106
2368
  const warnings = [];
2369
+ const abort = () => controller.abort(options.signal?.reason);
2370
+ options.signal?.addEventListener('abort', abort, { once: true });
2371
+ if (options.signal?.aborted) {
2372
+ abort();
2373
+ }
2107
2374
  const operation = syncWithSftp(alias, localPath, remotePath, direction, {
2108
2375
  ...options,
2109
2376
  signal: controller.signal,
2377
+ deadline,
2110
2378
  }, accumulator, warnings);
2111
- let timer;
2112
- const timedOut = new Promise((resolve) => {
2113
- timer = setTimeout(() => {
2114
- controller.abort(new SftpOperationTimeoutError(timeout));
2115
- resolve({
2116
- ...buildDirectorySyncResult(accumulator.toResult(), [...warnings]),
2117
- success: false,
2118
- method: 'sftp',
2119
- operationStatus: 'unknown',
2120
- timedOut: true,
2121
- retryable: true,
2122
- output: `SFTP operation timed out after ${timeout}ms; remote transfer state is unknown`,
2123
- });
2124
- }, timeout);
2125
- });
2126
- const result = await Promise.race([operation, timedOut]);
2127
- if (timer) {
2128
- clearTimeout(timer);
2379
+ const timer = setTimeout(() => controller.abort(new SftpOperationTimeoutError(timeout)), timeout);
2380
+ try {
2381
+ const result = await operation;
2382
+ return result.verification?.status === 'unknown'
2383
+ ? { ...result, operationStatus: 'unknown', timedOut: true }
2384
+ : result;
2385
+ }
2386
+ finally {
2387
+ if (timer) {
2388
+ clearTimeout(timer);
2389
+ }
2390
+ options.signal?.removeEventListener('abort', abort);
2129
2391
  }
2130
- return result;
2131
2392
  }
2132
2393
  async function syncWithSftp(alias, localPath, remotePath, direction, options, accumulator, warnings) {
2133
2394
  throwIfAborted(options.signal);
@@ -2137,7 +2398,7 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options, ac
2137
2398
  if (options.dryRun) {
2138
2399
  const localExists = fs.existsSync(localPath);
2139
2400
  const localStats = localExists ? fs.lstatSync(localPath) : undefined;
2140
- const remoteTargetExists = await remoteExists(alias, remotePath);
2401
+ const remoteTargetExists = await remoteExists(alias, remotePath, options.deadline, options.signal);
2141
2402
  return {
2142
2403
  success: true,
2143
2404
  method: 'sftp',
@@ -2188,26 +2449,28 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options, ac
2188
2449
  };
2189
2450
  }
2190
2451
  if (stats.isDirectory()) {
2191
- const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0, options.signal, '', options.remotePathStyle, undefined, accumulator);
2452
+ const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0, options.signal, '', options.remotePathStyle, undefined, accumulator, options.deadline, options.onProgress);
2192
2453
  return buildDirectorySyncResult(result, warnings);
2193
2454
  }
2194
- const targetRemotePath = await resolveSftpUploadFileTarget(alias, localPath, remotePath, options.remotePathStyle);
2195
- const existed = await remoteExists(alias, targetRemotePath);
2196
- const { size } = await uploadFile(alias, localPath, targetRemotePath, undefined, undefined, undefined, options.signal, options.followSymlinks === true);
2455
+ const targetRemotePath = await resolveSftpUploadFileTarget(alias, localPath, remotePath, options.remotePathStyle, options.deadline, options.signal);
2456
+ const existed = await remoteExists(alias, targetRemotePath, options.deadline, options.signal);
2457
+ const { size } = await uploadFile(alias, localPath, targetRemotePath, options.onProgress, undefined, undefined, options.signal, options.followSymlinks === true, false, remainingSftpTimeout(options.deadline, options.signal));
2458
+ accumulator.addFile(size, existed);
2197
2459
  return {
2198
2460
  ...buildSyncResult(1, size, warnings, 0, { added: existed ? 0 : 1, updated: existed ? 1 : 0 }),
2199
- verification: await buildSingleFileVerification(alias, localPath, targetRemotePath, 'upload'),
2461
+ verification: await buildSingleFileVerification(alias, localPath, targetRemotePath, 'upload', options.verifySha256 !== false, options.deadline, options.signal),
2200
2462
  };
2201
2463
  }
2202
2464
  // download
2203
- const info = await getFileInfo(alias, remotePath, undefined, options.remotePathStyle);
2465
+ const info = await getFileInfo(alias, remotePath, remainingSftpTimeout(options.deadline, options.signal), options.remotePathStyle, options.signal);
2204
2466
  if (info.isDirectory && options.recursive === false) {
2205
2467
  return {
2206
2468
  success: false,
2207
2469
  method: 'sftp',
2208
2470
  operationStatus: 'failed',
2209
2471
  stats: { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 1 },
2210
- output: 'recursive=false is not supported for directory sources; use a file source or enable recursion',
2472
+ output: 'recursive=false is not supported for directory sources; use a file ' +
2473
+ 'source or enable recursion',
2211
2474
  };
2212
2475
  }
2213
2476
  if (info.isDirectory) {
@@ -2215,26 +2478,29 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options, ac
2215
2478
  files: 0,
2216
2479
  directories: 0,
2217
2480
  bytes: 0,
2218
- }, 0, options.signal, '', options.remotePathStyle, accumulator);
2481
+ }, 0, options.signal, '', options.remotePathStyle, accumulator, options.deadline, options.onProgress);
2219
2482
  return buildDirectorySyncResult(result, warnings);
2220
2483
  }
2484
+ remainingSftpTimeout(options.deadline, options.signal);
2221
2485
  const targetLocalPath = resolveSftpDownloadFileTarget(remotePath, localPath, options.remotePathStyle);
2222
2486
  const existed = fs.existsSync(targetLocalPath);
2223
- const { size } = await downloadFile(alias, remotePath, targetLocalPath, undefined, undefined, options.signal);
2487
+ const { size } = await downloadFile(alias, remotePath, targetLocalPath, options.onProgress, undefined, options.signal, remainingSftpTimeout(options.deadline, options.signal));
2488
+ accumulator.addFile(size, existed);
2224
2489
  return {
2225
2490
  ...buildSyncResult(1, size, warnings, 0, { added: existed ? 0 : 1, updated: existed ? 1 : 0 }),
2226
- verification: await buildSingleFileVerification(alias, targetLocalPath, remotePath, 'download'),
2491
+ verification: await buildSingleFileVerification(alias, targetLocalPath, remotePath, 'download', options.verifySha256 !== false, options.deadline, options.signal),
2227
2492
  };
2228
2493
  }
2229
2494
  catch (err) {
2230
2495
  const result = buildDirectorySyncResult(accumulator.toResult(), warnings);
2231
- const timedOut = err instanceof SftpOperationTimeoutError;
2496
+ const outcome = sftpOperationOutcome(err);
2497
+ const timedOut = err instanceof SftpOperationTimeoutError || outcome.timeout !== undefined;
2232
2498
  return {
2233
2499
  ...result,
2234
2500
  success: false,
2235
- operationStatus: timedOut ? 'unknown' : 'failed',
2501
+ operationStatus: 'failed',
2502
+ ...outcome,
2236
2503
  timedOut: timedOut || undefined,
2237
- retryable: timedOut || undefined,
2238
2504
  stats: { ...result.stats, failed: Math.max(1, result.stats.failed) },
2239
2505
  output: [result.output, err instanceof Error ? err.message : String(err)].filter(Boolean).join('; '),
2240
2506
  };
@@ -2243,13 +2509,15 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options, ac
2243
2509
  /**
2244
2510
  * 递归上传目录
2245
2511
  */
2246
- async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', inspectNames, accumulator = new DirectoryTransferAccumulator()) {
2512
+ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', inspectNames, accumulator = new DirectoryTransferAccumulator(), deadline, onProgress) {
2247
2513
  throwIfAborted(signal);
2248
2514
  recordSftpDirectory(traversal, depth, localPath);
2515
+ const realPath = fs.realpathSync(localPath);
2249
2516
  const ownSftp = !sharedSftp && !inspectNames;
2250
- const sftp = inspectNames ? undefined : (sharedSftp ?? (await getSftpWithinDeadline(alias)));
2517
+ const sftp = inspectNames
2518
+ ? undefined
2519
+ : (sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal)));
2251
2520
  const activeDirectories = (traversal.activeDirectories ??= new Set());
2252
- const realPath = fs.realpathSync(localPath);
2253
2521
  if (activeDirectories.has(realPath)) {
2254
2522
  accumulator.addSkippedSymlink();
2255
2523
  if (ownSftp) {
@@ -2260,8 +2528,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
2260
2528
  activeDirectories.add(realPath);
2261
2529
  try {
2262
2530
  validateLocalPathAgainstAllowList(localPath);
2531
+ remainingSftpTimeout(deadline, signal);
2263
2532
  if (!inspectNames) {
2264
- await mkdir(alias, remotePath, true, sftp, undefined, style);
2533
+ await mkdir(alias, remotePath, true, sftp, remainingSftpTimeout(deadline, signal), style, signal);
2265
2534
  }
2266
2535
  throwIfAborted(signal);
2267
2536
  const items = fs.readdirSync(localPath);
@@ -2275,6 +2544,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
2275
2544
  }
2276
2545
  const itemLocalPath = path.join(localPath, item);
2277
2546
  const stats = followSymlinks ? fs.statSync(itemLocalPath) : fs.lstatSync(itemLocalPath);
2547
+ if (matchesDirectoryExclude(itemRelativePath, exclude, stats.isDirectory())) {
2548
+ continue;
2549
+ }
2278
2550
  if (!followSymlinks && stats.isSymbolicLink()) {
2279
2551
  if (!inspectNames) {
2280
2552
  console.warn(`[ssh_sync] skipping symlink: ${itemLocalPath}`);
@@ -2305,9 +2577,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
2305
2577
  await runWithConcurrency(fileItems, SFTP_PARALLEL_LIMIT, async ({ name, stats }) => {
2306
2578
  const itemLocalPath = path.join(localPath, name);
2307
2579
  const itemRemotePath = remoteJoin(style, remotePath, name);
2308
- const existed = await sftpExists(sftp, itemRemotePath);
2580
+ const existed = await sftpExists(sftp, itemRemotePath, remainingSftpTimeout(deadline, signal), signal);
2309
2581
  try {
2310
- await uploadFile(alias, itemLocalPath, itemRemotePath, undefined, sftp, undefined, signal, followSymlinks);
2582
+ await uploadFile(alias, itemLocalPath, itemRemotePath, onProgress, sftp, undefined, signal, followSymlinks, false, remainingSftpTimeout(deadline, signal));
2311
2583
  accumulator.addFile(stats.size, existed);
2312
2584
  }
2313
2585
  catch (error) {
@@ -2320,7 +2592,7 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
2320
2592
  for (const item of subDirItems) {
2321
2593
  const itemLocalPath = path.join(localPath, item);
2322
2594
  const itemRemotePath = remoteJoin(style, remotePath, item);
2323
- await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item}` : item, style, inspectNames, accumulator);
2595
+ await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item}` : item, style, inspectNames, accumulator, deadline, onProgress);
2324
2596
  }
2325
2597
  }
2326
2598
  finally {
@@ -2334,23 +2606,21 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
2334
2606
  /**
2335
2607
  * 递归下载目录
2336
2608
  */
2337
- async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', accumulator = new DirectoryTransferAccumulator()) {
2609
+ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', accumulator = new DirectoryTransferAccumulator(), deadline, onProgress) {
2338
2610
  throwIfAborted(signal);
2339
2611
  recordSftpDirectory(traversal, depth, remotePath);
2340
2612
  const ownSftp = !sharedSftp;
2341
- const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias));
2613
+ const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
2342
2614
  try {
2343
- const items = await listDir(alias, remotePath, true, sftp, undefined, style);
2615
+ validateLocalPathAgainstAllowList(localPath);
2616
+ const items = await listDir(alias, remotePath, true, sftp, remainingSftpTimeout(deadline, signal), style, signal);
2344
2617
  throwIfAborted(signal);
2345
2618
  if (process.platform === 'win32') {
2346
- const localNames = new Map((fs.existsSync(localPath) ? fs.readdirSync(localPath) : []).map((name) => [
2347
- name.normalize('NFC').toLowerCase(),
2348
- name,
2349
- ]));
2350
- const remoteNames = new Set();
2619
+ const remoteNames = [];
2351
2620
  for (const item of items) {
2352
2621
  const relativeName = relativeBase ? `${relativeBase}/${item.name}` : item.name;
2353
- if (matchesDirectoryExclude(relativeName, exclude) || (!item.isDirectory && !item.isFile)) {
2622
+ if (matchesDirectoryExclude(relativeName, exclude, item.isDirectory) ||
2623
+ (!item.isDirectory && !item.isFile)) {
2354
2624
  continue;
2355
2625
  }
2356
2626
  if (/[<>:"/\\|?*]/.test(item.name) ||
@@ -2359,13 +2629,12 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
2359
2629
  /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\..*)?$/i.test(item.name)) {
2360
2630
  throw new Error(`Remote entry cannot be represented as a Windows filename: ${relativeName}`);
2361
2631
  }
2362
- const key = item.name.normalize('NFC').toLowerCase();
2363
- if (remoteNames.has(key) || (localNames.has(key) && localNames.get(key) !== item.name)) {
2364
- throw new Error(`Windows local filename collision at ${relativeName}`);
2365
- }
2366
- remoteNames.add(key);
2632
+ remoteNames.push(item.name);
2367
2633
  }
2634
+ verifyWindowsDownloadNames(localPath, remoteNames, remainingSftpTimeout(deadline, signal));
2368
2635
  }
2636
+ remainingSftpTimeout(deadline, signal);
2637
+ validateLocalPathAgainstAllowList(localPath);
2369
2638
  if (!fs.existsSync(localPath)) {
2370
2639
  fs.mkdirSync(localPath, { recursive: true });
2371
2640
  }
@@ -2375,7 +2644,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
2375
2644
  const symlinkItems = [];
2376
2645
  for (const item of items) {
2377
2646
  const itemRelativePath = relativeBase ? `${relativeBase}/${item.name}` : item.name;
2378
- if (matchesDirectoryExclude(itemRelativePath, exclude)) {
2647
+ if (matchesDirectoryExclude(itemRelativePath, exclude, item.isDirectory)) {
2379
2648
  continue;
2380
2649
  }
2381
2650
  if (item.isDirectory) {
@@ -2396,7 +2665,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
2396
2665
  const itemLocalPath = path.join(localPath, item.name);
2397
2666
  const existed = fs.existsSync(itemLocalPath);
2398
2667
  try {
2399
- await downloadFile(alias, item.path, itemLocalPath, undefined, sftp, signal);
2668
+ await downloadFile(alias, item.path, itemLocalPath, onProgress, sftp, signal, remainingSftpTimeout(deadline, signal));
2400
2669
  accumulator.addFile(item.size, existed);
2401
2670
  }
2402
2671
  catch (error) {
@@ -2406,7 +2675,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
2406
2675
  });
2407
2676
  for (const item of dirItems) {
2408
2677
  const itemLocalPath = path.join(localPath, item.name);
2409
- await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item.name}` : item.name, style, accumulator);
2678
+ await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item.name}` : item.name, style, accumulator, deadline, onProgress);
2410
2679
  }
2411
2680
  for (const item of symlinkItems) {
2412
2681
  console.warn(`[ssh_sync] skipping remote symlink: ${item.path}`);