dsh-remote 0.8.93 → 1.0.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.
Files changed (186) hide show
  1. package/LICENSE +21 -21
  2. package/README.en.md +408 -370
  3. package/README.md +391 -355
  4. package/cordis.patch.yml +9 -9
  5. package/lib/audit.js +45 -45
  6. package/lib/auto-update.js +70 -0
  7. package/lib/binding.js +172 -172
  8. package/lib/boot.js +42 -0
  9. package/lib/client.js +5615 -4648
  10. package/lib/credential.js +277 -270
  11. package/lib/errors.js +141 -141
  12. package/lib/exec-job.js +245 -242
  13. package/lib/file-reference.js +457 -457
  14. package/lib/forwards.js +258 -258
  15. package/lib/host-api.js +822 -822
  16. package/lib/hostkey.js +108 -108
  17. package/lib/http-transport.js +201 -201
  18. package/lib/ignore.js +202 -202
  19. package/lib/index.js +4564 -4488
  20. package/lib/instructions-dynamic.js +415 -415
  21. package/lib/instructions.js +288 -288
  22. package/lib/open-folder.js +56 -56
  23. package/lib/openssh.js +950 -940
  24. package/lib/paths.js +292 -292
  25. package/lib/pool.js +1029 -1029
  26. package/lib/posix-shell.cjs +34 -34
  27. package/lib/registry.js +296 -296
  28. package/lib/remote-fs.js +173 -173
  29. package/lib/remote-sandbox.js +183 -183
  30. package/lib/remote-shell-child.cjs +413 -397
  31. package/lib/remote-shell.js +697 -697
  32. package/lib/remote-skills.js +312 -312
  33. package/lib/routes-audit.js +58 -58
  34. package/lib/routes-forwards.js +72 -72
  35. package/lib/routes-fs.js +251 -251
  36. package/lib/routes-machines.js +217 -186
  37. package/lib/routes-misc.js +187 -187
  38. package/lib/routes-term.js +169 -169
  39. package/lib/routes-update.js +147 -143
  40. package/lib/routes-web-attach.js +121 -112
  41. package/lib/routes-workspace.js +325 -312
  42. package/lib/search.js +235 -235
  43. package/lib/self-heal.js +130 -0
  44. package/lib/sftp-surface.js +119 -119
  45. package/lib/sshconfig.js +779 -779
  46. package/lib/sync-status.js +31 -0
  47. package/lib/sync.js +350 -350
  48. package/lib/tasks.js +95 -95
  49. package/lib/telemetry.js +247 -247
  50. package/lib/terminal.js +205 -205
  51. package/lib/tui/flow.js +130 -130
  52. package/lib/tui/format.js +380 -380
  53. package/lib/tui/i18n.js +350 -350
  54. package/lib/tui/index.js +531 -531
  55. package/lib/tui/provider.js +344 -344
  56. package/lib/tui/scene.js +986 -986
  57. package/lib/update.js +497 -490
  58. package/lib/web-attach.js +1081 -860
  59. package/lib/workspace-cleanup.js +337 -337
  60. package/node_modules/buildcheck/.eslintrc.js +5 -0
  61. package/node_modules/buildcheck/.github/workflows/ci.yml +88 -0
  62. package/node_modules/buildcheck/.github/workflows/lint.yml +27 -0
  63. package/node_modules/buildcheck/.npmignore +3 -0
  64. package/node_modules/buildcheck/LICENSE +19 -0
  65. package/node_modules/buildcheck/README.md +106 -0
  66. package/node_modules/buildcheck/deps/Find-VisualStudio.cs +250 -0
  67. package/node_modules/buildcheck/lib/findvs.js +361 -0
  68. package/node_modules/buildcheck/lib/index.js +738 -0
  69. package/node_modules/buildcheck/package.json +36 -0
  70. package/node_modules/buildcheck/test/test.js +8 -0
  71. package/node_modules/cpu-features/.eslintrc.js +5 -0
  72. package/node_modules/cpu-features/.github/workflows/ci.yml +74 -0
  73. package/node_modules/cpu-features/.github/workflows/lint.yml +27 -0
  74. package/node_modules/cpu-features/LICENSE +19 -0
  75. package/node_modules/cpu-features/README.md +59 -0
  76. package/node_modules/cpu-features/binding.gyp +16 -0
  77. package/node_modules/cpu-features/buildcheck.js +32 -0
  78. package/node_modules/cpu-features/deps/cpu_features/.clang-format +4 -0
  79. package/node_modules/cpu-features/deps/cpu_features/.dockerignore +31 -0
  80. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/Dockerfile +5 -0
  81. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/aarch64_linux_cmake.yml +30 -0
  82. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_freebsd_cmake.yml +22 -0
  83. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_linux_bazel.yml +26 -0
  84. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_linux_cmake.yml +31 -0
  85. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_macos_cmake.yml +43 -0
  86. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_windows_cmake.yml +25 -0
  87. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/arm_linux_cmake.yml +31 -0
  88. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/clang_format.yml +24 -0
  89. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/mips_linux_cmake.yml +30 -0
  90. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/power_linux_cmake.yml +29 -0
  91. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/riscv_linux_cmake.yml +28 -0
  92. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/s390x_linux_cmake.yml +27 -0
  93. package/node_modules/cpu-features/deps/cpu_features/.grenrc.yml +21 -0
  94. package/node_modules/cpu-features/deps/cpu_features/BUILD.bazel +329 -0
  95. package/node_modules/cpu-features/deps/cpu_features/CMakeLists.txt +261 -0
  96. package/node_modules/cpu-features/deps/cpu_features/CONTRIBUTING.md +23 -0
  97. package/node_modules/cpu-features/deps/cpu_features/LICENSE +230 -0
  98. package/node_modules/cpu-features/deps/cpu_features/README.md +272 -0
  99. package/node_modules/cpu-features/deps/cpu_features/WORKSPACE +19 -0
  100. package/node_modules/cpu-features/deps/cpu_features/bazel/ci/README.md +5 -0
  101. package/node_modules/cpu-features/deps/cpu_features/bazel/platforms.bzl +11 -0
  102. package/node_modules/cpu-features/deps/cpu_features/cmake/CpuFeaturesConfig.cmake.in +3 -0
  103. package/node_modules/cpu-features/deps/cpu_features/cmake/CpuFeaturesNdkCompatConfig.cmake.in +3 -0
  104. package/node_modules/cpu-features/deps/cpu_features/cmake/README.md +30 -0
  105. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/Makefile +252 -0
  106. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/README.md +40 -0
  107. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/docker.dot +64 -0
  108. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/docker.svg +312 -0
  109. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/generate_image.sh +7 -0
  110. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/docker/amd64/Dockerfile +48 -0
  111. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/docker/toolchain/Dockerfile +34 -0
  112. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/sample/CMakeLists.txt +22 -0
  113. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/sample/main.cpp +11 -0
  114. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/vagrant/freebsd/Vagrantfile +107 -0
  115. package/node_modules/cpu-features/deps/cpu_features/cmake/googletest.CMakeLists.txt.in +15 -0
  116. package/node_modules/cpu-features/deps/cpu_features/cpu_features.gyp +105 -0
  117. package/node_modules/cpu-features/deps/cpu_features/include/cpu_features_cache_info.h +54 -0
  118. package/node_modules/cpu-features/deps/cpu_features/include/cpu_features_macros.h +384 -0
  119. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_aarch64.h +259 -0
  120. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_arm.h +121 -0
  121. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_mips.h +74 -0
  122. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_ppc.h +149 -0
  123. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_riscv.h +72 -0
  124. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_s390x.h +108 -0
  125. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_x86.h +288 -0
  126. package/node_modules/cpu-features/deps/cpu_features/include/internal/bit_utils.h +40 -0
  127. package/node_modules/cpu-features/deps/cpu_features/include/internal/cpuid_x86.h +37 -0
  128. package/node_modules/cpu-features/deps/cpu_features/include/internal/filesystem.h +39 -0
  129. package/node_modules/cpu-features/deps/cpu_features/include/internal/hwcaps.h +240 -0
  130. package/node_modules/cpu-features/deps/cpu_features/include/internal/stack_line_reader.h +49 -0
  131. package/node_modules/cpu-features/deps/cpu_features/include/internal/string_view.h +110 -0
  132. package/node_modules/cpu-features/deps/cpu_features/include/internal/windows_utils.h +70 -0
  133. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/CMakeLists.txt +60 -0
  134. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/README.md +4 -0
  135. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/cpu-features.c +205 -0
  136. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/cpu-features.h +320 -0
  137. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/ndk-compat-test.c +12 -0
  138. package/node_modules/cpu-features/deps/cpu_features/patches/0001-Add-Apple-Silicon-Support.patch +456 -0
  139. package/node_modules/cpu-features/deps/cpu_features/scripts/generate_badges.d +165 -0
  140. package/node_modules/cpu-features/deps/cpu_features/scripts/make_release.sh +75 -0
  141. package/node_modules/cpu-features/deps/cpu_features/scripts/run_integration.sh +490 -0
  142. package/node_modules/cpu-features/deps/cpu_features/scripts/test_integration.sh +79 -0
  143. package/node_modules/cpu-features/deps/cpu_features/src/copy.inl +19 -0
  144. package/node_modules/cpu-features/deps/cpu_features/src/define_introspection.inl +86 -0
  145. package/node_modules/cpu-features/deps/cpu_features/src/define_introspection_and_hwcaps.inl +26 -0
  146. package/node_modules/cpu-features/deps/cpu_features/src/equals.inl +22 -0
  147. package/node_modules/cpu-features/deps/cpu_features/src/filesystem.c +62 -0
  148. package/node_modules/cpu-features/deps/cpu_features/src/hwcaps.c +169 -0
  149. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64__base_implementation.inl +88 -0
  150. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_linux_or_android.c +79 -0
  151. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_macos_or_iphone.c +82 -0
  152. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_windows.c +138 -0
  153. package/node_modules/cpu-features/deps/cpu_features/src/impl_arm_linux_or_android.c +212 -0
  154. package/node_modules/cpu-features/deps/cpu_features/src/impl_mips_linux_or_android.c +93 -0
  155. package/node_modules/cpu-features/deps/cpu_features/src/impl_ppc_linux.c +163 -0
  156. package/node_modules/cpu-features/deps/cpu_features/src/impl_riscv_linux.c +111 -0
  157. package/node_modules/cpu-features/deps/cpu_features/src/impl_s390x_linux.c +120 -0
  158. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86__base_implementation.inl +2055 -0
  159. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_freebsd.c +68 -0
  160. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_linux_or_android.c +58 -0
  161. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_macos.c +57 -0
  162. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_windows.c +58 -0
  163. package/node_modules/cpu-features/deps/cpu_features/src/stack_line_reader.c +132 -0
  164. package/node_modules/cpu-features/deps/cpu_features/src/string_view.c +192 -0
  165. package/node_modules/cpu-features/deps/cpu_features/src/utils/list_cpu_features.c +461 -0
  166. package/node_modules/cpu-features/deps/cpu_features/test/CMakeLists.txt +114 -0
  167. package/node_modules/cpu-features/deps/cpu_features/test/bit_utils_test.cc +53 -0
  168. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_aarch64_test.cc +396 -0
  169. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_arm_test.cc +384 -0
  170. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_mips_test.cc +176 -0
  171. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_ppc_test.cc +131 -0
  172. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_riscv_test.cc +180 -0
  173. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_s390x_test.cc +82 -0
  174. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_x86_test.cc +1729 -0
  175. package/node_modules/cpu-features/deps/cpu_features/test/filesystem_for_testing.cc +103 -0
  176. package/node_modules/cpu-features/deps/cpu_features/test/filesystem_for_testing.h +61 -0
  177. package/node_modules/cpu-features/deps/cpu_features/test/hwcaps_for_testing.cc +52 -0
  178. package/node_modules/cpu-features/deps/cpu_features/test/hwcaps_for_testing.h +31 -0
  179. package/node_modules/cpu-features/deps/cpu_features/test/stack_line_reader_test.cc +132 -0
  180. package/node_modules/cpu-features/deps/cpu_features/test/string_view_test.cc +202 -0
  181. package/node_modules/cpu-features/lib/index.js +5 -0
  182. package/node_modules/cpu-features/package.json +40 -0
  183. package/node_modules/cpu-features/src/binding.cc +151 -0
  184. package/node_modules/cpu-features/test/test.js +12 -0
  185. package/node_modules/nan/tools/1to2.js +0 -0
  186. package/package.json +106 -103
