dsh-remote-go 0.2.0 → 0.3.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/README.md +11 -3
- package/{client.js → dist/client.js} +7710 -8182
- package/dist/index.js +4 -0
- package/dist/src/admission.js +46 -0
- package/dist/src/api.js +442 -0
- package/dist/src/atomic-json.js +235 -0
- package/dist/src/bootstrap.js +106 -0
- package/dist/src/connect/attach.js +43 -0
- package/dist/src/connect/bootstrap.js +14 -0
- package/dist/src/connect/failures.js +44 -0
- package/dist/src/connect/guards.js +31 -0
- package/dist/src/connect/launch.js +80 -0
- package/dist/src/connect/links.js +70 -0
- package/dist/src/connect/ownership.js +220 -0
- package/dist/src/connect/pipeline.js +473 -0
- package/dist/src/connect/reattach.js +215 -0
- package/dist/src/connect/registry.js +11 -0
- package/dist/src/connect/repair.js +126 -0
- package/dist/src/connect/restart.js +316 -0
- package/dist/src/connect/stop.js +198 -0
- package/dist/src/connect/transport.js +109 -0
- package/dist/src/connect/verify.js +39 -0
- package/dist/src/connect/watchdog.js +76 -0
- package/dist/src/connect-log.js +162 -0
- package/dist/src/connect-progress.js +142 -0
- package/{src → dist/src}/constants.js +30 -56
- package/dist/src/context.js +117 -0
- package/dist/src/diagnostics.js +102 -0
- package/dist/src/download-jobs.js +230 -0
- package/dist/src/errors.js +243 -0
- package/dist/src/events.js +390 -0
- package/dist/src/host-lifecycle.js +504 -0
- package/dist/src/host-registry.js +232 -0
- package/dist/src/host-runtime.js +86 -0
- package/dist/src/host-view.js +75 -0
- package/dist/src/index.js +216 -0
- package/dist/src/kernel-adapter.js +434 -0
- package/dist/src/key-files.js +52 -0
- package/dist/src/known-hosts.js +125 -0
- package/dist/src/layout.js +43 -0
- package/dist/src/local-download.js +380 -0
- package/dist/src/local-ssh.js +30 -0
- package/dist/src/local-version.js +101 -0
- package/dist/src/node/download.js +160 -0
- package/dist/src/node-runtime.js +346 -0
- package/dist/src/probe.js +96 -0
- package/dist/src/prompts.js +270 -0
- package/dist/src/proxy/cookies.js +136 -0
- package/dist/src/proxy/headers.js +262 -0
- package/dist/src/proxy/http.js +142 -0
- package/dist/src/proxy/index.js +135 -0
- package/dist/src/proxy/limits.js +8 -0
- package/dist/src/proxy/routes.js +39 -0
- package/dist/src/proxy/upgrade.js +175 -0
- package/dist/src/redact.js +104 -0
- package/dist/src/remote-files.js +72 -0
- package/dist/src/remote-path.js +123 -0
- package/dist/src/remote-proc.js +339 -0
- package/dist/src/remote-web/registry.js +147 -0
- package/dist/src/remote-web/resolve.js +310 -0
- package/dist/src/remote-web/start.js +227 -0
- package/dist/src/remote-web/stop.js +93 -0
- package/dist/src/request-trust.js +87 -0
- package/dist/src/runtime/dispatch.js +310 -0
- package/dist/src/runtime/effects.js +91 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +120 -0
- package/dist/src/runtime/rules.js +565 -0
- package/dist/src/secrets.js +142 -0
- package/dist/src/service.js +406 -0
- package/dist/src/settings.js +144 -0
- package/dist/src/ssh-config.js +276 -0
- package/dist/src/ssh-failures.js +122 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +297 -0
- package/dist/src/ssh.js +477 -0
- package/dist/src/store.js +191 -0
- package/dist/src/tool-shared.js +181 -0
- package/dist/src/tools-exec-policy.js +167 -0
- package/dist/src/tools-exec.js +371 -0
- package/dist/src/tools-report.js +424 -0
- package/dist/src/tools.js +411 -0
- package/dist/src/tunnel.js +129 -0
- package/dist/src/util.js +149 -0
- package/dist/src/version.js +114 -0
- package/dist/src/watchdog-bus.js +29 -0
- package/dist/src/watchdog.js +156 -0
- package/index.js +5 -4
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +37 -19
- package/src/admission.js +0 -43
- package/src/api.js +0 -536
- package/src/atomic-json.js +0 -225
- package/src/bootstrap.js +0 -113
- package/src/connect-log.js +0 -176
- package/src/connect-pipeline.js +0 -1900
- package/src/connect-progress.js +0 -155
- package/src/diagnostics.js +0 -160
- package/src/download-jobs.js +0 -234
- package/src/errors.js +0 -412
- package/src/host-lifecycle.js +0 -604
- package/src/host-registry.js +0 -217
- package/src/host-runtime.js +0 -127
- package/src/host-view.js +0 -75
- package/src/index.js +0 -206
- package/src/key-files.js +0 -54
- package/src/known-hosts.js +0 -133
- package/src/layout.js +0 -53
- package/src/local-download.js +0 -381
- package/src/local-ssh.js +0 -27
- package/src/local-version.js +0 -88
- package/src/node-runtime.js +0 -521
- package/src/probe.js +0 -101
- package/src/prompts.js +0 -229
- package/src/redact.js +0 -68
- package/src/remote-files.js +0 -71
- package/src/remote-path.js +0 -137
- package/src/remote-proc.js +0 -343
- package/src/remote-proxy.js +0 -815
- package/src/remote-web.js +0 -751
- package/src/request-trust.js +0 -89
- package/src/secrets.js +0 -109
- package/src/service.js +0 -459
- package/src/settings.js +0 -163
- package/src/ssh-config.js +0 -271
- package/src/ssh.js +0 -731
- package/src/store.js +0 -193
- package/src/tools.js +0 -386
- package/src/tunnel.js +0 -132
- package/src/util.js +0 -153
- package/src/version.js +0 -103
- package/src/webapp.js +0 -43
- /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SFTP download capability of the ssh2 adapter: one session, stat/list, and streaming one file
|
|
3
|
+
* or a whole tree into a local path.
|
|
4
|
+
*
|
|
5
|
+
* `ssh.ts` keeps the transport itself and delegates here, for two reasons: the adapter has to stay
|
|
6
|
+
* within the 600-line budget, and ssh2's SFTP details are a capability — optional on `SshPort`
|
|
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
|
+
*/
|
|
17
|
+
import { createWriteStream } from 'node:fs';
|
|
18
|
+
import { mkdir, rm } from 'node:fs/promises';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { pipeline } from 'node:stream/promises';
|
|
21
|
+
import { errors } from './errors.js';
|
|
22
|
+
import { isIgnoredLocalName, localPathKey, safeLocalSegment, uniqueLocalPath } from './remote-path.js';
|
|
23
|
+
/** Open one SFTP session on an established transport. */
|
|
24
|
+
export function openSftpSession(client) {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
client.sftp((error, sftp) => {
|
|
27
|
+
if (error)
|
|
28
|
+
reject(error);
|
|
29
|
+
else
|
|
30
|
+
resolve(sftp);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** Close one SFTP session, ignoring a session that is already gone. */
|
|
35
|
+
export function endSftp(sftp) {
|
|
36
|
+
try {
|
|
37
|
+
sftp.end();
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// swallow-ok: the SFTP session may already be gone
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Stream one remote file into a local path. Nothing is buffered; cancellation destroys both
|
|
45
|
+
* streams and removes the partial file.
|
|
46
|
+
*/
|
|
47
|
+
export async function saveSftpFile(sftp, remotePath, localPath, options = {}) {
|
|
48
|
+
const stats = await new Promise((resolve, reject) => {
|
|
49
|
+
sftp.stat(remotePath, (error, value) => (error ? reject(error) : resolve(value)));
|
|
50
|
+
});
|
|
51
|
+
assertReadableFile(stats);
|
|
52
|
+
const totalBytes = Number(stats?.size ?? 0);
|
|
53
|
+
let doneBytes = 0;
|
|
54
|
+
try {
|
|
55
|
+
await copySftpFileToLocal(sftp, remotePath, localPath, (bytes) => {
|
|
56
|
+
doneBytes += bytes;
|
|
57
|
+
if (typeof options.onProgress !== 'function')
|
|
58
|
+
return;
|
|
59
|
+
try {
|
|
60
|
+
options.onProgress({ doneBytes, totalBytes, doneFiles: 0, totalFiles: 1 });
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// swallow-ok: progress listeners must not break the transfer
|
|
64
|
+
}
|
|
65
|
+
}, options.isCancelled);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
await rm(localPath, { force: true }).catch(() => { });
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
return { size: doneBytes || totalBytes };
|
|
72
|
+
}
|
|
73
|
+
/** Inspect one remote path without reading it. */
|
|
74
|
+
export async function inspectSftpPath(sftp, remotePath) {
|
|
75
|
+
const stats = await new Promise((resolve, reject) => {
|
|
76
|
+
sftp.stat(remotePath, (error, value) => (error ? reject(error) : resolve(value)));
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
isFile: Boolean(stats?.isFile?.()),
|
|
80
|
+
isDirectory: Boolean(stats?.isDirectory?.()),
|
|
81
|
+
size: Number(stats?.size ?? 0),
|
|
82
|
+
mtime: Number(stats?.mtime ?? 0),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Copy a remote directory to a local directory, one file at a time (no archive).
|
|
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
|
+
*/
|
|
94
|
+
export async function saveSftpTree(sftp, remotePath, targetRoot, options = {}) {
|
|
95
|
+
const rootStats = await new Promise((resolve, reject) => {
|
|
96
|
+
sftp.stat(remotePath, (error, value) => (error ? reject(error) : resolve(value)));
|
|
97
|
+
});
|
|
98
|
+
if (!rootStats?.isDirectory?.()) {
|
|
99
|
+
throw errors.remoteFileUnavailable({
|
|
100
|
+
zh: '这个远端路径不是目录,无法按文件夹下载。',
|
|
101
|
+
en: 'This remote path is not a directory and cannot be downloaded as a folder.',
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const localRoot = path.resolve(targetRoot);
|
|
105
|
+
// Case-insensitive local filesystems map `Report.md` and `report.md` to one file: the download
|
|
106
|
+
// would silently lose one of them. The default follows the platform (Windows and macOS both
|
|
107
|
+
// default to case-insensitive volumes); tests can force it either way.
|
|
108
|
+
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
|
+
// `desktop.ini` are ordinary files and must be downloaded like any other.
|
|
111
|
+
const ignoreWindowsNames = options.ignoreWindowsNames ?? process.platform === 'win32';
|
|
112
|
+
const insideRoot = (candidate) => {
|
|
113
|
+
const resolved = path.resolve(candidate);
|
|
114
|
+
if (resolved !== localRoot && !resolved.startsWith(localRoot + path.sep)) {
|
|
115
|
+
throw errors.remoteFileUnavailable({
|
|
116
|
+
zh: '远端文件名无法安全映射到本地目录。',
|
|
117
|
+
en: 'A remote file name cannot be mapped safely into the local directory.',
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return resolved;
|
|
121
|
+
};
|
|
122
|
+
const members = [];
|
|
123
|
+
const ignoredNames = [];
|
|
124
|
+
let totalBytes = 0;
|
|
125
|
+
let totalFiles = 0;
|
|
126
|
+
let renamedFiles = 0;
|
|
127
|
+
const walk = async (directory, relative, localDir) => {
|
|
128
|
+
const listing = await new Promise((resolve, reject) => {
|
|
129
|
+
sftp.readdir(directory, (error, value) => (error ? reject(error) : resolve(value)));
|
|
130
|
+
});
|
|
131
|
+
const items = Array.isArray(listing) ? listing.slice() : [];
|
|
132
|
+
items.sort((left, right) => String(left?.filename ?? '').localeCompare(String(right?.filename ?? '')));
|
|
133
|
+
// Names are claimed per directory, so a renamed member keeps its own children.
|
|
134
|
+
const used = new Set();
|
|
135
|
+
for (const item of items) {
|
|
136
|
+
const filename = String(item?.filename ?? '');
|
|
137
|
+
if (filename === '' || filename === '.' || filename === '..')
|
|
138
|
+
continue;
|
|
139
|
+
const attrs = item?.attrs ?? {};
|
|
140
|
+
if (attrs.isSymbolicLink?.() === true)
|
|
141
|
+
continue;
|
|
142
|
+
// Windows metadata and shell entries are never planted in a local download (M6); the
|
|
143
|
+
// skipped names are reported in the result so nothing disappears silently.
|
|
144
|
+
if (ignoreWindowsNames && isIgnoredLocalName(filename)) {
|
|
145
|
+
ignoredNames.push(relative === '' ? filename : `${relative}/${filename}`);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const childRelative = relative === '' ? filename : relative + '/' + filename;
|
|
149
|
+
const childPath = path.posix.join(directory, filename);
|
|
150
|
+
const desired = insideRoot(path.join(localDir, safeLocalSegment(filename)));
|
|
151
|
+
const localPath = insideRoot(uniqueLocalPath(desired, used, { caseInsensitive }));
|
|
152
|
+
used.add(localPathKey(localPath, { caseInsensitive }));
|
|
153
|
+
if (attrs.isDirectory?.() === true) {
|
|
154
|
+
members.push({ relative: childRelative, isDirectory: true, localPath });
|
|
155
|
+
await walk(childPath, childRelative, localPath);
|
|
156
|
+
}
|
|
157
|
+
else if (attrs.isFile?.() === true) {
|
|
158
|
+
const size = Number(attrs.size ?? 0);
|
|
159
|
+
if (!Number.isFinite(size) || size < 0)
|
|
160
|
+
continue;
|
|
161
|
+
if (localPath !== desired)
|
|
162
|
+
renamedFiles += 1;
|
|
163
|
+
totalFiles += 1;
|
|
164
|
+
totalBytes += size;
|
|
165
|
+
members.push({ relative: childRelative, remotePath: childPath, size, localPath });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
await walk(remotePath, '', localRoot);
|
|
170
|
+
if (typeof options.onInventory === 'function') {
|
|
171
|
+
try {
|
|
172
|
+
options.onInventory({ totalBytes, totalFiles });
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
// swallow-ok: inventory listeners must not break the download
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (ignoredNames.length > 0 && typeof options.onIgnored === 'function') {
|
|
179
|
+
try {
|
|
180
|
+
options.onIgnored(ignoredNames.slice());
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
// swallow-ok: the ignored-name listener must not break the download
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
await mkdir(localRoot, { recursive: true });
|
|
187
|
+
for (const member of members) {
|
|
188
|
+
if (member.isDirectory)
|
|
189
|
+
await mkdir(member.localPath, { recursive: true });
|
|
190
|
+
}
|
|
191
|
+
let doneBytes = 0;
|
|
192
|
+
let doneFiles = 0;
|
|
193
|
+
const report = () => {
|
|
194
|
+
if (typeof options.onProgress !== 'function')
|
|
195
|
+
return;
|
|
196
|
+
try {
|
|
197
|
+
options.onProgress({ doneBytes, totalBytes, doneFiles, totalFiles });
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
// swallow-ok: progress listeners must not break the download
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
for (const member of members) {
|
|
204
|
+
if (member.isDirectory || typeof member.remotePath !== 'string')
|
|
205
|
+
continue;
|
|
206
|
+
if (typeof options.isCancelled === 'function' && options.isCancelled()) {
|
|
207
|
+
await rm(localRoot, { recursive: true, force: true }).catch(() => { });
|
|
208
|
+
return { destination: localRoot, totalBytes, totalFiles, cancelled: true };
|
|
209
|
+
}
|
|
210
|
+
await mkdir(path.dirname(member.localPath), { recursive: true });
|
|
211
|
+
try {
|
|
212
|
+
await copySftpFileToLocal(sftp, member.remotePath, member.localPath, (bytes) => {
|
|
213
|
+
doneBytes += bytes;
|
|
214
|
+
report();
|
|
215
|
+
}, options.isCancelled);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
if (typeof options.isCancelled === 'function' && options.isCancelled()) {
|
|
219
|
+
await rm(localRoot, { recursive: true, force: true }).catch(() => { });
|
|
220
|
+
return { destination: localRoot, totalBytes, totalFiles, cancelled: true };
|
|
221
|
+
}
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
doneFiles += 1;
|
|
225
|
+
report();
|
|
226
|
+
}
|
|
227
|
+
return { destination: localRoot, totalBytes, totalFiles, renamedFiles, ignoredNames };
|
|
228
|
+
}
|
|
229
|
+
function assertReadableFile(stats) {
|
|
230
|
+
if (stats?.isDirectory?.()) {
|
|
231
|
+
throw errors.remoteFileUnavailable({
|
|
232
|
+
zh: '这是一个远端目录,不能作为文件下载。',
|
|
233
|
+
en: 'This remote path is a directory, not a downloadable file.',
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** Pipe one SFTP file into a local file, reporting bytes as they arrive. */
|
|
238
|
+
function copySftpFileToLocal(sftp, remotePath, localPath, onChunk, shouldCancel) {
|
|
239
|
+
return new Promise((resolve, reject) => {
|
|
240
|
+
let input;
|
|
241
|
+
try {
|
|
242
|
+
input = sftp.createReadStream(remotePath);
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
reject(error);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (!input || typeof input.on !== 'function') {
|
|
249
|
+
reject(new Error('SFTP read stream is unavailable'));
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const output = createWriteStream(localPath, { flags: 'w' });
|
|
253
|
+
let settled = false;
|
|
254
|
+
const fail = (error) => {
|
|
255
|
+
if (settled)
|
|
256
|
+
return;
|
|
257
|
+
settled = true;
|
|
258
|
+
try {
|
|
259
|
+
input.destroy();
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
// swallow-ok: the read stream is already destroyed
|
|
263
|
+
}
|
|
264
|
+
const finish = () => reject(error);
|
|
265
|
+
try {
|
|
266
|
+
if (output.closed === true) {
|
|
267
|
+
finish();
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
output.once('close', finish);
|
|
271
|
+
output.destroy();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
finish();
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
input.on('data', (chunk) => {
|
|
279
|
+
if (typeof shouldCancel === 'function' && shouldCancel()) {
|
|
280
|
+
fail(new Error('download-cancelled'));
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
try {
|
|
284
|
+
onChunk(Buffer.isBuffer(chunk) ? chunk.length : Buffer.byteLength(chunk));
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
// swallow-ok: the progress listener must not break the transfer
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
pipeline(input, output).then(() => {
|
|
291
|
+
if (!settled) {
|
|
292
|
+
settled = true;
|
|
293
|
+
resolve();
|
|
294
|
+
}
|
|
295
|
+
}, (error) => fail(error));
|
|
296
|
+
});
|
|
297
|
+
}
|