dsh-linked-workspace 0.8.3 → 0.10.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 +30 -0
- package/README.en.md +30 -0
- package/README.md +30 -0
- package/docs/API.md +8 -3
- package/docs/VERIFY.md +28 -0
- package/lib/client.js +910 -40
- package/lib/index.js +412 -6
- 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'
|
|
@@ -1495,6 +1495,9 @@ export function apply(ctx) {
|
|
|
1495
1495
|
return
|
|
1496
1496
|
}
|
|
1497
1497
|
const data = readFileSync(localPath)
|
|
1498
|
+
// A single-file transfer can target a directory that does not exist on the
|
|
1499
|
+
// remote side yet (only the directory branch used to mkdirp).
|
|
1500
|
+
await remote.mkdirp(posix.dirname(remotePath))
|
|
1498
1501
|
await withTimeout(remote.writeFileAtomic(remotePath, data), LINKED_SYNC_TIMEOUT_MS, `transfer ${relative}`)
|
|
1499
1502
|
limits.files += 1
|
|
1500
1503
|
limits.bytes += data.length
|
|
@@ -1785,6 +1788,95 @@ export function apply(ctx) {
|
|
|
1785
1788
|
try { return await target.scope.remote.stat(target.path) } catch { return null }
|
|
1786
1789
|
}
|
|
1787
1790
|
|
|
1791
|
+
// Content search: `fs.search` (git.localSearch / remoteSearch) only matches
|
|
1792
|
+
// file *names*. The workbench therefore needs a separate, bounded scan of
|
|
1793
|
+
// file bodies; both sides stop at a budget and report `truncated` instead of
|
|
1794
|
+
// hanging on a huge tree.
|
|
1795
|
+
const GREP_MAX_FILE_BYTES = 1024 * 1024
|
|
1796
|
+
const GREP_MAX_FILES = 2000
|
|
1797
|
+
const GREP_MAX_BYTES = 8 * 1024 * 1024
|
|
1798
|
+
const GREP_DEADLINE_MS = 5000
|
|
1799
|
+
|
|
1800
|
+
function grepDirectoryExcluded(name) {
|
|
1801
|
+
return LINKED_SYNC_EXCLUDES.has(name) || name === '.local' || name === '.remote'
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
function contentSearchLocal(root, query, maxResults) {
|
|
1805
|
+
const needle = Buffer.from(query, 'utf8')
|
|
1806
|
+
const entries = []
|
|
1807
|
+
const started = Date.now()
|
|
1808
|
+
let files = 0
|
|
1809
|
+
let bytes = 0
|
|
1810
|
+
let truncated = false
|
|
1811
|
+
const visit = (directory, prefix) => {
|
|
1812
|
+
if (truncated) return
|
|
1813
|
+
if (files >= GREP_MAX_FILES || bytes >= GREP_MAX_BYTES || Date.now() - started > GREP_DEADLINE_MS) {
|
|
1814
|
+
truncated = true
|
|
1815
|
+
return
|
|
1816
|
+
}
|
|
1817
|
+
let listing
|
|
1818
|
+
try {
|
|
1819
|
+
listing = readdirSync(directory, { withFileTypes: true })
|
|
1820
|
+
} catch {
|
|
1821
|
+
return
|
|
1822
|
+
}
|
|
1823
|
+
for (const entry of listing) {
|
|
1824
|
+
if (truncated) return
|
|
1825
|
+
if (grepDirectoryExcluded(entry.name)) continue
|
|
1826
|
+
const absolute = join(directory, entry.name)
|
|
1827
|
+
const relative = prefix === '' ? entry.name : `${prefix}/${entry.name}`
|
|
1828
|
+
if (entry.isDirectory()) {
|
|
1829
|
+
visit(absolute, relative)
|
|
1830
|
+
continue
|
|
1831
|
+
}
|
|
1832
|
+
if (!entry.isFile()) continue
|
|
1833
|
+
if (files >= GREP_MAX_FILES || bytes >= GREP_MAX_BYTES || Date.now() - started > GREP_DEADLINE_MS) {
|
|
1834
|
+
truncated = true
|
|
1835
|
+
return
|
|
1836
|
+
}
|
|
1837
|
+
files += 1
|
|
1838
|
+
let data
|
|
1839
|
+
try {
|
|
1840
|
+
data = readFileSync(absolute)
|
|
1841
|
+
} catch {
|
|
1842
|
+
continue
|
|
1843
|
+
}
|
|
1844
|
+
if (data.length > GREP_MAX_FILE_BYTES) continue
|
|
1845
|
+
bytes += data.length
|
|
1846
|
+
if (data.includes(0)) continue
|
|
1847
|
+
if (data.indexOf(needle) === -1) continue
|
|
1848
|
+
if (entries.length >= maxResults) {
|
|
1849
|
+
truncated = true
|
|
1850
|
+
return
|
|
1851
|
+
}
|
|
1852
|
+
entries.push({ path: absolute, relative, size: data.length })
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
visit(resolve(root), '')
|
|
1856
|
+
return { ok: true, query, entries, truncated }
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
async function contentSearchRemote(remote, root, query, maxResults) {
|
|
1860
|
+
const command = 'grep -rIl --binary-files=without-match'
|
|
1861
|
+
+ ' --exclude-dir=.git --exclude-dir=node_modules --exclude-dir=.local'
|
|
1862
|
+
+ ' --exclude-dir=.remote --exclude-dir=__pycache__'
|
|
1863
|
+
+ ` -e ${shellQuoteArg(query)} -- ${shellQuoteArg(root)} 2>/dev/null`
|
|
1864
|
+
+ ` | head -n ${maxResults + 1}`
|
|
1865
|
+
const result = await remote.exec(command, { timeoutMs: 20000 })
|
|
1866
|
+
if (result.code !== 0 && result.stdout.trim() === '') {
|
|
1867
|
+
// grep exits 1 when nothing matched; a real failure has code > 1.
|
|
1868
|
+
if (result.code > 1) throw new ApiError('INTERNAL', `内容搜索失败:${String(result.stderr || '').trim().slice(0, 200)}`)
|
|
1869
|
+
return { ok: true, query, entries: [], truncated: false }
|
|
1870
|
+
}
|
|
1871
|
+
const lines = String(result.stdout || '').split('\n').map((line) => line.trim()).filter((line) => line !== '')
|
|
1872
|
+
const truncated = lines.length > maxResults
|
|
1873
|
+
const entries = lines.slice(0, maxResults).map((line) => {
|
|
1874
|
+
const relative = line.startsWith(`${root}/`) ? line.slice(root.length + 1) : line
|
|
1875
|
+
return { path: line, relative, size: null }
|
|
1876
|
+
})
|
|
1877
|
+
return { ok: true, query, entries, truncated }
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1788
1880
|
// ── method table ─────────────────────────────────────────────────────────
|
|
1789
1881
|
|
|
1790
1882
|
const methods = {
|
|
@@ -2367,6 +2459,46 @@ export function apply(ctx) {
|
|
|
2367
2459
|
}
|
|
2368
2460
|
},
|
|
2369
2461
|
|
|
2462
|
+
/** Copy dropped OS files into a local directory (a local drop). */
|
|
2463
|
+
async 'local.import'(params = {}) {
|
|
2464
|
+
const rawDir = typeof params.dir === 'string' && params.dir.trim() !== '' ? params.dir.trim() : homedir()
|
|
2465
|
+
if (!isAbsolute(rawDir)) throw new ApiError('BAD_REQUEST', '本地路径必须是绝对路径')
|
|
2466
|
+
const dir = resolve(rawDir)
|
|
2467
|
+
let info
|
|
2468
|
+
try {
|
|
2469
|
+
info = await fsStat(dir)
|
|
2470
|
+
} catch (error) {
|
|
2471
|
+
throw mapLocalFsError(error)
|
|
2472
|
+
}
|
|
2473
|
+
if (!info.isDirectory()) throw new ApiError('FS_NOT_DIR', '目标不是目录')
|
|
2474
|
+
const rawPaths = Array.isArray(params.paths) ? params.paths : [params.path]
|
|
2475
|
+
const paths = rawPaths.filter((item) => typeof item === 'string' && item.trim() !== '').map((item) => resolve(item.trim()))
|
|
2476
|
+
if (paths.length === 0) throw new ApiError('BAD_REQUEST', '没有可导入的文件')
|
|
2477
|
+
if (paths.length > 50) throw new ApiError('BAD_REQUEST', '一次最多导入 50 个文件')
|
|
2478
|
+
let copied = 0
|
|
2479
|
+
let skipped = 0
|
|
2480
|
+
for (const source of paths) {
|
|
2481
|
+
let sourceInfo
|
|
2482
|
+
try {
|
|
2483
|
+
sourceInfo = await fsStat(source)
|
|
2484
|
+
} catch (error) {
|
|
2485
|
+
skipped += 1
|
|
2486
|
+
continue
|
|
2487
|
+
}
|
|
2488
|
+
if (!sourceInfo.isFile() || sourceInfo.size > LINKED_OPEN_FILE_CAP) {
|
|
2489
|
+
skipped += 1
|
|
2490
|
+
continue
|
|
2491
|
+
}
|
|
2492
|
+
try {
|
|
2493
|
+
cpSync(source, uniqueLocalPath(dir, basename(source)))
|
|
2494
|
+
copied += 1
|
|
2495
|
+
} catch (error) {
|
|
2496
|
+
skipped += 1
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
return { dir, copied, skipped }
|
|
2500
|
+
},
|
|
2501
|
+
|
|
2370
2502
|
async 'local.stat'(params = {}) {
|
|
2371
2503
|
const target = typeof params.path === 'string' ? params.path.trim() : ''
|
|
2372
2504
|
if (target === '' || !isAbsolute(target)) throw new ApiError('BAD_REQUEST', '本地路径必须是绝对路径')
|
|
@@ -2647,7 +2779,9 @@ export function apply(ctx) {
|
|
|
2647
2779
|
const snapshot = scope.kind === 'linked' ? (scope.record.hostSnapshot || {}) : (scope.workspace.hostSnapshot || {})
|
|
2648
2780
|
if (side === 'local') {
|
|
2649
2781
|
if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '该工作区没有本地目录')
|
|
2650
|
-
const
|
|
2782
|
+
const localRoot = resolve(scope.record.localPath)
|
|
2783
|
+
const target = typeof params.path === 'string' && params.path.trim() !== '' ? resolve(params.path.trim()) : localRoot
|
|
2784
|
+
if (!isInsideLocal(localRoot, target)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
|
|
2651
2785
|
let info
|
|
2652
2786
|
try {
|
|
2653
2787
|
info = await fsStat(target)
|
|
@@ -2659,14 +2793,15 @@ export function apply(ctx) {
|
|
|
2659
2793
|
if (!(await openLocalTerminal(target))) throw new ApiError('INTERNAL', '无法启动本地终端')
|
|
2660
2794
|
return { opened: true, side, path: target }
|
|
2661
2795
|
}
|
|
2662
|
-
const
|
|
2796
|
+
const target = typeof params.path === 'string' && params.path.trim() !== '' ? scopeTarget(scope, params.path) : scope.root
|
|
2797
|
+
const stat = await scope.remote.stat(target)
|
|
2663
2798
|
if (stat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '远程目录不存在')
|
|
2664
2799
|
if (!snapshot.host || !snapshot.username) throw new ApiError('AUTH_FAILED', '缺少 SSH 主机或用户名,无法打开终端')
|
|
2665
|
-
if (testMode) return { opened: true, side, path:
|
|
2666
|
-
if (!(await openRemoteTerminal(snapshot.host, snapshot.port, snapshot.username,
|
|
2800
|
+
if (testMode) return { opened: true, side, path: target, host: snapshot.host, test: true }
|
|
2801
|
+
if (!(await openRemoteTerminal(snapshot.host, snapshot.port, snapshot.username, target))) {
|
|
2667
2802
|
throw new ApiError('INTERNAL', '无法启动远程终端(找不到可用的终端程序)')
|
|
2668
2803
|
}
|
|
2669
|
-
return { opened: true, side, path:
|
|
2804
|
+
return { opened: true, side, path: target, host: snapshot.host }
|
|
2670
2805
|
},
|
|
2671
2806
|
|
|
2672
2807
|
async 'session.info'(params = {}) {
|
|
@@ -2714,6 +2849,270 @@ export function apply(ctx) {
|
|
|
2714
2849
|
return await git.localSearch(scope.record.localPath, query, { maxResults })
|
|
2715
2850
|
},
|
|
2716
2851
|
|
|
2852
|
+
/** Search file *contents* (fs.search only matches names). */
|
|
2853
|
+
async 'fs.grep'(params = {}) {
|
|
2854
|
+
const scope = await requireScope(params)
|
|
2855
|
+
const query = typeof params.query === 'string' ? params.query.trim() : ''
|
|
2856
|
+
const maxResults = Math.min(Math.max(Number(params.maxResults) || 200, 1), 1000)
|
|
2857
|
+
if (query === '') return { ok: true, query: '', entries: [], truncated: false }
|
|
2858
|
+
const side = params.side === 'local' ? 'local' : 'remote'
|
|
2859
|
+
if (side === 'remote') return await contentSearchRemote(scope.remote, scope.root, query, maxResults)
|
|
2860
|
+
if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '只有联动工作区有本地目录')
|
|
2861
|
+
return contentSearchLocal(scope.record.localPath, query, maxResults)
|
|
2862
|
+
},
|
|
2863
|
+
|
|
2864
|
+
/**
|
|
2865
|
+
* Read-only comparison of one directory pair. Lists files that exist on one
|
|
2866
|
+
* side only and files whose size or mtime disagree; it never touches data.
|
|
2867
|
+
*/
|
|
2868
|
+
async 'fs.compare'(params = {}) {
|
|
2869
|
+
const scope = await requireScope(params)
|
|
2870
|
+
if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '只有联动工作区支持本地与远端对比')
|
|
2871
|
+
const root = params.path ? scopeTarget(scope, params.path) : scope.root
|
|
2872
|
+
const rootStat = await scope.remote.stat(root)
|
|
2873
|
+
if (rootStat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '只能对比目录')
|
|
2874
|
+
const relativeRoot = linkedRelative(scope.record, root)
|
|
2875
|
+
const localPathRoot = resolve(scope.record.localPath)
|
|
2876
|
+
const localRoot = join(localPathRoot, ...(relativeRoot === '' ? [] : relativeRoot.split('/')))
|
|
2877
|
+
if (!isInsideLocal(localPathRoot, localRoot)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
|
|
2878
|
+
const MAX_FILES = 4000
|
|
2879
|
+
const MAX_DEPTH = 12
|
|
2880
|
+
const MAX_LIST = 200
|
|
2881
|
+
const excluded = (entryName) => LINKED_SYNC_EXCLUDES.has(entryName) || entryName === '.local' || entryName === '.remote'
|
|
2882
|
+
const remoteMap = new Map()
|
|
2883
|
+
const localMap = new Map()
|
|
2884
|
+
// Each side gets its own budget: a huge remote tree must not silently cut
|
|
2885
|
+
// the local walk (and the other way round).
|
|
2886
|
+
const state = { remoteFiles: 0, localFiles: 0, remoteTruncated: false, localTruncated: false, skippedLinks: 0 }
|
|
2887
|
+
const walkRemote = async (directory, prefix, depth) => {
|
|
2888
|
+
if (depth > MAX_DEPTH || state.remoteFiles >= MAX_FILES) {
|
|
2889
|
+
state.remoteTruncated = true
|
|
2890
|
+
return
|
|
2891
|
+
}
|
|
2892
|
+
let listing
|
|
2893
|
+
try {
|
|
2894
|
+
listing = await scope.remote.listDir(directory)
|
|
2895
|
+
} catch (error) {
|
|
2896
|
+
state.remoteTruncated = true
|
|
2897
|
+
return
|
|
2898
|
+
}
|
|
2899
|
+
for (const entry of listing.entries) {
|
|
2900
|
+
if (excluded(entry.name)) continue
|
|
2901
|
+
if (state.remoteFiles >= MAX_FILES) {
|
|
2902
|
+
state.remoteTruncated = true
|
|
2903
|
+
return
|
|
2904
|
+
}
|
|
2905
|
+
const relative = prefix === '' ? entry.name : `${prefix}/${entry.name}`
|
|
2906
|
+
if (entry.type === 'dir') {
|
|
2907
|
+
await walkRemote(entry.path, relative, depth + 1)
|
|
2908
|
+
continue
|
|
2909
|
+
}
|
|
2910
|
+
if (entry.type !== 'file') {
|
|
2911
|
+
state.skippedLinks += 1
|
|
2912
|
+
continue
|
|
2913
|
+
}
|
|
2914
|
+
remoteMap.set(relative, { size: entry.size, mtime: entry.mtime })
|
|
2915
|
+
state.remoteFiles += 1
|
|
2916
|
+
}
|
|
2917
|
+
}
|
|
2918
|
+
const walkLocal = (directory, prefix, depth) => {
|
|
2919
|
+
if (depth > MAX_DEPTH || state.localFiles >= MAX_FILES) {
|
|
2920
|
+
state.localTruncated = true
|
|
2921
|
+
return
|
|
2922
|
+
}
|
|
2923
|
+
let entries
|
|
2924
|
+
try {
|
|
2925
|
+
entries = readdirSync(directory, { withFileTypes: true })
|
|
2926
|
+
} catch (error) {
|
|
2927
|
+
state.localTruncated = true
|
|
2928
|
+
return
|
|
2929
|
+
}
|
|
2930
|
+
for (const entry of entries) {
|
|
2931
|
+
if (excluded(entry.name)) continue
|
|
2932
|
+
if (state.localFiles >= MAX_FILES) {
|
|
2933
|
+
state.localTruncated = true
|
|
2934
|
+
return
|
|
2935
|
+
}
|
|
2936
|
+
const relative = prefix === '' ? entry.name : `${prefix}/${entry.name}`
|
|
2937
|
+
const absolute = join(directory, entry.name)
|
|
2938
|
+
if (entry.isDirectory()) {
|
|
2939
|
+
walkLocal(absolute, relative, depth + 1)
|
|
2940
|
+
continue
|
|
2941
|
+
}
|
|
2942
|
+
if (!entry.isFile()) {
|
|
2943
|
+
state.skippedLinks += 1
|
|
2944
|
+
continue
|
|
2945
|
+
}
|
|
2946
|
+
let info
|
|
2947
|
+
try {
|
|
2948
|
+
info = statSync(absolute)
|
|
2949
|
+
} catch (error) {
|
|
2950
|
+
state.localTruncated = true
|
|
2951
|
+
continue
|
|
2952
|
+
}
|
|
2953
|
+
localMap.set(relative, { size: info.size, mtime: info.mtimeMs })
|
|
2954
|
+
state.localFiles += 1
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
await walkRemote(root, '', 0)
|
|
2958
|
+
const localExists = existsSync(localRoot)
|
|
2959
|
+
if (localExists) walkLocal(localRoot, '', 0)
|
|
2960
|
+
const onlyLocal = []
|
|
2961
|
+
const changed = []
|
|
2962
|
+
for (const [relative, localInfo] of localMap) {
|
|
2963
|
+
const remoteInfo = remoteMap.get(relative)
|
|
2964
|
+
if (!remoteInfo) {
|
|
2965
|
+
onlyLocal.push({ path: relative, size: localInfo.size, mtime: localInfo.mtime })
|
|
2966
|
+
continue
|
|
2967
|
+
}
|
|
2968
|
+
const sizeDiffers = remoteInfo.size !== localInfo.size
|
|
2969
|
+
const mtimeDiffers = Math.abs(Number(remoteInfo.mtime) - localInfo.mtime) > 2000
|
|
2970
|
+
if (sizeDiffers || mtimeDiffers) {
|
|
2971
|
+
changed.push({
|
|
2972
|
+
path: relative,
|
|
2973
|
+
localSize: localInfo.size,
|
|
2974
|
+
remoteSize: remoteInfo.size,
|
|
2975
|
+
localMtime: localInfo.mtime,
|
|
2976
|
+
remoteMtime: remoteInfo.mtime,
|
|
2977
|
+
mtimeDiffers,
|
|
2978
|
+
})
|
|
2979
|
+
}
|
|
2980
|
+
}
|
|
2981
|
+
const onlyRemote = []
|
|
2982
|
+
for (const [relative, remoteInfo] of remoteMap) {
|
|
2983
|
+
if (!localMap.has(relative)) onlyRemote.push({ path: relative, size: remoteInfo.size, mtime: remoteInfo.mtime })
|
|
2984
|
+
}
|
|
2985
|
+
const sorted = (list) => list.sort((left, right) => left.path.localeCompare(right.path))
|
|
2986
|
+
const same = localMap.size - onlyLocal.length - changed.length
|
|
2987
|
+
return {
|
|
2988
|
+
path: root,
|
|
2989
|
+
relative: relativeRoot,
|
|
2990
|
+
localPath: localRoot,
|
|
2991
|
+
localMissing: !localExists,
|
|
2992
|
+
counts: {
|
|
2993
|
+
onlyLocal: onlyLocal.length,
|
|
2994
|
+
onlyRemote: onlyRemote.length,
|
|
2995
|
+
changed: changed.length,
|
|
2996
|
+
same: Math.max(same, 0),
|
|
2997
|
+
},
|
|
2998
|
+
onlyLocal: sorted(onlyLocal).slice(0, MAX_LIST),
|
|
2999
|
+
onlyRemote: sorted(onlyRemote).slice(0, MAX_LIST),
|
|
3000
|
+
changed: sorted(changed).slice(0, MAX_LIST),
|
|
3001
|
+
truncated: state.remoteTruncated || state.localTruncated,
|
|
3002
|
+
remoteTruncated: state.remoteTruncated,
|
|
3003
|
+
localTruncated: state.localTruncated,
|
|
3004
|
+
skippedLinks: state.skippedLinks,
|
|
3005
|
+
}
|
|
3006
|
+
},
|
|
3007
|
+
|
|
3008
|
+
/** Copy one remote file into the linked local directory (a download). */
|
|
3009
|
+
async 'fs.pullToLocal'(params = {}) {
|
|
3010
|
+
const scope = await requireScope(params)
|
|
3011
|
+
if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '只有联动工作区支持下载到本地目录')
|
|
3012
|
+
const target = scopeTarget(scope, params.path)
|
|
3013
|
+
const relative = linkedRelative(scope.record, target)
|
|
3014
|
+
if (relative === '') throw new ApiError('BAD_REQUEST', '不能下载联动工作区的远端根目录')
|
|
3015
|
+
const stat = await scope.remote.stat(target)
|
|
3016
|
+
if (stat.type !== 'file') throw new ApiError('BAD_REQUEST', '只能下载单个文件')
|
|
3017
|
+
if (stat.size > LINKED_OPEN_FILE_CAP) throw new ApiError('FS_TOO_LARGE', '文件超过 64 MB,无法下载到本地')
|
|
3018
|
+
const read = await scope.remote.readFile(target, { maxBytes: LINKED_OPEN_FILE_CAP })
|
|
3019
|
+
if (read.truncated || read.data.length !== stat.size) throw new ApiError('FS_INCOMPLETE', '远端文件读取不完整,已取消下载')
|
|
3020
|
+
const localRoot = resolve(scope.record.localPath)
|
|
3021
|
+
const requestedDir = typeof params.targetDir === 'string' && params.targetDir.trim() !== '' ? resolve(params.targetDir.trim()) : ''
|
|
3022
|
+
let localTarget
|
|
3023
|
+
if (requestedDir !== '') {
|
|
3024
|
+
// Drag-and-drop lets the user pick an arbitrary drop folder inside the
|
|
3025
|
+
// linked local root; the plain "download to local" path keeps mirroring
|
|
3026
|
+
// the remote relative path.
|
|
3027
|
+
if (!isInsideLocal(localRoot, requestedDir)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
|
|
3028
|
+
const dirInfo = await fsStat(requestedDir).catch(() => null)
|
|
3029
|
+
if (!dirInfo || !dirInfo.isDirectory()) throw new ApiError('FS_NOT_DIR', '目标不是目录')
|
|
3030
|
+
localTarget = join(requestedDir, basename(target))
|
|
3031
|
+
} else {
|
|
3032
|
+
localTarget = join(localRoot, ...relative.split('/'))
|
|
3033
|
+
}
|
|
3034
|
+
// Dropping a file must never silently replace an existing local file.
|
|
3035
|
+
const conflict = params.conflict === 'overwrite' || params.conflict === 'unique' ? params.conflict : 'error'
|
|
3036
|
+
if (existsSync(localTarget)) {
|
|
3037
|
+
if (conflict === 'error') throw new ApiError('FS_EXISTS', `本地已存在同名文件:${basename(localTarget)}`)
|
|
3038
|
+
if (conflict === 'unique') localTarget = uniqueLocalPath(dirname(localTarget), basename(localTarget))
|
|
3039
|
+
}
|
|
3040
|
+
mkdirSync(dirname(localTarget), { recursive: true })
|
|
3041
|
+
const temp = join(dirname(localTarget), `.~${basename(localTarget)}.${process.pid}.${Date.now()}.tmp`)
|
|
3042
|
+
try {
|
|
3043
|
+
writeFileSync(temp, read.data)
|
|
3044
|
+
renameSync(temp, localTarget)
|
|
3045
|
+
} catch (error) {
|
|
3046
|
+
try { rmSync(temp, { force: true }) } catch {}
|
|
3047
|
+
throw mapLocalFsError(error)
|
|
3048
|
+
}
|
|
3049
|
+
const next = await fsStat(localTarget)
|
|
3050
|
+
forgetListings()
|
|
3051
|
+
return { path: localTarget, relative, size: next.size, mtime: next.mtimeMs }
|
|
3052
|
+
},
|
|
3053
|
+
|
|
3054
|
+
/** Upload dropped local files into a remote directory. */
|
|
3055
|
+
async 'fs.upload'(params = {}) {
|
|
3056
|
+
const scope = await requireScope(params)
|
|
3057
|
+
const targetDir = scopeTarget(scope, params.path)
|
|
3058
|
+
// Upload targets come from dialogs and from compare "push"; nested folders
|
|
3059
|
+
// may not exist remotely yet, so create the chain before validating.
|
|
3060
|
+
let stat = await scope.remote.stat(targetDir).catch(() => null)
|
|
3061
|
+
if (!stat) {
|
|
3062
|
+
await scope.remote.mkdirp(targetDir)
|
|
3063
|
+
stat = await scope.remote.stat(targetDir)
|
|
3064
|
+
}
|
|
3065
|
+
if (stat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '目标不是目录')
|
|
3066
|
+
const rawPaths = Array.isArray(params.paths) ? params.paths : [params.path]
|
|
3067
|
+
const paths = rawPaths.filter((item) => typeof item === 'string' && item.trim() !== '').map((item) => resolve(item.trim()))
|
|
3068
|
+
if (paths.length === 0) throw new ApiError('BAD_REQUEST', '没有可上传的文件')
|
|
3069
|
+
if (paths.length > 50) throw new ApiError('BAD_REQUEST', '一次最多上传 50 个文件')
|
|
3070
|
+
const conflict = params.conflict === 'overwrite' || params.conflict === 'unique' ? params.conflict : 'error'
|
|
3071
|
+
const prepared = []
|
|
3072
|
+
const limits = { files: 0, bytes: 0, skipped: 0 }
|
|
3073
|
+
for (const source of paths) {
|
|
3074
|
+
let info
|
|
3075
|
+
try {
|
|
3076
|
+
info = await fsStat(source)
|
|
3077
|
+
} catch (error) {
|
|
3078
|
+
limits.skipped += 1
|
|
3079
|
+
continue
|
|
3080
|
+
}
|
|
3081
|
+
if (!info.isFile() || info.size > LINKED_OPEN_FILE_CAP) {
|
|
3082
|
+
limits.skipped += 1
|
|
3083
|
+
continue
|
|
3084
|
+
}
|
|
3085
|
+
prepared.push({ source, name: basename(source) })
|
|
3086
|
+
}
|
|
3087
|
+
if (conflict === 'error' && prepared.length > 0) {
|
|
3088
|
+
const conflicts = []
|
|
3089
|
+
for (const item of prepared) {
|
|
3090
|
+
const existing = await scope.remote.stat(posix.join(targetDir, item.name)).catch(() => null)
|
|
3091
|
+
if (existing) conflicts.push(item.name)
|
|
3092
|
+
}
|
|
3093
|
+
if (conflicts.length > 0) {
|
|
3094
|
+
const shown = conflicts.slice(0, 5).join('、')
|
|
3095
|
+
const more = conflicts.length > 5 ? ` 等 ${conflicts.length} 个` : ''
|
|
3096
|
+
throw new ApiError('FS_EXISTS', `目标目录已有同名文件:${shown}${more}`)
|
|
3097
|
+
}
|
|
3098
|
+
}
|
|
3099
|
+
for (const item of prepared) {
|
|
3100
|
+
let targetName = item.name
|
|
3101
|
+
if (conflict === 'unique') {
|
|
3102
|
+
const existing = await scope.remote.stat(posix.join(targetDir, targetName)).catch(() => null)
|
|
3103
|
+
// Computed per item, after the previous write landed, so a batch of
|
|
3104
|
+
// same-named sources cannot collide with itself.
|
|
3105
|
+
if (existing) targetName = posix.basename(await remoteUniqueTarget(scope.remote, targetDir, targetName))
|
|
3106
|
+
}
|
|
3107
|
+
const data = readFileSync(item.source)
|
|
3108
|
+
await scope.remote.writeFileAtomic(posix.join(targetDir, targetName), data)
|
|
3109
|
+
limits.files += 1
|
|
3110
|
+
limits.bytes += data.length
|
|
3111
|
+
}
|
|
3112
|
+
forgetListings()
|
|
3113
|
+
return { side: 'remote', target: targetDir, files: limits.files, bytes: limits.bytes, skipped: limits.skipped }
|
|
3114
|
+
},
|
|
3115
|
+
|
|
2717
3116
|
// ── linked workspaces ──────────────────────────────────────────────────
|
|
2718
3117
|
|
|
2719
3118
|
async 'linked.list'() {
|
|
@@ -2805,6 +3204,13 @@ export function apply(ctx) {
|
|
|
2805
3204
|
const workspace = findWorkspaceByPath(linkedRegistry(), record.localPath)
|
|
2806
3205
|
if (workspace) await renameDshWorkspace(ctx, String(workspace.id), linkedWorkspaceTitle(record.name))
|
|
2807
3206
|
}
|
|
3207
|
+
if (params.ensureWorkspace === true) {
|
|
3208
|
+
const existing = findWorkspaceByPath(linkedRegistry(), record.localPath)
|
|
3209
|
+
if (!existing) {
|
|
3210
|
+
const created = await ensureDshWorkspace(ctx.get('workspaceRegistry', true), record.localPath, linkedWorkspaceTitle(record.name))
|
|
3211
|
+
if (!created) throw new ApiError('INTERNAL', '无法注册本地工作区')
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
2808
3214
|
if (nextMirror === true && params.mirror === true) {
|
|
2809
3215
|
try {
|
|
2810
3216
|
startSyncJob(record, { reason: 'enable' })
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-linked-workspace",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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",
|