@@ -1,344 +1,344 @@
1
- // dsh-remote TUI — 工作区 provider(scheme: dshremote)。
2
- //
3
- // 契约见 dsh-tui `dsh-adapter/workspaces.d.ts` 的 TuiWorkspaceProvider:
4
- // · `list(signal)` 只读**内存缓存**(宿主强制 2s 预算,绝不在这里连网)
5
- // · `resolve(uri)` / `resolvePath(path, cwd)` 走宿主 api 的 ensureRemoteWorkspace
6
- // · `describe(cwd)` 同步、只读本地镜像 meta(lib/binding.js#resolveMirror),
7
- // 不创建任何目录
8
- //
9
- // cwd 必须是本地真实存在的镜像目录:会话远程身份由 session header.cwd 落在哪个
10
- // 镜像目录决定。provider 的 `uri` 表达远程身份,`cwd` 永远指向镜像。
11
-
12
- import path from 'node:path'
13
- import { resolveMirror } from '../binding.js'
14
- import { buildUri, joinRemote, parseUri, targetFromWorkspace } from './format.js'
15
-
16
- /** 每台机器默认列出的最近工作区条数(与 docs/tui.md §5 一致)。 */
17
- const DEFAULT_RECENT_LIMIT = 5
18
- /** tuiProviderRecentLimit 的合法上限。 */
19
- const MAX_RECENT_LIMIT = 20
20
-
21
- /** 把 1..20 的限制值夹紧;非法值回落默认 5。 */
22
- function recentLimit(config) {
23
- if (!config || config.tuiProviderListRecent === false) return 0
24
- const raw = Number(config.tuiProviderRecentLimit)
25
- if (!Number.isFinite(raw) || raw <= 0) return DEFAULT_RECENT_LIMIT
26
- return Math.min(MAX_RECENT_LIMIT, Math.max(1, Math.floor(raw)))
27
- }
28
-
29
- /** 本地镜像目录的 registry 根(`$DSH_HOME/remote-workspaces`)。
30
- *
31
- * 布局由 lib/index.js 的 mirrorRootFor/mirrorDirFor 固定为
32
- * `<root>/<host>-<user>-<port>/<basename>`,所以从任一镜像目录上溯两级即可得到
33
- * 根;`describe` 是同步方法,不能为了 root 去 await 网络。 */
34
- function rootFromMirrorDir(localMirror) {
35
- if (!localMirror) return ''
36
- return path.dirname(path.dirname(path.resolve(String(localMirror))))
37
- }
38
-
39
- /** 从 cwd 路径本身认 root(layout 固定,字符串判断是纯同步的兜底)。 */
40
- function rootFromCwd(cwd) {
41
- const resolved = path.resolve(String(cwd || ''))
42
- const marker = `${path.sep}remote-workspaces${path.sep}`
43
- const index = resolved.indexOf(marker)
44
- if (index < 0) return ''
45
- return resolved.slice(0, index + marker.length - 1)
46
- }
47
-
48
- /**
49
- * 创建远程工作区 provider。
50
- *
51
- * @param {object} deps
52
- * @param {object} deps.api - 宿主进程内 API(docs/tui.md §3,apiVersion 1)。
53
- * @param {object} [deps.config] - 行 config(tuiProviderListRecent / tuiProviderRecentLimit)。
54
- * @param {object} [deps.logger] - cordis logger(只用于 warn,失败一律降级)。
55
- * @param {string} [deps.root] - 镜像 registry 根;省略时从 listRemoteProjects 结果推导。
56
- * @param {(cwd: string) => void} [deps.onDescribe] - 宿主每次用会话 cwd 调
57
- * `describe()` 时的只读提示(installTui 用它跟踪“当前会话”,见 lib/tui/index.js)。
58
- * @returns {object} TuiWorkspaceProvider(附加 `refresh()`,供 installTui 预热/订阅刷新)。
59
- */
60
- /**
61
- * 把镜像 meta 里的 `{host, port, username, alias}` 对到保存的机器记录:
62
- * ssh-config 别名机按 alias 命中(meta 记的是解析后地址 + alias),字面机按
63
- * host/port/username 命中。installTui 的底栏与 provider 共用这一套规则。
64
- *
65
- * @param {Array<object>} machines - `api.listMachines()` 的 MachineView 列表。
66
- * @param {{host: string, port: number, username: string, alias?: string}} mirrorMachine
67
- * @returns {object|undefined} 命中的机器记录。
68
- */
69
- export function matchMachineForMirror(machines, mirrorMachine) {
70
- const list = Array.isArray(machines) ? machines : []
71
- if (!mirrorMachine) return undefined
72
- const alias = String(mirrorMachine.alias || '')
73
- if (alias) {
74
- const byAlias = list.find((m) => m && m.useSshConfig && String(m.host || '') === alias)
75
- if (byAlias) return byAlias
76
- }
77
- const host = String(mirrorMachine.host || '')
78
- const port = Number(mirrorMachine.port) || 22
79
- const user = String(mirrorMachine.username || '')
80
- return list.find((m) => {
81
- if (!m || m.useSshConfig) return false
82
- if (String(m.host || '') !== host) return false
83
- if ((Number(m.port) || 22) !== port) return false
84
- return !user || String(m.username || '') === user
85
- })
86
- }
87
-
88
- export function createWorkspaceProvider({ api, config, logger, root, onDescribe } = {}) {
89
- const state = {
90
- targets: [],
91
- machines: [],
92
- currentId: null,
93
- projects: [],
94
- root: typeof root === 'string' && root ? String(root) : '',
95
- ready: false,
96
- }
97
- let inflight = null
98
-
99
- /** 只记日志、绝不抛错:provider 的失败不能让宿主选择器崩。 */
100
- const warn = (message, error) => {
101
- try {
102
- if (!logger || typeof logger.warn !== 'function') return
103
- const detail = error && (error.message || error)
104
- logger.warn(`[dsh-remote] tui workspace provider: ${message}${detail ? ': ' + detail : ''}`)
105
- } catch { /* logging is best-effort */ }
106
- }
107
-
108
- /** 当前镜像根:显式 deps.root > 项目缓存 > cwd 字符串兜底。 */
109
- const mirrorRoot = (cwd) => {
110
- if (state.root) return state.root
111
- const project = state.projects.find((entry) => entry && typeof entry.dir === 'string' && entry.dir)
112
- if (project) {
113
- state.root = rootFromMirrorDir(project.dir)
114
- if (state.root) return state.root
115
- }
116
- if (cwd) {
117
- const derived = rootFromCwd(cwd)
118
- if (derived) return derived
119
- }
120
- return ''
121
- }
122
-
123
- /** 按 id / 名称 / host(含 ssh-config 别名)软匹配机器。 */
124
- const findMachine = (token) => {
125
- const needle = String(token === undefined || token === null ? '' : token).trim().toLowerCase()
126
- if (!needle) return undefined
127
- return state.machines.find((m) => String(m.id || '') === String(token))
128
- || state.machines.find((m) => String(m.name || '').toLowerCase() === needle)
129
- || state.machines.find((m) => String(m.host || '').toLowerCase() === needle)
130
- }
131
-
132
- /** RemoteProject(listRemoteProjects 的行)→ 保存的机器。 */
133
- const machineForProject = (project) => {
134
- if (!project) return undefined
135
- if (project.machineId) {
136
- const direct = state.machines.find((m) => String(m.id || '') === String(project.machineId))
137
- if (direct) return direct
138
- }
139
- const alias = String(project.alias || '')
140
- if (alias) {
141
- const byAlias = state.machines.find((m) => m.useSshConfig && String(m.host || '') === alias)
142
- if (byAlias) return byAlias
143
- }
144
- const host = String(project.host || '')
145
- const port = Number(project.port) || 22
146
- const user = String(project.username || '')
147
- return state.machines.find((m) => {
148
- if (m.useSshConfig) return false
149
- if (String(m.host || '') !== host) return false
150
- if ((Number(m.port) || 22) !== port) return false
151
- return !user || String(m.username || '') === user
152
- })
153
- }
154
-
155
- /** resolveMirror 的 machine 段(meta)→ 保存的机器。 */
156
- const machineForMirror = (mirrorMachine) => matchMachineForMirror(state.machines, mirrorMachine)
157
-
158
- /** 把一个 target 放进缓存(uri 去重,先到先得)。 */
159
- const remember = (map, target) => {
160
- if (!target || !target.uri || !target.cwd) return
161
- if (map.has(target.uri)) return
162
- map.set(target.uri, target)
163
- }
164
-
165
- /** 把 resolve 出来的新 target 并进缓存,供后续 list() 使用。 */
166
- const rememberInState = (target) => {
167
- if (!target || !target.uri || !target.cwd) return
168
- if (state.targets.some((entry) => entry.uri === target.uri)) return
169
- state.targets = [...state.targets, target]
170
- }
171
-
172
- /** 调宿主 ensureRemoteWorkspace 并转成 target;失败返回 undefined(不抛)。
173
- *
174
- * `register` 控制是否写工作区台账:刷新(picker 的 list)必须为 false ——
175
- * 仅仅打开选择器就把每台机器的最近工作区登记进用户的全局台账,是没有点过
176
- * 「打开」的用户能看到的副作用;真正选中(resolve/resolvePath)才登记。 */
177
- const ensureTarget = async (machine, remotePath, register = true) => {
178
- if (!machine || !remotePath) return undefined
179
- try {
180
- const workspace = await api.ensureRemoteWorkspace({
181
- machineId: machine.id,
182
- remotePath,
183
- register,
184
- })
185
- if (!workspace || !workspace.localMirror) return undefined
186
- return targetFromWorkspace({
187
- machine,
188
- localMirror: workspace.localMirror,
189
- remotePath: workspace.remotePath || remotePath,
190
- title: workspace.title,
191
- })
192
- } catch (error) {
193
- warn(`ensureRemoteWorkspace failed for ${machine.id || machine.host}:${remotePath}`, error)
194
- return undefined
195
- }
196
- }
197
-
198
- /** 后台刷新缓存:机器列表 + 已存在镜像 +(可选)每机最近 N 个工作区。 */
199
- const refresh = () => {
200
- if (inflight) return inflight
201
- inflight = (async () => {
202
- try {
203
- const [machineResult, projectResult] = await Promise.all([
204
- Promise.resolve().then(() => api.listMachines()),
205
- Promise.resolve().then(() => api.listRemoteProjects()),
206
- ])
207
- const machines = Array.isArray(machineResult && machineResult.machines)
208
- ? machineResult.machines.filter((m) => m && m.id)
209
- : []
210
- const projects = Array.isArray(projectResult) ? projectResult.filter((p) => p && p.dir) : []
211
- const currentId = machineResult && machineResult.currentId !== undefined ? machineResult.currentId : null
212
-
213
- // 先把本轮的数据装进 state,使下面的机器匹配看到最新列表。
214
- state.machines = machines
215
- state.currentId = currentId
216
- state.projects = projects
217
- if (!state.root && projects.length) {
218
- const derived = rootFromMirrorDir(projects[0].dir)
219
- if (derived) state.root = derived
220
- }
221
-
222
- const targets = new Map()
223
- // 1) 已存在的镜像(listRemoteProjects 只读本地文件系统,不连网)。
224
- for (const project of projects) {
225
- const machine = machineForProject(project)
226
- if (!machine) continue
227
- remember(targets, targetFromWorkspace({
228
- machine,
229
- localMirror: project.dir,
230
- remotePath: project.remotePath,
231
- }))
232
- }
233
- // 2) 当前机的已记录工作区(只备镜像,不登记台账——见 ensureTarget)。
234
- const current = machines.find((m) => String(m.id) === String(currentId))
235
- if (current && current.workspace) {
236
- const target = await ensureTarget(current, current.workspace, false)
237
- remember(targets, target)
238
- }
239
- // 3) 每台机器最近的 N 个工作区(可在 config 里关掉;同样不登记台账)。
240
- const limit = recentLimit(config)
241
- if (limit > 0) {
242
- for (const machine of machines) {
243
- const recent = Array.isArray(machine.recentWorkspaces) ? machine.recentWorkspaces : []
244
- for (const remotePath of recent.slice(0, limit)) {
245
- if (!remotePath) continue
246
- if (targets.has(buildUri(machine.id, String(remotePath)))) continue
247
- const target = await ensureTarget(machine, String(remotePath), false)
248
- remember(targets, target)
249
- }
250
- }
251
- }
252
-
253
- state.targets = [...targets.values()]
254
- state.ready = true
255
- } catch (error) {
256
- warn('refresh failed', error)
257
- } finally {
258
- inflight = null
259
- }
260
- })()
261
- return inflight
262
- }
263
-
264
- /** 缓存副本;缓存为空时也返回 `[]`(绝不 throw,也不在 list 里等网络)。 */
265
- const list = (signal) => {
266
- if (signal && signal.aborted) return []
267
- if (!state.ready && !inflight) void refresh()
268
- return state.targets.map((target) => ({ ...target }))
269
- }
270
-
271
- /** URI → target;scheme 不对 / 机器不存在 / ensure 失败都返回 undefined。 */
272
- const resolve = async (uri, signal) => {
273
- if (signal && signal.aborted) return undefined
274
- const parsed = parseUri(uri)
275
- if (!parsed) return undefined
276
- let machine = findMachine(parsed.machineId)
277
- if (!machine) {
278
- await refresh()
279
- machine = findMachine(parsed.machineId)
280
- }
281
- if (!machine) return undefined
282
- const target = await ensureTarget(machine, parsed.remotePath)
283
- if (target) rememberInState(target)
284
- return target
285
- }
286
-
287
- /** 只读同步的镜像解析:cwd 属于某个镜像 → target;否则 undefined。
288
- * 宿主在每次会话切换后都会带着新 cwd 调这里(见 dsh-tui live adoption),
289
- * 所以顺手把 cwd 作为“当前会话”提示交给 installTui。 */
290
- const describe = (cwd) => {
291
- try { if (typeof onDescribe === 'function') onDescribe(cwd) } catch { /* hint is best-effort */ }
292
- if (!cwd) return undefined
293
- const root = mirrorRoot(String(cwd))
294
- if (!root) return undefined
295
- let mirror
296
- try {
297
- mirror = resolveMirror(String(cwd), root)
298
- } catch (error) {
299
- warn('describe failed', error)
300
- return undefined
301
- }
302
- if (!mirror || !mirror.mirrorDir || !mirror.machine || !mirror.machine.host) return undefined
303
- const machine = machineForMirror(mirror.machine) || {
304
- // 机器已被删除,但镜像还在:仍按 meta 提供只读身份(展示用),
305
- // resolvePath 会因为没有保存的机器而拒绝。
306
- id: mirror.machine.alias || mirror.machine.host,
307
- name: '',
308
- host: mirror.machine.host,
309
- port: mirror.machine.port,
310
- username: mirror.machine.username,
311
- }
312
- return targetFromWorkspace({
313
- machine,
314
- localMirror: mirror.mirrorDir,
315
- remotePath: mirror.remotePath,
316
- })
317
- }
318
-
319
- /** cwd 已属于本 provider 的镜像时,把 path 解析成该镜像的远程路径。 */
320
- const resolvePath = async (targetPath, cwd, signal) => {
321
- if (signal && signal.aborted) return undefined
322
- const described = describe(cwd)
323
- if (!described) return undefined
324
- const root = mirrorRoot(String(cwd))
325
- const mirror = resolveMirror(String(cwd), root)
326
- if (!mirror || !mirror.machine) return undefined
327
- const machine = machineForMirror(mirror.machine)
328
- if (!machine) return undefined
329
- const remotePath = joinRemote(mirror.remotePath, targetPath)
330
- const target = await ensureTarget(machine, remotePath)
331
- if (target) rememberInState(target)
332
- return target
333
- }
334
-
335
- return {
336
- schemes: ['dshremote'],
337
- list,
338
- resolve,
339
- resolvePath,
340
- describe,
341
- /** 后台预热 / 订阅刷新入口(不是 TuiWorkspaceProvider 的方法,宿主忽略)。 */
342
- refresh,
343
- }
344
- }
1
+ // dsh-remote TUI — 工作区 provider(scheme: dshremote)。
2
+ //
3
+ // 契约见 dsh-tui `dsh-adapter/workspaces.d.ts` 的 TuiWorkspaceProvider:
4
+ // · `list(signal)` 只读**内存缓存**(宿主强制 2s 预算,绝不在这里连网)
5
+ // · `resolve(uri)` / `resolvePath(path, cwd)` 走宿主 api 的 ensureRemoteWorkspace
6
+ // · `describe(cwd)` 同步、只读本地镜像 meta(lib/binding.js#resolveMirror),
7
+ // 不创建任何目录
8
+ //
9
+ // cwd 必须是本地真实存在的镜像目录:会话远程身份由 session header.cwd 落在哪个
10
+ // 镜像目录决定。provider 的 `uri` 表达远程身份,`cwd` 永远指向镜像。
11
+
12
+ import path from 'node:path'
13
+ import { resolveMirror } from '../binding.js'
14
+ import { buildUri, joinRemote, parseUri, targetFromWorkspace } from './format.js'
15
+
16
+ /** 每台机器默认列出的最近工作区条数(与 docs/tui.md §5 一致)。 */
17
+ const DEFAULT_RECENT_LIMIT = 5
18
+ /** tuiProviderRecentLimit 的合法上限。 */
19
+ const MAX_RECENT_LIMIT = 20
20
+
21
+ /** 把 1..20 的限制值夹紧;非法值回落默认 5。 */
22
+ function recentLimit(config) {
23
+ if (!config || config.tuiProviderListRecent === false) return 0
24
+ const raw = Number(config.tuiProviderRecentLimit)
25
+ if (!Number.isFinite(raw) || raw <= 0) return DEFAULT_RECENT_LIMIT
26
+ return Math.min(MAX_RECENT_LIMIT, Math.max(1, Math.floor(raw)))
27
+ }
28
+
29
+ /** 本地镜像目录的 registry 根(`$DSH_HOME/remote-workspaces`)。
30
+ *
31
+ * 布局由 lib/index.js 的 mirrorRootFor/mirrorDirFor 固定为
32
+ * `<root>/<host>-<user>-<port>/<basename>`,所以从任一镜像目录上溯两级即可得到
33
+ * 根;`describe` 是同步方法,不能为了 root 去 await 网络。 */
34
+ function rootFromMirrorDir(localMirror) {
35
+ if (!localMirror) return ''
36
+ return path.dirname(path.dirname(path.resolve(String(localMirror))))
37
+ }
38
+
39
+ /** 从 cwd 路径本身认 root(layout 固定,字符串判断是纯同步的兜底)。 */
40
+ function rootFromCwd(cwd) {
41
+ const resolved = path.resolve(String(cwd || ''))
42
+ const marker = `${path.sep}remote-workspaces${path.sep}`
43
+ const index = resolved.indexOf(marker)
44
+ if (index < 0) return ''
45
+ return resolved.slice(0, index + marker.length - 1)
46
+ }
47
+
48
+ /**
49
+ * 创建远程工作区 provider。
50
+ *
51
+ * @param {object} deps
52
+ * @param {object} deps.api - 宿主进程内 API(docs/tui.md §3,apiVersion 1)。
53
+ * @param {object} [deps.config] - 行 config(tuiProviderListRecent / tuiProviderRecentLimit)。
54
+ * @param {object} [deps.logger] - cordis logger(只用于 warn,失败一律降级)。
55
+ * @param {string} [deps.root] - 镜像 registry 根;省略时从 listRemoteProjects 结果推导。
56
+ * @param {(cwd: string) => void} [deps.onDescribe] - 宿主每次用会话 cwd 调
57
+ * `describe()` 时的只读提示(installTui 用它跟踪“当前会话”,见 lib/tui/index.js)。
58
+ * @returns {object} TuiWorkspaceProvider(附加 `refresh()`,供 installTui 预热/订阅刷新)。
59
+ */
60
+ /**
61
+ * 把镜像 meta 里的 `{host, port, username, alias}` 对到保存的机器记录:
62
+ * ssh-config 别名机按 alias 命中(meta 记的是解析后地址 + alias),字面机按
63
+ * host/port/username 命中。installTui 的底栏与 provider 共用这一套规则。
64
+ *
65
+ * @param {Array<object>} machines - `api.listMachines()` 的 MachineView 列表。
66
+ * @param {{host: string, port: number, username: string, alias?: string}} mirrorMachine
67
+ * @returns {object|undefined} 命中的机器记录。
68
+ */
69
+ export function matchMachineForMirror(machines, mirrorMachine) {
70
+ const list = Array.isArray(machines) ? machines : []
71
+ if (!mirrorMachine) return undefined
72
+ const alias = String(mirrorMachine.alias || '')
73
+ if (alias) {
74
+ const byAlias = list.find((m) => m && m.useSshConfig && String(m.host || '') === alias)
75
+ if (byAlias) return byAlias
76
+ }
77
+ const host = String(mirrorMachine.host || '')
78
+ const port = Number(mirrorMachine.port) || 22
79
+ const user = String(mirrorMachine.username || '')
80
+ return list.find((m) => {
81
+ if (!m || m.useSshConfig) return false
82
+ if (String(m.host || '') !== host) return false
83
+ if ((Number(m.port) || 22) !== port) return false
84
+ return !user || String(m.username || '') === user
85
+ })
86
+ }
87
+
88
+ export function createWorkspaceProvider({ api, config, logger, root, onDescribe } = {}) {
89
+ const state = {
90
+ targets: [],
91
+ machines: [],
92
+ currentId: null,
93
+ projects: [],
94
+ root: typeof root === 'string' && root ? String(root) : '',
95
+ ready: false,
96
+ }
97
+ let inflight = null
98
+
99
+ /** 只记日志、绝不抛错:provider 的失败不能让宿主选择器崩。 */
100
+ const warn = (message, error) => {
101
+ try {
102
+ if (!logger || typeof logger.warn !== 'function') return
103
+ const detail = error && (error.message || error)
104
+ logger.warn(`[dsh-remote] tui workspace provider: ${message}${detail ? ': ' + detail : ''}`)
105
+ } catch { /* logging is best-effort */ }
106
+ }
107
+
108
+ /** 当前镜像根:显式 deps.root > 项目缓存 > cwd 字符串兜底。 */
109
+ const mirrorRoot = (cwd) => {
110
+ if (state.root) return state.root
111
+ const project = state.projects.find((entry) => entry && typeof entry.dir === 'string' && entry.dir)
112
+ if (project) {
113
+ state.root = rootFromMirrorDir(project.dir)
114
+ if (state.root) return state.root
115
+ }
116
+ if (cwd) {
117
+ const derived = rootFromCwd(cwd)
118
+ if (derived) return derived
119
+ }
120
+ return ''
121
+ }
122
+
123
+ /** 按 id / 名称 / host(含 ssh-config 别名)软匹配机器。 */
124
+ const findMachine = (token) => {
125
+ const needle = String(token === undefined || token === null ? '' : token).trim().toLowerCase()
126
+ if (!needle) return undefined
127
+ return state.machines.find((m) => String(m.id || '') === String(token))
128
+ || state.machines.find((m) => String(m.name || '').toLowerCase() === needle)
129
+ || state.machines.find((m) => String(m.host || '').toLowerCase() === needle)
130
+ }
131
+
132
+ /** RemoteProject(listRemoteProjects 的行)→ 保存的机器。 */
133
+ const machineForProject = (project) => {
134
+ if (!project) return undefined
135
+ if (project.machineId) {
136
+ const direct = state.machines.find((m) => String(m.id || '') === String(project.machineId))
137
+ if (direct) return direct
138
+ }
139
+ const alias = String(project.alias || '')
140
+ if (alias) {
141
+ const byAlias = state.machines.find((m) => m.useSshConfig && String(m.host || '') === alias)
142
+ if (byAlias) return byAlias
143
+ }
144
+ const host = String(project.host || '')
145
+ const port = Number(project.port) || 22
146
+ const user = String(project.username || '')
147
+ return state.machines.find((m) => {
148
+ if (m.useSshConfig) return false
149
+ if (String(m.host || '') !== host) return false
150
+ if ((Number(m.port) || 22) !== port) return false
151
+ return !user || String(m.username || '') === user
152
+ })
153
+ }
154
+
155
+ /** resolveMirror 的 machine 段(meta)→ 保存的机器。 */
156
+ const machineForMirror = (mirrorMachine) => matchMachineForMirror(state.machines, mirrorMachine)
157
+
158
+ /** 把一个 target 放进缓存(uri 去重,先到先得)。 */
159
+ const remember = (map, target) => {
160
+ if (!target || !target.uri || !target.cwd) return
161
+ if (map.has(target.uri)) return
162
+ map.set(target.uri, target)
163
+ }
164
+
165
+ /** 把 resolve 出来的新 target 并进缓存,供后续 list() 使用。 */
166
+ const rememberInState = (target) => {
167
+ if (!target || !target.uri || !target.cwd) return
168
+ if (state.targets.some((entry) => entry.uri === target.uri)) return
169
+ state.targets = [...state.targets, target]
170
+ }
171
+
172
+ /** 调宿主 ensureRemoteWorkspace 并转成 target;失败返回 undefined(不抛)。
173
+ *
174
+ * `register` 控制是否写工作区台账:刷新(picker 的 list)必须为 false ——
175
+ * 仅仅打开选择器就把每台机器的最近工作区登记进用户的全局台账,是没有点过
176
+ * 「打开」的用户能看到的副作用;真正选中(resolve/resolvePath)才登记。 */
177
+ const ensureTarget = async (machine, remotePath, register = true) => {
178
+ if (!machine || !remotePath) return undefined
179
+ try {
180
+ const workspace = await api.ensureRemoteWorkspace({
181
+ machineId: machine.id,
182
+ remotePath,
183
+ register,
184
+ })
185
+ if (!workspace || !workspace.localMirror) return undefined
186
+ return targetFromWorkspace({
187
+ machine,
188
+ localMirror: workspace.localMirror,
189
+ remotePath: workspace.remotePath || remotePath,
190
+ title: workspace.title,
191
+ })
192
+ } catch (error) {
193
+ warn(`ensureRemoteWorkspace failed for ${machine.id || machine.host}:${remotePath}`, error)
194
+ return undefined
195
+ }
196
+ }
197
+
198
+ /** 后台刷新缓存:机器列表 + 已存在镜像 +(可选)每机最近 N 个工作区。 */
199
+ const refresh = () => {
200
+ if (inflight) return inflight
201
+ inflight = (async () => {
202
+ try {
203
+ const [machineResult, projectResult] = await Promise.all([
204
+ Promise.resolve().then(() => api.listMachines()),
205
+ Promise.resolve().then(() => api.listRemoteProjects()),
206
+ ])
207
+ const machines = Array.isArray(machineResult && machineResult.machines)
208
+ ? machineResult.machines.filter((m) => m && m.id)
209
+ : []
210
+ const projects = Array.isArray(projectResult) ? projectResult.filter((p) => p && p.dir) : []
211
+ const currentId = machineResult && machineResult.currentId !== undefined ? machineResult.currentId : null
212
+
213
+ // 先把本轮的数据装进 state,使下面的机器匹配看到最新列表。
214
+ state.machines = machines
215
+ state.currentId = currentId
216
+ state.projects = projects
217
+ if (!state.root && projects.length) {
218
+ const derived = rootFromMirrorDir(projects[0].dir)
219
+ if (derived) state.root = derived
220
+ }
221
+
222
+ const targets = new Map()
223
+ // 1) 已存在的镜像(listRemoteProjects 只读本地文件系统,不连网)。
224
+ for (const project of projects) {
225
+ const machine = machineForProject(project)
226
+ if (!machine) continue
227
+ remember(targets, targetFromWorkspace({
228
+ machine,
229
+ localMirror: project.dir,
230
+ remotePath: project.remotePath,
231
+ }))
232
+ }
233
+ // 2) 当前机的已记录工作区(只备镜像,不登记台账——见 ensureTarget)。
234
+ const current = machines.find((m) => String(m.id) === String(currentId))
235
+ if (current && current.workspace) {
236
+ const target = await ensureTarget(current, current.workspace, false)
237
+ remember(targets, target)
238
+ }
239
+ // 3) 每台机器最近的 N 个工作区(可在 config 里关掉;同样不登记台账)。
240
+ const limit = recentLimit(config)
241
+ if (limit > 0) {
242
+ for (const machine of machines) {
243
+ const recent = Array.isArray(machine.recentWorkspaces) ? machine.recentWorkspaces : []
244
+ for (const remotePath of recent.slice(0, limit)) {
245
+ if (!remotePath) continue
246
+ if (targets.has(buildUri(machine.id, String(remotePath)))) continue
247
+ const target = await ensureTarget(machine, String(remotePath), false)
248
+ remember(targets, target)
249
+ }
250
+ }
251
+ }
252
+
253
+ state.targets = [...targets.values()]
254
+ state.ready = true
255
+ } catch (error) {
256
+ warn('refresh failed', error)
257
+ } finally {
258
+ inflight = null
259
+ }
260
+ })()
261
+ return inflight
262
+ }
263
+
264
+ /** 缓存副本;缓存为空时也返回 `[]`(绝不 throw,也不在 list 里等网络)。 */
265
+ const list = (signal) => {
266
+ if (signal && signal.aborted) return []
267
+ if (!state.ready && !inflight) void refresh()
268
+ return state.targets.map((target) => ({ ...target }))
269
+ }
270
+
271
+ /** URI → target;scheme 不对 / 机器不存在 / ensure 失败都返回 undefined。 */
272
+ const resolve = async (uri, signal) => {
273
+ if (signal && signal.aborted) return undefined
274
+ const parsed = parseUri(uri)
275
+ if (!parsed) return undefined
276
+ let machine = findMachine(parsed.machineId)
277
+ if (!machine) {
278
+ await refresh()
279
+ machine = findMachine(parsed.machineId)
280
+ }
281
+ if (!machine) return undefined
282
+ const target = await ensureTarget(machine, parsed.remotePath)
283
+ if (target) rememberInState(target)
284
+ return target
285
+ }
286
+
287
+ /** 只读同步的镜像解析:cwd 属于某个镜像 → target;否则 undefined。
288
+ * 宿主在每次会话切换后都会带着新 cwd 调这里(见 dsh-tui live adoption),
289
+ * 所以顺手把 cwd 作为“当前会话”提示交给 installTui。 */
290
+ const describe = (cwd) => {
291
+ try { if (typeof onDescribe === 'function') onDescribe(cwd) } catch { /* hint is best-effort */ }
292
+ if (!cwd) return undefined
293
+ const root = mirrorRoot(String(cwd))
294
+ if (!root) return undefined
295
+ let mirror
296
+ try {
297
+ mirror = resolveMirror(String(cwd), root)
298
+ } catch (error) {
299
+ warn('describe failed', error)
300
+ return undefined
301
+ }
302
+ if (!mirror || !mirror.mirrorDir || !mirror.machine || !mirror.machine.host) return undefined
303
+ const machine = machineForMirror(mirror.machine) || {
304
+ // 机器已被删除,但镜像还在:仍按 meta 提供只读身份(展示用),
305
+ // resolvePath 会因为没有保存的机器而拒绝。
306
+ id: mirror.machine.alias || mirror.machine.host,
307
+ name: '',
308
+ host: mirror.machine.host,
309
+ port: mirror.machine.port,
310
+ username: mirror.machine.username,
311
+ }
312
+ return targetFromWorkspace({
313
+ machine,
314
+ localMirror: mirror.mirrorDir,
315
+ remotePath: mirror.remotePath,
316
+ })
317
+ }
318
+
319
+ /** cwd 已属于本 provider 的镜像时,把 path 解析成该镜像的远程路径。 */
320
+ const resolvePath = async (targetPath, cwd, signal) => {
321
+ if (signal && signal.aborted) return undefined
322
+ const described = describe(cwd)
323
+ if (!described) return undefined
324
+ const root = mirrorRoot(String(cwd))
325
+ const mirror = resolveMirror(String(cwd), root)
326
+ if (!mirror || !mirror.machine) return undefined
327
+ const machine = machineForMirror(mirror.machine)
328
+ if (!machine) return undefined
329
+ const remotePath = joinRemote(mirror.remotePath, targetPath)
330
+ const target = await ensureTarget(machine, remotePath)
331
+ if (target) rememberInState(target)
332
+ return target
333
+ }
334
+
335
+ return {
336
+ schemes: ['dshremote'],
337
+ list,
338
+ resolve,
339
+ resolvePath,
340
+ describe,
341
+ /** 后台预热 / 订阅刷新入口(不是 TuiWorkspaceProvider 的方法,宿主忽略)。 */
342
+ refresh,
343
+ }
344
+ }