dsh-linked-workspace 0.8.2 → 0.9.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/CHANGELOG.md +31 -0
- package/README.en.md +31 -0
- package/README.md +31 -0
- package/docs/API.md +7 -3
- package/docs/VERIFY.md +25 -0
- package/lib/client.js +625 -34
- package/lib/index.js +326 -18
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* addresses and workspace paths.
|
|
17
17
|
*/
|
|
18
18
|
import { randomUUID } from 'node:crypto'
|
|
19
|
-
import { chmodSync, closeSync, cpSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
19
|
+
import { chmodSync, closeSync, cpSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
20
20
|
import { spawn } from 'node:child_process'
|
|
21
21
|
import { mkdir, readdir, stat as fsStat } from 'node:fs/promises'
|
|
22
22
|
import { homedir, tmpdir } from 'node:os'
|
|
@@ -87,6 +87,21 @@ const OP_TIMEOUT_MS = 60_000
|
|
|
87
87
|
// cannot stall the Host or flood the renderer.
|
|
88
88
|
const LIST_PAGE_SIZE = 500
|
|
89
89
|
const LIST_PAGE_MAX = 5000
|
|
90
|
+
// SFTP readdir has no paging, so a directory is always read in one call. A
|
|
91
|
+
// freshly read listing is kept briefly so "load more" does not repeat the same
|
|
92
|
+
// expensive scan; a first page (offset 0) is always re-read.
|
|
93
|
+
const LISTING_CACHE_TTL_MS = 5000
|
|
94
|
+
const LISTING_CACHE_LIMIT = 128
|
|
95
|
+
const listingCache = new Map()
|
|
96
|
+
const listingCacheKey = (scope, target) => `${scope.kind === 'linked' ? 'L' : 'W'}:${scope.id}:${target}`
|
|
97
|
+
const rememberListing = (key, listing) => {
|
|
98
|
+
listingCache.delete(key)
|
|
99
|
+
listingCache.set(key, { at: Date.now(), listing })
|
|
100
|
+
while (listingCache.size > LISTING_CACHE_LIMIT) {
|
|
101
|
+
listingCache.delete(listingCache.keys().next().value)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const forgetListings = () => listingCache.clear()
|
|
90
105
|
|
|
91
106
|
/** Clamp a requested page window to the listing limits. */
|
|
92
107
|
const pageWindow = (params = {}, total = 0) => {
|
|
@@ -111,6 +126,7 @@ const ERROR_STATUS = {
|
|
|
111
126
|
FS_IS_DIR: 400,
|
|
112
127
|
FS_NOT_DIR: 400,
|
|
113
128
|
FS_TOO_LARGE: 413,
|
|
129
|
+
FS_INCOMPLETE: 500,
|
|
114
130
|
FS_EXISTS: 409,
|
|
115
131
|
CONFLICT: 409,
|
|
116
132
|
INTERNAL: 500,
|
|
@@ -703,7 +719,7 @@ class HostPool {
|
|
|
703
719
|
this.knownHostsFile = knownHostsFile
|
|
704
720
|
this.records = new Map() // hostId -> HostRecord
|
|
705
721
|
this.connections = new Map() // hostId -> { authority, remote: RemoteHost }
|
|
706
|
-
this.connecting = new Map() // hostId ->
|
|
722
|
+
this.connecting = new Map() // hostId -> { authority, promise }
|
|
707
723
|
this.statuses = new Map() // hostId -> { state, message?, latencyMs?, checkedAt }
|
|
708
724
|
}
|
|
709
725
|
|
|
@@ -718,6 +734,12 @@ class HostPool {
|
|
|
718
734
|
this.connections.delete(hostId)
|
|
719
735
|
}
|
|
720
736
|
}
|
|
737
|
+
// In-flight connects for removed or re-addressed hosts must not be reused:
|
|
738
|
+
// the attempt itself notices the lost map entry and disposes its socket.
|
|
739
|
+
for (const [hostId, pending] of [...this.connecting]) {
|
|
740
|
+
const host = this.records.get(hostId)
|
|
741
|
+
if (!host || `${host.host}:${host.port}` !== pending.authority) this.connecting.delete(hostId)
|
|
742
|
+
}
|
|
721
743
|
}
|
|
722
744
|
|
|
723
745
|
host(hostId) {
|
|
@@ -751,9 +773,11 @@ class HostPool {
|
|
|
751
773
|
}
|
|
752
774
|
// Two concurrent first requests for one host must share one connection;
|
|
753
775
|
// otherwise the second RemoteHost overwrites the map entry and the first
|
|
754
|
-
// SSH socket leaks.
|
|
776
|
+
// SSH socket leaks. The pending attempt is bound to the address it dials,
|
|
777
|
+
// so a host edit during connect never hands the old socket to the new
|
|
778
|
+
// address.
|
|
755
779
|
const pending = this.connecting.get(hostRecord.id)
|
|
756
|
-
if (pending) return pending
|
|
780
|
+
if (pending && pending.authority === authority) return pending.promise
|
|
757
781
|
const remote = new RemoteHost({
|
|
758
782
|
host: hostRecord,
|
|
759
783
|
knownHostsFile: this.knownHostsFile,
|
|
@@ -765,16 +789,22 @@ class HostPool {
|
|
|
765
789
|
const attempt = (async () => {
|
|
766
790
|
try {
|
|
767
791
|
await remote.connect()
|
|
792
|
+
// The host may have been removed while the socket was connecting: keep
|
|
793
|
+
// the map state authoritative so the unused connection is closed.
|
|
794
|
+
if (this.connections.get(hostRecord.id) !== entry) {
|
|
795
|
+
remote.dispose().catch(() => {})
|
|
796
|
+
}
|
|
768
797
|
return remote
|
|
769
798
|
} catch (error) {
|
|
770
799
|
if (this.connections.get(hostRecord.id) === entry) this.connections.delete(hostRecord.id)
|
|
771
800
|
remote.dispose().catch(() => {})
|
|
772
801
|
throw error
|
|
773
802
|
} finally {
|
|
774
|
-
this.connecting.
|
|
803
|
+
const current = this.connecting.get(hostRecord.id)
|
|
804
|
+
if (current && current.promise === attempt) this.connecting.delete(hostRecord.id)
|
|
775
805
|
}
|
|
776
806
|
})()
|
|
777
|
-
this.connecting.set(hostRecord.id, attempt)
|
|
807
|
+
this.connecting.set(hostRecord.id, { authority, promise: attempt })
|
|
778
808
|
return attempt
|
|
779
809
|
}
|
|
780
810
|
|
|
@@ -1387,7 +1417,7 @@ export function apply(ctx) {
|
|
|
1387
1417
|
}
|
|
1388
1418
|
const read = await remote.readFile(source, { maxBytes: LINKED_OPEN_FILE_CAP })
|
|
1389
1419
|
if (read.truncated || read.data.length !== stat.size) {
|
|
1390
|
-
throw new ApiError('
|
|
1420
|
+
throw new ApiError('FS_INCOMPLETE', `${posix.basename(source)} 读取不完整,已取消复制`)
|
|
1391
1421
|
}
|
|
1392
1422
|
await remote.writeFileAtomic(target, read.data)
|
|
1393
1423
|
return 1
|
|
@@ -1595,6 +1625,7 @@ export function apply(ctx) {
|
|
|
1595
1625
|
// ── document bridge (Markdown/PDF/DOCX/XLSX/PPTX) ──────────────────────
|
|
1596
1626
|
|
|
1597
1627
|
const DOC_MAX_BYTES = 80 * 1024 * 1024
|
|
1628
|
+
const DOC_PDF_SUFFIX_MAX = 999
|
|
1598
1629
|
const DOC_TOOL = join(dirname(fileURLToPath(import.meta.url)), '..', 'scripts', 'doc_tool.py')
|
|
1599
1630
|
const DOC_KIND_BY_EXT = new Map([
|
|
1600
1631
|
['.md', 'markdown'], ['.markdown', 'markdown'],
|
|
@@ -1867,7 +1898,18 @@ export function apply(ctx) {
|
|
|
1867
1898
|
const target = params.path === undefined || params.path === ''
|
|
1868
1899
|
? scope.root
|
|
1869
1900
|
: scopeTarget(scope, params.path)
|
|
1870
|
-
const
|
|
1901
|
+
const limit = Math.min(Math.max(Number(params.limit) || LIST_PAGE_SIZE, 1), LIST_PAGE_MAX)
|
|
1902
|
+
const requestedOffset = Math.max(Number(params.offset) || 0, 0)
|
|
1903
|
+
const key = listingCacheKey(scope, target)
|
|
1904
|
+
let listing
|
|
1905
|
+
if (requestedOffset > 0) {
|
|
1906
|
+
const hit = listingCache.get(key)
|
|
1907
|
+
if (hit && Date.now() - hit.at < LISTING_CACHE_TTL_MS) listing = hit.listing
|
|
1908
|
+
}
|
|
1909
|
+
if (!listing) {
|
|
1910
|
+
listing = await scope.remote.listDir(target)
|
|
1911
|
+
rememberListing(key, listing)
|
|
1912
|
+
}
|
|
1871
1913
|
const all = listing.entries.map((entry) => ({
|
|
1872
1914
|
name: entry.name,
|
|
1873
1915
|
path: entry.path,
|
|
@@ -1876,7 +1918,7 @@ export function apply(ctx) {
|
|
|
1876
1918
|
mtime: entry.mtime,
|
|
1877
1919
|
relative: scopeRelative(scope, entry.path),
|
|
1878
1920
|
}))
|
|
1879
|
-
const page = pageWindow(
|
|
1921
|
+
const page = pageWindow({ limit, offset: requestedOffset }, all.length)
|
|
1880
1922
|
return {
|
|
1881
1923
|
path: target,
|
|
1882
1924
|
entries: all.slice(page.offset, page.offset + page.limit),
|
|
@@ -1959,6 +2001,7 @@ export function apply(ctx) {
|
|
|
1959
2001
|
mode: current ? current.mode : undefined,
|
|
1960
2002
|
})
|
|
1961
2003
|
await scopeMirror(scope, target, data)
|
|
2004
|
+
forgetListings()
|
|
1962
2005
|
return { path: target, size: info.size, mtime: info.mtime }
|
|
1963
2006
|
},
|
|
1964
2007
|
|
|
@@ -2083,19 +2126,21 @@ export function apply(ctx) {
|
|
|
2083
2126
|
const directory = posix.dirname(outputPath)
|
|
2084
2127
|
let candidate = outputPath
|
|
2085
2128
|
let index = 1
|
|
2086
|
-
while (await target.scope.remote.stat(candidate).catch(() => null)) {
|
|
2129
|
+
while (index <= DOC_PDF_SUFFIX_MAX && await target.scope.remote.stat(candidate).catch(() => null)) {
|
|
2087
2130
|
candidate = posix.join(directory, posix.basename(base) + '-' + index + '.pdf')
|
|
2088
2131
|
index += 1
|
|
2089
2132
|
}
|
|
2133
|
+
if (index > DOC_PDF_SUFFIX_MAX) throw new ApiError('FS_EXISTS', '同名 PDF 过多,请先清理目录中的旧导出')
|
|
2090
2134
|
outputPath = candidate
|
|
2091
2135
|
} else {
|
|
2092
2136
|
const directory = dirname(outputPath)
|
|
2093
2137
|
let candidate = outputPath
|
|
2094
2138
|
let index = 1
|
|
2095
|
-
while (existsSync(candidate)) {
|
|
2139
|
+
while (index <= DOC_PDF_SUFFIX_MAX && existsSync(candidate)) {
|
|
2096
2140
|
candidate = join(directory, basename(base) + '-' + index + '.pdf')
|
|
2097
2141
|
index += 1
|
|
2098
2142
|
}
|
|
2143
|
+
if (index > DOC_PDF_SUFFIX_MAX) throw new ApiError('FS_EXISTS', '同名 PDF 过多,请先清理目录中的旧导出')
|
|
2099
2144
|
outputPath = candidate
|
|
2100
2145
|
}
|
|
2101
2146
|
const temp = await makeDocTemp()
|
|
@@ -2141,6 +2186,7 @@ export function apply(ctx) {
|
|
|
2141
2186
|
if (current) throw new ApiError('FS_EXISTS', '目标已存在,请使用其他名称')
|
|
2142
2187
|
}
|
|
2143
2188
|
await scope.remote.mkdirp(target)
|
|
2189
|
+
forgetListings()
|
|
2144
2190
|
return {}
|
|
2145
2191
|
},
|
|
2146
2192
|
|
|
@@ -2151,6 +2197,7 @@ export function apply(ctx) {
|
|
|
2151
2197
|
if (from === scope.root) throw new ApiError('BAD_REQUEST', '不能移动工作区根目录')
|
|
2152
2198
|
await scope.remote.rename(from, to)
|
|
2153
2199
|
await scopeMirrorRemove(scope, from)
|
|
2200
|
+
forgetListings()
|
|
2154
2201
|
return {}
|
|
2155
2202
|
},
|
|
2156
2203
|
|
|
@@ -2172,6 +2219,7 @@ export function apply(ctx) {
|
|
|
2172
2219
|
if (await scope.remote.stat(target).catch(() => null)) throw new ApiError('FS_EXISTS', '目标目录中已存在同名文件或文件夹')
|
|
2173
2220
|
await scope.remote.rename(source, target)
|
|
2174
2221
|
await scopeMirrorRemove(scope, source)
|
|
2222
|
+
forgetListings()
|
|
2175
2223
|
return { path: target }
|
|
2176
2224
|
},
|
|
2177
2225
|
|
|
@@ -2181,6 +2229,7 @@ export function apply(ctx) {
|
|
|
2181
2229
|
if (target === scope.root) throw new ApiError('BAD_REQUEST', '不能删除工作区根目录')
|
|
2182
2230
|
await remoteDeleteRecursive(scope.remote, target)
|
|
2183
2231
|
await scopeMirrorRemove(scope, target)
|
|
2232
|
+
forgetListings()
|
|
2184
2233
|
return {}
|
|
2185
2234
|
},
|
|
2186
2235
|
|
|
@@ -2188,7 +2237,9 @@ export function apply(ctx) {
|
|
|
2188
2237
|
const scope = await requireScope(params)
|
|
2189
2238
|
if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '只有联动工作区支持文件传输')
|
|
2190
2239
|
if (params.side === 'local') {
|
|
2191
|
-
|
|
2240
|
+
const result = await transferLocalToRemote(scope.record, scope.remote, params.path)
|
|
2241
|
+
forgetListings()
|
|
2242
|
+
return result
|
|
2192
2243
|
}
|
|
2193
2244
|
const target = scopeTarget(scope, params.path)
|
|
2194
2245
|
return await transferToMirror(scope.record, scope.remote, target)
|
|
@@ -2219,7 +2270,16 @@ export function apply(ctx) {
|
|
|
2219
2270
|
throw new ApiError('BAD_REQUEST', '不能把目录复制到自身内部')
|
|
2220
2271
|
}
|
|
2221
2272
|
const target = await remoteUniqueTarget(scope.remote, parent, posix.basename(source))
|
|
2222
|
-
|
|
2273
|
+
let files
|
|
2274
|
+
try {
|
|
2275
|
+
files = await remoteCopyRecursive(scope.remote, source, target)
|
|
2276
|
+
} catch (error) {
|
|
2277
|
+
// A half-copied tree is worse than a clean failure: remove what this
|
|
2278
|
+
// call created (the target name was unique, so nothing else lives there).
|
|
2279
|
+
await remoteDeleteRecursive(scope.remote, target).catch(() => {})
|
|
2280
|
+
throw error
|
|
2281
|
+
}
|
|
2282
|
+
forgetListings()
|
|
2223
2283
|
return { path: target, files }
|
|
2224
2284
|
},
|
|
2225
2285
|
|
|
@@ -2307,6 +2367,46 @@ export function apply(ctx) {
|
|
|
2307
2367
|
}
|
|
2308
2368
|
},
|
|
2309
2369
|
|
|
2370
|
+
/** Copy dropped OS files into a local directory (a local drop). */
|
|
2371
|
+
async 'local.import'(params = {}) {
|
|
2372
|
+
const rawDir = typeof params.dir === 'string' && params.dir.trim() !== '' ? params.dir.trim() : homedir()
|
|
2373
|
+
if (!isAbsolute(rawDir)) throw new ApiError('BAD_REQUEST', '本地路径必须是绝对路径')
|
|
2374
|
+
const dir = resolve(rawDir)
|
|
2375
|
+
let info
|
|
2376
|
+
try {
|
|
2377
|
+
info = await fsStat(dir)
|
|
2378
|
+
} catch (error) {
|
|
2379
|
+
throw mapLocalFsError(error)
|
|
2380
|
+
}
|
|
2381
|
+
if (!info.isDirectory()) throw new ApiError('FS_NOT_DIR', '目标不是目录')
|
|
2382
|
+
const rawPaths = Array.isArray(params.paths) ? params.paths : [params.path]
|
|
2383
|
+
const paths = rawPaths.filter((item) => typeof item === 'string' && item.trim() !== '').map((item) => resolve(item.trim()))
|
|
2384
|
+
if (paths.length === 0) throw new ApiError('BAD_REQUEST', '没有可导入的文件')
|
|
2385
|
+
if (paths.length > 50) throw new ApiError('BAD_REQUEST', '一次最多导入 50 个文件')
|
|
2386
|
+
let copied = 0
|
|
2387
|
+
let skipped = 0
|
|
2388
|
+
for (const source of paths) {
|
|
2389
|
+
let sourceInfo
|
|
2390
|
+
try {
|
|
2391
|
+
sourceInfo = await fsStat(source)
|
|
2392
|
+
} catch (error) {
|
|
2393
|
+
skipped += 1
|
|
2394
|
+
continue
|
|
2395
|
+
}
|
|
2396
|
+
if (!sourceInfo.isFile() || sourceInfo.size > LINKED_OPEN_FILE_CAP) {
|
|
2397
|
+
skipped += 1
|
|
2398
|
+
continue
|
|
2399
|
+
}
|
|
2400
|
+
try {
|
|
2401
|
+
cpSync(source, uniqueLocalPath(dir, basename(source)))
|
|
2402
|
+
copied += 1
|
|
2403
|
+
} catch (error) {
|
|
2404
|
+
skipped += 1
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
return { dir, copied, skipped }
|
|
2408
|
+
},
|
|
2409
|
+
|
|
2310
2410
|
async 'local.stat'(params = {}) {
|
|
2311
2411
|
const target = typeof params.path === 'string' ? params.path.trim() : ''
|
|
2312
2412
|
if (target === '' || !isAbsolute(target)) throw new ApiError('BAD_REQUEST', '本地路径必须是绝对路径')
|
|
@@ -2587,7 +2687,9 @@ export function apply(ctx) {
|
|
|
2587
2687
|
const snapshot = scope.kind === 'linked' ? (scope.record.hostSnapshot || {}) : (scope.workspace.hostSnapshot || {})
|
|
2588
2688
|
if (side === 'local') {
|
|
2589
2689
|
if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '该工作区没有本地目录')
|
|
2590
|
-
const
|
|
2690
|
+
const localRoot = resolve(scope.record.localPath)
|
|
2691
|
+
const target = typeof params.path === 'string' && params.path.trim() !== '' ? resolve(params.path.trim()) : localRoot
|
|
2692
|
+
if (!isInsideLocal(localRoot, target)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
|
|
2591
2693
|
let info
|
|
2592
2694
|
try {
|
|
2593
2695
|
info = await fsStat(target)
|
|
@@ -2599,14 +2701,15 @@ export function apply(ctx) {
|
|
|
2599
2701
|
if (!(await openLocalTerminal(target))) throw new ApiError('INTERNAL', '无法启动本地终端')
|
|
2600
2702
|
return { opened: true, side, path: target }
|
|
2601
2703
|
}
|
|
2602
|
-
const
|
|
2704
|
+
const target = typeof params.path === 'string' && params.path.trim() !== '' ? scopeTarget(scope, params.path) : scope.root
|
|
2705
|
+
const stat = await scope.remote.stat(target)
|
|
2603
2706
|
if (stat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '远程目录不存在')
|
|
2604
2707
|
if (!snapshot.host || !snapshot.username) throw new ApiError('AUTH_FAILED', '缺少 SSH 主机或用户名,无法打开终端')
|
|
2605
|
-
if (testMode) return { opened: true, side, path:
|
|
2606
|
-
if (!(await openRemoteTerminal(snapshot.host, snapshot.port, snapshot.username,
|
|
2708
|
+
if (testMode) return { opened: true, side, path: target, host: snapshot.host, test: true }
|
|
2709
|
+
if (!(await openRemoteTerminal(snapshot.host, snapshot.port, snapshot.username, target))) {
|
|
2607
2710
|
throw new ApiError('INTERNAL', '无法启动远程终端(找不到可用的终端程序)')
|
|
2608
2711
|
}
|
|
2609
|
-
return { opened: true, side, path:
|
|
2712
|
+
return { opened: true, side, path: target, host: snapshot.host }
|
|
2610
2713
|
},
|
|
2611
2714
|
|
|
2612
2715
|
async 'session.info'(params = {}) {
|
|
@@ -2654,6 +2757,211 @@ export function apply(ctx) {
|
|
|
2654
2757
|
return await git.localSearch(scope.record.localPath, query, { maxResults })
|
|
2655
2758
|
},
|
|
2656
2759
|
|
|
2760
|
+
/**
|
|
2761
|
+
* Read-only comparison of one directory pair. Lists files that exist on one
|
|
2762
|
+
* side only and files whose size or mtime disagree; it never touches data.
|
|
2763
|
+
*/
|
|
2764
|
+
async 'fs.compare'(params = {}) {
|
|
2765
|
+
const scope = await requireScope(params)
|
|
2766
|
+
if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '只有联动工作区支持本地与远端对比')
|
|
2767
|
+
const root = params.path ? scopeTarget(scope, params.path) : scope.root
|
|
2768
|
+
const rootStat = await scope.remote.stat(root)
|
|
2769
|
+
if (rootStat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '只能对比目录')
|
|
2770
|
+
const relativeRoot = linkedRelative(scope.record, root)
|
|
2771
|
+
const localPathRoot = resolve(scope.record.localPath)
|
|
2772
|
+
const localRoot = join(localPathRoot, ...(relativeRoot === '' ? [] : relativeRoot.split('/')))
|
|
2773
|
+
if (!isInsideLocal(localPathRoot, localRoot)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
|
|
2774
|
+
const MAX_FILES = 4000
|
|
2775
|
+
const MAX_DEPTH = 12
|
|
2776
|
+
const MAX_LIST = 200
|
|
2777
|
+
const excluded = (entryName) => LINKED_SYNC_EXCLUDES.has(entryName) || entryName === '.local' || entryName === '.remote'
|
|
2778
|
+
const remoteMap = new Map()
|
|
2779
|
+
const localMap = new Map()
|
|
2780
|
+
const state = { files: 0, truncated: false }
|
|
2781
|
+
const walkRemote = async (directory, prefix, depth) => {
|
|
2782
|
+
if (depth > MAX_DEPTH || state.files >= MAX_FILES) {
|
|
2783
|
+
state.truncated = true
|
|
2784
|
+
return
|
|
2785
|
+
}
|
|
2786
|
+
let listing
|
|
2787
|
+
try {
|
|
2788
|
+
listing = await scope.remote.listDir(directory)
|
|
2789
|
+
} catch (error) {
|
|
2790
|
+
state.truncated = true
|
|
2791
|
+
return
|
|
2792
|
+
}
|
|
2793
|
+
for (const entry of listing.entries) {
|
|
2794
|
+
if (excluded(entry.name)) continue
|
|
2795
|
+
if (state.files >= MAX_FILES) {
|
|
2796
|
+
state.truncated = true
|
|
2797
|
+
return
|
|
2798
|
+
}
|
|
2799
|
+
const relative = prefix === '' ? entry.name : `${prefix}/${entry.name}`
|
|
2800
|
+
if (entry.type === 'dir') {
|
|
2801
|
+
await walkRemote(entry.path, relative, depth + 1)
|
|
2802
|
+
continue
|
|
2803
|
+
}
|
|
2804
|
+
if (entry.type !== 'file') continue
|
|
2805
|
+
remoteMap.set(relative, { size: entry.size, mtime: entry.mtime })
|
|
2806
|
+
state.files += 1
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
const walkLocal = (directory, prefix, depth) => {
|
|
2810
|
+
if (depth > MAX_DEPTH || state.files >= MAX_FILES) {
|
|
2811
|
+
state.truncated = true
|
|
2812
|
+
return
|
|
2813
|
+
}
|
|
2814
|
+
let entries
|
|
2815
|
+
try {
|
|
2816
|
+
entries = readdirSync(directory, { withFileTypes: true })
|
|
2817
|
+
} catch (error) {
|
|
2818
|
+
state.truncated = true
|
|
2819
|
+
return
|
|
2820
|
+
}
|
|
2821
|
+
for (const entry of entries) {
|
|
2822
|
+
if (excluded(entry.name)) continue
|
|
2823
|
+
if (state.files >= MAX_FILES) {
|
|
2824
|
+
state.truncated = true
|
|
2825
|
+
return
|
|
2826
|
+
}
|
|
2827
|
+
const relative = prefix === '' ? entry.name : `${prefix}/${entry.name}`
|
|
2828
|
+
const absolute = join(directory, entry.name)
|
|
2829
|
+
if (entry.isDirectory()) {
|
|
2830
|
+
walkLocal(absolute, relative, depth + 1)
|
|
2831
|
+
continue
|
|
2832
|
+
}
|
|
2833
|
+
if (!entry.isFile()) continue
|
|
2834
|
+
let info
|
|
2835
|
+
try {
|
|
2836
|
+
info = statSync(absolute)
|
|
2837
|
+
} catch (error) {
|
|
2838
|
+
state.truncated = true
|
|
2839
|
+
continue
|
|
2840
|
+
}
|
|
2841
|
+
localMap.set(relative, { size: info.size, mtime: info.mtimeMs })
|
|
2842
|
+
state.files += 1
|
|
2843
|
+
}
|
|
2844
|
+
}
|
|
2845
|
+
await walkRemote(root, '', 0)
|
|
2846
|
+
const localExists = existsSync(localRoot)
|
|
2847
|
+
if (localExists) walkLocal(localRoot, '', 0)
|
|
2848
|
+
const onlyLocal = []
|
|
2849
|
+
const changed = []
|
|
2850
|
+
for (const [relative, localInfo] of localMap) {
|
|
2851
|
+
const remoteInfo = remoteMap.get(relative)
|
|
2852
|
+
if (!remoteInfo) {
|
|
2853
|
+
onlyLocal.push({ path: relative, size: localInfo.size, mtime: localInfo.mtime })
|
|
2854
|
+
continue
|
|
2855
|
+
}
|
|
2856
|
+
const sizeDiffers = remoteInfo.size !== localInfo.size
|
|
2857
|
+
const mtimeDiffers = Math.abs(Number(remoteInfo.mtime) - localInfo.mtime) > 2000
|
|
2858
|
+
if (sizeDiffers || mtimeDiffers) {
|
|
2859
|
+
changed.push({
|
|
2860
|
+
path: relative,
|
|
2861
|
+
localSize: localInfo.size,
|
|
2862
|
+
remoteSize: remoteInfo.size,
|
|
2863
|
+
localMtime: localInfo.mtime,
|
|
2864
|
+
remoteMtime: remoteInfo.mtime,
|
|
2865
|
+
mtimeDiffers,
|
|
2866
|
+
})
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
const onlyRemote = []
|
|
2870
|
+
for (const [relative, remoteInfo] of remoteMap) {
|
|
2871
|
+
if (!localMap.has(relative)) onlyRemote.push({ path: relative, size: remoteInfo.size, mtime: remoteInfo.mtime })
|
|
2872
|
+
}
|
|
2873
|
+
const sorted = (list) => list.sort((left, right) => left.path.localeCompare(right.path))
|
|
2874
|
+
const same = localMap.size - onlyLocal.length - changed.length
|
|
2875
|
+
return {
|
|
2876
|
+
path: root,
|
|
2877
|
+
relative: relativeRoot,
|
|
2878
|
+
localPath: localRoot,
|
|
2879
|
+
localMissing: !localExists,
|
|
2880
|
+
counts: {
|
|
2881
|
+
onlyLocal: onlyLocal.length,
|
|
2882
|
+
onlyRemote: onlyRemote.length,
|
|
2883
|
+
changed: changed.length,
|
|
2884
|
+
same: Math.max(same, 0),
|
|
2885
|
+
},
|
|
2886
|
+
onlyLocal: sorted(onlyLocal).slice(0, MAX_LIST),
|
|
2887
|
+
onlyRemote: sorted(onlyRemote).slice(0, MAX_LIST),
|
|
2888
|
+
changed: sorted(changed).slice(0, MAX_LIST),
|
|
2889
|
+
truncated: state.truncated,
|
|
2890
|
+
}
|
|
2891
|
+
},
|
|
2892
|
+
|
|
2893
|
+
/** Copy one remote file into the linked local directory (a download). */
|
|
2894
|
+
async 'fs.pullToLocal'(params = {}) {
|
|
2895
|
+
const scope = await requireScope(params)
|
|
2896
|
+
if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '只有联动工作区支持下载到本地目录')
|
|
2897
|
+
const target = scopeTarget(scope, params.path)
|
|
2898
|
+
const relative = linkedRelative(scope.record, target)
|
|
2899
|
+
if (relative === '') throw new ApiError('BAD_REQUEST', '不能下载联动工作区的远端根目录')
|
|
2900
|
+
const stat = await scope.remote.stat(target)
|
|
2901
|
+
if (stat.type !== 'file') throw new ApiError('BAD_REQUEST', '只能下载单个文件')
|
|
2902
|
+
if (stat.size > LINKED_OPEN_FILE_CAP) throw new ApiError('FS_TOO_LARGE', '文件超过 64 MB,无法下载到本地')
|
|
2903
|
+
const read = await scope.remote.readFile(target, { maxBytes: LINKED_OPEN_FILE_CAP })
|
|
2904
|
+
if (read.truncated || read.data.length !== stat.size) throw new ApiError('FS_INCOMPLETE', '远端文件读取不完整,已取消下载')
|
|
2905
|
+
const localRoot = resolve(scope.record.localPath)
|
|
2906
|
+
const requestedDir = typeof params.targetDir === 'string' && params.targetDir.trim() !== '' ? resolve(params.targetDir.trim()) : ''
|
|
2907
|
+
let localTarget
|
|
2908
|
+
if (requestedDir !== '') {
|
|
2909
|
+
// Drag-and-drop lets the user pick an arbitrary drop folder inside the
|
|
2910
|
+
// linked local root; the plain "download to local" path keeps mirroring
|
|
2911
|
+
// the remote relative path.
|
|
2912
|
+
if (!isInsideLocal(localRoot, requestedDir)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
|
|
2913
|
+
const dirInfo = await fsStat(requestedDir).catch(() => null)
|
|
2914
|
+
if (!dirInfo || !dirInfo.isDirectory()) throw new ApiError('FS_NOT_DIR', '目标不是目录')
|
|
2915
|
+
localTarget = join(requestedDir, basename(target))
|
|
2916
|
+
} else {
|
|
2917
|
+
localTarget = join(localRoot, ...relative.split('/'))
|
|
2918
|
+
}
|
|
2919
|
+
mkdirSync(dirname(localTarget), { recursive: true })
|
|
2920
|
+
const temp = join(dirname(localTarget), `.~${basename(localTarget)}.${process.pid}.${Date.now()}.tmp`)
|
|
2921
|
+
try {
|
|
2922
|
+
writeFileSync(temp, read.data)
|
|
2923
|
+
renameSync(temp, localTarget)
|
|
2924
|
+
} catch (error) {
|
|
2925
|
+
try { rmSync(temp, { force: true }) } catch {}
|
|
2926
|
+
throw mapLocalFsError(error)
|
|
2927
|
+
}
|
|
2928
|
+
const next = await fsStat(localTarget)
|
|
2929
|
+
forgetListings()
|
|
2930
|
+
return { path: localTarget, relative, size: next.size, mtime: next.mtimeMs }
|
|
2931
|
+
},
|
|
2932
|
+
|
|
2933
|
+
/** Upload dropped local files into a remote directory. */
|
|
2934
|
+
async 'fs.upload'(params = {}) {
|
|
2935
|
+
const scope = await requireScope(params)
|
|
2936
|
+
const targetDir = scopeTarget(scope, params.path)
|
|
2937
|
+
const stat = await scope.remote.stat(targetDir)
|
|
2938
|
+
if (stat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '目标不是目录')
|
|
2939
|
+
const rawPaths = Array.isArray(params.paths) ? params.paths : [params.path]
|
|
2940
|
+
const paths = rawPaths.filter((item) => typeof item === 'string' && item.trim() !== '').map((item) => resolve(item.trim()))
|
|
2941
|
+
if (paths.length === 0) throw new ApiError('BAD_REQUEST', '没有可上传的文件')
|
|
2942
|
+
if (paths.length > 50) throw new ApiError('BAD_REQUEST', '一次最多上传 50 个文件')
|
|
2943
|
+
const limits = { files: 0, bytes: 0, skipped: 0 }
|
|
2944
|
+
for (const source of paths) {
|
|
2945
|
+
let info
|
|
2946
|
+
try {
|
|
2947
|
+
info = await fsStat(source)
|
|
2948
|
+
} catch (error) {
|
|
2949
|
+
limits.skipped += 1
|
|
2950
|
+
continue
|
|
2951
|
+
}
|
|
2952
|
+
if (!info.isFile() || info.size > LINKED_OPEN_FILE_CAP) {
|
|
2953
|
+
limits.skipped += 1
|
|
2954
|
+
continue
|
|
2955
|
+
}
|
|
2956
|
+
const data = readFileSync(source)
|
|
2957
|
+
await scope.remote.writeFileAtomic(posix.join(targetDir, basename(source)), data)
|
|
2958
|
+
limits.files += 1
|
|
2959
|
+
limits.bytes += data.length
|
|
2960
|
+
}
|
|
2961
|
+
forgetListings()
|
|
2962
|
+
return { side: 'remote', target: targetDir, files: limits.files, bytes: limits.bytes, skipped: limits.skipped }
|
|
2963
|
+
},
|
|
2964
|
+
|
|
2657
2965
|
// ── linked workspaces ──────────────────────────────────────────────────
|
|
2658
2966
|
|
|
2659
2967
|
async 'linked.list'() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-linked-workspace",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Linked local ⇄ remote workspaces and SSH-backed remote directories for DeepSeek Harness: theme-aware split editor, file-type icons, and an agent tool that edits remote files directly.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|