@pyrokine/mcp-ssh 3.0.0 → 3.1.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
@@ -2,6 +2,7 @@
2
2
  * SSH File Operations - 文件操作
3
3
  */
4
4
  import { spawn } from 'child_process';
5
+ import { OpenSshError } from './openssh.js';
5
6
  import { createHash, randomBytes } from 'crypto';
6
7
  import * as fs from 'fs';
7
8
  import * as os from 'os';
@@ -31,22 +32,38 @@ function appendLimitedOutput(current, chunk) {
31
32
  }
32
33
  return (current + chunk).slice(0, LOCAL_PROCESS_OUTPUT_LIMIT);
33
34
  }
34
- function runLocalProcess(command, args, timeout, env) {
35
+ function runLocalProcess(command, args, timeout, env, connection) {
35
36
  return new Promise((resolve) => {
36
37
  let stdout = '';
37
38
  let stderr = '';
38
39
  let settled = false;
39
40
  let timedOut = false;
40
41
  let killTimer;
42
+ connection?.assertCurrent();
41
43
  const child = spawn(command, args, {
42
44
  stdio: ['ignore', 'pipe', 'pipe'],
43
45
  env: env ?? process.env,
44
46
  windowsHide: true,
47
+ detached: connection !== undefined,
45
48
  });
49
+ connection?.trackProcess(child, true);
50
+ const kill = (signal) => {
51
+ if (connection && child.pid) {
52
+ try {
53
+ process.kill(-child.pid, signal);
54
+ }
55
+ catch (error) {
56
+ if (error.code !== 'ESRCH')
57
+ console.error('Local process cleanup failed:', error);
58
+ }
59
+ }
60
+ else
61
+ child.kill(signal);
62
+ };
46
63
  const timer = setTimeout(() => {
47
64
  timedOut = true;
48
- child.kill('SIGTERM');
49
- killTimer = setTimeout(() => child.kill('SIGKILL'), 5000);
65
+ kill('SIGTERM');
66
+ killTimer = setTimeout(() => kill('SIGKILL'), 5000);
50
67
  }, timeout);
51
68
  child.stdout.on('data', (chunk) => {
52
69
  stdout = appendLimitedOutput(stdout, chunk.toString('utf-8'));
@@ -158,18 +175,29 @@ async function runWithConcurrency(items, limit, worker) {
158
175
  }
159
176
  const it = items[Symbol.iterator]();
160
177
  const runners = [];
178
+ let failed = false;
161
179
  for (let i = 0; i < Math.min(limit, items.length); i++) {
162
180
  runners.push((async () => {
163
- while (true) {
181
+ while (!failed) {
164
182
  const next = it.next();
165
183
  if (next.done) {
166
184
  return;
167
185
  }
168
- await worker(next.value);
186
+ try {
187
+ await worker(next.value);
188
+ }
189
+ catch (error) {
190
+ failed = true;
191
+ throw error;
192
+ }
169
193
  }
170
194
  })());
171
195
  }
172
- await Promise.all(runners);
196
+ const settled = await Promise.allSettled(runners);
197
+ const failure = settled.find((result) => result.status === 'rejected');
198
+ if (failure?.status === 'rejected') {
199
+ throw failure.reason;
200
+ }
173
201
  }
174
202
  /**
175
203
  * 校验本地路径是否在 SSH_MCP_FILE_OPS_ALLOW_DIRS 白名单内
@@ -376,12 +404,16 @@ async function remoteExists(alias, remotePath) {
376
404
  sftp.end();
377
405
  }
378
406
  }
379
- export async function hashRemoteFileDigest(alias, remotePath, algorithm) {
380
- const sftp = await getSftpWithinDeadline(alias);
407
+ export async function hashRemoteFileDigest(alias, remotePath, algorithm, timeout, limits) {
408
+ const connection = sessionManager.getOpenSshConnection(alias);
409
+ if (connection)
410
+ return connection.hashFile(remotePath, algorithm, timeout ?? 600_000, limits?.maxBytes, limits?.expectedSize);
411
+ const sftp = await getSftpWithinDeadline(alias, timeout);
381
412
  return withSftpDeadline(sftp, new Promise((resolve, reject) => {
382
413
  const hash = createHash(algorithm);
383
414
  const stream = sftp.createReadStream(remotePath);
384
415
  let settled = false;
416
+ let bytes = 0;
385
417
  const finish = (error) => {
386
418
  if (settled) {
387
419
  return;
@@ -395,10 +427,22 @@ export async function hashRemoteFileDigest(alias, remotePath, algorithm) {
395
427
  resolve(hash.digest('hex'));
396
428
  }
397
429
  };
398
- stream.on('data', (chunk) => hash.update(chunk));
430
+ stream.on('data', (chunk) => {
431
+ if (settled)
432
+ return;
433
+ bytes += chunk.length;
434
+ if (limits && (bytes > limits.maxBytes || bytes > limits.expectedSize)) {
435
+ stream.destroy();
436
+ finish(new Error('Remote file grew or exceeded the verification byte budget'));
437
+ }
438
+ else
439
+ hash.update(chunk);
440
+ });
399
441
  stream.once('error', finish);
400
- stream.once('end', () => finish());
401
- }));
442
+ stream.once('end', () => finish(limits && bytes !== limits.expectedSize
443
+ ? new Error('Remote file changed during verification')
444
+ : undefined));
445
+ }), timeout);
402
446
  }
403
447
  export async function atomicReplaceCapability(alias, timeout) {
404
448
  const sftp = await getSftpWithinDeadline(alias, timeout);
@@ -743,13 +787,14 @@ function detectReadEncoding(firstBytes, requested) {
743
787
  }
744
788
  return 'utf8';
745
789
  }
746
- function decodeReadBuffer(bytes, encoding, binary, budgetTruncated) {
790
+ function decodeReadBuffer(bytes, encoding, binary, budgetTruncated, atFileStart) {
747
791
  if (binary) {
748
792
  return { content: bytes.toString('base64'), content_base64: bytes.toString('base64'), read_bytes: bytes.length };
749
793
  }
750
794
  const label = encoding === 'utf8' ? 'utf-8' : encoding === 'gbk' ? 'gbk' : encoding === 'utf16le' ? 'utf-16le' : 'utf-16be';
751
795
  const invalid = () => new Error(`Invalid ${encoding} sequence or offset is not a character boundary`);
752
- const decoder = new TextDecoder(label, { fatal: true });
796
+ const decoderOptions = { fatal: true, ignoreBOM: !atFileStart };
797
+ const decoder = new TextDecoder(label, decoderOptions);
753
798
  let content;
754
799
  try {
755
800
  // Streaming validation rejects malformed complete characters before considering an incomplete suffix.
@@ -770,7 +815,7 @@ function decodeReadBuffer(bytes, encoding, binary, budgetTruncated) {
770
815
  // Only a byte-budget boundary may leave up to three bytes of an incomplete character.
771
816
  for (let end = bytes.length - 1; end >= Math.max(0, bytes.length - 3); --end) {
772
817
  try {
773
- content = new TextDecoder(label, { fatal: true }).decode(bytes.subarray(0, end));
818
+ content = new TextDecoder(label, decoderOptions).decode(bytes.subarray(0, end));
774
819
  }
775
820
  catch {
776
821
  continue;
@@ -893,7 +938,7 @@ async function readLineRange(sftp, remotePath, totalSize, maxBytes, lineRange, e
893
938
  sftp.end();
894
939
  }
895
940
  const raw = Buffer.concat(chunks);
896
- const decoded = decodeReadBuffer(raw, detected, binary, byteLimitReached);
941
+ const decoded = decodeReadBuffer(raw, detected, binary, byteLimitReached, lineStart === 1);
897
942
  if (decoded.read_bytes < raw.length) {
898
943
  byteLimitReached = true;
899
944
  }
@@ -974,6 +1019,23 @@ export async function readFile(alias, remotePath, options) {
974
1019
  sftp.end();
975
1020
  return emptyReadResult(actualSize, sampleKind, requestedOffset, readOptions.binary === true);
976
1021
  }
1022
+ let encoding = readOptions.encoding ?? 'auto';
1023
+ if (!readOptions.binary && encoding === 'auto' && requestedOffset > 0) {
1024
+ try {
1025
+ const first = await withSftpDeadline(sftp, new Promise((resolve, reject) => {
1026
+ const stream = sftp.createReadStream(remotePath, { start: 0, end: 1 });
1027
+ const chunks = [];
1028
+ stream.on('data', (chunk) => chunks.push(chunk));
1029
+ stream.once('error', reject);
1030
+ stream.once('end', () => resolve(Buffer.concat(chunks)));
1031
+ }), readOptions.timeout);
1032
+ encoding = detectReadEncoding(first, 'auto');
1033
+ }
1034
+ catch (error) {
1035
+ sftp.end();
1036
+ throw error;
1037
+ }
1038
+ }
977
1039
  return withSftpDeadline(sftp, new Promise((resolve, reject) => {
978
1040
  const chunks = [];
979
1041
  let settled = false;
@@ -989,7 +1051,13 @@ export async function readFile(alias, remotePath, options) {
989
1051
  }
990
1052
  try {
991
1053
  const buffer = Buffer.concat(chunks);
992
- const detected = detectReadEncoding(buffer, readOptions.encoding ?? 'auto');
1054
+ const detected = detectReadEncoding(buffer, encoding);
1055
+ if (!readOptions.binary &&
1056
+ detected.startsWith('utf16') &&
1057
+ !readOptions.tail &&
1058
+ requestedOffset % 2) {
1059
+ throw new Error(`Invalid ${detected} offset is not a character boundary`);
1060
+ }
993
1061
  let contentStart = 0;
994
1062
  if (!readOptions.binary && detected === 'utf8' && readOptions.tail && requestedOffset > 0) {
995
1063
  while (contentStart < buffer.length && isUtf8ContinuationByte(buffer[contentStart])) {
@@ -1013,7 +1081,7 @@ export async function readFile(alias, remotePath, options) {
1013
1081
  if (!readOptions.binary && buffer.length > 0 && contentStart >= buffer.length) {
1014
1082
  throw new Error(`maxBytes is too small to read a complete ${detected} tail character`);
1015
1083
  }
1016
- const decoded = decodeReadBuffer(buffer.subarray(contentStart), detected, readOptions.binary === true, requestedOffset + buffer.length < actualSize);
1084
+ const decoded = decodeReadBuffer(buffer.subarray(contentStart), detected, readOptions.binary === true, requestedOffset + buffer.length < actualSize, requestedOffset + contentStart === 0);
1017
1085
  const contentBuffer = buffer.subarray(contentStart, contentStart + decoded.read_bytes);
1018
1086
  const actualReadOffset = requestedOffset + contentStart;
1019
1087
  const nextOffset = actualReadOffset + contentBuffer.length;
@@ -1185,30 +1253,34 @@ export async function listDir(alias, remotePath, showHidden = false, sharedSftp,
1185
1253
  * 获取文件信息
1186
1254
  */
1187
1255
  export async function getFileInfo(alias, remotePath, timeout, style = 'posix') {
1188
- const sftp = await getSftpWithinDeadline(alias, timeout);
1189
- return withSftpDeadline(sftp, new Promise((resolve, reject) => {
1190
- sftp.stat(remotePath, (err, stats) => {
1256
+ const connection = sessionManager.getOpenSshConnection(alias);
1257
+ let stats;
1258
+ if (connection) {
1259
+ stats = await connection.stat(remotePath, timeout ?? 600_000);
1260
+ }
1261
+ else {
1262
+ const sftp = await getSftpWithinDeadline(alias, timeout);
1263
+ try {
1264
+ stats = await sftpStat(sftp, remotePath, timeout);
1265
+ }
1266
+ finally {
1191
1267
  sftp.end();
1192
- if (err) {
1193
- reject(err);
1194
- return;
1195
- }
1196
- resolve({
1197
- name: remotePathModule(style).basename(remotePath),
1198
- path: remotePath,
1199
- size: stats.size,
1200
- isDirectory: (stats.mode & S_IFMT) === S_IFDIR,
1201
- isFile: (stats.mode & S_IFMT) === S_IFREG,
1202
- isSymlink: (stats.mode & S_IFMT) === S_IFLNK,
1203
- mode: extractFileMode(stats.mode),
1204
- permissions: formatFilePermissions(stats.mode),
1205
- owner: stats.uid,
1206
- group: stats.gid,
1207
- mtime: new Date(stats.mtime * 1000),
1208
- atime: new Date(stats.atime * 1000),
1209
- });
1210
- });
1211
- }), timeout);
1268
+ }
1269
+ }
1270
+ return {
1271
+ name: remotePathModule(style).basename(remotePath),
1272
+ path: remotePath,
1273
+ size: stats.size,
1274
+ isDirectory: (stats.mode & S_IFMT) === S_IFDIR,
1275
+ isFile: (stats.mode & S_IFMT) === S_IFREG,
1276
+ isSymlink: (stats.mode & S_IFMT) === S_IFLNK,
1277
+ mode: extractFileMode(stats.mode),
1278
+ permissions: formatFilePermissions(stats.mode),
1279
+ owner: stats.uid,
1280
+ group: stats.gid,
1281
+ mtime: new Date(stats.mtime * 1000),
1282
+ atime: new Date(stats.atime * 1000),
1283
+ };
1212
1284
  }
1213
1285
  function sftpMkdir(sftp, remotePath, timeout) {
1214
1286
  return withSftpDeadline(sftp, new Promise((resolve, reject) => {
@@ -1475,6 +1547,10 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
1475
1547
  }
1476
1548
  const capability = sessionManager.getExternalTransferCapability(alias);
1477
1549
  const preflightTimeout = options.preflightTimeout ?? 10_000;
1550
+ const connection = sessionManager.getOpenSshConnection(alias);
1551
+ 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);
1553
+ }
1478
1554
  const rsyncProbe = capability.rsyncEligible
1479
1555
  ? await checkRsync(alias, preflightTimeout, capability.rsyncExecutable, capability.commandShell)
1480
1556
  : {
@@ -1491,6 +1567,15 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
1491
1567
  remotePathStyle: options.remotePathStyle ?? capability.remotePathStyle ?? 'posix',
1492
1568
  };
1493
1569
  const capabilityDuration = Date.now() - capabilityStartedAt;
1570
+ if (capability.backend === 'openssh' && !rsyncProbe.available) {
1571
+ throw new OpenSshError('The OpenSSH master backend requires remote rsync; no SFTP fallback is available', {
1572
+ code: 'unsupported_backend',
1573
+ backend: 'openssh',
1574
+ capability: 'rsync',
1575
+ reason: rsyncProbe.reason ?? rsyncProbe.error,
1576
+ retryable: false,
1577
+ });
1578
+ }
1494
1579
  const selectedTransport = capability.rsyncEligible && rsyncProbe?.available ? 'rsync' : 'sftp';
1495
1580
  const decisionReason = !capability.rsyncEligible
1496
1581
  ? capability.decisionReason
@@ -1537,11 +1622,22 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
1537
1622
  */
1538
1623
  async function syncWithRsync(alias, localPath, remotePath, direction, options, capability, generation) {
1539
1624
  assertCurrentRsyncGeneration(alias, generation);
1625
+ const connection = sessionManager.getOpenSshConnection(alias);
1626
+ const fallbackToSftp = (reason) => {
1627
+ if (connection)
1628
+ throw new OpenSshError('OpenSSH master sync preflight failed; the selected backend was preserved', {
1629
+ code: 'openssh_sync_preflight',
1630
+ backend: 'openssh',
1631
+ reason,
1632
+ retryable: false,
1633
+ });
1634
+ return syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
1635
+ };
1540
1636
  const checkCommand = os.platform() === 'win32' ? 'where' : 'which';
1541
1637
  const checkResult = await runLocalProcess(checkCommand, ['rsync'], options.preflightTimeout ?? 10_000);
1542
1638
  assertCurrentRsyncGeneration(alias, generation);
1543
1639
  if (checkResult.status !== 0) {
1544
- const fallback = await syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
1640
+ const fallback = await fallbackToSftp(checkResult.timedOut ? 'local_rsync_preflight_timeout' : 'local_rsync_unavailable');
1545
1641
  return {
1546
1642
  ...fallback,
1547
1643
  connectStatus: 'not_applicable',
@@ -1597,18 +1693,20 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1597
1693
  else if (capability.agent) {
1598
1694
  childEnv.SSH_AUTH_SOCK = capability.agent;
1599
1695
  }
1600
- const sshOptions = [
1601
- '-p',
1602
- String(capability.port),
1603
- '-o',
1604
- 'StrictHostKeyChecking=accept-new',
1605
- '-o',
1606
- 'BatchMode=yes',
1607
- '-o',
1608
- `ConnectTimeout=${Math.max(1, Math.ceil(connectTimeout / 1000))}`,
1609
- '-o',
1610
- 'ProxyCommand=none',
1611
- ];
1696
+ const sshOptions = connection
1697
+ ? connection.arguments().slice(0, -1)
1698
+ : [
1699
+ '-p',
1700
+ String(capability.port),
1701
+ '-o',
1702
+ 'StrictHostKeyChecking=accept-new',
1703
+ '-o',
1704
+ 'BatchMode=yes',
1705
+ '-o',
1706
+ `ConnectTimeout=${Math.max(1, Math.ceil(connectTimeout / 1000))}`,
1707
+ '-o',
1708
+ 'ProxyCommand=none',
1709
+ ];
1612
1710
  if (capability.agent === 'none') {
1613
1711
  sshOptions.push('-o', 'IdentityAgent=none');
1614
1712
  }
@@ -1638,11 +1736,11 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1638
1736
  : capability.commandShell === 'cmd'
1639
1737
  ? 'cmd.exe /D /C exit 0'
1640
1738
  : 'true',
1641
- ], connectTimeout, childEnv);
1739
+ ], connectTimeout, childEnv, connection);
1642
1740
  const connectDuration = Date.now() - connectStartedAt;
1643
1741
  assertCurrentRsyncGeneration(alias, generation);
1644
1742
  if (connectResult.error || connectResult.status !== 0) {
1645
- const fallback = await syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
1743
+ const fallback = await fallbackToSftp(connectResult.stderr.trim() || connectResult.error?.message || 'rsync_connect_failed');
1646
1744
  const fallbackReason = connectResult.timedOut ? 'rsync_connect_timeout' : 'rsync_connect_failed';
1647
1745
  const detail = connectResult.stderr.trim() || connectResult.error?.message;
1648
1746
  return {
@@ -1659,7 +1757,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1659
1757
  const remoteProbe = await runLocalProcess('ssh', [...sshOptions, destination, `${remoteCommand} --version`], connectTimeout, childEnv);
1660
1758
  assertCurrentRsyncGeneration(alias, generation);
1661
1759
  if (remoteProbe.error || remoteProbe.status !== 0) {
1662
- const fallback = await syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
1760
+ const fallback = await fallbackToSftp(remoteProbe.stderr.trim() || remoteProbe.error?.message || 'remote_rsync_unavailable');
1663
1761
  const reason = remoteProbe.stderr.trim() || remoteProbe.error?.message || `exit code ${remoteProbe.status}`;
1664
1762
  return {
1665
1763
  ...fallback,
@@ -1684,7 +1782,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1684
1782
  if (executable) {
1685
1783
  args.push(`--rsync-path=${remoteCommand}`);
1686
1784
  }
1687
- if (capability.commandShell !== 'posix' && capability.commandShell !== undefined) {
1785
+ if (connection || (capability.commandShell !== 'posix' && capability.commandShell !== undefined)) {
1688
1786
  args.push('--protect-args');
1689
1787
  }
1690
1788
  // 在 path 参数前插 `--`,明确剩余为路径而非选项(双重保险)
@@ -1694,7 +1792,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
1694
1792
  try {
1695
1793
  assertCurrentRsyncGeneration(alias, generation);
1696
1794
  const operationTimeout = options.operationTimeout ?? 600_000;
1697
- const result = await runLocalProcess('rsync', rsyncArgs, operationTimeout, childEnv);
1795
+ const result = await runLocalProcess('rsync', rsyncArgs, operationTimeout, childEnv, connection);
1698
1796
  if (!isCurrentRsyncGeneration(alias, generation)) {
1699
1797
  return {
1700
1798
  success: false,
@@ -1808,20 +1906,6 @@ class DirectoryTransferAccumulator {
1808
1906
  addFailed() {
1809
1907
  this.result.failed++;
1810
1908
  }
1811
- merge(result) {
1812
- this.result.fileCount += result.fileCount;
1813
- this.result.totalSize += result.totalSize;
1814
- this.result.skippedSymlinks += result.skippedSymlinks;
1815
- this.result.skippedUnsupported += result.skippedUnsupported;
1816
- for (const itemPath of result.unsupportedSamples) {
1817
- if (this.result.unsupportedSamples.length < 10) {
1818
- this.result.unsupportedSamples.push(itemPath);
1819
- }
1820
- }
1821
- this.result.added += result.added;
1822
- this.result.updated += result.updated;
1823
- this.result.failed += result.failed;
1824
- }
1825
1909
  toResult() {
1826
1910
  return { ...this.result };
1827
1911
  }
@@ -1841,7 +1925,7 @@ function buildDirectorySyncResult(result, warnings) {
1841
1925
  }
1842
1926
  function buildSyncResult(fileCount, totalSize, warnings, skippedSymlinks = 0, stats) {
1843
1927
  if (skippedSymlinks > 0) {
1844
- warnings.push(`skipped ${skippedSymlinks} symlink(s) (set followSymlinks=true to traverse)`);
1928
+ warnings.push(`skipped ${skippedSymlinks} symlink(s); link targets were not transferred`);
1845
1929
  }
1846
1930
  return {
1847
1931
  success: true,
@@ -1878,13 +1962,56 @@ function parseRsyncStats(output) {
1878
1962
  }
1879
1963
  return stats;
1880
1964
  }
1881
- function hashLocalFile(filePath) {
1965
+ export function hashLocalFile(filePath, algorithm = 'sha256', timeout, limits) {
1882
1966
  return new Promise((resolve, reject) => {
1883
- const hash = createHash('sha256');
1967
+ const before = fs.statSync(filePath);
1968
+ if (!before.isFile() || (limits && (before.size > limits.maxBytes || before.size !== limits.expectedSize))) {
1969
+ reject(new Error('Local file size changed or exceeds the verification byte budget'));
1970
+ return;
1971
+ }
1972
+ const hash = createHash(algorithm);
1884
1973
  const stream = fs.createReadStream(filePath);
1885
- stream.on('data', (chunk) => hash.update(chunk));
1886
- stream.on('error', reject);
1887
- stream.on('end', () => resolve(hash.digest('hex')));
1974
+ let bytes = 0;
1975
+ let settled = false;
1976
+ const finish = (error) => {
1977
+ if (settled)
1978
+ return;
1979
+ settled = true;
1980
+ if (timer)
1981
+ clearTimeout(timer);
1982
+ if (error) {
1983
+ stream.destroy();
1984
+ reject(error);
1985
+ }
1986
+ else
1987
+ resolve(hash.digest('hex'));
1988
+ };
1989
+ const timer = timeout === undefined
1990
+ ? undefined
1991
+ : setTimeout(() => finish(new Error(`Local file hashing timed out after ${timeout}ms`)), timeout);
1992
+ stream.on('data', (chunk) => {
1993
+ bytes += Buffer.byteLength(chunk);
1994
+ if (bytes > before.size || (limits && bytes > limits.maxBytes))
1995
+ finish(new Error('Local file grew during verification'));
1996
+ else
1997
+ hash.update(chunk);
1998
+ });
1999
+ stream.on('error', finish);
2000
+ stream.on('end', () => {
2001
+ try {
2002
+ const after = fs.statSync(filePath);
2003
+ const changed = bytes !== before.size ||
2004
+ after.size !== before.size ||
2005
+ after.ino !== before.ino ||
2006
+ after.dev !== before.dev ||
2007
+ after.mtimeMs !== before.mtimeMs ||
2008
+ after.ctimeMs !== before.ctimeMs;
2009
+ finish(changed ? new Error('Local file changed during verification') : undefined);
2010
+ }
2011
+ catch (error) {
2012
+ finish(error instanceof Error ? error : new Error(String(error)));
2013
+ }
2014
+ });
1888
2015
  });
1889
2016
  }
1890
2017
  async function hashRemoteFile(alias, remotePath) {
@@ -1975,15 +2102,18 @@ function resolveSftpDownloadFileTarget(remotePath, localPath, style = 'posix') {
1975
2102
  async function syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options) {
1976
2103
  const timeout = options.operationTimeout ?? 600_000;
1977
2104
  const controller = new AbortController();
2105
+ const accumulator = new DirectoryTransferAccumulator();
2106
+ const warnings = [];
1978
2107
  const operation = syncWithSftp(alias, localPath, remotePath, direction, {
1979
2108
  ...options,
1980
2109
  signal: controller.signal,
1981
- });
2110
+ }, accumulator, warnings);
1982
2111
  let timer;
1983
2112
  const timedOut = new Promise((resolve) => {
1984
2113
  timer = setTimeout(() => {
1985
2114
  controller.abort(new SftpOperationTimeoutError(timeout));
1986
2115
  resolve({
2116
+ ...buildDirectorySyncResult(accumulator.toResult(), [...warnings]),
1987
2117
  success: false,
1988
2118
  method: 'sftp',
1989
2119
  operationStatus: 'unknown',
@@ -1999,9 +2129,8 @@ async function syncWithSftpWithTimeout(alias, localPath, remotePath, direction,
1999
2129
  }
2000
2130
  return result;
2001
2131
  }
2002
- async function syncWithSftp(alias, localPath, remotePath, direction, options) {
2132
+ async function syncWithSftp(alias, localPath, remotePath, direction, options, accumulator, warnings) {
2003
2133
  throwIfAborted(options.signal);
2004
- const warnings = [];
2005
2134
  if (options.delete) {
2006
2135
  warnings.push('delete option is not supported in SFTP mode (requires rsync)');
2007
2136
  }
@@ -2059,7 +2188,7 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
2059
2188
  };
2060
2189
  }
2061
2190
  if (stats.isDirectory()) {
2062
- const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0, options.signal, '', options.remotePathStyle);
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);
2063
2192
  return buildDirectorySyncResult(result, warnings);
2064
2193
  }
2065
2194
  const targetRemotePath = await resolveSftpUploadFileTarget(alias, localPath, remotePath, options.remotePathStyle);
@@ -2086,7 +2215,7 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
2086
2215
  files: 0,
2087
2216
  directories: 0,
2088
2217
  bytes: 0,
2089
- }, 0, options.signal, '', options.remotePathStyle);
2218
+ }, 0, options.signal, '', options.remotePathStyle, accumulator);
2090
2219
  return buildDirectorySyncResult(result, warnings);
2091
2220
  }
2092
2221
  const targetLocalPath = resolveSftpDownloadFileTarget(remotePath, localPath, options.remotePathStyle);
@@ -2098,32 +2227,24 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
2098
2227
  };
2099
2228
  }
2100
2229
  catch (err) {
2101
- if (err instanceof SftpOperationTimeoutError) {
2102
- return {
2103
- success: false,
2104
- method: 'sftp',
2105
- operationStatus: 'unknown',
2106
- timedOut: true,
2107
- retryable: true,
2108
- stats: { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 1 },
2109
- output: err.message,
2110
- };
2111
- }
2230
+ const result = buildDirectorySyncResult(accumulator.toResult(), warnings);
2231
+ const timedOut = err instanceof SftpOperationTimeoutError;
2112
2232
  return {
2233
+ ...result,
2113
2234
  success: false,
2114
- method: 'sftp',
2115
- operationStatus: 'failed',
2116
- stats: { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 1 },
2117
- output: err instanceof Error ? err.message : String(err),
2235
+ operationStatus: timedOut ? 'unknown' : 'failed',
2236
+ timedOut: timedOut || undefined,
2237
+ retryable: timedOut || undefined,
2238
+ stats: { ...result.stats, failed: Math.max(1, result.stats.failed) },
2239
+ output: [result.output, err instanceof Error ? err.message : String(err)].filter(Boolean).join('; '),
2118
2240
  };
2119
2241
  }
2120
2242
  }
2121
2243
  /**
2122
2244
  * 递归上传目录
2123
2245
  */
2124
- async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', inspectNames) {
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()) {
2125
2247
  throwIfAborted(signal);
2126
- const accumulator = new DirectoryTransferAccumulator();
2127
2248
  recordSftpDirectory(traversal, depth, localPath);
2128
2249
  const ownSftp = !sharedSftp && !inspectNames;
2129
2250
  const sftp = inspectNames ? undefined : (sharedSftp ?? (await getSftpWithinDeadline(alias)));
@@ -2199,8 +2320,7 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
2199
2320
  for (const item of subDirItems) {
2200
2321
  const itemLocalPath = path.join(localPath, item);
2201
2322
  const itemRemotePath = remoteJoin(style, remotePath, item);
2202
- const result = await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item}` : item, style, inspectNames);
2203
- accumulator.merge(result);
2323
+ await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item}` : item, style, inspectNames, accumulator);
2204
2324
  }
2205
2325
  }
2206
2326
  finally {
@@ -2214,9 +2334,8 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
2214
2334
  /**
2215
2335
  * 递归下载目录
2216
2336
  */
2217
- async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix') {
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()) {
2218
2338
  throwIfAborted(signal);
2219
- const accumulator = new DirectoryTransferAccumulator();
2220
2339
  recordSftpDirectory(traversal, depth, remotePath);
2221
2340
  const ownSftp = !sharedSftp;
2222
2341
  const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias));
@@ -2287,8 +2406,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
2287
2406
  });
2288
2407
  for (const item of dirItems) {
2289
2408
  const itemLocalPath = path.join(localPath, item.name);
2290
- const result = await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item.name}` : item.name, style);
2291
- accumulator.merge(result);
2409
+ await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item.name}` : item.name, style, accumulator);
2292
2410
  }
2293
2411
  for (const item of symlinkItems) {
2294
2412
  console.warn(`[ssh_sync] skipping remote symlink: ${item.path}`);
@@ -0,0 +1,51 @@
1
+ import { type ChildProcess } from 'child_process';
2
+ import type { CommandChannel, OpenSshMasterInfo, SSHConnectionConfig } from './types.js';
3
+ export declare class OpenSshError extends Error {
4
+ readonly details: Record<string, unknown>;
5
+ constructor(message: string, details: Record<string, unknown>);
6
+ }
7
+ export declare function resolveOpenSshConfig(input: {
8
+ configHost?: string;
9
+ configPath?: string;
10
+ host?: string;
11
+ username?: string;
12
+ port?: number;
13
+ controlPath?: string;
14
+ readyTimeout?: number;
15
+ }): Promise<{
16
+ host: string;
17
+ username: string;
18
+ port: number;
19
+ controlPath: string;
20
+ }>;
21
+ export declare class OpenSshConnection {
22
+ readonly config: SSHConnectionConfig;
23
+ readonly info: OpenSshMasterInfo;
24
+ private readonly processes;
25
+ private readonly socketIdentity;
26
+ private closed;
27
+ private constructor();
28
+ static connect(config: SSHConnectionConfig, signal?: AbortSignal): Promise<OpenSshConnection>;
29
+ private static socket;
30
+ private static arguments;
31
+ arguments(): string[];
32
+ assertCurrent(): void;
33
+ isAlive(): boolean;
34
+ readonly trackProcess: (child: ChildProcess, processGroup?: boolean) => void;
35
+ open(command: string): CommandChannel & {
36
+ ready: Promise<void>;
37
+ };
38
+ consume(command: string, consume: (chunk: Buffer) => void, timeout: number): Promise<void>;
39
+ capture(command: string, timeout: number, maxBytes: number): Promise<Buffer>;
40
+ stat(remotePath: string, timeout: number, follow?: boolean): Promise<{
41
+ mode: number;
42
+ size: number;
43
+ uid: number;
44
+ gid: number;
45
+ mtime: number;
46
+ atime: number;
47
+ stamp: string;
48
+ }>;
49
+ hashFile(remotePath: string, algorithm: 'md5' | 'sha256', timeout: number, maxBytes?: number, expectedSize?: number): Promise<string>;
50
+ close(): void;
51
+ }