dsh-remote-go 0.3.0 → 0.3.1
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/client.js +1344 -2095
- package/dist/src/admission.js +3 -28
- package/dist/src/api.js +37 -99
- package/dist/src/atomic-json.js +32 -66
- package/dist/src/bootstrap.js +17 -27
- package/dist/src/connect/attach.js +3 -25
- package/dist/src/connect/bootstrap.js +2 -8
- package/dist/src/connect/failures.js +3 -18
- package/dist/src/connect/guards.js +13 -8
- package/dist/src/connect/launch.js +11 -21
- package/dist/src/connect/links.js +11 -27
- package/dist/src/connect/ownership.js +33 -114
- package/dist/src/connect/pipeline.js +61 -158
- package/dist/src/connect/reattach.js +20 -44
- package/dist/src/connect/registry.js +0 -1
- package/dist/src/connect/repair.js +8 -37
- package/dist/src/connect/restart.js +36 -97
- package/dist/src/connect/stop.js +16 -50
- package/dist/src/connect/transport.js +10 -18
- package/dist/src/connect/verify.js +3 -12
- package/dist/src/connect/watchdog.js +6 -38
- package/dist/src/connect-log.js +16 -41
- package/dist/src/connect-progress.js +13 -45
- package/dist/src/constants.js +2 -40
- package/dist/src/context.js +10 -31
- package/dist/src/diagnostics.js +56 -25
- package/dist/src/download-jobs.js +11 -20
- package/dist/src/errors.js +175 -190
- package/dist/src/events.js +20 -84
- package/dist/src/host-lifecycle.js +39 -175
- package/dist/src/host-registry.js +27 -78
- package/dist/src/host-runtime.js +0 -9
- package/dist/src/host-view.js +6 -29
- package/dist/src/index.js +15 -35
- package/dist/src/kernel-adapter.js +34 -80
- package/dist/src/key-files.js +10 -23
- package/dist/src/known-hosts.js +13 -34
- package/dist/src/layout.js +3 -20
- package/dist/src/local-download.js +94 -163
- package/dist/src/local-ssh.js +12 -19
- package/dist/src/local-version.js +35 -49
- package/dist/src/node/download.js +1 -4
- package/dist/src/node-runtime.js +19 -38
- package/dist/src/probe.js +6 -7
- package/dist/src/prompts.js +56 -128
- package/dist/src/proxy/cookies.js +26 -60
- package/dist/src/proxy/headers.js +30 -125
- package/dist/src/proxy/http.js +33 -41
- package/dist/src/proxy/index.js +20 -34
- package/dist/src/proxy/limits.js +4 -6
- package/dist/src/proxy/routes.js +10 -17
- package/dist/src/proxy/upgrade.js +27 -42
- package/dist/src/redact.js +14 -33
- package/dist/src/remote-files.js +9 -21
- package/dist/src/remote-path.js +21 -62
- package/dist/src/remote-proc.js +35 -103
- package/dist/src/remote-web/registry.js +10 -50
- package/dist/src/remote-web/resolve.js +23 -104
- package/dist/src/remote-web/start.js +27 -64
- package/dist/src/remote-web/stop.js +6 -25
- package/dist/src/request-trust.js +15 -32
- package/dist/src/runtime/dispatch.js +33 -143
- package/dist/src/runtime/effects.js +7 -17
- package/dist/src/runtime/events.js +1 -1
- package/dist/src/runtime/fsm.js +11 -61
- package/dist/src/runtime/rules.js +113 -198
- package/dist/src/secrets.js +15 -38
- package/dist/src/service.js +22 -128
- package/dist/src/settings.js +41 -42
- package/dist/src/ssh-config.js +22 -41
- package/dist/src/ssh-failures.js +23 -74
- package/dist/src/ssh-sftp.js +47 -86
- package/dist/src/ssh.js +95 -177
- package/dist/src/store.js +31 -48
- package/dist/src/tool-shared.js +17 -52
- package/dist/src/tools-exec-policy.js +33 -79
- package/dist/src/tools-exec.js +87 -151
- package/dist/src/tools-report.js +100 -167
- package/dist/src/tools.js +59 -133
- package/dist/src/tunnel.js +9 -27
- package/dist/src/util.js +19 -56
- package/dist/src/version.js +6 -12
- package/dist/src/watchdog-bus.js +0 -8
- package/dist/src/watchdog.js +7 -47
- package/package.json +1 -1
package/dist/src/ssh-failures.js
CHANGED
|
@@ -1,50 +1,30 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* Both adapters share it — the ssh2 one and the test double — so "which code and which sentence
|
|
6
|
-
* does this failure produce" has exactly one answer. The client-visible wording comes from
|
|
7
|
-
* `errors.ts`; this module only decides *which* factory applies, which is why extracting it keeps
|
|
8
|
-
* the text byte-for-byte identical to the pre-P5 `toSshError`.
|
|
9
|
-
*
|
|
10
|
-
* The three details the table pins and that must not be "simplified" away:
|
|
11
|
-
* - `errors.network` is retryable for connection failures, `errors.auth` too, and
|
|
12
|
-
* `host-key-changed` is not — that row decides whether the Client offers "Retry";
|
|
13
|
-
* - `exec` timeouts are **results** (`SshExecResult.timedOut`), never exceptions, so no timeout
|
|
14
|
-
* factory is called from the exec path;
|
|
15
|
-
* - a `RemoteGoError` that already reached us is the contract's own failure and is passed
|
|
16
|
-
* through untouched (the host-key factories build theirs directly).
|
|
17
|
-
*/
|
|
1
|
+
/** SSH failure classification: the one place that turns a transport failure into the coded, bilingual error the Client
|
|
2
|
+
* renders; both adapters share it, so "which code and which sentence" has one answer. The table pins three details:
|
|
3
|
+
* `network`/`auth` are retryable but `host-key-changed` is not, `exec` deadlines are results (`timedOut`) never
|
|
4
|
+
* exceptions, and a coded error passes through. */
|
|
18
5
|
import { errors, RemoteGoError } from './errors.js';
|
|
19
6
|
import { headTail } from './util.js';
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
7
|
+
/** The three message shapes the rules below recognise, in the order they are tried. */
|
|
8
|
+
const HOST_KEY_MESSAGE = /host key|hostkey|verification/i;
|
|
9
|
+
const AUTH_MESSAGE = /authentication|All configured authentication methods failed|Permission denied/i;
|
|
10
|
+
const TRANSPORT_MESSAGE = /ECONNREFUSED|ENOTFOUND|ETIMEDOUT|EHOSTUNREACH|ENETUNREACH|EAI_AGAIN|timed out|timeout|Cannot connect|connect failed/i;
|
|
11
|
+
/** The typed "the transport is gone" failure: retryable network, never a bare internal error. The wording and the
|
|
12
|
+
* `reason=ssh-closed` technical string are frozen: `exec`, `forwardOut` and the SFTP entry points have raised this
|
|
13
|
+
* exact sentence since the first release, and callers (plus tests) match on it. */
|
|
27
14
|
export function closedError() {
|
|
28
15
|
return errors.network({
|
|
29
16
|
zh: 'SSH 连接已断开,本次操作没有完成;如果主机仍在线,重连即可继续。',
|
|
30
17
|
en: 'SSH connection is not open; the transport closed before this step finished.',
|
|
31
18
|
}, { technical: 'reason=ssh-closed' });
|
|
32
19
|
}
|
|
33
|
-
/** The endpoint a host-key failure names in its sentence. */
|
|
34
|
-
function endpointOf(context) {
|
|
35
|
-
const host = context.host ?? 'the host';
|
|
36
|
-
const port = context.port ?? 22;
|
|
37
|
-
return `${host}:${port}`;
|
|
38
|
-
}
|
|
39
20
|
/** The host-key failure an adapter reports when it has no more specific factory to call. */
|
|
40
21
|
function hostKeyFailure(context) {
|
|
41
|
-
const endpoint =
|
|
22
|
+
const endpoint = `${context.host ?? 'the host'}:${context.port ?? 22}`;
|
|
42
23
|
return errors.network({
|
|
43
24
|
zh: `主机密钥校验未通过(${endpoint})。`,
|
|
44
25
|
en: `Host key verification failed for ${endpoint}.`,
|
|
45
26
|
});
|
|
46
27
|
}
|
|
47
|
-
/** The factory one explicit kind maps to; see the table in design-ssh §5. */
|
|
48
28
|
function failureOfKind(kind, context) {
|
|
49
29
|
if (kind === 'closed')
|
|
50
30
|
return closedError();
|
|
@@ -52,26 +32,13 @@ function failureOfKind(kind, context) {
|
|
|
52
32
|
return errors.auth();
|
|
53
33
|
if (kind === 'hostkey')
|
|
54
34
|
return hostKeyFailure(context);
|
|
55
|
-
// `transport` and `timeout` are the same client-visible failure: a retryable network error.
|
|
56
|
-
//
|
|
57
|
-
// second sentence for it (an `exec` deadline never reaches this function at all).
|
|
35
|
+
// `transport` and `timeout` are the same client-visible failure: a retryable network error. The timeout
|
|
36
|
+
// row only lets an adapter label a handshake deadline without inventing a second sentence for it.
|
|
58
37
|
return errors.network();
|
|
59
38
|
}
|
|
60
|
-
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* Rules, in order (each one is the pre-P5 `toSshError` rule, unchanged):
|
|
64
|
-
* 1. an already-coded `RemoteGoError` passes through;
|
|
65
|
-
* 2. an explicit `kind` chooses the factory without inspecting the message;
|
|
66
|
-
* 3. a host-key-looking message → network with the host-key sentence;
|
|
67
|
-
* 4. an authentication-looking message → `auth`;
|
|
68
|
-
* 5. a connection/timeout code or message → default `network`;
|
|
69
|
-
* 6. anything else → `network` with `SSH 连接失败:<headTail(message, 300)>`.
|
|
70
|
-
*
|
|
71
|
-
* @param error - the thrown value; `unknown` because every caller catches one.
|
|
72
|
-
* @param context - the endpoint (for the sentence) and the adapter's optional kind label.
|
|
73
|
-
* @returns the coded error; never throws.
|
|
74
|
-
*/
|
|
39
|
+
/** Turn any thrown value into the error the contract may expose. The order decides: an already-coded error passes
|
|
40
|
+
* through, then an explicit `kind` wins over the message, then host-key, auth, transport, and anything left becomes
|
|
41
|
+
* `network` naming `headTail(message, 300)`. */
|
|
75
42
|
export function classifySshFailure(error, context = {}) {
|
|
76
43
|
if (error instanceof RemoteGoError)
|
|
77
44
|
return error;
|
|
@@ -79,44 +46,26 @@ export function classifySshFailure(error, context = {}) {
|
|
|
79
46
|
return failureOfKind(context.kind, context);
|
|
80
47
|
if (!error)
|
|
81
48
|
return errors.internal('unknown ssh error');
|
|
82
|
-
// A thrown value is `unknown`: the `as` restates the two fields a node/ssh2 transport error
|
|
83
|
-
// carries, and the `??` fallbacks keep a thrown string working.
|
|
49
|
+
// A thrown value is `unknown`: the `as` restates the two fields a node/ssh2 transport error carries.
|
|
84
50
|
const message = String(error.message ?? error);
|
|
85
51
|
const code = String(error.code ?? '');
|
|
86
|
-
if (
|
|
52
|
+
if (HOST_KEY_MESSAGE.test(message))
|
|
87
53
|
return hostKeyFailure(context);
|
|
88
|
-
if (
|
|
54
|
+
if (AUTH_MESSAGE.test(message))
|
|
89
55
|
return errors.auth();
|
|
90
|
-
}
|
|
91
|
-
if (/ECONNREFUSED|ENOTFOUND|ETIMEDOUT|EHOSTUNREACH|ENETUNREACH|EAI_AGAIN|timed out|timeout|Cannot connect|connect failed/i.test(`${code} ${message}`)) {
|
|
56
|
+
if (TRANSPORT_MESSAGE.test(`${code} ${message}`))
|
|
92
57
|
return errors.network();
|
|
93
|
-
}
|
|
94
58
|
return errors.network({
|
|
95
59
|
zh: `SSH 连接失败:${headTail(message, 300)}`,
|
|
96
60
|
en: `SSH connection failed: ${headTail(message, 300)}`,
|
|
97
61
|
});
|
|
98
62
|
}
|
|
99
|
-
/**
|
|
100
|
-
* The historical name for {@link classifySshFailure}.
|
|
101
|
-
*
|
|
102
|
-
* It stays an alias (not a copy) so the two can never drift: callers/tests that have always
|
|
103
|
-
* imported `toSshError(error, host, port)` keep working, and there is exactly one rule table.
|
|
104
|
-
* @param error - the thrown value.
|
|
105
|
-
* @param host - the endpoint the failure is about.
|
|
106
|
-
* @param port - the endpoint's port.
|
|
107
|
-
*/
|
|
63
|
+
/** The historical name for {@link classifySshFailure}; an alias, not a copy, so the two cannot drift. */
|
|
108
64
|
export function toSshError(error, host, port) {
|
|
109
65
|
return classifySshFailure(error, { host, port });
|
|
110
66
|
}
|
|
111
|
-
/**
|
|
112
|
-
* Whether one thrown value is one of the two host-key refusals.
|
|
113
|
-
*
|
|
114
|
-
* `connect()`'s auth-candidate loop must rethrow these instead of trying the next credential: a
|
|
115
|
-
* rejected key is a policy decision, not an authentication failure.
|
|
116
|
-
* @param error - the thrown value.
|
|
117
|
-
*/
|
|
67
|
+
/** Whether one thrown value is a host-key refusal: `connect()` must rethrow it, not try the next credential. */
|
|
118
68
|
export function isHostKeyError(error) {
|
|
119
|
-
// Same narrowing as the classifier: a thrown value is `unknown`, and only its `code` is read.
|
|
120
69
|
const code = error?.code;
|
|
121
70
|
return code === 'host-key-changed' || code === 'host-key-unknown';
|
|
122
71
|
}
|
package/dist/src/ssh-sftp.js
CHANGED
|
@@ -1,35 +1,20 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* (`saveRemoteFile`/`saveRemoteTree`/`inspectRemotePath`) — rather than part of "open a connection".
|
|
8
|
-
* Nothing outside the adapter imports this module.
|
|
9
|
-
*
|
|
10
|
-
* Behaviour that is deliberately unchanged from the pre-P5 single file:
|
|
11
|
-
* - the local disk is the only bound (no byte/entry cap); cancellation deletes what it wrote;
|
|
12
|
-
* - symlinks are skipped, so a remote loop cannot cycle;
|
|
13
|
-
* - Windows metadata names are only special on Windows (and are reported, never dropped silently);
|
|
14
|
-
* - two names differing only by case are both kept on a case-insensitive local volume, by renaming;
|
|
15
|
-
* - a progress/inventory listener that throws must never abort the transfer.
|
|
16
|
-
*/
|
|
1
|
+
/** The SFTP download capability of the ssh2 adapter: one session, stat/list, and streaming one file or a whole tree
|
|
2
|
+
* into a local path. `ssh.ts` delegates here to stay inside the 600-line budget, and because ssh2's SFTP details are a
|
|
3
|
+
* capability — optional on `SshPort` (`saveRemoteFile`/`saveRemoteTree`/`inspectRemotePath`) — rather than part of
|
|
4
|
+
* "opening a connection"; nothing outside the adapter imports this module. Deliberately unchanged: the disk is the only
|
|
5
|
+
* bound (cancellation deletes), symlinks are skipped so a loop cannot cycle, Windows metadata names are special only
|
|
6
|
+
* there and reported, and a throwing listener never aborts the transfer. */
|
|
17
7
|
import { createWriteStream } from 'node:fs';
|
|
18
8
|
import { mkdir, rm } from 'node:fs/promises';
|
|
19
9
|
import path from 'node:path';
|
|
20
10
|
import { pipeline } from 'node:stream/promises';
|
|
21
11
|
import { errors } from './errors.js';
|
|
22
12
|
import { isIgnoredLocalName, localPathKey, safeLocalSegment, uniqueLocalPath } from './remote-path.js';
|
|
13
|
+
/** Wrap one node-style ssh2 callback as a promise (the vendor surface has no promise variant). */
|
|
14
|
+
const call = (run) => new Promise((resolve, reject) => run((error, value) => (error ? reject(error) : resolve(value))));
|
|
23
15
|
/** Open one SFTP session on an established transport. */
|
|
24
16
|
export function openSftpSession(client) {
|
|
25
|
-
return
|
|
26
|
-
client.sftp((error, sftp) => {
|
|
27
|
-
if (error)
|
|
28
|
-
reject(error);
|
|
29
|
-
else
|
|
30
|
-
resolve(sftp);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
17
|
+
return call((done) => client.sftp(done));
|
|
33
18
|
}
|
|
34
19
|
/** Close one SFTP session, ignoring a session that is already gone. */
|
|
35
20
|
export function endSftp(sftp) {
|
|
@@ -40,24 +25,18 @@ export function endSftp(sftp) {
|
|
|
40
25
|
// swallow-ok: the SFTP session may already be gone
|
|
41
26
|
}
|
|
42
27
|
}
|
|
43
|
-
/**
|
|
44
|
-
*
|
|
45
|
-
* streams and removes the partial file.
|
|
46
|
-
*/
|
|
28
|
+
/** Stream one remote file into a local path. Nothing is buffered; cancellation destroys both streams and removes the
|
|
29
|
+
* partial file. */
|
|
47
30
|
export async function saveSftpFile(sftp, remotePath, localPath, options = {}) {
|
|
48
|
-
const stats = await
|
|
49
|
-
sftp.stat(remotePath, (error, value) => (error ? reject(error) : resolve(value)));
|
|
50
|
-
});
|
|
31
|
+
const stats = await call((done) => sftp.stat(remotePath, done));
|
|
51
32
|
assertReadableFile(stats);
|
|
52
33
|
const totalBytes = Number(stats?.size ?? 0);
|
|
53
34
|
let doneBytes = 0;
|
|
54
35
|
try {
|
|
55
36
|
await copySftpFileToLocal(sftp, remotePath, localPath, (bytes) => {
|
|
56
37
|
doneBytes += bytes;
|
|
57
|
-
if (typeof options.onProgress !== 'function')
|
|
58
|
-
return;
|
|
59
38
|
try {
|
|
60
|
-
options.onProgress({ doneBytes, totalBytes, doneFiles: 0, totalFiles: 1 });
|
|
39
|
+
options.onProgress?.({ doneBytes, totalBytes, doneFiles: 0, totalFiles: 1 });
|
|
61
40
|
}
|
|
62
41
|
catch {
|
|
63
42
|
// swallow-ok: progress listeners must not break the transfer
|
|
@@ -70,11 +49,8 @@ export async function saveSftpFile(sftp, remotePath, localPath, options = {}) {
|
|
|
70
49
|
}
|
|
71
50
|
return { size: doneBytes || totalBytes };
|
|
72
51
|
}
|
|
73
|
-
/** Inspect one remote path without reading it. */
|
|
74
52
|
export async function inspectSftpPath(sftp, remotePath) {
|
|
75
|
-
const stats = await
|
|
76
|
-
sftp.stat(remotePath, (error, value) => (error ? reject(error) : resolve(value)));
|
|
77
|
-
});
|
|
53
|
+
const stats = await call((done) => sftp.stat(remotePath, done));
|
|
78
54
|
return {
|
|
79
55
|
isFile: Boolean(stats?.isFile?.()),
|
|
80
56
|
isDirectory: Boolean(stats?.isDirectory?.()),
|
|
@@ -82,19 +58,10 @@ export async function inspectSftpPath(sftp, remotePath) {
|
|
|
82
58
|
mtime: Number(stats?.mtime ?? 0),
|
|
83
59
|
};
|
|
84
60
|
}
|
|
85
|
-
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* Directories are created locally, empty ones included; symlinks are skipped so a remote loop
|
|
89
|
-
* cannot cycle. There is no byte/count cap: the local disk is the bound. `options.caseInsensitive`
|
|
90
|
-
* defaults to the local platform (Windows/macOS); `options.ignoreWindowsNames` defaults to Windows
|
|
91
|
-
* and is the only platform where the Windows-only names are skipped; `options.onIgnored` receives
|
|
92
|
-
* the skipped names so a download job can show them.
|
|
93
|
-
*/
|
|
61
|
+
/** Copy a remote directory to a local directory, one file at a time: directories are created locally (empty ones
|
|
62
|
+
* included), symlinks are skipped and there is no byte/count cap; `onIgnored` reports the skipped names. */
|
|
94
63
|
export async function saveSftpTree(sftp, remotePath, targetRoot, options = {}) {
|
|
95
|
-
const rootStats = await
|
|
96
|
-
sftp.stat(remotePath, (error, value) => (error ? reject(error) : resolve(value)));
|
|
97
|
-
});
|
|
64
|
+
const rootStats = await call((done) => sftp.stat(remotePath, done));
|
|
98
65
|
if (!rootStats?.isDirectory?.()) {
|
|
99
66
|
throw errors.remoteFileUnavailable({
|
|
100
67
|
zh: '这个远端路径不是目录,无法按文件夹下载。',
|
|
@@ -102,22 +69,21 @@ export async function saveSftpTree(sftp, remotePath, targetRoot, options = {}) {
|
|
|
102
69
|
});
|
|
103
70
|
}
|
|
104
71
|
const localRoot = path.resolve(targetRoot);
|
|
105
|
-
// Case-insensitive local filesystems map `Report.md` and `report.md` to one file: the download
|
|
106
|
-
//
|
|
107
|
-
//
|
|
72
|
+
// Case-insensitive local filesystems map `Report.md` and `report.md` to one file: the download would silently lose
|
|
73
|
+
// one of them. The default follows the platform (Windows and macOS both default to case-insensitive volumes);
|
|
74
|
+
// tests can force it either way.
|
|
108
75
|
const caseInsensitive = options.caseInsensitive ?? (process.platform === 'win32' || process.platform === 'darwin');
|
|
109
|
-
// The Windows metadata / shortcut names are only special on Windows: on Linux/macOS `*.lnk` or
|
|
110
|
-
//
|
|
76
|
+
// The Windows metadata / shortcut names are only special on Windows: on Linux/macOS `*.lnk` or `desktop.ini` are
|
|
77
|
+
// ordinary files and must be downloaded like any other.
|
|
111
78
|
const ignoreWindowsNames = options.ignoreWindowsNames ?? process.platform === 'win32';
|
|
112
79
|
const insideRoot = (candidate) => {
|
|
113
80
|
const resolved = path.resolve(candidate);
|
|
114
|
-
if (resolved
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
return resolved;
|
|
81
|
+
if (resolved === localRoot || resolved.startsWith(localRoot + path.sep))
|
|
82
|
+
return resolved;
|
|
83
|
+
throw errors.remoteFileUnavailable({
|
|
84
|
+
zh: '远端文件名无法安全映射到本地目录。',
|
|
85
|
+
en: 'A remote file name cannot be mapped safely into the local directory.',
|
|
86
|
+
});
|
|
121
87
|
};
|
|
122
88
|
const members = [];
|
|
123
89
|
const ignoredNames = [];
|
|
@@ -125,9 +91,7 @@ export async function saveSftpTree(sftp, remotePath, targetRoot, options = {}) {
|
|
|
125
91
|
let totalFiles = 0;
|
|
126
92
|
let renamedFiles = 0;
|
|
127
93
|
const walk = async (directory, relative, localDir) => {
|
|
128
|
-
const listing = await
|
|
129
|
-
sftp.readdir(directory, (error, value) => (error ? reject(error) : resolve(value)));
|
|
130
|
-
});
|
|
94
|
+
const listing = await call((done) => sftp.readdir(directory, done));
|
|
131
95
|
const items = Array.isArray(listing) ? listing.slice() : [];
|
|
132
96
|
items.sort((left, right) => String(left?.filename ?? '').localeCompare(String(right?.filename ?? '')));
|
|
133
97
|
// Names are claimed per directory, so a renamed member keeps its own children.
|
|
@@ -139,8 +103,8 @@ export async function saveSftpTree(sftp, remotePath, targetRoot, options = {}) {
|
|
|
139
103
|
const attrs = item?.attrs ?? {};
|
|
140
104
|
if (attrs.isSymbolicLink?.() === true)
|
|
141
105
|
continue;
|
|
142
|
-
// Windows metadata and shell entries are never planted in a local download
|
|
143
|
-
//
|
|
106
|
+
// Windows metadata and shell entries are never planted in a local download; the skipped names are reported in
|
|
107
|
+
// the result so nothing disappears silently.
|
|
144
108
|
if (ignoreWindowsNames && isIgnoredLocalName(filename)) {
|
|
145
109
|
ignoredNames.push(relative === '' ? filename : `${relative}/${filename}`);
|
|
146
110
|
continue;
|
|
@@ -191,22 +155,22 @@ export async function saveSftpTree(sftp, remotePath, targetRoot, options = {}) {
|
|
|
191
155
|
let doneBytes = 0;
|
|
192
156
|
let doneFiles = 0;
|
|
193
157
|
const report = () => {
|
|
194
|
-
if (typeof options.onProgress !== 'function')
|
|
195
|
-
return;
|
|
196
158
|
try {
|
|
197
|
-
options.onProgress({ doneBytes, totalBytes, doneFiles, totalFiles });
|
|
159
|
+
options.onProgress?.({ doneBytes, totalBytes, doneFiles, totalFiles });
|
|
198
160
|
}
|
|
199
161
|
catch {
|
|
200
162
|
// swallow-ok: progress listeners must not break the download
|
|
201
163
|
}
|
|
202
164
|
};
|
|
165
|
+
const cancelled = async () => {
|
|
166
|
+
await rm(localRoot, { recursive: true, force: true }).catch(() => { });
|
|
167
|
+
return { destination: localRoot, totalBytes, totalFiles, cancelled: true };
|
|
168
|
+
};
|
|
203
169
|
for (const member of members) {
|
|
204
170
|
if (member.isDirectory || typeof member.remotePath !== 'string')
|
|
205
171
|
continue;
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
return { destination: localRoot, totalBytes, totalFiles, cancelled: true };
|
|
209
|
-
}
|
|
172
|
+
if (options.isCancelled?.())
|
|
173
|
+
return cancelled();
|
|
210
174
|
await mkdir(path.dirname(member.localPath), { recursive: true });
|
|
211
175
|
try {
|
|
212
176
|
await copySftpFileToLocal(sftp, member.remotePath, member.localPath, (bytes) => {
|
|
@@ -215,10 +179,8 @@ export async function saveSftpTree(sftp, remotePath, targetRoot, options = {}) {
|
|
|
215
179
|
}, options.isCancelled);
|
|
216
180
|
}
|
|
217
181
|
catch (error) {
|
|
218
|
-
if (
|
|
219
|
-
|
|
220
|
-
return { destination: localRoot, totalBytes, totalFiles, cancelled: true };
|
|
221
|
-
}
|
|
182
|
+
if (options.isCancelled?.())
|
|
183
|
+
return cancelled();
|
|
222
184
|
throw error;
|
|
223
185
|
}
|
|
224
186
|
doneFiles += 1;
|
|
@@ -263,9 +225,8 @@ function copySftpFileToLocal(sftp, remotePath, localPath, onChunk, shouldCancel)
|
|
|
263
225
|
}
|
|
264
226
|
const finish = () => reject(error);
|
|
265
227
|
try {
|
|
266
|
-
if (output.closed === true)
|
|
228
|
+
if (output.closed === true)
|
|
267
229
|
finish();
|
|
268
|
-
}
|
|
269
230
|
else {
|
|
270
231
|
output.once('close', finish);
|
|
271
232
|
output.destroy();
|
|
@@ -276,7 +237,7 @@ function copySftpFileToLocal(sftp, remotePath, localPath, onChunk, shouldCancel)
|
|
|
276
237
|
}
|
|
277
238
|
};
|
|
278
239
|
input.on('data', (chunk) => {
|
|
279
|
-
if (
|
|
240
|
+
if (shouldCancel?.()) {
|
|
280
241
|
fail(new Error('download-cancelled'));
|
|
281
242
|
return;
|
|
282
243
|
}
|
|
@@ -288,10 +249,10 @@ function copySftpFileToLocal(sftp, remotePath, localPath, onChunk, shouldCancel)
|
|
|
288
249
|
}
|
|
289
250
|
});
|
|
290
251
|
pipeline(input, output).then(() => {
|
|
291
|
-
if (
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
},
|
|
252
|
+
if (settled)
|
|
253
|
+
return;
|
|
254
|
+
settled = true;
|
|
255
|
+
resolve();
|
|
256
|
+
}, fail);
|
|
296
257
|
});
|
|
297
258
|
}
|